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/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include <linux/falloc.h>
44 #include "transaction.h"
45 #include "btrfs_inode.h"
47 #include "print-tree.h"
49 #include "ordered-data.h"
52 #include "ref-cache.h"
53 #include "compression.h"
55 struct btrfs_iget_args
{
57 struct btrfs_root
*root
;
60 static struct inode_operations btrfs_dir_inode_operations
;
61 static struct inode_operations btrfs_symlink_inode_operations
;
62 static struct inode_operations btrfs_dir_ro_inode_operations
;
63 static struct inode_operations btrfs_special_inode_operations
;
64 static struct inode_operations btrfs_file_inode_operations
;
65 static struct address_space_operations btrfs_aops
;
66 static struct address_space_operations btrfs_symlink_aops
;
67 static struct file_operations btrfs_dir_file_operations
;
68 static struct extent_io_ops btrfs_extent_io_ops
;
70 static struct kmem_cache
*btrfs_inode_cachep
;
71 struct kmem_cache
*btrfs_trans_handle_cachep
;
72 struct kmem_cache
*btrfs_transaction_cachep
;
73 struct kmem_cache
*btrfs_bit_radix_cachep
;
74 struct kmem_cache
*btrfs_path_cachep
;
77 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
78 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
79 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
80 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
81 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
82 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
83 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
84 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
87 static void btrfs_truncate(struct inode
*inode
);
88 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
);
89 static noinline
int cow_file_range(struct inode
*inode
,
90 struct page
*locked_page
,
91 u64 start
, u64 end
, int *page_started
,
92 unsigned long *nr_written
, int unlock
);
95 * a very lame attempt at stopping writes when the FS is 85% full. There
96 * are countless ways this is incorrect, but it is better than nothing.
98 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
106 spin_lock(&root
->fs_info
->delalloc_lock
);
107 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
108 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
116 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
118 spin_unlock(&root
->fs_info
->delalloc_lock
);
123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
127 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
128 struct btrfs_root
*root
, struct inode
*inode
,
129 u64 start
, size_t size
, size_t compressed_size
,
130 struct page
**compressed_pages
)
132 struct btrfs_key key
;
133 struct btrfs_path
*path
;
134 struct extent_buffer
*leaf
;
135 struct page
*page
= NULL
;
138 struct btrfs_file_extent_item
*ei
;
141 size_t cur_size
= size
;
143 unsigned long offset
;
144 int use_compress
= 0;
146 if (compressed_size
&& compressed_pages
) {
148 cur_size
= compressed_size
;
151 path
= btrfs_alloc_path();
155 btrfs_set_trans_block_group(trans
, inode
);
157 key
.objectid
= inode
->i_ino
;
159 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
160 inode_add_bytes(inode
, size
);
161 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
163 inode_add_bytes(inode
, size
);
164 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
171 leaf
= path
->nodes
[0];
172 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
173 struct btrfs_file_extent_item
);
174 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
175 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
176 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
177 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
178 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
179 ptr
= btrfs_file_extent_inline_start(ei
);
184 while (compressed_size
> 0) {
185 cpage
= compressed_pages
[i
];
186 cur_size
= min_t(unsigned long, compressed_size
,
190 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
195 compressed_size
-= cur_size
;
197 btrfs_set_file_extent_compression(leaf
, ei
,
198 BTRFS_COMPRESS_ZLIB
);
200 page
= find_get_page(inode
->i_mapping
,
201 start
>> PAGE_CACHE_SHIFT
);
202 btrfs_set_file_extent_compression(leaf
, ei
, 0);
203 kaddr
= kmap_atomic(page
, KM_USER0
);
204 offset
= start
& (PAGE_CACHE_SIZE
- 1);
205 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
206 kunmap_atomic(kaddr
, KM_USER0
);
207 page_cache_release(page
);
209 btrfs_mark_buffer_dirty(leaf
);
210 btrfs_free_path(path
);
212 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
213 btrfs_update_inode(trans
, root
, inode
);
216 btrfs_free_path(path
);
222 * conditionally insert an inline extent into the file. This
223 * does the checks required to make sure the data is small enough
224 * to fit as an inline extent.
226 static int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
227 struct btrfs_root
*root
,
228 struct inode
*inode
, u64 start
, u64 end
,
229 size_t compressed_size
,
230 struct page
**compressed_pages
)
232 u64 isize
= i_size_read(inode
);
233 u64 actual_end
= min(end
+ 1, isize
);
234 u64 inline_len
= actual_end
- start
;
235 u64 aligned_end
= (end
+ root
->sectorsize
- 1) &
236 ~((u64
)root
->sectorsize
- 1);
238 u64 data_len
= inline_len
;
242 data_len
= compressed_size
;
245 actual_end
>= PAGE_CACHE_SIZE
||
246 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
248 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
250 data_len
> root
->fs_info
->max_inline
) {
254 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
255 aligned_end
, start
, &hint_byte
);
258 if (isize
> actual_end
)
259 inline_len
= min_t(u64
, isize
, actual_end
);
260 ret
= insert_inline_extent(trans
, root
, inode
, start
,
261 inline_len
, compressed_size
,
264 btrfs_drop_extent_cache(inode
, start
, aligned_end
, 0);
268 struct async_extent
{
273 unsigned long nr_pages
;
274 struct list_head list
;
279 struct btrfs_root
*root
;
280 struct page
*locked_page
;
283 struct list_head extents
;
284 struct btrfs_work work
;
287 static noinline
int add_async_extent(struct async_cow
*cow
,
288 u64 start
, u64 ram_size
,
291 unsigned long nr_pages
)
293 struct async_extent
*async_extent
;
295 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
296 async_extent
->start
= start
;
297 async_extent
->ram_size
= ram_size
;
298 async_extent
->compressed_size
= compressed_size
;
299 async_extent
->pages
= pages
;
300 async_extent
->nr_pages
= nr_pages
;
301 list_add_tail(&async_extent
->list
, &cow
->extents
);
306 * we create compressed extents in two phases. The first
307 * phase compresses a range of pages that have already been
308 * locked (both pages and state bits are locked).
310 * This is done inside an ordered work queue, and the compression
311 * is spread across many cpus. The actual IO submission is step
312 * two, and the ordered work queue takes care of making sure that
313 * happens in the same order things were put onto the queue by
314 * writepages and friends.
316 * If this code finds it can't get good compression, it puts an
317 * entry onto the work queue to write the uncompressed bytes. This
318 * makes sure that both compressed inodes and uncompressed inodes
319 * are written in the same order that pdflush sent them down.
321 static noinline
int compress_file_range(struct inode
*inode
,
322 struct page
*locked_page
,
324 struct async_cow
*async_cow
,
327 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
328 struct btrfs_trans_handle
*trans
;
332 u64 blocksize
= root
->sectorsize
;
334 u64 isize
= i_size_read(inode
);
336 struct page
**pages
= NULL
;
337 unsigned long nr_pages
;
338 unsigned long nr_pages_ret
= 0;
339 unsigned long total_compressed
= 0;
340 unsigned long total_in
= 0;
341 unsigned long max_compressed
= 128 * 1024;
342 unsigned long max_uncompressed
= 128 * 1024;
348 actual_end
= min_t(u64
, isize
, end
+ 1);
351 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
352 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
354 total_compressed
= actual_end
- start
;
356 /* we want to make sure that amount of ram required to uncompress
357 * an extent is reasonable, so we limit the total size in ram
358 * of a compressed extent to 128k. This is a crucial number
359 * because it also controls how easily we can spread reads across
360 * cpus for decompression.
362 * We also want to make sure the amount of IO required to do
363 * a random read is reasonably small, so we limit the size of
364 * a compressed extent to 128k.
366 total_compressed
= min(total_compressed
, max_uncompressed
);
367 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
368 num_bytes
= max(blocksize
, num_bytes
);
369 disk_num_bytes
= num_bytes
;
374 * we do compression for mount -o compress and when the
375 * inode has not been flagged as nocompress. This flag can
376 * change at any time if we discover bad compression ratios.
378 if (!btrfs_test_flag(inode
, NOCOMPRESS
) &&
379 btrfs_test_opt(root
, COMPRESS
)) {
381 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
383 ret
= btrfs_zlib_compress_pages(inode
->i_mapping
, start
,
384 total_compressed
, pages
,
385 nr_pages
, &nr_pages_ret
,
391 unsigned long offset
= total_compressed
&
392 (PAGE_CACHE_SIZE
- 1);
393 struct page
*page
= pages
[nr_pages_ret
- 1];
396 /* zero the tail end of the last page, we might be
397 * sending it down to disk
400 kaddr
= kmap_atomic(page
, KM_USER0
);
401 memset(kaddr
+ offset
, 0,
402 PAGE_CACHE_SIZE
- offset
);
403 kunmap_atomic(kaddr
, KM_USER0
);
409 trans
= btrfs_join_transaction(root
, 1);
411 btrfs_set_trans_block_group(trans
, inode
);
413 /* lets try to make an inline extent */
414 if (ret
|| total_in
< (actual_end
- start
)) {
415 /* we didn't compress the entire range, try
416 * to make an uncompressed inline extent.
418 ret
= cow_file_range_inline(trans
, root
, inode
,
419 start
, end
, 0, NULL
);
421 /* try making a compressed inline extent */
422 ret
= cow_file_range_inline(trans
, root
, inode
,
424 total_compressed
, pages
);
426 btrfs_end_transaction(trans
, root
);
429 * inline extent creation worked, we don't need
430 * to create any more async work items. Unlock
431 * and free up our temp pages.
433 extent_clear_unlock_delalloc(inode
,
434 &BTRFS_I(inode
)->io_tree
,
435 start
, end
, NULL
, 1, 0,
444 * we aren't doing an inline extent round the compressed size
445 * up to a block size boundary so the allocator does sane
448 total_compressed
= (total_compressed
+ blocksize
- 1) &
452 * one last check to make sure the compression is really a
453 * win, compare the page count read with the blocks on disk
455 total_in
= (total_in
+ PAGE_CACHE_SIZE
- 1) &
456 ~(PAGE_CACHE_SIZE
- 1);
457 if (total_compressed
>= total_in
) {
460 disk_num_bytes
= total_compressed
;
461 num_bytes
= total_in
;
464 if (!will_compress
&& pages
) {
466 * the compression code ran but failed to make things smaller,
467 * free any pages it allocated and our page pointer array
469 for (i
= 0; i
< nr_pages_ret
; i
++) {
470 WARN_ON(pages
[i
]->mapping
);
471 page_cache_release(pages
[i
]);
475 total_compressed
= 0;
478 /* flag the file so we don't compress in the future */
479 btrfs_set_flag(inode
, NOCOMPRESS
);
484 /* the async work queues will take care of doing actual
485 * allocation on disk for these compressed pages,
486 * and will submit them to the elevator.
488 add_async_extent(async_cow
, start
, num_bytes
,
489 total_compressed
, pages
, nr_pages_ret
);
491 if (start
+ num_bytes
< end
&& start
+ num_bytes
< actual_end
) {
499 * No compression, but we still need to write the pages in
500 * the file we've been given so far. redirty the locked
501 * page if it corresponds to our extent and set things up
502 * for the async work queue to run cow_file_range to do
503 * the normal delalloc dance
505 if (page_offset(locked_page
) >= start
&&
506 page_offset(locked_page
) <= end
) {
507 __set_page_dirty_nobuffers(locked_page
);
508 /* unlocked later on in the async handlers */
510 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0);
518 for (i
= 0; i
< nr_pages_ret
; i
++) {
519 WARN_ON(pages
[i
]->mapping
);
520 page_cache_release(pages
[i
]);
528 * phase two of compressed writeback. This is the ordered portion
529 * of the code, which only gets called in the order the work was
530 * queued. We walk all the async extents created by compress_file_range
531 * and send them down to the disk.
533 static noinline
int submit_compressed_extents(struct inode
*inode
,
534 struct async_cow
*async_cow
)
536 struct async_extent
*async_extent
;
538 struct btrfs_trans_handle
*trans
;
539 struct btrfs_key ins
;
540 struct extent_map
*em
;
541 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
542 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
543 struct extent_io_tree
*io_tree
;
546 if (list_empty(&async_cow
->extents
))
549 trans
= btrfs_join_transaction(root
, 1);
551 while (!list_empty(&async_cow
->extents
)) {
552 async_extent
= list_entry(async_cow
->extents
.next
,
553 struct async_extent
, list
);
554 list_del(&async_extent
->list
);
556 io_tree
= &BTRFS_I(inode
)->io_tree
;
558 /* did the compression code fall back to uncompressed IO? */
559 if (!async_extent
->pages
) {
560 int page_started
= 0;
561 unsigned long nr_written
= 0;
563 lock_extent(io_tree
, async_extent
->start
,
564 async_extent
->start
+
565 async_extent
->ram_size
- 1, GFP_NOFS
);
567 /* allocate blocks */
568 cow_file_range(inode
, async_cow
->locked_page
,
570 async_extent
->start
+
571 async_extent
->ram_size
- 1,
572 &page_started
, &nr_written
, 0);
575 * if page_started, cow_file_range inserted an
576 * inline extent and took care of all the unlocking
577 * and IO for us. Otherwise, we need to submit
578 * all those pages down to the drive.
581 extent_write_locked_range(io_tree
,
582 inode
, async_extent
->start
,
583 async_extent
->start
+
584 async_extent
->ram_size
- 1,
592 lock_extent(io_tree
, async_extent
->start
,
593 async_extent
->start
+ async_extent
->ram_size
- 1,
596 * here we're doing allocation and writeback of the
599 btrfs_drop_extent_cache(inode
, async_extent
->start
,
600 async_extent
->start
+
601 async_extent
->ram_size
- 1, 0);
603 ret
= btrfs_reserve_extent(trans
, root
,
604 async_extent
->compressed_size
,
605 async_extent
->compressed_size
,
609 em
= alloc_extent_map(GFP_NOFS
);
610 em
->start
= async_extent
->start
;
611 em
->len
= async_extent
->ram_size
;
612 em
->orig_start
= em
->start
;
614 em
->block_start
= ins
.objectid
;
615 em
->block_len
= ins
.offset
;
616 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
617 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
618 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
621 spin_lock(&em_tree
->lock
);
622 ret
= add_extent_mapping(em_tree
, em
);
623 spin_unlock(&em_tree
->lock
);
624 if (ret
!= -EEXIST
) {
628 btrfs_drop_extent_cache(inode
, async_extent
->start
,
629 async_extent
->start
+
630 async_extent
->ram_size
- 1, 0);
633 ret
= btrfs_add_ordered_extent(inode
, async_extent
->start
,
635 async_extent
->ram_size
,
637 BTRFS_ORDERED_COMPRESSED
);
640 btrfs_end_transaction(trans
, root
);
643 * clear dirty, set writeback and unlock the pages.
645 extent_clear_unlock_delalloc(inode
,
646 &BTRFS_I(inode
)->io_tree
,
648 async_extent
->start
+
649 async_extent
->ram_size
- 1,
650 NULL
, 1, 1, 0, 1, 1, 0);
652 ret
= btrfs_submit_compressed_write(inode
,
654 async_extent
->ram_size
,
656 ins
.offset
, async_extent
->pages
,
657 async_extent
->nr_pages
);
660 trans
= btrfs_join_transaction(root
, 1);
661 alloc_hint
= ins
.objectid
+ ins
.offset
;
666 btrfs_end_transaction(trans
, root
);
671 * when extent_io.c finds a delayed allocation range in the file,
672 * the call backs end up in this code. The basic idea is to
673 * allocate extents on disk for the range, and create ordered data structs
674 * in ram to track those extents.
676 * locked_page is the page that writepage had locked already. We use
677 * it to make sure we don't do extra locks or unlocks.
679 * *page_started is set to one if we unlock locked_page and do everything
680 * required to start IO on it. It may be clean and already done with
683 static noinline
int cow_file_range(struct inode
*inode
,
684 struct page
*locked_page
,
685 u64 start
, u64 end
, int *page_started
,
686 unsigned long *nr_written
,
689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
690 struct btrfs_trans_handle
*trans
;
693 unsigned long ram_size
;
696 u64 blocksize
= root
->sectorsize
;
698 u64 isize
= i_size_read(inode
);
699 struct btrfs_key ins
;
700 struct extent_map
*em
;
701 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
704 trans
= btrfs_join_transaction(root
, 1);
706 btrfs_set_trans_block_group(trans
, inode
);
708 actual_end
= min_t(u64
, isize
, end
+ 1);
710 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
711 num_bytes
= max(blocksize
, num_bytes
);
712 disk_num_bytes
= num_bytes
;
716 /* lets try to make an inline extent */
717 ret
= cow_file_range_inline(trans
, root
, inode
,
718 start
, end
, 0, NULL
);
720 extent_clear_unlock_delalloc(inode
,
721 &BTRFS_I(inode
)->io_tree
,
722 start
, end
, NULL
, 1, 1,
724 *nr_written
= *nr_written
+
725 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
732 BUG_ON(disk_num_bytes
>
733 btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
735 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
737 while (disk_num_bytes
> 0) {
738 cur_alloc_size
= min(disk_num_bytes
, root
->fs_info
->max_extent
);
739 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
740 root
->sectorsize
, 0, alloc_hint
,
744 em
= alloc_extent_map(GFP_NOFS
);
746 em
->orig_start
= em
->start
;
748 ram_size
= ins
.offset
;
749 em
->len
= ins
.offset
;
751 em
->block_start
= ins
.objectid
;
752 em
->block_len
= ins
.offset
;
753 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
754 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
757 spin_lock(&em_tree
->lock
);
758 ret
= add_extent_mapping(em_tree
, em
);
759 spin_unlock(&em_tree
->lock
);
760 if (ret
!= -EEXIST
) {
764 btrfs_drop_extent_cache(inode
, start
,
765 start
+ ram_size
- 1, 0);
768 cur_alloc_size
= ins
.offset
;
769 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
770 ram_size
, cur_alloc_size
, 0);
773 if (root
->root_key
.objectid
==
774 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
775 ret
= btrfs_reloc_clone_csums(inode
, start
,
780 if (disk_num_bytes
< cur_alloc_size
)
783 /* we're not doing compressed IO, don't unlock the first
784 * page (which the caller expects to stay locked), don't
785 * clear any dirty bits and don't set any writeback bits
787 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
788 start
, start
+ ram_size
- 1,
789 locked_page
, unlock
, 1,
791 disk_num_bytes
-= cur_alloc_size
;
792 num_bytes
-= cur_alloc_size
;
793 alloc_hint
= ins
.objectid
+ ins
.offset
;
794 start
+= cur_alloc_size
;
798 btrfs_end_transaction(trans
, root
);
804 * work queue call back to started compression on a file and pages
806 static noinline
void async_cow_start(struct btrfs_work
*work
)
808 struct async_cow
*async_cow
;
810 async_cow
= container_of(work
, struct async_cow
, work
);
812 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
813 async_cow
->start
, async_cow
->end
, async_cow
,
816 async_cow
->inode
= NULL
;
820 * work queue call back to submit previously compressed pages
822 static noinline
void async_cow_submit(struct btrfs_work
*work
)
824 struct async_cow
*async_cow
;
825 struct btrfs_root
*root
;
826 unsigned long nr_pages
;
828 async_cow
= container_of(work
, struct async_cow
, work
);
830 root
= async_cow
->root
;
831 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
834 atomic_sub(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
836 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
838 waitqueue_active(&root
->fs_info
->async_submit_wait
))
839 wake_up(&root
->fs_info
->async_submit_wait
);
841 if (async_cow
->inode
)
842 submit_compressed_extents(async_cow
->inode
, async_cow
);
845 static noinline
void async_cow_free(struct btrfs_work
*work
)
847 struct async_cow
*async_cow
;
848 async_cow
= container_of(work
, struct async_cow
, work
);
852 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
853 u64 start
, u64 end
, int *page_started
,
854 unsigned long *nr_written
)
856 struct async_cow
*async_cow
;
857 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
858 unsigned long nr_pages
;
860 int limit
= 10 * 1024 * 1042;
862 if (!btrfs_test_opt(root
, COMPRESS
)) {
863 return cow_file_range(inode
, locked_page
, start
, end
,
864 page_started
, nr_written
, 1);
867 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
|
868 EXTENT_DELALLOC
, 1, 0, GFP_NOFS
);
869 while (start
< end
) {
870 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
871 async_cow
->inode
= inode
;
872 async_cow
->root
= root
;
873 async_cow
->locked_page
= locked_page
;
874 async_cow
->start
= start
;
876 if (btrfs_test_flag(inode
, NOCOMPRESS
))
879 cur_end
= min(end
, start
+ 512 * 1024 - 1);
881 async_cow
->end
= cur_end
;
882 INIT_LIST_HEAD(&async_cow
->extents
);
884 async_cow
->work
.func
= async_cow_start
;
885 async_cow
->work
.ordered_func
= async_cow_submit
;
886 async_cow
->work
.ordered_free
= async_cow_free
;
887 async_cow
->work
.flags
= 0;
889 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
891 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
893 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
896 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
897 wait_event(root
->fs_info
->async_submit_wait
,
898 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
902 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
903 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
904 wait_event(root
->fs_info
->async_submit_wait
,
905 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
909 *nr_written
+= nr_pages
;
916 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
917 u64 bytenr
, u64 num_bytes
)
920 struct btrfs_ordered_sum
*sums
;
923 ret
= btrfs_lookup_csums_range(root
, bytenr
, bytenr
+ num_bytes
- 1,
925 if (ret
== 0 && list_empty(&list
))
928 while (!list_empty(&list
)) {
929 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
930 list_del(&sums
->list
);
937 * when nowcow writeback call back. This checks for snapshots or COW copies
938 * of the extents that exist in the file, and COWs the file as required.
940 * If no cow copies or snapshots exist, we write directly to the existing
943 static int run_delalloc_nocow(struct inode
*inode
, struct page
*locked_page
,
944 u64 start
, u64 end
, int *page_started
, int force
,
945 unsigned long *nr_written
)
947 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
948 struct btrfs_trans_handle
*trans
;
949 struct extent_buffer
*leaf
;
950 struct btrfs_path
*path
;
951 struct btrfs_file_extent_item
*fi
;
952 struct btrfs_key found_key
;
964 path
= btrfs_alloc_path();
966 trans
= btrfs_join_transaction(root
, 1);
972 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
975 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
976 leaf
= path
->nodes
[0];
977 btrfs_item_key_to_cpu(leaf
, &found_key
,
979 if (found_key
.objectid
== inode
->i_ino
&&
980 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
985 leaf
= path
->nodes
[0];
986 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
987 ret
= btrfs_next_leaf(root
, path
);
992 leaf
= path
->nodes
[0];
998 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1000 if (found_key
.objectid
> inode
->i_ino
||
1001 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1002 found_key
.offset
> end
)
1005 if (found_key
.offset
> cur_offset
) {
1006 extent_end
= found_key
.offset
;
1010 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1011 struct btrfs_file_extent_item
);
1012 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1014 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1015 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1016 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1017 extent_end
= found_key
.offset
+
1018 btrfs_file_extent_num_bytes(leaf
, fi
);
1019 if (extent_end
<= start
) {
1023 if (disk_bytenr
== 0)
1025 if (btrfs_file_extent_compression(leaf
, fi
) ||
1026 btrfs_file_extent_encryption(leaf
, fi
) ||
1027 btrfs_file_extent_other_encoding(leaf
, fi
))
1029 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1031 if (btrfs_extent_readonly(root
, disk_bytenr
))
1033 if (btrfs_cross_ref_exist(trans
, root
, inode
->i_ino
,
1036 disk_bytenr
+= btrfs_file_extent_offset(leaf
, fi
);
1037 disk_bytenr
+= cur_offset
- found_key
.offset
;
1038 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1040 * force cow if csum exists in the range.
1041 * this ensure that csum for a given extent are
1042 * either valid or do not exist.
1044 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1047 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1048 extent_end
= found_key
.offset
+
1049 btrfs_file_extent_inline_len(leaf
, fi
);
1050 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1055 if (extent_end
<= start
) {
1060 if (cow_start
== (u64
)-1)
1061 cow_start
= cur_offset
;
1062 cur_offset
= extent_end
;
1063 if (cur_offset
> end
)
1069 btrfs_release_path(root
, path
);
1070 if (cow_start
!= (u64
)-1) {
1071 ret
= cow_file_range(inode
, locked_page
, cow_start
,
1072 found_key
.offset
- 1, page_started
,
1075 cow_start
= (u64
)-1;
1078 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1079 struct extent_map
*em
;
1080 struct extent_map_tree
*em_tree
;
1081 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1082 em
= alloc_extent_map(GFP_NOFS
);
1083 em
->start
= cur_offset
;
1084 em
->orig_start
= em
->start
;
1085 em
->len
= num_bytes
;
1086 em
->block_len
= num_bytes
;
1087 em
->block_start
= disk_bytenr
;
1088 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1089 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1091 spin_lock(&em_tree
->lock
);
1092 ret
= add_extent_mapping(em_tree
, em
);
1093 spin_unlock(&em_tree
->lock
);
1094 if (ret
!= -EEXIST
) {
1095 free_extent_map(em
);
1098 btrfs_drop_extent_cache(inode
, em
->start
,
1099 em
->start
+ em
->len
- 1, 0);
1101 type
= BTRFS_ORDERED_PREALLOC
;
1103 type
= BTRFS_ORDERED_NOCOW
;
1106 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1107 num_bytes
, num_bytes
, type
);
1110 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1111 cur_offset
, cur_offset
+ num_bytes
- 1,
1112 locked_page
, 1, 1, 1, 0, 0, 0);
1113 cur_offset
= extent_end
;
1114 if (cur_offset
> end
)
1117 btrfs_release_path(root
, path
);
1119 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1120 cow_start
= cur_offset
;
1121 if (cow_start
!= (u64
)-1) {
1122 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1123 page_started
, nr_written
, 1);
1127 ret
= btrfs_end_transaction(trans
, root
);
1129 btrfs_free_path(path
);
1134 * extent_io.c call back to do delayed allocation processing
1136 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1137 u64 start
, u64 end
, int *page_started
,
1138 unsigned long *nr_written
)
1142 if (btrfs_test_flag(inode
, NODATACOW
))
1143 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1144 page_started
, 1, nr_written
);
1145 else if (btrfs_test_flag(inode
, PREALLOC
))
1146 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1147 page_started
, 0, nr_written
);
1149 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1150 page_started
, nr_written
);
1156 * extent_io.c set_bit_hook, used to track delayed allocation
1157 * bytes in this file, and to maintain the list of inodes that
1158 * have pending delalloc work to be done.
1160 static int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
1161 unsigned long old
, unsigned long bits
)
1164 * set_bit and clear bit hooks normally require _irqsave/restore
1165 * but in this case, we are only testeing for the DELALLOC
1166 * bit, which is only set or cleared with irqs on
1168 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
1169 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1170 spin_lock(&root
->fs_info
->delalloc_lock
);
1171 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
1172 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
1173 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1174 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1175 &root
->fs_info
->delalloc_inodes
);
1177 spin_unlock(&root
->fs_info
->delalloc_lock
);
1183 * extent_io.c clear_bit_hook, see set_bit_hook for why
1185 static int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
1186 unsigned long old
, unsigned long bits
)
1189 * set_bit and clear bit hooks normally require _irqsave/restore
1190 * but in this case, we are only testeing for the DELALLOC
1191 * bit, which is only set or cleared with irqs on
1193 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
1194 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1196 spin_lock(&root
->fs_info
->delalloc_lock
);
1197 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
1198 printk(KERN_INFO
"btrfs warning: delalloc account "
1200 (unsigned long long)end
- start
+ 1,
1201 (unsigned long long)
1202 root
->fs_info
->delalloc_bytes
);
1203 root
->fs_info
->delalloc_bytes
= 0;
1204 BTRFS_I(inode
)->delalloc_bytes
= 0;
1206 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
1207 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
1209 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1210 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1211 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1213 spin_unlock(&root
->fs_info
->delalloc_lock
);
1219 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1220 * we don't create bios that span stripes or chunks
1222 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1223 size_t size
, struct bio
*bio
,
1224 unsigned long bio_flags
)
1226 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1227 struct btrfs_mapping_tree
*map_tree
;
1228 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1233 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1236 length
= bio
->bi_size
;
1237 map_tree
= &root
->fs_info
->mapping_tree
;
1238 map_length
= length
;
1239 ret
= btrfs_map_block(map_tree
, READ
, logical
,
1240 &map_length
, NULL
, 0);
1242 if (map_length
< length
+ size
)
1248 * in order to insert checksums into the metadata in large chunks,
1249 * we wait until bio submission time. All the pages in the bio are
1250 * checksummed and sums are attached onto the ordered extent record.
1252 * At IO completion time the cums attached on the ordered extent record
1253 * are inserted into the btree
1255 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1256 struct bio
*bio
, int mirror_num
,
1257 unsigned long bio_flags
)
1259 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1262 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1268 * in order to insert checksums into the metadata in large chunks,
1269 * we wait until bio submission time. All the pages in the bio are
1270 * checksummed and sums are attached onto the ordered extent record.
1272 * At IO completion time the cums attached on the ordered extent record
1273 * are inserted into the btree
1275 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1276 int mirror_num
, unsigned long bio_flags
)
1278 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1279 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1283 * extent_io.c submission hook. This does the right thing for csum calculation
1284 * on write, or reading the csums from the tree before a read
1286 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1287 int mirror_num
, unsigned long bio_flags
)
1289 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1293 skip_sum
= btrfs_test_flag(inode
, NODATASUM
);
1295 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
1298 if (!(rw
& (1 << BIO_RW
))) {
1299 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1300 return btrfs_submit_compressed_read(inode
, bio
,
1301 mirror_num
, bio_flags
);
1302 } else if (!skip_sum
)
1303 btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1305 } else if (!skip_sum
) {
1306 /* csum items have already been cloned */
1307 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1309 /* we're doing a write, do the async checksumming */
1310 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1311 inode
, rw
, bio
, mirror_num
,
1312 bio_flags
, __btrfs_submit_bio_start
,
1313 __btrfs_submit_bio_done
);
1317 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1321 * given a list of ordered sums record them in the inode. This happens
1322 * at IO completion time based on sums calculated at bio submission time.
1324 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1325 struct inode
*inode
, u64 file_offset
,
1326 struct list_head
*list
)
1328 struct list_head
*cur
;
1329 struct btrfs_ordered_sum
*sum
;
1331 btrfs_set_trans_block_group(trans
, inode
);
1332 list_for_each(cur
, list
) {
1333 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
1334 btrfs_csum_file_blocks(trans
,
1335 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1340 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
)
1342 if ((end
& (PAGE_CACHE_SIZE
- 1)) == 0)
1344 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1348 /* see btrfs_writepage_start_hook for details on why this is required */
1349 struct btrfs_writepage_fixup
{
1351 struct btrfs_work work
;
1354 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1356 struct btrfs_writepage_fixup
*fixup
;
1357 struct btrfs_ordered_extent
*ordered
;
1359 struct inode
*inode
;
1363 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1367 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1368 ClearPageChecked(page
);
1372 inode
= page
->mapping
->host
;
1373 page_start
= page_offset(page
);
1374 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1376 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
1378 /* already ordered? We're done */
1379 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1380 EXTENT_ORDERED
, 0)) {
1384 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1386 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
1387 page_end
, GFP_NOFS
);
1389 btrfs_start_ordered_extent(inode
, ordered
, 1);
1393 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
1394 ClearPageChecked(page
);
1396 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
1399 page_cache_release(page
);
1403 * There are a few paths in the higher layers of the kernel that directly
1404 * set the page dirty bit without asking the filesystem if it is a
1405 * good idea. This causes problems because we want to make sure COW
1406 * properly happens and the data=ordered rules are followed.
1408 * In our case any range that doesn't have the ORDERED bit set
1409 * hasn't been properly setup for IO. We kick off an async process
1410 * to fix it up. The async helper will wait for ordered extents, set
1411 * the delalloc bit and make it safe to write the page.
1413 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1415 struct inode
*inode
= page
->mapping
->host
;
1416 struct btrfs_writepage_fixup
*fixup
;
1417 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1420 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1425 if (PageChecked(page
))
1428 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1432 SetPageChecked(page
);
1433 page_cache_get(page
);
1434 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1436 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1440 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1441 struct inode
*inode
, u64 file_pos
,
1442 u64 disk_bytenr
, u64 disk_num_bytes
,
1443 u64 num_bytes
, u64 ram_bytes
,
1444 u8 compression
, u8 encryption
,
1445 u16 other_encoding
, int extent_type
)
1447 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1448 struct btrfs_file_extent_item
*fi
;
1449 struct btrfs_path
*path
;
1450 struct extent_buffer
*leaf
;
1451 struct btrfs_key ins
;
1455 path
= btrfs_alloc_path();
1458 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1459 file_pos
+ num_bytes
, file_pos
, &hint
);
1462 ins
.objectid
= inode
->i_ino
;
1463 ins
.offset
= file_pos
;
1464 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1465 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1467 leaf
= path
->nodes
[0];
1468 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1469 struct btrfs_file_extent_item
);
1470 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1471 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1472 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1473 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1474 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1475 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1476 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1477 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1478 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1479 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1480 btrfs_mark_buffer_dirty(leaf
);
1482 inode_add_bytes(inode
, num_bytes
);
1483 btrfs_drop_extent_cache(inode
, file_pos
, file_pos
+ num_bytes
- 1, 0);
1485 ins
.objectid
= disk_bytenr
;
1486 ins
.offset
= disk_num_bytes
;
1487 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1488 ret
= btrfs_alloc_reserved_extent(trans
, root
, leaf
->start
,
1489 root
->root_key
.objectid
,
1490 trans
->transid
, inode
->i_ino
, &ins
);
1493 btrfs_free_path(path
);
1497 /* as ordered data IO finishes, this gets called so we can finish
1498 * an ordered extent if the range of bytes in the file it covers are
1501 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
1503 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1504 struct btrfs_trans_handle
*trans
;
1505 struct btrfs_ordered_extent
*ordered_extent
;
1506 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1510 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
1514 trans
= btrfs_join_transaction(root
, 1);
1516 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
1517 BUG_ON(!ordered_extent
);
1518 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
))
1521 lock_extent(io_tree
, ordered_extent
->file_offset
,
1522 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1525 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
1527 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
1529 ret
= btrfs_mark_extent_written(trans
, root
, inode
,
1530 ordered_extent
->file_offset
,
1531 ordered_extent
->file_offset
+
1532 ordered_extent
->len
);
1535 ret
= insert_reserved_file_extent(trans
, inode
,
1536 ordered_extent
->file_offset
,
1537 ordered_extent
->start
,
1538 ordered_extent
->disk_len
,
1539 ordered_extent
->len
,
1540 ordered_extent
->len
,
1542 BTRFS_FILE_EXTENT_REG
);
1545 unlock_extent(io_tree
, ordered_extent
->file_offset
,
1546 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
1549 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
1550 &ordered_extent
->list
);
1552 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1553 btrfs_ordered_update_i_size(inode
, ordered_extent
);
1554 btrfs_update_inode(trans
, root
, inode
);
1555 btrfs_remove_ordered_extent(inode
, ordered_extent
);
1556 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1559 btrfs_put_ordered_extent(ordered_extent
);
1560 /* once for the tree */
1561 btrfs_put_ordered_extent(ordered_extent
);
1563 btrfs_end_transaction(trans
, root
);
1567 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1568 struct extent_state
*state
, int uptodate
)
1570 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
1574 * When IO fails, either with EIO or csum verification fails, we
1575 * try other mirrors that might have a good copy of the data. This
1576 * io_failure_record is used to record state as we go through all the
1577 * mirrors. If another mirror has good data, the page is set up to date
1578 * and things continue. If a good mirror can't be found, the original
1579 * bio end_io callback is called to indicate things have failed.
1581 struct io_failure_record
{
1586 unsigned long bio_flags
;
1590 static int btrfs_io_failed_hook(struct bio
*failed_bio
,
1591 struct page
*page
, u64 start
, u64 end
,
1592 struct extent_state
*state
)
1594 struct io_failure_record
*failrec
= NULL
;
1596 struct extent_map
*em
;
1597 struct inode
*inode
= page
->mapping
->host
;
1598 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
1599 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1606 ret
= get_state_private(failure_tree
, start
, &private);
1608 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
1611 failrec
->start
= start
;
1612 failrec
->len
= end
- start
+ 1;
1613 failrec
->last_mirror
= 0;
1614 failrec
->bio_flags
= 0;
1616 spin_lock(&em_tree
->lock
);
1617 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
1618 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
1619 free_extent_map(em
);
1622 spin_unlock(&em_tree
->lock
);
1624 if (!em
|| IS_ERR(em
)) {
1628 logical
= start
- em
->start
;
1629 logical
= em
->block_start
+ logical
;
1630 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
1631 logical
= em
->block_start
;
1632 failrec
->bio_flags
= EXTENT_BIO_COMPRESSED
;
1634 failrec
->logical
= logical
;
1635 free_extent_map(em
);
1636 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
1637 EXTENT_DIRTY
, GFP_NOFS
);
1638 set_state_private(failure_tree
, start
,
1639 (u64
)(unsigned long)failrec
);
1641 failrec
= (struct io_failure_record
*)(unsigned long)private;
1643 num_copies
= btrfs_num_copies(
1644 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1645 failrec
->logical
, failrec
->len
);
1646 failrec
->last_mirror
++;
1648 spin_lock(&BTRFS_I(inode
)->io_tree
.lock
);
1649 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
1652 if (state
&& state
->start
!= failrec
->start
)
1654 spin_unlock(&BTRFS_I(inode
)->io_tree
.lock
);
1656 if (!state
|| failrec
->last_mirror
> num_copies
) {
1657 set_state_private(failure_tree
, failrec
->start
, 0);
1658 clear_extent_bits(failure_tree
, failrec
->start
,
1659 failrec
->start
+ failrec
->len
- 1,
1660 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
1664 bio
= bio_alloc(GFP_NOFS
, 1);
1665 bio
->bi_private
= state
;
1666 bio
->bi_end_io
= failed_bio
->bi_end_io
;
1667 bio
->bi_sector
= failrec
->logical
>> 9;
1668 bio
->bi_bdev
= failed_bio
->bi_bdev
;
1671 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
1672 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
1677 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
1678 failrec
->last_mirror
,
1679 failrec
->bio_flags
);
1684 * each time an IO finishes, we do a fast check in the IO failure tree
1685 * to see if we need to process or clean up an io_failure_record
1687 static int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
1690 u64 private_failure
;
1691 struct io_failure_record
*failure
;
1695 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
1696 (u64
)-1, 1, EXTENT_DIRTY
)) {
1697 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1698 start
, &private_failure
);
1700 failure
= (struct io_failure_record
*)(unsigned long)
1702 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
1704 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
1706 failure
->start
+ failure
->len
- 1,
1707 EXTENT_DIRTY
| EXTENT_LOCKED
,
1716 * when reads are done, we need to check csums to verify the data is correct
1717 * if there's a match, we allow the bio to finish. If not, we go through
1718 * the io_failure_record routines to find good copies
1720 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
1721 struct extent_state
*state
)
1723 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
1724 struct inode
*inode
= page
->mapping
->host
;
1725 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1727 u64
private = ~(u32
)0;
1729 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1731 unsigned long flags
;
1733 if (PageChecked(page
)) {
1734 ClearPageChecked(page
);
1737 if (btrfs_test_flag(inode
, NODATASUM
))
1740 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1741 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1)) {
1742 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
1747 if (state
&& state
->start
== start
) {
1748 private = state
->private;
1751 ret
= get_state_private(io_tree
, start
, &private);
1753 local_irq_save(flags
);
1754 kaddr
= kmap_atomic(page
, KM_IRQ0
);
1758 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
1759 btrfs_csum_final(csum
, (char *)&csum
);
1760 if (csum
!= private)
1763 kunmap_atomic(kaddr
, KM_IRQ0
);
1764 local_irq_restore(flags
);
1766 /* if the io failure tree for this inode is non-empty,
1767 * check to see if we've recovered from a failed IO
1769 btrfs_clean_io_failures(inode
, start
);
1773 printk(KERN_INFO
"btrfs csum failed ino %lu off %llu csum %u "
1774 "private %llu\n", page
->mapping
->host
->i_ino
,
1775 (unsigned long long)start
, csum
,
1776 (unsigned long long)private);
1777 memset(kaddr
+ offset
, 1, end
- start
+ 1);
1778 flush_dcache_page(page
);
1779 kunmap_atomic(kaddr
, KM_IRQ0
);
1780 local_irq_restore(flags
);
1787 * This creates an orphan entry for the given inode in case something goes
1788 * wrong in the middle of an unlink/truncate.
1790 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
1792 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1795 spin_lock(&root
->list_lock
);
1797 /* already on the orphan list, we're good */
1798 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
1799 spin_unlock(&root
->list_lock
);
1803 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
1805 spin_unlock(&root
->list_lock
);
1808 * insert an orphan item to track this unlinked/truncated file
1810 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
1816 * We have done the truncate/delete so we can go ahead and remove the orphan
1817 * item for this particular inode.
1819 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
1821 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1824 spin_lock(&root
->list_lock
);
1826 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
1827 spin_unlock(&root
->list_lock
);
1831 list_del_init(&BTRFS_I(inode
)->i_orphan
);
1833 spin_unlock(&root
->list_lock
);
1837 spin_unlock(&root
->list_lock
);
1839 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
1845 * this cleans up any orphans that may be left on the list from the last use
1848 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
1850 struct btrfs_path
*path
;
1851 struct extent_buffer
*leaf
;
1852 struct btrfs_item
*item
;
1853 struct btrfs_key key
, found_key
;
1854 struct btrfs_trans_handle
*trans
;
1855 struct inode
*inode
;
1856 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
1858 path
= btrfs_alloc_path();
1863 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
1864 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
1865 key
.offset
= (u64
)-1;
1869 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1871 printk(KERN_ERR
"Error searching slot for orphan: %d"
1877 * if ret == 0 means we found what we were searching for, which
1878 * is weird, but possible, so only screw with path if we didnt
1879 * find the key and see if we have stuff that matches
1882 if (path
->slots
[0] == 0)
1887 /* pull out the item */
1888 leaf
= path
->nodes
[0];
1889 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
1890 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1892 /* make sure the item matches what we want */
1893 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
1895 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
1898 /* release the path since we're done with it */
1899 btrfs_release_path(root
, path
);
1902 * this is where we are basically btrfs_lookup, without the
1903 * crossing root thing. we store the inode number in the
1904 * offset of the orphan item.
1906 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
1907 found_key
.offset
, root
);
1911 if (inode
->i_state
& I_NEW
) {
1912 BTRFS_I(inode
)->root
= root
;
1914 /* have to set the location manually */
1915 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
1916 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
1917 BTRFS_I(inode
)->location
.offset
= 0;
1919 btrfs_read_locked_inode(inode
);
1920 unlock_new_inode(inode
);
1924 * add this inode to the orphan list so btrfs_orphan_del does
1925 * the proper thing when we hit it
1927 spin_lock(&root
->list_lock
);
1928 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
1929 spin_unlock(&root
->list_lock
);
1932 * if this is a bad inode, means we actually succeeded in
1933 * removing the inode, but not the orphan record, which means
1934 * we need to manually delete the orphan since iput will just
1935 * do a destroy_inode
1937 if (is_bad_inode(inode
)) {
1938 trans
= btrfs_start_transaction(root
, 1);
1939 btrfs_orphan_del(trans
, inode
);
1940 btrfs_end_transaction(trans
, root
);
1945 /* if we have links, this was a truncate, lets do that */
1946 if (inode
->i_nlink
) {
1948 btrfs_truncate(inode
);
1953 /* this will do delete_inode and everything for us */
1958 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
1960 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
1962 btrfs_free_path(path
);
1966 * read an inode from the btree into the in-memory inode
1968 void btrfs_read_locked_inode(struct inode
*inode
)
1970 struct btrfs_path
*path
;
1971 struct extent_buffer
*leaf
;
1972 struct btrfs_inode_item
*inode_item
;
1973 struct btrfs_timespec
*tspec
;
1974 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1975 struct btrfs_key location
;
1976 u64 alloc_group_block
;
1980 path
= btrfs_alloc_path();
1982 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
1984 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
1988 leaf
= path
->nodes
[0];
1989 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1990 struct btrfs_inode_item
);
1992 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
1993 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
1994 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
1995 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
1996 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
1998 tspec
= btrfs_inode_atime(inode_item
);
1999 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2000 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2002 tspec
= btrfs_inode_mtime(inode_item
);
2003 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2004 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2006 tspec
= btrfs_inode_ctime(inode_item
);
2007 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
2008 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
2010 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
2011 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
2012 BTRFS_I(inode
)->sequence
= btrfs_inode_sequence(leaf
, inode_item
);
2013 inode
->i_generation
= BTRFS_I(inode
)->generation
;
2015 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
2017 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
2018 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
2020 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
2021 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
, 0,
2022 alloc_group_block
, 0);
2023 btrfs_free_path(path
);
2026 switch (inode
->i_mode
& S_IFMT
) {
2028 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2029 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2030 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2031 inode
->i_fop
= &btrfs_file_operations
;
2032 inode
->i_op
= &btrfs_file_inode_operations
;
2035 inode
->i_fop
= &btrfs_dir_file_operations
;
2036 if (root
== root
->fs_info
->tree_root
)
2037 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
2039 inode
->i_op
= &btrfs_dir_inode_operations
;
2042 inode
->i_op
= &btrfs_symlink_inode_operations
;
2043 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2044 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2047 init_special_inode(inode
, inode
->i_mode
, rdev
);
2053 btrfs_free_path(path
);
2054 make_bad_inode(inode
);
2058 * given a leaf and an inode, copy the inode fields into the leaf
2060 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2061 struct extent_buffer
*leaf
,
2062 struct btrfs_inode_item
*item
,
2063 struct inode
*inode
)
2065 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
2066 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
2067 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
2068 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
2069 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
2071 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
2072 inode
->i_atime
.tv_sec
);
2073 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2074 inode
->i_atime
.tv_nsec
);
2076 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2077 inode
->i_mtime
.tv_sec
);
2078 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
2079 inode
->i_mtime
.tv_nsec
);
2081 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
2082 inode
->i_ctime
.tv_sec
);
2083 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
2084 inode
->i_ctime
.tv_nsec
);
2086 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
2087 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
2088 btrfs_set_inode_sequence(leaf
, item
, BTRFS_I(inode
)->sequence
);
2089 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
2090 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
2091 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
2092 btrfs_set_inode_block_group(leaf
, item
, BTRFS_I(inode
)->block_group
);
2096 * copy everything in the in-memory inode into the btree.
2098 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
2099 struct btrfs_root
*root
, struct inode
*inode
)
2101 struct btrfs_inode_item
*inode_item
;
2102 struct btrfs_path
*path
;
2103 struct extent_buffer
*leaf
;
2106 path
= btrfs_alloc_path();
2108 ret
= btrfs_lookup_inode(trans
, root
, path
,
2109 &BTRFS_I(inode
)->location
, 1);
2116 leaf
= path
->nodes
[0];
2117 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2118 struct btrfs_inode_item
);
2120 fill_inode_item(trans
, leaf
, inode_item
, inode
);
2121 btrfs_mark_buffer_dirty(leaf
);
2122 btrfs_set_inode_last_trans(trans
, inode
);
2125 btrfs_free_path(path
);
2131 * unlink helper that gets used here in inode.c and in the tree logging
2132 * recovery code. It remove a link in a directory with a given name, and
2133 * also drops the back refs in the inode to the directory
2135 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
2136 struct btrfs_root
*root
,
2137 struct inode
*dir
, struct inode
*inode
,
2138 const char *name
, int name_len
)
2140 struct btrfs_path
*path
;
2142 struct extent_buffer
*leaf
;
2143 struct btrfs_dir_item
*di
;
2144 struct btrfs_key key
;
2147 path
= btrfs_alloc_path();
2153 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
2154 name
, name_len
, -1);
2163 leaf
= path
->nodes
[0];
2164 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
2165 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2168 btrfs_release_path(root
, path
);
2170 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
2172 dir
->i_ino
, &index
);
2174 printk(KERN_INFO
"btrfs failed to delete reference to %.*s, "
2175 "inode %lu parent %lu\n", name_len
, name
,
2176 inode
->i_ino
, dir
->i_ino
);
2180 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
2181 index
, name
, name_len
, -1);
2190 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
2191 btrfs_release_path(root
, path
);
2193 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
2195 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
2197 BTRFS_I(dir
)->log_dirty_trans
= trans
->transid
;
2199 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
2203 btrfs_free_path(path
);
2207 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
2208 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
2209 btrfs_update_inode(trans
, root
, dir
);
2210 btrfs_drop_nlink(inode
);
2211 ret
= btrfs_update_inode(trans
, root
, inode
);
2212 dir
->i_sb
->s_dirt
= 1;
2217 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2219 struct btrfs_root
*root
;
2220 struct btrfs_trans_handle
*trans
;
2221 struct inode
*inode
= dentry
->d_inode
;
2223 unsigned long nr
= 0;
2225 root
= BTRFS_I(dir
)->root
;
2227 ret
= btrfs_check_free_space(root
, 1, 1);
2231 trans
= btrfs_start_transaction(root
, 1);
2233 btrfs_set_trans_block_group(trans
, dir
);
2234 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2235 dentry
->d_name
.name
, dentry
->d_name
.len
);
2237 if (inode
->i_nlink
== 0)
2238 ret
= btrfs_orphan_add(trans
, inode
);
2240 nr
= trans
->blocks_used
;
2242 btrfs_end_transaction_throttle(trans
, root
);
2244 btrfs_btree_balance_dirty(root
, nr
);
2248 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2250 struct inode
*inode
= dentry
->d_inode
;
2253 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2254 struct btrfs_trans_handle
*trans
;
2255 unsigned long nr
= 0;
2258 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2259 * the root of a subvolume or snapshot
2261 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
||
2262 inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
2266 ret
= btrfs_check_free_space(root
, 1, 1);
2270 trans
= btrfs_start_transaction(root
, 1);
2271 btrfs_set_trans_block_group(trans
, dir
);
2273 err
= btrfs_orphan_add(trans
, inode
);
2277 /* now the directory is empty */
2278 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
2279 dentry
->d_name
.name
, dentry
->d_name
.len
);
2281 btrfs_i_size_write(inode
, 0);
2284 nr
= trans
->blocks_used
;
2285 ret
= btrfs_end_transaction_throttle(trans
, root
);
2287 btrfs_btree_balance_dirty(root
, nr
);
2296 * when truncating bytes in a file, it is possible to avoid reading
2297 * the leaves that contain only checksum items. This can be the
2298 * majority of the IO required to delete a large file, but it must
2299 * be done carefully.
2301 * The keys in the level just above the leaves are checked to make sure
2302 * the lowest key in a given leaf is a csum key, and starts at an offset
2303 * after the new size.
2305 * Then the key for the next leaf is checked to make sure it also has
2306 * a checksum item for the same file. If it does, we know our target leaf
2307 * contains only checksum items, and it can be safely freed without reading
2310 * This is just an optimization targeted at large files. It may do
2311 * nothing. It will return 0 unless things went badly.
2313 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
2314 struct btrfs_root
*root
,
2315 struct btrfs_path
*path
,
2316 struct inode
*inode
, u64 new_size
)
2318 struct btrfs_key key
;
2321 struct btrfs_key found_key
;
2322 struct btrfs_key other_key
;
2323 struct btrfs_leaf_ref
*ref
;
2327 path
->lowest_level
= 1;
2328 key
.objectid
= inode
->i_ino
;
2329 key
.type
= BTRFS_CSUM_ITEM_KEY
;
2330 key
.offset
= new_size
;
2332 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2336 if (path
->nodes
[1] == NULL
) {
2341 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
2342 nritems
= btrfs_header_nritems(path
->nodes
[1]);
2347 if (path
->slots
[1] >= nritems
)
2350 /* did we find a key greater than anything we want to delete? */
2351 if (found_key
.objectid
> inode
->i_ino
||
2352 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
2355 /* we check the next key in the node to make sure the leave contains
2356 * only checksum items. This comparison doesn't work if our
2357 * leaf is the last one in the node
2359 if (path
->slots
[1] + 1 >= nritems
) {
2361 /* search forward from the last key in the node, this
2362 * will bring us into the next node in the tree
2364 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
2366 /* unlikely, but we inc below, so check to be safe */
2367 if (found_key
.offset
== (u64
)-1)
2370 /* search_forward needs a path with locks held, do the
2371 * search again for the original key. It is possible
2372 * this will race with a balance and return a path that
2373 * we could modify, but this drop is just an optimization
2374 * and is allowed to miss some leaves.
2376 btrfs_release_path(root
, path
);
2379 /* setup a max key for search_forward */
2380 other_key
.offset
= (u64
)-1;
2381 other_key
.type
= key
.type
;
2382 other_key
.objectid
= key
.objectid
;
2384 path
->keep_locks
= 1;
2385 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
2387 path
->keep_locks
= 0;
2388 if (ret
|| found_key
.objectid
!= key
.objectid
||
2389 found_key
.type
!= key
.type
) {
2394 key
.offset
= found_key
.offset
;
2395 btrfs_release_path(root
, path
);
2400 /* we know there's one more slot after us in the tree,
2401 * read that key so we can verify it is also a checksum item
2403 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
2405 if (found_key
.objectid
< inode
->i_ino
)
2408 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
2412 * if the key for the next leaf isn't a csum key from this objectid,
2413 * we can't be sure there aren't good items inside this leaf.
2416 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
2419 leaf_start
= btrfs_node_blockptr(path
->nodes
[1], path
->slots
[1]);
2420 leaf_gen
= btrfs_node_ptr_generation(path
->nodes
[1], path
->slots
[1]);
2422 * it is safe to delete this leaf, it contains only
2423 * csum items from this inode at an offset >= new_size
2425 ret
= btrfs_del_leaf(trans
, root
, path
, leaf_start
);
2428 if (root
->ref_cows
&& leaf_gen
< trans
->transid
) {
2429 ref
= btrfs_alloc_leaf_ref(root
, 0);
2431 ref
->root_gen
= root
->root_key
.offset
;
2432 ref
->bytenr
= leaf_start
;
2434 ref
->generation
= leaf_gen
;
2437 ret
= btrfs_add_leaf_ref(root
, ref
, 0);
2439 btrfs_free_leaf_ref(root
, ref
);
2445 btrfs_release_path(root
, path
);
2447 if (other_key
.objectid
== inode
->i_ino
&&
2448 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
2449 key
.offset
= other_key
.offset
;
2455 /* fixup any changes we've made to the path */
2456 path
->lowest_level
= 0;
2457 path
->keep_locks
= 0;
2458 btrfs_release_path(root
, path
);
2465 * this can truncate away extent items, csum items and directory items.
2466 * It starts at a high offset and removes keys until it can't find
2467 * any higher than new_size
2469 * csum items that cross the new i_size are truncated to the new size
2472 * min_type is the minimum key type to truncate down to. If set to 0, this
2473 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2475 noinline
int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
2476 struct btrfs_root
*root
,
2477 struct inode
*inode
,
2478 u64 new_size
, u32 min_type
)
2481 struct btrfs_path
*path
;
2482 struct btrfs_key key
;
2483 struct btrfs_key found_key
;
2485 struct extent_buffer
*leaf
;
2486 struct btrfs_file_extent_item
*fi
;
2487 u64 extent_start
= 0;
2488 u64 extent_num_bytes
= 0;
2494 int pending_del_nr
= 0;
2495 int pending_del_slot
= 0;
2496 int extent_type
= -1;
2498 u64 mask
= root
->sectorsize
- 1;
2501 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
2502 path
= btrfs_alloc_path();
2506 /* FIXME, add redo link to tree so we don't leak on crash */
2507 key
.objectid
= inode
->i_ino
;
2508 key
.offset
= (u64
)-1;
2511 btrfs_init_path(path
);
2514 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2519 /* there are no items in the tree for us to truncate, we're
2522 if (path
->slots
[0] == 0) {
2531 leaf
= path
->nodes
[0];
2532 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2533 found_type
= btrfs_key_type(&found_key
);
2536 if (found_key
.objectid
!= inode
->i_ino
)
2539 if (found_type
< min_type
)
2542 item_end
= found_key
.offset
;
2543 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
2544 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2545 struct btrfs_file_extent_item
);
2546 extent_type
= btrfs_file_extent_type(leaf
, fi
);
2547 encoding
= btrfs_file_extent_compression(leaf
, fi
);
2548 encoding
|= btrfs_file_extent_encryption(leaf
, fi
);
2549 encoding
|= btrfs_file_extent_other_encoding(leaf
, fi
);
2551 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
2553 btrfs_file_extent_num_bytes(leaf
, fi
);
2554 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
2555 item_end
+= btrfs_file_extent_inline_len(leaf
,
2560 if (item_end
< new_size
) {
2561 if (found_type
== BTRFS_DIR_ITEM_KEY
)
2562 found_type
= BTRFS_INODE_ITEM_KEY
;
2563 else if (found_type
== BTRFS_EXTENT_ITEM_KEY
)
2564 found_type
= BTRFS_EXTENT_DATA_KEY
;
2565 else if (found_type
== BTRFS_EXTENT_DATA_KEY
)
2566 found_type
= BTRFS_XATTR_ITEM_KEY
;
2567 else if (found_type
== BTRFS_XATTR_ITEM_KEY
)
2568 found_type
= BTRFS_INODE_REF_KEY
;
2569 else if (found_type
)
2573 btrfs_set_key_type(&key
, found_type
);
2576 if (found_key
.offset
>= new_size
)
2582 /* FIXME, shrink the extent if the ref count is only 1 */
2583 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
2586 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
2588 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
2589 if (!del_item
&& !encoding
) {
2590 u64 orig_num_bytes
=
2591 btrfs_file_extent_num_bytes(leaf
, fi
);
2592 extent_num_bytes
= new_size
-
2593 found_key
.offset
+ root
->sectorsize
- 1;
2594 extent_num_bytes
= extent_num_bytes
&
2595 ~((u64
)root
->sectorsize
- 1);
2596 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2598 num_dec
= (orig_num_bytes
-
2600 if (root
->ref_cows
&& extent_start
!= 0)
2601 inode_sub_bytes(inode
, num_dec
);
2602 btrfs_mark_buffer_dirty(leaf
);
2605 btrfs_file_extent_disk_num_bytes(leaf
,
2607 /* FIXME blocksize != 4096 */
2608 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
2609 if (extent_start
!= 0) {
2612 inode_sub_bytes(inode
, num_dec
);
2614 root_gen
= btrfs_header_generation(leaf
);
2615 root_owner
= btrfs_header_owner(leaf
);
2617 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
2619 * we can't truncate inline items that have had
2623 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
2624 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
2625 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
2626 u32 size
= new_size
- found_key
.offset
;
2628 if (root
->ref_cows
) {
2629 inode_sub_bytes(inode
, item_end
+ 1 -
2633 btrfs_file_extent_calc_inline_size(size
);
2634 ret
= btrfs_truncate_item(trans
, root
, path
,
2637 } else if (root
->ref_cows
) {
2638 inode_sub_bytes(inode
, item_end
+ 1 -
2644 if (!pending_del_nr
) {
2645 /* no pending yet, add ourselves */
2646 pending_del_slot
= path
->slots
[0];
2648 } else if (pending_del_nr
&&
2649 path
->slots
[0] + 1 == pending_del_slot
) {
2650 /* hop on the pending chunk */
2652 pending_del_slot
= path
->slots
[0];
2660 ret
= btrfs_free_extent(trans
, root
, extent_start
,
2662 leaf
->start
, root_owner
,
2663 root_gen
, inode
->i_ino
, 0);
2667 if (path
->slots
[0] == 0) {
2670 btrfs_release_path(root
, path
);
2675 if (pending_del_nr
&&
2676 path
->slots
[0] + 1 != pending_del_slot
) {
2677 struct btrfs_key debug
;
2679 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
2681 ret
= btrfs_del_items(trans
, root
, path
,
2686 btrfs_release_path(root
, path
);
2692 if (pending_del_nr
) {
2693 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
2696 btrfs_free_path(path
);
2697 inode
->i_sb
->s_dirt
= 1;
2702 * taken from block_truncate_page, but does cow as it zeros out
2703 * any bytes left in the last page in the file.
2705 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
2707 struct inode
*inode
= mapping
->host
;
2708 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2709 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2710 struct btrfs_ordered_extent
*ordered
;
2712 u32 blocksize
= root
->sectorsize
;
2713 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
2714 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
2720 if ((offset
& (blocksize
- 1)) == 0)
2725 page
= grab_cache_page(mapping
, index
);
2729 page_start
= page_offset(page
);
2730 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2732 if (!PageUptodate(page
)) {
2733 ret
= btrfs_readpage(NULL
, page
);
2735 if (page
->mapping
!= mapping
) {
2737 page_cache_release(page
);
2740 if (!PageUptodate(page
)) {
2745 wait_on_page_writeback(page
);
2747 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2748 set_page_extent_mapped(page
);
2750 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2752 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2754 page_cache_release(page
);
2755 btrfs_start_ordered_extent(inode
, ordered
, 1);
2756 btrfs_put_ordered_extent(ordered
);
2760 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
2762 if (offset
!= PAGE_CACHE_SIZE
) {
2764 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
2765 flush_dcache_page(page
);
2768 ClearPageChecked(page
);
2769 set_page_dirty(page
);
2770 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2774 page_cache_release(page
);
2779 int btrfs_cont_expand(struct inode
*inode
, loff_t size
)
2781 struct btrfs_trans_handle
*trans
;
2782 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2783 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2784 struct extent_map
*em
;
2785 u64 mask
= root
->sectorsize
- 1;
2786 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
2787 u64 block_end
= (size
+ mask
) & ~mask
;
2793 if (size
<= hole_start
)
2796 err
= btrfs_check_free_space(root
, 1, 0);
2800 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2803 struct btrfs_ordered_extent
*ordered
;
2804 btrfs_wait_ordered_range(inode
, hole_start
,
2805 block_end
- hole_start
);
2806 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
2807 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
2810 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
2811 btrfs_put_ordered_extent(ordered
);
2814 trans
= btrfs_start_transaction(root
, 1);
2815 btrfs_set_trans_block_group(trans
, inode
);
2817 cur_offset
= hole_start
;
2819 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
2820 block_end
- cur_offset
, 0);
2821 BUG_ON(IS_ERR(em
) || !em
);
2822 last_byte
= min(extent_map_end(em
), block_end
);
2823 last_byte
= (last_byte
+ mask
) & ~mask
;
2824 if (test_bit(EXTENT_FLAG_VACANCY
, &em
->flags
)) {
2826 hole_size
= last_byte
- cur_offset
;
2827 err
= btrfs_drop_extents(trans
, root
, inode
,
2829 cur_offset
+ hole_size
,
2830 cur_offset
, &hint_byte
);
2833 err
= btrfs_insert_file_extent(trans
, root
,
2834 inode
->i_ino
, cur_offset
, 0,
2835 0, hole_size
, 0, hole_size
,
2837 btrfs_drop_extent_cache(inode
, hole_start
,
2840 free_extent_map(em
);
2841 cur_offset
= last_byte
;
2842 if (err
|| cur_offset
>= block_end
)
2846 btrfs_end_transaction(trans
, root
);
2847 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
2851 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
2853 struct inode
*inode
= dentry
->d_inode
;
2856 err
= inode_change_ok(inode
, attr
);
2860 if (S_ISREG(inode
->i_mode
) &&
2861 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
2862 err
= btrfs_cont_expand(inode
, attr
->ia_size
);
2867 err
= inode_setattr(inode
, attr
);
2869 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
2870 err
= btrfs_acl_chmod(inode
);
2874 void btrfs_delete_inode(struct inode
*inode
)
2876 struct btrfs_trans_handle
*trans
;
2877 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2881 truncate_inode_pages(&inode
->i_data
, 0);
2882 if (is_bad_inode(inode
)) {
2883 btrfs_orphan_del(NULL
, inode
);
2886 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
2888 btrfs_i_size_write(inode
, 0);
2889 trans
= btrfs_start_transaction(root
, 1);
2891 btrfs_set_trans_block_group(trans
, inode
);
2892 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, inode
->i_size
, 0);
2894 btrfs_orphan_del(NULL
, inode
);
2895 goto no_delete_lock
;
2898 btrfs_orphan_del(trans
, inode
);
2900 nr
= trans
->blocks_used
;
2903 btrfs_end_transaction(trans
, root
);
2904 btrfs_btree_balance_dirty(root
, nr
);
2908 nr
= trans
->blocks_used
;
2909 btrfs_end_transaction(trans
, root
);
2910 btrfs_btree_balance_dirty(root
, nr
);
2916 * this returns the key found in the dir entry in the location pointer.
2917 * If no dir entries were found, location->objectid is 0.
2919 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
2920 struct btrfs_key
*location
)
2922 const char *name
= dentry
->d_name
.name
;
2923 int namelen
= dentry
->d_name
.len
;
2924 struct btrfs_dir_item
*di
;
2925 struct btrfs_path
*path
;
2926 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2929 path
= btrfs_alloc_path();
2932 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
2937 if (!di
|| IS_ERR(di
))
2940 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
2942 btrfs_free_path(path
);
2945 location
->objectid
= 0;
2950 * when we hit a tree root in a directory, the btrfs part of the inode
2951 * needs to be changed to reflect the root directory of the tree root. This
2952 * is kind of like crossing a mount point.
2954 static int fixup_tree_root_location(struct btrfs_root
*root
,
2955 struct btrfs_key
*location
,
2956 struct btrfs_root
**sub_root
,
2957 struct dentry
*dentry
)
2959 struct btrfs_root_item
*ri
;
2961 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
2963 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
2966 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
2967 dentry
->d_name
.name
,
2968 dentry
->d_name
.len
);
2969 if (IS_ERR(*sub_root
))
2970 return PTR_ERR(*sub_root
);
2972 ri
= &(*sub_root
)->root_item
;
2973 location
->objectid
= btrfs_root_dirid(ri
);
2974 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2975 location
->offset
= 0;
2980 static noinline
void init_btrfs_i(struct inode
*inode
)
2982 struct btrfs_inode
*bi
= BTRFS_I(inode
);
2985 bi
->i_default_acl
= NULL
;
2990 bi
->logged_trans
= 0;
2991 bi
->delalloc_bytes
= 0;
2992 bi
->disk_i_size
= 0;
2994 bi
->index_cnt
= (u64
)-1;
2995 bi
->log_dirty_trans
= 0;
2996 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2997 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2998 inode
->i_mapping
, GFP_NOFS
);
2999 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3000 inode
->i_mapping
, GFP_NOFS
);
3001 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
3002 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3003 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3004 mutex_init(&BTRFS_I(inode
)->log_mutex
);
3007 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
3009 struct btrfs_iget_args
*args
= p
;
3010 inode
->i_ino
= args
->ino
;
3011 init_btrfs_i(inode
);
3012 BTRFS_I(inode
)->root
= args
->root
;
3016 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
3018 struct btrfs_iget_args
*args
= opaque
;
3019 return args
->ino
== inode
->i_ino
&&
3020 args
->root
== BTRFS_I(inode
)->root
;
3023 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
3024 struct btrfs_root
*root
, int wait
)
3026 struct inode
*inode
;
3027 struct btrfs_iget_args args
;
3028 args
.ino
= objectid
;
3032 inode
= ilookup5(s
, objectid
, btrfs_find_actor
,
3035 inode
= ilookup5_nowait(s
, objectid
, btrfs_find_actor
,
3041 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
3042 struct btrfs_root
*root
)
3044 struct inode
*inode
;
3045 struct btrfs_iget_args args
;
3046 args
.ino
= objectid
;
3049 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
3050 btrfs_init_locked_inode
,
3055 /* Get an inode object given its location and corresponding root.
3056 * Returns in *is_new if the inode was read from disk
3058 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
3059 struct btrfs_root
*root
, int *is_new
)
3061 struct inode
*inode
;
3063 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
3065 return ERR_PTR(-EACCES
);
3067 if (inode
->i_state
& I_NEW
) {
3068 BTRFS_I(inode
)->root
= root
;
3069 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
3070 btrfs_read_locked_inode(inode
);
3071 unlock_new_inode(inode
);
3082 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
3084 struct inode
*inode
;
3085 struct btrfs_inode
*bi
= BTRFS_I(dir
);
3086 struct btrfs_root
*root
= bi
->root
;
3087 struct btrfs_root
*sub_root
= root
;
3088 struct btrfs_key location
;
3091 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
3092 return ERR_PTR(-ENAMETOOLONG
);
3094 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
3097 return ERR_PTR(ret
);
3100 if (location
.objectid
) {
3101 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
3104 return ERR_PTR(ret
);
3106 return ERR_PTR(-ENOENT
);
3107 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, &new);
3109 return ERR_CAST(inode
);
3114 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
3115 struct nameidata
*nd
)
3117 struct inode
*inode
;
3119 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
3120 return ERR_PTR(-ENAMETOOLONG
);
3122 inode
= btrfs_lookup_dentry(dir
, dentry
);
3124 return ERR_CAST(inode
);
3126 return d_splice_alias(inode
, dentry
);
3129 static unsigned char btrfs_filetype_table
[] = {
3130 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
3133 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
3136 struct inode
*inode
= filp
->f_dentry
->d_inode
;
3137 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3138 struct btrfs_item
*item
;
3139 struct btrfs_dir_item
*di
;
3140 struct btrfs_key key
;
3141 struct btrfs_key found_key
;
3142 struct btrfs_path
*path
;
3145 struct extent_buffer
*leaf
;
3148 unsigned char d_type
;
3153 int key_type
= BTRFS_DIR_INDEX_KEY
;
3158 /* FIXME, use a real flag for deciding about the key type */
3159 if (root
->fs_info
->tree_root
== root
)
3160 key_type
= BTRFS_DIR_ITEM_KEY
;
3162 /* special case for "." */
3163 if (filp
->f_pos
== 0) {
3164 over
= filldir(dirent
, ".", 1,
3171 /* special case for .., just use the back ref */
3172 if (filp
->f_pos
== 1) {
3173 u64 pino
= parent_ino(filp
->f_path
.dentry
);
3174 over
= filldir(dirent
, "..", 2,
3180 path
= btrfs_alloc_path();
3183 btrfs_set_key_type(&key
, key_type
);
3184 key
.offset
= filp
->f_pos
;
3185 key
.objectid
= inode
->i_ino
;
3187 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3193 leaf
= path
->nodes
[0];
3194 nritems
= btrfs_header_nritems(leaf
);
3195 slot
= path
->slots
[0];
3196 if (advance
|| slot
>= nritems
) {
3197 if (slot
>= nritems
- 1) {
3198 ret
= btrfs_next_leaf(root
, path
);
3201 leaf
= path
->nodes
[0];
3202 nritems
= btrfs_header_nritems(leaf
);
3203 slot
= path
->slots
[0];
3211 item
= btrfs_item_nr(leaf
, slot
);
3212 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3214 if (found_key
.objectid
!= key
.objectid
)
3216 if (btrfs_key_type(&found_key
) != key_type
)
3218 if (found_key
.offset
< filp
->f_pos
)
3221 filp
->f_pos
= found_key
.offset
;
3223 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
3225 di_total
= btrfs_item_size(leaf
, item
);
3227 while (di_cur
< di_total
) {
3228 struct btrfs_key location
;
3230 name_len
= btrfs_dir_name_len(leaf
, di
);
3231 if (name_len
<= sizeof(tmp_name
)) {
3232 name_ptr
= tmp_name
;
3234 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
3240 read_extent_buffer(leaf
, name_ptr
,
3241 (unsigned long)(di
+ 1), name_len
);
3243 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
3244 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
3246 /* is this a reference to our own snapshot? If so
3249 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
3250 location
.objectid
== root
->root_key
.objectid
) {
3254 over
= filldir(dirent
, name_ptr
, name_len
,
3255 found_key
.offset
, location
.objectid
,
3259 if (name_ptr
!= tmp_name
)
3264 di_len
= btrfs_dir_name_len(leaf
, di
) +
3265 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
3267 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
3271 /* Reached end of directory/root. Bump pos past the last item. */
3272 if (key_type
== BTRFS_DIR_INDEX_KEY
)
3273 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
3279 btrfs_free_path(path
);
3283 int btrfs_write_inode(struct inode
*inode
, int wait
)
3285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3286 struct btrfs_trans_handle
*trans
;
3289 if (root
->fs_info
->btree_inode
== inode
)
3293 trans
= btrfs_join_transaction(root
, 1);
3294 btrfs_set_trans_block_group(trans
, inode
);
3295 ret
= btrfs_commit_transaction(trans
, root
);
3301 * This is somewhat expensive, updating the tree every time the
3302 * inode changes. But, it is most likely to find the inode in cache.
3303 * FIXME, needs more benchmarking...there are no reasons other than performance
3304 * to keep or drop this code.
3306 void btrfs_dirty_inode(struct inode
*inode
)
3308 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3309 struct btrfs_trans_handle
*trans
;
3311 trans
= btrfs_join_transaction(root
, 1);
3312 btrfs_set_trans_block_group(trans
, inode
);
3313 btrfs_update_inode(trans
, root
, inode
);
3314 btrfs_end_transaction(trans
, root
);
3318 * find the highest existing sequence number in a directory
3319 * and then set the in-memory index_cnt variable to reflect
3320 * free sequence numbers
3322 static int btrfs_set_inode_index_count(struct inode
*inode
)
3324 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3325 struct btrfs_key key
, found_key
;
3326 struct btrfs_path
*path
;
3327 struct extent_buffer
*leaf
;
3330 key
.objectid
= inode
->i_ino
;
3331 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
3332 key
.offset
= (u64
)-1;
3334 path
= btrfs_alloc_path();
3338 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3341 /* FIXME: we should be able to handle this */
3347 * MAGIC NUMBER EXPLANATION:
3348 * since we search a directory based on f_pos we have to start at 2
3349 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3350 * else has to start at 2
3352 if (path
->slots
[0] == 0) {
3353 BTRFS_I(inode
)->index_cnt
= 2;
3359 leaf
= path
->nodes
[0];
3360 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3362 if (found_key
.objectid
!= inode
->i_ino
||
3363 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
3364 BTRFS_I(inode
)->index_cnt
= 2;
3368 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
3370 btrfs_free_path(path
);
3375 * helper to find a free sequence number in a given directory. This current
3376 * code is very simple, later versions will do smarter things in the btree
3378 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
3382 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
3383 ret
= btrfs_set_inode_index_count(dir
);
3388 *index
= BTRFS_I(dir
)->index_cnt
;
3389 BTRFS_I(dir
)->index_cnt
++;
3394 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
3395 struct btrfs_root
*root
,
3397 const char *name
, int name_len
,
3398 u64 ref_objectid
, u64 objectid
,
3399 u64 alloc_hint
, int mode
, u64
*index
)
3401 struct inode
*inode
;
3402 struct btrfs_inode_item
*inode_item
;
3403 struct btrfs_key
*location
;
3404 struct btrfs_path
*path
;
3405 struct btrfs_inode_ref
*ref
;
3406 struct btrfs_key key
[2];
3412 path
= btrfs_alloc_path();
3415 inode
= new_inode(root
->fs_info
->sb
);
3417 return ERR_PTR(-ENOMEM
);
3420 ret
= btrfs_set_inode_index(dir
, index
);
3422 return ERR_PTR(ret
);
3425 * index_cnt is ignored for everything but a dir,
3426 * btrfs_get_inode_index_count has an explanation for the magic
3429 init_btrfs_i(inode
);
3430 BTRFS_I(inode
)->index_cnt
= 2;
3431 BTRFS_I(inode
)->root
= root
;
3432 BTRFS_I(inode
)->generation
= trans
->transid
;
3438 BTRFS_I(inode
)->block_group
=
3439 btrfs_find_block_group(root
, 0, alloc_hint
, owner
);
3440 if ((mode
& S_IFREG
)) {
3441 if (btrfs_test_opt(root
, NODATASUM
))
3442 btrfs_set_flag(inode
, NODATASUM
);
3443 if (btrfs_test_opt(root
, NODATACOW
))
3444 btrfs_set_flag(inode
, NODATACOW
);
3447 key
[0].objectid
= objectid
;
3448 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
3451 key
[1].objectid
= objectid
;
3452 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
3453 key
[1].offset
= ref_objectid
;
3455 sizes
[0] = sizeof(struct btrfs_inode_item
);
3456 sizes
[1] = name_len
+ sizeof(*ref
);
3458 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
3462 if (objectid
> root
->highest_inode
)
3463 root
->highest_inode
= objectid
;
3465 inode
->i_uid
= current_fsuid();
3466 inode
->i_gid
= current_fsgid();
3467 inode
->i_mode
= mode
;
3468 inode
->i_ino
= objectid
;
3469 inode_set_bytes(inode
, 0);
3470 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
3471 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3472 struct btrfs_inode_item
);
3473 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
3475 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
3476 struct btrfs_inode_ref
);
3477 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
3478 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
3479 ptr
= (unsigned long)(ref
+ 1);
3480 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
3482 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3483 btrfs_free_path(path
);
3485 location
= &BTRFS_I(inode
)->location
;
3486 location
->objectid
= objectid
;
3487 location
->offset
= 0;
3488 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
3490 insert_inode_hash(inode
);
3494 BTRFS_I(dir
)->index_cnt
--;
3495 btrfs_free_path(path
);
3496 return ERR_PTR(ret
);
3499 static inline u8
btrfs_inode_type(struct inode
*inode
)
3501 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
3505 * utility function to add 'inode' into 'parent_inode' with
3506 * a give name and a given sequence number.
3507 * if 'add_backref' is true, also insert a backref from the
3508 * inode to the parent directory.
3510 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
3511 struct inode
*parent_inode
, struct inode
*inode
,
3512 const char *name
, int name_len
, int add_backref
, u64 index
)
3515 struct btrfs_key key
;
3516 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
3518 key
.objectid
= inode
->i_ino
;
3519 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
3522 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
3523 parent_inode
->i_ino
,
3524 &key
, btrfs_inode_type(inode
),
3528 ret
= btrfs_insert_inode_ref(trans
, root
,
3531 parent_inode
->i_ino
,
3534 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
3536 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
3537 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
3542 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
3543 struct dentry
*dentry
, struct inode
*inode
,
3544 int backref
, u64 index
)
3546 int err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
3547 inode
, dentry
->d_name
.name
,
3548 dentry
->d_name
.len
, backref
, index
);
3550 d_instantiate(dentry
, inode
);
3558 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
3559 int mode
, dev_t rdev
)
3561 struct btrfs_trans_handle
*trans
;
3562 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3563 struct inode
*inode
= NULL
;
3567 unsigned long nr
= 0;
3570 if (!new_valid_dev(rdev
))
3573 err
= btrfs_check_free_space(root
, 1, 0);
3577 trans
= btrfs_start_transaction(root
, 1);
3578 btrfs_set_trans_block_group(trans
, dir
);
3580 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3586 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3588 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3589 BTRFS_I(dir
)->block_group
, mode
, &index
);
3590 err
= PTR_ERR(inode
);
3594 err
= btrfs_init_acl(inode
, dir
);
3600 btrfs_set_trans_block_group(trans
, inode
);
3601 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
3605 inode
->i_op
= &btrfs_special_inode_operations
;
3606 init_special_inode(inode
, inode
->i_mode
, rdev
);
3607 btrfs_update_inode(trans
, root
, inode
);
3609 dir
->i_sb
->s_dirt
= 1;
3610 btrfs_update_inode_block_group(trans
, inode
);
3611 btrfs_update_inode_block_group(trans
, dir
);
3613 nr
= trans
->blocks_used
;
3614 btrfs_end_transaction_throttle(trans
, root
);
3617 inode_dec_link_count(inode
);
3620 btrfs_btree_balance_dirty(root
, nr
);
3624 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
3625 int mode
, struct nameidata
*nd
)
3627 struct btrfs_trans_handle
*trans
;
3628 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3629 struct inode
*inode
= NULL
;
3632 unsigned long nr
= 0;
3636 err
= btrfs_check_free_space(root
, 1, 0);
3639 trans
= btrfs_start_transaction(root
, 1);
3640 btrfs_set_trans_block_group(trans
, dir
);
3642 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3648 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3650 dentry
->d_parent
->d_inode
->i_ino
,
3651 objectid
, BTRFS_I(dir
)->block_group
, mode
,
3653 err
= PTR_ERR(inode
);
3657 err
= btrfs_init_acl(inode
, dir
);
3663 btrfs_set_trans_block_group(trans
, inode
);
3664 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
3668 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3669 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3670 inode
->i_fop
= &btrfs_file_operations
;
3671 inode
->i_op
= &btrfs_file_inode_operations
;
3672 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3674 dir
->i_sb
->s_dirt
= 1;
3675 btrfs_update_inode_block_group(trans
, inode
);
3676 btrfs_update_inode_block_group(trans
, dir
);
3678 nr
= trans
->blocks_used
;
3679 btrfs_end_transaction_throttle(trans
, root
);
3682 inode_dec_link_count(inode
);
3685 btrfs_btree_balance_dirty(root
, nr
);
3689 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
3690 struct dentry
*dentry
)
3692 struct btrfs_trans_handle
*trans
;
3693 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3694 struct inode
*inode
= old_dentry
->d_inode
;
3696 unsigned long nr
= 0;
3700 if (inode
->i_nlink
== 0)
3703 btrfs_inc_nlink(inode
);
3704 err
= btrfs_check_free_space(root
, 1, 0);
3707 err
= btrfs_set_inode_index(dir
, &index
);
3711 trans
= btrfs_start_transaction(root
, 1);
3713 btrfs_set_trans_block_group(trans
, dir
);
3714 atomic_inc(&inode
->i_count
);
3716 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
3721 dir
->i_sb
->s_dirt
= 1;
3722 btrfs_update_inode_block_group(trans
, dir
);
3723 err
= btrfs_update_inode(trans
, root
, inode
);
3728 nr
= trans
->blocks_used
;
3729 btrfs_end_transaction_throttle(trans
, root
);
3732 inode_dec_link_count(inode
);
3735 btrfs_btree_balance_dirty(root
, nr
);
3739 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
3741 struct inode
*inode
= NULL
;
3742 struct btrfs_trans_handle
*trans
;
3743 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3745 int drop_on_err
= 0;
3748 unsigned long nr
= 1;
3750 err
= btrfs_check_free_space(root
, 1, 0);
3754 trans
= btrfs_start_transaction(root
, 1);
3755 btrfs_set_trans_block_group(trans
, dir
);
3757 if (IS_ERR(trans
)) {
3758 err
= PTR_ERR(trans
);
3762 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3768 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3770 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3771 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
3773 if (IS_ERR(inode
)) {
3774 err
= PTR_ERR(inode
);
3780 err
= btrfs_init_acl(inode
, dir
);
3784 inode
->i_op
= &btrfs_dir_inode_operations
;
3785 inode
->i_fop
= &btrfs_dir_file_operations
;
3786 btrfs_set_trans_block_group(trans
, inode
);
3788 btrfs_i_size_write(inode
, 0);
3789 err
= btrfs_update_inode(trans
, root
, inode
);
3793 err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
3794 inode
, dentry
->d_name
.name
,
3795 dentry
->d_name
.len
, 0, index
);
3799 d_instantiate(dentry
, inode
);
3801 dir
->i_sb
->s_dirt
= 1;
3802 btrfs_update_inode_block_group(trans
, inode
);
3803 btrfs_update_inode_block_group(trans
, dir
);
3806 nr
= trans
->blocks_used
;
3807 btrfs_end_transaction_throttle(trans
, root
);
3812 btrfs_btree_balance_dirty(root
, nr
);
3816 /* helper for btfs_get_extent. Given an existing extent in the tree,
3817 * and an extent that you want to insert, deal with overlap and insert
3818 * the new extent into the tree.
3820 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
3821 struct extent_map
*existing
,
3822 struct extent_map
*em
,
3823 u64 map_start
, u64 map_len
)
3827 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
3828 start_diff
= map_start
- em
->start
;
3829 em
->start
= map_start
;
3831 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
3832 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
3833 em
->block_start
+= start_diff
;
3834 em
->block_len
-= start_diff
;
3836 return add_extent_mapping(em_tree
, em
);
3839 static noinline
int uncompress_inline(struct btrfs_path
*path
,
3840 struct inode
*inode
, struct page
*page
,
3841 size_t pg_offset
, u64 extent_offset
,
3842 struct btrfs_file_extent_item
*item
)
3845 struct extent_buffer
*leaf
= path
->nodes
[0];
3848 unsigned long inline_size
;
3851 WARN_ON(pg_offset
!= 0);
3852 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
3853 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
3854 btrfs_item_nr(leaf
, path
->slots
[0]));
3855 tmp
= kmalloc(inline_size
, GFP_NOFS
);
3856 ptr
= btrfs_file_extent_inline_start(item
);
3858 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
3860 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
3861 ret
= btrfs_zlib_decompress(tmp
, page
, extent_offset
,
3862 inline_size
, max_size
);
3864 char *kaddr
= kmap_atomic(page
, KM_USER0
);
3865 unsigned long copy_size
= min_t(u64
,
3866 PAGE_CACHE_SIZE
- pg_offset
,
3867 max_size
- extent_offset
);
3868 memset(kaddr
+ pg_offset
, 0, copy_size
);
3869 kunmap_atomic(kaddr
, KM_USER0
);
3876 * a bit scary, this does extent mapping from logical file offset to the disk.
3877 * the ugly parts come from merging extents from the disk with the in-ram
3878 * representation. This gets more complex because of the data=ordered code,
3879 * where the in-ram extents might be locked pending data=ordered completion.
3881 * This also copies inline extents directly into the page.
3884 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
3885 size_t pg_offset
, u64 start
, u64 len
,
3891 u64 extent_start
= 0;
3893 u64 objectid
= inode
->i_ino
;
3895 struct btrfs_path
*path
= NULL
;
3896 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3897 struct btrfs_file_extent_item
*item
;
3898 struct extent_buffer
*leaf
;
3899 struct btrfs_key found_key
;
3900 struct extent_map
*em
= NULL
;
3901 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
3902 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3903 struct btrfs_trans_handle
*trans
= NULL
;
3907 spin_lock(&em_tree
->lock
);
3908 em
= lookup_extent_mapping(em_tree
, start
, len
);
3910 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3911 spin_unlock(&em_tree
->lock
);
3914 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
3915 free_extent_map(em
);
3916 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
3917 free_extent_map(em
);
3921 em
= alloc_extent_map(GFP_NOFS
);
3926 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
3927 em
->start
= EXTENT_MAP_HOLE
;
3928 em
->orig_start
= EXTENT_MAP_HOLE
;
3930 em
->block_len
= (u64
)-1;
3933 path
= btrfs_alloc_path();
3937 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3938 objectid
, start
, trans
!= NULL
);
3945 if (path
->slots
[0] == 0)
3950 leaf
= path
->nodes
[0];
3951 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3952 struct btrfs_file_extent_item
);
3953 /* are we inside the extent that was found? */
3954 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3955 found_type
= btrfs_key_type(&found_key
);
3956 if (found_key
.objectid
!= objectid
||
3957 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
3961 found_type
= btrfs_file_extent_type(leaf
, item
);
3962 extent_start
= found_key
.offset
;
3963 compressed
= btrfs_file_extent_compression(leaf
, item
);
3964 if (found_type
== BTRFS_FILE_EXTENT_REG
||
3965 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3966 extent_end
= extent_start
+
3967 btrfs_file_extent_num_bytes(leaf
, item
);
3968 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
3970 size
= btrfs_file_extent_inline_len(leaf
, item
);
3971 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
3972 ~((u64
)root
->sectorsize
- 1);
3975 if (start
>= extent_end
) {
3977 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3978 ret
= btrfs_next_leaf(root
, path
);
3985 leaf
= path
->nodes
[0];
3987 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3988 if (found_key
.objectid
!= objectid
||
3989 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3991 if (start
+ len
<= found_key
.offset
)
3994 em
->len
= found_key
.offset
- start
;
3998 if (found_type
== BTRFS_FILE_EXTENT_REG
||
3999 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4000 em
->start
= extent_start
;
4001 em
->len
= extent_end
- extent_start
;
4002 em
->orig_start
= extent_start
-
4003 btrfs_file_extent_offset(leaf
, item
);
4004 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
4006 em
->block_start
= EXTENT_MAP_HOLE
;
4010 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
4011 em
->block_start
= bytenr
;
4012 em
->block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
4015 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
4016 em
->block_start
= bytenr
;
4017 em
->block_len
= em
->len
;
4018 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
4019 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
4022 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
4026 size_t extent_offset
;
4029 em
->block_start
= EXTENT_MAP_INLINE
;
4030 if (!page
|| create
) {
4031 em
->start
= extent_start
;
4032 em
->len
= extent_end
- extent_start
;
4036 size
= btrfs_file_extent_inline_len(leaf
, item
);
4037 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
4038 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
4039 size
- extent_offset
);
4040 em
->start
= extent_start
+ extent_offset
;
4041 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
4042 ~((u64
)root
->sectorsize
- 1);
4043 em
->orig_start
= EXTENT_MAP_INLINE
;
4045 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
4046 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
4047 if (create
== 0 && !PageUptodate(page
)) {
4048 if (btrfs_file_extent_compression(leaf
, item
) ==
4049 BTRFS_COMPRESS_ZLIB
) {
4050 ret
= uncompress_inline(path
, inode
, page
,
4052 extent_offset
, item
);
4056 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
4060 flush_dcache_page(page
);
4061 } else if (create
&& PageUptodate(page
)) {
4064 free_extent_map(em
);
4066 btrfs_release_path(root
, path
);
4067 trans
= btrfs_join_transaction(root
, 1);
4071 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
4074 btrfs_mark_buffer_dirty(leaf
);
4076 set_extent_uptodate(io_tree
, em
->start
,
4077 extent_map_end(em
) - 1, GFP_NOFS
);
4080 printk(KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
4087 em
->block_start
= EXTENT_MAP_HOLE
;
4088 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
4090 btrfs_release_path(root
, path
);
4091 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
4092 printk(KERN_ERR
"Btrfs: bad extent! em: [%llu %llu] passed "
4093 "[%llu %llu]\n", (unsigned long long)em
->start
,
4094 (unsigned long long)em
->len
,
4095 (unsigned long long)start
,
4096 (unsigned long long)len
);
4102 spin_lock(&em_tree
->lock
);
4103 ret
= add_extent_mapping(em_tree
, em
);
4104 /* it is possible that someone inserted the extent into the tree
4105 * while we had the lock dropped. It is also possible that
4106 * an overlapping map exists in the tree
4108 if (ret
== -EEXIST
) {
4109 struct extent_map
*existing
;
4113 existing
= lookup_extent_mapping(em_tree
, start
, len
);
4114 if (existing
&& (existing
->start
> start
||
4115 existing
->start
+ existing
->len
<= start
)) {
4116 free_extent_map(existing
);
4120 existing
= lookup_extent_mapping(em_tree
, em
->start
,
4123 err
= merge_extent_mapping(em_tree
, existing
,
4126 free_extent_map(existing
);
4128 free_extent_map(em
);
4133 free_extent_map(em
);
4137 free_extent_map(em
);
4142 spin_unlock(&em_tree
->lock
);
4145 btrfs_free_path(path
);
4147 ret
= btrfs_end_transaction(trans
, root
);
4152 free_extent_map(em
);
4154 return ERR_PTR(err
);
4159 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
4160 const struct iovec
*iov
, loff_t offset
,
4161 unsigned long nr_segs
)
4166 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
4168 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
4171 int btrfs_readpage(struct file
*file
, struct page
*page
)
4173 struct extent_io_tree
*tree
;
4174 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
4175 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
4178 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
4180 struct extent_io_tree
*tree
;
4183 if (current
->flags
& PF_MEMALLOC
) {
4184 redirty_page_for_writepage(wbc
, page
);
4188 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
4189 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
4192 int btrfs_writepages(struct address_space
*mapping
,
4193 struct writeback_control
*wbc
)
4195 struct extent_io_tree
*tree
;
4197 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
4198 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
4202 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
4203 struct list_head
*pages
, unsigned nr_pages
)
4205 struct extent_io_tree
*tree
;
4206 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
4207 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
4210 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
4212 struct extent_io_tree
*tree
;
4213 struct extent_map_tree
*map
;
4216 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
4217 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
4218 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
4220 ClearPagePrivate(page
);
4221 set_page_private(page
, 0);
4222 page_cache_release(page
);
4227 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
4229 if (PageWriteback(page
) || PageDirty(page
))
4231 return __btrfs_releasepage(page
, gfp_flags
);
4234 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
4236 struct extent_io_tree
*tree
;
4237 struct btrfs_ordered_extent
*ordered
;
4238 u64 page_start
= page_offset(page
);
4239 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4241 wait_on_page_writeback(page
);
4242 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
4244 btrfs_releasepage(page
, GFP_NOFS
);
4248 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
4249 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
4253 * IO on this page will never be started, so we need
4254 * to account for any ordered extents now
4256 clear_extent_bit(tree
, page_start
, page_end
,
4257 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4258 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
4259 btrfs_finish_ordered_io(page
->mapping
->host
,
4260 page_start
, page_end
);
4261 btrfs_put_ordered_extent(ordered
);
4262 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
4264 clear_extent_bit(tree
, page_start
, page_end
,
4265 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
4268 __btrfs_releasepage(page
, GFP_NOFS
);
4270 ClearPageChecked(page
);
4271 if (PagePrivate(page
)) {
4272 ClearPagePrivate(page
);
4273 set_page_private(page
, 0);
4274 page_cache_release(page
);
4279 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4280 * called from a page fault handler when a page is first dirtied. Hence we must
4281 * be careful to check for EOF conditions here. We set the page up correctly
4282 * for a written page which means we get ENOSPC checking when writing into
4283 * holes and correct delalloc and unwritten extent mapping on filesystems that
4284 * support these features.
4286 * We are not allowed to take the i_mutex here so we have to play games to
4287 * protect against truncate races as the page could now be beyond EOF. Because
4288 * vmtruncate() writes the inode size before removing pages, once we have the
4289 * page lock we can determine safely if the page is beyond EOF. If it is not
4290 * beyond EOF, then the page is guaranteed safe against truncation until we
4293 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
4295 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
4296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4297 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4298 struct btrfs_ordered_extent
*ordered
;
4300 unsigned long zero_start
;
4306 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
4313 size
= i_size_read(inode
);
4314 page_start
= page_offset(page
);
4315 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4317 if ((page
->mapping
!= inode
->i_mapping
) ||
4318 (page_start
>= size
)) {
4319 /* page got truncated out from underneath us */
4322 wait_on_page_writeback(page
);
4324 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4325 set_page_extent_mapped(page
);
4328 * we can't set the delalloc bits if there are pending ordered
4329 * extents. Drop our locks and wait for them to finish
4331 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4333 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4335 btrfs_start_ordered_extent(inode
, ordered
, 1);
4336 btrfs_put_ordered_extent(ordered
);
4340 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
4343 /* page is wholly or partially inside EOF */
4344 if (page_start
+ PAGE_CACHE_SIZE
> size
)
4345 zero_start
= size
& ~PAGE_CACHE_MASK
;
4347 zero_start
= PAGE_CACHE_SIZE
;
4349 if (zero_start
!= PAGE_CACHE_SIZE
) {
4351 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
4352 flush_dcache_page(page
);
4355 ClearPageChecked(page
);
4356 set_page_dirty(page
);
4357 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
4365 static void btrfs_truncate(struct inode
*inode
)
4367 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4369 struct btrfs_trans_handle
*trans
;
4371 u64 mask
= root
->sectorsize
- 1;
4373 if (!S_ISREG(inode
->i_mode
))
4375 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
4378 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
4379 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
4381 trans
= btrfs_start_transaction(root
, 1);
4382 btrfs_set_trans_block_group(trans
, inode
);
4383 btrfs_i_size_write(inode
, inode
->i_size
);
4385 ret
= btrfs_orphan_add(trans
, inode
);
4388 /* FIXME, add redo link to tree so we don't leak on crash */
4389 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, inode
->i_size
,
4390 BTRFS_EXTENT_DATA_KEY
);
4391 btrfs_update_inode(trans
, root
, inode
);
4393 ret
= btrfs_orphan_del(trans
, inode
);
4397 nr
= trans
->blocks_used
;
4398 ret
= btrfs_end_transaction_throttle(trans
, root
);
4400 btrfs_btree_balance_dirty(root
, nr
);
4404 * create a new subvolume directory/inode (helper for the ioctl).
4406 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
4407 struct btrfs_root
*new_root
, struct dentry
*dentry
,
4408 u64 new_dirid
, u64 alloc_hint
)
4410 struct inode
*inode
;
4414 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
4415 new_dirid
, alloc_hint
, S_IFDIR
| 0700, &index
);
4417 return PTR_ERR(inode
);
4418 inode
->i_op
= &btrfs_dir_inode_operations
;
4419 inode
->i_fop
= &btrfs_dir_file_operations
;
4422 btrfs_i_size_write(inode
, 0);
4424 error
= btrfs_update_inode(trans
, new_root
, inode
);
4428 d_instantiate(dentry
, inode
);
4432 /* helper function for file defrag and space balancing. This
4433 * forces readahead on a given range of bytes in an inode
4435 unsigned long btrfs_force_ra(struct address_space
*mapping
,
4436 struct file_ra_state
*ra
, struct file
*file
,
4437 pgoff_t offset
, pgoff_t last_index
)
4439 pgoff_t req_size
= last_index
- offset
+ 1;
4441 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
4442 return offset
+ req_size
;
4445 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
4447 struct btrfs_inode
*ei
;
4449 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
4453 ei
->logged_trans
= 0;
4454 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
4455 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
4456 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
4457 INIT_LIST_HEAD(&ei
->i_orphan
);
4458 return &ei
->vfs_inode
;
4461 void btrfs_destroy_inode(struct inode
*inode
)
4463 struct btrfs_ordered_extent
*ordered
;
4464 WARN_ON(!list_empty(&inode
->i_dentry
));
4465 WARN_ON(inode
->i_data
.nrpages
);
4467 if (BTRFS_I(inode
)->i_acl
&&
4468 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
4469 posix_acl_release(BTRFS_I(inode
)->i_acl
);
4470 if (BTRFS_I(inode
)->i_default_acl
&&
4471 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
4472 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
4474 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
4475 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
4476 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
4477 " list\n", inode
->i_ino
);
4480 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
4483 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
4487 printk(KERN_ERR
"btrfs found ordered "
4488 "extent %llu %llu on inode cleanup\n",
4489 (unsigned long long)ordered
->file_offset
,
4490 (unsigned long long)ordered
->len
);
4491 btrfs_remove_ordered_extent(inode
, ordered
);
4492 btrfs_put_ordered_extent(ordered
);
4493 btrfs_put_ordered_extent(ordered
);
4496 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
4497 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
4500 static void init_once(void *foo
)
4502 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
4504 inode_init_once(&ei
->vfs_inode
);
4507 void btrfs_destroy_cachep(void)
4509 if (btrfs_inode_cachep
)
4510 kmem_cache_destroy(btrfs_inode_cachep
);
4511 if (btrfs_trans_handle_cachep
)
4512 kmem_cache_destroy(btrfs_trans_handle_cachep
);
4513 if (btrfs_transaction_cachep
)
4514 kmem_cache_destroy(btrfs_transaction_cachep
);
4515 if (btrfs_bit_radix_cachep
)
4516 kmem_cache_destroy(btrfs_bit_radix_cachep
);
4517 if (btrfs_path_cachep
)
4518 kmem_cache_destroy(btrfs_path_cachep
);
4521 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
4522 unsigned long extra_flags
,
4523 void (*ctor
)(void *))
4525 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
4526 SLAB_MEM_SPREAD
| extra_flags
), ctor
);
4529 int btrfs_init_cachep(void)
4531 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
4532 sizeof(struct btrfs_inode
),
4534 if (!btrfs_inode_cachep
)
4536 btrfs_trans_handle_cachep
=
4537 btrfs_cache_create("btrfs_trans_handle_cache",
4538 sizeof(struct btrfs_trans_handle
),
4540 if (!btrfs_trans_handle_cachep
)
4542 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
4543 sizeof(struct btrfs_transaction
),
4545 if (!btrfs_transaction_cachep
)
4547 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
4548 sizeof(struct btrfs_path
),
4550 if (!btrfs_path_cachep
)
4552 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
4553 SLAB_DESTROY_BY_RCU
, NULL
);
4554 if (!btrfs_bit_radix_cachep
)
4558 btrfs_destroy_cachep();
4562 static int btrfs_getattr(struct vfsmount
*mnt
,
4563 struct dentry
*dentry
, struct kstat
*stat
)
4565 struct inode
*inode
= dentry
->d_inode
;
4566 generic_fillattr(inode
, stat
);
4567 stat
->dev
= BTRFS_I(inode
)->root
->anon_super
.s_dev
;
4568 stat
->blksize
= PAGE_CACHE_SIZE
;
4569 stat
->blocks
= (inode_get_bytes(inode
) +
4570 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
4574 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4575 struct inode
*new_dir
, struct dentry
*new_dentry
)
4577 struct btrfs_trans_handle
*trans
;
4578 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
4579 struct inode
*new_inode
= new_dentry
->d_inode
;
4580 struct inode
*old_inode
= old_dentry
->d_inode
;
4581 struct timespec ctime
= CURRENT_TIME
;
4585 /* we're not allowed to rename between subvolumes */
4586 if (BTRFS_I(old_inode
)->root
->root_key
.objectid
!=
4587 BTRFS_I(new_dir
)->root
->root_key
.objectid
)
4590 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
4591 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
4595 /* to rename a snapshot or subvolume, we need to juggle the
4596 * backrefs. This isn't coded yet
4598 if (old_inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
)
4601 ret
= btrfs_check_free_space(root
, 1, 0);
4605 trans
= btrfs_start_transaction(root
, 1);
4607 btrfs_set_trans_block_group(trans
, new_dir
);
4609 btrfs_inc_nlink(old_dentry
->d_inode
);
4610 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
4611 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
4612 old_inode
->i_ctime
= ctime
;
4614 ret
= btrfs_unlink_inode(trans
, root
, old_dir
, old_dentry
->d_inode
,
4615 old_dentry
->d_name
.name
,
4616 old_dentry
->d_name
.len
);
4621 new_inode
->i_ctime
= CURRENT_TIME
;
4622 ret
= btrfs_unlink_inode(trans
, root
, new_dir
,
4623 new_dentry
->d_inode
,
4624 new_dentry
->d_name
.name
,
4625 new_dentry
->d_name
.len
);
4628 if (new_inode
->i_nlink
== 0) {
4629 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
4635 ret
= btrfs_set_inode_index(new_dir
, &index
);
4639 ret
= btrfs_add_link(trans
, new_dentry
->d_parent
->d_inode
,
4640 old_inode
, new_dentry
->d_name
.name
,
4641 new_dentry
->d_name
.len
, 1, index
);
4646 btrfs_end_transaction_throttle(trans
, root
);
4652 * some fairly slow code that needs optimization. This walks the list
4653 * of all the inodes with pending delalloc and forces them to disk.
4655 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
)
4657 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
4658 struct btrfs_inode
*binode
;
4659 struct inode
*inode
;
4661 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
4664 spin_lock(&root
->fs_info
->delalloc_lock
);
4665 while (!list_empty(head
)) {
4666 binode
= list_entry(head
->next
, struct btrfs_inode
,
4668 inode
= igrab(&binode
->vfs_inode
);
4670 list_del_init(&binode
->delalloc_inodes
);
4671 spin_unlock(&root
->fs_info
->delalloc_lock
);
4673 filemap_flush(inode
->i_mapping
);
4677 spin_lock(&root
->fs_info
->delalloc_lock
);
4679 spin_unlock(&root
->fs_info
->delalloc_lock
);
4681 /* the filemap_flush will queue IO into the worker threads, but
4682 * we have to make sure the IO is actually started and that
4683 * ordered extents get created before we return
4685 atomic_inc(&root
->fs_info
->async_submit_draining
);
4686 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
4687 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
4688 wait_event(root
->fs_info
->async_submit_wait
,
4689 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
4690 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
4692 atomic_dec(&root
->fs_info
->async_submit_draining
);
4696 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
4697 const char *symname
)
4699 struct btrfs_trans_handle
*trans
;
4700 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4701 struct btrfs_path
*path
;
4702 struct btrfs_key key
;
4703 struct inode
*inode
= NULL
;
4711 struct btrfs_file_extent_item
*ei
;
4712 struct extent_buffer
*leaf
;
4713 unsigned long nr
= 0;
4715 name_len
= strlen(symname
) + 1;
4716 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
4717 return -ENAMETOOLONG
;
4719 err
= btrfs_check_free_space(root
, 1, 0);
4723 trans
= btrfs_start_transaction(root
, 1);
4724 btrfs_set_trans_block_group(trans
, dir
);
4726 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
4732 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
4734 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
4735 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
4737 err
= PTR_ERR(inode
);
4741 err
= btrfs_init_acl(inode
, dir
);
4747 btrfs_set_trans_block_group(trans
, inode
);
4748 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
4752 inode
->i_mapping
->a_ops
= &btrfs_aops
;
4753 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4754 inode
->i_fop
= &btrfs_file_operations
;
4755 inode
->i_op
= &btrfs_file_inode_operations
;
4756 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
4758 dir
->i_sb
->s_dirt
= 1;
4759 btrfs_update_inode_block_group(trans
, inode
);
4760 btrfs_update_inode_block_group(trans
, dir
);
4764 path
= btrfs_alloc_path();
4766 key
.objectid
= inode
->i_ino
;
4768 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
4769 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
4770 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
4776 leaf
= path
->nodes
[0];
4777 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
4778 struct btrfs_file_extent_item
);
4779 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
4780 btrfs_set_file_extent_type(leaf
, ei
,
4781 BTRFS_FILE_EXTENT_INLINE
);
4782 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
4783 btrfs_set_file_extent_compression(leaf
, ei
, 0);
4784 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
4785 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
4787 ptr
= btrfs_file_extent_inline_start(ei
);
4788 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
4789 btrfs_mark_buffer_dirty(leaf
);
4790 btrfs_free_path(path
);
4792 inode
->i_op
= &btrfs_symlink_inode_operations
;
4793 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
4794 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
4795 inode_set_bytes(inode
, name_len
);
4796 btrfs_i_size_write(inode
, name_len
- 1);
4797 err
= btrfs_update_inode(trans
, root
, inode
);
4802 nr
= trans
->blocks_used
;
4803 btrfs_end_transaction_throttle(trans
, root
);
4806 inode_dec_link_count(inode
);
4809 btrfs_btree_balance_dirty(root
, nr
);
4813 static int prealloc_file_range(struct inode
*inode
, u64 start
, u64 end
,
4814 u64 alloc_hint
, int mode
)
4816 struct btrfs_trans_handle
*trans
;
4817 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4818 struct btrfs_key ins
;
4820 u64 cur_offset
= start
;
4821 u64 num_bytes
= end
- start
;
4824 trans
= btrfs_join_transaction(root
, 1);
4826 btrfs_set_trans_block_group(trans
, inode
);
4828 while (num_bytes
> 0) {
4829 alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
4830 ret
= btrfs_reserve_extent(trans
, root
, alloc_size
,
4831 root
->sectorsize
, 0, alloc_hint
,
4837 ret
= insert_reserved_file_extent(trans
, inode
,
4838 cur_offset
, ins
.objectid
,
4839 ins
.offset
, ins
.offset
,
4840 ins
.offset
, 0, 0, 0,
4841 BTRFS_FILE_EXTENT_PREALLOC
);
4843 num_bytes
-= ins
.offset
;
4844 cur_offset
+= ins
.offset
;
4845 alloc_hint
= ins
.objectid
+ ins
.offset
;
4848 if (cur_offset
> start
) {
4849 inode
->i_ctime
= CURRENT_TIME
;
4850 btrfs_set_flag(inode
, PREALLOC
);
4851 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
4852 cur_offset
> i_size_read(inode
))
4853 btrfs_i_size_write(inode
, cur_offset
);
4854 ret
= btrfs_update_inode(trans
, root
, inode
);
4858 btrfs_end_transaction(trans
, root
);
4862 static long btrfs_fallocate(struct inode
*inode
, int mode
,
4863 loff_t offset
, loff_t len
)
4870 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
4871 struct extent_map
*em
;
4874 alloc_start
= offset
& ~mask
;
4875 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
4877 mutex_lock(&inode
->i_mutex
);
4878 if (alloc_start
> inode
->i_size
) {
4879 ret
= btrfs_cont_expand(inode
, alloc_start
);
4885 struct btrfs_ordered_extent
*ordered
;
4886 lock_extent(&BTRFS_I(inode
)->io_tree
, alloc_start
,
4887 alloc_end
- 1, GFP_NOFS
);
4888 ordered
= btrfs_lookup_first_ordered_extent(inode
,
4891 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
4892 ordered
->file_offset
< alloc_end
) {
4893 btrfs_put_ordered_extent(ordered
);
4894 unlock_extent(&BTRFS_I(inode
)->io_tree
,
4895 alloc_start
, alloc_end
- 1, GFP_NOFS
);
4896 btrfs_wait_ordered_range(inode
, alloc_start
,
4897 alloc_end
- alloc_start
);
4900 btrfs_put_ordered_extent(ordered
);
4905 cur_offset
= alloc_start
;
4907 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4908 alloc_end
- cur_offset
, 0);
4909 BUG_ON(IS_ERR(em
) || !em
);
4910 last_byte
= min(extent_map_end(em
), alloc_end
);
4911 last_byte
= (last_byte
+ mask
) & ~mask
;
4912 if (em
->block_start
== EXTENT_MAP_HOLE
) {
4913 ret
= prealloc_file_range(inode
, cur_offset
,
4914 last_byte
, alloc_hint
, mode
);
4916 free_extent_map(em
);
4920 if (em
->block_start
<= EXTENT_MAP_LAST_BYTE
)
4921 alloc_hint
= em
->block_start
;
4922 free_extent_map(em
);
4924 cur_offset
= last_byte
;
4925 if (cur_offset
>= alloc_end
) {
4930 unlock_extent(&BTRFS_I(inode
)->io_tree
, alloc_start
, alloc_end
- 1,
4933 mutex_unlock(&inode
->i_mutex
);
4937 static int btrfs_set_page_dirty(struct page
*page
)
4939 return __set_page_dirty_nobuffers(page
);
4942 static int btrfs_permission(struct inode
*inode
, int mask
)
4944 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
4946 return generic_permission(inode
, mask
, btrfs_check_acl
);
4949 static struct inode_operations btrfs_dir_inode_operations
= {
4950 .getattr
= btrfs_getattr
,
4951 .lookup
= btrfs_lookup
,
4952 .create
= btrfs_create
,
4953 .unlink
= btrfs_unlink
,
4955 .mkdir
= btrfs_mkdir
,
4956 .rmdir
= btrfs_rmdir
,
4957 .rename
= btrfs_rename
,
4958 .symlink
= btrfs_symlink
,
4959 .setattr
= btrfs_setattr
,
4960 .mknod
= btrfs_mknod
,
4961 .setxattr
= btrfs_setxattr
,
4962 .getxattr
= btrfs_getxattr
,
4963 .listxattr
= btrfs_listxattr
,
4964 .removexattr
= btrfs_removexattr
,
4965 .permission
= btrfs_permission
,
4967 static struct inode_operations btrfs_dir_ro_inode_operations
= {
4968 .lookup
= btrfs_lookup
,
4969 .permission
= btrfs_permission
,
4971 static struct file_operations btrfs_dir_file_operations
= {
4972 .llseek
= generic_file_llseek
,
4973 .read
= generic_read_dir
,
4974 .readdir
= btrfs_real_readdir
,
4975 .unlocked_ioctl
= btrfs_ioctl
,
4976 #ifdef CONFIG_COMPAT
4977 .compat_ioctl
= btrfs_ioctl
,
4979 .release
= btrfs_release_file
,
4980 .fsync
= btrfs_sync_file
,
4983 static struct extent_io_ops btrfs_extent_io_ops
= {
4984 .fill_delalloc
= run_delalloc_range
,
4985 .submit_bio_hook
= btrfs_submit_bio_hook
,
4986 .merge_bio_hook
= btrfs_merge_bio_hook
,
4987 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
4988 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
4989 .writepage_start_hook
= btrfs_writepage_start_hook
,
4990 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
4991 .set_bit_hook
= btrfs_set_bit_hook
,
4992 .clear_bit_hook
= btrfs_clear_bit_hook
,
4995 static struct address_space_operations btrfs_aops
= {
4996 .readpage
= btrfs_readpage
,
4997 .writepage
= btrfs_writepage
,
4998 .writepages
= btrfs_writepages
,
4999 .readpages
= btrfs_readpages
,
5000 .sync_page
= block_sync_page
,
5002 .direct_IO
= btrfs_direct_IO
,
5003 .invalidatepage
= btrfs_invalidatepage
,
5004 .releasepage
= btrfs_releasepage
,
5005 .set_page_dirty
= btrfs_set_page_dirty
,
5008 static struct address_space_operations btrfs_symlink_aops
= {
5009 .readpage
= btrfs_readpage
,
5010 .writepage
= btrfs_writepage
,
5011 .invalidatepage
= btrfs_invalidatepage
,
5012 .releasepage
= btrfs_releasepage
,
5015 static struct inode_operations btrfs_file_inode_operations
= {
5016 .truncate
= btrfs_truncate
,
5017 .getattr
= btrfs_getattr
,
5018 .setattr
= btrfs_setattr
,
5019 .setxattr
= btrfs_setxattr
,
5020 .getxattr
= btrfs_getxattr
,
5021 .listxattr
= btrfs_listxattr
,
5022 .removexattr
= btrfs_removexattr
,
5023 .permission
= btrfs_permission
,
5024 .fallocate
= btrfs_fallocate
,
5026 static struct inode_operations btrfs_special_inode_operations
= {
5027 .getattr
= btrfs_getattr
,
5028 .setattr
= btrfs_setattr
,
5029 .permission
= btrfs_permission
,
5030 .setxattr
= btrfs_setxattr
,
5031 .getxattr
= btrfs_getxattr
,
5032 .listxattr
= btrfs_listxattr
,
5033 .removexattr
= btrfs_removexattr
,
5035 static struct inode_operations btrfs_symlink_inode_operations
= {
5036 .readlink
= generic_readlink
,
5037 .follow_link
= page_follow_link_light
,
5038 .put_link
= page_put_link
,
5039 .permission
= btrfs_permission
,