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 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
307 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
308 &root
->fs_info
->delalloc_inodes
);
310 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
315 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
316 unsigned long old
, unsigned long bits
)
318 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
319 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
322 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
323 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
324 printk("warning: delalloc account %Lu %Lu\n",
325 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
326 root
->fs_info
->delalloc_bytes
= 0;
327 BTRFS_I(inode
)->delalloc_bytes
= 0;
329 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
330 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
332 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
333 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
334 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
336 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
341 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
342 size_t size
, struct bio
*bio
)
344 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
345 struct btrfs_mapping_tree
*map_tree
;
346 u64 logical
= bio
->bi_sector
<< 9;
351 length
= bio
->bi_size
;
352 map_tree
= &root
->fs_info
->mapping_tree
;
354 ret
= btrfs_map_block(map_tree
, READ
, logical
,
355 &map_length
, NULL
, 0);
357 if (map_length
< length
+ size
) {
363 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
369 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
372 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
375 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
378 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
381 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
384 if (!(rw
& (1 << BIO_RW
))) {
388 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
389 inode
, rw
, bio
, mirror_num
,
390 __btrfs_submit_bio_hook
);
392 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
395 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
396 struct inode
*inode
, u64 file_offset
,
397 struct list_head
*list
)
399 struct list_head
*cur
;
400 struct btrfs_ordered_sum
*sum
;
402 btrfs_set_trans_block_group(trans
, inode
);
403 list_for_each(cur
, list
) {
404 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
405 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
406 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
408 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
413 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
)
415 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
419 struct btrfs_writepage_fixup
{
421 struct btrfs_work work
;
424 /* see btrfs_writepage_start_hook for details on why this is required */
425 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
427 struct btrfs_writepage_fixup
*fixup
;
428 struct btrfs_ordered_extent
*ordered
;
434 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
438 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
439 ClearPageChecked(page
);
443 inode
= page
->mapping
->host
;
444 page_start
= page_offset(page
);
445 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
447 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
449 /* already ordered? We're done */
450 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
451 EXTENT_ORDERED
, 0)) {
455 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
457 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
460 btrfs_start_ordered_extent(inode
, ordered
, 1);
464 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
465 ClearPageChecked(page
);
467 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
470 page_cache_release(page
);
474 * There are a few paths in the higher layers of the kernel that directly
475 * set the page dirty bit without asking the filesystem if it is a
476 * good idea. This causes problems because we want to make sure COW
477 * properly happens and the data=ordered rules are followed.
479 * In our case any range that doesn't have the EXTENT_ORDERED bit set
480 * hasn't been properly setup for IO. We kick off an async process
481 * to fix it up. The async helper will wait for ordered extents, set
482 * the delalloc bit and make it safe to write the page.
484 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
486 struct inode
*inode
= page
->mapping
->host
;
487 struct btrfs_writepage_fixup
*fixup
;
488 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
491 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
496 if (PageChecked(page
))
499 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
503 SetPageChecked(page
);
504 page_cache_get(page
);
505 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
507 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
511 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
513 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
514 struct btrfs_trans_handle
*trans
;
515 struct btrfs_ordered_extent
*ordered_extent
;
516 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
518 struct list_head list
;
519 struct btrfs_key ins
;
522 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
526 trans
= btrfs_join_transaction(root
, 1);
528 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
529 BUG_ON(!ordered_extent
);
531 lock_extent(io_tree
, ordered_extent
->file_offset
,
532 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
535 INIT_LIST_HEAD(&list
);
537 ins
.objectid
= ordered_extent
->start
;
538 ins
.offset
= ordered_extent
->len
;
539 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
541 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
542 trans
->transid
, inode
->i_ino
,
543 ordered_extent
->file_offset
, &ins
);
546 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
548 ret
= btrfs_drop_extents(trans
, root
, inode
,
549 ordered_extent
->file_offset
,
550 ordered_extent
->file_offset
+
552 ordered_extent
->file_offset
, &alloc_hint
);
554 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
555 ordered_extent
->file_offset
,
556 ordered_extent
->start
,
558 ordered_extent
->len
, 0);
561 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
562 ordered_extent
->file_offset
+
563 ordered_extent
->len
- 1);
564 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
566 inode
->i_blocks
+= ordered_extent
->len
>> 9;
567 unlock_extent(io_tree
, ordered_extent
->file_offset
,
568 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
570 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
571 &ordered_extent
->list
);
573 btrfs_ordered_update_i_size(inode
, ordered_extent
);
574 btrfs_remove_ordered_extent(inode
, ordered_extent
);
577 btrfs_put_ordered_extent(ordered_extent
);
578 /* once for the tree */
579 btrfs_put_ordered_extent(ordered_extent
);
581 btrfs_update_inode(trans
, root
, inode
);
582 btrfs_end_transaction(trans
, root
);
586 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
587 struct extent_state
*state
, int uptodate
)
589 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
592 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
595 struct inode
*inode
= page
->mapping
->host
;
596 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
597 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
598 struct btrfs_csum_item
*item
;
599 struct btrfs_path
*path
= NULL
;
602 if (btrfs_test_opt(root
, NODATASUM
) ||
603 btrfs_test_flag(inode
, NODATASUM
))
607 * It is possible there is an ordered extent that has
608 * not yet finished for this range in the file. If so,
609 * that extent will have a csum cached, and it will insert
610 * the sum after all the blocks in the extent are fully
611 * on disk. So, look for an ordered extent and use the
612 * sum if found. We have to do this before looking in the
613 * btree because csum items are pre-inserted based on
614 * the file size. btrfs_lookup_csum might find an item
615 * that still hasn't been fully filled.
617 ret
= btrfs_find_ordered_sum(inode
, start
, &csum
);
622 path
= btrfs_alloc_path();
623 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
626 /* a csum that isn't present is a preallocated region. */
627 if (ret
== -ENOENT
|| ret
== -EFBIG
)
630 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
634 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
637 set_state_private(io_tree
, start
, csum
);
640 btrfs_free_path(path
);
644 struct io_failure_record
{
652 int btrfs_io_failed_hook(struct bio
*failed_bio
,
653 struct page
*page
, u64 start
, u64 end
,
654 struct extent_state
*state
)
656 struct io_failure_record
*failrec
= NULL
;
658 struct extent_map
*em
;
659 struct inode
*inode
= page
->mapping
->host
;
660 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
661 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
668 ret
= get_state_private(failure_tree
, start
, &private);
670 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
673 failrec
->start
= start
;
674 failrec
->len
= end
- start
+ 1;
675 failrec
->last_mirror
= 0;
677 spin_lock(&em_tree
->lock
);
678 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
679 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
683 spin_unlock(&em_tree
->lock
);
685 if (!em
|| IS_ERR(em
)) {
689 logical
= start
- em
->start
;
690 logical
= em
->block_start
+ logical
;
691 failrec
->logical
= logical
;
693 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
694 EXTENT_DIRTY
, GFP_NOFS
);
695 set_state_private(failure_tree
, start
,
696 (u64
)(unsigned long)failrec
);
698 failrec
= (struct io_failure_record
*)(unsigned long)private;
700 num_copies
= btrfs_num_copies(
701 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
702 failrec
->logical
, failrec
->len
);
703 failrec
->last_mirror
++;
705 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
706 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
709 if (state
&& state
->start
!= failrec
->start
)
711 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
713 if (!state
|| failrec
->last_mirror
> num_copies
) {
714 set_state_private(failure_tree
, failrec
->start
, 0);
715 clear_extent_bits(failure_tree
, failrec
->start
,
716 failrec
->start
+ failrec
->len
- 1,
717 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
721 bio
= bio_alloc(GFP_NOFS
, 1);
722 bio
->bi_private
= state
;
723 bio
->bi_end_io
= failed_bio
->bi_end_io
;
724 bio
->bi_sector
= failrec
->logical
>> 9;
725 bio
->bi_bdev
= failed_bio
->bi_bdev
;
727 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
728 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
733 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
734 failrec
->last_mirror
);
738 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
742 struct io_failure_record
*failure
;
746 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
747 (u64
)-1, 1, EXTENT_DIRTY
)) {
748 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
749 start
, &private_failure
);
751 failure
= (struct io_failure_record
*)(unsigned long)
753 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
755 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
757 failure
->start
+ failure
->len
- 1,
758 EXTENT_DIRTY
| EXTENT_LOCKED
,
766 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
767 struct extent_state
*state
)
769 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
770 struct inode
*inode
= page
->mapping
->host
;
771 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
773 u64
private = ~(u32
)0;
775 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
779 if (btrfs_test_opt(root
, NODATASUM
) ||
780 btrfs_test_flag(inode
, NODATASUM
))
782 if (state
&& state
->start
== start
) {
783 private = state
->private;
786 ret
= get_state_private(io_tree
, start
, &private);
788 local_irq_save(flags
);
789 kaddr
= kmap_atomic(page
, KM_IRQ0
);
793 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
794 btrfs_csum_final(csum
, (char *)&csum
);
795 if (csum
!= private) {
798 kunmap_atomic(kaddr
, KM_IRQ0
);
799 local_irq_restore(flags
);
801 /* if the io failure tree for this inode is non-empty,
802 * check to see if we've recovered from a failed IO
804 btrfs_clean_io_failures(inode
, start
);
808 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
809 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
811 memset(kaddr
+ offset
, 1, end
- start
+ 1);
812 flush_dcache_page(page
);
813 kunmap_atomic(kaddr
, KM_IRQ0
);
814 local_irq_restore(flags
);
821 * This creates an orphan entry for the given inode in case something goes
822 * wrong in the middle of an unlink/truncate.
824 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
826 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
829 spin_lock(&root
->list_lock
);
831 /* already on the orphan list, we're good */
832 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
833 spin_unlock(&root
->list_lock
);
837 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
839 spin_unlock(&root
->list_lock
);
842 * insert an orphan item to track this unlinked/truncated file
844 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
850 * We have done the truncate/delete so we can go ahead and remove the orphan
851 * item for this particular inode.
853 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
855 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
858 spin_lock(&root
->list_lock
);
860 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
861 spin_unlock(&root
->list_lock
);
865 list_del_init(&BTRFS_I(inode
)->i_orphan
);
867 spin_unlock(&root
->list_lock
);
871 spin_unlock(&root
->list_lock
);
873 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
879 * this cleans up any orphans that may be left on the list from the last use
882 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
884 struct btrfs_path
*path
;
885 struct extent_buffer
*leaf
;
886 struct btrfs_item
*item
;
887 struct btrfs_key key
, found_key
;
888 struct btrfs_trans_handle
*trans
;
890 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
892 /* don't do orphan cleanup if the fs is readonly. */
893 if (root
->inode
->i_sb
->s_flags
& MS_RDONLY
)
896 path
= btrfs_alloc_path();
901 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
902 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
903 key
.offset
= (u64
)-1;
905 trans
= btrfs_start_transaction(root
, 1);
906 btrfs_set_trans_block_group(trans
, root
->inode
);
909 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
911 printk(KERN_ERR
"Error searching slot for orphan: %d"
917 * if ret == 0 means we found what we were searching for, which
918 * is weird, but possible, so only screw with path if we didnt
919 * find the key and see if we have stuff that matches
922 if (path
->slots
[0] == 0)
927 /* pull out the item */
928 leaf
= path
->nodes
[0];
929 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
930 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
932 /* make sure the item matches what we want */
933 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
935 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
938 /* release the path since we're done with it */
939 btrfs_release_path(root
, path
);
942 * this is where we are basically btrfs_lookup, without the
943 * crossing root thing. we store the inode number in the
944 * offset of the orphan item.
946 inode
= btrfs_iget_locked(root
->inode
->i_sb
,
947 found_key
.offset
, root
);
951 if (inode
->i_state
& I_NEW
) {
952 BTRFS_I(inode
)->root
= root
;
954 /* have to set the location manually */
955 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
956 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
957 BTRFS_I(inode
)->location
.offset
= 0;
959 btrfs_read_locked_inode(inode
);
960 unlock_new_inode(inode
);
964 * add this inode to the orphan list so btrfs_orphan_del does
965 * the proper thing when we hit it
967 spin_lock(&root
->list_lock
);
968 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
969 spin_unlock(&root
->list_lock
);
972 * if this is a bad inode, means we actually succeeded in
973 * removing the inode, but not the orphan record, which means
974 * we need to manually delete the orphan since iput will just
977 if (is_bad_inode(inode
)) {
978 btrfs_orphan_del(trans
, inode
);
983 /* if we have links, this was a truncate, lets do that */
984 if (inode
->i_nlink
) {
986 btrfs_truncate(inode
);
991 /* this will do delete_inode and everything for us */
996 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
998 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
1000 btrfs_free_path(path
);
1001 btrfs_end_transaction(trans
, root
);
1004 void btrfs_read_locked_inode(struct inode
*inode
)
1006 struct btrfs_path
*path
;
1007 struct extent_buffer
*leaf
;
1008 struct btrfs_inode_item
*inode_item
;
1009 struct btrfs_timespec
*tspec
;
1010 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1011 struct btrfs_key location
;
1012 u64 alloc_group_block
;
1016 path
= btrfs_alloc_path();
1018 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
1020 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
1024 leaf
= path
->nodes
[0];
1025 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1026 struct btrfs_inode_item
);
1028 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
1029 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
1030 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
1031 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
1032 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
1034 tspec
= btrfs_inode_atime(inode_item
);
1035 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1036 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1038 tspec
= btrfs_inode_mtime(inode_item
);
1039 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1040 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1042 tspec
= btrfs_inode_ctime(inode_item
);
1043 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1044 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1046 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
1047 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
1049 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
1051 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1053 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
1054 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
1056 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
1057 if (!BTRFS_I(inode
)->block_group
) {
1058 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
1060 BTRFS_BLOCK_GROUP_METADATA
, 0);
1062 btrfs_free_path(path
);
1065 switch (inode
->i_mode
& S_IFMT
) {
1067 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1068 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1069 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1070 inode
->i_fop
= &btrfs_file_operations
;
1071 inode
->i_op
= &btrfs_file_inode_operations
;
1074 inode
->i_fop
= &btrfs_dir_file_operations
;
1075 if (root
== root
->fs_info
->tree_root
)
1076 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
1078 inode
->i_op
= &btrfs_dir_inode_operations
;
1081 inode
->i_op
= &btrfs_symlink_inode_operations
;
1082 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
1083 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1086 init_special_inode(inode
, inode
->i_mode
, rdev
);
1092 btrfs_free_path(path
);
1093 make_bad_inode(inode
);
1096 static void fill_inode_item(struct extent_buffer
*leaf
,
1097 struct btrfs_inode_item
*item
,
1098 struct inode
*inode
)
1100 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
1101 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
1102 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
1103 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
1104 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
1106 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
1107 inode
->i_atime
.tv_sec
);
1108 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
1109 inode
->i_atime
.tv_nsec
);
1111 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
1112 inode
->i_mtime
.tv_sec
);
1113 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
1114 inode
->i_mtime
.tv_nsec
);
1116 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
1117 inode
->i_ctime
.tv_sec
);
1118 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
1119 inode
->i_ctime
.tv_nsec
);
1121 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
1122 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
1123 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
1124 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
1125 btrfs_set_inode_block_group(leaf
, item
,
1126 BTRFS_I(inode
)->block_group
->key
.objectid
);
1129 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
1130 struct btrfs_root
*root
,
1131 struct inode
*inode
)
1133 struct btrfs_inode_item
*inode_item
;
1134 struct btrfs_path
*path
;
1135 struct extent_buffer
*leaf
;
1138 path
= btrfs_alloc_path();
1140 ret
= btrfs_lookup_inode(trans
, root
, path
,
1141 &BTRFS_I(inode
)->location
, 1);
1148 leaf
= path
->nodes
[0];
1149 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1150 struct btrfs_inode_item
);
1152 fill_inode_item(leaf
, inode_item
, inode
);
1153 btrfs_mark_buffer_dirty(leaf
);
1154 btrfs_set_inode_last_trans(trans
, inode
);
1157 btrfs_free_path(path
);
1162 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
1163 struct btrfs_root
*root
,
1165 struct dentry
*dentry
)
1167 struct btrfs_path
*path
;
1168 const char *name
= dentry
->d_name
.name
;
1169 int name_len
= dentry
->d_name
.len
;
1171 struct extent_buffer
*leaf
;
1172 struct btrfs_dir_item
*di
;
1173 struct btrfs_key key
;
1176 path
= btrfs_alloc_path();
1182 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1183 name
, name_len
, -1);
1192 leaf
= path
->nodes
[0];
1193 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1194 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1197 btrfs_release_path(root
, path
);
1199 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1200 dentry
->d_inode
->i_ino
,
1201 dentry
->d_parent
->d_inode
->i_ino
, &index
);
1203 printk("failed to delete reference to %.*s, "
1204 "inode %lu parent %lu\n", name_len
, name
,
1205 dentry
->d_inode
->i_ino
,
1206 dentry
->d_parent
->d_inode
->i_ino
);
1210 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1211 index
, name
, name_len
, -1);
1220 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1221 btrfs_release_path(root
, path
);
1223 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
1225 btrfs_free_path(path
);
1227 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1228 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1229 btrfs_update_inode(trans
, root
, dir
);
1230 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1231 dentry
->d_inode
->i_nlink
--;
1233 drop_nlink(dentry
->d_inode
);
1235 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1236 dir
->i_sb
->s_dirt
= 1;
1241 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1243 struct btrfs_root
*root
;
1244 struct btrfs_trans_handle
*trans
;
1245 struct inode
*inode
= dentry
->d_inode
;
1247 unsigned long nr
= 0;
1249 root
= BTRFS_I(dir
)->root
;
1251 ret
= btrfs_check_free_space(root
, 1, 1);
1255 trans
= btrfs_start_transaction(root
, 1);
1257 btrfs_set_trans_block_group(trans
, dir
);
1258 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1260 if (inode
->i_nlink
== 0)
1261 ret
= btrfs_orphan_add(trans
, inode
);
1263 nr
= trans
->blocks_used
;
1265 btrfs_end_transaction_throttle(trans
, root
);
1267 btrfs_btree_balance_dirty(root
, nr
);
1271 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1273 struct inode
*inode
= dentry
->d_inode
;
1276 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1277 struct btrfs_trans_handle
*trans
;
1278 unsigned long nr
= 0;
1280 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1284 ret
= btrfs_check_free_space(root
, 1, 1);
1288 trans
= btrfs_start_transaction(root
, 1);
1289 btrfs_set_trans_block_group(trans
, dir
);
1291 err
= btrfs_orphan_add(trans
, inode
);
1295 /* now the directory is empty */
1296 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1298 btrfs_i_size_write(inode
, 0);
1302 nr
= trans
->blocks_used
;
1303 ret
= btrfs_end_transaction_throttle(trans
, root
);
1305 btrfs_btree_balance_dirty(root
, nr
);
1313 * this can truncate away extent items, csum items and directory items.
1314 * It starts at a high offset and removes keys until it can't find
1315 * any higher than i_size.
1317 * csum items that cross the new i_size are truncated to the new size
1320 * min_type is the minimum key type to truncate down to. If set to 0, this
1321 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1323 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1324 struct btrfs_root
*root
,
1325 struct inode
*inode
,
1329 struct btrfs_path
*path
;
1330 struct btrfs_key key
;
1331 struct btrfs_key found_key
;
1333 struct extent_buffer
*leaf
;
1334 struct btrfs_file_extent_item
*fi
;
1335 u64 extent_start
= 0;
1336 u64 extent_num_bytes
= 0;
1342 int pending_del_nr
= 0;
1343 int pending_del_slot
= 0;
1344 int extent_type
= -1;
1345 u64 mask
= root
->sectorsize
- 1;
1347 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1348 path
= btrfs_alloc_path();
1352 /* FIXME, add redo link to tree so we don't leak on crash */
1353 key
.objectid
= inode
->i_ino
;
1354 key
.offset
= (u64
)-1;
1357 btrfs_init_path(path
);
1359 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1364 BUG_ON(path
->slots
[0] == 0);
1370 leaf
= path
->nodes
[0];
1371 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1372 found_type
= btrfs_key_type(&found_key
);
1374 if (found_key
.objectid
!= inode
->i_ino
)
1377 if (found_type
< min_type
)
1380 item_end
= found_key
.offset
;
1381 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1382 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1383 struct btrfs_file_extent_item
);
1384 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1385 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1387 btrfs_file_extent_num_bytes(leaf
, fi
);
1388 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1389 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1391 item_end
+= btrfs_file_extent_inline_len(leaf
,
1396 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1397 ret
= btrfs_csum_truncate(trans
, root
, path
,
1401 if (item_end
< inode
->i_size
) {
1402 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1403 found_type
= BTRFS_INODE_ITEM_KEY
;
1404 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1405 found_type
= BTRFS_CSUM_ITEM_KEY
;
1406 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1407 found_type
= BTRFS_XATTR_ITEM_KEY
;
1408 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1409 found_type
= BTRFS_INODE_REF_KEY
;
1410 } else if (found_type
) {
1415 btrfs_set_key_type(&key
, found_type
);
1418 if (found_key
.offset
>= inode
->i_size
)
1424 /* FIXME, shrink the extent if the ref count is only 1 */
1425 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1428 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1430 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1432 u64 orig_num_bytes
=
1433 btrfs_file_extent_num_bytes(leaf
, fi
);
1434 extent_num_bytes
= inode
->i_size
-
1435 found_key
.offset
+ root
->sectorsize
- 1;
1436 extent_num_bytes
= extent_num_bytes
&
1437 ~((u64
)root
->sectorsize
- 1);
1438 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1440 num_dec
= (orig_num_bytes
-
1442 if (extent_start
!= 0)
1443 dec_i_blocks(inode
, num_dec
);
1444 btrfs_mark_buffer_dirty(leaf
);
1447 btrfs_file_extent_disk_num_bytes(leaf
,
1449 /* FIXME blocksize != 4096 */
1450 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1451 if (extent_start
!= 0) {
1453 dec_i_blocks(inode
, num_dec
);
1455 root_gen
= btrfs_header_generation(leaf
);
1456 root_owner
= btrfs_header_owner(leaf
);
1458 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1460 u32 newsize
= inode
->i_size
- found_key
.offset
;
1461 dec_i_blocks(inode
, item_end
+ 1 -
1462 found_key
.offset
- newsize
);
1464 btrfs_file_extent_calc_inline_size(newsize
);
1465 ret
= btrfs_truncate_item(trans
, root
, path
,
1469 dec_i_blocks(inode
, item_end
+ 1 -
1475 if (!pending_del_nr
) {
1476 /* no pending yet, add ourselves */
1477 pending_del_slot
= path
->slots
[0];
1479 } else if (pending_del_nr
&&
1480 path
->slots
[0] + 1 == pending_del_slot
) {
1481 /* hop on the pending chunk */
1483 pending_del_slot
= path
->slots
[0];
1485 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1491 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1494 root_gen
, inode
->i_ino
,
1495 found_key
.offset
, 0);
1499 if (path
->slots
[0] == 0) {
1502 btrfs_release_path(root
, path
);
1507 if (pending_del_nr
&&
1508 path
->slots
[0] + 1 != pending_del_slot
) {
1509 struct btrfs_key debug
;
1511 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1513 ret
= btrfs_del_items(trans
, root
, path
,
1518 btrfs_release_path(root
, path
);
1524 if (pending_del_nr
) {
1525 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1528 btrfs_free_path(path
);
1529 inode
->i_sb
->s_dirt
= 1;
1534 * taken from block_truncate_page, but does cow as it zeros out
1535 * any bytes left in the last page in the file.
1537 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1539 struct inode
*inode
= mapping
->host
;
1540 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1541 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1542 struct btrfs_ordered_extent
*ordered
;
1544 u32 blocksize
= root
->sectorsize
;
1545 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1546 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1552 if ((offset
& (blocksize
- 1)) == 0)
1557 page
= grab_cache_page(mapping
, index
);
1561 page_start
= page_offset(page
);
1562 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1564 if (!PageUptodate(page
)) {
1565 ret
= btrfs_readpage(NULL
, page
);
1567 if (page
->mapping
!= mapping
) {
1569 page_cache_release(page
);
1572 if (!PageUptodate(page
)) {
1577 wait_on_page_writeback(page
);
1579 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1580 set_page_extent_mapped(page
);
1582 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1584 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1586 page_cache_release(page
);
1587 btrfs_start_ordered_extent(inode
, ordered
, 1);
1588 btrfs_put_ordered_extent(ordered
);
1592 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
1594 if (offset
!= PAGE_CACHE_SIZE
) {
1596 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1597 flush_dcache_page(page
);
1600 ClearPageChecked(page
);
1601 set_page_dirty(page
);
1602 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1606 page_cache_release(page
);
1611 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1613 struct inode
*inode
= dentry
->d_inode
;
1616 err
= inode_change_ok(inode
, attr
);
1620 if (S_ISREG(inode
->i_mode
) &&
1621 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1622 struct btrfs_trans_handle
*trans
;
1623 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1624 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1626 u64 mask
= root
->sectorsize
- 1;
1627 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1628 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1632 if (attr
->ia_size
<= hole_start
)
1635 err
= btrfs_check_free_space(root
, 1, 0);
1639 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1641 hole_size
= block_end
- hole_start
;
1642 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1643 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1645 trans
= btrfs_start_transaction(root
, 1);
1646 btrfs_set_trans_block_group(trans
, inode
);
1647 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1648 err
= btrfs_drop_extents(trans
, root
, inode
,
1649 hole_start
, block_end
, hole_start
,
1652 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1653 err
= btrfs_insert_file_extent(trans
, root
,
1657 btrfs_drop_extent_cache(inode
, hole_start
,
1659 btrfs_check_file(root
, inode
);
1661 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1662 btrfs_end_transaction(trans
, root
);
1663 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1668 err
= inode_setattr(inode
, attr
);
1670 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
1671 err
= btrfs_acl_chmod(inode
);
1676 void btrfs_delete_inode(struct inode
*inode
)
1678 struct btrfs_trans_handle
*trans
;
1679 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1683 truncate_inode_pages(&inode
->i_data
, 0);
1684 if (is_bad_inode(inode
)) {
1685 btrfs_orphan_del(NULL
, inode
);
1688 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1690 btrfs_i_size_write(inode
, 0);
1691 trans
= btrfs_start_transaction(root
, 1);
1693 btrfs_set_trans_block_group(trans
, inode
);
1694 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1696 btrfs_orphan_del(NULL
, inode
);
1697 goto no_delete_lock
;
1700 btrfs_orphan_del(trans
, inode
);
1702 nr
= trans
->blocks_used
;
1705 btrfs_end_transaction(trans
, root
);
1706 btrfs_btree_balance_dirty(root
, nr
);
1710 nr
= trans
->blocks_used
;
1711 btrfs_end_transaction(trans
, root
);
1712 btrfs_btree_balance_dirty(root
, nr
);
1718 * this returns the key found in the dir entry in the location pointer.
1719 * If no dir entries were found, location->objectid is 0.
1721 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1722 struct btrfs_key
*location
)
1724 const char *name
= dentry
->d_name
.name
;
1725 int namelen
= dentry
->d_name
.len
;
1726 struct btrfs_dir_item
*di
;
1727 struct btrfs_path
*path
;
1728 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1731 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1732 location
->objectid
= dir
->i_ino
;
1733 location
->type
= BTRFS_INODE_ITEM_KEY
;
1734 location
->offset
= 0;
1737 path
= btrfs_alloc_path();
1740 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1741 struct btrfs_key key
;
1742 struct extent_buffer
*leaf
;
1745 key
.objectid
= dir
->i_ino
;
1746 key
.offset
= (u64
)-1;
1747 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1748 if (ret
< 0 || path
->slots
[0] == 0)
1750 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1753 leaf
= path
->nodes
[0];
1754 slot
= path
->slots
[0] - 1;
1756 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1757 if (key
.objectid
!= dir
->i_ino
||
1758 key
.type
!= BTRFS_INODE_REF_KEY
) {
1761 location
->objectid
= key
.offset
;
1762 location
->type
= BTRFS_INODE_ITEM_KEY
;
1763 location
->offset
= 0;
1767 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1771 if (!di
|| IS_ERR(di
)) {
1774 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1776 btrfs_free_path(path
);
1779 location
->objectid
= 0;
1784 * when we hit a tree root in a directory, the btrfs part of the inode
1785 * needs to be changed to reflect the root directory of the tree root. This
1786 * is kind of like crossing a mount point.
1788 static int fixup_tree_root_location(struct btrfs_root
*root
,
1789 struct btrfs_key
*location
,
1790 struct btrfs_root
**sub_root
,
1791 struct dentry
*dentry
)
1793 struct btrfs_root_item
*ri
;
1795 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1797 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1800 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1801 dentry
->d_name
.name
,
1802 dentry
->d_name
.len
);
1803 if (IS_ERR(*sub_root
))
1804 return PTR_ERR(*sub_root
);
1806 ri
= &(*sub_root
)->root_item
;
1807 location
->objectid
= btrfs_root_dirid(ri
);
1808 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1809 location
->offset
= 0;
1814 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1816 struct btrfs_iget_args
*args
= p
;
1817 inode
->i_ino
= args
->ino
;
1818 BTRFS_I(inode
)->root
= args
->root
;
1819 BTRFS_I(inode
)->delalloc_bytes
= 0;
1820 BTRFS_I(inode
)->disk_i_size
= 0;
1821 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1822 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1823 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1824 inode
->i_mapping
, GFP_NOFS
);
1825 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1826 inode
->i_mapping
, GFP_NOFS
);
1827 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
1828 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1829 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1830 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1834 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1836 struct btrfs_iget_args
*args
= opaque
;
1837 return (args
->ino
== inode
->i_ino
&&
1838 args
->root
== BTRFS_I(inode
)->root
);
1841 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1844 struct btrfs_iget_args args
;
1845 args
.ino
= objectid
;
1846 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1851 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1854 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1855 struct btrfs_root
*root
)
1857 struct inode
*inode
;
1858 struct btrfs_iget_args args
;
1859 args
.ino
= objectid
;
1862 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1863 btrfs_init_locked_inode
,
1868 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1869 struct nameidata
*nd
)
1871 struct inode
* inode
;
1872 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1873 struct btrfs_root
*root
= bi
->root
;
1874 struct btrfs_root
*sub_root
= root
;
1875 struct btrfs_key location
;
1876 int ret
, do_orphan
= 0;
1878 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1879 return ERR_PTR(-ENAMETOOLONG
);
1881 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1884 return ERR_PTR(ret
);
1887 if (location
.objectid
) {
1888 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1891 return ERR_PTR(ret
);
1893 return ERR_PTR(-ENOENT
);
1895 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1898 return ERR_PTR(-EACCES
);
1899 if (inode
->i_state
& I_NEW
) {
1900 /* the inode and parent dir are two different roots */
1901 if (sub_root
!= root
) {
1903 sub_root
->inode
= inode
;
1906 BTRFS_I(inode
)->root
= sub_root
;
1907 memcpy(&BTRFS_I(inode
)->location
, &location
,
1909 btrfs_read_locked_inode(inode
);
1910 unlock_new_inode(inode
);
1914 if (unlikely(do_orphan
))
1915 btrfs_orphan_cleanup(sub_root
);
1917 return d_splice_alias(inode
, dentry
);
1920 static unsigned char btrfs_filetype_table
[] = {
1921 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1924 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1926 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1928 struct btrfs_item
*item
;
1929 struct btrfs_dir_item
*di
;
1930 struct btrfs_key key
;
1931 struct btrfs_key found_key
;
1932 struct btrfs_path
*path
;
1935 struct extent_buffer
*leaf
;
1938 unsigned char d_type
;
1943 int key_type
= BTRFS_DIR_INDEX_KEY
;
1948 /* FIXME, use a real flag for deciding about the key type */
1949 if (root
->fs_info
->tree_root
== root
)
1950 key_type
= BTRFS_DIR_ITEM_KEY
;
1952 /* special case for "." */
1953 if (filp
->f_pos
== 0) {
1954 over
= filldir(dirent
, ".", 1,
1962 key
.objectid
= inode
->i_ino
;
1963 path
= btrfs_alloc_path();
1966 /* special case for .., just use the back ref */
1967 if (filp
->f_pos
== 1) {
1968 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1969 key
.offset
= (u64
)-1;
1970 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1971 if (ret
< 0 || path
->slots
[0] == 0) {
1972 btrfs_release_path(root
, path
);
1973 goto read_dir_items
;
1976 leaf
= path
->nodes
[0];
1977 slot
= path
->slots
[0] - 1;
1978 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1979 btrfs_release_path(root
, path
);
1980 if (found_key
.objectid
!= key
.objectid
||
1981 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1982 goto read_dir_items
;
1983 over
= filldir(dirent
, "..", 2,
1984 2, found_key
.offset
, DT_DIR
);
1991 btrfs_set_key_type(&key
, key_type
);
1992 key
.offset
= filp
->f_pos
;
1994 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1999 leaf
= path
->nodes
[0];
2000 nritems
= btrfs_header_nritems(leaf
);
2001 slot
= path
->slots
[0];
2002 if (advance
|| slot
>= nritems
) {
2003 if (slot
>= nritems
-1) {
2004 ret
= btrfs_next_leaf(root
, path
);
2007 leaf
= path
->nodes
[0];
2008 nritems
= btrfs_header_nritems(leaf
);
2009 slot
= path
->slots
[0];
2016 item
= btrfs_item_nr(leaf
, slot
);
2017 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2019 if (found_key
.objectid
!= key
.objectid
)
2021 if (btrfs_key_type(&found_key
) != key_type
)
2023 if (found_key
.offset
< filp
->f_pos
)
2026 filp
->f_pos
= found_key
.offset
;
2028 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
2030 di_total
= btrfs_item_size(leaf
, item
);
2031 while(di_cur
< di_total
) {
2032 struct btrfs_key location
;
2034 name_len
= btrfs_dir_name_len(leaf
, di
);
2035 if (name_len
< 32) {
2036 name_ptr
= tmp_name
;
2038 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
2041 read_extent_buffer(leaf
, name_ptr
,
2042 (unsigned long)(di
+ 1), name_len
);
2044 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
2045 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
2046 over
= filldir(dirent
, name_ptr
, name_len
,
2051 if (name_ptr
!= tmp_name
)
2056 di_len
= btrfs_dir_name_len(leaf
, di
) +
2057 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
2059 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
2062 if (key_type
== BTRFS_DIR_INDEX_KEY
)
2063 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
2069 btrfs_free_path(path
);
2073 int btrfs_write_inode(struct inode
*inode
, int wait
)
2075 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2076 struct btrfs_trans_handle
*trans
;
2080 trans
= btrfs_join_transaction(root
, 1);
2081 btrfs_set_trans_block_group(trans
, inode
);
2082 ret
= btrfs_commit_transaction(trans
, root
);
2088 * This is somewhat expensive, updating the tree every time the
2089 * inode changes. But, it is most likely to find the inode in cache.
2090 * FIXME, needs more benchmarking...there are no reasons other than performance
2091 * to keep or drop this code.
2093 void btrfs_dirty_inode(struct inode
*inode
)
2095 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2096 struct btrfs_trans_handle
*trans
;
2098 trans
= btrfs_join_transaction(root
, 1);
2099 btrfs_set_trans_block_group(trans
, inode
);
2100 btrfs_update_inode(trans
, root
, inode
);
2101 btrfs_end_transaction(trans
, root
);
2104 static int btrfs_set_inode_index_count(struct inode
*inode
)
2106 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2107 struct btrfs_key key
, found_key
;
2108 struct btrfs_path
*path
;
2109 struct extent_buffer
*leaf
;
2112 key
.objectid
= inode
->i_ino
;
2113 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
2114 key
.offset
= (u64
)-1;
2116 path
= btrfs_alloc_path();
2120 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2123 /* FIXME: we should be able to handle this */
2129 * MAGIC NUMBER EXPLANATION:
2130 * since we search a directory based on f_pos we have to start at 2
2131 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2132 * else has to start at 2
2134 if (path
->slots
[0] == 0) {
2135 BTRFS_I(inode
)->index_cnt
= 2;
2141 leaf
= path
->nodes
[0];
2142 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2144 if (found_key
.objectid
!= inode
->i_ino
||
2145 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
2146 BTRFS_I(inode
)->index_cnt
= 2;
2150 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
2152 btrfs_free_path(path
);
2156 static int btrfs_set_inode_index(struct inode
*dir
, struct inode
*inode
,
2161 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
2162 ret
= btrfs_set_inode_index_count(dir
);
2167 *index
= BTRFS_I(dir
)->index_cnt
;
2168 BTRFS_I(dir
)->index_cnt
++;
2173 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
2174 struct btrfs_root
*root
,
2176 const char *name
, int name_len
,
2179 struct btrfs_block_group_cache
*group
,
2180 int mode
, u64
*index
)
2182 struct inode
*inode
;
2183 struct btrfs_inode_item
*inode_item
;
2184 struct btrfs_block_group_cache
*new_inode_group
;
2185 struct btrfs_key
*location
;
2186 struct btrfs_path
*path
;
2187 struct btrfs_inode_ref
*ref
;
2188 struct btrfs_key key
[2];
2194 path
= btrfs_alloc_path();
2197 inode
= new_inode(root
->fs_info
->sb
);
2199 return ERR_PTR(-ENOMEM
);
2202 ret
= btrfs_set_inode_index(dir
, inode
, index
);
2204 return ERR_PTR(ret
);
2207 * index_cnt is ignored for everything but a dir,
2208 * btrfs_get_inode_index_count has an explanation for the magic
2211 BTRFS_I(inode
)->index_cnt
= 2;
2213 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2214 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2215 inode
->i_mapping
, GFP_NOFS
);
2216 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2217 inode
->i_mapping
, GFP_NOFS
);
2218 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2219 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
2220 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2221 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2222 BTRFS_I(inode
)->delalloc_bytes
= 0;
2223 BTRFS_I(inode
)->disk_i_size
= 0;
2224 BTRFS_I(inode
)->root
= root
;
2230 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
2231 BTRFS_BLOCK_GROUP_METADATA
, owner
);
2232 if (!new_inode_group
) {
2233 printk("find_block group failed\n");
2234 new_inode_group
= group
;
2236 BTRFS_I(inode
)->block_group
= new_inode_group
;
2237 BTRFS_I(inode
)->flags
= 0;
2239 key
[0].objectid
= objectid
;
2240 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
2243 key
[1].objectid
= objectid
;
2244 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
2245 key
[1].offset
= ref_objectid
;
2247 sizes
[0] = sizeof(struct btrfs_inode_item
);
2248 sizes
[1] = name_len
+ sizeof(*ref
);
2250 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
2254 if (objectid
> root
->highest_inode
)
2255 root
->highest_inode
= objectid
;
2257 inode
->i_uid
= current
->fsuid
;
2258 inode
->i_gid
= current
->fsgid
;
2259 inode
->i_mode
= mode
;
2260 inode
->i_ino
= objectid
;
2261 inode
->i_blocks
= 0;
2262 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2263 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2264 struct btrfs_inode_item
);
2265 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
2267 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2268 struct btrfs_inode_ref
);
2269 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
2270 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
2271 ptr
= (unsigned long)(ref
+ 1);
2272 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
2274 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2275 btrfs_free_path(path
);
2277 location
= &BTRFS_I(inode
)->location
;
2278 location
->objectid
= objectid
;
2279 location
->offset
= 0;
2280 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2282 insert_inode_hash(inode
);
2286 BTRFS_I(dir
)->index_cnt
--;
2287 btrfs_free_path(path
);
2288 return ERR_PTR(ret
);
2291 static inline u8
btrfs_inode_type(struct inode
*inode
)
2293 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2296 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2297 struct dentry
*dentry
, struct inode
*inode
,
2298 int add_backref
, u64 index
)
2301 struct btrfs_key key
;
2302 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
2303 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
2305 key
.objectid
= inode
->i_ino
;
2306 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2309 ret
= btrfs_insert_dir_item(trans
, root
,
2310 dentry
->d_name
.name
, dentry
->d_name
.len
,
2311 dentry
->d_parent
->d_inode
->i_ino
,
2312 &key
, btrfs_inode_type(inode
),
2316 ret
= btrfs_insert_inode_ref(trans
, root
,
2317 dentry
->d_name
.name
,
2320 parent_inode
->i_ino
,
2323 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2324 dentry
->d_name
.len
* 2);
2325 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2326 ret
= btrfs_update_inode(trans
, root
,
2327 dentry
->d_parent
->d_inode
);
2332 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2333 struct dentry
*dentry
, struct inode
*inode
,
2334 int backref
, u64 index
)
2336 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
, index
);
2338 d_instantiate(dentry
, inode
);
2346 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2347 int mode
, dev_t rdev
)
2349 struct btrfs_trans_handle
*trans
;
2350 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2351 struct inode
*inode
= NULL
;
2355 unsigned long nr
= 0;
2358 if (!new_valid_dev(rdev
))
2361 err
= btrfs_check_free_space(root
, 1, 0);
2365 trans
= btrfs_start_transaction(root
, 1);
2366 btrfs_set_trans_block_group(trans
, dir
);
2368 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2374 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2376 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2377 BTRFS_I(dir
)->block_group
, mode
, &index
);
2378 err
= PTR_ERR(inode
);
2382 err
= btrfs_init_acl(inode
, dir
);
2388 btrfs_set_trans_block_group(trans
, inode
);
2389 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2393 inode
->i_op
= &btrfs_special_inode_operations
;
2394 init_special_inode(inode
, inode
->i_mode
, rdev
);
2395 btrfs_update_inode(trans
, root
, inode
);
2397 dir
->i_sb
->s_dirt
= 1;
2398 btrfs_update_inode_block_group(trans
, inode
);
2399 btrfs_update_inode_block_group(trans
, dir
);
2401 nr
= trans
->blocks_used
;
2402 btrfs_end_transaction_throttle(trans
, root
);
2405 inode_dec_link_count(inode
);
2408 btrfs_btree_balance_dirty(root
, nr
);
2412 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2413 int mode
, struct nameidata
*nd
)
2415 struct btrfs_trans_handle
*trans
;
2416 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2417 struct inode
*inode
= NULL
;
2420 unsigned long nr
= 0;
2424 err
= btrfs_check_free_space(root
, 1, 0);
2427 trans
= btrfs_start_transaction(root
, 1);
2428 btrfs_set_trans_block_group(trans
, dir
);
2430 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2436 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2438 dentry
->d_parent
->d_inode
->i_ino
,
2439 objectid
, BTRFS_I(dir
)->block_group
, mode
,
2441 err
= PTR_ERR(inode
);
2445 err
= btrfs_init_acl(inode
, dir
);
2451 btrfs_set_trans_block_group(trans
, inode
);
2452 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2456 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2457 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2458 inode
->i_fop
= &btrfs_file_operations
;
2459 inode
->i_op
= &btrfs_file_inode_operations
;
2460 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2461 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2462 inode
->i_mapping
, GFP_NOFS
);
2463 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2464 inode
->i_mapping
, GFP_NOFS
);
2465 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
2466 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2467 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2468 BTRFS_I(inode
)->delalloc_bytes
= 0;
2469 BTRFS_I(inode
)->disk_i_size
= 0;
2470 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2471 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2473 dir
->i_sb
->s_dirt
= 1;
2474 btrfs_update_inode_block_group(trans
, inode
);
2475 btrfs_update_inode_block_group(trans
, dir
);
2477 nr
= trans
->blocks_used
;
2478 btrfs_end_transaction_throttle(trans
, root
);
2481 inode_dec_link_count(inode
);
2484 btrfs_btree_balance_dirty(root
, nr
);
2488 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2489 struct dentry
*dentry
)
2491 struct btrfs_trans_handle
*trans
;
2492 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2493 struct inode
*inode
= old_dentry
->d_inode
;
2495 unsigned long nr
= 0;
2499 if (inode
->i_nlink
== 0)
2502 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2507 err
= btrfs_check_free_space(root
, 1, 0);
2510 err
= btrfs_set_inode_index(dir
, inode
, &index
);
2514 trans
= btrfs_start_transaction(root
, 1);
2516 btrfs_set_trans_block_group(trans
, dir
);
2517 atomic_inc(&inode
->i_count
);
2519 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
2524 dir
->i_sb
->s_dirt
= 1;
2525 btrfs_update_inode_block_group(trans
, dir
);
2526 err
= btrfs_update_inode(trans
, root
, inode
);
2531 nr
= trans
->blocks_used
;
2532 btrfs_end_transaction_throttle(trans
, root
);
2535 inode_dec_link_count(inode
);
2538 btrfs_btree_balance_dirty(root
, nr
);
2542 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2544 struct inode
*inode
= NULL
;
2545 struct btrfs_trans_handle
*trans
;
2546 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2548 int drop_on_err
= 0;
2551 unsigned long nr
= 1;
2553 err
= btrfs_check_free_space(root
, 1, 0);
2557 trans
= btrfs_start_transaction(root
, 1);
2558 btrfs_set_trans_block_group(trans
, dir
);
2560 if (IS_ERR(trans
)) {
2561 err
= PTR_ERR(trans
);
2565 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2571 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2573 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2574 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
2576 if (IS_ERR(inode
)) {
2577 err
= PTR_ERR(inode
);
2583 err
= btrfs_init_acl(inode
, dir
);
2587 inode
->i_op
= &btrfs_dir_inode_operations
;
2588 inode
->i_fop
= &btrfs_dir_file_operations
;
2589 btrfs_set_trans_block_group(trans
, inode
);
2591 btrfs_i_size_write(inode
, 0);
2592 err
= btrfs_update_inode(trans
, root
, inode
);
2596 err
= btrfs_add_link(trans
, dentry
, inode
, 0, index
);
2600 d_instantiate(dentry
, inode
);
2602 dir
->i_sb
->s_dirt
= 1;
2603 btrfs_update_inode_block_group(trans
, inode
);
2604 btrfs_update_inode_block_group(trans
, dir
);
2607 nr
= trans
->blocks_used
;
2608 btrfs_end_transaction_throttle(trans
, root
);
2613 btrfs_btree_balance_dirty(root
, nr
);
2617 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2618 struct extent_map
*existing
,
2619 struct extent_map
*em
,
2620 u64 map_start
, u64 map_len
)
2624 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2625 start_diff
= map_start
- em
->start
;
2626 em
->start
= map_start
;
2628 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2629 em
->block_start
+= start_diff
;
2630 return add_extent_mapping(em_tree
, em
);
2633 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2634 size_t pg_offset
, u64 start
, u64 len
,
2640 u64 extent_start
= 0;
2642 u64 objectid
= inode
->i_ino
;
2644 struct btrfs_path
*path
= NULL
;
2645 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2646 struct btrfs_file_extent_item
*item
;
2647 struct extent_buffer
*leaf
;
2648 struct btrfs_key found_key
;
2649 struct extent_map
*em
= NULL
;
2650 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2651 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2652 struct btrfs_trans_handle
*trans
= NULL
;
2655 spin_lock(&em_tree
->lock
);
2656 em
= lookup_extent_mapping(em_tree
, start
, len
);
2658 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2659 spin_unlock(&em_tree
->lock
);
2662 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2663 free_extent_map(em
);
2664 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2665 free_extent_map(em
);
2669 em
= alloc_extent_map(GFP_NOFS
);
2674 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2675 em
->start
= EXTENT_MAP_HOLE
;
2679 path
= btrfs_alloc_path();
2683 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2684 objectid
, start
, trans
!= NULL
);
2691 if (path
->slots
[0] == 0)
2696 leaf
= path
->nodes
[0];
2697 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2698 struct btrfs_file_extent_item
);
2699 /* are we inside the extent that was found? */
2700 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2701 found_type
= btrfs_key_type(&found_key
);
2702 if (found_key
.objectid
!= objectid
||
2703 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2707 found_type
= btrfs_file_extent_type(leaf
, item
);
2708 extent_start
= found_key
.offset
;
2709 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2710 extent_end
= extent_start
+
2711 btrfs_file_extent_num_bytes(leaf
, item
);
2713 if (start
< extent_start
|| start
>= extent_end
) {
2715 if (start
< extent_start
) {
2716 if (start
+ len
<= extent_start
)
2718 em
->len
= extent_end
- extent_start
;
2724 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2726 em
->start
= extent_start
;
2727 em
->len
= extent_end
- extent_start
;
2728 em
->block_start
= EXTENT_MAP_HOLE
;
2731 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2732 em
->block_start
= bytenr
;
2733 em
->start
= extent_start
;
2734 em
->len
= extent_end
- extent_start
;
2736 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2741 size_t extent_offset
;
2744 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2746 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2747 ~((u64
)root
->sectorsize
- 1);
2748 if (start
< extent_start
|| start
>= extent_end
) {
2750 if (start
< extent_start
) {
2751 if (start
+ len
<= extent_start
)
2753 em
->len
= extent_end
- extent_start
;
2759 em
->block_start
= EXTENT_MAP_INLINE
;
2762 em
->start
= extent_start
;
2767 page_start
= page_offset(page
) + pg_offset
;
2768 extent_offset
= page_start
- extent_start
;
2769 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2770 size
- extent_offset
);
2771 em
->start
= extent_start
+ extent_offset
;
2772 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2773 ~((u64
)root
->sectorsize
- 1);
2775 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2776 if (create
== 0 && !PageUptodate(page
)) {
2777 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2779 flush_dcache_page(page
);
2780 } else if (create
&& PageUptodate(page
)) {
2783 free_extent_map(em
);
2785 btrfs_release_path(root
, path
);
2786 trans
= btrfs_join_transaction(root
, 1);
2789 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2791 btrfs_mark_buffer_dirty(leaf
);
2794 set_extent_uptodate(io_tree
, em
->start
,
2795 extent_map_end(em
) - 1, GFP_NOFS
);
2798 printk("unkknown found_type %d\n", found_type
);
2805 em
->block_start
= EXTENT_MAP_HOLE
;
2807 btrfs_release_path(root
, path
);
2808 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2809 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2815 spin_lock(&em_tree
->lock
);
2816 ret
= add_extent_mapping(em_tree
, em
);
2817 /* it is possible that someone inserted the extent into the tree
2818 * while we had the lock dropped. It is also possible that
2819 * an overlapping map exists in the tree
2821 if (ret
== -EEXIST
) {
2822 struct extent_map
*existing
;
2826 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2827 if (existing
&& (existing
->start
> start
||
2828 existing
->start
+ existing
->len
<= start
)) {
2829 free_extent_map(existing
);
2833 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2836 err
= merge_extent_mapping(em_tree
, existing
,
2839 free_extent_map(existing
);
2841 free_extent_map(em
);
2846 printk("failing to insert %Lu %Lu\n",
2848 free_extent_map(em
);
2852 free_extent_map(em
);
2857 spin_unlock(&em_tree
->lock
);
2860 btrfs_free_path(path
);
2862 ret
= btrfs_end_transaction(trans
, root
);
2868 free_extent_map(em
);
2870 return ERR_PTR(err
);
2875 #if 0 /* waiting for O_DIRECT reads */
2876 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2877 struct buffer_head
*bh_result
, int create
)
2879 struct extent_map
*em
;
2880 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2881 struct btrfs_multi_bio
*multi
= NULL
;
2882 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2888 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2890 if (!em
|| IS_ERR(em
))
2893 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2897 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2902 len
= em
->start
+ em
->len
- start
;
2903 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2905 if (em
->block_start
== EXTENT_MAP_HOLE
||
2906 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2907 bh_result
->b_size
= len
;
2911 logical
= start
- em
->start
;
2912 logical
= em
->block_start
+ logical
;
2915 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2916 logical
, &map_length
, &multi
, 0);
2918 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2919 bh_result
->b_size
= min(map_length
, len
);
2921 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2922 set_buffer_mapped(bh_result
);
2925 free_extent_map(em
);
2930 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2931 const struct iovec
*iov
, loff_t offset
,
2932 unsigned long nr_segs
)
2936 struct file
*file
= iocb
->ki_filp
;
2937 struct inode
*inode
= file
->f_mapping
->host
;
2942 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2943 offset
, nr_segs
, btrfs_get_block
, NULL
);
2947 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2949 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2952 int btrfs_readpage(struct file
*file
, struct page
*page
)
2954 struct extent_io_tree
*tree
;
2955 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2956 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2959 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2961 struct extent_io_tree
*tree
;
2964 if (current
->flags
& PF_MEMALLOC
) {
2965 redirty_page_for_writepage(wbc
, page
);
2969 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2970 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2973 int btrfs_writepages(struct address_space
*mapping
,
2974 struct writeback_control
*wbc
)
2976 struct extent_io_tree
*tree
;
2977 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2978 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2982 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2983 struct list_head
*pages
, unsigned nr_pages
)
2985 struct extent_io_tree
*tree
;
2986 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2987 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2990 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2992 struct extent_io_tree
*tree
;
2993 struct extent_map_tree
*map
;
2996 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2997 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2998 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
3000 ClearPagePrivate(page
);
3001 set_page_private(page
, 0);
3002 page_cache_release(page
);
3007 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
3009 return __btrfs_releasepage(page
, gfp_flags
);
3012 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
3014 struct extent_io_tree
*tree
;
3015 struct btrfs_ordered_extent
*ordered
;
3016 u64 page_start
= page_offset(page
);
3017 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3019 wait_on_page_writeback(page
);
3020 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3022 btrfs_releasepage(page
, GFP_NOFS
);
3026 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3027 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
3031 * IO on this page will never be started, so we need
3032 * to account for any ordered extents now
3034 clear_extent_bit(tree
, page_start
, page_end
,
3035 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3036 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
3037 btrfs_finish_ordered_io(page
->mapping
->host
,
3038 page_start
, page_end
);
3039 btrfs_put_ordered_extent(ordered
);
3040 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3042 clear_extent_bit(tree
, page_start
, page_end
,
3043 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3046 __btrfs_releasepage(page
, GFP_NOFS
);
3048 ClearPageChecked(page
);
3049 if (PagePrivate(page
)) {
3050 ClearPagePrivate(page
);
3051 set_page_private(page
, 0);
3052 page_cache_release(page
);
3057 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3058 * called from a page fault handler when a page is first dirtied. Hence we must
3059 * be careful to check for EOF conditions here. We set the page up correctly
3060 * for a written page which means we get ENOSPC checking when writing into
3061 * holes and correct delalloc and unwritten extent mapping on filesystems that
3062 * support these features.
3064 * We are not allowed to take the i_mutex here so we have to play games to
3065 * protect against truncate races as the page could now be beyond EOF. Because
3066 * vmtruncate() writes the inode size before removing pages, once we have the
3067 * page lock we can determine safely if the page is beyond EOF. If it is not
3068 * beyond EOF, then the page is guaranteed safe against truncation until we
3071 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
3073 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
3074 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3075 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3076 struct btrfs_ordered_extent
*ordered
;
3078 unsigned long zero_start
;
3084 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
3091 size
= i_size_read(inode
);
3092 page_start
= page_offset(page
);
3093 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3095 if ((page
->mapping
!= inode
->i_mapping
) ||
3096 (page_start
>= size
)) {
3097 /* page got truncated out from underneath us */
3100 wait_on_page_writeback(page
);
3102 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3103 set_page_extent_mapped(page
);
3106 * we can't set the delalloc bits if there are pending ordered
3107 * extents. Drop our locks and wait for them to finish
3109 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3111 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3113 btrfs_start_ordered_extent(inode
, ordered
, 1);
3114 btrfs_put_ordered_extent(ordered
);
3118 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3121 /* page is wholly or partially inside EOF */
3122 if (page_start
+ PAGE_CACHE_SIZE
> size
)
3123 zero_start
= size
& ~PAGE_CACHE_MASK
;
3125 zero_start
= PAGE_CACHE_SIZE
;
3127 if (zero_start
!= PAGE_CACHE_SIZE
) {
3129 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
3130 flush_dcache_page(page
);
3133 ClearPageChecked(page
);
3134 set_page_dirty(page
);
3135 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3143 static void btrfs_truncate(struct inode
*inode
)
3145 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3147 struct btrfs_trans_handle
*trans
;
3149 u64 mask
= root
->sectorsize
- 1;
3151 if (!S_ISREG(inode
->i_mode
))
3153 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3156 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3157 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
3159 trans
= btrfs_start_transaction(root
, 1);
3160 btrfs_set_trans_block_group(trans
, inode
);
3161 btrfs_i_size_write(inode
, inode
->i_size
);
3163 ret
= btrfs_orphan_add(trans
, inode
);
3166 /* FIXME, add redo link to tree so we don't leak on crash */
3167 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
3168 BTRFS_EXTENT_DATA_KEY
);
3169 btrfs_update_inode(trans
, root
, inode
);
3171 ret
= btrfs_orphan_del(trans
, inode
);
3175 nr
= trans
->blocks_used
;
3176 ret
= btrfs_end_transaction_throttle(trans
, root
);
3178 btrfs_btree_balance_dirty(root
, nr
);
3182 * Invalidate a single dcache entry at the root of the filesystem.
3183 * Needed after creation of snapshot or subvolume.
3185 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
3188 struct dentry
*alias
, *entry
;
3191 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
3195 /* change me if btrfs ever gets a d_hash operation */
3196 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
3197 entry
= d_lookup(alias
, &qstr
);
3200 d_invalidate(entry
);
3206 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
3207 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
3208 struct btrfs_block_group_cache
*block_group
)
3210 struct inode
*inode
;
3213 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
3214 new_dirid
, block_group
, S_IFDIR
| 0700, &index
);
3216 return PTR_ERR(inode
);
3217 inode
->i_op
= &btrfs_dir_inode_operations
;
3218 inode
->i_fop
= &btrfs_dir_file_operations
;
3219 new_root
->inode
= inode
;
3222 btrfs_i_size_write(inode
, 0);
3224 return btrfs_update_inode(trans
, new_root
, inode
);
3227 unsigned long btrfs_force_ra(struct address_space
*mapping
,
3228 struct file_ra_state
*ra
, struct file
*file
,
3229 pgoff_t offset
, pgoff_t last_index
)
3231 pgoff_t req_size
= last_index
- offset
+ 1;
3233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3234 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
3237 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
3238 return offset
+ req_size
;
3242 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3244 struct btrfs_inode
*ei
;
3246 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3250 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
3251 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
3252 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
3253 INIT_LIST_HEAD(&ei
->i_orphan
);
3254 return &ei
->vfs_inode
;
3257 void btrfs_destroy_inode(struct inode
*inode
)
3259 struct btrfs_ordered_extent
*ordered
;
3260 WARN_ON(!list_empty(&inode
->i_dentry
));
3261 WARN_ON(inode
->i_data
.nrpages
);
3263 if (BTRFS_I(inode
)->i_acl
&&
3264 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
3265 posix_acl_release(BTRFS_I(inode
)->i_acl
);
3266 if (BTRFS_I(inode
)->i_default_acl
&&
3267 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
3268 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
3270 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
3271 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
3272 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
3273 " list\n", inode
->i_ino
);
3276 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
3279 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
3283 printk("found ordered extent %Lu %Lu\n",
3284 ordered
->file_offset
, ordered
->len
);
3285 btrfs_remove_ordered_extent(inode
, ordered
);
3286 btrfs_put_ordered_extent(ordered
);
3287 btrfs_put_ordered_extent(ordered
);
3290 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
3291 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3294 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3295 static void init_once(void *foo
)
3296 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3297 static void init_once(struct kmem_cache
* cachep
, void *foo
)
3299 static void init_once(void * foo
, struct kmem_cache
* cachep
,
3300 unsigned long flags
)
3303 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3305 inode_init_once(&ei
->vfs_inode
);
3308 void btrfs_destroy_cachep(void)
3310 if (btrfs_inode_cachep
)
3311 kmem_cache_destroy(btrfs_inode_cachep
);
3312 if (btrfs_trans_handle_cachep
)
3313 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3314 if (btrfs_transaction_cachep
)
3315 kmem_cache_destroy(btrfs_transaction_cachep
);
3316 if (btrfs_bit_radix_cachep
)
3317 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3318 if (btrfs_path_cachep
)
3319 kmem_cache_destroy(btrfs_path_cachep
);
3322 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3323 unsigned long extra_flags
,
3324 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3325 void (*ctor
)(void *)
3326 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3327 void (*ctor
)(struct kmem_cache
*, void *)
3329 void (*ctor
)(void *, struct kmem_cache
*,
3334 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3335 SLAB_MEM_SPREAD
| extra_flags
), ctor
3336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3342 int btrfs_init_cachep(void)
3344 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3345 sizeof(struct btrfs_inode
),
3347 if (!btrfs_inode_cachep
)
3349 btrfs_trans_handle_cachep
=
3350 btrfs_cache_create("btrfs_trans_handle_cache",
3351 sizeof(struct btrfs_trans_handle
),
3353 if (!btrfs_trans_handle_cachep
)
3355 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3356 sizeof(struct btrfs_transaction
),
3358 if (!btrfs_transaction_cachep
)
3360 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3361 sizeof(struct btrfs_path
),
3363 if (!btrfs_path_cachep
)
3365 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3366 SLAB_DESTROY_BY_RCU
, NULL
);
3367 if (!btrfs_bit_radix_cachep
)
3371 btrfs_destroy_cachep();
3375 static int btrfs_getattr(struct vfsmount
*mnt
,
3376 struct dentry
*dentry
, struct kstat
*stat
)
3378 struct inode
*inode
= dentry
->d_inode
;
3379 generic_fillattr(inode
, stat
);
3380 stat
->blksize
= PAGE_CACHE_SIZE
;
3381 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3385 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3386 struct inode
* new_dir
,struct dentry
*new_dentry
)
3388 struct btrfs_trans_handle
*trans
;
3389 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3390 struct inode
*new_inode
= new_dentry
->d_inode
;
3391 struct inode
*old_inode
= old_dentry
->d_inode
;
3392 struct timespec ctime
= CURRENT_TIME
;
3396 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3397 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3401 ret
= btrfs_check_free_space(root
, 1, 0);
3405 trans
= btrfs_start_transaction(root
, 1);
3407 btrfs_set_trans_block_group(trans
, new_dir
);
3409 old_dentry
->d_inode
->i_nlink
++;
3410 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3411 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3412 old_inode
->i_ctime
= ctime
;
3414 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3419 new_inode
->i_ctime
= CURRENT_TIME
;
3420 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3423 if (new_inode
->i_nlink
== 0) {
3424 ret
= btrfs_orphan_add(trans
, new_inode
);
3429 ret
= btrfs_set_inode_index(new_dir
, old_inode
, &index
);
3433 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1, index
);
3438 btrfs_end_transaction_throttle(trans
, root
);
3443 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
)
3445 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
3446 struct btrfs_inode
*binode
;
3447 unsigned long flags
;
3449 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3450 while(!list_empty(head
)) {
3451 binode
= list_entry(head
->next
, struct btrfs_inode
,
3453 atomic_inc(&binode
->vfs_inode
.i_count
);
3454 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3455 filemap_write_and_wait(binode
->vfs_inode
.i_mapping
);
3456 iput(&binode
->vfs_inode
);
3457 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3459 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3463 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3464 const char *symname
)
3466 struct btrfs_trans_handle
*trans
;
3467 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3468 struct btrfs_path
*path
;
3469 struct btrfs_key key
;
3470 struct inode
*inode
= NULL
;
3478 struct btrfs_file_extent_item
*ei
;
3479 struct extent_buffer
*leaf
;
3480 unsigned long nr
= 0;
3482 name_len
= strlen(symname
) + 1;
3483 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3484 return -ENAMETOOLONG
;
3486 err
= btrfs_check_free_space(root
, 1, 0);
3490 trans
= btrfs_start_transaction(root
, 1);
3491 btrfs_set_trans_block_group(trans
, dir
);
3493 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3499 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3501 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3502 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
3504 err
= PTR_ERR(inode
);
3508 err
= btrfs_init_acl(inode
, dir
);
3514 btrfs_set_trans_block_group(trans
, inode
);
3515 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
3519 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3520 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3521 inode
->i_fop
= &btrfs_file_operations
;
3522 inode
->i_op
= &btrfs_file_inode_operations
;
3523 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3524 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3525 inode
->i_mapping
, GFP_NOFS
);
3526 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3527 inode
->i_mapping
, GFP_NOFS
);
3528 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
3529 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3530 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3531 BTRFS_I(inode
)->delalloc_bytes
= 0;
3532 BTRFS_I(inode
)->disk_i_size
= 0;
3533 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3534 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3536 dir
->i_sb
->s_dirt
= 1;
3537 btrfs_update_inode_block_group(trans
, inode
);
3538 btrfs_update_inode_block_group(trans
, dir
);
3542 path
= btrfs_alloc_path();
3544 key
.objectid
= inode
->i_ino
;
3546 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3547 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3548 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3554 leaf
= path
->nodes
[0];
3555 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3556 struct btrfs_file_extent_item
);
3557 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3558 btrfs_set_file_extent_type(leaf
, ei
,
3559 BTRFS_FILE_EXTENT_INLINE
);
3560 ptr
= btrfs_file_extent_inline_start(ei
);
3561 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3562 btrfs_mark_buffer_dirty(leaf
);
3563 btrfs_free_path(path
);
3565 inode
->i_op
= &btrfs_symlink_inode_operations
;
3566 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3567 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3568 btrfs_i_size_write(inode
, name_len
- 1);
3569 err
= btrfs_update_inode(trans
, root
, inode
);
3574 nr
= trans
->blocks_used
;
3575 btrfs_end_transaction_throttle(trans
, root
);
3578 inode_dec_link_count(inode
);
3581 btrfs_btree_balance_dirty(root
, nr
);
3585 static int btrfs_set_page_dirty(struct page
*page
)
3587 return __set_page_dirty_nobuffers(page
);
3590 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3591 static int btrfs_permission(struct inode
*inode
, int mask
)
3593 static int btrfs_permission(struct inode
*inode
, int mask
,
3594 struct nameidata
*nd
)
3597 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3599 return generic_permission(inode
, mask
, btrfs_check_acl
);
3602 static struct inode_operations btrfs_dir_inode_operations
= {
3603 .lookup
= btrfs_lookup
,
3604 .create
= btrfs_create
,
3605 .unlink
= btrfs_unlink
,
3607 .mkdir
= btrfs_mkdir
,
3608 .rmdir
= btrfs_rmdir
,
3609 .rename
= btrfs_rename
,
3610 .symlink
= btrfs_symlink
,
3611 .setattr
= btrfs_setattr
,
3612 .mknod
= btrfs_mknod
,
3613 .setxattr
= generic_setxattr
,
3614 .getxattr
= generic_getxattr
,
3615 .listxattr
= btrfs_listxattr
,
3616 .removexattr
= generic_removexattr
,
3617 .permission
= btrfs_permission
,
3619 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3620 .lookup
= btrfs_lookup
,
3621 .permission
= btrfs_permission
,
3623 static struct file_operations btrfs_dir_file_operations
= {
3624 .llseek
= generic_file_llseek
,
3625 .read
= generic_read_dir
,
3626 .readdir
= btrfs_readdir
,
3627 .unlocked_ioctl
= btrfs_ioctl
,
3628 #ifdef CONFIG_COMPAT
3629 .compat_ioctl
= btrfs_ioctl
,
3631 .release
= btrfs_release_file
,
3634 static struct extent_io_ops btrfs_extent_io_ops
= {
3635 .fill_delalloc
= run_delalloc_range
,
3636 .submit_bio_hook
= btrfs_submit_bio_hook
,
3637 .merge_bio_hook
= btrfs_merge_bio_hook
,
3638 .readpage_io_hook
= btrfs_readpage_io_hook
,
3639 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3640 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3641 .writepage_start_hook
= btrfs_writepage_start_hook
,
3642 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3643 .set_bit_hook
= btrfs_set_bit_hook
,
3644 .clear_bit_hook
= btrfs_clear_bit_hook
,
3647 static struct address_space_operations btrfs_aops
= {
3648 .readpage
= btrfs_readpage
,
3649 .writepage
= btrfs_writepage
,
3650 .writepages
= btrfs_writepages
,
3651 .readpages
= btrfs_readpages
,
3652 .sync_page
= block_sync_page
,
3654 .direct_IO
= btrfs_direct_IO
,
3655 .invalidatepage
= btrfs_invalidatepage
,
3656 .releasepage
= btrfs_releasepage
,
3657 .set_page_dirty
= btrfs_set_page_dirty
,
3660 static struct address_space_operations btrfs_symlink_aops
= {
3661 .readpage
= btrfs_readpage
,
3662 .writepage
= btrfs_writepage
,
3663 .invalidatepage
= btrfs_invalidatepage
,
3664 .releasepage
= btrfs_releasepage
,
3667 static struct inode_operations btrfs_file_inode_operations
= {
3668 .truncate
= btrfs_truncate
,
3669 .getattr
= btrfs_getattr
,
3670 .setattr
= btrfs_setattr
,
3671 .setxattr
= generic_setxattr
,
3672 .getxattr
= generic_getxattr
,
3673 .listxattr
= btrfs_listxattr
,
3674 .removexattr
= generic_removexattr
,
3675 .permission
= btrfs_permission
,
3677 static struct inode_operations btrfs_special_inode_operations
= {
3678 .getattr
= btrfs_getattr
,
3679 .setattr
= btrfs_setattr
,
3680 .permission
= btrfs_permission
,
3681 .setxattr
= generic_setxattr
,
3682 .getxattr
= generic_getxattr
,
3683 .listxattr
= btrfs_listxattr
,
3684 .removexattr
= generic_removexattr
,
3686 static struct inode_operations btrfs_symlink_inode_operations
= {
3687 .readlink
= generic_readlink
,
3688 .follow_link
= page_follow_link_light
,
3689 .put_link
= page_put_link
,
3690 .permission
= btrfs_permission
,