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_init_inode_security(struct btrfs_trans_handle
*trans
,
107 struct inode
*inode
, struct inode
*dir
,
108 const struct qstr
*qstr
)
112 err
= btrfs_init_acl(trans
, inode
, dir
);
114 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
119 * this does all the hard work for inserting an inline extent into
120 * the btree. The caller should have done a btrfs_drop_extents so that
121 * no overlapping inline items exist in the btree
123 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
124 struct btrfs_root
*root
, struct inode
*inode
,
125 u64 start
, size_t size
, size_t compressed_size
,
127 struct page
**compressed_pages
)
129 struct btrfs_key key
;
130 struct btrfs_path
*path
;
131 struct extent_buffer
*leaf
;
132 struct page
*page
= NULL
;
135 struct btrfs_file_extent_item
*ei
;
138 size_t cur_size
= size
;
140 unsigned long offset
;
142 if (compressed_size
&& compressed_pages
)
143 cur_size
= compressed_size
;
145 path
= btrfs_alloc_path();
149 path
->leave_spinning
= 1;
151 key
.objectid
= btrfs_ino(inode
);
153 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
154 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
156 inode_add_bytes(inode
, size
);
157 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
163 leaf
= path
->nodes
[0];
164 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
165 struct btrfs_file_extent_item
);
166 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
167 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
168 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
169 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
170 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
171 ptr
= btrfs_file_extent_inline_start(ei
);
173 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
176 while (compressed_size
> 0) {
177 cpage
= compressed_pages
[i
];
178 cur_size
= min_t(unsigned long, compressed_size
,
181 kaddr
= kmap_atomic(cpage
);
182 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
183 kunmap_atomic(kaddr
);
187 compressed_size
-= cur_size
;
189 btrfs_set_file_extent_compression(leaf
, ei
,
192 page
= find_get_page(inode
->i_mapping
,
193 start
>> PAGE_CACHE_SHIFT
);
194 btrfs_set_file_extent_compression(leaf
, ei
, 0);
195 kaddr
= kmap_atomic(page
);
196 offset
= start
& (PAGE_CACHE_SIZE
- 1);
197 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
198 kunmap_atomic(kaddr
);
199 page_cache_release(page
);
201 btrfs_mark_buffer_dirty(leaf
);
202 btrfs_free_path(path
);
205 * we're an inline extent, so nobody can
206 * extend the file past i_size without locking
207 * a page we already have locked.
209 * We must do any isize and inode updates
210 * before we unlock the pages. Otherwise we
211 * could end up racing with unlink.
213 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
214 ret
= btrfs_update_inode(trans
, root
, inode
);
218 btrfs_free_path(path
);
224 * conditionally insert an inline extent into the file. This
225 * does the checks required to make sure the data is small enough
226 * to fit as an inline extent.
228 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
229 struct btrfs_root
*root
,
230 struct inode
*inode
, u64 start
, u64 end
,
231 size_t compressed_size
, int compress_type
,
232 struct page
**compressed_pages
)
234 u64 isize
= i_size_read(inode
);
235 u64 actual_end
= min(end
+ 1, isize
);
236 u64 inline_len
= actual_end
- start
;
237 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
238 u64 data_len
= inline_len
;
242 data_len
= compressed_size
;
245 actual_end
>= PAGE_CACHE_SIZE
||
246 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
248 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
250 data_len
> root
->fs_info
->max_inline
) {
254 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
258 if (isize
> actual_end
)
259 inline_len
= min_t(u64
, isize
, actual_end
);
260 ret
= insert_inline_extent(trans
, root
, inode
, start
,
261 inline_len
, compressed_size
,
262 compress_type
, compressed_pages
);
263 if (ret
&& ret
!= -ENOSPC
) {
264 btrfs_abort_transaction(trans
, root
, ret
);
266 } else if (ret
== -ENOSPC
) {
270 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
271 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
272 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
276 struct async_extent
{
281 unsigned long nr_pages
;
283 struct list_head list
;
288 struct btrfs_root
*root
;
289 struct page
*locked_page
;
292 struct list_head extents
;
293 struct btrfs_work work
;
296 static noinline
int add_async_extent(struct async_cow
*cow
,
297 u64 start
, u64 ram_size
,
300 unsigned long nr_pages
,
303 struct async_extent
*async_extent
;
305 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
306 BUG_ON(!async_extent
); /* -ENOMEM */
307 async_extent
->start
= start
;
308 async_extent
->ram_size
= ram_size
;
309 async_extent
->compressed_size
= compressed_size
;
310 async_extent
->pages
= pages
;
311 async_extent
->nr_pages
= nr_pages
;
312 async_extent
->compress_type
= compress_type
;
313 list_add_tail(&async_extent
->list
, &cow
->extents
);
318 * we create compressed extents in two phases. The first
319 * phase compresses a range of pages that have already been
320 * locked (both pages and state bits are locked).
322 * This is done inside an ordered work queue, and the compression
323 * is spread across many cpus. The actual IO submission is step
324 * two, and the ordered work queue takes care of making sure that
325 * happens in the same order things were put onto the queue by
326 * writepages and friends.
328 * If this code finds it can't get good compression, it puts an
329 * entry onto the work queue to write the uncompressed bytes. This
330 * makes sure that both compressed inodes and uncompressed inodes
331 * are written in the same order that the flusher thread sent them
334 static noinline
int compress_file_range(struct inode
*inode
,
335 struct page
*locked_page
,
337 struct async_cow
*async_cow
,
340 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
341 struct btrfs_trans_handle
*trans
;
343 u64 blocksize
= root
->sectorsize
;
345 u64 isize
= i_size_read(inode
);
347 struct page
**pages
= NULL
;
348 unsigned long nr_pages
;
349 unsigned long nr_pages_ret
= 0;
350 unsigned long total_compressed
= 0;
351 unsigned long total_in
= 0;
352 unsigned long max_compressed
= 128 * 1024;
353 unsigned long max_uncompressed
= 128 * 1024;
356 int compress_type
= root
->fs_info
->compress_type
;
359 /* if this is a small write inside eof, kick off a defrag */
360 if ((end
- start
+ 1) < 16 * 1024 &&
361 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
362 btrfs_add_inode_defrag(NULL
, inode
);
364 actual_end
= min_t(u64
, isize
, end
+ 1);
367 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
368 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
371 * we don't want to send crud past the end of i_size through
372 * compression, that's just a waste of CPU time. So, if the
373 * end of the file is before the start of our current
374 * requested range of bytes, we bail out to the uncompressed
375 * cleanup code that can deal with all of this.
377 * It isn't really the fastest way to fix things, but this is a
378 * very uncommon corner.
380 if (actual_end
<= start
)
381 goto cleanup_and_bail_uncompressed
;
383 total_compressed
= actual_end
- start
;
385 /* we want to make sure that amount of ram required to uncompress
386 * an extent is reasonable, so we limit the total size in ram
387 * of a compressed extent to 128k. This is a crucial number
388 * because it also controls how easily we can spread reads across
389 * cpus for decompression.
391 * We also want to make sure the amount of IO required to do
392 * a random read is reasonably small, so we limit the size of
393 * a compressed extent to 128k.
395 total_compressed
= min(total_compressed
, max_uncompressed
);
396 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
397 num_bytes
= max(blocksize
, num_bytes
);
402 * we do compression for mount -o compress and when the
403 * inode has not been flagged as nocompress. This flag can
404 * change at any time if we discover bad compression ratios.
406 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
407 (btrfs_test_opt(root
, COMPRESS
) ||
408 (BTRFS_I(inode
)->force_compress
) ||
409 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
411 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
413 /* just bail out to the uncompressed code */
417 if (BTRFS_I(inode
)->force_compress
)
418 compress_type
= BTRFS_I(inode
)->force_compress
;
421 * we need to call clear_page_dirty_for_io on each
422 * page in the range. Otherwise applications with the file
423 * mmap'd can wander in and change the page contents while
424 * we are compressing them.
426 * If the compression fails for any reason, we set the pages
427 * dirty again later on.
429 extent_range_clear_dirty_for_io(inode
, start
, end
);
431 ret
= btrfs_compress_pages(compress_type
,
432 inode
->i_mapping
, start
,
433 total_compressed
, pages
,
434 nr_pages
, &nr_pages_ret
,
440 unsigned long offset
= total_compressed
&
441 (PAGE_CACHE_SIZE
- 1);
442 struct page
*page
= pages
[nr_pages_ret
- 1];
445 /* zero the tail end of the last page, we might be
446 * sending it down to disk
449 kaddr
= kmap_atomic(page
);
450 memset(kaddr
+ offset
, 0,
451 PAGE_CACHE_SIZE
- offset
);
452 kunmap_atomic(kaddr
);
459 trans
= btrfs_join_transaction(root
);
461 ret
= PTR_ERR(trans
);
463 goto cleanup_and_out
;
465 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
467 /* lets try to make an inline extent */
468 if (ret
|| total_in
< (actual_end
- start
)) {
469 /* we didn't compress the entire range, try
470 * to make an uncompressed inline extent.
472 ret
= cow_file_range_inline(trans
, root
, inode
,
473 start
, end
, 0, 0, NULL
);
475 /* try making a compressed inline extent */
476 ret
= cow_file_range_inline(trans
, root
, inode
,
479 compress_type
, pages
);
483 * inline extent creation worked or returned error,
484 * we don't need to create any more async work items.
485 * Unlock and free up our temp pages.
487 extent_clear_unlock_delalloc(inode
,
488 &BTRFS_I(inode
)->io_tree
,
490 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
491 EXTENT_CLEAR_DELALLOC
|
492 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
494 btrfs_end_transaction(trans
, root
);
497 btrfs_end_transaction(trans
, root
);
502 * we aren't doing an inline extent round the compressed size
503 * up to a block size boundary so the allocator does sane
506 total_compressed
= ALIGN(total_compressed
, blocksize
);
509 * one last check to make sure the compression is really a
510 * win, compare the page count read with the blocks on disk
512 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
513 if (total_compressed
>= total_in
) {
516 num_bytes
= total_in
;
519 if (!will_compress
&& pages
) {
521 * the compression code ran but failed to make things smaller,
522 * free any pages it allocated and our page pointer array
524 for (i
= 0; i
< nr_pages_ret
; i
++) {
525 WARN_ON(pages
[i
]->mapping
);
526 page_cache_release(pages
[i
]);
530 total_compressed
= 0;
533 /* flag the file so we don't compress in the future */
534 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
535 !(BTRFS_I(inode
)->force_compress
)) {
536 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
542 /* the async work queues will take care of doing actual
543 * allocation on disk for these compressed pages,
544 * and will submit them to the elevator.
546 add_async_extent(async_cow
, start
, num_bytes
,
547 total_compressed
, pages
, nr_pages_ret
,
550 if (start
+ num_bytes
< end
) {
557 cleanup_and_bail_uncompressed
:
559 * No compression, but we still need to write the pages in
560 * the file we've been given so far. redirty the locked
561 * page if it corresponds to our extent and set things up
562 * for the async work queue to run cow_file_range to do
563 * the normal delalloc dance
565 if (page_offset(locked_page
) >= start
&&
566 page_offset(locked_page
) <= end
) {
567 __set_page_dirty_nobuffers(locked_page
);
568 /* unlocked later on in the async handlers */
571 extent_range_redirty_for_io(inode
, start
, end
);
572 add_async_extent(async_cow
, start
, end
- start
+ 1,
573 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
581 for (i
= 0; i
< nr_pages_ret
; i
++) {
582 WARN_ON(pages
[i
]->mapping
);
583 page_cache_release(pages
[i
]);
590 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
592 EXTENT_CLEAR_UNLOCK_PAGE
|
594 EXTENT_CLEAR_DELALLOC
|
595 EXTENT_SET_WRITEBACK
|
596 EXTENT_END_WRITEBACK
);
597 if (!trans
|| IS_ERR(trans
))
598 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
600 btrfs_abort_transaction(trans
, root
, ret
);
605 * phase two of compressed writeback. This is the ordered portion
606 * of the code, which only gets called in the order the work was
607 * queued. We walk all the async extents created by compress_file_range
608 * and send them down to the disk.
610 static noinline
int submit_compressed_extents(struct inode
*inode
,
611 struct async_cow
*async_cow
)
613 struct async_extent
*async_extent
;
615 struct btrfs_trans_handle
*trans
;
616 struct btrfs_key ins
;
617 struct extent_map
*em
;
618 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
619 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
620 struct extent_io_tree
*io_tree
;
623 if (list_empty(&async_cow
->extents
))
627 while (!list_empty(&async_cow
->extents
)) {
628 async_extent
= list_entry(async_cow
->extents
.next
,
629 struct async_extent
, list
);
630 list_del(&async_extent
->list
);
632 io_tree
= &BTRFS_I(inode
)->io_tree
;
635 /* did the compression code fall back to uncompressed IO? */
636 if (!async_extent
->pages
) {
637 int page_started
= 0;
638 unsigned long nr_written
= 0;
640 lock_extent(io_tree
, async_extent
->start
,
641 async_extent
->start
+
642 async_extent
->ram_size
- 1);
644 /* allocate blocks */
645 ret
= cow_file_range(inode
, async_cow
->locked_page
,
647 async_extent
->start
+
648 async_extent
->ram_size
- 1,
649 &page_started
, &nr_written
, 0);
654 * if page_started, cow_file_range inserted an
655 * inline extent and took care of all the unlocking
656 * and IO for us. Otherwise, we need to submit
657 * all those pages down to the drive.
659 if (!page_started
&& !ret
)
660 extent_write_locked_range(io_tree
,
661 inode
, async_extent
->start
,
662 async_extent
->start
+
663 async_extent
->ram_size
- 1,
667 unlock_page(async_cow
->locked_page
);
673 lock_extent(io_tree
, async_extent
->start
,
674 async_extent
->start
+ async_extent
->ram_size
- 1);
676 trans
= btrfs_join_transaction(root
);
678 ret
= PTR_ERR(trans
);
680 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
681 ret
= btrfs_reserve_extent(trans
, root
,
682 async_extent
->compressed_size
,
683 async_extent
->compressed_size
,
684 0, alloc_hint
, &ins
, 1);
685 if (ret
&& ret
!= -ENOSPC
)
686 btrfs_abort_transaction(trans
, root
, ret
);
687 btrfs_end_transaction(trans
, root
);
693 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
694 WARN_ON(async_extent
->pages
[i
]->mapping
);
695 page_cache_release(async_extent
->pages
[i
]);
697 kfree(async_extent
->pages
);
698 async_extent
->nr_pages
= 0;
699 async_extent
->pages
= NULL
;
707 * here we're doing allocation and writeback of the
710 btrfs_drop_extent_cache(inode
, async_extent
->start
,
711 async_extent
->start
+
712 async_extent
->ram_size
- 1, 0);
714 em
= alloc_extent_map();
716 goto out_free_reserve
;
717 em
->start
= async_extent
->start
;
718 em
->len
= async_extent
->ram_size
;
719 em
->orig_start
= em
->start
;
720 em
->mod_start
= em
->start
;
721 em
->mod_len
= em
->len
;
723 em
->block_start
= ins
.objectid
;
724 em
->block_len
= ins
.offset
;
725 em
->orig_block_len
= ins
.offset
;
726 em
->ram_bytes
= async_extent
->ram_size
;
727 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
728 em
->compress_type
= async_extent
->compress_type
;
729 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
730 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
734 write_lock(&em_tree
->lock
);
735 ret
= add_extent_mapping(em_tree
, em
);
738 &em_tree
->modified_extents
);
739 write_unlock(&em_tree
->lock
);
740 if (ret
!= -EEXIST
) {
744 btrfs_drop_extent_cache(inode
, async_extent
->start
,
745 async_extent
->start
+
746 async_extent
->ram_size
- 1, 0);
750 goto out_free_reserve
;
752 ret
= btrfs_add_ordered_extent_compress(inode
,
755 async_extent
->ram_size
,
757 BTRFS_ORDERED_COMPRESSED
,
758 async_extent
->compress_type
);
760 goto out_free_reserve
;
763 * clear dirty, set writeback and unlock the pages.
765 extent_clear_unlock_delalloc(inode
,
766 &BTRFS_I(inode
)->io_tree
,
768 async_extent
->start
+
769 async_extent
->ram_size
- 1,
770 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
771 EXTENT_CLEAR_UNLOCK
|
772 EXTENT_CLEAR_DELALLOC
|
773 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
775 ret
= btrfs_submit_compressed_write(inode
,
777 async_extent
->ram_size
,
779 ins
.offset
, async_extent
->pages
,
780 async_extent
->nr_pages
);
781 alloc_hint
= ins
.objectid
+ ins
.offset
;
791 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
793 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
795 async_extent
->start
+
796 async_extent
->ram_size
- 1,
797 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
798 EXTENT_CLEAR_UNLOCK
|
799 EXTENT_CLEAR_DELALLOC
|
801 EXTENT_SET_WRITEBACK
|
802 EXTENT_END_WRITEBACK
);
807 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
810 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
811 struct extent_map
*em
;
814 read_lock(&em_tree
->lock
);
815 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
818 * if block start isn't an actual block number then find the
819 * first block in this inode and use that as a hint. If that
820 * block is also bogus then just don't worry about it.
822 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
824 em
= search_extent_mapping(em_tree
, 0, 0);
825 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
826 alloc_hint
= em
->block_start
;
830 alloc_hint
= em
->block_start
;
834 read_unlock(&em_tree
->lock
);
840 * when extent_io.c finds a delayed allocation range in the file,
841 * the call backs end up in this code. The basic idea is to
842 * allocate extents on disk for the range, and create ordered data structs
843 * in ram to track those extents.
845 * locked_page is the page that writepage had locked already. We use
846 * it to make sure we don't do extra locks or unlocks.
848 * *page_started is set to one if we unlock locked_page and do everything
849 * required to start IO on it. It may be clean and already done with
852 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
854 struct btrfs_root
*root
,
855 struct page
*locked_page
,
856 u64 start
, u64 end
, int *page_started
,
857 unsigned long *nr_written
,
862 unsigned long ram_size
;
865 u64 blocksize
= root
->sectorsize
;
866 struct btrfs_key ins
;
867 struct extent_map
*em
;
868 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
871 BUG_ON(btrfs_is_free_space_inode(inode
));
873 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
874 num_bytes
= max(blocksize
, num_bytes
);
875 disk_num_bytes
= num_bytes
;
877 /* if this is a small write inside eof, kick off defrag */
878 if (num_bytes
< 64 * 1024 &&
879 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
880 btrfs_add_inode_defrag(trans
, inode
);
883 /* lets try to make an inline extent */
884 ret
= cow_file_range_inline(trans
, root
, inode
,
885 start
, end
, 0, 0, NULL
);
887 extent_clear_unlock_delalloc(inode
,
888 &BTRFS_I(inode
)->io_tree
,
890 EXTENT_CLEAR_UNLOCK_PAGE
|
891 EXTENT_CLEAR_UNLOCK
|
892 EXTENT_CLEAR_DELALLOC
|
894 EXTENT_SET_WRITEBACK
|
895 EXTENT_END_WRITEBACK
);
897 *nr_written
= *nr_written
+
898 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
901 } else if (ret
< 0) {
902 btrfs_abort_transaction(trans
, root
, ret
);
907 BUG_ON(disk_num_bytes
>
908 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
910 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
911 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
913 while (disk_num_bytes
> 0) {
916 cur_alloc_size
= disk_num_bytes
;
917 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
918 root
->sectorsize
, 0, alloc_hint
,
921 btrfs_abort_transaction(trans
, root
, ret
);
925 em
= alloc_extent_map();
926 BUG_ON(!em
); /* -ENOMEM */
928 em
->orig_start
= em
->start
;
929 ram_size
= ins
.offset
;
930 em
->len
= ins
.offset
;
931 em
->mod_start
= em
->start
;
932 em
->mod_len
= em
->len
;
934 em
->block_start
= ins
.objectid
;
935 em
->block_len
= ins
.offset
;
936 em
->orig_block_len
= ins
.offset
;
937 em
->ram_bytes
= ram_size
;
938 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
939 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
943 write_lock(&em_tree
->lock
);
944 ret
= add_extent_mapping(em_tree
, em
);
947 &em_tree
->modified_extents
);
948 write_unlock(&em_tree
->lock
);
949 if (ret
!= -EEXIST
) {
953 btrfs_drop_extent_cache(inode
, start
,
954 start
+ ram_size
- 1, 0);
957 cur_alloc_size
= ins
.offset
;
958 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
959 ram_size
, cur_alloc_size
, 0);
960 BUG_ON(ret
); /* -ENOMEM */
962 if (root
->root_key
.objectid
==
963 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
964 ret
= btrfs_reloc_clone_csums(inode
, start
,
967 btrfs_abort_transaction(trans
, root
, ret
);
972 if (disk_num_bytes
< cur_alloc_size
)
975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
982 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
983 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
986 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
987 start
, start
+ ram_size
- 1,
989 disk_num_bytes
-= cur_alloc_size
;
990 num_bytes
-= cur_alloc_size
;
991 alloc_hint
= ins
.objectid
+ ins
.offset
;
992 start
+= cur_alloc_size
;
998 extent_clear_unlock_delalloc(inode
,
999 &BTRFS_I(inode
)->io_tree
,
1000 start
, end
, locked_page
,
1001 EXTENT_CLEAR_UNLOCK_PAGE
|
1002 EXTENT_CLEAR_UNLOCK
|
1003 EXTENT_CLEAR_DELALLOC
|
1004 EXTENT_CLEAR_DIRTY
|
1005 EXTENT_SET_WRITEBACK
|
1006 EXTENT_END_WRITEBACK
);
1011 static noinline
int cow_file_range(struct inode
*inode
,
1012 struct page
*locked_page
,
1013 u64 start
, u64 end
, int *page_started
,
1014 unsigned long *nr_written
,
1017 struct btrfs_trans_handle
*trans
;
1018 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1021 trans
= btrfs_join_transaction(root
);
1022 if (IS_ERR(trans
)) {
1023 extent_clear_unlock_delalloc(inode
,
1024 &BTRFS_I(inode
)->io_tree
,
1025 start
, end
, locked_page
,
1026 EXTENT_CLEAR_UNLOCK_PAGE
|
1027 EXTENT_CLEAR_UNLOCK
|
1028 EXTENT_CLEAR_DELALLOC
|
1029 EXTENT_CLEAR_DIRTY
|
1030 EXTENT_SET_WRITEBACK
|
1031 EXTENT_END_WRITEBACK
);
1032 return PTR_ERR(trans
);
1034 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1036 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1037 page_started
, nr_written
, unlock
);
1039 btrfs_end_transaction(trans
, root
);
1045 * work queue call back to started compression on a file and pages
1047 static noinline
void async_cow_start(struct btrfs_work
*work
)
1049 struct async_cow
*async_cow
;
1051 async_cow
= container_of(work
, struct async_cow
, work
);
1053 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1054 async_cow
->start
, async_cow
->end
, async_cow
,
1056 if (num_added
== 0) {
1057 btrfs_add_delayed_iput(async_cow
->inode
);
1058 async_cow
->inode
= NULL
;
1063 * work queue call back to submit previously compressed pages
1065 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1067 struct async_cow
*async_cow
;
1068 struct btrfs_root
*root
;
1069 unsigned long nr_pages
;
1071 async_cow
= container_of(work
, struct async_cow
, work
);
1073 root
= async_cow
->root
;
1074 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1077 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1079 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1080 wake_up(&root
->fs_info
->async_submit_wait
);
1082 if (async_cow
->inode
)
1083 submit_compressed_extents(async_cow
->inode
, async_cow
);
1086 static noinline
void async_cow_free(struct btrfs_work
*work
)
1088 struct async_cow
*async_cow
;
1089 async_cow
= container_of(work
, struct async_cow
, work
);
1090 if (async_cow
->inode
)
1091 btrfs_add_delayed_iput(async_cow
->inode
);
1095 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1096 u64 start
, u64 end
, int *page_started
,
1097 unsigned long *nr_written
)
1099 struct async_cow
*async_cow
;
1100 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1101 unsigned long nr_pages
;
1103 int limit
= 10 * 1024 * 1024;
1105 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1106 1, 0, NULL
, GFP_NOFS
);
1107 while (start
< end
) {
1108 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1109 BUG_ON(!async_cow
); /* -ENOMEM */
1110 async_cow
->inode
= igrab(inode
);
1111 async_cow
->root
= root
;
1112 async_cow
->locked_page
= locked_page
;
1113 async_cow
->start
= start
;
1115 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1118 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1120 async_cow
->end
= cur_end
;
1121 INIT_LIST_HEAD(&async_cow
->extents
);
1123 async_cow
->work
.func
= async_cow_start
;
1124 async_cow
->work
.ordered_func
= async_cow_submit
;
1125 async_cow
->work
.ordered_free
= async_cow_free
;
1126 async_cow
->work
.flags
= 0;
1128 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1130 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1132 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1135 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1136 wait_event(root
->fs_info
->async_submit_wait
,
1137 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1141 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1142 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1143 wait_event(root
->fs_info
->async_submit_wait
,
1144 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1148 *nr_written
+= nr_pages
;
1149 start
= cur_end
+ 1;
1155 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1156 u64 bytenr
, u64 num_bytes
)
1159 struct btrfs_ordered_sum
*sums
;
1162 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1163 bytenr
+ num_bytes
- 1, &list
, 0);
1164 if (ret
== 0 && list_empty(&list
))
1167 while (!list_empty(&list
)) {
1168 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1169 list_del(&sums
->list
);
1176 * when nowcow writeback call back. This checks for snapshots or COW copies
1177 * of the extents that exist in the file, and COWs the file as required.
1179 * If no cow copies or snapshots exist, we write directly to the existing
1182 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1183 struct page
*locked_page
,
1184 u64 start
, u64 end
, int *page_started
, int force
,
1185 unsigned long *nr_written
)
1187 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1188 struct btrfs_trans_handle
*trans
;
1189 struct extent_buffer
*leaf
;
1190 struct btrfs_path
*path
;
1191 struct btrfs_file_extent_item
*fi
;
1192 struct btrfs_key found_key
;
1207 u64 ino
= btrfs_ino(inode
);
1209 path
= btrfs_alloc_path();
1211 extent_clear_unlock_delalloc(inode
,
1212 &BTRFS_I(inode
)->io_tree
,
1213 start
, end
, locked_page
,
1214 EXTENT_CLEAR_UNLOCK_PAGE
|
1215 EXTENT_CLEAR_UNLOCK
|
1216 EXTENT_CLEAR_DELALLOC
|
1217 EXTENT_CLEAR_DIRTY
|
1218 EXTENT_SET_WRITEBACK
|
1219 EXTENT_END_WRITEBACK
);
1223 nolock
= btrfs_is_free_space_inode(inode
);
1226 trans
= btrfs_join_transaction_nolock(root
);
1228 trans
= btrfs_join_transaction(root
);
1230 if (IS_ERR(trans
)) {
1231 extent_clear_unlock_delalloc(inode
,
1232 &BTRFS_I(inode
)->io_tree
,
1233 start
, end
, locked_page
,
1234 EXTENT_CLEAR_UNLOCK_PAGE
|
1235 EXTENT_CLEAR_UNLOCK
|
1236 EXTENT_CLEAR_DELALLOC
|
1237 EXTENT_CLEAR_DIRTY
|
1238 EXTENT_SET_WRITEBACK
|
1239 EXTENT_END_WRITEBACK
);
1240 btrfs_free_path(path
);
1241 return PTR_ERR(trans
);
1244 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1246 cow_start
= (u64
)-1;
1249 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1252 btrfs_abort_transaction(trans
, root
, ret
);
1255 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1256 leaf
= path
->nodes
[0];
1257 btrfs_item_key_to_cpu(leaf
, &found_key
,
1258 path
->slots
[0] - 1);
1259 if (found_key
.objectid
== ino
&&
1260 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1265 leaf
= path
->nodes
[0];
1266 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1267 ret
= btrfs_next_leaf(root
, path
);
1269 btrfs_abort_transaction(trans
, root
, ret
);
1274 leaf
= path
->nodes
[0];
1280 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1282 if (found_key
.objectid
> ino
||
1283 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1284 found_key
.offset
> end
)
1287 if (found_key
.offset
> cur_offset
) {
1288 extent_end
= found_key
.offset
;
1293 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1294 struct btrfs_file_extent_item
);
1295 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1297 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1298 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1299 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1300 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1301 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1302 extent_end
= found_key
.offset
+
1303 btrfs_file_extent_num_bytes(leaf
, fi
);
1305 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1306 if (extent_end
<= start
) {
1310 if (disk_bytenr
== 0)
1312 if (btrfs_file_extent_compression(leaf
, fi
) ||
1313 btrfs_file_extent_encryption(leaf
, fi
) ||
1314 btrfs_file_extent_other_encoding(leaf
, fi
))
1316 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1318 if (btrfs_extent_readonly(root
, disk_bytenr
))
1320 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1322 extent_offset
, disk_bytenr
))
1324 disk_bytenr
+= extent_offset
;
1325 disk_bytenr
+= cur_offset
- found_key
.offset
;
1326 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1328 * force cow if csum exists in the range.
1329 * this ensure that csum for a given extent are
1330 * either valid or do not exist.
1332 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1335 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1336 extent_end
= found_key
.offset
+
1337 btrfs_file_extent_inline_len(leaf
, fi
);
1338 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1343 if (extent_end
<= start
) {
1348 if (cow_start
== (u64
)-1)
1349 cow_start
= cur_offset
;
1350 cur_offset
= extent_end
;
1351 if (cur_offset
> end
)
1357 btrfs_release_path(path
);
1358 if (cow_start
!= (u64
)-1) {
1359 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1360 cow_start
, found_key
.offset
- 1,
1361 page_started
, nr_written
, 1);
1363 btrfs_abort_transaction(trans
, root
, ret
);
1366 cow_start
= (u64
)-1;
1369 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1370 struct extent_map
*em
;
1371 struct extent_map_tree
*em_tree
;
1372 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1373 em
= alloc_extent_map();
1374 BUG_ON(!em
); /* -ENOMEM */
1375 em
->start
= cur_offset
;
1376 em
->orig_start
= found_key
.offset
- extent_offset
;
1377 em
->len
= num_bytes
;
1378 em
->block_len
= num_bytes
;
1379 em
->block_start
= disk_bytenr
;
1380 em
->orig_block_len
= disk_num_bytes
;
1381 em
->ram_bytes
= ram_bytes
;
1382 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1383 em
->mod_start
= em
->start
;
1384 em
->mod_len
= em
->len
;
1385 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1386 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1387 em
->generation
= -1;
1389 write_lock(&em_tree
->lock
);
1390 ret
= add_extent_mapping(em_tree
, em
);
1392 list_move(&em
->list
,
1393 &em_tree
->modified_extents
);
1394 write_unlock(&em_tree
->lock
);
1395 if (ret
!= -EEXIST
) {
1396 free_extent_map(em
);
1399 btrfs_drop_extent_cache(inode
, em
->start
,
1400 em
->start
+ em
->len
- 1, 0);
1402 type
= BTRFS_ORDERED_PREALLOC
;
1404 type
= BTRFS_ORDERED_NOCOW
;
1407 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1408 num_bytes
, num_bytes
, type
);
1409 BUG_ON(ret
); /* -ENOMEM */
1411 if (root
->root_key
.objectid
==
1412 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1413 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1416 btrfs_abort_transaction(trans
, root
, ret
);
1421 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1422 cur_offset
, cur_offset
+ num_bytes
- 1,
1423 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1424 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1425 EXTENT_SET_PRIVATE2
);
1426 cur_offset
= extent_end
;
1427 if (cur_offset
> end
)
1430 btrfs_release_path(path
);
1432 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1433 cow_start
= cur_offset
;
1437 if (cow_start
!= (u64
)-1) {
1438 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1440 page_started
, nr_written
, 1);
1442 btrfs_abort_transaction(trans
, root
, ret
);
1448 err
= btrfs_end_transaction(trans
, root
);
1452 if (ret
&& cur_offset
< end
)
1453 extent_clear_unlock_delalloc(inode
,
1454 &BTRFS_I(inode
)->io_tree
,
1455 cur_offset
, end
, locked_page
,
1456 EXTENT_CLEAR_UNLOCK_PAGE
|
1457 EXTENT_CLEAR_UNLOCK
|
1458 EXTENT_CLEAR_DELALLOC
|
1459 EXTENT_CLEAR_DIRTY
|
1460 EXTENT_SET_WRITEBACK
|
1461 EXTENT_END_WRITEBACK
);
1463 btrfs_free_path(path
);
1468 * extent_io.c call back to do delayed allocation processing
1470 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1471 u64 start
, u64 end
, int *page_started
,
1472 unsigned long *nr_written
)
1475 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1477 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1478 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1479 page_started
, 1, nr_written
);
1480 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1481 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1482 page_started
, 0, nr_written
);
1483 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1484 !(BTRFS_I(inode
)->force_compress
) &&
1485 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1486 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1487 page_started
, nr_written
, 1);
1489 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1490 &BTRFS_I(inode
)->runtime_flags
);
1491 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1492 page_started
, nr_written
);
1497 static void btrfs_split_extent_hook(struct inode
*inode
,
1498 struct extent_state
*orig
, u64 split
)
1500 /* not delalloc, ignore it */
1501 if (!(orig
->state
& EXTENT_DELALLOC
))
1504 spin_lock(&BTRFS_I(inode
)->lock
);
1505 BTRFS_I(inode
)->outstanding_extents
++;
1506 spin_unlock(&BTRFS_I(inode
)->lock
);
1510 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1511 * extents so we can keep track of new extents that are just merged onto old
1512 * extents, such as when we are doing sequential writes, so we can properly
1513 * account for the metadata space we'll need.
1515 static void btrfs_merge_extent_hook(struct inode
*inode
,
1516 struct extent_state
*new,
1517 struct extent_state
*other
)
1519 /* not delalloc, ignore it */
1520 if (!(other
->state
& EXTENT_DELALLOC
))
1523 spin_lock(&BTRFS_I(inode
)->lock
);
1524 BTRFS_I(inode
)->outstanding_extents
--;
1525 spin_unlock(&BTRFS_I(inode
)->lock
);
1529 * extent_io.c set_bit_hook, used to track delayed allocation
1530 * bytes in this file, and to maintain the list of inodes that
1531 * have pending delalloc work to be done.
1533 static void btrfs_set_bit_hook(struct inode
*inode
,
1534 struct extent_state
*state
, int *bits
)
1538 * set_bit and clear bit hooks normally require _irqsave/restore
1539 * but in this case, we are only testing for the DELALLOC
1540 * bit, which is only set or cleared with irqs on
1542 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1543 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1544 u64 len
= state
->end
+ 1 - state
->start
;
1545 bool do_list
= !btrfs_is_free_space_inode(inode
);
1547 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1548 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1550 spin_lock(&BTRFS_I(inode
)->lock
);
1551 BTRFS_I(inode
)->outstanding_extents
++;
1552 spin_unlock(&BTRFS_I(inode
)->lock
);
1555 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1556 root
->fs_info
->delalloc_batch
);
1557 spin_lock(&BTRFS_I(inode
)->lock
);
1558 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1559 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1560 &BTRFS_I(inode
)->runtime_flags
)) {
1561 spin_lock(&root
->fs_info
->delalloc_lock
);
1562 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1563 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1564 &root
->fs_info
->delalloc_inodes
);
1565 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1566 &BTRFS_I(inode
)->runtime_flags
);
1568 spin_unlock(&root
->fs_info
->delalloc_lock
);
1570 spin_unlock(&BTRFS_I(inode
)->lock
);
1575 * extent_io.c clear_bit_hook, see set_bit_hook for why
1577 static void btrfs_clear_bit_hook(struct inode
*inode
,
1578 struct extent_state
*state
, int *bits
)
1581 * set_bit and clear bit hooks normally require _irqsave/restore
1582 * but in this case, we are only testing for the DELALLOC
1583 * bit, which is only set or cleared with irqs on
1585 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1586 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1587 u64 len
= state
->end
+ 1 - state
->start
;
1588 bool do_list
= !btrfs_is_free_space_inode(inode
);
1590 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1591 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1592 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1593 spin_lock(&BTRFS_I(inode
)->lock
);
1594 BTRFS_I(inode
)->outstanding_extents
--;
1595 spin_unlock(&BTRFS_I(inode
)->lock
);
1598 if (*bits
& EXTENT_DO_ACCOUNTING
)
1599 btrfs_delalloc_release_metadata(inode
, len
);
1601 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1603 btrfs_free_reserved_data_space(inode
, len
);
1605 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1606 root
->fs_info
->delalloc_batch
);
1607 spin_lock(&BTRFS_I(inode
)->lock
);
1608 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1609 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1610 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1611 &BTRFS_I(inode
)->runtime_flags
)) {
1612 spin_lock(&root
->fs_info
->delalloc_lock
);
1613 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1614 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1615 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1616 &BTRFS_I(inode
)->runtime_flags
);
1618 spin_unlock(&root
->fs_info
->delalloc_lock
);
1620 spin_unlock(&BTRFS_I(inode
)->lock
);
1625 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1626 * we don't create bios that span stripes or chunks
1628 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1629 size_t size
, struct bio
*bio
,
1630 unsigned long bio_flags
)
1632 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1633 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1638 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1641 length
= bio
->bi_size
;
1642 map_length
= length
;
1643 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1644 &map_length
, NULL
, 0);
1645 /* Will always return 0 with map_multi == NULL */
1647 if (map_length
< length
+ size
)
1653 * in order to insert checksums into the metadata in large chunks,
1654 * we wait until bio submission time. All the pages in the bio are
1655 * checksummed and sums are attached onto the ordered extent record.
1657 * At IO completion time the cums attached on the ordered extent record
1658 * are inserted into the btree
1660 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1661 struct bio
*bio
, int mirror_num
,
1662 unsigned long bio_flags
,
1665 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1668 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1669 BUG_ON(ret
); /* -ENOMEM */
1674 * in order to insert checksums into the metadata in large chunks,
1675 * we wait until bio submission time. All the pages in the bio are
1676 * checksummed and sums are attached onto the ordered extent record.
1678 * At IO completion time the cums attached on the ordered extent record
1679 * are inserted into the btree
1681 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1682 int mirror_num
, unsigned long bio_flags
,
1685 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1688 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1690 bio_endio(bio
, ret
);
1695 * extent_io.c submission hook. This does the right thing for csum calculation
1696 * on write, or reading the csums from the tree before a read
1698 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1699 int mirror_num
, unsigned long bio_flags
,
1702 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1706 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1708 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1710 if (btrfs_is_free_space_inode(inode
))
1713 if (!(rw
& REQ_WRITE
)) {
1714 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1718 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1719 ret
= btrfs_submit_compressed_read(inode
, bio
,
1723 } else if (!skip_sum
) {
1724 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1729 } else if (async
&& !skip_sum
) {
1730 /* csum items have already been cloned */
1731 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1733 /* we're doing a write, do the async checksumming */
1734 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1735 inode
, rw
, bio
, mirror_num
,
1736 bio_flags
, bio_offset
,
1737 __btrfs_submit_bio_start
,
1738 __btrfs_submit_bio_done
);
1740 } else if (!skip_sum
) {
1741 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1747 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1751 bio_endio(bio
, ret
);
1756 * given a list of ordered sums record them in the inode. This happens
1757 * at IO completion time based on sums calculated at bio submission time.
1759 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1760 struct inode
*inode
, u64 file_offset
,
1761 struct list_head
*list
)
1763 struct btrfs_ordered_sum
*sum
;
1765 list_for_each_entry(sum
, list
, list
) {
1766 trans
->adding_csums
= 1;
1767 btrfs_csum_file_blocks(trans
,
1768 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1769 trans
->adding_csums
= 0;
1774 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1775 struct extent_state
**cached_state
)
1777 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1778 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1779 cached_state
, GFP_NOFS
);
1782 /* see btrfs_writepage_start_hook for details on why this is required */
1783 struct btrfs_writepage_fixup
{
1785 struct btrfs_work work
;
1788 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1790 struct btrfs_writepage_fixup
*fixup
;
1791 struct btrfs_ordered_extent
*ordered
;
1792 struct extent_state
*cached_state
= NULL
;
1794 struct inode
*inode
;
1799 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1803 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1804 ClearPageChecked(page
);
1808 inode
= page
->mapping
->host
;
1809 page_start
= page_offset(page
);
1810 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1812 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1815 /* already ordered? We're done */
1816 if (PagePrivate2(page
))
1819 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1821 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1822 page_end
, &cached_state
, GFP_NOFS
);
1824 btrfs_start_ordered_extent(inode
, ordered
, 1);
1825 btrfs_put_ordered_extent(ordered
);
1829 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1831 mapping_set_error(page
->mapping
, ret
);
1832 end_extent_writepage(page
, ret
, page_start
, page_end
);
1833 ClearPageChecked(page
);
1837 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1838 ClearPageChecked(page
);
1839 set_page_dirty(page
);
1841 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1842 &cached_state
, GFP_NOFS
);
1845 page_cache_release(page
);
1850 * There are a few paths in the higher layers of the kernel that directly
1851 * set the page dirty bit without asking the filesystem if it is a
1852 * good idea. This causes problems because we want to make sure COW
1853 * properly happens and the data=ordered rules are followed.
1855 * In our case any range that doesn't have the ORDERED bit set
1856 * hasn't been properly setup for IO. We kick off an async process
1857 * to fix it up. The async helper will wait for ordered extents, set
1858 * the delalloc bit and make it safe to write the page.
1860 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1862 struct inode
*inode
= page
->mapping
->host
;
1863 struct btrfs_writepage_fixup
*fixup
;
1864 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1866 /* this page is properly in the ordered list */
1867 if (TestClearPagePrivate2(page
))
1870 if (PageChecked(page
))
1873 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1877 SetPageChecked(page
);
1878 page_cache_get(page
);
1879 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1881 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1885 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1886 struct inode
*inode
, u64 file_pos
,
1887 u64 disk_bytenr
, u64 disk_num_bytes
,
1888 u64 num_bytes
, u64 ram_bytes
,
1889 u8 compression
, u8 encryption
,
1890 u16 other_encoding
, int extent_type
)
1892 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1893 struct btrfs_file_extent_item
*fi
;
1894 struct btrfs_path
*path
;
1895 struct extent_buffer
*leaf
;
1896 struct btrfs_key ins
;
1899 path
= btrfs_alloc_path();
1903 path
->leave_spinning
= 1;
1906 * we may be replacing one extent in the tree with another.
1907 * The new extent is pinned in the extent map, and we don't want
1908 * to drop it from the cache until it is completely in the btree.
1910 * So, tell btrfs_drop_extents to leave this extent in the cache.
1911 * the caller is expected to unpin it and allow it to be merged
1914 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1915 file_pos
+ num_bytes
, 0);
1919 ins
.objectid
= btrfs_ino(inode
);
1920 ins
.offset
= file_pos
;
1921 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1922 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1925 leaf
= path
->nodes
[0];
1926 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1927 struct btrfs_file_extent_item
);
1928 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1929 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1930 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1931 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1932 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1933 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1934 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1935 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1936 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1937 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1939 btrfs_mark_buffer_dirty(leaf
);
1940 btrfs_release_path(path
);
1942 inode_add_bytes(inode
, num_bytes
);
1944 ins
.objectid
= disk_bytenr
;
1945 ins
.offset
= disk_num_bytes
;
1946 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1947 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1948 root
->root_key
.objectid
,
1949 btrfs_ino(inode
), file_pos
, &ins
);
1951 btrfs_free_path(path
);
1956 /* snapshot-aware defrag */
1957 struct sa_defrag_extent_backref
{
1958 struct rb_node node
;
1959 struct old_sa_defrag_extent
*old
;
1968 struct old_sa_defrag_extent
{
1969 struct list_head list
;
1970 struct new_sa_defrag_extent
*new;
1979 struct new_sa_defrag_extent
{
1980 struct rb_root root
;
1981 struct list_head head
;
1982 struct btrfs_path
*path
;
1983 struct inode
*inode
;
1991 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1992 struct sa_defrag_extent_backref
*b2
)
1994 if (b1
->root_id
< b2
->root_id
)
1996 else if (b1
->root_id
> b2
->root_id
)
1999 if (b1
->inum
< b2
->inum
)
2001 else if (b1
->inum
> b2
->inum
)
2004 if (b1
->file_pos
< b2
->file_pos
)
2006 else if (b1
->file_pos
> b2
->file_pos
)
2010 * [------------------------------] ===> (a range of space)
2011 * |<--->| |<---->| =============> (fs/file tree A)
2012 * |<---------------------------->| ===> (fs/file tree B)
2014 * A range of space can refer to two file extents in one tree while
2015 * refer to only one file extent in another tree.
2017 * So we may process a disk offset more than one time(two extents in A)
2018 * and locate at the same extent(one extent in B), then insert two same
2019 * backrefs(both refer to the extent in B).
2024 static void backref_insert(struct rb_root
*root
,
2025 struct sa_defrag_extent_backref
*backref
)
2027 struct rb_node
**p
= &root
->rb_node
;
2028 struct rb_node
*parent
= NULL
;
2029 struct sa_defrag_extent_backref
*entry
;
2034 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2036 ret
= backref_comp(backref
, entry
);
2040 p
= &(*p
)->rb_right
;
2043 rb_link_node(&backref
->node
, parent
, p
);
2044 rb_insert_color(&backref
->node
, root
);
2048 * Note the backref might has changed, and in this case we just return 0.
2050 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2053 struct btrfs_file_extent_item
*extent
;
2054 struct btrfs_fs_info
*fs_info
;
2055 struct old_sa_defrag_extent
*old
= ctx
;
2056 struct new_sa_defrag_extent
*new = old
->new;
2057 struct btrfs_path
*path
= new->path
;
2058 struct btrfs_key key
;
2059 struct btrfs_root
*root
;
2060 struct sa_defrag_extent_backref
*backref
;
2061 struct extent_buffer
*leaf
;
2062 struct inode
*inode
= new->inode
;
2068 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2069 inum
== btrfs_ino(inode
))
2072 key
.objectid
= root_id
;
2073 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2074 key
.offset
= (u64
)-1;
2076 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2077 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2079 if (PTR_ERR(root
) == -ENOENT
)
2082 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2083 inum
, offset
, root_id
);
2084 return PTR_ERR(root
);
2087 key
.objectid
= inum
;
2088 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2089 if (offset
> (u64
)-1 << 32)
2092 key
.offset
= offset
;
2094 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2103 leaf
= path
->nodes
[0];
2104 slot
= path
->slots
[0];
2106 if (slot
>= btrfs_header_nritems(leaf
)) {
2107 ret
= btrfs_next_leaf(root
, path
);
2110 } else if (ret
> 0) {
2119 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2121 if (key
.objectid
> inum
)
2124 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2127 extent
= btrfs_item_ptr(leaf
, slot
,
2128 struct btrfs_file_extent_item
);
2130 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2133 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2134 if (key
.offset
- extent_offset
!= offset
)
2137 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2138 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2139 old
->len
|| extent_offset
+ num_bytes
<=
2140 old
->extent_offset
+ old
->offset
)
2146 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2152 backref
->root_id
= root_id
;
2153 backref
->inum
= inum
;
2154 backref
->file_pos
= offset
+ extent_offset
;
2155 backref
->num_bytes
= num_bytes
;
2156 backref
->extent_offset
= extent_offset
;
2157 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2159 backref_insert(&new->root
, backref
);
2162 btrfs_release_path(path
);
2167 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2168 struct new_sa_defrag_extent
*new)
2170 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2171 struct old_sa_defrag_extent
*old
, *tmp
;
2176 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2177 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2178 path
, record_one_backref
,
2180 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2182 /* no backref to be processed for this extent */
2184 list_del(&old
->list
);
2189 if (list_empty(&new->head
))
2195 static int relink_is_mergable(struct extent_buffer
*leaf
,
2196 struct btrfs_file_extent_item
*fi
,
2199 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2202 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2205 if (btrfs_file_extent_compression(leaf
, fi
) ||
2206 btrfs_file_extent_encryption(leaf
, fi
) ||
2207 btrfs_file_extent_other_encoding(leaf
, fi
))
2214 * Note the backref might has changed, and in this case we just return 0.
2216 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2217 struct sa_defrag_extent_backref
*prev
,
2218 struct sa_defrag_extent_backref
*backref
)
2220 struct btrfs_file_extent_item
*extent
;
2221 struct btrfs_file_extent_item
*item
;
2222 struct btrfs_ordered_extent
*ordered
;
2223 struct btrfs_trans_handle
*trans
;
2224 struct btrfs_fs_info
*fs_info
;
2225 struct btrfs_root
*root
;
2226 struct btrfs_key key
;
2227 struct extent_buffer
*leaf
;
2228 struct old_sa_defrag_extent
*old
= backref
->old
;
2229 struct new_sa_defrag_extent
*new = old
->new;
2230 struct inode
*src_inode
= new->inode
;
2231 struct inode
*inode
;
2232 struct extent_state
*cached
= NULL
;
2241 if (prev
&& prev
->root_id
== backref
->root_id
&&
2242 prev
->inum
== backref
->inum
&&
2243 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2246 /* step 1: get root */
2247 key
.objectid
= backref
->root_id
;
2248 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2249 key
.offset
= (u64
)-1;
2251 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2252 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2254 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2256 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2257 if (PTR_ERR(root
) == -ENOENT
)
2259 return PTR_ERR(root
);
2261 if (btrfs_root_refs(&root
->root_item
) == 0) {
2262 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2263 /* parse ENOENT to 0 */
2267 /* step 2: get inode */
2268 key
.objectid
= backref
->inum
;
2269 key
.type
= BTRFS_INODE_ITEM_KEY
;
2272 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2273 if (IS_ERR(inode
)) {
2274 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2278 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2280 /* step 3: relink backref */
2281 lock_start
= backref
->file_pos
;
2282 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2283 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2286 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2288 btrfs_put_ordered_extent(ordered
);
2292 trans
= btrfs_join_transaction(root
);
2293 if (IS_ERR(trans
)) {
2294 ret
= PTR_ERR(trans
);
2298 key
.objectid
= backref
->inum
;
2299 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2300 key
.offset
= backref
->file_pos
;
2302 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2305 } else if (ret
> 0) {
2310 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2311 struct btrfs_file_extent_item
);
2313 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2314 backref
->generation
)
2317 btrfs_release_path(path
);
2319 start
= backref
->file_pos
;
2320 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2321 start
+= old
->extent_offset
+ old
->offset
-
2322 backref
->extent_offset
;
2324 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2325 old
->extent_offset
+ old
->offset
+ old
->len
);
2326 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2328 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2333 key
.objectid
= btrfs_ino(inode
);
2334 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2337 path
->leave_spinning
= 1;
2339 struct btrfs_file_extent_item
*fi
;
2341 struct btrfs_key found_key
;
2343 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2348 leaf
= path
->nodes
[0];
2349 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2351 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2352 struct btrfs_file_extent_item
);
2353 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2355 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2356 extent_len
+ found_key
.offset
== start
) {
2357 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2359 btrfs_mark_buffer_dirty(leaf
);
2360 inode_add_bytes(inode
, len
);
2366 btrfs_release_path(path
);
2371 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2374 btrfs_abort_transaction(trans
, root
, ret
);
2378 leaf
= path
->nodes
[0];
2379 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2380 struct btrfs_file_extent_item
);
2381 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2382 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2383 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2384 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2385 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2386 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2387 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2388 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2389 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2390 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2392 btrfs_mark_buffer_dirty(leaf
);
2393 inode_add_bytes(inode
, len
);
2394 btrfs_release_path(path
);
2396 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2398 backref
->root_id
, backref
->inum
,
2399 new->file_pos
, 0); /* start - extent_offset */
2401 btrfs_abort_transaction(trans
, root
, ret
);
2407 btrfs_release_path(path
);
2408 path
->leave_spinning
= 0;
2409 btrfs_end_transaction(trans
, root
);
2411 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2417 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2419 struct btrfs_path
*path
;
2420 struct old_sa_defrag_extent
*old
, *tmp
;
2421 struct sa_defrag_extent_backref
*backref
;
2422 struct sa_defrag_extent_backref
*prev
= NULL
;
2423 struct inode
*inode
;
2424 struct btrfs_root
*root
;
2425 struct rb_node
*node
;
2429 root
= BTRFS_I(inode
)->root
;
2431 path
= btrfs_alloc_path();
2435 if (!record_extent_backrefs(path
, new)) {
2436 btrfs_free_path(path
);
2439 btrfs_release_path(path
);
2442 node
= rb_first(&new->root
);
2445 rb_erase(node
, &new->root
);
2447 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2449 ret
= relink_extent_backref(path
, prev
, backref
);
2462 btrfs_free_path(path
);
2464 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2465 list_del(&old
->list
);
2469 atomic_dec(&root
->fs_info
->defrag_running
);
2470 wake_up(&root
->fs_info
->transaction_wait
);
2475 static struct new_sa_defrag_extent
*
2476 record_old_file_extents(struct inode
*inode
,
2477 struct btrfs_ordered_extent
*ordered
)
2479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2480 struct btrfs_path
*path
;
2481 struct btrfs_key key
;
2482 struct old_sa_defrag_extent
*old
, *tmp
;
2483 struct new_sa_defrag_extent
*new;
2486 new = kmalloc(sizeof(*new), GFP_NOFS
);
2491 new->file_pos
= ordered
->file_offset
;
2492 new->len
= ordered
->len
;
2493 new->bytenr
= ordered
->start
;
2494 new->disk_len
= ordered
->disk_len
;
2495 new->compress_type
= ordered
->compress_type
;
2496 new->root
= RB_ROOT
;
2497 INIT_LIST_HEAD(&new->head
);
2499 path
= btrfs_alloc_path();
2503 key
.objectid
= btrfs_ino(inode
);
2504 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2505 key
.offset
= new->file_pos
;
2507 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2510 if (ret
> 0 && path
->slots
[0] > 0)
2513 /* find out all the old extents for the file range */
2515 struct btrfs_file_extent_item
*extent
;
2516 struct extent_buffer
*l
;
2525 slot
= path
->slots
[0];
2527 if (slot
>= btrfs_header_nritems(l
)) {
2528 ret
= btrfs_next_leaf(root
, path
);
2536 btrfs_item_key_to_cpu(l
, &key
, slot
);
2538 if (key
.objectid
!= btrfs_ino(inode
))
2540 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2542 if (key
.offset
>= new->file_pos
+ new->len
)
2545 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2547 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2548 if (key
.offset
+ num_bytes
< new->file_pos
)
2551 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2555 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2557 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2561 offset
= max(new->file_pos
, key
.offset
);
2562 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2564 old
->bytenr
= disk_bytenr
;
2565 old
->extent_offset
= extent_offset
;
2566 old
->offset
= offset
- key
.offset
;
2567 old
->len
= end
- offset
;
2570 list_add_tail(&old
->list
, &new->head
);
2576 btrfs_free_path(path
);
2577 atomic_inc(&root
->fs_info
->defrag_running
);
2582 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2583 list_del(&old
->list
);
2587 btrfs_free_path(path
);
2594 * helper function for btrfs_finish_ordered_io, this
2595 * just reads in some of the csum leaves to prime them into ram
2596 * before we start the transaction. It limits the amount of btree
2597 * reads required while inside the transaction.
2599 /* as ordered data IO finishes, this gets called so we can finish
2600 * an ordered extent if the range of bytes in the file it covers are
2603 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2605 struct inode
*inode
= ordered_extent
->inode
;
2606 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2607 struct btrfs_trans_handle
*trans
= NULL
;
2608 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2609 struct extent_state
*cached_state
= NULL
;
2610 struct new_sa_defrag_extent
*new = NULL
;
2611 int compress_type
= 0;
2615 nolock
= btrfs_is_free_space_inode(inode
);
2617 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2622 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2623 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2624 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2626 trans
= btrfs_join_transaction_nolock(root
);
2628 trans
= btrfs_join_transaction(root
);
2629 if (IS_ERR(trans
)) {
2630 ret
= PTR_ERR(trans
);
2634 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2635 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2636 if (ret
) /* -ENOMEM or corruption */
2637 btrfs_abort_transaction(trans
, root
, ret
);
2641 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2642 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2645 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2646 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2647 EXTENT_DEFRAG
, 1, cached_state
);
2649 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2650 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2651 /* the inode is shared */
2652 new = record_old_file_extents(inode
, ordered_extent
);
2654 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2655 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2656 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2660 trans
= btrfs_join_transaction_nolock(root
);
2662 trans
= btrfs_join_transaction(root
);
2663 if (IS_ERR(trans
)) {
2664 ret
= PTR_ERR(trans
);
2668 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2670 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2671 compress_type
= ordered_extent
->compress_type
;
2672 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2673 BUG_ON(compress_type
);
2674 ret
= btrfs_mark_extent_written(trans
, inode
,
2675 ordered_extent
->file_offset
,
2676 ordered_extent
->file_offset
+
2677 ordered_extent
->len
);
2679 BUG_ON(root
== root
->fs_info
->tree_root
);
2680 ret
= insert_reserved_file_extent(trans
, inode
,
2681 ordered_extent
->file_offset
,
2682 ordered_extent
->start
,
2683 ordered_extent
->disk_len
,
2684 ordered_extent
->len
,
2685 ordered_extent
->len
,
2686 compress_type
, 0, 0,
2687 BTRFS_FILE_EXTENT_REG
);
2689 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2690 ordered_extent
->file_offset
, ordered_extent
->len
,
2693 btrfs_abort_transaction(trans
, root
, ret
);
2697 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2698 &ordered_extent
->list
);
2700 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2701 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2702 if (ret
) { /* -ENOMEM or corruption */
2703 btrfs_abort_transaction(trans
, root
, ret
);
2708 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2709 ordered_extent
->file_offset
+
2710 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2712 if (root
!= root
->fs_info
->tree_root
)
2713 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2715 btrfs_end_transaction(trans
, root
);
2718 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2719 ordered_extent
->file_offset
+
2720 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2723 * If the ordered extent had an IOERR or something else went
2724 * wrong we need to return the space for this ordered extent
2725 * back to the allocator.
2727 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2728 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2729 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2730 ordered_extent
->disk_len
);
2735 * This needs to be done to make sure anybody waiting knows we are done
2736 * updating everything for this ordered extent.
2738 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2740 /* for snapshot-aware defrag */
2742 relink_file_extents(new);
2745 btrfs_put_ordered_extent(ordered_extent
);
2746 /* once for the tree */
2747 btrfs_put_ordered_extent(ordered_extent
);
2752 static void finish_ordered_fn(struct btrfs_work
*work
)
2754 struct btrfs_ordered_extent
*ordered_extent
;
2755 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2756 btrfs_finish_ordered_io(ordered_extent
);
2759 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2760 struct extent_state
*state
, int uptodate
)
2762 struct inode
*inode
= page
->mapping
->host
;
2763 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2764 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2765 struct btrfs_workers
*workers
;
2767 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2769 ClearPagePrivate2(page
);
2770 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2771 end
- start
+ 1, uptodate
))
2774 ordered_extent
->work
.func
= finish_ordered_fn
;
2775 ordered_extent
->work
.flags
= 0;
2777 if (btrfs_is_free_space_inode(inode
))
2778 workers
= &root
->fs_info
->endio_freespace_worker
;
2780 workers
= &root
->fs_info
->endio_write_workers
;
2781 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2787 * when reads are done, we need to check csums to verify the data is correct
2788 * if there's a match, we allow the bio to finish. If not, the code in
2789 * extent_io.c will try to find good copies for us.
2791 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2792 struct extent_state
*state
, int mirror
)
2794 size_t offset
= start
- page_offset(page
);
2795 struct inode
*inode
= page
->mapping
->host
;
2796 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2798 u64
private = ~(u32
)0;
2800 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2802 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2803 DEFAULT_RATELIMIT_BURST
);
2805 if (PageChecked(page
)) {
2806 ClearPageChecked(page
);
2810 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2813 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2814 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2815 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2820 if (state
&& state
->start
== start
) {
2821 private = state
->private;
2824 ret
= get_state_private(io_tree
, start
, &private);
2826 kaddr
= kmap_atomic(page
);
2830 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2831 btrfs_csum_final(csum
, (char *)&csum
);
2832 if (csum
!= private)
2835 kunmap_atomic(kaddr
);
2840 if (__ratelimit(&_rs
))
2841 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2842 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2843 (unsigned long long)start
, csum
,
2844 (unsigned long long)private);
2845 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2846 flush_dcache_page(page
);
2847 kunmap_atomic(kaddr
);
2853 struct delayed_iput
{
2854 struct list_head list
;
2855 struct inode
*inode
;
2858 /* JDM: If this is fs-wide, why can't we add a pointer to
2859 * btrfs_inode instead and avoid the allocation? */
2860 void btrfs_add_delayed_iput(struct inode
*inode
)
2862 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2863 struct delayed_iput
*delayed
;
2865 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2868 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2869 delayed
->inode
= inode
;
2871 spin_lock(&fs_info
->delayed_iput_lock
);
2872 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2873 spin_unlock(&fs_info
->delayed_iput_lock
);
2876 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2879 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2880 struct delayed_iput
*delayed
;
2883 spin_lock(&fs_info
->delayed_iput_lock
);
2884 empty
= list_empty(&fs_info
->delayed_iputs
);
2885 spin_unlock(&fs_info
->delayed_iput_lock
);
2889 spin_lock(&fs_info
->delayed_iput_lock
);
2890 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2891 spin_unlock(&fs_info
->delayed_iput_lock
);
2893 while (!list_empty(&list
)) {
2894 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2895 list_del(&delayed
->list
);
2896 iput(delayed
->inode
);
2902 * This is called in transaction commit time. If there are no orphan
2903 * files in the subvolume, it removes orphan item and frees block_rsv
2906 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2907 struct btrfs_root
*root
)
2909 struct btrfs_block_rsv
*block_rsv
;
2912 if (atomic_read(&root
->orphan_inodes
) ||
2913 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2916 spin_lock(&root
->orphan_lock
);
2917 if (atomic_read(&root
->orphan_inodes
)) {
2918 spin_unlock(&root
->orphan_lock
);
2922 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2923 spin_unlock(&root
->orphan_lock
);
2927 block_rsv
= root
->orphan_block_rsv
;
2928 root
->orphan_block_rsv
= NULL
;
2929 spin_unlock(&root
->orphan_lock
);
2931 if (root
->orphan_item_inserted
&&
2932 btrfs_root_refs(&root
->root_item
) > 0) {
2933 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2934 root
->root_key
.objectid
);
2936 root
->orphan_item_inserted
= 0;
2940 WARN_ON(block_rsv
->size
> 0);
2941 btrfs_free_block_rsv(root
, block_rsv
);
2946 * This creates an orphan entry for the given inode in case something goes
2947 * wrong in the middle of an unlink/truncate.
2949 * NOTE: caller of this function should reserve 5 units of metadata for
2952 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2954 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2955 struct btrfs_block_rsv
*block_rsv
= NULL
;
2960 if (!root
->orphan_block_rsv
) {
2961 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2966 spin_lock(&root
->orphan_lock
);
2967 if (!root
->orphan_block_rsv
) {
2968 root
->orphan_block_rsv
= block_rsv
;
2969 } else if (block_rsv
) {
2970 btrfs_free_block_rsv(root
, block_rsv
);
2974 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2975 &BTRFS_I(inode
)->runtime_flags
)) {
2978 * For proper ENOSPC handling, we should do orphan
2979 * cleanup when mounting. But this introduces backward
2980 * compatibility issue.
2982 if (!xchg(&root
->orphan_item_inserted
, 1))
2988 atomic_inc(&root
->orphan_inodes
);
2991 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2992 &BTRFS_I(inode
)->runtime_flags
))
2994 spin_unlock(&root
->orphan_lock
);
2996 /* grab metadata reservation from transaction handle */
2998 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2999 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3002 /* insert an orphan item to track this unlinked/truncated file */
3004 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3005 if (ret
&& ret
!= -EEXIST
) {
3006 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3007 &BTRFS_I(inode
)->runtime_flags
);
3008 btrfs_abort_transaction(trans
, root
, ret
);
3014 /* insert an orphan item to track subvolume contains orphan files */
3016 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3017 root
->root_key
.objectid
);
3018 if (ret
&& ret
!= -EEXIST
) {
3019 btrfs_abort_transaction(trans
, root
, ret
);
3027 * We have done the truncate/delete so we can go ahead and remove the orphan
3028 * item for this particular inode.
3030 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, 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_free_path(path
);
3633 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3634 inode_inc_iversion(inode
);
3635 inode_inc_iversion(dir
);
3636 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3637 ret
= btrfs_update_inode(trans
, root
, dir
);
3642 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3643 struct btrfs_root
*root
,
3644 struct inode
*dir
, struct inode
*inode
,
3645 const char *name
, int name_len
)
3648 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3650 btrfs_drop_nlink(inode
);
3651 ret
= btrfs_update_inode(trans
, root
, inode
);
3657 /* helper to check if there is any shared block in the path */
3658 static int check_path_shared(struct btrfs_root
*root
,
3659 struct btrfs_path
*path
)
3661 struct extent_buffer
*eb
;
3665 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
3668 if (!path
->nodes
[level
])
3670 eb
= path
->nodes
[level
];
3671 if (!btrfs_block_can_be_shared(root
, eb
))
3673 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, level
, 1,
3682 * helper to start transaction for unlink and rmdir.
3684 * unlink and rmdir are special in btrfs, they do not always free space.
3685 * so in enospc case, we should make sure they will free space before
3686 * allowing them to use the global metadata reservation.
3688 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3689 struct dentry
*dentry
)
3691 struct btrfs_trans_handle
*trans
;
3692 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3693 struct btrfs_path
*path
;
3694 struct btrfs_dir_item
*di
;
3695 struct inode
*inode
= dentry
->d_inode
;
3700 u64 ino
= btrfs_ino(inode
);
3701 u64 dir_ino
= btrfs_ino(dir
);
3704 * 1 for the possible orphan item
3705 * 1 for the dir item
3706 * 1 for the dir index
3707 * 1 for the inode ref
3710 trans
= btrfs_start_transaction(root
, 5);
3711 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3714 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3715 return ERR_PTR(-ENOSPC
);
3717 /* check if there is someone else holds reference */
3718 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3719 return ERR_PTR(-ENOSPC
);
3721 if (atomic_read(&inode
->i_count
) > 2)
3722 return ERR_PTR(-ENOSPC
);
3724 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3725 return ERR_PTR(-ENOSPC
);
3727 path
= btrfs_alloc_path();
3729 root
->fs_info
->enospc_unlink
= 0;
3730 return ERR_PTR(-ENOMEM
);
3733 /* 1 for the orphan item */
3734 trans
= btrfs_start_transaction(root
, 1);
3735 if (IS_ERR(trans
)) {
3736 btrfs_free_path(path
);
3737 root
->fs_info
->enospc_unlink
= 0;
3741 path
->skip_locking
= 1;
3742 path
->search_commit_root
= 1;
3744 ret
= btrfs_lookup_inode(trans
, root
, path
,
3745 &BTRFS_I(dir
)->location
, 0);
3751 if (check_path_shared(root
, path
))
3756 btrfs_release_path(path
);
3758 ret
= btrfs_lookup_inode(trans
, root
, path
,
3759 &BTRFS_I(inode
)->location
, 0);
3765 if (check_path_shared(root
, path
))
3770 btrfs_release_path(path
);
3772 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3773 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3779 BUG_ON(ret
== 0); /* Corruption */
3780 if (check_path_shared(root
, path
))
3782 btrfs_release_path(path
);
3790 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3791 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3797 if (check_path_shared(root
, path
))
3803 btrfs_release_path(path
);
3805 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3806 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3813 if (check_path_shared(root
, path
))
3816 btrfs_release_path(path
);
3819 * This is a commit root search, if we can lookup inode item and other
3820 * relative items in the commit root, it means the transaction of
3821 * dir/file creation has been committed, and the dir index item that we
3822 * delay to insert has also been inserted into the commit root. So
3823 * we needn't worry about the delayed insertion of the dir index item
3826 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3827 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3832 BUG_ON(ret
== -ENOENT
);
3833 if (check_path_shared(root
, path
))
3838 btrfs_free_path(path
);
3839 /* Migrate the orphan reservation over */
3841 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3842 &root
->fs_info
->global_block_rsv
,
3843 trans
->bytes_reserved
);
3846 btrfs_end_transaction(trans
, root
);
3847 root
->fs_info
->enospc_unlink
= 0;
3848 return ERR_PTR(err
);
3851 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3855 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3856 struct btrfs_root
*root
)
3858 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3859 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3860 trans
->bytes_reserved
);
3861 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3862 BUG_ON(!root
->fs_info
->enospc_unlink
);
3863 root
->fs_info
->enospc_unlink
= 0;
3865 btrfs_end_transaction(trans
, root
);
3868 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3870 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3871 struct btrfs_trans_handle
*trans
;
3872 struct inode
*inode
= dentry
->d_inode
;
3875 trans
= __unlink_start_trans(dir
, dentry
);
3877 return PTR_ERR(trans
);
3879 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3881 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3882 dentry
->d_name
.name
, dentry
->d_name
.len
);
3886 if (inode
->i_nlink
== 0) {
3887 ret
= btrfs_orphan_add(trans
, inode
);
3893 __unlink_end_trans(trans
, root
);
3894 btrfs_btree_balance_dirty(root
);
3898 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3899 struct btrfs_root
*root
,
3900 struct inode
*dir
, u64 objectid
,
3901 const char *name
, int name_len
)
3903 struct btrfs_path
*path
;
3904 struct extent_buffer
*leaf
;
3905 struct btrfs_dir_item
*di
;
3906 struct btrfs_key key
;
3909 u64 dir_ino
= btrfs_ino(dir
);
3911 path
= btrfs_alloc_path();
3915 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3916 name
, name_len
, -1);
3917 if (IS_ERR_OR_NULL(di
)) {
3925 leaf
= path
->nodes
[0];
3926 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3927 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3928 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3930 btrfs_abort_transaction(trans
, root
, ret
);
3933 btrfs_release_path(path
);
3935 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3936 objectid
, root
->root_key
.objectid
,
3937 dir_ino
, &index
, name
, name_len
);
3939 if (ret
!= -ENOENT
) {
3940 btrfs_abort_transaction(trans
, root
, ret
);
3943 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3945 if (IS_ERR_OR_NULL(di
)) {
3950 btrfs_abort_transaction(trans
, root
, ret
);
3954 leaf
= path
->nodes
[0];
3955 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3956 btrfs_release_path(path
);
3959 btrfs_release_path(path
);
3961 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3963 btrfs_abort_transaction(trans
, root
, ret
);
3967 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3968 inode_inc_iversion(dir
);
3969 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3970 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3972 btrfs_abort_transaction(trans
, root
, ret
);
3974 btrfs_free_path(path
);
3978 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3980 struct inode
*inode
= dentry
->d_inode
;
3982 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3983 struct btrfs_trans_handle
*trans
;
3985 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3987 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3990 trans
= __unlink_start_trans(dir
, dentry
);
3992 return PTR_ERR(trans
);
3994 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3995 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3996 BTRFS_I(inode
)->location
.objectid
,
3997 dentry
->d_name
.name
,
3998 dentry
->d_name
.len
);
4002 err
= btrfs_orphan_add(trans
, inode
);
4006 /* now the directory is empty */
4007 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4008 dentry
->d_name
.name
, dentry
->d_name
.len
);
4010 btrfs_i_size_write(inode
, 0);
4012 __unlink_end_trans(trans
, root
);
4013 btrfs_btree_balance_dirty(root
);
4019 * this can truncate away extent items, csum items and directory items.
4020 * It starts at a high offset and removes keys until it can't find
4021 * any higher than new_size
4023 * csum items that cross the new i_size are truncated to the new size
4026 * min_type is the minimum key type to truncate down to. If set to 0, this
4027 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4029 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4030 struct btrfs_root
*root
,
4031 struct inode
*inode
,
4032 u64 new_size
, u32 min_type
)
4034 struct btrfs_path
*path
;
4035 struct extent_buffer
*leaf
;
4036 struct btrfs_file_extent_item
*fi
;
4037 struct btrfs_key key
;
4038 struct btrfs_key found_key
;
4039 u64 extent_start
= 0;
4040 u64 extent_num_bytes
= 0;
4041 u64 extent_offset
= 0;
4043 u32 found_type
= (u8
)-1;
4046 int pending_del_nr
= 0;
4047 int pending_del_slot
= 0;
4048 int extent_type
= -1;
4051 u64 ino
= btrfs_ino(inode
);
4053 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4055 path
= btrfs_alloc_path();
4061 * We want to drop from the next block forward in case this new size is
4062 * not block aligned since we will be keeping the last block of the
4063 * extent just the way it is.
4065 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
4066 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4067 root
->sectorsize
), (u64
)-1, 0);
4070 * This function is also used to drop the items in the log tree before
4071 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4072 * it is used to drop the loged items. So we shouldn't kill the delayed
4075 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4076 btrfs_kill_delayed_inode_items(inode
);
4079 key
.offset
= (u64
)-1;
4083 path
->leave_spinning
= 1;
4084 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4091 /* there are no items in the tree for us to truncate, we're
4094 if (path
->slots
[0] == 0)
4101 leaf
= path
->nodes
[0];
4102 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4103 found_type
= btrfs_key_type(&found_key
);
4105 if (found_key
.objectid
!= ino
)
4108 if (found_type
< min_type
)
4111 item_end
= found_key
.offset
;
4112 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4113 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4114 struct btrfs_file_extent_item
);
4115 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4116 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4118 btrfs_file_extent_num_bytes(leaf
, fi
);
4119 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4120 item_end
+= btrfs_file_extent_inline_len(leaf
,
4125 if (found_type
> min_type
) {
4128 if (item_end
< new_size
)
4130 if (found_key
.offset
>= new_size
)
4136 /* FIXME, shrink the extent if the ref count is only 1 */
4137 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4140 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4142 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4144 u64 orig_num_bytes
=
4145 btrfs_file_extent_num_bytes(leaf
, fi
);
4146 extent_num_bytes
= ALIGN(new_size
-
4149 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4151 num_dec
= (orig_num_bytes
-
4153 if (root
->ref_cows
&& extent_start
!= 0)
4154 inode_sub_bytes(inode
, num_dec
);
4155 btrfs_mark_buffer_dirty(leaf
);
4158 btrfs_file_extent_disk_num_bytes(leaf
,
4160 extent_offset
= found_key
.offset
-
4161 btrfs_file_extent_offset(leaf
, fi
);
4163 /* FIXME blocksize != 4096 */
4164 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4165 if (extent_start
!= 0) {
4168 inode_sub_bytes(inode
, num_dec
);
4171 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4173 * we can't truncate inline items that have had
4177 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4178 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4179 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4180 u32 size
= new_size
- found_key
.offset
;
4182 if (root
->ref_cows
) {
4183 inode_sub_bytes(inode
, item_end
+ 1 -
4187 btrfs_file_extent_calc_inline_size(size
);
4188 btrfs_truncate_item(trans
, root
, path
,
4190 } else if (root
->ref_cows
) {
4191 inode_sub_bytes(inode
, item_end
+ 1 -
4197 if (!pending_del_nr
) {
4198 /* no pending yet, add ourselves */
4199 pending_del_slot
= path
->slots
[0];
4201 } else if (pending_del_nr
&&
4202 path
->slots
[0] + 1 == pending_del_slot
) {
4203 /* hop on the pending chunk */
4205 pending_del_slot
= path
->slots
[0];
4212 if (found_extent
&& (root
->ref_cows
||
4213 root
== root
->fs_info
->tree_root
)) {
4214 btrfs_set_path_blocking(path
);
4215 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4216 extent_num_bytes
, 0,
4217 btrfs_header_owner(leaf
),
4218 ino
, extent_offset
, 0);
4222 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4225 if (path
->slots
[0] == 0 ||
4226 path
->slots
[0] != pending_del_slot
) {
4227 if (pending_del_nr
) {
4228 ret
= btrfs_del_items(trans
, root
, path
,
4232 btrfs_abort_transaction(trans
,
4238 btrfs_release_path(path
);
4245 if (pending_del_nr
) {
4246 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4249 btrfs_abort_transaction(trans
, root
, ret
);
4252 btrfs_free_path(path
);
4257 * btrfs_truncate_page - read, zero a chunk and write a page
4258 * @inode - inode that we're zeroing
4259 * @from - the offset to start zeroing
4260 * @len - the length to zero, 0 to zero the entire range respective to the
4262 * @front - zero up to the offset instead of from the offset on
4264 * This will find the page for the "from" offset and cow the page and zero the
4265 * part we want to zero. This is used with truncate and hole punching.
4267 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4270 struct address_space
*mapping
= inode
->i_mapping
;
4271 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4272 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4273 struct btrfs_ordered_extent
*ordered
;
4274 struct extent_state
*cached_state
= NULL
;
4276 u32 blocksize
= root
->sectorsize
;
4277 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4278 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4280 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4285 if ((offset
& (blocksize
- 1)) == 0 &&
4286 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4288 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4293 page
= find_or_create_page(mapping
, index
, mask
);
4295 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4300 page_start
= page_offset(page
);
4301 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4303 if (!PageUptodate(page
)) {
4304 ret
= btrfs_readpage(NULL
, page
);
4306 if (page
->mapping
!= mapping
) {
4308 page_cache_release(page
);
4311 if (!PageUptodate(page
)) {
4316 wait_on_page_writeback(page
);
4318 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4319 set_page_extent_mapped(page
);
4321 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4323 unlock_extent_cached(io_tree
, page_start
, page_end
,
4324 &cached_state
, GFP_NOFS
);
4326 page_cache_release(page
);
4327 btrfs_start_ordered_extent(inode
, ordered
, 1);
4328 btrfs_put_ordered_extent(ordered
);
4332 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4333 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4334 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4335 0, 0, &cached_state
, GFP_NOFS
);
4337 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4340 unlock_extent_cached(io_tree
, page_start
, page_end
,
4341 &cached_state
, GFP_NOFS
);
4345 if (offset
!= PAGE_CACHE_SIZE
) {
4347 len
= PAGE_CACHE_SIZE
- offset
;
4350 memset(kaddr
, 0, offset
);
4352 memset(kaddr
+ offset
, 0, len
);
4353 flush_dcache_page(page
);
4356 ClearPageChecked(page
);
4357 set_page_dirty(page
);
4358 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4363 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4365 page_cache_release(page
);
4371 * This function puts in dummy file extents for the area we're creating a hole
4372 * for. So if we are truncating this file to a larger size we need to insert
4373 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4374 * the range between oldsize and size
4376 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4378 struct btrfs_trans_handle
*trans
;
4379 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4380 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4381 struct extent_map
*em
= NULL
;
4382 struct extent_state
*cached_state
= NULL
;
4383 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4384 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4385 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4391 if (size
<= hole_start
)
4395 struct btrfs_ordered_extent
*ordered
;
4396 btrfs_wait_ordered_range(inode
, hole_start
,
4397 block_end
- hole_start
);
4398 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4400 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4403 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4404 &cached_state
, GFP_NOFS
);
4405 btrfs_put_ordered_extent(ordered
);
4408 cur_offset
= hole_start
;
4410 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4411 block_end
- cur_offset
, 0);
4417 last_byte
= min(extent_map_end(em
), block_end
);
4418 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4419 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4420 struct extent_map
*hole_em
;
4421 hole_size
= last_byte
- cur_offset
;
4423 trans
= btrfs_start_transaction(root
, 3);
4424 if (IS_ERR(trans
)) {
4425 err
= PTR_ERR(trans
);
4429 err
= btrfs_drop_extents(trans
, root
, inode
,
4431 cur_offset
+ hole_size
, 1);
4433 btrfs_abort_transaction(trans
, root
, err
);
4434 btrfs_end_transaction(trans
, root
);
4438 err
= btrfs_insert_file_extent(trans
, root
,
4439 btrfs_ino(inode
), cur_offset
, 0,
4440 0, hole_size
, 0, hole_size
,
4443 btrfs_abort_transaction(trans
, root
, err
);
4444 btrfs_end_transaction(trans
, root
);
4448 btrfs_drop_extent_cache(inode
, cur_offset
,
4449 cur_offset
+ hole_size
- 1, 0);
4450 hole_em
= alloc_extent_map();
4452 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4453 &BTRFS_I(inode
)->runtime_flags
);
4456 hole_em
->start
= cur_offset
;
4457 hole_em
->len
= hole_size
;
4458 hole_em
->orig_start
= cur_offset
;
4460 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4461 hole_em
->block_len
= 0;
4462 hole_em
->orig_block_len
= 0;
4463 hole_em
->ram_bytes
= hole_size
;
4464 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4465 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4466 hole_em
->generation
= trans
->transid
;
4469 write_lock(&em_tree
->lock
);
4470 err
= add_extent_mapping(em_tree
, hole_em
);
4472 list_move(&hole_em
->list
,
4473 &em_tree
->modified_extents
);
4474 write_unlock(&em_tree
->lock
);
4477 btrfs_drop_extent_cache(inode
, cur_offset
,
4481 free_extent_map(hole_em
);
4483 btrfs_update_inode(trans
, root
, inode
);
4484 btrfs_end_transaction(trans
, root
);
4486 free_extent_map(em
);
4488 cur_offset
= last_byte
;
4489 if (cur_offset
>= block_end
)
4493 free_extent_map(em
);
4494 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4499 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4501 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4502 struct btrfs_trans_handle
*trans
;
4503 loff_t oldsize
= i_size_read(inode
);
4504 loff_t newsize
= attr
->ia_size
;
4505 int mask
= attr
->ia_valid
;
4508 if (newsize
== oldsize
)
4512 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4513 * special case where we need to update the times despite not having
4514 * these flags set. For all other operations the VFS set these flags
4515 * explicitly if it wants a timestamp update.
4517 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4518 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4520 if (newsize
> oldsize
) {
4521 truncate_pagecache(inode
, oldsize
, newsize
);
4522 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4526 trans
= btrfs_start_transaction(root
, 1);
4528 return PTR_ERR(trans
);
4530 i_size_write(inode
, newsize
);
4531 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4532 ret
= btrfs_update_inode(trans
, root
, inode
);
4533 btrfs_end_transaction(trans
, root
);
4537 * We're truncating a file that used to have good data down to
4538 * zero. Make sure it gets into the ordered flush list so that
4539 * any new writes get down to disk quickly.
4542 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4543 &BTRFS_I(inode
)->runtime_flags
);
4546 * 1 for the orphan item we're going to add
4547 * 1 for the orphan item deletion.
4549 trans
= btrfs_start_transaction(root
, 2);
4551 return PTR_ERR(trans
);
4554 * We need to do this in case we fail at _any_ point during the
4555 * actual truncate. Once we do the truncate_setsize we could
4556 * invalidate pages which forces any outstanding ordered io to
4557 * be instantly completed which will give us extents that need
4558 * to be truncated. If we fail to get an orphan inode down we
4559 * could have left over extents that were never meant to live,
4560 * so we need to garuntee from this point on that everything
4561 * will be consistent.
4563 ret
= btrfs_orphan_add(trans
, inode
);
4564 btrfs_end_transaction(trans
, root
);
4568 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4569 truncate_setsize(inode
, newsize
);
4571 /* Disable nonlocked read DIO to avoid the end less truncate */
4572 btrfs_inode_block_unlocked_dio(inode
);
4573 inode_dio_wait(inode
);
4574 btrfs_inode_resume_unlocked_dio(inode
);
4576 ret
= btrfs_truncate(inode
);
4577 if (ret
&& inode
->i_nlink
)
4578 btrfs_orphan_del(NULL
, inode
);
4584 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4586 struct inode
*inode
= dentry
->d_inode
;
4587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4590 if (btrfs_root_readonly(root
))
4593 err
= inode_change_ok(inode
, attr
);
4597 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4598 err
= btrfs_setsize(inode
, attr
);
4603 if (attr
->ia_valid
) {
4604 setattr_copy(inode
, attr
);
4605 inode_inc_iversion(inode
);
4606 err
= btrfs_dirty_inode(inode
);
4608 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4609 err
= btrfs_acl_chmod(inode
);
4615 void btrfs_evict_inode(struct inode
*inode
)
4617 struct btrfs_trans_handle
*trans
;
4618 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4619 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4620 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4623 trace_btrfs_inode_evict(inode
);
4625 truncate_inode_pages(&inode
->i_data
, 0);
4626 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4627 btrfs_is_free_space_inode(inode
)))
4630 if (is_bad_inode(inode
)) {
4631 btrfs_orphan_del(NULL
, inode
);
4634 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4635 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4637 if (root
->fs_info
->log_root_recovering
) {
4638 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4639 &BTRFS_I(inode
)->runtime_flags
));
4643 if (inode
->i_nlink
> 0) {
4644 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4648 ret
= btrfs_commit_inode_delayed_inode(inode
);
4650 btrfs_orphan_del(NULL
, inode
);
4654 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4656 btrfs_orphan_del(NULL
, inode
);
4659 rsv
->size
= min_size
;
4661 global_rsv
= &root
->fs_info
->global_block_rsv
;
4663 btrfs_i_size_write(inode
, 0);
4666 * This is a bit simpler than btrfs_truncate since we've already
4667 * reserved our space for our orphan item in the unlink, so we just
4668 * need to reserve some slack space in case we add bytes and update
4669 * inode item when doing the truncate.
4672 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4673 BTRFS_RESERVE_FLUSH_LIMIT
);
4676 * Try and steal from the global reserve since we will
4677 * likely not use this space anyway, we want to try as
4678 * hard as possible to get this to work.
4681 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4684 btrfs_warn(root
->fs_info
,
4685 "Could not get space for a delete, will truncate on mount %d",
4687 btrfs_orphan_del(NULL
, inode
);
4688 btrfs_free_block_rsv(root
, rsv
);
4692 trans
= btrfs_join_transaction(root
);
4693 if (IS_ERR(trans
)) {
4694 btrfs_orphan_del(NULL
, inode
);
4695 btrfs_free_block_rsv(root
, rsv
);
4699 trans
->block_rsv
= rsv
;
4701 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4705 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4706 btrfs_end_transaction(trans
, root
);
4708 btrfs_btree_balance_dirty(root
);
4711 btrfs_free_block_rsv(root
, rsv
);
4714 trans
->block_rsv
= root
->orphan_block_rsv
;
4715 ret
= btrfs_orphan_del(trans
, inode
);
4719 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4720 if (!(root
== root
->fs_info
->tree_root
||
4721 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4722 btrfs_return_ino(root
, btrfs_ino(inode
));
4724 btrfs_end_transaction(trans
, root
);
4725 btrfs_btree_balance_dirty(root
);
4732 * this returns the key found in the dir entry in the location pointer.
4733 * If no dir entries were found, location->objectid is 0.
4735 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4736 struct btrfs_key
*location
)
4738 const char *name
= dentry
->d_name
.name
;
4739 int namelen
= dentry
->d_name
.len
;
4740 struct btrfs_dir_item
*di
;
4741 struct btrfs_path
*path
;
4742 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4745 path
= btrfs_alloc_path();
4749 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4754 if (IS_ERR_OR_NULL(di
))
4757 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4759 btrfs_free_path(path
);
4762 location
->objectid
= 0;
4767 * when we hit a tree root in a directory, the btrfs part of the inode
4768 * needs to be changed to reflect the root directory of the tree root. This
4769 * is kind of like crossing a mount point.
4771 static int fixup_tree_root_location(struct btrfs_root
*root
,
4773 struct dentry
*dentry
,
4774 struct btrfs_key
*location
,
4775 struct btrfs_root
**sub_root
)
4777 struct btrfs_path
*path
;
4778 struct btrfs_root
*new_root
;
4779 struct btrfs_root_ref
*ref
;
4780 struct extent_buffer
*leaf
;
4784 path
= btrfs_alloc_path();
4791 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4792 BTRFS_I(dir
)->root
->root_key
.objectid
,
4793 location
->objectid
);
4800 leaf
= path
->nodes
[0];
4801 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4802 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4803 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4806 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4807 (unsigned long)(ref
+ 1),
4808 dentry
->d_name
.len
);
4812 btrfs_release_path(path
);
4814 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4815 if (IS_ERR(new_root
)) {
4816 err
= PTR_ERR(new_root
);
4820 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4825 *sub_root
= new_root
;
4826 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4827 location
->type
= BTRFS_INODE_ITEM_KEY
;
4828 location
->offset
= 0;
4831 btrfs_free_path(path
);
4835 static void inode_tree_add(struct inode
*inode
)
4837 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4838 struct btrfs_inode
*entry
;
4840 struct rb_node
*parent
;
4841 u64 ino
= btrfs_ino(inode
);
4843 p
= &root
->inode_tree
.rb_node
;
4846 if (inode_unhashed(inode
))
4849 spin_lock(&root
->inode_lock
);
4852 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4854 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4855 p
= &parent
->rb_left
;
4856 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4857 p
= &parent
->rb_right
;
4859 WARN_ON(!(entry
->vfs_inode
.i_state
&
4860 (I_WILL_FREE
| I_FREEING
)));
4861 rb_erase(parent
, &root
->inode_tree
);
4862 RB_CLEAR_NODE(parent
);
4863 spin_unlock(&root
->inode_lock
);
4867 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4868 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4869 spin_unlock(&root
->inode_lock
);
4872 static void inode_tree_del(struct inode
*inode
)
4874 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4877 spin_lock(&root
->inode_lock
);
4878 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4879 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4880 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4881 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4883 spin_unlock(&root
->inode_lock
);
4886 * Free space cache has inodes in the tree root, but the tree root has a
4887 * root_refs of 0, so this could end up dropping the tree root as a
4888 * snapshot, so we need the extra !root->fs_info->tree_root check to
4889 * make sure we don't drop it.
4891 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4892 root
!= root
->fs_info
->tree_root
) {
4893 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4894 spin_lock(&root
->inode_lock
);
4895 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4896 spin_unlock(&root
->inode_lock
);
4898 btrfs_add_dead_root(root
);
4902 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4904 struct rb_node
*node
;
4905 struct rb_node
*prev
;
4906 struct btrfs_inode
*entry
;
4907 struct inode
*inode
;
4910 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4912 spin_lock(&root
->inode_lock
);
4914 node
= root
->inode_tree
.rb_node
;
4918 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4920 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4921 node
= node
->rb_left
;
4922 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4923 node
= node
->rb_right
;
4929 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4930 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4934 prev
= rb_next(prev
);
4938 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4939 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4940 inode
= igrab(&entry
->vfs_inode
);
4942 spin_unlock(&root
->inode_lock
);
4943 if (atomic_read(&inode
->i_count
) > 1)
4944 d_prune_aliases(inode
);
4946 * btrfs_drop_inode will have it removed from
4947 * the inode cache when its usage count
4952 spin_lock(&root
->inode_lock
);
4956 if (cond_resched_lock(&root
->inode_lock
))
4959 node
= rb_next(node
);
4961 spin_unlock(&root
->inode_lock
);
4964 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4966 struct btrfs_iget_args
*args
= p
;
4967 inode
->i_ino
= args
->ino
;
4968 BTRFS_I(inode
)->root
= args
->root
;
4972 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4974 struct btrfs_iget_args
*args
= opaque
;
4975 return args
->ino
== btrfs_ino(inode
) &&
4976 args
->root
== BTRFS_I(inode
)->root
;
4979 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4981 struct btrfs_root
*root
)
4983 struct inode
*inode
;
4984 struct btrfs_iget_args args
;
4985 args
.ino
= objectid
;
4988 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4989 btrfs_init_locked_inode
,
4994 /* Get an inode object given its location and corresponding root.
4995 * Returns in *is_new if the inode was read from disk
4997 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4998 struct btrfs_root
*root
, int *new)
5000 struct inode
*inode
;
5002 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
5004 return ERR_PTR(-ENOMEM
);
5006 if (inode
->i_state
& I_NEW
) {
5007 BTRFS_I(inode
)->root
= root
;
5008 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
5009 btrfs_read_locked_inode(inode
);
5010 if (!is_bad_inode(inode
)) {
5011 inode_tree_add(inode
);
5012 unlock_new_inode(inode
);
5016 unlock_new_inode(inode
);
5018 inode
= ERR_PTR(-ESTALE
);
5025 static struct inode
*new_simple_dir(struct super_block
*s
,
5026 struct btrfs_key
*key
,
5027 struct btrfs_root
*root
)
5029 struct inode
*inode
= new_inode(s
);
5032 return ERR_PTR(-ENOMEM
);
5034 BTRFS_I(inode
)->root
= root
;
5035 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5036 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5038 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5039 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5040 inode
->i_fop
= &simple_dir_operations
;
5041 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5042 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5047 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5049 struct inode
*inode
;
5050 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5051 struct btrfs_root
*sub_root
= root
;
5052 struct btrfs_key location
;
5056 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5057 return ERR_PTR(-ENAMETOOLONG
);
5059 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5061 return ERR_PTR(ret
);
5063 if (location
.objectid
== 0)
5066 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5067 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5071 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5073 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5074 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5075 &location
, &sub_root
);
5078 inode
= ERR_PTR(ret
);
5080 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5082 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5084 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5086 if (!IS_ERR(inode
) && root
!= sub_root
) {
5087 down_read(&root
->fs_info
->cleanup_work_sem
);
5088 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5089 ret
= btrfs_orphan_cleanup(sub_root
);
5090 up_read(&root
->fs_info
->cleanup_work_sem
);
5092 inode
= ERR_PTR(ret
);
5098 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5100 struct btrfs_root
*root
;
5101 struct inode
*inode
= dentry
->d_inode
;
5103 if (!inode
&& !IS_ROOT(dentry
))
5104 inode
= dentry
->d_parent
->d_inode
;
5107 root
= BTRFS_I(inode
)->root
;
5108 if (btrfs_root_refs(&root
->root_item
) == 0)
5111 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5117 static void btrfs_dentry_release(struct dentry
*dentry
)
5119 if (dentry
->d_fsdata
)
5120 kfree(dentry
->d_fsdata
);
5123 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5128 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5132 unsigned char btrfs_filetype_table
[] = {
5133 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5136 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
5139 struct inode
*inode
= file_inode(filp
);
5140 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5141 struct btrfs_item
*item
;
5142 struct btrfs_dir_item
*di
;
5143 struct btrfs_key key
;
5144 struct btrfs_key found_key
;
5145 struct btrfs_path
*path
;
5146 struct list_head ins_list
;
5147 struct list_head del_list
;
5149 struct extent_buffer
*leaf
;
5151 unsigned char d_type
;
5156 int key_type
= BTRFS_DIR_INDEX_KEY
;
5160 int is_curr
= 0; /* filp->f_pos points to the current index? */
5162 /* FIXME, use a real flag for deciding about the key type */
5163 if (root
->fs_info
->tree_root
== root
)
5164 key_type
= BTRFS_DIR_ITEM_KEY
;
5166 /* special case for "." */
5167 if (filp
->f_pos
== 0) {
5168 over
= filldir(dirent
, ".", 1,
5169 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5174 /* special case for .., just use the back ref */
5175 if (filp
->f_pos
== 1) {
5176 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5177 over
= filldir(dirent
, "..", 2,
5178 filp
->f_pos
, pino
, DT_DIR
);
5183 path
= btrfs_alloc_path();
5189 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5190 INIT_LIST_HEAD(&ins_list
);
5191 INIT_LIST_HEAD(&del_list
);
5192 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5195 btrfs_set_key_type(&key
, key_type
);
5196 key
.offset
= filp
->f_pos
;
5197 key
.objectid
= btrfs_ino(inode
);
5199 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5204 leaf
= path
->nodes
[0];
5205 slot
= path
->slots
[0];
5206 if (slot
>= btrfs_header_nritems(leaf
)) {
5207 ret
= btrfs_next_leaf(root
, path
);
5215 item
= btrfs_item_nr(leaf
, slot
);
5216 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5218 if (found_key
.objectid
!= key
.objectid
)
5220 if (btrfs_key_type(&found_key
) != key_type
)
5222 if (found_key
.offset
< filp
->f_pos
)
5224 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5225 btrfs_should_delete_dir_index(&del_list
,
5229 filp
->f_pos
= found_key
.offset
;
5232 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5234 di_total
= btrfs_item_size(leaf
, item
);
5236 while (di_cur
< di_total
) {
5237 struct btrfs_key location
;
5239 if (verify_dir_item(root
, leaf
, di
))
5242 name_len
= btrfs_dir_name_len(leaf
, di
);
5243 if (name_len
<= sizeof(tmp_name
)) {
5244 name_ptr
= tmp_name
;
5246 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5252 read_extent_buffer(leaf
, name_ptr
,
5253 (unsigned long)(di
+ 1), name_len
);
5255 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5256 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5259 /* is this a reference to our own snapshot? If so
5262 * In contrast to old kernels, we insert the snapshot's
5263 * dir item and dir index after it has been created, so
5264 * we won't find a reference to our own snapshot. We
5265 * still keep the following code for backward
5268 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5269 location
.objectid
== root
->root_key
.objectid
) {
5273 over
= filldir(dirent
, name_ptr
, name_len
,
5274 found_key
.offset
, location
.objectid
,
5278 if (name_ptr
!= tmp_name
)
5283 di_len
= btrfs_dir_name_len(leaf
, di
) +
5284 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5286 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5292 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5295 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5301 /* Reached end of directory/root. Bump pos past the last item. */
5302 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5304 * 32-bit glibc will use getdents64, but then strtol -
5305 * so the last number we can serve is this.
5307 filp
->f_pos
= 0x7fffffff;
5313 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5314 btrfs_put_delayed_items(&ins_list
, &del_list
);
5315 btrfs_free_path(path
);
5319 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5321 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5322 struct btrfs_trans_handle
*trans
;
5324 bool nolock
= false;
5326 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5329 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5332 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5334 trans
= btrfs_join_transaction_nolock(root
);
5336 trans
= btrfs_join_transaction(root
);
5338 return PTR_ERR(trans
);
5339 ret
= btrfs_commit_transaction(trans
, root
);
5345 * This is somewhat expensive, updating the tree every time the
5346 * inode changes. But, it is most likely to find the inode in cache.
5347 * FIXME, needs more benchmarking...there are no reasons other than performance
5348 * to keep or drop this code.
5350 int btrfs_dirty_inode(struct inode
*inode
)
5352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5353 struct btrfs_trans_handle
*trans
;
5356 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5359 trans
= btrfs_join_transaction(root
);
5361 return PTR_ERR(trans
);
5363 ret
= btrfs_update_inode(trans
, root
, inode
);
5364 if (ret
&& ret
== -ENOSPC
) {
5365 /* whoops, lets try again with the full transaction */
5366 btrfs_end_transaction(trans
, root
);
5367 trans
= btrfs_start_transaction(root
, 1);
5369 return PTR_ERR(trans
);
5371 ret
= btrfs_update_inode(trans
, root
, inode
);
5373 btrfs_end_transaction(trans
, root
);
5374 if (BTRFS_I(inode
)->delayed_node
)
5375 btrfs_balance_delayed_items(root
);
5381 * This is a copy of file_update_time. We need this so we can return error on
5382 * ENOSPC for updating the inode in the case of file write and mmap writes.
5384 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5387 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5389 if (btrfs_root_readonly(root
))
5392 if (flags
& S_VERSION
)
5393 inode_inc_iversion(inode
);
5394 if (flags
& S_CTIME
)
5395 inode
->i_ctime
= *now
;
5396 if (flags
& S_MTIME
)
5397 inode
->i_mtime
= *now
;
5398 if (flags
& S_ATIME
)
5399 inode
->i_atime
= *now
;
5400 return btrfs_dirty_inode(inode
);
5404 * find the highest existing sequence number in a directory
5405 * and then set the in-memory index_cnt variable to reflect
5406 * free sequence numbers
5408 static int btrfs_set_inode_index_count(struct inode
*inode
)
5410 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5411 struct btrfs_key key
, found_key
;
5412 struct btrfs_path
*path
;
5413 struct extent_buffer
*leaf
;
5416 key
.objectid
= btrfs_ino(inode
);
5417 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5418 key
.offset
= (u64
)-1;
5420 path
= btrfs_alloc_path();
5424 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5427 /* FIXME: we should be able to handle this */
5433 * MAGIC NUMBER EXPLANATION:
5434 * since we search a directory based on f_pos we have to start at 2
5435 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5436 * else has to start at 2
5438 if (path
->slots
[0] == 0) {
5439 BTRFS_I(inode
)->index_cnt
= 2;
5445 leaf
= path
->nodes
[0];
5446 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5448 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5449 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5450 BTRFS_I(inode
)->index_cnt
= 2;
5454 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5456 btrfs_free_path(path
);
5461 * helper to find a free sequence number in a given directory. This current
5462 * code is very simple, later versions will do smarter things in the btree
5464 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5468 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5469 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5471 ret
= btrfs_set_inode_index_count(dir
);
5477 *index
= BTRFS_I(dir
)->index_cnt
;
5478 BTRFS_I(dir
)->index_cnt
++;
5483 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5484 struct btrfs_root
*root
,
5486 const char *name
, int name_len
,
5487 u64 ref_objectid
, u64 objectid
,
5488 umode_t mode
, u64
*index
)
5490 struct inode
*inode
;
5491 struct btrfs_inode_item
*inode_item
;
5492 struct btrfs_key
*location
;
5493 struct btrfs_path
*path
;
5494 struct btrfs_inode_ref
*ref
;
5495 struct btrfs_key key
[2];
5501 path
= btrfs_alloc_path();
5503 return ERR_PTR(-ENOMEM
);
5505 inode
= new_inode(root
->fs_info
->sb
);
5507 btrfs_free_path(path
);
5508 return ERR_PTR(-ENOMEM
);
5512 * we have to initialize this early, so we can reclaim the inode
5513 * number if we fail afterwards in this function.
5515 inode
->i_ino
= objectid
;
5518 trace_btrfs_inode_request(dir
);
5520 ret
= btrfs_set_inode_index(dir
, index
);
5522 btrfs_free_path(path
);
5524 return ERR_PTR(ret
);
5528 * index_cnt is ignored for everything but a dir,
5529 * btrfs_get_inode_index_count has an explanation for the magic
5532 BTRFS_I(inode
)->index_cnt
= 2;
5533 BTRFS_I(inode
)->root
= root
;
5534 BTRFS_I(inode
)->generation
= trans
->transid
;
5535 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5538 * We could have gotten an inode number from somebody who was fsynced
5539 * and then removed in this same transaction, so let's just set full
5540 * sync since it will be a full sync anyway and this will blow away the
5541 * old info in the log.
5543 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5550 key
[0].objectid
= objectid
;
5551 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5555 * Start new inodes with an inode_ref. This is slightly more
5556 * efficient for small numbers of hard links since they will
5557 * be packed into one item. Extended refs will kick in if we
5558 * add more hard links than can fit in the ref item.
5560 key
[1].objectid
= objectid
;
5561 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5562 key
[1].offset
= ref_objectid
;
5564 sizes
[0] = sizeof(struct btrfs_inode_item
);
5565 sizes
[1] = name_len
+ sizeof(*ref
);
5567 path
->leave_spinning
= 1;
5568 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5572 inode_init_owner(inode
, dir
, mode
);
5573 inode_set_bytes(inode
, 0);
5574 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5575 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5576 struct btrfs_inode_item
);
5577 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5578 sizeof(*inode_item
));
5579 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5581 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5582 struct btrfs_inode_ref
);
5583 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5584 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5585 ptr
= (unsigned long)(ref
+ 1);
5586 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5588 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5589 btrfs_free_path(path
);
5591 location
= &BTRFS_I(inode
)->location
;
5592 location
->objectid
= objectid
;
5593 location
->offset
= 0;
5594 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5596 btrfs_inherit_iflags(inode
, dir
);
5598 if (S_ISREG(mode
)) {
5599 if (btrfs_test_opt(root
, NODATASUM
))
5600 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5601 if (btrfs_test_opt(root
, NODATACOW
))
5602 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5603 BTRFS_INODE_NODATASUM
;
5606 insert_inode_hash(inode
);
5607 inode_tree_add(inode
);
5609 trace_btrfs_inode_new(inode
);
5610 btrfs_set_inode_last_trans(trans
, inode
);
5612 btrfs_update_root_times(trans
, root
);
5617 BTRFS_I(dir
)->index_cnt
--;
5618 btrfs_free_path(path
);
5620 return ERR_PTR(ret
);
5623 static inline u8
btrfs_inode_type(struct inode
*inode
)
5625 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5629 * utility function to add 'inode' into 'parent_inode' with
5630 * a give name and a given sequence number.
5631 * if 'add_backref' is true, also insert a backref from the
5632 * inode to the parent directory.
5634 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5635 struct inode
*parent_inode
, struct inode
*inode
,
5636 const char *name
, int name_len
, int add_backref
, u64 index
)
5639 struct btrfs_key key
;
5640 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5641 u64 ino
= btrfs_ino(inode
);
5642 u64 parent_ino
= btrfs_ino(parent_inode
);
5644 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5645 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5648 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5652 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5653 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5654 key
.objectid
, root
->root_key
.objectid
,
5655 parent_ino
, index
, name
, name_len
);
5656 } else if (add_backref
) {
5657 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5661 /* Nothing to clean up yet */
5665 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5667 btrfs_inode_type(inode
), index
);
5668 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5671 btrfs_abort_transaction(trans
, root
, ret
);
5675 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5677 inode_inc_iversion(parent_inode
);
5678 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5679 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5681 btrfs_abort_transaction(trans
, root
, ret
);
5685 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5688 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5689 key
.objectid
, root
->root_key
.objectid
,
5690 parent_ino
, &local_index
, name
, name_len
);
5692 } else if (add_backref
) {
5696 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5697 ino
, parent_ino
, &local_index
);
5702 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5703 struct inode
*dir
, struct dentry
*dentry
,
5704 struct inode
*inode
, int backref
, u64 index
)
5706 int err
= btrfs_add_link(trans
, dir
, inode
,
5707 dentry
->d_name
.name
, dentry
->d_name
.len
,
5714 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5715 umode_t mode
, dev_t rdev
)
5717 struct btrfs_trans_handle
*trans
;
5718 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5719 struct inode
*inode
= NULL
;
5725 if (!new_valid_dev(rdev
))
5729 * 2 for inode item and ref
5731 * 1 for xattr if selinux is on
5733 trans
= btrfs_start_transaction(root
, 5);
5735 return PTR_ERR(trans
);
5737 err
= btrfs_find_free_ino(root
, &objectid
);
5741 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5742 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5744 if (IS_ERR(inode
)) {
5745 err
= PTR_ERR(inode
);
5749 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5756 * If the active LSM wants to access the inode during
5757 * d_instantiate it needs these. Smack checks to see
5758 * if the filesystem supports xattrs by looking at the
5762 inode
->i_op
= &btrfs_special_inode_operations
;
5763 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5767 init_special_inode(inode
, inode
->i_mode
, rdev
);
5768 btrfs_update_inode(trans
, root
, inode
);
5769 d_instantiate(dentry
, inode
);
5772 btrfs_end_transaction(trans
, root
);
5773 btrfs_btree_balance_dirty(root
);
5775 inode_dec_link_count(inode
);
5781 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5782 umode_t mode
, bool excl
)
5784 struct btrfs_trans_handle
*trans
;
5785 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5786 struct inode
*inode
= NULL
;
5787 int drop_inode_on_err
= 0;
5793 * 2 for inode item and ref
5795 * 1 for xattr if selinux is on
5797 trans
= btrfs_start_transaction(root
, 5);
5799 return PTR_ERR(trans
);
5801 err
= btrfs_find_free_ino(root
, &objectid
);
5805 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5806 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5808 if (IS_ERR(inode
)) {
5809 err
= PTR_ERR(inode
);
5812 drop_inode_on_err
= 1;
5814 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5818 err
= btrfs_update_inode(trans
, root
, inode
);
5823 * If the active LSM wants to access the inode during
5824 * d_instantiate it needs these. Smack checks to see
5825 * if the filesystem supports xattrs by looking at the
5828 inode
->i_fop
= &btrfs_file_operations
;
5829 inode
->i_op
= &btrfs_file_inode_operations
;
5831 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5835 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5836 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5837 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5838 d_instantiate(dentry
, inode
);
5841 btrfs_end_transaction(trans
, root
);
5842 if (err
&& drop_inode_on_err
) {
5843 inode_dec_link_count(inode
);
5846 btrfs_btree_balance_dirty(root
);
5850 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5851 struct dentry
*dentry
)
5853 struct btrfs_trans_handle
*trans
;
5854 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5855 struct inode
*inode
= old_dentry
->d_inode
;
5860 /* do not allow sys_link's with other subvols of the same device */
5861 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5864 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5867 err
= btrfs_set_inode_index(dir
, &index
);
5872 * 2 items for inode and inode ref
5873 * 2 items for dir items
5874 * 1 item for parent inode
5876 trans
= btrfs_start_transaction(root
, 5);
5877 if (IS_ERR(trans
)) {
5878 err
= PTR_ERR(trans
);
5882 btrfs_inc_nlink(inode
);
5883 inode_inc_iversion(inode
);
5884 inode
->i_ctime
= CURRENT_TIME
;
5886 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5888 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5893 struct dentry
*parent
= dentry
->d_parent
;
5894 err
= btrfs_update_inode(trans
, root
, inode
);
5897 d_instantiate(dentry
, inode
);
5898 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5901 btrfs_end_transaction(trans
, root
);
5904 inode_dec_link_count(inode
);
5907 btrfs_btree_balance_dirty(root
);
5911 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5913 struct inode
*inode
= NULL
;
5914 struct btrfs_trans_handle
*trans
;
5915 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5917 int drop_on_err
= 0;
5922 * 2 items for inode and ref
5923 * 2 items for dir items
5924 * 1 for xattr if selinux is on
5926 trans
= btrfs_start_transaction(root
, 5);
5928 return PTR_ERR(trans
);
5930 err
= btrfs_find_free_ino(root
, &objectid
);
5934 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5935 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5936 S_IFDIR
| mode
, &index
);
5937 if (IS_ERR(inode
)) {
5938 err
= PTR_ERR(inode
);
5944 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5948 inode
->i_op
= &btrfs_dir_inode_operations
;
5949 inode
->i_fop
= &btrfs_dir_file_operations
;
5951 btrfs_i_size_write(inode
, 0);
5952 err
= btrfs_update_inode(trans
, root
, inode
);
5956 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5957 dentry
->d_name
.len
, 0, index
);
5961 d_instantiate(dentry
, inode
);
5965 btrfs_end_transaction(trans
, root
);
5968 btrfs_btree_balance_dirty(root
);
5972 /* helper for btfs_get_extent. Given an existing extent in the tree,
5973 * and an extent that you want to insert, deal with overlap and insert
5974 * the new extent into the tree.
5976 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5977 struct extent_map
*existing
,
5978 struct extent_map
*em
,
5979 u64 map_start
, u64 map_len
)
5983 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5984 start_diff
= map_start
- em
->start
;
5985 em
->start
= map_start
;
5987 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5988 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5989 em
->block_start
+= start_diff
;
5990 em
->block_len
-= start_diff
;
5992 return add_extent_mapping(em_tree
, em
);
5995 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5996 struct inode
*inode
, struct page
*page
,
5997 size_t pg_offset
, u64 extent_offset
,
5998 struct btrfs_file_extent_item
*item
)
6001 struct extent_buffer
*leaf
= path
->nodes
[0];
6004 unsigned long inline_size
;
6008 WARN_ON(pg_offset
!= 0);
6009 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6010 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6011 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6012 btrfs_item_nr(leaf
, path
->slots
[0]));
6013 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6016 ptr
= btrfs_file_extent_inline_start(item
);
6018 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6020 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6021 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6022 extent_offset
, inline_size
, max_size
);
6024 char *kaddr
= kmap_atomic(page
);
6025 unsigned long copy_size
= min_t(u64
,
6026 PAGE_CACHE_SIZE
- pg_offset
,
6027 max_size
- extent_offset
);
6028 memset(kaddr
+ pg_offset
, 0, copy_size
);
6029 kunmap_atomic(kaddr
);
6036 * a bit scary, this does extent mapping from logical file offset to the disk.
6037 * the ugly parts come from merging extents from the disk with the in-ram
6038 * representation. This gets more complex because of the data=ordered code,
6039 * where the in-ram extents might be locked pending data=ordered completion.
6041 * This also copies inline extents directly into the page.
6044 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6045 size_t pg_offset
, u64 start
, u64 len
,
6051 u64 extent_start
= 0;
6053 u64 objectid
= btrfs_ino(inode
);
6055 struct btrfs_path
*path
= NULL
;
6056 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6057 struct btrfs_file_extent_item
*item
;
6058 struct extent_buffer
*leaf
;
6059 struct btrfs_key found_key
;
6060 struct extent_map
*em
= NULL
;
6061 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6062 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6063 struct btrfs_trans_handle
*trans
= NULL
;
6067 read_lock(&em_tree
->lock
);
6068 em
= lookup_extent_mapping(em_tree
, start
, len
);
6070 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6071 read_unlock(&em_tree
->lock
);
6074 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6075 free_extent_map(em
);
6076 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6077 free_extent_map(em
);
6081 em
= alloc_extent_map();
6086 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6087 em
->start
= EXTENT_MAP_HOLE
;
6088 em
->orig_start
= EXTENT_MAP_HOLE
;
6090 em
->block_len
= (u64
)-1;
6093 path
= btrfs_alloc_path();
6099 * Chances are we'll be called again, so go ahead and do
6105 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6106 objectid
, start
, trans
!= NULL
);
6113 if (path
->slots
[0] == 0)
6118 leaf
= path
->nodes
[0];
6119 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6120 struct btrfs_file_extent_item
);
6121 /* are we inside the extent that was found? */
6122 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6123 found_type
= btrfs_key_type(&found_key
);
6124 if (found_key
.objectid
!= objectid
||
6125 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6129 found_type
= btrfs_file_extent_type(leaf
, item
);
6130 extent_start
= found_key
.offset
;
6131 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6132 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6133 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6134 extent_end
= extent_start
+
6135 btrfs_file_extent_num_bytes(leaf
, item
);
6136 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6138 size
= btrfs_file_extent_inline_len(leaf
, item
);
6139 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6142 if (start
>= extent_end
) {
6144 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6145 ret
= btrfs_next_leaf(root
, path
);
6152 leaf
= path
->nodes
[0];
6154 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6155 if (found_key
.objectid
!= objectid
||
6156 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6158 if (start
+ len
<= found_key
.offset
)
6161 em
->orig_start
= start
;
6162 em
->len
= found_key
.offset
- start
;
6166 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6167 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6168 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6169 em
->start
= extent_start
;
6170 em
->len
= extent_end
- extent_start
;
6171 em
->orig_start
= extent_start
-
6172 btrfs_file_extent_offset(leaf
, item
);
6173 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6175 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6177 em
->block_start
= EXTENT_MAP_HOLE
;
6180 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6181 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6182 em
->compress_type
= compress_type
;
6183 em
->block_start
= bytenr
;
6184 em
->block_len
= em
->orig_block_len
;
6186 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6187 em
->block_start
= bytenr
;
6188 em
->block_len
= em
->len
;
6189 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6190 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6193 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6197 size_t extent_offset
;
6200 em
->block_start
= EXTENT_MAP_INLINE
;
6201 if (!page
|| create
) {
6202 em
->start
= extent_start
;
6203 em
->len
= extent_end
- extent_start
;
6207 size
= btrfs_file_extent_inline_len(leaf
, item
);
6208 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6209 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6210 size
- extent_offset
);
6211 em
->start
= extent_start
+ extent_offset
;
6212 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6213 em
->orig_block_len
= em
->len
;
6214 em
->orig_start
= em
->start
;
6215 if (compress_type
) {
6216 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6217 em
->compress_type
= compress_type
;
6219 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6220 if (create
== 0 && !PageUptodate(page
)) {
6221 if (btrfs_file_extent_compression(leaf
, item
) !=
6222 BTRFS_COMPRESS_NONE
) {
6223 ret
= uncompress_inline(path
, inode
, page
,
6225 extent_offset
, item
);
6226 BUG_ON(ret
); /* -ENOMEM */
6229 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6231 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6232 memset(map
+ pg_offset
+ copy_size
, 0,
6233 PAGE_CACHE_SIZE
- pg_offset
-
6238 flush_dcache_page(page
);
6239 } else if (create
&& PageUptodate(page
)) {
6243 free_extent_map(em
);
6246 btrfs_release_path(path
);
6247 trans
= btrfs_join_transaction(root
);
6250 return ERR_CAST(trans
);
6254 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6257 btrfs_mark_buffer_dirty(leaf
);
6259 set_extent_uptodate(io_tree
, em
->start
,
6260 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6263 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6267 em
->orig_start
= start
;
6270 em
->block_start
= EXTENT_MAP_HOLE
;
6271 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6273 btrfs_release_path(path
);
6274 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6275 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6276 (unsigned long long)em
->start
,
6277 (unsigned long long)em
->len
,
6278 (unsigned long long)start
,
6279 (unsigned long long)len
);
6285 write_lock(&em_tree
->lock
);
6286 ret
= add_extent_mapping(em_tree
, em
);
6287 /* it is possible that someone inserted the extent into the tree
6288 * while we had the lock dropped. It is also possible that
6289 * an overlapping map exists in the tree
6291 if (ret
== -EEXIST
) {
6292 struct extent_map
*existing
;
6296 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6297 if (existing
&& (existing
->start
> start
||
6298 existing
->start
+ existing
->len
<= start
)) {
6299 free_extent_map(existing
);
6303 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6306 err
= merge_extent_mapping(em_tree
, existing
,
6309 free_extent_map(existing
);
6311 free_extent_map(em
);
6316 free_extent_map(em
);
6320 free_extent_map(em
);
6325 write_unlock(&em_tree
->lock
);
6329 trace_btrfs_get_extent(root
, em
);
6332 btrfs_free_path(path
);
6334 ret
= btrfs_end_transaction(trans
, root
);
6339 free_extent_map(em
);
6340 return ERR_PTR(err
);
6342 BUG_ON(!em
); /* Error is always set */
6346 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6347 size_t pg_offset
, u64 start
, u64 len
,
6350 struct extent_map
*em
;
6351 struct extent_map
*hole_em
= NULL
;
6352 u64 range_start
= start
;
6358 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6365 * - a pre-alloc extent,
6366 * there might actually be delalloc bytes behind it.
6368 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6369 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6375 /* check to see if we've wrapped (len == -1 or similar) */
6384 /* ok, we didn't find anything, lets look for delalloc */
6385 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6386 end
, len
, EXTENT_DELALLOC
, 1);
6387 found_end
= range_start
+ found
;
6388 if (found_end
< range_start
)
6389 found_end
= (u64
)-1;
6392 * we didn't find anything useful, return
6393 * the original results from get_extent()
6395 if (range_start
> end
|| found_end
<= start
) {
6401 /* adjust the range_start to make sure it doesn't
6402 * go backwards from the start they passed in
6404 range_start
= max(start
,range_start
);
6405 found
= found_end
- range_start
;
6408 u64 hole_start
= start
;
6411 em
= alloc_extent_map();
6417 * when btrfs_get_extent can't find anything it
6418 * returns one huge hole
6420 * make sure what it found really fits our range, and
6421 * adjust to make sure it is based on the start from
6425 u64 calc_end
= extent_map_end(hole_em
);
6427 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6428 free_extent_map(hole_em
);
6431 hole_start
= max(hole_em
->start
, start
);
6432 hole_len
= calc_end
- hole_start
;
6436 if (hole_em
&& range_start
> hole_start
) {
6437 /* our hole starts before our delalloc, so we
6438 * have to return just the parts of the hole
6439 * that go until the delalloc starts
6441 em
->len
= min(hole_len
,
6442 range_start
- hole_start
);
6443 em
->start
= hole_start
;
6444 em
->orig_start
= hole_start
;
6446 * don't adjust block start at all,
6447 * it is fixed at EXTENT_MAP_HOLE
6449 em
->block_start
= hole_em
->block_start
;
6450 em
->block_len
= hole_len
;
6451 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6452 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6454 em
->start
= range_start
;
6456 em
->orig_start
= range_start
;
6457 em
->block_start
= EXTENT_MAP_DELALLOC
;
6458 em
->block_len
= found
;
6460 } else if (hole_em
) {
6465 free_extent_map(hole_em
);
6467 free_extent_map(em
);
6468 return ERR_PTR(err
);
6473 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6476 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6477 struct btrfs_trans_handle
*trans
;
6478 struct extent_map
*em
;
6479 struct btrfs_key ins
;
6483 trans
= btrfs_join_transaction(root
);
6485 return ERR_CAST(trans
);
6487 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6489 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6490 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6491 alloc_hint
, &ins
, 1);
6497 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6498 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6502 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6503 ins
.offset
, ins
.offset
, 0);
6505 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6509 btrfs_end_transaction(trans
, root
);
6514 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6515 * block must be cow'd
6517 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6518 struct inode
*inode
, u64 offset
, u64 len
)
6520 struct btrfs_path
*path
;
6522 struct extent_buffer
*leaf
;
6523 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6524 struct btrfs_file_extent_item
*fi
;
6525 struct btrfs_key key
;
6533 path
= btrfs_alloc_path();
6537 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6542 slot
= path
->slots
[0];
6545 /* can't find the item, must cow */
6552 leaf
= path
->nodes
[0];
6553 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6554 if (key
.objectid
!= btrfs_ino(inode
) ||
6555 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6556 /* not our file or wrong item type, must cow */
6560 if (key
.offset
> offset
) {
6561 /* Wrong offset, must cow */
6565 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6566 found_type
= btrfs_file_extent_type(leaf
, fi
);
6567 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6568 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6569 /* not a regular extent, must cow */
6572 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6573 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6575 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6576 if (extent_end
< offset
+ len
) {
6577 /* extent doesn't include our full range, must cow */
6581 if (btrfs_extent_readonly(root
, disk_bytenr
))
6585 * look for other files referencing this extent, if we
6586 * find any we must cow
6588 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6589 key
.offset
- backref_offset
, disk_bytenr
))
6593 * adjust disk_bytenr and num_bytes to cover just the bytes
6594 * in this extent we are about to write. If there
6595 * are any csums in that range we have to cow in order
6596 * to keep the csums correct
6598 disk_bytenr
+= backref_offset
;
6599 disk_bytenr
+= offset
- key
.offset
;
6600 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
6601 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6604 * 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
);
6711 list_move(&em
->list
,
6712 &em_tree
->modified_extents
);
6713 write_unlock(&em_tree
->lock
);
6714 } while (ret
== -EEXIST
);
6717 free_extent_map(em
);
6718 return ERR_PTR(ret
);
6725 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6726 struct buffer_head
*bh_result
, int create
)
6728 struct extent_map
*em
;
6729 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6730 struct extent_state
*cached_state
= NULL
;
6731 u64 start
= iblock
<< inode
->i_blkbits
;
6732 u64 lockstart
, lockend
;
6733 u64 len
= bh_result
->b_size
;
6734 struct btrfs_trans_handle
*trans
;
6735 int unlock_bits
= EXTENT_LOCKED
;
6739 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6741 len
= min_t(u64
, len
, root
->sectorsize
);
6744 lockend
= start
+ len
- 1;
6747 * If this errors out it's because we couldn't invalidate pagecache for
6748 * this range and we need to fallback to buffered.
6750 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6753 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6760 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6761 * io. INLINE is special, and we could probably kludge it in here, but
6762 * it's still buffered so for safety lets just fall back to the generic
6765 * For COMPRESSED we _have_ to read the entire extent in so we can
6766 * decompress it, so there will be buffering required no matter what we
6767 * do, so go ahead and fallback to buffered.
6769 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6770 * to buffered IO. Don't blame me, this is the price we pay for using
6773 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6774 em
->block_start
== EXTENT_MAP_INLINE
) {
6775 free_extent_map(em
);
6780 /* Just a good old fashioned hole, return */
6781 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6782 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6783 free_extent_map(em
);
6788 * We don't allocate a new extent in the following cases
6790 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6792 * 2) The extent is marked as PREALLOC. We're good to go here and can
6793 * just use the extent.
6797 len
= min(len
, em
->len
- (start
- em
->start
));
6798 lockstart
= start
+ len
;
6802 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6803 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6804 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6809 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6810 type
= BTRFS_ORDERED_PREALLOC
;
6812 type
= BTRFS_ORDERED_NOCOW
;
6813 len
= min(len
, em
->len
- (start
- em
->start
));
6814 block_start
= em
->block_start
+ (start
- em
->start
);
6817 * we're not going to log anything, but we do need
6818 * to make sure the current transaction stays open
6819 * while we look for nocow cross refs
6821 trans
= btrfs_join_transaction(root
);
6825 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6826 u64 orig_start
= em
->orig_start
;
6827 u64 orig_block_len
= em
->orig_block_len
;
6828 u64 ram_bytes
= em
->ram_bytes
;
6830 if (type
== BTRFS_ORDERED_PREALLOC
) {
6831 free_extent_map(em
);
6832 em
= create_pinned_em(inode
, start
, len
,
6838 btrfs_end_transaction(trans
, root
);
6843 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6844 block_start
, len
, len
, type
);
6845 btrfs_end_transaction(trans
, root
);
6847 free_extent_map(em
);
6852 btrfs_end_transaction(trans
, root
);
6856 * this will cow the extent, reset the len in case we changed
6859 len
= bh_result
->b_size
;
6860 free_extent_map(em
);
6861 em
= btrfs_new_extent_direct(inode
, start
, len
);
6866 len
= min(len
, em
->len
- (start
- em
->start
));
6868 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6870 bh_result
->b_size
= len
;
6871 bh_result
->b_bdev
= em
->bdev
;
6872 set_buffer_mapped(bh_result
);
6874 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6875 set_buffer_new(bh_result
);
6878 * Need to update the i_size under the extent lock so buffered
6879 * readers will get the updated i_size when we unlock.
6881 if (start
+ len
> i_size_read(inode
))
6882 i_size_write(inode
, start
+ len
);
6884 spin_lock(&BTRFS_I(inode
)->lock
);
6885 BTRFS_I(inode
)->outstanding_extents
++;
6886 spin_unlock(&BTRFS_I(inode
)->lock
);
6888 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6889 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6890 &cached_state
, GFP_NOFS
);
6895 * In the case of write we need to clear and unlock the entire range,
6896 * in the case of read we need to unlock only the end area that we
6897 * aren't using if there is any left over space.
6899 if (lockstart
< lockend
) {
6900 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6901 lockend
, unlock_bits
, 1, 0,
6902 &cached_state
, GFP_NOFS
);
6904 free_extent_state(cached_state
);
6907 free_extent_map(em
);
6912 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6913 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6917 struct btrfs_dio_private
{
6918 struct inode
*inode
;
6924 /* number of bios pending for this dio */
6925 atomic_t pending_bios
;
6930 struct bio
*orig_bio
;
6933 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6935 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6936 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6937 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6938 struct inode
*inode
= dip
->inode
;
6939 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6942 start
= dip
->logical_offset
;
6944 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6945 struct page
*page
= bvec
->bv_page
;
6948 u64
private = ~(u32
)0;
6949 unsigned long flags
;
6951 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6954 local_irq_save(flags
);
6955 kaddr
= kmap_atomic(page
);
6956 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6957 csum
, bvec
->bv_len
);
6958 btrfs_csum_final(csum
, (char *)&csum
);
6959 kunmap_atomic(kaddr
);
6960 local_irq_restore(flags
);
6962 flush_dcache_page(bvec
->bv_page
);
6963 if (csum
!= private) {
6965 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6966 (unsigned long long)btrfs_ino(inode
),
6967 (unsigned long long)start
,
6968 csum
, (unsigned)private);
6973 start
+= bvec
->bv_len
;
6975 } while (bvec
<= bvec_end
);
6977 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6978 dip
->logical_offset
+ dip
->bytes
- 1);
6979 bio
->bi_private
= dip
->private;
6983 /* If we had a csum failure make sure to clear the uptodate flag */
6985 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6986 dio_end_io(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
;
7002 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
7004 ordered_bytes
, !err
);
7008 ordered
->work
.func
= finish_ordered_fn
;
7009 ordered
->work
.flags
= 0;
7010 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
7014 * our bio might span multiple ordered extents. If we haven't
7015 * completed the accounting for the whole dio, go back and try again
7017 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7018 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7024 bio
->bi_private
= dip
->private;
7028 /* If we had an error make sure to clear the uptodate flag */
7030 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
7031 dio_end_io(bio
, err
);
7034 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7035 struct bio
*bio
, int mirror_num
,
7036 unsigned long bio_flags
, u64 offset
)
7039 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7040 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7041 BUG_ON(ret
); /* -ENOMEM */
7045 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7047 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7050 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
7051 "sector %#Lx len %u err no %d\n",
7052 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
7053 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
7057 * before atomic variable goto zero, we must make sure
7058 * dip->errors is perceived to be set.
7060 smp_mb__before_atomic_dec();
7063 /* if there are more bios still pending for this dio, just exit */
7064 if (!atomic_dec_and_test(&dip
->pending_bios
))
7068 bio_io_error(dip
->orig_bio
);
7070 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
7071 bio_endio(dip
->orig_bio
, 0);
7077 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7078 u64 first_sector
, gfp_t gfp_flags
)
7080 int nr_vecs
= bio_get_nr_vecs(bdev
);
7081 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7084 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7085 int rw
, u64 file_offset
, int skip_sum
,
7088 int write
= rw
& REQ_WRITE
;
7089 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7093 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7098 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7106 if (write
&& async_submit
) {
7107 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7108 inode
, rw
, bio
, 0, 0,
7110 __btrfs_submit_bio_start_direct_io
,
7111 __btrfs_submit_bio_done
);
7115 * If we aren't doing async submit, calculate the csum of the
7118 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7121 } else if (!skip_sum
) {
7122 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7128 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7134 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7137 struct inode
*inode
= dip
->inode
;
7138 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7140 struct bio
*orig_bio
= dip
->orig_bio
;
7141 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7142 u64 start_sector
= orig_bio
->bi_sector
;
7143 u64 file_offset
= dip
->logical_offset
;
7148 int async_submit
= 0;
7150 map_length
= orig_bio
->bi_size
;
7151 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7152 &map_length
, NULL
, 0);
7157 if (map_length
>= orig_bio
->bi_size
) {
7162 /* async crcs make it difficult to collect full stripe writes. */
7163 if (btrfs_get_alloc_profile(root
, 1) &
7164 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7169 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7172 bio
->bi_private
= dip
;
7173 bio
->bi_end_io
= btrfs_end_dio_bio
;
7174 atomic_inc(&dip
->pending_bios
);
7176 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7177 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7178 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7179 bvec
->bv_offset
) < bvec
->bv_len
)) {
7181 * inc the count before we submit the bio so
7182 * we know the end IO handler won't happen before
7183 * we inc the count. Otherwise, the dip might get freed
7184 * before we're done setting it up
7186 atomic_inc(&dip
->pending_bios
);
7187 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7188 file_offset
, skip_sum
,
7192 atomic_dec(&dip
->pending_bios
);
7196 start_sector
+= submit_len
>> 9;
7197 file_offset
+= submit_len
;
7202 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7203 start_sector
, GFP_NOFS
);
7206 bio
->bi_private
= dip
;
7207 bio
->bi_end_io
= btrfs_end_dio_bio
;
7209 map_length
= orig_bio
->bi_size
;
7210 ret
= btrfs_map_block(root
->fs_info
, rw
,
7212 &map_length
, NULL
, 0);
7218 submit_len
+= bvec
->bv_len
;
7225 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7234 * before atomic variable goto zero, we must
7235 * make sure dip->errors is perceived to be set.
7237 smp_mb__before_atomic_dec();
7238 if (atomic_dec_and_test(&dip
->pending_bios
))
7239 bio_io_error(dip
->orig_bio
);
7241 /* bio_end_io() will handle error, so we needn't return it */
7245 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
7248 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7249 struct btrfs_dio_private
*dip
;
7250 struct bio_vec
*bvec
= bio
->bi_io_vec
;
7252 int write
= rw
& REQ_WRITE
;
7255 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7257 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7263 dip
->private = bio
->bi_private
;
7265 dip
->logical_offset
= file_offset
;
7269 dip
->bytes
+= bvec
->bv_len
;
7271 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
7273 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
7274 bio
->bi_private
= dip
;
7276 dip
->orig_bio
= bio
;
7277 atomic_set(&dip
->pending_bios
, 0);
7280 bio
->bi_end_io
= btrfs_endio_direct_write
;
7282 bio
->bi_end_io
= btrfs_endio_direct_read
;
7284 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7289 * If this is a write, we need to clean up the reserved space and kill
7290 * the ordered extent.
7293 struct btrfs_ordered_extent
*ordered
;
7294 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7295 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7296 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7297 btrfs_free_reserved_extent(root
, ordered
->start
,
7299 btrfs_put_ordered_extent(ordered
);
7300 btrfs_put_ordered_extent(ordered
);
7302 bio_endio(bio
, ret
);
7305 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7306 const struct iovec
*iov
, loff_t offset
,
7307 unsigned long nr_segs
)
7313 unsigned blocksize_mask
= root
->sectorsize
- 1;
7314 ssize_t retval
= -EINVAL
;
7315 loff_t end
= offset
;
7317 if (offset
& blocksize_mask
)
7320 /* Check the memory alignment. Blocks cannot straddle pages */
7321 for (seg
= 0; seg
< nr_segs
; seg
++) {
7322 addr
= (unsigned long)iov
[seg
].iov_base
;
7323 size
= iov
[seg
].iov_len
;
7325 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7328 /* If this is a write we don't need to check anymore */
7333 * Check to make sure we don't have duplicate iov_base's in this
7334 * iovec, if so return EINVAL, otherwise we'll get csum errors
7335 * when reading back.
7337 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7338 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7347 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7348 const struct iovec
*iov
, loff_t offset
,
7349 unsigned long nr_segs
)
7351 struct file
*file
= iocb
->ki_filp
;
7352 struct inode
*inode
= file
->f_mapping
->host
;
7356 bool relock
= false;
7359 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7363 atomic_inc(&inode
->i_dio_count
);
7364 smp_mb__after_atomic_inc();
7367 count
= iov_length(iov
, nr_segs
);
7369 * If the write DIO is beyond the EOF, we need update
7370 * the isize, but it is protected by i_mutex. So we can
7371 * not unlock the i_mutex at this case.
7373 if (offset
+ count
<= inode
->i_size
) {
7374 mutex_unlock(&inode
->i_mutex
);
7377 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7380 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7381 &BTRFS_I(inode
)->runtime_flags
))) {
7382 inode_dio_done(inode
);
7383 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7387 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7388 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7389 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7390 btrfs_submit_direct
, flags
);
7392 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7393 btrfs_delalloc_release_space(inode
, count
);
7394 else if (ret
>= 0 && (size_t)ret
< count
)
7395 btrfs_delalloc_release_space(inode
,
7396 count
- (size_t)ret
);
7398 btrfs_delalloc_release_metadata(inode
, 0);
7402 inode_dio_done(inode
);
7404 mutex_lock(&inode
->i_mutex
);
7409 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7411 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7412 __u64 start
, __u64 len
)
7416 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7420 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7423 int btrfs_readpage(struct file
*file
, struct page
*page
)
7425 struct extent_io_tree
*tree
;
7426 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7427 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7430 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7432 struct extent_io_tree
*tree
;
7435 if (current
->flags
& PF_MEMALLOC
) {
7436 redirty_page_for_writepage(wbc
, page
);
7440 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7441 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7444 int btrfs_writepages(struct address_space
*mapping
,
7445 struct writeback_control
*wbc
)
7447 struct extent_io_tree
*tree
;
7449 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7450 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7454 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7455 struct list_head
*pages
, unsigned nr_pages
)
7457 struct extent_io_tree
*tree
;
7458 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7459 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7462 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7464 struct extent_io_tree
*tree
;
7465 struct extent_map_tree
*map
;
7468 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7469 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7470 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7472 ClearPagePrivate(page
);
7473 set_page_private(page
, 0);
7474 page_cache_release(page
);
7479 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7481 if (PageWriteback(page
) || PageDirty(page
))
7483 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7486 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7488 struct inode
*inode
= page
->mapping
->host
;
7489 struct extent_io_tree
*tree
;
7490 struct btrfs_ordered_extent
*ordered
;
7491 struct extent_state
*cached_state
= NULL
;
7492 u64 page_start
= page_offset(page
);
7493 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7496 * we have the page locked, so new writeback can't start,
7497 * and the dirty bit won't be cleared while we are here.
7499 * Wait for IO on this page so that we can safely clear
7500 * the PagePrivate2 bit and do ordered accounting
7502 wait_on_page_writeback(page
);
7504 tree
= &BTRFS_I(inode
)->io_tree
;
7506 btrfs_releasepage(page
, GFP_NOFS
);
7509 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7510 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7513 * IO on this page will never be started, so we need
7514 * to account for any ordered extents now
7516 clear_extent_bit(tree
, page_start
, page_end
,
7517 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7518 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7519 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7521 * whoever cleared the private bit is responsible
7522 * for the finish_ordered_io
7524 if (TestClearPagePrivate2(page
) &&
7525 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7526 PAGE_CACHE_SIZE
, 1)) {
7527 btrfs_finish_ordered_io(ordered
);
7529 btrfs_put_ordered_extent(ordered
);
7530 cached_state
= NULL
;
7531 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7533 clear_extent_bit(tree
, page_start
, page_end
,
7534 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7535 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7536 &cached_state
, GFP_NOFS
);
7537 __btrfs_releasepage(page
, GFP_NOFS
);
7539 ClearPageChecked(page
);
7540 if (PagePrivate(page
)) {
7541 ClearPagePrivate(page
);
7542 set_page_private(page
, 0);
7543 page_cache_release(page
);
7548 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7549 * called from a page fault handler when a page is first dirtied. Hence we must
7550 * be careful to check for EOF conditions here. We set the page up correctly
7551 * for a written page which means we get ENOSPC checking when writing into
7552 * holes and correct delalloc and unwritten extent mapping on filesystems that
7553 * support these features.
7555 * We are not allowed to take the i_mutex here so we have to play games to
7556 * protect against truncate races as the page could now be beyond EOF. Because
7557 * vmtruncate() writes the inode size before removing pages, once we have the
7558 * page lock we can determine safely if the page is beyond EOF. If it is not
7559 * beyond EOF, then the page is guaranteed safe against truncation until we
7562 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7564 struct page
*page
= vmf
->page
;
7565 struct inode
*inode
= file_inode(vma
->vm_file
);
7566 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7567 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7568 struct btrfs_ordered_extent
*ordered
;
7569 struct extent_state
*cached_state
= NULL
;
7571 unsigned long zero_start
;
7578 sb_start_pagefault(inode
->i_sb
);
7579 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7581 ret
= file_update_time(vma
->vm_file
);
7587 else /* -ENOSPC, -EIO, etc */
7588 ret
= VM_FAULT_SIGBUS
;
7594 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7597 size
= i_size_read(inode
);
7598 page_start
= page_offset(page
);
7599 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7601 if ((page
->mapping
!= inode
->i_mapping
) ||
7602 (page_start
>= size
)) {
7603 /* page got truncated out from underneath us */
7606 wait_on_page_writeback(page
);
7608 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7609 set_page_extent_mapped(page
);
7612 * we can't set the delalloc bits if there are pending ordered
7613 * extents. Drop our locks and wait for them to finish
7615 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7617 unlock_extent_cached(io_tree
, page_start
, page_end
,
7618 &cached_state
, GFP_NOFS
);
7620 btrfs_start_ordered_extent(inode
, ordered
, 1);
7621 btrfs_put_ordered_extent(ordered
);
7626 * XXX - page_mkwrite gets called every time the page is dirtied, even
7627 * if it was already dirty, so for space accounting reasons we need to
7628 * clear any delalloc bits for the range we are fixing to save. There
7629 * is probably a better way to do this, but for now keep consistent with
7630 * prepare_pages in the normal write path.
7632 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7633 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7634 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7635 0, 0, &cached_state
, GFP_NOFS
);
7637 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7640 unlock_extent_cached(io_tree
, page_start
, page_end
,
7641 &cached_state
, GFP_NOFS
);
7642 ret
= VM_FAULT_SIGBUS
;
7647 /* page is wholly or partially inside EOF */
7648 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7649 zero_start
= size
& ~PAGE_CACHE_MASK
;
7651 zero_start
= PAGE_CACHE_SIZE
;
7653 if (zero_start
!= PAGE_CACHE_SIZE
) {
7655 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7656 flush_dcache_page(page
);
7659 ClearPageChecked(page
);
7660 set_page_dirty(page
);
7661 SetPageUptodate(page
);
7663 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7664 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7665 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7667 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7671 sb_end_pagefault(inode
->i_sb
);
7672 return VM_FAULT_LOCKED
;
7676 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7678 sb_end_pagefault(inode
->i_sb
);
7682 static int btrfs_truncate(struct inode
*inode
)
7684 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7685 struct btrfs_block_rsv
*rsv
;
7688 struct btrfs_trans_handle
*trans
;
7689 u64 mask
= root
->sectorsize
- 1;
7690 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7692 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7696 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7697 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7700 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7701 * 3 things going on here
7703 * 1) We need to reserve space for our orphan item and the space to
7704 * delete our orphan item. Lord knows we don't want to have a dangling
7705 * orphan item because we didn't reserve space to remove it.
7707 * 2) We need to reserve space to update our inode.
7709 * 3) We need to have something to cache all the space that is going to
7710 * be free'd up by the truncate operation, but also have some slack
7711 * space reserved in case it uses space during the truncate (thank you
7712 * very much snapshotting).
7714 * And we need these to all be seperate. The fact is we can use alot of
7715 * space doing the truncate, and we have no earthly idea how much space
7716 * we will use, so we need the truncate reservation to be seperate so it
7717 * doesn't end up using space reserved for updating the inode or
7718 * removing the orphan item. We also need to be able to stop the
7719 * transaction and start a new one, which means we need to be able to
7720 * update the inode several times, and we have no idea of knowing how
7721 * many times that will be, so we can't just reserve 1 item for the
7722 * entirety of the opration, so that has to be done seperately as well.
7723 * Then there is the orphan item, which does indeed need to be held on
7724 * to for the whole operation, and we need nobody to touch this reserved
7725 * space except the orphan code.
7727 * So that leaves us with
7729 * 1) root->orphan_block_rsv - for the orphan deletion.
7730 * 2) rsv - for the truncate reservation, which we will steal from the
7731 * transaction reservation.
7732 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7733 * updating the inode.
7735 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7738 rsv
->size
= min_size
;
7742 * 1 for the truncate slack space
7743 * 1 for updating the inode.
7745 trans
= btrfs_start_transaction(root
, 2);
7746 if (IS_ERR(trans
)) {
7747 err
= PTR_ERR(trans
);
7751 /* Migrate the slack space for the truncate to our reserve */
7752 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7757 * setattr is responsible for setting the ordered_data_close flag,
7758 * but that is only tested during the last file release. That
7759 * could happen well after the next commit, leaving a great big
7760 * window where new writes may get lost if someone chooses to write
7761 * to this file after truncating to zero
7763 * The inode doesn't have any dirty data here, and so if we commit
7764 * this is a noop. If someone immediately starts writing to the inode
7765 * it is very likely we'll catch some of their writes in this
7766 * transaction, and the commit will find this file on the ordered
7767 * data list with good things to send down.
7769 * This is a best effort solution, there is still a window where
7770 * using truncate to replace the contents of the file will
7771 * end up with a zero length file after a crash.
7773 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7774 &BTRFS_I(inode
)->runtime_flags
))
7775 btrfs_add_ordered_operation(trans
, root
, inode
);
7778 * So if we truncate and then write and fsync we normally would just
7779 * write the extents that changed, which is a problem if we need to
7780 * first truncate that entire inode. So set this flag so we write out
7781 * all of the extents in the inode to the sync log so we're completely
7784 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7785 trans
->block_rsv
= rsv
;
7788 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7790 BTRFS_EXTENT_DATA_KEY
);
7791 if (ret
!= -ENOSPC
) {
7796 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7797 ret
= btrfs_update_inode(trans
, root
, inode
);
7803 btrfs_end_transaction(trans
, root
);
7804 btrfs_btree_balance_dirty(root
);
7806 trans
= btrfs_start_transaction(root
, 2);
7807 if (IS_ERR(trans
)) {
7808 ret
= err
= PTR_ERR(trans
);
7813 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7815 BUG_ON(ret
); /* shouldn't happen */
7816 trans
->block_rsv
= rsv
;
7819 if (ret
== 0 && inode
->i_nlink
> 0) {
7820 trans
->block_rsv
= root
->orphan_block_rsv
;
7821 ret
= btrfs_orphan_del(trans
, inode
);
7827 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7828 ret
= btrfs_update_inode(trans
, root
, inode
);
7832 ret
= btrfs_end_transaction(trans
, root
);
7833 btrfs_btree_balance_dirty(root
);
7837 btrfs_free_block_rsv(root
, rsv
);
7846 * create a new subvolume directory/inode (helper for the ioctl).
7848 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7849 struct btrfs_root
*new_root
, u64 new_dirid
)
7851 struct inode
*inode
;
7855 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7856 new_dirid
, new_dirid
,
7857 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7860 return PTR_ERR(inode
);
7861 inode
->i_op
= &btrfs_dir_inode_operations
;
7862 inode
->i_fop
= &btrfs_dir_file_operations
;
7864 set_nlink(inode
, 1);
7865 btrfs_i_size_write(inode
, 0);
7867 err
= btrfs_update_inode(trans
, new_root
, inode
);
7873 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7875 struct btrfs_inode
*ei
;
7876 struct inode
*inode
;
7878 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7885 ei
->last_sub_trans
= 0;
7886 ei
->logged_trans
= 0;
7887 ei
->delalloc_bytes
= 0;
7888 ei
->disk_i_size
= 0;
7891 ei
->index_cnt
= (u64
)-1;
7892 ei
->last_unlink_trans
= 0;
7893 ei
->last_log_commit
= 0;
7895 spin_lock_init(&ei
->lock
);
7896 ei
->outstanding_extents
= 0;
7897 ei
->reserved_extents
= 0;
7899 ei
->runtime_flags
= 0;
7900 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7902 ei
->delayed_node
= NULL
;
7904 inode
= &ei
->vfs_inode
;
7905 extent_map_tree_init(&ei
->extent_tree
);
7906 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7907 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7908 ei
->io_tree
.track_uptodate
= 1;
7909 ei
->io_failure_tree
.track_uptodate
= 1;
7910 atomic_set(&ei
->sync_writers
, 0);
7911 mutex_init(&ei
->log_mutex
);
7912 mutex_init(&ei
->delalloc_mutex
);
7913 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7914 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7915 INIT_LIST_HEAD(&ei
->ordered_operations
);
7916 RB_CLEAR_NODE(&ei
->rb_node
);
7921 static void btrfs_i_callback(struct rcu_head
*head
)
7923 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7924 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7927 void btrfs_destroy_inode(struct inode
*inode
)
7929 struct btrfs_ordered_extent
*ordered
;
7930 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7932 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7933 WARN_ON(inode
->i_data
.nrpages
);
7934 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7935 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7936 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7937 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7940 * This can happen where we create an inode, but somebody else also
7941 * created the same inode and we need to destroy the one we already
7948 * Make sure we're properly removed from the ordered operation
7952 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7953 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7954 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7955 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7958 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7959 &BTRFS_I(inode
)->runtime_flags
)) {
7960 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7961 (unsigned long long)btrfs_ino(inode
));
7962 atomic_dec(&root
->orphan_inodes
);
7966 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7970 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7971 (unsigned long long)ordered
->file_offset
,
7972 (unsigned long long)ordered
->len
);
7973 btrfs_remove_ordered_extent(inode
, ordered
);
7974 btrfs_put_ordered_extent(ordered
);
7975 btrfs_put_ordered_extent(ordered
);
7978 inode_tree_del(inode
);
7979 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7981 btrfs_remove_delayed_node(inode
);
7982 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7985 int btrfs_drop_inode(struct inode
*inode
)
7987 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7989 /* the snap/subvol tree is on deleting */
7990 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7991 root
!= root
->fs_info
->tree_root
)
7994 return generic_drop_inode(inode
);
7997 static void init_once(void *foo
)
7999 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
8001 inode_init_once(&ei
->vfs_inode
);
8004 void btrfs_destroy_cachep(void)
8007 * Make sure all delayed rcu free inodes are flushed before we
8011 if (btrfs_inode_cachep
)
8012 kmem_cache_destroy(btrfs_inode_cachep
);
8013 if (btrfs_trans_handle_cachep
)
8014 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8015 if (btrfs_transaction_cachep
)
8016 kmem_cache_destroy(btrfs_transaction_cachep
);
8017 if (btrfs_path_cachep
)
8018 kmem_cache_destroy(btrfs_path_cachep
);
8019 if (btrfs_free_space_cachep
)
8020 kmem_cache_destroy(btrfs_free_space_cachep
);
8021 if (btrfs_delalloc_work_cachep
)
8022 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8025 int btrfs_init_cachep(void)
8027 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8028 sizeof(struct btrfs_inode
), 0,
8029 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8030 if (!btrfs_inode_cachep
)
8033 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8034 sizeof(struct btrfs_trans_handle
), 0,
8035 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8036 if (!btrfs_trans_handle_cachep
)
8039 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8040 sizeof(struct btrfs_transaction
), 0,
8041 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8042 if (!btrfs_transaction_cachep
)
8045 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8046 sizeof(struct btrfs_path
), 0,
8047 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8048 if (!btrfs_path_cachep
)
8051 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8052 sizeof(struct btrfs_free_space
), 0,
8053 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8054 if (!btrfs_free_space_cachep
)
8057 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8058 sizeof(struct btrfs_delalloc_work
), 0,
8059 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8061 if (!btrfs_delalloc_work_cachep
)
8066 btrfs_destroy_cachep();
8070 static int btrfs_getattr(struct vfsmount
*mnt
,
8071 struct dentry
*dentry
, struct kstat
*stat
)
8074 struct inode
*inode
= dentry
->d_inode
;
8075 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8077 generic_fillattr(inode
, stat
);
8078 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8079 stat
->blksize
= PAGE_CACHE_SIZE
;
8081 spin_lock(&BTRFS_I(inode
)->lock
);
8082 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8083 spin_unlock(&BTRFS_I(inode
)->lock
);
8084 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8085 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8089 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8090 struct inode
*new_dir
, struct dentry
*new_dentry
)
8092 struct btrfs_trans_handle
*trans
;
8093 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8094 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8095 struct inode
*new_inode
= new_dentry
->d_inode
;
8096 struct inode
*old_inode
= old_dentry
->d_inode
;
8097 struct timespec ctime
= CURRENT_TIME
;
8101 u64 old_ino
= btrfs_ino(old_inode
);
8103 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8106 /* we only allow rename subvolume link between subvolumes */
8107 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8110 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8111 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8114 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8115 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8119 /* check for collisions, even if the name isn't there */
8120 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8121 new_dentry
->d_name
.name
,
8122 new_dentry
->d_name
.len
);
8125 if (ret
== -EEXIST
) {
8127 * eexist without a new_inode */
8133 /* maybe -EOVERFLOW */
8140 * we're using rename to replace one file with another.
8141 * and the replacement file is large. Start IO on it now so
8142 * we don't add too much work to the end of the transaction
8144 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8145 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8146 filemap_flush(old_inode
->i_mapping
);
8148 /* close the racy window with snapshot create/destroy ioctl */
8149 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8150 down_read(&root
->fs_info
->subvol_sem
);
8152 * We want to reserve the absolute worst case amount of items. So if
8153 * both inodes are subvols and we need to unlink them then that would
8154 * require 4 item modifications, but if they are both normal inodes it
8155 * would require 5 item modifications, so we'll assume their normal
8156 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8157 * should cover the worst case number of items we'll modify.
8159 trans
= btrfs_start_transaction(root
, 11);
8160 if (IS_ERR(trans
)) {
8161 ret
= PTR_ERR(trans
);
8166 btrfs_record_root_in_trans(trans
, dest
);
8168 ret
= btrfs_set_inode_index(new_dir
, &index
);
8172 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8173 /* force full log commit if subvolume involved. */
8174 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8176 ret
= btrfs_insert_inode_ref(trans
, dest
,
8177 new_dentry
->d_name
.name
,
8178 new_dentry
->d_name
.len
,
8180 btrfs_ino(new_dir
), index
);
8184 * this is an ugly little race, but the rename is required
8185 * to make sure that if we crash, the inode is either at the
8186 * old name or the new one. pinning the log transaction lets
8187 * us make sure we don't allow a log commit to come in after
8188 * we unlink the name but before we add the new name back in.
8190 btrfs_pin_log_trans(root
);
8193 * make sure the inode gets flushed if it is replacing
8196 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8197 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8199 inode_inc_iversion(old_dir
);
8200 inode_inc_iversion(new_dir
);
8201 inode_inc_iversion(old_inode
);
8202 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8203 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8204 old_inode
->i_ctime
= ctime
;
8206 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8207 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8209 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8210 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8211 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8212 old_dentry
->d_name
.name
,
8213 old_dentry
->d_name
.len
);
8215 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8216 old_dentry
->d_inode
,
8217 old_dentry
->d_name
.name
,
8218 old_dentry
->d_name
.len
);
8220 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8223 btrfs_abort_transaction(trans
, root
, ret
);
8228 inode_inc_iversion(new_inode
);
8229 new_inode
->i_ctime
= CURRENT_TIME
;
8230 if (unlikely(btrfs_ino(new_inode
) ==
8231 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8232 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8233 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8235 new_dentry
->d_name
.name
,
8236 new_dentry
->d_name
.len
);
8237 BUG_ON(new_inode
->i_nlink
== 0);
8239 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8240 new_dentry
->d_inode
,
8241 new_dentry
->d_name
.name
,
8242 new_dentry
->d_name
.len
);
8244 if (!ret
&& new_inode
->i_nlink
== 0) {
8245 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8249 btrfs_abort_transaction(trans
, root
, ret
);
8254 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8255 new_dentry
->d_name
.name
,
8256 new_dentry
->d_name
.len
, 0, index
);
8258 btrfs_abort_transaction(trans
, root
, ret
);
8262 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8263 struct dentry
*parent
= new_dentry
->d_parent
;
8264 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8265 btrfs_end_log_trans(root
);
8268 btrfs_end_transaction(trans
, root
);
8270 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8271 up_read(&root
->fs_info
->subvol_sem
);
8276 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8278 struct btrfs_delalloc_work
*delalloc_work
;
8280 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8282 if (delalloc_work
->wait
)
8283 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8285 filemap_flush(delalloc_work
->inode
->i_mapping
);
8287 if (delalloc_work
->delay_iput
)
8288 btrfs_add_delayed_iput(delalloc_work
->inode
);
8290 iput(delalloc_work
->inode
);
8291 complete(&delalloc_work
->completion
);
8294 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8295 int wait
, int delay_iput
)
8297 struct btrfs_delalloc_work
*work
;
8299 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8303 init_completion(&work
->completion
);
8304 INIT_LIST_HEAD(&work
->list
);
8305 work
->inode
= inode
;
8307 work
->delay_iput
= delay_iput
;
8308 work
->work
.func
= btrfs_run_delalloc_work
;
8313 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8315 wait_for_completion(&work
->completion
);
8316 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8320 * some fairly slow code that needs optimization. This walks the list
8321 * of all the inodes with pending delalloc and forces them to disk.
8323 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8325 struct btrfs_inode
*binode
;
8326 struct inode
*inode
;
8327 struct btrfs_delalloc_work
*work
, *next
;
8328 struct list_head works
;
8329 struct list_head splice
;
8332 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8335 INIT_LIST_HEAD(&works
);
8336 INIT_LIST_HEAD(&splice
);
8338 spin_lock(&root
->fs_info
->delalloc_lock
);
8339 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
8340 while (!list_empty(&splice
)) {
8341 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8344 list_del_init(&binode
->delalloc_inodes
);
8346 inode
= igrab(&binode
->vfs_inode
);
8348 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
8349 &binode
->runtime_flags
);
8353 list_add_tail(&binode
->delalloc_inodes
,
8354 &root
->fs_info
->delalloc_inodes
);
8355 spin_unlock(&root
->fs_info
->delalloc_lock
);
8357 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8358 if (unlikely(!work
)) {
8362 list_add_tail(&work
->list
, &works
);
8363 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8367 spin_lock(&root
->fs_info
->delalloc_lock
);
8369 spin_unlock(&root
->fs_info
->delalloc_lock
);
8371 list_for_each_entry_safe(work
, next
, &works
, list
) {
8372 list_del_init(&work
->list
);
8373 btrfs_wait_and_free_delalloc_work(work
);
8376 /* the filemap_flush will queue IO into the worker threads, but
8377 * we have to make sure the IO is actually started and that
8378 * ordered extents get created before we return
8380 atomic_inc(&root
->fs_info
->async_submit_draining
);
8381 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8382 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8383 wait_event(root
->fs_info
->async_submit_wait
,
8384 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8385 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8387 atomic_dec(&root
->fs_info
->async_submit_draining
);
8390 list_for_each_entry_safe(work
, next
, &works
, list
) {
8391 list_del_init(&work
->list
);
8392 btrfs_wait_and_free_delalloc_work(work
);
8395 if (!list_empty_careful(&splice
)) {
8396 spin_lock(&root
->fs_info
->delalloc_lock
);
8397 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
8398 spin_unlock(&root
->fs_info
->delalloc_lock
);
8403 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8404 const char *symname
)
8406 struct btrfs_trans_handle
*trans
;
8407 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8408 struct btrfs_path
*path
;
8409 struct btrfs_key key
;
8410 struct inode
*inode
= NULL
;
8418 struct btrfs_file_extent_item
*ei
;
8419 struct extent_buffer
*leaf
;
8421 name_len
= strlen(symname
) + 1;
8422 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8423 return -ENAMETOOLONG
;
8426 * 2 items for inode item and ref
8427 * 2 items for dir items
8428 * 1 item for xattr if selinux is on
8430 trans
= btrfs_start_transaction(root
, 5);
8432 return PTR_ERR(trans
);
8434 err
= btrfs_find_free_ino(root
, &objectid
);
8438 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8439 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8440 S_IFLNK
|S_IRWXUGO
, &index
);
8441 if (IS_ERR(inode
)) {
8442 err
= PTR_ERR(inode
);
8446 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8453 * If the active LSM wants to access the inode during
8454 * d_instantiate it needs these. Smack checks to see
8455 * if the filesystem supports xattrs by looking at the
8458 inode
->i_fop
= &btrfs_file_operations
;
8459 inode
->i_op
= &btrfs_file_inode_operations
;
8461 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8465 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8466 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8467 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8472 path
= btrfs_alloc_path();
8478 key
.objectid
= btrfs_ino(inode
);
8480 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8481 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8482 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8486 btrfs_free_path(path
);
8489 leaf
= path
->nodes
[0];
8490 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8491 struct btrfs_file_extent_item
);
8492 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8493 btrfs_set_file_extent_type(leaf
, ei
,
8494 BTRFS_FILE_EXTENT_INLINE
);
8495 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8496 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8497 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8498 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8500 ptr
= btrfs_file_extent_inline_start(ei
);
8501 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8502 btrfs_mark_buffer_dirty(leaf
);
8503 btrfs_free_path(path
);
8505 inode
->i_op
= &btrfs_symlink_inode_operations
;
8506 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8507 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8508 inode_set_bytes(inode
, name_len
);
8509 btrfs_i_size_write(inode
, name_len
- 1);
8510 err
= btrfs_update_inode(trans
, root
, inode
);
8516 d_instantiate(dentry
, inode
);
8517 btrfs_end_transaction(trans
, root
);
8519 inode_dec_link_count(inode
);
8522 btrfs_btree_balance_dirty(root
);
8526 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8527 u64 start
, u64 num_bytes
, u64 min_size
,
8528 loff_t actual_len
, u64
*alloc_hint
,
8529 struct btrfs_trans_handle
*trans
)
8531 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8532 struct extent_map
*em
;
8533 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8534 struct btrfs_key ins
;
8535 u64 cur_offset
= start
;
8539 bool own_trans
= true;
8543 while (num_bytes
> 0) {
8545 trans
= btrfs_start_transaction(root
, 3);
8546 if (IS_ERR(trans
)) {
8547 ret
= PTR_ERR(trans
);
8552 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8553 cur_bytes
= max(cur_bytes
, min_size
);
8554 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8555 min_size
, 0, *alloc_hint
, &ins
, 1);
8558 btrfs_end_transaction(trans
, root
);
8562 ret
= insert_reserved_file_extent(trans
, inode
,
8563 cur_offset
, ins
.objectid
,
8564 ins
.offset
, ins
.offset
,
8565 ins
.offset
, 0, 0, 0,
8566 BTRFS_FILE_EXTENT_PREALLOC
);
8568 btrfs_abort_transaction(trans
, root
, ret
);
8570 btrfs_end_transaction(trans
, root
);
8573 btrfs_drop_extent_cache(inode
, cur_offset
,
8574 cur_offset
+ ins
.offset
-1, 0);
8576 em
= alloc_extent_map();
8578 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8579 &BTRFS_I(inode
)->runtime_flags
);
8583 em
->start
= cur_offset
;
8584 em
->orig_start
= cur_offset
;
8585 em
->len
= ins
.offset
;
8586 em
->block_start
= ins
.objectid
;
8587 em
->block_len
= ins
.offset
;
8588 em
->orig_block_len
= ins
.offset
;
8589 em
->ram_bytes
= ins
.offset
;
8590 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8591 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8592 em
->generation
= trans
->transid
;
8595 write_lock(&em_tree
->lock
);
8596 ret
= add_extent_mapping(em_tree
, em
);
8598 list_move(&em
->list
,
8599 &em_tree
->modified_extents
);
8600 write_unlock(&em_tree
->lock
);
8603 btrfs_drop_extent_cache(inode
, cur_offset
,
8604 cur_offset
+ ins
.offset
- 1,
8607 free_extent_map(em
);
8609 num_bytes
-= ins
.offset
;
8610 cur_offset
+= ins
.offset
;
8611 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8613 inode_inc_iversion(inode
);
8614 inode
->i_ctime
= CURRENT_TIME
;
8615 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8616 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8617 (actual_len
> inode
->i_size
) &&
8618 (cur_offset
> inode
->i_size
)) {
8619 if (cur_offset
> actual_len
)
8620 i_size
= actual_len
;
8622 i_size
= cur_offset
;
8623 i_size_write(inode
, i_size
);
8624 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8627 ret
= btrfs_update_inode(trans
, root
, inode
);
8630 btrfs_abort_transaction(trans
, root
, ret
);
8632 btrfs_end_transaction(trans
, root
);
8637 btrfs_end_transaction(trans
, root
);
8642 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8643 u64 start
, u64 num_bytes
, u64 min_size
,
8644 loff_t actual_len
, u64
*alloc_hint
)
8646 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8647 min_size
, actual_len
, alloc_hint
,
8651 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8652 struct btrfs_trans_handle
*trans
, int mode
,
8653 u64 start
, u64 num_bytes
, u64 min_size
,
8654 loff_t actual_len
, u64
*alloc_hint
)
8656 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8657 min_size
, actual_len
, alloc_hint
, trans
);
8660 static int btrfs_set_page_dirty(struct page
*page
)
8662 return __set_page_dirty_nobuffers(page
);
8665 static int btrfs_permission(struct inode
*inode
, int mask
)
8667 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8668 umode_t mode
= inode
->i_mode
;
8670 if (mask
& MAY_WRITE
&&
8671 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8672 if (btrfs_root_readonly(root
))
8674 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8677 return generic_permission(inode
, mask
);
8680 static const struct inode_operations btrfs_dir_inode_operations
= {
8681 .getattr
= btrfs_getattr
,
8682 .lookup
= btrfs_lookup
,
8683 .create
= btrfs_create
,
8684 .unlink
= btrfs_unlink
,
8686 .mkdir
= btrfs_mkdir
,
8687 .rmdir
= btrfs_rmdir
,
8688 .rename
= btrfs_rename
,
8689 .symlink
= btrfs_symlink
,
8690 .setattr
= btrfs_setattr
,
8691 .mknod
= btrfs_mknod
,
8692 .setxattr
= btrfs_setxattr
,
8693 .getxattr
= btrfs_getxattr
,
8694 .listxattr
= btrfs_listxattr
,
8695 .removexattr
= btrfs_removexattr
,
8696 .permission
= btrfs_permission
,
8697 .get_acl
= btrfs_get_acl
,
8699 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8700 .lookup
= btrfs_lookup
,
8701 .permission
= btrfs_permission
,
8702 .get_acl
= btrfs_get_acl
,
8705 static const struct file_operations btrfs_dir_file_operations
= {
8706 .llseek
= generic_file_llseek
,
8707 .read
= generic_read_dir
,
8708 .readdir
= btrfs_real_readdir
,
8709 .unlocked_ioctl
= btrfs_ioctl
,
8710 #ifdef CONFIG_COMPAT
8711 .compat_ioctl
= btrfs_ioctl
,
8713 .release
= btrfs_release_file
,
8714 .fsync
= btrfs_sync_file
,
8717 static struct extent_io_ops btrfs_extent_io_ops
= {
8718 .fill_delalloc
= run_delalloc_range
,
8719 .submit_bio_hook
= btrfs_submit_bio_hook
,
8720 .merge_bio_hook
= btrfs_merge_bio_hook
,
8721 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8722 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8723 .writepage_start_hook
= btrfs_writepage_start_hook
,
8724 .set_bit_hook
= btrfs_set_bit_hook
,
8725 .clear_bit_hook
= btrfs_clear_bit_hook
,
8726 .merge_extent_hook
= btrfs_merge_extent_hook
,
8727 .split_extent_hook
= btrfs_split_extent_hook
,
8731 * btrfs doesn't support the bmap operation because swapfiles
8732 * use bmap to make a mapping of extents in the file. They assume
8733 * these extents won't change over the life of the file and they
8734 * use the bmap result to do IO directly to the drive.
8736 * the btrfs bmap call would return logical addresses that aren't
8737 * suitable for IO and they also will change frequently as COW
8738 * operations happen. So, swapfile + btrfs == corruption.
8740 * For now we're avoiding this by dropping bmap.
8742 static const struct address_space_operations btrfs_aops
= {
8743 .readpage
= btrfs_readpage
,
8744 .writepage
= btrfs_writepage
,
8745 .writepages
= btrfs_writepages
,
8746 .readpages
= btrfs_readpages
,
8747 .direct_IO
= btrfs_direct_IO
,
8748 .invalidatepage
= btrfs_invalidatepage
,
8749 .releasepage
= btrfs_releasepage
,
8750 .set_page_dirty
= btrfs_set_page_dirty
,
8751 .error_remove_page
= generic_error_remove_page
,
8754 static const struct address_space_operations btrfs_symlink_aops
= {
8755 .readpage
= btrfs_readpage
,
8756 .writepage
= btrfs_writepage
,
8757 .invalidatepage
= btrfs_invalidatepage
,
8758 .releasepage
= btrfs_releasepage
,
8761 static const struct inode_operations btrfs_file_inode_operations
= {
8762 .getattr
= btrfs_getattr
,
8763 .setattr
= btrfs_setattr
,
8764 .setxattr
= btrfs_setxattr
,
8765 .getxattr
= btrfs_getxattr
,
8766 .listxattr
= btrfs_listxattr
,
8767 .removexattr
= btrfs_removexattr
,
8768 .permission
= btrfs_permission
,
8769 .fiemap
= btrfs_fiemap
,
8770 .get_acl
= btrfs_get_acl
,
8771 .update_time
= btrfs_update_time
,
8773 static const struct inode_operations btrfs_special_inode_operations
= {
8774 .getattr
= btrfs_getattr
,
8775 .setattr
= btrfs_setattr
,
8776 .permission
= btrfs_permission
,
8777 .setxattr
= btrfs_setxattr
,
8778 .getxattr
= btrfs_getxattr
,
8779 .listxattr
= btrfs_listxattr
,
8780 .removexattr
= btrfs_removexattr
,
8781 .get_acl
= btrfs_get_acl
,
8782 .update_time
= btrfs_update_time
,
8784 static const struct inode_operations btrfs_symlink_inode_operations
= {
8785 .readlink
= generic_readlink
,
8786 .follow_link
= page_follow_link_light
,
8787 .put_link
= page_put_link
,
8788 .getattr
= btrfs_getattr
,
8789 .setattr
= btrfs_setattr
,
8790 .permission
= btrfs_permission
,
8791 .setxattr
= btrfs_setxattr
,
8792 .getxattr
= btrfs_getxattr
,
8793 .listxattr
= btrfs_listxattr
,
8794 .removexattr
= btrfs_removexattr
,
8795 .get_acl
= btrfs_get_acl
,
8796 .update_time
= btrfs_update_time
,
8799 const struct dentry_operations btrfs_dentry_operations
= {
8800 .d_delete
= btrfs_dentry_delete
,
8801 .d_release
= btrfs_dentry_release
,