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 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
132 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1);
133 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
135 while(num_bytes
> 0) {
136 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
137 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
138 root
->sectorsize
, 0, 0,
144 em
= alloc_extent_map(GFP_NOFS
);
146 em
->len
= ins
.offset
;
147 em
->block_start
= ins
.objectid
;
148 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
149 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
150 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
152 spin_lock(&em_tree
->lock
);
153 ret
= add_extent_mapping(em_tree
, em
);
154 spin_unlock(&em_tree
->lock
);
155 if (ret
!= -EEXIST
) {
159 btrfs_drop_extent_cache(inode
, start
,
160 start
+ ins
.offset
- 1);
162 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
164 cur_alloc_size
= ins
.offset
;
165 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
168 if (num_bytes
< cur_alloc_size
) {
169 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
173 num_bytes
-= cur_alloc_size
;
174 alloc_hint
= ins
.objectid
+ ins
.offset
;
175 start
+= cur_alloc_size
;
178 btrfs_end_transaction(trans
, root
);
182 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
190 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
191 struct btrfs_block_group_cache
*block_group
;
192 struct extent_buffer
*leaf
;
194 struct btrfs_path
*path
;
195 struct btrfs_file_extent_item
*item
;
198 struct btrfs_key found_key
;
200 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
201 path
= btrfs_alloc_path();
204 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
205 inode
->i_ino
, start
, 0);
207 btrfs_free_path(path
);
213 if (path
->slots
[0] == 0)
218 leaf
= path
->nodes
[0];
219 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
220 struct btrfs_file_extent_item
);
222 /* are we inside the extent that was found? */
223 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
224 found_type
= btrfs_key_type(&found_key
);
225 if (found_key
.objectid
!= inode
->i_ino
||
226 found_type
!= BTRFS_EXTENT_DATA_KEY
)
229 found_type
= btrfs_file_extent_type(leaf
, item
);
230 extent_start
= found_key
.offset
;
231 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
232 u64 extent_num_bytes
;
234 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
235 extent_end
= extent_start
+ extent_num_bytes
;
238 if (loops
&& start
!= extent_start
)
241 if (start
< extent_start
|| start
>= extent_end
)
244 cow_end
= min(end
, extent_end
- 1);
245 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
249 if (btrfs_count_snapshots_in_path(root
, path
, inode
->i_ino
,
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
258 block_group
= btrfs_lookup_block_group(root
->fs_info
,
260 if (!block_group
|| block_group
->ro
)
269 btrfs_free_path(path
);
272 btrfs_release_path(root
, path
);
277 cow_file_range(inode
, start
, end
);
282 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
284 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
287 if (btrfs_test_opt(root
, NODATACOW
) ||
288 btrfs_test_flag(inode
, NODATACOW
))
289 ret
= run_delalloc_nocow(inode
, start
, end
);
291 ret
= cow_file_range(inode
, start
, end
);
296 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
297 unsigned long old
, unsigned long bits
)
300 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
301 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
302 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
303 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
304 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
305 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
310 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
311 unsigned long old
, unsigned long bits
)
313 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
314 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
317 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
318 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
319 printk("warning: delalloc account %Lu %Lu\n",
320 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
321 root
->fs_info
->delalloc_bytes
= 0;
322 BTRFS_I(inode
)->delalloc_bytes
= 0;
324 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
325 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
327 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
332 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
333 size_t size
, struct bio
*bio
)
335 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
336 struct btrfs_mapping_tree
*map_tree
;
337 u64 logical
= bio
->bi_sector
<< 9;
342 length
= bio
->bi_size
;
343 map_tree
= &root
->fs_info
->mapping_tree
;
345 ret
= btrfs_map_block(map_tree
, READ
, logical
,
346 &map_length
, NULL
, 0);
348 if (map_length
< length
+ size
) {
354 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
357 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
360 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
363 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
366 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
369 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
372 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
375 if (!(rw
& (1 << BIO_RW
))) {
379 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
380 inode
, rw
, bio
, mirror_num
,
381 __btrfs_submit_bio_hook
);
383 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
386 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
387 struct inode
*inode
, u64 file_offset
,
388 struct list_head
*list
)
390 struct list_head
*cur
;
391 struct btrfs_ordered_sum
*sum
;
393 btrfs_set_trans_block_group(trans
, inode
);
394 list_for_each(cur
, 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
);
404 struct btrfs_writepage_fixup
{
406 struct btrfs_work work
;
409 /* see btrfs_writepage_start_hook for details on why this is required */
410 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
412 struct btrfs_writepage_fixup
*fixup
;
413 struct btrfs_ordered_extent
*ordered
;
419 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
423 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
424 ClearPageChecked(page
);
428 inode
= page
->mapping
->host
;
429 page_start
= page_offset(page
);
430 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
432 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
434 /* already ordered? We're done */
435 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
436 EXTENT_ORDERED
, 0)) {
440 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
442 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
445 btrfs_start_ordered_extent(inode
, ordered
, 1);
449 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
451 ClearPageChecked(page
);
453 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
456 page_cache_release(page
);
460 * There are a few paths in the higher layers of the kernel that directly
461 * set the page dirty bit without asking the filesystem if it is a
462 * good idea. This causes problems because we want to make sure COW
463 * properly happens and the data=ordered rules are followed.
465 * In our case any range that doesn't have the EXTENT_ORDERED bit set
466 * hasn't been properly setup for IO. We kick off an async process
467 * to fix it up. The async helper will wait for ordered extents, set
468 * the delalloc bit and make it safe to write the page.
470 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
472 struct inode
*inode
= page
->mapping
->host
;
473 struct btrfs_writepage_fixup
*fixup
;
474 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
477 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
482 if (PageChecked(page
))
485 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
489 SetPageChecked(page
);
490 page_cache_get(page
);
491 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
493 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
497 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
499 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
500 struct btrfs_trans_handle
*trans
;
501 struct btrfs_ordered_extent
*ordered_extent
;
502 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
503 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
504 struct extent_map
*em
;
505 struct extent_map
*em_orig
;
509 struct list_head list
;
510 struct btrfs_key ins
;
514 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
518 trans
= btrfs_join_transaction(root
, 1);
520 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
521 BUG_ON(!ordered_extent
);
523 lock_extent(io_tree
, ordered_extent
->file_offset
,
524 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
527 INIT_LIST_HEAD(&list
);
529 ins
.objectid
= ordered_extent
->start
;
530 ins
.offset
= ordered_extent
->len
;
531 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
533 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
534 trans
->transid
, inode
->i_ino
,
535 ordered_extent
->file_offset
, &ins
);
538 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
540 spin_lock(&em_tree
->lock
);
541 clear_start
= ordered_extent
->file_offset
;
542 clear_end
= ordered_extent
->file_offset
+ ordered_extent
->len
;
543 em
= lookup_extent_mapping(em_tree
, clear_start
,
544 ordered_extent
->len
);
546 while(em
&& clear_start
< extent_map_end(em
) && clear_end
> em
->start
) {
547 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
548 rb
= rb_next(&em
->rb_node
);
551 em
= rb_entry(rb
, struct extent_map
, rb_node
);
553 free_extent_map(em_orig
);
554 spin_unlock(&em_tree
->lock
);
556 ret
= btrfs_drop_extents(trans
, root
, inode
,
557 ordered_extent
->file_offset
,
558 ordered_extent
->file_offset
+
560 ordered_extent
->file_offset
, &alloc_hint
);
562 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
563 ordered_extent
->file_offset
,
564 ordered_extent
->start
,
566 ordered_extent
->len
, 0);
569 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
570 ordered_extent
->file_offset
+
571 ordered_extent
->len
- 1);
572 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
574 inode
->i_blocks
+= ordered_extent
->len
>> 9;
575 unlock_extent(io_tree
, ordered_extent
->file_offset
,
576 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
578 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
579 &ordered_extent
->list
);
581 btrfs_ordered_update_i_size(inode
, ordered_extent
);
582 btrfs_remove_ordered_extent(inode
, ordered_extent
);
585 btrfs_put_ordered_extent(ordered_extent
);
586 /* once for the tree */
587 btrfs_put_ordered_extent(ordered_extent
);
589 btrfs_update_inode(trans
, root
, inode
);
590 btrfs_end_transaction(trans
, root
);
594 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
595 struct extent_state
*state
, int uptodate
)
597 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
600 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
603 struct inode
*inode
= page
->mapping
->host
;
604 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
605 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
606 struct btrfs_csum_item
*item
;
607 struct btrfs_path
*path
= NULL
;
610 if (btrfs_test_opt(root
, NODATASUM
) ||
611 btrfs_test_flag(inode
, NODATASUM
))
614 path
= btrfs_alloc_path();
615 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
618 * It is possible there is an ordered extent that has
619 * not yet finished for this range in the file. If so,
620 * that extent will have a csum cached, and it will insert
621 * the sum after all the blocks in the extent are fully
622 * on disk. So, look for an ordered extent and use the
625 ret
= btrfs_find_ordered_sum(inode
, start
, &csum
);
630 /* a csum that isn't present is a preallocated region. */
631 if (ret
== -ENOENT
|| ret
== -EFBIG
)
634 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
638 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
641 set_state_private(io_tree
, start
, csum
);
644 btrfs_free_path(path
);
648 struct io_failure_record
{
656 int btrfs_io_failed_hook(struct bio
*failed_bio
,
657 struct page
*page
, u64 start
, u64 end
,
658 struct extent_state
*state
)
660 struct io_failure_record
*failrec
= NULL
;
662 struct extent_map
*em
;
663 struct inode
*inode
= page
->mapping
->host
;
664 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
665 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
672 ret
= get_state_private(failure_tree
, start
, &private);
674 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
677 failrec
->start
= start
;
678 failrec
->len
= end
- start
+ 1;
679 failrec
->last_mirror
= 0;
681 spin_lock(&em_tree
->lock
);
682 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
683 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
687 spin_unlock(&em_tree
->lock
);
689 if (!em
|| IS_ERR(em
)) {
693 logical
= start
- em
->start
;
694 logical
= em
->block_start
+ logical
;
695 failrec
->logical
= logical
;
697 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
698 EXTENT_DIRTY
, GFP_NOFS
);
699 set_state_private(failure_tree
, start
,
700 (u64
)(unsigned long)failrec
);
702 failrec
= (struct io_failure_record
*)(unsigned long)private;
704 num_copies
= btrfs_num_copies(
705 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
706 failrec
->logical
, failrec
->len
);
707 failrec
->last_mirror
++;
709 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
710 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
713 if (state
&& state
->start
!= failrec
->start
)
715 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
717 if (!state
|| failrec
->last_mirror
> num_copies
) {
718 set_state_private(failure_tree
, failrec
->start
, 0);
719 clear_extent_bits(failure_tree
, failrec
->start
,
720 failrec
->start
+ failrec
->len
- 1,
721 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
725 bio
= bio_alloc(GFP_NOFS
, 1);
726 bio
->bi_private
= state
;
727 bio
->bi_end_io
= failed_bio
->bi_end_io
;
728 bio
->bi_sector
= failrec
->logical
>> 9;
729 bio
->bi_bdev
= failed_bio
->bi_bdev
;
731 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
732 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
737 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
738 failrec
->last_mirror
);
742 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
746 struct io_failure_record
*failure
;
750 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
751 (u64
)-1, 1, EXTENT_DIRTY
)) {
752 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
753 start
, &private_failure
);
755 failure
= (struct io_failure_record
*)(unsigned long)
757 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
759 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
761 failure
->start
+ failure
->len
- 1,
762 EXTENT_DIRTY
| EXTENT_LOCKED
,
770 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
771 struct extent_state
*state
)
773 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
774 struct inode
*inode
= page
->mapping
->host
;
775 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
777 u64
private = ~(u32
)0;
779 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
783 if (btrfs_test_opt(root
, NODATASUM
) ||
784 btrfs_test_flag(inode
, NODATASUM
))
786 if (state
&& state
->start
== start
) {
787 private = state
->private;
790 ret
= get_state_private(io_tree
, start
, &private);
792 local_irq_save(flags
);
793 kaddr
= kmap_atomic(page
, KM_IRQ0
);
797 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
798 btrfs_csum_final(csum
, (char *)&csum
);
799 if (csum
!= private) {
802 kunmap_atomic(kaddr
, KM_IRQ0
);
803 local_irq_restore(flags
);
805 /* if the io failure tree for this inode is non-empty,
806 * check to see if we've recovered from a failed IO
808 btrfs_clean_io_failures(inode
, start
);
812 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
813 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
815 memset(kaddr
+ offset
, 1, end
- start
+ 1);
816 flush_dcache_page(page
);
817 kunmap_atomic(kaddr
, KM_IRQ0
);
818 local_irq_restore(flags
);
824 void btrfs_read_locked_inode(struct inode
*inode
)
826 struct btrfs_path
*path
;
827 struct extent_buffer
*leaf
;
828 struct btrfs_inode_item
*inode_item
;
829 struct btrfs_timespec
*tspec
;
830 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
831 struct btrfs_key location
;
832 u64 alloc_group_block
;
836 path
= btrfs_alloc_path();
838 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
840 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
844 leaf
= path
->nodes
[0];
845 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
846 struct btrfs_inode_item
);
848 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
849 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
850 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
851 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
852 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
854 tspec
= btrfs_inode_atime(inode_item
);
855 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
856 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
858 tspec
= btrfs_inode_mtime(inode_item
);
859 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
860 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
862 tspec
= btrfs_inode_ctime(inode_item
);
863 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
864 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
866 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
867 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
869 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
871 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
872 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
874 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
875 if (!BTRFS_I(inode
)->block_group
) {
876 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
878 BTRFS_BLOCK_GROUP_METADATA
, 0);
880 btrfs_free_path(path
);
883 switch (inode
->i_mode
& S_IFMT
) {
885 inode
->i_mapping
->a_ops
= &btrfs_aops
;
886 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
887 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
888 inode
->i_fop
= &btrfs_file_operations
;
889 inode
->i_op
= &btrfs_file_inode_operations
;
892 inode
->i_fop
= &btrfs_dir_file_operations
;
893 if (root
== root
->fs_info
->tree_root
)
894 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
896 inode
->i_op
= &btrfs_dir_inode_operations
;
899 inode
->i_op
= &btrfs_symlink_inode_operations
;
900 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
901 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
904 init_special_inode(inode
, inode
->i_mode
, rdev
);
910 btrfs_free_path(path
);
911 make_bad_inode(inode
);
914 static void fill_inode_item(struct extent_buffer
*leaf
,
915 struct btrfs_inode_item
*item
,
918 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
919 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
920 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
921 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
922 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
924 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
925 inode
->i_atime
.tv_sec
);
926 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
927 inode
->i_atime
.tv_nsec
);
929 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
930 inode
->i_mtime
.tv_sec
);
931 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
932 inode
->i_mtime
.tv_nsec
);
934 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
935 inode
->i_ctime
.tv_sec
);
936 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
937 inode
->i_ctime
.tv_nsec
);
939 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
940 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
941 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
942 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
943 btrfs_set_inode_block_group(leaf
, item
,
944 BTRFS_I(inode
)->block_group
->key
.objectid
);
947 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
948 struct btrfs_root
*root
,
951 struct btrfs_inode_item
*inode_item
;
952 struct btrfs_path
*path
;
953 struct extent_buffer
*leaf
;
956 path
= btrfs_alloc_path();
958 ret
= btrfs_lookup_inode(trans
, root
, path
,
959 &BTRFS_I(inode
)->location
, 1);
966 leaf
= path
->nodes
[0];
967 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
968 struct btrfs_inode_item
);
970 fill_inode_item(leaf
, inode_item
, inode
);
971 btrfs_mark_buffer_dirty(leaf
);
972 btrfs_set_inode_last_trans(trans
, inode
);
975 btrfs_free_path(path
);
980 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
981 struct btrfs_root
*root
,
983 struct dentry
*dentry
)
985 struct btrfs_path
*path
;
986 const char *name
= dentry
->d_name
.name
;
987 int name_len
= dentry
->d_name
.len
;
989 struct extent_buffer
*leaf
;
990 struct btrfs_dir_item
*di
;
991 struct btrfs_key key
;
993 path
= btrfs_alloc_path();
999 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1000 name
, name_len
, -1);
1009 leaf
= path
->nodes
[0];
1010 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1011 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1014 btrfs_release_path(root
, path
);
1016 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1017 key
.objectid
, name
, name_len
, -1);
1026 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1027 btrfs_release_path(root
, path
);
1029 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
1030 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1031 dentry
->d_inode
->i_ino
,
1032 dentry
->d_parent
->d_inode
->i_ino
);
1034 printk("failed to delete reference to %.*s, "
1035 "inode %lu parent %lu\n", name_len
, name
,
1036 dentry
->d_inode
->i_ino
,
1037 dentry
->d_parent
->d_inode
->i_ino
);
1040 btrfs_free_path(path
);
1042 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1043 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1044 btrfs_update_inode(trans
, root
, dir
);
1045 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1046 dentry
->d_inode
->i_nlink
--;
1048 drop_nlink(dentry
->d_inode
);
1050 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1051 dir
->i_sb
->s_dirt
= 1;
1056 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1058 struct btrfs_root
*root
;
1059 struct btrfs_trans_handle
*trans
;
1061 unsigned long nr
= 0;
1063 root
= BTRFS_I(dir
)->root
;
1065 ret
= btrfs_check_free_space(root
, 1, 1);
1069 trans
= btrfs_start_transaction(root
, 1);
1071 btrfs_set_trans_block_group(trans
, dir
);
1072 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1073 nr
= trans
->blocks_used
;
1075 btrfs_end_transaction_throttle(trans
, root
);
1077 btrfs_btree_balance_dirty(root
, nr
);
1081 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1083 struct inode
*inode
= dentry
->d_inode
;
1086 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1087 struct btrfs_trans_handle
*trans
;
1088 unsigned long nr
= 0;
1090 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1094 ret
= btrfs_check_free_space(root
, 1, 1);
1098 trans
= btrfs_start_transaction(root
, 1);
1099 btrfs_set_trans_block_group(trans
, dir
);
1101 /* now the directory is empty */
1102 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1104 btrfs_i_size_write(inode
, 0);
1107 nr
= trans
->blocks_used
;
1108 ret
= btrfs_end_transaction_throttle(trans
, root
);
1110 btrfs_btree_balance_dirty(root
, nr
);
1118 * this can truncate away extent items, csum items and directory items.
1119 * It starts at a high offset and removes keys until it can't find
1120 * any higher than i_size.
1122 * csum items that cross the new i_size are truncated to the new size
1125 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1126 struct btrfs_root
*root
,
1127 struct inode
*inode
,
1131 struct btrfs_path
*path
;
1132 struct btrfs_key key
;
1133 struct btrfs_key found_key
;
1135 struct extent_buffer
*leaf
;
1136 struct btrfs_file_extent_item
*fi
;
1137 u64 extent_start
= 0;
1138 u64 extent_num_bytes
= 0;
1144 int pending_del_nr
= 0;
1145 int pending_del_slot
= 0;
1146 int extent_type
= -1;
1147 u64 mask
= root
->sectorsize
- 1;
1149 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1150 path
= btrfs_alloc_path();
1154 /* FIXME, add redo link to tree so we don't leak on crash */
1155 key
.objectid
= inode
->i_ino
;
1156 key
.offset
= (u64
)-1;
1159 btrfs_init_path(path
);
1161 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1166 BUG_ON(path
->slots
[0] == 0);
1172 leaf
= path
->nodes
[0];
1173 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1174 found_type
= btrfs_key_type(&found_key
);
1176 if (found_key
.objectid
!= inode
->i_ino
)
1179 if (found_type
< min_type
)
1182 item_end
= found_key
.offset
;
1183 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1184 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1185 struct btrfs_file_extent_item
);
1186 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1187 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1189 btrfs_file_extent_num_bytes(leaf
, fi
);
1190 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1191 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1193 item_end
+= btrfs_file_extent_inline_len(leaf
,
1198 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1199 ret
= btrfs_csum_truncate(trans
, root
, path
,
1203 if (item_end
< inode
->i_size
) {
1204 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1205 found_type
= BTRFS_INODE_ITEM_KEY
;
1206 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1207 found_type
= BTRFS_CSUM_ITEM_KEY
;
1208 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1209 found_type
= BTRFS_XATTR_ITEM_KEY
;
1210 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1211 found_type
= BTRFS_INODE_REF_KEY
;
1212 } else if (found_type
) {
1217 btrfs_set_key_type(&key
, found_type
);
1220 if (found_key
.offset
>= inode
->i_size
)
1226 /* FIXME, shrink the extent if the ref count is only 1 */
1227 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1230 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1232 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1234 u64 orig_num_bytes
=
1235 btrfs_file_extent_num_bytes(leaf
, fi
);
1236 extent_num_bytes
= inode
->i_size
-
1237 found_key
.offset
+ root
->sectorsize
- 1;
1238 extent_num_bytes
= extent_num_bytes
&
1239 ~((u64
)root
->sectorsize
- 1);
1240 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1242 num_dec
= (orig_num_bytes
-
1244 if (extent_start
!= 0)
1245 dec_i_blocks(inode
, num_dec
);
1246 btrfs_mark_buffer_dirty(leaf
);
1249 btrfs_file_extent_disk_num_bytes(leaf
,
1251 /* FIXME blocksize != 4096 */
1252 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1253 if (extent_start
!= 0) {
1255 dec_i_blocks(inode
, num_dec
);
1257 root_gen
= btrfs_header_generation(leaf
);
1258 root_owner
= btrfs_header_owner(leaf
);
1260 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1262 u32 newsize
= inode
->i_size
- found_key
.offset
;
1263 dec_i_blocks(inode
, item_end
+ 1 -
1264 found_key
.offset
- newsize
);
1266 btrfs_file_extent_calc_inline_size(newsize
);
1267 ret
= btrfs_truncate_item(trans
, root
, path
,
1271 dec_i_blocks(inode
, item_end
+ 1 -
1277 if (!pending_del_nr
) {
1278 /* no pending yet, add ourselves */
1279 pending_del_slot
= path
->slots
[0];
1281 } else if (pending_del_nr
&&
1282 path
->slots
[0] + 1 == pending_del_slot
) {
1283 /* hop on the pending chunk */
1285 pending_del_slot
= path
->slots
[0];
1287 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1293 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1296 root_gen
, inode
->i_ino
,
1297 found_key
.offset
, 0);
1301 if (path
->slots
[0] == 0) {
1304 btrfs_release_path(root
, path
);
1309 if (pending_del_nr
&&
1310 path
->slots
[0] + 1 != pending_del_slot
) {
1311 struct btrfs_key debug
;
1313 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1315 ret
= btrfs_del_items(trans
, root
, path
,
1320 btrfs_release_path(root
, path
);
1326 if (pending_del_nr
) {
1327 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1330 btrfs_free_path(path
);
1331 inode
->i_sb
->s_dirt
= 1;
1336 * taken from block_truncate_page, but does cow as it zeros out
1337 * any bytes left in the last page in the file.
1339 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1341 struct inode
*inode
= mapping
->host
;
1342 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1343 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1344 struct btrfs_ordered_extent
*ordered
;
1346 u32 blocksize
= root
->sectorsize
;
1347 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1348 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1354 if ((offset
& (blocksize
- 1)) == 0)
1359 page
= grab_cache_page(mapping
, index
);
1363 page_start
= page_offset(page
);
1364 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1366 if (!PageUptodate(page
)) {
1367 ret
= btrfs_readpage(NULL
, page
);
1369 if (page
->mapping
!= mapping
) {
1371 page_cache_release(page
);
1374 if (!PageUptodate(page
)) {
1379 wait_on_page_writeback(page
);
1381 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1382 set_page_extent_mapped(page
);
1384 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1386 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1388 page_cache_release(page
);
1389 btrfs_start_ordered_extent(inode
, ordered
, 1);
1390 btrfs_put_ordered_extent(ordered
);
1394 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1395 page_end
, GFP_NOFS
);
1397 if (offset
!= PAGE_CACHE_SIZE
) {
1399 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1400 flush_dcache_page(page
);
1403 ClearPageChecked(page
);
1404 set_page_dirty(page
);
1405 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1408 page_cache_release(page
);
1413 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1415 struct inode
*inode
= dentry
->d_inode
;
1418 err
= inode_change_ok(inode
, attr
);
1422 if (S_ISREG(inode
->i_mode
) &&
1423 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1424 struct btrfs_trans_handle
*trans
;
1425 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1426 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1428 u64 mask
= root
->sectorsize
- 1;
1429 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1430 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1434 if (attr
->ia_size
<= hole_start
)
1437 err
= btrfs_check_free_space(root
, 1, 0);
1441 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1443 hole_size
= block_end
- hole_start
;
1444 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1445 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1447 trans
= btrfs_start_transaction(root
, 1);
1448 btrfs_set_trans_block_group(trans
, inode
);
1449 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1450 err
= btrfs_drop_extents(trans
, root
, inode
,
1451 hole_start
, block_end
, hole_start
,
1454 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1455 err
= btrfs_insert_file_extent(trans
, root
,
1459 btrfs_drop_extent_cache(inode
, hole_start
,
1461 btrfs_check_file(root
, inode
);
1463 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1464 btrfs_end_transaction(trans
, root
);
1465 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1470 err
= inode_setattr(inode
, attr
);
1475 void btrfs_delete_inode(struct inode
*inode
)
1477 struct btrfs_trans_handle
*trans
;
1478 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1482 truncate_inode_pages(&inode
->i_data
, 0);
1483 if (is_bad_inode(inode
)) {
1486 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1488 btrfs_i_size_write(inode
, 0);
1489 trans
= btrfs_start_transaction(root
, 1);
1491 btrfs_set_trans_block_group(trans
, inode
);
1492 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1494 goto no_delete_lock
;
1496 nr
= trans
->blocks_used
;
1499 btrfs_end_transaction(trans
, root
);
1500 btrfs_btree_balance_dirty(root
, nr
);
1504 nr
= trans
->blocks_used
;
1505 btrfs_end_transaction(trans
, root
);
1506 btrfs_btree_balance_dirty(root
, nr
);
1512 * this returns the key found in the dir entry in the location pointer.
1513 * If no dir entries were found, location->objectid is 0.
1515 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1516 struct btrfs_key
*location
)
1518 const char *name
= dentry
->d_name
.name
;
1519 int namelen
= dentry
->d_name
.len
;
1520 struct btrfs_dir_item
*di
;
1521 struct btrfs_path
*path
;
1522 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1525 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1526 location
->objectid
= dir
->i_ino
;
1527 location
->type
= BTRFS_INODE_ITEM_KEY
;
1528 location
->offset
= 0;
1531 path
= btrfs_alloc_path();
1534 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1535 struct btrfs_key key
;
1536 struct extent_buffer
*leaf
;
1540 key
.objectid
= dir
->i_ino
;
1541 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1543 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1547 leaf
= path
->nodes
[0];
1548 slot
= path
->slots
[0];
1549 nritems
= btrfs_header_nritems(leaf
);
1550 if (slot
>= nritems
)
1553 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1554 if (key
.objectid
!= dir
->i_ino
||
1555 key
.type
!= BTRFS_INODE_REF_KEY
) {
1558 location
->objectid
= key
.offset
;
1559 location
->type
= BTRFS_INODE_ITEM_KEY
;
1560 location
->offset
= 0;
1564 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1568 if (!di
|| IS_ERR(di
)) {
1571 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1573 btrfs_free_path(path
);
1576 location
->objectid
= 0;
1581 * when we hit a tree root in a directory, the btrfs part of the inode
1582 * needs to be changed to reflect the root directory of the tree root. This
1583 * is kind of like crossing a mount point.
1585 static int fixup_tree_root_location(struct btrfs_root
*root
,
1586 struct btrfs_key
*location
,
1587 struct btrfs_root
**sub_root
,
1588 struct dentry
*dentry
)
1590 struct btrfs_path
*path
;
1591 struct btrfs_root_item
*ri
;
1593 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1595 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1598 path
= btrfs_alloc_path();
1601 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1602 dentry
->d_name
.name
,
1603 dentry
->d_name
.len
);
1604 if (IS_ERR(*sub_root
))
1605 return PTR_ERR(*sub_root
);
1607 ri
= &(*sub_root
)->root_item
;
1608 location
->objectid
= btrfs_root_dirid(ri
);
1609 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1610 location
->offset
= 0;
1612 btrfs_free_path(path
);
1616 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1618 struct btrfs_iget_args
*args
= p
;
1619 inode
->i_ino
= args
->ino
;
1620 BTRFS_I(inode
)->root
= args
->root
;
1621 BTRFS_I(inode
)->delalloc_bytes
= 0;
1622 BTRFS_I(inode
)->disk_i_size
= 0;
1623 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1624 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1625 inode
->i_mapping
, GFP_NOFS
);
1626 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1627 inode
->i_mapping
, GFP_NOFS
);
1628 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1629 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1630 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1634 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1636 struct btrfs_iget_args
*args
= opaque
;
1637 return (args
->ino
== inode
->i_ino
&&
1638 args
->root
== BTRFS_I(inode
)->root
);
1641 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1644 struct btrfs_iget_args args
;
1645 args
.ino
= objectid
;
1646 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1651 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1654 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1655 struct btrfs_root
*root
)
1657 struct inode
*inode
;
1658 struct btrfs_iget_args args
;
1659 args
.ino
= objectid
;
1662 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1663 btrfs_init_locked_inode
,
1668 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1669 struct nameidata
*nd
)
1671 struct inode
* inode
;
1672 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1673 struct btrfs_root
*root
= bi
->root
;
1674 struct btrfs_root
*sub_root
= root
;
1675 struct btrfs_key location
;
1678 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1679 return ERR_PTR(-ENAMETOOLONG
);
1681 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1684 return ERR_PTR(ret
);
1687 if (location
.objectid
) {
1688 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1691 return ERR_PTR(ret
);
1693 return ERR_PTR(-ENOENT
);
1694 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1697 return ERR_PTR(-EACCES
);
1698 if (inode
->i_state
& I_NEW
) {
1699 /* the inode and parent dir are two different roots */
1700 if (sub_root
!= root
) {
1702 sub_root
->inode
= inode
;
1704 BTRFS_I(inode
)->root
= sub_root
;
1705 memcpy(&BTRFS_I(inode
)->location
, &location
,
1707 btrfs_read_locked_inode(inode
);
1708 unlock_new_inode(inode
);
1711 return d_splice_alias(inode
, dentry
);
1714 static unsigned char btrfs_filetype_table
[] = {
1715 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1718 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1720 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1721 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1722 struct btrfs_item
*item
;
1723 struct btrfs_dir_item
*di
;
1724 struct btrfs_key key
;
1725 struct btrfs_key found_key
;
1726 struct btrfs_path
*path
;
1729 struct extent_buffer
*leaf
;
1732 unsigned char d_type
;
1737 int key_type
= BTRFS_DIR_INDEX_KEY
;
1742 /* FIXME, use a real flag for deciding about the key type */
1743 if (root
->fs_info
->tree_root
== root
)
1744 key_type
= BTRFS_DIR_ITEM_KEY
;
1746 /* special case for "." */
1747 if (filp
->f_pos
== 0) {
1748 over
= filldir(dirent
, ".", 1,
1756 key
.objectid
= inode
->i_ino
;
1757 path
= btrfs_alloc_path();
1760 /* special case for .., just use the back ref */
1761 if (filp
->f_pos
== 1) {
1762 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1764 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1766 leaf
= path
->nodes
[0];
1767 slot
= path
->slots
[0];
1768 nritems
= btrfs_header_nritems(leaf
);
1769 if (slot
>= nritems
) {
1770 btrfs_release_path(root
, path
);
1771 goto read_dir_items
;
1773 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1774 btrfs_release_path(root
, path
);
1775 if (found_key
.objectid
!= key
.objectid
||
1776 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1777 goto read_dir_items
;
1778 over
= filldir(dirent
, "..", 2,
1779 2, found_key
.offset
, DT_DIR
);
1786 btrfs_set_key_type(&key
, key_type
);
1787 key
.offset
= filp
->f_pos
;
1789 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1794 leaf
= path
->nodes
[0];
1795 nritems
= btrfs_header_nritems(leaf
);
1796 slot
= path
->slots
[0];
1797 if (advance
|| slot
>= nritems
) {
1798 if (slot
>= nritems
-1) {
1799 ret
= btrfs_next_leaf(root
, path
);
1802 leaf
= path
->nodes
[0];
1803 nritems
= btrfs_header_nritems(leaf
);
1804 slot
= path
->slots
[0];
1811 item
= btrfs_item_nr(leaf
, slot
);
1812 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1814 if (found_key
.objectid
!= key
.objectid
)
1816 if (btrfs_key_type(&found_key
) != key_type
)
1818 if (found_key
.offset
< filp
->f_pos
)
1821 filp
->f_pos
= found_key
.offset
;
1823 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1825 di_total
= btrfs_item_size(leaf
, item
);
1826 while(di_cur
< di_total
) {
1827 struct btrfs_key location
;
1829 name_len
= btrfs_dir_name_len(leaf
, di
);
1830 if (name_len
< 32) {
1831 name_ptr
= tmp_name
;
1833 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1836 read_extent_buffer(leaf
, name_ptr
,
1837 (unsigned long)(di
+ 1), name_len
);
1839 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1840 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1841 over
= filldir(dirent
, name_ptr
, name_len
,
1846 if (name_ptr
!= tmp_name
)
1851 di_len
= btrfs_dir_name_len(leaf
, di
) +
1852 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1854 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1857 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1858 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1864 btrfs_free_path(path
);
1868 int btrfs_write_inode(struct inode
*inode
, int wait
)
1870 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 struct btrfs_trans_handle
*trans
;
1875 trans
= btrfs_join_transaction(root
, 1);
1876 btrfs_set_trans_block_group(trans
, inode
);
1877 ret
= btrfs_commit_transaction(trans
, root
);
1883 * This is somewhat expensive, updating the tree every time the
1884 * inode changes. But, it is most likely to find the inode in cache.
1885 * FIXME, needs more benchmarking...there are no reasons other than performance
1886 * to keep or drop this code.
1888 void btrfs_dirty_inode(struct inode
*inode
)
1890 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1891 struct btrfs_trans_handle
*trans
;
1893 trans
= btrfs_join_transaction(root
, 1);
1894 btrfs_set_trans_block_group(trans
, inode
);
1895 btrfs_update_inode(trans
, root
, inode
);
1896 btrfs_end_transaction(trans
, root
);
1899 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1900 struct btrfs_root
*root
,
1901 const char *name
, int name_len
,
1904 struct btrfs_block_group_cache
*group
,
1907 struct inode
*inode
;
1908 struct btrfs_inode_item
*inode_item
;
1909 struct btrfs_block_group_cache
*new_inode_group
;
1910 struct btrfs_key
*location
;
1911 struct btrfs_path
*path
;
1912 struct btrfs_inode_ref
*ref
;
1913 struct btrfs_key key
[2];
1919 path
= btrfs_alloc_path();
1922 inode
= new_inode(root
->fs_info
->sb
);
1924 return ERR_PTR(-ENOMEM
);
1926 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1927 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1928 inode
->i_mapping
, GFP_NOFS
);
1929 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1930 inode
->i_mapping
, GFP_NOFS
);
1931 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1932 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1933 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1934 BTRFS_I(inode
)->delalloc_bytes
= 0;
1935 BTRFS_I(inode
)->disk_i_size
= 0;
1936 BTRFS_I(inode
)->root
= root
;
1942 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1943 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1944 if (!new_inode_group
) {
1945 printk("find_block group failed\n");
1946 new_inode_group
= group
;
1948 BTRFS_I(inode
)->block_group
= new_inode_group
;
1949 BTRFS_I(inode
)->flags
= 0;
1951 key
[0].objectid
= objectid
;
1952 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1955 key
[1].objectid
= objectid
;
1956 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1957 key
[1].offset
= ref_objectid
;
1959 sizes
[0] = sizeof(struct btrfs_inode_item
);
1960 sizes
[1] = name_len
+ sizeof(*ref
);
1962 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1966 if (objectid
> root
->highest_inode
)
1967 root
->highest_inode
= objectid
;
1969 inode
->i_uid
= current
->fsuid
;
1970 inode
->i_gid
= current
->fsgid
;
1971 inode
->i_mode
= mode
;
1972 inode
->i_ino
= objectid
;
1973 inode
->i_blocks
= 0;
1974 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1975 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1976 struct btrfs_inode_item
);
1977 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1979 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1980 struct btrfs_inode_ref
);
1981 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1982 ptr
= (unsigned long)(ref
+ 1);
1983 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1985 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1986 btrfs_free_path(path
);
1988 location
= &BTRFS_I(inode
)->location
;
1989 location
->objectid
= objectid
;
1990 location
->offset
= 0;
1991 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1993 insert_inode_hash(inode
);
1996 btrfs_free_path(path
);
1997 return ERR_PTR(ret
);
2000 static inline u8
btrfs_inode_type(struct inode
*inode
)
2002 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2005 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2006 struct dentry
*dentry
, struct inode
*inode
,
2010 struct btrfs_key key
;
2011 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
2012 struct inode
*parent_inode
;
2014 key
.objectid
= inode
->i_ino
;
2015 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2018 ret
= btrfs_insert_dir_item(trans
, root
,
2019 dentry
->d_name
.name
, dentry
->d_name
.len
,
2020 dentry
->d_parent
->d_inode
->i_ino
,
2021 &key
, btrfs_inode_type(inode
));
2024 ret
= btrfs_insert_inode_ref(trans
, root
,
2025 dentry
->d_name
.name
,
2028 dentry
->d_parent
->d_inode
->i_ino
);
2030 parent_inode
= dentry
->d_parent
->d_inode
;
2031 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2032 dentry
->d_name
.len
* 2);
2033 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2034 ret
= btrfs_update_inode(trans
, root
,
2035 dentry
->d_parent
->d_inode
);
2040 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2041 struct dentry
*dentry
, struct inode
*inode
,
2044 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
2046 d_instantiate(dentry
, inode
);
2054 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2055 int mode
, dev_t rdev
)
2057 struct btrfs_trans_handle
*trans
;
2058 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2059 struct inode
*inode
= NULL
;
2063 unsigned long nr
= 0;
2065 if (!new_valid_dev(rdev
))
2068 err
= btrfs_check_free_space(root
, 1, 0);
2072 trans
= btrfs_start_transaction(root
, 1);
2073 btrfs_set_trans_block_group(trans
, dir
);
2075 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2081 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2083 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2084 BTRFS_I(dir
)->block_group
, mode
);
2085 err
= PTR_ERR(inode
);
2089 btrfs_set_trans_block_group(trans
, inode
);
2090 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2094 inode
->i_op
= &btrfs_special_inode_operations
;
2095 init_special_inode(inode
, inode
->i_mode
, rdev
);
2096 btrfs_update_inode(trans
, root
, inode
);
2098 dir
->i_sb
->s_dirt
= 1;
2099 btrfs_update_inode_block_group(trans
, inode
);
2100 btrfs_update_inode_block_group(trans
, dir
);
2102 nr
= trans
->blocks_used
;
2103 btrfs_end_transaction_throttle(trans
, root
);
2106 inode_dec_link_count(inode
);
2109 btrfs_btree_balance_dirty(root
, nr
);
2113 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2114 int mode
, struct nameidata
*nd
)
2116 struct btrfs_trans_handle
*trans
;
2117 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2118 struct inode
*inode
= NULL
;
2121 unsigned long nr
= 0;
2124 err
= btrfs_check_free_space(root
, 1, 0);
2127 trans
= btrfs_start_transaction(root
, 1);
2128 btrfs_set_trans_block_group(trans
, dir
);
2130 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2136 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2138 dentry
->d_parent
->d_inode
->i_ino
,
2139 objectid
, BTRFS_I(dir
)->block_group
, mode
);
2140 err
= PTR_ERR(inode
);
2144 btrfs_set_trans_block_group(trans
, inode
);
2145 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2149 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2150 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2151 inode
->i_fop
= &btrfs_file_operations
;
2152 inode
->i_op
= &btrfs_file_inode_operations
;
2153 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2154 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2155 inode
->i_mapping
, GFP_NOFS
);
2156 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2157 inode
->i_mapping
, GFP_NOFS
);
2158 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2159 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2160 BTRFS_I(inode
)->delalloc_bytes
= 0;
2161 BTRFS_I(inode
)->disk_i_size
= 0;
2162 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2163 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2165 dir
->i_sb
->s_dirt
= 1;
2166 btrfs_update_inode_block_group(trans
, inode
);
2167 btrfs_update_inode_block_group(trans
, dir
);
2169 nr
= trans
->blocks_used
;
2170 btrfs_end_transaction_throttle(trans
, root
);
2173 inode_dec_link_count(inode
);
2176 btrfs_btree_balance_dirty(root
, nr
);
2180 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2181 struct dentry
*dentry
)
2183 struct btrfs_trans_handle
*trans
;
2184 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2185 struct inode
*inode
= old_dentry
->d_inode
;
2186 unsigned long nr
= 0;
2190 if (inode
->i_nlink
== 0)
2193 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2198 err
= btrfs_check_free_space(root
, 1, 0);
2201 trans
= btrfs_start_transaction(root
, 1);
2203 btrfs_set_trans_block_group(trans
, dir
);
2204 atomic_inc(&inode
->i_count
);
2205 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2210 dir
->i_sb
->s_dirt
= 1;
2211 btrfs_update_inode_block_group(trans
, dir
);
2212 err
= btrfs_update_inode(trans
, root
, inode
);
2217 nr
= trans
->blocks_used
;
2218 btrfs_end_transaction_throttle(trans
, root
);
2221 inode_dec_link_count(inode
);
2224 btrfs_btree_balance_dirty(root
, nr
);
2228 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2230 struct inode
*inode
= NULL
;
2231 struct btrfs_trans_handle
*trans
;
2232 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2234 int drop_on_err
= 0;
2236 unsigned long nr
= 1;
2238 err
= btrfs_check_free_space(root
, 1, 0);
2242 trans
= btrfs_start_transaction(root
, 1);
2243 btrfs_set_trans_block_group(trans
, dir
);
2245 if (IS_ERR(trans
)) {
2246 err
= PTR_ERR(trans
);
2250 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2256 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2258 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2259 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2260 if (IS_ERR(inode
)) {
2261 err
= PTR_ERR(inode
);
2266 inode
->i_op
= &btrfs_dir_inode_operations
;
2267 inode
->i_fop
= &btrfs_dir_file_operations
;
2268 btrfs_set_trans_block_group(trans
, inode
);
2270 btrfs_i_size_write(inode
, 0);
2271 err
= btrfs_update_inode(trans
, root
, inode
);
2275 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2279 d_instantiate(dentry
, inode
);
2281 dir
->i_sb
->s_dirt
= 1;
2282 btrfs_update_inode_block_group(trans
, inode
);
2283 btrfs_update_inode_block_group(trans
, dir
);
2286 nr
= trans
->blocks_used
;
2287 btrfs_end_transaction_throttle(trans
, root
);
2292 btrfs_btree_balance_dirty(root
, nr
);
2296 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2297 struct extent_map
*existing
,
2298 struct extent_map
*em
,
2299 u64 map_start
, u64 map_len
)
2303 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2304 start_diff
= map_start
- em
->start
;
2305 em
->start
= map_start
;
2307 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2308 em
->block_start
+= start_diff
;
2309 return add_extent_mapping(em_tree
, em
);
2312 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2313 size_t pg_offset
, u64 start
, u64 len
,
2319 u64 extent_start
= 0;
2321 u64 objectid
= inode
->i_ino
;
2323 struct btrfs_path
*path
= NULL
;
2324 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2325 struct btrfs_file_extent_item
*item
;
2326 struct extent_buffer
*leaf
;
2327 struct btrfs_key found_key
;
2328 struct extent_map
*em
= NULL
;
2329 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2330 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2331 struct btrfs_trans_handle
*trans
= NULL
;
2334 spin_lock(&em_tree
->lock
);
2335 em
= lookup_extent_mapping(em_tree
, start
, len
);
2337 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2338 spin_unlock(&em_tree
->lock
);
2341 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2342 free_extent_map(em
);
2343 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2344 free_extent_map(em
);
2348 em
= alloc_extent_map(GFP_NOFS
);
2353 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2354 em
->start
= EXTENT_MAP_HOLE
;
2358 path
= btrfs_alloc_path();
2362 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2363 objectid
, start
, trans
!= NULL
);
2370 if (path
->slots
[0] == 0)
2375 leaf
= path
->nodes
[0];
2376 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2377 struct btrfs_file_extent_item
);
2378 /* are we inside the extent that was found? */
2379 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2380 found_type
= btrfs_key_type(&found_key
);
2381 if (found_key
.objectid
!= objectid
||
2382 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2386 found_type
= btrfs_file_extent_type(leaf
, item
);
2387 extent_start
= found_key
.offset
;
2388 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2389 extent_end
= extent_start
+
2390 btrfs_file_extent_num_bytes(leaf
, item
);
2392 if (start
< extent_start
|| start
>= extent_end
) {
2394 if (start
< extent_start
) {
2395 if (start
+ len
<= extent_start
)
2397 em
->len
= extent_end
- extent_start
;
2403 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2405 em
->start
= extent_start
;
2406 em
->len
= extent_end
- extent_start
;
2407 em
->block_start
= EXTENT_MAP_HOLE
;
2410 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2411 em
->block_start
= bytenr
;
2412 em
->start
= extent_start
;
2413 em
->len
= extent_end
- extent_start
;
2415 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2420 size_t extent_offset
;
2423 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2425 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2426 ~((u64
)root
->sectorsize
- 1);
2427 if (start
< extent_start
|| start
>= extent_end
) {
2429 if (start
< extent_start
) {
2430 if (start
+ len
<= extent_start
)
2432 em
->len
= extent_end
- extent_start
;
2438 em
->block_start
= EXTENT_MAP_INLINE
;
2441 em
->start
= extent_start
;
2446 page_start
= page_offset(page
) + pg_offset
;
2447 extent_offset
= page_start
- extent_start
;
2448 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2449 size
- extent_offset
);
2450 em
->start
= extent_start
+ extent_offset
;
2451 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2452 ~((u64
)root
->sectorsize
- 1);
2454 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2455 if (create
== 0 && !PageUptodate(page
)) {
2456 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2458 flush_dcache_page(page
);
2459 } else if (create
&& PageUptodate(page
)) {
2462 free_extent_map(em
);
2464 btrfs_release_path(root
, path
);
2465 trans
= btrfs_join_transaction(root
, 1);
2468 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2470 btrfs_mark_buffer_dirty(leaf
);
2473 set_extent_uptodate(io_tree
, em
->start
,
2474 extent_map_end(em
) - 1, GFP_NOFS
);
2477 printk("unkknown found_type %d\n", found_type
);
2484 em
->block_start
= EXTENT_MAP_HOLE
;
2486 btrfs_release_path(root
, path
);
2487 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2488 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2494 spin_lock(&em_tree
->lock
);
2495 ret
= add_extent_mapping(em_tree
, em
);
2496 /* it is possible that someone inserted the extent into the tree
2497 * while we had the lock dropped. It is also possible that
2498 * an overlapping map exists in the tree
2500 if (ret
== -EEXIST
) {
2501 struct extent_map
*existing
;
2505 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2506 if (existing
&& (existing
->start
> start
||
2507 existing
->start
+ existing
->len
<= start
)) {
2508 free_extent_map(existing
);
2512 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2515 err
= merge_extent_mapping(em_tree
, existing
,
2518 free_extent_map(existing
);
2520 free_extent_map(em
);
2525 printk("failing to insert %Lu %Lu\n",
2527 free_extent_map(em
);
2531 free_extent_map(em
);
2536 spin_unlock(&em_tree
->lock
);
2539 btrfs_free_path(path
);
2541 ret
= btrfs_end_transaction(trans
, root
);
2547 free_extent_map(em
);
2549 return ERR_PTR(err
);
2554 #if 0 /* waiting for O_DIRECT reads */
2555 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2556 struct buffer_head
*bh_result
, int create
)
2558 struct extent_map
*em
;
2559 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2560 struct btrfs_multi_bio
*multi
= NULL
;
2561 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2567 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2569 if (!em
|| IS_ERR(em
))
2572 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2576 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2581 len
= em
->start
+ em
->len
- start
;
2582 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2584 if (em
->block_start
== EXTENT_MAP_HOLE
||
2585 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2586 bh_result
->b_size
= len
;
2590 logical
= start
- em
->start
;
2591 logical
= em
->block_start
+ logical
;
2594 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2595 logical
, &map_length
, &multi
, 0);
2597 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2598 bh_result
->b_size
= min(map_length
, len
);
2600 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2601 set_buffer_mapped(bh_result
);
2604 free_extent_map(em
);
2609 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2610 const struct iovec
*iov
, loff_t offset
,
2611 unsigned long nr_segs
)
2615 struct file
*file
= iocb
->ki_filp
;
2616 struct inode
*inode
= file
->f_mapping
->host
;
2621 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2622 offset
, nr_segs
, btrfs_get_block
, NULL
);
2626 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2628 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2631 int btrfs_readpage(struct file
*file
, struct page
*page
)
2633 struct extent_io_tree
*tree
;
2634 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2635 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2638 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2640 struct extent_io_tree
*tree
;
2643 if (current
->flags
& PF_MEMALLOC
) {
2644 redirty_page_for_writepage(wbc
, page
);
2648 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2649 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2652 int btrfs_writepages(struct address_space
*mapping
,
2653 struct writeback_control
*wbc
)
2655 struct extent_io_tree
*tree
;
2656 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2657 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2661 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2662 struct list_head
*pages
, unsigned nr_pages
)
2664 struct extent_io_tree
*tree
;
2665 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2666 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2669 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2671 struct extent_io_tree
*tree
;
2672 struct extent_map_tree
*map
;
2675 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2676 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2677 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2679 ClearPagePrivate(page
);
2680 set_page_private(page
, 0);
2681 page_cache_release(page
);
2686 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2688 return __btrfs_releasepage(page
, gfp_flags
);
2691 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2693 struct extent_io_tree
*tree
;
2694 struct btrfs_ordered_extent
*ordered
;
2695 u64 page_start
= page_offset(page
);
2696 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2698 wait_on_page_writeback(page
);
2699 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2701 btrfs_releasepage(page
, GFP_NOFS
);
2705 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2706 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2710 * IO on this page will never be started, so we need
2711 * to account for any ordered extents now
2713 clear_extent_bit(tree
, page_start
, page_end
,
2714 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2715 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2716 btrfs_finish_ordered_io(page
->mapping
->host
,
2717 page_start
, page_end
);
2718 btrfs_put_ordered_extent(ordered
);
2719 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2721 clear_extent_bit(tree
, page_start
, page_end
,
2722 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2725 __btrfs_releasepage(page
, GFP_NOFS
);
2727 ClearPageChecked(page
);
2728 if (PagePrivate(page
)) {
2729 ClearPagePrivate(page
);
2730 set_page_private(page
, 0);
2731 page_cache_release(page
);
2736 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2737 * called from a page fault handler when a page is first dirtied. Hence we must
2738 * be careful to check for EOF conditions here. We set the page up correctly
2739 * for a written page which means we get ENOSPC checking when writing into
2740 * holes and correct delalloc and unwritten extent mapping on filesystems that
2741 * support these features.
2743 * We are not allowed to take the i_mutex here so we have to play games to
2744 * protect against truncate races as the page could now be beyond EOF. Because
2745 * vmtruncate() writes the inode size before removing pages, once we have the
2746 * page lock we can determine safely if the page is beyond EOF. If it is not
2747 * beyond EOF, then the page is guaranteed safe against truncation until we
2750 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2752 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2753 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2754 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2755 struct btrfs_ordered_extent
*ordered
;
2757 unsigned long zero_start
;
2763 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2770 size
= i_size_read(inode
);
2771 page_start
= page_offset(page
);
2772 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2774 if ((page
->mapping
!= inode
->i_mapping
) ||
2775 (page_start
>= size
)) {
2776 /* page got truncated out from underneath us */
2779 wait_on_page_writeback(page
);
2781 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2782 set_page_extent_mapped(page
);
2785 * we can't set the delalloc bits if there are pending ordered
2786 * extents. Drop our locks and wait for them to finish
2788 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2790 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2792 btrfs_start_ordered_extent(inode
, ordered
, 1);
2793 btrfs_put_ordered_extent(ordered
);
2797 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
2798 page_end
, GFP_NOFS
);
2801 /* page is wholly or partially inside EOF */
2802 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2803 zero_start
= size
& ~PAGE_CACHE_MASK
;
2805 zero_start
= PAGE_CACHE_SIZE
;
2807 if (zero_start
!= PAGE_CACHE_SIZE
) {
2809 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
2810 flush_dcache_page(page
);
2813 ClearPageChecked(page
);
2814 set_page_dirty(page
);
2815 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2823 static void btrfs_truncate(struct inode
*inode
)
2825 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2827 struct btrfs_trans_handle
*trans
;
2829 u64 mask
= root
->sectorsize
- 1;
2831 if (!S_ISREG(inode
->i_mode
))
2833 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2836 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2837 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
2839 trans
= btrfs_start_transaction(root
, 1);
2840 btrfs_set_trans_block_group(trans
, inode
);
2841 btrfs_i_size_write(inode
, inode
->i_size
);
2843 /* FIXME, add redo link to tree so we don't leak on crash */
2844 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2845 BTRFS_EXTENT_DATA_KEY
);
2846 btrfs_update_inode(trans
, root
, inode
);
2847 nr
= trans
->blocks_used
;
2849 ret
= btrfs_end_transaction_throttle(trans
, root
);
2851 btrfs_btree_balance_dirty(root
, nr
);
2855 * Invalidate a single dcache entry at the root of the filesystem.
2856 * Needed after creation of snapshot or subvolume.
2858 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
2861 struct dentry
*alias
, *entry
;
2864 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
2868 /* change me if btrfs ever gets a d_hash operation */
2869 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
2870 entry
= d_lookup(alias
, &qstr
);
2873 d_invalidate(entry
);
2879 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
2880 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
2881 struct btrfs_block_group_cache
*block_group
)
2883 struct inode
*inode
;
2886 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2887 new_dirid
, block_group
, S_IFDIR
| 0700);
2889 return PTR_ERR(inode
);
2890 inode
->i_op
= &btrfs_dir_inode_operations
;
2891 inode
->i_fop
= &btrfs_dir_file_operations
;
2892 new_root
->inode
= inode
;
2894 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2897 btrfs_i_size_write(inode
, 0);
2899 return btrfs_update_inode(trans
, new_root
, inode
);
2902 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2903 struct file_ra_state
*ra
, struct file
*file
,
2904 pgoff_t offset
, pgoff_t last_index
)
2906 pgoff_t req_size
= last_index
- offset
+ 1;
2908 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2909 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2912 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2913 return offset
+ req_size
;
2917 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2919 struct btrfs_inode
*ei
;
2921 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2925 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
2926 return &ei
->vfs_inode
;
2929 void btrfs_destroy_inode(struct inode
*inode
)
2931 struct btrfs_ordered_extent
*ordered
;
2932 WARN_ON(!list_empty(&inode
->i_dentry
));
2933 WARN_ON(inode
->i_data
.nrpages
);
2936 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
2940 printk("found ordered extent %Lu %Lu\n",
2941 ordered
->file_offset
, ordered
->len
);
2942 btrfs_remove_ordered_extent(inode
, ordered
);
2943 btrfs_put_ordered_extent(ordered
);
2944 btrfs_put_ordered_extent(ordered
);
2947 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2948 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2951 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2952 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2954 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2955 unsigned long flags
)
2958 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2960 inode_init_once(&ei
->vfs_inode
);
2963 void btrfs_destroy_cachep(void)
2965 if (btrfs_inode_cachep
)
2966 kmem_cache_destroy(btrfs_inode_cachep
);
2967 if (btrfs_trans_handle_cachep
)
2968 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2969 if (btrfs_transaction_cachep
)
2970 kmem_cache_destroy(btrfs_transaction_cachep
);
2971 if (btrfs_bit_radix_cachep
)
2972 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2973 if (btrfs_path_cachep
)
2974 kmem_cache_destroy(btrfs_path_cachep
);
2977 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2978 unsigned long extra_flags
,
2979 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2980 void (*ctor
)(struct kmem_cache
*, void *)
2982 void (*ctor
)(void *, struct kmem_cache
*,
2987 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2988 SLAB_MEM_SPREAD
| extra_flags
), ctor
2989 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2995 int btrfs_init_cachep(void)
2997 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2998 sizeof(struct btrfs_inode
),
3000 if (!btrfs_inode_cachep
)
3002 btrfs_trans_handle_cachep
=
3003 btrfs_cache_create("btrfs_trans_handle_cache",
3004 sizeof(struct btrfs_trans_handle
),
3006 if (!btrfs_trans_handle_cachep
)
3008 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3009 sizeof(struct btrfs_transaction
),
3011 if (!btrfs_transaction_cachep
)
3013 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3014 sizeof(struct btrfs_path
),
3016 if (!btrfs_path_cachep
)
3018 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3019 SLAB_DESTROY_BY_RCU
, NULL
);
3020 if (!btrfs_bit_radix_cachep
)
3024 btrfs_destroy_cachep();
3028 static int btrfs_getattr(struct vfsmount
*mnt
,
3029 struct dentry
*dentry
, struct kstat
*stat
)
3031 struct inode
*inode
= dentry
->d_inode
;
3032 generic_fillattr(inode
, stat
);
3033 stat
->blksize
= PAGE_CACHE_SIZE
;
3034 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3038 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3039 struct inode
* new_dir
,struct dentry
*new_dentry
)
3041 struct btrfs_trans_handle
*trans
;
3042 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3043 struct inode
*new_inode
= new_dentry
->d_inode
;
3044 struct inode
*old_inode
= old_dentry
->d_inode
;
3045 struct timespec ctime
= CURRENT_TIME
;
3048 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3049 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3053 ret
= btrfs_check_free_space(root
, 1, 0);
3057 trans
= btrfs_start_transaction(root
, 1);
3059 btrfs_set_trans_block_group(trans
, new_dir
);
3061 old_dentry
->d_inode
->i_nlink
++;
3062 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3063 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3064 old_inode
->i_ctime
= ctime
;
3066 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3071 new_inode
->i_ctime
= CURRENT_TIME
;
3072 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3076 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3081 btrfs_end_transaction(trans
, root
);
3086 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3087 const char *symname
)
3089 struct btrfs_trans_handle
*trans
;
3090 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3091 struct btrfs_path
*path
;
3092 struct btrfs_key key
;
3093 struct inode
*inode
= NULL
;
3100 struct btrfs_file_extent_item
*ei
;
3101 struct extent_buffer
*leaf
;
3102 unsigned long nr
= 0;
3104 name_len
= strlen(symname
) + 1;
3105 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3106 return -ENAMETOOLONG
;
3108 err
= btrfs_check_free_space(root
, 1, 0);
3112 trans
= btrfs_start_transaction(root
, 1);
3113 btrfs_set_trans_block_group(trans
, dir
);
3115 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3121 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
3123 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3124 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3125 err
= PTR_ERR(inode
);
3129 btrfs_set_trans_block_group(trans
, inode
);
3130 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3134 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3135 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3136 inode
->i_fop
= &btrfs_file_operations
;
3137 inode
->i_op
= &btrfs_file_inode_operations
;
3138 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3139 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3140 inode
->i_mapping
, GFP_NOFS
);
3141 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3142 inode
->i_mapping
, GFP_NOFS
);
3143 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3144 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3145 BTRFS_I(inode
)->delalloc_bytes
= 0;
3146 BTRFS_I(inode
)->disk_i_size
= 0;
3147 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3148 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3150 dir
->i_sb
->s_dirt
= 1;
3151 btrfs_update_inode_block_group(trans
, inode
);
3152 btrfs_update_inode_block_group(trans
, dir
);
3156 path
= btrfs_alloc_path();
3158 key
.objectid
= inode
->i_ino
;
3160 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3161 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3162 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3168 leaf
= path
->nodes
[0];
3169 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3170 struct btrfs_file_extent_item
);
3171 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3172 btrfs_set_file_extent_type(leaf
, ei
,
3173 BTRFS_FILE_EXTENT_INLINE
);
3174 ptr
= btrfs_file_extent_inline_start(ei
);
3175 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3176 btrfs_mark_buffer_dirty(leaf
);
3177 btrfs_free_path(path
);
3179 inode
->i_op
= &btrfs_symlink_inode_operations
;
3180 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3181 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3182 btrfs_i_size_write(inode
, name_len
- 1);
3183 err
= btrfs_update_inode(trans
, root
, inode
);
3188 nr
= trans
->blocks_used
;
3189 btrfs_end_transaction_throttle(trans
, root
);
3192 inode_dec_link_count(inode
);
3195 btrfs_btree_balance_dirty(root
, nr
);
3199 static int btrfs_set_page_dirty(struct page
*page
)
3201 return __set_page_dirty_nobuffers(page
);
3204 static int btrfs_permission(struct inode
*inode
, int mask
,
3205 struct nameidata
*nd
)
3207 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3209 return generic_permission(inode
, mask
, NULL
);
3212 static struct inode_operations btrfs_dir_inode_operations
= {
3213 .lookup
= btrfs_lookup
,
3214 .create
= btrfs_create
,
3215 .unlink
= btrfs_unlink
,
3217 .mkdir
= btrfs_mkdir
,
3218 .rmdir
= btrfs_rmdir
,
3219 .rename
= btrfs_rename
,
3220 .symlink
= btrfs_symlink
,
3221 .setattr
= btrfs_setattr
,
3222 .mknod
= btrfs_mknod
,
3223 .setxattr
= generic_setxattr
,
3224 .getxattr
= generic_getxattr
,
3225 .listxattr
= btrfs_listxattr
,
3226 .removexattr
= generic_removexattr
,
3227 .permission
= btrfs_permission
,
3229 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3230 .lookup
= btrfs_lookup
,
3231 .permission
= btrfs_permission
,
3233 static struct file_operations btrfs_dir_file_operations
= {
3234 .llseek
= generic_file_llseek
,
3235 .read
= generic_read_dir
,
3236 .readdir
= btrfs_readdir
,
3237 .unlocked_ioctl
= btrfs_ioctl
,
3238 #ifdef CONFIG_COMPAT
3239 .compat_ioctl
= btrfs_ioctl
,
3241 .release
= btrfs_release_file
,
3244 static struct extent_io_ops btrfs_extent_io_ops
= {
3245 .fill_delalloc
= run_delalloc_range
,
3246 .submit_bio_hook
= btrfs_submit_bio_hook
,
3247 .merge_bio_hook
= btrfs_merge_bio_hook
,
3248 .readpage_io_hook
= btrfs_readpage_io_hook
,
3249 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3250 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3251 .writepage_start_hook
= btrfs_writepage_start_hook
,
3252 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3253 .set_bit_hook
= btrfs_set_bit_hook
,
3254 .clear_bit_hook
= btrfs_clear_bit_hook
,
3257 static struct address_space_operations btrfs_aops
= {
3258 .readpage
= btrfs_readpage
,
3259 .writepage
= btrfs_writepage
,
3260 .writepages
= btrfs_writepages
,
3261 .readpages
= btrfs_readpages
,
3262 .sync_page
= block_sync_page
,
3264 .direct_IO
= btrfs_direct_IO
,
3265 .invalidatepage
= btrfs_invalidatepage
,
3266 .releasepage
= btrfs_releasepage
,
3267 .set_page_dirty
= btrfs_set_page_dirty
,
3270 static struct address_space_operations btrfs_symlink_aops
= {
3271 .readpage
= btrfs_readpage
,
3272 .writepage
= btrfs_writepage
,
3273 .invalidatepage
= btrfs_invalidatepage
,
3274 .releasepage
= btrfs_releasepage
,
3277 static struct inode_operations btrfs_file_inode_operations
= {
3278 .truncate
= btrfs_truncate
,
3279 .getattr
= btrfs_getattr
,
3280 .setattr
= btrfs_setattr
,
3281 .setxattr
= generic_setxattr
,
3282 .getxattr
= generic_getxattr
,
3283 .listxattr
= btrfs_listxattr
,
3284 .removexattr
= generic_removexattr
,
3285 .permission
= btrfs_permission
,
3287 static struct inode_operations btrfs_special_inode_operations
= {
3288 .getattr
= btrfs_getattr
,
3289 .setattr
= btrfs_setattr
,
3290 .permission
= btrfs_permission
,
3292 static struct inode_operations btrfs_symlink_inode_operations
= {
3293 .readlink
= generic_readlink
,
3294 .follow_link
= page_follow_link_light
,
3295 .put_link
= page_put_link
,
3296 .permission
= btrfs_permission
,