2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/aio.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/xattr.h>
38 #include <linux/posix_acl.h>
39 #include <linux/falloc.h>
40 #include <linux/slab.h>
41 #include <linux/ratelimit.h>
42 #include <linux/mount.h>
43 #include <linux/btrfs.h>
44 #include <linux/blkdev.h>
45 #include <linux/posix_acl_xattr.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
51 #include "ordered-data.h"
55 #include "compression.h"
57 #include "free-space-cache.h"
58 #include "inode-map.h"
63 struct btrfs_iget_args
{
64 struct btrfs_key
*location
;
65 struct btrfs_root
*root
;
68 static const struct inode_operations btrfs_dir_inode_operations
;
69 static const struct inode_operations btrfs_symlink_inode_operations
;
70 static const struct inode_operations btrfs_dir_ro_inode_operations
;
71 static const struct inode_operations btrfs_special_inode_operations
;
72 static const struct inode_operations btrfs_file_inode_operations
;
73 static const struct address_space_operations btrfs_aops
;
74 static const struct address_space_operations btrfs_symlink_aops
;
75 static const struct file_operations btrfs_dir_file_operations
;
76 static struct extent_io_ops btrfs_extent_io_ops
;
78 static struct kmem_cache
*btrfs_inode_cachep
;
79 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
80 struct kmem_cache
*btrfs_trans_handle_cachep
;
81 struct kmem_cache
*btrfs_transaction_cachep
;
82 struct kmem_cache
*btrfs_path_cachep
;
83 struct kmem_cache
*btrfs_free_space_cachep
;
86 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
87 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
88 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
89 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
90 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
91 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
92 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
93 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
96 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
97 static int btrfs_truncate(struct inode
*inode
);
98 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
99 static noinline
int cow_file_range(struct inode
*inode
,
100 struct page
*locked_page
,
101 u64 start
, u64 end
, int *page_started
,
102 unsigned long *nr_written
, int unlock
);
103 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
104 u64 len
, u64 orig_start
,
105 u64 block_start
, u64 block_len
,
106 u64 orig_block_len
, u64 ram_bytes
,
109 static int btrfs_dirty_inode(struct inode
*inode
);
111 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
112 struct inode
*inode
, struct inode
*dir
,
113 const struct qstr
*qstr
)
117 err
= btrfs_init_acl(trans
, inode
, dir
);
119 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
128 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
129 struct btrfs_path
*path
, int extent_inserted
,
130 struct btrfs_root
*root
, struct inode
*inode
,
131 u64 start
, size_t size
, size_t compressed_size
,
133 struct page
**compressed_pages
)
135 struct extent_buffer
*leaf
;
136 struct page
*page
= NULL
;
139 struct btrfs_file_extent_item
*ei
;
142 size_t cur_size
= size
;
143 unsigned long offset
;
145 if (compressed_size
&& compressed_pages
)
146 cur_size
= compressed_size
;
148 inode_add_bytes(inode
, size
);
150 if (!extent_inserted
) {
151 struct btrfs_key key
;
154 key
.objectid
= btrfs_ino(inode
);
156 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
158 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
159 path
->leave_spinning
= 1;
160 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
167 leaf
= path
->nodes
[0];
168 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
169 struct btrfs_file_extent_item
);
170 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
171 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
172 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
173 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
174 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
175 ptr
= btrfs_file_extent_inline_start(ei
);
177 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
180 while (compressed_size
> 0) {
181 cpage
= compressed_pages
[i
];
182 cur_size
= min_t(unsigned long, compressed_size
,
185 kaddr
= kmap_atomic(cpage
);
186 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
187 kunmap_atomic(kaddr
);
191 compressed_size
-= cur_size
;
193 btrfs_set_file_extent_compression(leaf
, ei
,
196 page
= find_get_page(inode
->i_mapping
,
197 start
>> PAGE_CACHE_SHIFT
);
198 btrfs_set_file_extent_compression(leaf
, ei
, 0);
199 kaddr
= kmap_atomic(page
);
200 offset
= start
& (PAGE_CACHE_SIZE
- 1);
201 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
202 kunmap_atomic(kaddr
);
203 page_cache_release(page
);
205 btrfs_mark_buffer_dirty(leaf
);
206 btrfs_release_path(path
);
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
217 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
218 ret
= btrfs_update_inode(trans
, root
, inode
);
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
231 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
232 struct inode
*inode
, u64 start
,
233 u64 end
, size_t compressed_size
,
235 struct page
**compressed_pages
)
237 struct btrfs_trans_handle
*trans
;
238 u64 isize
= i_size_read(inode
);
239 u64 actual_end
= min(end
+ 1, isize
);
240 u64 inline_len
= actual_end
- start
;
241 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
242 u64 data_len
= inline_len
;
244 struct btrfs_path
*path
;
245 int extent_inserted
= 0;
246 u32 extent_item_size
;
249 data_len
= compressed_size
;
252 actual_end
>= PAGE_CACHE_SIZE
||
253 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
255 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
257 data_len
> root
->fs_info
->max_inline
) {
261 path
= btrfs_alloc_path();
265 trans
= btrfs_join_transaction(root
);
267 btrfs_free_path(path
);
268 return PTR_ERR(trans
);
270 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
272 if (compressed_size
&& compressed_pages
)
273 extent_item_size
= btrfs_file_extent_calc_inline_size(
276 extent_item_size
= btrfs_file_extent_calc_inline_size(
279 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
280 start
, aligned_end
, NULL
,
281 1, 1, extent_item_size
, &extent_inserted
);
283 btrfs_abort_transaction(trans
, root
, ret
);
287 if (isize
> actual_end
)
288 inline_len
= min_t(u64
, isize
, actual_end
);
289 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
291 inline_len
, compressed_size
,
292 compress_type
, compressed_pages
);
293 if (ret
&& ret
!= -ENOSPC
) {
294 btrfs_abort_transaction(trans
, root
, ret
);
296 } else if (ret
== -ENOSPC
) {
301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
302 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
303 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
305 btrfs_free_path(path
);
306 btrfs_end_transaction(trans
, root
);
310 struct async_extent
{
315 unsigned long nr_pages
;
317 struct list_head list
;
322 struct btrfs_root
*root
;
323 struct page
*locked_page
;
326 struct list_head extents
;
327 struct btrfs_work work
;
330 static noinline
int add_async_extent(struct async_cow
*cow
,
331 u64 start
, u64 ram_size
,
334 unsigned long nr_pages
,
337 struct async_extent
*async_extent
;
339 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
340 BUG_ON(!async_extent
); /* -ENOMEM */
341 async_extent
->start
= start
;
342 async_extent
->ram_size
= ram_size
;
343 async_extent
->compressed_size
= compressed_size
;
344 async_extent
->pages
= pages
;
345 async_extent
->nr_pages
= nr_pages
;
346 async_extent
->compress_type
= compress_type
;
347 list_add_tail(&async_extent
->list
, &cow
->extents
);
352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
365 * are written in the same order that the flusher thread sent them
368 static noinline
int compress_file_range(struct inode
*inode
,
369 struct page
*locked_page
,
371 struct async_cow
*async_cow
,
374 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
376 u64 blocksize
= root
->sectorsize
;
378 u64 isize
= i_size_read(inode
);
380 struct page
**pages
= NULL
;
381 unsigned long nr_pages
;
382 unsigned long nr_pages_ret
= 0;
383 unsigned long total_compressed
= 0;
384 unsigned long total_in
= 0;
385 unsigned long max_compressed
= 128 * 1024;
386 unsigned long max_uncompressed
= 128 * 1024;
389 int compress_type
= root
->fs_info
->compress_type
;
392 /* if this is a small write inside eof, kick off a defrag */
393 if ((end
- start
+ 1) < 16 * 1024 &&
394 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
395 btrfs_add_inode_defrag(NULL
, inode
);
398 * skip compression for a small file range(<=blocksize) that
399 * isn't an inline extent, since it dosen't save disk space at all.
401 if ((end
- start
+ 1) <= blocksize
&&
402 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
403 goto cleanup_and_bail_uncompressed
;
405 actual_end
= min_t(u64
, isize
, end
+ 1);
408 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
409 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
412 * we don't want to send crud past the end of i_size through
413 * compression, that's just a waste of CPU time. So, if the
414 * end of the file is before the start of our current
415 * requested range of bytes, we bail out to the uncompressed
416 * cleanup code that can deal with all of this.
418 * It isn't really the fastest way to fix things, but this is a
419 * very uncommon corner.
421 if (actual_end
<= start
)
422 goto cleanup_and_bail_uncompressed
;
424 total_compressed
= actual_end
- start
;
426 /* we want to make sure that amount of ram required to uncompress
427 * an extent is reasonable, so we limit the total size in ram
428 * of a compressed extent to 128k. This is a crucial number
429 * because it also controls how easily we can spread reads across
430 * cpus for decompression.
432 * We also want to make sure the amount of IO required to do
433 * a random read is reasonably small, so we limit the size of
434 * a compressed extent to 128k.
436 total_compressed
= min(total_compressed
, max_uncompressed
);
437 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
438 num_bytes
= max(blocksize
, num_bytes
);
443 * we do compression for mount -o compress and when the
444 * inode has not been flagged as nocompress. This flag can
445 * change at any time if we discover bad compression ratios.
447 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
448 (btrfs_test_opt(root
, COMPRESS
) ||
449 (BTRFS_I(inode
)->force_compress
) ||
450 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
452 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
454 /* just bail out to the uncompressed code */
458 if (BTRFS_I(inode
)->force_compress
)
459 compress_type
= BTRFS_I(inode
)->force_compress
;
462 * we need to call clear_page_dirty_for_io on each
463 * page in the range. Otherwise applications with the file
464 * mmap'd can wander in and change the page contents while
465 * we are compressing them.
467 * If the compression fails for any reason, we set the pages
468 * dirty again later on.
470 extent_range_clear_dirty_for_io(inode
, start
, end
);
472 ret
= btrfs_compress_pages(compress_type
,
473 inode
->i_mapping
, start
,
474 total_compressed
, pages
,
475 nr_pages
, &nr_pages_ret
,
481 unsigned long offset
= total_compressed
&
482 (PAGE_CACHE_SIZE
- 1);
483 struct page
*page
= pages
[nr_pages_ret
- 1];
486 /* zero the tail end of the last page, we might be
487 * sending it down to disk
490 kaddr
= kmap_atomic(page
);
491 memset(kaddr
+ offset
, 0,
492 PAGE_CACHE_SIZE
- offset
);
493 kunmap_atomic(kaddr
);
500 /* lets try to make an inline extent */
501 if (ret
|| total_in
< (actual_end
- start
)) {
502 /* we didn't compress the entire range, try
503 * to make an uncompressed inline extent.
505 ret
= cow_file_range_inline(root
, inode
, start
, end
,
508 /* try making a compressed inline extent */
509 ret
= cow_file_range_inline(root
, inode
, start
, end
,
511 compress_type
, pages
);
514 unsigned long clear_flags
= EXTENT_DELALLOC
|
516 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
519 * inline extent creation worked or returned error,
520 * we don't need to create any more async work items.
521 * Unlock and free up our temp pages.
523 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
524 clear_flags
, PAGE_UNLOCK
|
534 * we aren't doing an inline extent round the compressed size
535 * up to a block size boundary so the allocator does sane
538 total_compressed
= ALIGN(total_compressed
, blocksize
);
541 * one last check to make sure the compression is really a
542 * win, compare the page count read with the blocks on disk
544 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
545 if (total_compressed
>= total_in
) {
548 num_bytes
= total_in
;
551 if (!will_compress
&& pages
) {
553 * the compression code ran but failed to make things smaller,
554 * free any pages it allocated and our page pointer array
556 for (i
= 0; i
< nr_pages_ret
; i
++) {
557 WARN_ON(pages
[i
]->mapping
);
558 page_cache_release(pages
[i
]);
562 total_compressed
= 0;
565 /* flag the file so we don't compress in the future */
566 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
567 !(BTRFS_I(inode
)->force_compress
)) {
568 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
574 /* the async work queues will take care of doing actual
575 * allocation on disk for these compressed pages,
576 * and will submit them to the elevator.
578 add_async_extent(async_cow
, start
, num_bytes
,
579 total_compressed
, pages
, nr_pages_ret
,
582 if (start
+ num_bytes
< end
) {
589 cleanup_and_bail_uncompressed
:
591 * No compression, but we still need to write the pages in
592 * the file we've been given so far. redirty the locked
593 * page if it corresponds to our extent and set things up
594 * for the async work queue to run cow_file_range to do
595 * the normal delalloc dance
597 if (page_offset(locked_page
) >= start
&&
598 page_offset(locked_page
) <= end
) {
599 __set_page_dirty_nobuffers(locked_page
);
600 /* unlocked later on in the async handlers */
603 extent_range_redirty_for_io(inode
, start
, end
);
604 add_async_extent(async_cow
, start
, end
- start
+ 1,
605 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
613 for (i
= 0; i
< nr_pages_ret
; i
++) {
614 WARN_ON(pages
[i
]->mapping
);
615 page_cache_release(pages
[i
]);
623 * phase two of compressed writeback. This is the ordered portion
624 * of the code, which only gets called in the order the work was
625 * queued. We walk all the async extents created by compress_file_range
626 * and send them down to the disk.
628 static noinline
int submit_compressed_extents(struct inode
*inode
,
629 struct async_cow
*async_cow
)
631 struct async_extent
*async_extent
;
633 struct btrfs_key ins
;
634 struct extent_map
*em
;
635 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
636 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
637 struct extent_io_tree
*io_tree
;
640 if (list_empty(&async_cow
->extents
))
644 while (!list_empty(&async_cow
->extents
)) {
645 async_extent
= list_entry(async_cow
->extents
.next
,
646 struct async_extent
, list
);
647 list_del(&async_extent
->list
);
649 io_tree
= &BTRFS_I(inode
)->io_tree
;
652 /* did the compression code fall back to uncompressed IO? */
653 if (!async_extent
->pages
) {
654 int page_started
= 0;
655 unsigned long nr_written
= 0;
657 lock_extent(io_tree
, async_extent
->start
,
658 async_extent
->start
+
659 async_extent
->ram_size
- 1);
661 /* allocate blocks */
662 ret
= cow_file_range(inode
, async_cow
->locked_page
,
664 async_extent
->start
+
665 async_extent
->ram_size
- 1,
666 &page_started
, &nr_written
, 0);
671 * if page_started, cow_file_range inserted an
672 * inline extent and took care of all the unlocking
673 * and IO for us. Otherwise, we need to submit
674 * all those pages down to the drive.
676 if (!page_started
&& !ret
)
677 extent_write_locked_range(io_tree
,
678 inode
, async_extent
->start
,
679 async_extent
->start
+
680 async_extent
->ram_size
- 1,
684 unlock_page(async_cow
->locked_page
);
690 lock_extent(io_tree
, async_extent
->start
,
691 async_extent
->start
+ async_extent
->ram_size
- 1);
693 ret
= btrfs_reserve_extent(root
,
694 async_extent
->compressed_size
,
695 async_extent
->compressed_size
,
696 0, alloc_hint
, &ins
, 1, 1);
700 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
701 WARN_ON(async_extent
->pages
[i
]->mapping
);
702 page_cache_release(async_extent
->pages
[i
]);
704 kfree(async_extent
->pages
);
705 async_extent
->nr_pages
= 0;
706 async_extent
->pages
= NULL
;
708 if (ret
== -ENOSPC
) {
709 unlock_extent(io_tree
, async_extent
->start
,
710 async_extent
->start
+
711 async_extent
->ram_size
- 1);
714 * we need to redirty the pages if we decide to
715 * fallback to uncompressed IO, otherwise we
716 * will not submit these pages down to lower
719 extent_range_redirty_for_io(inode
,
721 async_extent
->start
+
722 async_extent
->ram_size
- 1);
730 * here we're doing allocation and writeback of the
733 btrfs_drop_extent_cache(inode
, async_extent
->start
,
734 async_extent
->start
+
735 async_extent
->ram_size
- 1, 0);
737 em
= alloc_extent_map();
740 goto out_free_reserve
;
742 em
->start
= async_extent
->start
;
743 em
->len
= async_extent
->ram_size
;
744 em
->orig_start
= em
->start
;
745 em
->mod_start
= em
->start
;
746 em
->mod_len
= em
->len
;
748 em
->block_start
= ins
.objectid
;
749 em
->block_len
= ins
.offset
;
750 em
->orig_block_len
= ins
.offset
;
751 em
->ram_bytes
= async_extent
->ram_size
;
752 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
753 em
->compress_type
= async_extent
->compress_type
;
754 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
755 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
759 write_lock(&em_tree
->lock
);
760 ret
= add_extent_mapping(em_tree
, em
, 1);
761 write_unlock(&em_tree
->lock
);
762 if (ret
!= -EEXIST
) {
766 btrfs_drop_extent_cache(inode
, async_extent
->start
,
767 async_extent
->start
+
768 async_extent
->ram_size
- 1, 0);
772 goto out_free_reserve
;
774 ret
= btrfs_add_ordered_extent_compress(inode
,
777 async_extent
->ram_size
,
779 BTRFS_ORDERED_COMPRESSED
,
780 async_extent
->compress_type
);
782 goto out_free_reserve
;
785 * clear dirty, set writeback and unlock the pages.
787 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
788 async_extent
->start
+
789 async_extent
->ram_size
- 1,
790 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
791 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
793 ret
= btrfs_submit_compressed_write(inode
,
795 async_extent
->ram_size
,
797 ins
.offset
, async_extent
->pages
,
798 async_extent
->nr_pages
);
799 alloc_hint
= ins
.objectid
+ ins
.offset
;
809 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
811 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
812 async_extent
->start
+
813 async_extent
->ram_size
- 1,
814 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
815 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
816 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
817 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
822 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
825 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
826 struct extent_map
*em
;
829 read_lock(&em_tree
->lock
);
830 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
833 * if block start isn't an actual block number then find the
834 * first block in this inode and use that as a hint. If that
835 * block is also bogus then just don't worry about it.
837 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
839 em
= search_extent_mapping(em_tree
, 0, 0);
840 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
841 alloc_hint
= em
->block_start
;
845 alloc_hint
= em
->block_start
;
849 read_unlock(&em_tree
->lock
);
855 * when extent_io.c finds a delayed allocation range in the file,
856 * the call backs end up in this code. The basic idea is to
857 * allocate extents on disk for the range, and create ordered data structs
858 * in ram to track those extents.
860 * locked_page is the page that writepage had locked already. We use
861 * it to make sure we don't do extra locks or unlocks.
863 * *page_started is set to one if we unlock locked_page and do everything
864 * required to start IO on it. It may be clean and already done with
867 static noinline
int cow_file_range(struct inode
*inode
,
868 struct page
*locked_page
,
869 u64 start
, u64 end
, int *page_started
,
870 unsigned long *nr_written
,
873 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
876 unsigned long ram_size
;
879 u64 blocksize
= root
->sectorsize
;
880 struct btrfs_key ins
;
881 struct extent_map
*em
;
882 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
885 if (btrfs_is_free_space_inode(inode
)) {
891 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
892 num_bytes
= max(blocksize
, num_bytes
);
893 disk_num_bytes
= num_bytes
;
895 /* if this is a small write inside eof, kick off defrag */
896 if (num_bytes
< 64 * 1024 &&
897 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
898 btrfs_add_inode_defrag(NULL
, inode
);
901 /* lets try to make an inline extent */
902 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
905 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
906 EXTENT_LOCKED
| EXTENT_DELALLOC
|
907 EXTENT_DEFRAG
, PAGE_UNLOCK
|
908 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
911 *nr_written
= *nr_written
+
912 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
915 } else if (ret
< 0) {
920 BUG_ON(disk_num_bytes
>
921 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
923 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
924 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
926 while (disk_num_bytes
> 0) {
929 cur_alloc_size
= disk_num_bytes
;
930 ret
= btrfs_reserve_extent(root
, cur_alloc_size
,
931 root
->sectorsize
, 0, alloc_hint
,
936 em
= alloc_extent_map();
942 em
->orig_start
= em
->start
;
943 ram_size
= ins
.offset
;
944 em
->len
= ins
.offset
;
945 em
->mod_start
= em
->start
;
946 em
->mod_len
= em
->len
;
948 em
->block_start
= ins
.objectid
;
949 em
->block_len
= ins
.offset
;
950 em
->orig_block_len
= ins
.offset
;
951 em
->ram_bytes
= ram_size
;
952 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
953 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
957 write_lock(&em_tree
->lock
);
958 ret
= add_extent_mapping(em_tree
, em
, 1);
959 write_unlock(&em_tree
->lock
);
960 if (ret
!= -EEXIST
) {
964 btrfs_drop_extent_cache(inode
, start
,
965 start
+ ram_size
- 1, 0);
970 cur_alloc_size
= ins
.offset
;
971 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
972 ram_size
, cur_alloc_size
, 0);
976 if (root
->root_key
.objectid
==
977 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
978 ret
= btrfs_reloc_clone_csums(inode
, start
,
984 if (disk_num_bytes
< cur_alloc_size
)
987 /* we're not doing compressed IO, don't unlock the first
988 * page (which the caller expects to stay locked), don't
989 * clear any dirty bits and don't set any writeback bits
991 * Do set the Private2 bit so we know this page was properly
992 * setup for writepage
994 op
= unlock
? PAGE_UNLOCK
: 0;
995 op
|= PAGE_SET_PRIVATE2
;
997 extent_clear_unlock_delalloc(inode
, start
,
998 start
+ ram_size
- 1, locked_page
,
999 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1001 disk_num_bytes
-= cur_alloc_size
;
1002 num_bytes
-= cur_alloc_size
;
1003 alloc_hint
= ins
.objectid
+ ins
.offset
;
1004 start
+= cur_alloc_size
;
1010 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
1012 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1013 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1014 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1015 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1016 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1021 * work queue call back to started compression on a file and pages
1023 static noinline
void async_cow_start(struct btrfs_work
*work
)
1025 struct async_cow
*async_cow
;
1027 async_cow
= container_of(work
, struct async_cow
, work
);
1029 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1030 async_cow
->start
, async_cow
->end
, async_cow
,
1032 if (num_added
== 0) {
1033 btrfs_add_delayed_iput(async_cow
->inode
);
1034 async_cow
->inode
= NULL
;
1039 * work queue call back to submit previously compressed pages
1041 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1043 struct async_cow
*async_cow
;
1044 struct btrfs_root
*root
;
1045 unsigned long nr_pages
;
1047 async_cow
= container_of(work
, struct async_cow
, work
);
1049 root
= async_cow
->root
;
1050 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1053 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1055 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1056 wake_up(&root
->fs_info
->async_submit_wait
);
1058 if (async_cow
->inode
)
1059 submit_compressed_extents(async_cow
->inode
, async_cow
);
1062 static noinline
void async_cow_free(struct btrfs_work
*work
)
1064 struct async_cow
*async_cow
;
1065 async_cow
= container_of(work
, struct async_cow
, work
);
1066 if (async_cow
->inode
)
1067 btrfs_add_delayed_iput(async_cow
->inode
);
1071 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1072 u64 start
, u64 end
, int *page_started
,
1073 unsigned long *nr_written
)
1075 struct async_cow
*async_cow
;
1076 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1077 unsigned long nr_pages
;
1079 int limit
= 10 * 1024 * 1024;
1081 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1082 1, 0, NULL
, GFP_NOFS
);
1083 while (start
< end
) {
1084 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1085 BUG_ON(!async_cow
); /* -ENOMEM */
1086 async_cow
->inode
= igrab(inode
);
1087 async_cow
->root
= root
;
1088 async_cow
->locked_page
= locked_page
;
1089 async_cow
->start
= start
;
1091 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1094 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1096 async_cow
->end
= cur_end
;
1097 INIT_LIST_HEAD(&async_cow
->extents
);
1099 btrfs_init_work(&async_cow
->work
, async_cow_start
,
1100 async_cow_submit
, async_cow_free
);
1102 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1104 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1106 btrfs_queue_work(root
->fs_info
->delalloc_workers
,
1109 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1110 wait_event(root
->fs_info
->async_submit_wait
,
1111 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1115 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1116 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1117 wait_event(root
->fs_info
->async_submit_wait
,
1118 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1122 *nr_written
+= nr_pages
;
1123 start
= cur_end
+ 1;
1129 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1130 u64 bytenr
, u64 num_bytes
)
1133 struct btrfs_ordered_sum
*sums
;
1136 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1137 bytenr
+ num_bytes
- 1, &list
, 0);
1138 if (ret
== 0 && list_empty(&list
))
1141 while (!list_empty(&list
)) {
1142 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1143 list_del(&sums
->list
);
1150 * when nowcow writeback call back. This checks for snapshots or COW copies
1151 * of the extents that exist in the file, and COWs the file as required.
1153 * If no cow copies or snapshots exist, we write directly to the existing
1156 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1157 struct page
*locked_page
,
1158 u64 start
, u64 end
, int *page_started
, int force
,
1159 unsigned long *nr_written
)
1161 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1162 struct btrfs_trans_handle
*trans
;
1163 struct extent_buffer
*leaf
;
1164 struct btrfs_path
*path
;
1165 struct btrfs_file_extent_item
*fi
;
1166 struct btrfs_key found_key
;
1181 u64 ino
= btrfs_ino(inode
);
1183 path
= btrfs_alloc_path();
1185 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1186 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1187 EXTENT_DO_ACCOUNTING
|
1188 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1190 PAGE_SET_WRITEBACK
|
1191 PAGE_END_WRITEBACK
);
1195 nolock
= btrfs_is_free_space_inode(inode
);
1198 trans
= btrfs_join_transaction_nolock(root
);
1200 trans
= btrfs_join_transaction(root
);
1202 if (IS_ERR(trans
)) {
1203 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1204 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1205 EXTENT_DO_ACCOUNTING
|
1206 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1208 PAGE_SET_WRITEBACK
|
1209 PAGE_END_WRITEBACK
);
1210 btrfs_free_path(path
);
1211 return PTR_ERR(trans
);
1214 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1216 cow_start
= (u64
)-1;
1219 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1223 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1224 leaf
= path
->nodes
[0];
1225 btrfs_item_key_to_cpu(leaf
, &found_key
,
1226 path
->slots
[0] - 1);
1227 if (found_key
.objectid
== ino
&&
1228 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1233 leaf
= path
->nodes
[0];
1234 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1235 ret
= btrfs_next_leaf(root
, path
);
1240 leaf
= path
->nodes
[0];
1246 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1248 if (found_key
.objectid
> ino
||
1249 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1250 found_key
.offset
> end
)
1253 if (found_key
.offset
> cur_offset
) {
1254 extent_end
= found_key
.offset
;
1259 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1260 struct btrfs_file_extent_item
);
1261 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1263 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1264 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1265 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1266 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1267 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1268 extent_end
= found_key
.offset
+
1269 btrfs_file_extent_num_bytes(leaf
, fi
);
1271 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1272 if (extent_end
<= start
) {
1276 if (disk_bytenr
== 0)
1278 if (btrfs_file_extent_compression(leaf
, fi
) ||
1279 btrfs_file_extent_encryption(leaf
, fi
) ||
1280 btrfs_file_extent_other_encoding(leaf
, fi
))
1282 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1284 if (btrfs_extent_readonly(root
, disk_bytenr
))
1286 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1288 extent_offset
, disk_bytenr
))
1290 disk_bytenr
+= extent_offset
;
1291 disk_bytenr
+= cur_offset
- found_key
.offset
;
1292 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1294 * if there are pending snapshots for this root,
1295 * we fall into common COW way.
1298 err
= btrfs_start_nocow_write(root
);
1303 * force cow if csum exists in the range.
1304 * this ensure that csum for a given extent are
1305 * either valid or do not exist.
1307 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1310 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1311 extent_end
= found_key
.offset
+
1312 btrfs_file_extent_inline_len(leaf
,
1313 path
->slots
[0], fi
);
1314 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1319 if (extent_end
<= start
) {
1321 if (!nolock
&& nocow
)
1322 btrfs_end_nocow_write(root
);
1326 if (cow_start
== (u64
)-1)
1327 cow_start
= cur_offset
;
1328 cur_offset
= extent_end
;
1329 if (cur_offset
> end
)
1335 btrfs_release_path(path
);
1336 if (cow_start
!= (u64
)-1) {
1337 ret
= cow_file_range(inode
, locked_page
,
1338 cow_start
, found_key
.offset
- 1,
1339 page_started
, nr_written
, 1);
1341 if (!nolock
&& nocow
)
1342 btrfs_end_nocow_write(root
);
1345 cow_start
= (u64
)-1;
1348 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1349 struct extent_map
*em
;
1350 struct extent_map_tree
*em_tree
;
1351 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1352 em
= alloc_extent_map();
1353 BUG_ON(!em
); /* -ENOMEM */
1354 em
->start
= cur_offset
;
1355 em
->orig_start
= found_key
.offset
- extent_offset
;
1356 em
->len
= num_bytes
;
1357 em
->block_len
= num_bytes
;
1358 em
->block_start
= disk_bytenr
;
1359 em
->orig_block_len
= disk_num_bytes
;
1360 em
->ram_bytes
= ram_bytes
;
1361 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1362 em
->mod_start
= em
->start
;
1363 em
->mod_len
= em
->len
;
1364 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1365 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1366 em
->generation
= -1;
1368 write_lock(&em_tree
->lock
);
1369 ret
= add_extent_mapping(em_tree
, em
, 1);
1370 write_unlock(&em_tree
->lock
);
1371 if (ret
!= -EEXIST
) {
1372 free_extent_map(em
);
1375 btrfs_drop_extent_cache(inode
, em
->start
,
1376 em
->start
+ em
->len
- 1, 0);
1378 type
= BTRFS_ORDERED_PREALLOC
;
1380 type
= BTRFS_ORDERED_NOCOW
;
1383 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1384 num_bytes
, num_bytes
, type
);
1385 BUG_ON(ret
); /* -ENOMEM */
1387 if (root
->root_key
.objectid
==
1388 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1389 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1392 if (!nolock
&& nocow
)
1393 btrfs_end_nocow_write(root
);
1398 extent_clear_unlock_delalloc(inode
, cur_offset
,
1399 cur_offset
+ num_bytes
- 1,
1400 locked_page
, EXTENT_LOCKED
|
1401 EXTENT_DELALLOC
, PAGE_UNLOCK
|
1403 if (!nolock
&& nocow
)
1404 btrfs_end_nocow_write(root
);
1405 cur_offset
= extent_end
;
1406 if (cur_offset
> end
)
1409 btrfs_release_path(path
);
1411 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1412 cow_start
= cur_offset
;
1416 if (cow_start
!= (u64
)-1) {
1417 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1418 page_started
, nr_written
, 1);
1424 err
= btrfs_end_transaction(trans
, root
);
1428 if (ret
&& cur_offset
< end
)
1429 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1430 locked_page
, EXTENT_LOCKED
|
1431 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1432 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1434 PAGE_SET_WRITEBACK
|
1435 PAGE_END_WRITEBACK
);
1436 btrfs_free_path(path
);
1441 * extent_io.c call back to do delayed allocation processing
1443 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1444 u64 start
, u64 end
, int *page_started
,
1445 unsigned long *nr_written
)
1448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1450 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1451 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1452 page_started
, 1, nr_written
);
1453 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1454 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1455 page_started
, 0, nr_written
);
1456 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1457 !(BTRFS_I(inode
)->force_compress
) &&
1458 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1459 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1460 page_started
, nr_written
, 1);
1462 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1463 &BTRFS_I(inode
)->runtime_flags
);
1464 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1465 page_started
, nr_written
);
1470 static void btrfs_split_extent_hook(struct inode
*inode
,
1471 struct extent_state
*orig
, u64 split
)
1473 /* not delalloc, ignore it */
1474 if (!(orig
->state
& EXTENT_DELALLOC
))
1477 spin_lock(&BTRFS_I(inode
)->lock
);
1478 BTRFS_I(inode
)->outstanding_extents
++;
1479 spin_unlock(&BTRFS_I(inode
)->lock
);
1483 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1484 * extents so we can keep track of new extents that are just merged onto old
1485 * extents, such as when we are doing sequential writes, so we can properly
1486 * account for the metadata space we'll need.
1488 static void btrfs_merge_extent_hook(struct inode
*inode
,
1489 struct extent_state
*new,
1490 struct extent_state
*other
)
1492 /* not delalloc, ignore it */
1493 if (!(other
->state
& EXTENT_DELALLOC
))
1496 spin_lock(&BTRFS_I(inode
)->lock
);
1497 BTRFS_I(inode
)->outstanding_extents
--;
1498 spin_unlock(&BTRFS_I(inode
)->lock
);
1501 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1502 struct inode
*inode
)
1504 spin_lock(&root
->delalloc_lock
);
1505 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1506 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1507 &root
->delalloc_inodes
);
1508 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1509 &BTRFS_I(inode
)->runtime_flags
);
1510 root
->nr_delalloc_inodes
++;
1511 if (root
->nr_delalloc_inodes
== 1) {
1512 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1513 BUG_ON(!list_empty(&root
->delalloc_root
));
1514 list_add_tail(&root
->delalloc_root
,
1515 &root
->fs_info
->delalloc_roots
);
1516 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1519 spin_unlock(&root
->delalloc_lock
);
1522 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1523 struct inode
*inode
)
1525 spin_lock(&root
->delalloc_lock
);
1526 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1527 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1528 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1529 &BTRFS_I(inode
)->runtime_flags
);
1530 root
->nr_delalloc_inodes
--;
1531 if (!root
->nr_delalloc_inodes
) {
1532 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1533 BUG_ON(list_empty(&root
->delalloc_root
));
1534 list_del_init(&root
->delalloc_root
);
1535 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1538 spin_unlock(&root
->delalloc_lock
);
1542 * extent_io.c set_bit_hook, used to track delayed allocation
1543 * bytes in this file, and to maintain the list of inodes that
1544 * have pending delalloc work to be done.
1546 static void btrfs_set_bit_hook(struct inode
*inode
,
1547 struct extent_state
*state
, unsigned long *bits
)
1551 * set_bit and clear bit hooks normally require _irqsave/restore
1552 * but in this case, we are only testing for the DELALLOC
1553 * bit, which is only set or cleared with irqs on
1555 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1556 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1557 u64 len
= state
->end
+ 1 - state
->start
;
1558 bool do_list
= !btrfs_is_free_space_inode(inode
);
1560 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1561 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1563 spin_lock(&BTRFS_I(inode
)->lock
);
1564 BTRFS_I(inode
)->outstanding_extents
++;
1565 spin_unlock(&BTRFS_I(inode
)->lock
);
1568 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1569 root
->fs_info
->delalloc_batch
);
1570 spin_lock(&BTRFS_I(inode
)->lock
);
1571 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1572 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1573 &BTRFS_I(inode
)->runtime_flags
))
1574 btrfs_add_delalloc_inodes(root
, inode
);
1575 spin_unlock(&BTRFS_I(inode
)->lock
);
1580 * extent_io.c clear_bit_hook, see set_bit_hook for why
1582 static void btrfs_clear_bit_hook(struct inode
*inode
,
1583 struct extent_state
*state
,
1584 unsigned long *bits
)
1587 * set_bit and clear bit hooks normally require _irqsave/restore
1588 * but in this case, we are only testing for the DELALLOC
1589 * bit, which is only set or cleared with irqs on
1591 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1592 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1593 u64 len
= state
->end
+ 1 - state
->start
;
1594 bool do_list
= !btrfs_is_free_space_inode(inode
);
1596 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1597 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1598 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1599 spin_lock(&BTRFS_I(inode
)->lock
);
1600 BTRFS_I(inode
)->outstanding_extents
--;
1601 spin_unlock(&BTRFS_I(inode
)->lock
);
1605 * We don't reserve metadata space for space cache inodes so we
1606 * don't need to call dellalloc_release_metadata if there is an
1609 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1610 root
!= root
->fs_info
->tree_root
)
1611 btrfs_delalloc_release_metadata(inode
, len
);
1613 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1614 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1615 btrfs_free_reserved_data_space(inode
, len
);
1617 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1618 root
->fs_info
->delalloc_batch
);
1619 spin_lock(&BTRFS_I(inode
)->lock
);
1620 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1621 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1622 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1623 &BTRFS_I(inode
)->runtime_flags
))
1624 btrfs_del_delalloc_inode(root
, inode
);
1625 spin_unlock(&BTRFS_I(inode
)->lock
);
1630 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1631 * we don't create bios that span stripes or chunks
1633 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1634 size_t size
, struct bio
*bio
,
1635 unsigned long bio_flags
)
1637 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1638 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1643 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1646 length
= bio
->bi_iter
.bi_size
;
1647 map_length
= length
;
1648 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1649 &map_length
, NULL
, 0);
1650 /* Will always return 0 with map_multi == NULL */
1652 if (map_length
< length
+ size
)
1658 * in order to insert checksums into the metadata in large chunks,
1659 * we wait until bio submission time. All the pages in the bio are
1660 * checksummed and sums are attached onto the ordered extent record.
1662 * At IO completion time the cums attached on the ordered extent record
1663 * are inserted into the btree
1665 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1666 struct bio
*bio
, int mirror_num
,
1667 unsigned long bio_flags
,
1670 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1673 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1674 BUG_ON(ret
); /* -ENOMEM */
1679 * in order to insert checksums into the metadata in large chunks,
1680 * we wait until bio submission time. All the pages in the bio are
1681 * checksummed and sums are attached onto the ordered extent record.
1683 * At IO completion time the cums attached on the ordered extent record
1684 * are inserted into the btree
1686 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1687 int mirror_num
, unsigned long bio_flags
,
1690 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1693 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1695 bio_endio(bio
, ret
);
1700 * extent_io.c submission hook. This does the right thing for csum calculation
1701 * on write, or reading the csums from the tree before a read
1703 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1704 int mirror_num
, unsigned long bio_flags
,
1707 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1711 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1713 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1715 if (btrfs_is_free_space_inode(inode
))
1718 if (!(rw
& REQ_WRITE
)) {
1719 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1723 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1724 ret
= btrfs_submit_compressed_read(inode
, bio
,
1728 } else if (!skip_sum
) {
1729 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1734 } else if (async
&& !skip_sum
) {
1735 /* csum items have already been cloned */
1736 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1738 /* we're doing a write, do the async checksumming */
1739 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1740 inode
, rw
, bio
, mirror_num
,
1741 bio_flags
, bio_offset
,
1742 __btrfs_submit_bio_start
,
1743 __btrfs_submit_bio_done
);
1745 } else if (!skip_sum
) {
1746 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1752 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1756 bio_endio(bio
, ret
);
1761 * given a list of ordered sums record them in the inode. This happens
1762 * at IO completion time based on sums calculated at bio submission time.
1764 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1765 struct inode
*inode
, u64 file_offset
,
1766 struct list_head
*list
)
1768 struct btrfs_ordered_sum
*sum
;
1770 list_for_each_entry(sum
, list
, list
) {
1771 trans
->adding_csums
= 1;
1772 btrfs_csum_file_blocks(trans
,
1773 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1774 trans
->adding_csums
= 0;
1779 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1780 struct extent_state
**cached_state
)
1782 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1783 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1784 cached_state
, GFP_NOFS
);
1787 /* see btrfs_writepage_start_hook for details on why this is required */
1788 struct btrfs_writepage_fixup
{
1790 struct btrfs_work work
;
1793 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1795 struct btrfs_writepage_fixup
*fixup
;
1796 struct btrfs_ordered_extent
*ordered
;
1797 struct extent_state
*cached_state
= NULL
;
1799 struct inode
*inode
;
1804 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1808 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1809 ClearPageChecked(page
);
1813 inode
= page
->mapping
->host
;
1814 page_start
= page_offset(page
);
1815 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1817 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1820 /* already ordered? We're done */
1821 if (PagePrivate2(page
))
1824 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1826 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1827 page_end
, &cached_state
, GFP_NOFS
);
1829 btrfs_start_ordered_extent(inode
, ordered
, 1);
1830 btrfs_put_ordered_extent(ordered
);
1834 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1836 mapping_set_error(page
->mapping
, ret
);
1837 end_extent_writepage(page
, ret
, page_start
, page_end
);
1838 ClearPageChecked(page
);
1842 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1843 ClearPageChecked(page
);
1844 set_page_dirty(page
);
1846 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1847 &cached_state
, GFP_NOFS
);
1850 page_cache_release(page
);
1855 * There are a few paths in the higher layers of the kernel that directly
1856 * set the page dirty bit without asking the filesystem if it is a
1857 * good idea. This causes problems because we want to make sure COW
1858 * properly happens and the data=ordered rules are followed.
1860 * In our case any range that doesn't have the ORDERED bit set
1861 * hasn't been properly setup for IO. We kick off an async process
1862 * to fix it up. The async helper will wait for ordered extents, set
1863 * the delalloc bit and make it safe to write the page.
1865 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1867 struct inode
*inode
= page
->mapping
->host
;
1868 struct btrfs_writepage_fixup
*fixup
;
1869 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 /* this page is properly in the ordered list */
1872 if (TestClearPagePrivate2(page
))
1875 if (PageChecked(page
))
1878 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1882 SetPageChecked(page
);
1883 page_cache_get(page
);
1884 btrfs_init_work(&fixup
->work
, btrfs_writepage_fixup_worker
, NULL
, NULL
);
1886 btrfs_queue_work(root
->fs_info
->fixup_workers
, &fixup
->work
);
1890 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1891 struct inode
*inode
, u64 file_pos
,
1892 u64 disk_bytenr
, u64 disk_num_bytes
,
1893 u64 num_bytes
, u64 ram_bytes
,
1894 u8 compression
, u8 encryption
,
1895 u16 other_encoding
, int extent_type
)
1897 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1898 struct btrfs_file_extent_item
*fi
;
1899 struct btrfs_path
*path
;
1900 struct extent_buffer
*leaf
;
1901 struct btrfs_key ins
;
1902 int extent_inserted
= 0;
1905 path
= btrfs_alloc_path();
1910 * we may be replacing one extent in the tree with another.
1911 * The new extent is pinned in the extent map, and we don't want
1912 * to drop it from the cache until it is completely in the btree.
1914 * So, tell btrfs_drop_extents to leave this extent in the cache.
1915 * the caller is expected to unpin it and allow it to be merged
1918 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
1919 file_pos
+ num_bytes
, NULL
, 0,
1920 1, sizeof(*fi
), &extent_inserted
);
1924 if (!extent_inserted
) {
1925 ins
.objectid
= btrfs_ino(inode
);
1926 ins
.offset
= file_pos
;
1927 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1929 path
->leave_spinning
= 1;
1930 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1935 leaf
= path
->nodes
[0];
1936 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1937 struct btrfs_file_extent_item
);
1938 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1939 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1940 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1941 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1942 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1943 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1944 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1945 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1946 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1947 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1949 btrfs_mark_buffer_dirty(leaf
);
1950 btrfs_release_path(path
);
1952 inode_add_bytes(inode
, num_bytes
);
1954 ins
.objectid
= disk_bytenr
;
1955 ins
.offset
= disk_num_bytes
;
1956 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1957 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1958 root
->root_key
.objectid
,
1959 btrfs_ino(inode
), file_pos
, &ins
);
1961 btrfs_free_path(path
);
1966 /* snapshot-aware defrag */
1967 struct sa_defrag_extent_backref
{
1968 struct rb_node node
;
1969 struct old_sa_defrag_extent
*old
;
1978 struct old_sa_defrag_extent
{
1979 struct list_head list
;
1980 struct new_sa_defrag_extent
*new;
1989 struct new_sa_defrag_extent
{
1990 struct rb_root root
;
1991 struct list_head head
;
1992 struct btrfs_path
*path
;
1993 struct inode
*inode
;
2001 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2002 struct sa_defrag_extent_backref
*b2
)
2004 if (b1
->root_id
< b2
->root_id
)
2006 else if (b1
->root_id
> b2
->root_id
)
2009 if (b1
->inum
< b2
->inum
)
2011 else if (b1
->inum
> b2
->inum
)
2014 if (b1
->file_pos
< b2
->file_pos
)
2016 else if (b1
->file_pos
> b2
->file_pos
)
2020 * [------------------------------] ===> (a range of space)
2021 * |<--->| |<---->| =============> (fs/file tree A)
2022 * |<---------------------------->| ===> (fs/file tree B)
2024 * A range of space can refer to two file extents in one tree while
2025 * refer to only one file extent in another tree.
2027 * So we may process a disk offset more than one time(two extents in A)
2028 * and locate at the same extent(one extent in B), then insert two same
2029 * backrefs(both refer to the extent in B).
2034 static void backref_insert(struct rb_root
*root
,
2035 struct sa_defrag_extent_backref
*backref
)
2037 struct rb_node
**p
= &root
->rb_node
;
2038 struct rb_node
*parent
= NULL
;
2039 struct sa_defrag_extent_backref
*entry
;
2044 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2046 ret
= backref_comp(backref
, entry
);
2050 p
= &(*p
)->rb_right
;
2053 rb_link_node(&backref
->node
, parent
, p
);
2054 rb_insert_color(&backref
->node
, root
);
2058 * Note the backref might has changed, and in this case we just return 0.
2060 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2063 struct btrfs_file_extent_item
*extent
;
2064 struct btrfs_fs_info
*fs_info
;
2065 struct old_sa_defrag_extent
*old
= ctx
;
2066 struct new_sa_defrag_extent
*new = old
->new;
2067 struct btrfs_path
*path
= new->path
;
2068 struct btrfs_key key
;
2069 struct btrfs_root
*root
;
2070 struct sa_defrag_extent_backref
*backref
;
2071 struct extent_buffer
*leaf
;
2072 struct inode
*inode
= new->inode
;
2078 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2079 inum
== btrfs_ino(inode
))
2082 key
.objectid
= root_id
;
2083 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2084 key
.offset
= (u64
)-1;
2086 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2087 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2089 if (PTR_ERR(root
) == -ENOENT
)
2092 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2093 inum
, offset
, root_id
);
2094 return PTR_ERR(root
);
2097 key
.objectid
= inum
;
2098 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2099 if (offset
> (u64
)-1 << 32)
2102 key
.offset
= offset
;
2104 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2105 if (WARN_ON(ret
< 0))
2112 leaf
= path
->nodes
[0];
2113 slot
= path
->slots
[0];
2115 if (slot
>= btrfs_header_nritems(leaf
)) {
2116 ret
= btrfs_next_leaf(root
, path
);
2119 } else if (ret
> 0) {
2128 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2130 if (key
.objectid
> inum
)
2133 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2136 extent
= btrfs_item_ptr(leaf
, slot
,
2137 struct btrfs_file_extent_item
);
2139 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2143 * 'offset' refers to the exact key.offset,
2144 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2145 * (key.offset - extent_offset).
2147 if (key
.offset
!= offset
)
2150 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2151 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2153 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2154 old
->len
|| extent_offset
+ num_bytes
<=
2155 old
->extent_offset
+ old
->offset
)
2160 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2166 backref
->root_id
= root_id
;
2167 backref
->inum
= inum
;
2168 backref
->file_pos
= offset
;
2169 backref
->num_bytes
= num_bytes
;
2170 backref
->extent_offset
= extent_offset
;
2171 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2173 backref_insert(&new->root
, backref
);
2176 btrfs_release_path(path
);
2181 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2182 struct new_sa_defrag_extent
*new)
2184 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2185 struct old_sa_defrag_extent
*old
, *tmp
;
2190 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2191 ret
= iterate_inodes_from_logical(old
->bytenr
+
2192 old
->extent_offset
, fs_info
,
2193 path
, record_one_backref
,
2195 if (ret
< 0 && ret
!= -ENOENT
)
2198 /* no backref to be processed for this extent */
2200 list_del(&old
->list
);
2205 if (list_empty(&new->head
))
2211 static int relink_is_mergable(struct extent_buffer
*leaf
,
2212 struct btrfs_file_extent_item
*fi
,
2213 struct new_sa_defrag_extent
*new)
2215 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2218 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2221 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2224 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2225 btrfs_file_extent_other_encoding(leaf
, fi
))
2232 * Note the backref might has changed, and in this case we just return 0.
2234 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2235 struct sa_defrag_extent_backref
*prev
,
2236 struct sa_defrag_extent_backref
*backref
)
2238 struct btrfs_file_extent_item
*extent
;
2239 struct btrfs_file_extent_item
*item
;
2240 struct btrfs_ordered_extent
*ordered
;
2241 struct btrfs_trans_handle
*trans
;
2242 struct btrfs_fs_info
*fs_info
;
2243 struct btrfs_root
*root
;
2244 struct btrfs_key key
;
2245 struct extent_buffer
*leaf
;
2246 struct old_sa_defrag_extent
*old
= backref
->old
;
2247 struct new_sa_defrag_extent
*new = old
->new;
2248 struct inode
*src_inode
= new->inode
;
2249 struct inode
*inode
;
2250 struct extent_state
*cached
= NULL
;
2259 if (prev
&& prev
->root_id
== backref
->root_id
&&
2260 prev
->inum
== backref
->inum
&&
2261 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2264 /* step 1: get root */
2265 key
.objectid
= backref
->root_id
;
2266 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2267 key
.offset
= (u64
)-1;
2269 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2270 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2272 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2274 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2275 if (PTR_ERR(root
) == -ENOENT
)
2277 return PTR_ERR(root
);
2280 if (btrfs_root_readonly(root
)) {
2281 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2285 /* step 2: get inode */
2286 key
.objectid
= backref
->inum
;
2287 key
.type
= BTRFS_INODE_ITEM_KEY
;
2290 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2291 if (IS_ERR(inode
)) {
2292 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2296 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2298 /* step 3: relink backref */
2299 lock_start
= backref
->file_pos
;
2300 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2301 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2304 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2306 btrfs_put_ordered_extent(ordered
);
2310 trans
= btrfs_join_transaction(root
);
2311 if (IS_ERR(trans
)) {
2312 ret
= PTR_ERR(trans
);
2316 key
.objectid
= backref
->inum
;
2317 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2318 key
.offset
= backref
->file_pos
;
2320 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2323 } else if (ret
> 0) {
2328 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2329 struct btrfs_file_extent_item
);
2331 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2332 backref
->generation
)
2335 btrfs_release_path(path
);
2337 start
= backref
->file_pos
;
2338 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2339 start
+= old
->extent_offset
+ old
->offset
-
2340 backref
->extent_offset
;
2342 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2343 old
->extent_offset
+ old
->offset
+ old
->len
);
2344 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2346 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2351 key
.objectid
= btrfs_ino(inode
);
2352 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2355 path
->leave_spinning
= 1;
2357 struct btrfs_file_extent_item
*fi
;
2359 struct btrfs_key found_key
;
2361 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2366 leaf
= path
->nodes
[0];
2367 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2369 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2370 struct btrfs_file_extent_item
);
2371 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2373 if (extent_len
+ found_key
.offset
== start
&&
2374 relink_is_mergable(leaf
, fi
, new)) {
2375 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2377 btrfs_mark_buffer_dirty(leaf
);
2378 inode_add_bytes(inode
, len
);
2384 btrfs_release_path(path
);
2389 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2392 btrfs_abort_transaction(trans
, root
, ret
);
2396 leaf
= path
->nodes
[0];
2397 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2398 struct btrfs_file_extent_item
);
2399 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2400 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2401 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2402 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2403 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2404 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2405 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2406 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2407 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2408 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2410 btrfs_mark_buffer_dirty(leaf
);
2411 inode_add_bytes(inode
, len
);
2412 btrfs_release_path(path
);
2414 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2416 backref
->root_id
, backref
->inum
,
2417 new->file_pos
, 0); /* start - extent_offset */
2419 btrfs_abort_transaction(trans
, root
, ret
);
2425 btrfs_release_path(path
);
2426 path
->leave_spinning
= 0;
2427 btrfs_end_transaction(trans
, root
);
2429 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2435 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2437 struct old_sa_defrag_extent
*old
, *tmp
;
2442 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2443 list_del(&old
->list
);
2449 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2451 struct btrfs_path
*path
;
2452 struct sa_defrag_extent_backref
*backref
;
2453 struct sa_defrag_extent_backref
*prev
= NULL
;
2454 struct inode
*inode
;
2455 struct btrfs_root
*root
;
2456 struct rb_node
*node
;
2460 root
= BTRFS_I(inode
)->root
;
2462 path
= btrfs_alloc_path();
2466 if (!record_extent_backrefs(path
, new)) {
2467 btrfs_free_path(path
);
2470 btrfs_release_path(path
);
2473 node
= rb_first(&new->root
);
2476 rb_erase(node
, &new->root
);
2478 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2480 ret
= relink_extent_backref(path
, prev
, backref
);
2493 btrfs_free_path(path
);
2495 free_sa_defrag_extent(new);
2497 atomic_dec(&root
->fs_info
->defrag_running
);
2498 wake_up(&root
->fs_info
->transaction_wait
);
2501 static struct new_sa_defrag_extent
*
2502 record_old_file_extents(struct inode
*inode
,
2503 struct btrfs_ordered_extent
*ordered
)
2505 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2506 struct btrfs_path
*path
;
2507 struct btrfs_key key
;
2508 struct old_sa_defrag_extent
*old
;
2509 struct new_sa_defrag_extent
*new;
2512 new = kmalloc(sizeof(*new), GFP_NOFS
);
2517 new->file_pos
= ordered
->file_offset
;
2518 new->len
= ordered
->len
;
2519 new->bytenr
= ordered
->start
;
2520 new->disk_len
= ordered
->disk_len
;
2521 new->compress_type
= ordered
->compress_type
;
2522 new->root
= RB_ROOT
;
2523 INIT_LIST_HEAD(&new->head
);
2525 path
= btrfs_alloc_path();
2529 key
.objectid
= btrfs_ino(inode
);
2530 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2531 key
.offset
= new->file_pos
;
2533 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2536 if (ret
> 0 && path
->slots
[0] > 0)
2539 /* find out all the old extents for the file range */
2541 struct btrfs_file_extent_item
*extent
;
2542 struct extent_buffer
*l
;
2551 slot
= path
->slots
[0];
2553 if (slot
>= btrfs_header_nritems(l
)) {
2554 ret
= btrfs_next_leaf(root
, path
);
2562 btrfs_item_key_to_cpu(l
, &key
, slot
);
2564 if (key
.objectid
!= btrfs_ino(inode
))
2566 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2568 if (key
.offset
>= new->file_pos
+ new->len
)
2571 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2573 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2574 if (key
.offset
+ num_bytes
< new->file_pos
)
2577 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2581 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2583 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2587 offset
= max(new->file_pos
, key
.offset
);
2588 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2590 old
->bytenr
= disk_bytenr
;
2591 old
->extent_offset
= extent_offset
;
2592 old
->offset
= offset
- key
.offset
;
2593 old
->len
= end
- offset
;
2596 list_add_tail(&old
->list
, &new->head
);
2602 btrfs_free_path(path
);
2603 atomic_inc(&root
->fs_info
->defrag_running
);
2608 btrfs_free_path(path
);
2610 free_sa_defrag_extent(new);
2614 static void btrfs_release_delalloc_bytes(struct btrfs_root
*root
,
2617 struct btrfs_block_group_cache
*cache
;
2619 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2622 spin_lock(&cache
->lock
);
2623 cache
->delalloc_bytes
-= len
;
2624 spin_unlock(&cache
->lock
);
2626 btrfs_put_block_group(cache
);
2629 /* as ordered data IO finishes, this gets called so we can finish
2630 * an ordered extent if the range of bytes in the file it covers are
2633 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2635 struct inode
*inode
= ordered_extent
->inode
;
2636 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2637 struct btrfs_trans_handle
*trans
= NULL
;
2638 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2639 struct extent_state
*cached_state
= NULL
;
2640 struct new_sa_defrag_extent
*new = NULL
;
2641 int compress_type
= 0;
2643 u64 logical_len
= ordered_extent
->len
;
2645 bool truncated
= false;
2647 nolock
= btrfs_is_free_space_inode(inode
);
2649 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2654 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2656 logical_len
= ordered_extent
->truncated_len
;
2657 /* Truncated the entire extent, don't bother adding */
2662 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2663 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2664 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2666 trans
= btrfs_join_transaction_nolock(root
);
2668 trans
= btrfs_join_transaction(root
);
2669 if (IS_ERR(trans
)) {
2670 ret
= PTR_ERR(trans
);
2674 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2675 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2676 if (ret
) /* -ENOMEM or corruption */
2677 btrfs_abort_transaction(trans
, root
, ret
);
2681 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2682 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2685 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2686 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2687 EXTENT_DEFRAG
, 1, cached_state
);
2689 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2690 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2691 /* the inode is shared */
2692 new = record_old_file_extents(inode
, ordered_extent
);
2694 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2695 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2696 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2700 trans
= btrfs_join_transaction_nolock(root
);
2702 trans
= btrfs_join_transaction(root
);
2703 if (IS_ERR(trans
)) {
2704 ret
= PTR_ERR(trans
);
2709 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2711 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2712 compress_type
= ordered_extent
->compress_type
;
2713 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2714 BUG_ON(compress_type
);
2715 ret
= btrfs_mark_extent_written(trans
, inode
,
2716 ordered_extent
->file_offset
,
2717 ordered_extent
->file_offset
+
2720 BUG_ON(root
== root
->fs_info
->tree_root
);
2721 ret
= insert_reserved_file_extent(trans
, inode
,
2722 ordered_extent
->file_offset
,
2723 ordered_extent
->start
,
2724 ordered_extent
->disk_len
,
2725 logical_len
, logical_len
,
2726 compress_type
, 0, 0,
2727 BTRFS_FILE_EXTENT_REG
);
2729 btrfs_release_delalloc_bytes(root
,
2730 ordered_extent
->start
,
2731 ordered_extent
->disk_len
);
2733 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2734 ordered_extent
->file_offset
, ordered_extent
->len
,
2737 btrfs_abort_transaction(trans
, root
, ret
);
2741 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2742 &ordered_extent
->list
);
2744 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2745 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2746 if (ret
) { /* -ENOMEM or corruption */
2747 btrfs_abort_transaction(trans
, root
, ret
);
2752 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2753 ordered_extent
->file_offset
+
2754 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2756 if (root
!= root
->fs_info
->tree_root
)
2757 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2759 btrfs_end_transaction(trans
, root
);
2761 if (ret
|| truncated
) {
2765 start
= ordered_extent
->file_offset
+ logical_len
;
2767 start
= ordered_extent
->file_offset
;
2768 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2769 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2771 /* Drop the cache for the part of the extent we didn't write. */
2772 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2775 * If the ordered extent had an IOERR or something else went
2776 * wrong we need to return the space for this ordered extent
2777 * back to the allocator. We only free the extent in the
2778 * truncated case if we didn't write out the extent at all.
2780 if ((ret
|| !logical_len
) &&
2781 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2782 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2783 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2784 ordered_extent
->disk_len
, 1);
2789 * This needs to be done to make sure anybody waiting knows we are done
2790 * updating everything for this ordered extent.
2792 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2794 /* for snapshot-aware defrag */
2797 free_sa_defrag_extent(new);
2798 atomic_dec(&root
->fs_info
->defrag_running
);
2800 relink_file_extents(new);
2805 btrfs_put_ordered_extent(ordered_extent
);
2806 /* once for the tree */
2807 btrfs_put_ordered_extent(ordered_extent
);
2812 static void finish_ordered_fn(struct btrfs_work
*work
)
2814 struct btrfs_ordered_extent
*ordered_extent
;
2815 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2816 btrfs_finish_ordered_io(ordered_extent
);
2819 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2820 struct extent_state
*state
, int uptodate
)
2822 struct inode
*inode
= page
->mapping
->host
;
2823 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2824 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2825 struct btrfs_workqueue
*workers
;
2827 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2829 ClearPagePrivate2(page
);
2830 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2831 end
- start
+ 1, uptodate
))
2834 btrfs_init_work(&ordered_extent
->work
, finish_ordered_fn
, NULL
, NULL
);
2836 if (btrfs_is_free_space_inode(inode
))
2837 workers
= root
->fs_info
->endio_freespace_worker
;
2839 workers
= root
->fs_info
->endio_write_workers
;
2840 btrfs_queue_work(workers
, &ordered_extent
->work
);
2846 * when reads are done, we need to check csums to verify the data is correct
2847 * if there's a match, we allow the bio to finish. If not, the code in
2848 * extent_io.c will try to find good copies for us.
2850 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
2851 u64 phy_offset
, struct page
*page
,
2852 u64 start
, u64 end
, int mirror
)
2854 size_t offset
= start
- page_offset(page
);
2855 struct inode
*inode
= page
->mapping
->host
;
2856 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2861 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2862 DEFAULT_RATELIMIT_BURST
);
2864 if (PageChecked(page
)) {
2865 ClearPageChecked(page
);
2869 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2872 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2873 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2874 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2879 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2880 csum_expected
= *(((u32
*)io_bio
->csum
) + phy_offset
);
2882 kaddr
= kmap_atomic(page
);
2883 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2884 btrfs_csum_final(csum
, (char *)&csum
);
2885 if (csum
!= csum_expected
)
2888 kunmap_atomic(kaddr
);
2893 if (__ratelimit(&_rs
))
2894 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
2895 btrfs_ino(page
->mapping
->host
), start
, csum
, csum_expected
);
2896 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2897 flush_dcache_page(page
);
2898 kunmap_atomic(kaddr
);
2899 if (csum_expected
== 0)
2904 struct delayed_iput
{
2905 struct list_head list
;
2906 struct inode
*inode
;
2909 /* JDM: If this is fs-wide, why can't we add a pointer to
2910 * btrfs_inode instead and avoid the allocation? */
2911 void btrfs_add_delayed_iput(struct inode
*inode
)
2913 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2914 struct delayed_iput
*delayed
;
2916 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2919 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2920 delayed
->inode
= inode
;
2922 spin_lock(&fs_info
->delayed_iput_lock
);
2923 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2924 spin_unlock(&fs_info
->delayed_iput_lock
);
2927 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2930 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2931 struct delayed_iput
*delayed
;
2934 spin_lock(&fs_info
->delayed_iput_lock
);
2935 empty
= list_empty(&fs_info
->delayed_iputs
);
2936 spin_unlock(&fs_info
->delayed_iput_lock
);
2940 spin_lock(&fs_info
->delayed_iput_lock
);
2941 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2942 spin_unlock(&fs_info
->delayed_iput_lock
);
2944 while (!list_empty(&list
)) {
2945 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2946 list_del(&delayed
->list
);
2947 iput(delayed
->inode
);
2953 * This is called in transaction commit time. If there are no orphan
2954 * files in the subvolume, it removes orphan item and frees block_rsv
2957 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2958 struct btrfs_root
*root
)
2960 struct btrfs_block_rsv
*block_rsv
;
2963 if (atomic_read(&root
->orphan_inodes
) ||
2964 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2967 spin_lock(&root
->orphan_lock
);
2968 if (atomic_read(&root
->orphan_inodes
)) {
2969 spin_unlock(&root
->orphan_lock
);
2973 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2974 spin_unlock(&root
->orphan_lock
);
2978 block_rsv
= root
->orphan_block_rsv
;
2979 root
->orphan_block_rsv
= NULL
;
2980 spin_unlock(&root
->orphan_lock
);
2982 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
2983 btrfs_root_refs(&root
->root_item
) > 0) {
2984 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2985 root
->root_key
.objectid
);
2987 btrfs_abort_transaction(trans
, root
, ret
);
2989 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
2994 WARN_ON(block_rsv
->size
> 0);
2995 btrfs_free_block_rsv(root
, block_rsv
);
3000 * This creates an orphan entry for the given inode in case something goes
3001 * wrong in the middle of an unlink/truncate.
3003 * NOTE: caller of this function should reserve 5 units of metadata for
3006 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3009 struct btrfs_block_rsv
*block_rsv
= NULL
;
3014 if (!root
->orphan_block_rsv
) {
3015 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3020 spin_lock(&root
->orphan_lock
);
3021 if (!root
->orphan_block_rsv
) {
3022 root
->orphan_block_rsv
= block_rsv
;
3023 } else if (block_rsv
) {
3024 btrfs_free_block_rsv(root
, block_rsv
);
3028 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3029 &BTRFS_I(inode
)->runtime_flags
)) {
3032 * For proper ENOSPC handling, we should do orphan
3033 * cleanup when mounting. But this introduces backward
3034 * compatibility issue.
3036 if (!xchg(&root
->orphan_item_inserted
, 1))
3042 atomic_inc(&root
->orphan_inodes
);
3045 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3046 &BTRFS_I(inode
)->runtime_flags
))
3048 spin_unlock(&root
->orphan_lock
);
3050 /* grab metadata reservation from transaction handle */
3052 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3053 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3056 /* insert an orphan item to track this unlinked/truncated file */
3058 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3060 atomic_dec(&root
->orphan_inodes
);
3062 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3063 &BTRFS_I(inode
)->runtime_flags
);
3064 btrfs_orphan_release_metadata(inode
);
3066 if (ret
!= -EEXIST
) {
3067 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3068 &BTRFS_I(inode
)->runtime_flags
);
3069 btrfs_abort_transaction(trans
, root
, ret
);
3076 /* insert an orphan item to track subvolume contains orphan files */
3078 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3079 root
->root_key
.objectid
);
3080 if (ret
&& ret
!= -EEXIST
) {
3081 btrfs_abort_transaction(trans
, root
, ret
);
3089 * We have done the truncate/delete so we can go ahead and remove the orphan
3090 * item for this particular inode.
3092 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3093 struct inode
*inode
)
3095 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3096 int delete_item
= 0;
3097 int release_rsv
= 0;
3100 spin_lock(&root
->orphan_lock
);
3101 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3102 &BTRFS_I(inode
)->runtime_flags
))
3105 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3106 &BTRFS_I(inode
)->runtime_flags
))
3108 spin_unlock(&root
->orphan_lock
);
3111 atomic_dec(&root
->orphan_inodes
);
3113 ret
= btrfs_del_orphan_item(trans
, root
,
3118 btrfs_orphan_release_metadata(inode
);
3124 * this cleans up any orphans that may be left on the list from the last use
3127 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3129 struct btrfs_path
*path
;
3130 struct extent_buffer
*leaf
;
3131 struct btrfs_key key
, found_key
;
3132 struct btrfs_trans_handle
*trans
;
3133 struct inode
*inode
;
3134 u64 last_objectid
= 0;
3135 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3137 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3140 path
= btrfs_alloc_path();
3147 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3148 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3149 key
.offset
= (u64
)-1;
3152 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3157 * if ret == 0 means we found what we were searching for, which
3158 * is weird, but possible, so only screw with path if we didn't
3159 * find the key and see if we have stuff that matches
3163 if (path
->slots
[0] == 0)
3168 /* pull out the item */
3169 leaf
= path
->nodes
[0];
3170 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3172 /* make sure the item matches what we want */
3173 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3175 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3178 /* release the path since we're done with it */
3179 btrfs_release_path(path
);
3182 * this is where we are basically btrfs_lookup, without the
3183 * crossing root thing. we store the inode number in the
3184 * offset of the orphan item.
3187 if (found_key
.offset
== last_objectid
) {
3188 btrfs_err(root
->fs_info
,
3189 "Error removing orphan entry, stopping orphan cleanup");
3194 last_objectid
= found_key
.offset
;
3196 found_key
.objectid
= found_key
.offset
;
3197 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3198 found_key
.offset
= 0;
3199 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3200 ret
= PTR_ERR_OR_ZERO(inode
);
3201 if (ret
&& ret
!= -ESTALE
)
3204 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3205 struct btrfs_root
*dead_root
;
3206 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3207 int is_dead_root
= 0;
3210 * this is an orphan in the tree root. Currently these
3211 * could come from 2 sources:
3212 * a) a snapshot deletion in progress
3213 * b) a free space cache inode
3214 * We need to distinguish those two, as the snapshot
3215 * orphan must not get deleted.
3216 * find_dead_roots already ran before us, so if this
3217 * is a snapshot deletion, we should find the root
3218 * in the dead_roots list
3220 spin_lock(&fs_info
->trans_lock
);
3221 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3223 if (dead_root
->root_key
.objectid
==
3224 found_key
.objectid
) {
3229 spin_unlock(&fs_info
->trans_lock
);
3231 /* prevent this orphan from being found again */
3232 key
.offset
= found_key
.objectid
- 1;
3237 * Inode is already gone but the orphan item is still there,
3238 * kill the orphan item.
3240 if (ret
== -ESTALE
) {
3241 trans
= btrfs_start_transaction(root
, 1);
3242 if (IS_ERR(trans
)) {
3243 ret
= PTR_ERR(trans
);
3246 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3247 found_key
.objectid
);
3248 ret
= btrfs_del_orphan_item(trans
, root
,
3249 found_key
.objectid
);
3250 btrfs_end_transaction(trans
, root
);
3257 * add this inode to the orphan list so btrfs_orphan_del does
3258 * the proper thing when we hit it
3260 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3261 &BTRFS_I(inode
)->runtime_flags
);
3262 atomic_inc(&root
->orphan_inodes
);
3264 /* if we have links, this was a truncate, lets do that */
3265 if (inode
->i_nlink
) {
3266 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3272 /* 1 for the orphan item deletion. */
3273 trans
= btrfs_start_transaction(root
, 1);
3274 if (IS_ERR(trans
)) {
3276 ret
= PTR_ERR(trans
);
3279 ret
= btrfs_orphan_add(trans
, inode
);
3280 btrfs_end_transaction(trans
, root
);
3286 ret
= btrfs_truncate(inode
);
3288 btrfs_orphan_del(NULL
, inode
);
3293 /* this will do delete_inode and everything for us */
3298 /* release the path since we're done with it */
3299 btrfs_release_path(path
);
3301 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3303 if (root
->orphan_block_rsv
)
3304 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3307 if (root
->orphan_block_rsv
||
3308 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3309 trans
= btrfs_join_transaction(root
);
3311 btrfs_end_transaction(trans
, root
);
3315 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3317 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3321 btrfs_crit(root
->fs_info
,
3322 "could not do orphan cleanup %d", ret
);
3323 btrfs_free_path(path
);
3328 * very simple check to peek ahead in the leaf looking for xattrs. If we
3329 * don't find any xattrs, we know there can't be any acls.
3331 * slot is the slot the inode is in, objectid is the objectid of the inode
3333 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3334 int slot
, u64 objectid
,
3335 int *first_xattr_slot
)
3337 u32 nritems
= btrfs_header_nritems(leaf
);
3338 struct btrfs_key found_key
;
3339 static u64 xattr_access
= 0;
3340 static u64 xattr_default
= 0;
3343 if (!xattr_access
) {
3344 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3345 strlen(POSIX_ACL_XATTR_ACCESS
));
3346 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3347 strlen(POSIX_ACL_XATTR_DEFAULT
));
3351 *first_xattr_slot
= -1;
3352 while (slot
< nritems
) {
3353 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3355 /* we found a different objectid, there must not be acls */
3356 if (found_key
.objectid
!= objectid
)
3359 /* we found an xattr, assume we've got an acl */
3360 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3361 if (*first_xattr_slot
== -1)
3362 *first_xattr_slot
= slot
;
3363 if (found_key
.offset
== xattr_access
||
3364 found_key
.offset
== xattr_default
)
3369 * we found a key greater than an xattr key, there can't
3370 * be any acls later on
3372 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3379 * it goes inode, inode backrefs, xattrs, extents,
3380 * so if there are a ton of hard links to an inode there can
3381 * be a lot of backrefs. Don't waste time searching too hard,
3382 * this is just an optimization
3387 /* we hit the end of the leaf before we found an xattr or
3388 * something larger than an xattr. We have to assume the inode
3391 if (*first_xattr_slot
== -1)
3392 *first_xattr_slot
= slot
;
3397 * read an inode from the btree into the in-memory inode
3399 static void btrfs_read_locked_inode(struct inode
*inode
)
3401 struct btrfs_path
*path
;
3402 struct extent_buffer
*leaf
;
3403 struct btrfs_inode_item
*inode_item
;
3404 struct btrfs_timespec
*tspec
;
3405 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3406 struct btrfs_key location
;
3411 bool filled
= false;
3412 int first_xattr_slot
;
3414 ret
= btrfs_fill_inode(inode
, &rdev
);
3418 path
= btrfs_alloc_path();
3422 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3424 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3428 leaf
= path
->nodes
[0];
3433 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3434 struct btrfs_inode_item
);
3435 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3436 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3437 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3438 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3439 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3441 tspec
= btrfs_inode_atime(inode_item
);
3442 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3443 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3445 tspec
= btrfs_inode_mtime(inode_item
);
3446 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3447 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3449 tspec
= btrfs_inode_ctime(inode_item
);
3450 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3451 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3453 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3454 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3455 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3458 * If we were modified in the current generation and evicted from memory
3459 * and then re-read we need to do a full sync since we don't have any
3460 * idea about which extents were modified before we were evicted from
3463 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3464 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3465 &BTRFS_I(inode
)->runtime_flags
);
3467 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3468 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3470 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3472 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3473 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3477 if (inode
->i_nlink
!= 1 ||
3478 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3481 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3482 if (location
.objectid
!= btrfs_ino(inode
))
3485 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3486 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3487 struct btrfs_inode_ref
*ref
;
3489 ref
= (struct btrfs_inode_ref
*)ptr
;
3490 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3491 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3492 struct btrfs_inode_extref
*extref
;
3494 extref
= (struct btrfs_inode_extref
*)ptr
;
3495 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3500 * try to precache a NULL acl entry for files that don't have
3501 * any xattrs or acls
3503 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3504 btrfs_ino(inode
), &first_xattr_slot
);
3505 if (first_xattr_slot
!= -1) {
3506 path
->slots
[0] = first_xattr_slot
;
3507 ret
= btrfs_load_inode_props(inode
, path
);
3509 btrfs_err(root
->fs_info
,
3510 "error loading props for ino %llu (root %llu): %d",
3512 root
->root_key
.objectid
, ret
);
3514 btrfs_free_path(path
);
3517 cache_no_acl(inode
);
3519 switch (inode
->i_mode
& S_IFMT
) {
3521 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3522 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3523 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3524 inode
->i_fop
= &btrfs_file_operations
;
3525 inode
->i_op
= &btrfs_file_inode_operations
;
3528 inode
->i_fop
= &btrfs_dir_file_operations
;
3529 if (root
== root
->fs_info
->tree_root
)
3530 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3532 inode
->i_op
= &btrfs_dir_inode_operations
;
3535 inode
->i_op
= &btrfs_symlink_inode_operations
;
3536 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3537 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3540 inode
->i_op
= &btrfs_special_inode_operations
;
3541 init_special_inode(inode
, inode
->i_mode
, rdev
);
3545 btrfs_update_iflags(inode
);
3549 btrfs_free_path(path
);
3550 make_bad_inode(inode
);
3554 * given a leaf and an inode, copy the inode fields into the leaf
3556 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3557 struct extent_buffer
*leaf
,
3558 struct btrfs_inode_item
*item
,
3559 struct inode
*inode
)
3561 struct btrfs_map_token token
;
3563 btrfs_init_map_token(&token
);
3565 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3566 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3567 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3569 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3570 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3572 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3573 inode
->i_atime
.tv_sec
, &token
);
3574 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3575 inode
->i_atime
.tv_nsec
, &token
);
3577 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3578 inode
->i_mtime
.tv_sec
, &token
);
3579 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3580 inode
->i_mtime
.tv_nsec
, &token
);
3582 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3583 inode
->i_ctime
.tv_sec
, &token
);
3584 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3585 inode
->i_ctime
.tv_nsec
, &token
);
3587 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3589 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3591 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3592 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3593 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3594 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3595 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3599 * copy everything in the in-memory inode into the btree.
3601 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3602 struct btrfs_root
*root
, struct inode
*inode
)
3604 struct btrfs_inode_item
*inode_item
;
3605 struct btrfs_path
*path
;
3606 struct extent_buffer
*leaf
;
3609 path
= btrfs_alloc_path();
3613 path
->leave_spinning
= 1;
3614 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3622 leaf
= path
->nodes
[0];
3623 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3624 struct btrfs_inode_item
);
3626 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3627 btrfs_mark_buffer_dirty(leaf
);
3628 btrfs_set_inode_last_trans(trans
, inode
);
3631 btrfs_free_path(path
);
3636 * copy everything in the in-memory inode into the btree.
3638 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3639 struct btrfs_root
*root
, struct inode
*inode
)
3644 * If the inode is a free space inode, we can deadlock during commit
3645 * if we put it into the delayed code.
3647 * The data relocation inode should also be directly updated
3650 if (!btrfs_is_free_space_inode(inode
)
3651 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3652 btrfs_update_root_times(trans
, root
);
3654 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3656 btrfs_set_inode_last_trans(trans
, inode
);
3660 return btrfs_update_inode_item(trans
, root
, inode
);
3663 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3664 struct btrfs_root
*root
,
3665 struct inode
*inode
)
3669 ret
= btrfs_update_inode(trans
, root
, inode
);
3671 return btrfs_update_inode_item(trans
, root
, inode
);
3676 * unlink helper that gets used here in inode.c and in the tree logging
3677 * recovery code. It remove a link in a directory with a given name, and
3678 * also drops the back refs in the inode to the directory
3680 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3681 struct btrfs_root
*root
,
3682 struct inode
*dir
, struct inode
*inode
,
3683 const char *name
, int name_len
)
3685 struct btrfs_path
*path
;
3687 struct extent_buffer
*leaf
;
3688 struct btrfs_dir_item
*di
;
3689 struct btrfs_key key
;
3691 u64 ino
= btrfs_ino(inode
);
3692 u64 dir_ino
= btrfs_ino(dir
);
3694 path
= btrfs_alloc_path();
3700 path
->leave_spinning
= 1;
3701 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3702 name
, name_len
, -1);
3711 leaf
= path
->nodes
[0];
3712 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3713 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3716 btrfs_release_path(path
);
3719 * If we don't have dir index, we have to get it by looking up
3720 * the inode ref, since we get the inode ref, remove it directly,
3721 * it is unnecessary to do delayed deletion.
3723 * But if we have dir index, needn't search inode ref to get it.
3724 * Since the inode ref is close to the inode item, it is better
3725 * that we delay to delete it, and just do this deletion when
3726 * we update the inode item.
3728 if (BTRFS_I(inode
)->dir_index
) {
3729 ret
= btrfs_delayed_delete_inode_ref(inode
);
3731 index
= BTRFS_I(inode
)->dir_index
;
3736 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3739 btrfs_info(root
->fs_info
,
3740 "failed to delete reference to %.*s, inode %llu parent %llu",
3741 name_len
, name
, ino
, dir_ino
);
3742 btrfs_abort_transaction(trans
, root
, ret
);
3746 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3748 btrfs_abort_transaction(trans
, root
, ret
);
3752 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3754 if (ret
!= 0 && ret
!= -ENOENT
) {
3755 btrfs_abort_transaction(trans
, root
, ret
);
3759 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3764 btrfs_abort_transaction(trans
, root
, ret
);
3766 btrfs_free_path(path
);
3770 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3771 inode_inc_iversion(inode
);
3772 inode_inc_iversion(dir
);
3773 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3774 ret
= btrfs_update_inode(trans
, root
, dir
);
3779 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3780 struct btrfs_root
*root
,
3781 struct inode
*dir
, struct inode
*inode
,
3782 const char *name
, int name_len
)
3785 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3788 ret
= btrfs_update_inode(trans
, root
, inode
);
3794 * helper to start transaction for unlink and rmdir.
3796 * unlink and rmdir are special in btrfs, they do not always free space, so
3797 * if we cannot make our reservations the normal way try and see if there is
3798 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3799 * allow the unlink to occur.
3801 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3803 struct btrfs_trans_handle
*trans
;
3804 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3808 * 1 for the possible orphan item
3809 * 1 for the dir item
3810 * 1 for the dir index
3811 * 1 for the inode ref
3814 trans
= btrfs_start_transaction(root
, 5);
3815 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3818 if (PTR_ERR(trans
) == -ENOSPC
) {
3819 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3821 trans
= btrfs_start_transaction(root
, 0);
3824 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3825 &root
->fs_info
->trans_block_rsv
,
3828 btrfs_end_transaction(trans
, root
);
3829 return ERR_PTR(ret
);
3831 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3832 trans
->bytes_reserved
= num_bytes
;
3837 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3839 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3840 struct btrfs_trans_handle
*trans
;
3841 struct inode
*inode
= dentry
->d_inode
;
3844 trans
= __unlink_start_trans(dir
);
3846 return PTR_ERR(trans
);
3848 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3850 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3851 dentry
->d_name
.name
, dentry
->d_name
.len
);
3855 if (inode
->i_nlink
== 0) {
3856 ret
= btrfs_orphan_add(trans
, inode
);
3862 btrfs_end_transaction(trans
, root
);
3863 btrfs_btree_balance_dirty(root
);
3867 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3868 struct btrfs_root
*root
,
3869 struct inode
*dir
, u64 objectid
,
3870 const char *name
, int name_len
)
3872 struct btrfs_path
*path
;
3873 struct extent_buffer
*leaf
;
3874 struct btrfs_dir_item
*di
;
3875 struct btrfs_key key
;
3878 u64 dir_ino
= btrfs_ino(dir
);
3880 path
= btrfs_alloc_path();
3884 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3885 name
, name_len
, -1);
3886 if (IS_ERR_OR_NULL(di
)) {
3894 leaf
= path
->nodes
[0];
3895 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3896 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3897 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3899 btrfs_abort_transaction(trans
, root
, ret
);
3902 btrfs_release_path(path
);
3904 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3905 objectid
, root
->root_key
.objectid
,
3906 dir_ino
, &index
, name
, name_len
);
3908 if (ret
!= -ENOENT
) {
3909 btrfs_abort_transaction(trans
, root
, ret
);
3912 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3914 if (IS_ERR_OR_NULL(di
)) {
3919 btrfs_abort_transaction(trans
, root
, ret
);
3923 leaf
= path
->nodes
[0];
3924 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3925 btrfs_release_path(path
);
3928 btrfs_release_path(path
);
3930 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3932 btrfs_abort_transaction(trans
, root
, ret
);
3936 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3937 inode_inc_iversion(dir
);
3938 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3939 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3941 btrfs_abort_transaction(trans
, root
, ret
);
3943 btrfs_free_path(path
);
3947 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3949 struct inode
*inode
= dentry
->d_inode
;
3951 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3952 struct btrfs_trans_handle
*trans
;
3954 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3956 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3959 trans
= __unlink_start_trans(dir
);
3961 return PTR_ERR(trans
);
3963 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3964 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3965 BTRFS_I(inode
)->location
.objectid
,
3966 dentry
->d_name
.name
,
3967 dentry
->d_name
.len
);
3971 err
= btrfs_orphan_add(trans
, inode
);
3975 /* now the directory is empty */
3976 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3977 dentry
->d_name
.name
, dentry
->d_name
.len
);
3979 btrfs_i_size_write(inode
, 0);
3981 btrfs_end_transaction(trans
, root
);
3982 btrfs_btree_balance_dirty(root
);
3988 * this can truncate away extent items, csum items and directory items.
3989 * It starts at a high offset and removes keys until it can't find
3990 * any higher than new_size
3992 * csum items that cross the new i_size are truncated to the new size
3995 * min_type is the minimum key type to truncate down to. If set to 0, this
3996 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3998 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3999 struct btrfs_root
*root
,
4000 struct inode
*inode
,
4001 u64 new_size
, u32 min_type
)
4003 struct btrfs_path
*path
;
4004 struct extent_buffer
*leaf
;
4005 struct btrfs_file_extent_item
*fi
;
4006 struct btrfs_key key
;
4007 struct btrfs_key found_key
;
4008 u64 extent_start
= 0;
4009 u64 extent_num_bytes
= 0;
4010 u64 extent_offset
= 0;
4012 u64 last_size
= (u64
)-1;
4013 u32 found_type
= (u8
)-1;
4016 int pending_del_nr
= 0;
4017 int pending_del_slot
= 0;
4018 int extent_type
= -1;
4021 u64 ino
= btrfs_ino(inode
);
4023 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4025 path
= btrfs_alloc_path();
4031 * We want to drop from the next block forward in case this new size is
4032 * not block aligned since we will be keeping the last block of the
4033 * extent just the way it is.
4035 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4036 root
== root
->fs_info
->tree_root
)
4037 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4038 root
->sectorsize
), (u64
)-1, 0);
4041 * This function is also used to drop the items in the log tree before
4042 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4043 * it is used to drop the loged items. So we shouldn't kill the delayed
4046 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4047 btrfs_kill_delayed_inode_items(inode
);
4050 key
.offset
= (u64
)-1;
4054 path
->leave_spinning
= 1;
4055 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4062 /* there are no items in the tree for us to truncate, we're
4065 if (path
->slots
[0] == 0)
4072 leaf
= path
->nodes
[0];
4073 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4074 found_type
= btrfs_key_type(&found_key
);
4076 if (found_key
.objectid
!= ino
)
4079 if (found_type
< min_type
)
4082 item_end
= found_key
.offset
;
4083 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4084 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4085 struct btrfs_file_extent_item
);
4086 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4087 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4089 btrfs_file_extent_num_bytes(leaf
, fi
);
4090 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4091 item_end
+= btrfs_file_extent_inline_len(leaf
,
4092 path
->slots
[0], fi
);
4096 if (found_type
> min_type
) {
4099 if (item_end
< new_size
)
4101 if (found_key
.offset
>= new_size
)
4107 /* FIXME, shrink the extent if the ref count is only 1 */
4108 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4112 last_size
= found_key
.offset
;
4114 last_size
= new_size
;
4116 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4118 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4120 u64 orig_num_bytes
=
4121 btrfs_file_extent_num_bytes(leaf
, fi
);
4122 extent_num_bytes
= ALIGN(new_size
-
4125 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4127 num_dec
= (orig_num_bytes
-
4129 if (test_bit(BTRFS_ROOT_REF_COWS
,
4132 inode_sub_bytes(inode
, num_dec
);
4133 btrfs_mark_buffer_dirty(leaf
);
4136 btrfs_file_extent_disk_num_bytes(leaf
,
4138 extent_offset
= found_key
.offset
-
4139 btrfs_file_extent_offset(leaf
, fi
);
4141 /* FIXME blocksize != 4096 */
4142 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4143 if (extent_start
!= 0) {
4145 if (test_bit(BTRFS_ROOT_REF_COWS
,
4147 inode_sub_bytes(inode
, num_dec
);
4150 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4152 * we can't truncate inline items that have had
4156 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4157 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4158 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4159 u32 size
= new_size
- found_key
.offset
;
4161 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4162 inode_sub_bytes(inode
, item_end
+ 1 -
4166 * update the ram bytes to properly reflect
4167 * the new size of our item
4169 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4171 btrfs_file_extent_calc_inline_size(size
);
4172 btrfs_truncate_item(root
, path
, size
, 1);
4173 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4175 inode_sub_bytes(inode
, item_end
+ 1 -
4181 if (!pending_del_nr
) {
4182 /* no pending yet, add ourselves */
4183 pending_del_slot
= path
->slots
[0];
4185 } else if (pending_del_nr
&&
4186 path
->slots
[0] + 1 == pending_del_slot
) {
4187 /* hop on the pending chunk */
4189 pending_del_slot
= path
->slots
[0];
4197 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4198 root
== root
->fs_info
->tree_root
)) {
4199 btrfs_set_path_blocking(path
);
4200 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4201 extent_num_bytes
, 0,
4202 btrfs_header_owner(leaf
),
4203 ino
, extent_offset
, 0);
4207 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4210 if (path
->slots
[0] == 0 ||
4211 path
->slots
[0] != pending_del_slot
) {
4212 if (pending_del_nr
) {
4213 ret
= btrfs_del_items(trans
, root
, path
,
4217 btrfs_abort_transaction(trans
,
4223 btrfs_release_path(path
);
4230 if (pending_del_nr
) {
4231 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4234 btrfs_abort_transaction(trans
, root
, ret
);
4237 if (last_size
!= (u64
)-1)
4238 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4239 btrfs_free_path(path
);
4244 * btrfs_truncate_page - read, zero a chunk and write a page
4245 * @inode - inode that we're zeroing
4246 * @from - the offset to start zeroing
4247 * @len - the length to zero, 0 to zero the entire range respective to the
4249 * @front - zero up to the offset instead of from the offset on
4251 * This will find the page for the "from" offset and cow the page and zero the
4252 * part we want to zero. This is used with truncate and hole punching.
4254 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4257 struct address_space
*mapping
= inode
->i_mapping
;
4258 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4259 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4260 struct btrfs_ordered_extent
*ordered
;
4261 struct extent_state
*cached_state
= NULL
;
4263 u32 blocksize
= root
->sectorsize
;
4264 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4265 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4267 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4272 if ((offset
& (blocksize
- 1)) == 0 &&
4273 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4275 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4280 page
= find_or_create_page(mapping
, index
, mask
);
4282 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4287 page_start
= page_offset(page
);
4288 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4290 if (!PageUptodate(page
)) {
4291 ret
= btrfs_readpage(NULL
, page
);
4293 if (page
->mapping
!= mapping
) {
4295 page_cache_release(page
);
4298 if (!PageUptodate(page
)) {
4303 wait_on_page_writeback(page
);
4305 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4306 set_page_extent_mapped(page
);
4308 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4310 unlock_extent_cached(io_tree
, page_start
, page_end
,
4311 &cached_state
, GFP_NOFS
);
4313 page_cache_release(page
);
4314 btrfs_start_ordered_extent(inode
, ordered
, 1);
4315 btrfs_put_ordered_extent(ordered
);
4319 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4320 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4321 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4322 0, 0, &cached_state
, GFP_NOFS
);
4324 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4327 unlock_extent_cached(io_tree
, page_start
, page_end
,
4328 &cached_state
, GFP_NOFS
);
4332 if (offset
!= PAGE_CACHE_SIZE
) {
4334 len
= PAGE_CACHE_SIZE
- offset
;
4337 memset(kaddr
, 0, offset
);
4339 memset(kaddr
+ offset
, 0, len
);
4340 flush_dcache_page(page
);
4343 ClearPageChecked(page
);
4344 set_page_dirty(page
);
4345 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4350 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4352 page_cache_release(page
);
4357 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4358 u64 offset
, u64 len
)
4360 struct btrfs_trans_handle
*trans
;
4364 * Still need to make sure the inode looks like it's been updated so
4365 * that any holes get logged if we fsync.
4367 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4368 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4369 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4370 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4375 * 1 - for the one we're dropping
4376 * 1 - for the one we're adding
4377 * 1 - for updating the inode.
4379 trans
= btrfs_start_transaction(root
, 3);
4381 return PTR_ERR(trans
);
4383 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4385 btrfs_abort_transaction(trans
, root
, ret
);
4386 btrfs_end_transaction(trans
, root
);
4390 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4391 0, 0, len
, 0, len
, 0, 0, 0);
4393 btrfs_abort_transaction(trans
, root
, ret
);
4395 btrfs_update_inode(trans
, root
, inode
);
4396 btrfs_end_transaction(trans
, root
);
4401 * This function puts in dummy file extents for the area we're creating a hole
4402 * for. So if we are truncating this file to a larger size we need to insert
4403 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4404 * the range between oldsize and size
4406 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4408 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4409 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4410 struct extent_map
*em
= NULL
;
4411 struct extent_state
*cached_state
= NULL
;
4412 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4413 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4414 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4421 * If our size started in the middle of a page we need to zero out the
4422 * rest of the page before we expand the i_size, otherwise we could
4423 * expose stale data.
4425 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4429 if (size
<= hole_start
)
4433 struct btrfs_ordered_extent
*ordered
;
4435 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4437 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4438 block_end
- hole_start
);
4441 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4442 &cached_state
, GFP_NOFS
);
4443 btrfs_start_ordered_extent(inode
, ordered
, 1);
4444 btrfs_put_ordered_extent(ordered
);
4447 cur_offset
= hole_start
;
4449 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4450 block_end
- cur_offset
, 0);
4456 last_byte
= min(extent_map_end(em
), block_end
);
4457 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4458 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4459 struct extent_map
*hole_em
;
4460 hole_size
= last_byte
- cur_offset
;
4462 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4466 btrfs_drop_extent_cache(inode
, cur_offset
,
4467 cur_offset
+ hole_size
- 1, 0);
4468 hole_em
= alloc_extent_map();
4470 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4471 &BTRFS_I(inode
)->runtime_flags
);
4474 hole_em
->start
= cur_offset
;
4475 hole_em
->len
= hole_size
;
4476 hole_em
->orig_start
= cur_offset
;
4478 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4479 hole_em
->block_len
= 0;
4480 hole_em
->orig_block_len
= 0;
4481 hole_em
->ram_bytes
= hole_size
;
4482 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4483 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4484 hole_em
->generation
= root
->fs_info
->generation
;
4487 write_lock(&em_tree
->lock
);
4488 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4489 write_unlock(&em_tree
->lock
);
4492 btrfs_drop_extent_cache(inode
, cur_offset
,
4496 free_extent_map(hole_em
);
4499 free_extent_map(em
);
4501 cur_offset
= last_byte
;
4502 if (cur_offset
>= block_end
)
4505 free_extent_map(em
);
4506 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4511 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4513 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4514 struct btrfs_trans_handle
*trans
;
4515 loff_t oldsize
= i_size_read(inode
);
4516 loff_t newsize
= attr
->ia_size
;
4517 int mask
= attr
->ia_valid
;
4521 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4522 * special case where we need to update the times despite not having
4523 * these flags set. For all other operations the VFS set these flags
4524 * explicitly if it wants a timestamp update.
4526 if (newsize
!= oldsize
) {
4527 inode_inc_iversion(inode
);
4528 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4529 inode
->i_ctime
= inode
->i_mtime
=
4530 current_fs_time(inode
->i_sb
);
4533 if (newsize
> oldsize
) {
4534 truncate_pagecache(inode
, newsize
);
4535 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4539 trans
= btrfs_start_transaction(root
, 1);
4541 return PTR_ERR(trans
);
4543 i_size_write(inode
, newsize
);
4544 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4545 ret
= btrfs_update_inode(trans
, root
, inode
);
4546 btrfs_end_transaction(trans
, root
);
4550 * We're truncating a file that used to have good data down to
4551 * zero. Make sure it gets into the ordered flush list so that
4552 * any new writes get down to disk quickly.
4555 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4556 &BTRFS_I(inode
)->runtime_flags
);
4559 * 1 for the orphan item we're going to add
4560 * 1 for the orphan item deletion.
4562 trans
= btrfs_start_transaction(root
, 2);
4564 return PTR_ERR(trans
);
4567 * We need to do this in case we fail at _any_ point during the
4568 * actual truncate. Once we do the truncate_setsize we could
4569 * invalidate pages which forces any outstanding ordered io to
4570 * be instantly completed which will give us extents that need
4571 * to be truncated. If we fail to get an orphan inode down we
4572 * could have left over extents that were never meant to live,
4573 * so we need to garuntee from this point on that everything
4574 * will be consistent.
4576 ret
= btrfs_orphan_add(trans
, inode
);
4577 btrfs_end_transaction(trans
, root
);
4581 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4582 truncate_setsize(inode
, newsize
);
4584 /* Disable nonlocked read DIO to avoid the end less truncate */
4585 btrfs_inode_block_unlocked_dio(inode
);
4586 inode_dio_wait(inode
);
4587 btrfs_inode_resume_unlocked_dio(inode
);
4589 ret
= btrfs_truncate(inode
);
4590 if (ret
&& inode
->i_nlink
) {
4594 * failed to truncate, disk_i_size is only adjusted down
4595 * as we remove extents, so it should represent the true
4596 * size of the inode, so reset the in memory size and
4597 * delete our orphan entry.
4599 trans
= btrfs_join_transaction(root
);
4600 if (IS_ERR(trans
)) {
4601 btrfs_orphan_del(NULL
, inode
);
4604 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4605 err
= btrfs_orphan_del(trans
, inode
);
4607 btrfs_abort_transaction(trans
, root
, err
);
4608 btrfs_end_transaction(trans
, root
);
4615 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4617 struct inode
*inode
= dentry
->d_inode
;
4618 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4621 if (btrfs_root_readonly(root
))
4624 err
= inode_change_ok(inode
, attr
);
4628 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4629 err
= btrfs_setsize(inode
, attr
);
4634 if (attr
->ia_valid
) {
4635 setattr_copy(inode
, attr
);
4636 inode_inc_iversion(inode
);
4637 err
= btrfs_dirty_inode(inode
);
4639 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4640 err
= posix_acl_chmod(inode
, inode
->i_mode
);
4647 * While truncating the inode pages during eviction, we get the VFS calling
4648 * btrfs_invalidatepage() against each page of the inode. This is slow because
4649 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4650 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4651 * extent_state structures over and over, wasting lots of time.
4653 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4654 * those expensive operations on a per page basis and do only the ordered io
4655 * finishing, while we release here the extent_map and extent_state structures,
4656 * without the excessive merging and splitting.
4658 static void evict_inode_truncate_pages(struct inode
*inode
)
4660 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4661 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
4662 struct rb_node
*node
;
4664 ASSERT(inode
->i_state
& I_FREEING
);
4665 truncate_inode_pages_final(&inode
->i_data
);
4667 write_lock(&map_tree
->lock
);
4668 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
4669 struct extent_map
*em
;
4671 node
= rb_first(&map_tree
->map
);
4672 em
= rb_entry(node
, struct extent_map
, rb_node
);
4673 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
4674 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4675 remove_extent_mapping(map_tree
, em
);
4676 free_extent_map(em
);
4677 if (need_resched()) {
4678 write_unlock(&map_tree
->lock
);
4680 write_lock(&map_tree
->lock
);
4683 write_unlock(&map_tree
->lock
);
4685 spin_lock(&io_tree
->lock
);
4686 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
4687 struct extent_state
*state
;
4688 struct extent_state
*cached_state
= NULL
;
4690 node
= rb_first(&io_tree
->state
);
4691 state
= rb_entry(node
, struct extent_state
, rb_node
);
4692 atomic_inc(&state
->refs
);
4693 spin_unlock(&io_tree
->lock
);
4695 lock_extent_bits(io_tree
, state
->start
, state
->end
,
4697 clear_extent_bit(io_tree
, state
->start
, state
->end
,
4698 EXTENT_LOCKED
| EXTENT_DIRTY
|
4699 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
4700 EXTENT_DEFRAG
, 1, 1,
4701 &cached_state
, GFP_NOFS
);
4702 free_extent_state(state
);
4705 spin_lock(&io_tree
->lock
);
4707 spin_unlock(&io_tree
->lock
);
4710 void btrfs_evict_inode(struct inode
*inode
)
4712 struct btrfs_trans_handle
*trans
;
4713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4714 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4715 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4718 trace_btrfs_inode_evict(inode
);
4720 evict_inode_truncate_pages(inode
);
4722 if (inode
->i_nlink
&&
4723 ((btrfs_root_refs(&root
->root_item
) != 0 &&
4724 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
4725 btrfs_is_free_space_inode(inode
)))
4728 if (is_bad_inode(inode
)) {
4729 btrfs_orphan_del(NULL
, inode
);
4732 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4733 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4735 if (root
->fs_info
->log_root_recovering
) {
4736 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4737 &BTRFS_I(inode
)->runtime_flags
));
4741 if (inode
->i_nlink
> 0) {
4742 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
4743 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
4747 ret
= btrfs_commit_inode_delayed_inode(inode
);
4749 btrfs_orphan_del(NULL
, inode
);
4753 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4755 btrfs_orphan_del(NULL
, inode
);
4758 rsv
->size
= min_size
;
4760 global_rsv
= &root
->fs_info
->global_block_rsv
;
4762 btrfs_i_size_write(inode
, 0);
4765 * This is a bit simpler than btrfs_truncate since we've already
4766 * reserved our space for our orphan item in the unlink, so we just
4767 * need to reserve some slack space in case we add bytes and update
4768 * inode item when doing the truncate.
4771 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4772 BTRFS_RESERVE_FLUSH_LIMIT
);
4775 * Try and steal from the global reserve since we will
4776 * likely not use this space anyway, we want to try as
4777 * hard as possible to get this to work.
4780 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4783 btrfs_warn(root
->fs_info
,
4784 "Could not get space for a delete, will truncate on mount %d",
4786 btrfs_orphan_del(NULL
, inode
);
4787 btrfs_free_block_rsv(root
, rsv
);
4791 trans
= btrfs_join_transaction(root
);
4792 if (IS_ERR(trans
)) {
4793 btrfs_orphan_del(NULL
, inode
);
4794 btrfs_free_block_rsv(root
, rsv
);
4798 trans
->block_rsv
= rsv
;
4800 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4804 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4805 btrfs_end_transaction(trans
, root
);
4807 btrfs_btree_balance_dirty(root
);
4810 btrfs_free_block_rsv(root
, rsv
);
4813 * Errors here aren't a big deal, it just means we leave orphan items
4814 * in the tree. They will be cleaned up on the next mount.
4817 trans
->block_rsv
= root
->orphan_block_rsv
;
4818 btrfs_orphan_del(trans
, inode
);
4820 btrfs_orphan_del(NULL
, inode
);
4823 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4824 if (!(root
== root
->fs_info
->tree_root
||
4825 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4826 btrfs_return_ino(root
, btrfs_ino(inode
));
4828 btrfs_end_transaction(trans
, root
);
4829 btrfs_btree_balance_dirty(root
);
4831 btrfs_remove_delayed_node(inode
);
4837 * this returns the key found in the dir entry in the location pointer.
4838 * If no dir entries were found, location->objectid is 0.
4840 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4841 struct btrfs_key
*location
)
4843 const char *name
= dentry
->d_name
.name
;
4844 int namelen
= dentry
->d_name
.len
;
4845 struct btrfs_dir_item
*di
;
4846 struct btrfs_path
*path
;
4847 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4850 path
= btrfs_alloc_path();
4854 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4859 if (IS_ERR_OR_NULL(di
))
4862 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4864 btrfs_free_path(path
);
4867 location
->objectid
= 0;
4872 * when we hit a tree root in a directory, the btrfs part of the inode
4873 * needs to be changed to reflect the root directory of the tree root. This
4874 * is kind of like crossing a mount point.
4876 static int fixup_tree_root_location(struct btrfs_root
*root
,
4878 struct dentry
*dentry
,
4879 struct btrfs_key
*location
,
4880 struct btrfs_root
**sub_root
)
4882 struct btrfs_path
*path
;
4883 struct btrfs_root
*new_root
;
4884 struct btrfs_root_ref
*ref
;
4885 struct extent_buffer
*leaf
;
4889 path
= btrfs_alloc_path();
4896 ret
= btrfs_find_item(root
->fs_info
->tree_root
, path
,
4897 BTRFS_I(dir
)->root
->root_key
.objectid
,
4898 location
->objectid
, BTRFS_ROOT_REF_KEY
, NULL
);
4905 leaf
= path
->nodes
[0];
4906 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4907 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4908 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4911 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4912 (unsigned long)(ref
+ 1),
4913 dentry
->d_name
.len
);
4917 btrfs_release_path(path
);
4919 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4920 if (IS_ERR(new_root
)) {
4921 err
= PTR_ERR(new_root
);
4925 *sub_root
= new_root
;
4926 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4927 location
->type
= BTRFS_INODE_ITEM_KEY
;
4928 location
->offset
= 0;
4931 btrfs_free_path(path
);
4935 static void inode_tree_add(struct inode
*inode
)
4937 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4938 struct btrfs_inode
*entry
;
4940 struct rb_node
*parent
;
4941 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
4942 u64 ino
= btrfs_ino(inode
);
4944 if (inode_unhashed(inode
))
4947 spin_lock(&root
->inode_lock
);
4948 p
= &root
->inode_tree
.rb_node
;
4951 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4953 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4954 p
= &parent
->rb_left
;
4955 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4956 p
= &parent
->rb_right
;
4958 WARN_ON(!(entry
->vfs_inode
.i_state
&
4959 (I_WILL_FREE
| I_FREEING
)));
4960 rb_replace_node(parent
, new, &root
->inode_tree
);
4961 RB_CLEAR_NODE(parent
);
4962 spin_unlock(&root
->inode_lock
);
4966 rb_link_node(new, parent
, p
);
4967 rb_insert_color(new, &root
->inode_tree
);
4968 spin_unlock(&root
->inode_lock
);
4971 static void inode_tree_del(struct inode
*inode
)
4973 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4976 spin_lock(&root
->inode_lock
);
4977 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4978 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4979 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4980 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4982 spin_unlock(&root
->inode_lock
);
4984 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
4985 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4986 spin_lock(&root
->inode_lock
);
4987 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4988 spin_unlock(&root
->inode_lock
);
4990 btrfs_add_dead_root(root
);
4994 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4996 struct rb_node
*node
;
4997 struct rb_node
*prev
;
4998 struct btrfs_inode
*entry
;
4999 struct inode
*inode
;
5002 if (!test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
5003 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5005 spin_lock(&root
->inode_lock
);
5007 node
= root
->inode_tree
.rb_node
;
5011 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5013 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5014 node
= node
->rb_left
;
5015 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5016 node
= node
->rb_right
;
5022 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5023 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5027 prev
= rb_next(prev
);
5031 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5032 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5033 inode
= igrab(&entry
->vfs_inode
);
5035 spin_unlock(&root
->inode_lock
);
5036 if (atomic_read(&inode
->i_count
) > 1)
5037 d_prune_aliases(inode
);
5039 * btrfs_drop_inode will have it removed from
5040 * the inode cache when its usage count
5045 spin_lock(&root
->inode_lock
);
5049 if (cond_resched_lock(&root
->inode_lock
))
5052 node
= rb_next(node
);
5054 spin_unlock(&root
->inode_lock
);
5057 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5059 struct btrfs_iget_args
*args
= p
;
5060 inode
->i_ino
= args
->location
->objectid
;
5061 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5062 sizeof(*args
->location
));
5063 BTRFS_I(inode
)->root
= args
->root
;
5067 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5069 struct btrfs_iget_args
*args
= opaque
;
5070 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5071 args
->root
== BTRFS_I(inode
)->root
;
5074 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5075 struct btrfs_key
*location
,
5076 struct btrfs_root
*root
)
5078 struct inode
*inode
;
5079 struct btrfs_iget_args args
;
5080 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5082 args
.location
= location
;
5085 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5086 btrfs_init_locked_inode
,
5091 /* Get an inode object given its location and corresponding root.
5092 * Returns in *is_new if the inode was read from disk
5094 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5095 struct btrfs_root
*root
, int *new)
5097 struct inode
*inode
;
5099 inode
= btrfs_iget_locked(s
, location
, root
);
5101 return ERR_PTR(-ENOMEM
);
5103 if (inode
->i_state
& I_NEW
) {
5104 btrfs_read_locked_inode(inode
);
5105 if (!is_bad_inode(inode
)) {
5106 inode_tree_add(inode
);
5107 unlock_new_inode(inode
);
5111 unlock_new_inode(inode
);
5113 inode
= ERR_PTR(-ESTALE
);
5120 static struct inode
*new_simple_dir(struct super_block
*s
,
5121 struct btrfs_key
*key
,
5122 struct btrfs_root
*root
)
5124 struct inode
*inode
= new_inode(s
);
5127 return ERR_PTR(-ENOMEM
);
5129 BTRFS_I(inode
)->root
= root
;
5130 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5131 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5133 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5134 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5135 inode
->i_fop
= &simple_dir_operations
;
5136 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5137 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5142 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5144 struct inode
*inode
;
5145 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5146 struct btrfs_root
*sub_root
= root
;
5147 struct btrfs_key location
;
5151 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5152 return ERR_PTR(-ENAMETOOLONG
);
5154 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5156 return ERR_PTR(ret
);
5158 if (location
.objectid
== 0)
5159 return ERR_PTR(-ENOENT
);
5161 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5162 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5166 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5168 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5169 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5170 &location
, &sub_root
);
5173 inode
= ERR_PTR(ret
);
5175 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5177 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5179 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5181 if (!IS_ERR(inode
) && root
!= sub_root
) {
5182 down_read(&root
->fs_info
->cleanup_work_sem
);
5183 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5184 ret
= btrfs_orphan_cleanup(sub_root
);
5185 up_read(&root
->fs_info
->cleanup_work_sem
);
5188 inode
= ERR_PTR(ret
);
5191 * If orphan cleanup did remove any orphans, it means the tree
5192 * was modified and therefore the commit root is not the same as
5193 * the current root anymore. This is a problem, because send
5194 * uses the commit root and therefore can see inode items that
5195 * don't exist in the current root anymore, and for example make
5196 * calls to btrfs_iget, which will do tree lookups based on the
5197 * current root and not on the commit root. Those lookups will
5198 * fail, returning a -ESTALE error, and making send fail with
5199 * that error. So make sure a send does not see any orphans we
5200 * have just removed, and that it will see the same inodes
5201 * regardless of whether a transaction commit happened before
5202 * it started (meaning that the commit root will be the same as
5203 * the current root) or not.
5205 if (sub_root
->node
!= sub_root
->commit_root
) {
5206 u64 sub_flags
= btrfs_root_flags(&sub_root
->root_item
);
5208 if (sub_flags
& BTRFS_ROOT_SUBVOL_RDONLY
) {
5209 struct extent_buffer
*eb
;
5212 * Assert we can't have races between dentry
5213 * lookup called through the snapshot creation
5214 * ioctl and the VFS.
5216 ASSERT(mutex_is_locked(&dir
->i_mutex
));
5218 down_write(&root
->fs_info
->commit_root_sem
);
5219 eb
= sub_root
->commit_root
;
5220 sub_root
->commit_root
=
5221 btrfs_root_node(sub_root
);
5222 up_write(&root
->fs_info
->commit_root_sem
);
5223 free_extent_buffer(eb
);
5231 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5233 struct btrfs_root
*root
;
5234 struct inode
*inode
= dentry
->d_inode
;
5236 if (!inode
&& !IS_ROOT(dentry
))
5237 inode
= dentry
->d_parent
->d_inode
;
5240 root
= BTRFS_I(inode
)->root
;
5241 if (btrfs_root_refs(&root
->root_item
) == 0)
5244 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5250 static void btrfs_dentry_release(struct dentry
*dentry
)
5252 kfree(dentry
->d_fsdata
);
5255 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5258 struct inode
*inode
;
5260 inode
= btrfs_lookup_dentry(dir
, dentry
);
5261 if (IS_ERR(inode
)) {
5262 if (PTR_ERR(inode
) == -ENOENT
)
5265 return ERR_CAST(inode
);
5268 return d_materialise_unique(dentry
, inode
);
5271 unsigned char btrfs_filetype_table
[] = {
5272 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5275 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5277 struct inode
*inode
= file_inode(file
);
5278 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5279 struct btrfs_item
*item
;
5280 struct btrfs_dir_item
*di
;
5281 struct btrfs_key key
;
5282 struct btrfs_key found_key
;
5283 struct btrfs_path
*path
;
5284 struct list_head ins_list
;
5285 struct list_head del_list
;
5287 struct extent_buffer
*leaf
;
5289 unsigned char d_type
;
5294 int key_type
= BTRFS_DIR_INDEX_KEY
;
5298 int is_curr
= 0; /* ctx->pos points to the current index? */
5300 /* FIXME, use a real flag for deciding about the key type */
5301 if (root
->fs_info
->tree_root
== root
)
5302 key_type
= BTRFS_DIR_ITEM_KEY
;
5304 if (!dir_emit_dots(file
, ctx
))
5307 path
= btrfs_alloc_path();
5313 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5314 INIT_LIST_HEAD(&ins_list
);
5315 INIT_LIST_HEAD(&del_list
);
5316 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5319 btrfs_set_key_type(&key
, key_type
);
5320 key
.offset
= ctx
->pos
;
5321 key
.objectid
= btrfs_ino(inode
);
5323 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5328 leaf
= path
->nodes
[0];
5329 slot
= path
->slots
[0];
5330 if (slot
>= btrfs_header_nritems(leaf
)) {
5331 ret
= btrfs_next_leaf(root
, path
);
5339 item
= btrfs_item_nr(slot
);
5340 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5342 if (found_key
.objectid
!= key
.objectid
)
5344 if (btrfs_key_type(&found_key
) != key_type
)
5346 if (found_key
.offset
< ctx
->pos
)
5348 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5349 btrfs_should_delete_dir_index(&del_list
,
5353 ctx
->pos
= found_key
.offset
;
5356 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5358 di_total
= btrfs_item_size(leaf
, item
);
5360 while (di_cur
< di_total
) {
5361 struct btrfs_key location
;
5363 if (verify_dir_item(root
, leaf
, di
))
5366 name_len
= btrfs_dir_name_len(leaf
, di
);
5367 if (name_len
<= sizeof(tmp_name
)) {
5368 name_ptr
= tmp_name
;
5370 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5376 read_extent_buffer(leaf
, name_ptr
,
5377 (unsigned long)(di
+ 1), name_len
);
5379 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5380 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5383 /* is this a reference to our own snapshot? If so
5386 * In contrast to old kernels, we insert the snapshot's
5387 * dir item and dir index after it has been created, so
5388 * we won't find a reference to our own snapshot. We
5389 * still keep the following code for backward
5392 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5393 location
.objectid
== root
->root_key
.objectid
) {
5397 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5398 location
.objectid
, d_type
);
5401 if (name_ptr
!= tmp_name
)
5406 di_len
= btrfs_dir_name_len(leaf
, di
) +
5407 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5409 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5415 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5418 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5423 /* Reached end of directory/root. Bump pos past the last item. */
5427 * Stop new entries from being returned after we return the last
5430 * New directory entries are assigned a strictly increasing
5431 * offset. This means that new entries created during readdir
5432 * are *guaranteed* to be seen in the future by that readdir.
5433 * This has broken buggy programs which operate on names as
5434 * they're returned by readdir. Until we re-use freed offsets
5435 * we have this hack to stop new entries from being returned
5436 * under the assumption that they'll never reach this huge
5439 * This is being careful not to overflow 32bit loff_t unless the
5440 * last entry requires it because doing so has broken 32bit apps
5443 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5444 if (ctx
->pos
>= INT_MAX
)
5445 ctx
->pos
= LLONG_MAX
;
5452 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5453 btrfs_put_delayed_items(&ins_list
, &del_list
);
5454 btrfs_free_path(path
);
5458 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5460 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5461 struct btrfs_trans_handle
*trans
;
5463 bool nolock
= false;
5465 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5468 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5471 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5473 trans
= btrfs_join_transaction_nolock(root
);
5475 trans
= btrfs_join_transaction(root
);
5477 return PTR_ERR(trans
);
5478 ret
= btrfs_commit_transaction(trans
, root
);
5484 * This is somewhat expensive, updating the tree every time the
5485 * inode changes. But, it is most likely to find the inode in cache.
5486 * FIXME, needs more benchmarking...there are no reasons other than performance
5487 * to keep or drop this code.
5489 static int btrfs_dirty_inode(struct inode
*inode
)
5491 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5492 struct btrfs_trans_handle
*trans
;
5495 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5498 trans
= btrfs_join_transaction(root
);
5500 return PTR_ERR(trans
);
5502 ret
= btrfs_update_inode(trans
, root
, inode
);
5503 if (ret
&& ret
== -ENOSPC
) {
5504 /* whoops, lets try again with the full transaction */
5505 btrfs_end_transaction(trans
, root
);
5506 trans
= btrfs_start_transaction(root
, 1);
5508 return PTR_ERR(trans
);
5510 ret
= btrfs_update_inode(trans
, root
, inode
);
5512 btrfs_end_transaction(trans
, root
);
5513 if (BTRFS_I(inode
)->delayed_node
)
5514 btrfs_balance_delayed_items(root
);
5520 * This is a copy of file_update_time. We need this so we can return error on
5521 * ENOSPC for updating the inode in the case of file write and mmap writes.
5523 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5526 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5528 if (btrfs_root_readonly(root
))
5531 if (flags
& S_VERSION
)
5532 inode_inc_iversion(inode
);
5533 if (flags
& S_CTIME
)
5534 inode
->i_ctime
= *now
;
5535 if (flags
& S_MTIME
)
5536 inode
->i_mtime
= *now
;
5537 if (flags
& S_ATIME
)
5538 inode
->i_atime
= *now
;
5539 return btrfs_dirty_inode(inode
);
5543 * find the highest existing sequence number in a directory
5544 * and then set the in-memory index_cnt variable to reflect
5545 * free sequence numbers
5547 static int btrfs_set_inode_index_count(struct inode
*inode
)
5549 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5550 struct btrfs_key key
, found_key
;
5551 struct btrfs_path
*path
;
5552 struct extent_buffer
*leaf
;
5555 key
.objectid
= btrfs_ino(inode
);
5556 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5557 key
.offset
= (u64
)-1;
5559 path
= btrfs_alloc_path();
5563 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5566 /* FIXME: we should be able to handle this */
5572 * MAGIC NUMBER EXPLANATION:
5573 * since we search a directory based on f_pos we have to start at 2
5574 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5575 * else has to start at 2
5577 if (path
->slots
[0] == 0) {
5578 BTRFS_I(inode
)->index_cnt
= 2;
5584 leaf
= path
->nodes
[0];
5585 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5587 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5588 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5589 BTRFS_I(inode
)->index_cnt
= 2;
5593 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5595 btrfs_free_path(path
);
5600 * helper to find a free sequence number in a given directory. This current
5601 * code is very simple, later versions will do smarter things in the btree
5603 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5607 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5608 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5610 ret
= btrfs_set_inode_index_count(dir
);
5616 *index
= BTRFS_I(dir
)->index_cnt
;
5617 BTRFS_I(dir
)->index_cnt
++;
5622 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5623 struct btrfs_root
*root
,
5625 const char *name
, int name_len
,
5626 u64 ref_objectid
, u64 objectid
,
5627 umode_t mode
, u64
*index
)
5629 struct inode
*inode
;
5630 struct btrfs_inode_item
*inode_item
;
5631 struct btrfs_key
*location
;
5632 struct btrfs_path
*path
;
5633 struct btrfs_inode_ref
*ref
;
5634 struct btrfs_key key
[2];
5636 int nitems
= name
? 2 : 1;
5640 path
= btrfs_alloc_path();
5642 return ERR_PTR(-ENOMEM
);
5644 inode
= new_inode(root
->fs_info
->sb
);
5646 btrfs_free_path(path
);
5647 return ERR_PTR(-ENOMEM
);
5651 * O_TMPFILE, set link count to 0, so that after this point,
5652 * we fill in an inode item with the correct link count.
5655 set_nlink(inode
, 0);
5658 * we have to initialize this early, so we can reclaim the inode
5659 * number if we fail afterwards in this function.
5661 inode
->i_ino
= objectid
;
5664 trace_btrfs_inode_request(dir
);
5666 ret
= btrfs_set_inode_index(dir
, index
);
5668 btrfs_free_path(path
);
5670 return ERR_PTR(ret
);
5676 * index_cnt is ignored for everything but a dir,
5677 * btrfs_get_inode_index_count has an explanation for the magic
5680 BTRFS_I(inode
)->index_cnt
= 2;
5681 BTRFS_I(inode
)->dir_index
= *index
;
5682 BTRFS_I(inode
)->root
= root
;
5683 BTRFS_I(inode
)->generation
= trans
->transid
;
5684 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5687 * We could have gotten an inode number from somebody who was fsynced
5688 * and then removed in this same transaction, so let's just set full
5689 * sync since it will be a full sync anyway and this will blow away the
5690 * old info in the log.
5692 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5694 key
[0].objectid
= objectid
;
5695 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5698 sizes
[0] = sizeof(struct btrfs_inode_item
);
5702 * Start new inodes with an inode_ref. This is slightly more
5703 * efficient for small numbers of hard links since they will
5704 * be packed into one item. Extended refs will kick in if we
5705 * add more hard links than can fit in the ref item.
5707 key
[1].objectid
= objectid
;
5708 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5709 key
[1].offset
= ref_objectid
;
5711 sizes
[1] = name_len
+ sizeof(*ref
);
5714 path
->leave_spinning
= 1;
5715 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
5719 inode_init_owner(inode
, dir
, mode
);
5720 inode_set_bytes(inode
, 0);
5721 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5722 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5723 struct btrfs_inode_item
);
5724 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5725 sizeof(*inode_item
));
5726 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5729 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5730 struct btrfs_inode_ref
);
5731 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5732 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5733 ptr
= (unsigned long)(ref
+ 1);
5734 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5737 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5738 btrfs_free_path(path
);
5740 location
= &BTRFS_I(inode
)->location
;
5741 location
->objectid
= objectid
;
5742 location
->offset
= 0;
5743 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5745 btrfs_inherit_iflags(inode
, dir
);
5747 if (S_ISREG(mode
)) {
5748 if (btrfs_test_opt(root
, NODATASUM
))
5749 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5750 if (btrfs_test_opt(root
, NODATACOW
))
5751 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5752 BTRFS_INODE_NODATASUM
;
5755 btrfs_insert_inode_hash(inode
);
5756 inode_tree_add(inode
);
5758 trace_btrfs_inode_new(inode
);
5759 btrfs_set_inode_last_trans(trans
, inode
);
5761 btrfs_update_root_times(trans
, root
);
5763 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
5765 btrfs_err(root
->fs_info
,
5766 "error inheriting props for ino %llu (root %llu): %d",
5767 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
5772 BTRFS_I(dir
)->index_cnt
--;
5773 btrfs_free_path(path
);
5775 return ERR_PTR(ret
);
5778 static inline u8
btrfs_inode_type(struct inode
*inode
)
5780 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5784 * utility function to add 'inode' into 'parent_inode' with
5785 * a give name and a given sequence number.
5786 * if 'add_backref' is true, also insert a backref from the
5787 * inode to the parent directory.
5789 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5790 struct inode
*parent_inode
, struct inode
*inode
,
5791 const char *name
, int name_len
, int add_backref
, u64 index
)
5794 struct btrfs_key key
;
5795 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5796 u64 ino
= btrfs_ino(inode
);
5797 u64 parent_ino
= btrfs_ino(parent_inode
);
5799 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5800 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5803 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5807 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5808 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5809 key
.objectid
, root
->root_key
.objectid
,
5810 parent_ino
, index
, name
, name_len
);
5811 } else if (add_backref
) {
5812 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5816 /* Nothing to clean up yet */
5820 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5822 btrfs_inode_type(inode
), index
);
5823 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5826 btrfs_abort_transaction(trans
, root
, ret
);
5830 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5832 inode_inc_iversion(parent_inode
);
5833 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5834 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5836 btrfs_abort_transaction(trans
, root
, ret
);
5840 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5843 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5844 key
.objectid
, root
->root_key
.objectid
,
5845 parent_ino
, &local_index
, name
, name_len
);
5847 } else if (add_backref
) {
5851 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5852 ino
, parent_ino
, &local_index
);
5857 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5858 struct inode
*dir
, struct dentry
*dentry
,
5859 struct inode
*inode
, int backref
, u64 index
)
5861 int err
= btrfs_add_link(trans
, dir
, inode
,
5862 dentry
->d_name
.name
, dentry
->d_name
.len
,
5869 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5870 umode_t mode
, dev_t rdev
)
5872 struct btrfs_trans_handle
*trans
;
5873 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5874 struct inode
*inode
= NULL
;
5880 if (!new_valid_dev(rdev
))
5884 * 2 for inode item and ref
5886 * 1 for xattr if selinux is on
5888 trans
= btrfs_start_transaction(root
, 5);
5890 return PTR_ERR(trans
);
5892 err
= btrfs_find_free_ino(root
, &objectid
);
5896 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5897 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5899 if (IS_ERR(inode
)) {
5900 err
= PTR_ERR(inode
);
5904 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5911 * If the active LSM wants to access the inode during
5912 * d_instantiate it needs these. Smack checks to see
5913 * if the filesystem supports xattrs by looking at the
5917 inode
->i_op
= &btrfs_special_inode_operations
;
5918 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5922 init_special_inode(inode
, inode
->i_mode
, rdev
);
5923 btrfs_update_inode(trans
, root
, inode
);
5924 d_instantiate(dentry
, inode
);
5927 btrfs_end_transaction(trans
, root
);
5928 btrfs_balance_delayed_items(root
);
5929 btrfs_btree_balance_dirty(root
);
5931 inode_dec_link_count(inode
);
5937 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5938 umode_t mode
, bool excl
)
5940 struct btrfs_trans_handle
*trans
;
5941 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5942 struct inode
*inode
= NULL
;
5943 int drop_inode_on_err
= 0;
5949 * 2 for inode item and ref
5951 * 1 for xattr if selinux is on
5953 trans
= btrfs_start_transaction(root
, 5);
5955 return PTR_ERR(trans
);
5957 err
= btrfs_find_free_ino(root
, &objectid
);
5961 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5962 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5964 if (IS_ERR(inode
)) {
5965 err
= PTR_ERR(inode
);
5968 drop_inode_on_err
= 1;
5970 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5974 err
= btrfs_update_inode(trans
, root
, inode
);
5979 * If the active LSM wants to access the inode during
5980 * d_instantiate it needs these. Smack checks to see
5981 * if the filesystem supports xattrs by looking at the
5984 inode
->i_fop
= &btrfs_file_operations
;
5985 inode
->i_op
= &btrfs_file_inode_operations
;
5987 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5991 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5992 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5993 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5994 d_instantiate(dentry
, inode
);
5997 btrfs_end_transaction(trans
, root
);
5998 if (err
&& drop_inode_on_err
) {
5999 inode_dec_link_count(inode
);
6002 btrfs_balance_delayed_items(root
);
6003 btrfs_btree_balance_dirty(root
);
6007 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6008 struct dentry
*dentry
)
6010 struct btrfs_trans_handle
*trans
;
6011 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6012 struct inode
*inode
= old_dentry
->d_inode
;
6017 /* do not allow sys_link's with other subvols of the same device */
6018 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6021 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6024 err
= btrfs_set_inode_index(dir
, &index
);
6029 * 2 items for inode and inode ref
6030 * 2 items for dir items
6031 * 1 item for parent inode
6033 trans
= btrfs_start_transaction(root
, 5);
6034 if (IS_ERR(trans
)) {
6035 err
= PTR_ERR(trans
);
6039 /* There are several dir indexes for this inode, clear the cache. */
6040 BTRFS_I(inode
)->dir_index
= 0ULL;
6042 inode_inc_iversion(inode
);
6043 inode
->i_ctime
= CURRENT_TIME
;
6045 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6047 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6052 struct dentry
*parent
= dentry
->d_parent
;
6053 err
= btrfs_update_inode(trans
, root
, inode
);
6056 if (inode
->i_nlink
== 1) {
6058 * If new hard link count is 1, it's a file created
6059 * with open(2) O_TMPFILE flag.
6061 err
= btrfs_orphan_del(trans
, inode
);
6065 d_instantiate(dentry
, inode
);
6066 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6069 btrfs_end_transaction(trans
, root
);
6070 btrfs_balance_delayed_items(root
);
6073 inode_dec_link_count(inode
);
6076 btrfs_btree_balance_dirty(root
);
6080 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6082 struct inode
*inode
= NULL
;
6083 struct btrfs_trans_handle
*trans
;
6084 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6086 int drop_on_err
= 0;
6091 * 2 items for inode and ref
6092 * 2 items for dir items
6093 * 1 for xattr if selinux is on
6095 trans
= btrfs_start_transaction(root
, 5);
6097 return PTR_ERR(trans
);
6099 err
= btrfs_find_free_ino(root
, &objectid
);
6103 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6104 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6105 S_IFDIR
| mode
, &index
);
6106 if (IS_ERR(inode
)) {
6107 err
= PTR_ERR(inode
);
6113 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6117 inode
->i_op
= &btrfs_dir_inode_operations
;
6118 inode
->i_fop
= &btrfs_dir_file_operations
;
6120 btrfs_i_size_write(inode
, 0);
6121 err
= btrfs_update_inode(trans
, root
, inode
);
6125 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6126 dentry
->d_name
.len
, 0, index
);
6130 d_instantiate(dentry
, inode
);
6134 btrfs_end_transaction(trans
, root
);
6137 btrfs_balance_delayed_items(root
);
6138 btrfs_btree_balance_dirty(root
);
6142 /* helper for btfs_get_extent. Given an existing extent in the tree,
6143 * and an extent that you want to insert, deal with overlap and insert
6144 * the new extent into the tree.
6146 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6147 struct extent_map
*existing
,
6148 struct extent_map
*em
,
6153 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6154 start_diff
= map_start
- em
->start
;
6155 em
->start
= map_start
;
6156 em
->len
= existing
->start
- em
->start
;
6157 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6158 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6159 em
->block_start
+= start_diff
;
6160 em
->block_len
-= start_diff
;
6162 return add_extent_mapping(em_tree
, em
, 0);
6165 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6166 struct inode
*inode
, struct page
*page
,
6167 size_t pg_offset
, u64 extent_offset
,
6168 struct btrfs_file_extent_item
*item
)
6171 struct extent_buffer
*leaf
= path
->nodes
[0];
6174 unsigned long inline_size
;
6178 WARN_ON(pg_offset
!= 0);
6179 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6180 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6181 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6182 btrfs_item_nr(path
->slots
[0]));
6183 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6186 ptr
= btrfs_file_extent_inline_start(item
);
6188 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6190 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6191 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6192 extent_offset
, inline_size
, max_size
);
6198 * a bit scary, this does extent mapping from logical file offset to the disk.
6199 * the ugly parts come from merging extents from the disk with the in-ram
6200 * representation. This gets more complex because of the data=ordered code,
6201 * where the in-ram extents might be locked pending data=ordered completion.
6203 * This also copies inline extents directly into the page.
6206 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6207 size_t pg_offset
, u64 start
, u64 len
,
6212 u64 extent_start
= 0;
6214 u64 objectid
= btrfs_ino(inode
);
6216 struct btrfs_path
*path
= NULL
;
6217 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6218 struct btrfs_file_extent_item
*item
;
6219 struct extent_buffer
*leaf
;
6220 struct btrfs_key found_key
;
6221 struct extent_map
*em
= NULL
;
6222 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6223 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6224 struct btrfs_trans_handle
*trans
= NULL
;
6225 const bool new_inline
= !page
|| create
;
6228 read_lock(&em_tree
->lock
);
6229 em
= lookup_extent_mapping(em_tree
, start
, len
);
6231 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6232 read_unlock(&em_tree
->lock
);
6235 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6236 free_extent_map(em
);
6237 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6238 free_extent_map(em
);
6242 em
= alloc_extent_map();
6247 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6248 em
->start
= EXTENT_MAP_HOLE
;
6249 em
->orig_start
= EXTENT_MAP_HOLE
;
6251 em
->block_len
= (u64
)-1;
6254 path
= btrfs_alloc_path();
6260 * Chances are we'll be called again, so go ahead and do
6266 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6267 objectid
, start
, trans
!= NULL
);
6274 if (path
->slots
[0] == 0)
6279 leaf
= path
->nodes
[0];
6280 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6281 struct btrfs_file_extent_item
);
6282 /* are we inside the extent that was found? */
6283 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6284 found_type
= btrfs_key_type(&found_key
);
6285 if (found_key
.objectid
!= objectid
||
6286 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6288 * If we backup past the first extent we want to move forward
6289 * and see if there is an extent in front of us, otherwise we'll
6290 * say there is a hole for our whole search range which can
6297 found_type
= btrfs_file_extent_type(leaf
, item
);
6298 extent_start
= found_key
.offset
;
6299 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6300 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6301 extent_end
= extent_start
+
6302 btrfs_file_extent_num_bytes(leaf
, item
);
6303 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6305 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6306 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6309 if (start
>= extent_end
) {
6311 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6312 ret
= btrfs_next_leaf(root
, path
);
6319 leaf
= path
->nodes
[0];
6321 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6322 if (found_key
.objectid
!= objectid
||
6323 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6325 if (start
+ len
<= found_key
.offset
)
6327 if (start
> found_key
.offset
)
6330 em
->orig_start
= start
;
6331 em
->len
= found_key
.offset
- start
;
6335 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6337 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6338 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6340 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6344 size_t extent_offset
;
6350 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6351 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6352 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6353 size
- extent_offset
);
6354 em
->start
= extent_start
+ extent_offset
;
6355 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6356 em
->orig_block_len
= em
->len
;
6357 em
->orig_start
= em
->start
;
6358 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6359 if (create
== 0 && !PageUptodate(page
)) {
6360 if (btrfs_file_extent_compression(leaf
, item
) !=
6361 BTRFS_COMPRESS_NONE
) {
6362 ret
= uncompress_inline(path
, inode
, page
,
6364 extent_offset
, item
);
6371 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6373 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6374 memset(map
+ pg_offset
+ copy_size
, 0,
6375 PAGE_CACHE_SIZE
- pg_offset
-
6380 flush_dcache_page(page
);
6381 } else if (create
&& PageUptodate(page
)) {
6385 free_extent_map(em
);
6388 btrfs_release_path(path
);
6389 trans
= btrfs_join_transaction(root
);
6392 return ERR_CAST(trans
);
6396 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6399 btrfs_mark_buffer_dirty(leaf
);
6401 set_extent_uptodate(io_tree
, em
->start
,
6402 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6407 em
->orig_start
= start
;
6410 em
->block_start
= EXTENT_MAP_HOLE
;
6411 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6413 btrfs_release_path(path
);
6414 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6415 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6416 em
->start
, em
->len
, start
, len
);
6422 write_lock(&em_tree
->lock
);
6423 ret
= add_extent_mapping(em_tree
, em
, 0);
6424 /* it is possible that someone inserted the extent into the tree
6425 * while we had the lock dropped. It is also possible that
6426 * an overlapping map exists in the tree
6428 if (ret
== -EEXIST
) {
6429 struct extent_map
*existing
;
6433 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6434 if (existing
&& (existing
->start
> start
||
6435 existing
->start
+ existing
->len
<= start
)) {
6436 free_extent_map(existing
);
6440 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6443 err
= merge_extent_mapping(em_tree
, existing
,
6445 free_extent_map(existing
);
6447 free_extent_map(em
);
6452 free_extent_map(em
);
6456 free_extent_map(em
);
6461 write_unlock(&em_tree
->lock
);
6464 trace_btrfs_get_extent(root
, em
);
6467 btrfs_free_path(path
);
6469 ret
= btrfs_end_transaction(trans
, root
);
6474 free_extent_map(em
);
6475 return ERR_PTR(err
);
6477 BUG_ON(!em
); /* Error is always set */
6481 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6482 size_t pg_offset
, u64 start
, u64 len
,
6485 struct extent_map
*em
;
6486 struct extent_map
*hole_em
= NULL
;
6487 u64 range_start
= start
;
6493 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6500 * - a pre-alloc extent,
6501 * there might actually be delalloc bytes behind it.
6503 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6504 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6510 /* check to see if we've wrapped (len == -1 or similar) */
6519 /* ok, we didn't find anything, lets look for delalloc */
6520 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6521 end
, len
, EXTENT_DELALLOC
, 1);
6522 found_end
= range_start
+ found
;
6523 if (found_end
< range_start
)
6524 found_end
= (u64
)-1;
6527 * we didn't find anything useful, return
6528 * the original results from get_extent()
6530 if (range_start
> end
|| found_end
<= start
) {
6536 /* adjust the range_start to make sure it doesn't
6537 * go backwards from the start they passed in
6539 range_start
= max(start
, range_start
);
6540 found
= found_end
- range_start
;
6543 u64 hole_start
= start
;
6546 em
= alloc_extent_map();
6552 * when btrfs_get_extent can't find anything it
6553 * returns one huge hole
6555 * make sure what it found really fits our range, and
6556 * adjust to make sure it is based on the start from
6560 u64 calc_end
= extent_map_end(hole_em
);
6562 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6563 free_extent_map(hole_em
);
6566 hole_start
= max(hole_em
->start
, start
);
6567 hole_len
= calc_end
- hole_start
;
6571 if (hole_em
&& range_start
> hole_start
) {
6572 /* our hole starts before our delalloc, so we
6573 * have to return just the parts of the hole
6574 * that go until the delalloc starts
6576 em
->len
= min(hole_len
,
6577 range_start
- hole_start
);
6578 em
->start
= hole_start
;
6579 em
->orig_start
= hole_start
;
6581 * don't adjust block start at all,
6582 * it is fixed at EXTENT_MAP_HOLE
6584 em
->block_start
= hole_em
->block_start
;
6585 em
->block_len
= hole_len
;
6586 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6587 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6589 em
->start
= range_start
;
6591 em
->orig_start
= range_start
;
6592 em
->block_start
= EXTENT_MAP_DELALLOC
;
6593 em
->block_len
= found
;
6595 } else if (hole_em
) {
6600 free_extent_map(hole_em
);
6602 free_extent_map(em
);
6603 return ERR_PTR(err
);
6608 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6611 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6612 struct extent_map
*em
;
6613 struct btrfs_key ins
;
6617 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6618 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
6619 alloc_hint
, &ins
, 1, 1);
6621 return ERR_PTR(ret
);
6623 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6624 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6626 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
6630 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6631 ins
.offset
, ins
.offset
, 0);
6633 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
6634 free_extent_map(em
);
6635 return ERR_PTR(ret
);
6642 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6643 * block must be cow'd
6645 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6646 u64
*orig_start
, u64
*orig_block_len
,
6649 struct btrfs_trans_handle
*trans
;
6650 struct btrfs_path
*path
;
6652 struct extent_buffer
*leaf
;
6653 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6654 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6655 struct btrfs_file_extent_item
*fi
;
6656 struct btrfs_key key
;
6663 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6665 path
= btrfs_alloc_path();
6669 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
6674 slot
= path
->slots
[0];
6677 /* can't find the item, must cow */
6684 leaf
= path
->nodes
[0];
6685 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6686 if (key
.objectid
!= btrfs_ino(inode
) ||
6687 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6688 /* not our file or wrong item type, must cow */
6692 if (key
.offset
> offset
) {
6693 /* Wrong offset, must cow */
6697 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6698 found_type
= btrfs_file_extent_type(leaf
, fi
);
6699 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6700 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6701 /* not a regular extent, must cow */
6705 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6708 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6709 if (extent_end
<= offset
)
6712 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6713 if (disk_bytenr
== 0)
6716 if (btrfs_file_extent_compression(leaf
, fi
) ||
6717 btrfs_file_extent_encryption(leaf
, fi
) ||
6718 btrfs_file_extent_other_encoding(leaf
, fi
))
6721 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6724 *orig_start
= key
.offset
- backref_offset
;
6725 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6726 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6729 if (btrfs_extent_readonly(root
, disk_bytenr
))
6732 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6733 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6736 range_end
= round_up(offset
+ num_bytes
, root
->sectorsize
) - 1;
6737 ret
= test_range_bit(io_tree
, offset
, range_end
,
6738 EXTENT_DELALLOC
, 0, NULL
);
6745 btrfs_release_path(path
);
6748 * look for other files referencing this extent, if we
6749 * find any we must cow
6751 trans
= btrfs_join_transaction(root
);
6752 if (IS_ERR(trans
)) {
6757 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6758 key
.offset
- backref_offset
, disk_bytenr
);
6759 btrfs_end_transaction(trans
, root
);
6766 * adjust disk_bytenr and num_bytes to cover just the bytes
6767 * in this extent we are about to write. If there
6768 * are any csums in that range we have to cow in order
6769 * to keep the csums correct
6771 disk_bytenr
+= backref_offset
;
6772 disk_bytenr
+= offset
- key
.offset
;
6773 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6776 * all of the above have passed, it is safe to overwrite this extent
6782 btrfs_free_path(path
);
6786 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
6788 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
6790 void **pagep
= NULL
;
6791 struct page
*page
= NULL
;
6795 start_idx
= start
>> PAGE_CACHE_SHIFT
;
6798 * end is the last byte in the last page. end == start is legal
6800 end_idx
= end
>> PAGE_CACHE_SHIFT
;
6804 /* Most of the code in this while loop is lifted from
6805 * find_get_page. It's been modified to begin searching from a
6806 * page and return just the first page found in that range. If the
6807 * found idx is less than or equal to the end idx then we know that
6808 * a page exists. If no pages are found or if those pages are
6809 * outside of the range then we're fine (yay!) */
6810 while (page
== NULL
&&
6811 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
6812 page
= radix_tree_deref_slot(pagep
);
6813 if (unlikely(!page
))
6816 if (radix_tree_exception(page
)) {
6817 if (radix_tree_deref_retry(page
)) {
6822 * Otherwise, shmem/tmpfs must be storing a swap entry
6823 * here as an exceptional entry: so return it without
6824 * attempting to raise page count.
6827 break; /* TODO: Is this relevant for this use case? */
6830 if (!page_cache_get_speculative(page
)) {
6836 * Has the page moved?
6837 * This is part of the lockless pagecache protocol. See
6838 * include/linux/pagemap.h for details.
6840 if (unlikely(page
!= *pagep
)) {
6841 page_cache_release(page
);
6847 if (page
->index
<= end_idx
)
6849 page_cache_release(page
);
6856 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6857 struct extent_state
**cached_state
, int writing
)
6859 struct btrfs_ordered_extent
*ordered
;
6863 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6866 * We're concerned with the entire range that we're going to be
6867 * doing DIO to, so we need to make sure theres no ordered
6868 * extents in this range.
6870 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6871 lockend
- lockstart
+ 1);
6874 * We need to make sure there are no buffered pages in this
6875 * range either, we could have raced between the invalidate in
6876 * generic_file_direct_write and locking the extent. The
6877 * invalidate needs to happen so that reads after a write do not
6882 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
6885 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6886 cached_state
, GFP_NOFS
);
6889 btrfs_start_ordered_extent(inode
, ordered
, 1);
6890 btrfs_put_ordered_extent(ordered
);
6892 /* Screw you mmap */
6893 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6900 * If we found a page that couldn't be invalidated just
6901 * fall back to buffered.
6903 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6904 lockstart
>> PAGE_CACHE_SHIFT
,
6905 lockend
>> PAGE_CACHE_SHIFT
);
6916 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6917 u64 len
, u64 orig_start
,
6918 u64 block_start
, u64 block_len
,
6919 u64 orig_block_len
, u64 ram_bytes
,
6922 struct extent_map_tree
*em_tree
;
6923 struct extent_map
*em
;
6924 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6927 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6928 em
= alloc_extent_map();
6930 return ERR_PTR(-ENOMEM
);
6933 em
->orig_start
= orig_start
;
6934 em
->mod_start
= start
;
6937 em
->block_len
= block_len
;
6938 em
->block_start
= block_start
;
6939 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6940 em
->orig_block_len
= orig_block_len
;
6941 em
->ram_bytes
= ram_bytes
;
6942 em
->generation
= -1;
6943 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6944 if (type
== BTRFS_ORDERED_PREALLOC
)
6945 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6948 btrfs_drop_extent_cache(inode
, em
->start
,
6949 em
->start
+ em
->len
- 1, 0);
6950 write_lock(&em_tree
->lock
);
6951 ret
= add_extent_mapping(em_tree
, em
, 1);
6952 write_unlock(&em_tree
->lock
);
6953 } while (ret
== -EEXIST
);
6956 free_extent_map(em
);
6957 return ERR_PTR(ret
);
6964 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6965 struct buffer_head
*bh_result
, int create
)
6967 struct extent_map
*em
;
6968 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6969 struct extent_state
*cached_state
= NULL
;
6970 u64 start
= iblock
<< inode
->i_blkbits
;
6971 u64 lockstart
, lockend
;
6972 u64 len
= bh_result
->b_size
;
6973 int unlock_bits
= EXTENT_LOCKED
;
6977 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6979 len
= min_t(u64
, len
, root
->sectorsize
);
6982 lockend
= start
+ len
- 1;
6985 * If this errors out it's because we couldn't invalidate pagecache for
6986 * this range and we need to fallback to buffered.
6988 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6991 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6998 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6999 * io. INLINE is special, and we could probably kludge it in here, but
7000 * it's still buffered so for safety lets just fall back to the generic
7003 * For COMPRESSED we _have_ to read the entire extent in so we can
7004 * decompress it, so there will be buffering required no matter what we
7005 * do, so go ahead and fallback to buffered.
7007 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7008 * to buffered IO. Don't blame me, this is the price we pay for using
7011 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7012 em
->block_start
== EXTENT_MAP_INLINE
) {
7013 free_extent_map(em
);
7018 /* Just a good old fashioned hole, return */
7019 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7020 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7021 free_extent_map(em
);
7026 * We don't allocate a new extent in the following cases
7028 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7030 * 2) The extent is marked as PREALLOC. We're good to go here and can
7031 * just use the extent.
7035 len
= min(len
, em
->len
- (start
- em
->start
));
7036 lockstart
= start
+ len
;
7040 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7041 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7042 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7045 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7047 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7048 type
= BTRFS_ORDERED_PREALLOC
;
7050 type
= BTRFS_ORDERED_NOCOW
;
7051 len
= min(len
, em
->len
- (start
- em
->start
));
7052 block_start
= em
->block_start
+ (start
- em
->start
);
7054 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7055 &orig_block_len
, &ram_bytes
) == 1) {
7056 if (type
== BTRFS_ORDERED_PREALLOC
) {
7057 free_extent_map(em
);
7058 em
= create_pinned_em(inode
, start
, len
,
7067 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
7068 block_start
, len
, len
, type
);
7070 free_extent_map(em
);
7078 * this will cow the extent, reset the len in case we changed
7081 len
= bh_result
->b_size
;
7082 free_extent_map(em
);
7083 em
= btrfs_new_extent_direct(inode
, start
, len
);
7088 len
= min(len
, em
->len
- (start
- em
->start
));
7090 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7092 bh_result
->b_size
= len
;
7093 bh_result
->b_bdev
= em
->bdev
;
7094 set_buffer_mapped(bh_result
);
7096 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7097 set_buffer_new(bh_result
);
7100 * Need to update the i_size under the extent lock so buffered
7101 * readers will get the updated i_size when we unlock.
7103 if (start
+ len
> i_size_read(inode
))
7104 i_size_write(inode
, start
+ len
);
7106 spin_lock(&BTRFS_I(inode
)->lock
);
7107 BTRFS_I(inode
)->outstanding_extents
++;
7108 spin_unlock(&BTRFS_I(inode
)->lock
);
7110 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7111 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
7112 &cached_state
, GFP_NOFS
);
7117 * In the case of write we need to clear and unlock the entire range,
7118 * in the case of read we need to unlock only the end area that we
7119 * aren't using if there is any left over space.
7121 if (lockstart
< lockend
) {
7122 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7123 lockend
, unlock_bits
, 1, 0,
7124 &cached_state
, GFP_NOFS
);
7126 free_extent_state(cached_state
);
7129 free_extent_map(em
);
7134 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7135 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7139 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
7141 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7142 struct bio_vec
*bvec
;
7143 struct inode
*inode
= dip
->inode
;
7144 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7145 struct bio
*dio_bio
;
7146 u32
*csums
= (u32
*)dip
->csum
;
7150 start
= dip
->logical_offset
;
7151 bio_for_each_segment_all(bvec
, bio
, i
) {
7152 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
7153 struct page
*page
= bvec
->bv_page
;
7156 unsigned long flags
;
7158 local_irq_save(flags
);
7159 kaddr
= kmap_atomic(page
);
7160 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
7161 csum
, bvec
->bv_len
);
7162 btrfs_csum_final(csum
, (char *)&csum
);
7163 kunmap_atomic(kaddr
);
7164 local_irq_restore(flags
);
7166 flush_dcache_page(bvec
->bv_page
);
7167 if (csum
!= csums
[i
]) {
7168 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
7169 btrfs_ino(inode
), start
, csum
,
7175 start
+= bvec
->bv_len
;
7178 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
7179 dip
->logical_offset
+ dip
->bytes
- 1);
7180 dio_bio
= dip
->dio_bio
;
7184 /* If we had a csum failure make sure to clear the uptodate flag */
7186 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
7187 dio_end_io(dio_bio
, err
);
7191 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
7193 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7194 struct inode
*inode
= dip
->inode
;
7195 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7196 struct btrfs_ordered_extent
*ordered
= NULL
;
7197 u64 ordered_offset
= dip
->logical_offset
;
7198 u64 ordered_bytes
= dip
->bytes
;
7199 struct bio
*dio_bio
;
7205 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
7207 ordered_bytes
, !err
);
7211 btrfs_init_work(&ordered
->work
, finish_ordered_fn
, NULL
, NULL
);
7212 btrfs_queue_work(root
->fs_info
->endio_write_workers
,
7216 * our bio might span multiple ordered extents. If we haven't
7217 * completed the accounting for the whole dio, go back and try again
7219 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7220 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7226 dio_bio
= dip
->dio_bio
;
7230 /* If we had an error make sure to clear the uptodate flag */
7232 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
7233 dio_end_io(dio_bio
, err
);
7237 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7238 struct bio
*bio
, int mirror_num
,
7239 unsigned long bio_flags
, u64 offset
)
7242 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7243 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7244 BUG_ON(ret
); /* -ENOMEM */
7248 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7250 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7253 btrfs_err(BTRFS_I(dip
->inode
)->root
->fs_info
,
7254 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7255 btrfs_ino(dip
->inode
), bio
->bi_rw
,
7256 (unsigned long long)bio
->bi_iter
.bi_sector
,
7257 bio
->bi_iter
.bi_size
, err
);
7261 * before atomic variable goto zero, we must make sure
7262 * dip->errors is perceived to be set.
7264 smp_mb__before_atomic();
7267 /* if there are more bios still pending for this dio, just exit */
7268 if (!atomic_dec_and_test(&dip
->pending_bios
))
7272 bio_io_error(dip
->orig_bio
);
7274 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
7275 bio_endio(dip
->orig_bio
, 0);
7281 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7282 u64 first_sector
, gfp_t gfp_flags
)
7284 int nr_vecs
= bio_get_nr_vecs(bdev
);
7285 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7288 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7289 int rw
, u64 file_offset
, int skip_sum
,
7292 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7293 int write
= rw
& REQ_WRITE
;
7294 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7298 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7303 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7311 if (write
&& async_submit
) {
7312 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7313 inode
, rw
, bio
, 0, 0,
7315 __btrfs_submit_bio_start_direct_io
,
7316 __btrfs_submit_bio_done
);
7320 * If we aren't doing async submit, calculate the csum of the
7323 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7326 } else if (!skip_sum
) {
7327 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
, bio
,
7334 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7340 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7343 struct inode
*inode
= dip
->inode
;
7344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7346 struct bio
*orig_bio
= dip
->orig_bio
;
7347 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7348 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
7349 u64 file_offset
= dip
->logical_offset
;
7354 int async_submit
= 0;
7356 map_length
= orig_bio
->bi_iter
.bi_size
;
7357 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7358 &map_length
, NULL
, 0);
7362 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
7367 /* async crcs make it difficult to collect full stripe writes. */
7368 if (btrfs_get_alloc_profile(root
, 1) &
7369 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7374 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7378 bio
->bi_private
= dip
;
7379 bio
->bi_end_io
= btrfs_end_dio_bio
;
7380 atomic_inc(&dip
->pending_bios
);
7382 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7383 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7384 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7385 bvec
->bv_offset
) < bvec
->bv_len
)) {
7387 * inc the count before we submit the bio so
7388 * we know the end IO handler won't happen before
7389 * we inc the count. Otherwise, the dip might get freed
7390 * before we're done setting it up
7392 atomic_inc(&dip
->pending_bios
);
7393 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7394 file_offset
, skip_sum
,
7398 atomic_dec(&dip
->pending_bios
);
7402 start_sector
+= submit_len
>> 9;
7403 file_offset
+= submit_len
;
7408 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7409 start_sector
, GFP_NOFS
);
7412 bio
->bi_private
= dip
;
7413 bio
->bi_end_io
= btrfs_end_dio_bio
;
7415 map_length
= orig_bio
->bi_iter
.bi_size
;
7416 ret
= btrfs_map_block(root
->fs_info
, rw
,
7418 &map_length
, NULL
, 0);
7424 submit_len
+= bvec
->bv_len
;
7431 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7440 * before atomic variable goto zero, we must
7441 * make sure dip->errors is perceived to be set.
7443 smp_mb__before_atomic();
7444 if (atomic_dec_and_test(&dip
->pending_bios
))
7445 bio_io_error(dip
->orig_bio
);
7447 /* bio_end_io() will handle error, so we needn't return it */
7451 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7452 struct inode
*inode
, loff_t file_offset
)
7454 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7455 struct btrfs_dio_private
*dip
;
7459 int write
= rw
& REQ_WRITE
;
7463 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7465 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7471 if (!skip_sum
&& !write
) {
7472 csum_size
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
7473 sum_len
= dio_bio
->bi_iter
.bi_size
>>
7474 inode
->i_sb
->s_blocksize_bits
;
7475 sum_len
*= csum_size
;
7480 dip
= kmalloc(sizeof(*dip
) + sum_len
, GFP_NOFS
);
7486 dip
->private = dio_bio
->bi_private
;
7488 dip
->logical_offset
= file_offset
;
7489 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
7490 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
7491 io_bio
->bi_private
= dip
;
7493 dip
->orig_bio
= io_bio
;
7494 dip
->dio_bio
= dio_bio
;
7495 atomic_set(&dip
->pending_bios
, 0);
7498 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7500 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7502 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7511 * If this is a write, we need to clean up the reserved space and kill
7512 * the ordered extent.
7515 struct btrfs_ordered_extent
*ordered
;
7516 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7517 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7518 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7519 btrfs_free_reserved_extent(root
, ordered
->start
,
7520 ordered
->disk_len
, 1);
7521 btrfs_put_ordered_extent(ordered
);
7522 btrfs_put_ordered_extent(ordered
);
7524 bio_endio(dio_bio
, ret
);
7527 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7528 const struct iov_iter
*iter
, loff_t offset
)
7532 unsigned blocksize_mask
= root
->sectorsize
- 1;
7533 ssize_t retval
= -EINVAL
;
7535 if (offset
& blocksize_mask
)
7538 if (iov_iter_alignment(iter
) & blocksize_mask
)
7541 /* If this is a write we don't need to check anymore */
7545 * Check to make sure we don't have duplicate iov_base's in this
7546 * iovec, if so return EINVAL, otherwise we'll get csum errors
7547 * when reading back.
7549 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
7550 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
7551 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
7560 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7561 struct iov_iter
*iter
, loff_t offset
)
7563 struct file
*file
= iocb
->ki_filp
;
7564 struct inode
*inode
= file
->f_mapping
->host
;
7568 bool relock
= false;
7571 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iter
, offset
))
7574 atomic_inc(&inode
->i_dio_count
);
7575 smp_mb__after_atomic();
7578 * The generic stuff only does filemap_write_and_wait_range, which
7579 * isn't enough if we've written compressed pages to this area, so
7580 * we need to flush the dirty pages again to make absolutely sure
7581 * that any outstanding dirty pages are on disk.
7583 count
= iov_iter_count(iter
);
7584 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
7585 &BTRFS_I(inode
)->runtime_flags
))
7586 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
7587 offset
+ count
- 1);
7591 * If the write DIO is beyond the EOF, we need update
7592 * the isize, but it is protected by i_mutex. So we can
7593 * not unlock the i_mutex at this case.
7595 if (offset
+ count
<= inode
->i_size
) {
7596 mutex_unlock(&inode
->i_mutex
);
7599 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7602 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7603 &BTRFS_I(inode
)->runtime_flags
))) {
7604 inode_dio_done(inode
);
7605 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7609 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7610 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7611 iter
, offset
, btrfs_get_blocks_direct
, NULL
,
7612 btrfs_submit_direct
, flags
);
7614 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7615 btrfs_delalloc_release_space(inode
, count
);
7616 else if (ret
>= 0 && (size_t)ret
< count
)
7617 btrfs_delalloc_release_space(inode
,
7618 count
- (size_t)ret
);
7620 btrfs_delalloc_release_metadata(inode
, 0);
7624 inode_dio_done(inode
);
7626 mutex_lock(&inode
->i_mutex
);
7631 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7633 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7634 __u64 start
, __u64 len
)
7638 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7642 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7645 int btrfs_readpage(struct file
*file
, struct page
*page
)
7647 struct extent_io_tree
*tree
;
7648 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7649 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7652 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7654 struct extent_io_tree
*tree
;
7657 if (current
->flags
& PF_MEMALLOC
) {
7658 redirty_page_for_writepage(wbc
, page
);
7662 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7663 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7666 static int btrfs_writepages(struct address_space
*mapping
,
7667 struct writeback_control
*wbc
)
7669 struct extent_io_tree
*tree
;
7671 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7672 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7676 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7677 struct list_head
*pages
, unsigned nr_pages
)
7679 struct extent_io_tree
*tree
;
7680 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7681 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7684 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7686 struct extent_io_tree
*tree
;
7687 struct extent_map_tree
*map
;
7690 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7691 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7692 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7694 ClearPagePrivate(page
);
7695 set_page_private(page
, 0);
7696 page_cache_release(page
);
7701 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7703 if (PageWriteback(page
) || PageDirty(page
))
7705 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7708 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
7709 unsigned int length
)
7711 struct inode
*inode
= page
->mapping
->host
;
7712 struct extent_io_tree
*tree
;
7713 struct btrfs_ordered_extent
*ordered
;
7714 struct extent_state
*cached_state
= NULL
;
7715 u64 page_start
= page_offset(page
);
7716 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7717 int inode_evicting
= inode
->i_state
& I_FREEING
;
7720 * we have the page locked, so new writeback can't start,
7721 * and the dirty bit won't be cleared while we are here.
7723 * Wait for IO on this page so that we can safely clear
7724 * the PagePrivate2 bit and do ordered accounting
7726 wait_on_page_writeback(page
);
7728 tree
= &BTRFS_I(inode
)->io_tree
;
7730 btrfs_releasepage(page
, GFP_NOFS
);
7734 if (!inode_evicting
)
7735 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7736 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7739 * IO on this page will never be started, so we need
7740 * to account for any ordered extents now
7742 if (!inode_evicting
)
7743 clear_extent_bit(tree
, page_start
, page_end
,
7744 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7745 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7746 EXTENT_DEFRAG
, 1, 0, &cached_state
,
7749 * whoever cleared the private bit is responsible
7750 * for the finish_ordered_io
7752 if (TestClearPagePrivate2(page
)) {
7753 struct btrfs_ordered_inode_tree
*tree
;
7756 tree
= &BTRFS_I(inode
)->ordered_tree
;
7758 spin_lock_irq(&tree
->lock
);
7759 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
7760 new_len
= page_start
- ordered
->file_offset
;
7761 if (new_len
< ordered
->truncated_len
)
7762 ordered
->truncated_len
= new_len
;
7763 spin_unlock_irq(&tree
->lock
);
7765 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
7767 PAGE_CACHE_SIZE
, 1))
7768 btrfs_finish_ordered_io(ordered
);
7770 btrfs_put_ordered_extent(ordered
);
7771 if (!inode_evicting
) {
7772 cached_state
= NULL
;
7773 lock_extent_bits(tree
, page_start
, page_end
, 0,
7778 if (!inode_evicting
) {
7779 clear_extent_bit(tree
, page_start
, page_end
,
7780 EXTENT_LOCKED
| EXTENT_DIRTY
|
7781 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
7782 EXTENT_DEFRAG
, 1, 1,
7783 &cached_state
, GFP_NOFS
);
7785 __btrfs_releasepage(page
, GFP_NOFS
);
7788 ClearPageChecked(page
);
7789 if (PagePrivate(page
)) {
7790 ClearPagePrivate(page
);
7791 set_page_private(page
, 0);
7792 page_cache_release(page
);
7797 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7798 * called from a page fault handler when a page is first dirtied. Hence we must
7799 * be careful to check for EOF conditions here. We set the page up correctly
7800 * for a written page which means we get ENOSPC checking when writing into
7801 * holes and correct delalloc and unwritten extent mapping on filesystems that
7802 * support these features.
7804 * We are not allowed to take the i_mutex here so we have to play games to
7805 * protect against truncate races as the page could now be beyond EOF. Because
7806 * vmtruncate() writes the inode size before removing pages, once we have the
7807 * page lock we can determine safely if the page is beyond EOF. If it is not
7808 * beyond EOF, then the page is guaranteed safe against truncation until we
7811 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7813 struct page
*page
= vmf
->page
;
7814 struct inode
*inode
= file_inode(vma
->vm_file
);
7815 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7816 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7817 struct btrfs_ordered_extent
*ordered
;
7818 struct extent_state
*cached_state
= NULL
;
7820 unsigned long zero_start
;
7827 sb_start_pagefault(inode
->i_sb
);
7828 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7830 ret
= file_update_time(vma
->vm_file
);
7836 else /* -ENOSPC, -EIO, etc */
7837 ret
= VM_FAULT_SIGBUS
;
7843 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7846 size
= i_size_read(inode
);
7847 page_start
= page_offset(page
);
7848 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7850 if ((page
->mapping
!= inode
->i_mapping
) ||
7851 (page_start
>= size
)) {
7852 /* page got truncated out from underneath us */
7855 wait_on_page_writeback(page
);
7857 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7858 set_page_extent_mapped(page
);
7861 * we can't set the delalloc bits if there are pending ordered
7862 * extents. Drop our locks and wait for them to finish
7864 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7866 unlock_extent_cached(io_tree
, page_start
, page_end
,
7867 &cached_state
, GFP_NOFS
);
7869 btrfs_start_ordered_extent(inode
, ordered
, 1);
7870 btrfs_put_ordered_extent(ordered
);
7875 * XXX - page_mkwrite gets called every time the page is dirtied, even
7876 * if it was already dirty, so for space accounting reasons we need to
7877 * clear any delalloc bits for the range we are fixing to save. There
7878 * is probably a better way to do this, but for now keep consistent with
7879 * prepare_pages in the normal write path.
7881 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7882 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7883 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7884 0, 0, &cached_state
, GFP_NOFS
);
7886 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7889 unlock_extent_cached(io_tree
, page_start
, page_end
,
7890 &cached_state
, GFP_NOFS
);
7891 ret
= VM_FAULT_SIGBUS
;
7896 /* page is wholly or partially inside EOF */
7897 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7898 zero_start
= size
& ~PAGE_CACHE_MASK
;
7900 zero_start
= PAGE_CACHE_SIZE
;
7902 if (zero_start
!= PAGE_CACHE_SIZE
) {
7904 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7905 flush_dcache_page(page
);
7908 ClearPageChecked(page
);
7909 set_page_dirty(page
);
7910 SetPageUptodate(page
);
7912 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7913 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7914 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7916 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7920 sb_end_pagefault(inode
->i_sb
);
7921 return VM_FAULT_LOCKED
;
7925 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7927 sb_end_pagefault(inode
->i_sb
);
7931 static int btrfs_truncate(struct inode
*inode
)
7933 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7934 struct btrfs_block_rsv
*rsv
;
7937 struct btrfs_trans_handle
*trans
;
7938 u64 mask
= root
->sectorsize
- 1;
7939 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7941 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
7947 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7948 * 3 things going on here
7950 * 1) We need to reserve space for our orphan item and the space to
7951 * delete our orphan item. Lord knows we don't want to have a dangling
7952 * orphan item because we didn't reserve space to remove it.
7954 * 2) We need to reserve space to update our inode.
7956 * 3) We need to have something to cache all the space that is going to
7957 * be free'd up by the truncate operation, but also have some slack
7958 * space reserved in case it uses space during the truncate (thank you
7959 * very much snapshotting).
7961 * And we need these to all be seperate. The fact is we can use alot of
7962 * space doing the truncate, and we have no earthly idea how much space
7963 * we will use, so we need the truncate reservation to be seperate so it
7964 * doesn't end up using space reserved for updating the inode or
7965 * removing the orphan item. We also need to be able to stop the
7966 * transaction and start a new one, which means we need to be able to
7967 * update the inode several times, and we have no idea of knowing how
7968 * many times that will be, so we can't just reserve 1 item for the
7969 * entirety of the opration, so that has to be done seperately as well.
7970 * Then there is the orphan item, which does indeed need to be held on
7971 * to for the whole operation, and we need nobody to touch this reserved
7972 * space except the orphan code.
7974 * So that leaves us with
7976 * 1) root->orphan_block_rsv - for the orphan deletion.
7977 * 2) rsv - for the truncate reservation, which we will steal from the
7978 * transaction reservation.
7979 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7980 * updating the inode.
7982 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7985 rsv
->size
= min_size
;
7989 * 1 for the truncate slack space
7990 * 1 for updating the inode.
7992 trans
= btrfs_start_transaction(root
, 2);
7993 if (IS_ERR(trans
)) {
7994 err
= PTR_ERR(trans
);
7998 /* Migrate the slack space for the truncate to our reserve */
7999 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
8004 * So if we truncate and then write and fsync we normally would just
8005 * write the extents that changed, which is a problem if we need to
8006 * first truncate that entire inode. So set this flag so we write out
8007 * all of the extents in the inode to the sync log so we're completely
8010 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
8011 trans
->block_rsv
= rsv
;
8014 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
8016 BTRFS_EXTENT_DATA_KEY
);
8017 if (ret
!= -ENOSPC
) {
8022 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8023 ret
= btrfs_update_inode(trans
, root
, inode
);
8029 btrfs_end_transaction(trans
, root
);
8030 btrfs_btree_balance_dirty(root
);
8032 trans
= btrfs_start_transaction(root
, 2);
8033 if (IS_ERR(trans
)) {
8034 ret
= err
= PTR_ERR(trans
);
8039 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
8041 BUG_ON(ret
); /* shouldn't happen */
8042 trans
->block_rsv
= rsv
;
8045 if (ret
== 0 && inode
->i_nlink
> 0) {
8046 trans
->block_rsv
= root
->orphan_block_rsv
;
8047 ret
= btrfs_orphan_del(trans
, inode
);
8053 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8054 ret
= btrfs_update_inode(trans
, root
, inode
);
8058 ret
= btrfs_end_transaction(trans
, root
);
8059 btrfs_btree_balance_dirty(root
);
8063 btrfs_free_block_rsv(root
, rsv
);
8072 * create a new subvolume directory/inode (helper for the ioctl).
8074 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
8075 struct btrfs_root
*new_root
,
8076 struct btrfs_root
*parent_root
,
8079 struct inode
*inode
;
8083 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
8084 new_dirid
, new_dirid
,
8085 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
8088 return PTR_ERR(inode
);
8089 inode
->i_op
= &btrfs_dir_inode_operations
;
8090 inode
->i_fop
= &btrfs_dir_file_operations
;
8092 set_nlink(inode
, 1);
8093 btrfs_i_size_write(inode
, 0);
8095 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
8097 btrfs_err(new_root
->fs_info
,
8098 "error inheriting subvolume %llu properties: %d",
8099 new_root
->root_key
.objectid
, err
);
8101 err
= btrfs_update_inode(trans
, new_root
, inode
);
8107 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
8109 struct btrfs_inode
*ei
;
8110 struct inode
*inode
;
8112 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
8119 ei
->last_sub_trans
= 0;
8120 ei
->logged_trans
= 0;
8121 ei
->delalloc_bytes
= 0;
8122 ei
->disk_i_size
= 0;
8125 ei
->index_cnt
= (u64
)-1;
8127 ei
->last_unlink_trans
= 0;
8128 ei
->last_log_commit
= 0;
8130 spin_lock_init(&ei
->lock
);
8131 ei
->outstanding_extents
= 0;
8132 ei
->reserved_extents
= 0;
8134 ei
->runtime_flags
= 0;
8135 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
8137 ei
->delayed_node
= NULL
;
8139 inode
= &ei
->vfs_inode
;
8140 extent_map_tree_init(&ei
->extent_tree
);
8141 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
8142 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
8143 ei
->io_tree
.track_uptodate
= 1;
8144 ei
->io_failure_tree
.track_uptodate
= 1;
8145 atomic_set(&ei
->sync_writers
, 0);
8146 mutex_init(&ei
->log_mutex
);
8147 mutex_init(&ei
->delalloc_mutex
);
8148 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
8149 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
8150 RB_CLEAR_NODE(&ei
->rb_node
);
8155 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8156 void btrfs_test_destroy_inode(struct inode
*inode
)
8158 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
8159 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8163 static void btrfs_i_callback(struct rcu_head
*head
)
8165 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
8166 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8169 void btrfs_destroy_inode(struct inode
*inode
)
8171 struct btrfs_ordered_extent
*ordered
;
8172 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8174 WARN_ON(!hlist_empty(&inode
->i_dentry
));
8175 WARN_ON(inode
->i_data
.nrpages
);
8176 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
8177 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
8178 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
8179 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
8182 * This can happen where we create an inode, but somebody else also
8183 * created the same inode and we need to destroy the one we already
8189 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
8190 &BTRFS_I(inode
)->runtime_flags
)) {
8191 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
8193 atomic_dec(&root
->orphan_inodes
);
8197 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
8201 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
8202 ordered
->file_offset
, ordered
->len
);
8203 btrfs_remove_ordered_extent(inode
, ordered
);
8204 btrfs_put_ordered_extent(ordered
);
8205 btrfs_put_ordered_extent(ordered
);
8208 inode_tree_del(inode
);
8209 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
8211 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
8214 int btrfs_drop_inode(struct inode
*inode
)
8216 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8221 /* the snap/subvol tree is on deleting */
8222 if (btrfs_root_refs(&root
->root_item
) == 0)
8225 return generic_drop_inode(inode
);
8228 static void init_once(void *foo
)
8230 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
8232 inode_init_once(&ei
->vfs_inode
);
8235 void btrfs_destroy_cachep(void)
8238 * Make sure all delayed rcu free inodes are flushed before we
8242 if (btrfs_inode_cachep
)
8243 kmem_cache_destroy(btrfs_inode_cachep
);
8244 if (btrfs_trans_handle_cachep
)
8245 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8246 if (btrfs_transaction_cachep
)
8247 kmem_cache_destroy(btrfs_transaction_cachep
);
8248 if (btrfs_path_cachep
)
8249 kmem_cache_destroy(btrfs_path_cachep
);
8250 if (btrfs_free_space_cachep
)
8251 kmem_cache_destroy(btrfs_free_space_cachep
);
8252 if (btrfs_delalloc_work_cachep
)
8253 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8256 int btrfs_init_cachep(void)
8258 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8259 sizeof(struct btrfs_inode
), 0,
8260 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8261 if (!btrfs_inode_cachep
)
8264 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8265 sizeof(struct btrfs_trans_handle
), 0,
8266 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8267 if (!btrfs_trans_handle_cachep
)
8270 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8271 sizeof(struct btrfs_transaction
), 0,
8272 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8273 if (!btrfs_transaction_cachep
)
8276 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8277 sizeof(struct btrfs_path
), 0,
8278 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8279 if (!btrfs_path_cachep
)
8282 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8283 sizeof(struct btrfs_free_space
), 0,
8284 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8285 if (!btrfs_free_space_cachep
)
8288 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8289 sizeof(struct btrfs_delalloc_work
), 0,
8290 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8292 if (!btrfs_delalloc_work_cachep
)
8297 btrfs_destroy_cachep();
8301 static int btrfs_getattr(struct vfsmount
*mnt
,
8302 struct dentry
*dentry
, struct kstat
*stat
)
8305 struct inode
*inode
= dentry
->d_inode
;
8306 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8308 generic_fillattr(inode
, stat
);
8309 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8310 stat
->blksize
= PAGE_CACHE_SIZE
;
8312 spin_lock(&BTRFS_I(inode
)->lock
);
8313 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8314 spin_unlock(&BTRFS_I(inode
)->lock
);
8315 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8316 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8320 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8321 struct inode
*new_dir
, struct dentry
*new_dentry
)
8323 struct btrfs_trans_handle
*trans
;
8324 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8325 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8326 struct inode
*new_inode
= new_dentry
->d_inode
;
8327 struct inode
*old_inode
= old_dentry
->d_inode
;
8328 struct timespec ctime
= CURRENT_TIME
;
8332 u64 old_ino
= btrfs_ino(old_inode
);
8334 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8337 /* we only allow rename subvolume link between subvolumes */
8338 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8341 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8342 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8345 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8346 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8350 /* check for collisions, even if the name isn't there */
8351 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
8352 new_dentry
->d_name
.name
,
8353 new_dentry
->d_name
.len
);
8356 if (ret
== -EEXIST
) {
8358 * eexist without a new_inode */
8359 if (WARN_ON(!new_inode
)) {
8363 /* maybe -EOVERFLOW */
8370 * we're using rename to replace one file with another. Start IO on it
8371 * now so we don't add too much work to the end of the transaction
8373 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
8374 filemap_flush(old_inode
->i_mapping
);
8376 /* close the racy window with snapshot create/destroy ioctl */
8377 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8378 down_read(&root
->fs_info
->subvol_sem
);
8380 * We want to reserve the absolute worst case amount of items. So if
8381 * both inodes are subvols and we need to unlink them then that would
8382 * require 4 item modifications, but if they are both normal inodes it
8383 * would require 5 item modifications, so we'll assume their normal
8384 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8385 * should cover the worst case number of items we'll modify.
8387 trans
= btrfs_start_transaction(root
, 11);
8388 if (IS_ERR(trans
)) {
8389 ret
= PTR_ERR(trans
);
8394 btrfs_record_root_in_trans(trans
, dest
);
8396 ret
= btrfs_set_inode_index(new_dir
, &index
);
8400 BTRFS_I(old_inode
)->dir_index
= 0ULL;
8401 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8402 /* force full log commit if subvolume involved. */
8403 btrfs_set_log_full_commit(root
->fs_info
, trans
);
8405 ret
= btrfs_insert_inode_ref(trans
, dest
,
8406 new_dentry
->d_name
.name
,
8407 new_dentry
->d_name
.len
,
8409 btrfs_ino(new_dir
), index
);
8413 * this is an ugly little race, but the rename is required
8414 * to make sure that if we crash, the inode is either at the
8415 * old name or the new one. pinning the log transaction lets
8416 * us make sure we don't allow a log commit to come in after
8417 * we unlink the name but before we add the new name back in.
8419 btrfs_pin_log_trans(root
);
8422 inode_inc_iversion(old_dir
);
8423 inode_inc_iversion(new_dir
);
8424 inode_inc_iversion(old_inode
);
8425 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8426 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8427 old_inode
->i_ctime
= ctime
;
8429 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8430 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8432 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8433 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8434 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8435 old_dentry
->d_name
.name
,
8436 old_dentry
->d_name
.len
);
8438 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8439 old_dentry
->d_inode
,
8440 old_dentry
->d_name
.name
,
8441 old_dentry
->d_name
.len
);
8443 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8446 btrfs_abort_transaction(trans
, root
, ret
);
8451 inode_inc_iversion(new_inode
);
8452 new_inode
->i_ctime
= CURRENT_TIME
;
8453 if (unlikely(btrfs_ino(new_inode
) ==
8454 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8455 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8456 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8458 new_dentry
->d_name
.name
,
8459 new_dentry
->d_name
.len
);
8460 BUG_ON(new_inode
->i_nlink
== 0);
8462 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8463 new_dentry
->d_inode
,
8464 new_dentry
->d_name
.name
,
8465 new_dentry
->d_name
.len
);
8467 if (!ret
&& new_inode
->i_nlink
== 0)
8468 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8470 btrfs_abort_transaction(trans
, root
, ret
);
8475 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8476 new_dentry
->d_name
.name
,
8477 new_dentry
->d_name
.len
, 0, index
);
8479 btrfs_abort_transaction(trans
, root
, ret
);
8483 if (old_inode
->i_nlink
== 1)
8484 BTRFS_I(old_inode
)->dir_index
= index
;
8486 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8487 struct dentry
*parent
= new_dentry
->d_parent
;
8488 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8489 btrfs_end_log_trans(root
);
8492 btrfs_end_transaction(trans
, root
);
8494 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8495 up_read(&root
->fs_info
->subvol_sem
);
8500 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8502 struct btrfs_delalloc_work
*delalloc_work
;
8503 struct inode
*inode
;
8505 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8507 inode
= delalloc_work
->inode
;
8508 if (delalloc_work
->wait
) {
8509 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
8511 filemap_flush(inode
->i_mapping
);
8512 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8513 &BTRFS_I(inode
)->runtime_flags
))
8514 filemap_flush(inode
->i_mapping
);
8517 if (delalloc_work
->delay_iput
)
8518 btrfs_add_delayed_iput(inode
);
8521 complete(&delalloc_work
->completion
);
8524 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8525 int wait
, int delay_iput
)
8527 struct btrfs_delalloc_work
*work
;
8529 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8533 init_completion(&work
->completion
);
8534 INIT_LIST_HEAD(&work
->list
);
8535 work
->inode
= inode
;
8537 work
->delay_iput
= delay_iput
;
8538 btrfs_init_work(&work
->work
, btrfs_run_delalloc_work
, NULL
, NULL
);
8543 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8545 wait_for_completion(&work
->completion
);
8546 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8550 * some fairly slow code that needs optimization. This walks the list
8551 * of all the inodes with pending delalloc and forces them to disk.
8553 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
8556 struct btrfs_inode
*binode
;
8557 struct inode
*inode
;
8558 struct btrfs_delalloc_work
*work
, *next
;
8559 struct list_head works
;
8560 struct list_head splice
;
8563 INIT_LIST_HEAD(&works
);
8564 INIT_LIST_HEAD(&splice
);
8566 mutex_lock(&root
->delalloc_mutex
);
8567 spin_lock(&root
->delalloc_lock
);
8568 list_splice_init(&root
->delalloc_inodes
, &splice
);
8569 while (!list_empty(&splice
)) {
8570 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8573 list_move_tail(&binode
->delalloc_inodes
,
8574 &root
->delalloc_inodes
);
8575 inode
= igrab(&binode
->vfs_inode
);
8577 cond_resched_lock(&root
->delalloc_lock
);
8580 spin_unlock(&root
->delalloc_lock
);
8582 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8583 if (unlikely(!work
)) {
8585 btrfs_add_delayed_iput(inode
);
8591 list_add_tail(&work
->list
, &works
);
8592 btrfs_queue_work(root
->fs_info
->flush_workers
,
8595 if (nr
!= -1 && ret
>= nr
)
8598 spin_lock(&root
->delalloc_lock
);
8600 spin_unlock(&root
->delalloc_lock
);
8603 list_for_each_entry_safe(work
, next
, &works
, list
) {
8604 list_del_init(&work
->list
);
8605 btrfs_wait_and_free_delalloc_work(work
);
8608 if (!list_empty_careful(&splice
)) {
8609 spin_lock(&root
->delalloc_lock
);
8610 list_splice_tail(&splice
, &root
->delalloc_inodes
);
8611 spin_unlock(&root
->delalloc_lock
);
8613 mutex_unlock(&root
->delalloc_mutex
);
8617 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8621 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
8624 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
8628 * the filemap_flush will queue IO into the worker threads, but
8629 * we have to make sure the IO is actually started and that
8630 * ordered extents get created before we return
8632 atomic_inc(&root
->fs_info
->async_submit_draining
);
8633 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8634 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8635 wait_event(root
->fs_info
->async_submit_wait
,
8636 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8637 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8639 atomic_dec(&root
->fs_info
->async_submit_draining
);
8643 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
8646 struct btrfs_root
*root
;
8647 struct list_head splice
;
8650 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
8653 INIT_LIST_HEAD(&splice
);
8655 mutex_lock(&fs_info
->delalloc_root_mutex
);
8656 spin_lock(&fs_info
->delalloc_root_lock
);
8657 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8658 while (!list_empty(&splice
) && nr
) {
8659 root
= list_first_entry(&splice
, struct btrfs_root
,
8661 root
= btrfs_grab_fs_root(root
);
8663 list_move_tail(&root
->delalloc_root
,
8664 &fs_info
->delalloc_roots
);
8665 spin_unlock(&fs_info
->delalloc_root_lock
);
8667 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
8668 btrfs_put_fs_root(root
);
8676 spin_lock(&fs_info
->delalloc_root_lock
);
8678 spin_unlock(&fs_info
->delalloc_root_lock
);
8681 atomic_inc(&fs_info
->async_submit_draining
);
8682 while (atomic_read(&fs_info
->nr_async_submits
) ||
8683 atomic_read(&fs_info
->async_delalloc_pages
)) {
8684 wait_event(fs_info
->async_submit_wait
,
8685 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8686 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8688 atomic_dec(&fs_info
->async_submit_draining
);
8690 if (!list_empty_careful(&splice
)) {
8691 spin_lock(&fs_info
->delalloc_root_lock
);
8692 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8693 spin_unlock(&fs_info
->delalloc_root_lock
);
8695 mutex_unlock(&fs_info
->delalloc_root_mutex
);
8699 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8700 const char *symname
)
8702 struct btrfs_trans_handle
*trans
;
8703 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8704 struct btrfs_path
*path
;
8705 struct btrfs_key key
;
8706 struct inode
*inode
= NULL
;
8714 struct btrfs_file_extent_item
*ei
;
8715 struct extent_buffer
*leaf
;
8717 name_len
= strlen(symname
);
8718 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8719 return -ENAMETOOLONG
;
8722 * 2 items for inode item and ref
8723 * 2 items for dir items
8724 * 1 item for xattr if selinux is on
8726 trans
= btrfs_start_transaction(root
, 5);
8728 return PTR_ERR(trans
);
8730 err
= btrfs_find_free_ino(root
, &objectid
);
8734 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8735 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8736 S_IFLNK
|S_IRWXUGO
, &index
);
8737 if (IS_ERR(inode
)) {
8738 err
= PTR_ERR(inode
);
8742 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8749 * If the active LSM wants to access the inode during
8750 * d_instantiate it needs these. Smack checks to see
8751 * if the filesystem supports xattrs by looking at the
8754 inode
->i_fop
= &btrfs_file_operations
;
8755 inode
->i_op
= &btrfs_file_inode_operations
;
8757 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8761 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8762 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8763 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8768 path
= btrfs_alloc_path();
8774 key
.objectid
= btrfs_ino(inode
);
8776 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8777 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8778 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8782 btrfs_free_path(path
);
8785 leaf
= path
->nodes
[0];
8786 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8787 struct btrfs_file_extent_item
);
8788 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8789 btrfs_set_file_extent_type(leaf
, ei
,
8790 BTRFS_FILE_EXTENT_INLINE
);
8791 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8792 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8793 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8794 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8796 ptr
= btrfs_file_extent_inline_start(ei
);
8797 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8798 btrfs_mark_buffer_dirty(leaf
);
8799 btrfs_free_path(path
);
8801 inode
->i_op
= &btrfs_symlink_inode_operations
;
8802 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8803 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8804 inode_set_bytes(inode
, name_len
);
8805 btrfs_i_size_write(inode
, name_len
);
8806 err
= btrfs_update_inode(trans
, root
, inode
);
8812 d_instantiate(dentry
, inode
);
8813 btrfs_end_transaction(trans
, root
);
8815 inode_dec_link_count(inode
);
8818 btrfs_btree_balance_dirty(root
);
8822 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8823 u64 start
, u64 num_bytes
, u64 min_size
,
8824 loff_t actual_len
, u64
*alloc_hint
,
8825 struct btrfs_trans_handle
*trans
)
8827 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8828 struct extent_map
*em
;
8829 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8830 struct btrfs_key ins
;
8831 u64 cur_offset
= start
;
8835 bool own_trans
= true;
8839 while (num_bytes
> 0) {
8841 trans
= btrfs_start_transaction(root
, 3);
8842 if (IS_ERR(trans
)) {
8843 ret
= PTR_ERR(trans
);
8848 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8849 cur_bytes
= max(cur_bytes
, min_size
);
8850 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
8851 *alloc_hint
, &ins
, 1, 0);
8854 btrfs_end_transaction(trans
, root
);
8858 ret
= insert_reserved_file_extent(trans
, inode
,
8859 cur_offset
, ins
.objectid
,
8860 ins
.offset
, ins
.offset
,
8861 ins
.offset
, 0, 0, 0,
8862 BTRFS_FILE_EXTENT_PREALLOC
);
8864 btrfs_free_reserved_extent(root
, ins
.objectid
,
8866 btrfs_abort_transaction(trans
, root
, ret
);
8868 btrfs_end_transaction(trans
, root
);
8871 btrfs_drop_extent_cache(inode
, cur_offset
,
8872 cur_offset
+ ins
.offset
-1, 0);
8874 em
= alloc_extent_map();
8876 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8877 &BTRFS_I(inode
)->runtime_flags
);
8881 em
->start
= cur_offset
;
8882 em
->orig_start
= cur_offset
;
8883 em
->len
= ins
.offset
;
8884 em
->block_start
= ins
.objectid
;
8885 em
->block_len
= ins
.offset
;
8886 em
->orig_block_len
= ins
.offset
;
8887 em
->ram_bytes
= ins
.offset
;
8888 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8889 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8890 em
->generation
= trans
->transid
;
8893 write_lock(&em_tree
->lock
);
8894 ret
= add_extent_mapping(em_tree
, em
, 1);
8895 write_unlock(&em_tree
->lock
);
8898 btrfs_drop_extent_cache(inode
, cur_offset
,
8899 cur_offset
+ ins
.offset
- 1,
8902 free_extent_map(em
);
8904 num_bytes
-= ins
.offset
;
8905 cur_offset
+= ins
.offset
;
8906 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8908 inode_inc_iversion(inode
);
8909 inode
->i_ctime
= CURRENT_TIME
;
8910 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8911 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8912 (actual_len
> inode
->i_size
) &&
8913 (cur_offset
> inode
->i_size
)) {
8914 if (cur_offset
> actual_len
)
8915 i_size
= actual_len
;
8917 i_size
= cur_offset
;
8918 i_size_write(inode
, i_size
);
8919 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8922 ret
= btrfs_update_inode(trans
, root
, inode
);
8925 btrfs_abort_transaction(trans
, root
, ret
);
8927 btrfs_end_transaction(trans
, root
);
8932 btrfs_end_transaction(trans
, root
);
8937 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8938 u64 start
, u64 num_bytes
, u64 min_size
,
8939 loff_t actual_len
, u64
*alloc_hint
)
8941 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8942 min_size
, actual_len
, alloc_hint
,
8946 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8947 struct btrfs_trans_handle
*trans
, int mode
,
8948 u64 start
, u64 num_bytes
, u64 min_size
,
8949 loff_t actual_len
, u64
*alloc_hint
)
8951 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8952 min_size
, actual_len
, alloc_hint
, trans
);
8955 static int btrfs_set_page_dirty(struct page
*page
)
8957 return __set_page_dirty_nobuffers(page
);
8960 static int btrfs_permission(struct inode
*inode
, int mask
)
8962 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8963 umode_t mode
= inode
->i_mode
;
8965 if (mask
& MAY_WRITE
&&
8966 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8967 if (btrfs_root_readonly(root
))
8969 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8972 return generic_permission(inode
, mask
);
8975 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
8977 struct btrfs_trans_handle
*trans
;
8978 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8979 struct inode
*inode
= NULL
;
8985 * 5 units required for adding orphan entry
8987 trans
= btrfs_start_transaction(root
, 5);
8989 return PTR_ERR(trans
);
8991 ret
= btrfs_find_free_ino(root
, &objectid
);
8995 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
8996 btrfs_ino(dir
), objectid
, mode
, &index
);
8997 if (IS_ERR(inode
)) {
8998 ret
= PTR_ERR(inode
);
9003 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
9007 ret
= btrfs_update_inode(trans
, root
, inode
);
9011 inode
->i_fop
= &btrfs_file_operations
;
9012 inode
->i_op
= &btrfs_file_inode_operations
;
9014 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9015 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
9016 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9018 ret
= btrfs_orphan_add(trans
, inode
);
9023 * We set number of links to 0 in btrfs_new_inode(), and here we set
9024 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9027 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9029 set_nlink(inode
, 1);
9030 d_tmpfile(dentry
, inode
);
9031 mark_inode_dirty(inode
);
9034 btrfs_end_transaction(trans
, root
);
9037 btrfs_balance_delayed_items(root
);
9038 btrfs_btree_balance_dirty(root
);
9043 static const struct inode_operations btrfs_dir_inode_operations
= {
9044 .getattr
= btrfs_getattr
,
9045 .lookup
= btrfs_lookup
,
9046 .create
= btrfs_create
,
9047 .unlink
= btrfs_unlink
,
9049 .mkdir
= btrfs_mkdir
,
9050 .rmdir
= btrfs_rmdir
,
9051 .rename
= btrfs_rename
,
9052 .symlink
= btrfs_symlink
,
9053 .setattr
= btrfs_setattr
,
9054 .mknod
= btrfs_mknod
,
9055 .setxattr
= btrfs_setxattr
,
9056 .getxattr
= btrfs_getxattr
,
9057 .listxattr
= btrfs_listxattr
,
9058 .removexattr
= btrfs_removexattr
,
9059 .permission
= btrfs_permission
,
9060 .get_acl
= btrfs_get_acl
,
9061 .set_acl
= btrfs_set_acl
,
9062 .update_time
= btrfs_update_time
,
9063 .tmpfile
= btrfs_tmpfile
,
9065 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
9066 .lookup
= btrfs_lookup
,
9067 .permission
= btrfs_permission
,
9068 .get_acl
= btrfs_get_acl
,
9069 .set_acl
= btrfs_set_acl
,
9070 .update_time
= btrfs_update_time
,
9073 static const struct file_operations btrfs_dir_file_operations
= {
9074 .llseek
= generic_file_llseek
,
9075 .read
= generic_read_dir
,
9076 .iterate
= btrfs_real_readdir
,
9077 .unlocked_ioctl
= btrfs_ioctl
,
9078 #ifdef CONFIG_COMPAT
9079 .compat_ioctl
= btrfs_ioctl
,
9081 .release
= btrfs_release_file
,
9082 .fsync
= btrfs_sync_file
,
9085 static struct extent_io_ops btrfs_extent_io_ops
= {
9086 .fill_delalloc
= run_delalloc_range
,
9087 .submit_bio_hook
= btrfs_submit_bio_hook
,
9088 .merge_bio_hook
= btrfs_merge_bio_hook
,
9089 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
9090 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
9091 .writepage_start_hook
= btrfs_writepage_start_hook
,
9092 .set_bit_hook
= btrfs_set_bit_hook
,
9093 .clear_bit_hook
= btrfs_clear_bit_hook
,
9094 .merge_extent_hook
= btrfs_merge_extent_hook
,
9095 .split_extent_hook
= btrfs_split_extent_hook
,
9099 * btrfs doesn't support the bmap operation because swapfiles
9100 * use bmap to make a mapping of extents in the file. They assume
9101 * these extents won't change over the life of the file and they
9102 * use the bmap result to do IO directly to the drive.
9104 * the btrfs bmap call would return logical addresses that aren't
9105 * suitable for IO and they also will change frequently as COW
9106 * operations happen. So, swapfile + btrfs == corruption.
9108 * For now we're avoiding this by dropping bmap.
9110 static const struct address_space_operations btrfs_aops
= {
9111 .readpage
= btrfs_readpage
,
9112 .writepage
= btrfs_writepage
,
9113 .writepages
= btrfs_writepages
,
9114 .readpages
= btrfs_readpages
,
9115 .direct_IO
= btrfs_direct_IO
,
9116 .invalidatepage
= btrfs_invalidatepage
,
9117 .releasepage
= btrfs_releasepage
,
9118 .set_page_dirty
= btrfs_set_page_dirty
,
9119 .error_remove_page
= generic_error_remove_page
,
9122 static const struct address_space_operations btrfs_symlink_aops
= {
9123 .readpage
= btrfs_readpage
,
9124 .writepage
= btrfs_writepage
,
9125 .invalidatepage
= btrfs_invalidatepage
,
9126 .releasepage
= btrfs_releasepage
,
9129 static const struct inode_operations btrfs_file_inode_operations
= {
9130 .getattr
= btrfs_getattr
,
9131 .setattr
= btrfs_setattr
,
9132 .setxattr
= btrfs_setxattr
,
9133 .getxattr
= btrfs_getxattr
,
9134 .listxattr
= btrfs_listxattr
,
9135 .removexattr
= btrfs_removexattr
,
9136 .permission
= btrfs_permission
,
9137 .fiemap
= btrfs_fiemap
,
9138 .get_acl
= btrfs_get_acl
,
9139 .set_acl
= btrfs_set_acl
,
9140 .update_time
= btrfs_update_time
,
9142 static const struct inode_operations btrfs_special_inode_operations
= {
9143 .getattr
= btrfs_getattr
,
9144 .setattr
= btrfs_setattr
,
9145 .permission
= btrfs_permission
,
9146 .setxattr
= btrfs_setxattr
,
9147 .getxattr
= btrfs_getxattr
,
9148 .listxattr
= btrfs_listxattr
,
9149 .removexattr
= btrfs_removexattr
,
9150 .get_acl
= btrfs_get_acl
,
9151 .set_acl
= btrfs_set_acl
,
9152 .update_time
= btrfs_update_time
,
9154 static const struct inode_operations btrfs_symlink_inode_operations
= {
9155 .readlink
= generic_readlink
,
9156 .follow_link
= page_follow_link_light
,
9157 .put_link
= page_put_link
,
9158 .getattr
= btrfs_getattr
,
9159 .setattr
= btrfs_setattr
,
9160 .permission
= btrfs_permission
,
9161 .setxattr
= btrfs_setxattr
,
9162 .getxattr
= btrfs_getxattr
,
9163 .listxattr
= btrfs_listxattr
,
9164 .removexattr
= btrfs_removexattr
,
9165 .update_time
= btrfs_update_time
,
9168 const struct dentry_operations btrfs_dentry_operations
= {
9169 .d_delete
= btrfs_dentry_delete
,
9170 .d_release
= btrfs_dentry_release
,