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_start_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
;
354 struct btrfs_ordered_sum
*sums
;
356 ret
= btrfs_csum_one_bio(root
, bio
, &sums
);
359 ret
= btrfs_add_ordered_sum(inode
, sums
);
362 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
365 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
368 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
371 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
374 if (!(rw
& (1 << BIO_RW
))) {
378 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
379 inode
, rw
, bio
, mirror_num
,
380 __btrfs_submit_bio_hook
);
382 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
385 static int add_pending_csums(struct btrfs_trans_handle
*trans
,
386 struct inode
*inode
, u64 file_offset
,
387 struct list_head
*list
)
389 struct list_head
*cur
;
390 struct btrfs_ordered_sum
*sum
;
392 btrfs_set_trans_block_group(trans
, inode
);
393 while(!list_empty(list
)) {
395 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
396 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
397 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
399 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
400 list_del(&sum
->list
);
406 struct btrfs_writepage_fixup
{
408 struct btrfs_work work
;
411 /* see btrfs_writepage_start_hook for details on why this is required */
412 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
414 struct btrfs_writepage_fixup
*fixup
;
415 struct btrfs_ordered_extent
*ordered
;
421 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
425 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
426 ClearPageChecked(page
);
430 inode
= page
->mapping
->host
;
431 page_start
= page_offset(page
);
432 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
434 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
435 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
439 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
441 ClearPageChecked(page
);
443 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
446 page_cache_release(page
);
450 * There are a few paths in the higher layers of the kernel that directly
451 * set the page dirty bit without asking the filesystem if it is a
452 * good idea. This causes problems because we want to make sure COW
453 * properly happens and the data=ordered rules are followed.
455 * In our case any range that doesn't have the EXTENT_ORDERED bit set
456 * hasn't been properly setup for IO. We kick off an async process
457 * to fix it up. The async helper will wait for ordered extents, set
458 * the delalloc bit and make it safe to write the page.
460 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
462 struct inode
*inode
= page
->mapping
->host
;
463 struct btrfs_writepage_fixup
*fixup
;
464 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
467 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
472 if (PageChecked(page
))
475 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
478 printk("queueing worker to fixup page %lu %Lu\n", inode
->i_ino
, page_offset(page
));
479 SetPageChecked(page
);
480 page_cache_get(page
);
481 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
483 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
487 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
488 struct extent_state
*state
, int uptodate
)
490 struct inode
*inode
= page
->mapping
->host
;
491 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
492 struct btrfs_trans_handle
*trans
;
493 struct btrfs_ordered_extent
*ordered_extent
;
494 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
496 struct list_head list
;
497 struct btrfs_key ins
;
500 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
505 trans
= btrfs_start_transaction(root
, 1);
507 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
508 BUG_ON(!ordered_extent
);
510 lock_extent(io_tree
, ordered_extent
->file_offset
,
511 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
514 INIT_LIST_HEAD(&list
);
516 ins
.objectid
= ordered_extent
->start
;
517 ins
.offset
= ordered_extent
->len
;
518 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
519 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
520 trans
->transid
, inode
->i_ino
,
521 ordered_extent
->file_offset
, &ins
);
523 ret
= btrfs_drop_extents(trans
, root
, inode
,
524 ordered_extent
->file_offset
,
525 ordered_extent
->file_offset
+
527 ordered_extent
->file_offset
, &alloc_hint
);
529 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
530 ordered_extent
->file_offset
,
531 ordered_extent
->start
,
533 ordered_extent
->len
, 0);
535 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
536 ordered_extent
->file_offset
+
537 ordered_extent
->len
- 1);
538 inode
->i_blocks
+= ordered_extent
->len
>> 9;
539 unlock_extent(io_tree
, ordered_extent
->file_offset
,
540 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
542 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
543 &ordered_extent
->list
);
545 btrfs_ordered_update_i_size(inode
, ordered_extent
);
546 btrfs_remove_ordered_extent(inode
, ordered_extent
);
548 btrfs_put_ordered_extent(ordered_extent
);
549 /* once for the tree */
550 btrfs_put_ordered_extent(ordered_extent
);
552 btrfs_update_inode(trans
, root
, inode
);
553 btrfs_end_transaction(trans
, root
);
557 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
560 struct inode
*inode
= page
->mapping
->host
;
561 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
562 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
563 struct btrfs_csum_item
*item
;
564 struct btrfs_path
*path
= NULL
;
567 if (btrfs_test_opt(root
, NODATASUM
) ||
568 btrfs_test_flag(inode
, NODATASUM
))
571 path
= btrfs_alloc_path();
572 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
575 /* a csum that isn't present is a preallocated region. */
576 if (ret
== -ENOENT
|| ret
== -EFBIG
)
579 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
583 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
585 set_state_private(io_tree
, start
, csum
);
588 btrfs_free_path(path
);
592 struct io_failure_record
{
600 int btrfs_io_failed_hook(struct bio
*failed_bio
,
601 struct page
*page
, u64 start
, u64 end
,
602 struct extent_state
*state
)
604 struct io_failure_record
*failrec
= NULL
;
606 struct extent_map
*em
;
607 struct inode
*inode
= page
->mapping
->host
;
608 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
609 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
616 ret
= get_state_private(failure_tree
, start
, &private);
618 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
621 failrec
->start
= start
;
622 failrec
->len
= end
- start
+ 1;
623 failrec
->last_mirror
= 0;
625 spin_lock(&em_tree
->lock
);
626 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
627 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
631 spin_unlock(&em_tree
->lock
);
633 if (!em
|| IS_ERR(em
)) {
637 logical
= start
- em
->start
;
638 logical
= em
->block_start
+ logical
;
639 failrec
->logical
= logical
;
641 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
642 EXTENT_DIRTY
, GFP_NOFS
);
643 set_state_private(failure_tree
, start
,
644 (u64
)(unsigned long)failrec
);
646 failrec
= (struct io_failure_record
*)(unsigned long)private;
648 num_copies
= btrfs_num_copies(
649 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
650 failrec
->logical
, failrec
->len
);
651 failrec
->last_mirror
++;
653 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
654 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
657 if (state
&& state
->start
!= failrec
->start
)
659 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
661 if (!state
|| failrec
->last_mirror
> num_copies
) {
662 set_state_private(failure_tree
, failrec
->start
, 0);
663 clear_extent_bits(failure_tree
, failrec
->start
,
664 failrec
->start
+ failrec
->len
- 1,
665 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
669 bio
= bio_alloc(GFP_NOFS
, 1);
670 bio
->bi_private
= state
;
671 bio
->bi_end_io
= failed_bio
->bi_end_io
;
672 bio
->bi_sector
= failrec
->logical
>> 9;
673 bio
->bi_bdev
= failed_bio
->bi_bdev
;
675 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
676 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
681 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
682 failrec
->last_mirror
);
686 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
690 struct io_failure_record
*failure
;
694 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
695 (u64
)-1, 1, EXTENT_DIRTY
)) {
696 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
697 start
, &private_failure
);
699 failure
= (struct io_failure_record
*)(unsigned long)
701 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
703 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
705 failure
->start
+ failure
->len
- 1,
706 EXTENT_DIRTY
| EXTENT_LOCKED
,
714 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
715 struct extent_state
*state
)
717 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
718 struct inode
*inode
= page
->mapping
->host
;
719 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
721 u64
private = ~(u32
)0;
723 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
727 if (btrfs_test_opt(root
, NODATASUM
) ||
728 btrfs_test_flag(inode
, NODATASUM
))
730 if (state
&& state
->start
== start
) {
731 private = state
->private;
734 ret
= get_state_private(io_tree
, start
, &private);
736 local_irq_save(flags
);
737 kaddr
= kmap_atomic(page
, KM_IRQ0
);
741 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
742 btrfs_csum_final(csum
, (char *)&csum
);
743 if (csum
!= private) {
746 kunmap_atomic(kaddr
, KM_IRQ0
);
747 local_irq_restore(flags
);
749 /* if the io failure tree for this inode is non-empty,
750 * check to see if we've recovered from a failed IO
752 btrfs_clean_io_failures(inode
, start
);
756 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
757 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
759 memset(kaddr
+ offset
, 1, end
- start
+ 1);
760 flush_dcache_page(page
);
761 kunmap_atomic(kaddr
, KM_IRQ0
);
762 local_irq_restore(flags
);
768 void btrfs_read_locked_inode(struct inode
*inode
)
770 struct btrfs_path
*path
;
771 struct extent_buffer
*leaf
;
772 struct btrfs_inode_item
*inode_item
;
773 struct btrfs_timespec
*tspec
;
774 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
775 struct btrfs_key location
;
776 u64 alloc_group_block
;
780 path
= btrfs_alloc_path();
782 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
784 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
788 leaf
= path
->nodes
[0];
789 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
790 struct btrfs_inode_item
);
792 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
793 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
794 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
795 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
796 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
798 tspec
= btrfs_inode_atime(inode_item
);
799 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
800 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
802 tspec
= btrfs_inode_mtime(inode_item
);
803 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
804 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
806 tspec
= btrfs_inode_ctime(inode_item
);
807 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
808 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
810 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
811 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
813 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
815 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
816 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
818 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
819 if (!BTRFS_I(inode
)->block_group
) {
820 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
822 BTRFS_BLOCK_GROUP_METADATA
, 0);
824 btrfs_free_path(path
);
827 switch (inode
->i_mode
& S_IFMT
) {
829 inode
->i_mapping
->a_ops
= &btrfs_aops
;
830 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
831 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
832 inode
->i_fop
= &btrfs_file_operations
;
833 inode
->i_op
= &btrfs_file_inode_operations
;
836 inode
->i_fop
= &btrfs_dir_file_operations
;
837 if (root
== root
->fs_info
->tree_root
)
838 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
840 inode
->i_op
= &btrfs_dir_inode_operations
;
843 inode
->i_op
= &btrfs_symlink_inode_operations
;
844 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
845 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
848 init_special_inode(inode
, inode
->i_mode
, rdev
);
854 btrfs_free_path(path
);
855 make_bad_inode(inode
);
858 static void fill_inode_item(struct extent_buffer
*leaf
,
859 struct btrfs_inode_item
*item
,
862 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
863 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
864 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
865 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
866 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
868 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
869 inode
->i_atime
.tv_sec
);
870 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
871 inode
->i_atime
.tv_nsec
);
873 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
874 inode
->i_mtime
.tv_sec
);
875 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
876 inode
->i_mtime
.tv_nsec
);
878 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
879 inode
->i_ctime
.tv_sec
);
880 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
881 inode
->i_ctime
.tv_nsec
);
883 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
884 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
885 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
886 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
887 btrfs_set_inode_block_group(leaf
, item
,
888 BTRFS_I(inode
)->block_group
->key
.objectid
);
891 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
892 struct btrfs_root
*root
,
895 struct btrfs_inode_item
*inode_item
;
896 struct btrfs_path
*path
;
897 struct extent_buffer
*leaf
;
900 path
= btrfs_alloc_path();
902 ret
= btrfs_lookup_inode(trans
, root
, path
,
903 &BTRFS_I(inode
)->location
, 1);
910 leaf
= path
->nodes
[0];
911 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
912 struct btrfs_inode_item
);
914 fill_inode_item(leaf
, inode_item
, inode
);
915 btrfs_mark_buffer_dirty(leaf
);
916 btrfs_set_inode_last_trans(trans
, inode
);
919 btrfs_free_path(path
);
924 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
925 struct btrfs_root
*root
,
927 struct dentry
*dentry
)
929 struct btrfs_path
*path
;
930 const char *name
= dentry
->d_name
.name
;
931 int name_len
= dentry
->d_name
.len
;
933 struct extent_buffer
*leaf
;
934 struct btrfs_dir_item
*di
;
935 struct btrfs_key key
;
937 path
= btrfs_alloc_path();
943 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
953 leaf
= path
->nodes
[0];
954 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
955 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
958 btrfs_release_path(root
, path
);
960 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
961 key
.objectid
, name
, name_len
, -1);
970 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
971 btrfs_release_path(root
, path
);
973 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
974 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
975 dentry
->d_inode
->i_ino
,
976 dentry
->d_parent
->d_inode
->i_ino
);
978 printk("failed to delete reference to %.*s, "
979 "inode %lu parent %lu\n", name_len
, name
,
980 dentry
->d_inode
->i_ino
,
981 dentry
->d_parent
->d_inode
->i_ino
);
984 btrfs_free_path(path
);
986 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
987 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
988 btrfs_update_inode(trans
, root
, dir
);
989 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
990 dentry
->d_inode
->i_nlink
--;
992 drop_nlink(dentry
->d_inode
);
994 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
995 dir
->i_sb
->s_dirt
= 1;
1000 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1002 struct btrfs_root
*root
;
1003 struct btrfs_trans_handle
*trans
;
1005 unsigned long nr
= 0;
1007 root
= BTRFS_I(dir
)->root
;
1009 ret
= btrfs_check_free_space(root
, 1, 1);
1013 trans
= btrfs_start_transaction(root
, 1);
1015 btrfs_set_trans_block_group(trans
, dir
);
1016 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1017 nr
= trans
->blocks_used
;
1019 btrfs_end_transaction_throttle(trans
, root
);
1021 btrfs_btree_balance_dirty(root
, nr
);
1025 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1027 struct inode
*inode
= dentry
->d_inode
;
1030 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1031 struct btrfs_trans_handle
*trans
;
1032 unsigned long nr
= 0;
1034 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1038 ret
= btrfs_check_free_space(root
, 1, 1);
1042 trans
= btrfs_start_transaction(root
, 1);
1043 btrfs_set_trans_block_group(trans
, dir
);
1045 /* now the directory is empty */
1046 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1048 btrfs_i_size_write(inode
, 0);
1051 nr
= trans
->blocks_used
;
1052 ret
= btrfs_end_transaction_throttle(trans
, root
);
1054 btrfs_btree_balance_dirty(root
, nr
);
1062 * this can truncate away extent items, csum items and directory items.
1063 * It starts at a high offset and removes keys until it can't find
1064 * any higher than i_size.
1066 * csum items that cross the new i_size are truncated to the new size
1069 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1070 struct btrfs_root
*root
,
1071 struct inode
*inode
,
1075 struct btrfs_path
*path
;
1076 struct btrfs_key key
;
1077 struct btrfs_key found_key
;
1079 struct extent_buffer
*leaf
;
1080 struct btrfs_file_extent_item
*fi
;
1081 u64 extent_start
= 0;
1082 u64 extent_num_bytes
= 0;
1088 int pending_del_nr
= 0;
1089 int pending_del_slot
= 0;
1090 int extent_type
= -1;
1091 u64 mask
= root
->sectorsize
- 1;
1093 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1094 path
= btrfs_alloc_path();
1098 /* FIXME, add redo link to tree so we don't leak on crash */
1099 key
.objectid
= inode
->i_ino
;
1100 key
.offset
= (u64
)-1;
1103 btrfs_init_path(path
);
1105 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1110 BUG_ON(path
->slots
[0] == 0);
1116 leaf
= path
->nodes
[0];
1117 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1118 found_type
= btrfs_key_type(&found_key
);
1120 if (found_key
.objectid
!= inode
->i_ino
)
1123 if (found_type
< min_type
)
1126 item_end
= found_key
.offset
;
1127 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1128 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1129 struct btrfs_file_extent_item
);
1130 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1131 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1133 btrfs_file_extent_num_bytes(leaf
, fi
);
1134 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1135 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1137 item_end
+= btrfs_file_extent_inline_len(leaf
,
1142 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1143 ret
= btrfs_csum_truncate(trans
, root
, path
,
1147 if (item_end
< inode
->i_size
) {
1148 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1149 found_type
= BTRFS_INODE_ITEM_KEY
;
1150 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1151 found_type
= BTRFS_CSUM_ITEM_KEY
;
1152 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1153 found_type
= BTRFS_XATTR_ITEM_KEY
;
1154 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1155 found_type
= BTRFS_INODE_REF_KEY
;
1156 } else if (found_type
) {
1161 btrfs_set_key_type(&key
, found_type
);
1164 if (found_key
.offset
>= inode
->i_size
)
1170 /* FIXME, shrink the extent if the ref count is only 1 */
1171 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1174 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1176 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1178 u64 orig_num_bytes
=
1179 btrfs_file_extent_num_bytes(leaf
, fi
);
1180 extent_num_bytes
= inode
->i_size
-
1181 found_key
.offset
+ root
->sectorsize
- 1;
1182 extent_num_bytes
= extent_num_bytes
&
1183 ~((u64
)root
->sectorsize
- 1);
1184 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1186 num_dec
= (orig_num_bytes
-
1188 if (extent_start
!= 0)
1189 dec_i_blocks(inode
, num_dec
);
1190 btrfs_mark_buffer_dirty(leaf
);
1193 btrfs_file_extent_disk_num_bytes(leaf
,
1195 /* FIXME blocksize != 4096 */
1196 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1197 if (extent_start
!= 0) {
1199 dec_i_blocks(inode
, num_dec
);
1201 root_gen
= btrfs_header_generation(leaf
);
1202 root_owner
= btrfs_header_owner(leaf
);
1204 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1206 u32 newsize
= inode
->i_size
- found_key
.offset
;
1207 dec_i_blocks(inode
, item_end
+ 1 -
1208 found_key
.offset
- newsize
);
1210 btrfs_file_extent_calc_inline_size(newsize
);
1211 ret
= btrfs_truncate_item(trans
, root
, path
,
1215 dec_i_blocks(inode
, item_end
+ 1 -
1221 if (!pending_del_nr
) {
1222 /* no pending yet, add ourselves */
1223 pending_del_slot
= path
->slots
[0];
1225 } else if (pending_del_nr
&&
1226 path
->slots
[0] + 1 == pending_del_slot
) {
1227 /* hop on the pending chunk */
1229 pending_del_slot
= path
->slots
[0];
1231 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1237 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1240 root_gen
, inode
->i_ino
,
1241 found_key
.offset
, 0);
1245 if (path
->slots
[0] == 0) {
1248 btrfs_release_path(root
, path
);
1253 if (pending_del_nr
&&
1254 path
->slots
[0] + 1 != pending_del_slot
) {
1255 struct btrfs_key debug
;
1257 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1259 ret
= btrfs_del_items(trans
, root
, path
,
1264 btrfs_release_path(root
, path
);
1270 if (pending_del_nr
) {
1271 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1274 btrfs_free_path(path
);
1275 inode
->i_sb
->s_dirt
= 1;
1280 * taken from block_truncate_page, but does cow as it zeros out
1281 * any bytes left in the last page in the file.
1283 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1285 struct inode
*inode
= mapping
->host
;
1286 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1287 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1288 struct btrfs_ordered_extent
*ordered
;
1290 u32 blocksize
= root
->sectorsize
;
1291 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1292 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1298 if ((offset
& (blocksize
- 1)) == 0)
1303 page
= grab_cache_page(mapping
, index
);
1307 page_start
= page_offset(page
);
1308 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1310 if (!PageUptodate(page
)) {
1311 ret
= btrfs_readpage(NULL
, page
);
1313 if (page
->mapping
!= mapping
) {
1315 page_cache_release(page
);
1318 if (!PageUptodate(page
)) {
1323 wait_on_page_writeback(page
);
1325 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1326 set_page_extent_mapped(page
);
1328 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1330 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1332 page_cache_release(page
);
1333 btrfs_wait_ordered_extent(inode
, ordered
);
1334 btrfs_put_ordered_extent(ordered
);
1338 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1339 page_end
, GFP_NOFS
);
1341 if (offset
!= PAGE_CACHE_SIZE
) {
1343 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1344 flush_dcache_page(page
);
1347 ClearPageChecked(page
);
1348 set_page_dirty(page
);
1349 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1352 page_cache_release(page
);
1357 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1359 struct inode
*inode
= dentry
->d_inode
;
1362 err
= inode_change_ok(inode
, attr
);
1366 if (S_ISREG(inode
->i_mode
) &&
1367 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1368 struct btrfs_trans_handle
*trans
;
1369 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1370 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1372 u64 mask
= root
->sectorsize
- 1;
1373 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1374 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1378 if (attr
->ia_size
<= hole_start
)
1381 err
= btrfs_check_free_space(root
, 1, 0);
1385 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1387 hole_size
= block_end
- hole_start
;
1388 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1389 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1391 trans
= btrfs_start_transaction(root
, 1);
1392 btrfs_set_trans_block_group(trans
, inode
);
1393 err
= btrfs_drop_extents(trans
, root
, inode
,
1394 hole_start
, block_end
, hole_start
,
1397 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1398 err
= btrfs_insert_file_extent(trans
, root
,
1402 btrfs_drop_extent_cache(inode
, hole_start
,
1404 btrfs_check_file(root
, inode
);
1406 btrfs_end_transaction(trans
, root
);
1407 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1412 err
= inode_setattr(inode
, attr
);
1417 void btrfs_delete_inode(struct inode
*inode
)
1419 struct btrfs_trans_handle
*trans
;
1420 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1424 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1425 truncate_inode_pages(&inode
->i_data
, 0);
1426 if (is_bad_inode(inode
)) {
1430 btrfs_i_size_write(inode
, 0);
1431 trans
= btrfs_start_transaction(root
, 1);
1433 btrfs_set_trans_block_group(trans
, inode
);
1434 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1436 goto no_delete_lock
;
1438 nr
= trans
->blocks_used
;
1441 btrfs_end_transaction(trans
, root
);
1442 btrfs_btree_balance_dirty(root
, nr
);
1446 nr
= trans
->blocks_used
;
1447 btrfs_end_transaction(trans
, root
);
1448 btrfs_btree_balance_dirty(root
, nr
);
1454 * this returns the key found in the dir entry in the location pointer.
1455 * If no dir entries were found, location->objectid is 0.
1457 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1458 struct btrfs_key
*location
)
1460 const char *name
= dentry
->d_name
.name
;
1461 int namelen
= dentry
->d_name
.len
;
1462 struct btrfs_dir_item
*di
;
1463 struct btrfs_path
*path
;
1464 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1467 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1468 location
->objectid
= dir
->i_ino
;
1469 location
->type
= BTRFS_INODE_ITEM_KEY
;
1470 location
->offset
= 0;
1473 path
= btrfs_alloc_path();
1476 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1477 struct btrfs_key key
;
1478 struct extent_buffer
*leaf
;
1482 key
.objectid
= dir
->i_ino
;
1483 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1485 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1489 leaf
= path
->nodes
[0];
1490 slot
= path
->slots
[0];
1491 nritems
= btrfs_header_nritems(leaf
);
1492 if (slot
>= nritems
)
1495 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1496 if (key
.objectid
!= dir
->i_ino
||
1497 key
.type
!= BTRFS_INODE_REF_KEY
) {
1500 location
->objectid
= key
.offset
;
1501 location
->type
= BTRFS_INODE_ITEM_KEY
;
1502 location
->offset
= 0;
1506 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1510 if (!di
|| IS_ERR(di
)) {
1513 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1515 btrfs_free_path(path
);
1518 location
->objectid
= 0;
1523 * when we hit a tree root in a directory, the btrfs part of the inode
1524 * needs to be changed to reflect the root directory of the tree root. This
1525 * is kind of like crossing a mount point.
1527 static int fixup_tree_root_location(struct btrfs_root
*root
,
1528 struct btrfs_key
*location
,
1529 struct btrfs_root
**sub_root
,
1530 struct dentry
*dentry
)
1532 struct btrfs_path
*path
;
1533 struct btrfs_root_item
*ri
;
1535 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1537 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1540 path
= btrfs_alloc_path();
1543 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1544 dentry
->d_name
.name
,
1545 dentry
->d_name
.len
);
1546 if (IS_ERR(*sub_root
))
1547 return PTR_ERR(*sub_root
);
1549 ri
= &(*sub_root
)->root_item
;
1550 location
->objectid
= btrfs_root_dirid(ri
);
1551 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1552 location
->offset
= 0;
1554 btrfs_free_path(path
);
1558 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1560 struct btrfs_iget_args
*args
= p
;
1561 inode
->i_ino
= args
->ino
;
1562 BTRFS_I(inode
)->root
= args
->root
;
1563 BTRFS_I(inode
)->delalloc_bytes
= 0;
1564 BTRFS_I(inode
)->disk_i_size
= 0;
1565 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1566 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1567 inode
->i_mapping
, GFP_NOFS
);
1568 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1569 inode
->i_mapping
, GFP_NOFS
);
1570 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1574 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1576 struct btrfs_iget_args
*args
= opaque
;
1577 return (args
->ino
== inode
->i_ino
&&
1578 args
->root
== BTRFS_I(inode
)->root
);
1581 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1584 struct btrfs_iget_args args
;
1585 args
.ino
= objectid
;
1586 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1591 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1594 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1595 struct btrfs_root
*root
)
1597 struct inode
*inode
;
1598 struct btrfs_iget_args args
;
1599 args
.ino
= objectid
;
1602 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1603 btrfs_init_locked_inode
,
1608 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1609 struct nameidata
*nd
)
1611 struct inode
* inode
;
1612 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1613 struct btrfs_root
*root
= bi
->root
;
1614 struct btrfs_root
*sub_root
= root
;
1615 struct btrfs_key location
;
1618 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1619 return ERR_PTR(-ENAMETOOLONG
);
1621 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1624 return ERR_PTR(ret
);
1627 if (location
.objectid
) {
1628 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1631 return ERR_PTR(ret
);
1633 return ERR_PTR(-ENOENT
);
1634 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1637 return ERR_PTR(-EACCES
);
1638 if (inode
->i_state
& I_NEW
) {
1639 /* the inode and parent dir are two different roots */
1640 if (sub_root
!= root
) {
1642 sub_root
->inode
= inode
;
1644 BTRFS_I(inode
)->root
= sub_root
;
1645 memcpy(&BTRFS_I(inode
)->location
, &location
,
1647 btrfs_read_locked_inode(inode
);
1648 unlock_new_inode(inode
);
1651 return d_splice_alias(inode
, dentry
);
1654 static unsigned char btrfs_filetype_table
[] = {
1655 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1658 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1660 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1661 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1662 struct btrfs_item
*item
;
1663 struct btrfs_dir_item
*di
;
1664 struct btrfs_key key
;
1665 struct btrfs_key found_key
;
1666 struct btrfs_path
*path
;
1669 struct extent_buffer
*leaf
;
1672 unsigned char d_type
;
1677 int key_type
= BTRFS_DIR_INDEX_KEY
;
1682 /* FIXME, use a real flag for deciding about the key type */
1683 if (root
->fs_info
->tree_root
== root
)
1684 key_type
= BTRFS_DIR_ITEM_KEY
;
1686 /* special case for "." */
1687 if (filp
->f_pos
== 0) {
1688 over
= filldir(dirent
, ".", 1,
1696 key
.objectid
= inode
->i_ino
;
1697 path
= btrfs_alloc_path();
1700 /* special case for .., just use the back ref */
1701 if (filp
->f_pos
== 1) {
1702 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1704 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1706 leaf
= path
->nodes
[0];
1707 slot
= path
->slots
[0];
1708 nritems
= btrfs_header_nritems(leaf
);
1709 if (slot
>= nritems
) {
1710 btrfs_release_path(root
, path
);
1711 goto read_dir_items
;
1713 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1714 btrfs_release_path(root
, path
);
1715 if (found_key
.objectid
!= key
.objectid
||
1716 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1717 goto read_dir_items
;
1718 over
= filldir(dirent
, "..", 2,
1719 2, found_key
.offset
, DT_DIR
);
1726 btrfs_set_key_type(&key
, key_type
);
1727 key
.offset
= filp
->f_pos
;
1729 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1734 leaf
= path
->nodes
[0];
1735 nritems
= btrfs_header_nritems(leaf
);
1736 slot
= path
->slots
[0];
1737 if (advance
|| slot
>= nritems
) {
1738 if (slot
>= nritems
-1) {
1739 ret
= btrfs_next_leaf(root
, path
);
1742 leaf
= path
->nodes
[0];
1743 nritems
= btrfs_header_nritems(leaf
);
1744 slot
= path
->slots
[0];
1751 item
= btrfs_item_nr(leaf
, slot
);
1752 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1754 if (found_key
.objectid
!= key
.objectid
)
1756 if (btrfs_key_type(&found_key
) != key_type
)
1758 if (found_key
.offset
< filp
->f_pos
)
1761 filp
->f_pos
= found_key
.offset
;
1763 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1765 di_total
= btrfs_item_size(leaf
, item
);
1766 while(di_cur
< di_total
) {
1767 struct btrfs_key location
;
1769 name_len
= btrfs_dir_name_len(leaf
, di
);
1770 if (name_len
< 32) {
1771 name_ptr
= tmp_name
;
1773 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1776 read_extent_buffer(leaf
, name_ptr
,
1777 (unsigned long)(di
+ 1), name_len
);
1779 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1780 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1781 over
= filldir(dirent
, name_ptr
, name_len
,
1786 if (name_ptr
!= tmp_name
)
1791 di_len
= btrfs_dir_name_len(leaf
, di
) +
1792 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1794 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1797 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1798 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1804 btrfs_free_path(path
);
1808 int btrfs_write_inode(struct inode
*inode
, int wait
)
1810 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1811 struct btrfs_trans_handle
*trans
;
1815 trans
= btrfs_start_transaction(root
, 1);
1816 btrfs_set_trans_block_group(trans
, inode
);
1817 ret
= btrfs_commit_transaction(trans
, root
);
1823 * This is somewhat expensive, updating the tree every time the
1824 * inode changes. But, it is most likely to find the inode in cache.
1825 * FIXME, needs more benchmarking...there are no reasons other than performance
1826 * to keep or drop this code.
1828 void btrfs_dirty_inode(struct inode
*inode
)
1830 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1831 struct btrfs_trans_handle
*trans
;
1833 trans
= btrfs_start_transaction(root
, 1);
1834 btrfs_set_trans_block_group(trans
, inode
);
1835 btrfs_update_inode(trans
, root
, inode
);
1836 btrfs_end_transaction(trans
, root
);
1839 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1840 struct btrfs_root
*root
,
1841 const char *name
, int name_len
,
1844 struct btrfs_block_group_cache
*group
,
1847 struct inode
*inode
;
1848 struct btrfs_inode_item
*inode_item
;
1849 struct btrfs_block_group_cache
*new_inode_group
;
1850 struct btrfs_key
*location
;
1851 struct btrfs_path
*path
;
1852 struct btrfs_inode_ref
*ref
;
1853 struct btrfs_key key
[2];
1859 path
= btrfs_alloc_path();
1862 inode
= new_inode(root
->fs_info
->sb
);
1864 return ERR_PTR(-ENOMEM
);
1866 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1867 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1868 inode
->i_mapping
, GFP_NOFS
);
1869 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1870 inode
->i_mapping
, GFP_NOFS
);
1871 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1872 BTRFS_I(inode
)->delalloc_bytes
= 0;
1873 BTRFS_I(inode
)->disk_i_size
= 0;
1874 BTRFS_I(inode
)->root
= root
;
1880 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1881 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1882 if (!new_inode_group
) {
1883 printk("find_block group failed\n");
1884 new_inode_group
= group
;
1886 BTRFS_I(inode
)->block_group
= new_inode_group
;
1887 BTRFS_I(inode
)->flags
= 0;
1889 key
[0].objectid
= objectid
;
1890 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1893 key
[1].objectid
= objectid
;
1894 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1895 key
[1].offset
= ref_objectid
;
1897 sizes
[0] = sizeof(struct btrfs_inode_item
);
1898 sizes
[1] = name_len
+ sizeof(*ref
);
1900 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1904 if (objectid
> root
->highest_inode
)
1905 root
->highest_inode
= objectid
;
1907 inode
->i_uid
= current
->fsuid
;
1908 inode
->i_gid
= current
->fsgid
;
1909 inode
->i_mode
= mode
;
1910 inode
->i_ino
= objectid
;
1911 inode
->i_blocks
= 0;
1912 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1913 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1914 struct btrfs_inode_item
);
1915 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1917 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1918 struct btrfs_inode_ref
);
1919 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1920 ptr
= (unsigned long)(ref
+ 1);
1921 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1923 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1924 btrfs_free_path(path
);
1926 location
= &BTRFS_I(inode
)->location
;
1927 location
->objectid
= objectid
;
1928 location
->offset
= 0;
1929 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1931 insert_inode_hash(inode
);
1934 btrfs_free_path(path
);
1935 return ERR_PTR(ret
);
1938 static inline u8
btrfs_inode_type(struct inode
*inode
)
1940 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1943 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1944 struct dentry
*dentry
, struct inode
*inode
,
1948 struct btrfs_key key
;
1949 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1950 struct inode
*parent_inode
;
1952 key
.objectid
= inode
->i_ino
;
1953 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1956 ret
= btrfs_insert_dir_item(trans
, root
,
1957 dentry
->d_name
.name
, dentry
->d_name
.len
,
1958 dentry
->d_parent
->d_inode
->i_ino
,
1959 &key
, btrfs_inode_type(inode
));
1962 ret
= btrfs_insert_inode_ref(trans
, root
,
1963 dentry
->d_name
.name
,
1966 dentry
->d_parent
->d_inode
->i_ino
);
1968 parent_inode
= dentry
->d_parent
->d_inode
;
1969 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
1970 dentry
->d_name
.len
* 2);
1971 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1972 ret
= btrfs_update_inode(trans
, root
,
1973 dentry
->d_parent
->d_inode
);
1978 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1979 struct dentry
*dentry
, struct inode
*inode
,
1982 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1984 d_instantiate(dentry
, inode
);
1992 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1993 int mode
, dev_t rdev
)
1995 struct btrfs_trans_handle
*trans
;
1996 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1997 struct inode
*inode
= NULL
;
2001 unsigned long nr
= 0;
2003 if (!new_valid_dev(rdev
))
2006 err
= btrfs_check_free_space(root
, 1, 0);
2010 trans
= btrfs_start_transaction(root
, 1);
2011 btrfs_set_trans_block_group(trans
, dir
);
2013 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2019 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2021 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2022 BTRFS_I(dir
)->block_group
, mode
);
2023 err
= PTR_ERR(inode
);
2027 btrfs_set_trans_block_group(trans
, inode
);
2028 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2032 inode
->i_op
= &btrfs_special_inode_operations
;
2033 init_special_inode(inode
, inode
->i_mode
, rdev
);
2034 btrfs_update_inode(trans
, root
, inode
);
2036 dir
->i_sb
->s_dirt
= 1;
2037 btrfs_update_inode_block_group(trans
, inode
);
2038 btrfs_update_inode_block_group(trans
, dir
);
2040 nr
= trans
->blocks_used
;
2041 btrfs_end_transaction_throttle(trans
, root
);
2044 inode_dec_link_count(inode
);
2047 btrfs_btree_balance_dirty(root
, nr
);
2051 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2052 int mode
, struct nameidata
*nd
)
2054 struct btrfs_trans_handle
*trans
;
2055 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2056 struct inode
*inode
= NULL
;
2059 unsigned long nr
= 0;
2062 err
= btrfs_check_free_space(root
, 1, 0);
2065 trans
= btrfs_start_transaction(root
, 1);
2066 btrfs_set_trans_block_group(trans
, dir
);
2068 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2074 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2076 dentry
->d_parent
->d_inode
->i_ino
,
2077 objectid
, BTRFS_I(dir
)->block_group
, mode
);
2078 err
= PTR_ERR(inode
);
2082 btrfs_set_trans_block_group(trans
, inode
);
2083 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2087 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2088 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2089 inode
->i_fop
= &btrfs_file_operations
;
2090 inode
->i_op
= &btrfs_file_inode_operations
;
2091 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2092 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2093 inode
->i_mapping
, GFP_NOFS
);
2094 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2095 inode
->i_mapping
, GFP_NOFS
);
2096 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2097 BTRFS_I(inode
)->delalloc_bytes
= 0;
2098 BTRFS_I(inode
)->disk_i_size
= 0;
2099 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2101 dir
->i_sb
->s_dirt
= 1;
2102 btrfs_update_inode_block_group(trans
, inode
);
2103 btrfs_update_inode_block_group(trans
, dir
);
2105 nr
= trans
->blocks_used
;
2106 btrfs_end_transaction_throttle(trans
, root
);
2109 inode_dec_link_count(inode
);
2112 btrfs_btree_balance_dirty(root
, nr
);
2116 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2117 struct dentry
*dentry
)
2119 struct btrfs_trans_handle
*trans
;
2120 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2121 struct inode
*inode
= old_dentry
->d_inode
;
2122 unsigned long nr
= 0;
2126 if (inode
->i_nlink
== 0)
2129 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2134 err
= btrfs_check_free_space(root
, 1, 0);
2137 trans
= btrfs_start_transaction(root
, 1);
2139 btrfs_set_trans_block_group(trans
, dir
);
2140 atomic_inc(&inode
->i_count
);
2141 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2146 dir
->i_sb
->s_dirt
= 1;
2147 btrfs_update_inode_block_group(trans
, dir
);
2148 err
= btrfs_update_inode(trans
, root
, inode
);
2153 nr
= trans
->blocks_used
;
2154 btrfs_end_transaction_throttle(trans
, root
);
2157 inode_dec_link_count(inode
);
2160 btrfs_btree_balance_dirty(root
, nr
);
2164 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2166 struct inode
*inode
= NULL
;
2167 struct btrfs_trans_handle
*trans
;
2168 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2170 int drop_on_err
= 0;
2172 unsigned long nr
= 1;
2174 err
= btrfs_check_free_space(root
, 1, 0);
2178 trans
= btrfs_start_transaction(root
, 1);
2179 btrfs_set_trans_block_group(trans
, dir
);
2181 if (IS_ERR(trans
)) {
2182 err
= PTR_ERR(trans
);
2186 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2192 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2194 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2195 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2196 if (IS_ERR(inode
)) {
2197 err
= PTR_ERR(inode
);
2202 inode
->i_op
= &btrfs_dir_inode_operations
;
2203 inode
->i_fop
= &btrfs_dir_file_operations
;
2204 btrfs_set_trans_block_group(trans
, inode
);
2206 btrfs_i_size_write(inode
, 0);
2207 err
= btrfs_update_inode(trans
, root
, inode
);
2211 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2215 d_instantiate(dentry
, inode
);
2217 dir
->i_sb
->s_dirt
= 1;
2218 btrfs_update_inode_block_group(trans
, inode
);
2219 btrfs_update_inode_block_group(trans
, dir
);
2222 nr
= trans
->blocks_used
;
2223 btrfs_end_transaction_throttle(trans
, root
);
2228 btrfs_btree_balance_dirty(root
, nr
);
2232 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2233 struct extent_map
*existing
,
2234 struct extent_map
*em
,
2235 u64 map_start
, u64 map_len
)
2239 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2240 start_diff
= map_start
- em
->start
;
2241 em
->start
= map_start
;
2243 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2244 em
->block_start
+= start_diff
;
2245 return add_extent_mapping(em_tree
, em
);
2248 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2249 size_t pg_offset
, u64 start
, u64 len
,
2255 u64 extent_start
= 0;
2257 u64 objectid
= inode
->i_ino
;
2259 struct btrfs_path
*path
;
2260 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2261 struct btrfs_file_extent_item
*item
;
2262 struct extent_buffer
*leaf
;
2263 struct btrfs_key found_key
;
2264 struct extent_map
*em
= NULL
;
2265 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2266 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2267 struct btrfs_trans_handle
*trans
= NULL
;
2269 path
= btrfs_alloc_path();
2273 spin_lock(&em_tree
->lock
);
2274 em
= lookup_extent_mapping(em_tree
, start
, len
);
2276 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2277 spin_unlock(&em_tree
->lock
);
2280 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2281 free_extent_map(em
);
2282 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2283 free_extent_map(em
);
2287 em
= alloc_extent_map(GFP_NOFS
);
2292 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2293 em
->start
= EXTENT_MAP_HOLE
;
2295 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2296 objectid
, start
, trans
!= NULL
);
2303 if (path
->slots
[0] == 0)
2308 leaf
= path
->nodes
[0];
2309 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2310 struct btrfs_file_extent_item
);
2311 /* are we inside the extent that was found? */
2312 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2313 found_type
= btrfs_key_type(&found_key
);
2314 if (found_key
.objectid
!= objectid
||
2315 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2319 found_type
= btrfs_file_extent_type(leaf
, item
);
2320 extent_start
= found_key
.offset
;
2321 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2322 extent_end
= extent_start
+
2323 btrfs_file_extent_num_bytes(leaf
, item
);
2325 if (start
< extent_start
|| start
>= extent_end
) {
2327 if (start
< extent_start
) {
2328 if (start
+ len
<= extent_start
)
2330 em
->len
= extent_end
- extent_start
;
2336 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2338 em
->start
= extent_start
;
2339 em
->len
= extent_end
- extent_start
;
2340 em
->block_start
= EXTENT_MAP_HOLE
;
2343 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2344 em
->block_start
= bytenr
;
2345 em
->start
= extent_start
;
2346 em
->len
= extent_end
- extent_start
;
2348 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2353 size_t extent_offset
;
2356 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2358 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2359 ~((u64
)root
->sectorsize
- 1);
2360 if (start
< extent_start
|| start
>= extent_end
) {
2362 if (start
< extent_start
) {
2363 if (start
+ len
<= extent_start
)
2365 em
->len
= extent_end
- extent_start
;
2371 em
->block_start
= EXTENT_MAP_INLINE
;
2374 em
->start
= extent_start
;
2379 page_start
= page_offset(page
) + pg_offset
;
2380 extent_offset
= page_start
- extent_start
;
2381 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2382 size
- extent_offset
);
2383 em
->start
= extent_start
+ extent_offset
;
2384 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2385 ~((u64
)root
->sectorsize
- 1);
2387 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2388 if (create
== 0 && !PageUptodate(page
)) {
2389 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2391 flush_dcache_page(page
);
2392 } else if (create
&& PageUptodate(page
)) {
2395 free_extent_map(em
);
2397 btrfs_release_path(root
, path
);
2398 trans
= btrfs_start_transaction(root
, 1);
2401 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2403 btrfs_mark_buffer_dirty(leaf
);
2406 set_extent_uptodate(io_tree
, em
->start
,
2407 extent_map_end(em
) - 1, GFP_NOFS
);
2410 printk("unkknown found_type %d\n", found_type
);
2417 em
->block_start
= EXTENT_MAP_HOLE
;
2419 btrfs_release_path(root
, path
);
2420 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2421 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2427 spin_lock(&em_tree
->lock
);
2428 ret
= add_extent_mapping(em_tree
, em
);
2429 /* it is possible that someone inserted the extent into the tree
2430 * while we had the lock dropped. It is also possible that
2431 * an overlapping map exists in the tree
2433 if (ret
== -EEXIST
) {
2434 struct extent_map
*existing
;
2438 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2439 if (existing
&& (existing
->start
> start
||
2440 existing
->start
+ existing
->len
<= start
)) {
2441 free_extent_map(existing
);
2445 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2448 err
= merge_extent_mapping(em_tree
, existing
,
2451 free_extent_map(existing
);
2453 free_extent_map(em
);
2458 printk("failing to insert %Lu %Lu\n",
2460 free_extent_map(em
);
2464 free_extent_map(em
);
2469 spin_unlock(&em_tree
->lock
);
2471 btrfs_free_path(path
);
2473 ret
= btrfs_end_transaction(trans
, root
);
2479 free_extent_map(em
);
2481 return ERR_PTR(err
);
2486 #if 0 /* waiting for O_DIRECT reads */
2487 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2488 struct buffer_head
*bh_result
, int create
)
2490 struct extent_map
*em
;
2491 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2492 struct btrfs_multi_bio
*multi
= NULL
;
2493 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2499 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2501 if (!em
|| IS_ERR(em
))
2504 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2508 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2513 len
= em
->start
+ em
->len
- start
;
2514 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2516 if (em
->block_start
== EXTENT_MAP_HOLE
||
2517 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2518 bh_result
->b_size
= len
;
2522 logical
= start
- em
->start
;
2523 logical
= em
->block_start
+ logical
;
2526 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2527 logical
, &map_length
, &multi
, 0);
2529 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2530 bh_result
->b_size
= min(map_length
, len
);
2532 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2533 set_buffer_mapped(bh_result
);
2536 free_extent_map(em
);
2541 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2542 const struct iovec
*iov
, loff_t offset
,
2543 unsigned long nr_segs
)
2547 struct file
*file
= iocb
->ki_filp
;
2548 struct inode
*inode
= file
->f_mapping
->host
;
2553 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2554 offset
, nr_segs
, btrfs_get_block
, NULL
);
2558 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2560 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2563 int btrfs_readpage(struct file
*file
, struct page
*page
)
2565 struct extent_io_tree
*tree
;
2566 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2567 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2570 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2572 struct extent_io_tree
*tree
;
2575 if (current
->flags
& PF_MEMALLOC
) {
2576 redirty_page_for_writepage(wbc
, page
);
2580 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2581 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2584 static int btrfs_writepages(struct address_space
*mapping
,
2585 struct writeback_control
*wbc
)
2587 struct extent_io_tree
*tree
;
2588 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2589 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2593 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2594 struct list_head
*pages
, unsigned nr_pages
)
2596 struct extent_io_tree
*tree
;
2597 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2598 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2601 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2603 struct extent_io_tree
*tree
;
2604 struct extent_map_tree
*map
;
2607 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2608 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2609 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2611 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
2612 ClearPagePrivate(page
);
2613 set_page_private(page
, 0);
2614 page_cache_release(page
);
2619 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2621 struct btrfs_ordered_extent
*ordered
;
2623 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2626 btrfs_put_ordered_extent(ordered
);
2629 return __btrfs_releasepage(page
, gfp_flags
);
2632 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2634 struct extent_io_tree
*tree
;
2635 struct btrfs_ordered_extent
*ordered
;
2636 u64 page_start
= page_offset(page
);
2637 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2639 wait_on_page_writeback(page
);
2640 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2642 btrfs_releasepage(page
, GFP_NOFS
);
2646 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2647 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2650 clear_extent_bit(tree
, page_start
, page_end
,
2651 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2652 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2653 btrfs_writepage_end_io_hook(page
, page_start
,
2655 btrfs_put_ordered_extent(ordered
);
2656 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2658 clear_extent_bit(tree
, page_start
, page_end
,
2659 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2662 __btrfs_releasepage(page
, GFP_NOFS
);
2664 if (PagePrivate(page
)) {
2665 invalidate_extent_lru(tree
, page_offset(page
),
2667 ClearPagePrivate(page
);
2668 set_page_private(page
, 0);
2669 page_cache_release(page
);
2674 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2675 * called from a page fault handler when a page is first dirtied. Hence we must
2676 * be careful to check for EOF conditions here. We set the page up correctly
2677 * for a written page which means we get ENOSPC checking when writing into
2678 * holes and correct delalloc and unwritten extent mapping on filesystems that
2679 * support these features.
2681 * We are not allowed to take the i_mutex here so we have to play games to
2682 * protect against truncate races as the page could now be beyond EOF. Because
2683 * vmtruncate() writes the inode size before removing pages, once we have the
2684 * page lock we can determine safely if the page is beyond EOF. If it is not
2685 * beyond EOF, then the page is guaranteed safe against truncation until we
2688 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2690 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2691 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2692 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2693 struct btrfs_ordered_extent
*ordered
;
2695 unsigned long zero_start
;
2701 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2708 size
= i_size_read(inode
);
2709 page_start
= page_offset(page
);
2710 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2712 if ((page
->mapping
!= inode
->i_mapping
) ||
2713 (page_start
>= size
)) {
2714 /* page got truncated out from underneath us */
2717 wait_on_page_writeback(page
);
2719 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2720 set_page_extent_mapped(page
);
2722 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2724 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2726 btrfs_wait_ordered_extent(inode
, ordered
);
2727 btrfs_put_ordered_extent(ordered
);
2731 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
2732 page_end
, GFP_NOFS
);
2735 /* page is wholly or partially inside EOF */
2736 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2737 zero_start
= size
& ~PAGE_CACHE_MASK
;
2739 zero_start
= PAGE_CACHE_SIZE
;
2741 if (zero_start
!= PAGE_CACHE_SIZE
) {
2743 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
2744 flush_dcache_page(page
);
2747 ClearPageChecked(page
);
2748 set_page_dirty(page
);
2749 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2757 static void btrfs_truncate(struct inode
*inode
)
2759 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2761 struct btrfs_trans_handle
*trans
;
2763 u64 mask
= root
->sectorsize
- 1;
2765 if (!S_ISREG(inode
->i_mode
))
2767 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2770 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2772 trans
= btrfs_start_transaction(root
, 1);
2773 btrfs_set_trans_block_group(trans
, inode
);
2774 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
2775 btrfs_i_size_write(inode
, inode
->i_size
);
2777 /* FIXME, add redo link to tree so we don't leak on crash */
2778 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2779 BTRFS_EXTENT_DATA_KEY
);
2780 btrfs_update_inode(trans
, root
, inode
);
2781 nr
= trans
->blocks_used
;
2783 ret
= btrfs_end_transaction_throttle(trans
, root
);
2785 btrfs_btree_balance_dirty(root
, nr
);
2789 * Invalidate a single dcache entry at the root of the filesystem.
2790 * Needed after creation of snapshot or subvolume.
2792 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
2795 struct dentry
*alias
, *entry
;
2798 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
2802 /* change me if btrfs ever gets a d_hash operation */
2803 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
2804 entry
= d_lookup(alias
, &qstr
);
2807 d_invalidate(entry
);
2813 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
2814 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
2815 struct btrfs_block_group_cache
*block_group
)
2817 struct inode
*inode
;
2820 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2821 new_dirid
, block_group
, S_IFDIR
| 0700);
2823 return PTR_ERR(inode
);
2824 inode
->i_op
= &btrfs_dir_inode_operations
;
2825 inode
->i_fop
= &btrfs_dir_file_operations
;
2826 new_root
->inode
= inode
;
2828 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2831 btrfs_i_size_write(inode
, 0);
2833 return btrfs_update_inode(trans
, new_root
, inode
);
2836 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2837 struct file_ra_state
*ra
, struct file
*file
,
2838 pgoff_t offset
, pgoff_t last_index
)
2840 pgoff_t req_size
= last_index
- offset
+ 1;
2842 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2843 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2846 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2847 return offset
+ req_size
;
2851 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2853 struct btrfs_inode
*ei
;
2855 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2859 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
2860 return &ei
->vfs_inode
;
2863 void btrfs_destroy_inode(struct inode
*inode
)
2865 struct btrfs_ordered_extent
*ordered
;
2866 WARN_ON(!list_empty(&inode
->i_dentry
));
2867 WARN_ON(inode
->i_data
.nrpages
);
2870 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
2874 printk("found ordered extent %Lu %Lu\n",
2875 ordered
->file_offset
, ordered
->len
);
2876 btrfs_remove_ordered_extent(inode
, ordered
);
2877 btrfs_put_ordered_extent(ordered
);
2878 btrfs_put_ordered_extent(ordered
);
2881 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2882 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2885 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2886 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2888 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2889 unsigned long flags
)
2892 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2894 inode_init_once(&ei
->vfs_inode
);
2897 void btrfs_destroy_cachep(void)
2899 if (btrfs_inode_cachep
)
2900 kmem_cache_destroy(btrfs_inode_cachep
);
2901 if (btrfs_trans_handle_cachep
)
2902 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2903 if (btrfs_transaction_cachep
)
2904 kmem_cache_destroy(btrfs_transaction_cachep
);
2905 if (btrfs_bit_radix_cachep
)
2906 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2907 if (btrfs_path_cachep
)
2908 kmem_cache_destroy(btrfs_path_cachep
);
2911 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2912 unsigned long extra_flags
,
2913 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2914 void (*ctor
)(struct kmem_cache
*, void *)
2916 void (*ctor
)(void *, struct kmem_cache
*,
2921 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2922 SLAB_MEM_SPREAD
| extra_flags
), ctor
2923 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2929 int btrfs_init_cachep(void)
2931 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2932 sizeof(struct btrfs_inode
),
2934 if (!btrfs_inode_cachep
)
2936 btrfs_trans_handle_cachep
=
2937 btrfs_cache_create("btrfs_trans_handle_cache",
2938 sizeof(struct btrfs_trans_handle
),
2940 if (!btrfs_trans_handle_cachep
)
2942 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2943 sizeof(struct btrfs_transaction
),
2945 if (!btrfs_transaction_cachep
)
2947 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2948 sizeof(struct btrfs_path
),
2950 if (!btrfs_path_cachep
)
2952 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2953 SLAB_DESTROY_BY_RCU
, NULL
);
2954 if (!btrfs_bit_radix_cachep
)
2958 btrfs_destroy_cachep();
2962 static int btrfs_getattr(struct vfsmount
*mnt
,
2963 struct dentry
*dentry
, struct kstat
*stat
)
2965 struct inode
*inode
= dentry
->d_inode
;
2966 generic_fillattr(inode
, stat
);
2967 stat
->blksize
= PAGE_CACHE_SIZE
;
2968 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
2972 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2973 struct inode
* new_dir
,struct dentry
*new_dentry
)
2975 struct btrfs_trans_handle
*trans
;
2976 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2977 struct inode
*new_inode
= new_dentry
->d_inode
;
2978 struct inode
*old_inode
= old_dentry
->d_inode
;
2979 struct timespec ctime
= CURRENT_TIME
;
2982 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2983 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2987 ret
= btrfs_check_free_space(root
, 1, 0);
2991 trans
= btrfs_start_transaction(root
, 1);
2993 btrfs_set_trans_block_group(trans
, new_dir
);
2995 old_dentry
->d_inode
->i_nlink
++;
2996 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2997 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2998 old_inode
->i_ctime
= ctime
;
3000 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3005 new_inode
->i_ctime
= CURRENT_TIME
;
3006 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3010 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3015 btrfs_end_transaction(trans
, root
);
3020 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3021 const char *symname
)
3023 struct btrfs_trans_handle
*trans
;
3024 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3025 struct btrfs_path
*path
;
3026 struct btrfs_key key
;
3027 struct inode
*inode
= NULL
;
3034 struct btrfs_file_extent_item
*ei
;
3035 struct extent_buffer
*leaf
;
3036 unsigned long nr
= 0;
3038 name_len
= strlen(symname
) + 1;
3039 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3040 return -ENAMETOOLONG
;
3042 err
= btrfs_check_free_space(root
, 1, 0);
3046 trans
= btrfs_start_transaction(root
, 1);
3047 btrfs_set_trans_block_group(trans
, dir
);
3049 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3055 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
3057 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3058 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3059 err
= PTR_ERR(inode
);
3063 btrfs_set_trans_block_group(trans
, inode
);
3064 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3068 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3069 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3070 inode
->i_fop
= &btrfs_file_operations
;
3071 inode
->i_op
= &btrfs_file_inode_operations
;
3072 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3073 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3074 inode
->i_mapping
, GFP_NOFS
);
3075 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3076 inode
->i_mapping
, GFP_NOFS
);
3077 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3078 BTRFS_I(inode
)->delalloc_bytes
= 0;
3079 BTRFS_I(inode
)->disk_i_size
= 0;
3080 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3082 dir
->i_sb
->s_dirt
= 1;
3083 btrfs_update_inode_block_group(trans
, inode
);
3084 btrfs_update_inode_block_group(trans
, dir
);
3088 path
= btrfs_alloc_path();
3090 key
.objectid
= inode
->i_ino
;
3092 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3093 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3094 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3100 leaf
= path
->nodes
[0];
3101 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3102 struct btrfs_file_extent_item
);
3103 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3104 btrfs_set_file_extent_type(leaf
, ei
,
3105 BTRFS_FILE_EXTENT_INLINE
);
3106 ptr
= btrfs_file_extent_inline_start(ei
);
3107 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3108 btrfs_mark_buffer_dirty(leaf
);
3109 btrfs_free_path(path
);
3111 inode
->i_op
= &btrfs_symlink_inode_operations
;
3112 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3113 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3114 btrfs_i_size_write(inode
, name_len
- 1);
3115 err
= btrfs_update_inode(trans
, root
, inode
);
3120 nr
= trans
->blocks_used
;
3121 btrfs_end_transaction_throttle(trans
, root
);
3124 inode_dec_link_count(inode
);
3127 btrfs_btree_balance_dirty(root
, nr
);
3131 static int btrfs_set_page_dirty(struct page
*page
)
3133 return __set_page_dirty_nobuffers(page
);
3136 static int btrfs_permission(struct inode
*inode
, int mask
,
3137 struct nameidata
*nd
)
3139 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3141 return generic_permission(inode
, mask
, NULL
);
3144 static struct inode_operations btrfs_dir_inode_operations
= {
3145 .lookup
= btrfs_lookup
,
3146 .create
= btrfs_create
,
3147 .unlink
= btrfs_unlink
,
3149 .mkdir
= btrfs_mkdir
,
3150 .rmdir
= btrfs_rmdir
,
3151 .rename
= btrfs_rename
,
3152 .symlink
= btrfs_symlink
,
3153 .setattr
= btrfs_setattr
,
3154 .mknod
= btrfs_mknod
,
3155 .setxattr
= generic_setxattr
,
3156 .getxattr
= generic_getxattr
,
3157 .listxattr
= btrfs_listxattr
,
3158 .removexattr
= generic_removexattr
,
3159 .permission
= btrfs_permission
,
3161 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3162 .lookup
= btrfs_lookup
,
3163 .permission
= btrfs_permission
,
3165 static struct file_operations btrfs_dir_file_operations
= {
3166 .llseek
= generic_file_llseek
,
3167 .read
= generic_read_dir
,
3168 .readdir
= btrfs_readdir
,
3169 .unlocked_ioctl
= btrfs_ioctl
,
3170 #ifdef CONFIG_COMPAT
3171 .compat_ioctl
= btrfs_ioctl
,
3173 .release
= btrfs_release_file
,
3176 static struct extent_io_ops btrfs_extent_io_ops
= {
3177 .fill_delalloc
= run_delalloc_range
,
3178 .submit_bio_hook
= btrfs_submit_bio_hook
,
3179 .merge_bio_hook
= btrfs_merge_bio_hook
,
3180 .readpage_io_hook
= btrfs_readpage_io_hook
,
3181 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3182 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3183 .writepage_start_hook
= btrfs_writepage_start_hook
,
3184 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3185 .set_bit_hook
= btrfs_set_bit_hook
,
3186 .clear_bit_hook
= btrfs_clear_bit_hook
,
3189 static struct address_space_operations btrfs_aops
= {
3190 .readpage
= btrfs_readpage
,
3191 .writepage
= btrfs_writepage
,
3192 .writepages
= btrfs_writepages
,
3193 .readpages
= btrfs_readpages
,
3194 .sync_page
= block_sync_page
,
3196 .direct_IO
= btrfs_direct_IO
,
3197 .invalidatepage
= btrfs_invalidatepage
,
3198 .releasepage
= btrfs_releasepage
,
3199 .set_page_dirty
= btrfs_set_page_dirty
,
3202 static struct address_space_operations btrfs_symlink_aops
= {
3203 .readpage
= btrfs_readpage
,
3204 .writepage
= btrfs_writepage
,
3205 .invalidatepage
= btrfs_invalidatepage
,
3206 .releasepage
= btrfs_releasepage
,
3209 static struct inode_operations btrfs_file_inode_operations
= {
3210 .truncate
= btrfs_truncate
,
3211 .getattr
= btrfs_getattr
,
3212 .setattr
= btrfs_setattr
,
3213 .setxattr
= generic_setxattr
,
3214 .getxattr
= generic_getxattr
,
3215 .listxattr
= btrfs_listxattr
,
3216 .removexattr
= generic_removexattr
,
3217 .permission
= btrfs_permission
,
3219 static struct inode_operations btrfs_special_inode_operations
= {
3220 .getattr
= btrfs_getattr
,
3221 .setattr
= btrfs_setattr
,
3222 .permission
= btrfs_permission
,
3224 static struct inode_operations btrfs_symlink_inode_operations
= {
3225 .readlink
= generic_readlink
,
3226 .follow_link
= page_follow_link_light
,
3227 .put_link
= page_put_link
,
3228 .permission
= btrfs_permission
,