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>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
47 #include "ordered-data.h"
49 struct btrfs_iget_args
{
51 struct btrfs_root
*root
;
54 static struct inode_operations btrfs_dir_inode_operations
;
55 static struct inode_operations btrfs_symlink_inode_operations
;
56 static struct inode_operations btrfs_dir_ro_inode_operations
;
57 static struct inode_operations btrfs_special_inode_operations
;
58 static struct inode_operations btrfs_file_inode_operations
;
59 static struct address_space_operations btrfs_aops
;
60 static struct address_space_operations btrfs_symlink_aops
;
61 static struct file_operations btrfs_dir_file_operations
;
62 static struct extent_io_ops btrfs_extent_io_ops
;
64 static struct kmem_cache
*btrfs_inode_cachep
;
65 struct kmem_cache
*btrfs_trans_handle_cachep
;
66 struct kmem_cache
*btrfs_transaction_cachep
;
67 struct kmem_cache
*btrfs_bit_radix_cachep
;
68 struct kmem_cache
*btrfs_path_cachep
;
71 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
72 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
73 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
74 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
75 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
76 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
77 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
78 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
81 static void btrfs_truncate(struct inode
*inode
);
83 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
92 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
93 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
94 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
102 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
104 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
108 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
111 struct btrfs_trans_handle
*trans
;
115 u64 blocksize
= root
->sectorsize
;
117 struct btrfs_key ins
;
118 struct extent_map
*em
;
119 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
122 trans
= btrfs_join_transaction(root
, 1);
124 btrfs_set_trans_block_group(trans
, inode
);
126 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
127 num_bytes
= max(blocksize
, num_bytes
);
128 orig_num_bytes
= num_bytes
;
130 if (alloc_hint
== EXTENT_MAP_INLINE
)
133 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
134 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
135 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1);
136 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
138 while(num_bytes
> 0) {
139 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
140 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
141 root
->sectorsize
, 0, 0,
147 em
= alloc_extent_map(GFP_NOFS
);
149 em
->len
= ins
.offset
;
150 em
->block_start
= ins
.objectid
;
151 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
152 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
153 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
155 spin_lock(&em_tree
->lock
);
156 ret
= add_extent_mapping(em_tree
, em
);
157 spin_unlock(&em_tree
->lock
);
158 if (ret
!= -EEXIST
) {
162 btrfs_drop_extent_cache(inode
, start
,
163 start
+ ins
.offset
- 1);
165 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
167 cur_alloc_size
= ins
.offset
;
168 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
171 if (num_bytes
< cur_alloc_size
) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
176 num_bytes
-= cur_alloc_size
;
177 alloc_hint
= ins
.objectid
+ ins
.offset
;
178 start
+= cur_alloc_size
;
181 btrfs_end_transaction(trans
, root
);
185 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
194 struct btrfs_block_group_cache
*block_group
;
195 struct extent_buffer
*leaf
;
197 struct btrfs_path
*path
;
198 struct btrfs_file_extent_item
*item
;
201 struct btrfs_key found_key
;
203 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
204 path
= btrfs_alloc_path();
207 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
208 inode
->i_ino
, start
, 0);
210 btrfs_free_path(path
);
216 if (path
->slots
[0] == 0)
221 leaf
= path
->nodes
[0];
222 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
223 struct btrfs_file_extent_item
);
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
227 found_type
= btrfs_key_type(&found_key
);
228 if (found_key
.objectid
!= inode
->i_ino
||
229 found_type
!= BTRFS_EXTENT_DATA_KEY
)
232 found_type
= btrfs_file_extent_type(leaf
, item
);
233 extent_start
= found_key
.offset
;
234 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
235 u64 extent_num_bytes
;
237 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
238 extent_end
= extent_start
+ extent_num_bytes
;
241 if (loops
&& start
!= extent_start
)
244 if (start
< extent_start
|| start
>= extent_end
)
247 cow_end
= min(end
, extent_end
- 1);
248 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
252 if (btrfs_cross_ref_exists(root
, &found_key
, bytenr
))
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
258 block_group
= btrfs_lookup_block_group(root
->fs_info
,
260 if (!block_group
|| block_group
->ro
)
269 btrfs_free_path(path
);
272 btrfs_release_path(root
, path
);
277 btrfs_release_path(root
, path
);
278 cow_file_range(inode
, start
, end
);
283 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
288 if (btrfs_test_opt(root
, NODATACOW
) ||
289 btrfs_test_flag(inode
, NODATACOW
))
290 ret
= run_delalloc_nocow(inode
, start
, end
);
292 ret
= cow_file_range(inode
, start
, end
);
297 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
298 unsigned long old
, unsigned long bits
)
301 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
302 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
303 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
304 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
305 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
306 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
311 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
312 unsigned long old
, unsigned long bits
)
314 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
315 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
318 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
319 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
322 root
->fs_info
->delalloc_bytes
= 0;
323 BTRFS_I(inode
)->delalloc_bytes
= 0;
325 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
326 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
328 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
333 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
334 size_t size
, struct bio
*bio
)
336 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
337 struct btrfs_mapping_tree
*map_tree
;
338 u64 logical
= bio
->bi_sector
<< 9;
343 length
= bio
->bi_size
;
344 map_tree
= &root
->fs_info
->mapping_tree
;
346 ret
= btrfs_map_block(map_tree
, READ
, logical
,
347 &map_length
, NULL
, 0);
349 if (map_length
< length
+ size
) {
355 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
358 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
361 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
364 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
367 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
370 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
373 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
376 if (!(rw
& (1 << BIO_RW
))) {
377 if (!btrfs_test_opt(root
, NODATASUM
) &&
378 !btrfs_test_flag(inode
, NODATASUM
)) {
379 btrfs_lookup_bio_sums(root
, inode
, bio
);
384 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
385 inode
, rw
, bio
, mirror_num
,
386 __btrfs_submit_bio_hook
);
388 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
391 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
392 struct inode
*inode
, u64 file_offset
,
393 struct list_head
*list
)
395 struct list_head
*cur
;
396 struct btrfs_ordered_sum
*sum
;
398 btrfs_set_trans_block_group(trans
, inode
);
399 list_for_each(cur
, list
) {
400 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
401 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
402 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
404 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
409 struct btrfs_writepage_fixup
{
411 struct btrfs_work work
;
414 /* see btrfs_writepage_start_hook for details on why this is required */
415 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
417 struct btrfs_writepage_fixup
*fixup
;
418 struct btrfs_ordered_extent
*ordered
;
424 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
428 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
429 ClearPageChecked(page
);
433 inode
= page
->mapping
->host
;
434 page_start
= page_offset(page
);
435 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
437 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
439 /* already ordered? We're done */
440 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
441 EXTENT_ORDERED
, 0)) {
445 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
447 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
450 btrfs_start_ordered_extent(inode
, ordered
, 1);
454 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
456 ClearPageChecked(page
);
458 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
461 page_cache_release(page
);
465 * There are a few paths in the higher layers of the kernel that directly
466 * set the page dirty bit without asking the filesystem if it is a
467 * good idea. This causes problems because we want to make sure COW
468 * properly happens and the data=ordered rules are followed.
470 * In our case any range that doesn't have the EXTENT_ORDERED bit set
471 * hasn't been properly setup for IO. We kick off an async process
472 * to fix it up. The async helper will wait for ordered extents, set
473 * the delalloc bit and make it safe to write the page.
475 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
477 struct inode
*inode
= page
->mapping
->host
;
478 struct btrfs_writepage_fixup
*fixup
;
479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
482 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
487 if (PageChecked(page
))
490 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
494 SetPageChecked(page
);
495 page_cache_get(page
);
496 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
498 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
502 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
504 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
505 struct btrfs_trans_handle
*trans
;
506 struct btrfs_ordered_extent
*ordered_extent
;
507 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
508 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
509 struct extent_map
*em
;
510 struct extent_map
*em_orig
;
514 struct list_head list
;
515 struct btrfs_key ins
;
519 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
523 trans
= btrfs_join_transaction(root
, 1);
525 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
526 BUG_ON(!ordered_extent
);
528 lock_extent(io_tree
, ordered_extent
->file_offset
,
529 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
532 INIT_LIST_HEAD(&list
);
534 ins
.objectid
= ordered_extent
->start
;
535 ins
.offset
= ordered_extent
->len
;
536 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
538 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
539 trans
->transid
, inode
->i_ino
,
540 ordered_extent
->file_offset
, &ins
);
543 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
545 spin_lock(&em_tree
->lock
);
546 clear_start
= ordered_extent
->file_offset
;
547 clear_end
= ordered_extent
->file_offset
+ ordered_extent
->len
;
548 em
= lookup_extent_mapping(em_tree
, clear_start
,
549 ordered_extent
->len
);
551 while(em
&& clear_start
< extent_map_end(em
) && clear_end
> em
->start
) {
552 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
553 rb
= rb_next(&em
->rb_node
);
556 em
= rb_entry(rb
, struct extent_map
, rb_node
);
558 free_extent_map(em_orig
);
559 spin_unlock(&em_tree
->lock
);
561 ret
= btrfs_drop_extents(trans
, root
, inode
,
562 ordered_extent
->file_offset
,
563 ordered_extent
->file_offset
+
565 ordered_extent
->file_offset
, &alloc_hint
);
567 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
568 ordered_extent
->file_offset
,
569 ordered_extent
->start
,
571 ordered_extent
->len
, 0);
574 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
575 ordered_extent
->file_offset
+
576 ordered_extent
->len
- 1);
577 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
579 inode
->i_blocks
+= ordered_extent
->len
>> 9;
580 unlock_extent(io_tree
, ordered_extent
->file_offset
,
581 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
583 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
584 &ordered_extent
->list
);
586 btrfs_ordered_update_i_size(inode
, ordered_extent
);
587 btrfs_remove_ordered_extent(inode
, ordered_extent
);
590 btrfs_put_ordered_extent(ordered_extent
);
591 /* once for the tree */
592 btrfs_put_ordered_extent(ordered_extent
);
594 btrfs_update_inode(trans
, root
, inode
);
595 btrfs_end_transaction(trans
, root
);
599 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
600 struct extent_state
*state
, int uptodate
)
602 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
605 struct io_failure_record
{
613 int btrfs_io_failed_hook(struct bio
*failed_bio
,
614 struct page
*page
, u64 start
, u64 end
,
615 struct extent_state
*state
)
617 struct io_failure_record
*failrec
= NULL
;
619 struct extent_map
*em
;
620 struct inode
*inode
= page
->mapping
->host
;
621 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
622 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
629 ret
= get_state_private(failure_tree
, start
, &private);
631 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
634 failrec
->start
= start
;
635 failrec
->len
= end
- start
+ 1;
636 failrec
->last_mirror
= 0;
638 spin_lock(&em_tree
->lock
);
639 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
640 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
644 spin_unlock(&em_tree
->lock
);
646 if (!em
|| IS_ERR(em
)) {
650 logical
= start
- em
->start
;
651 logical
= em
->block_start
+ logical
;
652 failrec
->logical
= logical
;
654 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
655 EXTENT_DIRTY
, GFP_NOFS
);
656 set_state_private(failure_tree
, start
,
657 (u64
)(unsigned long)failrec
);
659 failrec
= (struct io_failure_record
*)(unsigned long)private;
661 num_copies
= btrfs_num_copies(
662 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
663 failrec
->logical
, failrec
->len
);
664 failrec
->last_mirror
++;
666 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
667 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
670 if (state
&& state
->start
!= failrec
->start
)
672 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
674 if (!state
|| failrec
->last_mirror
> num_copies
) {
675 set_state_private(failure_tree
, failrec
->start
, 0);
676 clear_extent_bits(failure_tree
, failrec
->start
,
677 failrec
->start
+ failrec
->len
- 1,
678 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
682 bio
= bio_alloc(GFP_NOFS
, 1);
683 bio
->bi_private
= state
;
684 bio
->bi_end_io
= failed_bio
->bi_end_io
;
685 bio
->bi_sector
= failrec
->logical
>> 9;
686 bio
->bi_bdev
= failed_bio
->bi_bdev
;
688 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
689 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
694 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
695 failrec
->last_mirror
);
699 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
703 struct io_failure_record
*failure
;
707 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
708 (u64
)-1, 1, EXTENT_DIRTY
)) {
709 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
710 start
, &private_failure
);
712 failure
= (struct io_failure_record
*)(unsigned long)
714 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
716 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
718 failure
->start
+ failure
->len
- 1,
719 EXTENT_DIRTY
| EXTENT_LOCKED
,
727 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
728 struct extent_state
*state
)
730 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
731 struct inode
*inode
= page
->mapping
->host
;
732 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
734 u64
private = ~(u32
)0;
736 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
740 if (btrfs_test_opt(root
, NODATASUM
) ||
741 btrfs_test_flag(inode
, NODATASUM
))
743 if (state
&& state
->start
== start
) {
744 private = state
->private;
747 ret
= get_state_private(io_tree
, start
, &private);
749 local_irq_save(flags
);
750 kaddr
= kmap_atomic(page
, KM_IRQ0
);
754 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
755 btrfs_csum_final(csum
, (char *)&csum
);
756 if (csum
!= private) {
759 kunmap_atomic(kaddr
, KM_IRQ0
);
760 local_irq_restore(flags
);
762 /* if the io failure tree for this inode is non-empty,
763 * check to see if we've recovered from a failed IO
765 btrfs_clean_io_failures(inode
, start
);
769 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
770 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
772 memset(kaddr
+ offset
, 1, end
- start
+ 1);
773 flush_dcache_page(page
);
774 kunmap_atomic(kaddr
, KM_IRQ0
);
775 local_irq_restore(flags
);
782 * This creates an orphan entry for the given inode in case something goes
783 * wrong in the middle of an unlink/truncate.
785 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
787 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
790 spin_lock(&root
->list_lock
);
792 /* already on the orphan list, we're good */
793 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
794 spin_unlock(&root
->list_lock
);
798 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
800 spin_unlock(&root
->list_lock
);
803 * insert an orphan item to track this unlinked/truncated file
805 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
811 * We have done the truncate/delete so we can go ahead and remove the orphan
812 * item for this particular inode.
814 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
816 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
819 spin_lock(&root
->list_lock
);
821 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
822 spin_unlock(&root
->list_lock
);
826 list_del_init(&BTRFS_I(inode
)->i_orphan
);
828 spin_unlock(&root
->list_lock
);
832 spin_unlock(&root
->list_lock
);
834 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
840 * this cleans up any orphans that may be left on the list from the last use
843 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
845 struct btrfs_path
*path
;
846 struct extent_buffer
*leaf
;
847 struct btrfs_item
*item
;
848 struct btrfs_key key
, found_key
;
849 struct btrfs_trans_handle
*trans
;
851 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
853 /* don't do orphan cleanup if the fs is readonly. */
854 if (root
->inode
->i_sb
->s_flags
& MS_RDONLY
)
857 path
= btrfs_alloc_path();
862 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
863 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
864 key
.offset
= (u64
)-1;
866 trans
= btrfs_start_transaction(root
, 1);
867 btrfs_set_trans_block_group(trans
, root
->inode
);
870 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
872 printk(KERN_ERR
"Error searching slot for orphan: %d"
878 * if ret == 0 means we found what we were searching for, which
879 * is weird, but possible, so only screw with path if we didnt
880 * find the key and see if we have stuff that matches
883 if (path
->slots
[0] == 0)
888 /* pull out the item */
889 leaf
= path
->nodes
[0];
890 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
891 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
893 /* make sure the item matches what we want */
894 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
896 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
899 /* release the path since we're done with it */
900 btrfs_release_path(root
, path
);
903 * this is where we are basically btrfs_lookup, without the
904 * crossing root thing. we store the inode number in the
905 * offset of the orphan item.
907 inode
= btrfs_iget_locked(root
->inode
->i_sb
,
908 found_key
.offset
, root
);
912 if (inode
->i_state
& I_NEW
) {
913 BTRFS_I(inode
)->root
= root
;
915 /* have to set the location manually */
916 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
917 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
918 BTRFS_I(inode
)->location
.offset
= 0;
920 btrfs_read_locked_inode(inode
);
921 unlock_new_inode(inode
);
925 * add this inode to the orphan list so btrfs_orphan_del does
926 * the proper thing when we hit it
928 spin_lock(&root
->list_lock
);
929 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
930 spin_unlock(&root
->list_lock
);
933 * if this is a bad inode, means we actually succeeded in
934 * removing the inode, but not the orphan record, which means
935 * we need to manually delete the orphan since iput will just
938 if (is_bad_inode(inode
)) {
939 btrfs_orphan_del(trans
, inode
);
944 /* if we have links, this was a truncate, lets do that */
945 if (inode
->i_nlink
) {
947 btrfs_truncate(inode
);
952 /* this will do delete_inode and everything for us */
957 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
959 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
961 btrfs_free_path(path
);
962 btrfs_end_transaction(trans
, root
);
965 void btrfs_read_locked_inode(struct inode
*inode
)
967 struct btrfs_path
*path
;
968 struct extent_buffer
*leaf
;
969 struct btrfs_inode_item
*inode_item
;
970 struct btrfs_timespec
*tspec
;
971 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
972 struct btrfs_key location
;
973 u64 alloc_group_block
;
977 path
= btrfs_alloc_path();
979 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
981 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
985 leaf
= path
->nodes
[0];
986 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
987 struct btrfs_inode_item
);
989 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
990 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
991 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
992 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
993 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
995 tspec
= btrfs_inode_atime(inode_item
);
996 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
997 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
999 tspec
= btrfs_inode_mtime(inode_item
);
1000 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1001 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1003 tspec
= btrfs_inode_ctime(inode_item
);
1004 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1005 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1007 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
1008 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
1010 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
1012 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1014 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
1015 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
1017 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
1018 if (!BTRFS_I(inode
)->block_group
) {
1019 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
1021 BTRFS_BLOCK_GROUP_METADATA
, 0);
1023 btrfs_free_path(path
);
1026 switch (inode
->i_mode
& S_IFMT
) {
1028 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1029 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1030 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1031 inode
->i_fop
= &btrfs_file_operations
;
1032 inode
->i_op
= &btrfs_file_inode_operations
;
1035 inode
->i_fop
= &btrfs_dir_file_operations
;
1036 if (root
== root
->fs_info
->tree_root
)
1037 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
1039 inode
->i_op
= &btrfs_dir_inode_operations
;
1042 inode
->i_op
= &btrfs_symlink_inode_operations
;
1043 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
1044 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1047 init_special_inode(inode
, inode
->i_mode
, rdev
);
1053 btrfs_free_path(path
);
1054 make_bad_inode(inode
);
1057 static void fill_inode_item(struct extent_buffer
*leaf
,
1058 struct btrfs_inode_item
*item
,
1059 struct inode
*inode
)
1061 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
1062 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
1063 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
1064 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
1065 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
1067 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
1068 inode
->i_atime
.tv_sec
);
1069 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
1070 inode
->i_atime
.tv_nsec
);
1072 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
1073 inode
->i_mtime
.tv_sec
);
1074 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
1075 inode
->i_mtime
.tv_nsec
);
1077 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
1078 inode
->i_ctime
.tv_sec
);
1079 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
1080 inode
->i_ctime
.tv_nsec
);
1082 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
1083 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
1084 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
1085 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
1086 btrfs_set_inode_block_group(leaf
, item
,
1087 BTRFS_I(inode
)->block_group
->key
.objectid
);
1090 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
1091 struct btrfs_root
*root
,
1092 struct inode
*inode
)
1094 struct btrfs_inode_item
*inode_item
;
1095 struct btrfs_path
*path
;
1096 struct extent_buffer
*leaf
;
1099 path
= btrfs_alloc_path();
1101 ret
= btrfs_lookup_inode(trans
, root
, path
,
1102 &BTRFS_I(inode
)->location
, 1);
1109 leaf
= path
->nodes
[0];
1110 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1111 struct btrfs_inode_item
);
1113 fill_inode_item(leaf
, inode_item
, inode
);
1114 btrfs_mark_buffer_dirty(leaf
);
1115 btrfs_set_inode_last_trans(trans
, inode
);
1118 btrfs_free_path(path
);
1123 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
1124 struct btrfs_root
*root
,
1126 struct dentry
*dentry
)
1128 struct btrfs_path
*path
;
1129 const char *name
= dentry
->d_name
.name
;
1130 int name_len
= dentry
->d_name
.len
;
1132 struct extent_buffer
*leaf
;
1133 struct btrfs_dir_item
*di
;
1134 struct btrfs_key key
;
1137 path
= btrfs_alloc_path();
1143 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1144 name
, name_len
, -1);
1153 leaf
= path
->nodes
[0];
1154 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1155 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1158 btrfs_release_path(root
, path
);
1160 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1161 dentry
->d_inode
->i_ino
,
1162 dentry
->d_parent
->d_inode
->i_ino
, &index
);
1164 printk("failed to delete reference to %.*s, "
1165 "inode %lu parent %lu\n", name_len
, name
,
1166 dentry
->d_inode
->i_ino
,
1167 dentry
->d_parent
->d_inode
->i_ino
);
1171 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1172 index
, name
, name_len
, -1);
1181 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1182 btrfs_release_path(root
, path
);
1184 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
1186 btrfs_free_path(path
);
1188 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1189 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1190 btrfs_update_inode(trans
, root
, dir
);
1191 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1192 dentry
->d_inode
->i_nlink
--;
1194 drop_nlink(dentry
->d_inode
);
1196 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1197 dir
->i_sb
->s_dirt
= 1;
1202 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1204 struct btrfs_root
*root
;
1205 struct btrfs_trans_handle
*trans
;
1206 struct inode
*inode
= dentry
->d_inode
;
1208 unsigned long nr
= 0;
1210 root
= BTRFS_I(dir
)->root
;
1212 ret
= btrfs_check_free_space(root
, 1, 1);
1216 trans
= btrfs_start_transaction(root
, 1);
1218 btrfs_set_trans_block_group(trans
, dir
);
1219 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1221 if (inode
->i_nlink
== 0)
1222 ret
= btrfs_orphan_add(trans
, inode
);
1224 nr
= trans
->blocks_used
;
1226 btrfs_end_transaction_throttle(trans
, root
);
1228 btrfs_btree_balance_dirty(root
, nr
);
1232 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1234 struct inode
*inode
= dentry
->d_inode
;
1237 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1238 struct btrfs_trans_handle
*trans
;
1239 unsigned long nr
= 0;
1241 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1245 ret
= btrfs_check_free_space(root
, 1, 1);
1249 trans
= btrfs_start_transaction(root
, 1);
1250 btrfs_set_trans_block_group(trans
, dir
);
1252 err
= btrfs_orphan_add(trans
, inode
);
1256 /* now the directory is empty */
1257 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1259 btrfs_i_size_write(inode
, 0);
1263 nr
= trans
->blocks_used
;
1264 ret
= btrfs_end_transaction_throttle(trans
, root
);
1266 btrfs_btree_balance_dirty(root
, nr
);
1274 * this can truncate away extent items, csum items and directory items.
1275 * It starts at a high offset and removes keys until it can't find
1276 * any higher than i_size.
1278 * csum items that cross the new i_size are truncated to the new size
1281 * min_type is the minimum key type to truncate down to. If set to 0, this
1282 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1284 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1285 struct btrfs_root
*root
,
1286 struct inode
*inode
,
1290 struct btrfs_path
*path
;
1291 struct btrfs_key key
;
1292 struct btrfs_key found_key
;
1294 struct extent_buffer
*leaf
;
1295 struct btrfs_file_extent_item
*fi
;
1296 u64 extent_start
= 0;
1297 u64 extent_num_bytes
= 0;
1303 int pending_del_nr
= 0;
1304 int pending_del_slot
= 0;
1305 int extent_type
= -1;
1306 u64 mask
= root
->sectorsize
- 1;
1308 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1309 path
= btrfs_alloc_path();
1313 /* FIXME, add redo link to tree so we don't leak on crash */
1314 key
.objectid
= inode
->i_ino
;
1315 key
.offset
= (u64
)-1;
1318 btrfs_init_path(path
);
1320 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1325 BUG_ON(path
->slots
[0] == 0);
1331 leaf
= path
->nodes
[0];
1332 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1333 found_type
= btrfs_key_type(&found_key
);
1335 if (found_key
.objectid
!= inode
->i_ino
)
1338 if (found_type
< min_type
)
1341 item_end
= found_key
.offset
;
1342 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1343 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1344 struct btrfs_file_extent_item
);
1345 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1346 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1348 btrfs_file_extent_num_bytes(leaf
, fi
);
1349 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1350 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1352 item_end
+= btrfs_file_extent_inline_len(leaf
,
1357 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1358 ret
= btrfs_csum_truncate(trans
, root
, path
,
1362 if (item_end
< inode
->i_size
) {
1363 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1364 found_type
= BTRFS_INODE_ITEM_KEY
;
1365 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1366 found_type
= BTRFS_CSUM_ITEM_KEY
;
1367 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1368 found_type
= BTRFS_XATTR_ITEM_KEY
;
1369 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1370 found_type
= BTRFS_INODE_REF_KEY
;
1371 } else if (found_type
) {
1376 btrfs_set_key_type(&key
, found_type
);
1379 if (found_key
.offset
>= inode
->i_size
)
1385 /* FIXME, shrink the extent if the ref count is only 1 */
1386 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1389 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1391 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1393 u64 orig_num_bytes
=
1394 btrfs_file_extent_num_bytes(leaf
, fi
);
1395 extent_num_bytes
= inode
->i_size
-
1396 found_key
.offset
+ root
->sectorsize
- 1;
1397 extent_num_bytes
= extent_num_bytes
&
1398 ~((u64
)root
->sectorsize
- 1);
1399 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1401 num_dec
= (orig_num_bytes
-
1403 if (extent_start
!= 0)
1404 dec_i_blocks(inode
, num_dec
);
1405 btrfs_mark_buffer_dirty(leaf
);
1408 btrfs_file_extent_disk_num_bytes(leaf
,
1410 /* FIXME blocksize != 4096 */
1411 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1412 if (extent_start
!= 0) {
1414 dec_i_blocks(inode
, num_dec
);
1416 root_gen
= btrfs_header_generation(leaf
);
1417 root_owner
= btrfs_header_owner(leaf
);
1419 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1421 u32 newsize
= inode
->i_size
- found_key
.offset
;
1422 dec_i_blocks(inode
, item_end
+ 1 -
1423 found_key
.offset
- newsize
);
1425 btrfs_file_extent_calc_inline_size(newsize
);
1426 ret
= btrfs_truncate_item(trans
, root
, path
,
1430 dec_i_blocks(inode
, item_end
+ 1 -
1436 if (!pending_del_nr
) {
1437 /* no pending yet, add ourselves */
1438 pending_del_slot
= path
->slots
[0];
1440 } else if (pending_del_nr
&&
1441 path
->slots
[0] + 1 == pending_del_slot
) {
1442 /* hop on the pending chunk */
1444 pending_del_slot
= path
->slots
[0];
1446 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1452 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1455 root_gen
, inode
->i_ino
,
1456 found_key
.offset
, 0);
1460 if (path
->slots
[0] == 0) {
1463 btrfs_release_path(root
, path
);
1468 if (pending_del_nr
&&
1469 path
->slots
[0] + 1 != pending_del_slot
) {
1470 struct btrfs_key debug
;
1472 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1474 ret
= btrfs_del_items(trans
, root
, path
,
1479 btrfs_release_path(root
, path
);
1485 if (pending_del_nr
) {
1486 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1489 btrfs_free_path(path
);
1490 inode
->i_sb
->s_dirt
= 1;
1495 * taken from block_truncate_page, but does cow as it zeros out
1496 * any bytes left in the last page in the file.
1498 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1500 struct inode
*inode
= mapping
->host
;
1501 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1502 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1503 struct btrfs_ordered_extent
*ordered
;
1505 u32 blocksize
= root
->sectorsize
;
1506 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1507 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1513 if ((offset
& (blocksize
- 1)) == 0)
1518 page
= grab_cache_page(mapping
, index
);
1522 page_start
= page_offset(page
);
1523 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1525 if (!PageUptodate(page
)) {
1526 ret
= btrfs_readpage(NULL
, page
);
1528 if (page
->mapping
!= mapping
) {
1530 page_cache_release(page
);
1533 if (!PageUptodate(page
)) {
1538 wait_on_page_writeback(page
);
1540 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1541 set_page_extent_mapped(page
);
1543 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1545 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1547 page_cache_release(page
);
1548 btrfs_start_ordered_extent(inode
, ordered
, 1);
1549 btrfs_put_ordered_extent(ordered
);
1553 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1554 page_end
, GFP_NOFS
);
1556 if (offset
!= PAGE_CACHE_SIZE
) {
1558 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1559 flush_dcache_page(page
);
1562 ClearPageChecked(page
);
1563 set_page_dirty(page
);
1564 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1568 page_cache_release(page
);
1573 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1575 struct inode
*inode
= dentry
->d_inode
;
1578 err
= inode_change_ok(inode
, attr
);
1582 if (S_ISREG(inode
->i_mode
) &&
1583 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1584 struct btrfs_trans_handle
*trans
;
1585 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1586 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1588 u64 mask
= root
->sectorsize
- 1;
1589 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1590 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1594 if (attr
->ia_size
<= hole_start
)
1597 err
= btrfs_check_free_space(root
, 1, 0);
1601 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1603 hole_size
= block_end
- hole_start
;
1604 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1605 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1607 trans
= btrfs_start_transaction(root
, 1);
1608 btrfs_set_trans_block_group(trans
, inode
);
1609 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1610 err
= btrfs_drop_extents(trans
, root
, inode
,
1611 hole_start
, block_end
, hole_start
,
1614 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1615 err
= btrfs_insert_file_extent(trans
, root
,
1619 btrfs_drop_extent_cache(inode
, hole_start
,
1621 btrfs_check_file(root
, inode
);
1623 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1624 btrfs_end_transaction(trans
, root
);
1625 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1630 err
= inode_setattr(inode
, attr
);
1632 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
1633 err
= btrfs_acl_chmod(inode
);
1638 void btrfs_delete_inode(struct inode
*inode
)
1640 struct btrfs_trans_handle
*trans
;
1641 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1645 truncate_inode_pages(&inode
->i_data
, 0);
1646 if (is_bad_inode(inode
)) {
1647 btrfs_orphan_del(NULL
, inode
);
1650 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1652 btrfs_i_size_write(inode
, 0);
1653 trans
= btrfs_start_transaction(root
, 1);
1655 btrfs_set_trans_block_group(trans
, inode
);
1656 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1658 btrfs_orphan_del(NULL
, inode
);
1659 goto no_delete_lock
;
1662 btrfs_orphan_del(trans
, inode
);
1664 nr
= trans
->blocks_used
;
1667 btrfs_end_transaction(trans
, root
);
1668 btrfs_btree_balance_dirty(root
, nr
);
1672 nr
= trans
->blocks_used
;
1673 btrfs_end_transaction(trans
, root
);
1674 btrfs_btree_balance_dirty(root
, nr
);
1680 * this returns the key found in the dir entry in the location pointer.
1681 * If no dir entries were found, location->objectid is 0.
1683 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1684 struct btrfs_key
*location
)
1686 const char *name
= dentry
->d_name
.name
;
1687 int namelen
= dentry
->d_name
.len
;
1688 struct btrfs_dir_item
*di
;
1689 struct btrfs_path
*path
;
1690 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1693 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1694 location
->objectid
= dir
->i_ino
;
1695 location
->type
= BTRFS_INODE_ITEM_KEY
;
1696 location
->offset
= 0;
1699 path
= btrfs_alloc_path();
1702 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1703 struct btrfs_key key
;
1704 struct extent_buffer
*leaf
;
1707 key
.objectid
= dir
->i_ino
;
1708 key
.offset
= (u64
)-1;
1709 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1710 if (ret
< 0 || path
->slots
[0] == 0)
1712 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1715 leaf
= path
->nodes
[0];
1716 slot
= path
->slots
[0] - 1;
1718 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1719 if (key
.objectid
!= dir
->i_ino
||
1720 key
.type
!= BTRFS_INODE_REF_KEY
) {
1723 location
->objectid
= key
.offset
;
1724 location
->type
= BTRFS_INODE_ITEM_KEY
;
1725 location
->offset
= 0;
1729 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1733 if (!di
|| IS_ERR(di
)) {
1736 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1738 btrfs_free_path(path
);
1741 location
->objectid
= 0;
1746 * when we hit a tree root in a directory, the btrfs part of the inode
1747 * needs to be changed to reflect the root directory of the tree root. This
1748 * is kind of like crossing a mount point.
1750 static int fixup_tree_root_location(struct btrfs_root
*root
,
1751 struct btrfs_key
*location
,
1752 struct btrfs_root
**sub_root
,
1753 struct dentry
*dentry
)
1755 struct btrfs_root_item
*ri
;
1757 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1759 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1762 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1763 dentry
->d_name
.name
,
1764 dentry
->d_name
.len
);
1765 if (IS_ERR(*sub_root
))
1766 return PTR_ERR(*sub_root
);
1768 ri
= &(*sub_root
)->root_item
;
1769 location
->objectid
= btrfs_root_dirid(ri
);
1770 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1771 location
->offset
= 0;
1776 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1778 struct btrfs_iget_args
*args
= p
;
1779 inode
->i_ino
= args
->ino
;
1780 BTRFS_I(inode
)->root
= args
->root
;
1781 BTRFS_I(inode
)->delalloc_bytes
= 0;
1782 BTRFS_I(inode
)->disk_i_size
= 0;
1783 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1784 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1785 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1786 inode
->i_mapping
, GFP_NOFS
);
1787 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1788 inode
->i_mapping
, GFP_NOFS
);
1789 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1790 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1791 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1795 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1797 struct btrfs_iget_args
*args
= opaque
;
1798 return (args
->ino
== inode
->i_ino
&&
1799 args
->root
== BTRFS_I(inode
)->root
);
1802 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1805 struct btrfs_iget_args args
;
1806 args
.ino
= objectid
;
1807 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1812 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1815 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1816 struct btrfs_root
*root
)
1818 struct inode
*inode
;
1819 struct btrfs_iget_args args
;
1820 args
.ino
= objectid
;
1823 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1824 btrfs_init_locked_inode
,
1829 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1830 struct nameidata
*nd
)
1832 struct inode
* inode
;
1833 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1834 struct btrfs_root
*root
= bi
->root
;
1835 struct btrfs_root
*sub_root
= root
;
1836 struct btrfs_key location
;
1837 int ret
, do_orphan
= 0;
1839 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1840 return ERR_PTR(-ENAMETOOLONG
);
1842 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1845 return ERR_PTR(ret
);
1848 if (location
.objectid
) {
1849 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1852 return ERR_PTR(ret
);
1854 return ERR_PTR(-ENOENT
);
1856 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1859 return ERR_PTR(-EACCES
);
1860 if (inode
->i_state
& I_NEW
) {
1861 /* the inode and parent dir are two different roots */
1862 if (sub_root
!= root
) {
1864 sub_root
->inode
= inode
;
1867 BTRFS_I(inode
)->root
= sub_root
;
1868 memcpy(&BTRFS_I(inode
)->location
, &location
,
1870 btrfs_read_locked_inode(inode
);
1871 unlock_new_inode(inode
);
1875 if (unlikely(do_orphan
))
1876 btrfs_orphan_cleanup(sub_root
);
1878 return d_splice_alias(inode
, dentry
);
1881 static unsigned char btrfs_filetype_table
[] = {
1882 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1885 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1887 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1888 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1889 struct btrfs_item
*item
;
1890 struct btrfs_dir_item
*di
;
1891 struct btrfs_key key
;
1892 struct btrfs_key found_key
;
1893 struct btrfs_path
*path
;
1896 struct extent_buffer
*leaf
;
1899 unsigned char d_type
;
1904 int key_type
= BTRFS_DIR_INDEX_KEY
;
1909 /* FIXME, use a real flag for deciding about the key type */
1910 if (root
->fs_info
->tree_root
== root
)
1911 key_type
= BTRFS_DIR_ITEM_KEY
;
1913 /* special case for "." */
1914 if (filp
->f_pos
== 0) {
1915 over
= filldir(dirent
, ".", 1,
1923 key
.objectid
= inode
->i_ino
;
1924 path
= btrfs_alloc_path();
1927 /* special case for .., just use the back ref */
1928 if (filp
->f_pos
== 1) {
1929 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1930 key
.offset
= (u64
)-1;
1931 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1932 if (ret
< 0 || path
->slots
[0] == 0) {
1933 btrfs_release_path(root
, path
);
1934 goto read_dir_items
;
1937 leaf
= path
->nodes
[0];
1938 slot
= path
->slots
[0] - 1;
1939 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1940 btrfs_release_path(root
, path
);
1941 if (found_key
.objectid
!= key
.objectid
||
1942 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1943 goto read_dir_items
;
1944 over
= filldir(dirent
, "..", 2,
1945 2, found_key
.offset
, DT_DIR
);
1952 btrfs_set_key_type(&key
, key_type
);
1953 key
.offset
= filp
->f_pos
;
1955 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1960 leaf
= path
->nodes
[0];
1961 nritems
= btrfs_header_nritems(leaf
);
1962 slot
= path
->slots
[0];
1963 if (advance
|| slot
>= nritems
) {
1964 if (slot
>= nritems
-1) {
1965 ret
= btrfs_next_leaf(root
, path
);
1968 leaf
= path
->nodes
[0];
1969 nritems
= btrfs_header_nritems(leaf
);
1970 slot
= path
->slots
[0];
1977 item
= btrfs_item_nr(leaf
, slot
);
1978 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1980 if (found_key
.objectid
!= key
.objectid
)
1982 if (btrfs_key_type(&found_key
) != key_type
)
1984 if (found_key
.offset
< filp
->f_pos
)
1987 filp
->f_pos
= found_key
.offset
;
1989 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1991 di_total
= btrfs_item_size(leaf
, item
);
1992 while(di_cur
< di_total
) {
1993 struct btrfs_key location
;
1995 name_len
= btrfs_dir_name_len(leaf
, di
);
1996 if (name_len
< 32) {
1997 name_ptr
= tmp_name
;
1999 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
2002 read_extent_buffer(leaf
, name_ptr
,
2003 (unsigned long)(di
+ 1), name_len
);
2005 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
2006 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
2007 over
= filldir(dirent
, name_ptr
, name_len
,
2012 if (name_ptr
!= tmp_name
)
2017 di_len
= btrfs_dir_name_len(leaf
, di
) +
2018 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
2020 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
2023 if (key_type
== BTRFS_DIR_INDEX_KEY
)
2024 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
2030 btrfs_free_path(path
);
2034 int btrfs_write_inode(struct inode
*inode
, int wait
)
2036 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2037 struct btrfs_trans_handle
*trans
;
2041 trans
= btrfs_join_transaction(root
, 1);
2042 btrfs_set_trans_block_group(trans
, inode
);
2043 ret
= btrfs_commit_transaction(trans
, root
);
2049 * This is somewhat expensive, updating the tree every time the
2050 * inode changes. But, it is most likely to find the inode in cache.
2051 * FIXME, needs more benchmarking...there are no reasons other than performance
2052 * to keep or drop this code.
2054 void btrfs_dirty_inode(struct inode
*inode
)
2056 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2057 struct btrfs_trans_handle
*trans
;
2059 trans
= btrfs_join_transaction(root
, 1);
2060 btrfs_set_trans_block_group(trans
, inode
);
2061 btrfs_update_inode(trans
, root
, inode
);
2062 btrfs_end_transaction(trans
, root
);
2065 static int btrfs_set_inode_index_count(struct inode
*inode
)
2067 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2068 struct btrfs_key key
, found_key
;
2069 struct btrfs_path
*path
;
2070 struct extent_buffer
*leaf
;
2073 key
.objectid
= inode
->i_ino
;
2074 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
2075 key
.offset
= (u64
)-1;
2077 path
= btrfs_alloc_path();
2081 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2084 /* FIXME: we should be able to handle this */
2090 * MAGIC NUMBER EXPLANATION:
2091 * since we search a directory based on f_pos we have to start at 2
2092 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2093 * else has to start at 2
2095 if (path
->slots
[0] == 0) {
2096 BTRFS_I(inode
)->index_cnt
= 2;
2102 leaf
= path
->nodes
[0];
2103 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2105 if (found_key
.objectid
!= inode
->i_ino
||
2106 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
2107 BTRFS_I(inode
)->index_cnt
= 2;
2111 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
2113 btrfs_free_path(path
);
2117 static int btrfs_set_inode_index(struct inode
*dir
, struct inode
*inode
)
2121 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
2122 ret
= btrfs_set_inode_index_count(dir
);
2127 BTRFS_I(inode
)->index
= BTRFS_I(dir
)->index_cnt
;
2128 BTRFS_I(dir
)->index_cnt
++;
2133 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
2134 struct btrfs_root
*root
,
2136 const char *name
, int name_len
,
2139 struct btrfs_block_group_cache
*group
,
2142 struct inode
*inode
;
2143 struct btrfs_inode_item
*inode_item
;
2144 struct btrfs_block_group_cache
*new_inode_group
;
2145 struct btrfs_key
*location
;
2146 struct btrfs_path
*path
;
2147 struct btrfs_inode_ref
*ref
;
2148 struct btrfs_key key
[2];
2154 path
= btrfs_alloc_path();
2157 inode
= new_inode(root
->fs_info
->sb
);
2159 return ERR_PTR(-ENOMEM
);
2162 ret
= btrfs_set_inode_index(dir
, inode
);
2164 return ERR_PTR(ret
);
2166 BTRFS_I(inode
)->index
= 0;
2169 * index_cnt is ignored for everything but a dir,
2170 * btrfs_get_inode_index_count has an explanation for the magic
2173 BTRFS_I(inode
)->index_cnt
= 2;
2175 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2176 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2177 inode
->i_mapping
, GFP_NOFS
);
2178 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2179 inode
->i_mapping
, GFP_NOFS
);
2180 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2181 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2182 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2183 BTRFS_I(inode
)->delalloc_bytes
= 0;
2184 BTRFS_I(inode
)->disk_i_size
= 0;
2185 BTRFS_I(inode
)->root
= root
;
2191 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
2192 BTRFS_BLOCK_GROUP_METADATA
, owner
);
2193 if (!new_inode_group
) {
2194 printk("find_block group failed\n");
2195 new_inode_group
= group
;
2197 BTRFS_I(inode
)->block_group
= new_inode_group
;
2198 BTRFS_I(inode
)->flags
= 0;
2200 key
[0].objectid
= objectid
;
2201 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
2204 key
[1].objectid
= objectid
;
2205 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
2206 key
[1].offset
= ref_objectid
;
2208 sizes
[0] = sizeof(struct btrfs_inode_item
);
2209 sizes
[1] = name_len
+ sizeof(*ref
);
2211 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
2215 if (objectid
> root
->highest_inode
)
2216 root
->highest_inode
= objectid
;
2218 inode
->i_uid
= current
->fsuid
;
2219 inode
->i_gid
= current
->fsgid
;
2220 inode
->i_mode
= mode
;
2221 inode
->i_ino
= objectid
;
2222 inode
->i_blocks
= 0;
2223 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2224 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2225 struct btrfs_inode_item
);
2226 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
2228 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2229 struct btrfs_inode_ref
);
2230 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
2231 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, BTRFS_I(inode
)->index
);
2232 ptr
= (unsigned long)(ref
+ 1);
2233 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
2235 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2236 btrfs_free_path(path
);
2238 location
= &BTRFS_I(inode
)->location
;
2239 location
->objectid
= objectid
;
2240 location
->offset
= 0;
2241 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2243 insert_inode_hash(inode
);
2247 BTRFS_I(dir
)->index_cnt
--;
2248 btrfs_free_path(path
);
2249 return ERR_PTR(ret
);
2252 static inline u8
btrfs_inode_type(struct inode
*inode
)
2254 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2257 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2258 struct dentry
*dentry
, struct inode
*inode
,
2262 struct btrfs_key key
;
2263 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
2264 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
2266 key
.objectid
= inode
->i_ino
;
2267 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2270 ret
= btrfs_insert_dir_item(trans
, root
,
2271 dentry
->d_name
.name
, dentry
->d_name
.len
,
2272 dentry
->d_parent
->d_inode
->i_ino
,
2273 &key
, btrfs_inode_type(inode
),
2274 BTRFS_I(inode
)->index
);
2277 ret
= btrfs_insert_inode_ref(trans
, root
,
2278 dentry
->d_name
.name
,
2281 parent_inode
->i_ino
,
2282 BTRFS_I(inode
)->index
);
2284 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2285 dentry
->d_name
.len
* 2);
2286 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2287 ret
= btrfs_update_inode(trans
, root
,
2288 dentry
->d_parent
->d_inode
);
2293 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2294 struct dentry
*dentry
, struct inode
*inode
,
2297 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
2299 d_instantiate(dentry
, inode
);
2307 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2308 int mode
, dev_t rdev
)
2310 struct btrfs_trans_handle
*trans
;
2311 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2312 struct inode
*inode
= NULL
;
2316 unsigned long nr
= 0;
2318 if (!new_valid_dev(rdev
))
2321 err
= btrfs_check_free_space(root
, 1, 0);
2325 trans
= btrfs_start_transaction(root
, 1);
2326 btrfs_set_trans_block_group(trans
, dir
);
2328 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2334 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2336 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2337 BTRFS_I(dir
)->block_group
, mode
);
2338 err
= PTR_ERR(inode
);
2342 err
= btrfs_init_acl(inode
, dir
);
2348 btrfs_set_trans_block_group(trans
, inode
);
2349 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2353 inode
->i_op
= &btrfs_special_inode_operations
;
2354 init_special_inode(inode
, inode
->i_mode
, rdev
);
2355 btrfs_update_inode(trans
, root
, inode
);
2357 dir
->i_sb
->s_dirt
= 1;
2358 btrfs_update_inode_block_group(trans
, inode
);
2359 btrfs_update_inode_block_group(trans
, dir
);
2361 nr
= trans
->blocks_used
;
2362 btrfs_end_transaction_throttle(trans
, root
);
2365 inode_dec_link_count(inode
);
2368 btrfs_btree_balance_dirty(root
, nr
);
2372 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2373 int mode
, struct nameidata
*nd
)
2375 struct btrfs_trans_handle
*trans
;
2376 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2377 struct inode
*inode
= NULL
;
2380 unsigned long nr
= 0;
2383 err
= btrfs_check_free_space(root
, 1, 0);
2386 trans
= btrfs_start_transaction(root
, 1);
2387 btrfs_set_trans_block_group(trans
, dir
);
2389 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2395 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2397 dentry
->d_parent
->d_inode
->i_ino
,
2398 objectid
, BTRFS_I(dir
)->block_group
, mode
);
2399 err
= PTR_ERR(inode
);
2403 err
= btrfs_init_acl(inode
, dir
);
2409 btrfs_set_trans_block_group(trans
, inode
);
2410 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2414 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2415 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2416 inode
->i_fop
= &btrfs_file_operations
;
2417 inode
->i_op
= &btrfs_file_inode_operations
;
2418 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2419 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2420 inode
->i_mapping
, GFP_NOFS
);
2421 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2422 inode
->i_mapping
, GFP_NOFS
);
2423 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2424 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2425 BTRFS_I(inode
)->delalloc_bytes
= 0;
2426 BTRFS_I(inode
)->disk_i_size
= 0;
2427 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2428 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2430 dir
->i_sb
->s_dirt
= 1;
2431 btrfs_update_inode_block_group(trans
, inode
);
2432 btrfs_update_inode_block_group(trans
, dir
);
2434 nr
= trans
->blocks_used
;
2435 btrfs_end_transaction_throttle(trans
, root
);
2438 inode_dec_link_count(inode
);
2441 btrfs_btree_balance_dirty(root
, nr
);
2445 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2446 struct dentry
*dentry
)
2448 struct btrfs_trans_handle
*trans
;
2449 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2450 struct inode
*inode
= old_dentry
->d_inode
;
2451 unsigned long nr
= 0;
2455 if (inode
->i_nlink
== 0)
2458 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2463 err
= btrfs_check_free_space(root
, 1, 0);
2466 err
= btrfs_set_inode_index(dir
, inode
);
2470 trans
= btrfs_start_transaction(root
, 1);
2472 btrfs_set_trans_block_group(trans
, dir
);
2473 atomic_inc(&inode
->i_count
);
2475 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2480 dir
->i_sb
->s_dirt
= 1;
2481 btrfs_update_inode_block_group(trans
, dir
);
2482 err
= btrfs_update_inode(trans
, root
, inode
);
2487 nr
= trans
->blocks_used
;
2488 btrfs_end_transaction_throttle(trans
, root
);
2491 inode_dec_link_count(inode
);
2494 btrfs_btree_balance_dirty(root
, nr
);
2498 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2500 struct inode
*inode
= NULL
;
2501 struct btrfs_trans_handle
*trans
;
2502 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2504 int drop_on_err
= 0;
2506 unsigned long nr
= 1;
2508 err
= btrfs_check_free_space(root
, 1, 0);
2512 trans
= btrfs_start_transaction(root
, 1);
2513 btrfs_set_trans_block_group(trans
, dir
);
2515 if (IS_ERR(trans
)) {
2516 err
= PTR_ERR(trans
);
2520 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2526 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2528 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2529 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2530 if (IS_ERR(inode
)) {
2531 err
= PTR_ERR(inode
);
2537 err
= btrfs_init_acl(inode
, dir
);
2541 inode
->i_op
= &btrfs_dir_inode_operations
;
2542 inode
->i_fop
= &btrfs_dir_file_operations
;
2543 btrfs_set_trans_block_group(trans
, inode
);
2545 btrfs_i_size_write(inode
, 0);
2546 err
= btrfs_update_inode(trans
, root
, inode
);
2550 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2554 d_instantiate(dentry
, inode
);
2556 dir
->i_sb
->s_dirt
= 1;
2557 btrfs_update_inode_block_group(trans
, inode
);
2558 btrfs_update_inode_block_group(trans
, dir
);
2561 nr
= trans
->blocks_used
;
2562 btrfs_end_transaction_throttle(trans
, root
);
2567 btrfs_btree_balance_dirty(root
, nr
);
2571 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2572 struct extent_map
*existing
,
2573 struct extent_map
*em
,
2574 u64 map_start
, u64 map_len
)
2578 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2579 start_diff
= map_start
- em
->start
;
2580 em
->start
= map_start
;
2582 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2583 em
->block_start
+= start_diff
;
2584 return add_extent_mapping(em_tree
, em
);
2587 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2588 size_t pg_offset
, u64 start
, u64 len
,
2594 u64 extent_start
= 0;
2596 u64 objectid
= inode
->i_ino
;
2598 struct btrfs_path
*path
= NULL
;
2599 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2600 struct btrfs_file_extent_item
*item
;
2601 struct extent_buffer
*leaf
;
2602 struct btrfs_key found_key
;
2603 struct extent_map
*em
= NULL
;
2604 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2605 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2606 struct btrfs_trans_handle
*trans
= NULL
;
2609 spin_lock(&em_tree
->lock
);
2610 em
= lookup_extent_mapping(em_tree
, start
, len
);
2612 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2613 spin_unlock(&em_tree
->lock
);
2616 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2617 free_extent_map(em
);
2618 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2619 free_extent_map(em
);
2623 em
= alloc_extent_map(GFP_NOFS
);
2628 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2629 em
->start
= EXTENT_MAP_HOLE
;
2633 path
= btrfs_alloc_path();
2637 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2638 objectid
, start
, trans
!= NULL
);
2645 if (path
->slots
[0] == 0)
2650 leaf
= path
->nodes
[0];
2651 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2652 struct btrfs_file_extent_item
);
2653 /* are we inside the extent that was found? */
2654 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2655 found_type
= btrfs_key_type(&found_key
);
2656 if (found_key
.objectid
!= objectid
||
2657 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2661 found_type
= btrfs_file_extent_type(leaf
, item
);
2662 extent_start
= found_key
.offset
;
2663 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2664 extent_end
= extent_start
+
2665 btrfs_file_extent_num_bytes(leaf
, item
);
2667 if (start
< extent_start
|| start
>= extent_end
) {
2669 if (start
< extent_start
) {
2670 if (start
+ len
<= extent_start
)
2672 em
->len
= extent_end
- extent_start
;
2678 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2680 em
->start
= extent_start
;
2681 em
->len
= extent_end
- extent_start
;
2682 em
->block_start
= EXTENT_MAP_HOLE
;
2685 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2686 em
->block_start
= bytenr
;
2687 em
->start
= extent_start
;
2688 em
->len
= extent_end
- extent_start
;
2690 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2695 size_t extent_offset
;
2698 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2700 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2701 ~((u64
)root
->sectorsize
- 1);
2702 if (start
< extent_start
|| start
>= extent_end
) {
2704 if (start
< extent_start
) {
2705 if (start
+ len
<= extent_start
)
2707 em
->len
= extent_end
- extent_start
;
2713 em
->block_start
= EXTENT_MAP_INLINE
;
2716 em
->start
= extent_start
;
2721 page_start
= page_offset(page
) + pg_offset
;
2722 extent_offset
= page_start
- extent_start
;
2723 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2724 size
- extent_offset
);
2725 em
->start
= extent_start
+ extent_offset
;
2726 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2727 ~((u64
)root
->sectorsize
- 1);
2729 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2730 if (create
== 0 && !PageUptodate(page
)) {
2731 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2733 flush_dcache_page(page
);
2734 } else if (create
&& PageUptodate(page
)) {
2737 free_extent_map(em
);
2739 btrfs_release_path(root
, path
);
2740 trans
= btrfs_join_transaction(root
, 1);
2743 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2745 btrfs_mark_buffer_dirty(leaf
);
2748 set_extent_uptodate(io_tree
, em
->start
,
2749 extent_map_end(em
) - 1, GFP_NOFS
);
2752 printk("unkknown found_type %d\n", found_type
);
2759 em
->block_start
= EXTENT_MAP_HOLE
;
2761 btrfs_release_path(root
, path
);
2762 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2763 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2769 spin_lock(&em_tree
->lock
);
2770 ret
= add_extent_mapping(em_tree
, em
);
2771 /* it is possible that someone inserted the extent into the tree
2772 * while we had the lock dropped. It is also possible that
2773 * an overlapping map exists in the tree
2775 if (ret
== -EEXIST
) {
2776 struct extent_map
*existing
;
2780 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2781 if (existing
&& (existing
->start
> start
||
2782 existing
->start
+ existing
->len
<= start
)) {
2783 free_extent_map(existing
);
2787 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2790 err
= merge_extent_mapping(em_tree
, existing
,
2793 free_extent_map(existing
);
2795 free_extent_map(em
);
2800 printk("failing to insert %Lu %Lu\n",
2802 free_extent_map(em
);
2806 free_extent_map(em
);
2811 spin_unlock(&em_tree
->lock
);
2814 btrfs_free_path(path
);
2816 ret
= btrfs_end_transaction(trans
, root
);
2822 free_extent_map(em
);
2824 return ERR_PTR(err
);
2829 #if 0 /* waiting for O_DIRECT reads */
2830 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2831 struct buffer_head
*bh_result
, int create
)
2833 struct extent_map
*em
;
2834 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2835 struct btrfs_multi_bio
*multi
= NULL
;
2836 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2842 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2844 if (!em
|| IS_ERR(em
))
2847 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2851 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2856 len
= em
->start
+ em
->len
- start
;
2857 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2859 if (em
->block_start
== EXTENT_MAP_HOLE
||
2860 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2861 bh_result
->b_size
= len
;
2865 logical
= start
- em
->start
;
2866 logical
= em
->block_start
+ logical
;
2869 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2870 logical
, &map_length
, &multi
, 0);
2872 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2873 bh_result
->b_size
= min(map_length
, len
);
2875 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2876 set_buffer_mapped(bh_result
);
2879 free_extent_map(em
);
2884 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2885 const struct iovec
*iov
, loff_t offset
,
2886 unsigned long nr_segs
)
2890 struct file
*file
= iocb
->ki_filp
;
2891 struct inode
*inode
= file
->f_mapping
->host
;
2896 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2897 offset
, nr_segs
, btrfs_get_block
, NULL
);
2901 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2903 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2906 int btrfs_readpage(struct file
*file
, struct page
*page
)
2908 struct extent_io_tree
*tree
;
2909 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2910 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2913 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2915 struct extent_io_tree
*tree
;
2918 if (current
->flags
& PF_MEMALLOC
) {
2919 redirty_page_for_writepage(wbc
, page
);
2923 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2924 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2927 int btrfs_writepages(struct address_space
*mapping
,
2928 struct writeback_control
*wbc
)
2930 struct extent_io_tree
*tree
;
2931 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2932 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2936 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2937 struct list_head
*pages
, unsigned nr_pages
)
2939 struct extent_io_tree
*tree
;
2940 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2941 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2944 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2946 struct extent_io_tree
*tree
;
2947 struct extent_map_tree
*map
;
2950 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2951 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2952 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2954 ClearPagePrivate(page
);
2955 set_page_private(page
, 0);
2956 page_cache_release(page
);
2961 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2963 return __btrfs_releasepage(page
, gfp_flags
);
2966 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2968 struct extent_io_tree
*tree
;
2969 struct btrfs_ordered_extent
*ordered
;
2970 u64 page_start
= page_offset(page
);
2971 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2973 wait_on_page_writeback(page
);
2974 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2976 btrfs_releasepage(page
, GFP_NOFS
);
2980 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2981 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2985 * IO on this page will never be started, so we need
2986 * to account for any ordered extents now
2988 clear_extent_bit(tree
, page_start
, page_end
,
2989 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2990 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2991 btrfs_finish_ordered_io(page
->mapping
->host
,
2992 page_start
, page_end
);
2993 btrfs_put_ordered_extent(ordered
);
2994 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2996 clear_extent_bit(tree
, page_start
, page_end
,
2997 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3000 __btrfs_releasepage(page
, GFP_NOFS
);
3002 ClearPageChecked(page
);
3003 if (PagePrivate(page
)) {
3004 ClearPagePrivate(page
);
3005 set_page_private(page
, 0);
3006 page_cache_release(page
);
3011 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3012 * called from a page fault handler when a page is first dirtied. Hence we must
3013 * be careful to check for EOF conditions here. We set the page up correctly
3014 * for a written page which means we get ENOSPC checking when writing into
3015 * holes and correct delalloc and unwritten extent mapping on filesystems that
3016 * support these features.
3018 * We are not allowed to take the i_mutex here so we have to play games to
3019 * protect against truncate races as the page could now be beyond EOF. Because
3020 * vmtruncate() writes the inode size before removing pages, once we have the
3021 * page lock we can determine safely if the page is beyond EOF. If it is not
3022 * beyond EOF, then the page is guaranteed safe against truncation until we
3025 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
3027 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
3028 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3029 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3030 struct btrfs_ordered_extent
*ordered
;
3032 unsigned long zero_start
;
3038 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
3045 size
= i_size_read(inode
);
3046 page_start
= page_offset(page
);
3047 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3049 if ((page
->mapping
!= inode
->i_mapping
) ||
3050 (page_start
>= size
)) {
3051 /* page got truncated out from underneath us */
3054 wait_on_page_writeback(page
);
3056 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3057 set_page_extent_mapped(page
);
3060 * we can't set the delalloc bits if there are pending ordered
3061 * extents. Drop our locks and wait for them to finish
3063 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3065 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3067 btrfs_start_ordered_extent(inode
, ordered
, 1);
3068 btrfs_put_ordered_extent(ordered
);
3072 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
3073 page_end
, GFP_NOFS
);
3076 /* page is wholly or partially inside EOF */
3077 if (page_start
+ PAGE_CACHE_SIZE
> size
)
3078 zero_start
= size
& ~PAGE_CACHE_MASK
;
3080 zero_start
= PAGE_CACHE_SIZE
;
3082 if (zero_start
!= PAGE_CACHE_SIZE
) {
3084 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
3085 flush_dcache_page(page
);
3088 ClearPageChecked(page
);
3089 set_page_dirty(page
);
3090 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3098 static void btrfs_truncate(struct inode
*inode
)
3100 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3102 struct btrfs_trans_handle
*trans
;
3104 u64 mask
= root
->sectorsize
- 1;
3106 if (!S_ISREG(inode
->i_mode
))
3108 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3111 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3112 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
3114 trans
= btrfs_start_transaction(root
, 1);
3115 btrfs_set_trans_block_group(trans
, inode
);
3116 btrfs_i_size_write(inode
, inode
->i_size
);
3118 ret
= btrfs_orphan_add(trans
, inode
);
3121 /* FIXME, add redo link to tree so we don't leak on crash */
3122 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
3123 BTRFS_EXTENT_DATA_KEY
);
3124 btrfs_update_inode(trans
, root
, inode
);
3126 ret
= btrfs_orphan_del(trans
, inode
);
3130 nr
= trans
->blocks_used
;
3131 ret
= btrfs_end_transaction_throttle(trans
, root
);
3133 btrfs_btree_balance_dirty(root
, nr
);
3137 * Invalidate a single dcache entry at the root of the filesystem.
3138 * Needed after creation of snapshot or subvolume.
3140 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
3143 struct dentry
*alias
, *entry
;
3146 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
3150 /* change me if btrfs ever gets a d_hash operation */
3151 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
3152 entry
= d_lookup(alias
, &qstr
);
3155 d_invalidate(entry
);
3161 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
3162 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
3163 struct btrfs_block_group_cache
*block_group
)
3165 struct inode
*inode
;
3167 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
3168 new_dirid
, block_group
, S_IFDIR
| 0700);
3170 return PTR_ERR(inode
);
3171 inode
->i_op
= &btrfs_dir_inode_operations
;
3172 inode
->i_fop
= &btrfs_dir_file_operations
;
3173 new_root
->inode
= inode
;
3176 btrfs_i_size_write(inode
, 0);
3178 return btrfs_update_inode(trans
, new_root
, inode
);
3181 unsigned long btrfs_force_ra(struct address_space
*mapping
,
3182 struct file_ra_state
*ra
, struct file
*file
,
3183 pgoff_t offset
, pgoff_t last_index
)
3185 pgoff_t req_size
= last_index
- offset
+ 1;
3187 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3188 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
3191 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
3192 return offset
+ req_size
;
3196 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3198 struct btrfs_inode
*ei
;
3200 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3204 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
3205 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
3206 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
3207 INIT_LIST_HEAD(&ei
->i_orphan
);
3208 return &ei
->vfs_inode
;
3211 void btrfs_destroy_inode(struct inode
*inode
)
3213 struct btrfs_ordered_extent
*ordered
;
3214 WARN_ON(!list_empty(&inode
->i_dentry
));
3215 WARN_ON(inode
->i_data
.nrpages
);
3217 if (BTRFS_I(inode
)->i_acl
&&
3218 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
3219 posix_acl_release(BTRFS_I(inode
)->i_acl
);
3220 if (BTRFS_I(inode
)->i_default_acl
&&
3221 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
3222 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
3224 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
3225 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
3226 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
3227 " list\n", inode
->i_ino
);
3230 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
3233 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
3237 printk("found ordered extent %Lu %Lu\n",
3238 ordered
->file_offset
, ordered
->len
);
3239 btrfs_remove_ordered_extent(inode
, ordered
);
3240 btrfs_put_ordered_extent(ordered
);
3241 btrfs_put_ordered_extent(ordered
);
3244 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
3245 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3248 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3249 static void init_once(void *foo
)
3250 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3251 static void init_once(struct kmem_cache
* cachep
, void *foo
)
3253 static void init_once(void * foo
, struct kmem_cache
* cachep
,
3254 unsigned long flags
)
3257 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3259 inode_init_once(&ei
->vfs_inode
);
3262 void btrfs_destroy_cachep(void)
3264 if (btrfs_inode_cachep
)
3265 kmem_cache_destroy(btrfs_inode_cachep
);
3266 if (btrfs_trans_handle_cachep
)
3267 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3268 if (btrfs_transaction_cachep
)
3269 kmem_cache_destroy(btrfs_transaction_cachep
);
3270 if (btrfs_bit_radix_cachep
)
3271 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3272 if (btrfs_path_cachep
)
3273 kmem_cache_destroy(btrfs_path_cachep
);
3276 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3277 unsigned long extra_flags
,
3278 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3279 void (*ctor
)(void *)
3280 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3281 void (*ctor
)(struct kmem_cache
*, void *)
3283 void (*ctor
)(void *, struct kmem_cache
*,
3288 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3289 SLAB_MEM_SPREAD
| extra_flags
), ctor
3290 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3296 int btrfs_init_cachep(void)
3298 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3299 sizeof(struct btrfs_inode
),
3301 if (!btrfs_inode_cachep
)
3303 btrfs_trans_handle_cachep
=
3304 btrfs_cache_create("btrfs_trans_handle_cache",
3305 sizeof(struct btrfs_trans_handle
),
3307 if (!btrfs_trans_handle_cachep
)
3309 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3310 sizeof(struct btrfs_transaction
),
3312 if (!btrfs_transaction_cachep
)
3314 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3315 sizeof(struct btrfs_path
),
3317 if (!btrfs_path_cachep
)
3319 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3320 SLAB_DESTROY_BY_RCU
, NULL
);
3321 if (!btrfs_bit_radix_cachep
)
3325 btrfs_destroy_cachep();
3329 static int btrfs_getattr(struct vfsmount
*mnt
,
3330 struct dentry
*dentry
, struct kstat
*stat
)
3332 struct inode
*inode
= dentry
->d_inode
;
3333 generic_fillattr(inode
, stat
);
3334 stat
->blksize
= PAGE_CACHE_SIZE
;
3335 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3339 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3340 struct inode
* new_dir
,struct dentry
*new_dentry
)
3342 struct btrfs_trans_handle
*trans
;
3343 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3344 struct inode
*new_inode
= new_dentry
->d_inode
;
3345 struct inode
*old_inode
= old_dentry
->d_inode
;
3346 struct timespec ctime
= CURRENT_TIME
;
3349 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3350 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3354 ret
= btrfs_check_free_space(root
, 1, 0);
3358 trans
= btrfs_start_transaction(root
, 1);
3360 btrfs_set_trans_block_group(trans
, new_dir
);
3362 old_dentry
->d_inode
->i_nlink
++;
3363 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3364 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3365 old_inode
->i_ctime
= ctime
;
3367 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3372 new_inode
->i_ctime
= CURRENT_TIME
;
3373 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3376 if (new_inode
->i_nlink
== 0) {
3377 ret
= btrfs_orphan_add(trans
, new_inode
);
3382 ret
= btrfs_set_inode_index(new_dir
, old_inode
);
3386 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3391 btrfs_end_transaction_throttle(trans
, root
);
3396 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3397 const char *symname
)
3399 struct btrfs_trans_handle
*trans
;
3400 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3401 struct btrfs_path
*path
;
3402 struct btrfs_key key
;
3403 struct inode
*inode
= NULL
;
3410 struct btrfs_file_extent_item
*ei
;
3411 struct extent_buffer
*leaf
;
3412 unsigned long nr
= 0;
3414 name_len
= strlen(symname
) + 1;
3415 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3416 return -ENAMETOOLONG
;
3418 err
= btrfs_check_free_space(root
, 1, 0);
3422 trans
= btrfs_start_transaction(root
, 1);
3423 btrfs_set_trans_block_group(trans
, dir
);
3425 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3431 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3433 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3434 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3435 err
= PTR_ERR(inode
);
3439 err
= btrfs_init_acl(inode
, dir
);
3445 btrfs_set_trans_block_group(trans
, inode
);
3446 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3450 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3451 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3452 inode
->i_fop
= &btrfs_file_operations
;
3453 inode
->i_op
= &btrfs_file_inode_operations
;
3454 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3455 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3456 inode
->i_mapping
, GFP_NOFS
);
3457 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3458 inode
->i_mapping
, GFP_NOFS
);
3459 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3460 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3461 BTRFS_I(inode
)->delalloc_bytes
= 0;
3462 BTRFS_I(inode
)->disk_i_size
= 0;
3463 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3464 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3466 dir
->i_sb
->s_dirt
= 1;
3467 btrfs_update_inode_block_group(trans
, inode
);
3468 btrfs_update_inode_block_group(trans
, dir
);
3472 path
= btrfs_alloc_path();
3474 key
.objectid
= inode
->i_ino
;
3476 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3477 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3478 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3484 leaf
= path
->nodes
[0];
3485 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3486 struct btrfs_file_extent_item
);
3487 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3488 btrfs_set_file_extent_type(leaf
, ei
,
3489 BTRFS_FILE_EXTENT_INLINE
);
3490 ptr
= btrfs_file_extent_inline_start(ei
);
3491 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3492 btrfs_mark_buffer_dirty(leaf
);
3493 btrfs_free_path(path
);
3495 inode
->i_op
= &btrfs_symlink_inode_operations
;
3496 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3497 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3498 btrfs_i_size_write(inode
, name_len
- 1);
3499 err
= btrfs_update_inode(trans
, root
, inode
);
3504 nr
= trans
->blocks_used
;
3505 btrfs_end_transaction_throttle(trans
, root
);
3508 inode_dec_link_count(inode
);
3511 btrfs_btree_balance_dirty(root
, nr
);
3515 static int btrfs_set_page_dirty(struct page
*page
)
3517 return __set_page_dirty_nobuffers(page
);
3520 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3521 static int btrfs_permission(struct inode
*inode
, int mask
)
3523 static int btrfs_permission(struct inode
*inode
, int mask
,
3524 struct nameidata
*nd
)
3527 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3529 return generic_permission(inode
, mask
, btrfs_check_acl
);
3532 static struct inode_operations btrfs_dir_inode_operations
= {
3533 .lookup
= btrfs_lookup
,
3534 .create
= btrfs_create
,
3535 .unlink
= btrfs_unlink
,
3537 .mkdir
= btrfs_mkdir
,
3538 .rmdir
= btrfs_rmdir
,
3539 .rename
= btrfs_rename
,
3540 .symlink
= btrfs_symlink
,
3541 .setattr
= btrfs_setattr
,
3542 .mknod
= btrfs_mknod
,
3543 .setxattr
= generic_setxattr
,
3544 .getxattr
= generic_getxattr
,
3545 .listxattr
= btrfs_listxattr
,
3546 .removexattr
= generic_removexattr
,
3547 .permission
= btrfs_permission
,
3549 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3550 .lookup
= btrfs_lookup
,
3551 .permission
= btrfs_permission
,
3553 static struct file_operations btrfs_dir_file_operations
= {
3554 .llseek
= generic_file_llseek
,
3555 .read
= generic_read_dir
,
3556 .readdir
= btrfs_readdir
,
3557 .unlocked_ioctl
= btrfs_ioctl
,
3558 #ifdef CONFIG_COMPAT
3559 .compat_ioctl
= btrfs_ioctl
,
3561 .release
= btrfs_release_file
,
3564 static struct extent_io_ops btrfs_extent_io_ops
= {
3565 .fill_delalloc
= run_delalloc_range
,
3566 .submit_bio_hook
= btrfs_submit_bio_hook
,
3567 .merge_bio_hook
= btrfs_merge_bio_hook
,
3568 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3569 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3570 .writepage_start_hook
= btrfs_writepage_start_hook
,
3571 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3572 .set_bit_hook
= btrfs_set_bit_hook
,
3573 .clear_bit_hook
= btrfs_clear_bit_hook
,
3576 static struct address_space_operations btrfs_aops
= {
3577 .readpage
= btrfs_readpage
,
3578 .writepage
= btrfs_writepage
,
3579 .writepages
= btrfs_writepages
,
3580 .readpages
= btrfs_readpages
,
3581 .sync_page
= block_sync_page
,
3583 .direct_IO
= btrfs_direct_IO
,
3584 .invalidatepage
= btrfs_invalidatepage
,
3585 .releasepage
= btrfs_releasepage
,
3586 .set_page_dirty
= btrfs_set_page_dirty
,
3589 static struct address_space_operations btrfs_symlink_aops
= {
3590 .readpage
= btrfs_readpage
,
3591 .writepage
= btrfs_writepage
,
3592 .invalidatepage
= btrfs_invalidatepage
,
3593 .releasepage
= btrfs_releasepage
,
3596 static struct inode_operations btrfs_file_inode_operations
= {
3597 .truncate
= btrfs_truncate
,
3598 .getattr
= btrfs_getattr
,
3599 .setattr
= btrfs_setattr
,
3600 .setxattr
= generic_setxattr
,
3601 .getxattr
= generic_getxattr
,
3602 .listxattr
= btrfs_listxattr
,
3603 .removexattr
= generic_removexattr
,
3604 .permission
= btrfs_permission
,
3606 static struct inode_operations btrfs_special_inode_operations
= {
3607 .getattr
= btrfs_getattr
,
3608 .setattr
= btrfs_setattr
,
3609 .permission
= btrfs_permission
,
3610 .setxattr
= generic_setxattr
,
3611 .getxattr
= generic_getxattr
,
3612 .listxattr
= btrfs_listxattr
,
3613 .removexattr
= generic_removexattr
,
3615 static struct inode_operations btrfs_symlink_inode_operations
= {
3616 .readlink
= generic_readlink
,
3617 .follow_link
= page_follow_link_light
,
3618 .put_link
= page_put_link
,
3619 .permission
= btrfs_permission
,