2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
60 struct btrfs_iget_args
{
62 struct btrfs_root
*root
;
65 static const struct inode_operations btrfs_dir_inode_operations
;
66 static const struct inode_operations btrfs_symlink_inode_operations
;
67 static const struct inode_operations btrfs_dir_ro_inode_operations
;
68 static const struct inode_operations btrfs_special_inode_operations
;
69 static const struct inode_operations btrfs_file_inode_operations
;
70 static const struct address_space_operations btrfs_aops
;
71 static const struct address_space_operations btrfs_symlink_aops
;
72 static const struct file_operations btrfs_dir_file_operations
;
73 static struct extent_io_ops btrfs_extent_io_ops
;
75 static struct kmem_cache
*btrfs_inode_cachep
;
76 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
77 struct kmem_cache
*btrfs_trans_handle_cachep
;
78 struct kmem_cache
*btrfs_transaction_cachep
;
79 struct kmem_cache
*btrfs_path_cachep
;
80 struct kmem_cache
*btrfs_free_space_cachep
;
83 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
84 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
85 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
86 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
87 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
88 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
89 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
90 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
93 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
94 static int btrfs_truncate(struct inode
*inode
);
95 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
96 static noinline
int cow_file_range(struct inode
*inode
,
97 struct page
*locked_page
,
98 u64 start
, u64 end
, int *page_started
,
99 unsigned long *nr_written
, int unlock
);
100 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
101 u64 len
, u64 orig_start
,
102 u64 block_start
, u64 block_len
,
103 u64 orig_block_len
, int type
);
105 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
106 struct inode
*inode
, struct inode
*dir
,
107 const struct qstr
*qstr
)
111 err
= btrfs_init_acl(trans
, inode
, dir
);
113 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
118 * this does all the hard work for inserting an inline extent into
119 * the btree. The caller should have done a btrfs_drop_extents so that
120 * no overlapping inline items exist in the btree
122 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
123 struct btrfs_root
*root
, struct inode
*inode
,
124 u64 start
, size_t size
, size_t compressed_size
,
126 struct page
**compressed_pages
)
128 struct btrfs_key key
;
129 struct btrfs_path
*path
;
130 struct extent_buffer
*leaf
;
131 struct page
*page
= NULL
;
134 struct btrfs_file_extent_item
*ei
;
137 size_t cur_size
= size
;
139 unsigned long offset
;
141 if (compressed_size
&& compressed_pages
)
142 cur_size
= compressed_size
;
144 path
= btrfs_alloc_path();
148 path
->leave_spinning
= 1;
150 key
.objectid
= btrfs_ino(inode
);
152 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
153 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
155 inode_add_bytes(inode
, size
);
156 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
162 leaf
= path
->nodes
[0];
163 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
164 struct btrfs_file_extent_item
);
165 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
166 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
167 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
168 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
169 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
170 ptr
= btrfs_file_extent_inline_start(ei
);
172 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
175 while (compressed_size
> 0) {
176 cpage
= compressed_pages
[i
];
177 cur_size
= min_t(unsigned long, compressed_size
,
180 kaddr
= kmap_atomic(cpage
);
181 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
182 kunmap_atomic(kaddr
);
186 compressed_size
-= cur_size
;
188 btrfs_set_file_extent_compression(leaf
, ei
,
191 page
= find_get_page(inode
->i_mapping
,
192 start
>> PAGE_CACHE_SHIFT
);
193 btrfs_set_file_extent_compression(leaf
, ei
, 0);
194 kaddr
= kmap_atomic(page
);
195 offset
= start
& (PAGE_CACHE_SIZE
- 1);
196 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
197 kunmap_atomic(kaddr
);
198 page_cache_release(page
);
200 btrfs_mark_buffer_dirty(leaf
);
201 btrfs_free_path(path
);
204 * we're an inline extent, so nobody can
205 * extend the file past i_size without locking
206 * a page we already have locked.
208 * We must do any isize and inode updates
209 * before we unlock the pages. Otherwise we
210 * could end up racing with unlink.
212 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
213 ret
= btrfs_update_inode(trans
, root
, inode
);
217 btrfs_free_path(path
);
223 * conditionally insert an inline extent into the file. This
224 * does the checks required to make sure the data is small enough
225 * to fit as an inline extent.
227 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
228 struct btrfs_root
*root
,
229 struct inode
*inode
, u64 start
, u64 end
,
230 size_t compressed_size
, int compress_type
,
231 struct page
**compressed_pages
)
233 u64 isize
= i_size_read(inode
);
234 u64 actual_end
= min(end
+ 1, isize
);
235 u64 inline_len
= actual_end
- start
;
236 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
237 u64 data_len
= inline_len
;
241 data_len
= compressed_size
;
244 actual_end
>= PAGE_CACHE_SIZE
||
245 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
247 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
249 data_len
> root
->fs_info
->max_inline
) {
253 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
257 if (isize
> actual_end
)
258 inline_len
= min_t(u64
, isize
, actual_end
);
259 ret
= insert_inline_extent(trans
, root
, inode
, start
,
260 inline_len
, compressed_size
,
261 compress_type
, compressed_pages
);
262 if (ret
&& ret
!= -ENOSPC
) {
263 btrfs_abort_transaction(trans
, root
, ret
);
265 } else if (ret
== -ENOSPC
) {
269 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
270 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
271 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
275 struct async_extent
{
280 unsigned long nr_pages
;
282 struct list_head list
;
287 struct btrfs_root
*root
;
288 struct page
*locked_page
;
291 struct list_head extents
;
292 struct btrfs_work work
;
295 static noinline
int add_async_extent(struct async_cow
*cow
,
296 u64 start
, u64 ram_size
,
299 unsigned long nr_pages
,
302 struct async_extent
*async_extent
;
304 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
305 BUG_ON(!async_extent
); /* -ENOMEM */
306 async_extent
->start
= start
;
307 async_extent
->ram_size
= ram_size
;
308 async_extent
->compressed_size
= compressed_size
;
309 async_extent
->pages
= pages
;
310 async_extent
->nr_pages
= nr_pages
;
311 async_extent
->compress_type
= compress_type
;
312 list_add_tail(&async_extent
->list
, &cow
->extents
);
317 * we create compressed extents in two phases. The first
318 * phase compresses a range of pages that have already been
319 * locked (both pages and state bits are locked).
321 * This is done inside an ordered work queue, and the compression
322 * is spread across many cpus. The actual IO submission is step
323 * two, and the ordered work queue takes care of making sure that
324 * happens in the same order things were put onto the queue by
325 * writepages and friends.
327 * If this code finds it can't get good compression, it puts an
328 * entry onto the work queue to write the uncompressed bytes. This
329 * makes sure that both compressed inodes and uncompressed inodes
330 * are written in the same order that the flusher thread sent them
333 static noinline
int compress_file_range(struct inode
*inode
,
334 struct page
*locked_page
,
336 struct async_cow
*async_cow
,
339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
340 struct btrfs_trans_handle
*trans
;
342 u64 blocksize
= root
->sectorsize
;
344 u64 isize
= i_size_read(inode
);
346 struct page
**pages
= NULL
;
347 unsigned long nr_pages
;
348 unsigned long nr_pages_ret
= 0;
349 unsigned long total_compressed
= 0;
350 unsigned long total_in
= 0;
351 unsigned long max_compressed
= 128 * 1024;
352 unsigned long max_uncompressed
= 128 * 1024;
355 int compress_type
= root
->fs_info
->compress_type
;
357 /* if this is a small write inside eof, kick off a defrag */
358 if ((end
- start
+ 1) < 16 * 1024 &&
359 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
360 btrfs_add_inode_defrag(NULL
, inode
);
362 actual_end
= min_t(u64
, isize
, end
+ 1);
365 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
366 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
369 * we don't want to send crud past the end of i_size through
370 * compression, that's just a waste of CPU time. So, if the
371 * end of the file is before the start of our current
372 * requested range of bytes, we bail out to the uncompressed
373 * cleanup code that can deal with all of this.
375 * It isn't really the fastest way to fix things, but this is a
376 * very uncommon corner.
378 if (actual_end
<= start
)
379 goto cleanup_and_bail_uncompressed
;
381 total_compressed
= actual_end
- start
;
383 /* we want to make sure that amount of ram required to uncompress
384 * an extent is reasonable, so we limit the total size in ram
385 * of a compressed extent to 128k. This is a crucial number
386 * because it also controls how easily we can spread reads across
387 * cpus for decompression.
389 * We also want to make sure the amount of IO required to do
390 * a random read is reasonably small, so we limit the size of
391 * a compressed extent to 128k.
393 total_compressed
= min(total_compressed
, max_uncompressed
);
394 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
395 num_bytes
= max(blocksize
, num_bytes
);
400 * we do compression for mount -o compress and when the
401 * inode has not been flagged as nocompress. This flag can
402 * change at any time if we discover bad compression ratios.
404 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
405 (btrfs_test_opt(root
, COMPRESS
) ||
406 (BTRFS_I(inode
)->force_compress
) ||
407 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
409 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
411 /* just bail out to the uncompressed code */
415 if (BTRFS_I(inode
)->force_compress
)
416 compress_type
= BTRFS_I(inode
)->force_compress
;
418 ret
= btrfs_compress_pages(compress_type
,
419 inode
->i_mapping
, start
,
420 total_compressed
, pages
,
421 nr_pages
, &nr_pages_ret
,
427 unsigned long offset
= total_compressed
&
428 (PAGE_CACHE_SIZE
- 1);
429 struct page
*page
= pages
[nr_pages_ret
- 1];
432 /* zero the tail end of the last page, we might be
433 * sending it down to disk
436 kaddr
= kmap_atomic(page
);
437 memset(kaddr
+ offset
, 0,
438 PAGE_CACHE_SIZE
- offset
);
439 kunmap_atomic(kaddr
);
446 trans
= btrfs_join_transaction(root
);
448 ret
= PTR_ERR(trans
);
450 goto cleanup_and_out
;
452 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
454 /* lets try to make an inline extent */
455 if (ret
|| total_in
< (actual_end
- start
)) {
456 /* we didn't compress the entire range, try
457 * to make an uncompressed inline extent.
459 ret
= cow_file_range_inline(trans
, root
, inode
,
460 start
, end
, 0, 0, NULL
);
462 /* try making a compressed inline extent */
463 ret
= cow_file_range_inline(trans
, root
, inode
,
466 compress_type
, pages
);
470 * inline extent creation worked or returned error,
471 * we don't need to create any more async work items.
472 * Unlock and free up our temp pages.
474 extent_clear_unlock_delalloc(inode
,
475 &BTRFS_I(inode
)->io_tree
,
477 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
478 EXTENT_CLEAR_DELALLOC
|
479 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
481 btrfs_end_transaction(trans
, root
);
484 btrfs_end_transaction(trans
, root
);
489 * we aren't doing an inline extent round the compressed size
490 * up to a block size boundary so the allocator does sane
493 total_compressed
= ALIGN(total_compressed
, blocksize
);
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
499 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
500 if (total_compressed
>= total_in
) {
503 num_bytes
= total_in
;
506 if (!will_compress
&& pages
) {
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
511 for (i
= 0; i
< nr_pages_ret
; i
++) {
512 WARN_ON(pages
[i
]->mapping
);
513 page_cache_release(pages
[i
]);
517 total_compressed
= 0;
520 /* flag the file so we don't compress in the future */
521 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
522 !(BTRFS_I(inode
)->force_compress
)) {
523 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
533 add_async_extent(async_cow
, start
, num_bytes
,
534 total_compressed
, pages
, nr_pages_ret
,
537 if (start
+ num_bytes
< end
) {
544 cleanup_and_bail_uncompressed
:
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
552 if (page_offset(locked_page
) >= start
&&
553 page_offset(locked_page
) <= end
) {
554 __set_page_dirty_nobuffers(locked_page
);
555 /* unlocked later on in the async handlers */
557 add_async_extent(async_cow
, start
, end
- start
+ 1,
558 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
566 for (i
= 0; i
< nr_pages_ret
; i
++) {
567 WARN_ON(pages
[i
]->mapping
);
568 page_cache_release(pages
[i
]);
575 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
577 EXTENT_CLEAR_UNLOCK_PAGE
|
579 EXTENT_CLEAR_DELALLOC
|
580 EXTENT_SET_WRITEBACK
|
581 EXTENT_END_WRITEBACK
);
582 if (!trans
|| IS_ERR(trans
))
583 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
585 btrfs_abort_transaction(trans
, root
, ret
);
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
595 static noinline
int submit_compressed_extents(struct inode
*inode
,
596 struct async_cow
*async_cow
)
598 struct async_extent
*async_extent
;
600 struct btrfs_trans_handle
*trans
;
601 struct btrfs_key ins
;
602 struct extent_map
*em
;
603 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
604 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
605 struct extent_io_tree
*io_tree
;
608 if (list_empty(&async_cow
->extents
))
612 while (!list_empty(&async_cow
->extents
)) {
613 async_extent
= list_entry(async_cow
->extents
.next
,
614 struct async_extent
, list
);
615 list_del(&async_extent
->list
);
617 io_tree
= &BTRFS_I(inode
)->io_tree
;
620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent
->pages
) {
622 int page_started
= 0;
623 unsigned long nr_written
= 0;
625 lock_extent(io_tree
, async_extent
->start
,
626 async_extent
->start
+
627 async_extent
->ram_size
- 1);
629 /* allocate blocks */
630 ret
= cow_file_range(inode
, async_cow
->locked_page
,
632 async_extent
->start
+
633 async_extent
->ram_size
- 1,
634 &page_started
, &nr_written
, 0);
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
644 if (!page_started
&& !ret
)
645 extent_write_locked_range(io_tree
,
646 inode
, async_extent
->start
,
647 async_extent
->start
+
648 async_extent
->ram_size
- 1,
652 unlock_page(async_cow
->locked_page
);
658 lock_extent(io_tree
, async_extent
->start
,
659 async_extent
->start
+ async_extent
->ram_size
- 1);
661 trans
= btrfs_join_transaction(root
);
663 ret
= PTR_ERR(trans
);
665 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
666 ret
= btrfs_reserve_extent(trans
, root
,
667 async_extent
->compressed_size
,
668 async_extent
->compressed_size
,
669 0, alloc_hint
, &ins
, 1);
670 if (ret
&& ret
!= -ENOSPC
)
671 btrfs_abort_transaction(trans
, root
, ret
);
672 btrfs_end_transaction(trans
, root
);
678 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
679 WARN_ON(async_extent
->pages
[i
]->mapping
);
680 page_cache_release(async_extent
->pages
[i
]);
682 kfree(async_extent
->pages
);
683 async_extent
->nr_pages
= 0;
684 async_extent
->pages
= NULL
;
692 * here we're doing allocation and writeback of the
695 btrfs_drop_extent_cache(inode
, async_extent
->start
,
696 async_extent
->start
+
697 async_extent
->ram_size
- 1, 0);
699 em
= alloc_extent_map();
701 goto out_free_reserve
;
702 em
->start
= async_extent
->start
;
703 em
->len
= async_extent
->ram_size
;
704 em
->orig_start
= em
->start
;
705 em
->mod_start
= em
->start
;
706 em
->mod_len
= em
->len
;
708 em
->block_start
= ins
.objectid
;
709 em
->block_len
= ins
.offset
;
710 em
->orig_block_len
= ins
.offset
;
711 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
712 em
->compress_type
= async_extent
->compress_type
;
713 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
714 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
718 write_lock(&em_tree
->lock
);
719 ret
= add_extent_mapping(em_tree
, em
);
722 &em_tree
->modified_extents
);
723 write_unlock(&em_tree
->lock
);
724 if (ret
!= -EEXIST
) {
728 btrfs_drop_extent_cache(inode
, async_extent
->start
,
729 async_extent
->start
+
730 async_extent
->ram_size
- 1, 0);
734 goto out_free_reserve
;
736 ret
= btrfs_add_ordered_extent_compress(inode
,
739 async_extent
->ram_size
,
741 BTRFS_ORDERED_COMPRESSED
,
742 async_extent
->compress_type
);
744 goto out_free_reserve
;
747 * clear dirty, set writeback and unlock the pages.
749 extent_clear_unlock_delalloc(inode
,
750 &BTRFS_I(inode
)->io_tree
,
752 async_extent
->start
+
753 async_extent
->ram_size
- 1,
754 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
755 EXTENT_CLEAR_UNLOCK
|
756 EXTENT_CLEAR_DELALLOC
|
757 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
759 ret
= btrfs_submit_compressed_write(inode
,
761 async_extent
->ram_size
,
763 ins
.offset
, async_extent
->pages
,
764 async_extent
->nr_pages
);
765 alloc_hint
= ins
.objectid
+ ins
.offset
;
775 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
777 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
779 async_extent
->start
+
780 async_extent
->ram_size
- 1,
781 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
782 EXTENT_CLEAR_UNLOCK
|
783 EXTENT_CLEAR_DELALLOC
|
785 EXTENT_SET_WRITEBACK
|
786 EXTENT_END_WRITEBACK
);
791 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
794 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
795 struct extent_map
*em
;
798 read_lock(&em_tree
->lock
);
799 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
802 * if block start isn't an actual block number then find the
803 * first block in this inode and use that as a hint. If that
804 * block is also bogus then just don't worry about it.
806 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
808 em
= search_extent_mapping(em_tree
, 0, 0);
809 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
810 alloc_hint
= em
->block_start
;
814 alloc_hint
= em
->block_start
;
818 read_unlock(&em_tree
->lock
);
824 * when extent_io.c finds a delayed allocation range in the file,
825 * the call backs end up in this code. The basic idea is to
826 * allocate extents on disk for the range, and create ordered data structs
827 * in ram to track those extents.
829 * locked_page is the page that writepage had locked already. We use
830 * it to make sure we don't do extra locks or unlocks.
832 * *page_started is set to one if we unlock locked_page and do everything
833 * required to start IO on it. It may be clean and already done with
836 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
838 struct btrfs_root
*root
,
839 struct page
*locked_page
,
840 u64 start
, u64 end
, int *page_started
,
841 unsigned long *nr_written
,
846 unsigned long ram_size
;
849 u64 blocksize
= root
->sectorsize
;
850 struct btrfs_key ins
;
851 struct extent_map
*em
;
852 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
855 BUG_ON(btrfs_is_free_space_inode(inode
));
857 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
858 num_bytes
= max(blocksize
, num_bytes
);
859 disk_num_bytes
= num_bytes
;
861 /* if this is a small write inside eof, kick off defrag */
862 if (num_bytes
< 64 * 1024 &&
863 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
864 btrfs_add_inode_defrag(trans
, inode
);
867 /* lets try to make an inline extent */
868 ret
= cow_file_range_inline(trans
, root
, inode
,
869 start
, end
, 0, 0, NULL
);
871 extent_clear_unlock_delalloc(inode
,
872 &BTRFS_I(inode
)->io_tree
,
874 EXTENT_CLEAR_UNLOCK_PAGE
|
875 EXTENT_CLEAR_UNLOCK
|
876 EXTENT_CLEAR_DELALLOC
|
878 EXTENT_SET_WRITEBACK
|
879 EXTENT_END_WRITEBACK
);
881 *nr_written
= *nr_written
+
882 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
885 } else if (ret
< 0) {
886 btrfs_abort_transaction(trans
, root
, ret
);
891 BUG_ON(disk_num_bytes
>
892 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
894 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
895 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
897 while (disk_num_bytes
> 0) {
900 cur_alloc_size
= disk_num_bytes
;
901 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
902 root
->sectorsize
, 0, alloc_hint
,
905 btrfs_abort_transaction(trans
, root
, ret
);
909 em
= alloc_extent_map();
910 BUG_ON(!em
); /* -ENOMEM */
912 em
->orig_start
= em
->start
;
913 ram_size
= ins
.offset
;
914 em
->len
= ins
.offset
;
915 em
->mod_start
= em
->start
;
916 em
->mod_len
= em
->len
;
918 em
->block_start
= ins
.objectid
;
919 em
->block_len
= ins
.offset
;
920 em
->orig_block_len
= ins
.offset
;
921 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
922 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
926 write_lock(&em_tree
->lock
);
927 ret
= add_extent_mapping(em_tree
, em
);
930 &em_tree
->modified_extents
);
931 write_unlock(&em_tree
->lock
);
932 if (ret
!= -EEXIST
) {
936 btrfs_drop_extent_cache(inode
, start
,
937 start
+ ram_size
- 1, 0);
940 cur_alloc_size
= ins
.offset
;
941 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
942 ram_size
, cur_alloc_size
, 0);
943 BUG_ON(ret
); /* -ENOMEM */
945 if (root
->root_key
.objectid
==
946 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
947 ret
= btrfs_reloc_clone_csums(inode
, start
,
950 btrfs_abort_transaction(trans
, root
, ret
);
955 if (disk_num_bytes
< cur_alloc_size
)
958 /* we're not doing compressed IO, don't unlock the first
959 * page (which the caller expects to stay locked), don't
960 * clear any dirty bits and don't set any writeback bits
962 * Do set the Private2 bit so we know this page was properly
963 * setup for writepage
965 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
966 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
969 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
970 start
, start
+ ram_size
- 1,
972 disk_num_bytes
-= cur_alloc_size
;
973 num_bytes
-= cur_alloc_size
;
974 alloc_hint
= ins
.objectid
+ ins
.offset
;
975 start
+= cur_alloc_size
;
981 extent_clear_unlock_delalloc(inode
,
982 &BTRFS_I(inode
)->io_tree
,
983 start
, end
, locked_page
,
984 EXTENT_CLEAR_UNLOCK_PAGE
|
985 EXTENT_CLEAR_UNLOCK
|
986 EXTENT_CLEAR_DELALLOC
|
988 EXTENT_SET_WRITEBACK
|
989 EXTENT_END_WRITEBACK
);
994 static noinline
int cow_file_range(struct inode
*inode
,
995 struct page
*locked_page
,
996 u64 start
, u64 end
, int *page_started
,
997 unsigned long *nr_written
,
1000 struct btrfs_trans_handle
*trans
;
1001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1004 trans
= btrfs_join_transaction(root
);
1005 if (IS_ERR(trans
)) {
1006 extent_clear_unlock_delalloc(inode
,
1007 &BTRFS_I(inode
)->io_tree
,
1008 start
, end
, locked_page
,
1009 EXTENT_CLEAR_UNLOCK_PAGE
|
1010 EXTENT_CLEAR_UNLOCK
|
1011 EXTENT_CLEAR_DELALLOC
|
1012 EXTENT_CLEAR_DIRTY
|
1013 EXTENT_SET_WRITEBACK
|
1014 EXTENT_END_WRITEBACK
);
1015 return PTR_ERR(trans
);
1017 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1019 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1020 page_started
, nr_written
, unlock
);
1022 btrfs_end_transaction(trans
, root
);
1028 * work queue call back to started compression on a file and pages
1030 static noinline
void async_cow_start(struct btrfs_work
*work
)
1032 struct async_cow
*async_cow
;
1034 async_cow
= container_of(work
, struct async_cow
, work
);
1036 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1037 async_cow
->start
, async_cow
->end
, async_cow
,
1039 if (num_added
== 0) {
1040 btrfs_add_delayed_iput(async_cow
->inode
);
1041 async_cow
->inode
= NULL
;
1046 * work queue call back to submit previously compressed pages
1048 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1050 struct async_cow
*async_cow
;
1051 struct btrfs_root
*root
;
1052 unsigned long nr_pages
;
1054 async_cow
= container_of(work
, struct async_cow
, work
);
1056 root
= async_cow
->root
;
1057 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1060 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1062 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1063 wake_up(&root
->fs_info
->async_submit_wait
);
1065 if (async_cow
->inode
)
1066 submit_compressed_extents(async_cow
->inode
, async_cow
);
1069 static noinline
void async_cow_free(struct btrfs_work
*work
)
1071 struct async_cow
*async_cow
;
1072 async_cow
= container_of(work
, struct async_cow
, work
);
1073 if (async_cow
->inode
)
1074 btrfs_add_delayed_iput(async_cow
->inode
);
1078 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1079 u64 start
, u64 end
, int *page_started
,
1080 unsigned long *nr_written
)
1082 struct async_cow
*async_cow
;
1083 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1084 unsigned long nr_pages
;
1086 int limit
= 10 * 1024 * 1024;
1088 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1089 1, 0, NULL
, GFP_NOFS
);
1090 while (start
< end
) {
1091 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1092 BUG_ON(!async_cow
); /* -ENOMEM */
1093 async_cow
->inode
= igrab(inode
);
1094 async_cow
->root
= root
;
1095 async_cow
->locked_page
= locked_page
;
1096 async_cow
->start
= start
;
1098 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1101 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1103 async_cow
->end
= cur_end
;
1104 INIT_LIST_HEAD(&async_cow
->extents
);
1106 async_cow
->work
.func
= async_cow_start
;
1107 async_cow
->work
.ordered_func
= async_cow_submit
;
1108 async_cow
->work
.ordered_free
= async_cow_free
;
1109 async_cow
->work
.flags
= 0;
1111 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1113 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1115 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1118 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1119 wait_event(root
->fs_info
->async_submit_wait
,
1120 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1124 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1125 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1126 wait_event(root
->fs_info
->async_submit_wait
,
1127 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1131 *nr_written
+= nr_pages
;
1132 start
= cur_end
+ 1;
1138 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1139 u64 bytenr
, u64 num_bytes
)
1142 struct btrfs_ordered_sum
*sums
;
1145 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1146 bytenr
+ num_bytes
- 1, &list
, 0);
1147 if (ret
== 0 && list_empty(&list
))
1150 while (!list_empty(&list
)) {
1151 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1152 list_del(&sums
->list
);
1159 * when nowcow writeback call back. This checks for snapshots or COW copies
1160 * of the extents that exist in the file, and COWs the file as required.
1162 * If no cow copies or snapshots exist, we write directly to the existing
1165 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1166 struct page
*locked_page
,
1167 u64 start
, u64 end
, int *page_started
, int force
,
1168 unsigned long *nr_written
)
1170 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1171 struct btrfs_trans_handle
*trans
;
1172 struct extent_buffer
*leaf
;
1173 struct btrfs_path
*path
;
1174 struct btrfs_file_extent_item
*fi
;
1175 struct btrfs_key found_key
;
1189 u64 ino
= btrfs_ino(inode
);
1191 path
= btrfs_alloc_path();
1193 extent_clear_unlock_delalloc(inode
,
1194 &BTRFS_I(inode
)->io_tree
,
1195 start
, end
, locked_page
,
1196 EXTENT_CLEAR_UNLOCK_PAGE
|
1197 EXTENT_CLEAR_UNLOCK
|
1198 EXTENT_CLEAR_DELALLOC
|
1199 EXTENT_CLEAR_DIRTY
|
1200 EXTENT_SET_WRITEBACK
|
1201 EXTENT_END_WRITEBACK
);
1205 nolock
= btrfs_is_free_space_inode(inode
);
1208 trans
= btrfs_join_transaction_nolock(root
);
1210 trans
= btrfs_join_transaction(root
);
1212 if (IS_ERR(trans
)) {
1213 extent_clear_unlock_delalloc(inode
,
1214 &BTRFS_I(inode
)->io_tree
,
1215 start
, end
, locked_page
,
1216 EXTENT_CLEAR_UNLOCK_PAGE
|
1217 EXTENT_CLEAR_UNLOCK
|
1218 EXTENT_CLEAR_DELALLOC
|
1219 EXTENT_CLEAR_DIRTY
|
1220 EXTENT_SET_WRITEBACK
|
1221 EXTENT_END_WRITEBACK
);
1222 btrfs_free_path(path
);
1223 return PTR_ERR(trans
);
1226 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1228 cow_start
= (u64
)-1;
1231 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1234 btrfs_abort_transaction(trans
, root
, ret
);
1237 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1238 leaf
= path
->nodes
[0];
1239 btrfs_item_key_to_cpu(leaf
, &found_key
,
1240 path
->slots
[0] - 1);
1241 if (found_key
.objectid
== ino
&&
1242 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1247 leaf
= path
->nodes
[0];
1248 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1249 ret
= btrfs_next_leaf(root
, path
);
1251 btrfs_abort_transaction(trans
, root
, ret
);
1256 leaf
= path
->nodes
[0];
1262 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1264 if (found_key
.objectid
> ino
||
1265 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1266 found_key
.offset
> end
)
1269 if (found_key
.offset
> cur_offset
) {
1270 extent_end
= found_key
.offset
;
1275 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1276 struct btrfs_file_extent_item
);
1277 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1279 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1280 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1281 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1282 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1283 extent_end
= found_key
.offset
+
1284 btrfs_file_extent_num_bytes(leaf
, fi
);
1286 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1287 if (extent_end
<= start
) {
1291 if (disk_bytenr
== 0)
1293 if (btrfs_file_extent_compression(leaf
, fi
) ||
1294 btrfs_file_extent_encryption(leaf
, fi
) ||
1295 btrfs_file_extent_other_encoding(leaf
, fi
))
1297 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1299 if (btrfs_extent_readonly(root
, disk_bytenr
))
1301 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1303 extent_offset
, disk_bytenr
))
1305 disk_bytenr
+= extent_offset
;
1306 disk_bytenr
+= cur_offset
- found_key
.offset
;
1307 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1309 * force cow if csum exists in the range.
1310 * this ensure that csum for a given extent are
1311 * either valid or do not exist.
1313 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1316 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1317 extent_end
= found_key
.offset
+
1318 btrfs_file_extent_inline_len(leaf
, fi
);
1319 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1324 if (extent_end
<= start
) {
1329 if (cow_start
== (u64
)-1)
1330 cow_start
= cur_offset
;
1331 cur_offset
= extent_end
;
1332 if (cur_offset
> end
)
1338 btrfs_release_path(path
);
1339 if (cow_start
!= (u64
)-1) {
1340 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1341 cow_start
, found_key
.offset
- 1,
1342 page_started
, nr_written
, 1);
1344 btrfs_abort_transaction(trans
, root
, ret
);
1347 cow_start
= (u64
)-1;
1350 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1351 struct extent_map
*em
;
1352 struct extent_map_tree
*em_tree
;
1353 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1354 em
= alloc_extent_map();
1355 BUG_ON(!em
); /* -ENOMEM */
1356 em
->start
= cur_offset
;
1357 em
->orig_start
= found_key
.offset
- extent_offset
;
1358 em
->len
= num_bytes
;
1359 em
->block_len
= num_bytes
;
1360 em
->block_start
= disk_bytenr
;
1361 em
->orig_block_len
= disk_num_bytes
;
1362 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1363 em
->mod_start
= em
->start
;
1364 em
->mod_len
= em
->len
;
1365 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1366 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1367 em
->generation
= -1;
1369 write_lock(&em_tree
->lock
);
1370 ret
= add_extent_mapping(em_tree
, em
);
1372 list_move(&em
->list
,
1373 &em_tree
->modified_extents
);
1374 write_unlock(&em_tree
->lock
);
1375 if (ret
!= -EEXIST
) {
1376 free_extent_map(em
);
1379 btrfs_drop_extent_cache(inode
, em
->start
,
1380 em
->start
+ em
->len
- 1, 0);
1382 type
= BTRFS_ORDERED_PREALLOC
;
1384 type
= BTRFS_ORDERED_NOCOW
;
1387 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1388 num_bytes
, num_bytes
, type
);
1389 BUG_ON(ret
); /* -ENOMEM */
1391 if (root
->root_key
.objectid
==
1392 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1393 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1396 btrfs_abort_transaction(trans
, root
, ret
);
1401 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1402 cur_offset
, cur_offset
+ num_bytes
- 1,
1403 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1404 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1405 EXTENT_SET_PRIVATE2
);
1406 cur_offset
= extent_end
;
1407 if (cur_offset
> end
)
1410 btrfs_release_path(path
);
1412 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1413 cow_start
= cur_offset
;
1417 if (cow_start
!= (u64
)-1) {
1418 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1420 page_started
, nr_written
, 1);
1422 btrfs_abort_transaction(trans
, root
, ret
);
1428 err
= btrfs_end_transaction(trans
, root
);
1432 if (ret
&& cur_offset
< end
)
1433 extent_clear_unlock_delalloc(inode
,
1434 &BTRFS_I(inode
)->io_tree
,
1435 cur_offset
, end
, locked_page
,
1436 EXTENT_CLEAR_UNLOCK_PAGE
|
1437 EXTENT_CLEAR_UNLOCK
|
1438 EXTENT_CLEAR_DELALLOC
|
1439 EXTENT_CLEAR_DIRTY
|
1440 EXTENT_SET_WRITEBACK
|
1441 EXTENT_END_WRITEBACK
);
1443 btrfs_free_path(path
);
1448 * extent_io.c call back to do delayed allocation processing
1450 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1451 u64 start
, u64 end
, int *page_started
,
1452 unsigned long *nr_written
)
1455 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1457 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1458 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1459 page_started
, 1, nr_written
);
1460 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1461 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1462 page_started
, 0, nr_written
);
1463 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1464 !(BTRFS_I(inode
)->force_compress
) &&
1465 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1466 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1467 page_started
, nr_written
, 1);
1469 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1470 &BTRFS_I(inode
)->runtime_flags
);
1471 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1472 page_started
, nr_written
);
1477 static void btrfs_split_extent_hook(struct inode
*inode
,
1478 struct extent_state
*orig
, u64 split
)
1480 /* not delalloc, ignore it */
1481 if (!(orig
->state
& EXTENT_DELALLOC
))
1484 spin_lock(&BTRFS_I(inode
)->lock
);
1485 BTRFS_I(inode
)->outstanding_extents
++;
1486 spin_unlock(&BTRFS_I(inode
)->lock
);
1490 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1491 * extents so we can keep track of new extents that are just merged onto old
1492 * extents, such as when we are doing sequential writes, so we can properly
1493 * account for the metadata space we'll need.
1495 static void btrfs_merge_extent_hook(struct inode
*inode
,
1496 struct extent_state
*new,
1497 struct extent_state
*other
)
1499 /* not delalloc, ignore it */
1500 if (!(other
->state
& EXTENT_DELALLOC
))
1503 spin_lock(&BTRFS_I(inode
)->lock
);
1504 BTRFS_I(inode
)->outstanding_extents
--;
1505 spin_unlock(&BTRFS_I(inode
)->lock
);
1509 * extent_io.c set_bit_hook, used to track delayed allocation
1510 * bytes in this file, and to maintain the list of inodes that
1511 * have pending delalloc work to be done.
1513 static void btrfs_set_bit_hook(struct inode
*inode
,
1514 struct extent_state
*state
, int *bits
)
1518 * set_bit and clear bit hooks normally require _irqsave/restore
1519 * but in this case, we are only testing for the DELALLOC
1520 * bit, which is only set or cleared with irqs on
1522 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1523 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1524 u64 len
= state
->end
+ 1 - state
->start
;
1525 bool do_list
= !btrfs_is_free_space_inode(inode
);
1527 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1528 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1530 spin_lock(&BTRFS_I(inode
)->lock
);
1531 BTRFS_I(inode
)->outstanding_extents
++;
1532 spin_unlock(&BTRFS_I(inode
)->lock
);
1535 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1536 root
->fs_info
->delalloc_batch
);
1537 spin_lock(&BTRFS_I(inode
)->lock
);
1538 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1539 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1540 &BTRFS_I(inode
)->runtime_flags
)) {
1541 spin_lock(&root
->fs_info
->delalloc_lock
);
1542 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1543 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1544 &root
->fs_info
->delalloc_inodes
);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1546 &BTRFS_I(inode
)->runtime_flags
);
1548 spin_unlock(&root
->fs_info
->delalloc_lock
);
1550 spin_unlock(&BTRFS_I(inode
)->lock
);
1555 * extent_io.c clear_bit_hook, see set_bit_hook for why
1557 static void btrfs_clear_bit_hook(struct inode
*inode
,
1558 struct extent_state
*state
, int *bits
)
1561 * set_bit and clear bit hooks normally require _irqsave/restore
1562 * but in this case, we are only testing for the DELALLOC
1563 * bit, which is only set or cleared with irqs on
1565 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1566 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1567 u64 len
= state
->end
+ 1 - state
->start
;
1568 bool do_list
= !btrfs_is_free_space_inode(inode
);
1570 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1571 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1572 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1573 spin_lock(&BTRFS_I(inode
)->lock
);
1574 BTRFS_I(inode
)->outstanding_extents
--;
1575 spin_unlock(&BTRFS_I(inode
)->lock
);
1578 if (*bits
& EXTENT_DO_ACCOUNTING
)
1579 btrfs_delalloc_release_metadata(inode
, len
);
1581 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1583 btrfs_free_reserved_data_space(inode
, len
);
1585 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1586 root
->fs_info
->delalloc_batch
);
1587 spin_lock(&BTRFS_I(inode
)->lock
);
1588 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1589 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1590 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1591 &BTRFS_I(inode
)->runtime_flags
)) {
1592 spin_lock(&root
->fs_info
->delalloc_lock
);
1593 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1594 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1595 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1596 &BTRFS_I(inode
)->runtime_flags
);
1598 spin_unlock(&root
->fs_info
->delalloc_lock
);
1600 spin_unlock(&BTRFS_I(inode
)->lock
);
1605 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1606 * we don't create bios that span stripes or chunks
1608 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1609 size_t size
, struct bio
*bio
,
1610 unsigned long bio_flags
)
1612 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1613 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1618 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1621 length
= bio
->bi_size
;
1622 map_length
= length
;
1623 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1624 &map_length
, NULL
, 0);
1625 /* Will always return 0 with map_multi == NULL */
1627 if (map_length
< length
+ size
)
1633 * in order to insert checksums into the metadata in large chunks,
1634 * we wait until bio submission time. All the pages in the bio are
1635 * checksummed and sums are attached onto the ordered extent record.
1637 * At IO completion time the cums attached on the ordered extent record
1638 * are inserted into the btree
1640 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1641 struct bio
*bio
, int mirror_num
,
1642 unsigned long bio_flags
,
1645 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1648 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1649 BUG_ON(ret
); /* -ENOMEM */
1654 * in order to insert checksums into the metadata in large chunks,
1655 * we wait until bio submission time. All the pages in the bio are
1656 * checksummed and sums are attached onto the ordered extent record.
1658 * At IO completion time the cums attached on the ordered extent record
1659 * are inserted into the btree
1661 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1662 int mirror_num
, unsigned long bio_flags
,
1665 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1668 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1670 bio_endio(bio
, ret
);
1675 * extent_io.c submission hook. This does the right thing for csum calculation
1676 * on write, or reading the csums from the tree before a read
1678 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1679 int mirror_num
, unsigned long bio_flags
,
1682 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1686 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1688 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1690 if (btrfs_is_free_space_inode(inode
))
1693 if (!(rw
& REQ_WRITE
)) {
1694 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1698 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1699 ret
= btrfs_submit_compressed_read(inode
, bio
,
1703 } else if (!skip_sum
) {
1704 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1709 } else if (async
&& !skip_sum
) {
1710 /* csum items have already been cloned */
1711 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1713 /* we're doing a write, do the async checksumming */
1714 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1715 inode
, rw
, bio
, mirror_num
,
1716 bio_flags
, bio_offset
,
1717 __btrfs_submit_bio_start
,
1718 __btrfs_submit_bio_done
);
1720 } else if (!skip_sum
) {
1721 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1727 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1731 bio_endio(bio
, ret
);
1736 * given a list of ordered sums record them in the inode. This happens
1737 * at IO completion time based on sums calculated at bio submission time.
1739 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1740 struct inode
*inode
, u64 file_offset
,
1741 struct list_head
*list
)
1743 struct btrfs_ordered_sum
*sum
;
1745 list_for_each_entry(sum
, list
, list
) {
1746 btrfs_csum_file_blocks(trans
,
1747 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1752 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1753 struct extent_state
**cached_state
)
1755 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1756 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1757 cached_state
, GFP_NOFS
);
1760 /* see btrfs_writepage_start_hook for details on why this is required */
1761 struct btrfs_writepage_fixup
{
1763 struct btrfs_work work
;
1766 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1768 struct btrfs_writepage_fixup
*fixup
;
1769 struct btrfs_ordered_extent
*ordered
;
1770 struct extent_state
*cached_state
= NULL
;
1772 struct inode
*inode
;
1777 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1781 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1782 ClearPageChecked(page
);
1786 inode
= page
->mapping
->host
;
1787 page_start
= page_offset(page
);
1788 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1790 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1793 /* already ordered? We're done */
1794 if (PagePrivate2(page
))
1797 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1799 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1800 page_end
, &cached_state
, GFP_NOFS
);
1802 btrfs_start_ordered_extent(inode
, ordered
, 1);
1803 btrfs_put_ordered_extent(ordered
);
1807 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1809 mapping_set_error(page
->mapping
, ret
);
1810 end_extent_writepage(page
, ret
, page_start
, page_end
);
1811 ClearPageChecked(page
);
1815 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1816 ClearPageChecked(page
);
1817 set_page_dirty(page
);
1819 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1820 &cached_state
, GFP_NOFS
);
1823 page_cache_release(page
);
1828 * There are a few paths in the higher layers of the kernel that directly
1829 * set the page dirty bit without asking the filesystem if it is a
1830 * good idea. This causes problems because we want to make sure COW
1831 * properly happens and the data=ordered rules are followed.
1833 * In our case any range that doesn't have the ORDERED bit set
1834 * hasn't been properly setup for IO. We kick off an async process
1835 * to fix it up. The async helper will wait for ordered extents, set
1836 * the delalloc bit and make it safe to write the page.
1838 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1840 struct inode
*inode
= page
->mapping
->host
;
1841 struct btrfs_writepage_fixup
*fixup
;
1842 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1844 /* this page is properly in the ordered list */
1845 if (TestClearPagePrivate2(page
))
1848 if (PageChecked(page
))
1851 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1855 SetPageChecked(page
);
1856 page_cache_get(page
);
1857 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1859 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1863 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1864 struct inode
*inode
, u64 file_pos
,
1865 u64 disk_bytenr
, u64 disk_num_bytes
,
1866 u64 num_bytes
, u64 ram_bytes
,
1867 u8 compression
, u8 encryption
,
1868 u16 other_encoding
, int extent_type
)
1870 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 struct btrfs_file_extent_item
*fi
;
1872 struct btrfs_path
*path
;
1873 struct extent_buffer
*leaf
;
1874 struct btrfs_key ins
;
1877 path
= btrfs_alloc_path();
1881 path
->leave_spinning
= 1;
1884 * we may be replacing one extent in the tree with another.
1885 * The new extent is pinned in the extent map, and we don't want
1886 * to drop it from the cache until it is completely in the btree.
1888 * So, tell btrfs_drop_extents to leave this extent in the cache.
1889 * the caller is expected to unpin it and allow it to be merged
1892 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1893 file_pos
+ num_bytes
, 0);
1897 ins
.objectid
= btrfs_ino(inode
);
1898 ins
.offset
= file_pos
;
1899 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1900 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1903 leaf
= path
->nodes
[0];
1904 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1905 struct btrfs_file_extent_item
);
1906 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1907 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1908 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1909 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1910 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1911 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1912 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1913 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1914 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1915 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1917 btrfs_mark_buffer_dirty(leaf
);
1918 btrfs_release_path(path
);
1920 inode_add_bytes(inode
, num_bytes
);
1922 ins
.objectid
= disk_bytenr
;
1923 ins
.offset
= disk_num_bytes
;
1924 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1925 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1926 root
->root_key
.objectid
,
1927 btrfs_ino(inode
), file_pos
, &ins
);
1929 btrfs_free_path(path
);
1934 /* snapshot-aware defrag */
1935 struct sa_defrag_extent_backref
{
1936 struct rb_node node
;
1937 struct old_sa_defrag_extent
*old
;
1946 struct old_sa_defrag_extent
{
1947 struct list_head list
;
1948 struct new_sa_defrag_extent
*new;
1957 struct new_sa_defrag_extent
{
1958 struct rb_root root
;
1959 struct list_head head
;
1960 struct btrfs_path
*path
;
1961 struct inode
*inode
;
1969 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1970 struct sa_defrag_extent_backref
*b2
)
1972 if (b1
->root_id
< b2
->root_id
)
1974 else if (b1
->root_id
> b2
->root_id
)
1977 if (b1
->inum
< b2
->inum
)
1979 else if (b1
->inum
> b2
->inum
)
1982 if (b1
->file_pos
< b2
->file_pos
)
1984 else if (b1
->file_pos
> b2
->file_pos
)
1988 * [------------------------------] ===> (a range of space)
1989 * |<--->| |<---->| =============> (fs/file tree A)
1990 * |<---------------------------->| ===> (fs/file tree B)
1992 * A range of space can refer to two file extents in one tree while
1993 * refer to only one file extent in another tree.
1995 * So we may process a disk offset more than one time(two extents in A)
1996 * and locate at the same extent(one extent in B), then insert two same
1997 * backrefs(both refer to the extent in B).
2002 static void backref_insert(struct rb_root
*root
,
2003 struct sa_defrag_extent_backref
*backref
)
2005 struct rb_node
**p
= &root
->rb_node
;
2006 struct rb_node
*parent
= NULL
;
2007 struct sa_defrag_extent_backref
*entry
;
2012 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2014 ret
= backref_comp(backref
, entry
);
2018 p
= &(*p
)->rb_right
;
2021 rb_link_node(&backref
->node
, parent
, p
);
2022 rb_insert_color(&backref
->node
, root
);
2026 * Note the backref might has changed, and in this case we just return 0.
2028 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2031 struct btrfs_file_extent_item
*extent
;
2032 struct btrfs_fs_info
*fs_info
;
2033 struct old_sa_defrag_extent
*old
= ctx
;
2034 struct new_sa_defrag_extent
*new = old
->new;
2035 struct btrfs_path
*path
= new->path
;
2036 struct btrfs_key key
;
2037 struct btrfs_root
*root
;
2038 struct sa_defrag_extent_backref
*backref
;
2039 struct extent_buffer
*leaf
;
2040 struct inode
*inode
= new->inode
;
2046 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2047 inum
== btrfs_ino(inode
))
2050 key
.objectid
= root_id
;
2051 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2052 key
.offset
= (u64
)-1;
2054 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2055 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2057 if (PTR_ERR(root
) == -ENOENT
)
2060 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2061 inum
, offset
, root_id
);
2062 return PTR_ERR(root
);
2065 key
.objectid
= inum
;
2066 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2067 if (offset
> (u64
)-1 << 32)
2070 key
.offset
= offset
;
2072 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2081 leaf
= path
->nodes
[0];
2082 slot
= path
->slots
[0];
2084 if (slot
>= btrfs_header_nritems(leaf
)) {
2085 ret
= btrfs_next_leaf(root
, path
);
2088 } else if (ret
> 0) {
2097 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2099 if (key
.objectid
> inum
)
2102 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2105 extent
= btrfs_item_ptr(leaf
, slot
,
2106 struct btrfs_file_extent_item
);
2108 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2111 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2112 if (key
.offset
- extent_offset
!= offset
)
2115 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2116 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2117 old
->len
|| extent_offset
+ num_bytes
<=
2118 old
->extent_offset
+ old
->offset
)
2124 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2130 backref
->root_id
= root_id
;
2131 backref
->inum
= inum
;
2132 backref
->file_pos
= offset
+ extent_offset
;
2133 backref
->num_bytes
= num_bytes
;
2134 backref
->extent_offset
= extent_offset
;
2135 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2137 backref_insert(&new->root
, backref
);
2140 btrfs_release_path(path
);
2145 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2146 struct new_sa_defrag_extent
*new)
2148 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2149 struct old_sa_defrag_extent
*old
, *tmp
;
2154 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2155 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2156 path
, record_one_backref
,
2158 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2160 /* no backref to be processed for this extent */
2162 list_del(&old
->list
);
2167 if (list_empty(&new->head
))
2173 static int relink_is_mergable(struct extent_buffer
*leaf
,
2174 struct btrfs_file_extent_item
*fi
,
2177 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2180 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2183 if (btrfs_file_extent_compression(leaf
, fi
) ||
2184 btrfs_file_extent_encryption(leaf
, fi
) ||
2185 btrfs_file_extent_other_encoding(leaf
, fi
))
2192 * Note the backref might has changed, and in this case we just return 0.
2194 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2195 struct sa_defrag_extent_backref
*prev
,
2196 struct sa_defrag_extent_backref
*backref
)
2198 struct btrfs_file_extent_item
*extent
;
2199 struct btrfs_file_extent_item
*item
;
2200 struct btrfs_ordered_extent
*ordered
;
2201 struct btrfs_trans_handle
*trans
;
2202 struct btrfs_fs_info
*fs_info
;
2203 struct btrfs_root
*root
;
2204 struct btrfs_key key
;
2205 struct extent_buffer
*leaf
;
2206 struct old_sa_defrag_extent
*old
= backref
->old
;
2207 struct new_sa_defrag_extent
*new = old
->new;
2208 struct inode
*src_inode
= new->inode
;
2209 struct inode
*inode
;
2210 struct extent_state
*cached
= NULL
;
2219 if (prev
&& prev
->root_id
== backref
->root_id
&&
2220 prev
->inum
== backref
->inum
&&
2221 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2224 /* step 1: get root */
2225 key
.objectid
= backref
->root_id
;
2226 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2227 key
.offset
= (u64
)-1;
2229 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2230 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2232 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2234 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2235 if (PTR_ERR(root
) == -ENOENT
)
2237 return PTR_ERR(root
);
2239 if (btrfs_root_refs(&root
->root_item
) == 0) {
2240 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2241 /* parse ENOENT to 0 */
2245 /* step 2: get inode */
2246 key
.objectid
= backref
->inum
;
2247 key
.type
= BTRFS_INODE_ITEM_KEY
;
2250 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2251 if (IS_ERR(inode
)) {
2252 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2256 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2258 /* step 3: relink backref */
2259 lock_start
= backref
->file_pos
;
2260 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2261 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2264 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2266 btrfs_put_ordered_extent(ordered
);
2270 trans
= btrfs_join_transaction(root
);
2271 if (IS_ERR(trans
)) {
2272 ret
= PTR_ERR(trans
);
2276 key
.objectid
= backref
->inum
;
2277 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2278 key
.offset
= backref
->file_pos
;
2280 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2283 } else if (ret
> 0) {
2288 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2289 struct btrfs_file_extent_item
);
2291 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2292 backref
->generation
)
2295 btrfs_release_path(path
);
2297 start
= backref
->file_pos
;
2298 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2299 start
+= old
->extent_offset
+ old
->offset
-
2300 backref
->extent_offset
;
2302 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2303 old
->extent_offset
+ old
->offset
+ old
->len
);
2304 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2306 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2311 key
.objectid
= btrfs_ino(inode
);
2312 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2315 path
->leave_spinning
= 1;
2317 struct btrfs_file_extent_item
*fi
;
2319 struct btrfs_key found_key
;
2321 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2326 leaf
= path
->nodes
[0];
2327 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2329 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2330 struct btrfs_file_extent_item
);
2331 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2333 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2334 extent_len
+ found_key
.offset
== start
) {
2335 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2337 btrfs_mark_buffer_dirty(leaf
);
2338 inode_add_bytes(inode
, len
);
2344 btrfs_release_path(path
);
2349 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2352 btrfs_abort_transaction(trans
, root
, ret
);
2356 leaf
= path
->nodes
[0];
2357 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2358 struct btrfs_file_extent_item
);
2359 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2360 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2361 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2362 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2363 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2364 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2365 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2366 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2367 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2368 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2370 btrfs_mark_buffer_dirty(leaf
);
2371 inode_add_bytes(inode
, len
);
2372 btrfs_release_path(path
);
2374 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2376 backref
->root_id
, backref
->inum
,
2377 new->file_pos
, 0); /* start - extent_offset */
2379 btrfs_abort_transaction(trans
, root
, ret
);
2385 btrfs_release_path(path
);
2386 path
->leave_spinning
= 0;
2387 btrfs_end_transaction(trans
, root
);
2389 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2395 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2397 struct btrfs_path
*path
;
2398 struct old_sa_defrag_extent
*old
, *tmp
;
2399 struct sa_defrag_extent_backref
*backref
;
2400 struct sa_defrag_extent_backref
*prev
= NULL
;
2401 struct inode
*inode
;
2402 struct btrfs_root
*root
;
2403 struct rb_node
*node
;
2407 root
= BTRFS_I(inode
)->root
;
2409 path
= btrfs_alloc_path();
2413 if (!record_extent_backrefs(path
, new)) {
2414 btrfs_free_path(path
);
2417 btrfs_release_path(path
);
2420 node
= rb_first(&new->root
);
2423 rb_erase(node
, &new->root
);
2425 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2427 ret
= relink_extent_backref(path
, prev
, backref
);
2440 btrfs_free_path(path
);
2442 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2443 list_del(&old
->list
);
2447 atomic_dec(&root
->fs_info
->defrag_running
);
2448 wake_up(&root
->fs_info
->transaction_wait
);
2453 static struct new_sa_defrag_extent
*
2454 record_old_file_extents(struct inode
*inode
,
2455 struct btrfs_ordered_extent
*ordered
)
2457 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2458 struct btrfs_path
*path
;
2459 struct btrfs_key key
;
2460 struct old_sa_defrag_extent
*old
, *tmp
;
2461 struct new_sa_defrag_extent
*new;
2464 new = kmalloc(sizeof(*new), GFP_NOFS
);
2469 new->file_pos
= ordered
->file_offset
;
2470 new->len
= ordered
->len
;
2471 new->bytenr
= ordered
->start
;
2472 new->disk_len
= ordered
->disk_len
;
2473 new->compress_type
= ordered
->compress_type
;
2474 new->root
= RB_ROOT
;
2475 INIT_LIST_HEAD(&new->head
);
2477 path
= btrfs_alloc_path();
2481 key
.objectid
= btrfs_ino(inode
);
2482 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2483 key
.offset
= new->file_pos
;
2485 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2488 if (ret
> 0 && path
->slots
[0] > 0)
2491 /* find out all the old extents for the file range */
2493 struct btrfs_file_extent_item
*extent
;
2494 struct extent_buffer
*l
;
2503 slot
= path
->slots
[0];
2505 if (slot
>= btrfs_header_nritems(l
)) {
2506 ret
= btrfs_next_leaf(root
, path
);
2514 btrfs_item_key_to_cpu(l
, &key
, slot
);
2516 if (key
.objectid
!= btrfs_ino(inode
))
2518 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2520 if (key
.offset
>= new->file_pos
+ new->len
)
2523 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2525 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2526 if (key
.offset
+ num_bytes
< new->file_pos
)
2529 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2533 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2535 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2539 offset
= max(new->file_pos
, key
.offset
);
2540 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2542 old
->bytenr
= disk_bytenr
;
2543 old
->extent_offset
= extent_offset
;
2544 old
->offset
= offset
- key
.offset
;
2545 old
->len
= end
- offset
;
2548 list_add_tail(&old
->list
, &new->head
);
2554 btrfs_free_path(path
);
2555 atomic_inc(&root
->fs_info
->defrag_running
);
2560 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2561 list_del(&old
->list
);
2565 btrfs_free_path(path
);
2572 * helper function for btrfs_finish_ordered_io, this
2573 * just reads in some of the csum leaves to prime them into ram
2574 * before we start the transaction. It limits the amount of btree
2575 * reads required while inside the transaction.
2577 /* as ordered data IO finishes, this gets called so we can finish
2578 * an ordered extent if the range of bytes in the file it covers are
2581 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2583 struct inode
*inode
= ordered_extent
->inode
;
2584 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2585 struct btrfs_trans_handle
*trans
= NULL
;
2586 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2587 struct extent_state
*cached_state
= NULL
;
2588 struct new_sa_defrag_extent
*new = NULL
;
2589 int compress_type
= 0;
2593 nolock
= btrfs_is_free_space_inode(inode
);
2595 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2600 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2601 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2602 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2604 trans
= btrfs_join_transaction_nolock(root
);
2606 trans
= btrfs_join_transaction(root
);
2607 if (IS_ERR(trans
)) {
2608 ret
= PTR_ERR(trans
);
2612 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2613 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2614 if (ret
) /* -ENOMEM or corruption */
2615 btrfs_abort_transaction(trans
, root
, ret
);
2619 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2620 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2623 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2624 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2625 EXTENT_DEFRAG
, 1, cached_state
);
2627 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2628 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2629 /* the inode is shared */
2630 new = record_old_file_extents(inode
, ordered_extent
);
2632 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2633 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2634 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2638 trans
= btrfs_join_transaction_nolock(root
);
2640 trans
= btrfs_join_transaction(root
);
2641 if (IS_ERR(trans
)) {
2642 ret
= PTR_ERR(trans
);
2646 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2648 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2649 compress_type
= ordered_extent
->compress_type
;
2650 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2651 BUG_ON(compress_type
);
2652 ret
= btrfs_mark_extent_written(trans
, inode
,
2653 ordered_extent
->file_offset
,
2654 ordered_extent
->file_offset
+
2655 ordered_extent
->len
);
2657 BUG_ON(root
== root
->fs_info
->tree_root
);
2658 ret
= insert_reserved_file_extent(trans
, inode
,
2659 ordered_extent
->file_offset
,
2660 ordered_extent
->start
,
2661 ordered_extent
->disk_len
,
2662 ordered_extent
->len
,
2663 ordered_extent
->len
,
2664 compress_type
, 0, 0,
2665 BTRFS_FILE_EXTENT_REG
);
2667 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2668 ordered_extent
->file_offset
, ordered_extent
->len
,
2671 btrfs_abort_transaction(trans
, root
, ret
);
2675 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2676 &ordered_extent
->list
);
2678 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2679 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2680 if (ret
) { /* -ENOMEM or corruption */
2681 btrfs_abort_transaction(trans
, root
, ret
);
2686 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2687 ordered_extent
->file_offset
+
2688 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2690 if (root
!= root
->fs_info
->tree_root
)
2691 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2693 btrfs_end_transaction(trans
, root
);
2696 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2697 ordered_extent
->file_offset
+
2698 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2701 * If the ordered extent had an IOERR or something else went
2702 * wrong we need to return the space for this ordered extent
2703 * back to the allocator.
2705 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2706 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2707 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2708 ordered_extent
->disk_len
);
2713 * This needs to be done to make sure anybody waiting knows we are done
2714 * updating everything for this ordered extent.
2716 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2718 /* for snapshot-aware defrag */
2720 relink_file_extents(new);
2723 btrfs_put_ordered_extent(ordered_extent
);
2724 /* once for the tree */
2725 btrfs_put_ordered_extent(ordered_extent
);
2730 static void finish_ordered_fn(struct btrfs_work
*work
)
2732 struct btrfs_ordered_extent
*ordered_extent
;
2733 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2734 btrfs_finish_ordered_io(ordered_extent
);
2737 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2738 struct extent_state
*state
, int uptodate
)
2740 struct inode
*inode
= page
->mapping
->host
;
2741 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2742 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2743 struct btrfs_workers
*workers
;
2745 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2747 ClearPagePrivate2(page
);
2748 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2749 end
- start
+ 1, uptodate
))
2752 ordered_extent
->work
.func
= finish_ordered_fn
;
2753 ordered_extent
->work
.flags
= 0;
2755 if (btrfs_is_free_space_inode(inode
))
2756 workers
= &root
->fs_info
->endio_freespace_worker
;
2758 workers
= &root
->fs_info
->endio_write_workers
;
2759 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2765 * when reads are done, we need to check csums to verify the data is correct
2766 * if there's a match, we allow the bio to finish. If not, the code in
2767 * extent_io.c will try to find good copies for us.
2769 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2770 struct extent_state
*state
, int mirror
)
2772 size_t offset
= start
- page_offset(page
);
2773 struct inode
*inode
= page
->mapping
->host
;
2774 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2776 u64
private = ~(u32
)0;
2778 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2781 if (PageChecked(page
)) {
2782 ClearPageChecked(page
);
2786 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2789 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2790 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2791 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2796 if (state
&& state
->start
== start
) {
2797 private = state
->private;
2800 ret
= get_state_private(io_tree
, start
, &private);
2802 kaddr
= kmap_atomic(page
);
2806 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
2807 btrfs_csum_final(csum
, (char *)&csum
);
2808 if (csum
!= private)
2811 kunmap_atomic(kaddr
);
2816 printk_ratelimited(KERN_INFO
"btrfs csum failed ino %llu off %llu csum %u "
2818 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2819 (unsigned long long)start
, csum
,
2820 (unsigned long long)private);
2821 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2822 flush_dcache_page(page
);
2823 kunmap_atomic(kaddr
);
2829 struct delayed_iput
{
2830 struct list_head list
;
2831 struct inode
*inode
;
2834 /* JDM: If this is fs-wide, why can't we add a pointer to
2835 * btrfs_inode instead and avoid the allocation? */
2836 void btrfs_add_delayed_iput(struct inode
*inode
)
2838 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2839 struct delayed_iput
*delayed
;
2841 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2844 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2845 delayed
->inode
= inode
;
2847 spin_lock(&fs_info
->delayed_iput_lock
);
2848 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2849 spin_unlock(&fs_info
->delayed_iput_lock
);
2852 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2855 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2856 struct delayed_iput
*delayed
;
2859 spin_lock(&fs_info
->delayed_iput_lock
);
2860 empty
= list_empty(&fs_info
->delayed_iputs
);
2861 spin_unlock(&fs_info
->delayed_iput_lock
);
2865 spin_lock(&fs_info
->delayed_iput_lock
);
2866 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2867 spin_unlock(&fs_info
->delayed_iput_lock
);
2869 while (!list_empty(&list
)) {
2870 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2871 list_del(&delayed
->list
);
2872 iput(delayed
->inode
);
2878 * This is called in transaction commit time. If there are no orphan
2879 * files in the subvolume, it removes orphan item and frees block_rsv
2882 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2883 struct btrfs_root
*root
)
2885 struct btrfs_block_rsv
*block_rsv
;
2888 if (atomic_read(&root
->orphan_inodes
) ||
2889 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2892 spin_lock(&root
->orphan_lock
);
2893 if (atomic_read(&root
->orphan_inodes
)) {
2894 spin_unlock(&root
->orphan_lock
);
2898 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2899 spin_unlock(&root
->orphan_lock
);
2903 block_rsv
= root
->orphan_block_rsv
;
2904 root
->orphan_block_rsv
= NULL
;
2905 spin_unlock(&root
->orphan_lock
);
2907 if (root
->orphan_item_inserted
&&
2908 btrfs_root_refs(&root
->root_item
) > 0) {
2909 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2910 root
->root_key
.objectid
);
2912 root
->orphan_item_inserted
= 0;
2916 WARN_ON(block_rsv
->size
> 0);
2917 btrfs_free_block_rsv(root
, block_rsv
);
2922 * This creates an orphan entry for the given inode in case something goes
2923 * wrong in the middle of an unlink/truncate.
2925 * NOTE: caller of this function should reserve 5 units of metadata for
2928 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2930 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2931 struct btrfs_block_rsv
*block_rsv
= NULL
;
2936 if (!root
->orphan_block_rsv
) {
2937 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2942 spin_lock(&root
->orphan_lock
);
2943 if (!root
->orphan_block_rsv
) {
2944 root
->orphan_block_rsv
= block_rsv
;
2945 } else if (block_rsv
) {
2946 btrfs_free_block_rsv(root
, block_rsv
);
2950 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2951 &BTRFS_I(inode
)->runtime_flags
)) {
2954 * For proper ENOSPC handling, we should do orphan
2955 * cleanup when mounting. But this introduces backward
2956 * compatibility issue.
2958 if (!xchg(&root
->orphan_item_inserted
, 1))
2964 atomic_inc(&root
->orphan_inodes
);
2967 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2968 &BTRFS_I(inode
)->runtime_flags
))
2970 spin_unlock(&root
->orphan_lock
);
2972 /* grab metadata reservation from transaction handle */
2974 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2975 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2978 /* insert an orphan item to track this unlinked/truncated file */
2980 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2981 if (ret
&& ret
!= -EEXIST
) {
2982 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2983 &BTRFS_I(inode
)->runtime_flags
);
2984 btrfs_abort_transaction(trans
, root
, ret
);
2990 /* insert an orphan item to track subvolume contains orphan files */
2992 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2993 root
->root_key
.objectid
);
2994 if (ret
&& ret
!= -EEXIST
) {
2995 btrfs_abort_transaction(trans
, root
, ret
);
3003 * We have done the truncate/delete so we can go ahead and remove the orphan
3004 * item for this particular inode.
3006 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3009 int delete_item
= 0;
3010 int release_rsv
= 0;
3013 spin_lock(&root
->orphan_lock
);
3014 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3015 &BTRFS_I(inode
)->runtime_flags
))
3018 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3019 &BTRFS_I(inode
)->runtime_flags
))
3021 spin_unlock(&root
->orphan_lock
);
3023 if (trans
&& delete_item
) {
3024 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3025 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3029 btrfs_orphan_release_metadata(inode
);
3030 atomic_dec(&root
->orphan_inodes
);
3037 * this cleans up any orphans that may be left on the list from the last use
3040 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3042 struct btrfs_path
*path
;
3043 struct extent_buffer
*leaf
;
3044 struct btrfs_key key
, found_key
;
3045 struct btrfs_trans_handle
*trans
;
3046 struct inode
*inode
;
3047 u64 last_objectid
= 0;
3048 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3050 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3053 path
= btrfs_alloc_path();
3060 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3061 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3062 key
.offset
= (u64
)-1;
3065 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3070 * if ret == 0 means we found what we were searching for, which
3071 * is weird, but possible, so only screw with path if we didn't
3072 * find the key and see if we have stuff that matches
3076 if (path
->slots
[0] == 0)
3081 /* pull out the item */
3082 leaf
= path
->nodes
[0];
3083 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3085 /* make sure the item matches what we want */
3086 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3088 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3091 /* release the path since we're done with it */
3092 btrfs_release_path(path
);
3095 * this is where we are basically btrfs_lookup, without the
3096 * crossing root thing. we store the inode number in the
3097 * offset of the orphan item.
3100 if (found_key
.offset
== last_objectid
) {
3101 printk(KERN_ERR
"btrfs: Error removing orphan entry, "
3102 "stopping orphan cleanup\n");
3107 last_objectid
= found_key
.offset
;
3109 found_key
.objectid
= found_key
.offset
;
3110 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3111 found_key
.offset
= 0;
3112 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3113 ret
= PTR_RET(inode
);
3114 if (ret
&& ret
!= -ESTALE
)
3117 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3118 struct btrfs_root
*dead_root
;
3119 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3120 int is_dead_root
= 0;
3123 * this is an orphan in the tree root. Currently these
3124 * could come from 2 sources:
3125 * a) a snapshot deletion in progress
3126 * b) a free space cache inode
3127 * We need to distinguish those two, as the snapshot
3128 * orphan must not get deleted.
3129 * find_dead_roots already ran before us, so if this
3130 * is a snapshot deletion, we should find the root
3131 * in the dead_roots list
3133 spin_lock(&fs_info
->trans_lock
);
3134 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3136 if (dead_root
->root_key
.objectid
==
3137 found_key
.objectid
) {
3142 spin_unlock(&fs_info
->trans_lock
);
3144 /* prevent this orphan from being found again */
3145 key
.offset
= found_key
.objectid
- 1;
3150 * Inode is already gone but the orphan item is still there,
3151 * kill the orphan item.
3153 if (ret
== -ESTALE
) {
3154 trans
= btrfs_start_transaction(root
, 1);
3155 if (IS_ERR(trans
)) {
3156 ret
= PTR_ERR(trans
);
3159 printk(KERN_ERR
"auto deleting %Lu\n",
3160 found_key
.objectid
);
3161 ret
= btrfs_del_orphan_item(trans
, root
,
3162 found_key
.objectid
);
3163 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3164 btrfs_end_transaction(trans
, root
);
3169 * add this inode to the orphan list so btrfs_orphan_del does
3170 * the proper thing when we hit it
3172 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3173 &BTRFS_I(inode
)->runtime_flags
);
3174 atomic_inc(&root
->orphan_inodes
);
3176 /* if we have links, this was a truncate, lets do that */
3177 if (inode
->i_nlink
) {
3178 if (!S_ISREG(inode
->i_mode
)) {
3185 /* 1 for the orphan item deletion. */
3186 trans
= btrfs_start_transaction(root
, 1);
3187 if (IS_ERR(trans
)) {
3188 ret
= PTR_ERR(trans
);
3191 ret
= btrfs_orphan_add(trans
, inode
);
3192 btrfs_end_transaction(trans
, root
);
3196 ret
= btrfs_truncate(inode
);
3198 btrfs_orphan_del(NULL
, inode
);
3203 /* this will do delete_inode and everything for us */
3208 /* release the path since we're done with it */
3209 btrfs_release_path(path
);
3211 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3213 if (root
->orphan_block_rsv
)
3214 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3217 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3218 trans
= btrfs_join_transaction(root
);
3220 btrfs_end_transaction(trans
, root
);
3224 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
3226 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
3230 printk(KERN_CRIT
"btrfs: could not do orphan cleanup %d\n", ret
);
3231 btrfs_free_path(path
);
3236 * very simple check to peek ahead in the leaf looking for xattrs. If we
3237 * don't find any xattrs, we know there can't be any acls.
3239 * slot is the slot the inode is in, objectid is the objectid of the inode
3241 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3242 int slot
, u64 objectid
)
3244 u32 nritems
= btrfs_header_nritems(leaf
);
3245 struct btrfs_key found_key
;
3249 while (slot
< nritems
) {
3250 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3252 /* we found a different objectid, there must not be acls */
3253 if (found_key
.objectid
!= objectid
)
3256 /* we found an xattr, assume we've got an acl */
3257 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
3261 * we found a key greater than an xattr key, there can't
3262 * be any acls later on
3264 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3271 * it goes inode, inode backrefs, xattrs, extents,
3272 * so if there are a ton of hard links to an inode there can
3273 * be a lot of backrefs. Don't waste time searching too hard,
3274 * this is just an optimization
3279 /* we hit the end of the leaf before we found an xattr or
3280 * something larger than an xattr. We have to assume the inode
3287 * read an inode from the btree into the in-memory inode
3289 static void btrfs_read_locked_inode(struct inode
*inode
)
3291 struct btrfs_path
*path
;
3292 struct extent_buffer
*leaf
;
3293 struct btrfs_inode_item
*inode_item
;
3294 struct btrfs_timespec
*tspec
;
3295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3296 struct btrfs_key location
;
3300 bool filled
= false;
3302 ret
= btrfs_fill_inode(inode
, &rdev
);
3306 path
= btrfs_alloc_path();
3310 path
->leave_spinning
= 1;
3311 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3313 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3317 leaf
= path
->nodes
[0];
3322 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3323 struct btrfs_inode_item
);
3324 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3325 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3326 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3327 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3328 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3330 tspec
= btrfs_inode_atime(inode_item
);
3331 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3332 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3334 tspec
= btrfs_inode_mtime(inode_item
);
3335 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3336 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3338 tspec
= btrfs_inode_ctime(inode_item
);
3339 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3340 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3342 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3343 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3344 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3347 * If we were modified in the current generation and evicted from memory
3348 * and then re-read we need to do a full sync since we don't have any
3349 * idea about which extents were modified before we were evicted from
3352 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3353 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3354 &BTRFS_I(inode
)->runtime_flags
);
3356 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3357 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3359 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3361 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3362 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3365 * try to precache a NULL acl entry for files that don't have
3366 * any xattrs or acls
3368 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3371 cache_no_acl(inode
);
3373 btrfs_free_path(path
);
3375 switch (inode
->i_mode
& S_IFMT
) {
3377 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3378 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3379 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3380 inode
->i_fop
= &btrfs_file_operations
;
3381 inode
->i_op
= &btrfs_file_inode_operations
;
3384 inode
->i_fop
= &btrfs_dir_file_operations
;
3385 if (root
== root
->fs_info
->tree_root
)
3386 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3388 inode
->i_op
= &btrfs_dir_inode_operations
;
3391 inode
->i_op
= &btrfs_symlink_inode_operations
;
3392 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3393 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3396 inode
->i_op
= &btrfs_special_inode_operations
;
3397 init_special_inode(inode
, inode
->i_mode
, rdev
);
3401 btrfs_update_iflags(inode
);
3405 btrfs_free_path(path
);
3406 make_bad_inode(inode
);
3410 * given a leaf and an inode, copy the inode fields into the leaf
3412 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3413 struct extent_buffer
*leaf
,
3414 struct btrfs_inode_item
*item
,
3415 struct inode
*inode
)
3417 struct btrfs_map_token token
;
3419 btrfs_init_map_token(&token
);
3421 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3422 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3423 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3425 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3426 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3428 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3429 inode
->i_atime
.tv_sec
, &token
);
3430 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3431 inode
->i_atime
.tv_nsec
, &token
);
3433 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3434 inode
->i_mtime
.tv_sec
, &token
);
3435 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3436 inode
->i_mtime
.tv_nsec
, &token
);
3438 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3439 inode
->i_ctime
.tv_sec
, &token
);
3440 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3441 inode
->i_ctime
.tv_nsec
, &token
);
3443 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3445 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3447 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3448 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3449 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3450 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3451 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3455 * copy everything in the in-memory inode into the btree.
3457 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3458 struct btrfs_root
*root
, struct inode
*inode
)
3460 struct btrfs_inode_item
*inode_item
;
3461 struct btrfs_path
*path
;
3462 struct extent_buffer
*leaf
;
3465 path
= btrfs_alloc_path();
3469 path
->leave_spinning
= 1;
3470 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3478 btrfs_unlock_up_safe(path
, 1);
3479 leaf
= path
->nodes
[0];
3480 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3481 struct btrfs_inode_item
);
3483 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3484 btrfs_mark_buffer_dirty(leaf
);
3485 btrfs_set_inode_last_trans(trans
, inode
);
3488 btrfs_free_path(path
);
3493 * copy everything in the in-memory inode into the btree.
3495 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3496 struct btrfs_root
*root
, struct inode
*inode
)
3501 * If the inode is a free space inode, we can deadlock during commit
3502 * if we put it into the delayed code.
3504 * The data relocation inode should also be directly updated
3507 if (!btrfs_is_free_space_inode(inode
)
3508 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3509 btrfs_update_root_times(trans
, root
);
3511 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3513 btrfs_set_inode_last_trans(trans
, inode
);
3517 return btrfs_update_inode_item(trans
, root
, inode
);
3520 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3521 struct btrfs_root
*root
,
3522 struct inode
*inode
)
3526 ret
= btrfs_update_inode(trans
, root
, inode
);
3528 return btrfs_update_inode_item(trans
, root
, inode
);
3533 * unlink helper that gets used here in inode.c and in the tree logging
3534 * recovery code. It remove a link in a directory with a given name, and
3535 * also drops the back refs in the inode to the directory
3537 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3538 struct btrfs_root
*root
,
3539 struct inode
*dir
, struct inode
*inode
,
3540 const char *name
, int name_len
)
3542 struct btrfs_path
*path
;
3544 struct extent_buffer
*leaf
;
3545 struct btrfs_dir_item
*di
;
3546 struct btrfs_key key
;
3548 u64 ino
= btrfs_ino(inode
);
3549 u64 dir_ino
= btrfs_ino(dir
);
3551 path
= btrfs_alloc_path();
3557 path
->leave_spinning
= 1;
3558 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3559 name
, name_len
, -1);
3568 leaf
= path
->nodes
[0];
3569 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3570 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3573 btrfs_release_path(path
);
3575 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3578 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
3579 "inode %llu parent %llu\n", name_len
, name
,
3580 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3581 btrfs_abort_transaction(trans
, root
, ret
);
3585 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3587 btrfs_abort_transaction(trans
, root
, ret
);
3591 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3593 if (ret
!= 0 && ret
!= -ENOENT
) {
3594 btrfs_abort_transaction(trans
, root
, ret
);
3598 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3603 btrfs_free_path(path
);
3607 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3608 inode_inc_iversion(inode
);
3609 inode_inc_iversion(dir
);
3610 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3611 ret
= btrfs_update_inode(trans
, root
, dir
);
3616 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3617 struct btrfs_root
*root
,
3618 struct inode
*dir
, struct inode
*inode
,
3619 const char *name
, int name_len
)
3622 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3624 btrfs_drop_nlink(inode
);
3625 ret
= btrfs_update_inode(trans
, root
, inode
);
3631 /* helper to check if there is any shared block in the path */
3632 static int check_path_shared(struct btrfs_root
*root
,
3633 struct btrfs_path
*path
)
3635 struct extent_buffer
*eb
;
3639 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
3642 if (!path
->nodes
[level
])
3644 eb
= path
->nodes
[level
];
3645 if (!btrfs_block_can_be_shared(root
, eb
))
3647 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, eb
->len
,
3656 * helper to start transaction for unlink and rmdir.
3658 * unlink and rmdir are special in btrfs, they do not always free space.
3659 * so in enospc case, we should make sure they will free space before
3660 * allowing them to use the global metadata reservation.
3662 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3663 struct dentry
*dentry
)
3665 struct btrfs_trans_handle
*trans
;
3666 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3667 struct btrfs_path
*path
;
3668 struct btrfs_dir_item
*di
;
3669 struct inode
*inode
= dentry
->d_inode
;
3674 u64 ino
= btrfs_ino(inode
);
3675 u64 dir_ino
= btrfs_ino(dir
);
3678 * 1 for the possible orphan item
3679 * 1 for the dir item
3680 * 1 for the dir index
3681 * 1 for the inode ref
3682 * 1 for the inode ref in the tree log
3683 * 2 for the dir entries in the log
3686 trans
= btrfs_start_transaction(root
, 8);
3687 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3690 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3691 return ERR_PTR(-ENOSPC
);
3693 /* check if there is someone else holds reference */
3694 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3695 return ERR_PTR(-ENOSPC
);
3697 if (atomic_read(&inode
->i_count
) > 2)
3698 return ERR_PTR(-ENOSPC
);
3700 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3701 return ERR_PTR(-ENOSPC
);
3703 path
= btrfs_alloc_path();
3705 root
->fs_info
->enospc_unlink
= 0;
3706 return ERR_PTR(-ENOMEM
);
3709 /* 1 for the orphan item */
3710 trans
= btrfs_start_transaction(root
, 1);
3711 if (IS_ERR(trans
)) {
3712 btrfs_free_path(path
);
3713 root
->fs_info
->enospc_unlink
= 0;
3717 path
->skip_locking
= 1;
3718 path
->search_commit_root
= 1;
3720 ret
= btrfs_lookup_inode(trans
, root
, path
,
3721 &BTRFS_I(dir
)->location
, 0);
3727 if (check_path_shared(root
, path
))
3732 btrfs_release_path(path
);
3734 ret
= btrfs_lookup_inode(trans
, root
, path
,
3735 &BTRFS_I(inode
)->location
, 0);
3741 if (check_path_shared(root
, path
))
3746 btrfs_release_path(path
);
3748 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3749 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3755 BUG_ON(ret
== 0); /* Corruption */
3756 if (check_path_shared(root
, path
))
3758 btrfs_release_path(path
);
3766 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3767 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3773 if (check_path_shared(root
, path
))
3779 btrfs_release_path(path
);
3781 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3782 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3789 if (check_path_shared(root
, path
))
3792 btrfs_release_path(path
);
3795 * This is a commit root search, if we can lookup inode item and other
3796 * relative items in the commit root, it means the transaction of
3797 * dir/file creation has been committed, and the dir index item that we
3798 * delay to insert has also been inserted into the commit root. So
3799 * we needn't worry about the delayed insertion of the dir index item
3802 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3803 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3808 BUG_ON(ret
== -ENOENT
);
3809 if (check_path_shared(root
, path
))
3814 btrfs_free_path(path
);
3815 /* Migrate the orphan reservation over */
3817 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3818 &root
->fs_info
->global_block_rsv
,
3819 trans
->bytes_reserved
);
3822 btrfs_end_transaction(trans
, root
);
3823 root
->fs_info
->enospc_unlink
= 0;
3824 return ERR_PTR(err
);
3827 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3831 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3832 struct btrfs_root
*root
)
3834 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3835 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3836 trans
->bytes_reserved
);
3837 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3838 BUG_ON(!root
->fs_info
->enospc_unlink
);
3839 root
->fs_info
->enospc_unlink
= 0;
3841 btrfs_end_transaction(trans
, root
);
3844 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3846 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3847 struct btrfs_trans_handle
*trans
;
3848 struct inode
*inode
= dentry
->d_inode
;
3851 trans
= __unlink_start_trans(dir
, dentry
);
3853 return PTR_ERR(trans
);
3855 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3857 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3858 dentry
->d_name
.name
, dentry
->d_name
.len
);
3862 if (inode
->i_nlink
== 0) {
3863 ret
= btrfs_orphan_add(trans
, inode
);
3869 __unlink_end_trans(trans
, root
);
3870 btrfs_btree_balance_dirty(root
);
3874 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3875 struct btrfs_root
*root
,
3876 struct inode
*dir
, u64 objectid
,
3877 const char *name
, int name_len
)
3879 struct btrfs_path
*path
;
3880 struct extent_buffer
*leaf
;
3881 struct btrfs_dir_item
*di
;
3882 struct btrfs_key key
;
3885 u64 dir_ino
= btrfs_ino(dir
);
3887 path
= btrfs_alloc_path();
3891 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3892 name
, name_len
, -1);
3893 if (IS_ERR_OR_NULL(di
)) {
3901 leaf
= path
->nodes
[0];
3902 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3903 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3904 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3906 btrfs_abort_transaction(trans
, root
, ret
);
3909 btrfs_release_path(path
);
3911 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3912 objectid
, root
->root_key
.objectid
,
3913 dir_ino
, &index
, name
, name_len
);
3915 if (ret
!= -ENOENT
) {
3916 btrfs_abort_transaction(trans
, root
, ret
);
3919 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3921 if (IS_ERR_OR_NULL(di
)) {
3926 btrfs_abort_transaction(trans
, root
, ret
);
3930 leaf
= path
->nodes
[0];
3931 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3932 btrfs_release_path(path
);
3935 btrfs_release_path(path
);
3937 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3939 btrfs_abort_transaction(trans
, root
, ret
);
3943 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3944 inode_inc_iversion(dir
);
3945 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3946 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3948 btrfs_abort_transaction(trans
, root
, ret
);
3950 btrfs_free_path(path
);
3954 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3956 struct inode
*inode
= dentry
->d_inode
;
3958 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3959 struct btrfs_trans_handle
*trans
;
3961 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3963 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3966 trans
= __unlink_start_trans(dir
, dentry
);
3968 return PTR_ERR(trans
);
3970 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3971 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3972 BTRFS_I(inode
)->location
.objectid
,
3973 dentry
->d_name
.name
,
3974 dentry
->d_name
.len
);
3978 err
= btrfs_orphan_add(trans
, inode
);
3982 /* now the directory is empty */
3983 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3984 dentry
->d_name
.name
, dentry
->d_name
.len
);
3986 btrfs_i_size_write(inode
, 0);
3988 __unlink_end_trans(trans
, root
);
3989 btrfs_btree_balance_dirty(root
);
3995 * this can truncate away extent items, csum items and directory items.
3996 * It starts at a high offset and removes keys until it can't find
3997 * any higher than new_size
3999 * csum items that cross the new i_size are truncated to the new size
4002 * min_type is the minimum key type to truncate down to. If set to 0, this
4003 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4005 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4006 struct btrfs_root
*root
,
4007 struct inode
*inode
,
4008 u64 new_size
, u32 min_type
)
4010 struct btrfs_path
*path
;
4011 struct extent_buffer
*leaf
;
4012 struct btrfs_file_extent_item
*fi
;
4013 struct btrfs_key key
;
4014 struct btrfs_key found_key
;
4015 u64 extent_start
= 0;
4016 u64 extent_num_bytes
= 0;
4017 u64 extent_offset
= 0;
4019 u32 found_type
= (u8
)-1;
4022 int pending_del_nr
= 0;
4023 int pending_del_slot
= 0;
4024 int extent_type
= -1;
4027 u64 ino
= btrfs_ino(inode
);
4029 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4031 path
= btrfs_alloc_path();
4037 * We want to drop from the next block forward in case this new size is
4038 * not block aligned since we will be keeping the last block of the
4039 * extent just the way it is.
4041 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
4042 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4043 root
->sectorsize
), (u64
)-1, 0);
4046 * This function is also used to drop the items in the log tree before
4047 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4048 * it is used to drop the loged items. So we shouldn't kill the delayed
4051 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4052 btrfs_kill_delayed_inode_items(inode
);
4055 key
.offset
= (u64
)-1;
4059 path
->leave_spinning
= 1;
4060 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4067 /* there are no items in the tree for us to truncate, we're
4070 if (path
->slots
[0] == 0)
4077 leaf
= path
->nodes
[0];
4078 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4079 found_type
= btrfs_key_type(&found_key
);
4081 if (found_key
.objectid
!= ino
)
4084 if (found_type
< min_type
)
4087 item_end
= found_key
.offset
;
4088 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4089 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4090 struct btrfs_file_extent_item
);
4091 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4092 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4094 btrfs_file_extent_num_bytes(leaf
, fi
);
4095 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4096 item_end
+= btrfs_file_extent_inline_len(leaf
,
4101 if (found_type
> min_type
) {
4104 if (item_end
< new_size
)
4106 if (found_key
.offset
>= new_size
)
4112 /* FIXME, shrink the extent if the ref count is only 1 */
4113 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4116 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4118 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4120 u64 orig_num_bytes
=
4121 btrfs_file_extent_num_bytes(leaf
, fi
);
4122 extent_num_bytes
= ALIGN(new_size
-
4125 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4127 num_dec
= (orig_num_bytes
-
4129 if (root
->ref_cows
&& extent_start
!= 0)
4130 inode_sub_bytes(inode
, num_dec
);
4131 btrfs_mark_buffer_dirty(leaf
);
4134 btrfs_file_extent_disk_num_bytes(leaf
,
4136 extent_offset
= found_key
.offset
-
4137 btrfs_file_extent_offset(leaf
, fi
);
4139 /* FIXME blocksize != 4096 */
4140 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4141 if (extent_start
!= 0) {
4144 inode_sub_bytes(inode
, num_dec
);
4147 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4149 * we can't truncate inline items that have had
4153 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4154 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4155 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4156 u32 size
= new_size
- found_key
.offset
;
4158 if (root
->ref_cows
) {
4159 inode_sub_bytes(inode
, item_end
+ 1 -
4163 btrfs_file_extent_calc_inline_size(size
);
4164 btrfs_truncate_item(trans
, root
, path
,
4166 } else if (root
->ref_cows
) {
4167 inode_sub_bytes(inode
, item_end
+ 1 -
4173 if (!pending_del_nr
) {
4174 /* no pending yet, add ourselves */
4175 pending_del_slot
= path
->slots
[0];
4177 } else if (pending_del_nr
&&
4178 path
->slots
[0] + 1 == pending_del_slot
) {
4179 /* hop on the pending chunk */
4181 pending_del_slot
= path
->slots
[0];
4188 if (found_extent
&& (root
->ref_cows
||
4189 root
== root
->fs_info
->tree_root
)) {
4190 btrfs_set_path_blocking(path
);
4191 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4192 extent_num_bytes
, 0,
4193 btrfs_header_owner(leaf
),
4194 ino
, extent_offset
, 0);
4198 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4201 if (path
->slots
[0] == 0 ||
4202 path
->slots
[0] != pending_del_slot
) {
4203 if (pending_del_nr
) {
4204 ret
= btrfs_del_items(trans
, root
, path
,
4208 btrfs_abort_transaction(trans
,
4214 btrfs_release_path(path
);
4221 if (pending_del_nr
) {
4222 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4225 btrfs_abort_transaction(trans
, root
, ret
);
4228 btrfs_free_path(path
);
4233 * btrfs_truncate_page - read, zero a chunk and write a page
4234 * @inode - inode that we're zeroing
4235 * @from - the offset to start zeroing
4236 * @len - the length to zero, 0 to zero the entire range respective to the
4238 * @front - zero up to the offset instead of from the offset on
4240 * This will find the page for the "from" offset and cow the page and zero the
4241 * part we want to zero. This is used with truncate and hole punching.
4243 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4246 struct address_space
*mapping
= inode
->i_mapping
;
4247 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4248 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4249 struct btrfs_ordered_extent
*ordered
;
4250 struct extent_state
*cached_state
= NULL
;
4252 u32 blocksize
= root
->sectorsize
;
4253 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4254 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4256 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4261 if ((offset
& (blocksize
- 1)) == 0 &&
4262 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4264 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4269 page
= find_or_create_page(mapping
, index
, mask
);
4271 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4276 page_start
= page_offset(page
);
4277 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4279 if (!PageUptodate(page
)) {
4280 ret
= btrfs_readpage(NULL
, page
);
4282 if (page
->mapping
!= mapping
) {
4284 page_cache_release(page
);
4287 if (!PageUptodate(page
)) {
4292 wait_on_page_writeback(page
);
4294 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4295 set_page_extent_mapped(page
);
4297 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4299 unlock_extent_cached(io_tree
, page_start
, page_end
,
4300 &cached_state
, GFP_NOFS
);
4302 page_cache_release(page
);
4303 btrfs_start_ordered_extent(inode
, ordered
, 1);
4304 btrfs_put_ordered_extent(ordered
);
4308 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4309 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4310 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4311 0, 0, &cached_state
, GFP_NOFS
);
4313 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4316 unlock_extent_cached(io_tree
, page_start
, page_end
,
4317 &cached_state
, GFP_NOFS
);
4321 if (offset
!= PAGE_CACHE_SIZE
) {
4323 len
= PAGE_CACHE_SIZE
- offset
;
4326 memset(kaddr
, 0, offset
);
4328 memset(kaddr
+ offset
, 0, len
);
4329 flush_dcache_page(page
);
4332 ClearPageChecked(page
);
4333 set_page_dirty(page
);
4334 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4339 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4341 page_cache_release(page
);
4347 * This function puts in dummy file extents for the area we're creating a hole
4348 * for. So if we are truncating this file to a larger size we need to insert
4349 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4350 * the range between oldsize and size
4352 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4354 struct btrfs_trans_handle
*trans
;
4355 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4356 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4357 struct extent_map
*em
= NULL
;
4358 struct extent_state
*cached_state
= NULL
;
4359 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4360 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4361 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4367 if (size
<= hole_start
)
4371 struct btrfs_ordered_extent
*ordered
;
4372 btrfs_wait_ordered_range(inode
, hole_start
,
4373 block_end
- hole_start
);
4374 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4376 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4379 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4380 &cached_state
, GFP_NOFS
);
4381 btrfs_put_ordered_extent(ordered
);
4384 cur_offset
= hole_start
;
4386 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4387 block_end
- cur_offset
, 0);
4393 last_byte
= min(extent_map_end(em
), block_end
);
4394 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4395 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4396 struct extent_map
*hole_em
;
4397 hole_size
= last_byte
- cur_offset
;
4399 trans
= btrfs_start_transaction(root
, 3);
4400 if (IS_ERR(trans
)) {
4401 err
= PTR_ERR(trans
);
4405 err
= btrfs_drop_extents(trans
, root
, inode
,
4407 cur_offset
+ hole_size
, 1);
4409 btrfs_abort_transaction(trans
, root
, err
);
4410 btrfs_end_transaction(trans
, root
);
4414 err
= btrfs_insert_file_extent(trans
, root
,
4415 btrfs_ino(inode
), cur_offset
, 0,
4416 0, hole_size
, 0, hole_size
,
4419 btrfs_abort_transaction(trans
, root
, err
);
4420 btrfs_end_transaction(trans
, root
);
4424 btrfs_drop_extent_cache(inode
, cur_offset
,
4425 cur_offset
+ hole_size
- 1, 0);
4426 hole_em
= alloc_extent_map();
4428 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4429 &BTRFS_I(inode
)->runtime_flags
);
4432 hole_em
->start
= cur_offset
;
4433 hole_em
->len
= hole_size
;
4434 hole_em
->orig_start
= cur_offset
;
4436 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4437 hole_em
->block_len
= 0;
4438 hole_em
->orig_block_len
= 0;
4439 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4440 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4441 hole_em
->generation
= trans
->transid
;
4444 write_lock(&em_tree
->lock
);
4445 err
= add_extent_mapping(em_tree
, hole_em
);
4447 list_move(&hole_em
->list
,
4448 &em_tree
->modified_extents
);
4449 write_unlock(&em_tree
->lock
);
4452 btrfs_drop_extent_cache(inode
, cur_offset
,
4456 free_extent_map(hole_em
);
4458 btrfs_update_inode(trans
, root
, inode
);
4459 btrfs_end_transaction(trans
, root
);
4461 free_extent_map(em
);
4463 cur_offset
= last_byte
;
4464 if (cur_offset
>= block_end
)
4468 free_extent_map(em
);
4469 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4474 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4476 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4477 struct btrfs_trans_handle
*trans
;
4478 loff_t oldsize
= i_size_read(inode
);
4479 loff_t newsize
= attr
->ia_size
;
4480 int mask
= attr
->ia_valid
;
4483 if (newsize
== oldsize
)
4487 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4488 * special case where we need to update the times despite not having
4489 * these flags set. For all other operations the VFS set these flags
4490 * explicitly if it wants a timestamp update.
4492 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4493 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4495 if (newsize
> oldsize
) {
4496 truncate_pagecache(inode
, oldsize
, newsize
);
4497 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4501 trans
= btrfs_start_transaction(root
, 1);
4503 return PTR_ERR(trans
);
4505 i_size_write(inode
, newsize
);
4506 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4507 ret
= btrfs_update_inode(trans
, root
, inode
);
4508 btrfs_end_transaction(trans
, root
);
4512 * We're truncating a file that used to have good data down to
4513 * zero. Make sure it gets into the ordered flush list so that
4514 * any new writes get down to disk quickly.
4517 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4518 &BTRFS_I(inode
)->runtime_flags
);
4521 * 1 for the orphan item we're going to add
4522 * 1 for the orphan item deletion.
4524 trans
= btrfs_start_transaction(root
, 2);
4526 return PTR_ERR(trans
);
4529 * We need to do this in case we fail at _any_ point during the
4530 * actual truncate. Once we do the truncate_setsize we could
4531 * invalidate pages which forces any outstanding ordered io to
4532 * be instantly completed which will give us extents that need
4533 * to be truncated. If we fail to get an orphan inode down we
4534 * could have left over extents that were never meant to live,
4535 * so we need to garuntee from this point on that everything
4536 * will be consistent.
4538 ret
= btrfs_orphan_add(trans
, inode
);
4539 btrfs_end_transaction(trans
, root
);
4543 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4544 truncate_setsize(inode
, newsize
);
4546 /* Disable nonlocked read DIO to avoid the end less truncate */
4547 btrfs_inode_block_unlocked_dio(inode
);
4548 inode_dio_wait(inode
);
4549 btrfs_inode_resume_unlocked_dio(inode
);
4551 ret
= btrfs_truncate(inode
);
4552 if (ret
&& inode
->i_nlink
)
4553 btrfs_orphan_del(NULL
, inode
);
4559 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4561 struct inode
*inode
= dentry
->d_inode
;
4562 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4565 if (btrfs_root_readonly(root
))
4568 err
= inode_change_ok(inode
, attr
);
4572 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4573 err
= btrfs_setsize(inode
, attr
);
4578 if (attr
->ia_valid
) {
4579 setattr_copy(inode
, attr
);
4580 inode_inc_iversion(inode
);
4581 err
= btrfs_dirty_inode(inode
);
4583 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4584 err
= btrfs_acl_chmod(inode
);
4590 void btrfs_evict_inode(struct inode
*inode
)
4592 struct btrfs_trans_handle
*trans
;
4593 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4594 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4595 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4598 trace_btrfs_inode_evict(inode
);
4600 truncate_inode_pages(&inode
->i_data
, 0);
4601 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4602 btrfs_is_free_space_inode(inode
)))
4605 if (is_bad_inode(inode
)) {
4606 btrfs_orphan_del(NULL
, inode
);
4609 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4610 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4612 if (root
->fs_info
->log_root_recovering
) {
4613 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4614 &BTRFS_I(inode
)->runtime_flags
));
4618 if (inode
->i_nlink
> 0) {
4619 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4623 ret
= btrfs_commit_inode_delayed_inode(inode
);
4625 btrfs_orphan_del(NULL
, inode
);
4629 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4631 btrfs_orphan_del(NULL
, inode
);
4634 rsv
->size
= min_size
;
4636 global_rsv
= &root
->fs_info
->global_block_rsv
;
4638 btrfs_i_size_write(inode
, 0);
4641 * This is a bit simpler than btrfs_truncate since we've already
4642 * reserved our space for our orphan item in the unlink, so we just
4643 * need to reserve some slack space in case we add bytes and update
4644 * inode item when doing the truncate.
4647 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4648 BTRFS_RESERVE_FLUSH_LIMIT
);
4651 * Try and steal from the global reserve since we will
4652 * likely not use this space anyway, we want to try as
4653 * hard as possible to get this to work.
4656 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4659 printk(KERN_WARNING
"Could not get space for a "
4660 "delete, will truncate on mount %d\n", ret
);
4661 btrfs_orphan_del(NULL
, inode
);
4662 btrfs_free_block_rsv(root
, rsv
);
4666 trans
= btrfs_join_transaction(root
);
4667 if (IS_ERR(trans
)) {
4668 btrfs_orphan_del(NULL
, inode
);
4669 btrfs_free_block_rsv(root
, rsv
);
4673 trans
->block_rsv
= rsv
;
4675 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4679 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4680 btrfs_end_transaction(trans
, root
);
4682 btrfs_btree_balance_dirty(root
);
4685 btrfs_free_block_rsv(root
, rsv
);
4688 trans
->block_rsv
= root
->orphan_block_rsv
;
4689 ret
= btrfs_orphan_del(trans
, inode
);
4693 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4694 if (!(root
== root
->fs_info
->tree_root
||
4695 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4696 btrfs_return_ino(root
, btrfs_ino(inode
));
4698 btrfs_end_transaction(trans
, root
);
4699 btrfs_btree_balance_dirty(root
);
4706 * this returns the key found in the dir entry in the location pointer.
4707 * If no dir entries were found, location->objectid is 0.
4709 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4710 struct btrfs_key
*location
)
4712 const char *name
= dentry
->d_name
.name
;
4713 int namelen
= dentry
->d_name
.len
;
4714 struct btrfs_dir_item
*di
;
4715 struct btrfs_path
*path
;
4716 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4719 path
= btrfs_alloc_path();
4723 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4728 if (IS_ERR_OR_NULL(di
))
4731 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4733 btrfs_free_path(path
);
4736 location
->objectid
= 0;
4741 * when we hit a tree root in a directory, the btrfs part of the inode
4742 * needs to be changed to reflect the root directory of the tree root. This
4743 * is kind of like crossing a mount point.
4745 static int fixup_tree_root_location(struct btrfs_root
*root
,
4747 struct dentry
*dentry
,
4748 struct btrfs_key
*location
,
4749 struct btrfs_root
**sub_root
)
4751 struct btrfs_path
*path
;
4752 struct btrfs_root
*new_root
;
4753 struct btrfs_root_ref
*ref
;
4754 struct extent_buffer
*leaf
;
4758 path
= btrfs_alloc_path();
4765 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4766 BTRFS_I(dir
)->root
->root_key
.objectid
,
4767 location
->objectid
);
4774 leaf
= path
->nodes
[0];
4775 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4776 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4777 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4780 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4781 (unsigned long)(ref
+ 1),
4782 dentry
->d_name
.len
);
4786 btrfs_release_path(path
);
4788 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4789 if (IS_ERR(new_root
)) {
4790 err
= PTR_ERR(new_root
);
4794 if (btrfs_root_refs(&new_root
->root_item
) == 0) {
4799 *sub_root
= new_root
;
4800 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4801 location
->type
= BTRFS_INODE_ITEM_KEY
;
4802 location
->offset
= 0;
4805 btrfs_free_path(path
);
4809 static void inode_tree_add(struct inode
*inode
)
4811 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4812 struct btrfs_inode
*entry
;
4814 struct rb_node
*parent
;
4815 u64 ino
= btrfs_ino(inode
);
4817 p
= &root
->inode_tree
.rb_node
;
4820 if (inode_unhashed(inode
))
4823 spin_lock(&root
->inode_lock
);
4826 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4828 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4829 p
= &parent
->rb_left
;
4830 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4831 p
= &parent
->rb_right
;
4833 WARN_ON(!(entry
->vfs_inode
.i_state
&
4834 (I_WILL_FREE
| I_FREEING
)));
4835 rb_erase(parent
, &root
->inode_tree
);
4836 RB_CLEAR_NODE(parent
);
4837 spin_unlock(&root
->inode_lock
);
4841 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4842 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4843 spin_unlock(&root
->inode_lock
);
4846 static void inode_tree_del(struct inode
*inode
)
4848 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4851 spin_lock(&root
->inode_lock
);
4852 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4853 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4854 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4855 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4857 spin_unlock(&root
->inode_lock
);
4860 * Free space cache has inodes in the tree root, but the tree root has a
4861 * root_refs of 0, so this could end up dropping the tree root as a
4862 * snapshot, so we need the extra !root->fs_info->tree_root check to
4863 * make sure we don't drop it.
4865 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4866 root
!= root
->fs_info
->tree_root
) {
4867 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4868 spin_lock(&root
->inode_lock
);
4869 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4870 spin_unlock(&root
->inode_lock
);
4872 btrfs_add_dead_root(root
);
4876 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4878 struct rb_node
*node
;
4879 struct rb_node
*prev
;
4880 struct btrfs_inode
*entry
;
4881 struct inode
*inode
;
4884 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4886 spin_lock(&root
->inode_lock
);
4888 node
= root
->inode_tree
.rb_node
;
4892 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4894 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4895 node
= node
->rb_left
;
4896 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4897 node
= node
->rb_right
;
4903 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4904 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4908 prev
= rb_next(prev
);
4912 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4913 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4914 inode
= igrab(&entry
->vfs_inode
);
4916 spin_unlock(&root
->inode_lock
);
4917 if (atomic_read(&inode
->i_count
) > 1)
4918 d_prune_aliases(inode
);
4920 * btrfs_drop_inode will have it removed from
4921 * the inode cache when its usage count
4926 spin_lock(&root
->inode_lock
);
4930 if (cond_resched_lock(&root
->inode_lock
))
4933 node
= rb_next(node
);
4935 spin_unlock(&root
->inode_lock
);
4938 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4940 struct btrfs_iget_args
*args
= p
;
4941 inode
->i_ino
= args
->ino
;
4942 BTRFS_I(inode
)->root
= args
->root
;
4946 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4948 struct btrfs_iget_args
*args
= opaque
;
4949 return args
->ino
== btrfs_ino(inode
) &&
4950 args
->root
== BTRFS_I(inode
)->root
;
4953 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4955 struct btrfs_root
*root
)
4957 struct inode
*inode
;
4958 struct btrfs_iget_args args
;
4959 args
.ino
= objectid
;
4962 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4963 btrfs_init_locked_inode
,
4968 /* Get an inode object given its location and corresponding root.
4969 * Returns in *is_new if the inode was read from disk
4971 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4972 struct btrfs_root
*root
, int *new)
4974 struct inode
*inode
;
4976 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4978 return ERR_PTR(-ENOMEM
);
4980 if (inode
->i_state
& I_NEW
) {
4981 BTRFS_I(inode
)->root
= root
;
4982 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4983 btrfs_read_locked_inode(inode
);
4984 if (!is_bad_inode(inode
)) {
4985 inode_tree_add(inode
);
4986 unlock_new_inode(inode
);
4990 unlock_new_inode(inode
);
4992 inode
= ERR_PTR(-ESTALE
);
4999 static struct inode
*new_simple_dir(struct super_block
*s
,
5000 struct btrfs_key
*key
,
5001 struct btrfs_root
*root
)
5003 struct inode
*inode
= new_inode(s
);
5006 return ERR_PTR(-ENOMEM
);
5008 BTRFS_I(inode
)->root
= root
;
5009 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5010 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5012 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5013 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5014 inode
->i_fop
= &simple_dir_operations
;
5015 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5016 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5021 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5023 struct inode
*inode
;
5024 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5025 struct btrfs_root
*sub_root
= root
;
5026 struct btrfs_key location
;
5030 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5031 return ERR_PTR(-ENAMETOOLONG
);
5033 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5035 return ERR_PTR(ret
);
5037 if (location
.objectid
== 0)
5040 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5041 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5045 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5047 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5048 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5049 &location
, &sub_root
);
5052 inode
= ERR_PTR(ret
);
5054 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5056 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5058 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5060 if (!IS_ERR(inode
) && root
!= sub_root
) {
5061 down_read(&root
->fs_info
->cleanup_work_sem
);
5062 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5063 ret
= btrfs_orphan_cleanup(sub_root
);
5064 up_read(&root
->fs_info
->cleanup_work_sem
);
5066 inode
= ERR_PTR(ret
);
5072 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5074 struct btrfs_root
*root
;
5075 struct inode
*inode
= dentry
->d_inode
;
5077 if (!inode
&& !IS_ROOT(dentry
))
5078 inode
= dentry
->d_parent
->d_inode
;
5081 root
= BTRFS_I(inode
)->root
;
5082 if (btrfs_root_refs(&root
->root_item
) == 0)
5085 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5091 static void btrfs_dentry_release(struct dentry
*dentry
)
5093 if (dentry
->d_fsdata
)
5094 kfree(dentry
->d_fsdata
);
5097 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5102 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5106 unsigned char btrfs_filetype_table
[] = {
5107 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5110 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
5113 struct inode
*inode
= file_inode(filp
);
5114 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5115 struct btrfs_item
*item
;
5116 struct btrfs_dir_item
*di
;
5117 struct btrfs_key key
;
5118 struct btrfs_key found_key
;
5119 struct btrfs_path
*path
;
5120 struct list_head ins_list
;
5121 struct list_head del_list
;
5123 struct extent_buffer
*leaf
;
5125 unsigned char d_type
;
5130 int key_type
= BTRFS_DIR_INDEX_KEY
;
5134 int is_curr
= 0; /* filp->f_pos points to the current index? */
5136 /* FIXME, use a real flag for deciding about the key type */
5137 if (root
->fs_info
->tree_root
== root
)
5138 key_type
= BTRFS_DIR_ITEM_KEY
;
5140 /* special case for "." */
5141 if (filp
->f_pos
== 0) {
5142 over
= filldir(dirent
, ".", 1,
5143 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5148 /* special case for .., just use the back ref */
5149 if (filp
->f_pos
== 1) {
5150 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5151 over
= filldir(dirent
, "..", 2,
5152 filp
->f_pos
, pino
, DT_DIR
);
5157 path
= btrfs_alloc_path();
5163 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5164 INIT_LIST_HEAD(&ins_list
);
5165 INIT_LIST_HEAD(&del_list
);
5166 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5169 btrfs_set_key_type(&key
, key_type
);
5170 key
.offset
= filp
->f_pos
;
5171 key
.objectid
= btrfs_ino(inode
);
5173 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5178 leaf
= path
->nodes
[0];
5179 slot
= path
->slots
[0];
5180 if (slot
>= btrfs_header_nritems(leaf
)) {
5181 ret
= btrfs_next_leaf(root
, path
);
5189 item
= btrfs_item_nr(leaf
, slot
);
5190 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5192 if (found_key
.objectid
!= key
.objectid
)
5194 if (btrfs_key_type(&found_key
) != key_type
)
5196 if (found_key
.offset
< filp
->f_pos
)
5198 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5199 btrfs_should_delete_dir_index(&del_list
,
5203 filp
->f_pos
= found_key
.offset
;
5206 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5208 di_total
= btrfs_item_size(leaf
, item
);
5210 while (di_cur
< di_total
) {
5211 struct btrfs_key location
;
5213 if (verify_dir_item(root
, leaf
, di
))
5216 name_len
= btrfs_dir_name_len(leaf
, di
);
5217 if (name_len
<= sizeof(tmp_name
)) {
5218 name_ptr
= tmp_name
;
5220 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5226 read_extent_buffer(leaf
, name_ptr
,
5227 (unsigned long)(di
+ 1), name_len
);
5229 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5230 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5233 /* is this a reference to our own snapshot? If so
5236 * In contrast to old kernels, we insert the snapshot's
5237 * dir item and dir index after it has been created, so
5238 * we won't find a reference to our own snapshot. We
5239 * still keep the following code for backward
5242 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5243 location
.objectid
== root
->root_key
.objectid
) {
5247 over
= filldir(dirent
, name_ptr
, name_len
,
5248 found_key
.offset
, location
.objectid
,
5252 if (name_ptr
!= tmp_name
)
5257 di_len
= btrfs_dir_name_len(leaf
, di
) +
5258 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5260 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5266 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5269 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5275 /* Reached end of directory/root. Bump pos past the last item. */
5276 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5278 * 32-bit glibc will use getdents64, but then strtol -
5279 * so the last number we can serve is this.
5281 filp
->f_pos
= 0x7fffffff;
5287 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5288 btrfs_put_delayed_items(&ins_list
, &del_list
);
5289 btrfs_free_path(path
);
5293 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5296 struct btrfs_trans_handle
*trans
;
5298 bool nolock
= false;
5300 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5303 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5306 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5308 trans
= btrfs_join_transaction_nolock(root
);
5310 trans
= btrfs_join_transaction(root
);
5312 return PTR_ERR(trans
);
5313 ret
= btrfs_commit_transaction(trans
, root
);
5319 * This is somewhat expensive, updating the tree every time the
5320 * inode changes. But, it is most likely to find the inode in cache.
5321 * FIXME, needs more benchmarking...there are no reasons other than performance
5322 * to keep or drop this code.
5324 int btrfs_dirty_inode(struct inode
*inode
)
5326 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5327 struct btrfs_trans_handle
*trans
;
5330 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5333 trans
= btrfs_join_transaction(root
);
5335 return PTR_ERR(trans
);
5337 ret
= btrfs_update_inode(trans
, root
, inode
);
5338 if (ret
&& ret
== -ENOSPC
) {
5339 /* whoops, lets try again with the full transaction */
5340 btrfs_end_transaction(trans
, root
);
5341 trans
= btrfs_start_transaction(root
, 1);
5343 return PTR_ERR(trans
);
5345 ret
= btrfs_update_inode(trans
, root
, inode
);
5347 btrfs_end_transaction(trans
, root
);
5348 if (BTRFS_I(inode
)->delayed_node
)
5349 btrfs_balance_delayed_items(root
);
5355 * This is a copy of file_update_time. We need this so we can return error on
5356 * ENOSPC for updating the inode in the case of file write and mmap writes.
5358 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5361 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5363 if (btrfs_root_readonly(root
))
5366 if (flags
& S_VERSION
)
5367 inode_inc_iversion(inode
);
5368 if (flags
& S_CTIME
)
5369 inode
->i_ctime
= *now
;
5370 if (flags
& S_MTIME
)
5371 inode
->i_mtime
= *now
;
5372 if (flags
& S_ATIME
)
5373 inode
->i_atime
= *now
;
5374 return btrfs_dirty_inode(inode
);
5378 * find the highest existing sequence number in a directory
5379 * and then set the in-memory index_cnt variable to reflect
5380 * free sequence numbers
5382 static int btrfs_set_inode_index_count(struct inode
*inode
)
5384 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5385 struct btrfs_key key
, found_key
;
5386 struct btrfs_path
*path
;
5387 struct extent_buffer
*leaf
;
5390 key
.objectid
= btrfs_ino(inode
);
5391 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5392 key
.offset
= (u64
)-1;
5394 path
= btrfs_alloc_path();
5398 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5401 /* FIXME: we should be able to handle this */
5407 * MAGIC NUMBER EXPLANATION:
5408 * since we search a directory based on f_pos we have to start at 2
5409 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5410 * else has to start at 2
5412 if (path
->slots
[0] == 0) {
5413 BTRFS_I(inode
)->index_cnt
= 2;
5419 leaf
= path
->nodes
[0];
5420 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5422 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5423 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5424 BTRFS_I(inode
)->index_cnt
= 2;
5428 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5430 btrfs_free_path(path
);
5435 * helper to find a free sequence number in a given directory. This current
5436 * code is very simple, later versions will do smarter things in the btree
5438 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5442 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5443 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5445 ret
= btrfs_set_inode_index_count(dir
);
5451 *index
= BTRFS_I(dir
)->index_cnt
;
5452 BTRFS_I(dir
)->index_cnt
++;
5457 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5458 struct btrfs_root
*root
,
5460 const char *name
, int name_len
,
5461 u64 ref_objectid
, u64 objectid
,
5462 umode_t mode
, u64
*index
)
5464 struct inode
*inode
;
5465 struct btrfs_inode_item
*inode_item
;
5466 struct btrfs_key
*location
;
5467 struct btrfs_path
*path
;
5468 struct btrfs_inode_ref
*ref
;
5469 struct btrfs_key key
[2];
5475 path
= btrfs_alloc_path();
5477 return ERR_PTR(-ENOMEM
);
5479 inode
= new_inode(root
->fs_info
->sb
);
5481 btrfs_free_path(path
);
5482 return ERR_PTR(-ENOMEM
);
5486 * we have to initialize this early, so we can reclaim the inode
5487 * number if we fail afterwards in this function.
5489 inode
->i_ino
= objectid
;
5492 trace_btrfs_inode_request(dir
);
5494 ret
= btrfs_set_inode_index(dir
, index
);
5496 btrfs_free_path(path
);
5498 return ERR_PTR(ret
);
5502 * index_cnt is ignored for everything but a dir,
5503 * btrfs_get_inode_index_count has an explanation for the magic
5506 BTRFS_I(inode
)->index_cnt
= 2;
5507 BTRFS_I(inode
)->root
= root
;
5508 BTRFS_I(inode
)->generation
= trans
->transid
;
5509 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5512 * We could have gotten an inode number from somebody who was fsynced
5513 * and then removed in this same transaction, so let's just set full
5514 * sync since it will be a full sync anyway and this will blow away the
5515 * old info in the log.
5517 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5524 key
[0].objectid
= objectid
;
5525 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5529 * Start new inodes with an inode_ref. This is slightly more
5530 * efficient for small numbers of hard links since they will
5531 * be packed into one item. Extended refs will kick in if we
5532 * add more hard links than can fit in the ref item.
5534 key
[1].objectid
= objectid
;
5535 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5536 key
[1].offset
= ref_objectid
;
5538 sizes
[0] = sizeof(struct btrfs_inode_item
);
5539 sizes
[1] = name_len
+ sizeof(*ref
);
5541 path
->leave_spinning
= 1;
5542 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5546 inode_init_owner(inode
, dir
, mode
);
5547 inode_set_bytes(inode
, 0);
5548 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5549 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5550 struct btrfs_inode_item
);
5551 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5552 sizeof(*inode_item
));
5553 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5555 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5556 struct btrfs_inode_ref
);
5557 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5558 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5559 ptr
= (unsigned long)(ref
+ 1);
5560 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5562 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5563 btrfs_free_path(path
);
5565 location
= &BTRFS_I(inode
)->location
;
5566 location
->objectid
= objectid
;
5567 location
->offset
= 0;
5568 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5570 btrfs_inherit_iflags(inode
, dir
);
5572 if (S_ISREG(mode
)) {
5573 if (btrfs_test_opt(root
, NODATASUM
))
5574 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5575 if (btrfs_test_opt(root
, NODATACOW
))
5576 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5577 BTRFS_INODE_NODATASUM
;
5580 insert_inode_hash(inode
);
5581 inode_tree_add(inode
);
5583 trace_btrfs_inode_new(inode
);
5584 btrfs_set_inode_last_trans(trans
, inode
);
5586 btrfs_update_root_times(trans
, root
);
5591 BTRFS_I(dir
)->index_cnt
--;
5592 btrfs_free_path(path
);
5594 return ERR_PTR(ret
);
5597 static inline u8
btrfs_inode_type(struct inode
*inode
)
5599 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5603 * utility function to add 'inode' into 'parent_inode' with
5604 * a give name and a given sequence number.
5605 * if 'add_backref' is true, also insert a backref from the
5606 * inode to the parent directory.
5608 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5609 struct inode
*parent_inode
, struct inode
*inode
,
5610 const char *name
, int name_len
, int add_backref
, u64 index
)
5613 struct btrfs_key key
;
5614 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5615 u64 ino
= btrfs_ino(inode
);
5616 u64 parent_ino
= btrfs_ino(parent_inode
);
5618 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5619 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5622 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5626 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5627 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5628 key
.objectid
, root
->root_key
.objectid
,
5629 parent_ino
, index
, name
, name_len
);
5630 } else if (add_backref
) {
5631 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5635 /* Nothing to clean up yet */
5639 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5641 btrfs_inode_type(inode
), index
);
5642 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5645 btrfs_abort_transaction(trans
, root
, ret
);
5649 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5651 inode_inc_iversion(parent_inode
);
5652 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5653 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5655 btrfs_abort_transaction(trans
, root
, ret
);
5659 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5662 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5663 key
.objectid
, root
->root_key
.objectid
,
5664 parent_ino
, &local_index
, name
, name_len
);
5666 } else if (add_backref
) {
5670 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5671 ino
, parent_ino
, &local_index
);
5676 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5677 struct inode
*dir
, struct dentry
*dentry
,
5678 struct inode
*inode
, int backref
, u64 index
)
5680 int err
= btrfs_add_link(trans
, dir
, inode
,
5681 dentry
->d_name
.name
, dentry
->d_name
.len
,
5688 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5689 umode_t mode
, dev_t rdev
)
5691 struct btrfs_trans_handle
*trans
;
5692 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5693 struct inode
*inode
= NULL
;
5699 if (!new_valid_dev(rdev
))
5703 * 2 for inode item and ref
5705 * 1 for xattr if selinux is on
5707 trans
= btrfs_start_transaction(root
, 5);
5709 return PTR_ERR(trans
);
5711 err
= btrfs_find_free_ino(root
, &objectid
);
5715 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5716 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5718 if (IS_ERR(inode
)) {
5719 err
= PTR_ERR(inode
);
5723 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5730 * If the active LSM wants to access the inode during
5731 * d_instantiate it needs these. Smack checks to see
5732 * if the filesystem supports xattrs by looking at the
5736 inode
->i_op
= &btrfs_special_inode_operations
;
5737 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5741 init_special_inode(inode
, inode
->i_mode
, rdev
);
5742 btrfs_update_inode(trans
, root
, inode
);
5743 d_instantiate(dentry
, inode
);
5746 btrfs_end_transaction(trans
, root
);
5747 btrfs_btree_balance_dirty(root
);
5749 inode_dec_link_count(inode
);
5755 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5756 umode_t mode
, bool excl
)
5758 struct btrfs_trans_handle
*trans
;
5759 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5760 struct inode
*inode
= NULL
;
5761 int drop_inode_on_err
= 0;
5767 * 2 for inode item and ref
5769 * 1 for xattr if selinux is on
5771 trans
= btrfs_start_transaction(root
, 5);
5773 return PTR_ERR(trans
);
5775 err
= btrfs_find_free_ino(root
, &objectid
);
5779 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5780 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5782 if (IS_ERR(inode
)) {
5783 err
= PTR_ERR(inode
);
5786 drop_inode_on_err
= 1;
5788 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5792 err
= btrfs_update_inode(trans
, root
, inode
);
5797 * If the active LSM wants to access the inode during
5798 * d_instantiate it needs these. Smack checks to see
5799 * if the filesystem supports xattrs by looking at the
5802 inode
->i_fop
= &btrfs_file_operations
;
5803 inode
->i_op
= &btrfs_file_inode_operations
;
5805 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5809 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5810 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5811 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5812 d_instantiate(dentry
, inode
);
5815 btrfs_end_transaction(trans
, root
);
5816 if (err
&& drop_inode_on_err
) {
5817 inode_dec_link_count(inode
);
5820 btrfs_btree_balance_dirty(root
);
5824 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5825 struct dentry
*dentry
)
5827 struct btrfs_trans_handle
*trans
;
5828 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5829 struct inode
*inode
= old_dentry
->d_inode
;
5834 /* do not allow sys_link's with other subvols of the same device */
5835 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5838 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5841 err
= btrfs_set_inode_index(dir
, &index
);
5846 * 2 items for inode and inode ref
5847 * 2 items for dir items
5848 * 1 item for parent inode
5850 trans
= btrfs_start_transaction(root
, 5);
5851 if (IS_ERR(trans
)) {
5852 err
= PTR_ERR(trans
);
5856 btrfs_inc_nlink(inode
);
5857 inode_inc_iversion(inode
);
5858 inode
->i_ctime
= CURRENT_TIME
;
5860 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5862 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5867 struct dentry
*parent
= dentry
->d_parent
;
5868 err
= btrfs_update_inode(trans
, root
, inode
);
5871 d_instantiate(dentry
, inode
);
5872 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5875 btrfs_end_transaction(trans
, root
);
5878 inode_dec_link_count(inode
);
5881 btrfs_btree_balance_dirty(root
);
5885 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5887 struct inode
*inode
= NULL
;
5888 struct btrfs_trans_handle
*trans
;
5889 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5891 int drop_on_err
= 0;
5896 * 2 items for inode and ref
5897 * 2 items for dir items
5898 * 1 for xattr if selinux is on
5900 trans
= btrfs_start_transaction(root
, 5);
5902 return PTR_ERR(trans
);
5904 err
= btrfs_find_free_ino(root
, &objectid
);
5908 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5909 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5910 S_IFDIR
| mode
, &index
);
5911 if (IS_ERR(inode
)) {
5912 err
= PTR_ERR(inode
);
5918 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5922 inode
->i_op
= &btrfs_dir_inode_operations
;
5923 inode
->i_fop
= &btrfs_dir_file_operations
;
5925 btrfs_i_size_write(inode
, 0);
5926 err
= btrfs_update_inode(trans
, root
, inode
);
5930 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5931 dentry
->d_name
.len
, 0, index
);
5935 d_instantiate(dentry
, inode
);
5939 btrfs_end_transaction(trans
, root
);
5942 btrfs_btree_balance_dirty(root
);
5946 /* helper for btfs_get_extent. Given an existing extent in the tree,
5947 * and an extent that you want to insert, deal with overlap and insert
5948 * the new extent into the tree.
5950 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5951 struct extent_map
*existing
,
5952 struct extent_map
*em
,
5953 u64 map_start
, u64 map_len
)
5957 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5958 start_diff
= map_start
- em
->start
;
5959 em
->start
= map_start
;
5961 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5962 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5963 em
->block_start
+= start_diff
;
5964 em
->block_len
-= start_diff
;
5966 return add_extent_mapping(em_tree
, em
);
5969 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5970 struct inode
*inode
, struct page
*page
,
5971 size_t pg_offset
, u64 extent_offset
,
5972 struct btrfs_file_extent_item
*item
)
5975 struct extent_buffer
*leaf
= path
->nodes
[0];
5978 unsigned long inline_size
;
5982 WARN_ON(pg_offset
!= 0);
5983 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5984 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5985 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5986 btrfs_item_nr(leaf
, path
->slots
[0]));
5987 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5990 ptr
= btrfs_file_extent_inline_start(item
);
5992 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5994 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5995 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5996 extent_offset
, inline_size
, max_size
);
5998 char *kaddr
= kmap_atomic(page
);
5999 unsigned long copy_size
= min_t(u64
,
6000 PAGE_CACHE_SIZE
- pg_offset
,
6001 max_size
- extent_offset
);
6002 memset(kaddr
+ pg_offset
, 0, copy_size
);
6003 kunmap_atomic(kaddr
);
6010 * a bit scary, this does extent mapping from logical file offset to the disk.
6011 * the ugly parts come from merging extents from the disk with the in-ram
6012 * representation. This gets more complex because of the data=ordered code,
6013 * where the in-ram extents might be locked pending data=ordered completion.
6015 * This also copies inline extents directly into the page.
6018 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6019 size_t pg_offset
, u64 start
, u64 len
,
6025 u64 extent_start
= 0;
6027 u64 objectid
= btrfs_ino(inode
);
6029 struct btrfs_path
*path
= NULL
;
6030 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6031 struct btrfs_file_extent_item
*item
;
6032 struct extent_buffer
*leaf
;
6033 struct btrfs_key found_key
;
6034 struct extent_map
*em
= NULL
;
6035 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6036 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6037 struct btrfs_trans_handle
*trans
= NULL
;
6041 read_lock(&em_tree
->lock
);
6042 em
= lookup_extent_mapping(em_tree
, start
, len
);
6044 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6045 read_unlock(&em_tree
->lock
);
6048 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6049 free_extent_map(em
);
6050 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6051 free_extent_map(em
);
6055 em
= alloc_extent_map();
6060 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6061 em
->start
= EXTENT_MAP_HOLE
;
6062 em
->orig_start
= EXTENT_MAP_HOLE
;
6064 em
->block_len
= (u64
)-1;
6067 path
= btrfs_alloc_path();
6073 * Chances are we'll be called again, so go ahead and do
6079 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6080 objectid
, start
, trans
!= NULL
);
6087 if (path
->slots
[0] == 0)
6092 leaf
= path
->nodes
[0];
6093 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6094 struct btrfs_file_extent_item
);
6095 /* are we inside the extent that was found? */
6096 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6097 found_type
= btrfs_key_type(&found_key
);
6098 if (found_key
.objectid
!= objectid
||
6099 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6103 found_type
= btrfs_file_extent_type(leaf
, item
);
6104 extent_start
= found_key
.offset
;
6105 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6106 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6107 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6108 extent_end
= extent_start
+
6109 btrfs_file_extent_num_bytes(leaf
, item
);
6110 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6112 size
= btrfs_file_extent_inline_len(leaf
, item
);
6113 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6116 if (start
>= extent_end
) {
6118 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6119 ret
= btrfs_next_leaf(root
, path
);
6126 leaf
= path
->nodes
[0];
6128 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6129 if (found_key
.objectid
!= objectid
||
6130 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6132 if (start
+ len
<= found_key
.offset
)
6135 em
->orig_start
= start
;
6136 em
->len
= found_key
.offset
- start
;
6140 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6141 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6142 em
->start
= extent_start
;
6143 em
->len
= extent_end
- extent_start
;
6144 em
->orig_start
= extent_start
-
6145 btrfs_file_extent_offset(leaf
, item
);
6146 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6148 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6150 em
->block_start
= EXTENT_MAP_HOLE
;
6153 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6154 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6155 em
->compress_type
= compress_type
;
6156 em
->block_start
= bytenr
;
6157 em
->block_len
= em
->orig_block_len
;
6159 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6160 em
->block_start
= bytenr
;
6161 em
->block_len
= em
->len
;
6162 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6163 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6166 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6170 size_t extent_offset
;
6173 em
->block_start
= EXTENT_MAP_INLINE
;
6174 if (!page
|| create
) {
6175 em
->start
= extent_start
;
6176 em
->len
= extent_end
- extent_start
;
6180 size
= btrfs_file_extent_inline_len(leaf
, item
);
6181 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6182 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6183 size
- extent_offset
);
6184 em
->start
= extent_start
+ extent_offset
;
6185 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6186 em
->orig_block_len
= em
->len
;
6187 em
->orig_start
= em
->start
;
6188 if (compress_type
) {
6189 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6190 em
->compress_type
= compress_type
;
6192 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6193 if (create
== 0 && !PageUptodate(page
)) {
6194 if (btrfs_file_extent_compression(leaf
, item
) !=
6195 BTRFS_COMPRESS_NONE
) {
6196 ret
= uncompress_inline(path
, inode
, page
,
6198 extent_offset
, item
);
6199 BUG_ON(ret
); /* -ENOMEM */
6202 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6204 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6205 memset(map
+ pg_offset
+ copy_size
, 0,
6206 PAGE_CACHE_SIZE
- pg_offset
-
6211 flush_dcache_page(page
);
6212 } else if (create
&& PageUptodate(page
)) {
6216 free_extent_map(em
);
6219 btrfs_release_path(path
);
6220 trans
= btrfs_join_transaction(root
);
6223 return ERR_CAST(trans
);
6227 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6230 btrfs_mark_buffer_dirty(leaf
);
6232 set_extent_uptodate(io_tree
, em
->start
,
6233 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6236 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6240 em
->orig_start
= start
;
6243 em
->block_start
= EXTENT_MAP_HOLE
;
6244 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6246 btrfs_release_path(path
);
6247 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6248 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
6249 "[%llu %llu]\n", (unsigned long long)em
->start
,
6250 (unsigned long long)em
->len
,
6251 (unsigned long long)start
,
6252 (unsigned long long)len
);
6258 write_lock(&em_tree
->lock
);
6259 ret
= add_extent_mapping(em_tree
, em
);
6260 /* it is possible that someone inserted the extent into the tree
6261 * while we had the lock dropped. It is also possible that
6262 * an overlapping map exists in the tree
6264 if (ret
== -EEXIST
) {
6265 struct extent_map
*existing
;
6269 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6270 if (existing
&& (existing
->start
> start
||
6271 existing
->start
+ existing
->len
<= start
)) {
6272 free_extent_map(existing
);
6276 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6279 err
= merge_extent_mapping(em_tree
, existing
,
6282 free_extent_map(existing
);
6284 free_extent_map(em
);
6289 free_extent_map(em
);
6293 free_extent_map(em
);
6298 write_unlock(&em_tree
->lock
);
6302 trace_btrfs_get_extent(root
, em
);
6305 btrfs_free_path(path
);
6307 ret
= btrfs_end_transaction(trans
, root
);
6312 free_extent_map(em
);
6313 return ERR_PTR(err
);
6315 BUG_ON(!em
); /* Error is always set */
6319 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6320 size_t pg_offset
, u64 start
, u64 len
,
6323 struct extent_map
*em
;
6324 struct extent_map
*hole_em
= NULL
;
6325 u64 range_start
= start
;
6331 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6338 * - a pre-alloc extent,
6339 * there might actually be delalloc bytes behind it.
6341 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6342 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6348 /* check to see if we've wrapped (len == -1 or similar) */
6357 /* ok, we didn't find anything, lets look for delalloc */
6358 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6359 end
, len
, EXTENT_DELALLOC
, 1);
6360 found_end
= range_start
+ found
;
6361 if (found_end
< range_start
)
6362 found_end
= (u64
)-1;
6365 * we didn't find anything useful, return
6366 * the original results from get_extent()
6368 if (range_start
> end
|| found_end
<= start
) {
6374 /* adjust the range_start to make sure it doesn't
6375 * go backwards from the start they passed in
6377 range_start
= max(start
,range_start
);
6378 found
= found_end
- range_start
;
6381 u64 hole_start
= start
;
6384 em
= alloc_extent_map();
6390 * when btrfs_get_extent can't find anything it
6391 * returns one huge hole
6393 * make sure what it found really fits our range, and
6394 * adjust to make sure it is based on the start from
6398 u64 calc_end
= extent_map_end(hole_em
);
6400 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6401 free_extent_map(hole_em
);
6404 hole_start
= max(hole_em
->start
, start
);
6405 hole_len
= calc_end
- hole_start
;
6409 if (hole_em
&& range_start
> hole_start
) {
6410 /* our hole starts before our delalloc, so we
6411 * have to return just the parts of the hole
6412 * that go until the delalloc starts
6414 em
->len
= min(hole_len
,
6415 range_start
- hole_start
);
6416 em
->start
= hole_start
;
6417 em
->orig_start
= hole_start
;
6419 * don't adjust block start at all,
6420 * it is fixed at EXTENT_MAP_HOLE
6422 em
->block_start
= hole_em
->block_start
;
6423 em
->block_len
= hole_len
;
6424 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6425 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6427 em
->start
= range_start
;
6429 em
->orig_start
= range_start
;
6430 em
->block_start
= EXTENT_MAP_DELALLOC
;
6431 em
->block_len
= found
;
6433 } else if (hole_em
) {
6438 free_extent_map(hole_em
);
6440 free_extent_map(em
);
6441 return ERR_PTR(err
);
6446 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6449 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6450 struct btrfs_trans_handle
*trans
;
6451 struct extent_map
*em
;
6452 struct btrfs_key ins
;
6456 trans
= btrfs_join_transaction(root
);
6458 return ERR_CAST(trans
);
6460 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6462 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6463 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6464 alloc_hint
, &ins
, 1);
6470 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6471 ins
.offset
, ins
.offset
, 0);
6475 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6476 ins
.offset
, ins
.offset
, 0);
6478 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6482 btrfs_end_transaction(trans
, root
);
6487 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6488 * block must be cow'd
6490 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6491 struct inode
*inode
, u64 offset
, u64 len
)
6493 struct btrfs_path
*path
;
6495 struct extent_buffer
*leaf
;
6496 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6497 struct btrfs_file_extent_item
*fi
;
6498 struct btrfs_key key
;
6506 path
= btrfs_alloc_path();
6510 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6515 slot
= path
->slots
[0];
6518 /* can't find the item, must cow */
6525 leaf
= path
->nodes
[0];
6526 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6527 if (key
.objectid
!= btrfs_ino(inode
) ||
6528 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6529 /* not our file or wrong item type, must cow */
6533 if (key
.offset
> offset
) {
6534 /* Wrong offset, must cow */
6538 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6539 found_type
= btrfs_file_extent_type(leaf
, fi
);
6540 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6541 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6542 /* not a regular extent, must cow */
6545 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6546 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6548 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6549 if (extent_end
< offset
+ len
) {
6550 /* extent doesn't include our full range, must cow */
6554 if (btrfs_extent_readonly(root
, disk_bytenr
))
6558 * look for other files referencing this extent, if we
6559 * find any we must cow
6561 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6562 key
.offset
- backref_offset
, disk_bytenr
))
6566 * adjust disk_bytenr and num_bytes to cover just the bytes
6567 * in this extent we are about to write. If there
6568 * are any csums in that range we have to cow in order
6569 * to keep the csums correct
6571 disk_bytenr
+= backref_offset
;
6572 disk_bytenr
+= offset
- key
.offset
;
6573 num_bytes
= min(offset
+ len
, extent_end
) - offset
;
6574 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6577 * all of the above have passed, it is safe to overwrite this extent
6582 btrfs_free_path(path
);
6586 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6587 struct extent_state
**cached_state
, int writing
)
6589 struct btrfs_ordered_extent
*ordered
;
6593 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6596 * We're concerned with the entire range that we're going to be
6597 * doing DIO to, so we need to make sure theres no ordered
6598 * extents in this range.
6600 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6601 lockend
- lockstart
+ 1);
6604 * We need to make sure there are no buffered pages in this
6605 * range either, we could have raced between the invalidate in
6606 * generic_file_direct_write and locking the extent. The
6607 * invalidate needs to happen so that reads after a write do not
6610 if (!ordered
&& (!writing
||
6611 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6612 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6616 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6617 cached_state
, GFP_NOFS
);
6620 btrfs_start_ordered_extent(inode
, ordered
, 1);
6621 btrfs_put_ordered_extent(ordered
);
6623 /* Screw you mmap */
6624 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6631 * If we found a page that couldn't be invalidated just
6632 * fall back to buffered.
6634 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6635 lockstart
>> PAGE_CACHE_SHIFT
,
6636 lockend
>> PAGE_CACHE_SHIFT
);
6647 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6648 u64 len
, u64 orig_start
,
6649 u64 block_start
, u64 block_len
,
6650 u64 orig_block_len
, int type
)
6652 struct extent_map_tree
*em_tree
;
6653 struct extent_map
*em
;
6654 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6657 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6658 em
= alloc_extent_map();
6660 return ERR_PTR(-ENOMEM
);
6663 em
->orig_start
= orig_start
;
6664 em
->mod_start
= start
;
6667 em
->block_len
= block_len
;
6668 em
->block_start
= block_start
;
6669 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6670 em
->orig_block_len
= orig_block_len
;
6671 em
->generation
= -1;
6672 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6673 if (type
== BTRFS_ORDERED_PREALLOC
)
6674 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6677 btrfs_drop_extent_cache(inode
, em
->start
,
6678 em
->start
+ em
->len
- 1, 0);
6679 write_lock(&em_tree
->lock
);
6680 ret
= add_extent_mapping(em_tree
, em
);
6682 list_move(&em
->list
,
6683 &em_tree
->modified_extents
);
6684 write_unlock(&em_tree
->lock
);
6685 } while (ret
== -EEXIST
);
6688 free_extent_map(em
);
6689 return ERR_PTR(ret
);
6696 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6697 struct buffer_head
*bh_result
, int create
)
6699 struct extent_map
*em
;
6700 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6701 struct extent_state
*cached_state
= NULL
;
6702 u64 start
= iblock
<< inode
->i_blkbits
;
6703 u64 lockstart
, lockend
;
6704 u64 len
= bh_result
->b_size
;
6705 struct btrfs_trans_handle
*trans
;
6706 int unlock_bits
= EXTENT_LOCKED
;
6710 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6712 len
= min_t(u64
, len
, root
->sectorsize
);
6715 lockend
= start
+ len
- 1;
6718 * If this errors out it's because we couldn't invalidate pagecache for
6719 * this range and we need to fallback to buffered.
6721 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6724 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6731 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6732 * io. INLINE is special, and we could probably kludge it in here, but
6733 * it's still buffered so for safety lets just fall back to the generic
6736 * For COMPRESSED we _have_ to read the entire extent in so we can
6737 * decompress it, so there will be buffering required no matter what we
6738 * do, so go ahead and fallback to buffered.
6740 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6741 * to buffered IO. Don't blame me, this is the price we pay for using
6744 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6745 em
->block_start
== EXTENT_MAP_INLINE
) {
6746 free_extent_map(em
);
6751 /* Just a good old fashioned hole, return */
6752 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6753 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6754 free_extent_map(em
);
6759 * We don't allocate a new extent in the following cases
6761 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6763 * 2) The extent is marked as PREALLOC. We're good to go here and can
6764 * just use the extent.
6768 len
= min(len
, em
->len
- (start
- em
->start
));
6769 lockstart
= start
+ len
;
6773 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6774 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6775 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6780 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6781 type
= BTRFS_ORDERED_PREALLOC
;
6783 type
= BTRFS_ORDERED_NOCOW
;
6784 len
= min(len
, em
->len
- (start
- em
->start
));
6785 block_start
= em
->block_start
+ (start
- em
->start
);
6788 * we're not going to log anything, but we do need
6789 * to make sure the current transaction stays open
6790 * while we look for nocow cross refs
6792 trans
= btrfs_join_transaction(root
);
6796 if (can_nocow_odirect(trans
, inode
, start
, len
) == 1) {
6797 u64 orig_start
= em
->orig_start
;
6798 u64 orig_block_len
= em
->orig_block_len
;
6800 if (type
== BTRFS_ORDERED_PREALLOC
) {
6801 free_extent_map(em
);
6802 em
= create_pinned_em(inode
, start
, len
,
6805 orig_block_len
, type
);
6807 btrfs_end_transaction(trans
, root
);
6812 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6813 block_start
, len
, len
, type
);
6814 btrfs_end_transaction(trans
, root
);
6816 free_extent_map(em
);
6821 btrfs_end_transaction(trans
, root
);
6825 * this will cow the extent, reset the len in case we changed
6828 len
= bh_result
->b_size
;
6829 free_extent_map(em
);
6830 em
= btrfs_new_extent_direct(inode
, start
, len
);
6835 len
= min(len
, em
->len
- (start
- em
->start
));
6837 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6839 bh_result
->b_size
= len
;
6840 bh_result
->b_bdev
= em
->bdev
;
6841 set_buffer_mapped(bh_result
);
6843 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6844 set_buffer_new(bh_result
);
6847 * Need to update the i_size under the extent lock so buffered
6848 * readers will get the updated i_size when we unlock.
6850 if (start
+ len
> i_size_read(inode
))
6851 i_size_write(inode
, start
+ len
);
6853 spin_lock(&BTRFS_I(inode
)->lock
);
6854 BTRFS_I(inode
)->outstanding_extents
++;
6855 spin_unlock(&BTRFS_I(inode
)->lock
);
6857 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6858 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6859 &cached_state
, GFP_NOFS
);
6864 * In the case of write we need to clear and unlock the entire range,
6865 * in the case of read we need to unlock only the end area that we
6866 * aren't using if there is any left over space.
6868 if (lockstart
< lockend
) {
6869 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6870 lockend
, unlock_bits
, 1, 0,
6871 &cached_state
, GFP_NOFS
);
6873 free_extent_state(cached_state
);
6876 free_extent_map(em
);
6881 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6882 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6886 struct btrfs_dio_private
{
6887 struct inode
*inode
;
6893 /* number of bios pending for this dio */
6894 atomic_t pending_bios
;
6899 struct bio
*orig_bio
;
6902 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6904 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6905 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6906 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6907 struct inode
*inode
= dip
->inode
;
6908 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6911 start
= dip
->logical_offset
;
6913 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6914 struct page
*page
= bvec
->bv_page
;
6917 u64
private = ~(u32
)0;
6918 unsigned long flags
;
6920 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6923 local_irq_save(flags
);
6924 kaddr
= kmap_atomic(page
);
6925 csum
= btrfs_csum_data(root
, kaddr
+ bvec
->bv_offset
,
6926 csum
, bvec
->bv_len
);
6927 btrfs_csum_final(csum
, (char *)&csum
);
6928 kunmap_atomic(kaddr
);
6929 local_irq_restore(flags
);
6931 flush_dcache_page(bvec
->bv_page
);
6932 if (csum
!= private) {
6934 printk(KERN_ERR
"btrfs csum failed ino %llu off"
6935 " %llu csum %u private %u\n",
6936 (unsigned long long)btrfs_ino(inode
),
6937 (unsigned long long)start
,
6938 csum
, (unsigned)private);
6943 start
+= bvec
->bv_len
;
6945 } while (bvec
<= bvec_end
);
6947 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6948 dip
->logical_offset
+ dip
->bytes
- 1);
6949 bio
->bi_private
= dip
->private;
6953 /* If we had a csum failure make sure to clear the uptodate flag */
6955 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
6956 dio_end_io(bio
, err
);
6959 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6961 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6962 struct inode
*inode
= dip
->inode
;
6963 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6964 struct btrfs_ordered_extent
*ordered
= NULL
;
6965 u64 ordered_offset
= dip
->logical_offset
;
6966 u64 ordered_bytes
= dip
->bytes
;
6972 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6974 ordered_bytes
, !err
);
6978 ordered
->work
.func
= finish_ordered_fn
;
6979 ordered
->work
.flags
= 0;
6980 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6984 * our bio might span multiple ordered extents. If we haven't
6985 * completed the accounting for the whole dio, go back and try again
6987 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6988 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6994 bio
->bi_private
= dip
->private;
6998 /* If we had an error make sure to clear the uptodate flag */
7000 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
7001 dio_end_io(bio
, err
);
7004 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7005 struct bio
*bio
, int mirror_num
,
7006 unsigned long bio_flags
, u64 offset
)
7009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7010 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7011 BUG_ON(ret
); /* -ENOMEM */
7015 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7017 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7020 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
7021 "sector %#Lx len %u err no %d\n",
7022 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
7023 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
7027 * before atomic variable goto zero, we must make sure
7028 * dip->errors is perceived to be set.
7030 smp_mb__before_atomic_dec();
7033 /* if there are more bios still pending for this dio, just exit */
7034 if (!atomic_dec_and_test(&dip
->pending_bios
))
7038 bio_io_error(dip
->orig_bio
);
7040 set_bit(BIO_UPTODATE
, &dip
->orig_bio
->bi_flags
);
7041 bio_endio(dip
->orig_bio
, 0);
7047 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7048 u64 first_sector
, gfp_t gfp_flags
)
7050 int nr_vecs
= bio_get_nr_vecs(bdev
);
7051 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7054 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7055 int rw
, u64 file_offset
, int skip_sum
,
7058 int write
= rw
& REQ_WRITE
;
7059 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7063 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7068 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7076 if (write
&& async_submit
) {
7077 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7078 inode
, rw
, bio
, 0, 0,
7080 __btrfs_submit_bio_start_direct_io
,
7081 __btrfs_submit_bio_done
);
7085 * If we aren't doing async submit, calculate the csum of the
7088 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7091 } else if (!skip_sum
) {
7092 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7098 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7104 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7107 struct inode
*inode
= dip
->inode
;
7108 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7110 struct bio
*orig_bio
= dip
->orig_bio
;
7111 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7112 u64 start_sector
= orig_bio
->bi_sector
;
7113 u64 file_offset
= dip
->logical_offset
;
7118 int async_submit
= 0;
7120 map_length
= orig_bio
->bi_size
;
7121 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7122 &map_length
, NULL
, 0);
7127 if (map_length
>= orig_bio
->bi_size
) {
7132 /* async crcs make it difficult to collect full stripe writes. */
7133 if (btrfs_get_alloc_profile(root
, 1) &
7134 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7139 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7142 bio
->bi_private
= dip
;
7143 bio
->bi_end_io
= btrfs_end_dio_bio
;
7144 atomic_inc(&dip
->pending_bios
);
7146 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7147 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7148 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7149 bvec
->bv_offset
) < bvec
->bv_len
)) {
7151 * inc the count before we submit the bio so
7152 * we know the end IO handler won't happen before
7153 * we inc the count. Otherwise, the dip might get freed
7154 * before we're done setting it up
7156 atomic_inc(&dip
->pending_bios
);
7157 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7158 file_offset
, skip_sum
,
7162 atomic_dec(&dip
->pending_bios
);
7166 start_sector
+= submit_len
>> 9;
7167 file_offset
+= submit_len
;
7172 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7173 start_sector
, GFP_NOFS
);
7176 bio
->bi_private
= dip
;
7177 bio
->bi_end_io
= btrfs_end_dio_bio
;
7179 map_length
= orig_bio
->bi_size
;
7180 ret
= btrfs_map_block(root
->fs_info
, rw
,
7182 &map_length
, NULL
, 0);
7188 submit_len
+= bvec
->bv_len
;
7195 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7204 * before atomic variable goto zero, we must
7205 * make sure dip->errors is perceived to be set.
7207 smp_mb__before_atomic_dec();
7208 if (atomic_dec_and_test(&dip
->pending_bios
))
7209 bio_io_error(dip
->orig_bio
);
7211 /* bio_end_io() will handle error, so we needn't return it */
7215 static void btrfs_submit_direct(int rw
, struct bio
*bio
, struct inode
*inode
,
7218 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7219 struct btrfs_dio_private
*dip
;
7220 struct bio_vec
*bvec
= bio
->bi_io_vec
;
7222 int write
= rw
& REQ_WRITE
;
7225 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7227 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7233 dip
->private = bio
->bi_private
;
7235 dip
->logical_offset
= file_offset
;
7239 dip
->bytes
+= bvec
->bv_len
;
7241 } while (bvec
<= (bio
->bi_io_vec
+ bio
->bi_vcnt
- 1));
7243 dip
->disk_bytenr
= (u64
)bio
->bi_sector
<< 9;
7244 bio
->bi_private
= dip
;
7246 dip
->orig_bio
= bio
;
7247 atomic_set(&dip
->pending_bios
, 0);
7250 bio
->bi_end_io
= btrfs_endio_direct_write
;
7252 bio
->bi_end_io
= btrfs_endio_direct_read
;
7254 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7259 * If this is a write, we need to clean up the reserved space and kill
7260 * the ordered extent.
7263 struct btrfs_ordered_extent
*ordered
;
7264 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7265 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7266 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7267 btrfs_free_reserved_extent(root
, ordered
->start
,
7269 btrfs_put_ordered_extent(ordered
);
7270 btrfs_put_ordered_extent(ordered
);
7272 bio_endio(bio
, ret
);
7275 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7276 const struct iovec
*iov
, loff_t offset
,
7277 unsigned long nr_segs
)
7283 unsigned blocksize_mask
= root
->sectorsize
- 1;
7284 ssize_t retval
= -EINVAL
;
7285 loff_t end
= offset
;
7287 if (offset
& blocksize_mask
)
7290 /* Check the memory alignment. Blocks cannot straddle pages */
7291 for (seg
= 0; seg
< nr_segs
; seg
++) {
7292 addr
= (unsigned long)iov
[seg
].iov_base
;
7293 size
= iov
[seg
].iov_len
;
7295 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7298 /* If this is a write we don't need to check anymore */
7303 * Check to make sure we don't have duplicate iov_base's in this
7304 * iovec, if so return EINVAL, otherwise we'll get csum errors
7305 * when reading back.
7307 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7308 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7317 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7318 const struct iovec
*iov
, loff_t offset
,
7319 unsigned long nr_segs
)
7321 struct file
*file
= iocb
->ki_filp
;
7322 struct inode
*inode
= file
->f_mapping
->host
;
7326 bool relock
= false;
7329 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7333 atomic_inc(&inode
->i_dio_count
);
7334 smp_mb__after_atomic_inc();
7337 count
= iov_length(iov
, nr_segs
);
7339 * If the write DIO is beyond the EOF, we need update
7340 * the isize, but it is protected by i_mutex. So we can
7341 * not unlock the i_mutex at this case.
7343 if (offset
+ count
<= inode
->i_size
) {
7344 mutex_unlock(&inode
->i_mutex
);
7347 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7350 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7351 &BTRFS_I(inode
)->runtime_flags
))) {
7352 inode_dio_done(inode
);
7353 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7357 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7358 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7359 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7360 btrfs_submit_direct
, flags
);
7362 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7363 btrfs_delalloc_release_space(inode
, count
);
7364 else if (ret
>= 0 && (size_t)ret
< count
)
7365 btrfs_delalloc_release_space(inode
,
7366 count
- (size_t)ret
);
7368 btrfs_delalloc_release_metadata(inode
, 0);
7372 inode_dio_done(inode
);
7374 mutex_lock(&inode
->i_mutex
);
7379 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7381 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7382 __u64 start
, __u64 len
)
7386 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7390 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7393 int btrfs_readpage(struct file
*file
, struct page
*page
)
7395 struct extent_io_tree
*tree
;
7396 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7397 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7400 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7402 struct extent_io_tree
*tree
;
7405 if (current
->flags
& PF_MEMALLOC
) {
7406 redirty_page_for_writepage(wbc
, page
);
7410 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7411 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7414 int btrfs_writepages(struct address_space
*mapping
,
7415 struct writeback_control
*wbc
)
7417 struct extent_io_tree
*tree
;
7419 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7420 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7424 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7425 struct list_head
*pages
, unsigned nr_pages
)
7427 struct extent_io_tree
*tree
;
7428 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7429 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7432 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7434 struct extent_io_tree
*tree
;
7435 struct extent_map_tree
*map
;
7438 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7439 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7440 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7442 ClearPagePrivate(page
);
7443 set_page_private(page
, 0);
7444 page_cache_release(page
);
7449 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7451 if (PageWriteback(page
) || PageDirty(page
))
7453 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7456 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7458 struct inode
*inode
= page
->mapping
->host
;
7459 struct extent_io_tree
*tree
;
7460 struct btrfs_ordered_extent
*ordered
;
7461 struct extent_state
*cached_state
= NULL
;
7462 u64 page_start
= page_offset(page
);
7463 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7466 * we have the page locked, so new writeback can't start,
7467 * and the dirty bit won't be cleared while we are here.
7469 * Wait for IO on this page so that we can safely clear
7470 * the PagePrivate2 bit and do ordered accounting
7472 wait_on_page_writeback(page
);
7474 tree
= &BTRFS_I(inode
)->io_tree
;
7476 btrfs_releasepage(page
, GFP_NOFS
);
7479 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7480 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7483 * IO on this page will never be started, so we need
7484 * to account for any ordered extents now
7486 clear_extent_bit(tree
, page_start
, page_end
,
7487 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7488 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7489 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7491 * whoever cleared the private bit is responsible
7492 * for the finish_ordered_io
7494 if (TestClearPagePrivate2(page
) &&
7495 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7496 PAGE_CACHE_SIZE
, 1)) {
7497 btrfs_finish_ordered_io(ordered
);
7499 btrfs_put_ordered_extent(ordered
);
7500 cached_state
= NULL
;
7501 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7503 clear_extent_bit(tree
, page_start
, page_end
,
7504 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7505 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7506 &cached_state
, GFP_NOFS
);
7507 __btrfs_releasepage(page
, GFP_NOFS
);
7509 ClearPageChecked(page
);
7510 if (PagePrivate(page
)) {
7511 ClearPagePrivate(page
);
7512 set_page_private(page
, 0);
7513 page_cache_release(page
);
7518 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7519 * called from a page fault handler when a page is first dirtied. Hence we must
7520 * be careful to check for EOF conditions here. We set the page up correctly
7521 * for a written page which means we get ENOSPC checking when writing into
7522 * holes and correct delalloc and unwritten extent mapping on filesystems that
7523 * support these features.
7525 * We are not allowed to take the i_mutex here so we have to play games to
7526 * protect against truncate races as the page could now be beyond EOF. Because
7527 * vmtruncate() writes the inode size before removing pages, once we have the
7528 * page lock we can determine safely if the page is beyond EOF. If it is not
7529 * beyond EOF, then the page is guaranteed safe against truncation until we
7532 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7534 struct page
*page
= vmf
->page
;
7535 struct inode
*inode
= file_inode(vma
->vm_file
);
7536 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7537 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7538 struct btrfs_ordered_extent
*ordered
;
7539 struct extent_state
*cached_state
= NULL
;
7541 unsigned long zero_start
;
7548 sb_start_pagefault(inode
->i_sb
);
7549 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7551 ret
= file_update_time(vma
->vm_file
);
7557 else /* -ENOSPC, -EIO, etc */
7558 ret
= VM_FAULT_SIGBUS
;
7564 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7567 size
= i_size_read(inode
);
7568 page_start
= page_offset(page
);
7569 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7571 if ((page
->mapping
!= inode
->i_mapping
) ||
7572 (page_start
>= size
)) {
7573 /* page got truncated out from underneath us */
7576 wait_on_page_writeback(page
);
7578 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7579 set_page_extent_mapped(page
);
7582 * we can't set the delalloc bits if there are pending ordered
7583 * extents. Drop our locks and wait for them to finish
7585 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7587 unlock_extent_cached(io_tree
, page_start
, page_end
,
7588 &cached_state
, GFP_NOFS
);
7590 btrfs_start_ordered_extent(inode
, ordered
, 1);
7591 btrfs_put_ordered_extent(ordered
);
7596 * XXX - page_mkwrite gets called every time the page is dirtied, even
7597 * if it was already dirty, so for space accounting reasons we need to
7598 * clear any delalloc bits for the range we are fixing to save. There
7599 * is probably a better way to do this, but for now keep consistent with
7600 * prepare_pages in the normal write path.
7602 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7603 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7604 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7605 0, 0, &cached_state
, GFP_NOFS
);
7607 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7610 unlock_extent_cached(io_tree
, page_start
, page_end
,
7611 &cached_state
, GFP_NOFS
);
7612 ret
= VM_FAULT_SIGBUS
;
7617 /* page is wholly or partially inside EOF */
7618 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7619 zero_start
= size
& ~PAGE_CACHE_MASK
;
7621 zero_start
= PAGE_CACHE_SIZE
;
7623 if (zero_start
!= PAGE_CACHE_SIZE
) {
7625 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7626 flush_dcache_page(page
);
7629 ClearPageChecked(page
);
7630 set_page_dirty(page
);
7631 SetPageUptodate(page
);
7633 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7634 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7635 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7637 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7641 sb_end_pagefault(inode
->i_sb
);
7642 return VM_FAULT_LOCKED
;
7646 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7648 sb_end_pagefault(inode
->i_sb
);
7652 static int btrfs_truncate(struct inode
*inode
)
7654 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7655 struct btrfs_block_rsv
*rsv
;
7658 struct btrfs_trans_handle
*trans
;
7659 u64 mask
= root
->sectorsize
- 1;
7660 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7662 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7666 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7667 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7670 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7671 * 3 things going on here
7673 * 1) We need to reserve space for our orphan item and the space to
7674 * delete our orphan item. Lord knows we don't want to have a dangling
7675 * orphan item because we didn't reserve space to remove it.
7677 * 2) We need to reserve space to update our inode.
7679 * 3) We need to have something to cache all the space that is going to
7680 * be free'd up by the truncate operation, but also have some slack
7681 * space reserved in case it uses space during the truncate (thank you
7682 * very much snapshotting).
7684 * And we need these to all be seperate. The fact is we can use alot of
7685 * space doing the truncate, and we have no earthly idea how much space
7686 * we will use, so we need the truncate reservation to be seperate so it
7687 * doesn't end up using space reserved for updating the inode or
7688 * removing the orphan item. We also need to be able to stop the
7689 * transaction and start a new one, which means we need to be able to
7690 * update the inode several times, and we have no idea of knowing how
7691 * many times that will be, so we can't just reserve 1 item for the
7692 * entirety of the opration, so that has to be done seperately as well.
7693 * Then there is the orphan item, which does indeed need to be held on
7694 * to for the whole operation, and we need nobody to touch this reserved
7695 * space except the orphan code.
7697 * So that leaves us with
7699 * 1) root->orphan_block_rsv - for the orphan deletion.
7700 * 2) rsv - for the truncate reservation, which we will steal from the
7701 * transaction reservation.
7702 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7703 * updating the inode.
7705 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7708 rsv
->size
= min_size
;
7712 * 1 for the truncate slack space
7713 * 1 for updating the inode.
7715 trans
= btrfs_start_transaction(root
, 2);
7716 if (IS_ERR(trans
)) {
7717 err
= PTR_ERR(trans
);
7721 /* Migrate the slack space for the truncate to our reserve */
7722 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7727 * setattr is responsible for setting the ordered_data_close flag,
7728 * but that is only tested during the last file release. That
7729 * could happen well after the next commit, leaving a great big
7730 * window where new writes may get lost if someone chooses to write
7731 * to this file after truncating to zero
7733 * The inode doesn't have any dirty data here, and so if we commit
7734 * this is a noop. If someone immediately starts writing to the inode
7735 * it is very likely we'll catch some of their writes in this
7736 * transaction, and the commit will find this file on the ordered
7737 * data list with good things to send down.
7739 * This is a best effort solution, there is still a window where
7740 * using truncate to replace the contents of the file will
7741 * end up with a zero length file after a crash.
7743 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7744 &BTRFS_I(inode
)->runtime_flags
))
7745 btrfs_add_ordered_operation(trans
, root
, inode
);
7748 * So if we truncate and then write and fsync we normally would just
7749 * write the extents that changed, which is a problem if we need to
7750 * first truncate that entire inode. So set this flag so we write out
7751 * all of the extents in the inode to the sync log so we're completely
7754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7755 trans
->block_rsv
= rsv
;
7758 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7760 BTRFS_EXTENT_DATA_KEY
);
7761 if (ret
!= -ENOSPC
) {
7766 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7767 ret
= btrfs_update_inode(trans
, root
, inode
);
7773 btrfs_end_transaction(trans
, root
);
7774 btrfs_btree_balance_dirty(root
);
7776 trans
= btrfs_start_transaction(root
, 2);
7777 if (IS_ERR(trans
)) {
7778 ret
= err
= PTR_ERR(trans
);
7783 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7785 BUG_ON(ret
); /* shouldn't happen */
7786 trans
->block_rsv
= rsv
;
7789 if (ret
== 0 && inode
->i_nlink
> 0) {
7790 trans
->block_rsv
= root
->orphan_block_rsv
;
7791 ret
= btrfs_orphan_del(trans
, inode
);
7797 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7798 ret
= btrfs_update_inode(trans
, root
, inode
);
7802 ret
= btrfs_end_transaction(trans
, root
);
7803 btrfs_btree_balance_dirty(root
);
7807 btrfs_free_block_rsv(root
, rsv
);
7816 * create a new subvolume directory/inode (helper for the ioctl).
7818 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7819 struct btrfs_root
*new_root
, u64 new_dirid
)
7821 struct inode
*inode
;
7825 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7826 new_dirid
, new_dirid
,
7827 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7830 return PTR_ERR(inode
);
7831 inode
->i_op
= &btrfs_dir_inode_operations
;
7832 inode
->i_fop
= &btrfs_dir_file_operations
;
7834 set_nlink(inode
, 1);
7835 btrfs_i_size_write(inode
, 0);
7837 err
= btrfs_update_inode(trans
, new_root
, inode
);
7843 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7845 struct btrfs_inode
*ei
;
7846 struct inode
*inode
;
7848 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7855 ei
->last_sub_trans
= 0;
7856 ei
->logged_trans
= 0;
7857 ei
->delalloc_bytes
= 0;
7858 ei
->disk_i_size
= 0;
7861 ei
->index_cnt
= (u64
)-1;
7862 ei
->last_unlink_trans
= 0;
7863 ei
->last_log_commit
= 0;
7865 spin_lock_init(&ei
->lock
);
7866 ei
->outstanding_extents
= 0;
7867 ei
->reserved_extents
= 0;
7869 ei
->runtime_flags
= 0;
7870 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7872 ei
->delayed_node
= NULL
;
7874 inode
= &ei
->vfs_inode
;
7875 extent_map_tree_init(&ei
->extent_tree
);
7876 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7877 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7878 ei
->io_tree
.track_uptodate
= 1;
7879 ei
->io_failure_tree
.track_uptodate
= 1;
7880 atomic_set(&ei
->sync_writers
, 0);
7881 mutex_init(&ei
->log_mutex
);
7882 mutex_init(&ei
->delalloc_mutex
);
7883 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7884 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7885 INIT_LIST_HEAD(&ei
->ordered_operations
);
7886 RB_CLEAR_NODE(&ei
->rb_node
);
7891 static void btrfs_i_callback(struct rcu_head
*head
)
7893 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7894 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7897 void btrfs_destroy_inode(struct inode
*inode
)
7899 struct btrfs_ordered_extent
*ordered
;
7900 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7902 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7903 WARN_ON(inode
->i_data
.nrpages
);
7904 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7905 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7906 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7907 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7910 * This can happen where we create an inode, but somebody else also
7911 * created the same inode and we need to destroy the one we already
7918 * Make sure we're properly removed from the ordered operation
7922 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7923 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7924 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7925 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7928 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7929 &BTRFS_I(inode
)->runtime_flags
)) {
7930 printk(KERN_INFO
"BTRFS: inode %llu still on the orphan list\n",
7931 (unsigned long long)btrfs_ino(inode
));
7932 atomic_dec(&root
->orphan_inodes
);
7936 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7940 printk(KERN_ERR
"btrfs found ordered "
7941 "extent %llu %llu on inode cleanup\n",
7942 (unsigned long long)ordered
->file_offset
,
7943 (unsigned long long)ordered
->len
);
7944 btrfs_remove_ordered_extent(inode
, ordered
);
7945 btrfs_put_ordered_extent(ordered
);
7946 btrfs_put_ordered_extent(ordered
);
7949 inode_tree_del(inode
);
7950 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7952 btrfs_remove_delayed_node(inode
);
7953 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7956 int btrfs_drop_inode(struct inode
*inode
)
7958 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7960 /* the snap/subvol tree is on deleting */
7961 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7962 root
!= root
->fs_info
->tree_root
)
7965 return generic_drop_inode(inode
);
7968 static void init_once(void *foo
)
7970 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7972 inode_init_once(&ei
->vfs_inode
);
7975 void btrfs_destroy_cachep(void)
7978 * Make sure all delayed rcu free inodes are flushed before we
7982 if (btrfs_inode_cachep
)
7983 kmem_cache_destroy(btrfs_inode_cachep
);
7984 if (btrfs_trans_handle_cachep
)
7985 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7986 if (btrfs_transaction_cachep
)
7987 kmem_cache_destroy(btrfs_transaction_cachep
);
7988 if (btrfs_path_cachep
)
7989 kmem_cache_destroy(btrfs_path_cachep
);
7990 if (btrfs_free_space_cachep
)
7991 kmem_cache_destroy(btrfs_free_space_cachep
);
7992 if (btrfs_delalloc_work_cachep
)
7993 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7996 int btrfs_init_cachep(void)
7998 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7999 sizeof(struct btrfs_inode
), 0,
8000 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8001 if (!btrfs_inode_cachep
)
8004 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8005 sizeof(struct btrfs_trans_handle
), 0,
8006 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8007 if (!btrfs_trans_handle_cachep
)
8010 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8011 sizeof(struct btrfs_transaction
), 0,
8012 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8013 if (!btrfs_transaction_cachep
)
8016 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8017 sizeof(struct btrfs_path
), 0,
8018 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8019 if (!btrfs_path_cachep
)
8022 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8023 sizeof(struct btrfs_free_space
), 0,
8024 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8025 if (!btrfs_free_space_cachep
)
8028 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8029 sizeof(struct btrfs_delalloc_work
), 0,
8030 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8032 if (!btrfs_delalloc_work_cachep
)
8037 btrfs_destroy_cachep();
8041 static int btrfs_getattr(struct vfsmount
*mnt
,
8042 struct dentry
*dentry
, struct kstat
*stat
)
8045 struct inode
*inode
= dentry
->d_inode
;
8046 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8048 generic_fillattr(inode
, stat
);
8049 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8050 stat
->blksize
= PAGE_CACHE_SIZE
;
8052 spin_lock(&BTRFS_I(inode
)->lock
);
8053 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8054 spin_unlock(&BTRFS_I(inode
)->lock
);
8055 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8056 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8060 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8061 struct inode
*new_dir
, struct dentry
*new_dentry
)
8063 struct btrfs_trans_handle
*trans
;
8064 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8065 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8066 struct inode
*new_inode
= new_dentry
->d_inode
;
8067 struct inode
*old_inode
= old_dentry
->d_inode
;
8068 struct timespec ctime
= CURRENT_TIME
;
8072 u64 old_ino
= btrfs_ino(old_inode
);
8074 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8077 /* we only allow rename subvolume link between subvolumes */
8078 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8081 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8082 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8085 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8086 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8090 /* check for collisions, even if the name isn't there */
8091 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8092 new_dentry
->d_name
.name
,
8093 new_dentry
->d_name
.len
);
8096 if (ret
== -EEXIST
) {
8098 * eexist without a new_inode */
8104 /* maybe -EOVERFLOW */
8111 * we're using rename to replace one file with another.
8112 * and the replacement file is large. Start IO on it now so
8113 * we don't add too much work to the end of the transaction
8115 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8116 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8117 filemap_flush(old_inode
->i_mapping
);
8119 /* close the racy window with snapshot create/destroy ioctl */
8120 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8121 down_read(&root
->fs_info
->subvol_sem
);
8123 * We want to reserve the absolute worst case amount of items. So if
8124 * both inodes are subvols and we need to unlink them then that would
8125 * require 4 item modifications, but if they are both normal inodes it
8126 * would require 5 item modifications, so we'll assume their normal
8127 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8128 * should cover the worst case number of items we'll modify.
8130 trans
= btrfs_start_transaction(root
, 20);
8131 if (IS_ERR(trans
)) {
8132 ret
= PTR_ERR(trans
);
8137 btrfs_record_root_in_trans(trans
, dest
);
8139 ret
= btrfs_set_inode_index(new_dir
, &index
);
8143 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8144 /* force full log commit if subvolume involved. */
8145 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8147 ret
= btrfs_insert_inode_ref(trans
, dest
,
8148 new_dentry
->d_name
.name
,
8149 new_dentry
->d_name
.len
,
8151 btrfs_ino(new_dir
), index
);
8155 * this is an ugly little race, but the rename is required
8156 * to make sure that if we crash, the inode is either at the
8157 * old name or the new one. pinning the log transaction lets
8158 * us make sure we don't allow a log commit to come in after
8159 * we unlink the name but before we add the new name back in.
8161 btrfs_pin_log_trans(root
);
8164 * make sure the inode gets flushed if it is replacing
8167 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8168 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8170 inode_inc_iversion(old_dir
);
8171 inode_inc_iversion(new_dir
);
8172 inode_inc_iversion(old_inode
);
8173 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8174 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8175 old_inode
->i_ctime
= ctime
;
8177 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8178 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8180 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8181 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8182 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8183 old_dentry
->d_name
.name
,
8184 old_dentry
->d_name
.len
);
8186 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8187 old_dentry
->d_inode
,
8188 old_dentry
->d_name
.name
,
8189 old_dentry
->d_name
.len
);
8191 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8194 btrfs_abort_transaction(trans
, root
, ret
);
8199 inode_inc_iversion(new_inode
);
8200 new_inode
->i_ctime
= CURRENT_TIME
;
8201 if (unlikely(btrfs_ino(new_inode
) ==
8202 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8203 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8204 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8206 new_dentry
->d_name
.name
,
8207 new_dentry
->d_name
.len
);
8208 BUG_ON(new_inode
->i_nlink
== 0);
8210 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8211 new_dentry
->d_inode
,
8212 new_dentry
->d_name
.name
,
8213 new_dentry
->d_name
.len
);
8215 if (!ret
&& new_inode
->i_nlink
== 0) {
8216 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8220 btrfs_abort_transaction(trans
, root
, ret
);
8225 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8226 new_dentry
->d_name
.name
,
8227 new_dentry
->d_name
.len
, 0, index
);
8229 btrfs_abort_transaction(trans
, root
, ret
);
8233 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8234 struct dentry
*parent
= new_dentry
->d_parent
;
8235 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8236 btrfs_end_log_trans(root
);
8239 btrfs_end_transaction(trans
, root
);
8241 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8242 up_read(&root
->fs_info
->subvol_sem
);
8247 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8249 struct btrfs_delalloc_work
*delalloc_work
;
8251 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8253 if (delalloc_work
->wait
)
8254 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8256 filemap_flush(delalloc_work
->inode
->i_mapping
);
8258 if (delalloc_work
->delay_iput
)
8259 btrfs_add_delayed_iput(delalloc_work
->inode
);
8261 iput(delalloc_work
->inode
);
8262 complete(&delalloc_work
->completion
);
8265 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8266 int wait
, int delay_iput
)
8268 struct btrfs_delalloc_work
*work
;
8270 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8274 init_completion(&work
->completion
);
8275 INIT_LIST_HEAD(&work
->list
);
8276 work
->inode
= inode
;
8278 work
->delay_iput
= delay_iput
;
8279 work
->work
.func
= btrfs_run_delalloc_work
;
8284 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8286 wait_for_completion(&work
->completion
);
8287 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8291 * some fairly slow code that needs optimization. This walks the list
8292 * of all the inodes with pending delalloc and forces them to disk.
8294 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8296 struct btrfs_inode
*binode
;
8297 struct inode
*inode
;
8298 struct btrfs_delalloc_work
*work
, *next
;
8299 struct list_head works
;
8300 struct list_head splice
;
8303 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8306 INIT_LIST_HEAD(&works
);
8307 INIT_LIST_HEAD(&splice
);
8309 spin_lock(&root
->fs_info
->delalloc_lock
);
8310 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
8311 while (!list_empty(&splice
)) {
8312 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8315 list_del_init(&binode
->delalloc_inodes
);
8317 inode
= igrab(&binode
->vfs_inode
);
8319 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
8320 &binode
->runtime_flags
);
8324 list_add_tail(&binode
->delalloc_inodes
,
8325 &root
->fs_info
->delalloc_inodes
);
8326 spin_unlock(&root
->fs_info
->delalloc_lock
);
8328 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8329 if (unlikely(!work
)) {
8333 list_add_tail(&work
->list
, &works
);
8334 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8338 spin_lock(&root
->fs_info
->delalloc_lock
);
8340 spin_unlock(&root
->fs_info
->delalloc_lock
);
8342 list_for_each_entry_safe(work
, next
, &works
, list
) {
8343 list_del_init(&work
->list
);
8344 btrfs_wait_and_free_delalloc_work(work
);
8347 /* the filemap_flush will queue IO into the worker threads, but
8348 * we have to make sure the IO is actually started and that
8349 * ordered extents get created before we return
8351 atomic_inc(&root
->fs_info
->async_submit_draining
);
8352 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8353 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8354 wait_event(root
->fs_info
->async_submit_wait
,
8355 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8356 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8358 atomic_dec(&root
->fs_info
->async_submit_draining
);
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 if (!list_empty_careful(&splice
)) {
8367 spin_lock(&root
->fs_info
->delalloc_lock
);
8368 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
8369 spin_unlock(&root
->fs_info
->delalloc_lock
);
8374 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8375 const char *symname
)
8377 struct btrfs_trans_handle
*trans
;
8378 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8379 struct btrfs_path
*path
;
8380 struct btrfs_key key
;
8381 struct inode
*inode
= NULL
;
8389 struct btrfs_file_extent_item
*ei
;
8390 struct extent_buffer
*leaf
;
8392 name_len
= strlen(symname
) + 1;
8393 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8394 return -ENAMETOOLONG
;
8397 * 2 items for inode item and ref
8398 * 2 items for dir items
8399 * 1 item for xattr if selinux is on
8401 trans
= btrfs_start_transaction(root
, 5);
8403 return PTR_ERR(trans
);
8405 err
= btrfs_find_free_ino(root
, &objectid
);
8409 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8410 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8411 S_IFLNK
|S_IRWXUGO
, &index
);
8412 if (IS_ERR(inode
)) {
8413 err
= PTR_ERR(inode
);
8417 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8424 * If the active LSM wants to access the inode during
8425 * d_instantiate it needs these. Smack checks to see
8426 * if the filesystem supports xattrs by looking at the
8429 inode
->i_fop
= &btrfs_file_operations
;
8430 inode
->i_op
= &btrfs_file_inode_operations
;
8432 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8436 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8437 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8438 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8443 path
= btrfs_alloc_path();
8449 key
.objectid
= btrfs_ino(inode
);
8451 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8452 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8453 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8457 btrfs_free_path(path
);
8460 leaf
= path
->nodes
[0];
8461 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8462 struct btrfs_file_extent_item
);
8463 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8464 btrfs_set_file_extent_type(leaf
, ei
,
8465 BTRFS_FILE_EXTENT_INLINE
);
8466 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8467 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8468 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8469 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8471 ptr
= btrfs_file_extent_inline_start(ei
);
8472 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8473 btrfs_mark_buffer_dirty(leaf
);
8474 btrfs_free_path(path
);
8476 inode
->i_op
= &btrfs_symlink_inode_operations
;
8477 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8478 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8479 inode_set_bytes(inode
, name_len
);
8480 btrfs_i_size_write(inode
, name_len
- 1);
8481 err
= btrfs_update_inode(trans
, root
, inode
);
8487 d_instantiate(dentry
, inode
);
8488 btrfs_end_transaction(trans
, root
);
8490 inode_dec_link_count(inode
);
8493 btrfs_btree_balance_dirty(root
);
8497 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8498 u64 start
, u64 num_bytes
, u64 min_size
,
8499 loff_t actual_len
, u64
*alloc_hint
,
8500 struct btrfs_trans_handle
*trans
)
8502 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8503 struct extent_map
*em
;
8504 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8505 struct btrfs_key ins
;
8506 u64 cur_offset
= start
;
8510 bool own_trans
= true;
8514 while (num_bytes
> 0) {
8516 trans
= btrfs_start_transaction(root
, 3);
8517 if (IS_ERR(trans
)) {
8518 ret
= PTR_ERR(trans
);
8523 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8524 cur_bytes
= max(cur_bytes
, min_size
);
8525 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8526 min_size
, 0, *alloc_hint
, &ins
, 1);
8529 btrfs_end_transaction(trans
, root
);
8533 ret
= insert_reserved_file_extent(trans
, inode
,
8534 cur_offset
, ins
.objectid
,
8535 ins
.offset
, ins
.offset
,
8536 ins
.offset
, 0, 0, 0,
8537 BTRFS_FILE_EXTENT_PREALLOC
);
8539 btrfs_abort_transaction(trans
, root
, ret
);
8541 btrfs_end_transaction(trans
, root
);
8544 btrfs_drop_extent_cache(inode
, cur_offset
,
8545 cur_offset
+ ins
.offset
-1, 0);
8547 em
= alloc_extent_map();
8549 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8550 &BTRFS_I(inode
)->runtime_flags
);
8554 em
->start
= cur_offset
;
8555 em
->orig_start
= cur_offset
;
8556 em
->len
= ins
.offset
;
8557 em
->block_start
= ins
.objectid
;
8558 em
->block_len
= ins
.offset
;
8559 em
->orig_block_len
= ins
.offset
;
8560 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8561 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8562 em
->generation
= trans
->transid
;
8565 write_lock(&em_tree
->lock
);
8566 ret
= add_extent_mapping(em_tree
, em
);
8568 list_move(&em
->list
,
8569 &em_tree
->modified_extents
);
8570 write_unlock(&em_tree
->lock
);
8573 btrfs_drop_extent_cache(inode
, cur_offset
,
8574 cur_offset
+ ins
.offset
- 1,
8577 free_extent_map(em
);
8579 num_bytes
-= ins
.offset
;
8580 cur_offset
+= ins
.offset
;
8581 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8583 inode_inc_iversion(inode
);
8584 inode
->i_ctime
= CURRENT_TIME
;
8585 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8586 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8587 (actual_len
> inode
->i_size
) &&
8588 (cur_offset
> inode
->i_size
)) {
8589 if (cur_offset
> actual_len
)
8590 i_size
= actual_len
;
8592 i_size
= cur_offset
;
8593 i_size_write(inode
, i_size
);
8594 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8597 ret
= btrfs_update_inode(trans
, root
, inode
);
8600 btrfs_abort_transaction(trans
, root
, ret
);
8602 btrfs_end_transaction(trans
, root
);
8607 btrfs_end_transaction(trans
, root
);
8612 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8613 u64 start
, u64 num_bytes
, u64 min_size
,
8614 loff_t actual_len
, u64
*alloc_hint
)
8616 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8617 min_size
, actual_len
, alloc_hint
,
8621 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8622 struct btrfs_trans_handle
*trans
, int mode
,
8623 u64 start
, u64 num_bytes
, u64 min_size
,
8624 loff_t actual_len
, u64
*alloc_hint
)
8626 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8627 min_size
, actual_len
, alloc_hint
, trans
);
8630 static int btrfs_set_page_dirty(struct page
*page
)
8632 return __set_page_dirty_nobuffers(page
);
8635 static int btrfs_permission(struct inode
*inode
, int mask
)
8637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8638 umode_t mode
= inode
->i_mode
;
8640 if (mask
& MAY_WRITE
&&
8641 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8642 if (btrfs_root_readonly(root
))
8644 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8647 return generic_permission(inode
, mask
);
8650 static const struct inode_operations btrfs_dir_inode_operations
= {
8651 .getattr
= btrfs_getattr
,
8652 .lookup
= btrfs_lookup
,
8653 .create
= btrfs_create
,
8654 .unlink
= btrfs_unlink
,
8656 .mkdir
= btrfs_mkdir
,
8657 .rmdir
= btrfs_rmdir
,
8658 .rename
= btrfs_rename
,
8659 .symlink
= btrfs_symlink
,
8660 .setattr
= btrfs_setattr
,
8661 .mknod
= btrfs_mknod
,
8662 .setxattr
= btrfs_setxattr
,
8663 .getxattr
= btrfs_getxattr
,
8664 .listxattr
= btrfs_listxattr
,
8665 .removexattr
= btrfs_removexattr
,
8666 .permission
= btrfs_permission
,
8667 .get_acl
= btrfs_get_acl
,
8669 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8670 .lookup
= btrfs_lookup
,
8671 .permission
= btrfs_permission
,
8672 .get_acl
= btrfs_get_acl
,
8675 static const struct file_operations btrfs_dir_file_operations
= {
8676 .llseek
= generic_file_llseek
,
8677 .read
= generic_read_dir
,
8678 .readdir
= btrfs_real_readdir
,
8679 .unlocked_ioctl
= btrfs_ioctl
,
8680 #ifdef CONFIG_COMPAT
8681 .compat_ioctl
= btrfs_ioctl
,
8683 .release
= btrfs_release_file
,
8684 .fsync
= btrfs_sync_file
,
8687 static struct extent_io_ops btrfs_extent_io_ops
= {
8688 .fill_delalloc
= run_delalloc_range
,
8689 .submit_bio_hook
= btrfs_submit_bio_hook
,
8690 .merge_bio_hook
= btrfs_merge_bio_hook
,
8691 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8692 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8693 .writepage_start_hook
= btrfs_writepage_start_hook
,
8694 .set_bit_hook
= btrfs_set_bit_hook
,
8695 .clear_bit_hook
= btrfs_clear_bit_hook
,
8696 .merge_extent_hook
= btrfs_merge_extent_hook
,
8697 .split_extent_hook
= btrfs_split_extent_hook
,
8701 * btrfs doesn't support the bmap operation because swapfiles
8702 * use bmap to make a mapping of extents in the file. They assume
8703 * these extents won't change over the life of the file and they
8704 * use the bmap result to do IO directly to the drive.
8706 * the btrfs bmap call would return logical addresses that aren't
8707 * suitable for IO and they also will change frequently as COW
8708 * operations happen. So, swapfile + btrfs == corruption.
8710 * For now we're avoiding this by dropping bmap.
8712 static const struct address_space_operations btrfs_aops
= {
8713 .readpage
= btrfs_readpage
,
8714 .writepage
= btrfs_writepage
,
8715 .writepages
= btrfs_writepages
,
8716 .readpages
= btrfs_readpages
,
8717 .direct_IO
= btrfs_direct_IO
,
8718 .invalidatepage
= btrfs_invalidatepage
,
8719 .releasepage
= btrfs_releasepage
,
8720 .set_page_dirty
= btrfs_set_page_dirty
,
8721 .error_remove_page
= generic_error_remove_page
,
8724 static const struct address_space_operations btrfs_symlink_aops
= {
8725 .readpage
= btrfs_readpage
,
8726 .writepage
= btrfs_writepage
,
8727 .invalidatepage
= btrfs_invalidatepage
,
8728 .releasepage
= btrfs_releasepage
,
8731 static const struct inode_operations btrfs_file_inode_operations
= {
8732 .getattr
= btrfs_getattr
,
8733 .setattr
= btrfs_setattr
,
8734 .setxattr
= btrfs_setxattr
,
8735 .getxattr
= btrfs_getxattr
,
8736 .listxattr
= btrfs_listxattr
,
8737 .removexattr
= btrfs_removexattr
,
8738 .permission
= btrfs_permission
,
8739 .fiemap
= btrfs_fiemap
,
8740 .get_acl
= btrfs_get_acl
,
8741 .update_time
= btrfs_update_time
,
8743 static const struct inode_operations btrfs_special_inode_operations
= {
8744 .getattr
= btrfs_getattr
,
8745 .setattr
= btrfs_setattr
,
8746 .permission
= btrfs_permission
,
8747 .setxattr
= btrfs_setxattr
,
8748 .getxattr
= btrfs_getxattr
,
8749 .listxattr
= btrfs_listxattr
,
8750 .removexattr
= btrfs_removexattr
,
8751 .get_acl
= btrfs_get_acl
,
8752 .update_time
= btrfs_update_time
,
8754 static const struct inode_operations btrfs_symlink_inode_operations
= {
8755 .readlink
= generic_readlink
,
8756 .follow_link
= page_follow_link_light
,
8757 .put_link
= page_put_link
,
8758 .getattr
= btrfs_getattr
,
8759 .setattr
= btrfs_setattr
,
8760 .permission
= btrfs_permission
,
8761 .setxattr
= btrfs_setxattr
,
8762 .getxattr
= btrfs_getxattr
,
8763 .listxattr
= btrfs_listxattr
,
8764 .removexattr
= btrfs_removexattr
,
8765 .get_acl
= btrfs_get_acl
,
8766 .update_time
= btrfs_update_time
,
8769 const struct dentry_operations btrfs_dentry_operations
= {
8770 .d_delete
= btrfs_dentry_delete
,
8771 .d_release
= btrfs_dentry_release
,