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>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
46 #include "ordered-data.h"
48 struct btrfs_iget_args
{
50 struct btrfs_root
*root
;
53 static struct inode_operations btrfs_dir_inode_operations
;
54 static struct inode_operations btrfs_symlink_inode_operations
;
55 static struct inode_operations btrfs_dir_ro_inode_operations
;
56 static struct inode_operations btrfs_special_inode_operations
;
57 static struct inode_operations btrfs_file_inode_operations
;
58 static struct address_space_operations btrfs_aops
;
59 static struct address_space_operations btrfs_symlink_aops
;
60 static struct file_operations btrfs_dir_file_operations
;
61 static struct extent_io_ops btrfs_extent_io_ops
;
63 static struct kmem_cache
*btrfs_inode_cachep
;
64 struct kmem_cache
*btrfs_trans_handle_cachep
;
65 struct kmem_cache
*btrfs_transaction_cachep
;
66 struct kmem_cache
*btrfs_bit_radix_cachep
;
67 struct kmem_cache
*btrfs_path_cachep
;
70 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
71 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
72 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
73 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
74 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
75 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
76 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
77 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
80 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
89 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
90 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
91 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
99 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
101 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
105 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
107 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
108 struct btrfs_trans_handle
*trans
;
112 u64 blocksize
= root
->sectorsize
;
114 struct btrfs_key ins
;
115 struct extent_map
*em
;
116 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
119 trans
= btrfs_join_transaction(root
, 1);
121 btrfs_set_trans_block_group(trans
, inode
);
123 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
124 num_bytes
= max(blocksize
, num_bytes
);
125 orig_num_bytes
= num_bytes
;
127 if (alloc_hint
== EXTENT_MAP_INLINE
)
130 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
131 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1);
133 while(num_bytes
> 0) {
134 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
135 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
136 root
->sectorsize
, 0, 0,
142 em
= alloc_extent_map(GFP_NOFS
);
144 em
->len
= ins
.offset
;
145 em
->block_start
= ins
.objectid
;
146 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
148 spin_lock(&em_tree
->lock
);
149 ret
= add_extent_mapping(em_tree
, em
);
150 spin_unlock(&em_tree
->lock
);
151 if (ret
!= -EEXIST
) {
155 btrfs_drop_extent_cache(inode
, start
,
156 start
+ ins
.offset
- 1);
159 cur_alloc_size
= ins
.offset
;
160 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
163 if (num_bytes
< cur_alloc_size
) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
168 num_bytes
-= cur_alloc_size
;
169 alloc_hint
= ins
.objectid
+ ins
.offset
;
170 start
+= cur_alloc_size
;
173 btrfs_end_transaction(trans
, root
);
177 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
185 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
186 struct btrfs_block_group_cache
*block_group
;
187 struct extent_buffer
*leaf
;
189 struct btrfs_path
*path
;
190 struct btrfs_file_extent_item
*item
;
193 struct btrfs_key found_key
;
195 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
196 path
= btrfs_alloc_path();
199 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
200 inode
->i_ino
, start
, 0);
202 btrfs_free_path(path
);
208 if (path
->slots
[0] == 0)
213 leaf
= path
->nodes
[0];
214 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
215 struct btrfs_file_extent_item
);
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
219 found_type
= btrfs_key_type(&found_key
);
220 if (found_key
.objectid
!= inode
->i_ino
||
221 found_type
!= BTRFS_EXTENT_DATA_KEY
)
224 found_type
= btrfs_file_extent_type(leaf
, item
);
225 extent_start
= found_key
.offset
;
226 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
227 u64 extent_num_bytes
;
229 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
230 extent_end
= extent_start
+ extent_num_bytes
;
233 if (loops
&& start
!= extent_start
)
236 if (start
< extent_start
|| start
>= extent_end
)
239 cow_end
= min(end
, extent_end
- 1);
240 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
244 if (btrfs_count_snapshots_in_path(root
, path
, inode
->i_ino
,
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
253 block_group
= btrfs_lookup_block_group(root
->fs_info
,
255 if (!block_group
|| block_group
->ro
)
264 btrfs_free_path(path
);
267 btrfs_release_path(root
, path
);
272 cow_file_range(inode
, start
, end
);
277 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
279 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
282 if (btrfs_test_opt(root
, NODATACOW
) ||
283 btrfs_test_flag(inode
, NODATACOW
))
284 ret
= run_delalloc_nocow(inode
, start
, end
);
286 ret
= cow_file_range(inode
, start
, end
);
291 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
292 unsigned long old
, unsigned long bits
)
295 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
297 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
298 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
299 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
300 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
305 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
306 unsigned long old
, unsigned long bits
)
308 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
309 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
312 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
313 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
316 root
->fs_info
->delalloc_bytes
= 0;
317 BTRFS_I(inode
)->delalloc_bytes
= 0;
319 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
320 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
322 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
327 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
328 size_t size
, struct bio
*bio
)
330 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
331 struct btrfs_mapping_tree
*map_tree
;
332 u64 logical
= bio
->bi_sector
<< 9;
337 length
= bio
->bi_size
;
338 map_tree
= &root
->fs_info
->mapping_tree
;
340 ret
= btrfs_map_block(map_tree
, READ
, logical
,
341 &map_length
, NULL
, 0);
343 if (map_length
< length
+ size
) {
349 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
355 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
358 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
361 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
364 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
367 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
370 if (!(rw
& (1 << BIO_RW
))) {
374 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
375 inode
, rw
, bio
, mirror_num
,
376 __btrfs_submit_bio_hook
);
378 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
381 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
382 struct inode
*inode
, u64 file_offset
,
383 struct list_head
*list
)
385 struct list_head
*cur
;
386 struct btrfs_ordered_sum
*sum
;
388 btrfs_set_trans_block_group(trans
, inode
);
389 list_for_each(cur
, list
) {
390 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
391 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
392 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
394 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
399 struct btrfs_writepage_fixup
{
401 struct btrfs_work work
;
404 /* see btrfs_writepage_start_hook for details on why this is required */
405 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
407 struct btrfs_writepage_fixup
*fixup
;
408 struct btrfs_ordered_extent
*ordered
;
414 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
418 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
419 ClearPageChecked(page
);
423 inode
= page
->mapping
->host
;
424 page_start
= page_offset(page
);
425 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
427 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
428 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
432 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
434 ClearPageChecked(page
);
436 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
439 page_cache_release(page
);
443 * There are a few paths in the higher layers of the kernel that directly
444 * set the page dirty bit without asking the filesystem if it is a
445 * good idea. This causes problems because we want to make sure COW
446 * properly happens and the data=ordered rules are followed.
448 * In our case any range that doesn't have the EXTENT_ORDERED bit set
449 * hasn't been properly setup for IO. We kick off an async process
450 * to fix it up. The async helper will wait for ordered extents, set
451 * the delalloc bit and make it safe to write the page.
453 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
455 struct inode
*inode
= page
->mapping
->host
;
456 struct btrfs_writepage_fixup
*fixup
;
457 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
460 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
465 if (PageChecked(page
))
468 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
471 printk("queueing worker to fixup page %lu %Lu\n", inode
->i_ino
, page_offset(page
));
472 SetPageChecked(page
);
473 page_cache_get(page
);
474 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
476 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
480 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
481 struct extent_state
*state
, int uptodate
)
483 struct inode
*inode
= page
->mapping
->host
;
484 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
485 struct btrfs_trans_handle
*trans
;
486 struct btrfs_ordered_extent
*ordered_extent
;
487 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
489 struct list_head list
;
490 struct btrfs_key ins
;
493 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
497 trans
= btrfs_join_transaction(root
, 1);
499 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
500 BUG_ON(!ordered_extent
);
502 lock_extent(io_tree
, ordered_extent
->file_offset
,
503 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
506 INIT_LIST_HEAD(&list
);
508 ins
.objectid
= ordered_extent
->start
;
509 ins
.offset
= ordered_extent
->len
;
510 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
511 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
512 trans
->transid
, inode
->i_ino
,
513 ordered_extent
->file_offset
, &ins
);
516 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
517 ret
= btrfs_drop_extents(trans
, root
, inode
,
518 ordered_extent
->file_offset
,
519 ordered_extent
->file_offset
+
521 ordered_extent
->file_offset
, &alloc_hint
);
523 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
524 ordered_extent
->file_offset
,
525 ordered_extent
->start
,
527 ordered_extent
->len
, 0);
529 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
530 ordered_extent
->file_offset
+
531 ordered_extent
->len
- 1);
532 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
534 inode
->i_blocks
+= ordered_extent
->len
>> 9;
535 unlock_extent(io_tree
, ordered_extent
->file_offset
,
536 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
538 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
539 &ordered_extent
->list
);
541 btrfs_ordered_update_i_size(inode
, ordered_extent
);
542 btrfs_remove_ordered_extent(inode
, ordered_extent
);
544 btrfs_put_ordered_extent(ordered_extent
);
545 /* once for the tree */
546 btrfs_put_ordered_extent(ordered_extent
);
548 btrfs_update_inode(trans
, root
, inode
);
549 btrfs_end_transaction(trans
, root
);
553 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
556 struct inode
*inode
= page
->mapping
->host
;
557 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
558 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
559 struct btrfs_csum_item
*item
;
560 struct btrfs_path
*path
= NULL
;
563 if (btrfs_test_opt(root
, NODATASUM
) ||
564 btrfs_test_flag(inode
, NODATASUM
))
567 path
= btrfs_alloc_path();
568 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
571 * It is possible there is an ordered extent that has
572 * not yet finished for this range in the file. If so,
573 * that extent will have a csum cached, and it will insert
574 * the sum after all the blocks in the extent are fully
575 * on disk. So, look for an ordered extent and use the
578 ret
= btrfs_find_ordered_sum(inode
, start
, &csum
);
583 /* a csum that isn't present is a preallocated region. */
584 if (ret
== -ENOENT
|| ret
== -EFBIG
)
587 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
591 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
594 set_state_private(io_tree
, start
, csum
);
597 btrfs_free_path(path
);
601 struct io_failure_record
{
609 int btrfs_io_failed_hook(struct bio
*failed_bio
,
610 struct page
*page
, u64 start
, u64 end
,
611 struct extent_state
*state
)
613 struct io_failure_record
*failrec
= NULL
;
615 struct extent_map
*em
;
616 struct inode
*inode
= page
->mapping
->host
;
617 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
618 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
625 ret
= get_state_private(failure_tree
, start
, &private);
627 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
630 failrec
->start
= start
;
631 failrec
->len
= end
- start
+ 1;
632 failrec
->last_mirror
= 0;
634 spin_lock(&em_tree
->lock
);
635 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
636 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
640 spin_unlock(&em_tree
->lock
);
642 if (!em
|| IS_ERR(em
)) {
646 logical
= start
- em
->start
;
647 logical
= em
->block_start
+ logical
;
648 failrec
->logical
= logical
;
650 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
651 EXTENT_DIRTY
, GFP_NOFS
);
652 set_state_private(failure_tree
, start
,
653 (u64
)(unsigned long)failrec
);
655 failrec
= (struct io_failure_record
*)(unsigned long)private;
657 num_copies
= btrfs_num_copies(
658 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
659 failrec
->logical
, failrec
->len
);
660 failrec
->last_mirror
++;
662 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
663 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
666 if (state
&& state
->start
!= failrec
->start
)
668 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
670 if (!state
|| failrec
->last_mirror
> num_copies
) {
671 set_state_private(failure_tree
, failrec
->start
, 0);
672 clear_extent_bits(failure_tree
, failrec
->start
,
673 failrec
->start
+ failrec
->len
- 1,
674 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
678 bio
= bio_alloc(GFP_NOFS
, 1);
679 bio
->bi_private
= state
;
680 bio
->bi_end_io
= failed_bio
->bi_end_io
;
681 bio
->bi_sector
= failrec
->logical
>> 9;
682 bio
->bi_bdev
= failed_bio
->bi_bdev
;
684 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
685 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
690 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
691 failrec
->last_mirror
);
695 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
699 struct io_failure_record
*failure
;
703 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
704 (u64
)-1, 1, EXTENT_DIRTY
)) {
705 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
706 start
, &private_failure
);
708 failure
= (struct io_failure_record
*)(unsigned long)
710 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
712 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
714 failure
->start
+ failure
->len
- 1,
715 EXTENT_DIRTY
| EXTENT_LOCKED
,
723 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
724 struct extent_state
*state
)
726 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
727 struct inode
*inode
= page
->mapping
->host
;
728 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
730 u64
private = ~(u32
)0;
732 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
736 if (btrfs_test_opt(root
, NODATASUM
) ||
737 btrfs_test_flag(inode
, NODATASUM
))
739 if (state
&& state
->start
== start
) {
740 private = state
->private;
743 ret
= get_state_private(io_tree
, start
, &private);
745 local_irq_save(flags
);
746 kaddr
= kmap_atomic(page
, KM_IRQ0
);
750 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
751 btrfs_csum_final(csum
, (char *)&csum
);
752 if (csum
!= private) {
755 kunmap_atomic(kaddr
, KM_IRQ0
);
756 local_irq_restore(flags
);
758 /* if the io failure tree for this inode is non-empty,
759 * check to see if we've recovered from a failed IO
761 btrfs_clean_io_failures(inode
, start
);
765 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
766 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
768 memset(kaddr
+ offset
, 1, end
- start
+ 1);
769 flush_dcache_page(page
);
770 kunmap_atomic(kaddr
, KM_IRQ0
);
771 local_irq_restore(flags
);
777 void btrfs_read_locked_inode(struct inode
*inode
)
779 struct btrfs_path
*path
;
780 struct extent_buffer
*leaf
;
781 struct btrfs_inode_item
*inode_item
;
782 struct btrfs_timespec
*tspec
;
783 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
784 struct btrfs_key location
;
785 u64 alloc_group_block
;
789 path
= btrfs_alloc_path();
791 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
793 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
797 leaf
= path
->nodes
[0];
798 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
799 struct btrfs_inode_item
);
801 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
802 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
803 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
804 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
805 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
807 tspec
= btrfs_inode_atime(inode_item
);
808 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
809 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
811 tspec
= btrfs_inode_mtime(inode_item
);
812 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
813 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
815 tspec
= btrfs_inode_ctime(inode_item
);
816 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
817 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
819 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
820 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
822 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
824 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
825 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
827 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
828 if (!BTRFS_I(inode
)->block_group
) {
829 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
831 BTRFS_BLOCK_GROUP_METADATA
, 0);
833 btrfs_free_path(path
);
836 switch (inode
->i_mode
& S_IFMT
) {
838 inode
->i_mapping
->a_ops
= &btrfs_aops
;
839 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
840 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
841 inode
->i_fop
= &btrfs_file_operations
;
842 inode
->i_op
= &btrfs_file_inode_operations
;
845 inode
->i_fop
= &btrfs_dir_file_operations
;
846 if (root
== root
->fs_info
->tree_root
)
847 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
849 inode
->i_op
= &btrfs_dir_inode_operations
;
852 inode
->i_op
= &btrfs_symlink_inode_operations
;
853 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
854 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
857 init_special_inode(inode
, inode
->i_mode
, rdev
);
863 btrfs_free_path(path
);
864 make_bad_inode(inode
);
867 static void fill_inode_item(struct extent_buffer
*leaf
,
868 struct btrfs_inode_item
*item
,
871 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
872 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
873 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
874 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
875 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
877 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
878 inode
->i_atime
.tv_sec
);
879 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
880 inode
->i_atime
.tv_nsec
);
882 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
883 inode
->i_mtime
.tv_sec
);
884 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
885 inode
->i_mtime
.tv_nsec
);
887 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
888 inode
->i_ctime
.tv_sec
);
889 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
890 inode
->i_ctime
.tv_nsec
);
892 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
893 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
894 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
895 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
896 btrfs_set_inode_block_group(leaf
, item
,
897 BTRFS_I(inode
)->block_group
->key
.objectid
);
900 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
901 struct btrfs_root
*root
,
904 struct btrfs_inode_item
*inode_item
;
905 struct btrfs_path
*path
;
906 struct extent_buffer
*leaf
;
909 path
= btrfs_alloc_path();
911 ret
= btrfs_lookup_inode(trans
, root
, path
,
912 &BTRFS_I(inode
)->location
, 1);
919 leaf
= path
->nodes
[0];
920 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
921 struct btrfs_inode_item
);
923 fill_inode_item(leaf
, inode_item
, inode
);
924 btrfs_mark_buffer_dirty(leaf
);
925 btrfs_set_inode_last_trans(trans
, inode
);
928 btrfs_free_path(path
);
933 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
934 struct btrfs_root
*root
,
936 struct dentry
*dentry
)
938 struct btrfs_path
*path
;
939 const char *name
= dentry
->d_name
.name
;
940 int name_len
= dentry
->d_name
.len
;
942 struct extent_buffer
*leaf
;
943 struct btrfs_dir_item
*di
;
944 struct btrfs_key key
;
946 path
= btrfs_alloc_path();
952 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
962 leaf
= path
->nodes
[0];
963 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
964 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
967 btrfs_release_path(root
, path
);
969 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
970 key
.objectid
, name
, name_len
, -1);
979 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
980 btrfs_release_path(root
, path
);
982 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
983 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
984 dentry
->d_inode
->i_ino
,
985 dentry
->d_parent
->d_inode
->i_ino
);
987 printk("failed to delete reference to %.*s, "
988 "inode %lu parent %lu\n", name_len
, name
,
989 dentry
->d_inode
->i_ino
,
990 dentry
->d_parent
->d_inode
->i_ino
);
993 btrfs_free_path(path
);
995 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
996 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
997 btrfs_update_inode(trans
, root
, dir
);
998 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
999 dentry
->d_inode
->i_nlink
--;
1001 drop_nlink(dentry
->d_inode
);
1003 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1004 dir
->i_sb
->s_dirt
= 1;
1009 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1011 struct btrfs_root
*root
;
1012 struct btrfs_trans_handle
*trans
;
1014 unsigned long nr
= 0;
1016 root
= BTRFS_I(dir
)->root
;
1018 ret
= btrfs_check_free_space(root
, 1, 1);
1022 trans
= btrfs_start_transaction(root
, 1);
1024 btrfs_set_trans_block_group(trans
, dir
);
1025 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1026 nr
= trans
->blocks_used
;
1028 btrfs_end_transaction_throttle(trans
, root
);
1030 btrfs_btree_balance_dirty(root
, nr
);
1034 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1036 struct inode
*inode
= dentry
->d_inode
;
1039 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1040 struct btrfs_trans_handle
*trans
;
1041 unsigned long nr
= 0;
1043 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1047 ret
= btrfs_check_free_space(root
, 1, 1);
1051 trans
= btrfs_start_transaction(root
, 1);
1052 btrfs_set_trans_block_group(trans
, dir
);
1054 /* now the directory is empty */
1055 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1057 btrfs_i_size_write(inode
, 0);
1060 nr
= trans
->blocks_used
;
1061 ret
= btrfs_end_transaction_throttle(trans
, root
);
1063 btrfs_btree_balance_dirty(root
, nr
);
1071 * this can truncate away extent items, csum items and directory items.
1072 * It starts at a high offset and removes keys until it can't find
1073 * any higher than i_size.
1075 * csum items that cross the new i_size are truncated to the new size
1078 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1079 struct btrfs_root
*root
,
1080 struct inode
*inode
,
1084 struct btrfs_path
*path
;
1085 struct btrfs_key key
;
1086 struct btrfs_key found_key
;
1088 struct extent_buffer
*leaf
;
1089 struct btrfs_file_extent_item
*fi
;
1090 u64 extent_start
= 0;
1091 u64 extent_num_bytes
= 0;
1097 int pending_del_nr
= 0;
1098 int pending_del_slot
= 0;
1099 int extent_type
= -1;
1100 u64 mask
= root
->sectorsize
- 1;
1102 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1103 path
= btrfs_alloc_path();
1107 /* FIXME, add redo link to tree so we don't leak on crash */
1108 key
.objectid
= inode
->i_ino
;
1109 key
.offset
= (u64
)-1;
1112 btrfs_init_path(path
);
1114 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1119 BUG_ON(path
->slots
[0] == 0);
1125 leaf
= path
->nodes
[0];
1126 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1127 found_type
= btrfs_key_type(&found_key
);
1129 if (found_key
.objectid
!= inode
->i_ino
)
1132 if (found_type
< min_type
)
1135 item_end
= found_key
.offset
;
1136 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1137 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1138 struct btrfs_file_extent_item
);
1139 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1140 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1142 btrfs_file_extent_num_bytes(leaf
, fi
);
1143 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1144 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1146 item_end
+= btrfs_file_extent_inline_len(leaf
,
1151 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1152 ret
= btrfs_csum_truncate(trans
, root
, path
,
1156 if (item_end
< inode
->i_size
) {
1157 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1158 found_type
= BTRFS_INODE_ITEM_KEY
;
1159 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1160 found_type
= BTRFS_CSUM_ITEM_KEY
;
1161 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1162 found_type
= BTRFS_XATTR_ITEM_KEY
;
1163 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1164 found_type
= BTRFS_INODE_REF_KEY
;
1165 } else if (found_type
) {
1170 btrfs_set_key_type(&key
, found_type
);
1173 if (found_key
.offset
>= inode
->i_size
)
1179 /* FIXME, shrink the extent if the ref count is only 1 */
1180 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1183 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1185 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1187 u64 orig_num_bytes
=
1188 btrfs_file_extent_num_bytes(leaf
, fi
);
1189 extent_num_bytes
= inode
->i_size
-
1190 found_key
.offset
+ root
->sectorsize
- 1;
1191 extent_num_bytes
= extent_num_bytes
&
1192 ~((u64
)root
->sectorsize
- 1);
1193 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1195 num_dec
= (orig_num_bytes
-
1197 if (extent_start
!= 0)
1198 dec_i_blocks(inode
, num_dec
);
1199 btrfs_mark_buffer_dirty(leaf
);
1202 btrfs_file_extent_disk_num_bytes(leaf
,
1204 /* FIXME blocksize != 4096 */
1205 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1206 if (extent_start
!= 0) {
1208 dec_i_blocks(inode
, num_dec
);
1210 root_gen
= btrfs_header_generation(leaf
);
1211 root_owner
= btrfs_header_owner(leaf
);
1213 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1215 u32 newsize
= inode
->i_size
- found_key
.offset
;
1216 dec_i_blocks(inode
, item_end
+ 1 -
1217 found_key
.offset
- newsize
);
1219 btrfs_file_extent_calc_inline_size(newsize
);
1220 ret
= btrfs_truncate_item(trans
, root
, path
,
1224 dec_i_blocks(inode
, item_end
+ 1 -
1230 if (!pending_del_nr
) {
1231 /* no pending yet, add ourselves */
1232 pending_del_slot
= path
->slots
[0];
1234 } else if (pending_del_nr
&&
1235 path
->slots
[0] + 1 == pending_del_slot
) {
1236 /* hop on the pending chunk */
1238 pending_del_slot
= path
->slots
[0];
1240 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1246 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1249 root_gen
, inode
->i_ino
,
1250 found_key
.offset
, 0);
1254 if (path
->slots
[0] == 0) {
1257 btrfs_release_path(root
, path
);
1262 if (pending_del_nr
&&
1263 path
->slots
[0] + 1 != pending_del_slot
) {
1264 struct btrfs_key debug
;
1266 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1268 ret
= btrfs_del_items(trans
, root
, path
,
1273 btrfs_release_path(root
, path
);
1279 if (pending_del_nr
) {
1280 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1283 btrfs_free_path(path
);
1284 inode
->i_sb
->s_dirt
= 1;
1289 * taken from block_truncate_page, but does cow as it zeros out
1290 * any bytes left in the last page in the file.
1292 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1294 struct inode
*inode
= mapping
->host
;
1295 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1296 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1297 struct btrfs_ordered_extent
*ordered
;
1299 u32 blocksize
= root
->sectorsize
;
1300 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1301 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1307 if ((offset
& (blocksize
- 1)) == 0)
1312 page
= grab_cache_page(mapping
, index
);
1316 page_start
= page_offset(page
);
1317 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1319 if (!PageUptodate(page
)) {
1320 ret
= btrfs_readpage(NULL
, page
);
1322 if (page
->mapping
!= mapping
) {
1324 page_cache_release(page
);
1327 if (!PageUptodate(page
)) {
1332 wait_on_page_writeback(page
);
1334 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1335 set_page_extent_mapped(page
);
1337 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1339 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1341 page_cache_release(page
);
1342 btrfs_start_ordered_extent(inode
, ordered
, 1);
1343 btrfs_put_ordered_extent(ordered
);
1347 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1348 page_end
, GFP_NOFS
);
1350 if (offset
!= PAGE_CACHE_SIZE
) {
1352 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1353 flush_dcache_page(page
);
1356 ClearPageChecked(page
);
1357 set_page_dirty(page
);
1358 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1361 page_cache_release(page
);
1366 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1368 struct inode
*inode
= dentry
->d_inode
;
1371 err
= inode_change_ok(inode
, attr
);
1375 if (S_ISREG(inode
->i_mode
) &&
1376 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1377 struct btrfs_trans_handle
*trans
;
1378 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1379 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1381 u64 mask
= root
->sectorsize
- 1;
1382 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1383 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1387 if (attr
->ia_size
<= hole_start
)
1390 err
= btrfs_check_free_space(root
, 1, 0);
1394 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1396 hole_size
= block_end
- hole_start
;
1397 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1398 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1400 trans
= btrfs_start_transaction(root
, 1);
1401 btrfs_set_trans_block_group(trans
, inode
);
1402 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1403 err
= btrfs_drop_extents(trans
, root
, inode
,
1404 hole_start
, block_end
, hole_start
,
1407 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1408 err
= btrfs_insert_file_extent(trans
, root
,
1412 btrfs_drop_extent_cache(inode
, hole_start
,
1414 btrfs_check_file(root
, inode
);
1416 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1417 btrfs_end_transaction(trans
, root
);
1418 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1423 err
= inode_setattr(inode
, attr
);
1428 void btrfs_delete_inode(struct inode
*inode
)
1430 struct btrfs_trans_handle
*trans
;
1431 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1435 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1436 truncate_inode_pages(&inode
->i_data
, 0);
1437 if (is_bad_inode(inode
)) {
1441 btrfs_i_size_write(inode
, 0);
1442 trans
= btrfs_start_transaction(root
, 1);
1444 btrfs_set_trans_block_group(trans
, inode
);
1445 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1447 goto no_delete_lock
;
1449 nr
= trans
->blocks_used
;
1452 btrfs_end_transaction(trans
, root
);
1453 btrfs_btree_balance_dirty(root
, nr
);
1457 nr
= trans
->blocks_used
;
1458 btrfs_end_transaction(trans
, root
);
1459 btrfs_btree_balance_dirty(root
, nr
);
1465 * this returns the key found in the dir entry in the location pointer.
1466 * If no dir entries were found, location->objectid is 0.
1468 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1469 struct btrfs_key
*location
)
1471 const char *name
= dentry
->d_name
.name
;
1472 int namelen
= dentry
->d_name
.len
;
1473 struct btrfs_dir_item
*di
;
1474 struct btrfs_path
*path
;
1475 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1478 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1479 location
->objectid
= dir
->i_ino
;
1480 location
->type
= BTRFS_INODE_ITEM_KEY
;
1481 location
->offset
= 0;
1484 path
= btrfs_alloc_path();
1487 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1488 struct btrfs_key key
;
1489 struct extent_buffer
*leaf
;
1493 key
.objectid
= dir
->i_ino
;
1494 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1496 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1500 leaf
= path
->nodes
[0];
1501 slot
= path
->slots
[0];
1502 nritems
= btrfs_header_nritems(leaf
);
1503 if (slot
>= nritems
)
1506 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1507 if (key
.objectid
!= dir
->i_ino
||
1508 key
.type
!= BTRFS_INODE_REF_KEY
) {
1511 location
->objectid
= key
.offset
;
1512 location
->type
= BTRFS_INODE_ITEM_KEY
;
1513 location
->offset
= 0;
1517 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1521 if (!di
|| IS_ERR(di
)) {
1524 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1526 btrfs_free_path(path
);
1529 location
->objectid
= 0;
1534 * when we hit a tree root in a directory, the btrfs part of the inode
1535 * needs to be changed to reflect the root directory of the tree root. This
1536 * is kind of like crossing a mount point.
1538 static int fixup_tree_root_location(struct btrfs_root
*root
,
1539 struct btrfs_key
*location
,
1540 struct btrfs_root
**sub_root
,
1541 struct dentry
*dentry
)
1543 struct btrfs_path
*path
;
1544 struct btrfs_root_item
*ri
;
1546 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1548 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1551 path
= btrfs_alloc_path();
1554 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1555 dentry
->d_name
.name
,
1556 dentry
->d_name
.len
);
1557 if (IS_ERR(*sub_root
))
1558 return PTR_ERR(*sub_root
);
1560 ri
= &(*sub_root
)->root_item
;
1561 location
->objectid
= btrfs_root_dirid(ri
);
1562 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1563 location
->offset
= 0;
1565 btrfs_free_path(path
);
1569 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1571 struct btrfs_iget_args
*args
= p
;
1572 inode
->i_ino
= args
->ino
;
1573 BTRFS_I(inode
)->root
= args
->root
;
1574 BTRFS_I(inode
)->delalloc_bytes
= 0;
1575 BTRFS_I(inode
)->disk_i_size
= 0;
1576 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1577 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1578 inode
->i_mapping
, GFP_NOFS
);
1579 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1580 inode
->i_mapping
, GFP_NOFS
);
1581 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1582 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1583 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1587 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1589 struct btrfs_iget_args
*args
= opaque
;
1590 return (args
->ino
== inode
->i_ino
&&
1591 args
->root
== BTRFS_I(inode
)->root
);
1594 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1597 struct btrfs_iget_args args
;
1598 args
.ino
= objectid
;
1599 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1604 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1607 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1608 struct btrfs_root
*root
)
1610 struct inode
*inode
;
1611 struct btrfs_iget_args args
;
1612 args
.ino
= objectid
;
1615 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1616 btrfs_init_locked_inode
,
1621 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1622 struct nameidata
*nd
)
1624 struct inode
* inode
;
1625 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1626 struct btrfs_root
*root
= bi
->root
;
1627 struct btrfs_root
*sub_root
= root
;
1628 struct btrfs_key location
;
1631 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1632 return ERR_PTR(-ENAMETOOLONG
);
1634 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1637 return ERR_PTR(ret
);
1640 if (location
.objectid
) {
1641 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1644 return ERR_PTR(ret
);
1646 return ERR_PTR(-ENOENT
);
1647 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1650 return ERR_PTR(-EACCES
);
1651 if (inode
->i_state
& I_NEW
) {
1652 /* the inode and parent dir are two different roots */
1653 if (sub_root
!= root
) {
1655 sub_root
->inode
= inode
;
1657 BTRFS_I(inode
)->root
= sub_root
;
1658 memcpy(&BTRFS_I(inode
)->location
, &location
,
1660 btrfs_read_locked_inode(inode
);
1661 unlock_new_inode(inode
);
1664 return d_splice_alias(inode
, dentry
);
1667 static unsigned char btrfs_filetype_table
[] = {
1668 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1671 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1673 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1675 struct btrfs_item
*item
;
1676 struct btrfs_dir_item
*di
;
1677 struct btrfs_key key
;
1678 struct btrfs_key found_key
;
1679 struct btrfs_path
*path
;
1682 struct extent_buffer
*leaf
;
1685 unsigned char d_type
;
1690 int key_type
= BTRFS_DIR_INDEX_KEY
;
1695 /* FIXME, use a real flag for deciding about the key type */
1696 if (root
->fs_info
->tree_root
== root
)
1697 key_type
= BTRFS_DIR_ITEM_KEY
;
1699 /* special case for "." */
1700 if (filp
->f_pos
== 0) {
1701 over
= filldir(dirent
, ".", 1,
1709 key
.objectid
= inode
->i_ino
;
1710 path
= btrfs_alloc_path();
1713 /* special case for .., just use the back ref */
1714 if (filp
->f_pos
== 1) {
1715 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1717 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1719 leaf
= path
->nodes
[0];
1720 slot
= path
->slots
[0];
1721 nritems
= btrfs_header_nritems(leaf
);
1722 if (slot
>= nritems
) {
1723 btrfs_release_path(root
, path
);
1724 goto read_dir_items
;
1726 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1727 btrfs_release_path(root
, path
);
1728 if (found_key
.objectid
!= key
.objectid
||
1729 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1730 goto read_dir_items
;
1731 over
= filldir(dirent
, "..", 2,
1732 2, found_key
.offset
, DT_DIR
);
1739 btrfs_set_key_type(&key
, key_type
);
1740 key
.offset
= filp
->f_pos
;
1742 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1747 leaf
= path
->nodes
[0];
1748 nritems
= btrfs_header_nritems(leaf
);
1749 slot
= path
->slots
[0];
1750 if (advance
|| slot
>= nritems
) {
1751 if (slot
>= nritems
-1) {
1752 ret
= btrfs_next_leaf(root
, path
);
1755 leaf
= path
->nodes
[0];
1756 nritems
= btrfs_header_nritems(leaf
);
1757 slot
= path
->slots
[0];
1764 item
= btrfs_item_nr(leaf
, slot
);
1765 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1767 if (found_key
.objectid
!= key
.objectid
)
1769 if (btrfs_key_type(&found_key
) != key_type
)
1771 if (found_key
.offset
< filp
->f_pos
)
1774 filp
->f_pos
= found_key
.offset
;
1776 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1778 di_total
= btrfs_item_size(leaf
, item
);
1779 while(di_cur
< di_total
) {
1780 struct btrfs_key location
;
1782 name_len
= btrfs_dir_name_len(leaf
, di
);
1783 if (name_len
< 32) {
1784 name_ptr
= tmp_name
;
1786 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1789 read_extent_buffer(leaf
, name_ptr
,
1790 (unsigned long)(di
+ 1), name_len
);
1792 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1793 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1794 over
= filldir(dirent
, name_ptr
, name_len
,
1799 if (name_ptr
!= tmp_name
)
1804 di_len
= btrfs_dir_name_len(leaf
, di
) +
1805 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1807 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1810 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1811 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1817 btrfs_free_path(path
);
1821 int btrfs_write_inode(struct inode
*inode
, int wait
)
1823 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1824 struct btrfs_trans_handle
*trans
;
1828 trans
= btrfs_join_transaction(root
, 1);
1829 btrfs_set_trans_block_group(trans
, inode
);
1830 ret
= btrfs_commit_transaction(trans
, root
);
1836 * This is somewhat expensive, updating the tree every time the
1837 * inode changes. But, it is most likely to find the inode in cache.
1838 * FIXME, needs more benchmarking...there are no reasons other than performance
1839 * to keep or drop this code.
1841 void btrfs_dirty_inode(struct inode
*inode
)
1843 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1844 struct btrfs_trans_handle
*trans
;
1846 trans
= btrfs_join_transaction(root
, 1);
1847 btrfs_set_trans_block_group(trans
, inode
);
1848 btrfs_update_inode(trans
, root
, inode
);
1849 btrfs_end_transaction(trans
, root
);
1852 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1853 struct btrfs_root
*root
,
1854 const char *name
, int name_len
,
1857 struct btrfs_block_group_cache
*group
,
1860 struct inode
*inode
;
1861 struct btrfs_inode_item
*inode_item
;
1862 struct btrfs_block_group_cache
*new_inode_group
;
1863 struct btrfs_key
*location
;
1864 struct btrfs_path
*path
;
1865 struct btrfs_inode_ref
*ref
;
1866 struct btrfs_key key
[2];
1872 path
= btrfs_alloc_path();
1875 inode
= new_inode(root
->fs_info
->sb
);
1877 return ERR_PTR(-ENOMEM
);
1879 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1880 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1881 inode
->i_mapping
, GFP_NOFS
);
1882 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1883 inode
->i_mapping
, GFP_NOFS
);
1884 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1885 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1886 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1887 BTRFS_I(inode
)->delalloc_bytes
= 0;
1888 BTRFS_I(inode
)->disk_i_size
= 0;
1889 BTRFS_I(inode
)->root
= root
;
1895 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1896 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1897 if (!new_inode_group
) {
1898 printk("find_block group failed\n");
1899 new_inode_group
= group
;
1901 BTRFS_I(inode
)->block_group
= new_inode_group
;
1902 BTRFS_I(inode
)->flags
= 0;
1904 key
[0].objectid
= objectid
;
1905 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1908 key
[1].objectid
= objectid
;
1909 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1910 key
[1].offset
= ref_objectid
;
1912 sizes
[0] = sizeof(struct btrfs_inode_item
);
1913 sizes
[1] = name_len
+ sizeof(*ref
);
1915 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1919 if (objectid
> root
->highest_inode
)
1920 root
->highest_inode
= objectid
;
1922 inode
->i_uid
= current
->fsuid
;
1923 inode
->i_gid
= current
->fsgid
;
1924 inode
->i_mode
= mode
;
1925 inode
->i_ino
= objectid
;
1926 inode
->i_blocks
= 0;
1927 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1928 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1929 struct btrfs_inode_item
);
1930 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1932 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1933 struct btrfs_inode_ref
);
1934 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1935 ptr
= (unsigned long)(ref
+ 1);
1936 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1938 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1939 btrfs_free_path(path
);
1941 location
= &BTRFS_I(inode
)->location
;
1942 location
->objectid
= objectid
;
1943 location
->offset
= 0;
1944 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1946 insert_inode_hash(inode
);
1949 btrfs_free_path(path
);
1950 return ERR_PTR(ret
);
1953 static inline u8
btrfs_inode_type(struct inode
*inode
)
1955 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1958 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1959 struct dentry
*dentry
, struct inode
*inode
,
1963 struct btrfs_key key
;
1964 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1965 struct inode
*parent_inode
;
1967 key
.objectid
= inode
->i_ino
;
1968 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1971 ret
= btrfs_insert_dir_item(trans
, root
,
1972 dentry
->d_name
.name
, dentry
->d_name
.len
,
1973 dentry
->d_parent
->d_inode
->i_ino
,
1974 &key
, btrfs_inode_type(inode
));
1977 ret
= btrfs_insert_inode_ref(trans
, root
,
1978 dentry
->d_name
.name
,
1981 dentry
->d_parent
->d_inode
->i_ino
);
1983 parent_inode
= dentry
->d_parent
->d_inode
;
1984 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
1985 dentry
->d_name
.len
* 2);
1986 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1987 ret
= btrfs_update_inode(trans
, root
,
1988 dentry
->d_parent
->d_inode
);
1993 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1994 struct dentry
*dentry
, struct inode
*inode
,
1997 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1999 d_instantiate(dentry
, inode
);
2007 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2008 int mode
, dev_t rdev
)
2010 struct btrfs_trans_handle
*trans
;
2011 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2012 struct inode
*inode
= NULL
;
2016 unsigned long nr
= 0;
2018 if (!new_valid_dev(rdev
))
2021 err
= btrfs_check_free_space(root
, 1, 0);
2025 trans
= btrfs_start_transaction(root
, 1);
2026 btrfs_set_trans_block_group(trans
, dir
);
2028 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2034 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2036 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2037 BTRFS_I(dir
)->block_group
, mode
);
2038 err
= PTR_ERR(inode
);
2042 btrfs_set_trans_block_group(trans
, inode
);
2043 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2047 inode
->i_op
= &btrfs_special_inode_operations
;
2048 init_special_inode(inode
, inode
->i_mode
, rdev
);
2049 btrfs_update_inode(trans
, root
, inode
);
2051 dir
->i_sb
->s_dirt
= 1;
2052 btrfs_update_inode_block_group(trans
, inode
);
2053 btrfs_update_inode_block_group(trans
, dir
);
2055 nr
= trans
->blocks_used
;
2056 btrfs_end_transaction_throttle(trans
, root
);
2059 inode_dec_link_count(inode
);
2062 btrfs_btree_balance_dirty(root
, nr
);
2066 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2067 int mode
, struct nameidata
*nd
)
2069 struct btrfs_trans_handle
*trans
;
2070 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2071 struct inode
*inode
= NULL
;
2074 unsigned long nr
= 0;
2077 err
= btrfs_check_free_space(root
, 1, 0);
2080 trans
= btrfs_start_transaction(root
, 1);
2081 btrfs_set_trans_block_group(trans
, dir
);
2083 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2089 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2091 dentry
->d_parent
->d_inode
->i_ino
,
2092 objectid
, BTRFS_I(dir
)->block_group
, mode
);
2093 err
= PTR_ERR(inode
);
2097 btrfs_set_trans_block_group(trans
, inode
);
2098 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2102 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2103 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2104 inode
->i_fop
= &btrfs_file_operations
;
2105 inode
->i_op
= &btrfs_file_inode_operations
;
2106 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2107 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2108 inode
->i_mapping
, GFP_NOFS
);
2109 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2110 inode
->i_mapping
, GFP_NOFS
);
2111 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2112 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2113 BTRFS_I(inode
)->delalloc_bytes
= 0;
2114 BTRFS_I(inode
)->disk_i_size
= 0;
2115 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2116 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2118 dir
->i_sb
->s_dirt
= 1;
2119 btrfs_update_inode_block_group(trans
, inode
);
2120 btrfs_update_inode_block_group(trans
, dir
);
2122 nr
= trans
->blocks_used
;
2123 btrfs_end_transaction_throttle(trans
, root
);
2126 inode_dec_link_count(inode
);
2129 btrfs_btree_balance_dirty(root
, nr
);
2133 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2134 struct dentry
*dentry
)
2136 struct btrfs_trans_handle
*trans
;
2137 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2138 struct inode
*inode
= old_dentry
->d_inode
;
2139 unsigned long nr
= 0;
2143 if (inode
->i_nlink
== 0)
2146 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2151 err
= btrfs_check_free_space(root
, 1, 0);
2154 trans
= btrfs_start_transaction(root
, 1);
2156 btrfs_set_trans_block_group(trans
, dir
);
2157 atomic_inc(&inode
->i_count
);
2158 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2163 dir
->i_sb
->s_dirt
= 1;
2164 btrfs_update_inode_block_group(trans
, dir
);
2165 err
= btrfs_update_inode(trans
, root
, inode
);
2170 nr
= trans
->blocks_used
;
2171 btrfs_end_transaction_throttle(trans
, root
);
2174 inode_dec_link_count(inode
);
2177 btrfs_btree_balance_dirty(root
, nr
);
2181 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2183 struct inode
*inode
= NULL
;
2184 struct btrfs_trans_handle
*trans
;
2185 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2187 int drop_on_err
= 0;
2189 unsigned long nr
= 1;
2191 err
= btrfs_check_free_space(root
, 1, 0);
2195 trans
= btrfs_start_transaction(root
, 1);
2196 btrfs_set_trans_block_group(trans
, dir
);
2198 if (IS_ERR(trans
)) {
2199 err
= PTR_ERR(trans
);
2203 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2209 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2211 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2212 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2213 if (IS_ERR(inode
)) {
2214 err
= PTR_ERR(inode
);
2219 inode
->i_op
= &btrfs_dir_inode_operations
;
2220 inode
->i_fop
= &btrfs_dir_file_operations
;
2221 btrfs_set_trans_block_group(trans
, inode
);
2223 btrfs_i_size_write(inode
, 0);
2224 err
= btrfs_update_inode(trans
, root
, inode
);
2228 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2232 d_instantiate(dentry
, inode
);
2234 dir
->i_sb
->s_dirt
= 1;
2235 btrfs_update_inode_block_group(trans
, inode
);
2236 btrfs_update_inode_block_group(trans
, dir
);
2239 nr
= trans
->blocks_used
;
2240 btrfs_end_transaction_throttle(trans
, root
);
2245 btrfs_btree_balance_dirty(root
, nr
);
2249 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2250 struct extent_map
*existing
,
2251 struct extent_map
*em
,
2252 u64 map_start
, u64 map_len
)
2256 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2257 start_diff
= map_start
- em
->start
;
2258 em
->start
= map_start
;
2260 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2261 em
->block_start
+= start_diff
;
2262 return add_extent_mapping(em_tree
, em
);
2265 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2266 size_t pg_offset
, u64 start
, u64 len
,
2272 u64 extent_start
= 0;
2274 u64 objectid
= inode
->i_ino
;
2276 struct btrfs_path
*path
;
2277 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2278 struct btrfs_file_extent_item
*item
;
2279 struct extent_buffer
*leaf
;
2280 struct btrfs_key found_key
;
2281 struct extent_map
*em
= NULL
;
2282 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2283 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2284 struct btrfs_trans_handle
*trans
= NULL
;
2286 path
= btrfs_alloc_path();
2290 spin_lock(&em_tree
->lock
);
2291 em
= lookup_extent_mapping(em_tree
, start
, len
);
2293 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2294 spin_unlock(&em_tree
->lock
);
2297 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2298 free_extent_map(em
);
2299 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2300 free_extent_map(em
);
2304 em
= alloc_extent_map(GFP_NOFS
);
2309 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2310 em
->start
= EXTENT_MAP_HOLE
;
2312 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2313 objectid
, start
, trans
!= NULL
);
2320 if (path
->slots
[0] == 0)
2325 leaf
= path
->nodes
[0];
2326 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2327 struct btrfs_file_extent_item
);
2328 /* are we inside the extent that was found? */
2329 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2330 found_type
= btrfs_key_type(&found_key
);
2331 if (found_key
.objectid
!= objectid
||
2332 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2336 found_type
= btrfs_file_extent_type(leaf
, item
);
2337 extent_start
= found_key
.offset
;
2338 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2339 extent_end
= extent_start
+
2340 btrfs_file_extent_num_bytes(leaf
, item
);
2342 if (start
< extent_start
|| start
>= extent_end
) {
2344 if (start
< extent_start
) {
2345 if (start
+ len
<= extent_start
)
2347 em
->len
= extent_end
- extent_start
;
2353 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2355 em
->start
= extent_start
;
2356 em
->len
= extent_end
- extent_start
;
2357 em
->block_start
= EXTENT_MAP_HOLE
;
2360 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2361 em
->block_start
= bytenr
;
2362 em
->start
= extent_start
;
2363 em
->len
= extent_end
- extent_start
;
2365 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2370 size_t extent_offset
;
2373 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2375 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2376 ~((u64
)root
->sectorsize
- 1);
2377 if (start
< extent_start
|| start
>= extent_end
) {
2379 if (start
< extent_start
) {
2380 if (start
+ len
<= extent_start
)
2382 em
->len
= extent_end
- extent_start
;
2388 em
->block_start
= EXTENT_MAP_INLINE
;
2391 em
->start
= extent_start
;
2396 page_start
= page_offset(page
) + pg_offset
;
2397 extent_offset
= page_start
- extent_start
;
2398 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2399 size
- extent_offset
);
2400 em
->start
= extent_start
+ extent_offset
;
2401 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2402 ~((u64
)root
->sectorsize
- 1);
2404 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2405 if (create
== 0 && !PageUptodate(page
)) {
2406 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2408 flush_dcache_page(page
);
2409 } else if (create
&& PageUptodate(page
)) {
2412 free_extent_map(em
);
2414 btrfs_release_path(root
, path
);
2415 trans
= btrfs_join_transaction(root
, 1);
2418 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2420 btrfs_mark_buffer_dirty(leaf
);
2423 set_extent_uptodate(io_tree
, em
->start
,
2424 extent_map_end(em
) - 1, GFP_NOFS
);
2427 printk("unkknown found_type %d\n", found_type
);
2434 em
->block_start
= EXTENT_MAP_HOLE
;
2436 btrfs_release_path(root
, path
);
2437 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2438 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2444 spin_lock(&em_tree
->lock
);
2445 ret
= add_extent_mapping(em_tree
, em
);
2446 /* it is possible that someone inserted the extent into the tree
2447 * while we had the lock dropped. It is also possible that
2448 * an overlapping map exists in the tree
2450 if (ret
== -EEXIST
) {
2451 struct extent_map
*existing
;
2455 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2456 if (existing
&& (existing
->start
> start
||
2457 existing
->start
+ existing
->len
<= start
)) {
2458 free_extent_map(existing
);
2462 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2465 err
= merge_extent_mapping(em_tree
, existing
,
2468 free_extent_map(existing
);
2470 free_extent_map(em
);
2475 printk("failing to insert %Lu %Lu\n",
2477 free_extent_map(em
);
2481 free_extent_map(em
);
2486 spin_unlock(&em_tree
->lock
);
2488 btrfs_free_path(path
);
2490 ret
= btrfs_end_transaction(trans
, root
);
2496 free_extent_map(em
);
2498 return ERR_PTR(err
);
2503 #if 0 /* waiting for O_DIRECT reads */
2504 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2505 struct buffer_head
*bh_result
, int create
)
2507 struct extent_map
*em
;
2508 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2509 struct btrfs_multi_bio
*multi
= NULL
;
2510 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2516 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2518 if (!em
|| IS_ERR(em
))
2521 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2525 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2530 len
= em
->start
+ em
->len
- start
;
2531 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2533 if (em
->block_start
== EXTENT_MAP_HOLE
||
2534 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2535 bh_result
->b_size
= len
;
2539 logical
= start
- em
->start
;
2540 logical
= em
->block_start
+ logical
;
2543 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2544 logical
, &map_length
, &multi
, 0);
2546 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2547 bh_result
->b_size
= min(map_length
, len
);
2549 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2550 set_buffer_mapped(bh_result
);
2553 free_extent_map(em
);
2558 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2559 const struct iovec
*iov
, loff_t offset
,
2560 unsigned long nr_segs
)
2564 struct file
*file
= iocb
->ki_filp
;
2565 struct inode
*inode
= file
->f_mapping
->host
;
2570 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2571 offset
, nr_segs
, btrfs_get_block
, NULL
);
2575 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2577 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2580 int btrfs_readpage(struct file
*file
, struct page
*page
)
2582 struct extent_io_tree
*tree
;
2583 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2584 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2587 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2589 struct extent_io_tree
*tree
;
2592 if (current
->flags
& PF_MEMALLOC
) {
2593 redirty_page_for_writepage(wbc
, page
);
2597 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2598 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2601 static int btrfs_writepages(struct address_space
*mapping
,
2602 struct writeback_control
*wbc
)
2604 struct extent_io_tree
*tree
;
2605 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2606 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2610 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2611 struct list_head
*pages
, unsigned nr_pages
)
2613 struct extent_io_tree
*tree
;
2614 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2615 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2618 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2620 struct extent_io_tree
*tree
;
2621 struct extent_map_tree
*map
;
2624 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2625 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2626 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2628 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
2629 ClearPagePrivate(page
);
2630 set_page_private(page
, 0);
2631 page_cache_release(page
);
2636 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2638 return __btrfs_releasepage(page
, gfp_flags
);
2641 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2643 struct extent_io_tree
*tree
;
2644 struct btrfs_ordered_extent
*ordered
;
2645 u64 page_start
= page_offset(page
);
2646 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2648 wait_on_page_writeback(page
);
2649 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2651 btrfs_releasepage(page
, GFP_NOFS
);
2655 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2656 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2660 * IO on this page will never be started, so we need
2661 * to account for any ordered extents now
2663 clear_extent_bit(tree
, page_start
, page_end
,
2664 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2665 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2666 btrfs_writepage_end_io_hook(page
, page_start
,
2668 btrfs_put_ordered_extent(ordered
);
2669 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2671 clear_extent_bit(tree
, page_start
, page_end
,
2672 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2675 __btrfs_releasepage(page
, GFP_NOFS
);
2677 if (PagePrivate(page
)) {
2678 invalidate_extent_lru(tree
, page_offset(page
),
2680 ClearPagePrivate(page
);
2681 set_page_private(page
, 0);
2682 page_cache_release(page
);
2687 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2688 * called from a page fault handler when a page is first dirtied. Hence we must
2689 * be careful to check for EOF conditions here. We set the page up correctly
2690 * for a written page which means we get ENOSPC checking when writing into
2691 * holes and correct delalloc and unwritten extent mapping on filesystems that
2692 * support these features.
2694 * We are not allowed to take the i_mutex here so we have to play games to
2695 * protect against truncate races as the page could now be beyond EOF. Because
2696 * vmtruncate() writes the inode size before removing pages, once we have the
2697 * page lock we can determine safely if the page is beyond EOF. If it is not
2698 * beyond EOF, then the page is guaranteed safe against truncation until we
2701 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2703 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2704 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2705 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2706 struct btrfs_ordered_extent
*ordered
;
2708 unsigned long zero_start
;
2714 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2721 size
= i_size_read(inode
);
2722 page_start
= page_offset(page
);
2723 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2725 if ((page
->mapping
!= inode
->i_mapping
) ||
2726 (page_start
>= size
)) {
2727 /* page got truncated out from underneath us */
2730 wait_on_page_writeback(page
);
2732 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2733 set_page_extent_mapped(page
);
2736 * we can't set the delalloc bits if there are pending ordered
2737 * extents. Drop our locks and wait for them to finish
2739 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2741 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2743 btrfs_start_ordered_extent(inode
, ordered
, 1);
2744 btrfs_put_ordered_extent(ordered
);
2748 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
2749 page_end
, GFP_NOFS
);
2752 /* page is wholly or partially inside EOF */
2753 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2754 zero_start
= size
& ~PAGE_CACHE_MASK
;
2756 zero_start
= PAGE_CACHE_SIZE
;
2758 if (zero_start
!= PAGE_CACHE_SIZE
) {
2760 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
2761 flush_dcache_page(page
);
2764 ClearPageChecked(page
);
2765 set_page_dirty(page
);
2766 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2774 static void btrfs_truncate(struct inode
*inode
)
2776 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2778 struct btrfs_trans_handle
*trans
;
2780 u64 mask
= root
->sectorsize
- 1;
2782 if (!S_ISREG(inode
->i_mode
))
2784 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2787 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2789 trans
= btrfs_start_transaction(root
, 1);
2790 btrfs_set_trans_block_group(trans
, inode
);
2791 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
2792 btrfs_i_size_write(inode
, inode
->i_size
);
2794 /* FIXME, add redo link to tree so we don't leak on crash */
2795 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2796 BTRFS_EXTENT_DATA_KEY
);
2797 btrfs_update_inode(trans
, root
, inode
);
2798 nr
= trans
->blocks_used
;
2800 ret
= btrfs_end_transaction_throttle(trans
, root
);
2802 btrfs_btree_balance_dirty(root
, nr
);
2806 * Invalidate a single dcache entry at the root of the filesystem.
2807 * Needed after creation of snapshot or subvolume.
2809 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
2812 struct dentry
*alias
, *entry
;
2815 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
2819 /* change me if btrfs ever gets a d_hash operation */
2820 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
2821 entry
= d_lookup(alias
, &qstr
);
2824 d_invalidate(entry
);
2830 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
2831 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
2832 struct btrfs_block_group_cache
*block_group
)
2834 struct inode
*inode
;
2837 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2838 new_dirid
, block_group
, S_IFDIR
| 0700);
2840 return PTR_ERR(inode
);
2841 inode
->i_op
= &btrfs_dir_inode_operations
;
2842 inode
->i_fop
= &btrfs_dir_file_operations
;
2843 new_root
->inode
= inode
;
2845 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2848 btrfs_i_size_write(inode
, 0);
2850 return btrfs_update_inode(trans
, new_root
, inode
);
2853 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2854 struct file_ra_state
*ra
, struct file
*file
,
2855 pgoff_t offset
, pgoff_t last_index
)
2857 pgoff_t req_size
= last_index
- offset
+ 1;
2859 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2860 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2863 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2864 return offset
+ req_size
;
2868 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2870 struct btrfs_inode
*ei
;
2872 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2876 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
2877 return &ei
->vfs_inode
;
2880 void btrfs_destroy_inode(struct inode
*inode
)
2882 struct btrfs_ordered_extent
*ordered
;
2883 WARN_ON(!list_empty(&inode
->i_dentry
));
2884 WARN_ON(inode
->i_data
.nrpages
);
2887 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
2891 printk("found ordered extent %Lu %Lu\n",
2892 ordered
->file_offset
, ordered
->len
);
2893 btrfs_remove_ordered_extent(inode
, ordered
);
2894 btrfs_put_ordered_extent(ordered
);
2895 btrfs_put_ordered_extent(ordered
);
2898 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2899 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2902 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2903 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2905 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2906 unsigned long flags
)
2909 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2911 inode_init_once(&ei
->vfs_inode
);
2914 void btrfs_destroy_cachep(void)
2916 if (btrfs_inode_cachep
)
2917 kmem_cache_destroy(btrfs_inode_cachep
);
2918 if (btrfs_trans_handle_cachep
)
2919 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2920 if (btrfs_transaction_cachep
)
2921 kmem_cache_destroy(btrfs_transaction_cachep
);
2922 if (btrfs_bit_radix_cachep
)
2923 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2924 if (btrfs_path_cachep
)
2925 kmem_cache_destroy(btrfs_path_cachep
);
2928 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2929 unsigned long extra_flags
,
2930 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2931 void (*ctor
)(struct kmem_cache
*, void *)
2933 void (*ctor
)(void *, struct kmem_cache
*,
2938 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2939 SLAB_MEM_SPREAD
| extra_flags
), ctor
2940 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2946 int btrfs_init_cachep(void)
2948 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2949 sizeof(struct btrfs_inode
),
2951 if (!btrfs_inode_cachep
)
2953 btrfs_trans_handle_cachep
=
2954 btrfs_cache_create("btrfs_trans_handle_cache",
2955 sizeof(struct btrfs_trans_handle
),
2957 if (!btrfs_trans_handle_cachep
)
2959 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2960 sizeof(struct btrfs_transaction
),
2962 if (!btrfs_transaction_cachep
)
2964 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2965 sizeof(struct btrfs_path
),
2967 if (!btrfs_path_cachep
)
2969 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2970 SLAB_DESTROY_BY_RCU
, NULL
);
2971 if (!btrfs_bit_radix_cachep
)
2975 btrfs_destroy_cachep();
2979 static int btrfs_getattr(struct vfsmount
*mnt
,
2980 struct dentry
*dentry
, struct kstat
*stat
)
2982 struct inode
*inode
= dentry
->d_inode
;
2983 generic_fillattr(inode
, stat
);
2984 stat
->blksize
= PAGE_CACHE_SIZE
;
2985 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
2989 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2990 struct inode
* new_dir
,struct dentry
*new_dentry
)
2992 struct btrfs_trans_handle
*trans
;
2993 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2994 struct inode
*new_inode
= new_dentry
->d_inode
;
2995 struct inode
*old_inode
= old_dentry
->d_inode
;
2996 struct timespec ctime
= CURRENT_TIME
;
2999 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3000 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3004 ret
= btrfs_check_free_space(root
, 1, 0);
3008 trans
= btrfs_start_transaction(root
, 1);
3010 btrfs_set_trans_block_group(trans
, new_dir
);
3012 old_dentry
->d_inode
->i_nlink
++;
3013 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3014 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3015 old_inode
->i_ctime
= ctime
;
3017 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3022 new_inode
->i_ctime
= CURRENT_TIME
;
3023 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3027 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3032 btrfs_end_transaction(trans
, root
);
3037 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3038 const char *symname
)
3040 struct btrfs_trans_handle
*trans
;
3041 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3042 struct btrfs_path
*path
;
3043 struct btrfs_key key
;
3044 struct inode
*inode
= NULL
;
3051 struct btrfs_file_extent_item
*ei
;
3052 struct extent_buffer
*leaf
;
3053 unsigned long nr
= 0;
3055 name_len
= strlen(symname
) + 1;
3056 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3057 return -ENAMETOOLONG
;
3059 err
= btrfs_check_free_space(root
, 1, 0);
3063 trans
= btrfs_start_transaction(root
, 1);
3064 btrfs_set_trans_block_group(trans
, dir
);
3066 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3072 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
3074 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3075 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3076 err
= PTR_ERR(inode
);
3080 btrfs_set_trans_block_group(trans
, inode
);
3081 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3085 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3086 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3087 inode
->i_fop
= &btrfs_file_operations
;
3088 inode
->i_op
= &btrfs_file_inode_operations
;
3089 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3090 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3091 inode
->i_mapping
, GFP_NOFS
);
3092 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3093 inode
->i_mapping
, GFP_NOFS
);
3094 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3095 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3096 BTRFS_I(inode
)->delalloc_bytes
= 0;
3097 BTRFS_I(inode
)->disk_i_size
= 0;
3098 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3099 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3101 dir
->i_sb
->s_dirt
= 1;
3102 btrfs_update_inode_block_group(trans
, inode
);
3103 btrfs_update_inode_block_group(trans
, dir
);
3107 path
= btrfs_alloc_path();
3109 key
.objectid
= inode
->i_ino
;
3111 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3112 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3113 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3119 leaf
= path
->nodes
[0];
3120 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3121 struct btrfs_file_extent_item
);
3122 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3123 btrfs_set_file_extent_type(leaf
, ei
,
3124 BTRFS_FILE_EXTENT_INLINE
);
3125 ptr
= btrfs_file_extent_inline_start(ei
);
3126 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3127 btrfs_mark_buffer_dirty(leaf
);
3128 btrfs_free_path(path
);
3130 inode
->i_op
= &btrfs_symlink_inode_operations
;
3131 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3132 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3133 btrfs_i_size_write(inode
, name_len
- 1);
3134 err
= btrfs_update_inode(trans
, root
, inode
);
3139 nr
= trans
->blocks_used
;
3140 btrfs_end_transaction_throttle(trans
, root
);
3143 inode_dec_link_count(inode
);
3146 btrfs_btree_balance_dirty(root
, nr
);
3150 static int btrfs_set_page_dirty(struct page
*page
)
3152 return __set_page_dirty_nobuffers(page
);
3155 static int btrfs_permission(struct inode
*inode
, int mask
,
3156 struct nameidata
*nd
)
3158 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3160 return generic_permission(inode
, mask
, NULL
);
3163 static struct inode_operations btrfs_dir_inode_operations
= {
3164 .lookup
= btrfs_lookup
,
3165 .create
= btrfs_create
,
3166 .unlink
= btrfs_unlink
,
3168 .mkdir
= btrfs_mkdir
,
3169 .rmdir
= btrfs_rmdir
,
3170 .rename
= btrfs_rename
,
3171 .symlink
= btrfs_symlink
,
3172 .setattr
= btrfs_setattr
,
3173 .mknod
= btrfs_mknod
,
3174 .setxattr
= generic_setxattr
,
3175 .getxattr
= generic_getxattr
,
3176 .listxattr
= btrfs_listxattr
,
3177 .removexattr
= generic_removexattr
,
3178 .permission
= btrfs_permission
,
3180 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3181 .lookup
= btrfs_lookup
,
3182 .permission
= btrfs_permission
,
3184 static struct file_operations btrfs_dir_file_operations
= {
3185 .llseek
= generic_file_llseek
,
3186 .read
= generic_read_dir
,
3187 .readdir
= btrfs_readdir
,
3188 .unlocked_ioctl
= btrfs_ioctl
,
3189 #ifdef CONFIG_COMPAT
3190 .compat_ioctl
= btrfs_ioctl
,
3192 .release
= btrfs_release_file
,
3195 static struct extent_io_ops btrfs_extent_io_ops
= {
3196 .fill_delalloc
= run_delalloc_range
,
3197 .submit_bio_hook
= btrfs_submit_bio_hook
,
3198 .merge_bio_hook
= btrfs_merge_bio_hook
,
3199 .readpage_io_hook
= btrfs_readpage_io_hook
,
3200 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3201 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3202 .writepage_start_hook
= btrfs_writepage_start_hook
,
3203 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3204 .set_bit_hook
= btrfs_set_bit_hook
,
3205 .clear_bit_hook
= btrfs_clear_bit_hook
,
3208 static struct address_space_operations btrfs_aops
= {
3209 .readpage
= btrfs_readpage
,
3210 .writepage
= btrfs_writepage
,
3211 .writepages
= btrfs_writepages
,
3212 .readpages
= btrfs_readpages
,
3213 .sync_page
= block_sync_page
,
3215 .direct_IO
= btrfs_direct_IO
,
3216 .invalidatepage
= btrfs_invalidatepage
,
3217 .releasepage
= btrfs_releasepage
,
3218 .set_page_dirty
= btrfs_set_page_dirty
,
3221 static struct address_space_operations btrfs_symlink_aops
= {
3222 .readpage
= btrfs_readpage
,
3223 .writepage
= btrfs_writepage
,
3224 .invalidatepage
= btrfs_invalidatepage
,
3225 .releasepage
= btrfs_releasepage
,
3228 static struct inode_operations btrfs_file_inode_operations
= {
3229 .truncate
= btrfs_truncate
,
3230 .getattr
= btrfs_getattr
,
3231 .setattr
= btrfs_setattr
,
3232 .setxattr
= generic_setxattr
,
3233 .getxattr
= generic_getxattr
,
3234 .listxattr
= btrfs_listxattr
,
3235 .removexattr
= generic_removexattr
,
3236 .permission
= btrfs_permission
,
3238 static struct inode_operations btrfs_special_inode_operations
= {
3239 .getattr
= btrfs_getattr
,
3240 .setattr
= btrfs_setattr
,
3241 .permission
= btrfs_permission
,
3243 static struct inode_operations btrfs_symlink_inode_operations
= {
3244 .readlink
= generic_readlink
,
3245 .follow_link
= page_follow_link_light
,
3246 .put_link
= page_put_link
,
3247 .permission
= btrfs_permission
,