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 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
380 btrfs_lookup_bio_sums(root
, inode
, bio
);
381 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
386 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
387 inode
, rw
, bio
, mirror_num
,
388 __btrfs_submit_bio_hook
);
390 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
393 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
394 struct inode
*inode
, u64 file_offset
,
395 struct list_head
*list
)
397 struct list_head
*cur
;
398 struct btrfs_ordered_sum
*sum
;
400 btrfs_set_trans_block_group(trans
, inode
);
401 list_for_each(cur
, list
) {
402 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
403 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
404 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
406 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
411 struct btrfs_writepage_fixup
{
413 struct btrfs_work work
;
416 /* see btrfs_writepage_start_hook for details on why this is required */
417 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
419 struct btrfs_writepage_fixup
*fixup
;
420 struct btrfs_ordered_extent
*ordered
;
426 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
430 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
431 ClearPageChecked(page
);
435 inode
= page
->mapping
->host
;
436 page_start
= page_offset(page
);
437 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
439 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
441 /* already ordered? We're done */
442 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
443 EXTENT_ORDERED
, 0)) {
447 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
449 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
452 btrfs_start_ordered_extent(inode
, ordered
, 1);
456 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
458 ClearPageChecked(page
);
460 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
463 page_cache_release(page
);
467 * There are a few paths in the higher layers of the kernel that directly
468 * set the page dirty bit without asking the filesystem if it is a
469 * good idea. This causes problems because we want to make sure COW
470 * properly happens and the data=ordered rules are followed.
472 * In our case any range that doesn't have the EXTENT_ORDERED bit set
473 * hasn't been properly setup for IO. We kick off an async process
474 * to fix it up. The async helper will wait for ordered extents, set
475 * the delalloc bit and make it safe to write the page.
477 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
479 struct inode
*inode
= page
->mapping
->host
;
480 struct btrfs_writepage_fixup
*fixup
;
481 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
484 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
489 if (PageChecked(page
))
492 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
496 SetPageChecked(page
);
497 page_cache_get(page
);
498 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
500 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
504 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
506 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
507 struct btrfs_trans_handle
*trans
;
508 struct btrfs_ordered_extent
*ordered_extent
;
509 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
511 struct list_head list
;
512 struct btrfs_key ins
;
515 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
519 trans
= btrfs_join_transaction(root
, 1);
521 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
522 BUG_ON(!ordered_extent
);
524 lock_extent(io_tree
, ordered_extent
->file_offset
,
525 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
528 INIT_LIST_HEAD(&list
);
530 ins
.objectid
= ordered_extent
->start
;
531 ins
.offset
= ordered_extent
->len
;
532 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
534 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
535 trans
->transid
, inode
->i_ino
,
536 ordered_extent
->file_offset
, &ins
);
539 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
541 ret
= btrfs_drop_extents(trans
, root
, inode
,
542 ordered_extent
->file_offset
,
543 ordered_extent
->file_offset
+
545 ordered_extent
->file_offset
, &alloc_hint
);
547 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
548 ordered_extent
->file_offset
,
549 ordered_extent
->start
,
551 ordered_extent
->len
, 0);
554 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
555 ordered_extent
->file_offset
+
556 ordered_extent
->len
- 1);
557 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
559 inode
->i_blocks
+= ordered_extent
->len
>> 9;
560 unlock_extent(io_tree
, ordered_extent
->file_offset
,
561 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
563 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
564 &ordered_extent
->list
);
566 btrfs_ordered_update_i_size(inode
, ordered_extent
);
567 btrfs_remove_ordered_extent(inode
, ordered_extent
);
570 btrfs_put_ordered_extent(ordered_extent
);
571 /* once for the tree */
572 btrfs_put_ordered_extent(ordered_extent
);
574 btrfs_update_inode(trans
, root
, inode
);
575 btrfs_end_transaction(trans
, root
);
579 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
580 struct extent_state
*state
, int uptodate
)
582 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
585 struct io_failure_record
{
593 int btrfs_io_failed_hook(struct bio
*failed_bio
,
594 struct page
*page
, u64 start
, u64 end
,
595 struct extent_state
*state
)
597 struct io_failure_record
*failrec
= NULL
;
599 struct extent_map
*em
;
600 struct inode
*inode
= page
->mapping
->host
;
601 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
602 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
609 ret
= get_state_private(failure_tree
, start
, &private);
611 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
614 failrec
->start
= start
;
615 failrec
->len
= end
- start
+ 1;
616 failrec
->last_mirror
= 0;
618 spin_lock(&em_tree
->lock
);
619 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
620 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
624 spin_unlock(&em_tree
->lock
);
626 if (!em
|| IS_ERR(em
)) {
630 logical
= start
- em
->start
;
631 logical
= em
->block_start
+ logical
;
632 failrec
->logical
= logical
;
634 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
635 EXTENT_DIRTY
, GFP_NOFS
);
636 set_state_private(failure_tree
, start
,
637 (u64
)(unsigned long)failrec
);
639 failrec
= (struct io_failure_record
*)(unsigned long)private;
641 num_copies
= btrfs_num_copies(
642 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
643 failrec
->logical
, failrec
->len
);
644 failrec
->last_mirror
++;
646 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
647 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
650 if (state
&& state
->start
!= failrec
->start
)
652 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
654 if (!state
|| failrec
->last_mirror
> num_copies
) {
655 set_state_private(failure_tree
, failrec
->start
, 0);
656 clear_extent_bits(failure_tree
, failrec
->start
,
657 failrec
->start
+ failrec
->len
- 1,
658 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
662 bio
= bio_alloc(GFP_NOFS
, 1);
663 bio
->bi_private
= state
;
664 bio
->bi_end_io
= failed_bio
->bi_end_io
;
665 bio
->bi_sector
= failrec
->logical
>> 9;
666 bio
->bi_bdev
= failed_bio
->bi_bdev
;
668 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
669 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
674 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
675 failrec
->last_mirror
);
679 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
683 struct io_failure_record
*failure
;
687 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
688 (u64
)-1, 1, EXTENT_DIRTY
)) {
689 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
690 start
, &private_failure
);
692 failure
= (struct io_failure_record
*)(unsigned long)
694 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
696 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
698 failure
->start
+ failure
->len
- 1,
699 EXTENT_DIRTY
| EXTENT_LOCKED
,
707 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
708 struct extent_state
*state
)
710 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
711 struct inode
*inode
= page
->mapping
->host
;
712 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
714 u64
private = ~(u32
)0;
716 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
720 if (btrfs_test_opt(root
, NODATASUM
) ||
721 btrfs_test_flag(inode
, NODATASUM
))
723 if (state
&& state
->start
== start
) {
724 private = state
->private;
727 ret
= get_state_private(io_tree
, start
, &private);
729 local_irq_save(flags
);
730 kaddr
= kmap_atomic(page
, KM_IRQ0
);
734 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
735 btrfs_csum_final(csum
, (char *)&csum
);
736 if (csum
!= private) {
739 kunmap_atomic(kaddr
, KM_IRQ0
);
740 local_irq_restore(flags
);
742 /* if the io failure tree for this inode is non-empty,
743 * check to see if we've recovered from a failed IO
745 btrfs_clean_io_failures(inode
, start
);
749 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
750 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
752 memset(kaddr
+ offset
, 1, end
- start
+ 1);
753 flush_dcache_page(page
);
754 kunmap_atomic(kaddr
, KM_IRQ0
);
755 local_irq_restore(flags
);
762 * This creates an orphan entry for the given inode in case something goes
763 * wrong in the middle of an unlink/truncate.
765 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
767 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
770 spin_lock(&root
->list_lock
);
772 /* already on the orphan list, we're good */
773 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
774 spin_unlock(&root
->list_lock
);
778 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
780 spin_unlock(&root
->list_lock
);
783 * insert an orphan item to track this unlinked/truncated file
785 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
791 * We have done the truncate/delete so we can go ahead and remove the orphan
792 * item for this particular inode.
794 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
796 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
799 spin_lock(&root
->list_lock
);
801 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
802 spin_unlock(&root
->list_lock
);
806 list_del_init(&BTRFS_I(inode
)->i_orphan
);
808 spin_unlock(&root
->list_lock
);
812 spin_unlock(&root
->list_lock
);
814 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
820 * this cleans up any orphans that may be left on the list from the last use
823 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
825 struct btrfs_path
*path
;
826 struct extent_buffer
*leaf
;
827 struct btrfs_item
*item
;
828 struct btrfs_key key
, found_key
;
829 struct btrfs_trans_handle
*trans
;
831 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
833 /* don't do orphan cleanup if the fs is readonly. */
834 if (root
->inode
->i_sb
->s_flags
& MS_RDONLY
)
837 path
= btrfs_alloc_path();
842 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
843 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
844 key
.offset
= (u64
)-1;
846 trans
= btrfs_start_transaction(root
, 1);
847 btrfs_set_trans_block_group(trans
, root
->inode
);
850 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
852 printk(KERN_ERR
"Error searching slot for orphan: %d"
858 * if ret == 0 means we found what we were searching for, which
859 * is weird, but possible, so only screw with path if we didnt
860 * find the key and see if we have stuff that matches
863 if (path
->slots
[0] == 0)
868 /* pull out the item */
869 leaf
= path
->nodes
[0];
870 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
871 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
873 /* make sure the item matches what we want */
874 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
876 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
879 /* release the path since we're done with it */
880 btrfs_release_path(root
, path
);
883 * this is where we are basically btrfs_lookup, without the
884 * crossing root thing. we store the inode number in the
885 * offset of the orphan item.
887 inode
= btrfs_iget_locked(root
->inode
->i_sb
,
888 found_key
.offset
, root
);
892 if (inode
->i_state
& I_NEW
) {
893 BTRFS_I(inode
)->root
= root
;
895 /* have to set the location manually */
896 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
897 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
898 BTRFS_I(inode
)->location
.offset
= 0;
900 btrfs_read_locked_inode(inode
);
901 unlock_new_inode(inode
);
905 * add this inode to the orphan list so btrfs_orphan_del does
906 * the proper thing when we hit it
908 spin_lock(&root
->list_lock
);
909 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
910 spin_unlock(&root
->list_lock
);
913 * if this is a bad inode, means we actually succeeded in
914 * removing the inode, but not the orphan record, which means
915 * we need to manually delete the orphan since iput will just
918 if (is_bad_inode(inode
)) {
919 btrfs_orphan_del(trans
, inode
);
924 /* if we have links, this was a truncate, lets do that */
925 if (inode
->i_nlink
) {
927 btrfs_truncate(inode
);
932 /* this will do delete_inode and everything for us */
937 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
939 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
941 btrfs_free_path(path
);
942 btrfs_end_transaction(trans
, root
);
945 void btrfs_read_locked_inode(struct inode
*inode
)
947 struct btrfs_path
*path
;
948 struct extent_buffer
*leaf
;
949 struct btrfs_inode_item
*inode_item
;
950 struct btrfs_timespec
*tspec
;
951 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
952 struct btrfs_key location
;
953 u64 alloc_group_block
;
957 path
= btrfs_alloc_path();
959 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
961 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
965 leaf
= path
->nodes
[0];
966 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
967 struct btrfs_inode_item
);
969 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
970 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
971 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
972 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
973 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
975 tspec
= btrfs_inode_atime(inode_item
);
976 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
977 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
979 tspec
= btrfs_inode_mtime(inode_item
);
980 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
981 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
983 tspec
= btrfs_inode_ctime(inode_item
);
984 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
985 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
987 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
988 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
990 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
992 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
994 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
995 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
997 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
998 if (!BTRFS_I(inode
)->block_group
) {
999 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
1001 BTRFS_BLOCK_GROUP_METADATA
, 0);
1003 btrfs_free_path(path
);
1006 switch (inode
->i_mode
& S_IFMT
) {
1008 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1009 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1010 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1011 inode
->i_fop
= &btrfs_file_operations
;
1012 inode
->i_op
= &btrfs_file_inode_operations
;
1015 inode
->i_fop
= &btrfs_dir_file_operations
;
1016 if (root
== root
->fs_info
->tree_root
)
1017 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
1019 inode
->i_op
= &btrfs_dir_inode_operations
;
1022 inode
->i_op
= &btrfs_symlink_inode_operations
;
1023 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
1024 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1027 init_special_inode(inode
, inode
->i_mode
, rdev
);
1033 btrfs_free_path(path
);
1034 make_bad_inode(inode
);
1037 static void fill_inode_item(struct extent_buffer
*leaf
,
1038 struct btrfs_inode_item
*item
,
1039 struct inode
*inode
)
1041 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
1042 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
1043 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
1044 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
1045 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
1047 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
1048 inode
->i_atime
.tv_sec
);
1049 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
1050 inode
->i_atime
.tv_nsec
);
1052 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
1053 inode
->i_mtime
.tv_sec
);
1054 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
1055 inode
->i_mtime
.tv_nsec
);
1057 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
1058 inode
->i_ctime
.tv_sec
);
1059 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
1060 inode
->i_ctime
.tv_nsec
);
1062 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
1063 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
1064 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
1065 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
1066 btrfs_set_inode_block_group(leaf
, item
,
1067 BTRFS_I(inode
)->block_group
->key
.objectid
);
1070 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
1071 struct btrfs_root
*root
,
1072 struct inode
*inode
)
1074 struct btrfs_inode_item
*inode_item
;
1075 struct btrfs_path
*path
;
1076 struct extent_buffer
*leaf
;
1079 path
= btrfs_alloc_path();
1081 ret
= btrfs_lookup_inode(trans
, root
, path
,
1082 &BTRFS_I(inode
)->location
, 1);
1089 leaf
= path
->nodes
[0];
1090 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1091 struct btrfs_inode_item
);
1093 fill_inode_item(leaf
, inode_item
, inode
);
1094 btrfs_mark_buffer_dirty(leaf
);
1095 btrfs_set_inode_last_trans(trans
, inode
);
1098 btrfs_free_path(path
);
1103 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
1104 struct btrfs_root
*root
,
1106 struct dentry
*dentry
)
1108 struct btrfs_path
*path
;
1109 const char *name
= dentry
->d_name
.name
;
1110 int name_len
= dentry
->d_name
.len
;
1112 struct extent_buffer
*leaf
;
1113 struct btrfs_dir_item
*di
;
1114 struct btrfs_key key
;
1117 path
= btrfs_alloc_path();
1123 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1124 name
, name_len
, -1);
1133 leaf
= path
->nodes
[0];
1134 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1135 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1138 btrfs_release_path(root
, path
);
1140 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1141 dentry
->d_inode
->i_ino
,
1142 dentry
->d_parent
->d_inode
->i_ino
, &index
);
1144 printk("failed to delete reference to %.*s, "
1145 "inode %lu parent %lu\n", name_len
, name
,
1146 dentry
->d_inode
->i_ino
,
1147 dentry
->d_parent
->d_inode
->i_ino
);
1151 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1152 index
, name
, name_len
, -1);
1161 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1162 btrfs_release_path(root
, path
);
1164 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
1166 btrfs_free_path(path
);
1168 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1169 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1170 btrfs_update_inode(trans
, root
, dir
);
1171 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1172 dentry
->d_inode
->i_nlink
--;
1174 drop_nlink(dentry
->d_inode
);
1176 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1177 dir
->i_sb
->s_dirt
= 1;
1182 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1184 struct btrfs_root
*root
;
1185 struct btrfs_trans_handle
*trans
;
1186 struct inode
*inode
= dentry
->d_inode
;
1188 unsigned long nr
= 0;
1190 root
= BTRFS_I(dir
)->root
;
1192 ret
= btrfs_check_free_space(root
, 1, 1);
1196 trans
= btrfs_start_transaction(root
, 1);
1198 btrfs_set_trans_block_group(trans
, dir
);
1199 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1201 if (inode
->i_nlink
== 0)
1202 ret
= btrfs_orphan_add(trans
, inode
);
1204 nr
= trans
->blocks_used
;
1206 btrfs_end_transaction_throttle(trans
, root
);
1208 btrfs_btree_balance_dirty(root
, nr
);
1212 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1214 struct inode
*inode
= dentry
->d_inode
;
1217 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1218 struct btrfs_trans_handle
*trans
;
1219 unsigned long nr
= 0;
1221 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1225 ret
= btrfs_check_free_space(root
, 1, 1);
1229 trans
= btrfs_start_transaction(root
, 1);
1230 btrfs_set_trans_block_group(trans
, dir
);
1232 err
= btrfs_orphan_add(trans
, inode
);
1236 /* now the directory is empty */
1237 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1239 btrfs_i_size_write(inode
, 0);
1243 nr
= trans
->blocks_used
;
1244 ret
= btrfs_end_transaction_throttle(trans
, root
);
1246 btrfs_btree_balance_dirty(root
, nr
);
1254 * this can truncate away extent items, csum items and directory items.
1255 * It starts at a high offset and removes keys until it can't find
1256 * any higher than i_size.
1258 * csum items that cross the new i_size are truncated to the new size
1261 * min_type is the minimum key type to truncate down to. If set to 0, this
1262 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1264 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1265 struct btrfs_root
*root
,
1266 struct inode
*inode
,
1270 struct btrfs_path
*path
;
1271 struct btrfs_key key
;
1272 struct btrfs_key found_key
;
1274 struct extent_buffer
*leaf
;
1275 struct btrfs_file_extent_item
*fi
;
1276 u64 extent_start
= 0;
1277 u64 extent_num_bytes
= 0;
1283 int pending_del_nr
= 0;
1284 int pending_del_slot
= 0;
1285 int extent_type
= -1;
1286 u64 mask
= root
->sectorsize
- 1;
1288 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1289 path
= btrfs_alloc_path();
1293 /* FIXME, add redo link to tree so we don't leak on crash */
1294 key
.objectid
= inode
->i_ino
;
1295 key
.offset
= (u64
)-1;
1298 btrfs_init_path(path
);
1300 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1305 BUG_ON(path
->slots
[0] == 0);
1311 leaf
= path
->nodes
[0];
1312 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1313 found_type
= btrfs_key_type(&found_key
);
1315 if (found_key
.objectid
!= inode
->i_ino
)
1318 if (found_type
< min_type
)
1321 item_end
= found_key
.offset
;
1322 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1323 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1324 struct btrfs_file_extent_item
);
1325 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1326 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1328 btrfs_file_extent_num_bytes(leaf
, fi
);
1329 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1330 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1332 item_end
+= btrfs_file_extent_inline_len(leaf
,
1337 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1338 ret
= btrfs_csum_truncate(trans
, root
, path
,
1342 if (item_end
< inode
->i_size
) {
1343 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1344 found_type
= BTRFS_INODE_ITEM_KEY
;
1345 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1346 found_type
= BTRFS_CSUM_ITEM_KEY
;
1347 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1348 found_type
= BTRFS_XATTR_ITEM_KEY
;
1349 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1350 found_type
= BTRFS_INODE_REF_KEY
;
1351 } else if (found_type
) {
1356 btrfs_set_key_type(&key
, found_type
);
1359 if (found_key
.offset
>= inode
->i_size
)
1365 /* FIXME, shrink the extent if the ref count is only 1 */
1366 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1369 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1371 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1373 u64 orig_num_bytes
=
1374 btrfs_file_extent_num_bytes(leaf
, fi
);
1375 extent_num_bytes
= inode
->i_size
-
1376 found_key
.offset
+ root
->sectorsize
- 1;
1377 extent_num_bytes
= extent_num_bytes
&
1378 ~((u64
)root
->sectorsize
- 1);
1379 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1381 num_dec
= (orig_num_bytes
-
1383 if (extent_start
!= 0)
1384 dec_i_blocks(inode
, num_dec
);
1385 btrfs_mark_buffer_dirty(leaf
);
1388 btrfs_file_extent_disk_num_bytes(leaf
,
1390 /* FIXME blocksize != 4096 */
1391 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1392 if (extent_start
!= 0) {
1394 dec_i_blocks(inode
, num_dec
);
1396 root_gen
= btrfs_header_generation(leaf
);
1397 root_owner
= btrfs_header_owner(leaf
);
1399 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1401 u32 newsize
= inode
->i_size
- found_key
.offset
;
1402 dec_i_blocks(inode
, item_end
+ 1 -
1403 found_key
.offset
- newsize
);
1405 btrfs_file_extent_calc_inline_size(newsize
);
1406 ret
= btrfs_truncate_item(trans
, root
, path
,
1410 dec_i_blocks(inode
, item_end
+ 1 -
1416 if (!pending_del_nr
) {
1417 /* no pending yet, add ourselves */
1418 pending_del_slot
= path
->slots
[0];
1420 } else if (pending_del_nr
&&
1421 path
->slots
[0] + 1 == pending_del_slot
) {
1422 /* hop on the pending chunk */
1424 pending_del_slot
= path
->slots
[0];
1426 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1432 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1435 root_gen
, inode
->i_ino
,
1436 found_key
.offset
, 0);
1440 if (path
->slots
[0] == 0) {
1443 btrfs_release_path(root
, path
);
1448 if (pending_del_nr
&&
1449 path
->slots
[0] + 1 != pending_del_slot
) {
1450 struct btrfs_key debug
;
1452 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1454 ret
= btrfs_del_items(trans
, root
, path
,
1459 btrfs_release_path(root
, path
);
1465 if (pending_del_nr
) {
1466 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1469 btrfs_free_path(path
);
1470 inode
->i_sb
->s_dirt
= 1;
1475 * taken from block_truncate_page, but does cow as it zeros out
1476 * any bytes left in the last page in the file.
1478 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1480 struct inode
*inode
= mapping
->host
;
1481 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1482 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1483 struct btrfs_ordered_extent
*ordered
;
1485 u32 blocksize
= root
->sectorsize
;
1486 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1487 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1493 if ((offset
& (blocksize
- 1)) == 0)
1498 page
= grab_cache_page(mapping
, index
);
1502 page_start
= page_offset(page
);
1503 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1505 if (!PageUptodate(page
)) {
1506 ret
= btrfs_readpage(NULL
, page
);
1508 if (page
->mapping
!= mapping
) {
1510 page_cache_release(page
);
1513 if (!PageUptodate(page
)) {
1518 wait_on_page_writeback(page
);
1520 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1521 set_page_extent_mapped(page
);
1523 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1525 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1527 page_cache_release(page
);
1528 btrfs_start_ordered_extent(inode
, ordered
, 1);
1529 btrfs_put_ordered_extent(ordered
);
1533 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1534 page_end
, GFP_NOFS
);
1536 if (offset
!= PAGE_CACHE_SIZE
) {
1538 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1539 flush_dcache_page(page
);
1542 ClearPageChecked(page
);
1543 set_page_dirty(page
);
1544 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1548 page_cache_release(page
);
1553 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1555 struct inode
*inode
= dentry
->d_inode
;
1558 err
= inode_change_ok(inode
, attr
);
1562 if (S_ISREG(inode
->i_mode
) &&
1563 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1564 struct btrfs_trans_handle
*trans
;
1565 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1566 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1568 u64 mask
= root
->sectorsize
- 1;
1569 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1570 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1574 if (attr
->ia_size
<= hole_start
)
1577 err
= btrfs_check_free_space(root
, 1, 0);
1581 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1583 hole_size
= block_end
- hole_start
;
1584 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1585 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1587 trans
= btrfs_start_transaction(root
, 1);
1588 btrfs_set_trans_block_group(trans
, inode
);
1589 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1590 err
= btrfs_drop_extents(trans
, root
, inode
,
1591 hole_start
, block_end
, hole_start
,
1594 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1595 err
= btrfs_insert_file_extent(trans
, root
,
1599 btrfs_drop_extent_cache(inode
, hole_start
,
1601 btrfs_check_file(root
, inode
);
1603 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1604 btrfs_end_transaction(trans
, root
);
1605 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1610 err
= inode_setattr(inode
, attr
);
1612 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
1613 err
= btrfs_acl_chmod(inode
);
1618 void btrfs_delete_inode(struct inode
*inode
)
1620 struct btrfs_trans_handle
*trans
;
1621 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1625 truncate_inode_pages(&inode
->i_data
, 0);
1626 if (is_bad_inode(inode
)) {
1627 btrfs_orphan_del(NULL
, inode
);
1630 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1632 btrfs_i_size_write(inode
, 0);
1633 trans
= btrfs_start_transaction(root
, 1);
1635 btrfs_set_trans_block_group(trans
, inode
);
1636 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1638 btrfs_orphan_del(NULL
, inode
);
1639 goto no_delete_lock
;
1642 btrfs_orphan_del(trans
, inode
);
1644 nr
= trans
->blocks_used
;
1647 btrfs_end_transaction(trans
, root
);
1648 btrfs_btree_balance_dirty(root
, nr
);
1652 nr
= trans
->blocks_used
;
1653 btrfs_end_transaction(trans
, root
);
1654 btrfs_btree_balance_dirty(root
, nr
);
1660 * this returns the key found in the dir entry in the location pointer.
1661 * If no dir entries were found, location->objectid is 0.
1663 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1664 struct btrfs_key
*location
)
1666 const char *name
= dentry
->d_name
.name
;
1667 int namelen
= dentry
->d_name
.len
;
1668 struct btrfs_dir_item
*di
;
1669 struct btrfs_path
*path
;
1670 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1673 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1674 location
->objectid
= dir
->i_ino
;
1675 location
->type
= BTRFS_INODE_ITEM_KEY
;
1676 location
->offset
= 0;
1679 path
= btrfs_alloc_path();
1682 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1683 struct btrfs_key key
;
1684 struct extent_buffer
*leaf
;
1687 key
.objectid
= dir
->i_ino
;
1688 key
.offset
= (u64
)-1;
1689 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1690 if (ret
< 0 || path
->slots
[0] == 0)
1692 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1695 leaf
= path
->nodes
[0];
1696 slot
= path
->slots
[0] - 1;
1698 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1699 if (key
.objectid
!= dir
->i_ino
||
1700 key
.type
!= BTRFS_INODE_REF_KEY
) {
1703 location
->objectid
= key
.offset
;
1704 location
->type
= BTRFS_INODE_ITEM_KEY
;
1705 location
->offset
= 0;
1709 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1713 if (!di
|| IS_ERR(di
)) {
1716 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1718 btrfs_free_path(path
);
1721 location
->objectid
= 0;
1726 * when we hit a tree root in a directory, the btrfs part of the inode
1727 * needs to be changed to reflect the root directory of the tree root. This
1728 * is kind of like crossing a mount point.
1730 static int fixup_tree_root_location(struct btrfs_root
*root
,
1731 struct btrfs_key
*location
,
1732 struct btrfs_root
**sub_root
,
1733 struct dentry
*dentry
)
1735 struct btrfs_root_item
*ri
;
1737 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1739 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1742 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1743 dentry
->d_name
.name
,
1744 dentry
->d_name
.len
);
1745 if (IS_ERR(*sub_root
))
1746 return PTR_ERR(*sub_root
);
1748 ri
= &(*sub_root
)->root_item
;
1749 location
->objectid
= btrfs_root_dirid(ri
);
1750 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1751 location
->offset
= 0;
1756 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1758 struct btrfs_iget_args
*args
= p
;
1759 inode
->i_ino
= args
->ino
;
1760 BTRFS_I(inode
)->root
= args
->root
;
1761 BTRFS_I(inode
)->delalloc_bytes
= 0;
1762 BTRFS_I(inode
)->disk_i_size
= 0;
1763 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1764 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1765 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1766 inode
->i_mapping
, GFP_NOFS
);
1767 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1768 inode
->i_mapping
, GFP_NOFS
);
1769 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1770 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1771 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1775 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1777 struct btrfs_iget_args
*args
= opaque
;
1778 return (args
->ino
== inode
->i_ino
&&
1779 args
->root
== BTRFS_I(inode
)->root
);
1782 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1785 struct btrfs_iget_args args
;
1786 args
.ino
= objectid
;
1787 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1792 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1795 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1796 struct btrfs_root
*root
)
1798 struct inode
*inode
;
1799 struct btrfs_iget_args args
;
1800 args
.ino
= objectid
;
1803 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1804 btrfs_init_locked_inode
,
1809 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1810 struct nameidata
*nd
)
1812 struct inode
* inode
;
1813 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1814 struct btrfs_root
*root
= bi
->root
;
1815 struct btrfs_root
*sub_root
= root
;
1816 struct btrfs_key location
;
1817 int ret
, do_orphan
= 0;
1819 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1820 return ERR_PTR(-ENAMETOOLONG
);
1822 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1825 return ERR_PTR(ret
);
1828 if (location
.objectid
) {
1829 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1832 return ERR_PTR(ret
);
1834 return ERR_PTR(-ENOENT
);
1836 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1839 return ERR_PTR(-EACCES
);
1840 if (inode
->i_state
& I_NEW
) {
1841 /* the inode and parent dir are two different roots */
1842 if (sub_root
!= root
) {
1844 sub_root
->inode
= inode
;
1847 BTRFS_I(inode
)->root
= sub_root
;
1848 memcpy(&BTRFS_I(inode
)->location
, &location
,
1850 btrfs_read_locked_inode(inode
);
1851 unlock_new_inode(inode
);
1855 if (unlikely(do_orphan
))
1856 btrfs_orphan_cleanup(sub_root
);
1858 return d_splice_alias(inode
, dentry
);
1861 static unsigned char btrfs_filetype_table
[] = {
1862 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1865 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1867 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1868 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1869 struct btrfs_item
*item
;
1870 struct btrfs_dir_item
*di
;
1871 struct btrfs_key key
;
1872 struct btrfs_key found_key
;
1873 struct btrfs_path
*path
;
1876 struct extent_buffer
*leaf
;
1879 unsigned char d_type
;
1884 int key_type
= BTRFS_DIR_INDEX_KEY
;
1889 /* FIXME, use a real flag for deciding about the key type */
1890 if (root
->fs_info
->tree_root
== root
)
1891 key_type
= BTRFS_DIR_ITEM_KEY
;
1893 /* special case for "." */
1894 if (filp
->f_pos
== 0) {
1895 over
= filldir(dirent
, ".", 1,
1903 key
.objectid
= inode
->i_ino
;
1904 path
= btrfs_alloc_path();
1907 /* special case for .., just use the back ref */
1908 if (filp
->f_pos
== 1) {
1909 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1910 key
.offset
= (u64
)-1;
1911 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1912 if (ret
< 0 || path
->slots
[0] == 0) {
1913 btrfs_release_path(root
, path
);
1914 goto read_dir_items
;
1917 leaf
= path
->nodes
[0];
1918 slot
= path
->slots
[0] - 1;
1919 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1920 btrfs_release_path(root
, path
);
1921 if (found_key
.objectid
!= key
.objectid
||
1922 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1923 goto read_dir_items
;
1924 over
= filldir(dirent
, "..", 2,
1925 2, found_key
.offset
, DT_DIR
);
1932 btrfs_set_key_type(&key
, key_type
);
1933 key
.offset
= filp
->f_pos
;
1935 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1940 leaf
= path
->nodes
[0];
1941 nritems
= btrfs_header_nritems(leaf
);
1942 slot
= path
->slots
[0];
1943 if (advance
|| slot
>= nritems
) {
1944 if (slot
>= nritems
-1) {
1945 ret
= btrfs_next_leaf(root
, path
);
1948 leaf
= path
->nodes
[0];
1949 nritems
= btrfs_header_nritems(leaf
);
1950 slot
= path
->slots
[0];
1957 item
= btrfs_item_nr(leaf
, slot
);
1958 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1960 if (found_key
.objectid
!= key
.objectid
)
1962 if (btrfs_key_type(&found_key
) != key_type
)
1964 if (found_key
.offset
< filp
->f_pos
)
1967 filp
->f_pos
= found_key
.offset
;
1969 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1971 di_total
= btrfs_item_size(leaf
, item
);
1972 while(di_cur
< di_total
) {
1973 struct btrfs_key location
;
1975 name_len
= btrfs_dir_name_len(leaf
, di
);
1976 if (name_len
< 32) {
1977 name_ptr
= tmp_name
;
1979 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1982 read_extent_buffer(leaf
, name_ptr
,
1983 (unsigned long)(di
+ 1), name_len
);
1985 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1986 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1987 over
= filldir(dirent
, name_ptr
, name_len
,
1992 if (name_ptr
!= tmp_name
)
1997 di_len
= btrfs_dir_name_len(leaf
, di
) +
1998 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
2000 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
2003 if (key_type
== BTRFS_DIR_INDEX_KEY
)
2004 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
2010 btrfs_free_path(path
);
2014 int btrfs_write_inode(struct inode
*inode
, int wait
)
2016 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2017 struct btrfs_trans_handle
*trans
;
2021 trans
= btrfs_join_transaction(root
, 1);
2022 btrfs_set_trans_block_group(trans
, inode
);
2023 ret
= btrfs_commit_transaction(trans
, root
);
2029 * This is somewhat expensive, updating the tree every time the
2030 * inode changes. But, it is most likely to find the inode in cache.
2031 * FIXME, needs more benchmarking...there are no reasons other than performance
2032 * to keep or drop this code.
2034 void btrfs_dirty_inode(struct inode
*inode
)
2036 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2037 struct btrfs_trans_handle
*trans
;
2039 trans
= btrfs_join_transaction(root
, 1);
2040 btrfs_set_trans_block_group(trans
, inode
);
2041 btrfs_update_inode(trans
, root
, inode
);
2042 btrfs_end_transaction(trans
, root
);
2045 static int btrfs_set_inode_index_count(struct inode
*inode
)
2047 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2048 struct btrfs_key key
, found_key
;
2049 struct btrfs_path
*path
;
2050 struct extent_buffer
*leaf
;
2053 key
.objectid
= inode
->i_ino
;
2054 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
2055 key
.offset
= (u64
)-1;
2057 path
= btrfs_alloc_path();
2061 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2064 /* FIXME: we should be able to handle this */
2070 * MAGIC NUMBER EXPLANATION:
2071 * since we search a directory based on f_pos we have to start at 2
2072 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2073 * else has to start at 2
2075 if (path
->slots
[0] == 0) {
2076 BTRFS_I(inode
)->index_cnt
= 2;
2082 leaf
= path
->nodes
[0];
2083 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2085 if (found_key
.objectid
!= inode
->i_ino
||
2086 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
2087 BTRFS_I(inode
)->index_cnt
= 2;
2091 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
2093 btrfs_free_path(path
);
2097 static int btrfs_set_inode_index(struct inode
*dir
, struct inode
*inode
)
2101 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
2102 ret
= btrfs_set_inode_index_count(dir
);
2107 BTRFS_I(inode
)->index
= BTRFS_I(dir
)->index_cnt
;
2108 BTRFS_I(dir
)->index_cnt
++;
2113 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
2114 struct btrfs_root
*root
,
2116 const char *name
, int name_len
,
2119 struct btrfs_block_group_cache
*group
,
2122 struct inode
*inode
;
2123 struct btrfs_inode_item
*inode_item
;
2124 struct btrfs_block_group_cache
*new_inode_group
;
2125 struct btrfs_key
*location
;
2126 struct btrfs_path
*path
;
2127 struct btrfs_inode_ref
*ref
;
2128 struct btrfs_key key
[2];
2134 path
= btrfs_alloc_path();
2137 inode
= new_inode(root
->fs_info
->sb
);
2139 return ERR_PTR(-ENOMEM
);
2142 ret
= btrfs_set_inode_index(dir
, inode
);
2144 return ERR_PTR(ret
);
2146 BTRFS_I(inode
)->index
= 0;
2149 * index_cnt is ignored for everything but a dir,
2150 * btrfs_get_inode_index_count has an explanation for the magic
2153 BTRFS_I(inode
)->index_cnt
= 2;
2155 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2156 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2157 inode
->i_mapping
, GFP_NOFS
);
2158 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2159 inode
->i_mapping
, GFP_NOFS
);
2160 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2161 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2162 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2163 BTRFS_I(inode
)->delalloc_bytes
= 0;
2164 BTRFS_I(inode
)->disk_i_size
= 0;
2165 BTRFS_I(inode
)->root
= root
;
2171 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
2172 BTRFS_BLOCK_GROUP_METADATA
, owner
);
2173 if (!new_inode_group
) {
2174 printk("find_block group failed\n");
2175 new_inode_group
= group
;
2177 BTRFS_I(inode
)->block_group
= new_inode_group
;
2178 BTRFS_I(inode
)->flags
= 0;
2180 key
[0].objectid
= objectid
;
2181 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
2184 key
[1].objectid
= objectid
;
2185 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
2186 key
[1].offset
= ref_objectid
;
2188 sizes
[0] = sizeof(struct btrfs_inode_item
);
2189 sizes
[1] = name_len
+ sizeof(*ref
);
2191 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
2195 if (objectid
> root
->highest_inode
)
2196 root
->highest_inode
= objectid
;
2198 inode
->i_uid
= current
->fsuid
;
2199 inode
->i_gid
= current
->fsgid
;
2200 inode
->i_mode
= mode
;
2201 inode
->i_ino
= objectid
;
2202 inode
->i_blocks
= 0;
2203 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2204 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2205 struct btrfs_inode_item
);
2206 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
2208 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2209 struct btrfs_inode_ref
);
2210 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
2211 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, BTRFS_I(inode
)->index
);
2212 ptr
= (unsigned long)(ref
+ 1);
2213 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
2215 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2216 btrfs_free_path(path
);
2218 location
= &BTRFS_I(inode
)->location
;
2219 location
->objectid
= objectid
;
2220 location
->offset
= 0;
2221 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2223 insert_inode_hash(inode
);
2227 BTRFS_I(dir
)->index_cnt
--;
2228 btrfs_free_path(path
);
2229 return ERR_PTR(ret
);
2232 static inline u8
btrfs_inode_type(struct inode
*inode
)
2234 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2237 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2238 struct dentry
*dentry
, struct inode
*inode
,
2242 struct btrfs_key key
;
2243 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
2244 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
2246 key
.objectid
= inode
->i_ino
;
2247 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2250 ret
= btrfs_insert_dir_item(trans
, root
,
2251 dentry
->d_name
.name
, dentry
->d_name
.len
,
2252 dentry
->d_parent
->d_inode
->i_ino
,
2253 &key
, btrfs_inode_type(inode
),
2254 BTRFS_I(inode
)->index
);
2257 ret
= btrfs_insert_inode_ref(trans
, root
,
2258 dentry
->d_name
.name
,
2261 parent_inode
->i_ino
,
2262 BTRFS_I(inode
)->index
);
2264 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2265 dentry
->d_name
.len
* 2);
2266 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2267 ret
= btrfs_update_inode(trans
, root
,
2268 dentry
->d_parent
->d_inode
);
2273 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2274 struct dentry
*dentry
, struct inode
*inode
,
2277 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
2279 d_instantiate(dentry
, inode
);
2287 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2288 int mode
, dev_t rdev
)
2290 struct btrfs_trans_handle
*trans
;
2291 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2292 struct inode
*inode
= NULL
;
2296 unsigned long nr
= 0;
2298 if (!new_valid_dev(rdev
))
2301 err
= btrfs_check_free_space(root
, 1, 0);
2305 trans
= btrfs_start_transaction(root
, 1);
2306 btrfs_set_trans_block_group(trans
, dir
);
2308 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2314 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2316 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2317 BTRFS_I(dir
)->block_group
, mode
);
2318 err
= PTR_ERR(inode
);
2322 err
= btrfs_init_acl(inode
, dir
);
2328 btrfs_set_trans_block_group(trans
, inode
);
2329 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2333 inode
->i_op
= &btrfs_special_inode_operations
;
2334 init_special_inode(inode
, inode
->i_mode
, rdev
);
2335 btrfs_update_inode(trans
, root
, inode
);
2337 dir
->i_sb
->s_dirt
= 1;
2338 btrfs_update_inode_block_group(trans
, inode
);
2339 btrfs_update_inode_block_group(trans
, dir
);
2341 nr
= trans
->blocks_used
;
2342 btrfs_end_transaction_throttle(trans
, root
);
2345 inode_dec_link_count(inode
);
2348 btrfs_btree_balance_dirty(root
, nr
);
2352 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2353 int mode
, struct nameidata
*nd
)
2355 struct btrfs_trans_handle
*trans
;
2356 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2357 struct inode
*inode
= NULL
;
2360 unsigned long nr
= 0;
2363 err
= btrfs_check_free_space(root
, 1, 0);
2366 trans
= btrfs_start_transaction(root
, 1);
2367 btrfs_set_trans_block_group(trans
, dir
);
2369 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2375 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2377 dentry
->d_parent
->d_inode
->i_ino
,
2378 objectid
, BTRFS_I(dir
)->block_group
, mode
);
2379 err
= PTR_ERR(inode
);
2383 err
= btrfs_init_acl(inode
, dir
);
2389 btrfs_set_trans_block_group(trans
, inode
);
2390 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2394 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2395 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2396 inode
->i_fop
= &btrfs_file_operations
;
2397 inode
->i_op
= &btrfs_file_inode_operations
;
2398 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2399 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2400 inode
->i_mapping
, GFP_NOFS
);
2401 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2402 inode
->i_mapping
, GFP_NOFS
);
2403 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2404 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2405 BTRFS_I(inode
)->delalloc_bytes
= 0;
2406 BTRFS_I(inode
)->disk_i_size
= 0;
2407 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2408 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2410 dir
->i_sb
->s_dirt
= 1;
2411 btrfs_update_inode_block_group(trans
, inode
);
2412 btrfs_update_inode_block_group(trans
, dir
);
2414 nr
= trans
->blocks_used
;
2415 btrfs_end_transaction_throttle(trans
, root
);
2418 inode_dec_link_count(inode
);
2421 btrfs_btree_balance_dirty(root
, nr
);
2425 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2426 struct dentry
*dentry
)
2428 struct btrfs_trans_handle
*trans
;
2429 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2430 struct inode
*inode
= old_dentry
->d_inode
;
2431 unsigned long nr
= 0;
2435 if (inode
->i_nlink
== 0)
2438 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2443 err
= btrfs_check_free_space(root
, 1, 0);
2446 err
= btrfs_set_inode_index(dir
, inode
);
2450 trans
= btrfs_start_transaction(root
, 1);
2452 btrfs_set_trans_block_group(trans
, dir
);
2453 atomic_inc(&inode
->i_count
);
2455 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2460 dir
->i_sb
->s_dirt
= 1;
2461 btrfs_update_inode_block_group(trans
, dir
);
2462 err
= btrfs_update_inode(trans
, root
, inode
);
2467 nr
= trans
->blocks_used
;
2468 btrfs_end_transaction_throttle(trans
, root
);
2471 inode_dec_link_count(inode
);
2474 btrfs_btree_balance_dirty(root
, nr
);
2478 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2480 struct inode
*inode
= NULL
;
2481 struct btrfs_trans_handle
*trans
;
2482 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2484 int drop_on_err
= 0;
2486 unsigned long nr
= 1;
2488 err
= btrfs_check_free_space(root
, 1, 0);
2492 trans
= btrfs_start_transaction(root
, 1);
2493 btrfs_set_trans_block_group(trans
, dir
);
2495 if (IS_ERR(trans
)) {
2496 err
= PTR_ERR(trans
);
2500 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2506 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2508 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2509 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2510 if (IS_ERR(inode
)) {
2511 err
= PTR_ERR(inode
);
2517 err
= btrfs_init_acl(inode
, dir
);
2521 inode
->i_op
= &btrfs_dir_inode_operations
;
2522 inode
->i_fop
= &btrfs_dir_file_operations
;
2523 btrfs_set_trans_block_group(trans
, inode
);
2525 btrfs_i_size_write(inode
, 0);
2526 err
= btrfs_update_inode(trans
, root
, inode
);
2530 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2534 d_instantiate(dentry
, inode
);
2536 dir
->i_sb
->s_dirt
= 1;
2537 btrfs_update_inode_block_group(trans
, inode
);
2538 btrfs_update_inode_block_group(trans
, dir
);
2541 nr
= trans
->blocks_used
;
2542 btrfs_end_transaction_throttle(trans
, root
);
2547 btrfs_btree_balance_dirty(root
, nr
);
2551 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2552 struct extent_map
*existing
,
2553 struct extent_map
*em
,
2554 u64 map_start
, u64 map_len
)
2558 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2559 start_diff
= map_start
- em
->start
;
2560 em
->start
= map_start
;
2562 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2563 em
->block_start
+= start_diff
;
2564 return add_extent_mapping(em_tree
, em
);
2567 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2568 size_t pg_offset
, u64 start
, u64 len
,
2574 u64 extent_start
= 0;
2576 u64 objectid
= inode
->i_ino
;
2578 struct btrfs_path
*path
= NULL
;
2579 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2580 struct btrfs_file_extent_item
*item
;
2581 struct extent_buffer
*leaf
;
2582 struct btrfs_key found_key
;
2583 struct extent_map
*em
= NULL
;
2584 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2585 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2586 struct btrfs_trans_handle
*trans
= NULL
;
2589 spin_lock(&em_tree
->lock
);
2590 em
= lookup_extent_mapping(em_tree
, start
, len
);
2592 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2593 spin_unlock(&em_tree
->lock
);
2596 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2597 free_extent_map(em
);
2598 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2599 free_extent_map(em
);
2603 em
= alloc_extent_map(GFP_NOFS
);
2608 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2609 em
->start
= EXTENT_MAP_HOLE
;
2613 path
= btrfs_alloc_path();
2617 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2618 objectid
, start
, trans
!= NULL
);
2625 if (path
->slots
[0] == 0)
2630 leaf
= path
->nodes
[0];
2631 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2632 struct btrfs_file_extent_item
);
2633 /* are we inside the extent that was found? */
2634 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2635 found_type
= btrfs_key_type(&found_key
);
2636 if (found_key
.objectid
!= objectid
||
2637 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2641 found_type
= btrfs_file_extent_type(leaf
, item
);
2642 extent_start
= found_key
.offset
;
2643 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2644 extent_end
= extent_start
+
2645 btrfs_file_extent_num_bytes(leaf
, item
);
2647 if (start
< extent_start
|| start
>= extent_end
) {
2649 if (start
< extent_start
) {
2650 if (start
+ len
<= extent_start
)
2652 em
->len
= extent_end
- extent_start
;
2658 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2660 em
->start
= extent_start
;
2661 em
->len
= extent_end
- extent_start
;
2662 em
->block_start
= EXTENT_MAP_HOLE
;
2665 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2666 em
->block_start
= bytenr
;
2667 em
->start
= extent_start
;
2668 em
->len
= extent_end
- extent_start
;
2670 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2675 size_t extent_offset
;
2678 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2680 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2681 ~((u64
)root
->sectorsize
- 1);
2682 if (start
< extent_start
|| start
>= extent_end
) {
2684 if (start
< extent_start
) {
2685 if (start
+ len
<= extent_start
)
2687 em
->len
= extent_end
- extent_start
;
2693 em
->block_start
= EXTENT_MAP_INLINE
;
2696 em
->start
= extent_start
;
2701 page_start
= page_offset(page
) + pg_offset
;
2702 extent_offset
= page_start
- extent_start
;
2703 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2704 size
- extent_offset
);
2705 em
->start
= extent_start
+ extent_offset
;
2706 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2707 ~((u64
)root
->sectorsize
- 1);
2709 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2710 if (create
== 0 && !PageUptodate(page
)) {
2711 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2713 flush_dcache_page(page
);
2714 } else if (create
&& PageUptodate(page
)) {
2717 free_extent_map(em
);
2719 btrfs_release_path(root
, path
);
2720 trans
= btrfs_join_transaction(root
, 1);
2723 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2725 btrfs_mark_buffer_dirty(leaf
);
2728 set_extent_uptodate(io_tree
, em
->start
,
2729 extent_map_end(em
) - 1, GFP_NOFS
);
2732 printk("unkknown found_type %d\n", found_type
);
2739 em
->block_start
= EXTENT_MAP_HOLE
;
2741 btrfs_release_path(root
, path
);
2742 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2743 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2749 spin_lock(&em_tree
->lock
);
2750 ret
= add_extent_mapping(em_tree
, em
);
2751 /* it is possible that someone inserted the extent into the tree
2752 * while we had the lock dropped. It is also possible that
2753 * an overlapping map exists in the tree
2755 if (ret
== -EEXIST
) {
2756 struct extent_map
*existing
;
2760 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2761 if (existing
&& (existing
->start
> start
||
2762 existing
->start
+ existing
->len
<= start
)) {
2763 free_extent_map(existing
);
2767 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2770 err
= merge_extent_mapping(em_tree
, existing
,
2773 free_extent_map(existing
);
2775 free_extent_map(em
);
2780 printk("failing to insert %Lu %Lu\n",
2782 free_extent_map(em
);
2786 free_extent_map(em
);
2791 spin_unlock(&em_tree
->lock
);
2794 btrfs_free_path(path
);
2796 ret
= btrfs_end_transaction(trans
, root
);
2802 free_extent_map(em
);
2804 return ERR_PTR(err
);
2809 #if 0 /* waiting for O_DIRECT reads */
2810 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2811 struct buffer_head
*bh_result
, int create
)
2813 struct extent_map
*em
;
2814 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2815 struct btrfs_multi_bio
*multi
= NULL
;
2816 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2822 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2824 if (!em
|| IS_ERR(em
))
2827 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2831 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2836 len
= em
->start
+ em
->len
- start
;
2837 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2839 if (em
->block_start
== EXTENT_MAP_HOLE
||
2840 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2841 bh_result
->b_size
= len
;
2845 logical
= start
- em
->start
;
2846 logical
= em
->block_start
+ logical
;
2849 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2850 logical
, &map_length
, &multi
, 0);
2852 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2853 bh_result
->b_size
= min(map_length
, len
);
2855 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2856 set_buffer_mapped(bh_result
);
2859 free_extent_map(em
);
2864 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2865 const struct iovec
*iov
, loff_t offset
,
2866 unsigned long nr_segs
)
2870 struct file
*file
= iocb
->ki_filp
;
2871 struct inode
*inode
= file
->f_mapping
->host
;
2876 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2877 offset
, nr_segs
, btrfs_get_block
, NULL
);
2881 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2883 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2886 int btrfs_readpage(struct file
*file
, struct page
*page
)
2888 struct extent_io_tree
*tree
;
2889 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2890 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2893 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2895 struct extent_io_tree
*tree
;
2898 if (current
->flags
& PF_MEMALLOC
) {
2899 redirty_page_for_writepage(wbc
, page
);
2903 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2904 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2907 int btrfs_writepages(struct address_space
*mapping
,
2908 struct writeback_control
*wbc
)
2910 struct extent_io_tree
*tree
;
2911 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2912 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2916 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2917 struct list_head
*pages
, unsigned nr_pages
)
2919 struct extent_io_tree
*tree
;
2920 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2921 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2924 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2926 struct extent_io_tree
*tree
;
2927 struct extent_map_tree
*map
;
2930 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2931 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2932 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2934 ClearPagePrivate(page
);
2935 set_page_private(page
, 0);
2936 page_cache_release(page
);
2941 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2943 return __btrfs_releasepage(page
, gfp_flags
);
2946 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2948 struct extent_io_tree
*tree
;
2949 struct btrfs_ordered_extent
*ordered
;
2950 u64 page_start
= page_offset(page
);
2951 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2953 wait_on_page_writeback(page
);
2954 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2956 btrfs_releasepage(page
, GFP_NOFS
);
2960 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2961 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2965 * IO on this page will never be started, so we need
2966 * to account for any ordered extents now
2968 clear_extent_bit(tree
, page_start
, page_end
,
2969 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2970 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2971 btrfs_finish_ordered_io(page
->mapping
->host
,
2972 page_start
, page_end
);
2973 btrfs_put_ordered_extent(ordered
);
2974 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2976 clear_extent_bit(tree
, page_start
, page_end
,
2977 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2980 __btrfs_releasepage(page
, GFP_NOFS
);
2982 ClearPageChecked(page
);
2983 if (PagePrivate(page
)) {
2984 ClearPagePrivate(page
);
2985 set_page_private(page
, 0);
2986 page_cache_release(page
);
2991 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2992 * called from a page fault handler when a page is first dirtied. Hence we must
2993 * be careful to check for EOF conditions here. We set the page up correctly
2994 * for a written page which means we get ENOSPC checking when writing into
2995 * holes and correct delalloc and unwritten extent mapping on filesystems that
2996 * support these features.
2998 * We are not allowed to take the i_mutex here so we have to play games to
2999 * protect against truncate races as the page could now be beyond EOF. Because
3000 * vmtruncate() writes the inode size before removing pages, once we have the
3001 * page lock we can determine safely if the page is beyond EOF. If it is not
3002 * beyond EOF, then the page is guaranteed safe against truncation until we
3005 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
3007 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
3008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3009 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3010 struct btrfs_ordered_extent
*ordered
;
3012 unsigned long zero_start
;
3018 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
3025 size
= i_size_read(inode
);
3026 page_start
= page_offset(page
);
3027 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3029 if ((page
->mapping
!= inode
->i_mapping
) ||
3030 (page_start
>= size
)) {
3031 /* page got truncated out from underneath us */
3034 wait_on_page_writeback(page
);
3036 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3037 set_page_extent_mapped(page
);
3040 * we can't set the delalloc bits if there are pending ordered
3041 * extents. Drop our locks and wait for them to finish
3043 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3045 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3047 btrfs_start_ordered_extent(inode
, ordered
, 1);
3048 btrfs_put_ordered_extent(ordered
);
3052 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
3053 page_end
, GFP_NOFS
);
3056 /* page is wholly or partially inside EOF */
3057 if (page_start
+ PAGE_CACHE_SIZE
> size
)
3058 zero_start
= size
& ~PAGE_CACHE_MASK
;
3060 zero_start
= PAGE_CACHE_SIZE
;
3062 if (zero_start
!= PAGE_CACHE_SIZE
) {
3064 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
3065 flush_dcache_page(page
);
3068 ClearPageChecked(page
);
3069 set_page_dirty(page
);
3070 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3078 static void btrfs_truncate(struct inode
*inode
)
3080 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3082 struct btrfs_trans_handle
*trans
;
3084 u64 mask
= root
->sectorsize
- 1;
3086 if (!S_ISREG(inode
->i_mode
))
3088 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3091 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3092 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
3094 trans
= btrfs_start_transaction(root
, 1);
3095 btrfs_set_trans_block_group(trans
, inode
);
3096 btrfs_i_size_write(inode
, inode
->i_size
);
3098 ret
= btrfs_orphan_add(trans
, inode
);
3101 /* FIXME, add redo link to tree so we don't leak on crash */
3102 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
3103 BTRFS_EXTENT_DATA_KEY
);
3104 btrfs_update_inode(trans
, root
, inode
);
3106 ret
= btrfs_orphan_del(trans
, inode
);
3110 nr
= trans
->blocks_used
;
3111 ret
= btrfs_end_transaction_throttle(trans
, root
);
3113 btrfs_btree_balance_dirty(root
, nr
);
3117 * Invalidate a single dcache entry at the root of the filesystem.
3118 * Needed after creation of snapshot or subvolume.
3120 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
3123 struct dentry
*alias
, *entry
;
3126 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
3130 /* change me if btrfs ever gets a d_hash operation */
3131 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
3132 entry
= d_lookup(alias
, &qstr
);
3135 d_invalidate(entry
);
3141 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
3142 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
3143 struct btrfs_block_group_cache
*block_group
)
3145 struct inode
*inode
;
3147 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
3148 new_dirid
, block_group
, S_IFDIR
| 0700);
3150 return PTR_ERR(inode
);
3151 inode
->i_op
= &btrfs_dir_inode_operations
;
3152 inode
->i_fop
= &btrfs_dir_file_operations
;
3153 new_root
->inode
= inode
;
3156 btrfs_i_size_write(inode
, 0);
3158 return btrfs_update_inode(trans
, new_root
, inode
);
3161 unsigned long btrfs_force_ra(struct address_space
*mapping
,
3162 struct file_ra_state
*ra
, struct file
*file
,
3163 pgoff_t offset
, pgoff_t last_index
)
3165 pgoff_t req_size
= last_index
- offset
+ 1;
3167 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3168 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
3171 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
3172 return offset
+ req_size
;
3176 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3178 struct btrfs_inode
*ei
;
3180 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3184 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
3185 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
3186 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
3187 INIT_LIST_HEAD(&ei
->i_orphan
);
3188 return &ei
->vfs_inode
;
3191 void btrfs_destroy_inode(struct inode
*inode
)
3193 struct btrfs_ordered_extent
*ordered
;
3194 WARN_ON(!list_empty(&inode
->i_dentry
));
3195 WARN_ON(inode
->i_data
.nrpages
);
3197 if (BTRFS_I(inode
)->i_acl
&&
3198 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
3199 posix_acl_release(BTRFS_I(inode
)->i_acl
);
3200 if (BTRFS_I(inode
)->i_default_acl
&&
3201 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
3202 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
3204 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
3205 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
3206 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
3207 " list\n", inode
->i_ino
);
3210 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
3213 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
3217 printk("found ordered extent %Lu %Lu\n",
3218 ordered
->file_offset
, ordered
->len
);
3219 btrfs_remove_ordered_extent(inode
, ordered
);
3220 btrfs_put_ordered_extent(ordered
);
3221 btrfs_put_ordered_extent(ordered
);
3224 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
3225 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3228 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3229 static void init_once(void *foo
)
3230 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3231 static void init_once(struct kmem_cache
* cachep
, void *foo
)
3233 static void init_once(void * foo
, struct kmem_cache
* cachep
,
3234 unsigned long flags
)
3237 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3239 inode_init_once(&ei
->vfs_inode
);
3242 void btrfs_destroy_cachep(void)
3244 if (btrfs_inode_cachep
)
3245 kmem_cache_destroy(btrfs_inode_cachep
);
3246 if (btrfs_trans_handle_cachep
)
3247 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3248 if (btrfs_transaction_cachep
)
3249 kmem_cache_destroy(btrfs_transaction_cachep
);
3250 if (btrfs_bit_radix_cachep
)
3251 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3252 if (btrfs_path_cachep
)
3253 kmem_cache_destroy(btrfs_path_cachep
);
3256 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3257 unsigned long extra_flags
,
3258 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3259 void (*ctor
)(void *)
3260 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3261 void (*ctor
)(struct kmem_cache
*, void *)
3263 void (*ctor
)(void *, struct kmem_cache
*,
3268 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3269 SLAB_MEM_SPREAD
| extra_flags
), ctor
3270 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3276 int btrfs_init_cachep(void)
3278 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3279 sizeof(struct btrfs_inode
),
3281 if (!btrfs_inode_cachep
)
3283 btrfs_trans_handle_cachep
=
3284 btrfs_cache_create("btrfs_trans_handle_cache",
3285 sizeof(struct btrfs_trans_handle
),
3287 if (!btrfs_trans_handle_cachep
)
3289 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3290 sizeof(struct btrfs_transaction
),
3292 if (!btrfs_transaction_cachep
)
3294 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3295 sizeof(struct btrfs_path
),
3297 if (!btrfs_path_cachep
)
3299 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3300 SLAB_DESTROY_BY_RCU
, NULL
);
3301 if (!btrfs_bit_radix_cachep
)
3305 btrfs_destroy_cachep();
3309 static int btrfs_getattr(struct vfsmount
*mnt
,
3310 struct dentry
*dentry
, struct kstat
*stat
)
3312 struct inode
*inode
= dentry
->d_inode
;
3313 generic_fillattr(inode
, stat
);
3314 stat
->blksize
= PAGE_CACHE_SIZE
;
3315 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3319 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3320 struct inode
* new_dir
,struct dentry
*new_dentry
)
3322 struct btrfs_trans_handle
*trans
;
3323 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3324 struct inode
*new_inode
= new_dentry
->d_inode
;
3325 struct inode
*old_inode
= old_dentry
->d_inode
;
3326 struct timespec ctime
= CURRENT_TIME
;
3329 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3330 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3334 ret
= btrfs_check_free_space(root
, 1, 0);
3338 trans
= btrfs_start_transaction(root
, 1);
3340 btrfs_set_trans_block_group(trans
, new_dir
);
3342 old_dentry
->d_inode
->i_nlink
++;
3343 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3344 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3345 old_inode
->i_ctime
= ctime
;
3347 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3352 new_inode
->i_ctime
= CURRENT_TIME
;
3353 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3356 if (new_inode
->i_nlink
== 0) {
3357 ret
= btrfs_orphan_add(trans
, new_inode
);
3362 ret
= btrfs_set_inode_index(new_dir
, old_inode
);
3366 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3371 btrfs_end_transaction_throttle(trans
, root
);
3376 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3377 const char *symname
)
3379 struct btrfs_trans_handle
*trans
;
3380 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3381 struct btrfs_path
*path
;
3382 struct btrfs_key key
;
3383 struct inode
*inode
= NULL
;
3390 struct btrfs_file_extent_item
*ei
;
3391 struct extent_buffer
*leaf
;
3392 unsigned long nr
= 0;
3394 name_len
= strlen(symname
) + 1;
3395 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3396 return -ENAMETOOLONG
;
3398 err
= btrfs_check_free_space(root
, 1, 0);
3402 trans
= btrfs_start_transaction(root
, 1);
3403 btrfs_set_trans_block_group(trans
, dir
);
3405 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3411 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3413 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3414 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3415 err
= PTR_ERR(inode
);
3419 err
= btrfs_init_acl(inode
, dir
);
3425 btrfs_set_trans_block_group(trans
, inode
);
3426 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3430 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3431 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3432 inode
->i_fop
= &btrfs_file_operations
;
3433 inode
->i_op
= &btrfs_file_inode_operations
;
3434 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3435 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3436 inode
->i_mapping
, GFP_NOFS
);
3437 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3438 inode
->i_mapping
, GFP_NOFS
);
3439 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3440 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3441 BTRFS_I(inode
)->delalloc_bytes
= 0;
3442 BTRFS_I(inode
)->disk_i_size
= 0;
3443 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3444 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3446 dir
->i_sb
->s_dirt
= 1;
3447 btrfs_update_inode_block_group(trans
, inode
);
3448 btrfs_update_inode_block_group(trans
, dir
);
3452 path
= btrfs_alloc_path();
3454 key
.objectid
= inode
->i_ino
;
3456 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3457 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3458 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3464 leaf
= path
->nodes
[0];
3465 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3466 struct btrfs_file_extent_item
);
3467 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3468 btrfs_set_file_extent_type(leaf
, ei
,
3469 BTRFS_FILE_EXTENT_INLINE
);
3470 ptr
= btrfs_file_extent_inline_start(ei
);
3471 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3472 btrfs_mark_buffer_dirty(leaf
);
3473 btrfs_free_path(path
);
3475 inode
->i_op
= &btrfs_symlink_inode_operations
;
3476 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3477 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3478 btrfs_i_size_write(inode
, name_len
- 1);
3479 err
= btrfs_update_inode(trans
, root
, inode
);
3484 nr
= trans
->blocks_used
;
3485 btrfs_end_transaction_throttle(trans
, root
);
3488 inode_dec_link_count(inode
);
3491 btrfs_btree_balance_dirty(root
, nr
);
3495 static int btrfs_set_page_dirty(struct page
*page
)
3497 return __set_page_dirty_nobuffers(page
);
3500 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3501 static int btrfs_permission(struct inode
*inode
, int mask
)
3503 static int btrfs_permission(struct inode
*inode
, int mask
,
3504 struct nameidata
*nd
)
3507 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3509 return generic_permission(inode
, mask
, btrfs_check_acl
);
3512 static struct inode_operations btrfs_dir_inode_operations
= {
3513 .lookup
= btrfs_lookup
,
3514 .create
= btrfs_create
,
3515 .unlink
= btrfs_unlink
,
3517 .mkdir
= btrfs_mkdir
,
3518 .rmdir
= btrfs_rmdir
,
3519 .rename
= btrfs_rename
,
3520 .symlink
= btrfs_symlink
,
3521 .setattr
= btrfs_setattr
,
3522 .mknod
= btrfs_mknod
,
3523 .setxattr
= generic_setxattr
,
3524 .getxattr
= generic_getxattr
,
3525 .listxattr
= btrfs_listxattr
,
3526 .removexattr
= generic_removexattr
,
3527 .permission
= btrfs_permission
,
3529 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3530 .lookup
= btrfs_lookup
,
3531 .permission
= btrfs_permission
,
3533 static struct file_operations btrfs_dir_file_operations
= {
3534 .llseek
= generic_file_llseek
,
3535 .read
= generic_read_dir
,
3536 .readdir
= btrfs_readdir
,
3537 .unlocked_ioctl
= btrfs_ioctl
,
3538 #ifdef CONFIG_COMPAT
3539 .compat_ioctl
= btrfs_ioctl
,
3541 .release
= btrfs_release_file
,
3544 static struct extent_io_ops btrfs_extent_io_ops
= {
3545 .fill_delalloc
= run_delalloc_range
,
3546 .submit_bio_hook
= btrfs_submit_bio_hook
,
3547 .merge_bio_hook
= btrfs_merge_bio_hook
,
3548 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3549 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3550 .writepage_start_hook
= btrfs_writepage_start_hook
,
3551 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3552 .set_bit_hook
= btrfs_set_bit_hook
,
3553 .clear_bit_hook
= btrfs_clear_bit_hook
,
3556 static struct address_space_operations btrfs_aops
= {
3557 .readpage
= btrfs_readpage
,
3558 .writepage
= btrfs_writepage
,
3559 .writepages
= btrfs_writepages
,
3560 .readpages
= btrfs_readpages
,
3561 .sync_page
= block_sync_page
,
3563 .direct_IO
= btrfs_direct_IO
,
3564 .invalidatepage
= btrfs_invalidatepage
,
3565 .releasepage
= btrfs_releasepage
,
3566 .set_page_dirty
= btrfs_set_page_dirty
,
3569 static struct address_space_operations btrfs_symlink_aops
= {
3570 .readpage
= btrfs_readpage
,
3571 .writepage
= btrfs_writepage
,
3572 .invalidatepage
= btrfs_invalidatepage
,
3573 .releasepage
= btrfs_releasepage
,
3576 static struct inode_operations btrfs_file_inode_operations
= {
3577 .truncate
= btrfs_truncate
,
3578 .getattr
= btrfs_getattr
,
3579 .setattr
= btrfs_setattr
,
3580 .setxattr
= generic_setxattr
,
3581 .getxattr
= generic_getxattr
,
3582 .listxattr
= btrfs_listxattr
,
3583 .removexattr
= generic_removexattr
,
3584 .permission
= btrfs_permission
,
3586 static struct inode_operations btrfs_special_inode_operations
= {
3587 .getattr
= btrfs_getattr
,
3588 .setattr
= btrfs_setattr
,
3589 .permission
= btrfs_permission
,
3590 .setxattr
= generic_setxattr
,
3591 .getxattr
= generic_getxattr
,
3592 .listxattr
= btrfs_listxattr
,
3593 .removexattr
= generic_removexattr
,
3595 static struct inode_operations btrfs_symlink_inode_operations
= {
3596 .readlink
= generic_readlink
,
3597 .follow_link
= page_follow_link_light
,
3598 .put_link
= page_put_link
,
3599 .permission
= btrfs_permission
,