2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
60 struct btrfs_iget_args
{
62 struct btrfs_root
*root
;
65 static const struct inode_operations btrfs_dir_inode_operations
;
66 static const struct inode_operations btrfs_symlink_inode_operations
;
67 static const struct inode_operations btrfs_dir_ro_inode_operations
;
68 static const struct inode_operations btrfs_special_inode_operations
;
69 static const struct inode_operations btrfs_file_inode_operations
;
70 static const struct address_space_operations btrfs_aops
;
71 static const struct address_space_operations btrfs_symlink_aops
;
72 static const struct file_operations btrfs_dir_file_operations
;
73 static struct extent_io_ops btrfs_extent_io_ops
;
75 static struct kmem_cache
*btrfs_inode_cachep
;
76 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
77 struct kmem_cache
*btrfs_trans_handle_cachep
;
78 struct kmem_cache
*btrfs_transaction_cachep
;
79 struct kmem_cache
*btrfs_path_cachep
;
80 struct kmem_cache
*btrfs_free_space_cachep
;
83 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
84 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
85 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
86 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
87 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
88 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
89 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
90 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
93 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
94 static int btrfs_truncate(struct inode
*inode
);
95 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
96 static noinline
int cow_file_range(struct inode
*inode
,
97 struct page
*locked_page
,
98 u64 start
, u64 end
, int *page_started
,
99 unsigned long *nr_written
, int unlock
);
100 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
101 u64 len
, u64 orig_start
,
102 u64 block_start
, u64 block_len
,
103 u64 orig_block_len
, int type
);
105 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
106 struct inode
*inode
, struct inode
*dir
,
107 const struct qstr
*qstr
)
111 err
= btrfs_init_acl(trans
, inode
, dir
);
113 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
118 * this does all the hard work for inserting an inline extent into
119 * the btree. The caller should have done a btrfs_drop_extents so that
120 * no overlapping inline items exist in the btree
122 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
123 struct btrfs_root
*root
, struct inode
*inode
,
124 u64 start
, size_t size
, size_t compressed_size
,
126 struct page
**compressed_pages
)
128 struct btrfs_key key
;
129 struct btrfs_path
*path
;
130 struct extent_buffer
*leaf
;
131 struct page
*page
= NULL
;
134 struct btrfs_file_extent_item
*ei
;
137 size_t cur_size
= size
;
139 unsigned long offset
;
141 if (compressed_size
&& compressed_pages
)
142 cur_size
= compressed_size
;
144 path
= btrfs_alloc_path();
148 path
->leave_spinning
= 1;
150 key
.objectid
= btrfs_ino(inode
);
152 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
153 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
155 inode_add_bytes(inode
, size
);
156 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
162 leaf
= path
->nodes
[0];
163 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
164 struct btrfs_file_extent_item
);
165 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
166 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
167 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
168 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
169 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
170 ptr
= btrfs_file_extent_inline_start(ei
);
172 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
175 while (compressed_size
> 0) {
176 cpage
= compressed_pages
[i
];
177 cur_size
= min_t(unsigned long, compressed_size
,
180 kaddr
= kmap_atomic(cpage
);
181 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
182 kunmap_atomic(kaddr
);
186 compressed_size
-= cur_size
;
188 btrfs_set_file_extent_compression(leaf
, ei
,
191 page
= find_get_page(inode
->i_mapping
,
192 start
>> PAGE_CACHE_SHIFT
);
193 btrfs_set_file_extent_compression(leaf
, ei
, 0);
194 kaddr
= kmap_atomic(page
);
195 offset
= start
& (PAGE_CACHE_SIZE
- 1);
196 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
197 kunmap_atomic(kaddr
);
198 page_cache_release(page
);
200 btrfs_mark_buffer_dirty(leaf
);
201 btrfs_free_path(path
);
204 * we're an inline extent, so nobody can
205 * extend the file past i_size without locking
206 * a page we already have locked.
208 * We must do any isize and inode updates
209 * before we unlock the pages. Otherwise we
210 * could end up racing with unlink.
212 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
213 ret
= btrfs_update_inode(trans
, root
, inode
);
217 btrfs_free_path(path
);
223 * conditionally insert an inline extent into the file. This
224 * does the checks required to make sure the data is small enough
225 * to fit as an inline extent.
227 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
228 struct btrfs_root
*root
,
229 struct inode
*inode
, u64 start
, u64 end
,
230 size_t compressed_size
, int compress_type
,
231 struct page
**compressed_pages
)
233 u64 isize
= i_size_read(inode
);
234 u64 actual_end
= min(end
+ 1, isize
);
235 u64 inline_len
= actual_end
- start
;
236 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
237 u64 data_len
= inline_len
;
241 data_len
= compressed_size
;
244 actual_end
>= PAGE_CACHE_SIZE
||
245 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
247 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
249 data_len
> root
->fs_info
->max_inline
) {
253 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
257 if (isize
> actual_end
)
258 inline_len
= min_t(u64
, isize
, actual_end
);
259 ret
= insert_inline_extent(trans
, root
, inode
, start
,
260 inline_len
, compressed_size
,
261 compress_type
, compressed_pages
);
262 if (ret
&& ret
!= -ENOSPC
) {
263 btrfs_abort_transaction(trans
, root
, ret
);
265 } else if (ret
== -ENOSPC
) {
269 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
270 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
271 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
275 struct async_extent
{
280 unsigned long nr_pages
;
282 struct list_head list
;
287 struct btrfs_root
*root
;
288 struct page
*locked_page
;
291 struct list_head extents
;
292 struct btrfs_work work
;
295 static noinline
int add_async_extent(struct async_cow
*cow
,
296 u64 start
, u64 ram_size
,
299 unsigned long nr_pages
,
302 struct async_extent
*async_extent
;
304 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
305 BUG_ON(!async_extent
); /* -ENOMEM */
306 async_extent
->start
= start
;
307 async_extent
->ram_size
= ram_size
;
308 async_extent
->compressed_size
= compressed_size
;
309 async_extent
->pages
= pages
;
310 async_extent
->nr_pages
= nr_pages
;
311 async_extent
->compress_type
= compress_type
;
312 list_add_tail(&async_extent
->list
, &cow
->extents
);
317 * we create compressed extents in two phases. The first
318 * phase compresses a range of pages that have already been
319 * locked (both pages and state bits are locked).
321 * This is done inside an ordered work queue, and the compression
322 * is spread across many cpus. The actual IO submission is step
323 * two, and the ordered work queue takes care of making sure that
324 * happens in the same order things were put onto the queue by
325 * writepages and friends.
327 * If this code finds it can't get good compression, it puts an
328 * entry onto the work queue to write the uncompressed bytes. This
329 * makes sure that both compressed inodes and uncompressed inodes
330 * are written in the same order that the flusher thread sent them
333 static noinline
int compress_file_range(struct inode
*inode
,
334 struct page
*locked_page
,
336 struct async_cow
*async_cow
,
339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
340 struct btrfs_trans_handle
*trans
;
342 u64 blocksize
= root
->sectorsize
;
344 u64 isize
= i_size_read(inode
);
346 struct page
**pages
= NULL
;
347 unsigned long nr_pages
;
348 unsigned long nr_pages_ret
= 0;
349 unsigned long total_compressed
= 0;
350 unsigned long total_in
= 0;
351 unsigned long max_compressed
= 128 * 1024;
352 unsigned long max_uncompressed
= 128 * 1024;
355 int compress_type
= root
->fs_info
->compress_type
;
358 /* if this is a small write inside eof, kick off a defrag */
359 if ((end
- start
+ 1) < 16 * 1024 &&
360 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
361 btrfs_add_inode_defrag(NULL
, inode
);
363 actual_end
= min_t(u64
, isize
, end
+ 1);
366 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
367 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
370 * we don't want to send crud past the end of i_size through
371 * compression, that's just a waste of CPU time. So, if the
372 * end of the file is before the start of our current
373 * requested range of bytes, we bail out to the uncompressed
374 * cleanup code that can deal with all of this.
376 * It isn't really the fastest way to fix things, but this is a
377 * very uncommon corner.
379 if (actual_end
<= start
)
380 goto cleanup_and_bail_uncompressed
;
382 total_compressed
= actual_end
- start
;
384 /* we want to make sure that amount of ram required to uncompress
385 * an extent is reasonable, so we limit the total size in ram
386 * of a compressed extent to 128k. This is a crucial number
387 * because it also controls how easily we can spread reads across
388 * cpus for decompression.
390 * We also want to make sure the amount of IO required to do
391 * a random read is reasonably small, so we limit the size of
392 * a compressed extent to 128k.
394 total_compressed
= min(total_compressed
, max_uncompressed
);
395 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
396 num_bytes
= max(blocksize
, num_bytes
);
401 * we do compression for mount -o compress and when the
402 * inode has not been flagged as nocompress. This flag can
403 * change at any time if we discover bad compression ratios.
405 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
406 (btrfs_test_opt(root
, COMPRESS
) ||
407 (BTRFS_I(inode
)->force_compress
) ||
408 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
410 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
412 /* just bail out to the uncompressed code */
416 if (BTRFS_I(inode
)->force_compress
)
417 compress_type
= BTRFS_I(inode
)->force_compress
;
420 * we need to call clear_page_dirty_for_io on each
421 * page in the range. Otherwise applications with the file
422 * mmap'd can wander in and change the page contents while
423 * we are compressing them.
425 * If the compression fails for any reason, we set the pages
426 * dirty again later on.
428 extent_range_clear_dirty_for_io(inode
, start
, end
);
430 ret
= btrfs_compress_pages(compress_type
,
431 inode
->i_mapping
, start
,
432 total_compressed
, pages
,
433 nr_pages
, &nr_pages_ret
,
439 unsigned long offset
= total_compressed
&
440 (PAGE_CACHE_SIZE
- 1);
441 struct page
*page
= pages
[nr_pages_ret
- 1];
444 /* zero the tail end of the last page, we might be
445 * sending it down to disk
448 kaddr
= kmap_atomic(page
);
449 memset(kaddr
+ offset
, 0,
450 PAGE_CACHE_SIZE
- offset
);
451 kunmap_atomic(kaddr
);
458 trans
= btrfs_join_transaction(root
);
460 ret
= PTR_ERR(trans
);
462 goto cleanup_and_out
;
464 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
466 /* lets try to make an inline extent */
467 if (ret
|| total_in
< (actual_end
- start
)) {
468 /* we didn't compress the entire range, try
469 * to make an uncompressed inline extent.
471 ret
= cow_file_range_inline(trans
, root
, inode
,
472 start
, end
, 0, 0, NULL
);
474 /* try making a compressed inline extent */
475 ret
= cow_file_range_inline(trans
, root
, inode
,
478 compress_type
, pages
);
482 * inline extent creation worked or returned error,
483 * we don't need to create any more async work items.
484 * Unlock and free up our temp pages.
486 extent_clear_unlock_delalloc(inode
,
487 &BTRFS_I(inode
)->io_tree
,
489 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
490 EXTENT_CLEAR_DELALLOC
|
491 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
493 btrfs_end_transaction(trans
, root
);
496 btrfs_end_transaction(trans
, root
);
501 * we aren't doing an inline extent round the compressed size
502 * up to a block size boundary so the allocator does sane
505 total_compressed
= ALIGN(total_compressed
, blocksize
);
508 * one last check to make sure the compression is really a
509 * win, compare the page count read with the blocks on disk
511 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
512 if (total_compressed
>= total_in
) {
515 num_bytes
= total_in
;
518 if (!will_compress
&& pages
) {
520 * the compression code ran but failed to make things smaller,
521 * free any pages it allocated and our page pointer array
523 for (i
= 0; i
< nr_pages_ret
; i
++) {
524 WARN_ON(pages
[i
]->mapping
);
525 page_cache_release(pages
[i
]);
529 total_compressed
= 0;
532 /* flag the file so we don't compress in the future */
533 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
534 !(BTRFS_I(inode
)->force_compress
)) {
535 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
541 /* the async work queues will take care of doing actual
542 * allocation on disk for these compressed pages,
543 * and will submit them to the elevator.
545 add_async_extent(async_cow
, start
, num_bytes
,
546 total_compressed
, pages
, nr_pages_ret
,
549 if (start
+ num_bytes
< end
) {
556 cleanup_and_bail_uncompressed
:
558 * No compression, but we still need to write the pages in
559 * the file we've been given so far. redirty the locked
560 * page if it corresponds to our extent and set things up
561 * for the async work queue to run cow_file_range to do
562 * the normal delalloc dance
564 if (page_offset(locked_page
) >= start
&&
565 page_offset(locked_page
) <= end
) {
566 __set_page_dirty_nobuffers(locked_page
);
567 /* unlocked later on in the async handlers */
570 extent_range_redirty_for_io(inode
, start
, end
);
571 add_async_extent(async_cow
, start
, end
- start
+ 1,
572 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
580 for (i
= 0; i
< nr_pages_ret
; i
++) {
581 WARN_ON(pages
[i
]->mapping
);
582 page_cache_release(pages
[i
]);
589 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
591 EXTENT_CLEAR_UNLOCK_PAGE
|
593 EXTENT_CLEAR_DELALLOC
|
594 EXTENT_SET_WRITEBACK
|
595 EXTENT_END_WRITEBACK
);
596 if (!trans
|| IS_ERR(trans
))
597 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
599 btrfs_abort_transaction(trans
, root
, ret
);
604 * phase two of compressed writeback. This is the ordered portion
605 * of the code, which only gets called in the order the work was
606 * queued. We walk all the async extents created by compress_file_range
607 * and send them down to the disk.
609 static noinline
int submit_compressed_extents(struct inode
*inode
,
610 struct async_cow
*async_cow
)
612 struct async_extent
*async_extent
;
614 struct btrfs_trans_handle
*trans
;
615 struct btrfs_key ins
;
616 struct extent_map
*em
;
617 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
618 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
619 struct extent_io_tree
*io_tree
;
622 if (list_empty(&async_cow
->extents
))
626 while (!list_empty(&async_cow
->extents
)) {
627 async_extent
= list_entry(async_cow
->extents
.next
,
628 struct async_extent
, list
);
629 list_del(&async_extent
->list
);
631 io_tree
= &BTRFS_I(inode
)->io_tree
;
634 /* did the compression code fall back to uncompressed IO? */
635 if (!async_extent
->pages
) {
636 int page_started
= 0;
637 unsigned long nr_written
= 0;
639 lock_extent(io_tree
, async_extent
->start
,
640 async_extent
->start
+
641 async_extent
->ram_size
- 1);
643 /* allocate blocks */
644 ret
= cow_file_range(inode
, async_cow
->locked_page
,
646 async_extent
->start
+
647 async_extent
->ram_size
- 1,
648 &page_started
, &nr_written
, 0);
653 * if page_started, cow_file_range inserted an
654 * inline extent and took care of all the unlocking
655 * and IO for us. Otherwise, we need to submit
656 * all those pages down to the drive.
658 if (!page_started
&& !ret
)
659 extent_write_locked_range(io_tree
,
660 inode
, async_extent
->start
,
661 async_extent
->start
+
662 async_extent
->ram_size
- 1,
666 unlock_page(async_cow
->locked_page
);
672 lock_extent(io_tree
, async_extent
->start
,
673 async_extent
->start
+ async_extent
->ram_size
- 1);
675 trans
= btrfs_join_transaction(root
);
677 ret
= PTR_ERR(trans
);
679 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
680 ret
= btrfs_reserve_extent(trans
, root
,
681 async_extent
->compressed_size
,
682 async_extent
->compressed_size
,
683 0, alloc_hint
, &ins
, 1);
684 if (ret
&& ret
!= -ENOSPC
)
685 btrfs_abort_transaction(trans
, root
, ret
);
686 btrfs_end_transaction(trans
, root
);
692 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
693 WARN_ON(async_extent
->pages
[i
]->mapping
);
694 page_cache_release(async_extent
->pages
[i
]);
696 kfree(async_extent
->pages
);
697 async_extent
->nr_pages
= 0;
698 async_extent
->pages
= NULL
;
706 * here we're doing allocation and writeback of the
709 btrfs_drop_extent_cache(inode
, async_extent
->start
,
710 async_extent
->start
+
711 async_extent
->ram_size
- 1, 0);
713 em
= alloc_extent_map();
715 goto out_free_reserve
;
716 em
->start
= async_extent
->start
;
717 em
->len
= async_extent
->ram_size
;
718 em
->orig_start
= em
->start
;
719 em
->mod_start
= em
->start
;
720 em
->mod_len
= em
->len
;
722 em
->block_start
= ins
.objectid
;
723 em
->block_len
= ins
.offset
;
724 em
->orig_block_len
= ins
.offset
;
725 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
726 em
->compress_type
= async_extent
->compress_type
;
727 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
728 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
732 write_lock(&em_tree
->lock
);
733 ret
= add_extent_mapping(em_tree
, em
);
736 &em_tree
->modified_extents
);
737 write_unlock(&em_tree
->lock
);
738 if (ret
!= -EEXIST
) {
742 btrfs_drop_extent_cache(inode
, async_extent
->start
,
743 async_extent
->start
+
744 async_extent
->ram_size
- 1, 0);
748 goto out_free_reserve
;
750 ret
= btrfs_add_ordered_extent_compress(inode
,
753 async_extent
->ram_size
,
755 BTRFS_ORDERED_COMPRESSED
,
756 async_extent
->compress_type
);
758 goto out_free_reserve
;
761 * clear dirty, set writeback and unlock the pages.
763 extent_clear_unlock_delalloc(inode
,
764 &BTRFS_I(inode
)->io_tree
,
766 async_extent
->start
+
767 async_extent
->ram_size
- 1,
768 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
769 EXTENT_CLEAR_UNLOCK
|
770 EXTENT_CLEAR_DELALLOC
|
771 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
773 ret
= btrfs_submit_compressed_write(inode
,
775 async_extent
->ram_size
,
777 ins
.offset
, async_extent
->pages
,
778 async_extent
->nr_pages
);
779 alloc_hint
= ins
.objectid
+ ins
.offset
;
789 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
791 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
793 async_extent
->start
+
794 async_extent
->ram_size
- 1,
795 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
796 EXTENT_CLEAR_UNLOCK
|
797 EXTENT_CLEAR_DELALLOC
|
799 EXTENT_SET_WRITEBACK
|
800 EXTENT_END_WRITEBACK
);
805 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
808 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
809 struct extent_map
*em
;
812 read_lock(&em_tree
->lock
);
813 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
816 * if block start isn't an actual block number then find the
817 * first block in this inode and use that as a hint. If that
818 * block is also bogus then just don't worry about it.
820 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
822 em
= search_extent_mapping(em_tree
, 0, 0);
823 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
824 alloc_hint
= em
->block_start
;
828 alloc_hint
= em
->block_start
;
832 read_unlock(&em_tree
->lock
);
838 * when extent_io.c finds a delayed allocation range in the file,
839 * the call backs end up in this code. The basic idea is to
840 * allocate extents on disk for the range, and create ordered data structs
841 * in ram to track those extents.
843 * locked_page is the page that writepage had locked already. We use
844 * it to make sure we don't do extra locks or unlocks.
846 * *page_started is set to one if we unlock locked_page and do everything
847 * required to start IO on it. It may be clean and already done with
850 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
852 struct btrfs_root
*root
,
853 struct page
*locked_page
,
854 u64 start
, u64 end
, int *page_started
,
855 unsigned long *nr_written
,
860 unsigned long ram_size
;
863 u64 blocksize
= root
->sectorsize
;
864 struct btrfs_key ins
;
865 struct extent_map
*em
;
866 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
869 BUG_ON(btrfs_is_free_space_inode(inode
));
871 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
872 num_bytes
= max(blocksize
, num_bytes
);
873 disk_num_bytes
= num_bytes
;
875 /* if this is a small write inside eof, kick off defrag */
876 if (num_bytes
< 64 * 1024 &&
877 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
878 btrfs_add_inode_defrag(trans
, inode
);
881 /* lets try to make an inline extent */
882 ret
= cow_file_range_inline(trans
, root
, inode
,
883 start
, end
, 0, 0, NULL
);
885 extent_clear_unlock_delalloc(inode
,
886 &BTRFS_I(inode
)->io_tree
,
888 EXTENT_CLEAR_UNLOCK_PAGE
|
889 EXTENT_CLEAR_UNLOCK
|
890 EXTENT_CLEAR_DELALLOC
|
892 EXTENT_SET_WRITEBACK
|
893 EXTENT_END_WRITEBACK
);
895 *nr_written
= *nr_written
+
896 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
899 } else if (ret
< 0) {
900 btrfs_abort_transaction(trans
, root
, ret
);
905 BUG_ON(disk_num_bytes
>
906 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
908 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
909 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
911 while (disk_num_bytes
> 0) {
914 cur_alloc_size
= disk_num_bytes
;
915 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
916 root
->sectorsize
, 0, alloc_hint
,
919 btrfs_abort_transaction(trans
, root
, ret
);
923 em
= alloc_extent_map();
924 BUG_ON(!em
); /* -ENOMEM */
926 em
->orig_start
= em
->start
;
927 ram_size
= ins
.offset
;
928 em
->len
= ins
.offset
;
929 em
->mod_start
= em
->start
;
930 em
->mod_len
= em
->len
;
932 em
->block_start
= ins
.objectid
;
933 em
->block_len
= ins
.offset
;
934 em
->orig_block_len
= ins
.offset
;
935 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
936 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
940 write_lock(&em_tree
->lock
);
941 ret
= add_extent_mapping(em_tree
, em
);
944 &em_tree
->modified_extents
);
945 write_unlock(&em_tree
->lock
);
946 if (ret
!= -EEXIST
) {
950 btrfs_drop_extent_cache(inode
, start
,
951 start
+ ram_size
- 1, 0);
954 cur_alloc_size
= ins
.offset
;
955 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
956 ram_size
, cur_alloc_size
, 0);
957 BUG_ON(ret
); /* -ENOMEM */
959 if (root
->root_key
.objectid
==
960 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
961 ret
= btrfs_reloc_clone_csums(inode
, start
,
964 btrfs_abort_transaction(trans
, root
, ret
);
969 if (disk_num_bytes
< cur_alloc_size
)
972 /* we're not doing compressed IO, don't unlock the first
973 * page (which the caller expects to stay locked), don't
974 * clear any dirty bits and don't set any writeback bits
976 * Do set the Private2 bit so we know this page was properly
977 * setup for writepage
979 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
980 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
983 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
984 start
, start
+ ram_size
- 1,
986 disk_num_bytes
-= cur_alloc_size
;
987 num_bytes
-= cur_alloc_size
;
988 alloc_hint
= ins
.objectid
+ ins
.offset
;
989 start
+= cur_alloc_size
;
995 extent_clear_unlock_delalloc(inode
,
996 &BTRFS_I(inode
)->io_tree
,
997 start
, end
, locked_page
,
998 EXTENT_CLEAR_UNLOCK_PAGE
|
999 EXTENT_CLEAR_UNLOCK
|
1000 EXTENT_CLEAR_DELALLOC
|
1001 EXTENT_CLEAR_DIRTY
|
1002 EXTENT_SET_WRITEBACK
|
1003 EXTENT_END_WRITEBACK
);
1008 static noinline
int cow_file_range(struct inode
*inode
,
1009 struct page
*locked_page
,
1010 u64 start
, u64 end
, int *page_started
,
1011 unsigned long *nr_written
,
1014 struct btrfs_trans_handle
*trans
;
1015 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1018 trans
= btrfs_join_transaction(root
);
1019 if (IS_ERR(trans
)) {
1020 extent_clear_unlock_delalloc(inode
,
1021 &BTRFS_I(inode
)->io_tree
,
1022 start
, end
, locked_page
,
1023 EXTENT_CLEAR_UNLOCK_PAGE
|
1024 EXTENT_CLEAR_UNLOCK
|
1025 EXTENT_CLEAR_DELALLOC
|
1026 EXTENT_CLEAR_DIRTY
|
1027 EXTENT_SET_WRITEBACK
|
1028 EXTENT_END_WRITEBACK
);
1029 return PTR_ERR(trans
);
1031 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1033 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1034 page_started
, nr_written
, unlock
);
1036 btrfs_end_transaction(trans
, root
);
1042 * work queue call back to started compression on a file and pages
1044 static noinline
void async_cow_start(struct btrfs_work
*work
)
1046 struct async_cow
*async_cow
;
1048 async_cow
= container_of(work
, struct async_cow
, work
);
1050 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1051 async_cow
->start
, async_cow
->end
, async_cow
,
1053 if (num_added
== 0) {
1054 btrfs_add_delayed_iput(async_cow
->inode
);
1055 async_cow
->inode
= NULL
;
1060 * work queue call back to submit previously compressed pages
1062 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1064 struct async_cow
*async_cow
;
1065 struct btrfs_root
*root
;
1066 unsigned long nr_pages
;
1068 async_cow
= container_of(work
, struct async_cow
, work
);
1070 root
= async_cow
->root
;
1071 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1074 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1076 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1077 wake_up(&root
->fs_info
->async_submit_wait
);
1079 if (async_cow
->inode
)
1080 submit_compressed_extents(async_cow
->inode
, async_cow
);
1083 static noinline
void async_cow_free(struct btrfs_work
*work
)
1085 struct async_cow
*async_cow
;
1086 async_cow
= container_of(work
, struct async_cow
, work
);
1087 if (async_cow
->inode
)
1088 btrfs_add_delayed_iput(async_cow
->inode
);
1092 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1093 u64 start
, u64 end
, int *page_started
,
1094 unsigned long *nr_written
)
1096 struct async_cow
*async_cow
;
1097 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1098 unsigned long nr_pages
;
1100 int limit
= 10 * 1024 * 1024;
1102 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1103 1, 0, NULL
, GFP_NOFS
);
1104 while (start
< end
) {
1105 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1106 BUG_ON(!async_cow
); /* -ENOMEM */
1107 async_cow
->inode
= igrab(inode
);
1108 async_cow
->root
= root
;
1109 async_cow
->locked_page
= locked_page
;
1110 async_cow
->start
= start
;
1112 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1115 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1117 async_cow
->end
= cur_end
;
1118 INIT_LIST_HEAD(&async_cow
->extents
);
1120 async_cow
->work
.func
= async_cow_start
;
1121 async_cow
->work
.ordered_func
= async_cow_submit
;
1122 async_cow
->work
.ordered_free
= async_cow_free
;
1123 async_cow
->work
.flags
= 0;
1125 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1127 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1129 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1132 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1133 wait_event(root
->fs_info
->async_submit_wait
,
1134 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1138 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1139 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1140 wait_event(root
->fs_info
->async_submit_wait
,
1141 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1145 *nr_written
+= nr_pages
;
1146 start
= cur_end
+ 1;
1152 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1153 u64 bytenr
, u64 num_bytes
)
1156 struct btrfs_ordered_sum
*sums
;
1159 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1160 bytenr
+ num_bytes
- 1, &list
, 0);
1161 if (ret
== 0 && list_empty(&list
))
1164 while (!list_empty(&list
)) {
1165 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1166 list_del(&sums
->list
);
1173 * when nowcow writeback call back. This checks for snapshots or COW copies
1174 * of the extents that exist in the file, and COWs the file as required.
1176 * If no cow copies or snapshots exist, we write directly to the existing
1179 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1180 struct page
*locked_page
,
1181 u64 start
, u64 end
, int *page_started
, int force
,
1182 unsigned long *nr_written
)
1184 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1185 struct btrfs_trans_handle
*trans
;
1186 struct extent_buffer
*leaf
;
1187 struct btrfs_path
*path
;
1188 struct btrfs_file_extent_item
*fi
;
1189 struct btrfs_key found_key
;
1203 u64 ino
= btrfs_ino(inode
);
1205 path
= btrfs_alloc_path();
1207 extent_clear_unlock_delalloc(inode
,
1208 &BTRFS_I(inode
)->io_tree
,
1209 start
, end
, locked_page
,
1210 EXTENT_CLEAR_UNLOCK_PAGE
|
1211 EXTENT_CLEAR_UNLOCK
|
1212 EXTENT_CLEAR_DELALLOC
|
1213 EXTENT_CLEAR_DIRTY
|
1214 EXTENT_SET_WRITEBACK
|
1215 EXTENT_END_WRITEBACK
);
1219 nolock
= btrfs_is_free_space_inode(inode
);
1222 trans
= btrfs_join_transaction_nolock(root
);
1224 trans
= btrfs_join_transaction(root
);
1226 if (IS_ERR(trans
)) {
1227 extent_clear_unlock_delalloc(inode
,
1228 &BTRFS_I(inode
)->io_tree
,
1229 start
, end
, locked_page
,
1230 EXTENT_CLEAR_UNLOCK_PAGE
|
1231 EXTENT_CLEAR_UNLOCK
|
1232 EXTENT_CLEAR_DELALLOC
|
1233 EXTENT_CLEAR_DIRTY
|
1234 EXTENT_SET_WRITEBACK
|
1235 EXTENT_END_WRITEBACK
);
1236 btrfs_free_path(path
);
1237 return PTR_ERR(trans
);
1240 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1242 cow_start
= (u64
)-1;
1245 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1248 btrfs_abort_transaction(trans
, root
, ret
);
1251 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1252 leaf
= path
->nodes
[0];
1253 btrfs_item_key_to_cpu(leaf
, &found_key
,
1254 path
->slots
[0] - 1);
1255 if (found_key
.objectid
== ino
&&
1256 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1261 leaf
= path
->nodes
[0];
1262 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1263 ret
= btrfs_next_leaf(root
, path
);
1265 btrfs_abort_transaction(trans
, root
, ret
);
1270 leaf
= path
->nodes
[0];
1276 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1278 if (found_key
.objectid
> ino
||
1279 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1280 found_key
.offset
> end
)
1283 if (found_key
.offset
> cur_offset
) {
1284 extent_end
= found_key
.offset
;
1289 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1290 struct btrfs_file_extent_item
);
1291 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1293 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1294 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1295 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1296 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1297 extent_end
= found_key
.offset
+
1298 btrfs_file_extent_num_bytes(leaf
, fi
);
1300 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1301 if (extent_end
<= start
) {
1305 if (disk_bytenr
== 0)
1307 if (btrfs_file_extent_compression(leaf
, fi
) ||
1308 btrfs_file_extent_encryption(leaf
, fi
) ||
1309 btrfs_file_extent_other_encoding(leaf
, fi
))
1311 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1313 if (btrfs_extent_readonly(root
, disk_bytenr
))
1315 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1317 extent_offset
, disk_bytenr
))
1319 disk_bytenr
+= extent_offset
;
1320 disk_bytenr
+= cur_offset
- found_key
.offset
;
1321 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1323 * force cow if csum exists in the range.
1324 * this ensure that csum for a given extent are
1325 * either valid or do not exist.
1327 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1330 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1331 extent_end
= found_key
.offset
+
1332 btrfs_file_extent_inline_len(leaf
, fi
);
1333 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1338 if (extent_end
<= start
) {
1343 if (cow_start
== (u64
)-1)
1344 cow_start
= cur_offset
;
1345 cur_offset
= extent_end
;
1346 if (cur_offset
> end
)
1352 btrfs_release_path(path
);
1353 if (cow_start
!= (u64
)-1) {
1354 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1355 cow_start
, found_key
.offset
- 1,
1356 page_started
, nr_written
, 1);
1358 btrfs_abort_transaction(trans
, root
, ret
);
1361 cow_start
= (u64
)-1;
1364 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1365 struct extent_map
*em
;
1366 struct extent_map_tree
*em_tree
;
1367 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1368 em
= alloc_extent_map();
1369 BUG_ON(!em
); /* -ENOMEM */
1370 em
->start
= cur_offset
;
1371 em
->orig_start
= found_key
.offset
- extent_offset
;
1372 em
->len
= num_bytes
;
1373 em
->block_len
= num_bytes
;
1374 em
->block_start
= disk_bytenr
;
1375 em
->orig_block_len
= disk_num_bytes
;
1376 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1377 em
->mod_start
= em
->start
;
1378 em
->mod_len
= em
->len
;
1379 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1380 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1381 em
->generation
= -1;
1383 write_lock(&em_tree
->lock
);
1384 ret
= add_extent_mapping(em_tree
, em
);
1386 list_move(&em
->list
,
1387 &em_tree
->modified_extents
);
1388 write_unlock(&em_tree
->lock
);
1389 if (ret
!= -EEXIST
) {
1390 free_extent_map(em
);
1393 btrfs_drop_extent_cache(inode
, em
->start
,
1394 em
->start
+ em
->len
- 1, 0);
1396 type
= BTRFS_ORDERED_PREALLOC
;
1398 type
= BTRFS_ORDERED_NOCOW
;
1401 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1402 num_bytes
, num_bytes
, type
);
1403 BUG_ON(ret
); /* -ENOMEM */
1405 if (root
->root_key
.objectid
==
1406 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1407 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1410 btrfs_abort_transaction(trans
, root
, ret
);
1415 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1416 cur_offset
, cur_offset
+ num_bytes
- 1,
1417 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1418 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1419 EXTENT_SET_PRIVATE2
);
1420 cur_offset
= extent_end
;
1421 if (cur_offset
> end
)
1424 btrfs_release_path(path
);
1426 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1427 cow_start
= cur_offset
;
1431 if (cow_start
!= (u64
)-1) {
1432 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1434 page_started
, nr_written
, 1);
1436 btrfs_abort_transaction(trans
, root
, ret
);
1442 err
= btrfs_end_transaction(trans
, root
);
1446 if (ret
&& cur_offset
< end
)
1447 extent_clear_unlock_delalloc(inode
,
1448 &BTRFS_I(inode
)->io_tree
,
1449 cur_offset
, end
, locked_page
,
1450 EXTENT_CLEAR_UNLOCK_PAGE
|
1451 EXTENT_CLEAR_UNLOCK
|
1452 EXTENT_CLEAR_DELALLOC
|
1453 EXTENT_CLEAR_DIRTY
|
1454 EXTENT_SET_WRITEBACK
|
1455 EXTENT_END_WRITEBACK
);
1457 btrfs_free_path(path
);
1462 * extent_io.c call back to do delayed allocation processing
1464 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1465 u64 start
, u64 end
, int *page_started
,
1466 unsigned long *nr_written
)
1469 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1471 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1472 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1473 page_started
, 1, nr_written
);
1474 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1475 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1476 page_started
, 0, nr_written
);
1477 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1478 !(BTRFS_I(inode
)->force_compress
) &&
1479 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1480 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1481 page_started
, nr_written
, 1);
1483 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1484 &BTRFS_I(inode
)->runtime_flags
);
1485 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1486 page_started
, nr_written
);
1491 static void btrfs_split_extent_hook(struct inode
*inode
,
1492 struct extent_state
*orig
, u64 split
)
1494 /* not delalloc, ignore it */
1495 if (!(orig
->state
& EXTENT_DELALLOC
))
1498 spin_lock(&BTRFS_I(inode
)->lock
);
1499 BTRFS_I(inode
)->outstanding_extents
++;
1500 spin_unlock(&BTRFS_I(inode
)->lock
);
1504 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1505 * extents so we can keep track of new extents that are just merged onto old
1506 * extents, such as when we are doing sequential writes, so we can properly
1507 * account for the metadata space we'll need.
1509 static void btrfs_merge_extent_hook(struct inode
*inode
,
1510 struct extent_state
*new,
1511 struct extent_state
*other
)
1513 /* not delalloc, ignore it */
1514 if (!(other
->state
& EXTENT_DELALLOC
))
1517 spin_lock(&BTRFS_I(inode
)->lock
);
1518 BTRFS_I(inode
)->outstanding_extents
--;
1519 spin_unlock(&BTRFS_I(inode
)->lock
);
1523 * extent_io.c set_bit_hook, used to track delayed allocation
1524 * bytes in this file, and to maintain the list of inodes that
1525 * have pending delalloc work to be done.
1527 static void btrfs_set_bit_hook(struct inode
*inode
,
1528 struct extent_state
*state
, int *bits
)
1532 * set_bit and clear bit hooks normally require _irqsave/restore
1533 * but in this case, we are only testing for the DELALLOC
1534 * bit, which is only set or cleared with irqs on
1536 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1537 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1538 u64 len
= state
->end
+ 1 - state
->start
;
1539 bool do_list
= !btrfs_is_free_space_inode(inode
);
1541 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1542 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1544 spin_lock(&BTRFS_I(inode
)->lock
);
1545 BTRFS_I(inode
)->outstanding_extents
++;
1546 spin_unlock(&BTRFS_I(inode
)->lock
);
1549 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1550 root
->fs_info
->delalloc_batch
);
1551 spin_lock(&BTRFS_I(inode
)->lock
);
1552 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1553 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1554 &BTRFS_I(inode
)->runtime_flags
)) {
1555 spin_lock(&root
->fs_info
->delalloc_lock
);
1556 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1557 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1558 &root
->fs_info
->delalloc_inodes
);
1559 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1560 &BTRFS_I(inode
)->runtime_flags
);
1562 spin_unlock(&root
->fs_info
->delalloc_lock
);
1564 spin_unlock(&BTRFS_I(inode
)->lock
);
1569 * extent_io.c clear_bit_hook, see set_bit_hook for why
1571 static void btrfs_clear_bit_hook(struct inode
*inode
,
1572 struct extent_state
*state
, int *bits
)
1575 * set_bit and clear bit hooks normally require _irqsave/restore
1576 * but in this case, we are only testing for the DELALLOC
1577 * bit, which is only set or cleared with irqs on
1579 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1581 u64 len
= state
->end
+ 1 - state
->start
;
1582 bool do_list
= !btrfs_is_free_space_inode(inode
);
1584 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1585 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1586 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1587 spin_lock(&BTRFS_I(inode
)->lock
);
1588 BTRFS_I(inode
)->outstanding_extents
--;
1589 spin_unlock(&BTRFS_I(inode
)->lock
);
1592 if (*bits
& EXTENT_DO_ACCOUNTING
)
1593 btrfs_delalloc_release_metadata(inode
, len
);
1595 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1597 btrfs_free_reserved_data_space(inode
, len
);
1599 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1600 root
->fs_info
->delalloc_batch
);
1601 spin_lock(&BTRFS_I(inode
)->lock
);
1602 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1603 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1604 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1605 &BTRFS_I(inode
)->runtime_flags
)) {
1606 spin_lock(&root
->fs_info
->delalloc_lock
);
1607 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1608 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1609 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1610 &BTRFS_I(inode
)->runtime_flags
);
1612 spin_unlock(&root
->fs_info
->delalloc_lock
);
1614 spin_unlock(&BTRFS_I(inode
)->lock
);
1619 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1620 * we don't create bios that span stripes or chunks
1622 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1623 size_t size
, struct bio
*bio
,
1624 unsigned long bio_flags
)
1626 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1627 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1632 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1635 length
= bio
->bi_size
;
1636 map_length
= length
;
1637 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1638 &map_length
, NULL
, 0);
1639 /* Will always return 0 with map_multi == NULL */
1641 if (map_length
< length
+ size
)
1647 * in order to insert checksums into the metadata in large chunks,
1648 * we wait until bio submission time. All the pages in the bio are
1649 * checksummed and sums are attached onto the ordered extent record.
1651 * At IO completion time the cums attached on the ordered extent record
1652 * are inserted into the btree
1654 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1655 struct bio
*bio
, int mirror_num
,
1656 unsigned long bio_flags
,
1659 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1662 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1663 BUG_ON(ret
); /* -ENOMEM */
1668 * in order to insert checksums into the metadata in large chunks,
1669 * we wait until bio submission time. All the pages in the bio are
1670 * checksummed and sums are attached onto the ordered extent record.
1672 * At IO completion time the cums attached on the ordered extent record
1673 * are inserted into the btree
1675 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1676 int mirror_num
, unsigned long bio_flags
,
1679 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1682 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1684 bio_endio(bio
, ret
);
1689 * extent_io.c submission hook. This does the right thing for csum calculation
1690 * on write, or reading the csums from the tree before a read
1692 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1693 int mirror_num
, unsigned long bio_flags
,
1696 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1700 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1702 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1704 if (btrfs_is_free_space_inode(inode
))
1707 if (!(rw
& REQ_WRITE
)) {
1708 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1712 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1713 ret
= btrfs_submit_compressed_read(inode
, bio
,
1717 } else if (!skip_sum
) {
1718 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1723 } else if (async
&& !skip_sum
) {
1724 /* csum items have already been cloned */
1725 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1727 /* we're doing a write, do the async checksumming */
1728 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1729 inode
, rw
, bio
, mirror_num
,
1730 bio_flags
, bio_offset
,
1731 __btrfs_submit_bio_start
,
1732 __btrfs_submit_bio_done
);
1734 } else if (!skip_sum
) {
1735 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1741 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1745 bio_endio(bio
, ret
);
1750 * given a list of ordered sums record them in the inode. This happens
1751 * at IO completion time based on sums calculated at bio submission time.
1753 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1754 struct inode
*inode
, u64 file_offset
,
1755 struct list_head
*list
)
1757 struct btrfs_ordered_sum
*sum
;
1759 list_for_each_entry(sum
, list
, list
) {
1760 trans
->adding_csums
= 1;
1761 btrfs_csum_file_blocks(trans
,
1762 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1763 trans
->adding_csums
= 0;
1768 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1769 struct extent_state
**cached_state
)
1771 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1772 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1773 cached_state
, GFP_NOFS
);
1776 /* see btrfs_writepage_start_hook for details on why this is required */
1777 struct btrfs_writepage_fixup
{
1779 struct btrfs_work work
;
1782 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1784 struct btrfs_writepage_fixup
*fixup
;
1785 struct btrfs_ordered_extent
*ordered
;
1786 struct extent_state
*cached_state
= NULL
;
1788 struct inode
*inode
;
1793 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1797 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1798 ClearPageChecked(page
);
1802 inode
= page
->mapping
->host
;
1803 page_start
= page_offset(page
);
1804 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1806 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1809 /* already ordered? We're done */
1810 if (PagePrivate2(page
))
1813 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1815 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1816 page_end
, &cached_state
, GFP_NOFS
);
1818 btrfs_start_ordered_extent(inode
, ordered
, 1);
1819 btrfs_put_ordered_extent(ordered
);
1823 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1825 mapping_set_error(page
->mapping
, ret
);
1826 end_extent_writepage(page
, ret
, page_start
, page_end
);
1827 ClearPageChecked(page
);
1831 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1832 ClearPageChecked(page
);
1833 set_page_dirty(page
);
1835 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1836 &cached_state
, GFP_NOFS
);
1839 page_cache_release(page
);
1844 * There are a few paths in the higher layers of the kernel that directly
1845 * set the page dirty bit without asking the filesystem if it is a
1846 * good idea. This causes problems because we want to make sure COW
1847 * properly happens and the data=ordered rules are followed.
1849 * In our case any range that doesn't have the ORDERED bit set
1850 * hasn't been properly setup for IO. We kick off an async process
1851 * to fix it up. The async helper will wait for ordered extents, set
1852 * the delalloc bit and make it safe to write the page.
1854 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1856 struct inode
*inode
= page
->mapping
->host
;
1857 struct btrfs_writepage_fixup
*fixup
;
1858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1860 /* this page is properly in the ordered list */
1861 if (TestClearPagePrivate2(page
))
1864 if (PageChecked(page
))
1867 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1871 SetPageChecked(page
);
1872 page_cache_get(page
);
1873 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1875 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1879 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1880 struct inode
*inode
, u64 file_pos
,
1881 u64 disk_bytenr
, u64 disk_num_bytes
,
1882 u64 num_bytes
, u64 ram_bytes
,
1883 u8 compression
, u8 encryption
,
1884 u16 other_encoding
, int extent_type
)
1886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1887 struct btrfs_file_extent_item
*fi
;
1888 struct btrfs_path
*path
;
1889 struct extent_buffer
*leaf
;
1890 struct btrfs_key ins
;
1893 path
= btrfs_alloc_path();
1897 path
->leave_spinning
= 1;
1900 * we may be replacing one extent in the tree with another.
1901 * The new extent is pinned in the extent map, and we don't want
1902 * to drop it from the cache until it is completely in the btree.
1904 * So, tell btrfs_drop_extents to leave this extent in the cache.
1905 * the caller is expected to unpin it and allow it to be merged
1908 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1909 file_pos
+ num_bytes
, 0);
1913 ins
.objectid
= btrfs_ino(inode
);
1914 ins
.offset
= file_pos
;
1915 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1916 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1919 leaf
= path
->nodes
[0];
1920 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1921 struct btrfs_file_extent_item
);
1922 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1923 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1924 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1925 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1926 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1927 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1928 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1929 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1930 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1931 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1933 btrfs_mark_buffer_dirty(leaf
);
1934 btrfs_release_path(path
);
1936 inode_add_bytes(inode
, num_bytes
);
1938 ins
.objectid
= disk_bytenr
;
1939 ins
.offset
= disk_num_bytes
;
1940 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1941 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1942 root
->root_key
.objectid
,
1943 btrfs_ino(inode
), file_pos
, &ins
);
1945 btrfs_free_path(path
);
1950 /* snapshot-aware defrag */
1951 struct sa_defrag_extent_backref
{
1952 struct rb_node node
;
1953 struct old_sa_defrag_extent
*old
;
1962 struct old_sa_defrag_extent
{
1963 struct list_head list
;
1964 struct new_sa_defrag_extent
*new;
1973 struct new_sa_defrag_extent
{
1974 struct rb_root root
;
1975 struct list_head head
;
1976 struct btrfs_path
*path
;
1977 struct inode
*inode
;
1985 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1986 struct sa_defrag_extent_backref
*b2
)
1988 if (b1
->root_id
< b2
->root_id
)
1990 else if (b1
->root_id
> b2
->root_id
)
1993 if (b1
->inum
< b2
->inum
)
1995 else if (b1
->inum
> b2
->inum
)
1998 if (b1
->file_pos
< b2
->file_pos
)
2000 else if (b1
->file_pos
> b2
->file_pos
)
2004 * [------------------------------] ===> (a range of space)
2005 * |<--->| |<---->| =============> (fs/file tree A)
2006 * |<---------------------------->| ===> (fs/file tree B)
2008 * A range of space can refer to two file extents in one tree while
2009 * refer to only one file extent in another tree.
2011 * So we may process a disk offset more than one time(two extents in A)
2012 * and locate at the same extent(one extent in B), then insert two same
2013 * backrefs(both refer to the extent in B).
2018 static void backref_insert(struct rb_root
*root
,
2019 struct sa_defrag_extent_backref
*backref
)
2021 struct rb_node
**p
= &root
->rb_node
;
2022 struct rb_node
*parent
= NULL
;
2023 struct sa_defrag_extent_backref
*entry
;
2028 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2030 ret
= backref_comp(backref
, entry
);
2034 p
= &(*p
)->rb_right
;
2037 rb_link_node(&backref
->node
, parent
, p
);
2038 rb_insert_color(&backref
->node
, root
);
2042 * Note the backref might has changed, and in this case we just return 0.
2044 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2047 struct btrfs_file_extent_item
*extent
;
2048 struct btrfs_fs_info
*fs_info
;
2049 struct old_sa_defrag_extent
*old
= ctx
;
2050 struct new_sa_defrag_extent
*new = old
->new;
2051 struct btrfs_path
*path
= new->path
;
2052 struct btrfs_key key
;
2053 struct btrfs_root
*root
;
2054 struct sa_defrag_extent_backref
*backref
;
2055 struct extent_buffer
*leaf
;
2056 struct inode
*inode
= new->inode
;
2062 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2063 inum
== btrfs_ino(inode
))
2066 key
.objectid
= root_id
;
2067 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2068 key
.offset
= (u64
)-1;
2070 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2071 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2073 if (PTR_ERR(root
) == -ENOENT
)
2076 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2077 inum
, offset
, root_id
);
2078 return PTR_ERR(root
);
2081 key
.objectid
= inum
;
2082 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2083 if (offset
> (u64
)-1 << 32)
2086 key
.offset
= offset
;
2088 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2097 leaf
= path
->nodes
[0];
2098 slot
= path
->slots
[0];
2100 if (slot
>= btrfs_header_nritems(leaf
)) {
2101 ret
= btrfs_next_leaf(root
, path
);
2104 } else if (ret
> 0) {
2113 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2115 if (key
.objectid
> inum
)
2118 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2121 extent
= btrfs_item_ptr(leaf
, slot
,
2122 struct btrfs_file_extent_item
);
2124 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2127 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2128 if (key
.offset
- extent_offset
!= offset
)
2131 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2132 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2133 old
->len
|| extent_offset
+ num_bytes
<=
2134 old
->extent_offset
+ old
->offset
)
2140 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2146 backref
->root_id
= root_id
;
2147 backref
->inum
= inum
;
2148 backref
->file_pos
= offset
+ extent_offset
;
2149 backref
->num_bytes
= num_bytes
;
2150 backref
->extent_offset
= extent_offset
;
2151 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2153 backref_insert(&new->root
, backref
);
2156 btrfs_release_path(path
);
2161 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2162 struct new_sa_defrag_extent
*new)
2164 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2165 struct old_sa_defrag_extent
*old
, *tmp
;
2170 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2171 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2172 path
, record_one_backref
,
2174 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2176 /* no backref to be processed for this extent */
2178 list_del(&old
->list
);
2183 if (list_empty(&new->head
))
2189 static int relink_is_mergable(struct extent_buffer
*leaf
,
2190 struct btrfs_file_extent_item
*fi
,
2193 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2196 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2199 if (btrfs_file_extent_compression(leaf
, fi
) ||
2200 btrfs_file_extent_encryption(leaf
, fi
) ||
2201 btrfs_file_extent_other_encoding(leaf
, fi
))
2208 * Note the backref might has changed, and in this case we just return 0.
2210 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2211 struct sa_defrag_extent_backref
*prev
,
2212 struct sa_defrag_extent_backref
*backref
)
2214 struct btrfs_file_extent_item
*extent
;
2215 struct btrfs_file_extent_item
*item
;
2216 struct btrfs_ordered_extent
*ordered
;
2217 struct btrfs_trans_handle
*trans
;
2218 struct btrfs_fs_info
*fs_info
;
2219 struct btrfs_root
*root
;
2220 struct btrfs_key key
;
2221 struct extent_buffer
*leaf
;
2222 struct old_sa_defrag_extent
*old
= backref
->old
;
2223 struct new_sa_defrag_extent
*new = old
->new;
2224 struct inode
*src_inode
= new->inode
;
2225 struct inode
*inode
;
2226 struct extent_state
*cached
= NULL
;
2235 if (prev
&& prev
->root_id
== backref
->root_id
&&
2236 prev
->inum
== backref
->inum
&&
2237 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2240 /* step 1: get root */
2241 key
.objectid
= backref
->root_id
;
2242 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2243 key
.offset
= (u64
)-1;
2245 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2246 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2248 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2250 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2251 if (PTR_ERR(root
) == -ENOENT
)
2253 return PTR_ERR(root
);
2255 if (btrfs_root_refs(&root
->root_item
) == 0) {
2256 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2257 /* parse ENOENT to 0 */
2261 /* step 2: get inode */
2262 key
.objectid
= backref
->inum
;
2263 key
.type
= BTRFS_INODE_ITEM_KEY
;
2266 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2267 if (IS_ERR(inode
)) {
2268 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2272 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2274 /* step 3: relink backref */
2275 lock_start
= backref
->file_pos
;
2276 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2277 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2280 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2282 btrfs_put_ordered_extent(ordered
);
2286 trans
= btrfs_join_transaction(root
);
2287 if (IS_ERR(trans
)) {
2288 ret
= PTR_ERR(trans
);
2292 key
.objectid
= backref
->inum
;
2293 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2294 key
.offset
= backref
->file_pos
;
2296 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2299 } else if (ret
> 0) {
2304 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2305 struct btrfs_file_extent_item
);
2307 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2308 backref
->generation
)
2311 btrfs_release_path(path
);
2313 start
= backref
->file_pos
;
2314 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2315 start
+= old
->extent_offset
+ old
->offset
-
2316 backref
->extent_offset
;
2318 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2319 old
->extent_offset
+ old
->offset
+ old
->len
);
2320 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2322 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2327 key
.objectid
= btrfs_ino(inode
);
2328 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2331 path
->leave_spinning
= 1;
2333 struct btrfs_file_extent_item
*fi
;
2335 struct btrfs_key found_key
;
2337 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2342 leaf
= path
->nodes
[0];
2343 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2345 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2346 struct btrfs_file_extent_item
);
2347 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2349 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2350 extent_len
+ found_key
.offset
== start
) {
2351 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2353 btrfs_mark_buffer_dirty(leaf
);
2354 inode_add_bytes(inode
, len
);
2360 btrfs_release_path(path
);
2365 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2368 btrfs_abort_transaction(trans
, root
, ret
);
2372 leaf
= path
->nodes
[0];
2373 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2374 struct btrfs_file_extent_item
);
2375 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2376 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2377 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2378 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2379 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2380 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2381 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2382 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2383 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2384 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2386 btrfs_mark_buffer_dirty(leaf
);
2387 inode_add_bytes(inode
, len
);
2388 btrfs_release_path(path
);
2390 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2392 backref
->root_id
, backref
->inum
,
2393 new->file_pos
, 0); /* start - extent_offset */
2395 btrfs_abort_transaction(trans
, root
, ret
);
2401 btrfs_release_path(path
);
2402 path
->leave_spinning
= 0;
2403 btrfs_end_transaction(trans
, root
);
2405 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2411 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2413 struct btrfs_path
*path
;
2414 struct old_sa_defrag_extent
*old
, *tmp
;
2415 struct sa_defrag_extent_backref
*backref
;
2416 struct sa_defrag_extent_backref
*prev
= NULL
;
2417 struct inode
*inode
;
2418 struct btrfs_root
*root
;
2419 struct rb_node
*node
;
2423 root
= BTRFS_I(inode
)->root
;
2425 path
= btrfs_alloc_path();
2429 if (!record_extent_backrefs(path
, new)) {
2430 btrfs_free_path(path
);
2433 btrfs_release_path(path
);
2436 node
= rb_first(&new->root
);
2439 rb_erase(node
, &new->root
);
2441 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2443 ret
= relink_extent_backref(path
, prev
, backref
);
2456 btrfs_free_path(path
);
2458 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2459 list_del(&old
->list
);
2463 atomic_dec(&root
->fs_info
->defrag_running
);
2464 wake_up(&root
->fs_info
->transaction_wait
);
2469 static struct new_sa_defrag_extent
*
2470 record_old_file_extents(struct inode
*inode
,
2471 struct btrfs_ordered_extent
*ordered
)
2473 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2474 struct btrfs_path
*path
;
2475 struct btrfs_key key
;
2476 struct old_sa_defrag_extent
*old
, *tmp
;
2477 struct new_sa_defrag_extent
*new;
2480 new = kmalloc(sizeof(*new), GFP_NOFS
);
2485 new->file_pos
= ordered
->file_offset
;
2486 new->len
= ordered
->len
;
2487 new->bytenr
= ordered
->start
;
2488 new->disk_len
= ordered
->disk_len
;
2489 new->compress_type
= ordered
->compress_type
;
2490 new->root
= RB_ROOT
;
2491 INIT_LIST_HEAD(&new->head
);
2493 path
= btrfs_alloc_path();
2497 key
.objectid
= btrfs_ino(inode
);
2498 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2499 key
.offset
= new->file_pos
;
2501 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2504 if (ret
> 0 && path
->slots
[0] > 0)
2507 /* find out all the old extents for the file range */
2509 struct btrfs_file_extent_item
*extent
;
2510 struct extent_buffer
*l
;
2519 slot
= path
->slots
[0];
2521 if (slot
>= btrfs_header_nritems(l
)) {
2522 ret
= btrfs_next_leaf(root
, path
);
2530 btrfs_item_key_to_cpu(l
, &key
, slot
);
2532 if (key
.objectid
!= btrfs_ino(inode
))
2534 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2536 if (key
.offset
>= new->file_pos
+ new->len
)
2539 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2541 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2542 if (key
.offset
+ num_bytes
< new->file_pos
)
2545 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2549 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2551 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2555 offset
= max(new->file_pos
, key
.offset
);
2556 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2558 old
->bytenr
= disk_bytenr
;
2559 old
->extent_offset
= extent_offset
;
2560 old
->offset
= offset
- key
.offset
;
2561 old
->len
= end
- offset
;
2564 list_add_tail(&old
->list
, &new->head
);
2570 btrfs_free_path(path
);
2571 atomic_inc(&root
->fs_info
->defrag_running
);
2576 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2577 list_del(&old
->list
);
2581 btrfs_free_path(path
);
2588 * helper function for btrfs_finish_ordered_io, this
2589 * just reads in some of the csum leaves to prime them into ram
2590 * before we start the transaction. It limits the amount of btree
2591 * reads required while inside the transaction.
2593 /* as ordered data IO finishes, this gets called so we can finish
2594 * an ordered extent if the range of bytes in the file it covers are
2597 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2599 struct inode
*inode
= ordered_extent
->inode
;
2600 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2601 struct btrfs_trans_handle
*trans
= NULL
;
2602 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2603 struct extent_state
*cached_state
= NULL
;
2604 struct new_sa_defrag_extent
*new = NULL
;
2605 int compress_type
= 0;
2609 nolock
= btrfs_is_free_space_inode(inode
);
2611 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2616 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2617 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2618 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2620 trans
= btrfs_join_transaction_nolock(root
);
2622 trans
= btrfs_join_transaction(root
);
2623 if (IS_ERR(trans
)) {
2624 ret
= PTR_ERR(trans
);
2628 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2629 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2630 if (ret
) /* -ENOMEM or corruption */
2631 btrfs_abort_transaction(trans
, root
, ret
);
2635 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2636 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2639 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2640 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2641 EXTENT_DEFRAG
, 1, cached_state
);
2643 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2644 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2645 /* the inode is shared */
2646 new = record_old_file_extents(inode
, ordered_extent
);
2648 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2649 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2650 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2654 trans
= btrfs_join_transaction_nolock(root
);
2656 trans
= btrfs_join_transaction(root
);
2657 if (IS_ERR(trans
)) {
2658 ret
= PTR_ERR(trans
);
2662 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2664 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2665 compress_type
= ordered_extent
->compress_type
;
2666 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2667 BUG_ON(compress_type
);
2668 ret
= btrfs_mark_extent_written(trans
, inode
,
2669 ordered_extent
->file_offset
,
2670 ordered_extent
->file_offset
+
2671 ordered_extent
->len
);
2673 BUG_ON(root
== root
->fs_info
->tree_root
);
2674 ret
= insert_reserved_file_extent(trans
, inode
,
2675 ordered_extent
->file_offset
,
2676 ordered_extent
->start
,
2677 ordered_extent
->disk_len
,
2678 ordered_extent
->len
,
2679 ordered_extent
->len
,
2680 compress_type
, 0, 0,
2681 BTRFS_FILE_EXTENT_REG
);
2683 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2684 ordered_extent
->file_offset
, ordered_extent
->len
,
2687 btrfs_abort_transaction(trans
, root
, ret
);
2691 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2692 &ordered_extent
->list
);
2694 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2695 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2696 if (ret
) { /* -ENOMEM or corruption */
2697 btrfs_abort_transaction(trans
, root
, ret
);
2702 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2703 ordered_extent
->file_offset
+
2704 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2706 if (root
!= root
->fs_info
->tree_root
)
2707 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2709 btrfs_end_transaction(trans
, root
);
2712 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2713 ordered_extent
->file_offset
+
2714 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2717 * If the ordered extent had an IOERR or something else went
2718 * wrong we need to return the space for this ordered extent
2719 * back to the allocator.
2721 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2722 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2723 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2724 ordered_extent
->disk_len
);
2729 * This needs to be done to make sure anybody waiting knows we are done
2730 * updating everything for this ordered extent.
2732 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2734 /* for snapshot-aware defrag */
2736 relink_file_extents(new);
2739 btrfs_put_ordered_extent(ordered_extent
);
2740 /* once for the tree */
2741 btrfs_put_ordered_extent(ordered_extent
);
2746 static void finish_ordered_fn(struct btrfs_work
*work
)
2748 struct btrfs_ordered_extent
*ordered_extent
;
2749 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2750 btrfs_finish_ordered_io(ordered_extent
);
2753 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2754 struct extent_state
*state
, int uptodate
)
2756 struct inode
*inode
= page
->mapping
->host
;
2757 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2758 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2759 struct btrfs_workers
*workers
;
2761 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2763 ClearPagePrivate2(page
);
2764 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2765 end
- start
+ 1, uptodate
))
2768 ordered_extent
->work
.func
= finish_ordered_fn
;
2769 ordered_extent
->work
.flags
= 0;
2771 if (btrfs_is_free_space_inode(inode
))
2772 workers
= &root
->fs_info
->endio_freespace_worker
;
2774 workers
= &root
->fs_info
->endio_write_workers
;
2775 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2781 * when reads are done, we need to check csums to verify the data is correct
2782 * if there's a match, we allow the bio to finish. If not, the code in
2783 * extent_io.c will try to find good copies for us.
2785 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2786 struct extent_state
*state
, int mirror
)
2788 size_t offset
= start
- page_offset(page
);
2789 struct inode
*inode
= page
->mapping
->host
;
2790 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2792 u64
private = ~(u32
)0;
2794 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2796 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2797 DEFAULT_RATELIMIT_BURST
);
2799 if (PageChecked(page
)) {
2800 ClearPageChecked(page
);
2804 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2807 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2808 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2809 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2814 if (state
&& state
->start
== start
) {
2815 private = state
->private;
2818 ret
= get_state_private(io_tree
, start
, &private);
2820 kaddr
= kmap_atomic(page
);
2824 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2825 btrfs_csum_final(csum
, (char *)&csum
);
2826 if (csum
!= private)
2829 kunmap_atomic(kaddr
);
2834 if (__ratelimit(&_rs
))
2835 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2836 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2837 (unsigned long long)start
, csum
,
2838 (unsigned long long)private);
2839 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2840 flush_dcache_page(page
);
2841 kunmap_atomic(kaddr
);
2847 struct delayed_iput
{
2848 struct list_head list
;
2849 struct inode
*inode
;
2852 /* JDM: If this is fs-wide, why can't we add a pointer to
2853 * btrfs_inode instead and avoid the allocation? */
2854 void btrfs_add_delayed_iput(struct inode
*inode
)
2856 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2857 struct delayed_iput
*delayed
;
2859 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2862 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2863 delayed
->inode
= inode
;
2865 spin_lock(&fs_info
->delayed_iput_lock
);
2866 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2867 spin_unlock(&fs_info
->delayed_iput_lock
);
2870 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2873 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2874 struct delayed_iput
*delayed
;
2877 spin_lock(&fs_info
->delayed_iput_lock
);
2878 empty
= list_empty(&fs_info
->delayed_iputs
);
2879 spin_unlock(&fs_info
->delayed_iput_lock
);
2883 spin_lock(&fs_info
->delayed_iput_lock
);
2884 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2885 spin_unlock(&fs_info
->delayed_iput_lock
);
2887 while (!list_empty(&list
)) {
2888 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2889 list_del(&delayed
->list
);
2890 iput(delayed
->inode
);
2896 * This is called in transaction commit time. If there are no orphan
2897 * files in the subvolume, it removes orphan item and frees block_rsv
2900 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2901 struct btrfs_root
*root
)
2903 struct btrfs_block_rsv
*block_rsv
;
2906 if (atomic_read(&root
->orphan_inodes
) ||
2907 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2910 spin_lock(&root
->orphan_lock
);
2911 if (atomic_read(&root
->orphan_inodes
)) {
2912 spin_unlock(&root
->orphan_lock
);
2916 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2917 spin_unlock(&root
->orphan_lock
);
2921 block_rsv
= root
->orphan_block_rsv
;
2922 root
->orphan_block_rsv
= NULL
;
2923 spin_unlock(&root
->orphan_lock
);
2925 if (root
->orphan_item_inserted
&&
2926 btrfs_root_refs(&root
->root_item
) > 0) {
2927 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2928 root
->root_key
.objectid
);
2930 root
->orphan_item_inserted
= 0;
2934 WARN_ON(block_rsv
->size
> 0);
2935 btrfs_free_block_rsv(root
, block_rsv
);
2940 * This creates an orphan entry for the given inode in case something goes
2941 * wrong in the middle of an unlink/truncate.
2943 * NOTE: caller of this function should reserve 5 units of metadata for
2946 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2948 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2949 struct btrfs_block_rsv
*block_rsv
= NULL
;
2954 if (!root
->orphan_block_rsv
) {
2955 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2960 spin_lock(&root
->orphan_lock
);
2961 if (!root
->orphan_block_rsv
) {
2962 root
->orphan_block_rsv
= block_rsv
;
2963 } else if (block_rsv
) {
2964 btrfs_free_block_rsv(root
, block_rsv
);
2968 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2969 &BTRFS_I(inode
)->runtime_flags
)) {
2972 * For proper ENOSPC handling, we should do orphan
2973 * cleanup when mounting. But this introduces backward
2974 * compatibility issue.
2976 if (!xchg(&root
->orphan_item_inserted
, 1))
2982 atomic_inc(&root
->orphan_inodes
);
2985 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2986 &BTRFS_I(inode
)->runtime_flags
))
2988 spin_unlock(&root
->orphan_lock
);
2990 /* grab metadata reservation from transaction handle */
2992 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2993 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2996 /* insert an orphan item to track this unlinked/truncated file */
2998 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2999 if (ret
&& ret
!= -EEXIST
) {
3000 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3001 &BTRFS_I(inode
)->runtime_flags
);
3002 btrfs_abort_transaction(trans
, root
, ret
);
3008 /* insert an orphan item to track subvolume contains orphan files */
3010 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3011 root
->root_key
.objectid
);
3012 if (ret
&& ret
!= -EEXIST
) {
3013 btrfs_abort_transaction(trans
, root
, ret
);
3021 * We have done the truncate/delete so we can go ahead and remove the orphan
3022 * item for this particular inode.
3024 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3026 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3027 int delete_item
= 0;
3028 int release_rsv
= 0;
3031 spin_lock(&root
->orphan_lock
);
3032 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3033 &BTRFS_I(inode
)->runtime_flags
))
3036 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3037 &BTRFS_I(inode
)->runtime_flags
))
3039 spin_unlock(&root
->orphan_lock
);
3041 if (trans
&& delete_item
) {
3042 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3043 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3047 btrfs_orphan_release_metadata(inode
);
3048 atomic_dec(&root
->orphan_inodes
);
3055 * this cleans up any orphans that may be left on the list from the last use
3058 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3060 struct btrfs_path
*path
;
3061 struct extent_buffer
*leaf
;
3062 struct btrfs_key key
, found_key
;
3063 struct btrfs_trans_handle
*trans
;
3064 struct inode
*inode
;
3065 u64 last_objectid
= 0;
3066 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3068 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3071 path
= btrfs_alloc_path();
3078 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3079 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3080 key
.offset
= (u64
)-1;
3083 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3088 * if ret == 0 means we found what we were searching for, which
3089 * is weird, but possible, so only screw with path if we didn't
3090 * find the key and see if we have stuff that matches
3094 if (path
->slots
[0] == 0)
3099 /* pull out the item */
3100 leaf
= path
->nodes
[0];
3101 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3103 /* make sure the item matches what we want */
3104 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3106 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3109 /* release the path since we're done with it */
3110 btrfs_release_path(path
);
3113 * this is where we are basically btrfs_lookup, without the
3114 * crossing root thing. we store the inode number in the
3115 * offset of the orphan item.
3118 if (found_key
.offset
== last_objectid
) {
3119 btrfs_err(root
->fs_info
,
3120 "Error removing orphan entry, stopping orphan cleanup");
3125 last_objectid
= found_key
.offset
;
3127 found_key
.objectid
= found_key
.offset
;
3128 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3129 found_key
.offset
= 0;
3130 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3131 ret
= PTR_RET(inode
);
3132 if (ret
&& ret
!= -ESTALE
)
3135 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3136 struct btrfs_root
*dead_root
;
3137 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3138 int is_dead_root
= 0;
3141 * this is an orphan in the tree root. Currently these
3142 * could come from 2 sources:
3143 * a) a snapshot deletion in progress
3144 * b) a free space cache inode
3145 * We need to distinguish those two, as the snapshot
3146 * orphan must not get deleted.
3147 * find_dead_roots already ran before us, so if this
3148 * is a snapshot deletion, we should find the root
3149 * in the dead_roots list
3151 spin_lock(&fs_info
->trans_lock
);
3152 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3154 if (dead_root
->root_key
.objectid
==
3155 found_key
.objectid
) {
3160 spin_unlock(&fs_info
->trans_lock
);
3162 /* prevent this orphan from being found again */
3163 key
.offset
= found_key
.objectid
- 1;
3168 * Inode is already gone but the orphan item is still there,
3169 * kill the orphan item.
3171 if (ret
== -ESTALE
) {
3172 trans
= btrfs_start_transaction(root
, 1);
3173 if (IS_ERR(trans
)) {
3174 ret
= PTR_ERR(trans
);
3177 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3178 found_key
.objectid
);
3179 ret
= btrfs_del_orphan_item(trans
, root
,
3180 found_key
.objectid
);
3181 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3182 btrfs_end_transaction(trans
, root
);
3187 * add this inode to the orphan list so btrfs_orphan_del does
3188 * the proper thing when we hit it
3190 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3191 &BTRFS_I(inode
)->runtime_flags
);
3192 atomic_inc(&root
->orphan_inodes
);
3194 /* if we have links, this was a truncate, lets do that */
3195 if (inode
->i_nlink
) {
3196 if (!S_ISREG(inode
->i_mode
)) {
3203 /* 1 for the orphan item deletion. */
3204 trans
= btrfs_start_transaction(root
, 1);
3205 if (IS_ERR(trans
)) {
3206 ret
= PTR_ERR(trans
);
3209 ret
= btrfs_orphan_add(trans
, inode
);
3210 btrfs_end_transaction(trans
, root
);
3214 ret
= btrfs_truncate(inode
);
3216 btrfs_orphan_del(NULL
, inode
);
3221 /* this will do delete_inode and everything for us */
3226 /* release the path since we're done with it */
3227 btrfs_release_path(path
);
3229 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3231 if (root
->orphan_block_rsv
)
3232 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3235 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3236 trans
= btrfs_join_transaction(root
);
3238 btrfs_end_transaction(trans
, root
);
3242 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3244 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3248 btrfs_crit(root
->fs_info
,
3249 "could not do orphan cleanup %d", ret
);
3250 btrfs_free_path(path
);
3255 * very simple check to peek ahead in the leaf looking for xattrs. If we
3256 * don't find any xattrs, we know there can't be any acls.
3258 * slot is the slot the inode is in, objectid is the objectid of the inode
3260 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3261 int slot
, u64 objectid
)
3263 u32 nritems
= btrfs_header_nritems(leaf
);
3264 struct btrfs_key found_key
;
3268 while (slot
< nritems
) {
3269 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3271 /* we found a different objectid, there must not be acls */
3272 if (found_key
.objectid
!= objectid
)
3275 /* we found an xattr, assume we've got an acl */
3276 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
3280 * we found a key greater than an xattr key, there can't
3281 * be any acls later on
3283 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3290 * it goes inode, inode backrefs, xattrs, extents,
3291 * so if there are a ton of hard links to an inode there can
3292 * be a lot of backrefs. Don't waste time searching too hard,
3293 * this is just an optimization
3298 /* we hit the end of the leaf before we found an xattr or
3299 * something larger than an xattr. We have to assume the inode
3306 * read an inode from the btree into the in-memory inode
3308 static void btrfs_read_locked_inode(struct inode
*inode
)
3310 struct btrfs_path
*path
;
3311 struct extent_buffer
*leaf
;
3312 struct btrfs_inode_item
*inode_item
;
3313 struct btrfs_timespec
*tspec
;
3314 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3315 struct btrfs_key location
;
3319 bool filled
= false;
3321 ret
= btrfs_fill_inode(inode
, &rdev
);
3325 path
= btrfs_alloc_path();
3329 path
->leave_spinning
= 1;
3330 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3332 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3336 leaf
= path
->nodes
[0];
3341 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3342 struct btrfs_inode_item
);
3343 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3344 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3345 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3346 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3347 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3349 tspec
= btrfs_inode_atime(inode_item
);
3350 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3351 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3353 tspec
= btrfs_inode_mtime(inode_item
);
3354 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3355 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3357 tspec
= btrfs_inode_ctime(inode_item
);
3358 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3359 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3361 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3362 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3363 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3366 * If we were modified in the current generation and evicted from memory
3367 * and then re-read we need to do a full sync since we don't have any
3368 * idea about which extents were modified before we were evicted from
3371 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3372 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3373 &BTRFS_I(inode
)->runtime_flags
);
3375 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3376 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3378 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3380 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3381 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3384 * try to precache a NULL acl entry for files that don't have
3385 * any xattrs or acls
3387 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3390 cache_no_acl(inode
);
3392 btrfs_free_path(path
);
3394 switch (inode
->i_mode
& S_IFMT
) {
3396 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3397 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3398 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3399 inode
->i_fop
= &btrfs_file_operations
;
3400 inode
->i_op
= &btrfs_file_inode_operations
;
3403 inode
->i_fop
= &btrfs_dir_file_operations
;
3404 if (root
== root
->fs_info
->tree_root
)
3405 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3407 inode
->i_op
= &btrfs_dir_inode_operations
;
3410 inode
->i_op
= &btrfs_symlink_inode_operations
;
3411 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3412 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3415 inode
->i_op
= &btrfs_special_inode_operations
;
3416 init_special_inode(inode
, inode
->i_mode
, rdev
);
3420 btrfs_update_iflags(inode
);
3424 btrfs_free_path(path
);
3425 make_bad_inode(inode
);
3429 * given a leaf and an inode, copy the inode fields into the leaf
3431 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3432 struct extent_buffer
*leaf
,
3433 struct btrfs_inode_item
*item
,
3434 struct inode
*inode
)
3436 struct btrfs_map_token token
;
3438 btrfs_init_map_token(&token
);
3440 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3441 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3442 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3444 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3445 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3447 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3448 inode
->i_atime
.tv_sec
, &token
);
3449 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3450 inode
->i_atime
.tv_nsec
, &token
);
3452 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3453 inode
->i_mtime
.tv_sec
, &token
);
3454 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3455 inode
->i_mtime
.tv_nsec
, &token
);
3457 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3458 inode
->i_ctime
.tv_sec
, &token
);
3459 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3460 inode
->i_ctime
.tv_nsec
, &token
);
3462 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3464 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3466 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3467 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3468 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3469 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3470 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3474 * copy everything in the in-memory inode into the btree.
3476 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3477 struct btrfs_root
*root
, struct inode
*inode
)
3479 struct btrfs_inode_item
*inode_item
;
3480 struct btrfs_path
*path
;
3481 struct extent_buffer
*leaf
;
3484 path
= btrfs_alloc_path();
3488 path
->leave_spinning
= 1;
3489 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3497 btrfs_unlock_up_safe(path
, 1);
3498 leaf
= path
->nodes
[0];
3499 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3500 struct btrfs_inode_item
);
3502 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3503 btrfs_mark_buffer_dirty(leaf
);
3504 btrfs_set_inode_last_trans(trans
, inode
);
3507 btrfs_free_path(path
);
3512 * copy everything in the in-memory inode into the btree.
3514 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3515 struct btrfs_root
*root
, struct inode
*inode
)
3520 * If the inode is a free space inode, we can deadlock during commit
3521 * if we put it into the delayed code.
3523 * The data relocation inode should also be directly updated
3526 if (!btrfs_is_free_space_inode(inode
)
3527 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3528 btrfs_update_root_times(trans
, root
);
3530 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3532 btrfs_set_inode_last_trans(trans
, inode
);
3536 return btrfs_update_inode_item(trans
, root
, inode
);
3539 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3540 struct btrfs_root
*root
,
3541 struct inode
*inode
)
3545 ret
= btrfs_update_inode(trans
, root
, inode
);
3547 return btrfs_update_inode_item(trans
, root
, inode
);
3552 * unlink helper that gets used here in inode.c and in the tree logging
3553 * recovery code. It remove a link in a directory with a given name, and
3554 * also drops the back refs in the inode to the directory
3556 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3557 struct btrfs_root
*root
,
3558 struct inode
*dir
, struct inode
*inode
,
3559 const char *name
, int name_len
)
3561 struct btrfs_path
*path
;
3563 struct extent_buffer
*leaf
;
3564 struct btrfs_dir_item
*di
;
3565 struct btrfs_key key
;
3567 u64 ino
= btrfs_ino(inode
);
3568 u64 dir_ino
= btrfs_ino(dir
);
3570 path
= btrfs_alloc_path();
3576 path
->leave_spinning
= 1;
3577 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3578 name
, name_len
, -1);
3587 leaf
= path
->nodes
[0];
3588 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3589 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3592 btrfs_release_path(path
);
3594 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3597 btrfs_info(root
->fs_info
,
3598 "failed to delete reference to %.*s, inode %llu parent %llu",
3600 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3601 btrfs_abort_transaction(trans
, root
, ret
);
3605 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3607 btrfs_abort_transaction(trans
, root
, ret
);
3611 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3613 if (ret
!= 0 && ret
!= -ENOENT
) {
3614 btrfs_abort_transaction(trans
, root
, ret
);
3618 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3623 btrfs_free_path(path
);
3627 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3628 inode_inc_iversion(inode
);
3629 inode_inc_iversion(dir
);
3630 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3631 ret
= btrfs_update_inode(trans
, root
, dir
);
3636 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3637 struct btrfs_root
*root
,
3638 struct inode
*dir
, struct inode
*inode
,
3639 const char *name
, int name_len
)
3642 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3644 btrfs_drop_nlink(inode
);
3645 ret
= btrfs_update_inode(trans
, root
, inode
);
3651 /* helper to check if there is any shared block in the path */
3652 static int check_path_shared(struct btrfs_root
*root
,
3653 struct btrfs_path
*path
)
3655 struct extent_buffer
*eb
;
3659 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
3662 if (!path
->nodes
[level
])
3664 eb
= path
->nodes
[level
];
3665 if (!btrfs_block_can_be_shared(root
, eb
))
3667 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, level
, 1,
3676 * helper to start transaction for unlink and rmdir.
3678 * unlink and rmdir are special in btrfs, they do not always free space.
3679 * so in enospc case, we should make sure they will free space before
3680 * allowing them to use the global metadata reservation.
3682 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3683 struct dentry
*dentry
)
3685 struct btrfs_trans_handle
*trans
;
3686 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3687 struct btrfs_path
*path
;
3688 struct btrfs_dir_item
*di
;
3689 struct inode
*inode
= dentry
->d_inode
;
3694 u64 ino
= btrfs_ino(inode
);
3695 u64 dir_ino
= btrfs_ino(dir
);
3698 * 1 for the possible orphan item
3699 * 1 for the dir item
3700 * 1 for the dir index
3701 * 1 for the inode ref
3704 trans
= btrfs_start_transaction(root
, 5);
3705 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3708 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3709 return ERR_PTR(-ENOSPC
);
3711 /* check if there is someone else holds reference */
3712 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3713 return ERR_PTR(-ENOSPC
);
3715 if (atomic_read(&inode
->i_count
) > 2)
3716 return ERR_PTR(-ENOSPC
);
3718 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3719 return ERR_PTR(-ENOSPC
);
3721 path
= btrfs_alloc_path();
3723 root
->fs_info
->enospc_unlink
= 0;
3724 return ERR_PTR(-ENOMEM
);
3727 /* 1 for the orphan item */
3728 trans
= btrfs_start_transaction(root
, 1);
3729 if (IS_ERR(trans
)) {
3730 btrfs_free_path(path
);
3731 root
->fs_info
->enospc_unlink
= 0;
3735 path
->skip_locking
= 1;
3736 path
->search_commit_root
= 1;
3738 ret
= btrfs_lookup_inode(trans
, root
, path
,
3739 &BTRFS_I(dir
)->location
, 0);
3745 if (check_path_shared(root
, path
))
3750 btrfs_release_path(path
);
3752 ret
= btrfs_lookup_inode(trans
, root
, path
,
3753 &BTRFS_I(inode
)->location
, 0);
3759 if (check_path_shared(root
, path
))
3764 btrfs_release_path(path
);
3766 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3767 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3773 BUG_ON(ret
== 0); /* Corruption */
3774 if (check_path_shared(root
, path
))
3776 btrfs_release_path(path
);
3784 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3785 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3791 if (check_path_shared(root
, path
))
3797 btrfs_release_path(path
);
3799 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3800 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3807 if (check_path_shared(root
, path
))
3810 btrfs_release_path(path
);
3813 * This is a commit root search, if we can lookup inode item and other
3814 * relative items in the commit root, it means the transaction of
3815 * dir/file creation has been committed, and the dir index item that we
3816 * delay to insert has also been inserted into the commit root. So
3817 * we needn't worry about the delayed insertion of the dir index item
3820 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3821 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3826 BUG_ON(ret
== -ENOENT
);
3827 if (check_path_shared(root
, path
))
3832 btrfs_free_path(path
);
3833 /* Migrate the orphan reservation over */
3835 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3836 &root
->fs_info
->global_block_rsv
,
3837 trans
->bytes_reserved
);
3840 btrfs_end_transaction(trans
, root
);
3841 root
->fs_info
->enospc_unlink
= 0;
3842 return ERR_PTR(err
);
3845 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3849 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3850 struct btrfs_root
*root
)
3852 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3853 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3854 trans
->bytes_reserved
);
3855 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3856 BUG_ON(!root
->fs_info
->enospc_unlink
);
3857 root
->fs_info
->enospc_unlink
= 0;
3859 btrfs_end_transaction(trans
, root
);
3862 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3864 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3865 struct btrfs_trans_handle
*trans
;
3866 struct inode
*inode
= dentry
->d_inode
;
3869 trans
= __unlink_start_trans(dir
, dentry
);
3871 return PTR_ERR(trans
);
3873 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3875 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3876 dentry
->d_name
.name
, dentry
->d_name
.len
);
3880 if (inode
->i_nlink
== 0) {
3881 ret
= btrfs_orphan_add(trans
, inode
);
3887 __unlink_end_trans(trans
, root
);
3888 btrfs_btree_balance_dirty(root
);
3892 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3893 struct btrfs_root
*root
,
3894 struct inode
*dir
, u64 objectid
,
3895 const char *name
, int name_len
)
3897 struct btrfs_path
*path
;
3898 struct extent_buffer
*leaf
;
3899 struct btrfs_dir_item
*di
;
3900 struct btrfs_key key
;
3903 u64 dir_ino
= btrfs_ino(dir
);
3905 path
= btrfs_alloc_path();
3909 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3910 name
, name_len
, -1);
3911 if (IS_ERR_OR_NULL(di
)) {
3919 leaf
= path
->nodes
[0];
3920 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3921 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3922 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3924 btrfs_abort_transaction(trans
, root
, ret
);
3927 btrfs_release_path(path
);
3929 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3930 objectid
, root
->root_key
.objectid
,
3931 dir_ino
, &index
, name
, name_len
);
3933 if (ret
!= -ENOENT
) {
3934 btrfs_abort_transaction(trans
, root
, ret
);
3937 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3939 if (IS_ERR_OR_NULL(di
)) {
3944 btrfs_abort_transaction(trans
, root
, ret
);
3948 leaf
= path
->nodes
[0];
3949 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3950 btrfs_release_path(path
);
3953 btrfs_release_path(path
);
3955 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3957 btrfs_abort_transaction(trans
, root
, ret
);
3961 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3962 inode_inc_iversion(dir
);
3963 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3964 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3966 btrfs_abort_transaction(trans
, root
, ret
);
3968 btrfs_free_path(path
);
3972 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3974 struct inode
*inode
= dentry
->d_inode
;
3976 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3977 struct btrfs_trans_handle
*trans
;
3979 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3981 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3984 trans
= __unlink_start_trans(dir
, dentry
);
3986 return PTR_ERR(trans
);
3988 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3989 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3990 BTRFS_I(inode
)->location
.objectid
,
3991 dentry
->d_name
.name
,
3992 dentry
->d_name
.len
);
3996 err
= btrfs_orphan_add(trans
, inode
);
4000 /* now the directory is empty */
4001 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4002 dentry
->d_name
.name
, dentry
->d_name
.len
);
4004 btrfs_i_size_write(inode
, 0);
4006 __unlink_end_trans(trans
, root
);
4007 btrfs_btree_balance_dirty(root
);
4013 * this can truncate away extent items, csum items and directory items.
4014 * It starts at a high offset and removes keys until it can't find
4015 * any higher than new_size
4017 * csum items that cross the new i_size are truncated to the new size
4020 * min_type is the minimum key type to truncate down to. If set to 0, this
4021 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4023 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4024 struct btrfs_root
*root
,
4025 struct inode
*inode
,
4026 u64 new_size
, u32 min_type
)
4028 struct btrfs_path
*path
;
4029 struct extent_buffer
*leaf
;
4030 struct btrfs_file_extent_item
*fi
;
4031 struct btrfs_key key
;
4032 struct btrfs_key found_key
;
4033 u64 extent_start
= 0;
4034 u64 extent_num_bytes
= 0;
4035 u64 extent_offset
= 0;
4037 u32 found_type
= (u8
)-1;
4040 int pending_del_nr
= 0;
4041 int pending_del_slot
= 0;
4042 int extent_type
= -1;
4045 u64 ino
= btrfs_ino(inode
);
4047 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4049 path
= btrfs_alloc_path();
4055 * We want to drop from the next block forward in case this new size is
4056 * not block aligned since we will be keeping the last block of the
4057 * extent just the way it is.
4059 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
4060 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4061 root
->sectorsize
), (u64
)-1, 0);
4064 * This function is also used to drop the items in the log tree before
4065 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4066 * it is used to drop the loged items. So we shouldn't kill the delayed
4069 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4070 btrfs_kill_delayed_inode_items(inode
);
4073 key
.offset
= (u64
)-1;
4077 path
->leave_spinning
= 1;
4078 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4085 /* there are no items in the tree for us to truncate, we're
4088 if (path
->slots
[0] == 0)
4095 leaf
= path
->nodes
[0];
4096 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4097 found_type
= btrfs_key_type(&found_key
);
4099 if (found_key
.objectid
!= ino
)
4102 if (found_type
< min_type
)
4105 item_end
= found_key
.offset
;
4106 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4107 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4108 struct btrfs_file_extent_item
);
4109 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4110 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4112 btrfs_file_extent_num_bytes(leaf
, fi
);
4113 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4114 item_end
+= btrfs_file_extent_inline_len(leaf
,
4119 if (found_type
> min_type
) {
4122 if (item_end
< new_size
)
4124 if (found_key
.offset
>= new_size
)
4130 /* FIXME, shrink the extent if the ref count is only 1 */
4131 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4134 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4136 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4138 u64 orig_num_bytes
=
4139 btrfs_file_extent_num_bytes(leaf
, fi
);
4140 extent_num_bytes
= ALIGN(new_size
-
4143 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4145 num_dec
= (orig_num_bytes
-
4147 if (root
->ref_cows
&& extent_start
!= 0)
4148 inode_sub_bytes(inode
, num_dec
);
4149 btrfs_mark_buffer_dirty(leaf
);
4152 btrfs_file_extent_disk_num_bytes(leaf
,
4154 extent_offset
= found_key
.offset
-
4155 btrfs_file_extent_offset(leaf
, fi
);
4157 /* FIXME blocksize != 4096 */
4158 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4159 if (extent_start
!= 0) {
4162 inode_sub_bytes(inode
, num_dec
);
4165 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4167 * we can't truncate inline items that have had
4171 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4172 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4173 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4174 u32 size
= new_size
- found_key
.offset
;
4176 if (root
->ref_cows
) {
4177 inode_sub_bytes(inode
, item_end
+ 1 -
4181 btrfs_file_extent_calc_inline_size(size
);
4182 btrfs_truncate_item(trans
, root
, path
,
4184 } else if (root
->ref_cows
) {
4185 inode_sub_bytes(inode
, item_end
+ 1 -
4191 if (!pending_del_nr
) {
4192 /* no pending yet, add ourselves */
4193 pending_del_slot
= path
->slots
[0];
4195 } else if (pending_del_nr
&&
4196 path
->slots
[0] + 1 == pending_del_slot
) {
4197 /* hop on the pending chunk */
4199 pending_del_slot
= path
->slots
[0];
4206 if (found_extent
&& (root
->ref_cows
||
4207 root
== root
->fs_info
->tree_root
)) {
4208 btrfs_set_path_blocking(path
);
4209 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4210 extent_num_bytes
, 0,
4211 btrfs_header_owner(leaf
),
4212 ino
, extent_offset
, 0);
4216 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4219 if (path
->slots
[0] == 0 ||
4220 path
->slots
[0] != pending_del_slot
) {
4221 if (pending_del_nr
) {
4222 ret
= btrfs_del_items(trans
, root
, path
,
4226 btrfs_abort_transaction(trans
,
4232 btrfs_release_path(path
);
4239 if (pending_del_nr
) {
4240 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4243 btrfs_abort_transaction(trans
, root
, ret
);
4246 btrfs_free_path(path
);
4251 * btrfs_truncate_page - read, zero a chunk and write a page
4252 * @inode - inode that we're zeroing
4253 * @from - the offset to start zeroing
4254 * @len - the length to zero, 0 to zero the entire range respective to the
4256 * @front - zero up to the offset instead of from the offset on
4258 * This will find the page for the "from" offset and cow the page and zero the
4259 * part we want to zero. This is used with truncate and hole punching.
4261 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4264 struct address_space
*mapping
= inode
->i_mapping
;
4265 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4266 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4267 struct btrfs_ordered_extent
*ordered
;
4268 struct extent_state
*cached_state
= NULL
;
4270 u32 blocksize
= root
->sectorsize
;
4271 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4272 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4274 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4279 if ((offset
& (blocksize
- 1)) == 0 &&
4280 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4282 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4287 page
= find_or_create_page(mapping
, index
, mask
);
4289 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4294 page_start
= page_offset(page
);
4295 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4297 if (!PageUptodate(page
)) {
4298 ret
= btrfs_readpage(NULL
, page
);
4300 if (page
->mapping
!= mapping
) {
4302 page_cache_release(page
);
4305 if (!PageUptodate(page
)) {
4310 wait_on_page_writeback(page
);
4312 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4313 set_page_extent_mapped(page
);
4315 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4317 unlock_extent_cached(io_tree
, page_start
, page_end
,
4318 &cached_state
, GFP_NOFS
);
4320 page_cache_release(page
);
4321 btrfs_start_ordered_extent(inode
, ordered
, 1);
4322 btrfs_put_ordered_extent(ordered
);
4326 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4327 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4328 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4329 0, 0, &cached_state
, GFP_NOFS
);
4331 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4334 unlock_extent_cached(io_tree
, page_start
, page_end
,
4335 &cached_state
, GFP_NOFS
);
4339 if (offset
!= PAGE_CACHE_SIZE
) {
4341 len
= PAGE_CACHE_SIZE
- offset
;
4344 memset(kaddr
, 0, offset
);
4346 memset(kaddr
+ offset
, 0, len
);
4347 flush_dcache_page(page
);
4350 ClearPageChecked(page
);
4351 set_page_dirty(page
);
4352 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4357 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4359 page_cache_release(page
);
4365 * This function puts in dummy file extents for the area we're creating a hole
4366 * for. So if we are truncating this file to a larger size we need to insert
4367 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4368 * the range between oldsize and size
4370 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4372 struct btrfs_trans_handle
*trans
;
4373 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4374 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4375 struct extent_map
*em
= NULL
;
4376 struct extent_state
*cached_state
= NULL
;
4377 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4378 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4379 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4385 if (size
<= hole_start
)
4389 struct btrfs_ordered_extent
*ordered
;
4390 btrfs_wait_ordered_range(inode
, hole_start
,
4391 block_end
- hole_start
);
4392 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4394 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4397 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4398 &cached_state
, GFP_NOFS
);
4399 btrfs_put_ordered_extent(ordered
);
4402 cur_offset
= hole_start
;
4404 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4405 block_end
- cur_offset
, 0);
4411 last_byte
= min(extent_map_end(em
), block_end
);
4412 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4413 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4414 struct extent_map
*hole_em
;
4415 hole_size
= last_byte
- cur_offset
;
4417 trans
= btrfs_start_transaction(root
, 3);
4418 if (IS_ERR(trans
)) {
4419 err
= PTR_ERR(trans
);
4423 err
= btrfs_drop_extents(trans
, root
, inode
,
4425 cur_offset
+ hole_size
, 1);
4427 btrfs_abort_transaction(trans
, root
, err
);
4428 btrfs_end_transaction(trans
, root
);
4432 err
= btrfs_insert_file_extent(trans
, root
,
4433 btrfs_ino(inode
), cur_offset
, 0,
4434 0, hole_size
, 0, hole_size
,
4437 btrfs_abort_transaction(trans
, root
, err
);
4438 btrfs_end_transaction(trans
, root
);
4442 btrfs_drop_extent_cache(inode
, cur_offset
,
4443 cur_offset
+ hole_size
- 1, 0);
4444 hole_em
= alloc_extent_map();
4446 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4447 &BTRFS_I(inode
)->runtime_flags
);
4450 hole_em
->start
= cur_offset
;
4451 hole_em
->len
= hole_size
;
4452 hole_em
->orig_start
= cur_offset
;
4454 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4455 hole_em
->block_len
= 0;
4456 hole_em
->orig_block_len
= 0;
4457 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4458 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4459 hole_em
->generation
= trans
->transid
;
4462 write_lock(&em_tree
->lock
);
4463 err
= add_extent_mapping(em_tree
, hole_em
);
4465 list_move(&hole_em
->list
,
4466 &em_tree
->modified_extents
);
4467 write_unlock(&em_tree
->lock
);
4470 btrfs_drop_extent_cache(inode
, cur_offset
,
4474 free_extent_map(hole_em
);
4476 btrfs_update_inode(trans
, root
, inode
);
4477 btrfs_end_transaction(trans
, root
);
4479 free_extent_map(em
);
4481 cur_offset
= last_byte
;
4482 if (cur_offset
>= block_end
)
4486 free_extent_map(em
);
4487 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4492 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4494 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4495 struct btrfs_trans_handle
*trans
;
4496 loff_t oldsize
= i_size_read(inode
);
4497 loff_t newsize
= attr
->ia_size
;
4498 int mask
= attr
->ia_valid
;
4501 if (newsize
== oldsize
)
4505 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4506 * special case where we need to update the times despite not having
4507 * these flags set. For all other operations the VFS set these flags
4508 * explicitly if it wants a timestamp update.
4510 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4511 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4513 if (newsize
> oldsize
) {
4514 truncate_pagecache(inode
, oldsize
, newsize
);
4515 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4519 trans
= btrfs_start_transaction(root
, 1);
4521 return PTR_ERR(trans
);
4523 i_size_write(inode
, newsize
);
4524 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4525 ret
= btrfs_update_inode(trans
, root
, inode
);
4526 btrfs_end_transaction(trans
, root
);
4530 * We're truncating a file that used to have good data down to
4531 * zero. Make sure it gets into the ordered flush list so that
4532 * any new writes get down to disk quickly.
4535 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4536 &BTRFS_I(inode
)->runtime_flags
);
4539 * 1 for the orphan item we're going to add
4540 * 1 for the orphan item deletion.
4542 trans
= btrfs_start_transaction(root
, 2);
4544 return PTR_ERR(trans
);
4547 * We need to do this in case we fail at _any_ point during the
4548 * actual truncate. Once we do the truncate_setsize we could
4549 * invalidate pages which forces any outstanding ordered io to
4550 * be instantly completed which will give us extents that need
4551 * to be truncated. If we fail to get an orphan inode down we
4552 * could have left over extents that were never meant to live,
4553 * so we need to garuntee from this point on that everything
4554 * will be consistent.
4556 ret
= btrfs_orphan_add(trans
, inode
);
4557 btrfs_end_transaction(trans
, root
);
4561 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4562 truncate_setsize(inode
, newsize
);
4564 /* Disable nonlocked read DIO to avoid the end less truncate */
4565 btrfs_inode_block_unlocked_dio(inode
);
4566 inode_dio_wait(inode
);
4567 btrfs_inode_resume_unlocked_dio(inode
);
4569 ret
= btrfs_truncate(inode
);
4570 if (ret
&& inode
->i_nlink
)
4571 btrfs_orphan_del(NULL
, inode
);
4577 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4579 struct inode
*inode
= dentry
->d_inode
;
4580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4583 if (btrfs_root_readonly(root
))
4586 err
= inode_change_ok(inode
, attr
);
4590 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4591 err
= btrfs_setsize(inode
, attr
);
4596 if (attr
->ia_valid
) {
4597 setattr_copy(inode
, attr
);
4598 inode_inc_iversion(inode
);
4599 err
= btrfs_dirty_inode(inode
);
4601 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4602 err
= btrfs_acl_chmod(inode
);
4608 void btrfs_evict_inode(struct inode
*inode
)
4610 struct btrfs_trans_handle
*trans
;
4611 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4612 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4613 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4616 trace_btrfs_inode_evict(inode
);
4618 truncate_inode_pages(&inode
->i_data
, 0);
4619 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4620 btrfs_is_free_space_inode(inode
)))
4623 if (is_bad_inode(inode
)) {
4624 btrfs_orphan_del(NULL
, inode
);
4627 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4628 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4630 if (root
->fs_info
->log_root_recovering
) {
4631 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4632 &BTRFS_I(inode
)->runtime_flags
));
4636 if (inode
->i_nlink
> 0) {
4637 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4641 ret
= btrfs_commit_inode_delayed_inode(inode
);
4643 btrfs_orphan_del(NULL
, inode
);
4647 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4649 btrfs_orphan_del(NULL
, inode
);
4652 rsv
->size
= min_size
;
4654 global_rsv
= &root
->fs_info
->global_block_rsv
;
4656 btrfs_i_size_write(inode
, 0);
4659 * This is a bit simpler than btrfs_truncate since we've already
4660 * reserved our space for our orphan item in the unlink, so we just
4661 * need to reserve some slack space in case we add bytes and update
4662 * inode item when doing the truncate.
4665 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4666 BTRFS_RESERVE_FLUSH_LIMIT
);
4669 * Try and steal from the global reserve since we will
4670 * likely not use this space anyway, we want to try as
4671 * hard as possible to get this to work.
4674 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4677 btrfs_warn(root
->fs_info
,
4678 "Could not get space for a delete, will truncate on mount %d",
4680 btrfs_orphan_del(NULL
, inode
);
4681 btrfs_free_block_rsv(root
, rsv
);
4685 trans
= btrfs_join_transaction(root
);
4686 if (IS_ERR(trans
)) {
4687 btrfs_orphan_del(NULL
, inode
);
4688 btrfs_free_block_rsv(root
, rsv
);
4692 trans
->block_rsv
= rsv
;
4694 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4698 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4699 btrfs_end_transaction(trans
, root
);
4701 btrfs_btree_balance_dirty(root
);
4704 btrfs_free_block_rsv(root
, rsv
);
4707 trans
->block_rsv
= root
->orphan_block_rsv
;
4708 ret
= btrfs_orphan_del(trans
, inode
);
4712 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4713 if (!(root
== root
->fs_info
->tree_root
||
4714 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4715 btrfs_return_ino(root
, btrfs_ino(inode
));
4717 btrfs_end_transaction(trans
, root
);
4718 btrfs_btree_balance_dirty(root
);
4725 * this returns the key found in the dir entry in the location pointer.
4726 * If no dir entries were found, location->objectid is 0.
4728 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4729 struct btrfs_key
*location
)
4731 const char *name
= dentry
->d_name
.name
;
4732 int namelen
= dentry
->d_name
.len
;
4733 struct btrfs_dir_item
*di
;
4734 struct btrfs_path
*path
;
4735 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4738 path
= btrfs_alloc_path();
4742 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4747 if (IS_ERR_OR_NULL(di
))
4750 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4752 btrfs_free_path(path
);
4755 location
->objectid
= 0;
4760 * when we hit a tree root in a directory, the btrfs part of the inode
4761 * needs to be changed to reflect the root directory of the tree root. This
4762 * is kind of like crossing a mount point.
4764 static int fixup_tree_root_location(struct btrfs_root
*root
,
4766 struct dentry
*dentry
,
4767 struct btrfs_key
*location
,
4768 struct btrfs_root
**sub_root
)
4770 struct btrfs_path
*path
;
4771 struct btrfs_root
*new_root
;
4772 struct btrfs_root_ref
*ref
;
4773 struct extent_buffer
*leaf
;
4777 path
= btrfs_alloc_path();
4784 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4785 BTRFS_I(dir
)->root
->root_key
.objectid
,
4786 location
->objectid
);
4793 leaf
= path
->nodes
[0];
4794 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4795 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4796 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4799 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4800 (unsigned long)(ref
+ 1),
4801 dentry
->d_name
.len
);
4805 btrfs_release_path(path
);
4807 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4808 if (IS_ERR(new_root
)) {
4809 err
= PTR_ERR(new_root
);
4813 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4818 *sub_root
= new_root
;
4819 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4820 location
->type
= BTRFS_INODE_ITEM_KEY
;
4821 location
->offset
= 0;
4824 btrfs_free_path(path
);
4828 static void inode_tree_add(struct inode
*inode
)
4830 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4831 struct btrfs_inode
*entry
;
4833 struct rb_node
*parent
;
4834 u64 ino
= btrfs_ino(inode
);
4836 p
= &root
->inode_tree
.rb_node
;
4839 if (inode_unhashed(inode
))
4842 spin_lock(&root
->inode_lock
);
4845 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4847 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4848 p
= &parent
->rb_left
;
4849 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4850 p
= &parent
->rb_right
;
4852 WARN_ON(!(entry
->vfs_inode
.i_state
&
4853 (I_WILL_FREE
| I_FREEING
)));
4854 rb_erase(parent
, &root
->inode_tree
);
4855 RB_CLEAR_NODE(parent
);
4856 spin_unlock(&root
->inode_lock
);
4860 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4861 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4862 spin_unlock(&root
->inode_lock
);
4865 static void inode_tree_del(struct inode
*inode
)
4867 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4870 spin_lock(&root
->inode_lock
);
4871 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4872 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4873 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4874 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4876 spin_unlock(&root
->inode_lock
);
4879 * Free space cache has inodes in the tree root, but the tree root has a
4880 * root_refs of 0, so this could end up dropping the tree root as a
4881 * snapshot, so we need the extra !root->fs_info->tree_root check to
4882 * make sure we don't drop it.
4884 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4885 root
!= root
->fs_info
->tree_root
) {
4886 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4887 spin_lock(&root
->inode_lock
);
4888 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4889 spin_unlock(&root
->inode_lock
);
4891 btrfs_add_dead_root(root
);
4895 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4897 struct rb_node
*node
;
4898 struct rb_node
*prev
;
4899 struct btrfs_inode
*entry
;
4900 struct inode
*inode
;
4903 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4905 spin_lock(&root
->inode_lock
);
4907 node
= root
->inode_tree
.rb_node
;
4911 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4913 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4914 node
= node
->rb_left
;
4915 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4916 node
= node
->rb_right
;
4922 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4923 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4927 prev
= rb_next(prev
);
4931 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4932 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4933 inode
= igrab(&entry
->vfs_inode
);
4935 spin_unlock(&root
->inode_lock
);
4936 if (atomic_read(&inode
->i_count
) > 1)
4937 d_prune_aliases(inode
);
4939 * btrfs_drop_inode will have it removed from
4940 * the inode cache when its usage count
4945 spin_lock(&root
->inode_lock
);
4949 if (cond_resched_lock(&root
->inode_lock
))
4952 node
= rb_next(node
);
4954 spin_unlock(&root
->inode_lock
);
4957 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4959 struct btrfs_iget_args
*args
= p
;
4960 inode
->i_ino
= args
->ino
;
4961 BTRFS_I(inode
)->root
= args
->root
;
4965 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4967 struct btrfs_iget_args
*args
= opaque
;
4968 return args
->ino
== btrfs_ino(inode
) &&
4969 args
->root
== BTRFS_I(inode
)->root
;
4972 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4974 struct btrfs_root
*root
)
4976 struct inode
*inode
;
4977 struct btrfs_iget_args args
;
4978 args
.ino
= objectid
;
4981 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4982 btrfs_init_locked_inode
,
4987 /* Get an inode object given its location and corresponding root.
4988 * Returns in *is_new if the inode was read from disk
4990 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4991 struct btrfs_root
*root
, int *new)
4993 struct inode
*inode
;
4995 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4997 return ERR_PTR(-ENOMEM
);
4999 if (inode
->i_state
& I_NEW
) {
5000 BTRFS_I(inode
)->root
= root
;
5001 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
5002 btrfs_read_locked_inode(inode
);
5003 if (!is_bad_inode(inode
)) {
5004 inode_tree_add(inode
);
5005 unlock_new_inode(inode
);
5009 unlock_new_inode(inode
);
5011 inode
= ERR_PTR(-ESTALE
);
5018 static struct inode
*new_simple_dir(struct super_block
*s
,
5019 struct btrfs_key
*key
,
5020 struct btrfs_root
*root
)
5022 struct inode
*inode
= new_inode(s
);
5025 return ERR_PTR(-ENOMEM
);
5027 BTRFS_I(inode
)->root
= root
;
5028 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5029 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5031 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5032 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5033 inode
->i_fop
= &simple_dir_operations
;
5034 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5035 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5040 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5042 struct inode
*inode
;
5043 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5044 struct btrfs_root
*sub_root
= root
;
5045 struct btrfs_key location
;
5049 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5050 return ERR_PTR(-ENAMETOOLONG
);
5052 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5054 return ERR_PTR(ret
);
5056 if (location
.objectid
== 0)
5059 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5060 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5064 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5066 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5067 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5068 &location
, &sub_root
);
5071 inode
= ERR_PTR(ret
);
5073 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5075 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5077 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5079 if (!IS_ERR(inode
) && root
!= sub_root
) {
5080 down_read(&root
->fs_info
->cleanup_work_sem
);
5081 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5082 ret
= btrfs_orphan_cleanup(sub_root
);
5083 up_read(&root
->fs_info
->cleanup_work_sem
);
5085 inode
= ERR_PTR(ret
);
5091 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5093 struct btrfs_root
*root
;
5094 struct inode
*inode
= dentry
->d_inode
;
5096 if (!inode
&& !IS_ROOT(dentry
))
5097 inode
= dentry
->d_parent
->d_inode
;
5100 root
= BTRFS_I(inode
)->root
;
5101 if (btrfs_root_refs(&root
->root_item
) == 0)
5104 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5110 static void btrfs_dentry_release(struct dentry
*dentry
)
5112 if (dentry
->d_fsdata
)
5113 kfree(dentry
->d_fsdata
);
5116 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5121 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5125 unsigned char btrfs_filetype_table
[] = {
5126 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5129 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
5132 struct inode
*inode
= file_inode(filp
);
5133 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5134 struct btrfs_item
*item
;
5135 struct btrfs_dir_item
*di
;
5136 struct btrfs_key key
;
5137 struct btrfs_key found_key
;
5138 struct btrfs_path
*path
;
5139 struct list_head ins_list
;
5140 struct list_head del_list
;
5142 struct extent_buffer
*leaf
;
5144 unsigned char d_type
;
5149 int key_type
= BTRFS_DIR_INDEX_KEY
;
5153 int is_curr
= 0; /* filp->f_pos points to the current index? */
5155 /* FIXME, use a real flag for deciding about the key type */
5156 if (root
->fs_info
->tree_root
== root
)
5157 key_type
= BTRFS_DIR_ITEM_KEY
;
5159 /* special case for "." */
5160 if (filp
->f_pos
== 0) {
5161 over
= filldir(dirent
, ".", 1,
5162 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5167 /* special case for .., just use the back ref */
5168 if (filp
->f_pos
== 1) {
5169 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5170 over
= filldir(dirent
, "..", 2,
5171 filp
->f_pos
, pino
, DT_DIR
);
5176 path
= btrfs_alloc_path();
5182 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5183 INIT_LIST_HEAD(&ins_list
);
5184 INIT_LIST_HEAD(&del_list
);
5185 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5188 btrfs_set_key_type(&key
, key_type
);
5189 key
.offset
= filp
->f_pos
;
5190 key
.objectid
= btrfs_ino(inode
);
5192 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5197 leaf
= path
->nodes
[0];
5198 slot
= path
->slots
[0];
5199 if (slot
>= btrfs_header_nritems(leaf
)) {
5200 ret
= btrfs_next_leaf(root
, path
);
5208 item
= btrfs_item_nr(leaf
, slot
);
5209 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5211 if (found_key
.objectid
!= key
.objectid
)
5213 if (btrfs_key_type(&found_key
) != key_type
)
5215 if (found_key
.offset
< filp
->f_pos
)
5217 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5218 btrfs_should_delete_dir_index(&del_list
,
5222 filp
->f_pos
= found_key
.offset
;
5225 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5227 di_total
= btrfs_item_size(leaf
, item
);
5229 while (di_cur
< di_total
) {
5230 struct btrfs_key location
;
5232 if (verify_dir_item(root
, leaf
, di
))
5235 name_len
= btrfs_dir_name_len(leaf
, di
);
5236 if (name_len
<= sizeof(tmp_name
)) {
5237 name_ptr
= tmp_name
;
5239 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5245 read_extent_buffer(leaf
, name_ptr
,
5246 (unsigned long)(di
+ 1), name_len
);
5248 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5249 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5252 /* is this a reference to our own snapshot? If so
5255 * In contrast to old kernels, we insert the snapshot's
5256 * dir item and dir index after it has been created, so
5257 * we won't find a reference to our own snapshot. We
5258 * still keep the following code for backward
5261 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5262 location
.objectid
== root
->root_key
.objectid
) {
5266 over
= filldir(dirent
, name_ptr
, name_len
,
5267 found_key
.offset
, location
.objectid
,
5271 if (name_ptr
!= tmp_name
)
5276 di_len
= btrfs_dir_name_len(leaf
, di
) +
5277 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5279 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5285 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5288 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5294 /* Reached end of directory/root. Bump pos past the last item. */
5295 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5297 * 32-bit glibc will use getdents64, but then strtol -
5298 * so the last number we can serve is this.
5300 filp
->f_pos
= 0x7fffffff;
5306 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5307 btrfs_put_delayed_items(&ins_list
, &del_list
);
5308 btrfs_free_path(path
);
5312 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5314 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5315 struct btrfs_trans_handle
*trans
;
5317 bool nolock
= false;
5319 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5322 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5325 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5327 trans
= btrfs_join_transaction_nolock(root
);
5329 trans
= btrfs_join_transaction(root
);
5331 return PTR_ERR(trans
);
5332 ret
= btrfs_commit_transaction(trans
, root
);
5338 * This is somewhat expensive, updating the tree every time the
5339 * inode changes. But, it is most likely to find the inode in cache.
5340 * FIXME, needs more benchmarking...there are no reasons other than performance
5341 * to keep or drop this code.
5343 int btrfs_dirty_inode(struct inode
*inode
)
5345 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5346 struct btrfs_trans_handle
*trans
;
5349 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5352 trans
= btrfs_join_transaction(root
);
5354 return PTR_ERR(trans
);
5356 ret
= btrfs_update_inode(trans
, root
, inode
);
5357 if (ret
&& ret
== -ENOSPC
) {
5358 /* whoops, lets try again with the full transaction */
5359 btrfs_end_transaction(trans
, root
);
5360 trans
= btrfs_start_transaction(root
, 1);
5362 return PTR_ERR(trans
);
5364 ret
= btrfs_update_inode(trans
, root
, inode
);
5366 btrfs_end_transaction(trans
, root
);
5367 if (BTRFS_I(inode
)->delayed_node
)
5368 btrfs_balance_delayed_items(root
);
5374 * This is a copy of file_update_time. We need this so we can return error on
5375 * ENOSPC for updating the inode in the case of file write and mmap writes.
5377 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5380 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5382 if (btrfs_root_readonly(root
))
5385 if (flags
& S_VERSION
)
5386 inode_inc_iversion(inode
);
5387 if (flags
& S_CTIME
)
5388 inode
->i_ctime
= *now
;
5389 if (flags
& S_MTIME
)
5390 inode
->i_mtime
= *now
;
5391 if (flags
& S_ATIME
)
5392 inode
->i_atime
= *now
;
5393 return btrfs_dirty_inode(inode
);
5397 * find the highest existing sequence number in a directory
5398 * and then set the in-memory index_cnt variable to reflect
5399 * free sequence numbers
5401 static int btrfs_set_inode_index_count(struct inode
*inode
)
5403 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5404 struct btrfs_key key
, found_key
;
5405 struct btrfs_path
*path
;
5406 struct extent_buffer
*leaf
;
5409 key
.objectid
= btrfs_ino(inode
);
5410 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5411 key
.offset
= (u64
)-1;
5413 path
= btrfs_alloc_path();
5417 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5420 /* FIXME: we should be able to handle this */
5426 * MAGIC NUMBER EXPLANATION:
5427 * since we search a directory based on f_pos we have to start at 2
5428 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5429 * else has to start at 2
5431 if (path
->slots
[0] == 0) {
5432 BTRFS_I(inode
)->index_cnt
= 2;
5438 leaf
= path
->nodes
[0];
5439 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5441 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5442 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5443 BTRFS_I(inode
)->index_cnt
= 2;
5447 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5449 btrfs_free_path(path
);
5454 * helper to find a free sequence number in a given directory. This current
5455 * code is very simple, later versions will do smarter things in the btree
5457 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5461 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5462 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5464 ret
= btrfs_set_inode_index_count(dir
);
5470 *index
= BTRFS_I(dir
)->index_cnt
;
5471 BTRFS_I(dir
)->index_cnt
++;
5476 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5477 struct btrfs_root
*root
,
5479 const char *name
, int name_len
,
5480 u64 ref_objectid
, u64 objectid
,
5481 umode_t mode
, u64
*index
)
5483 struct inode
*inode
;
5484 struct btrfs_inode_item
*inode_item
;
5485 struct btrfs_key
*location
;
5486 struct btrfs_path
*path
;
5487 struct btrfs_inode_ref
*ref
;
5488 struct btrfs_key key
[2];
5494 path
= btrfs_alloc_path();
5496 return ERR_PTR(-ENOMEM
);
5498 inode
= new_inode(root
->fs_info
->sb
);
5500 btrfs_free_path(path
);
5501 return ERR_PTR(-ENOMEM
);
5505 * we have to initialize this early, so we can reclaim the inode
5506 * number if we fail afterwards in this function.
5508 inode
->i_ino
= objectid
;
5511 trace_btrfs_inode_request(dir
);
5513 ret
= btrfs_set_inode_index(dir
, index
);
5515 btrfs_free_path(path
);
5517 return ERR_PTR(ret
);
5521 * index_cnt is ignored for everything but a dir,
5522 * btrfs_get_inode_index_count has an explanation for the magic
5525 BTRFS_I(inode
)->index_cnt
= 2;
5526 BTRFS_I(inode
)->root
= root
;
5527 BTRFS_I(inode
)->generation
= trans
->transid
;
5528 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5531 * We could have gotten an inode number from somebody who was fsynced
5532 * and then removed in this same transaction, so let's just set full
5533 * sync since it will be a full sync anyway and this will blow away the
5534 * old info in the log.
5536 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5543 key
[0].objectid
= objectid
;
5544 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5548 * Start new inodes with an inode_ref. This is slightly more
5549 * efficient for small numbers of hard links since they will
5550 * be packed into one item. Extended refs will kick in if we
5551 * add more hard links than can fit in the ref item.
5553 key
[1].objectid
= objectid
;
5554 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5555 key
[1].offset
= ref_objectid
;
5557 sizes
[0] = sizeof(struct btrfs_inode_item
);
5558 sizes
[1] = name_len
+ sizeof(*ref
);
5560 path
->leave_spinning
= 1;
5561 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5565 inode_init_owner(inode
, dir
, mode
);
5566 inode_set_bytes(inode
, 0);
5567 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5568 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5569 struct btrfs_inode_item
);
5570 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5571 sizeof(*inode_item
));
5572 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5574 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5575 struct btrfs_inode_ref
);
5576 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5577 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5578 ptr
= (unsigned long)(ref
+ 1);
5579 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5581 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5582 btrfs_free_path(path
);
5584 location
= &BTRFS_I(inode
)->location
;
5585 location
->objectid
= objectid
;
5586 location
->offset
= 0;
5587 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5589 btrfs_inherit_iflags(inode
, dir
);
5591 if (S_ISREG(mode
)) {
5592 if (btrfs_test_opt(root
, NODATASUM
))
5593 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5594 if (btrfs_test_opt(root
, NODATACOW
))
5595 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5596 BTRFS_INODE_NODATASUM
;
5599 insert_inode_hash(inode
);
5600 inode_tree_add(inode
);
5602 trace_btrfs_inode_new(inode
);
5603 btrfs_set_inode_last_trans(trans
, inode
);
5605 btrfs_update_root_times(trans
, root
);
5610 BTRFS_I(dir
)->index_cnt
--;
5611 btrfs_free_path(path
);
5613 return ERR_PTR(ret
);
5616 static inline u8
btrfs_inode_type(struct inode
*inode
)
5618 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5622 * utility function to add 'inode' into 'parent_inode' with
5623 * a give name and a given sequence number.
5624 * if 'add_backref' is true, also insert a backref from the
5625 * inode to the parent directory.
5627 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5628 struct inode
*parent_inode
, struct inode
*inode
,
5629 const char *name
, int name_len
, int add_backref
, u64 index
)
5632 struct btrfs_key key
;
5633 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5634 u64 ino
= btrfs_ino(inode
);
5635 u64 parent_ino
= btrfs_ino(parent_inode
);
5637 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5638 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5641 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5645 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5646 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5647 key
.objectid
, root
->root_key
.objectid
,
5648 parent_ino
, index
, name
, name_len
);
5649 } else if (add_backref
) {
5650 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5654 /* Nothing to clean up yet */
5658 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5660 btrfs_inode_type(inode
), index
);
5661 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5664 btrfs_abort_transaction(trans
, root
, ret
);
5668 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5670 inode_inc_iversion(parent_inode
);
5671 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5672 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5674 btrfs_abort_transaction(trans
, root
, ret
);
5678 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5681 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5682 key
.objectid
, root
->root_key
.objectid
,
5683 parent_ino
, &local_index
, name
, name_len
);
5685 } else if (add_backref
) {
5689 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5690 ino
, parent_ino
, &local_index
);
5695 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5696 struct inode
*dir
, struct dentry
*dentry
,
5697 struct inode
*inode
, int backref
, u64 index
)
5699 int err
= btrfs_add_link(trans
, dir
, inode
,
5700 dentry
->d_name
.name
, dentry
->d_name
.len
,
5707 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5708 umode_t mode
, dev_t rdev
)
5710 struct btrfs_trans_handle
*trans
;
5711 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5712 struct inode
*inode
= NULL
;
5718 if (!new_valid_dev(rdev
))
5722 * 2 for inode item and ref
5724 * 1 for xattr if selinux is on
5726 trans
= btrfs_start_transaction(root
, 5);
5728 return PTR_ERR(trans
);
5730 err
= btrfs_find_free_ino(root
, &objectid
);
5734 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5735 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5737 if (IS_ERR(inode
)) {
5738 err
= PTR_ERR(inode
);
5742 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5749 * If the active LSM wants to access the inode during
5750 * d_instantiate it needs these. Smack checks to see
5751 * if the filesystem supports xattrs by looking at the
5755 inode
->i_op
= &btrfs_special_inode_operations
;
5756 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5760 init_special_inode(inode
, inode
->i_mode
, rdev
);
5761 btrfs_update_inode(trans
, root
, inode
);
5762 d_instantiate(dentry
, inode
);
5765 btrfs_end_transaction(trans
, root
);
5766 btrfs_btree_balance_dirty(root
);
5768 inode_dec_link_count(inode
);
5774 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5775 umode_t mode
, bool excl
)
5777 struct btrfs_trans_handle
*trans
;
5778 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5779 struct inode
*inode
= NULL
;
5780 int drop_inode_on_err
= 0;
5786 * 2 for inode item and ref
5788 * 1 for xattr if selinux is on
5790 trans
= btrfs_start_transaction(root
, 5);
5792 return PTR_ERR(trans
);
5794 err
= btrfs_find_free_ino(root
, &objectid
);
5798 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5799 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5801 if (IS_ERR(inode
)) {
5802 err
= PTR_ERR(inode
);
5805 drop_inode_on_err
= 1;
5807 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5811 err
= btrfs_update_inode(trans
, root
, inode
);
5816 * If the active LSM wants to access the inode during
5817 * d_instantiate it needs these. Smack checks to see
5818 * if the filesystem supports xattrs by looking at the
5821 inode
->i_fop
= &btrfs_file_operations
;
5822 inode
->i_op
= &btrfs_file_inode_operations
;
5824 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5828 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5829 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5830 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5831 d_instantiate(dentry
, inode
);
5834 btrfs_end_transaction(trans
, root
);
5835 if (err
&& drop_inode_on_err
) {
5836 inode_dec_link_count(inode
);
5839 btrfs_btree_balance_dirty(root
);
5843 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5844 struct dentry
*dentry
)
5846 struct btrfs_trans_handle
*trans
;
5847 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5848 struct inode
*inode
= old_dentry
->d_inode
;
5853 /* do not allow sys_link's with other subvols of the same device */
5854 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5857 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5860 err
= btrfs_set_inode_index(dir
, &index
);
5865 * 2 items for inode and inode ref
5866 * 2 items for dir items
5867 * 1 item for parent inode
5869 trans
= btrfs_start_transaction(root
, 5);
5870 if (IS_ERR(trans
)) {
5871 err
= PTR_ERR(trans
);
5875 btrfs_inc_nlink(inode
);
5876 inode_inc_iversion(inode
);
5877 inode
->i_ctime
= CURRENT_TIME
;
5879 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5881 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5886 struct dentry
*parent
= dentry
->d_parent
;
5887 err
= btrfs_update_inode(trans
, root
, inode
);
5890 d_instantiate(dentry
, inode
);
5891 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5894 btrfs_end_transaction(trans
, root
);
5897 inode_dec_link_count(inode
);
5900 btrfs_btree_balance_dirty(root
);
5904 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5906 struct inode
*inode
= NULL
;
5907 struct btrfs_trans_handle
*trans
;
5908 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5910 int drop_on_err
= 0;
5915 * 2 items for inode and ref
5916 * 2 items for dir items
5917 * 1 for xattr if selinux is on
5919 trans
= btrfs_start_transaction(root
, 5);
5921 return PTR_ERR(trans
);
5923 err
= btrfs_find_free_ino(root
, &objectid
);
5927 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5928 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5929 S_IFDIR
| mode
, &index
);
5930 if (IS_ERR(inode
)) {
5931 err
= PTR_ERR(inode
);
5937 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5941 inode
->i_op
= &btrfs_dir_inode_operations
;
5942 inode
->i_fop
= &btrfs_dir_file_operations
;
5944 btrfs_i_size_write(inode
, 0);
5945 err
= btrfs_update_inode(trans
, root
, inode
);
5949 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5950 dentry
->d_name
.len
, 0, index
);
5954 d_instantiate(dentry
, inode
);
5958 btrfs_end_transaction(trans
, root
);
5961 btrfs_btree_balance_dirty(root
);
5965 /* helper for btfs_get_extent. Given an existing extent in the tree,
5966 * and an extent that you want to insert, deal with overlap and insert
5967 * the new extent into the tree.
5969 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5970 struct extent_map
*existing
,
5971 struct extent_map
*em
,
5972 u64 map_start
, u64 map_len
)
5976 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5977 start_diff
= map_start
- em
->start
;
5978 em
->start
= map_start
;
5980 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5981 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5982 em
->block_start
+= start_diff
;
5983 em
->block_len
-= start_diff
;
5985 return add_extent_mapping(em_tree
, em
);
5988 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5989 struct inode
*inode
, struct page
*page
,
5990 size_t pg_offset
, u64 extent_offset
,
5991 struct btrfs_file_extent_item
*item
)
5994 struct extent_buffer
*leaf
= path
->nodes
[0];
5997 unsigned long inline_size
;
6001 WARN_ON(pg_offset
!= 0);
6002 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6003 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6004 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6005 btrfs_item_nr(leaf
, path
->slots
[0]));
6006 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6009 ptr
= btrfs_file_extent_inline_start(item
);
6011 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6013 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6014 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6015 extent_offset
, inline_size
, max_size
);
6017 char *kaddr
= kmap_atomic(page
);
6018 unsigned long copy_size
= min_t(u64
,
6019 PAGE_CACHE_SIZE
- pg_offset
,
6020 max_size
- extent_offset
);
6021 memset(kaddr
+ pg_offset
, 0, copy_size
);
6022 kunmap_atomic(kaddr
);
6029 * a bit scary, this does extent mapping from logical file offset to the disk.
6030 * the ugly parts come from merging extents from the disk with the in-ram
6031 * representation. This gets more complex because of the data=ordered code,
6032 * where the in-ram extents might be locked pending data=ordered completion.
6034 * This also copies inline extents directly into the page.
6037 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6038 size_t pg_offset
, u64 start
, u64 len
,
6044 u64 extent_start
= 0;
6046 u64 objectid
= btrfs_ino(inode
);
6048 struct btrfs_path
*path
= NULL
;
6049 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6050 struct btrfs_file_extent_item
*item
;
6051 struct extent_buffer
*leaf
;
6052 struct btrfs_key found_key
;
6053 struct extent_map
*em
= NULL
;
6054 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6055 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6056 struct btrfs_trans_handle
*trans
= NULL
;
6060 read_lock(&em_tree
->lock
);
6061 em
= lookup_extent_mapping(em_tree
, start
, len
);
6063 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6064 read_unlock(&em_tree
->lock
);
6067 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6068 free_extent_map(em
);
6069 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6070 free_extent_map(em
);
6074 em
= alloc_extent_map();
6079 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6080 em
->start
= EXTENT_MAP_HOLE
;
6081 em
->orig_start
= EXTENT_MAP_HOLE
;
6083 em
->block_len
= (u64
)-1;
6086 path
= btrfs_alloc_path();
6092 * Chances are we'll be called again, so go ahead and do
6098 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6099 objectid
, start
, trans
!= NULL
);
6106 if (path
->slots
[0] == 0)
6111 leaf
= path
->nodes
[0];
6112 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6113 struct btrfs_file_extent_item
);
6114 /* are we inside the extent that was found? */
6115 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6116 found_type
= btrfs_key_type(&found_key
);
6117 if (found_key
.objectid
!= objectid
||
6118 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6122 found_type
= btrfs_file_extent_type(leaf
, item
);
6123 extent_start
= found_key
.offset
;
6124 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6125 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6126 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6127 extent_end
= extent_start
+
6128 btrfs_file_extent_num_bytes(leaf
, item
);
6129 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6131 size
= btrfs_file_extent_inline_len(leaf
, item
);
6132 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6135 if (start
>= extent_end
) {
6137 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6138 ret
= btrfs_next_leaf(root
, path
);
6145 leaf
= path
->nodes
[0];
6147 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6148 if (found_key
.objectid
!= objectid
||
6149 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6151 if (start
+ len
<= found_key
.offset
)
6154 em
->orig_start
= start
;
6155 em
->len
= found_key
.offset
- start
;
6159 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6160 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6161 em
->start
= extent_start
;
6162 em
->len
= extent_end
- extent_start
;
6163 em
->orig_start
= extent_start
-
6164 btrfs_file_extent_offset(leaf
, item
);
6165 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6167 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6169 em
->block_start
= EXTENT_MAP_HOLE
;
6172 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6173 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6174 em
->compress_type
= compress_type
;
6175 em
->block_start
= bytenr
;
6176 em
->block_len
= em
->orig_block_len
;
6178 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6179 em
->block_start
= bytenr
;
6180 em
->block_len
= em
->len
;
6181 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6182 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6185 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6189 size_t extent_offset
;
6192 em
->block_start
= EXTENT_MAP_INLINE
;
6193 if (!page
|| create
) {
6194 em
->start
= extent_start
;
6195 em
->len
= extent_end
- extent_start
;
6199 size
= btrfs_file_extent_inline_len(leaf
, item
);
6200 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6201 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6202 size
- extent_offset
);
6203 em
->start
= extent_start
+ extent_offset
;
6204 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6205 em
->orig_block_len
= em
->len
;
6206 em
->orig_start
= em
->start
;
6207 if (compress_type
) {
6208 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6209 em
->compress_type
= compress_type
;
6211 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6212 if (create
== 0 && !PageUptodate(page
)) {
6213 if (btrfs_file_extent_compression(leaf
, item
) !=
6214 BTRFS_COMPRESS_NONE
) {
6215 ret
= uncompress_inline(path
, inode
, page
,
6217 extent_offset
, item
);
6218 BUG_ON(ret
); /* -ENOMEM */
6221 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6223 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6224 memset(map
+ pg_offset
+ copy_size
, 0,
6225 PAGE_CACHE_SIZE
- pg_offset
-
6230 flush_dcache_page(page
);
6231 } else if (create
&& PageUptodate(page
)) {
6235 free_extent_map(em
);
6238 btrfs_release_path(path
);
6239 trans
= btrfs_join_transaction(root
);
6242 return ERR_CAST(trans
);
6246 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6249 btrfs_mark_buffer_dirty(leaf
);
6251 set_extent_uptodate(io_tree
, em
->start
,
6252 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6255 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6259 em
->orig_start
= start
;
6262 em
->block_start
= EXTENT_MAP_HOLE
;
6263 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6265 btrfs_release_path(path
);
6266 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6267 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6268 (unsigned long long)em
->start
,
6269 (unsigned long long)em
->len
,
6270 (unsigned long long)start
,
6271 (unsigned long long)len
);
6277 write_lock(&em_tree
->lock
);
6278 ret
= add_extent_mapping(em_tree
, em
);
6279 /* it is possible that someone inserted the extent into the tree
6280 * while we had the lock dropped. It is also possible that
6281 * an overlapping map exists in the tree
6283 if (ret
== -EEXIST
) {
6284 struct extent_map
*existing
;
6288 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6289 if (existing
&& (existing
->start
> start
||
6290 existing
->start
+ existing
->len
<= start
)) {
6291 free_extent_map(existing
);
6295 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6298 err
= merge_extent_mapping(em_tree
, existing
,
6301 free_extent_map(existing
);
6303 free_extent_map(em
);
6308 free_extent_map(em
);
6312 free_extent_map(em
);
6317 write_unlock(&em_tree
->lock
);
6321 trace_btrfs_get_extent(root
, em
);
6324 btrfs_free_path(path
);
6326 ret
= btrfs_end_transaction(trans
, root
);
6331 free_extent_map(em
);
6332 return ERR_PTR(err
);
6334 BUG_ON(!em
); /* Error is always set */
6338 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6339 size_t pg_offset
, u64 start
, u64 len
,
6342 struct extent_map
*em
;
6343 struct extent_map
*hole_em
= NULL
;
6344 u64 range_start
= start
;
6350 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6357 * - a pre-alloc extent,
6358 * there might actually be delalloc bytes behind it.
6360 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6361 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6367 /* check to see if we've wrapped (len == -1 or similar) */
6376 /* ok, we didn't find anything, lets look for delalloc */
6377 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6378 end
, len
, EXTENT_DELALLOC
, 1);
6379 found_end
= range_start
+ found
;
6380 if (found_end
< range_start
)
6381 found_end
= (u64
)-1;
6384 * we didn't find anything useful, return
6385 * the original results from get_extent()
6387 if (range_start
> end
|| found_end
<= start
) {
6393 /* adjust the range_start to make sure it doesn't
6394 * go backwards from the start they passed in
6396 range_start
= max(start
,range_start
);
6397 found
= found_end
- range_start
;
6400 u64 hole_start
= start
;
6403 em
= alloc_extent_map();
6409 * when btrfs_get_extent can't find anything it
6410 * returns one huge hole
6412 * make sure what it found really fits our range, and
6413 * adjust to make sure it is based on the start from
6417 u64 calc_end
= extent_map_end(hole_em
);
6419 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6420 free_extent_map(hole_em
);
6423 hole_start
= max(hole_em
->start
, start
);
6424 hole_len
= calc_end
- hole_start
;
6428 if (hole_em
&& range_start
> hole_start
) {
6429 /* our hole starts before our delalloc, so we
6430 * have to return just the parts of the hole
6431 * that go until the delalloc starts
6433 em
->len
= min(hole_len
,
6434 range_start
- hole_start
);
6435 em
->start
= hole_start
;
6436 em
->orig_start
= hole_start
;
6438 * don't adjust block start at all,
6439 * it is fixed at EXTENT_MAP_HOLE
6441 em
->block_start
= hole_em
->block_start
;
6442 em
->block_len
= hole_len
;
6443 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6444 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6446 em
->start
= range_start
;
6448 em
->orig_start
= range_start
;
6449 em
->block_start
= EXTENT_MAP_DELALLOC
;
6450 em
->block_len
= found
;
6452 } else if (hole_em
) {
6457 free_extent_map(hole_em
);
6459 free_extent_map(em
);
6460 return ERR_PTR(err
);
6465 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6468 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6469 struct btrfs_trans_handle
*trans
;
6470 struct extent_map
*em
;
6471 struct btrfs_key ins
;
6475 trans
= btrfs_join_transaction(root
);
6477 return ERR_CAST(trans
);
6479 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6481 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6482 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6483 alloc_hint
, &ins
, 1);
6489 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6490 ins
.offset
, ins
.offset
, 0);
6494 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6495 ins
.offset
, ins
.offset
, 0);
6497 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6501 btrfs_end_transaction(trans
, root
);
6506 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6507 * block must be cow'd
6509 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6510 struct inode
*inode
, u64 offset
, u64 len
)
6512 struct btrfs_path
*path
;
6514 struct extent_buffer
*leaf
;
6515 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6516 struct btrfs_file_extent_item
*fi
;
6517 struct btrfs_key key
;
6525 path
= btrfs_alloc_path();
6529 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6534 slot
= path
->slots
[0];
6537 /* can't find the item, must cow */
6544 leaf
= path
->nodes
[0];
6545 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6546 if (key
.objectid
!= btrfs_ino(inode
) ||
6547 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6548 /* not our file or wrong item type, must cow */
6552 if (key
.offset
> offset
) {
6553 /* Wrong offset, must cow */
6557 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6558 found_type
= btrfs_file_extent_type(leaf
, fi
);
6559 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6560 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6561 /* not a regular extent, must cow */
6564 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6565 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6567 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6568 if (extent_end
< offset
+ len
) {
6569 /* extent doesn't include our full range, must cow */
6573 if (btrfs_extent_readonly(root
, disk_bytenr
))
6577 * look for other files referencing this extent, if we
6578 * find any we must cow
6580 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6581 key
.offset
- backref_offset
, disk_bytenr
))
6585 * adjust disk_bytenr and num_bytes to cover just the bytes
6586 * in this extent we are about to write. If there
6587 * are any csums in that range we have to cow in order
6588 * to keep the csums correct
6590 disk_bytenr
+= backref_offset
;
6591 disk_bytenr
+= offset
- key
.offset
;
6592 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
6593 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6596 * all of the above have passed, it is safe to overwrite this extent
6601 btrfs_free_path(path
);
6605 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6606 struct extent_state
**cached_state
, int writing
)
6608 struct btrfs_ordered_extent
*ordered
;
6612 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6615 * We're concerned with the entire range that we're going to be
6616 * doing DIO to, so we need to make sure theres no ordered
6617 * extents in this range.
6619 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6620 lockend
- lockstart
+ 1);
6623 * We need to make sure there are no buffered pages in this
6624 * range either, we could have raced between the invalidate in
6625 * generic_file_direct_write and locking the extent. The
6626 * invalidate needs to happen so that reads after a write do not
6629 if (!ordered
&& (!writing
||
6630 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6631 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6635 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6636 cached_state
, GFP_NOFS
);
6639 btrfs_start_ordered_extent(inode
, ordered
, 1);
6640 btrfs_put_ordered_extent(ordered
);
6642 /* Screw you mmap */
6643 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6650 * If we found a page that couldn't be invalidated just
6651 * fall back to buffered.
6653 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6654 lockstart
>> PAGE_CACHE_SHIFT
,
6655 lockend
>> PAGE_CACHE_SHIFT
);
6666 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6667 u64 len
, u64 orig_start
,
6668 u64 block_start
, u64 block_len
,
6669 u64 orig_block_len
, int type
)
6671 struct extent_map_tree
*em_tree
;
6672 struct extent_map
*em
;
6673 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6676 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6677 em
= alloc_extent_map();
6679 return ERR_PTR(-ENOMEM
);
6682 em
->orig_start
= orig_start
;
6683 em
->mod_start
= start
;
6686 em
->block_len
= block_len
;
6687 em
->block_start
= block_start
;
6688 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6689 em
->orig_block_len
= orig_block_len
;
6690 em
->generation
= -1;
6691 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6692 if (type
== BTRFS_ORDERED_PREALLOC
)
6693 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6696 btrfs_drop_extent_cache(inode
, em
->start
,
6697 em
->start
+ em
->len
- 1, 0);
6698 write_lock(&em_tree
->lock
);
6699 ret
= add_extent_mapping(em_tree
, em
);
6701 list_move(&em
->list
,
6702 &em_tree
->modified_extents
);
6703 write_unlock(&em_tree
->lock
);
6704 } while (ret
== -EEXIST
);
6707 free_extent_map(em
);
6708 return ERR_PTR(ret
);
6715 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6716 struct buffer_head
*bh_result
, int create
)
6718 struct extent_map
*em
;
6719 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6720 struct extent_state
*cached_state
= NULL
;
6721 u64 start
= iblock
<< inode
->i_blkbits
;
6722 u64 lockstart
, lockend
;
6723 u64 len
= bh_result
->b_size
;
6724 struct btrfs_trans_handle
*trans
;
6725 int unlock_bits
= EXTENT_LOCKED
;
6729 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6731 len
= min_t(u64
, len
, root
->sectorsize
);
6734 lockend
= start
+ len
- 1;
6737 * If this errors out it's because we couldn't invalidate pagecache for
6738 * this range and we need to fallback to buffered.
6740 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6743 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6750 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6751 * io. INLINE is special, and we could probably kludge it in here, but
6752 * it's still buffered so for safety lets just fall back to the generic
6755 * For COMPRESSED we _have_ to read the entire extent in so we can
6756 * decompress it, so there will be buffering required no matter what we
6757 * do, so go ahead and fallback to buffered.
6759 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6760 * to buffered IO. Don't blame me, this is the price we pay for using
6763 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6764 em
->block_start
== EXTENT_MAP_INLINE
) {
6765 free_extent_map(em
);
6770 /* Just a good old fashioned hole, return */
6771 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6772 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6773 free_extent_map(em
);
6778 * We don't allocate a new extent in the following cases
6780 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6782 * 2) The extent is marked as PREALLOC. We're good to go here and can
6783 * just use the extent.
6787 len
= min(len
, em
->len
- (start
- em
->start
));
6788 lockstart
= start
+ len
;
6792 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6793 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6794 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6799 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6800 type
= BTRFS_ORDERED_PREALLOC
;
6802 type
= BTRFS_ORDERED_NOCOW
;
6803 len
= min(len
, em
->len
- (start
- em
->start
));
6804 block_start
= em
->block_start
+ (start
- em
->start
);
6807 * we're not going to log anything, but we do need
6808 * to make sure the current transaction stays open
6809 * while we look for nocow cross refs
6811 trans
= btrfs_join_transaction(root
);
6815 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6816 u64 orig_start
= em
->orig_start
;
6817 u64 orig_block_len
= em
->orig_block_len
;
6819 if (type
== BTRFS_ORDERED_PREALLOC
) {
6820 free_extent_map(em
);
6821 em
= create_pinned_em(inode
, start
, len
,
6824 orig_block_len
, type
);
6826 btrfs_end_transaction(trans
, root
);
6831 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6832 block_start
, len
, len
, type
);
6833 btrfs_end_transaction(trans
, root
);
6835 free_extent_map(em
);
6840 btrfs_end_transaction(trans
, root
);
6844 * this will cow the extent, reset the len in case we changed
6847 len
= bh_result
->b_size
;
6848 free_extent_map(em
);
6849 em
= btrfs_new_extent_direct(inode
, start
, len
);
6854 len
= min(len
, em
->len
- (start
- em
->start
));
6856 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6858 bh_result
->b_size
= len
;
6859 bh_result
->b_bdev
= em
->bdev
;
6860 set_buffer_mapped(bh_result
);
6862 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6863 set_buffer_new(bh_result
);
6866 * Need to update the i_size under the extent lock so buffered
6867 * readers will get the updated i_size when we unlock.
6869 if (start
+ len
> i_size_read(inode
))
6870 i_size_write(inode
, start
+ len
);
6872 spin_lock(&BTRFS_I(inode
)->lock
);
6873 BTRFS_I(inode
)->outstanding_extents
++;
6874 spin_unlock(&BTRFS_I(inode
)->lock
);
6876 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6877 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6878 &cached_state
, GFP_NOFS
);
6883 * In the case of write we need to clear and unlock the entire range,
6884 * in the case of read we need to unlock only the end area that we
6885 * aren't using if there is any left over space.
6887 if (lockstart
< lockend
) {
6888 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6889 lockend
, unlock_bits
, 1, 0,
6890 &cached_state
, GFP_NOFS
);
6892 free_extent_state(cached_state
);
6895 free_extent_map(em
);
6900 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6901 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6905 struct btrfs_dio_private
{
6906 struct inode
*inode
;
6912 /* number of bios pending for this dio */
6913 atomic_t pending_bios
;
6918 struct bio
*orig_bio
;
6921 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6923 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6924 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6925 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6926 struct inode
*inode
= dip
->inode
;
6927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6930 start
= dip
->logical_offset
;
6932 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6933 struct page
*page
= bvec
->bv_page
;
6936 u64
private = ~(u32
)0;
6937 unsigned long flags
;
6939 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6942 local_irq_save(flags
);
6943 kaddr
= kmap_atomic(page
);
6944 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6945 csum
, bvec
->bv_len
);
6946 btrfs_csum_final(csum
, (char *)&csum
);
6947 kunmap_atomic(kaddr
);
6948 local_irq_restore(flags
);
6950 flush_dcache_page(bvec
->bv_page
);
6951 if (csum
!= private) {
6953 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6954 (unsigned long long)btrfs_ino(inode
),
6955 (unsigned long long)start
,
6956 csum
, (unsigned)private);
6961 start
+= bvec
->bv_len
;
6963 } while (bvec
<= bvec_end
);
6965 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6966 dip
->logical_offset
+ dip
->bytes
- 1);
6967 bio
->bi_private
= dip
->private;
6971 /* If we had a csum failure make sure to clear the uptodate flag */
6973 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6974 dio_end_io(bio
, err
);
6977 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6979 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6980 struct inode
*inode
= dip
->inode
;
6981 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6982 struct btrfs_ordered_extent
*ordered
= NULL
;
6983 u64 ordered_offset
= dip
->logical_offset
;
6984 u64 ordered_bytes
= dip
->bytes
;
6990 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6992 ordered_bytes
, !err
);
6996 ordered
->work
.func
= finish_ordered_fn
;
6997 ordered
->work
.flags
= 0;
6998 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
7002 * our bio might span multiple ordered extents. If we haven't
7003 * completed the accounting for the whole dio, go back and try again
7005 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7006 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7012 bio
->bi_private
= dip
->private;
7016 /* If we had an error make sure to clear the uptodate flag */
7018 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
7019 dio_end_io(bio
, err
);
7022 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7023 struct bio
*bio
, int mirror_num
,
7024 unsigned long bio_flags
, u64 offset
)
7027 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7028 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7029 BUG_ON(ret
); /* -ENOMEM */
7033 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7035 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7038 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
7039 "sector %#Lx len %u err no %d\n",
7040 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
7041 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
7045 * before atomic variable goto zero, we must make sure
7046 * dip->errors is perceived to be set.
7048 smp_mb__before_atomic_dec();
7051 /* if there are more bios still pending for this dio, just exit */
7052 if (!atomic_dec_and_test(&dip
->pending_bios
))
7056 bio_io_error(dip
->orig_bio
);
7058 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
7059 bio_endio(dip
->orig_bio
, 0);
7065 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7066 u64 first_sector
, gfp_t gfp_flags
)
7068 int nr_vecs
= bio_get_nr_vecs(bdev
);
7069 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7072 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7073 int rw
, u64 file_offset
, int skip_sum
,
7076 int write
= rw
& REQ_WRITE
;
7077 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7081 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7086 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7094 if (write
&& async_submit
) {
7095 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7096 inode
, rw
, bio
, 0, 0,
7098 __btrfs_submit_bio_start_direct_io
,
7099 __btrfs_submit_bio_done
);
7103 * If we aren't doing async submit, calculate the csum of the
7106 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7109 } else if (!skip_sum
) {
7110 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7116 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7122 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7125 struct inode
*inode
= dip
->inode
;
7126 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7128 struct bio
*orig_bio
= dip
->orig_bio
;
7129 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7130 u64 start_sector
= orig_bio
->bi_sector
;
7131 u64 file_offset
= dip
->logical_offset
;
7136 int async_submit
= 0;
7138 map_length
= orig_bio
->bi_size
;
7139 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7140 &map_length
, NULL
, 0);
7145 if (map_length
>= orig_bio
->bi_size
) {
7150 /* async crcs make it difficult to collect full stripe writes. */
7151 if (btrfs_get_alloc_profile(root
, 1) &
7152 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7157 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7160 bio
->bi_private
= dip
;
7161 bio
->bi_end_io
= btrfs_end_dio_bio
;
7162 atomic_inc(&dip
->pending_bios
);
7164 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7165 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7166 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7167 bvec
->bv_offset
) < bvec
->bv_len
)) {
7169 * inc the count before we submit the bio so
7170 * we know the end IO handler won't happen before
7171 * we inc the count. Otherwise, the dip might get freed
7172 * before we're done setting it up
7174 atomic_inc(&dip
->pending_bios
);
7175 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7176 file_offset
, skip_sum
,
7180 atomic_dec(&dip
->pending_bios
);
7184 start_sector
+= submit_len
>> 9;
7185 file_offset
+= submit_len
;
7190 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7191 start_sector
, GFP_NOFS
);
7194 bio
->bi_private
= dip
;
7195 bio
->bi_end_io
= btrfs_end_dio_bio
;
7197 map_length
= orig_bio
->bi_size
;
7198 ret
= btrfs_map_block(root
->fs_info
, rw
,
7200 &map_length
, NULL
, 0);
7206 submit_len
+= bvec
->bv_len
;
7213 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7222 * before atomic variable goto zero, we must
7223 * make sure dip->errors is perceived to be set.
7225 smp_mb__before_atomic_dec();
7226 if (atomic_dec_and_test(&dip
->pending_bios
))
7227 bio_io_error(dip
->orig_bio
);
7229 /* bio_end_io() will handle error, so we needn't return it */
7233 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
7236 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7237 struct btrfs_dio_private
*dip
;
7238 struct bio_vec
*bvec
= bio
->bi_io_vec
;
7240 int write
= rw
& REQ_WRITE
;
7243 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7245 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7251 dip
->private = bio
->bi_private
;
7253 dip
->logical_offset
= file_offset
;
7257 dip
->bytes
+= bvec
->bv_len
;
7259 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
7261 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
7262 bio
->bi_private
= dip
;
7264 dip
->orig_bio
= bio
;
7265 atomic_set(&dip
->pending_bios
, 0);
7268 bio
->bi_end_io
= btrfs_endio_direct_write
;
7270 bio
->bi_end_io
= btrfs_endio_direct_read
;
7272 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7277 * If this is a write, we need to clean up the reserved space and kill
7278 * the ordered extent.
7281 struct btrfs_ordered_extent
*ordered
;
7282 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7283 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7284 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7285 btrfs_free_reserved_extent(root
, ordered
->start
,
7287 btrfs_put_ordered_extent(ordered
);
7288 btrfs_put_ordered_extent(ordered
);
7290 bio_endio(bio
, ret
);
7293 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7294 const struct iovec
*iov
, loff_t offset
,
7295 unsigned long nr_segs
)
7301 unsigned blocksize_mask
= root
->sectorsize
- 1;
7302 ssize_t retval
= -EINVAL
;
7303 loff_t end
= offset
;
7305 if (offset
& blocksize_mask
)
7308 /* Check the memory alignment. Blocks cannot straddle pages */
7309 for (seg
= 0; seg
< nr_segs
; seg
++) {
7310 addr
= (unsigned long)iov
[seg
].iov_base
;
7311 size
= iov
[seg
].iov_len
;
7313 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7316 /* If this is a write we don't need to check anymore */
7321 * Check to make sure we don't have duplicate iov_base's in this
7322 * iovec, if so return EINVAL, otherwise we'll get csum errors
7323 * when reading back.
7325 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7326 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7335 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7336 const struct iovec
*iov
, loff_t offset
,
7337 unsigned long nr_segs
)
7339 struct file
*file
= iocb
->ki_filp
;
7340 struct inode
*inode
= file
->f_mapping
->host
;
7344 bool relock
= false;
7347 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7351 atomic_inc(&inode
->i_dio_count
);
7352 smp_mb__after_atomic_inc();
7355 count
= iov_length(iov
, nr_segs
);
7357 * If the write DIO is beyond the EOF, we need update
7358 * the isize, but it is protected by i_mutex. So we can
7359 * not unlock the i_mutex at this case.
7361 if (offset
+ count
<= inode
->i_size
) {
7362 mutex_unlock(&inode
->i_mutex
);
7365 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7368 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7369 &BTRFS_I(inode
)->runtime_flags
))) {
7370 inode_dio_done(inode
);
7371 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7375 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7376 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7377 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7378 btrfs_submit_direct
, flags
);
7380 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7381 btrfs_delalloc_release_space(inode
, count
);
7382 else if (ret
>= 0 && (size_t)ret
< count
)
7383 btrfs_delalloc_release_space(inode
,
7384 count
- (size_t)ret
);
7386 btrfs_delalloc_release_metadata(inode
, 0);
7390 inode_dio_done(inode
);
7392 mutex_lock(&inode
->i_mutex
);
7397 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7399 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7400 __u64 start
, __u64 len
)
7404 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7408 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7411 int btrfs_readpage(struct file
*file
, struct page
*page
)
7413 struct extent_io_tree
*tree
;
7414 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7415 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7418 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7420 struct extent_io_tree
*tree
;
7423 if (current
->flags
& PF_MEMALLOC
) {
7424 redirty_page_for_writepage(wbc
, page
);
7428 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7429 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7432 int btrfs_writepages(struct address_space
*mapping
,
7433 struct writeback_control
*wbc
)
7435 struct extent_io_tree
*tree
;
7437 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7438 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7442 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7443 struct list_head
*pages
, unsigned nr_pages
)
7445 struct extent_io_tree
*tree
;
7446 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7447 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7450 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7452 struct extent_io_tree
*tree
;
7453 struct extent_map_tree
*map
;
7456 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7457 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7458 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7460 ClearPagePrivate(page
);
7461 set_page_private(page
, 0);
7462 page_cache_release(page
);
7467 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7469 if (PageWriteback(page
) || PageDirty(page
))
7471 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7474 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7476 struct inode
*inode
= page
->mapping
->host
;
7477 struct extent_io_tree
*tree
;
7478 struct btrfs_ordered_extent
*ordered
;
7479 struct extent_state
*cached_state
= NULL
;
7480 u64 page_start
= page_offset(page
);
7481 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7484 * we have the page locked, so new writeback can't start,
7485 * and the dirty bit won't be cleared while we are here.
7487 * Wait for IO on this page so that we can safely clear
7488 * the PagePrivate2 bit and do ordered accounting
7490 wait_on_page_writeback(page
);
7492 tree
= &BTRFS_I(inode
)->io_tree
;
7494 btrfs_releasepage(page
, GFP_NOFS
);
7497 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7498 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7501 * IO on this page will never be started, so we need
7502 * to account for any ordered extents now
7504 clear_extent_bit(tree
, page_start
, page_end
,
7505 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7506 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7507 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7509 * whoever cleared the private bit is responsible
7510 * for the finish_ordered_io
7512 if (TestClearPagePrivate2(page
) &&
7513 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7514 PAGE_CACHE_SIZE
, 1)) {
7515 btrfs_finish_ordered_io(ordered
);
7517 btrfs_put_ordered_extent(ordered
);
7518 cached_state
= NULL
;
7519 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7521 clear_extent_bit(tree
, page_start
, page_end
,
7522 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7523 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7524 &cached_state
, GFP_NOFS
);
7525 __btrfs_releasepage(page
, GFP_NOFS
);
7527 ClearPageChecked(page
);
7528 if (PagePrivate(page
)) {
7529 ClearPagePrivate(page
);
7530 set_page_private(page
, 0);
7531 page_cache_release(page
);
7536 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7537 * called from a page fault handler when a page is first dirtied. Hence we must
7538 * be careful to check for EOF conditions here. We set the page up correctly
7539 * for a written page which means we get ENOSPC checking when writing into
7540 * holes and correct delalloc and unwritten extent mapping on filesystems that
7541 * support these features.
7543 * We are not allowed to take the i_mutex here so we have to play games to
7544 * protect against truncate races as the page could now be beyond EOF. Because
7545 * vmtruncate() writes the inode size before removing pages, once we have the
7546 * page lock we can determine safely if the page is beyond EOF. If it is not
7547 * beyond EOF, then the page is guaranteed safe against truncation until we
7550 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7552 struct page
*page
= vmf
->page
;
7553 struct inode
*inode
= file_inode(vma
->vm_file
);
7554 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7555 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7556 struct btrfs_ordered_extent
*ordered
;
7557 struct extent_state
*cached_state
= NULL
;
7559 unsigned long zero_start
;
7566 sb_start_pagefault(inode
->i_sb
);
7567 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7569 ret
= file_update_time(vma
->vm_file
);
7575 else /* -ENOSPC, -EIO, etc */
7576 ret
= VM_FAULT_SIGBUS
;
7582 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7585 size
= i_size_read(inode
);
7586 page_start
= page_offset(page
);
7587 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7589 if ((page
->mapping
!= inode
->i_mapping
) ||
7590 (page_start
>= size
)) {
7591 /* page got truncated out from underneath us */
7594 wait_on_page_writeback(page
);
7596 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7597 set_page_extent_mapped(page
);
7600 * we can't set the delalloc bits if there are pending ordered
7601 * extents. Drop our locks and wait for them to finish
7603 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7605 unlock_extent_cached(io_tree
, page_start
, page_end
,
7606 &cached_state
, GFP_NOFS
);
7608 btrfs_start_ordered_extent(inode
, ordered
, 1);
7609 btrfs_put_ordered_extent(ordered
);
7614 * XXX - page_mkwrite gets called every time the page is dirtied, even
7615 * if it was already dirty, so for space accounting reasons we need to
7616 * clear any delalloc bits for the range we are fixing to save. There
7617 * is probably a better way to do this, but for now keep consistent with
7618 * prepare_pages in the normal write path.
7620 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7621 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7622 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7623 0, 0, &cached_state
, GFP_NOFS
);
7625 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7628 unlock_extent_cached(io_tree
, page_start
, page_end
,
7629 &cached_state
, GFP_NOFS
);
7630 ret
= VM_FAULT_SIGBUS
;
7635 /* page is wholly or partially inside EOF */
7636 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7637 zero_start
= size
& ~PAGE_CACHE_MASK
;
7639 zero_start
= PAGE_CACHE_SIZE
;
7641 if (zero_start
!= PAGE_CACHE_SIZE
) {
7643 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7644 flush_dcache_page(page
);
7647 ClearPageChecked(page
);
7648 set_page_dirty(page
);
7649 SetPageUptodate(page
);
7651 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7652 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7653 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7655 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7659 sb_end_pagefault(inode
->i_sb
);
7660 return VM_FAULT_LOCKED
;
7664 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7666 sb_end_pagefault(inode
->i_sb
);
7670 static int btrfs_truncate(struct inode
*inode
)
7672 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7673 struct btrfs_block_rsv
*rsv
;
7676 struct btrfs_trans_handle
*trans
;
7677 u64 mask
= root
->sectorsize
- 1;
7678 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7680 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7684 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7685 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7688 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7689 * 3 things going on here
7691 * 1) We need to reserve space for our orphan item and the space to
7692 * delete our orphan item. Lord knows we don't want to have a dangling
7693 * orphan item because we didn't reserve space to remove it.
7695 * 2) We need to reserve space to update our inode.
7697 * 3) We need to have something to cache all the space that is going to
7698 * be free'd up by the truncate operation, but also have some slack
7699 * space reserved in case it uses space during the truncate (thank you
7700 * very much snapshotting).
7702 * And we need these to all be seperate. The fact is we can use alot of
7703 * space doing the truncate, and we have no earthly idea how much space
7704 * we will use, so we need the truncate reservation to be seperate so it
7705 * doesn't end up using space reserved for updating the inode or
7706 * removing the orphan item. We also need to be able to stop the
7707 * transaction and start a new one, which means we need to be able to
7708 * update the inode several times, and we have no idea of knowing how
7709 * many times that will be, so we can't just reserve 1 item for the
7710 * entirety of the opration, so that has to be done seperately as well.
7711 * Then there is the orphan item, which does indeed need to be held on
7712 * to for the whole operation, and we need nobody to touch this reserved
7713 * space except the orphan code.
7715 * So that leaves us with
7717 * 1) root->orphan_block_rsv - for the orphan deletion.
7718 * 2) rsv - for the truncate reservation, which we will steal from the
7719 * transaction reservation.
7720 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7721 * updating the inode.
7723 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7726 rsv
->size
= min_size
;
7730 * 1 for the truncate slack space
7731 * 1 for updating the inode.
7733 trans
= btrfs_start_transaction(root
, 2);
7734 if (IS_ERR(trans
)) {
7735 err
= PTR_ERR(trans
);
7739 /* Migrate the slack space for the truncate to our reserve */
7740 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7745 * setattr is responsible for setting the ordered_data_close flag,
7746 * but that is only tested during the last file release. That
7747 * could happen well after the next commit, leaving a great big
7748 * window where new writes may get lost if someone chooses to write
7749 * to this file after truncating to zero
7751 * The inode doesn't have any dirty data here, and so if we commit
7752 * this is a noop. If someone immediately starts writing to the inode
7753 * it is very likely we'll catch some of their writes in this
7754 * transaction, and the commit will find this file on the ordered
7755 * data list with good things to send down.
7757 * This is a best effort solution, there is still a window where
7758 * using truncate to replace the contents of the file will
7759 * end up with a zero length file after a crash.
7761 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7762 &BTRFS_I(inode
)->runtime_flags
))
7763 btrfs_add_ordered_operation(trans
, root
, inode
);
7766 * So if we truncate and then write and fsync we normally would just
7767 * write the extents that changed, which is a problem if we need to
7768 * first truncate that entire inode. So set this flag so we write out
7769 * all of the extents in the inode to the sync log so we're completely
7772 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7773 trans
->block_rsv
= rsv
;
7776 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7778 BTRFS_EXTENT_DATA_KEY
);
7779 if (ret
!= -ENOSPC
) {
7784 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7785 ret
= btrfs_update_inode(trans
, root
, inode
);
7791 btrfs_end_transaction(trans
, root
);
7792 btrfs_btree_balance_dirty(root
);
7794 trans
= btrfs_start_transaction(root
, 2);
7795 if (IS_ERR(trans
)) {
7796 ret
= err
= PTR_ERR(trans
);
7801 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7803 BUG_ON(ret
); /* shouldn't happen */
7804 trans
->block_rsv
= rsv
;
7807 if (ret
== 0 && inode
->i_nlink
> 0) {
7808 trans
->block_rsv
= root
->orphan_block_rsv
;
7809 ret
= btrfs_orphan_del(trans
, inode
);
7815 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7816 ret
= btrfs_update_inode(trans
, root
, inode
);
7820 ret
= btrfs_end_transaction(trans
, root
);
7821 btrfs_btree_balance_dirty(root
);
7825 btrfs_free_block_rsv(root
, rsv
);
7834 * create a new subvolume directory/inode (helper for the ioctl).
7836 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7837 struct btrfs_root
*new_root
, u64 new_dirid
)
7839 struct inode
*inode
;
7843 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7844 new_dirid
, new_dirid
,
7845 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7848 return PTR_ERR(inode
);
7849 inode
->i_op
= &btrfs_dir_inode_operations
;
7850 inode
->i_fop
= &btrfs_dir_file_operations
;
7852 set_nlink(inode
, 1);
7853 btrfs_i_size_write(inode
, 0);
7855 err
= btrfs_update_inode(trans
, new_root
, inode
);
7861 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7863 struct btrfs_inode
*ei
;
7864 struct inode
*inode
;
7866 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7873 ei
->last_sub_trans
= 0;
7874 ei
->logged_trans
= 0;
7875 ei
->delalloc_bytes
= 0;
7876 ei
->disk_i_size
= 0;
7879 ei
->index_cnt
= (u64
)-1;
7880 ei
->last_unlink_trans
= 0;
7881 ei
->last_log_commit
= 0;
7883 spin_lock_init(&ei
->lock
);
7884 ei
->outstanding_extents
= 0;
7885 ei
->reserved_extents
= 0;
7887 ei
->runtime_flags
= 0;
7888 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7890 ei
->delayed_node
= NULL
;
7892 inode
= &ei
->vfs_inode
;
7893 extent_map_tree_init(&ei
->extent_tree
);
7894 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7895 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7896 ei
->io_tree
.track_uptodate
= 1;
7897 ei
->io_failure_tree
.track_uptodate
= 1;
7898 atomic_set(&ei
->sync_writers
, 0);
7899 mutex_init(&ei
->log_mutex
);
7900 mutex_init(&ei
->delalloc_mutex
);
7901 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7902 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7903 INIT_LIST_HEAD(&ei
->ordered_operations
);
7904 RB_CLEAR_NODE(&ei
->rb_node
);
7909 static void btrfs_i_callback(struct rcu_head
*head
)
7911 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7912 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7915 void btrfs_destroy_inode(struct inode
*inode
)
7917 struct btrfs_ordered_extent
*ordered
;
7918 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7920 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7921 WARN_ON(inode
->i_data
.nrpages
);
7922 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7923 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7924 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7925 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7928 * This can happen where we create an inode, but somebody else also
7929 * created the same inode and we need to destroy the one we already
7936 * Make sure we're properly removed from the ordered operation
7940 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7941 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7942 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7943 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7946 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7947 &BTRFS_I(inode
)->runtime_flags
)) {
7948 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7949 (unsigned long long)btrfs_ino(inode
));
7950 atomic_dec(&root
->orphan_inodes
);
7954 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7958 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7959 (unsigned long long)ordered
->file_offset
,
7960 (unsigned long long)ordered
->len
);
7961 btrfs_remove_ordered_extent(inode
, ordered
);
7962 btrfs_put_ordered_extent(ordered
);
7963 btrfs_put_ordered_extent(ordered
);
7966 inode_tree_del(inode
);
7967 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7969 btrfs_remove_delayed_node(inode
);
7970 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7973 int btrfs_drop_inode(struct inode
*inode
)
7975 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7977 /* the snap/subvol tree is on deleting */
7978 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7979 root
!= root
->fs_info
->tree_root
)
7982 return generic_drop_inode(inode
);
7985 static void init_once(void *foo
)
7987 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7989 inode_init_once(&ei
->vfs_inode
);
7992 void btrfs_destroy_cachep(void)
7995 * Make sure all delayed rcu free inodes are flushed before we
7999 if (btrfs_inode_cachep
)
8000 kmem_cache_destroy(btrfs_inode_cachep
);
8001 if (btrfs_trans_handle_cachep
)
8002 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8003 if (btrfs_transaction_cachep
)
8004 kmem_cache_destroy(btrfs_transaction_cachep
);
8005 if (btrfs_path_cachep
)
8006 kmem_cache_destroy(btrfs_path_cachep
);
8007 if (btrfs_free_space_cachep
)
8008 kmem_cache_destroy(btrfs_free_space_cachep
);
8009 if (btrfs_delalloc_work_cachep
)
8010 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8013 int btrfs_init_cachep(void)
8015 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8016 sizeof(struct btrfs_inode
), 0,
8017 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8018 if (!btrfs_inode_cachep
)
8021 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8022 sizeof(struct btrfs_trans_handle
), 0,
8023 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8024 if (!btrfs_trans_handle_cachep
)
8027 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8028 sizeof(struct btrfs_transaction
), 0,
8029 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8030 if (!btrfs_transaction_cachep
)
8033 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8034 sizeof(struct btrfs_path
), 0,
8035 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8036 if (!btrfs_path_cachep
)
8039 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8040 sizeof(struct btrfs_free_space
), 0,
8041 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8042 if (!btrfs_free_space_cachep
)
8045 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8046 sizeof(struct btrfs_delalloc_work
), 0,
8047 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8049 if (!btrfs_delalloc_work_cachep
)
8054 btrfs_destroy_cachep();
8058 static int btrfs_getattr(struct vfsmount
*mnt
,
8059 struct dentry
*dentry
, struct kstat
*stat
)
8062 struct inode
*inode
= dentry
->d_inode
;
8063 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8065 generic_fillattr(inode
, stat
);
8066 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8067 stat
->blksize
= PAGE_CACHE_SIZE
;
8069 spin_lock(&BTRFS_I(inode
)->lock
);
8070 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8071 spin_unlock(&BTRFS_I(inode
)->lock
);
8072 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8073 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8077 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8078 struct inode
*new_dir
, struct dentry
*new_dentry
)
8080 struct btrfs_trans_handle
*trans
;
8081 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8082 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8083 struct inode
*new_inode
= new_dentry
->d_inode
;
8084 struct inode
*old_inode
= old_dentry
->d_inode
;
8085 struct timespec ctime
= CURRENT_TIME
;
8089 u64 old_ino
= btrfs_ino(old_inode
);
8091 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8094 /* we only allow rename subvolume link between subvolumes */
8095 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8098 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8099 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8102 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8103 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8107 /* check for collisions, even if the name isn't there */
8108 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8109 new_dentry
->d_name
.name
,
8110 new_dentry
->d_name
.len
);
8113 if (ret
== -EEXIST
) {
8115 * eexist without a new_inode */
8121 /* maybe -EOVERFLOW */
8128 * we're using rename to replace one file with another.
8129 * and the replacement file is large. Start IO on it now so
8130 * we don't add too much work to the end of the transaction
8132 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8133 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8134 filemap_flush(old_inode
->i_mapping
);
8136 /* close the racy window with snapshot create/destroy ioctl */
8137 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8138 down_read(&root
->fs_info
->subvol_sem
);
8140 * We want to reserve the absolute worst case amount of items. So if
8141 * both inodes are subvols and we need to unlink them then that would
8142 * require 4 item modifications, but if they are both normal inodes it
8143 * would require 5 item modifications, so we'll assume their normal
8144 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8145 * should cover the worst case number of items we'll modify.
8147 trans
= btrfs_start_transaction(root
, 11);
8148 if (IS_ERR(trans
)) {
8149 ret
= PTR_ERR(trans
);
8154 btrfs_record_root_in_trans(trans
, dest
);
8156 ret
= btrfs_set_inode_index(new_dir
, &index
);
8160 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8161 /* force full log commit if subvolume involved. */
8162 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8164 ret
= btrfs_insert_inode_ref(trans
, dest
,
8165 new_dentry
->d_name
.name
,
8166 new_dentry
->d_name
.len
,
8168 btrfs_ino(new_dir
), index
);
8172 * this is an ugly little race, but the rename is required
8173 * to make sure that if we crash, the inode is either at the
8174 * old name or the new one. pinning the log transaction lets
8175 * us make sure we don't allow a log commit to come in after
8176 * we unlink the name but before we add the new name back in.
8178 btrfs_pin_log_trans(root
);
8181 * make sure the inode gets flushed if it is replacing
8184 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8185 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8187 inode_inc_iversion(old_dir
);
8188 inode_inc_iversion(new_dir
);
8189 inode_inc_iversion(old_inode
);
8190 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8191 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8192 old_inode
->i_ctime
= ctime
;
8194 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8195 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8197 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8198 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8199 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8200 old_dentry
->d_name
.name
,
8201 old_dentry
->d_name
.len
);
8203 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8204 old_dentry
->d_inode
,
8205 old_dentry
->d_name
.name
,
8206 old_dentry
->d_name
.len
);
8208 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8211 btrfs_abort_transaction(trans
, root
, ret
);
8216 inode_inc_iversion(new_inode
);
8217 new_inode
->i_ctime
= CURRENT_TIME
;
8218 if (unlikely(btrfs_ino(new_inode
) ==
8219 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8220 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8221 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8223 new_dentry
->d_name
.name
,
8224 new_dentry
->d_name
.len
);
8225 BUG_ON(new_inode
->i_nlink
== 0);
8227 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8228 new_dentry
->d_inode
,
8229 new_dentry
->d_name
.name
,
8230 new_dentry
->d_name
.len
);
8232 if (!ret
&& new_inode
->i_nlink
== 0) {
8233 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8237 btrfs_abort_transaction(trans
, root
, ret
);
8242 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8243 new_dentry
->d_name
.name
,
8244 new_dentry
->d_name
.len
, 0, index
);
8246 btrfs_abort_transaction(trans
, root
, ret
);
8250 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8251 struct dentry
*parent
= new_dentry
->d_parent
;
8252 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8253 btrfs_end_log_trans(root
);
8256 btrfs_end_transaction(trans
, root
);
8258 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8259 up_read(&root
->fs_info
->subvol_sem
);
8264 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8266 struct btrfs_delalloc_work
*delalloc_work
;
8268 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8270 if (delalloc_work
->wait
)
8271 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8273 filemap_flush(delalloc_work
->inode
->i_mapping
);
8275 if (delalloc_work
->delay_iput
)
8276 btrfs_add_delayed_iput(delalloc_work
->inode
);
8278 iput(delalloc_work
->inode
);
8279 complete(&delalloc_work
->completion
);
8282 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8283 int wait
, int delay_iput
)
8285 struct btrfs_delalloc_work
*work
;
8287 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8291 init_completion(&work
->completion
);
8292 INIT_LIST_HEAD(&work
->list
);
8293 work
->inode
= inode
;
8295 work
->delay_iput
= delay_iput
;
8296 work
->work
.func
= btrfs_run_delalloc_work
;
8301 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8303 wait_for_completion(&work
->completion
);
8304 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8308 * some fairly slow code that needs optimization. This walks the list
8309 * of all the inodes with pending delalloc and forces them to disk.
8311 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8313 struct btrfs_inode
*binode
;
8314 struct inode
*inode
;
8315 struct btrfs_delalloc_work
*work
, *next
;
8316 struct list_head works
;
8317 struct list_head splice
;
8320 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8323 INIT_LIST_HEAD(&works
);
8324 INIT_LIST_HEAD(&splice
);
8326 spin_lock(&root
->fs_info
->delalloc_lock
);
8327 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
8328 while (!list_empty(&splice
)) {
8329 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8332 list_del_init(&binode
->delalloc_inodes
);
8334 inode
= igrab(&binode
->vfs_inode
);
8336 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
8337 &binode
->runtime_flags
);
8341 list_add_tail(&binode
->delalloc_inodes
,
8342 &root
->fs_info
->delalloc_inodes
);
8343 spin_unlock(&root
->fs_info
->delalloc_lock
);
8345 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8346 if (unlikely(!work
)) {
8350 list_add_tail(&work
->list
, &works
);
8351 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8355 spin_lock(&root
->fs_info
->delalloc_lock
);
8357 spin_unlock(&root
->fs_info
->delalloc_lock
);
8359 list_for_each_entry_safe(work
, next
, &works
, list
) {
8360 list_del_init(&work
->list
);
8361 btrfs_wait_and_free_delalloc_work(work
);
8364 /* the filemap_flush will queue IO into the worker threads, but
8365 * we have to make sure the IO is actually started and that
8366 * ordered extents get created before we return
8368 atomic_inc(&root
->fs_info
->async_submit_draining
);
8369 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8370 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8371 wait_event(root
->fs_info
->async_submit_wait
,
8372 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8373 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8375 atomic_dec(&root
->fs_info
->async_submit_draining
);
8378 list_for_each_entry_safe(work
, next
, &works
, list
) {
8379 list_del_init(&work
->list
);
8380 btrfs_wait_and_free_delalloc_work(work
);
8383 if (!list_empty_careful(&splice
)) {
8384 spin_lock(&root
->fs_info
->delalloc_lock
);
8385 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
8386 spin_unlock(&root
->fs_info
->delalloc_lock
);
8391 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8392 const char *symname
)
8394 struct btrfs_trans_handle
*trans
;
8395 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8396 struct btrfs_path
*path
;
8397 struct btrfs_key key
;
8398 struct inode
*inode
= NULL
;
8406 struct btrfs_file_extent_item
*ei
;
8407 struct extent_buffer
*leaf
;
8409 name_len
= strlen(symname
) + 1;
8410 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8411 return -ENAMETOOLONG
;
8414 * 2 items for inode item and ref
8415 * 2 items for dir items
8416 * 1 item for xattr if selinux is on
8418 trans
= btrfs_start_transaction(root
, 5);
8420 return PTR_ERR(trans
);
8422 err
= btrfs_find_free_ino(root
, &objectid
);
8426 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8427 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8428 S_IFLNK
|S_IRWXUGO
, &index
);
8429 if (IS_ERR(inode
)) {
8430 err
= PTR_ERR(inode
);
8434 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8441 * If the active LSM wants to access the inode during
8442 * d_instantiate it needs these. Smack checks to see
8443 * if the filesystem supports xattrs by looking at the
8446 inode
->i_fop
= &btrfs_file_operations
;
8447 inode
->i_op
= &btrfs_file_inode_operations
;
8449 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8453 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8454 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8455 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8460 path
= btrfs_alloc_path();
8466 key
.objectid
= btrfs_ino(inode
);
8468 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8469 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8470 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8474 btrfs_free_path(path
);
8477 leaf
= path
->nodes
[0];
8478 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8479 struct btrfs_file_extent_item
);
8480 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8481 btrfs_set_file_extent_type(leaf
, ei
,
8482 BTRFS_FILE_EXTENT_INLINE
);
8483 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8484 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8485 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8486 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8488 ptr
= btrfs_file_extent_inline_start(ei
);
8489 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8490 btrfs_mark_buffer_dirty(leaf
);
8491 btrfs_free_path(path
);
8493 inode
->i_op
= &btrfs_symlink_inode_operations
;
8494 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8495 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8496 inode_set_bytes(inode
, name_len
);
8497 btrfs_i_size_write(inode
, name_len
- 1);
8498 err
= btrfs_update_inode(trans
, root
, inode
);
8504 d_instantiate(dentry
, inode
);
8505 btrfs_end_transaction(trans
, root
);
8507 inode_dec_link_count(inode
);
8510 btrfs_btree_balance_dirty(root
);
8514 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8515 u64 start
, u64 num_bytes
, u64 min_size
,
8516 loff_t actual_len
, u64
*alloc_hint
,
8517 struct btrfs_trans_handle
*trans
)
8519 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8520 struct extent_map
*em
;
8521 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8522 struct btrfs_key ins
;
8523 u64 cur_offset
= start
;
8527 bool own_trans
= true;
8531 while (num_bytes
> 0) {
8533 trans
= btrfs_start_transaction(root
, 3);
8534 if (IS_ERR(trans
)) {
8535 ret
= PTR_ERR(trans
);
8540 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8541 cur_bytes
= max(cur_bytes
, min_size
);
8542 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8543 min_size
, 0, *alloc_hint
, &ins
, 1);
8546 btrfs_end_transaction(trans
, root
);
8550 ret
= insert_reserved_file_extent(trans
, inode
,
8551 cur_offset
, ins
.objectid
,
8552 ins
.offset
, ins
.offset
,
8553 ins
.offset
, 0, 0, 0,
8554 BTRFS_FILE_EXTENT_PREALLOC
);
8556 btrfs_abort_transaction(trans
, root
, ret
);
8558 btrfs_end_transaction(trans
, root
);
8561 btrfs_drop_extent_cache(inode
, cur_offset
,
8562 cur_offset
+ ins
.offset
-1, 0);
8564 em
= alloc_extent_map();
8566 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8567 &BTRFS_I(inode
)->runtime_flags
);
8571 em
->start
= cur_offset
;
8572 em
->orig_start
= cur_offset
;
8573 em
->len
= ins
.offset
;
8574 em
->block_start
= ins
.objectid
;
8575 em
->block_len
= ins
.offset
;
8576 em
->orig_block_len
= ins
.offset
;
8577 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8578 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8579 em
->generation
= trans
->transid
;
8582 write_lock(&em_tree
->lock
);
8583 ret
= add_extent_mapping(em_tree
, em
);
8585 list_move(&em
->list
,
8586 &em_tree
->modified_extents
);
8587 write_unlock(&em_tree
->lock
);
8590 btrfs_drop_extent_cache(inode
, cur_offset
,
8591 cur_offset
+ ins
.offset
- 1,
8594 free_extent_map(em
);
8596 num_bytes
-= ins
.offset
;
8597 cur_offset
+= ins
.offset
;
8598 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8600 inode_inc_iversion(inode
);
8601 inode
->i_ctime
= CURRENT_TIME
;
8602 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8603 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8604 (actual_len
> inode
->i_size
) &&
8605 (cur_offset
> inode
->i_size
)) {
8606 if (cur_offset
> actual_len
)
8607 i_size
= actual_len
;
8609 i_size
= cur_offset
;
8610 i_size_write(inode
, i_size
);
8611 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8614 ret
= btrfs_update_inode(trans
, root
, inode
);
8617 btrfs_abort_transaction(trans
, root
, ret
);
8619 btrfs_end_transaction(trans
, root
);
8624 btrfs_end_transaction(trans
, root
);
8629 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8630 u64 start
, u64 num_bytes
, u64 min_size
,
8631 loff_t actual_len
, u64
*alloc_hint
)
8633 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8634 min_size
, actual_len
, alloc_hint
,
8638 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8639 struct btrfs_trans_handle
*trans
, int mode
,
8640 u64 start
, u64 num_bytes
, u64 min_size
,
8641 loff_t actual_len
, u64
*alloc_hint
)
8643 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8644 min_size
, actual_len
, alloc_hint
, trans
);
8647 static int btrfs_set_page_dirty(struct page
*page
)
8649 return __set_page_dirty_nobuffers(page
);
8652 static int btrfs_permission(struct inode
*inode
, int mask
)
8654 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8655 umode_t mode
= inode
->i_mode
;
8657 if (mask
& MAY_WRITE
&&
8658 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8659 if (btrfs_root_readonly(root
))
8661 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8664 return generic_permission(inode
, mask
);
8667 static const struct inode_operations btrfs_dir_inode_operations
= {
8668 .getattr
= btrfs_getattr
,
8669 .lookup
= btrfs_lookup
,
8670 .create
= btrfs_create
,
8671 .unlink
= btrfs_unlink
,
8673 .mkdir
= btrfs_mkdir
,
8674 .rmdir
= btrfs_rmdir
,
8675 .rename
= btrfs_rename
,
8676 .symlink
= btrfs_symlink
,
8677 .setattr
= btrfs_setattr
,
8678 .mknod
= btrfs_mknod
,
8679 .setxattr
= btrfs_setxattr
,
8680 .getxattr
= btrfs_getxattr
,
8681 .listxattr
= btrfs_listxattr
,
8682 .removexattr
= btrfs_removexattr
,
8683 .permission
= btrfs_permission
,
8684 .get_acl
= btrfs_get_acl
,
8686 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8687 .lookup
= btrfs_lookup
,
8688 .permission
= btrfs_permission
,
8689 .get_acl
= btrfs_get_acl
,
8692 static const struct file_operations btrfs_dir_file_operations
= {
8693 .llseek
= generic_file_llseek
,
8694 .read
= generic_read_dir
,
8695 .readdir
= btrfs_real_readdir
,
8696 .unlocked_ioctl
= btrfs_ioctl
,
8697 #ifdef CONFIG_COMPAT
8698 .compat_ioctl
= btrfs_ioctl
,
8700 .release
= btrfs_release_file
,
8701 .fsync
= btrfs_sync_file
,
8704 static struct extent_io_ops btrfs_extent_io_ops
= {
8705 .fill_delalloc
= run_delalloc_range
,
8706 .submit_bio_hook
= btrfs_submit_bio_hook
,
8707 .merge_bio_hook
= btrfs_merge_bio_hook
,
8708 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8709 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8710 .writepage_start_hook
= btrfs_writepage_start_hook
,
8711 .set_bit_hook
= btrfs_set_bit_hook
,
8712 .clear_bit_hook
= btrfs_clear_bit_hook
,
8713 .merge_extent_hook
= btrfs_merge_extent_hook
,
8714 .split_extent_hook
= btrfs_split_extent_hook
,
8718 * btrfs doesn't support the bmap operation because swapfiles
8719 * use bmap to make a mapping of extents in the file. They assume
8720 * these extents won't change over the life of the file and they
8721 * use the bmap result to do IO directly to the drive.
8723 * the btrfs bmap call would return logical addresses that aren't
8724 * suitable for IO and they also will change frequently as COW
8725 * operations happen. So, swapfile + btrfs == corruption.
8727 * For now we're avoiding this by dropping bmap.
8729 static const struct address_space_operations btrfs_aops
= {
8730 .readpage
= btrfs_readpage
,
8731 .writepage
= btrfs_writepage
,
8732 .writepages
= btrfs_writepages
,
8733 .readpages
= btrfs_readpages
,
8734 .direct_IO
= btrfs_direct_IO
,
8735 .invalidatepage
= btrfs_invalidatepage
,
8736 .releasepage
= btrfs_releasepage
,
8737 .set_page_dirty
= btrfs_set_page_dirty
,
8738 .error_remove_page
= generic_error_remove_page
,
8741 static const struct address_space_operations btrfs_symlink_aops
= {
8742 .readpage
= btrfs_readpage
,
8743 .writepage
= btrfs_writepage
,
8744 .invalidatepage
= btrfs_invalidatepage
,
8745 .releasepage
= btrfs_releasepage
,
8748 static const struct inode_operations btrfs_file_inode_operations
= {
8749 .getattr
= btrfs_getattr
,
8750 .setattr
= btrfs_setattr
,
8751 .setxattr
= btrfs_setxattr
,
8752 .getxattr
= btrfs_getxattr
,
8753 .listxattr
= btrfs_listxattr
,
8754 .removexattr
= btrfs_removexattr
,
8755 .permission
= btrfs_permission
,
8756 .fiemap
= btrfs_fiemap
,
8757 .get_acl
= btrfs_get_acl
,
8758 .update_time
= btrfs_update_time
,
8760 static const struct inode_operations btrfs_special_inode_operations
= {
8761 .getattr
= btrfs_getattr
,
8762 .setattr
= btrfs_setattr
,
8763 .permission
= btrfs_permission
,
8764 .setxattr
= btrfs_setxattr
,
8765 .getxattr
= btrfs_getxattr
,
8766 .listxattr
= btrfs_listxattr
,
8767 .removexattr
= btrfs_removexattr
,
8768 .get_acl
= btrfs_get_acl
,
8769 .update_time
= btrfs_update_time
,
8771 static const struct inode_operations btrfs_symlink_inode_operations
= {
8772 .readlink
= generic_readlink
,
8773 .follow_link
= page_follow_link_light
,
8774 .put_link
= page_put_link
,
8775 .getattr
= btrfs_getattr
,
8776 .setattr
= btrfs_setattr
,
8777 .permission
= btrfs_permission
,
8778 .setxattr
= btrfs_setxattr
,
8779 .getxattr
= btrfs_getxattr
,
8780 .listxattr
= btrfs_listxattr
,
8781 .removexattr
= btrfs_removexattr
,
8782 .get_acl
= btrfs_get_acl
,
8783 .update_time
= btrfs_update_time
,
8786 const struct dentry_operations btrfs_dentry_operations
= {
8787 .d_delete
= btrfs_dentry_delete
,
8788 .d_release
= btrfs_dentry_release
,