2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
60 struct btrfs_iget_args
{
62 struct btrfs_root
*root
;
65 static const struct inode_operations btrfs_dir_inode_operations
;
66 static const struct inode_operations btrfs_symlink_inode_operations
;
67 static const struct inode_operations btrfs_dir_ro_inode_operations
;
68 static const struct inode_operations btrfs_special_inode_operations
;
69 static const struct inode_operations btrfs_file_inode_operations
;
70 static const struct address_space_operations btrfs_aops
;
71 static const struct address_space_operations btrfs_symlink_aops
;
72 static const struct file_operations btrfs_dir_file_operations
;
73 static struct extent_io_ops btrfs_extent_io_ops
;
75 static struct kmem_cache
*btrfs_inode_cachep
;
76 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
77 struct kmem_cache
*btrfs_trans_handle_cachep
;
78 struct kmem_cache
*btrfs_transaction_cachep
;
79 struct kmem_cache
*btrfs_path_cachep
;
80 struct kmem_cache
*btrfs_free_space_cachep
;
83 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
84 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
85 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
86 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
87 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
88 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
89 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
90 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
93 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
94 static int btrfs_truncate(struct inode
*inode
);
95 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
96 static noinline
int cow_file_range(struct inode
*inode
,
97 struct page
*locked_page
,
98 u64 start
, u64 end
, int *page_started
,
99 unsigned long *nr_written
, int unlock
);
100 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
101 u64 len
, u64 orig_start
,
102 u64 block_start
, u64 block_len
,
103 u64 orig_block_len
, u64 ram_bytes
,
106 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
107 struct inode
*inode
, struct inode
*dir
,
108 const struct qstr
*qstr
)
112 err
= btrfs_init_acl(trans
, inode
, dir
);
114 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
119 * this does all the hard work for inserting an inline extent into
120 * the btree. The caller should have done a btrfs_drop_extents so that
121 * no overlapping inline items exist in the btree
123 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
124 struct btrfs_root
*root
, struct inode
*inode
,
125 u64 start
, size_t size
, size_t compressed_size
,
127 struct page
**compressed_pages
)
129 struct btrfs_key key
;
130 struct btrfs_path
*path
;
131 struct extent_buffer
*leaf
;
132 struct page
*page
= NULL
;
135 struct btrfs_file_extent_item
*ei
;
138 size_t cur_size
= size
;
140 unsigned long offset
;
142 if (compressed_size
&& compressed_pages
)
143 cur_size
= compressed_size
;
145 path
= btrfs_alloc_path();
149 path
->leave_spinning
= 1;
151 key
.objectid
= btrfs_ino(inode
);
153 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
154 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
156 inode_add_bytes(inode
, size
);
157 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
163 leaf
= path
->nodes
[0];
164 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
165 struct btrfs_file_extent_item
);
166 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
167 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
168 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
169 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
170 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
171 ptr
= btrfs_file_extent_inline_start(ei
);
173 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
176 while (compressed_size
> 0) {
177 cpage
= compressed_pages
[i
];
178 cur_size
= min_t(unsigned long, compressed_size
,
181 kaddr
= kmap_atomic(cpage
);
182 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
183 kunmap_atomic(kaddr
);
187 compressed_size
-= cur_size
;
189 btrfs_set_file_extent_compression(leaf
, ei
,
192 page
= find_get_page(inode
->i_mapping
,
193 start
>> PAGE_CACHE_SHIFT
);
194 btrfs_set_file_extent_compression(leaf
, ei
, 0);
195 kaddr
= kmap_atomic(page
);
196 offset
= start
& (PAGE_CACHE_SIZE
- 1);
197 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
198 kunmap_atomic(kaddr
);
199 page_cache_release(page
);
201 btrfs_mark_buffer_dirty(leaf
);
202 btrfs_free_path(path
);
205 * we're an inline extent, so nobody can
206 * extend the file past i_size without locking
207 * a page we already have locked.
209 * We must do any isize and inode updates
210 * before we unlock the pages. Otherwise we
211 * could end up racing with unlink.
213 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
214 ret
= btrfs_update_inode(trans
, root
, inode
);
218 btrfs_free_path(path
);
224 * conditionally insert an inline extent into the file. This
225 * does the checks required to make sure the data is small enough
226 * to fit as an inline extent.
228 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
229 struct btrfs_root
*root
,
230 struct inode
*inode
, u64 start
, u64 end
,
231 size_t compressed_size
, int compress_type
,
232 struct page
**compressed_pages
)
234 u64 isize
= i_size_read(inode
);
235 u64 actual_end
= min(end
+ 1, isize
);
236 u64 inline_len
= actual_end
- start
;
237 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
238 u64 data_len
= inline_len
;
242 data_len
= compressed_size
;
245 actual_end
>= PAGE_CACHE_SIZE
||
246 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
248 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
250 data_len
> root
->fs_info
->max_inline
) {
254 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
258 if (isize
> actual_end
)
259 inline_len
= min_t(u64
, isize
, actual_end
);
260 ret
= insert_inline_extent(trans
, root
, inode
, start
,
261 inline_len
, compressed_size
,
262 compress_type
, compressed_pages
);
263 if (ret
&& ret
!= -ENOSPC
) {
264 btrfs_abort_transaction(trans
, root
, ret
);
266 } else if (ret
== -ENOSPC
) {
270 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
271 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
272 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
276 struct async_extent
{
281 unsigned long nr_pages
;
283 struct list_head list
;
288 struct btrfs_root
*root
;
289 struct page
*locked_page
;
292 struct list_head extents
;
293 struct btrfs_work work
;
296 static noinline
int add_async_extent(struct async_cow
*cow
,
297 u64 start
, u64 ram_size
,
300 unsigned long nr_pages
,
303 struct async_extent
*async_extent
;
305 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
306 BUG_ON(!async_extent
); /* -ENOMEM */
307 async_extent
->start
= start
;
308 async_extent
->ram_size
= ram_size
;
309 async_extent
->compressed_size
= compressed_size
;
310 async_extent
->pages
= pages
;
311 async_extent
->nr_pages
= nr_pages
;
312 async_extent
->compress_type
= compress_type
;
313 list_add_tail(&async_extent
->list
, &cow
->extents
);
318 * we create compressed extents in two phases. The first
319 * phase compresses a range of pages that have already been
320 * locked (both pages and state bits are locked).
322 * This is done inside an ordered work queue, and the compression
323 * is spread across many cpus. The actual IO submission is step
324 * two, and the ordered work queue takes care of making sure that
325 * happens in the same order things were put onto the queue by
326 * writepages and friends.
328 * If this code finds it can't get good compression, it puts an
329 * entry onto the work queue to write the uncompressed bytes. This
330 * makes sure that both compressed inodes and uncompressed inodes
331 * are written in the same order that the flusher thread sent them
334 static noinline
int compress_file_range(struct inode
*inode
,
335 struct page
*locked_page
,
337 struct async_cow
*async_cow
,
340 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
341 struct btrfs_trans_handle
*trans
;
343 u64 blocksize
= root
->sectorsize
;
345 u64 isize
= i_size_read(inode
);
347 struct page
**pages
= NULL
;
348 unsigned long nr_pages
;
349 unsigned long nr_pages_ret
= 0;
350 unsigned long total_compressed
= 0;
351 unsigned long total_in
= 0;
352 unsigned long max_compressed
= 128 * 1024;
353 unsigned long max_uncompressed
= 128 * 1024;
356 int compress_type
= root
->fs_info
->compress_type
;
359 /* if this is a small write inside eof, kick off a defrag */
360 if ((end
- start
+ 1) < 16 * 1024 &&
361 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
362 btrfs_add_inode_defrag(NULL
, inode
);
364 actual_end
= min_t(u64
, isize
, end
+ 1);
367 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
368 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
371 * we don't want to send crud past the end of i_size through
372 * compression, that's just a waste of CPU time. So, if the
373 * end of the file is before the start of our current
374 * requested range of bytes, we bail out to the uncompressed
375 * cleanup code that can deal with all of this.
377 * It isn't really the fastest way to fix things, but this is a
378 * very uncommon corner.
380 if (actual_end
<= start
)
381 goto cleanup_and_bail_uncompressed
;
383 total_compressed
= actual_end
- start
;
385 /* we want to make sure that amount of ram required to uncompress
386 * an extent is reasonable, so we limit the total size in ram
387 * of a compressed extent to 128k. This is a crucial number
388 * because it also controls how easily we can spread reads across
389 * cpus for decompression.
391 * We also want to make sure the amount of IO required to do
392 * a random read is reasonably small, so we limit the size of
393 * a compressed extent to 128k.
395 total_compressed
= min(total_compressed
, max_uncompressed
);
396 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
397 num_bytes
= max(blocksize
, num_bytes
);
402 * we do compression for mount -o compress and when the
403 * inode has not been flagged as nocompress. This flag can
404 * change at any time if we discover bad compression ratios.
406 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
407 (btrfs_test_opt(root
, COMPRESS
) ||
408 (BTRFS_I(inode
)->force_compress
) ||
409 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
411 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
413 /* just bail out to the uncompressed code */
417 if (BTRFS_I(inode
)->force_compress
)
418 compress_type
= BTRFS_I(inode
)->force_compress
;
421 * we need to call clear_page_dirty_for_io on each
422 * page in the range. Otherwise applications with the file
423 * mmap'd can wander in and change the page contents while
424 * we are compressing them.
426 * If the compression fails for any reason, we set the pages
427 * dirty again later on.
429 extent_range_clear_dirty_for_io(inode
, start
, end
);
431 ret
= btrfs_compress_pages(compress_type
,
432 inode
->i_mapping
, start
,
433 total_compressed
, pages
,
434 nr_pages
, &nr_pages_ret
,
440 unsigned long offset
= total_compressed
&
441 (PAGE_CACHE_SIZE
- 1);
442 struct page
*page
= pages
[nr_pages_ret
- 1];
445 /* zero the tail end of the last page, we might be
446 * sending it down to disk
449 kaddr
= kmap_atomic(page
);
450 memset(kaddr
+ offset
, 0,
451 PAGE_CACHE_SIZE
- offset
);
452 kunmap_atomic(kaddr
);
459 trans
= btrfs_join_transaction(root
);
461 ret
= PTR_ERR(trans
);
463 goto cleanup_and_out
;
465 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
467 /* lets try to make an inline extent */
468 if (ret
|| total_in
< (actual_end
- start
)) {
469 /* we didn't compress the entire range, try
470 * to make an uncompressed inline extent.
472 ret
= cow_file_range_inline(trans
, root
, inode
,
473 start
, end
, 0, 0, NULL
);
475 /* try making a compressed inline extent */
476 ret
= cow_file_range_inline(trans
, root
, inode
,
479 compress_type
, pages
);
483 * inline extent creation worked or returned error,
484 * we don't need to create any more async work items.
485 * Unlock and free up our temp pages.
487 extent_clear_unlock_delalloc(inode
,
488 &BTRFS_I(inode
)->io_tree
,
490 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
491 EXTENT_CLEAR_DELALLOC
|
492 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
494 btrfs_end_transaction(trans
, root
);
497 btrfs_end_transaction(trans
, root
);
502 * we aren't doing an inline extent round the compressed size
503 * up to a block size boundary so the allocator does sane
506 total_compressed
= ALIGN(total_compressed
, blocksize
);
509 * one last check to make sure the compression is really a
510 * win, compare the page count read with the blocks on disk
512 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
513 if (total_compressed
>= total_in
) {
516 num_bytes
= total_in
;
519 if (!will_compress
&& pages
) {
521 * the compression code ran but failed to make things smaller,
522 * free any pages it allocated and our page pointer array
524 for (i
= 0; i
< nr_pages_ret
; i
++) {
525 WARN_ON(pages
[i
]->mapping
);
526 page_cache_release(pages
[i
]);
530 total_compressed
= 0;
533 /* flag the file so we don't compress in the future */
534 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
535 !(BTRFS_I(inode
)->force_compress
)) {
536 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
542 /* the async work queues will take care of doing actual
543 * allocation on disk for these compressed pages,
544 * and will submit them to the elevator.
546 add_async_extent(async_cow
, start
, num_bytes
,
547 total_compressed
, pages
, nr_pages_ret
,
550 if (start
+ num_bytes
< end
) {
557 cleanup_and_bail_uncompressed
:
559 * No compression, but we still need to write the pages in
560 * the file we've been given so far. redirty the locked
561 * page if it corresponds to our extent and set things up
562 * for the async work queue to run cow_file_range to do
563 * the normal delalloc dance
565 if (page_offset(locked_page
) >= start
&&
566 page_offset(locked_page
) <= end
) {
567 __set_page_dirty_nobuffers(locked_page
);
568 /* unlocked later on in the async handlers */
571 extent_range_redirty_for_io(inode
, start
, end
);
572 add_async_extent(async_cow
, start
, end
- start
+ 1,
573 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
581 for (i
= 0; i
< nr_pages_ret
; i
++) {
582 WARN_ON(pages
[i
]->mapping
);
583 page_cache_release(pages
[i
]);
590 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
592 EXTENT_CLEAR_UNLOCK_PAGE
|
594 EXTENT_CLEAR_DELALLOC
|
595 EXTENT_SET_WRITEBACK
|
596 EXTENT_END_WRITEBACK
);
597 if (!trans
|| IS_ERR(trans
))
598 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
600 btrfs_abort_transaction(trans
, root
, ret
);
605 * phase two of compressed writeback. This is the ordered portion
606 * of the code, which only gets called in the order the work was
607 * queued. We walk all the async extents created by compress_file_range
608 * and send them down to the disk.
610 static noinline
int submit_compressed_extents(struct inode
*inode
,
611 struct async_cow
*async_cow
)
613 struct async_extent
*async_extent
;
615 struct btrfs_trans_handle
*trans
;
616 struct btrfs_key ins
;
617 struct extent_map
*em
;
618 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
619 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
620 struct extent_io_tree
*io_tree
;
623 if (list_empty(&async_cow
->extents
))
627 while (!list_empty(&async_cow
->extents
)) {
628 async_extent
= list_entry(async_cow
->extents
.next
,
629 struct async_extent
, list
);
630 list_del(&async_extent
->list
);
632 io_tree
= &BTRFS_I(inode
)->io_tree
;
635 /* did the compression code fall back to uncompressed IO? */
636 if (!async_extent
->pages
) {
637 int page_started
= 0;
638 unsigned long nr_written
= 0;
640 lock_extent(io_tree
, async_extent
->start
,
641 async_extent
->start
+
642 async_extent
->ram_size
- 1);
644 /* allocate blocks */
645 ret
= cow_file_range(inode
, async_cow
->locked_page
,
647 async_extent
->start
+
648 async_extent
->ram_size
- 1,
649 &page_started
, &nr_written
, 0);
654 * if page_started, cow_file_range inserted an
655 * inline extent and took care of all the unlocking
656 * and IO for us. Otherwise, we need to submit
657 * all those pages down to the drive.
659 if (!page_started
&& !ret
)
660 extent_write_locked_range(io_tree
,
661 inode
, async_extent
->start
,
662 async_extent
->start
+
663 async_extent
->ram_size
- 1,
667 unlock_page(async_cow
->locked_page
);
673 lock_extent(io_tree
, async_extent
->start
,
674 async_extent
->start
+ async_extent
->ram_size
- 1);
676 trans
= btrfs_join_transaction(root
);
678 ret
= PTR_ERR(trans
);
680 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
681 ret
= btrfs_reserve_extent(trans
, root
,
682 async_extent
->compressed_size
,
683 async_extent
->compressed_size
,
684 0, alloc_hint
, &ins
, 1);
685 if (ret
&& ret
!= -ENOSPC
)
686 btrfs_abort_transaction(trans
, root
, ret
);
687 btrfs_end_transaction(trans
, root
);
693 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
694 WARN_ON(async_extent
->pages
[i
]->mapping
);
695 page_cache_release(async_extent
->pages
[i
]);
697 kfree(async_extent
->pages
);
698 async_extent
->nr_pages
= 0;
699 async_extent
->pages
= NULL
;
707 * here we're doing allocation and writeback of the
710 btrfs_drop_extent_cache(inode
, async_extent
->start
,
711 async_extent
->start
+
712 async_extent
->ram_size
- 1, 0);
714 em
= alloc_extent_map();
716 goto out_free_reserve
;
717 em
->start
= async_extent
->start
;
718 em
->len
= async_extent
->ram_size
;
719 em
->orig_start
= em
->start
;
720 em
->mod_start
= em
->start
;
721 em
->mod_len
= em
->len
;
723 em
->block_start
= ins
.objectid
;
724 em
->block_len
= ins
.offset
;
725 em
->orig_block_len
= ins
.offset
;
726 em
->ram_bytes
= async_extent
->ram_size
;
727 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
728 em
->compress_type
= async_extent
->compress_type
;
729 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
730 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
734 write_lock(&em_tree
->lock
);
735 ret
= add_extent_mapping(em_tree
, em
, 1);
736 write_unlock(&em_tree
->lock
);
737 if (ret
!= -EEXIST
) {
741 btrfs_drop_extent_cache(inode
, async_extent
->start
,
742 async_extent
->start
+
743 async_extent
->ram_size
- 1, 0);
747 goto out_free_reserve
;
749 ret
= btrfs_add_ordered_extent_compress(inode
,
752 async_extent
->ram_size
,
754 BTRFS_ORDERED_COMPRESSED
,
755 async_extent
->compress_type
);
757 goto out_free_reserve
;
760 * clear dirty, set writeback and unlock the pages.
762 extent_clear_unlock_delalloc(inode
,
763 &BTRFS_I(inode
)->io_tree
,
765 async_extent
->start
+
766 async_extent
->ram_size
- 1,
767 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
768 EXTENT_CLEAR_UNLOCK
|
769 EXTENT_CLEAR_DELALLOC
|
770 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
772 ret
= btrfs_submit_compressed_write(inode
,
774 async_extent
->ram_size
,
776 ins
.offset
, async_extent
->pages
,
777 async_extent
->nr_pages
);
778 alloc_hint
= ins
.objectid
+ ins
.offset
;
788 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
790 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
792 async_extent
->start
+
793 async_extent
->ram_size
- 1,
794 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
795 EXTENT_CLEAR_UNLOCK
|
796 EXTENT_CLEAR_DELALLOC
|
798 EXTENT_SET_WRITEBACK
|
799 EXTENT_END_WRITEBACK
);
804 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
807 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
808 struct extent_map
*em
;
811 read_lock(&em_tree
->lock
);
812 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
815 * if block start isn't an actual block number then find the
816 * first block in this inode and use that as a hint. If that
817 * block is also bogus then just don't worry about it.
819 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
821 em
= search_extent_mapping(em_tree
, 0, 0);
822 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
823 alloc_hint
= em
->block_start
;
827 alloc_hint
= em
->block_start
;
831 read_unlock(&em_tree
->lock
);
837 * when extent_io.c finds a delayed allocation range in the file,
838 * the call backs end up in this code. The basic idea is to
839 * allocate extents on disk for the range, and create ordered data structs
840 * in ram to track those extents.
842 * locked_page is the page that writepage had locked already. We use
843 * it to make sure we don't do extra locks or unlocks.
845 * *page_started is set to one if we unlock locked_page and do everything
846 * required to start IO on it. It may be clean and already done with
849 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
851 struct btrfs_root
*root
,
852 struct page
*locked_page
,
853 u64 start
, u64 end
, int *page_started
,
854 unsigned long *nr_written
,
859 unsigned long ram_size
;
862 u64 blocksize
= root
->sectorsize
;
863 struct btrfs_key ins
;
864 struct extent_map
*em
;
865 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
868 BUG_ON(btrfs_is_free_space_inode(inode
));
870 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
871 num_bytes
= max(blocksize
, num_bytes
);
872 disk_num_bytes
= num_bytes
;
874 /* if this is a small write inside eof, kick off defrag */
875 if (num_bytes
< 64 * 1024 &&
876 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
877 btrfs_add_inode_defrag(trans
, inode
);
880 /* lets try to make an inline extent */
881 ret
= cow_file_range_inline(trans
, root
, inode
,
882 start
, end
, 0, 0, NULL
);
884 extent_clear_unlock_delalloc(inode
,
885 &BTRFS_I(inode
)->io_tree
,
887 EXTENT_CLEAR_UNLOCK_PAGE
|
888 EXTENT_CLEAR_UNLOCK
|
889 EXTENT_CLEAR_DELALLOC
|
891 EXTENT_SET_WRITEBACK
|
892 EXTENT_END_WRITEBACK
);
894 *nr_written
= *nr_written
+
895 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
898 } else if (ret
< 0) {
899 btrfs_abort_transaction(trans
, root
, ret
);
904 BUG_ON(disk_num_bytes
>
905 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
907 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
908 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
910 while (disk_num_bytes
> 0) {
913 cur_alloc_size
= disk_num_bytes
;
914 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
915 root
->sectorsize
, 0, alloc_hint
,
918 btrfs_abort_transaction(trans
, root
, ret
);
922 em
= alloc_extent_map();
923 BUG_ON(!em
); /* -ENOMEM */
925 em
->orig_start
= em
->start
;
926 ram_size
= ins
.offset
;
927 em
->len
= ins
.offset
;
928 em
->mod_start
= em
->start
;
929 em
->mod_len
= em
->len
;
931 em
->block_start
= ins
.objectid
;
932 em
->block_len
= ins
.offset
;
933 em
->orig_block_len
= ins
.offset
;
934 em
->ram_bytes
= ram_size
;
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
, 1);
942 write_unlock(&em_tree
->lock
);
943 if (ret
!= -EEXIST
) {
947 btrfs_drop_extent_cache(inode
, start
,
948 start
+ ram_size
- 1, 0);
951 cur_alloc_size
= ins
.offset
;
952 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
953 ram_size
, cur_alloc_size
, 0);
954 BUG_ON(ret
); /* -ENOMEM */
956 if (root
->root_key
.objectid
==
957 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
958 ret
= btrfs_reloc_clone_csums(inode
, start
,
961 btrfs_abort_transaction(trans
, root
, ret
);
966 if (disk_num_bytes
< cur_alloc_size
)
969 /* we're not doing compressed IO, don't unlock the first
970 * page (which the caller expects to stay locked), don't
971 * clear any dirty bits and don't set any writeback bits
973 * Do set the Private2 bit so we know this page was properly
974 * setup for writepage
976 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
977 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
980 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
981 start
, start
+ ram_size
- 1,
983 disk_num_bytes
-= cur_alloc_size
;
984 num_bytes
-= cur_alloc_size
;
985 alloc_hint
= ins
.objectid
+ ins
.offset
;
986 start
+= cur_alloc_size
;
992 extent_clear_unlock_delalloc(inode
,
993 &BTRFS_I(inode
)->io_tree
,
994 start
, end
, locked_page
,
995 EXTENT_CLEAR_UNLOCK_PAGE
|
996 EXTENT_CLEAR_UNLOCK
|
997 EXTENT_CLEAR_DELALLOC
|
999 EXTENT_SET_WRITEBACK
|
1000 EXTENT_END_WRITEBACK
);
1005 static noinline
int cow_file_range(struct inode
*inode
,
1006 struct page
*locked_page
,
1007 u64 start
, u64 end
, int *page_started
,
1008 unsigned long *nr_written
,
1011 struct btrfs_trans_handle
*trans
;
1012 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1015 trans
= btrfs_join_transaction(root
);
1016 if (IS_ERR(trans
)) {
1017 extent_clear_unlock_delalloc(inode
,
1018 &BTRFS_I(inode
)->io_tree
,
1019 start
, end
, locked_page
,
1020 EXTENT_CLEAR_UNLOCK_PAGE
|
1021 EXTENT_CLEAR_UNLOCK
|
1022 EXTENT_CLEAR_DELALLOC
|
1023 EXTENT_CLEAR_DIRTY
|
1024 EXTENT_SET_WRITEBACK
|
1025 EXTENT_END_WRITEBACK
);
1026 return PTR_ERR(trans
);
1028 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1030 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1031 page_started
, nr_written
, unlock
);
1033 btrfs_end_transaction(trans
, root
);
1039 * work queue call back to started compression on a file and pages
1041 static noinline
void async_cow_start(struct btrfs_work
*work
)
1043 struct async_cow
*async_cow
;
1045 async_cow
= container_of(work
, struct async_cow
, work
);
1047 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1048 async_cow
->start
, async_cow
->end
, async_cow
,
1050 if (num_added
== 0) {
1051 btrfs_add_delayed_iput(async_cow
->inode
);
1052 async_cow
->inode
= NULL
;
1057 * work queue call back to submit previously compressed pages
1059 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1061 struct async_cow
*async_cow
;
1062 struct btrfs_root
*root
;
1063 unsigned long nr_pages
;
1065 async_cow
= container_of(work
, struct async_cow
, work
);
1067 root
= async_cow
->root
;
1068 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1071 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1073 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1074 wake_up(&root
->fs_info
->async_submit_wait
);
1076 if (async_cow
->inode
)
1077 submit_compressed_extents(async_cow
->inode
, async_cow
);
1080 static noinline
void async_cow_free(struct btrfs_work
*work
)
1082 struct async_cow
*async_cow
;
1083 async_cow
= container_of(work
, struct async_cow
, work
);
1084 if (async_cow
->inode
)
1085 btrfs_add_delayed_iput(async_cow
->inode
);
1089 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1090 u64 start
, u64 end
, int *page_started
,
1091 unsigned long *nr_written
)
1093 struct async_cow
*async_cow
;
1094 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1095 unsigned long nr_pages
;
1097 int limit
= 10 * 1024 * 1024;
1099 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1100 1, 0, NULL
, GFP_NOFS
);
1101 while (start
< end
) {
1102 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1103 BUG_ON(!async_cow
); /* -ENOMEM */
1104 async_cow
->inode
= igrab(inode
);
1105 async_cow
->root
= root
;
1106 async_cow
->locked_page
= locked_page
;
1107 async_cow
->start
= start
;
1109 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1112 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1114 async_cow
->end
= cur_end
;
1115 INIT_LIST_HEAD(&async_cow
->extents
);
1117 async_cow
->work
.func
= async_cow_start
;
1118 async_cow
->work
.ordered_func
= async_cow_submit
;
1119 async_cow
->work
.ordered_free
= async_cow_free
;
1120 async_cow
->work
.flags
= 0;
1122 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1124 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1126 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1129 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1130 wait_event(root
->fs_info
->async_submit_wait
,
1131 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1135 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1136 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1137 wait_event(root
->fs_info
->async_submit_wait
,
1138 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1142 *nr_written
+= nr_pages
;
1143 start
= cur_end
+ 1;
1149 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1150 u64 bytenr
, u64 num_bytes
)
1153 struct btrfs_ordered_sum
*sums
;
1156 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1157 bytenr
+ num_bytes
- 1, &list
, 0);
1158 if (ret
== 0 && list_empty(&list
))
1161 while (!list_empty(&list
)) {
1162 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1163 list_del(&sums
->list
);
1170 * when nowcow writeback call back. This checks for snapshots or COW copies
1171 * of the extents that exist in the file, and COWs the file as required.
1173 * If no cow copies or snapshots exist, we write directly to the existing
1176 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1177 struct page
*locked_page
,
1178 u64 start
, u64 end
, int *page_started
, int force
,
1179 unsigned long *nr_written
)
1181 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1182 struct btrfs_trans_handle
*trans
;
1183 struct extent_buffer
*leaf
;
1184 struct btrfs_path
*path
;
1185 struct btrfs_file_extent_item
*fi
;
1186 struct btrfs_key found_key
;
1201 u64 ino
= btrfs_ino(inode
);
1203 path
= btrfs_alloc_path();
1205 extent_clear_unlock_delalloc(inode
,
1206 &BTRFS_I(inode
)->io_tree
,
1207 start
, end
, locked_page
,
1208 EXTENT_CLEAR_UNLOCK_PAGE
|
1209 EXTENT_CLEAR_UNLOCK
|
1210 EXTENT_CLEAR_DELALLOC
|
1211 EXTENT_CLEAR_DIRTY
|
1212 EXTENT_SET_WRITEBACK
|
1213 EXTENT_END_WRITEBACK
);
1217 nolock
= btrfs_is_free_space_inode(inode
);
1220 trans
= btrfs_join_transaction_nolock(root
);
1222 trans
= btrfs_join_transaction(root
);
1224 if (IS_ERR(trans
)) {
1225 extent_clear_unlock_delalloc(inode
,
1226 &BTRFS_I(inode
)->io_tree
,
1227 start
, end
, locked_page
,
1228 EXTENT_CLEAR_UNLOCK_PAGE
|
1229 EXTENT_CLEAR_UNLOCK
|
1230 EXTENT_CLEAR_DELALLOC
|
1231 EXTENT_CLEAR_DIRTY
|
1232 EXTENT_SET_WRITEBACK
|
1233 EXTENT_END_WRITEBACK
);
1234 btrfs_free_path(path
);
1235 return PTR_ERR(trans
);
1238 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1240 cow_start
= (u64
)-1;
1243 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1246 btrfs_abort_transaction(trans
, root
, ret
);
1249 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1250 leaf
= path
->nodes
[0];
1251 btrfs_item_key_to_cpu(leaf
, &found_key
,
1252 path
->slots
[0] - 1);
1253 if (found_key
.objectid
== ino
&&
1254 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1259 leaf
= path
->nodes
[0];
1260 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1261 ret
= btrfs_next_leaf(root
, path
);
1263 btrfs_abort_transaction(trans
, root
, ret
);
1268 leaf
= path
->nodes
[0];
1274 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1276 if (found_key
.objectid
> ino
||
1277 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1278 found_key
.offset
> end
)
1281 if (found_key
.offset
> cur_offset
) {
1282 extent_end
= found_key
.offset
;
1287 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1288 struct btrfs_file_extent_item
);
1289 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1291 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1292 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1293 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1294 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1295 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1296 extent_end
= found_key
.offset
+
1297 btrfs_file_extent_num_bytes(leaf
, fi
);
1299 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1300 if (extent_end
<= start
) {
1304 if (disk_bytenr
== 0)
1306 if (btrfs_file_extent_compression(leaf
, fi
) ||
1307 btrfs_file_extent_encryption(leaf
, fi
) ||
1308 btrfs_file_extent_other_encoding(leaf
, fi
))
1310 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1312 if (btrfs_extent_readonly(root
, disk_bytenr
))
1314 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1316 extent_offset
, disk_bytenr
))
1318 disk_bytenr
+= extent_offset
;
1319 disk_bytenr
+= cur_offset
- found_key
.offset
;
1320 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1322 * force cow if csum exists in the range.
1323 * this ensure that csum for a given extent are
1324 * either valid or do not exist.
1326 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1329 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1330 extent_end
= found_key
.offset
+
1331 btrfs_file_extent_inline_len(leaf
, fi
);
1332 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1337 if (extent_end
<= start
) {
1342 if (cow_start
== (u64
)-1)
1343 cow_start
= cur_offset
;
1344 cur_offset
= extent_end
;
1345 if (cur_offset
> end
)
1351 btrfs_release_path(path
);
1352 if (cow_start
!= (u64
)-1) {
1353 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1354 cow_start
, found_key
.offset
- 1,
1355 page_started
, nr_written
, 1);
1357 btrfs_abort_transaction(trans
, root
, ret
);
1360 cow_start
= (u64
)-1;
1363 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1364 struct extent_map
*em
;
1365 struct extent_map_tree
*em_tree
;
1366 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1367 em
= alloc_extent_map();
1368 BUG_ON(!em
); /* -ENOMEM */
1369 em
->start
= cur_offset
;
1370 em
->orig_start
= found_key
.offset
- extent_offset
;
1371 em
->len
= num_bytes
;
1372 em
->block_len
= num_bytes
;
1373 em
->block_start
= disk_bytenr
;
1374 em
->orig_block_len
= disk_num_bytes
;
1375 em
->ram_bytes
= ram_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
, 1);
1385 write_unlock(&em_tree
->lock
);
1386 if (ret
!= -EEXIST
) {
1387 free_extent_map(em
);
1390 btrfs_drop_extent_cache(inode
, em
->start
,
1391 em
->start
+ em
->len
- 1, 0);
1393 type
= BTRFS_ORDERED_PREALLOC
;
1395 type
= BTRFS_ORDERED_NOCOW
;
1398 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1399 num_bytes
, num_bytes
, type
);
1400 BUG_ON(ret
); /* -ENOMEM */
1402 if (root
->root_key
.objectid
==
1403 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1404 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1407 btrfs_abort_transaction(trans
, root
, ret
);
1412 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1413 cur_offset
, cur_offset
+ num_bytes
- 1,
1414 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1415 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1416 EXTENT_SET_PRIVATE2
);
1417 cur_offset
= extent_end
;
1418 if (cur_offset
> end
)
1421 btrfs_release_path(path
);
1423 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1424 cow_start
= cur_offset
;
1428 if (cow_start
!= (u64
)-1) {
1429 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1431 page_started
, nr_written
, 1);
1433 btrfs_abort_transaction(trans
, root
, ret
);
1439 err
= btrfs_end_transaction(trans
, root
);
1443 if (ret
&& cur_offset
< end
)
1444 extent_clear_unlock_delalloc(inode
,
1445 &BTRFS_I(inode
)->io_tree
,
1446 cur_offset
, end
, locked_page
,
1447 EXTENT_CLEAR_UNLOCK_PAGE
|
1448 EXTENT_CLEAR_UNLOCK
|
1449 EXTENT_CLEAR_DELALLOC
|
1450 EXTENT_CLEAR_DIRTY
|
1451 EXTENT_SET_WRITEBACK
|
1452 EXTENT_END_WRITEBACK
);
1454 btrfs_free_path(path
);
1459 * extent_io.c call back to do delayed allocation processing
1461 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1462 u64 start
, u64 end
, int *page_started
,
1463 unsigned long *nr_written
)
1466 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1468 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1469 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1470 page_started
, 1, nr_written
);
1471 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1472 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1473 page_started
, 0, nr_written
);
1474 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1475 !(BTRFS_I(inode
)->force_compress
) &&
1476 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1477 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1478 page_started
, nr_written
, 1);
1480 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1481 &BTRFS_I(inode
)->runtime_flags
);
1482 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1483 page_started
, nr_written
);
1488 static void btrfs_split_extent_hook(struct inode
*inode
,
1489 struct extent_state
*orig
, u64 split
)
1491 /* not delalloc, ignore it */
1492 if (!(orig
->state
& EXTENT_DELALLOC
))
1495 spin_lock(&BTRFS_I(inode
)->lock
);
1496 BTRFS_I(inode
)->outstanding_extents
++;
1497 spin_unlock(&BTRFS_I(inode
)->lock
);
1501 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1502 * extents so we can keep track of new extents that are just merged onto old
1503 * extents, such as when we are doing sequential writes, so we can properly
1504 * account for the metadata space we'll need.
1506 static void btrfs_merge_extent_hook(struct inode
*inode
,
1507 struct extent_state
*new,
1508 struct extent_state
*other
)
1510 /* not delalloc, ignore it */
1511 if (!(other
->state
& EXTENT_DELALLOC
))
1514 spin_lock(&BTRFS_I(inode
)->lock
);
1515 BTRFS_I(inode
)->outstanding_extents
--;
1516 spin_unlock(&BTRFS_I(inode
)->lock
);
1520 * extent_io.c set_bit_hook, used to track delayed allocation
1521 * bytes in this file, and to maintain the list of inodes that
1522 * have pending delalloc work to be done.
1524 static void btrfs_set_bit_hook(struct inode
*inode
,
1525 struct extent_state
*state
, int *bits
)
1529 * set_bit and clear bit hooks normally require _irqsave/restore
1530 * but in this case, we are only testing for the DELALLOC
1531 * bit, which is only set or cleared with irqs on
1533 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1534 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1535 u64 len
= state
->end
+ 1 - state
->start
;
1536 bool do_list
= !btrfs_is_free_space_inode(inode
);
1538 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1539 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1541 spin_lock(&BTRFS_I(inode
)->lock
);
1542 BTRFS_I(inode
)->outstanding_extents
++;
1543 spin_unlock(&BTRFS_I(inode
)->lock
);
1546 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1547 root
->fs_info
->delalloc_batch
);
1548 spin_lock(&BTRFS_I(inode
)->lock
);
1549 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1550 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1551 &BTRFS_I(inode
)->runtime_flags
)) {
1552 spin_lock(&root
->fs_info
->delalloc_lock
);
1553 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1554 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1555 &root
->fs_info
->delalloc_inodes
);
1556 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1557 &BTRFS_I(inode
)->runtime_flags
);
1559 spin_unlock(&root
->fs_info
->delalloc_lock
);
1561 spin_unlock(&BTRFS_I(inode
)->lock
);
1566 * extent_io.c clear_bit_hook, see set_bit_hook for why
1568 static void btrfs_clear_bit_hook(struct inode
*inode
,
1569 struct extent_state
*state
, int *bits
)
1572 * set_bit and clear bit hooks normally require _irqsave/restore
1573 * but in this case, we are only testing for the DELALLOC
1574 * bit, which is only set or cleared with irqs on
1576 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1577 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1578 u64 len
= state
->end
+ 1 - state
->start
;
1579 bool do_list
= !btrfs_is_free_space_inode(inode
);
1581 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1582 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1583 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1584 spin_lock(&BTRFS_I(inode
)->lock
);
1585 BTRFS_I(inode
)->outstanding_extents
--;
1586 spin_unlock(&BTRFS_I(inode
)->lock
);
1589 if (*bits
& EXTENT_DO_ACCOUNTING
)
1590 btrfs_delalloc_release_metadata(inode
, len
);
1592 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1594 btrfs_free_reserved_data_space(inode
, len
);
1596 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1597 root
->fs_info
->delalloc_batch
);
1598 spin_lock(&BTRFS_I(inode
)->lock
);
1599 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1600 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1601 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1602 &BTRFS_I(inode
)->runtime_flags
)) {
1603 spin_lock(&root
->fs_info
->delalloc_lock
);
1604 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1605 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1606 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1607 &BTRFS_I(inode
)->runtime_flags
);
1609 spin_unlock(&root
->fs_info
->delalloc_lock
);
1611 spin_unlock(&BTRFS_I(inode
)->lock
);
1616 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1617 * we don't create bios that span stripes or chunks
1619 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1620 size_t size
, struct bio
*bio
,
1621 unsigned long bio_flags
)
1623 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1624 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1629 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1632 length
= bio
->bi_size
;
1633 map_length
= length
;
1634 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1635 &map_length
, NULL
, 0);
1636 /* Will always return 0 with map_multi == NULL */
1638 if (map_length
< length
+ size
)
1644 * in order to insert checksums into the metadata in large chunks,
1645 * we wait until bio submission time. All the pages in the bio are
1646 * checksummed and sums are attached onto the ordered extent record.
1648 * At IO completion time the cums attached on the ordered extent record
1649 * are inserted into the btree
1651 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1652 struct bio
*bio
, int mirror_num
,
1653 unsigned long bio_flags
,
1656 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1659 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1660 BUG_ON(ret
); /* -ENOMEM */
1665 * in order to insert checksums into the metadata in large chunks,
1666 * we wait until bio submission time. All the pages in the bio are
1667 * checksummed and sums are attached onto the ordered extent record.
1669 * At IO completion time the cums attached on the ordered extent record
1670 * are inserted into the btree
1672 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1673 int mirror_num
, unsigned long bio_flags
,
1676 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1679 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1681 bio_endio(bio
, ret
);
1686 * extent_io.c submission hook. This does the right thing for csum calculation
1687 * on write, or reading the csums from the tree before a read
1689 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1690 int mirror_num
, unsigned long bio_flags
,
1693 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1697 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1699 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1701 if (btrfs_is_free_space_inode(inode
))
1704 if (!(rw
& REQ_WRITE
)) {
1705 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1709 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1710 ret
= btrfs_submit_compressed_read(inode
, bio
,
1714 } else if (!skip_sum
) {
1715 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1720 } else if (async
&& !skip_sum
) {
1721 /* csum items have already been cloned */
1722 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1724 /* we're doing a write, do the async checksumming */
1725 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1726 inode
, rw
, bio
, mirror_num
,
1727 bio_flags
, bio_offset
,
1728 __btrfs_submit_bio_start
,
1729 __btrfs_submit_bio_done
);
1731 } else if (!skip_sum
) {
1732 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1738 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1742 bio_endio(bio
, ret
);
1747 * given a list of ordered sums record them in the inode. This happens
1748 * at IO completion time based on sums calculated at bio submission time.
1750 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1751 struct inode
*inode
, u64 file_offset
,
1752 struct list_head
*list
)
1754 struct btrfs_ordered_sum
*sum
;
1756 list_for_each_entry(sum
, list
, list
) {
1757 trans
->adding_csums
= 1;
1758 btrfs_csum_file_blocks(trans
,
1759 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1760 trans
->adding_csums
= 0;
1765 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1766 struct extent_state
**cached_state
)
1768 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1769 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1770 cached_state
, GFP_NOFS
);
1773 /* see btrfs_writepage_start_hook for details on why this is required */
1774 struct btrfs_writepage_fixup
{
1776 struct btrfs_work work
;
1779 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1781 struct btrfs_writepage_fixup
*fixup
;
1782 struct btrfs_ordered_extent
*ordered
;
1783 struct extent_state
*cached_state
= NULL
;
1785 struct inode
*inode
;
1790 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1794 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1795 ClearPageChecked(page
);
1799 inode
= page
->mapping
->host
;
1800 page_start
= page_offset(page
);
1801 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1803 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1806 /* already ordered? We're done */
1807 if (PagePrivate2(page
))
1810 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1812 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1813 page_end
, &cached_state
, GFP_NOFS
);
1815 btrfs_start_ordered_extent(inode
, ordered
, 1);
1816 btrfs_put_ordered_extent(ordered
);
1820 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1822 mapping_set_error(page
->mapping
, ret
);
1823 end_extent_writepage(page
, ret
, page_start
, page_end
);
1824 ClearPageChecked(page
);
1828 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1829 ClearPageChecked(page
);
1830 set_page_dirty(page
);
1832 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1833 &cached_state
, GFP_NOFS
);
1836 page_cache_release(page
);
1841 * There are a few paths in the higher layers of the kernel that directly
1842 * set the page dirty bit without asking the filesystem if it is a
1843 * good idea. This causes problems because we want to make sure COW
1844 * properly happens and the data=ordered rules are followed.
1846 * In our case any range that doesn't have the ORDERED bit set
1847 * hasn't been properly setup for IO. We kick off an async process
1848 * to fix it up. The async helper will wait for ordered extents, set
1849 * the delalloc bit and make it safe to write the page.
1851 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1853 struct inode
*inode
= page
->mapping
->host
;
1854 struct btrfs_writepage_fixup
*fixup
;
1855 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1857 /* this page is properly in the ordered list */
1858 if (TestClearPagePrivate2(page
))
1861 if (PageChecked(page
))
1864 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1868 SetPageChecked(page
);
1869 page_cache_get(page
);
1870 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1872 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1876 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1877 struct inode
*inode
, u64 file_pos
,
1878 u64 disk_bytenr
, u64 disk_num_bytes
,
1879 u64 num_bytes
, u64 ram_bytes
,
1880 u8 compression
, u8 encryption
,
1881 u16 other_encoding
, int extent_type
)
1883 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1884 struct btrfs_file_extent_item
*fi
;
1885 struct btrfs_path
*path
;
1886 struct extent_buffer
*leaf
;
1887 struct btrfs_key ins
;
1890 path
= btrfs_alloc_path();
1894 path
->leave_spinning
= 1;
1897 * we may be replacing one extent in the tree with another.
1898 * The new extent is pinned in the extent map, and we don't want
1899 * to drop it from the cache until it is completely in the btree.
1901 * So, tell btrfs_drop_extents to leave this extent in the cache.
1902 * the caller is expected to unpin it and allow it to be merged
1905 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1906 file_pos
+ num_bytes
, 0);
1910 ins
.objectid
= btrfs_ino(inode
);
1911 ins
.offset
= file_pos
;
1912 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1913 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1916 leaf
= path
->nodes
[0];
1917 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1918 struct btrfs_file_extent_item
);
1919 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1920 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1921 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1922 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1923 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1924 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1925 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1926 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1927 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1928 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1930 btrfs_mark_buffer_dirty(leaf
);
1931 btrfs_release_path(path
);
1933 inode_add_bytes(inode
, num_bytes
);
1935 ins
.objectid
= disk_bytenr
;
1936 ins
.offset
= disk_num_bytes
;
1937 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1938 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1939 root
->root_key
.objectid
,
1940 btrfs_ino(inode
), file_pos
, &ins
);
1942 btrfs_free_path(path
);
1947 /* snapshot-aware defrag */
1948 struct sa_defrag_extent_backref
{
1949 struct rb_node node
;
1950 struct old_sa_defrag_extent
*old
;
1959 struct old_sa_defrag_extent
{
1960 struct list_head list
;
1961 struct new_sa_defrag_extent
*new;
1970 struct new_sa_defrag_extent
{
1971 struct rb_root root
;
1972 struct list_head head
;
1973 struct btrfs_path
*path
;
1974 struct inode
*inode
;
1982 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1983 struct sa_defrag_extent_backref
*b2
)
1985 if (b1
->root_id
< b2
->root_id
)
1987 else if (b1
->root_id
> b2
->root_id
)
1990 if (b1
->inum
< b2
->inum
)
1992 else if (b1
->inum
> b2
->inum
)
1995 if (b1
->file_pos
< b2
->file_pos
)
1997 else if (b1
->file_pos
> b2
->file_pos
)
2001 * [------------------------------] ===> (a range of space)
2002 * |<--->| |<---->| =============> (fs/file tree A)
2003 * |<---------------------------->| ===> (fs/file tree B)
2005 * A range of space can refer to two file extents in one tree while
2006 * refer to only one file extent in another tree.
2008 * So we may process a disk offset more than one time(two extents in A)
2009 * and locate at the same extent(one extent in B), then insert two same
2010 * backrefs(both refer to the extent in B).
2015 static void backref_insert(struct rb_root
*root
,
2016 struct sa_defrag_extent_backref
*backref
)
2018 struct rb_node
**p
= &root
->rb_node
;
2019 struct rb_node
*parent
= NULL
;
2020 struct sa_defrag_extent_backref
*entry
;
2025 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2027 ret
= backref_comp(backref
, entry
);
2031 p
= &(*p
)->rb_right
;
2034 rb_link_node(&backref
->node
, parent
, p
);
2035 rb_insert_color(&backref
->node
, root
);
2039 * Note the backref might has changed, and in this case we just return 0.
2041 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2044 struct btrfs_file_extent_item
*extent
;
2045 struct btrfs_fs_info
*fs_info
;
2046 struct old_sa_defrag_extent
*old
= ctx
;
2047 struct new_sa_defrag_extent
*new = old
->new;
2048 struct btrfs_path
*path
= new->path
;
2049 struct btrfs_key key
;
2050 struct btrfs_root
*root
;
2051 struct sa_defrag_extent_backref
*backref
;
2052 struct extent_buffer
*leaf
;
2053 struct inode
*inode
= new->inode
;
2059 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2060 inum
== btrfs_ino(inode
))
2063 key
.objectid
= root_id
;
2064 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2065 key
.offset
= (u64
)-1;
2067 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2068 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2070 if (PTR_ERR(root
) == -ENOENT
)
2073 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2074 inum
, offset
, root_id
);
2075 return PTR_ERR(root
);
2078 key
.objectid
= inum
;
2079 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2080 if (offset
> (u64
)-1 << 32)
2083 key
.offset
= offset
;
2085 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2094 leaf
= path
->nodes
[0];
2095 slot
= path
->slots
[0];
2097 if (slot
>= btrfs_header_nritems(leaf
)) {
2098 ret
= btrfs_next_leaf(root
, path
);
2101 } else if (ret
> 0) {
2110 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2112 if (key
.objectid
> inum
)
2115 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2118 extent
= btrfs_item_ptr(leaf
, slot
,
2119 struct btrfs_file_extent_item
);
2121 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2124 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2125 if (key
.offset
- extent_offset
!= offset
)
2128 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2129 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2130 old
->len
|| extent_offset
+ num_bytes
<=
2131 old
->extent_offset
+ old
->offset
)
2137 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2143 backref
->root_id
= root_id
;
2144 backref
->inum
= inum
;
2145 backref
->file_pos
= offset
+ extent_offset
;
2146 backref
->num_bytes
= num_bytes
;
2147 backref
->extent_offset
= extent_offset
;
2148 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2150 backref_insert(&new->root
, backref
);
2153 btrfs_release_path(path
);
2158 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2159 struct new_sa_defrag_extent
*new)
2161 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2162 struct old_sa_defrag_extent
*old
, *tmp
;
2167 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2168 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2169 path
, record_one_backref
,
2171 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2173 /* no backref to be processed for this extent */
2175 list_del(&old
->list
);
2180 if (list_empty(&new->head
))
2186 static int relink_is_mergable(struct extent_buffer
*leaf
,
2187 struct btrfs_file_extent_item
*fi
,
2190 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2193 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2196 if (btrfs_file_extent_compression(leaf
, fi
) ||
2197 btrfs_file_extent_encryption(leaf
, fi
) ||
2198 btrfs_file_extent_other_encoding(leaf
, fi
))
2205 * Note the backref might has changed, and in this case we just return 0.
2207 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2208 struct sa_defrag_extent_backref
*prev
,
2209 struct sa_defrag_extent_backref
*backref
)
2211 struct btrfs_file_extent_item
*extent
;
2212 struct btrfs_file_extent_item
*item
;
2213 struct btrfs_ordered_extent
*ordered
;
2214 struct btrfs_trans_handle
*trans
;
2215 struct btrfs_fs_info
*fs_info
;
2216 struct btrfs_root
*root
;
2217 struct btrfs_key key
;
2218 struct extent_buffer
*leaf
;
2219 struct old_sa_defrag_extent
*old
= backref
->old
;
2220 struct new_sa_defrag_extent
*new = old
->new;
2221 struct inode
*src_inode
= new->inode
;
2222 struct inode
*inode
;
2223 struct extent_state
*cached
= NULL
;
2232 if (prev
&& prev
->root_id
== backref
->root_id
&&
2233 prev
->inum
== backref
->inum
&&
2234 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2237 /* step 1: get root */
2238 key
.objectid
= backref
->root_id
;
2239 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2240 key
.offset
= (u64
)-1;
2242 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2243 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2245 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2247 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2248 if (PTR_ERR(root
) == -ENOENT
)
2250 return PTR_ERR(root
);
2252 if (btrfs_root_refs(&root
->root_item
) == 0) {
2253 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2254 /* parse ENOENT to 0 */
2258 /* step 2: get inode */
2259 key
.objectid
= backref
->inum
;
2260 key
.type
= BTRFS_INODE_ITEM_KEY
;
2263 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2264 if (IS_ERR(inode
)) {
2265 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2269 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2271 /* step 3: relink backref */
2272 lock_start
= backref
->file_pos
;
2273 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2274 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2277 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2279 btrfs_put_ordered_extent(ordered
);
2283 trans
= btrfs_join_transaction(root
);
2284 if (IS_ERR(trans
)) {
2285 ret
= PTR_ERR(trans
);
2289 key
.objectid
= backref
->inum
;
2290 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2291 key
.offset
= backref
->file_pos
;
2293 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2296 } else if (ret
> 0) {
2301 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2302 struct btrfs_file_extent_item
);
2304 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2305 backref
->generation
)
2308 btrfs_release_path(path
);
2310 start
= backref
->file_pos
;
2311 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2312 start
+= old
->extent_offset
+ old
->offset
-
2313 backref
->extent_offset
;
2315 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2316 old
->extent_offset
+ old
->offset
+ old
->len
);
2317 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2319 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2324 key
.objectid
= btrfs_ino(inode
);
2325 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2328 path
->leave_spinning
= 1;
2330 struct btrfs_file_extent_item
*fi
;
2332 struct btrfs_key found_key
;
2334 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2339 leaf
= path
->nodes
[0];
2340 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2342 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2343 struct btrfs_file_extent_item
);
2344 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2346 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2347 extent_len
+ found_key
.offset
== start
) {
2348 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2350 btrfs_mark_buffer_dirty(leaf
);
2351 inode_add_bytes(inode
, len
);
2357 btrfs_release_path(path
);
2362 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2365 btrfs_abort_transaction(trans
, root
, ret
);
2369 leaf
= path
->nodes
[0];
2370 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2371 struct btrfs_file_extent_item
);
2372 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2373 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2374 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2375 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2376 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2377 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2378 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2379 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2380 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2381 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2383 btrfs_mark_buffer_dirty(leaf
);
2384 inode_add_bytes(inode
, len
);
2385 btrfs_release_path(path
);
2387 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2389 backref
->root_id
, backref
->inum
,
2390 new->file_pos
, 0); /* start - extent_offset */
2392 btrfs_abort_transaction(trans
, root
, ret
);
2398 btrfs_release_path(path
);
2399 path
->leave_spinning
= 0;
2400 btrfs_end_transaction(trans
, root
);
2402 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2408 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2410 struct btrfs_path
*path
;
2411 struct old_sa_defrag_extent
*old
, *tmp
;
2412 struct sa_defrag_extent_backref
*backref
;
2413 struct sa_defrag_extent_backref
*prev
= NULL
;
2414 struct inode
*inode
;
2415 struct btrfs_root
*root
;
2416 struct rb_node
*node
;
2420 root
= BTRFS_I(inode
)->root
;
2422 path
= btrfs_alloc_path();
2426 if (!record_extent_backrefs(path
, new)) {
2427 btrfs_free_path(path
);
2430 btrfs_release_path(path
);
2433 node
= rb_first(&new->root
);
2436 rb_erase(node
, &new->root
);
2438 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2440 ret
= relink_extent_backref(path
, prev
, backref
);
2453 btrfs_free_path(path
);
2455 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2456 list_del(&old
->list
);
2460 atomic_dec(&root
->fs_info
->defrag_running
);
2461 wake_up(&root
->fs_info
->transaction_wait
);
2466 static struct new_sa_defrag_extent
*
2467 record_old_file_extents(struct inode
*inode
,
2468 struct btrfs_ordered_extent
*ordered
)
2470 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2471 struct btrfs_path
*path
;
2472 struct btrfs_key key
;
2473 struct old_sa_defrag_extent
*old
, *tmp
;
2474 struct new_sa_defrag_extent
*new;
2477 new = kmalloc(sizeof(*new), GFP_NOFS
);
2482 new->file_pos
= ordered
->file_offset
;
2483 new->len
= ordered
->len
;
2484 new->bytenr
= ordered
->start
;
2485 new->disk_len
= ordered
->disk_len
;
2486 new->compress_type
= ordered
->compress_type
;
2487 new->root
= RB_ROOT
;
2488 INIT_LIST_HEAD(&new->head
);
2490 path
= btrfs_alloc_path();
2494 key
.objectid
= btrfs_ino(inode
);
2495 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2496 key
.offset
= new->file_pos
;
2498 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2501 if (ret
> 0 && path
->slots
[0] > 0)
2504 /* find out all the old extents for the file range */
2506 struct btrfs_file_extent_item
*extent
;
2507 struct extent_buffer
*l
;
2516 slot
= path
->slots
[0];
2518 if (slot
>= btrfs_header_nritems(l
)) {
2519 ret
= btrfs_next_leaf(root
, path
);
2527 btrfs_item_key_to_cpu(l
, &key
, slot
);
2529 if (key
.objectid
!= btrfs_ino(inode
))
2531 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2533 if (key
.offset
>= new->file_pos
+ new->len
)
2536 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2538 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2539 if (key
.offset
+ num_bytes
< new->file_pos
)
2542 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2546 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2548 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2552 offset
= max(new->file_pos
, key
.offset
);
2553 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2555 old
->bytenr
= disk_bytenr
;
2556 old
->extent_offset
= extent_offset
;
2557 old
->offset
= offset
- key
.offset
;
2558 old
->len
= end
- offset
;
2561 list_add_tail(&old
->list
, &new->head
);
2567 btrfs_free_path(path
);
2568 atomic_inc(&root
->fs_info
->defrag_running
);
2573 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2574 list_del(&old
->list
);
2578 btrfs_free_path(path
);
2585 * helper function for btrfs_finish_ordered_io, this
2586 * just reads in some of the csum leaves to prime them into ram
2587 * before we start the transaction. It limits the amount of btree
2588 * reads required while inside the transaction.
2590 /* as ordered data IO finishes, this gets called so we can finish
2591 * an ordered extent if the range of bytes in the file it covers are
2594 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2596 struct inode
*inode
= ordered_extent
->inode
;
2597 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2598 struct btrfs_trans_handle
*trans
= NULL
;
2599 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2600 struct extent_state
*cached_state
= NULL
;
2601 struct new_sa_defrag_extent
*new = NULL
;
2602 int compress_type
= 0;
2606 nolock
= btrfs_is_free_space_inode(inode
);
2608 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2613 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2614 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2615 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2617 trans
= btrfs_join_transaction_nolock(root
);
2619 trans
= btrfs_join_transaction(root
);
2620 if (IS_ERR(trans
)) {
2621 ret
= PTR_ERR(trans
);
2625 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2626 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2627 if (ret
) /* -ENOMEM or corruption */
2628 btrfs_abort_transaction(trans
, root
, ret
);
2632 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2633 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2636 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2637 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2638 EXTENT_DEFRAG
, 1, cached_state
);
2640 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2641 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2642 /* the inode is shared */
2643 new = record_old_file_extents(inode
, ordered_extent
);
2645 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2646 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2647 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2651 trans
= btrfs_join_transaction_nolock(root
);
2653 trans
= btrfs_join_transaction(root
);
2654 if (IS_ERR(trans
)) {
2655 ret
= PTR_ERR(trans
);
2659 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2661 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2662 compress_type
= ordered_extent
->compress_type
;
2663 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2664 BUG_ON(compress_type
);
2665 ret
= btrfs_mark_extent_written(trans
, inode
,
2666 ordered_extent
->file_offset
,
2667 ordered_extent
->file_offset
+
2668 ordered_extent
->len
);
2670 BUG_ON(root
== root
->fs_info
->tree_root
);
2671 ret
= insert_reserved_file_extent(trans
, inode
,
2672 ordered_extent
->file_offset
,
2673 ordered_extent
->start
,
2674 ordered_extent
->disk_len
,
2675 ordered_extent
->len
,
2676 ordered_extent
->len
,
2677 compress_type
, 0, 0,
2678 BTRFS_FILE_EXTENT_REG
);
2680 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2681 ordered_extent
->file_offset
, ordered_extent
->len
,
2684 btrfs_abort_transaction(trans
, root
, ret
);
2688 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2689 &ordered_extent
->list
);
2691 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2692 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2693 if (ret
) { /* -ENOMEM or corruption */
2694 btrfs_abort_transaction(trans
, root
, ret
);
2699 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2700 ordered_extent
->file_offset
+
2701 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2703 if (root
!= root
->fs_info
->tree_root
)
2704 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2706 btrfs_end_transaction(trans
, root
);
2709 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2710 ordered_extent
->file_offset
+
2711 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2714 * If the ordered extent had an IOERR or something else went
2715 * wrong we need to return the space for this ordered extent
2716 * back to the allocator.
2718 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2719 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2720 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2721 ordered_extent
->disk_len
);
2726 * This needs to be done to make sure anybody waiting knows we are done
2727 * updating everything for this ordered extent.
2729 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2731 /* for snapshot-aware defrag */
2733 relink_file_extents(new);
2736 btrfs_put_ordered_extent(ordered_extent
);
2737 /* once for the tree */
2738 btrfs_put_ordered_extent(ordered_extent
);
2743 static void finish_ordered_fn(struct btrfs_work
*work
)
2745 struct btrfs_ordered_extent
*ordered_extent
;
2746 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2747 btrfs_finish_ordered_io(ordered_extent
);
2750 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2751 struct extent_state
*state
, int uptodate
)
2753 struct inode
*inode
= page
->mapping
->host
;
2754 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2755 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2756 struct btrfs_workers
*workers
;
2758 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2760 ClearPagePrivate2(page
);
2761 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2762 end
- start
+ 1, uptodate
))
2765 ordered_extent
->work
.func
= finish_ordered_fn
;
2766 ordered_extent
->work
.flags
= 0;
2768 if (btrfs_is_free_space_inode(inode
))
2769 workers
= &root
->fs_info
->endio_freespace_worker
;
2771 workers
= &root
->fs_info
->endio_write_workers
;
2772 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2778 * when reads are done, we need to check csums to verify the data is correct
2779 * if there's a match, we allow the bio to finish. If not, the code in
2780 * extent_io.c will try to find good copies for us.
2782 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2783 struct extent_state
*state
, int mirror
)
2785 size_t offset
= start
- page_offset(page
);
2786 struct inode
*inode
= page
->mapping
->host
;
2787 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2789 u64
private = ~(u32
)0;
2791 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2793 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2794 DEFAULT_RATELIMIT_BURST
);
2796 if (PageChecked(page
)) {
2797 ClearPageChecked(page
);
2801 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2804 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2805 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2806 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2811 if (state
&& state
->start
== start
) {
2812 private = state
->private;
2815 ret
= get_state_private(io_tree
, start
, &private);
2817 kaddr
= kmap_atomic(page
);
2821 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2822 btrfs_csum_final(csum
, (char *)&csum
);
2823 if (csum
!= private)
2826 kunmap_atomic(kaddr
);
2831 if (__ratelimit(&_rs
))
2832 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2833 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2834 (unsigned long long)start
, csum
,
2835 (unsigned long long)private);
2836 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2837 flush_dcache_page(page
);
2838 kunmap_atomic(kaddr
);
2844 struct delayed_iput
{
2845 struct list_head list
;
2846 struct inode
*inode
;
2849 /* JDM: If this is fs-wide, why can't we add a pointer to
2850 * btrfs_inode instead and avoid the allocation? */
2851 void btrfs_add_delayed_iput(struct inode
*inode
)
2853 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2854 struct delayed_iput
*delayed
;
2856 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2859 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2860 delayed
->inode
= inode
;
2862 spin_lock(&fs_info
->delayed_iput_lock
);
2863 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2864 spin_unlock(&fs_info
->delayed_iput_lock
);
2867 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2870 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2871 struct delayed_iput
*delayed
;
2874 spin_lock(&fs_info
->delayed_iput_lock
);
2875 empty
= list_empty(&fs_info
->delayed_iputs
);
2876 spin_unlock(&fs_info
->delayed_iput_lock
);
2880 spin_lock(&fs_info
->delayed_iput_lock
);
2881 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2882 spin_unlock(&fs_info
->delayed_iput_lock
);
2884 while (!list_empty(&list
)) {
2885 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2886 list_del(&delayed
->list
);
2887 iput(delayed
->inode
);
2893 * This is called in transaction commit time. If there are no orphan
2894 * files in the subvolume, it removes orphan item and frees block_rsv
2897 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2898 struct btrfs_root
*root
)
2900 struct btrfs_block_rsv
*block_rsv
;
2903 if (atomic_read(&root
->orphan_inodes
) ||
2904 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2907 spin_lock(&root
->orphan_lock
);
2908 if (atomic_read(&root
->orphan_inodes
)) {
2909 spin_unlock(&root
->orphan_lock
);
2913 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2914 spin_unlock(&root
->orphan_lock
);
2918 block_rsv
= root
->orphan_block_rsv
;
2919 root
->orphan_block_rsv
= NULL
;
2920 spin_unlock(&root
->orphan_lock
);
2922 if (root
->orphan_item_inserted
&&
2923 btrfs_root_refs(&root
->root_item
) > 0) {
2924 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2925 root
->root_key
.objectid
);
2927 root
->orphan_item_inserted
= 0;
2931 WARN_ON(block_rsv
->size
> 0);
2932 btrfs_free_block_rsv(root
, block_rsv
);
2937 * This creates an orphan entry for the given inode in case something goes
2938 * wrong in the middle of an unlink/truncate.
2940 * NOTE: caller of this function should reserve 5 units of metadata for
2943 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2945 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2946 struct btrfs_block_rsv
*block_rsv
= NULL
;
2951 if (!root
->orphan_block_rsv
) {
2952 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2957 spin_lock(&root
->orphan_lock
);
2958 if (!root
->orphan_block_rsv
) {
2959 root
->orphan_block_rsv
= block_rsv
;
2960 } else if (block_rsv
) {
2961 btrfs_free_block_rsv(root
, block_rsv
);
2965 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2966 &BTRFS_I(inode
)->runtime_flags
)) {
2969 * For proper ENOSPC handling, we should do orphan
2970 * cleanup when mounting. But this introduces backward
2971 * compatibility issue.
2973 if (!xchg(&root
->orphan_item_inserted
, 1))
2979 atomic_inc(&root
->orphan_inodes
);
2982 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2983 &BTRFS_I(inode
)->runtime_flags
))
2985 spin_unlock(&root
->orphan_lock
);
2987 /* grab metadata reservation from transaction handle */
2989 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2990 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2993 /* insert an orphan item to track this unlinked/truncated file */
2995 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2996 if (ret
&& ret
!= -EEXIST
) {
2997 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2998 &BTRFS_I(inode
)->runtime_flags
);
2999 btrfs_abort_transaction(trans
, root
, ret
);
3005 /* insert an orphan item to track subvolume contains orphan files */
3007 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3008 root
->root_key
.objectid
);
3009 if (ret
&& ret
!= -EEXIST
) {
3010 btrfs_abort_transaction(trans
, root
, ret
);
3018 * We have done the truncate/delete so we can go ahead and remove the orphan
3019 * item for this particular inode.
3021 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3023 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3024 int delete_item
= 0;
3025 int release_rsv
= 0;
3028 spin_lock(&root
->orphan_lock
);
3029 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3030 &BTRFS_I(inode
)->runtime_flags
))
3033 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3034 &BTRFS_I(inode
)->runtime_flags
))
3036 spin_unlock(&root
->orphan_lock
);
3038 if (trans
&& delete_item
) {
3039 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3040 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3044 btrfs_orphan_release_metadata(inode
);
3045 atomic_dec(&root
->orphan_inodes
);
3052 * this cleans up any orphans that may be left on the list from the last use
3055 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3057 struct btrfs_path
*path
;
3058 struct extent_buffer
*leaf
;
3059 struct btrfs_key key
, found_key
;
3060 struct btrfs_trans_handle
*trans
;
3061 struct inode
*inode
;
3062 u64 last_objectid
= 0;
3063 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3065 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3068 path
= btrfs_alloc_path();
3075 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3076 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3077 key
.offset
= (u64
)-1;
3080 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3085 * if ret == 0 means we found what we were searching for, which
3086 * is weird, but possible, so only screw with path if we didn't
3087 * find the key and see if we have stuff that matches
3091 if (path
->slots
[0] == 0)
3096 /* pull out the item */
3097 leaf
= path
->nodes
[0];
3098 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3100 /* make sure the item matches what we want */
3101 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3103 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3106 /* release the path since we're done with it */
3107 btrfs_release_path(path
);
3110 * this is where we are basically btrfs_lookup, without the
3111 * crossing root thing. we store the inode number in the
3112 * offset of the orphan item.
3115 if (found_key
.offset
== last_objectid
) {
3116 btrfs_err(root
->fs_info
,
3117 "Error removing orphan entry, stopping orphan cleanup");
3122 last_objectid
= found_key
.offset
;
3124 found_key
.objectid
= found_key
.offset
;
3125 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3126 found_key
.offset
= 0;
3127 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3128 ret
= PTR_RET(inode
);
3129 if (ret
&& ret
!= -ESTALE
)
3132 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3133 struct btrfs_root
*dead_root
;
3134 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3135 int is_dead_root
= 0;
3138 * this is an orphan in the tree root. Currently these
3139 * could come from 2 sources:
3140 * a) a snapshot deletion in progress
3141 * b) a free space cache inode
3142 * We need to distinguish those two, as the snapshot
3143 * orphan must not get deleted.
3144 * find_dead_roots already ran before us, so if this
3145 * is a snapshot deletion, we should find the root
3146 * in the dead_roots list
3148 spin_lock(&fs_info
->trans_lock
);
3149 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3151 if (dead_root
->root_key
.objectid
==
3152 found_key
.objectid
) {
3157 spin_unlock(&fs_info
->trans_lock
);
3159 /* prevent this orphan from being found again */
3160 key
.offset
= found_key
.objectid
- 1;
3165 * Inode is already gone but the orphan item is still there,
3166 * kill the orphan item.
3168 if (ret
== -ESTALE
) {
3169 trans
= btrfs_start_transaction(root
, 1);
3170 if (IS_ERR(trans
)) {
3171 ret
= PTR_ERR(trans
);
3174 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3175 found_key
.objectid
);
3176 ret
= btrfs_del_orphan_item(trans
, root
,
3177 found_key
.objectid
);
3178 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3179 btrfs_end_transaction(trans
, root
);
3184 * add this inode to the orphan list so btrfs_orphan_del does
3185 * the proper thing when we hit it
3187 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3188 &BTRFS_I(inode
)->runtime_flags
);
3189 atomic_inc(&root
->orphan_inodes
);
3191 /* if we have links, this was a truncate, lets do that */
3192 if (inode
->i_nlink
) {
3193 if (!S_ISREG(inode
->i_mode
)) {
3200 /* 1 for the orphan item deletion. */
3201 trans
= btrfs_start_transaction(root
, 1);
3202 if (IS_ERR(trans
)) {
3203 ret
= PTR_ERR(trans
);
3206 ret
= btrfs_orphan_add(trans
, inode
);
3207 btrfs_end_transaction(trans
, root
);
3211 ret
= btrfs_truncate(inode
);
3213 btrfs_orphan_del(NULL
, inode
);
3218 /* this will do delete_inode and everything for us */
3223 /* release the path since we're done with it */
3224 btrfs_release_path(path
);
3226 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3228 if (root
->orphan_block_rsv
)
3229 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3232 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3233 trans
= btrfs_join_transaction(root
);
3235 btrfs_end_transaction(trans
, root
);
3239 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3241 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3245 btrfs_crit(root
->fs_info
,
3246 "could not do orphan cleanup %d", ret
);
3247 btrfs_free_path(path
);
3252 * very simple check to peek ahead in the leaf looking for xattrs. If we
3253 * don't find any xattrs, we know there can't be any acls.
3255 * slot is the slot the inode is in, objectid is the objectid of the inode
3257 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3258 int slot
, u64 objectid
)
3260 u32 nritems
= btrfs_header_nritems(leaf
);
3261 struct btrfs_key found_key
;
3265 while (slot
< nritems
) {
3266 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3268 /* we found a different objectid, there must not be acls */
3269 if (found_key
.objectid
!= objectid
)
3272 /* we found an xattr, assume we've got an acl */
3273 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
3277 * we found a key greater than an xattr key, there can't
3278 * be any acls later on
3280 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3287 * it goes inode, inode backrefs, xattrs, extents,
3288 * so if there are a ton of hard links to an inode there can
3289 * be a lot of backrefs. Don't waste time searching too hard,
3290 * this is just an optimization
3295 /* we hit the end of the leaf before we found an xattr or
3296 * something larger than an xattr. We have to assume the inode
3303 * read an inode from the btree into the in-memory inode
3305 static void btrfs_read_locked_inode(struct inode
*inode
)
3307 struct btrfs_path
*path
;
3308 struct extent_buffer
*leaf
;
3309 struct btrfs_inode_item
*inode_item
;
3310 struct btrfs_timespec
*tspec
;
3311 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3312 struct btrfs_key location
;
3316 bool filled
= false;
3318 ret
= btrfs_fill_inode(inode
, &rdev
);
3322 path
= btrfs_alloc_path();
3326 path
->leave_spinning
= 1;
3327 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3329 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3333 leaf
= path
->nodes
[0];
3338 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3339 struct btrfs_inode_item
);
3340 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3341 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3342 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3343 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3344 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3346 tspec
= btrfs_inode_atime(inode_item
);
3347 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3348 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3350 tspec
= btrfs_inode_mtime(inode_item
);
3351 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3352 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3354 tspec
= btrfs_inode_ctime(inode_item
);
3355 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3356 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3358 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3359 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3360 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3363 * If we were modified in the current generation and evicted from memory
3364 * and then re-read we need to do a full sync since we don't have any
3365 * idea about which extents were modified before we were evicted from
3368 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3369 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3370 &BTRFS_I(inode
)->runtime_flags
);
3372 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3373 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3375 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3377 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3378 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3381 * try to precache a NULL acl entry for files that don't have
3382 * any xattrs or acls
3384 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3387 cache_no_acl(inode
);
3389 btrfs_free_path(path
);
3391 switch (inode
->i_mode
& S_IFMT
) {
3393 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3394 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3395 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3396 inode
->i_fop
= &btrfs_file_operations
;
3397 inode
->i_op
= &btrfs_file_inode_operations
;
3400 inode
->i_fop
= &btrfs_dir_file_operations
;
3401 if (root
== root
->fs_info
->tree_root
)
3402 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3404 inode
->i_op
= &btrfs_dir_inode_operations
;
3407 inode
->i_op
= &btrfs_symlink_inode_operations
;
3408 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3409 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3412 inode
->i_op
= &btrfs_special_inode_operations
;
3413 init_special_inode(inode
, inode
->i_mode
, rdev
);
3417 btrfs_update_iflags(inode
);
3421 btrfs_free_path(path
);
3422 make_bad_inode(inode
);
3426 * given a leaf and an inode, copy the inode fields into the leaf
3428 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3429 struct extent_buffer
*leaf
,
3430 struct btrfs_inode_item
*item
,
3431 struct inode
*inode
)
3433 struct btrfs_map_token token
;
3435 btrfs_init_map_token(&token
);
3437 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3438 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3439 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3441 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3442 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3444 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3445 inode
->i_atime
.tv_sec
, &token
);
3446 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3447 inode
->i_atime
.tv_nsec
, &token
);
3449 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3450 inode
->i_mtime
.tv_sec
, &token
);
3451 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3452 inode
->i_mtime
.tv_nsec
, &token
);
3454 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3455 inode
->i_ctime
.tv_sec
, &token
);
3456 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3457 inode
->i_ctime
.tv_nsec
, &token
);
3459 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3461 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3463 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3464 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3465 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3466 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3467 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3471 * copy everything in the in-memory inode into the btree.
3473 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3474 struct btrfs_root
*root
, struct inode
*inode
)
3476 struct btrfs_inode_item
*inode_item
;
3477 struct btrfs_path
*path
;
3478 struct extent_buffer
*leaf
;
3481 path
= btrfs_alloc_path();
3485 path
->leave_spinning
= 1;
3486 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3494 btrfs_unlock_up_safe(path
, 1);
3495 leaf
= path
->nodes
[0];
3496 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3497 struct btrfs_inode_item
);
3499 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3500 btrfs_mark_buffer_dirty(leaf
);
3501 btrfs_set_inode_last_trans(trans
, inode
);
3504 btrfs_free_path(path
);
3509 * copy everything in the in-memory inode into the btree.
3511 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3512 struct btrfs_root
*root
, struct inode
*inode
)
3517 * If the inode is a free space inode, we can deadlock during commit
3518 * if we put it into the delayed code.
3520 * The data relocation inode should also be directly updated
3523 if (!btrfs_is_free_space_inode(inode
)
3524 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3525 btrfs_update_root_times(trans
, root
);
3527 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3529 btrfs_set_inode_last_trans(trans
, inode
);
3533 return btrfs_update_inode_item(trans
, root
, inode
);
3536 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3537 struct btrfs_root
*root
,
3538 struct inode
*inode
)
3542 ret
= btrfs_update_inode(trans
, root
, inode
);
3544 return btrfs_update_inode_item(trans
, root
, inode
);
3549 * unlink helper that gets used here in inode.c and in the tree logging
3550 * recovery code. It remove a link in a directory with a given name, and
3551 * also drops the back refs in the inode to the directory
3553 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3554 struct btrfs_root
*root
,
3555 struct inode
*dir
, struct inode
*inode
,
3556 const char *name
, int name_len
)
3558 struct btrfs_path
*path
;
3560 struct extent_buffer
*leaf
;
3561 struct btrfs_dir_item
*di
;
3562 struct btrfs_key key
;
3564 u64 ino
= btrfs_ino(inode
);
3565 u64 dir_ino
= btrfs_ino(dir
);
3567 path
= btrfs_alloc_path();
3573 path
->leave_spinning
= 1;
3574 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3575 name
, name_len
, -1);
3584 leaf
= path
->nodes
[0];
3585 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3586 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3589 btrfs_release_path(path
);
3591 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3594 btrfs_info(root
->fs_info
,
3595 "failed to delete reference to %.*s, inode %llu parent %llu",
3597 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3598 btrfs_abort_transaction(trans
, root
, ret
);
3602 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3604 btrfs_abort_transaction(trans
, root
, ret
);
3608 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3610 if (ret
!= 0 && ret
!= -ENOENT
) {
3611 btrfs_abort_transaction(trans
, root
, ret
);
3615 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3620 btrfs_abort_transaction(trans
, root
, ret
);
3622 btrfs_free_path(path
);
3626 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3627 inode_inc_iversion(inode
);
3628 inode_inc_iversion(dir
);
3629 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3630 ret
= btrfs_update_inode(trans
, root
, dir
);
3635 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3636 struct btrfs_root
*root
,
3637 struct inode
*dir
, struct inode
*inode
,
3638 const char *name
, int name_len
)
3641 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3643 btrfs_drop_nlink(inode
);
3644 ret
= btrfs_update_inode(trans
, root
, inode
);
3650 /* helper to check if there is any shared block in the path */
3651 static int check_path_shared(struct btrfs_root
*root
,
3652 struct btrfs_path
*path
)
3654 struct extent_buffer
*eb
;
3658 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
3661 if (!path
->nodes
[level
])
3663 eb
= path
->nodes
[level
];
3664 if (!btrfs_block_can_be_shared(root
, eb
))
3666 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, level
, 1,
3675 * helper to start transaction for unlink and rmdir.
3677 * unlink and rmdir are special in btrfs, they do not always free space.
3678 * so in enospc case, we should make sure they will free space before
3679 * allowing them to use the global metadata reservation.
3681 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3682 struct dentry
*dentry
)
3684 struct btrfs_trans_handle
*trans
;
3685 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3686 struct btrfs_path
*path
;
3687 struct btrfs_dir_item
*di
;
3688 struct inode
*inode
= dentry
->d_inode
;
3693 u64 ino
= btrfs_ino(inode
);
3694 u64 dir_ino
= btrfs_ino(dir
);
3697 * 1 for the possible orphan item
3698 * 1 for the dir item
3699 * 1 for the dir index
3700 * 1 for the inode ref
3703 trans
= btrfs_start_transaction(root
, 5);
3704 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3707 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3708 return ERR_PTR(-ENOSPC
);
3710 /* check if there is someone else holds reference */
3711 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3712 return ERR_PTR(-ENOSPC
);
3714 if (atomic_read(&inode
->i_count
) > 2)
3715 return ERR_PTR(-ENOSPC
);
3717 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3718 return ERR_PTR(-ENOSPC
);
3720 path
= btrfs_alloc_path();
3722 root
->fs_info
->enospc_unlink
= 0;
3723 return ERR_PTR(-ENOMEM
);
3726 /* 1 for the orphan item */
3727 trans
= btrfs_start_transaction(root
, 1);
3728 if (IS_ERR(trans
)) {
3729 btrfs_free_path(path
);
3730 root
->fs_info
->enospc_unlink
= 0;
3734 path
->skip_locking
= 1;
3735 path
->search_commit_root
= 1;
3737 ret
= btrfs_lookup_inode(trans
, root
, path
,
3738 &BTRFS_I(dir
)->location
, 0);
3744 if (check_path_shared(root
, path
))
3749 btrfs_release_path(path
);
3751 ret
= btrfs_lookup_inode(trans
, root
, path
,
3752 &BTRFS_I(inode
)->location
, 0);
3758 if (check_path_shared(root
, path
))
3763 btrfs_release_path(path
);
3765 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3766 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3772 BUG_ON(ret
== 0); /* Corruption */
3773 if (check_path_shared(root
, path
))
3775 btrfs_release_path(path
);
3783 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3784 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3790 if (check_path_shared(root
, path
))
3796 btrfs_release_path(path
);
3798 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3799 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3806 if (check_path_shared(root
, path
))
3809 btrfs_release_path(path
);
3812 * This is a commit root search, if we can lookup inode item and other
3813 * relative items in the commit root, it means the transaction of
3814 * dir/file creation has been committed, and the dir index item that we
3815 * delay to insert has also been inserted into the commit root. So
3816 * we needn't worry about the delayed insertion of the dir index item
3819 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3820 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3825 BUG_ON(ret
== -ENOENT
);
3826 if (check_path_shared(root
, path
))
3831 btrfs_free_path(path
);
3832 /* Migrate the orphan reservation over */
3834 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3835 &root
->fs_info
->global_block_rsv
,
3836 trans
->bytes_reserved
);
3839 btrfs_end_transaction(trans
, root
);
3840 root
->fs_info
->enospc_unlink
= 0;
3841 return ERR_PTR(err
);
3844 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3848 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3849 struct btrfs_root
*root
)
3851 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3852 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3853 trans
->bytes_reserved
);
3854 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3855 BUG_ON(!root
->fs_info
->enospc_unlink
);
3856 root
->fs_info
->enospc_unlink
= 0;
3858 btrfs_end_transaction(trans
, root
);
3861 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3863 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3864 struct btrfs_trans_handle
*trans
;
3865 struct inode
*inode
= dentry
->d_inode
;
3868 trans
= __unlink_start_trans(dir
, dentry
);
3870 return PTR_ERR(trans
);
3872 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3874 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3875 dentry
->d_name
.name
, dentry
->d_name
.len
);
3879 if (inode
->i_nlink
== 0) {
3880 ret
= btrfs_orphan_add(trans
, inode
);
3886 __unlink_end_trans(trans
, root
);
3887 btrfs_btree_balance_dirty(root
);
3891 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3892 struct btrfs_root
*root
,
3893 struct inode
*dir
, u64 objectid
,
3894 const char *name
, int name_len
)
3896 struct btrfs_path
*path
;
3897 struct extent_buffer
*leaf
;
3898 struct btrfs_dir_item
*di
;
3899 struct btrfs_key key
;
3902 u64 dir_ino
= btrfs_ino(dir
);
3904 path
= btrfs_alloc_path();
3908 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3909 name
, name_len
, -1);
3910 if (IS_ERR_OR_NULL(di
)) {
3918 leaf
= path
->nodes
[0];
3919 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3920 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3921 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3923 btrfs_abort_transaction(trans
, root
, ret
);
3926 btrfs_release_path(path
);
3928 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3929 objectid
, root
->root_key
.objectid
,
3930 dir_ino
, &index
, name
, name_len
);
3932 if (ret
!= -ENOENT
) {
3933 btrfs_abort_transaction(trans
, root
, ret
);
3936 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3938 if (IS_ERR_OR_NULL(di
)) {
3943 btrfs_abort_transaction(trans
, root
, ret
);
3947 leaf
= path
->nodes
[0];
3948 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3949 btrfs_release_path(path
);
3952 btrfs_release_path(path
);
3954 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3956 btrfs_abort_transaction(trans
, root
, ret
);
3960 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3961 inode_inc_iversion(dir
);
3962 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3963 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3965 btrfs_abort_transaction(trans
, root
, ret
);
3967 btrfs_free_path(path
);
3971 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3973 struct inode
*inode
= dentry
->d_inode
;
3975 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3976 struct btrfs_trans_handle
*trans
;
3978 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3980 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3983 trans
= __unlink_start_trans(dir
, dentry
);
3985 return PTR_ERR(trans
);
3987 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3988 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3989 BTRFS_I(inode
)->location
.objectid
,
3990 dentry
->d_name
.name
,
3991 dentry
->d_name
.len
);
3995 err
= btrfs_orphan_add(trans
, inode
);
3999 /* now the directory is empty */
4000 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4001 dentry
->d_name
.name
, dentry
->d_name
.len
);
4003 btrfs_i_size_write(inode
, 0);
4005 __unlink_end_trans(trans
, root
);
4006 btrfs_btree_balance_dirty(root
);
4012 * this can truncate away extent items, csum items and directory items.
4013 * It starts at a high offset and removes keys until it can't find
4014 * any higher than new_size
4016 * csum items that cross the new i_size are truncated to the new size
4019 * min_type is the minimum key type to truncate down to. If set to 0, this
4020 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4022 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4023 struct btrfs_root
*root
,
4024 struct inode
*inode
,
4025 u64 new_size
, u32 min_type
)
4027 struct btrfs_path
*path
;
4028 struct extent_buffer
*leaf
;
4029 struct btrfs_file_extent_item
*fi
;
4030 struct btrfs_key key
;
4031 struct btrfs_key found_key
;
4032 u64 extent_start
= 0;
4033 u64 extent_num_bytes
= 0;
4034 u64 extent_offset
= 0;
4036 u32 found_type
= (u8
)-1;
4039 int pending_del_nr
= 0;
4040 int pending_del_slot
= 0;
4041 int extent_type
= -1;
4044 u64 ino
= btrfs_ino(inode
);
4046 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4048 path
= btrfs_alloc_path();
4054 * We want to drop from the next block forward in case this new size is
4055 * not block aligned since we will be keeping the last block of the
4056 * extent just the way it is.
4058 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
4059 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4060 root
->sectorsize
), (u64
)-1, 0);
4063 * This function is also used to drop the items in the log tree before
4064 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4065 * it is used to drop the loged items. So we shouldn't kill the delayed
4068 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4069 btrfs_kill_delayed_inode_items(inode
);
4072 key
.offset
= (u64
)-1;
4076 path
->leave_spinning
= 1;
4077 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4084 /* there are no items in the tree for us to truncate, we're
4087 if (path
->slots
[0] == 0)
4094 leaf
= path
->nodes
[0];
4095 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4096 found_type
= btrfs_key_type(&found_key
);
4098 if (found_key
.objectid
!= ino
)
4101 if (found_type
< min_type
)
4104 item_end
= found_key
.offset
;
4105 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4106 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4107 struct btrfs_file_extent_item
);
4108 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4109 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4111 btrfs_file_extent_num_bytes(leaf
, fi
);
4112 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4113 item_end
+= btrfs_file_extent_inline_len(leaf
,
4118 if (found_type
> min_type
) {
4121 if (item_end
< new_size
)
4123 if (found_key
.offset
>= new_size
)
4129 /* FIXME, shrink the extent if the ref count is only 1 */
4130 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4133 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4135 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4137 u64 orig_num_bytes
=
4138 btrfs_file_extent_num_bytes(leaf
, fi
);
4139 extent_num_bytes
= ALIGN(new_size
-
4142 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4144 num_dec
= (orig_num_bytes
-
4146 if (root
->ref_cows
&& extent_start
!= 0)
4147 inode_sub_bytes(inode
, num_dec
);
4148 btrfs_mark_buffer_dirty(leaf
);
4151 btrfs_file_extent_disk_num_bytes(leaf
,
4153 extent_offset
= found_key
.offset
-
4154 btrfs_file_extent_offset(leaf
, fi
);
4156 /* FIXME blocksize != 4096 */
4157 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4158 if (extent_start
!= 0) {
4161 inode_sub_bytes(inode
, num_dec
);
4164 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4166 * we can't truncate inline items that have had
4170 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4171 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4172 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4173 u32 size
= new_size
- found_key
.offset
;
4175 if (root
->ref_cows
) {
4176 inode_sub_bytes(inode
, item_end
+ 1 -
4180 btrfs_file_extent_calc_inline_size(size
);
4181 btrfs_truncate_item(root
, path
, size
, 1);
4182 } else if (root
->ref_cows
) {
4183 inode_sub_bytes(inode
, item_end
+ 1 -
4189 if (!pending_del_nr
) {
4190 /* no pending yet, add ourselves */
4191 pending_del_slot
= path
->slots
[0];
4193 } else if (pending_del_nr
&&
4194 path
->slots
[0] + 1 == pending_del_slot
) {
4195 /* hop on the pending chunk */
4197 pending_del_slot
= path
->slots
[0];
4204 if (found_extent
&& (root
->ref_cows
||
4205 root
== root
->fs_info
->tree_root
)) {
4206 btrfs_set_path_blocking(path
);
4207 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4208 extent_num_bytes
, 0,
4209 btrfs_header_owner(leaf
),
4210 ino
, extent_offset
, 0);
4214 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4217 if (path
->slots
[0] == 0 ||
4218 path
->slots
[0] != pending_del_slot
) {
4219 if (pending_del_nr
) {
4220 ret
= btrfs_del_items(trans
, root
, path
,
4224 btrfs_abort_transaction(trans
,
4230 btrfs_release_path(path
);
4237 if (pending_del_nr
) {
4238 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4241 btrfs_abort_transaction(trans
, root
, ret
);
4244 btrfs_free_path(path
);
4249 * btrfs_truncate_page - read, zero a chunk and write a page
4250 * @inode - inode that we're zeroing
4251 * @from - the offset to start zeroing
4252 * @len - the length to zero, 0 to zero the entire range respective to the
4254 * @front - zero up to the offset instead of from the offset on
4256 * This will find the page for the "from" offset and cow the page and zero the
4257 * part we want to zero. This is used with truncate and hole punching.
4259 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4262 struct address_space
*mapping
= inode
->i_mapping
;
4263 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4264 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4265 struct btrfs_ordered_extent
*ordered
;
4266 struct extent_state
*cached_state
= NULL
;
4268 u32 blocksize
= root
->sectorsize
;
4269 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4270 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4272 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4277 if ((offset
& (blocksize
- 1)) == 0 &&
4278 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4280 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4285 page
= find_or_create_page(mapping
, index
, mask
);
4287 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4292 page_start
= page_offset(page
);
4293 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4295 if (!PageUptodate(page
)) {
4296 ret
= btrfs_readpage(NULL
, page
);
4298 if (page
->mapping
!= mapping
) {
4300 page_cache_release(page
);
4303 if (!PageUptodate(page
)) {
4308 wait_on_page_writeback(page
);
4310 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4311 set_page_extent_mapped(page
);
4313 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4315 unlock_extent_cached(io_tree
, page_start
, page_end
,
4316 &cached_state
, GFP_NOFS
);
4318 page_cache_release(page
);
4319 btrfs_start_ordered_extent(inode
, ordered
, 1);
4320 btrfs_put_ordered_extent(ordered
);
4324 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4325 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4326 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4327 0, 0, &cached_state
, GFP_NOFS
);
4329 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4332 unlock_extent_cached(io_tree
, page_start
, page_end
,
4333 &cached_state
, GFP_NOFS
);
4337 if (offset
!= PAGE_CACHE_SIZE
) {
4339 len
= PAGE_CACHE_SIZE
- offset
;
4342 memset(kaddr
, 0, offset
);
4344 memset(kaddr
+ offset
, 0, len
);
4345 flush_dcache_page(page
);
4348 ClearPageChecked(page
);
4349 set_page_dirty(page
);
4350 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4355 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4357 page_cache_release(page
);
4363 * This function puts in dummy file extents for the area we're creating a hole
4364 * for. So if we are truncating this file to a larger size we need to insert
4365 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4366 * the range between oldsize and size
4368 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4370 struct btrfs_trans_handle
*trans
;
4371 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4372 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4373 struct extent_map
*em
= NULL
;
4374 struct extent_state
*cached_state
= NULL
;
4375 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4376 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4377 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4383 if (size
<= hole_start
)
4387 struct btrfs_ordered_extent
*ordered
;
4388 btrfs_wait_ordered_range(inode
, hole_start
,
4389 block_end
- hole_start
);
4390 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4392 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4395 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4396 &cached_state
, GFP_NOFS
);
4397 btrfs_put_ordered_extent(ordered
);
4400 cur_offset
= hole_start
;
4402 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4403 block_end
- cur_offset
, 0);
4409 last_byte
= min(extent_map_end(em
), block_end
);
4410 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4411 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4412 struct extent_map
*hole_em
;
4413 hole_size
= last_byte
- cur_offset
;
4415 trans
= btrfs_start_transaction(root
, 3);
4416 if (IS_ERR(trans
)) {
4417 err
= PTR_ERR(trans
);
4421 err
= btrfs_drop_extents(trans
, root
, inode
,
4423 cur_offset
+ hole_size
, 1);
4425 btrfs_abort_transaction(trans
, root
, err
);
4426 btrfs_end_transaction(trans
, root
);
4430 err
= btrfs_insert_file_extent(trans
, root
,
4431 btrfs_ino(inode
), cur_offset
, 0,
4432 0, hole_size
, 0, hole_size
,
4435 btrfs_abort_transaction(trans
, root
, err
);
4436 btrfs_end_transaction(trans
, root
);
4440 btrfs_drop_extent_cache(inode
, cur_offset
,
4441 cur_offset
+ hole_size
- 1, 0);
4442 hole_em
= alloc_extent_map();
4444 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4445 &BTRFS_I(inode
)->runtime_flags
);
4448 hole_em
->start
= cur_offset
;
4449 hole_em
->len
= hole_size
;
4450 hole_em
->orig_start
= cur_offset
;
4452 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4453 hole_em
->block_len
= 0;
4454 hole_em
->orig_block_len
= 0;
4455 hole_em
->ram_bytes
= hole_size
;
4456 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4457 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4458 hole_em
->generation
= trans
->transid
;
4461 write_lock(&em_tree
->lock
);
4462 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4463 write_unlock(&em_tree
->lock
);
4466 btrfs_drop_extent_cache(inode
, cur_offset
,
4470 free_extent_map(hole_em
);
4472 btrfs_update_inode(trans
, root
, inode
);
4473 btrfs_end_transaction(trans
, root
);
4475 free_extent_map(em
);
4477 cur_offset
= last_byte
;
4478 if (cur_offset
>= block_end
)
4482 free_extent_map(em
);
4483 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4488 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4490 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4491 struct btrfs_trans_handle
*trans
;
4492 loff_t oldsize
= i_size_read(inode
);
4493 loff_t newsize
= attr
->ia_size
;
4494 int mask
= attr
->ia_valid
;
4497 if (newsize
== oldsize
)
4501 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4502 * special case where we need to update the times despite not having
4503 * these flags set. For all other operations the VFS set these flags
4504 * explicitly if it wants a timestamp update.
4506 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4507 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4509 if (newsize
> oldsize
) {
4510 truncate_pagecache(inode
, oldsize
, newsize
);
4511 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4515 trans
= btrfs_start_transaction(root
, 1);
4517 return PTR_ERR(trans
);
4519 i_size_write(inode
, newsize
);
4520 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4521 ret
= btrfs_update_inode(trans
, root
, inode
);
4522 btrfs_end_transaction(trans
, root
);
4526 * We're truncating a file that used to have good data down to
4527 * zero. Make sure it gets into the ordered flush list so that
4528 * any new writes get down to disk quickly.
4531 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4532 &BTRFS_I(inode
)->runtime_flags
);
4535 * 1 for the orphan item we're going to add
4536 * 1 for the orphan item deletion.
4538 trans
= btrfs_start_transaction(root
, 2);
4540 return PTR_ERR(trans
);
4543 * We need to do this in case we fail at _any_ point during the
4544 * actual truncate. Once we do the truncate_setsize we could
4545 * invalidate pages which forces any outstanding ordered io to
4546 * be instantly completed which will give us extents that need
4547 * to be truncated. If we fail to get an orphan inode down we
4548 * could have left over extents that were never meant to live,
4549 * so we need to garuntee from this point on that everything
4550 * will be consistent.
4552 ret
= btrfs_orphan_add(trans
, inode
);
4553 btrfs_end_transaction(trans
, root
);
4557 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4558 truncate_setsize(inode
, newsize
);
4560 /* Disable nonlocked read DIO to avoid the end less truncate */
4561 btrfs_inode_block_unlocked_dio(inode
);
4562 inode_dio_wait(inode
);
4563 btrfs_inode_resume_unlocked_dio(inode
);
4565 ret
= btrfs_truncate(inode
);
4566 if (ret
&& inode
->i_nlink
)
4567 btrfs_orphan_del(NULL
, inode
);
4573 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4575 struct inode
*inode
= dentry
->d_inode
;
4576 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4579 if (btrfs_root_readonly(root
))
4582 err
= inode_change_ok(inode
, attr
);
4586 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4587 err
= btrfs_setsize(inode
, attr
);
4592 if (attr
->ia_valid
) {
4593 setattr_copy(inode
, attr
);
4594 inode_inc_iversion(inode
);
4595 err
= btrfs_dirty_inode(inode
);
4597 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4598 err
= btrfs_acl_chmod(inode
);
4604 void btrfs_evict_inode(struct inode
*inode
)
4606 struct btrfs_trans_handle
*trans
;
4607 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4608 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4609 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4612 trace_btrfs_inode_evict(inode
);
4614 truncate_inode_pages(&inode
->i_data
, 0);
4615 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4616 btrfs_is_free_space_inode(inode
)))
4619 if (is_bad_inode(inode
)) {
4620 btrfs_orphan_del(NULL
, inode
);
4623 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4624 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4626 if (root
->fs_info
->log_root_recovering
) {
4627 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4628 &BTRFS_I(inode
)->runtime_flags
));
4632 if (inode
->i_nlink
> 0) {
4633 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4637 ret
= btrfs_commit_inode_delayed_inode(inode
);
4639 btrfs_orphan_del(NULL
, inode
);
4643 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4645 btrfs_orphan_del(NULL
, inode
);
4648 rsv
->size
= min_size
;
4650 global_rsv
= &root
->fs_info
->global_block_rsv
;
4652 btrfs_i_size_write(inode
, 0);
4655 * This is a bit simpler than btrfs_truncate since we've already
4656 * reserved our space for our orphan item in the unlink, so we just
4657 * need to reserve some slack space in case we add bytes and update
4658 * inode item when doing the truncate.
4661 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4662 BTRFS_RESERVE_FLUSH_LIMIT
);
4665 * Try and steal from the global reserve since we will
4666 * likely not use this space anyway, we want to try as
4667 * hard as possible to get this to work.
4670 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4673 btrfs_warn(root
->fs_info
,
4674 "Could not get space for a delete, will truncate on mount %d",
4676 btrfs_orphan_del(NULL
, inode
);
4677 btrfs_free_block_rsv(root
, rsv
);
4681 trans
= btrfs_join_transaction(root
);
4682 if (IS_ERR(trans
)) {
4683 btrfs_orphan_del(NULL
, inode
);
4684 btrfs_free_block_rsv(root
, rsv
);
4688 trans
->block_rsv
= rsv
;
4690 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4694 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4695 btrfs_end_transaction(trans
, root
);
4697 btrfs_btree_balance_dirty(root
);
4700 btrfs_free_block_rsv(root
, rsv
);
4703 trans
->block_rsv
= root
->orphan_block_rsv
;
4704 ret
= btrfs_orphan_del(trans
, inode
);
4708 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4709 if (!(root
== root
->fs_info
->tree_root
||
4710 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4711 btrfs_return_ino(root
, btrfs_ino(inode
));
4713 btrfs_end_transaction(trans
, root
);
4714 btrfs_btree_balance_dirty(root
);
4721 * this returns the key found in the dir entry in the location pointer.
4722 * If no dir entries were found, location->objectid is 0.
4724 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4725 struct btrfs_key
*location
)
4727 const char *name
= dentry
->d_name
.name
;
4728 int namelen
= dentry
->d_name
.len
;
4729 struct btrfs_dir_item
*di
;
4730 struct btrfs_path
*path
;
4731 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4734 path
= btrfs_alloc_path();
4738 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4743 if (IS_ERR_OR_NULL(di
))
4746 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4748 btrfs_free_path(path
);
4751 location
->objectid
= 0;
4756 * when we hit a tree root in a directory, the btrfs part of the inode
4757 * needs to be changed to reflect the root directory of the tree root. This
4758 * is kind of like crossing a mount point.
4760 static int fixup_tree_root_location(struct btrfs_root
*root
,
4762 struct dentry
*dentry
,
4763 struct btrfs_key
*location
,
4764 struct btrfs_root
**sub_root
)
4766 struct btrfs_path
*path
;
4767 struct btrfs_root
*new_root
;
4768 struct btrfs_root_ref
*ref
;
4769 struct extent_buffer
*leaf
;
4773 path
= btrfs_alloc_path();
4780 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4781 BTRFS_I(dir
)->root
->root_key
.objectid
,
4782 location
->objectid
);
4789 leaf
= path
->nodes
[0];
4790 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4791 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4792 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4795 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4796 (unsigned long)(ref
+ 1),
4797 dentry
->d_name
.len
);
4801 btrfs_release_path(path
);
4803 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4804 if (IS_ERR(new_root
)) {
4805 err
= PTR_ERR(new_root
);
4809 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4814 *sub_root
= new_root
;
4815 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4816 location
->type
= BTRFS_INODE_ITEM_KEY
;
4817 location
->offset
= 0;
4820 btrfs_free_path(path
);
4824 static void inode_tree_add(struct inode
*inode
)
4826 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4827 struct btrfs_inode
*entry
;
4829 struct rb_node
*parent
;
4830 u64 ino
= btrfs_ino(inode
);
4832 p
= &root
->inode_tree
.rb_node
;
4835 if (inode_unhashed(inode
))
4838 spin_lock(&root
->inode_lock
);
4841 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4843 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4844 p
= &parent
->rb_left
;
4845 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4846 p
= &parent
->rb_right
;
4848 WARN_ON(!(entry
->vfs_inode
.i_state
&
4849 (I_WILL_FREE
| I_FREEING
)));
4850 rb_erase(parent
, &root
->inode_tree
);
4851 RB_CLEAR_NODE(parent
);
4852 spin_unlock(&root
->inode_lock
);
4856 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4857 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4858 spin_unlock(&root
->inode_lock
);
4861 static void inode_tree_del(struct inode
*inode
)
4863 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4866 spin_lock(&root
->inode_lock
);
4867 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4868 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4869 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4870 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4872 spin_unlock(&root
->inode_lock
);
4875 * Free space cache has inodes in the tree root, but the tree root has a
4876 * root_refs of 0, so this could end up dropping the tree root as a
4877 * snapshot, so we need the extra !root->fs_info->tree_root check to
4878 * make sure we don't drop it.
4880 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4881 root
!= root
->fs_info
->tree_root
) {
4882 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4883 spin_lock(&root
->inode_lock
);
4884 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4885 spin_unlock(&root
->inode_lock
);
4887 btrfs_add_dead_root(root
);
4891 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4893 struct rb_node
*node
;
4894 struct rb_node
*prev
;
4895 struct btrfs_inode
*entry
;
4896 struct inode
*inode
;
4899 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4901 spin_lock(&root
->inode_lock
);
4903 node
= root
->inode_tree
.rb_node
;
4907 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4909 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4910 node
= node
->rb_left
;
4911 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4912 node
= node
->rb_right
;
4918 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4919 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4923 prev
= rb_next(prev
);
4927 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4928 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4929 inode
= igrab(&entry
->vfs_inode
);
4931 spin_unlock(&root
->inode_lock
);
4932 if (atomic_read(&inode
->i_count
) > 1)
4933 d_prune_aliases(inode
);
4935 * btrfs_drop_inode will have it removed from
4936 * the inode cache when its usage count
4941 spin_lock(&root
->inode_lock
);
4945 if (cond_resched_lock(&root
->inode_lock
))
4948 node
= rb_next(node
);
4950 spin_unlock(&root
->inode_lock
);
4953 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4955 struct btrfs_iget_args
*args
= p
;
4956 inode
->i_ino
= args
->ino
;
4957 BTRFS_I(inode
)->root
= args
->root
;
4961 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4963 struct btrfs_iget_args
*args
= opaque
;
4964 return args
->ino
== btrfs_ino(inode
) &&
4965 args
->root
== BTRFS_I(inode
)->root
;
4968 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4970 struct btrfs_root
*root
)
4972 struct inode
*inode
;
4973 struct btrfs_iget_args args
;
4974 args
.ino
= objectid
;
4977 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4978 btrfs_init_locked_inode
,
4983 /* Get an inode object given its location and corresponding root.
4984 * Returns in *is_new if the inode was read from disk
4986 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4987 struct btrfs_root
*root
, int *new)
4989 struct inode
*inode
;
4991 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4993 return ERR_PTR(-ENOMEM
);
4995 if (inode
->i_state
& I_NEW
) {
4996 BTRFS_I(inode
)->root
= root
;
4997 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4998 btrfs_read_locked_inode(inode
);
4999 if (!is_bad_inode(inode
)) {
5000 inode_tree_add(inode
);
5001 unlock_new_inode(inode
);
5005 unlock_new_inode(inode
);
5007 inode
= ERR_PTR(-ESTALE
);
5014 static struct inode
*new_simple_dir(struct super_block
*s
,
5015 struct btrfs_key
*key
,
5016 struct btrfs_root
*root
)
5018 struct inode
*inode
= new_inode(s
);
5021 return ERR_PTR(-ENOMEM
);
5023 BTRFS_I(inode
)->root
= root
;
5024 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5025 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5027 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5028 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5029 inode
->i_fop
= &simple_dir_operations
;
5030 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5031 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5036 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5038 struct inode
*inode
;
5039 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5040 struct btrfs_root
*sub_root
= root
;
5041 struct btrfs_key location
;
5045 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5046 return ERR_PTR(-ENAMETOOLONG
);
5048 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5050 return ERR_PTR(ret
);
5052 if (location
.objectid
== 0)
5055 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5056 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5060 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5062 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5063 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5064 &location
, &sub_root
);
5067 inode
= ERR_PTR(ret
);
5069 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5071 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5073 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5075 if (!IS_ERR(inode
) && root
!= sub_root
) {
5076 down_read(&root
->fs_info
->cleanup_work_sem
);
5077 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5078 ret
= btrfs_orphan_cleanup(sub_root
);
5079 up_read(&root
->fs_info
->cleanup_work_sem
);
5081 inode
= ERR_PTR(ret
);
5087 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5089 struct btrfs_root
*root
;
5090 struct inode
*inode
= dentry
->d_inode
;
5092 if (!inode
&& !IS_ROOT(dentry
))
5093 inode
= dentry
->d_parent
->d_inode
;
5096 root
= BTRFS_I(inode
)->root
;
5097 if (btrfs_root_refs(&root
->root_item
) == 0)
5100 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5106 static void btrfs_dentry_release(struct dentry
*dentry
)
5108 if (dentry
->d_fsdata
)
5109 kfree(dentry
->d_fsdata
);
5112 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5117 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5121 unsigned char btrfs_filetype_table
[] = {
5122 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5125 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
5128 struct inode
*inode
= file_inode(filp
);
5129 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5130 struct btrfs_item
*item
;
5131 struct btrfs_dir_item
*di
;
5132 struct btrfs_key key
;
5133 struct btrfs_key found_key
;
5134 struct btrfs_path
*path
;
5135 struct list_head ins_list
;
5136 struct list_head del_list
;
5138 struct extent_buffer
*leaf
;
5140 unsigned char d_type
;
5145 int key_type
= BTRFS_DIR_INDEX_KEY
;
5149 int is_curr
= 0; /* filp->f_pos points to the current index? */
5151 /* FIXME, use a real flag for deciding about the key type */
5152 if (root
->fs_info
->tree_root
== root
)
5153 key_type
= BTRFS_DIR_ITEM_KEY
;
5155 /* special case for "." */
5156 if (filp
->f_pos
== 0) {
5157 over
= filldir(dirent
, ".", 1,
5158 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5163 /* special case for .., just use the back ref */
5164 if (filp
->f_pos
== 1) {
5165 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5166 over
= filldir(dirent
, "..", 2,
5167 filp
->f_pos
, pino
, DT_DIR
);
5172 path
= btrfs_alloc_path();
5178 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5179 INIT_LIST_HEAD(&ins_list
);
5180 INIT_LIST_HEAD(&del_list
);
5181 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5184 btrfs_set_key_type(&key
, key_type
);
5185 key
.offset
= filp
->f_pos
;
5186 key
.objectid
= btrfs_ino(inode
);
5188 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5193 leaf
= path
->nodes
[0];
5194 slot
= path
->slots
[0];
5195 if (slot
>= btrfs_header_nritems(leaf
)) {
5196 ret
= btrfs_next_leaf(root
, path
);
5204 item
= btrfs_item_nr(leaf
, slot
);
5205 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5207 if (found_key
.objectid
!= key
.objectid
)
5209 if (btrfs_key_type(&found_key
) != key_type
)
5211 if (found_key
.offset
< filp
->f_pos
)
5213 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5214 btrfs_should_delete_dir_index(&del_list
,
5218 filp
->f_pos
= found_key
.offset
;
5221 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5223 di_total
= btrfs_item_size(leaf
, item
);
5225 while (di_cur
< di_total
) {
5226 struct btrfs_key location
;
5228 if (verify_dir_item(root
, leaf
, di
))
5231 name_len
= btrfs_dir_name_len(leaf
, di
);
5232 if (name_len
<= sizeof(tmp_name
)) {
5233 name_ptr
= tmp_name
;
5235 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5241 read_extent_buffer(leaf
, name_ptr
,
5242 (unsigned long)(di
+ 1), name_len
);
5244 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5245 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5248 /* is this a reference to our own snapshot? If so
5251 * In contrast to old kernels, we insert the snapshot's
5252 * dir item and dir index after it has been created, so
5253 * we won't find a reference to our own snapshot. We
5254 * still keep the following code for backward
5257 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5258 location
.objectid
== root
->root_key
.objectid
) {
5262 over
= filldir(dirent
, name_ptr
, name_len
,
5263 found_key
.offset
, location
.objectid
,
5267 if (name_ptr
!= tmp_name
)
5272 di_len
= btrfs_dir_name_len(leaf
, di
) +
5273 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5275 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5281 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5284 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5290 /* Reached end of directory/root. Bump pos past the last item. */
5291 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5293 * 32-bit glibc will use getdents64, but then strtol -
5294 * so the last number we can serve is this.
5296 filp
->f_pos
= 0x7fffffff;
5302 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5303 btrfs_put_delayed_items(&ins_list
, &del_list
);
5304 btrfs_free_path(path
);
5308 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5310 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5311 struct btrfs_trans_handle
*trans
;
5313 bool nolock
= false;
5315 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5318 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5321 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5323 trans
= btrfs_join_transaction_nolock(root
);
5325 trans
= btrfs_join_transaction(root
);
5327 return PTR_ERR(trans
);
5328 ret
= btrfs_commit_transaction(trans
, root
);
5334 * This is somewhat expensive, updating the tree every time the
5335 * inode changes. But, it is most likely to find the inode in cache.
5336 * FIXME, needs more benchmarking...there are no reasons other than performance
5337 * to keep or drop this code.
5339 int btrfs_dirty_inode(struct inode
*inode
)
5341 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5342 struct btrfs_trans_handle
*trans
;
5345 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5348 trans
= btrfs_join_transaction(root
);
5350 return PTR_ERR(trans
);
5352 ret
= btrfs_update_inode(trans
, root
, inode
);
5353 if (ret
&& ret
== -ENOSPC
) {
5354 /* whoops, lets try again with the full transaction */
5355 btrfs_end_transaction(trans
, root
);
5356 trans
= btrfs_start_transaction(root
, 1);
5358 return PTR_ERR(trans
);
5360 ret
= btrfs_update_inode(trans
, root
, inode
);
5362 btrfs_end_transaction(trans
, root
);
5363 if (BTRFS_I(inode
)->delayed_node
)
5364 btrfs_balance_delayed_items(root
);
5370 * This is a copy of file_update_time. We need this so we can return error on
5371 * ENOSPC for updating the inode in the case of file write and mmap writes.
5373 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5376 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5378 if (btrfs_root_readonly(root
))
5381 if (flags
& S_VERSION
)
5382 inode_inc_iversion(inode
);
5383 if (flags
& S_CTIME
)
5384 inode
->i_ctime
= *now
;
5385 if (flags
& S_MTIME
)
5386 inode
->i_mtime
= *now
;
5387 if (flags
& S_ATIME
)
5388 inode
->i_atime
= *now
;
5389 return btrfs_dirty_inode(inode
);
5393 * find the highest existing sequence number in a directory
5394 * and then set the in-memory index_cnt variable to reflect
5395 * free sequence numbers
5397 static int btrfs_set_inode_index_count(struct inode
*inode
)
5399 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5400 struct btrfs_key key
, found_key
;
5401 struct btrfs_path
*path
;
5402 struct extent_buffer
*leaf
;
5405 key
.objectid
= btrfs_ino(inode
);
5406 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5407 key
.offset
= (u64
)-1;
5409 path
= btrfs_alloc_path();
5413 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5416 /* FIXME: we should be able to handle this */
5422 * MAGIC NUMBER EXPLANATION:
5423 * since we search a directory based on f_pos we have to start at 2
5424 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5425 * else has to start at 2
5427 if (path
->slots
[0] == 0) {
5428 BTRFS_I(inode
)->index_cnt
= 2;
5434 leaf
= path
->nodes
[0];
5435 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5437 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5438 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5439 BTRFS_I(inode
)->index_cnt
= 2;
5443 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5445 btrfs_free_path(path
);
5450 * helper to find a free sequence number in a given directory. This current
5451 * code is very simple, later versions will do smarter things in the btree
5453 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5457 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5458 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5460 ret
= btrfs_set_inode_index_count(dir
);
5466 *index
= BTRFS_I(dir
)->index_cnt
;
5467 BTRFS_I(dir
)->index_cnt
++;
5472 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5473 struct btrfs_root
*root
,
5475 const char *name
, int name_len
,
5476 u64 ref_objectid
, u64 objectid
,
5477 umode_t mode
, u64
*index
)
5479 struct inode
*inode
;
5480 struct btrfs_inode_item
*inode_item
;
5481 struct btrfs_key
*location
;
5482 struct btrfs_path
*path
;
5483 struct btrfs_inode_ref
*ref
;
5484 struct btrfs_key key
[2];
5490 path
= btrfs_alloc_path();
5492 return ERR_PTR(-ENOMEM
);
5494 inode
= new_inode(root
->fs_info
->sb
);
5496 btrfs_free_path(path
);
5497 return ERR_PTR(-ENOMEM
);
5501 * we have to initialize this early, so we can reclaim the inode
5502 * number if we fail afterwards in this function.
5504 inode
->i_ino
= objectid
;
5507 trace_btrfs_inode_request(dir
);
5509 ret
= btrfs_set_inode_index(dir
, index
);
5511 btrfs_free_path(path
);
5513 return ERR_PTR(ret
);
5517 * index_cnt is ignored for everything but a dir,
5518 * btrfs_get_inode_index_count has an explanation for the magic
5521 BTRFS_I(inode
)->index_cnt
= 2;
5522 BTRFS_I(inode
)->root
= root
;
5523 BTRFS_I(inode
)->generation
= trans
->transid
;
5524 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5527 * We could have gotten an inode number from somebody who was fsynced
5528 * and then removed in this same transaction, so let's just set full
5529 * sync since it will be a full sync anyway and this will blow away the
5530 * old info in the log.
5532 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5539 key
[0].objectid
= objectid
;
5540 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5544 * Start new inodes with an inode_ref. This is slightly more
5545 * efficient for small numbers of hard links since they will
5546 * be packed into one item. Extended refs will kick in if we
5547 * add more hard links than can fit in the ref item.
5549 key
[1].objectid
= objectid
;
5550 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5551 key
[1].offset
= ref_objectid
;
5553 sizes
[0] = sizeof(struct btrfs_inode_item
);
5554 sizes
[1] = name_len
+ sizeof(*ref
);
5556 path
->leave_spinning
= 1;
5557 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5561 inode_init_owner(inode
, dir
, mode
);
5562 inode_set_bytes(inode
, 0);
5563 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5564 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5565 struct btrfs_inode_item
);
5566 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5567 sizeof(*inode_item
));
5568 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5570 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5571 struct btrfs_inode_ref
);
5572 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5573 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5574 ptr
= (unsigned long)(ref
+ 1);
5575 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5577 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5578 btrfs_free_path(path
);
5580 location
= &BTRFS_I(inode
)->location
;
5581 location
->objectid
= objectid
;
5582 location
->offset
= 0;
5583 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5585 btrfs_inherit_iflags(inode
, dir
);
5587 if (S_ISREG(mode
)) {
5588 if (btrfs_test_opt(root
, NODATASUM
))
5589 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5590 if (btrfs_test_opt(root
, NODATACOW
))
5591 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5592 BTRFS_INODE_NODATASUM
;
5595 insert_inode_hash(inode
);
5596 inode_tree_add(inode
);
5598 trace_btrfs_inode_new(inode
);
5599 btrfs_set_inode_last_trans(trans
, inode
);
5601 btrfs_update_root_times(trans
, root
);
5606 BTRFS_I(dir
)->index_cnt
--;
5607 btrfs_free_path(path
);
5609 return ERR_PTR(ret
);
5612 static inline u8
btrfs_inode_type(struct inode
*inode
)
5614 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5618 * utility function to add 'inode' into 'parent_inode' with
5619 * a give name and a given sequence number.
5620 * if 'add_backref' is true, also insert a backref from the
5621 * inode to the parent directory.
5623 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5624 struct inode
*parent_inode
, struct inode
*inode
,
5625 const char *name
, int name_len
, int add_backref
, u64 index
)
5628 struct btrfs_key key
;
5629 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5630 u64 ino
= btrfs_ino(inode
);
5631 u64 parent_ino
= btrfs_ino(parent_inode
);
5633 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5634 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5637 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5641 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5642 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5643 key
.objectid
, root
->root_key
.objectid
,
5644 parent_ino
, index
, name
, name_len
);
5645 } else if (add_backref
) {
5646 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5650 /* Nothing to clean up yet */
5654 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5656 btrfs_inode_type(inode
), index
);
5657 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5660 btrfs_abort_transaction(trans
, root
, ret
);
5664 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5666 inode_inc_iversion(parent_inode
);
5667 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5668 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5670 btrfs_abort_transaction(trans
, root
, ret
);
5674 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5677 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5678 key
.objectid
, root
->root_key
.objectid
,
5679 parent_ino
, &local_index
, name
, name_len
);
5681 } else if (add_backref
) {
5685 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5686 ino
, parent_ino
, &local_index
);
5691 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5692 struct inode
*dir
, struct dentry
*dentry
,
5693 struct inode
*inode
, int backref
, u64 index
)
5695 int err
= btrfs_add_link(trans
, dir
, inode
,
5696 dentry
->d_name
.name
, dentry
->d_name
.len
,
5703 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5704 umode_t mode
, dev_t rdev
)
5706 struct btrfs_trans_handle
*trans
;
5707 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5708 struct inode
*inode
= NULL
;
5714 if (!new_valid_dev(rdev
))
5718 * 2 for inode item and ref
5720 * 1 for xattr if selinux is on
5722 trans
= btrfs_start_transaction(root
, 5);
5724 return PTR_ERR(trans
);
5726 err
= btrfs_find_free_ino(root
, &objectid
);
5730 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5731 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5733 if (IS_ERR(inode
)) {
5734 err
= PTR_ERR(inode
);
5738 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5745 * If the active LSM wants to access the inode during
5746 * d_instantiate it needs these. Smack checks to see
5747 * if the filesystem supports xattrs by looking at the
5751 inode
->i_op
= &btrfs_special_inode_operations
;
5752 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5756 init_special_inode(inode
, inode
->i_mode
, rdev
);
5757 btrfs_update_inode(trans
, root
, inode
);
5758 d_instantiate(dentry
, inode
);
5761 btrfs_end_transaction(trans
, root
);
5762 btrfs_btree_balance_dirty(root
);
5764 inode_dec_link_count(inode
);
5770 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5771 umode_t mode
, bool excl
)
5773 struct btrfs_trans_handle
*trans
;
5774 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5775 struct inode
*inode
= NULL
;
5776 int drop_inode_on_err
= 0;
5782 * 2 for inode item and ref
5784 * 1 for xattr if selinux is on
5786 trans
= btrfs_start_transaction(root
, 5);
5788 return PTR_ERR(trans
);
5790 err
= btrfs_find_free_ino(root
, &objectid
);
5794 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5795 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5797 if (IS_ERR(inode
)) {
5798 err
= PTR_ERR(inode
);
5801 drop_inode_on_err
= 1;
5803 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5807 err
= btrfs_update_inode(trans
, root
, inode
);
5812 * If the active LSM wants to access the inode during
5813 * d_instantiate it needs these. Smack checks to see
5814 * if the filesystem supports xattrs by looking at the
5817 inode
->i_fop
= &btrfs_file_operations
;
5818 inode
->i_op
= &btrfs_file_inode_operations
;
5820 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5824 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5825 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5826 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5827 d_instantiate(dentry
, inode
);
5830 btrfs_end_transaction(trans
, root
);
5831 if (err
&& drop_inode_on_err
) {
5832 inode_dec_link_count(inode
);
5835 btrfs_btree_balance_dirty(root
);
5839 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5840 struct dentry
*dentry
)
5842 struct btrfs_trans_handle
*trans
;
5843 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5844 struct inode
*inode
= old_dentry
->d_inode
;
5849 /* do not allow sys_link's with other subvols of the same device */
5850 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5853 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5856 err
= btrfs_set_inode_index(dir
, &index
);
5861 * 2 items for inode and inode ref
5862 * 2 items for dir items
5863 * 1 item for parent inode
5865 trans
= btrfs_start_transaction(root
, 5);
5866 if (IS_ERR(trans
)) {
5867 err
= PTR_ERR(trans
);
5871 btrfs_inc_nlink(inode
);
5872 inode_inc_iversion(inode
);
5873 inode
->i_ctime
= CURRENT_TIME
;
5875 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5877 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5882 struct dentry
*parent
= dentry
->d_parent
;
5883 err
= btrfs_update_inode(trans
, root
, inode
);
5886 d_instantiate(dentry
, inode
);
5887 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5890 btrfs_end_transaction(trans
, root
);
5893 inode_dec_link_count(inode
);
5896 btrfs_btree_balance_dirty(root
);
5900 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5902 struct inode
*inode
= NULL
;
5903 struct btrfs_trans_handle
*trans
;
5904 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5906 int drop_on_err
= 0;
5911 * 2 items for inode and ref
5912 * 2 items for dir items
5913 * 1 for xattr if selinux is on
5915 trans
= btrfs_start_transaction(root
, 5);
5917 return PTR_ERR(trans
);
5919 err
= btrfs_find_free_ino(root
, &objectid
);
5923 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5924 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5925 S_IFDIR
| mode
, &index
);
5926 if (IS_ERR(inode
)) {
5927 err
= PTR_ERR(inode
);
5933 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5937 inode
->i_op
= &btrfs_dir_inode_operations
;
5938 inode
->i_fop
= &btrfs_dir_file_operations
;
5940 btrfs_i_size_write(inode
, 0);
5941 err
= btrfs_update_inode(trans
, root
, inode
);
5945 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5946 dentry
->d_name
.len
, 0, index
);
5950 d_instantiate(dentry
, inode
);
5954 btrfs_end_transaction(trans
, root
);
5957 btrfs_btree_balance_dirty(root
);
5961 /* helper for btfs_get_extent. Given an existing extent in the tree,
5962 * and an extent that you want to insert, deal with overlap and insert
5963 * the new extent into the tree.
5965 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5966 struct extent_map
*existing
,
5967 struct extent_map
*em
,
5968 u64 map_start
, u64 map_len
)
5972 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5973 start_diff
= map_start
- em
->start
;
5974 em
->start
= map_start
;
5976 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5977 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5978 em
->block_start
+= start_diff
;
5979 em
->block_len
-= start_diff
;
5981 return add_extent_mapping(em_tree
, em
, 0);
5984 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5985 struct inode
*inode
, struct page
*page
,
5986 size_t pg_offset
, u64 extent_offset
,
5987 struct btrfs_file_extent_item
*item
)
5990 struct extent_buffer
*leaf
= path
->nodes
[0];
5993 unsigned long inline_size
;
5997 WARN_ON(pg_offset
!= 0);
5998 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5999 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6000 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6001 btrfs_item_nr(leaf
, path
->slots
[0]));
6002 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6005 ptr
= btrfs_file_extent_inline_start(item
);
6007 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6009 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6010 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6011 extent_offset
, inline_size
, max_size
);
6013 char *kaddr
= kmap_atomic(page
);
6014 unsigned long copy_size
= min_t(u64
,
6015 PAGE_CACHE_SIZE
- pg_offset
,
6016 max_size
- extent_offset
);
6017 memset(kaddr
+ pg_offset
, 0, copy_size
);
6018 kunmap_atomic(kaddr
);
6025 * a bit scary, this does extent mapping from logical file offset to the disk.
6026 * the ugly parts come from merging extents from the disk with the in-ram
6027 * representation. This gets more complex because of the data=ordered code,
6028 * where the in-ram extents might be locked pending data=ordered completion.
6030 * This also copies inline extents directly into the page.
6033 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6034 size_t pg_offset
, u64 start
, u64 len
,
6040 u64 extent_start
= 0;
6042 u64 objectid
= btrfs_ino(inode
);
6044 struct btrfs_path
*path
= NULL
;
6045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6046 struct btrfs_file_extent_item
*item
;
6047 struct extent_buffer
*leaf
;
6048 struct btrfs_key found_key
;
6049 struct extent_map
*em
= NULL
;
6050 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6051 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6052 struct btrfs_trans_handle
*trans
= NULL
;
6056 read_lock(&em_tree
->lock
);
6057 em
= lookup_extent_mapping(em_tree
, start
, len
);
6059 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6060 read_unlock(&em_tree
->lock
);
6063 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6064 free_extent_map(em
);
6065 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6066 free_extent_map(em
);
6070 em
= alloc_extent_map();
6075 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6076 em
->start
= EXTENT_MAP_HOLE
;
6077 em
->orig_start
= EXTENT_MAP_HOLE
;
6079 em
->block_len
= (u64
)-1;
6082 path
= btrfs_alloc_path();
6088 * Chances are we'll be called again, so go ahead and do
6094 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6095 objectid
, start
, trans
!= NULL
);
6102 if (path
->slots
[0] == 0)
6107 leaf
= path
->nodes
[0];
6108 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6109 struct btrfs_file_extent_item
);
6110 /* are we inside the extent that was found? */
6111 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6112 found_type
= btrfs_key_type(&found_key
);
6113 if (found_key
.objectid
!= objectid
||
6114 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6118 found_type
= btrfs_file_extent_type(leaf
, item
);
6119 extent_start
= found_key
.offset
;
6120 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6121 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6122 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6123 extent_end
= extent_start
+
6124 btrfs_file_extent_num_bytes(leaf
, item
);
6125 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6127 size
= btrfs_file_extent_inline_len(leaf
, item
);
6128 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6131 if (start
>= extent_end
) {
6133 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6134 ret
= btrfs_next_leaf(root
, path
);
6141 leaf
= path
->nodes
[0];
6143 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6144 if (found_key
.objectid
!= objectid
||
6145 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6147 if (start
+ len
<= found_key
.offset
)
6150 em
->orig_start
= start
;
6151 em
->len
= found_key
.offset
- start
;
6155 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6156 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6157 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6158 em
->start
= extent_start
;
6159 em
->len
= extent_end
- extent_start
;
6160 em
->orig_start
= extent_start
-
6161 btrfs_file_extent_offset(leaf
, item
);
6162 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6164 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6166 em
->block_start
= EXTENT_MAP_HOLE
;
6169 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6170 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6171 em
->compress_type
= compress_type
;
6172 em
->block_start
= bytenr
;
6173 em
->block_len
= em
->orig_block_len
;
6175 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6176 em
->block_start
= bytenr
;
6177 em
->block_len
= em
->len
;
6178 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6179 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6182 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6186 size_t extent_offset
;
6189 em
->block_start
= EXTENT_MAP_INLINE
;
6190 if (!page
|| create
) {
6191 em
->start
= extent_start
;
6192 em
->len
= extent_end
- extent_start
;
6196 size
= btrfs_file_extent_inline_len(leaf
, item
);
6197 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6198 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6199 size
- extent_offset
);
6200 em
->start
= extent_start
+ extent_offset
;
6201 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6202 em
->orig_block_len
= em
->len
;
6203 em
->orig_start
= em
->start
;
6204 if (compress_type
) {
6205 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6206 em
->compress_type
= compress_type
;
6208 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6209 if (create
== 0 && !PageUptodate(page
)) {
6210 if (btrfs_file_extent_compression(leaf
, item
) !=
6211 BTRFS_COMPRESS_NONE
) {
6212 ret
= uncompress_inline(path
, inode
, page
,
6214 extent_offset
, item
);
6215 BUG_ON(ret
); /* -ENOMEM */
6218 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6220 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6221 memset(map
+ pg_offset
+ copy_size
, 0,
6222 PAGE_CACHE_SIZE
- pg_offset
-
6227 flush_dcache_page(page
);
6228 } else if (create
&& PageUptodate(page
)) {
6232 free_extent_map(em
);
6235 btrfs_release_path(path
);
6236 trans
= btrfs_join_transaction(root
);
6239 return ERR_CAST(trans
);
6243 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6246 btrfs_mark_buffer_dirty(leaf
);
6248 set_extent_uptodate(io_tree
, em
->start
,
6249 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6252 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6256 em
->orig_start
= start
;
6259 em
->block_start
= EXTENT_MAP_HOLE
;
6260 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6262 btrfs_release_path(path
);
6263 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6264 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6265 (unsigned long long)em
->start
,
6266 (unsigned long long)em
->len
,
6267 (unsigned long long)start
,
6268 (unsigned long long)len
);
6274 write_lock(&em_tree
->lock
);
6275 ret
= add_extent_mapping(em_tree
, em
, 0);
6276 /* it is possible that someone inserted the extent into the tree
6277 * while we had the lock dropped. It is also possible that
6278 * an overlapping map exists in the tree
6280 if (ret
== -EEXIST
) {
6281 struct extent_map
*existing
;
6285 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6286 if (existing
&& (existing
->start
> start
||
6287 existing
->start
+ existing
->len
<= start
)) {
6288 free_extent_map(existing
);
6292 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6295 err
= merge_extent_mapping(em_tree
, existing
,
6298 free_extent_map(existing
);
6300 free_extent_map(em
);
6305 free_extent_map(em
);
6309 free_extent_map(em
);
6314 write_unlock(&em_tree
->lock
);
6318 trace_btrfs_get_extent(root
, em
);
6321 btrfs_free_path(path
);
6323 ret
= btrfs_end_transaction(trans
, root
);
6328 free_extent_map(em
);
6329 return ERR_PTR(err
);
6331 BUG_ON(!em
); /* Error is always set */
6335 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6336 size_t pg_offset
, u64 start
, u64 len
,
6339 struct extent_map
*em
;
6340 struct extent_map
*hole_em
= NULL
;
6341 u64 range_start
= start
;
6347 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6354 * - a pre-alloc extent,
6355 * there might actually be delalloc bytes behind it.
6357 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6358 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6364 /* check to see if we've wrapped (len == -1 or similar) */
6373 /* ok, we didn't find anything, lets look for delalloc */
6374 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6375 end
, len
, EXTENT_DELALLOC
, 1);
6376 found_end
= range_start
+ found
;
6377 if (found_end
< range_start
)
6378 found_end
= (u64
)-1;
6381 * we didn't find anything useful, return
6382 * the original results from get_extent()
6384 if (range_start
> end
|| found_end
<= start
) {
6390 /* adjust the range_start to make sure it doesn't
6391 * go backwards from the start they passed in
6393 range_start
= max(start
,range_start
);
6394 found
= found_end
- range_start
;
6397 u64 hole_start
= start
;
6400 em
= alloc_extent_map();
6406 * when btrfs_get_extent can't find anything it
6407 * returns one huge hole
6409 * make sure what it found really fits our range, and
6410 * adjust to make sure it is based on the start from
6414 u64 calc_end
= extent_map_end(hole_em
);
6416 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6417 free_extent_map(hole_em
);
6420 hole_start
= max(hole_em
->start
, start
);
6421 hole_len
= calc_end
- hole_start
;
6425 if (hole_em
&& range_start
> hole_start
) {
6426 /* our hole starts before our delalloc, so we
6427 * have to return just the parts of the hole
6428 * that go until the delalloc starts
6430 em
->len
= min(hole_len
,
6431 range_start
- hole_start
);
6432 em
->start
= hole_start
;
6433 em
->orig_start
= hole_start
;
6435 * don't adjust block start at all,
6436 * it is fixed at EXTENT_MAP_HOLE
6438 em
->block_start
= hole_em
->block_start
;
6439 em
->block_len
= hole_len
;
6440 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6441 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6443 em
->start
= range_start
;
6445 em
->orig_start
= range_start
;
6446 em
->block_start
= EXTENT_MAP_DELALLOC
;
6447 em
->block_len
= found
;
6449 } else if (hole_em
) {
6454 free_extent_map(hole_em
);
6456 free_extent_map(em
);
6457 return ERR_PTR(err
);
6462 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6465 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6466 struct btrfs_trans_handle
*trans
;
6467 struct extent_map
*em
;
6468 struct btrfs_key ins
;
6472 trans
= btrfs_join_transaction(root
);
6474 return ERR_CAST(trans
);
6476 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6478 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6479 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6480 alloc_hint
, &ins
, 1);
6486 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6487 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6491 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6492 ins
.offset
, ins
.offset
, 0);
6494 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6498 btrfs_end_transaction(trans
, root
);
6503 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6504 * block must be cow'd
6506 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6507 struct inode
*inode
, u64 offset
, u64
*len
,
6508 u64
*orig_start
, u64
*orig_block_len
,
6511 struct btrfs_path
*path
;
6513 struct extent_buffer
*leaf
;
6514 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6515 struct btrfs_file_extent_item
*fi
;
6516 struct btrfs_key key
;
6524 path
= btrfs_alloc_path();
6528 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6533 slot
= path
->slots
[0];
6536 /* can't find the item, must cow */
6543 leaf
= path
->nodes
[0];
6544 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6545 if (key
.objectid
!= btrfs_ino(inode
) ||
6546 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6547 /* not our file or wrong item type, must cow */
6551 if (key
.offset
> offset
) {
6552 /* Wrong offset, must cow */
6556 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6557 found_type
= btrfs_file_extent_type(leaf
, fi
);
6558 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6559 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6560 /* not a regular extent, must cow */
6563 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6564 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6566 *orig_start
= key
.offset
- backref_offset
;
6567 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6568 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6570 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6571 if (extent_end
< offset
+ *len
) {
6572 /* extent doesn't include our full range, must cow */
6576 if (btrfs_extent_readonly(root
, disk_bytenr
))
6580 * look for other files referencing this extent, if we
6581 * find any we must cow
6583 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6584 key
.offset
- backref_offset
, disk_bytenr
))
6588 * adjust disk_bytenr and num_bytes to cover just the bytes
6589 * in this extent we are about to write. If there
6590 * are any csums in that range we have to cow in order
6591 * to keep the csums correct
6593 disk_bytenr
+= backref_offset
;
6594 disk_bytenr
+= offset
- key
.offset
;
6595 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6596 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6599 * all of the above have passed, it is safe to overwrite this extent
6605 btrfs_free_path(path
);
6609 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6610 struct extent_state
**cached_state
, int writing
)
6612 struct btrfs_ordered_extent
*ordered
;
6616 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6619 * We're concerned with the entire range that we're going to be
6620 * doing DIO to, so we need to make sure theres no ordered
6621 * extents in this range.
6623 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6624 lockend
- lockstart
+ 1);
6627 * We need to make sure there are no buffered pages in this
6628 * range either, we could have raced between the invalidate in
6629 * generic_file_direct_write and locking the extent. The
6630 * invalidate needs to happen so that reads after a write do not
6633 if (!ordered
&& (!writing
||
6634 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6635 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6639 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6640 cached_state
, GFP_NOFS
);
6643 btrfs_start_ordered_extent(inode
, ordered
, 1);
6644 btrfs_put_ordered_extent(ordered
);
6646 /* Screw you mmap */
6647 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6654 * If we found a page that couldn't be invalidated just
6655 * fall back to buffered.
6657 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6658 lockstart
>> PAGE_CACHE_SHIFT
,
6659 lockend
>> PAGE_CACHE_SHIFT
);
6670 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6671 u64 len
, u64 orig_start
,
6672 u64 block_start
, u64 block_len
,
6673 u64 orig_block_len
, u64 ram_bytes
,
6676 struct extent_map_tree
*em_tree
;
6677 struct extent_map
*em
;
6678 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6681 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6682 em
= alloc_extent_map();
6684 return ERR_PTR(-ENOMEM
);
6687 em
->orig_start
= orig_start
;
6688 em
->mod_start
= start
;
6691 em
->block_len
= block_len
;
6692 em
->block_start
= block_start
;
6693 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6694 em
->orig_block_len
= orig_block_len
;
6695 em
->ram_bytes
= ram_bytes
;
6696 em
->generation
= -1;
6697 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6698 if (type
== BTRFS_ORDERED_PREALLOC
)
6699 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6702 btrfs_drop_extent_cache(inode
, em
->start
,
6703 em
->start
+ em
->len
- 1, 0);
6704 write_lock(&em_tree
->lock
);
6705 ret
= add_extent_mapping(em_tree
, em
, 1);
6706 write_unlock(&em_tree
->lock
);
6707 } while (ret
== -EEXIST
);
6710 free_extent_map(em
);
6711 return ERR_PTR(ret
);
6718 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6719 struct buffer_head
*bh_result
, int create
)
6721 struct extent_map
*em
;
6722 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6723 struct extent_state
*cached_state
= NULL
;
6724 u64 start
= iblock
<< inode
->i_blkbits
;
6725 u64 lockstart
, lockend
;
6726 u64 len
= bh_result
->b_size
;
6727 struct btrfs_trans_handle
*trans
;
6728 int unlock_bits
= EXTENT_LOCKED
;
6732 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6734 len
= min_t(u64
, len
, root
->sectorsize
);
6737 lockend
= start
+ len
- 1;
6740 * If this errors out it's because we couldn't invalidate pagecache for
6741 * this range and we need to fallback to buffered.
6743 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6746 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6753 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6754 * io. INLINE is special, and we could probably kludge it in here, but
6755 * it's still buffered so for safety lets just fall back to the generic
6758 * For COMPRESSED we _have_ to read the entire extent in so we can
6759 * decompress it, so there will be buffering required no matter what we
6760 * do, so go ahead and fallback to buffered.
6762 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6763 * to buffered IO. Don't blame me, this is the price we pay for using
6766 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6767 em
->block_start
== EXTENT_MAP_INLINE
) {
6768 free_extent_map(em
);
6773 /* Just a good old fashioned hole, return */
6774 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6775 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6776 free_extent_map(em
);
6781 * We don't allocate a new extent in the following cases
6783 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6785 * 2) The extent is marked as PREALLOC. We're good to go here and can
6786 * just use the extent.
6790 len
= min(len
, em
->len
- (start
- em
->start
));
6791 lockstart
= start
+ len
;
6795 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6796 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6797 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6800 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6802 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6803 type
= BTRFS_ORDERED_PREALLOC
;
6805 type
= BTRFS_ORDERED_NOCOW
;
6806 len
= min(len
, em
->len
- (start
- em
->start
));
6807 block_start
= em
->block_start
+ (start
- em
->start
);
6810 * we're not going to log anything, but we do need
6811 * to make sure the current transaction stays open
6812 * while we look for nocow cross refs
6814 trans
= btrfs_join_transaction(root
);
6818 if (can_nocow_odirect(trans
, inode
, start
, &len
, &orig_start
,
6819 &orig_block_len
, &ram_bytes
) == 1) {
6820 if (type
== BTRFS_ORDERED_PREALLOC
) {
6821 free_extent_map(em
);
6822 em
= create_pinned_em(inode
, start
, len
,
6828 btrfs_end_transaction(trans
, root
);
6833 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6834 block_start
, len
, len
, type
);
6835 btrfs_end_transaction(trans
, root
);
6837 free_extent_map(em
);
6842 btrfs_end_transaction(trans
, root
);
6846 * this will cow the extent, reset the len in case we changed
6849 len
= bh_result
->b_size
;
6850 free_extent_map(em
);
6851 em
= btrfs_new_extent_direct(inode
, start
, len
);
6856 len
= min(len
, em
->len
- (start
- em
->start
));
6858 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6860 bh_result
->b_size
= len
;
6861 bh_result
->b_bdev
= em
->bdev
;
6862 set_buffer_mapped(bh_result
);
6864 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6865 set_buffer_new(bh_result
);
6868 * Need to update the i_size under the extent lock so buffered
6869 * readers will get the updated i_size when we unlock.
6871 if (start
+ len
> i_size_read(inode
))
6872 i_size_write(inode
, start
+ len
);
6874 spin_lock(&BTRFS_I(inode
)->lock
);
6875 BTRFS_I(inode
)->outstanding_extents
++;
6876 spin_unlock(&BTRFS_I(inode
)->lock
);
6878 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6879 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6880 &cached_state
, GFP_NOFS
);
6885 * In the case of write we need to clear and unlock the entire range,
6886 * in the case of read we need to unlock only the end area that we
6887 * aren't using if there is any left over space.
6889 if (lockstart
< lockend
) {
6890 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6891 lockend
, unlock_bits
, 1, 0,
6892 &cached_state
, GFP_NOFS
);
6894 free_extent_state(cached_state
);
6897 free_extent_map(em
);
6902 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6903 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6907 struct btrfs_dio_private
{
6908 struct inode
*inode
;
6914 /* number of bios pending for this dio */
6915 atomic_t pending_bios
;
6920 struct bio
*orig_bio
;
6923 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6925 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6926 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6927 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6928 struct inode
*inode
= dip
->inode
;
6929 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6932 start
= dip
->logical_offset
;
6934 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6935 struct page
*page
= bvec
->bv_page
;
6938 u64
private = ~(u32
)0;
6939 unsigned long flags
;
6941 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6944 local_irq_save(flags
);
6945 kaddr
= kmap_atomic(page
);
6946 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6947 csum
, bvec
->bv_len
);
6948 btrfs_csum_final(csum
, (char *)&csum
);
6949 kunmap_atomic(kaddr
);
6950 local_irq_restore(flags
);
6952 flush_dcache_page(bvec
->bv_page
);
6953 if (csum
!= private) {
6955 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6956 (unsigned long long)btrfs_ino(inode
),
6957 (unsigned long long)start
,
6958 csum
, (unsigned)private);
6963 start
+= bvec
->bv_len
;
6965 } while (bvec
<= bvec_end
);
6967 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6968 dip
->logical_offset
+ dip
->bytes
- 1);
6969 bio
->bi_private
= dip
->private;
6973 /* If we had a csum failure make sure to clear the uptodate flag */
6975 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6976 dio_end_io(bio
, err
);
6979 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6981 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6982 struct inode
*inode
= dip
->inode
;
6983 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6984 struct btrfs_ordered_extent
*ordered
= NULL
;
6985 u64 ordered_offset
= dip
->logical_offset
;
6986 u64 ordered_bytes
= dip
->bytes
;
6992 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6994 ordered_bytes
, !err
);
6998 ordered
->work
.func
= finish_ordered_fn
;
6999 ordered
->work
.flags
= 0;
7000 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
7004 * our bio might span multiple ordered extents. If we haven't
7005 * completed the accounting for the whole dio, go back and try again
7007 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7008 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7014 bio
->bi_private
= dip
->private;
7018 /* If we had an error make sure to clear the uptodate flag */
7020 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
7021 dio_end_io(bio
, err
);
7024 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7025 struct bio
*bio
, int mirror_num
,
7026 unsigned long bio_flags
, u64 offset
)
7029 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7030 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7031 BUG_ON(ret
); /* -ENOMEM */
7035 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7037 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7040 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
7041 "sector %#Lx len %u err no %d\n",
7042 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
7043 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
7047 * before atomic variable goto zero, we must make sure
7048 * dip->errors is perceived to be set.
7050 smp_mb__before_atomic_dec();
7053 /* if there are more bios still pending for this dio, just exit */
7054 if (!atomic_dec_and_test(&dip
->pending_bios
))
7058 bio_io_error(dip
->orig_bio
);
7060 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
7061 bio_endio(dip
->orig_bio
, 0);
7067 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7068 u64 first_sector
, gfp_t gfp_flags
)
7070 int nr_vecs
= bio_get_nr_vecs(bdev
);
7071 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7074 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7075 int rw
, u64 file_offset
, int skip_sum
,
7078 int write
= rw
& REQ_WRITE
;
7079 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7083 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7088 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7096 if (write
&& async_submit
) {
7097 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7098 inode
, rw
, bio
, 0, 0,
7100 __btrfs_submit_bio_start_direct_io
,
7101 __btrfs_submit_bio_done
);
7105 * If we aren't doing async submit, calculate the csum of the
7108 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7111 } else if (!skip_sum
) {
7112 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7118 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7124 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7127 struct inode
*inode
= dip
->inode
;
7128 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7130 struct bio
*orig_bio
= dip
->orig_bio
;
7131 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7132 u64 start_sector
= orig_bio
->bi_sector
;
7133 u64 file_offset
= dip
->logical_offset
;
7138 int async_submit
= 0;
7140 map_length
= orig_bio
->bi_size
;
7141 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7142 &map_length
, NULL
, 0);
7147 if (map_length
>= orig_bio
->bi_size
) {
7152 /* async crcs make it difficult to collect full stripe writes. */
7153 if (btrfs_get_alloc_profile(root
, 1) &
7154 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7159 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7162 bio
->bi_private
= dip
;
7163 bio
->bi_end_io
= btrfs_end_dio_bio
;
7164 atomic_inc(&dip
->pending_bios
);
7166 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7167 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7168 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7169 bvec
->bv_offset
) < bvec
->bv_len
)) {
7171 * inc the count before we submit the bio so
7172 * we know the end IO handler won't happen before
7173 * we inc the count. Otherwise, the dip might get freed
7174 * before we're done setting it up
7176 atomic_inc(&dip
->pending_bios
);
7177 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7178 file_offset
, skip_sum
,
7182 atomic_dec(&dip
->pending_bios
);
7186 start_sector
+= submit_len
>> 9;
7187 file_offset
+= submit_len
;
7192 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7193 start_sector
, GFP_NOFS
);
7196 bio
->bi_private
= dip
;
7197 bio
->bi_end_io
= btrfs_end_dio_bio
;
7199 map_length
= orig_bio
->bi_size
;
7200 ret
= btrfs_map_block(root
->fs_info
, rw
,
7202 &map_length
, NULL
, 0);
7208 submit_len
+= bvec
->bv_len
;
7215 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7224 * before atomic variable goto zero, we must
7225 * make sure dip->errors is perceived to be set.
7227 smp_mb__before_atomic_dec();
7228 if (atomic_dec_and_test(&dip
->pending_bios
))
7229 bio_io_error(dip
->orig_bio
);
7231 /* bio_end_io() will handle error, so we needn't return it */
7235 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
7238 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7239 struct btrfs_dio_private
*dip
;
7240 struct bio_vec
*bvec
= bio
->bi_io_vec
;
7242 int write
= rw
& REQ_WRITE
;
7245 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7247 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7253 dip
->private = bio
->bi_private
;
7255 dip
->logical_offset
= file_offset
;
7259 dip
->bytes
+= bvec
->bv_len
;
7261 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
7263 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
7264 bio
->bi_private
= dip
;
7266 dip
->orig_bio
= bio
;
7267 atomic_set(&dip
->pending_bios
, 0);
7270 bio
->bi_end_io
= btrfs_endio_direct_write
;
7272 bio
->bi_end_io
= btrfs_endio_direct_read
;
7274 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7279 * If this is a write, we need to clean up the reserved space and kill
7280 * the ordered extent.
7283 struct btrfs_ordered_extent
*ordered
;
7284 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7285 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7286 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7287 btrfs_free_reserved_extent(root
, ordered
->start
,
7289 btrfs_put_ordered_extent(ordered
);
7290 btrfs_put_ordered_extent(ordered
);
7292 bio_endio(bio
, ret
);
7295 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7296 const struct iovec
*iov
, loff_t offset
,
7297 unsigned long nr_segs
)
7303 unsigned blocksize_mask
= root
->sectorsize
- 1;
7304 ssize_t retval
= -EINVAL
;
7305 loff_t end
= offset
;
7307 if (offset
& blocksize_mask
)
7310 /* Check the memory alignment. Blocks cannot straddle pages */
7311 for (seg
= 0; seg
< nr_segs
; seg
++) {
7312 addr
= (unsigned long)iov
[seg
].iov_base
;
7313 size
= iov
[seg
].iov_len
;
7315 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7318 /* If this is a write we don't need to check anymore */
7323 * Check to make sure we don't have duplicate iov_base's in this
7324 * iovec, if so return EINVAL, otherwise we'll get csum errors
7325 * when reading back.
7327 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7328 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7337 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7338 const struct iovec
*iov
, loff_t offset
,
7339 unsigned long nr_segs
)
7341 struct file
*file
= iocb
->ki_filp
;
7342 struct inode
*inode
= file
->f_mapping
->host
;
7346 bool relock
= false;
7349 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7353 atomic_inc(&inode
->i_dio_count
);
7354 smp_mb__after_atomic_inc();
7357 count
= iov_length(iov
, nr_segs
);
7359 * If the write DIO is beyond the EOF, we need update
7360 * the isize, but it is protected by i_mutex. So we can
7361 * not unlock the i_mutex at this case.
7363 if (offset
+ count
<= inode
->i_size
) {
7364 mutex_unlock(&inode
->i_mutex
);
7367 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7370 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7371 &BTRFS_I(inode
)->runtime_flags
))) {
7372 inode_dio_done(inode
);
7373 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7377 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7378 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7379 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7380 btrfs_submit_direct
, flags
);
7382 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7383 btrfs_delalloc_release_space(inode
, count
);
7384 else if (ret
>= 0 && (size_t)ret
< count
)
7385 btrfs_delalloc_release_space(inode
,
7386 count
- (size_t)ret
);
7388 btrfs_delalloc_release_metadata(inode
, 0);
7392 inode_dio_done(inode
);
7394 mutex_lock(&inode
->i_mutex
);
7399 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7401 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7402 __u64 start
, __u64 len
)
7406 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7410 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7413 int btrfs_readpage(struct file
*file
, struct page
*page
)
7415 struct extent_io_tree
*tree
;
7416 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7417 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7420 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7422 struct extent_io_tree
*tree
;
7425 if (current
->flags
& PF_MEMALLOC
) {
7426 redirty_page_for_writepage(wbc
, page
);
7430 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7431 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7434 int btrfs_writepages(struct address_space
*mapping
,
7435 struct writeback_control
*wbc
)
7437 struct extent_io_tree
*tree
;
7439 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7440 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7444 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7445 struct list_head
*pages
, unsigned nr_pages
)
7447 struct extent_io_tree
*tree
;
7448 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7449 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7452 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7454 struct extent_io_tree
*tree
;
7455 struct extent_map_tree
*map
;
7458 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7459 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7460 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7462 ClearPagePrivate(page
);
7463 set_page_private(page
, 0);
7464 page_cache_release(page
);
7469 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7471 if (PageWriteback(page
) || PageDirty(page
))
7473 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7476 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7478 struct inode
*inode
= page
->mapping
->host
;
7479 struct extent_io_tree
*tree
;
7480 struct btrfs_ordered_extent
*ordered
;
7481 struct extent_state
*cached_state
= NULL
;
7482 u64 page_start
= page_offset(page
);
7483 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7486 * we have the page locked, so new writeback can't start,
7487 * and the dirty bit won't be cleared while we are here.
7489 * Wait for IO on this page so that we can safely clear
7490 * the PagePrivate2 bit and do ordered accounting
7492 wait_on_page_writeback(page
);
7494 tree
= &BTRFS_I(inode
)->io_tree
;
7496 btrfs_releasepage(page
, GFP_NOFS
);
7499 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7500 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7503 * IO on this page will never be started, so we need
7504 * to account for any ordered extents now
7506 clear_extent_bit(tree
, page_start
, page_end
,
7507 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7508 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7509 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7511 * whoever cleared the private bit is responsible
7512 * for the finish_ordered_io
7514 if (TestClearPagePrivate2(page
) &&
7515 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7516 PAGE_CACHE_SIZE
, 1)) {
7517 btrfs_finish_ordered_io(ordered
);
7519 btrfs_put_ordered_extent(ordered
);
7520 cached_state
= NULL
;
7521 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7523 clear_extent_bit(tree
, page_start
, page_end
,
7524 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7525 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7526 &cached_state
, GFP_NOFS
);
7527 __btrfs_releasepage(page
, GFP_NOFS
);
7529 ClearPageChecked(page
);
7530 if (PagePrivate(page
)) {
7531 ClearPagePrivate(page
);
7532 set_page_private(page
, 0);
7533 page_cache_release(page
);
7538 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7539 * called from a page fault handler when a page is first dirtied. Hence we must
7540 * be careful to check for EOF conditions here. We set the page up correctly
7541 * for a written page which means we get ENOSPC checking when writing into
7542 * holes and correct delalloc and unwritten extent mapping on filesystems that
7543 * support these features.
7545 * We are not allowed to take the i_mutex here so we have to play games to
7546 * protect against truncate races as the page could now be beyond EOF. Because
7547 * vmtruncate() writes the inode size before removing pages, once we have the
7548 * page lock we can determine safely if the page is beyond EOF. If it is not
7549 * beyond EOF, then the page is guaranteed safe against truncation until we
7552 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7554 struct page
*page
= vmf
->page
;
7555 struct inode
*inode
= file_inode(vma
->vm_file
);
7556 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7557 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7558 struct btrfs_ordered_extent
*ordered
;
7559 struct extent_state
*cached_state
= NULL
;
7561 unsigned long zero_start
;
7568 sb_start_pagefault(inode
->i_sb
);
7569 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7571 ret
= file_update_time(vma
->vm_file
);
7577 else /* -ENOSPC, -EIO, etc */
7578 ret
= VM_FAULT_SIGBUS
;
7584 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7587 size
= i_size_read(inode
);
7588 page_start
= page_offset(page
);
7589 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7591 if ((page
->mapping
!= inode
->i_mapping
) ||
7592 (page_start
>= size
)) {
7593 /* page got truncated out from underneath us */
7596 wait_on_page_writeback(page
);
7598 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7599 set_page_extent_mapped(page
);
7602 * we can't set the delalloc bits if there are pending ordered
7603 * extents. Drop our locks and wait for them to finish
7605 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7607 unlock_extent_cached(io_tree
, page_start
, page_end
,
7608 &cached_state
, GFP_NOFS
);
7610 btrfs_start_ordered_extent(inode
, ordered
, 1);
7611 btrfs_put_ordered_extent(ordered
);
7616 * XXX - page_mkwrite gets called every time the page is dirtied, even
7617 * if it was already dirty, so for space accounting reasons we need to
7618 * clear any delalloc bits for the range we are fixing to save. There
7619 * is probably a better way to do this, but for now keep consistent with
7620 * prepare_pages in the normal write path.
7622 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7623 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7624 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7625 0, 0, &cached_state
, GFP_NOFS
);
7627 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7630 unlock_extent_cached(io_tree
, page_start
, page_end
,
7631 &cached_state
, GFP_NOFS
);
7632 ret
= VM_FAULT_SIGBUS
;
7637 /* page is wholly or partially inside EOF */
7638 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7639 zero_start
= size
& ~PAGE_CACHE_MASK
;
7641 zero_start
= PAGE_CACHE_SIZE
;
7643 if (zero_start
!= PAGE_CACHE_SIZE
) {
7645 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7646 flush_dcache_page(page
);
7649 ClearPageChecked(page
);
7650 set_page_dirty(page
);
7651 SetPageUptodate(page
);
7653 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7654 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7655 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7657 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7661 sb_end_pagefault(inode
->i_sb
);
7662 return VM_FAULT_LOCKED
;
7666 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7668 sb_end_pagefault(inode
->i_sb
);
7672 static int btrfs_truncate(struct inode
*inode
)
7674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7675 struct btrfs_block_rsv
*rsv
;
7678 struct btrfs_trans_handle
*trans
;
7679 u64 mask
= root
->sectorsize
- 1;
7680 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7682 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7686 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7687 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7690 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7691 * 3 things going on here
7693 * 1) We need to reserve space for our orphan item and the space to
7694 * delete our orphan item. Lord knows we don't want to have a dangling
7695 * orphan item because we didn't reserve space to remove it.
7697 * 2) We need to reserve space to update our inode.
7699 * 3) We need to have something to cache all the space that is going to
7700 * be free'd up by the truncate operation, but also have some slack
7701 * space reserved in case it uses space during the truncate (thank you
7702 * very much snapshotting).
7704 * And we need these to all be seperate. The fact is we can use alot of
7705 * space doing the truncate, and we have no earthly idea how much space
7706 * we will use, so we need the truncate reservation to be seperate so it
7707 * doesn't end up using space reserved for updating the inode or
7708 * removing the orphan item. We also need to be able to stop the
7709 * transaction and start a new one, which means we need to be able to
7710 * update the inode several times, and we have no idea of knowing how
7711 * many times that will be, so we can't just reserve 1 item for the
7712 * entirety of the opration, so that has to be done seperately as well.
7713 * Then there is the orphan item, which does indeed need to be held on
7714 * to for the whole operation, and we need nobody to touch this reserved
7715 * space except the orphan code.
7717 * So that leaves us with
7719 * 1) root->orphan_block_rsv - for the orphan deletion.
7720 * 2) rsv - for the truncate reservation, which we will steal from the
7721 * transaction reservation.
7722 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7723 * updating the inode.
7725 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7728 rsv
->size
= min_size
;
7732 * 1 for the truncate slack space
7733 * 1 for updating the inode.
7735 trans
= btrfs_start_transaction(root
, 2);
7736 if (IS_ERR(trans
)) {
7737 err
= PTR_ERR(trans
);
7741 /* Migrate the slack space for the truncate to our reserve */
7742 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7747 * setattr is responsible for setting the ordered_data_close flag,
7748 * but that is only tested during the last file release. That
7749 * could happen well after the next commit, leaving a great big
7750 * window where new writes may get lost if someone chooses to write
7751 * to this file after truncating to zero
7753 * The inode doesn't have any dirty data here, and so if we commit
7754 * this is a noop. If someone immediately starts writing to the inode
7755 * it is very likely we'll catch some of their writes in this
7756 * transaction, and the commit will find this file on the ordered
7757 * data list with good things to send down.
7759 * This is a best effort solution, there is still a window where
7760 * using truncate to replace the contents of the file will
7761 * end up with a zero length file after a crash.
7763 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7764 &BTRFS_I(inode
)->runtime_flags
))
7765 btrfs_add_ordered_operation(trans
, root
, inode
);
7768 * So if we truncate and then write and fsync we normally would just
7769 * write the extents that changed, which is a problem if we need to
7770 * first truncate that entire inode. So set this flag so we write out
7771 * all of the extents in the inode to the sync log so we're completely
7774 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7775 trans
->block_rsv
= rsv
;
7778 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7780 BTRFS_EXTENT_DATA_KEY
);
7781 if (ret
!= -ENOSPC
) {
7786 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7787 ret
= btrfs_update_inode(trans
, root
, inode
);
7793 btrfs_end_transaction(trans
, root
);
7794 btrfs_btree_balance_dirty(root
);
7796 trans
= btrfs_start_transaction(root
, 2);
7797 if (IS_ERR(trans
)) {
7798 ret
= err
= PTR_ERR(trans
);
7803 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7805 BUG_ON(ret
); /* shouldn't happen */
7806 trans
->block_rsv
= rsv
;
7809 if (ret
== 0 && inode
->i_nlink
> 0) {
7810 trans
->block_rsv
= root
->orphan_block_rsv
;
7811 ret
= btrfs_orphan_del(trans
, inode
);
7817 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7818 ret
= btrfs_update_inode(trans
, root
, inode
);
7822 ret
= btrfs_end_transaction(trans
, root
);
7823 btrfs_btree_balance_dirty(root
);
7827 btrfs_free_block_rsv(root
, rsv
);
7836 * create a new subvolume directory/inode (helper for the ioctl).
7838 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7839 struct btrfs_root
*new_root
, u64 new_dirid
)
7841 struct inode
*inode
;
7845 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7846 new_dirid
, new_dirid
,
7847 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7850 return PTR_ERR(inode
);
7851 inode
->i_op
= &btrfs_dir_inode_operations
;
7852 inode
->i_fop
= &btrfs_dir_file_operations
;
7854 set_nlink(inode
, 1);
7855 btrfs_i_size_write(inode
, 0);
7857 err
= btrfs_update_inode(trans
, new_root
, inode
);
7863 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7865 struct btrfs_inode
*ei
;
7866 struct inode
*inode
;
7868 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7875 ei
->last_sub_trans
= 0;
7876 ei
->logged_trans
= 0;
7877 ei
->delalloc_bytes
= 0;
7878 ei
->disk_i_size
= 0;
7881 ei
->index_cnt
= (u64
)-1;
7882 ei
->last_unlink_trans
= 0;
7883 ei
->last_log_commit
= 0;
7885 spin_lock_init(&ei
->lock
);
7886 ei
->outstanding_extents
= 0;
7887 ei
->reserved_extents
= 0;
7889 ei
->runtime_flags
= 0;
7890 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7892 ei
->delayed_node
= NULL
;
7894 inode
= &ei
->vfs_inode
;
7895 extent_map_tree_init(&ei
->extent_tree
);
7896 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7897 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7898 ei
->io_tree
.track_uptodate
= 1;
7899 ei
->io_failure_tree
.track_uptodate
= 1;
7900 atomic_set(&ei
->sync_writers
, 0);
7901 mutex_init(&ei
->log_mutex
);
7902 mutex_init(&ei
->delalloc_mutex
);
7903 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7904 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7905 INIT_LIST_HEAD(&ei
->ordered_operations
);
7906 RB_CLEAR_NODE(&ei
->rb_node
);
7911 static void btrfs_i_callback(struct rcu_head
*head
)
7913 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7914 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7917 void btrfs_destroy_inode(struct inode
*inode
)
7919 struct btrfs_ordered_extent
*ordered
;
7920 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7922 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7923 WARN_ON(inode
->i_data
.nrpages
);
7924 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7925 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7926 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7927 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7930 * This can happen where we create an inode, but somebody else also
7931 * created the same inode and we need to destroy the one we already
7938 * Make sure we're properly removed from the ordered operation
7942 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7943 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7944 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7945 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7948 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7949 &BTRFS_I(inode
)->runtime_flags
)) {
7950 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7951 (unsigned long long)btrfs_ino(inode
));
7952 atomic_dec(&root
->orphan_inodes
);
7956 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7960 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7961 (unsigned long long)ordered
->file_offset
,
7962 (unsigned long long)ordered
->len
);
7963 btrfs_remove_ordered_extent(inode
, ordered
);
7964 btrfs_put_ordered_extent(ordered
);
7965 btrfs_put_ordered_extent(ordered
);
7968 inode_tree_del(inode
);
7969 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7971 btrfs_remove_delayed_node(inode
);
7972 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7975 int btrfs_drop_inode(struct inode
*inode
)
7977 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7979 /* the snap/subvol tree is on deleting */
7980 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7981 root
!= root
->fs_info
->tree_root
)
7984 return generic_drop_inode(inode
);
7987 static void init_once(void *foo
)
7989 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7991 inode_init_once(&ei
->vfs_inode
);
7994 void btrfs_destroy_cachep(void)
7997 * Make sure all delayed rcu free inodes are flushed before we
8001 if (btrfs_inode_cachep
)
8002 kmem_cache_destroy(btrfs_inode_cachep
);
8003 if (btrfs_trans_handle_cachep
)
8004 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8005 if (btrfs_transaction_cachep
)
8006 kmem_cache_destroy(btrfs_transaction_cachep
);
8007 if (btrfs_path_cachep
)
8008 kmem_cache_destroy(btrfs_path_cachep
);
8009 if (btrfs_free_space_cachep
)
8010 kmem_cache_destroy(btrfs_free_space_cachep
);
8011 if (btrfs_delalloc_work_cachep
)
8012 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8015 int btrfs_init_cachep(void)
8017 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8018 sizeof(struct btrfs_inode
), 0,
8019 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8020 if (!btrfs_inode_cachep
)
8023 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8024 sizeof(struct btrfs_trans_handle
), 0,
8025 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8026 if (!btrfs_trans_handle_cachep
)
8029 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8030 sizeof(struct btrfs_transaction
), 0,
8031 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8032 if (!btrfs_transaction_cachep
)
8035 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8036 sizeof(struct btrfs_path
), 0,
8037 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8038 if (!btrfs_path_cachep
)
8041 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8042 sizeof(struct btrfs_free_space
), 0,
8043 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8044 if (!btrfs_free_space_cachep
)
8047 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8048 sizeof(struct btrfs_delalloc_work
), 0,
8049 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8051 if (!btrfs_delalloc_work_cachep
)
8056 btrfs_destroy_cachep();
8060 static int btrfs_getattr(struct vfsmount
*mnt
,
8061 struct dentry
*dentry
, struct kstat
*stat
)
8064 struct inode
*inode
= dentry
->d_inode
;
8065 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8067 generic_fillattr(inode
, stat
);
8068 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8069 stat
->blksize
= PAGE_CACHE_SIZE
;
8071 spin_lock(&BTRFS_I(inode
)->lock
);
8072 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8073 spin_unlock(&BTRFS_I(inode
)->lock
);
8074 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8075 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8079 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8080 struct inode
*new_dir
, struct dentry
*new_dentry
)
8082 struct btrfs_trans_handle
*trans
;
8083 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8084 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8085 struct inode
*new_inode
= new_dentry
->d_inode
;
8086 struct inode
*old_inode
= old_dentry
->d_inode
;
8087 struct timespec ctime
= CURRENT_TIME
;
8091 u64 old_ino
= btrfs_ino(old_inode
);
8093 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8096 /* we only allow rename subvolume link between subvolumes */
8097 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8100 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8101 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8104 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8105 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8109 /* check for collisions, even if the name isn't there */
8110 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8111 new_dentry
->d_name
.name
,
8112 new_dentry
->d_name
.len
);
8115 if (ret
== -EEXIST
) {
8117 * eexist without a new_inode */
8123 /* maybe -EOVERFLOW */
8130 * we're using rename to replace one file with another.
8131 * and the replacement file is large. Start IO on it now so
8132 * we don't add too much work to the end of the transaction
8134 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8135 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8136 filemap_flush(old_inode
->i_mapping
);
8138 /* close the racy window with snapshot create/destroy ioctl */
8139 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8140 down_read(&root
->fs_info
->subvol_sem
);
8142 * We want to reserve the absolute worst case amount of items. So if
8143 * both inodes are subvols and we need to unlink them then that would
8144 * require 4 item modifications, but if they are both normal inodes it
8145 * would require 5 item modifications, so we'll assume their normal
8146 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8147 * should cover the worst case number of items we'll modify.
8149 trans
= btrfs_start_transaction(root
, 11);
8150 if (IS_ERR(trans
)) {
8151 ret
= PTR_ERR(trans
);
8156 btrfs_record_root_in_trans(trans
, dest
);
8158 ret
= btrfs_set_inode_index(new_dir
, &index
);
8162 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8163 /* force full log commit if subvolume involved. */
8164 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8166 ret
= btrfs_insert_inode_ref(trans
, dest
,
8167 new_dentry
->d_name
.name
,
8168 new_dentry
->d_name
.len
,
8170 btrfs_ino(new_dir
), index
);
8174 * this is an ugly little race, but the rename is required
8175 * to make sure that if we crash, the inode is either at the
8176 * old name or the new one. pinning the log transaction lets
8177 * us make sure we don't allow a log commit to come in after
8178 * we unlink the name but before we add the new name back in.
8180 btrfs_pin_log_trans(root
);
8183 * make sure the inode gets flushed if it is replacing
8186 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8187 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8189 inode_inc_iversion(old_dir
);
8190 inode_inc_iversion(new_dir
);
8191 inode_inc_iversion(old_inode
);
8192 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8193 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8194 old_inode
->i_ctime
= ctime
;
8196 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8197 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8199 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8200 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8201 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8202 old_dentry
->d_name
.name
,
8203 old_dentry
->d_name
.len
);
8205 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8206 old_dentry
->d_inode
,
8207 old_dentry
->d_name
.name
,
8208 old_dentry
->d_name
.len
);
8210 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8213 btrfs_abort_transaction(trans
, root
, ret
);
8218 inode_inc_iversion(new_inode
);
8219 new_inode
->i_ctime
= CURRENT_TIME
;
8220 if (unlikely(btrfs_ino(new_inode
) ==
8221 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8222 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8223 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8225 new_dentry
->d_name
.name
,
8226 new_dentry
->d_name
.len
);
8227 BUG_ON(new_inode
->i_nlink
== 0);
8229 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8230 new_dentry
->d_inode
,
8231 new_dentry
->d_name
.name
,
8232 new_dentry
->d_name
.len
);
8234 if (!ret
&& new_inode
->i_nlink
== 0) {
8235 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8239 btrfs_abort_transaction(trans
, root
, ret
);
8244 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8245 new_dentry
->d_name
.name
,
8246 new_dentry
->d_name
.len
, 0, index
);
8248 btrfs_abort_transaction(trans
, root
, ret
);
8252 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8253 struct dentry
*parent
= new_dentry
->d_parent
;
8254 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8255 btrfs_end_log_trans(root
);
8258 btrfs_end_transaction(trans
, root
);
8260 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8261 up_read(&root
->fs_info
->subvol_sem
);
8266 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8268 struct btrfs_delalloc_work
*delalloc_work
;
8270 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8272 if (delalloc_work
->wait
)
8273 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8275 filemap_flush(delalloc_work
->inode
->i_mapping
);
8277 if (delalloc_work
->delay_iput
)
8278 btrfs_add_delayed_iput(delalloc_work
->inode
);
8280 iput(delalloc_work
->inode
);
8281 complete(&delalloc_work
->completion
);
8284 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8285 int wait
, int delay_iput
)
8287 struct btrfs_delalloc_work
*work
;
8289 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8293 init_completion(&work
->completion
);
8294 INIT_LIST_HEAD(&work
->list
);
8295 work
->inode
= inode
;
8297 work
->delay_iput
= delay_iput
;
8298 work
->work
.func
= btrfs_run_delalloc_work
;
8303 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8305 wait_for_completion(&work
->completion
);
8306 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8310 * some fairly slow code that needs optimization. This walks the list
8311 * of all the inodes with pending delalloc and forces them to disk.
8313 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8315 struct btrfs_inode
*binode
;
8316 struct inode
*inode
;
8317 struct btrfs_delalloc_work
*work
, *next
;
8318 struct list_head works
;
8319 struct list_head splice
;
8322 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8325 INIT_LIST_HEAD(&works
);
8326 INIT_LIST_HEAD(&splice
);
8328 spin_lock(&root
->fs_info
->delalloc_lock
);
8329 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
8330 while (!list_empty(&splice
)) {
8331 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8334 list_del_init(&binode
->delalloc_inodes
);
8336 inode
= igrab(&binode
->vfs_inode
);
8338 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
8339 &binode
->runtime_flags
);
8343 list_add_tail(&binode
->delalloc_inodes
,
8344 &root
->fs_info
->delalloc_inodes
);
8345 spin_unlock(&root
->fs_info
->delalloc_lock
);
8347 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8348 if (unlikely(!work
)) {
8352 list_add_tail(&work
->list
, &works
);
8353 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8357 spin_lock(&root
->fs_info
->delalloc_lock
);
8359 spin_unlock(&root
->fs_info
->delalloc_lock
);
8361 list_for_each_entry_safe(work
, next
, &works
, list
) {
8362 list_del_init(&work
->list
);
8363 btrfs_wait_and_free_delalloc_work(work
);
8366 /* the filemap_flush will queue IO into the worker threads, but
8367 * we have to make sure the IO is actually started and that
8368 * ordered extents get created before we return
8370 atomic_inc(&root
->fs_info
->async_submit_draining
);
8371 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8372 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8373 wait_event(root
->fs_info
->async_submit_wait
,
8374 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8375 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8377 atomic_dec(&root
->fs_info
->async_submit_draining
);
8380 list_for_each_entry_safe(work
, next
, &works
, list
) {
8381 list_del_init(&work
->list
);
8382 btrfs_wait_and_free_delalloc_work(work
);
8385 if (!list_empty_careful(&splice
)) {
8386 spin_lock(&root
->fs_info
->delalloc_lock
);
8387 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
8388 spin_unlock(&root
->fs_info
->delalloc_lock
);
8393 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8394 const char *symname
)
8396 struct btrfs_trans_handle
*trans
;
8397 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8398 struct btrfs_path
*path
;
8399 struct btrfs_key key
;
8400 struct inode
*inode
= NULL
;
8408 struct btrfs_file_extent_item
*ei
;
8409 struct extent_buffer
*leaf
;
8411 name_len
= strlen(symname
) + 1;
8412 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8413 return -ENAMETOOLONG
;
8416 * 2 items for inode item and ref
8417 * 2 items for dir items
8418 * 1 item for xattr if selinux is on
8420 trans
= btrfs_start_transaction(root
, 5);
8422 return PTR_ERR(trans
);
8424 err
= btrfs_find_free_ino(root
, &objectid
);
8428 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8429 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8430 S_IFLNK
|S_IRWXUGO
, &index
);
8431 if (IS_ERR(inode
)) {
8432 err
= PTR_ERR(inode
);
8436 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8443 * If the active LSM wants to access the inode during
8444 * d_instantiate it needs these. Smack checks to see
8445 * if the filesystem supports xattrs by looking at the
8448 inode
->i_fop
= &btrfs_file_operations
;
8449 inode
->i_op
= &btrfs_file_inode_operations
;
8451 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8455 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8456 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8457 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8462 path
= btrfs_alloc_path();
8468 key
.objectid
= btrfs_ino(inode
);
8470 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8471 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8472 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8476 btrfs_free_path(path
);
8479 leaf
= path
->nodes
[0];
8480 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8481 struct btrfs_file_extent_item
);
8482 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8483 btrfs_set_file_extent_type(leaf
, ei
,
8484 BTRFS_FILE_EXTENT_INLINE
);
8485 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8486 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8487 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8488 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8490 ptr
= btrfs_file_extent_inline_start(ei
);
8491 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8492 btrfs_mark_buffer_dirty(leaf
);
8493 btrfs_free_path(path
);
8495 inode
->i_op
= &btrfs_symlink_inode_operations
;
8496 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8497 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8498 inode_set_bytes(inode
, name_len
);
8499 btrfs_i_size_write(inode
, name_len
- 1);
8500 err
= btrfs_update_inode(trans
, root
, inode
);
8506 d_instantiate(dentry
, inode
);
8507 btrfs_end_transaction(trans
, root
);
8509 inode_dec_link_count(inode
);
8512 btrfs_btree_balance_dirty(root
);
8516 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8517 u64 start
, u64 num_bytes
, u64 min_size
,
8518 loff_t actual_len
, u64
*alloc_hint
,
8519 struct btrfs_trans_handle
*trans
)
8521 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8522 struct extent_map
*em
;
8523 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8524 struct btrfs_key ins
;
8525 u64 cur_offset
= start
;
8529 bool own_trans
= true;
8533 while (num_bytes
> 0) {
8535 trans
= btrfs_start_transaction(root
, 3);
8536 if (IS_ERR(trans
)) {
8537 ret
= PTR_ERR(trans
);
8542 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8543 cur_bytes
= max(cur_bytes
, min_size
);
8544 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8545 min_size
, 0, *alloc_hint
, &ins
, 1);
8548 btrfs_end_transaction(trans
, root
);
8552 ret
= insert_reserved_file_extent(trans
, inode
,
8553 cur_offset
, ins
.objectid
,
8554 ins
.offset
, ins
.offset
,
8555 ins
.offset
, 0, 0, 0,
8556 BTRFS_FILE_EXTENT_PREALLOC
);
8558 btrfs_abort_transaction(trans
, root
, ret
);
8560 btrfs_end_transaction(trans
, root
);
8563 btrfs_drop_extent_cache(inode
, cur_offset
,
8564 cur_offset
+ ins
.offset
-1, 0);
8566 em
= alloc_extent_map();
8568 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8569 &BTRFS_I(inode
)->runtime_flags
);
8573 em
->start
= cur_offset
;
8574 em
->orig_start
= cur_offset
;
8575 em
->len
= ins
.offset
;
8576 em
->block_start
= ins
.objectid
;
8577 em
->block_len
= ins
.offset
;
8578 em
->orig_block_len
= ins
.offset
;
8579 em
->ram_bytes
= ins
.offset
;
8580 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8581 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8582 em
->generation
= trans
->transid
;
8585 write_lock(&em_tree
->lock
);
8586 ret
= add_extent_mapping(em_tree
, em
, 1);
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
,