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"
47 struct btrfs_iget_args
{
49 struct btrfs_root
*root
;
52 static struct inode_operations btrfs_dir_inode_operations
;
53 static struct inode_operations btrfs_symlink_inode_operations
;
54 static struct inode_operations btrfs_dir_ro_inode_operations
;
55 static struct inode_operations btrfs_special_inode_operations
;
56 static struct inode_operations btrfs_file_inode_operations
;
57 static struct address_space_operations btrfs_aops
;
58 static struct address_space_operations btrfs_symlink_aops
;
59 static struct file_operations btrfs_dir_file_operations
;
60 static struct extent_io_ops btrfs_extent_io_ops
;
62 static struct kmem_cache
*btrfs_inode_cachep
;
63 struct kmem_cache
*btrfs_trans_handle_cachep
;
64 struct kmem_cache
*btrfs_transaction_cachep
;
65 struct kmem_cache
*btrfs_bit_radix_cachep
;
66 struct kmem_cache
*btrfs_path_cachep
;
69 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
70 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
71 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
72 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
73 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
74 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
75 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
76 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
79 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
82 u64 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
83 u64 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
95 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
96 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
98 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
102 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
104 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
105 struct btrfs_trans_handle
*trans
;
109 u64 blocksize
= root
->sectorsize
;
110 u64 orig_start
= start
;
112 struct btrfs_key ins
;
115 trans
= btrfs_start_transaction(root
, 1);
117 btrfs_set_trans_block_group(trans
, inode
);
119 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
120 num_bytes
= max(blocksize
, num_bytes
);
121 ret
= btrfs_drop_extents(trans
, root
, inode
,
122 start
, start
+ num_bytes
, start
, &alloc_hint
);
123 orig_num_bytes
= num_bytes
;
125 if (alloc_hint
== EXTENT_MAP_INLINE
)
128 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
130 while(num_bytes
> 0) {
131 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
132 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
134 root
->root_key
.objectid
,
136 inode
->i_ino
, start
, 0,
137 alloc_hint
, (u64
)-1, &ins
, 1);
142 cur_alloc_size
= ins
.offset
;
143 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
144 start
, ins
.objectid
, ins
.offset
,
146 inode
->i_blocks
+= ins
.offset
>> 9;
147 btrfs_check_file(root
, inode
);
148 if (num_bytes
< cur_alloc_size
) {
149 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
153 num_bytes
-= cur_alloc_size
;
154 alloc_hint
= ins
.objectid
+ ins
.offset
;
155 start
+= cur_alloc_size
;
157 btrfs_drop_extent_cache(inode
, orig_start
,
158 orig_start
+ orig_num_bytes
- 1);
159 btrfs_add_ordered_inode(inode
);
160 btrfs_update_inode(trans
, root
, inode
);
162 btrfs_end_transaction(trans
, root
);
166 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
174 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
175 struct extent_buffer
*leaf
;
177 struct btrfs_path
*path
;
178 struct btrfs_file_extent_item
*item
;
181 struct btrfs_key found_key
;
183 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
184 path
= btrfs_alloc_path();
187 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
188 inode
->i_ino
, start
, 0);
190 btrfs_free_path(path
);
196 if (path
->slots
[0] == 0)
201 leaf
= path
->nodes
[0];
202 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
203 struct btrfs_file_extent_item
);
205 /* are we inside the extent that was found? */
206 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
207 found_type
= btrfs_key_type(&found_key
);
208 if (found_key
.objectid
!= inode
->i_ino
||
209 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
213 found_type
= btrfs_file_extent_type(leaf
, item
);
214 extent_start
= found_key
.offset
;
215 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
216 u64 extent_num_bytes
;
218 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
219 extent_end
= extent_start
+ extent_num_bytes
;
222 if (loops
&& start
!= extent_start
)
225 if (start
< extent_start
|| start
>= extent_end
)
228 cow_end
= min(end
, extent_end
- 1);
229 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
234 * we may be called by the resizer, make sure we're inside
235 * the limits of the FS
237 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
240 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
250 btrfs_free_path(path
);
253 btrfs_release_path(root
, path
);
258 cow_file_range(inode
, start
, cow_end
);
263 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
265 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
267 mutex_lock(&root
->fs_info
->fs_mutex
);
268 if (btrfs_test_opt(root
, NODATACOW
) ||
269 btrfs_test_flag(inode
, NODATACOW
))
270 ret
= run_delalloc_nocow(inode
, start
, end
);
272 ret
= cow_file_range(inode
, start
, end
);
274 mutex_unlock(&root
->fs_info
->fs_mutex
);
278 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
279 unsigned long old
, unsigned long bits
)
282 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
284 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
285 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
286 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
287 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
292 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
293 unsigned long old
, unsigned long bits
)
295 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
299 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
300 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
301 printk("warning: delalloc account %Lu %Lu\n",
302 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
303 root
->fs_info
->delalloc_bytes
= 0;
304 BTRFS_I(inode
)->delalloc_bytes
= 0;
306 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
307 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
309 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
314 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
315 size_t size
, struct bio
*bio
)
317 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
318 struct btrfs_mapping_tree
*map_tree
;
319 u64 logical
= bio
->bi_sector
<< 9;
324 length
= bio
->bi_size
;
325 map_tree
= &root
->fs_info
->mapping_tree
;
327 ret
= btrfs_map_block(map_tree
, READ
, logical
,
328 &map_length
, NULL
, 0);
330 if (map_length
< length
+ size
) {
336 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
340 struct btrfs_trans_handle
*trans
;
344 ret
= btrfs_csum_one_bio(root
, bio
, &sums
);
347 mutex_lock(&root
->fs_info
->fs_mutex
);
348 trans
= btrfs_start_transaction(root
, 1);
350 btrfs_set_trans_block_group(trans
, inode
);
351 btrfs_csum_file_blocks(trans
, root
, inode
, bio
, sums
);
353 ret
= btrfs_end_transaction(trans
, root
);
355 mutex_unlock(&root
->fs_info
->fs_mutex
);
359 return btrfs_map_bio(root
, rw
, bio
, mirror_num
);
362 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
365 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
368 if (!(rw
& (1 << BIO_RW
))) {
369 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
374 if (btrfs_test_opt(root
, NODATASUM
) ||
375 btrfs_test_flag(inode
, NODATASUM
)) {
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
);
386 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
389 struct inode
*inode
= page
->mapping
->host
;
390 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
391 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
392 struct btrfs_csum_item
*item
;
393 struct btrfs_path
*path
= NULL
;
396 if (btrfs_test_opt(root
, NODATASUM
) ||
397 btrfs_test_flag(inode
, NODATASUM
))
400 mutex_lock(&root
->fs_info
->fs_mutex
);
401 path
= btrfs_alloc_path();
402 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
405 /* a csum that isn't present is a preallocated region. */
406 if (ret
== -ENOENT
|| ret
== -EFBIG
)
409 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
, start
);
412 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
414 set_state_private(io_tree
, start
, csum
);
417 btrfs_free_path(path
);
418 mutex_unlock(&root
->fs_info
->fs_mutex
);
422 struct io_failure_record
{
430 int btrfs_readpage_io_failed_hook(struct bio
*failed_bio
,
431 struct page
*page
, u64 start
, u64 end
,
432 struct extent_state
*state
)
434 struct io_failure_record
*failrec
= NULL
;
436 struct extent_map
*em
;
437 struct inode
*inode
= page
->mapping
->host
;
438 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
439 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
445 ret
= get_state_private(failure_tree
, start
, &private);
447 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
450 failrec
->start
= start
;
451 failrec
->len
= end
- start
+ 1;
452 failrec
->last_mirror
= 0;
454 spin_lock(&em_tree
->lock
);
455 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
456 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
460 spin_unlock(&em_tree
->lock
);
462 if (!em
|| IS_ERR(em
)) {
466 logical
= start
- em
->start
;
467 logical
= em
->block_start
+ logical
;
468 failrec
->logical
= logical
;
470 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
471 EXTENT_DIRTY
, GFP_NOFS
);
472 set_state_private(failure_tree
, start
,
473 (u64
)(unsigned long)failrec
);
475 failrec
= (struct io_failure_record
*)(unsigned long)private;
477 num_copies
= btrfs_num_copies(
478 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
479 failrec
->logical
, failrec
->len
);
480 failrec
->last_mirror
++;
482 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
483 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
486 if (state
&& state
->start
!= failrec
->start
)
488 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
490 if (!state
|| failrec
->last_mirror
> num_copies
) {
491 set_state_private(failure_tree
, failrec
->start
, 0);
492 clear_extent_bits(failure_tree
, failrec
->start
,
493 failrec
->start
+ failrec
->len
- 1,
494 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
498 bio
= bio_alloc(GFP_NOFS
, 1);
499 bio
->bi_private
= state
;
500 bio
->bi_end_io
= failed_bio
->bi_end_io
;
501 bio
->bi_sector
= failrec
->logical
>> 9;
502 bio
->bi_bdev
= failed_bio
->bi_bdev
;
504 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
505 btrfs_submit_bio_hook(inode
, READ
, bio
, failrec
->last_mirror
);
509 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
510 struct extent_state
*state
)
512 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
513 struct inode
*inode
= page
->mapping
->host
;
514 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
516 u64
private = ~(u32
)0;
518 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
522 if (btrfs_test_opt(root
, NODATASUM
) ||
523 btrfs_test_flag(inode
, NODATASUM
))
525 if (state
&& state
->start
== start
) {
526 private = state
->private;
529 ret
= get_state_private(io_tree
, start
, &private);
531 local_irq_save(flags
);
532 kaddr
= kmap_atomic(page
, KM_IRQ0
);
536 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
537 btrfs_csum_final(csum
, (char *)&csum
);
538 if (csum
!= private) {
541 kunmap_atomic(kaddr
, KM_IRQ0
);
542 local_irq_restore(flags
);
544 /* if the io failure tree for this inode is non-empty,
545 * check to see if we've recovered from a failed IO
548 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
549 (u64
)-1, 1, EXTENT_DIRTY
)) {
551 struct io_failure_record
*failure
;
552 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
553 start
, &private_failure
);
555 failure
= (struct io_failure_record
*)(unsigned long)
557 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
559 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
561 failure
->start
+ failure
->len
- 1,
562 EXTENT_DIRTY
| EXTENT_LOCKED
,
570 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
571 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
573 memset(kaddr
+ offset
, 1, end
- start
+ 1);
574 flush_dcache_page(page
);
575 kunmap_atomic(kaddr
, KM_IRQ0
);
576 local_irq_restore(flags
);
582 void btrfs_read_locked_inode(struct inode
*inode
)
584 struct btrfs_path
*path
;
585 struct extent_buffer
*leaf
;
586 struct btrfs_inode_item
*inode_item
;
587 struct btrfs_timespec
*tspec
;
588 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
589 struct btrfs_key location
;
590 u64 alloc_group_block
;
594 path
= btrfs_alloc_path();
596 mutex_lock(&root
->fs_info
->fs_mutex
);
597 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
599 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
603 leaf
= path
->nodes
[0];
604 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
605 struct btrfs_inode_item
);
607 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
608 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
609 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
610 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
611 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
613 tspec
= btrfs_inode_atime(inode_item
);
614 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
615 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
617 tspec
= btrfs_inode_mtime(inode_item
);
618 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
619 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
621 tspec
= btrfs_inode_ctime(inode_item
);
622 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
623 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
625 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
626 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
628 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
630 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
631 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
633 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
634 if (!BTRFS_I(inode
)->block_group
) {
635 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
637 BTRFS_BLOCK_GROUP_METADATA
, 0);
639 btrfs_free_path(path
);
642 mutex_unlock(&root
->fs_info
->fs_mutex
);
644 switch (inode
->i_mode
& S_IFMT
) {
646 inode
->i_mapping
->a_ops
= &btrfs_aops
;
647 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
648 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
649 inode
->i_fop
= &btrfs_file_operations
;
650 inode
->i_op
= &btrfs_file_inode_operations
;
653 inode
->i_fop
= &btrfs_dir_file_operations
;
654 if (root
== root
->fs_info
->tree_root
)
655 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
657 inode
->i_op
= &btrfs_dir_inode_operations
;
660 inode
->i_op
= &btrfs_symlink_inode_operations
;
661 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
662 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
665 init_special_inode(inode
, inode
->i_mode
, rdev
);
671 btrfs_release_path(root
, path
);
672 btrfs_free_path(path
);
673 mutex_unlock(&root
->fs_info
->fs_mutex
);
674 make_bad_inode(inode
);
677 static void fill_inode_item(struct extent_buffer
*leaf
,
678 struct btrfs_inode_item
*item
,
681 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
682 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
683 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
684 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
685 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
687 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
688 inode
->i_atime
.tv_sec
);
689 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
690 inode
->i_atime
.tv_nsec
);
692 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
693 inode
->i_mtime
.tv_sec
);
694 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
695 inode
->i_mtime
.tv_nsec
);
697 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
698 inode
->i_ctime
.tv_sec
);
699 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
700 inode
->i_ctime
.tv_nsec
);
702 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
703 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
704 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
705 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
706 btrfs_set_inode_block_group(leaf
, item
,
707 BTRFS_I(inode
)->block_group
->key
.objectid
);
710 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
711 struct btrfs_root
*root
,
714 struct btrfs_inode_item
*inode_item
;
715 struct btrfs_path
*path
;
716 struct extent_buffer
*leaf
;
719 path
= btrfs_alloc_path();
721 ret
= btrfs_lookup_inode(trans
, root
, path
,
722 &BTRFS_I(inode
)->location
, 1);
729 leaf
= path
->nodes
[0];
730 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
731 struct btrfs_inode_item
);
733 fill_inode_item(leaf
, inode_item
, inode
);
734 btrfs_mark_buffer_dirty(leaf
);
735 btrfs_set_inode_last_trans(trans
, inode
);
738 btrfs_release_path(root
, path
);
739 btrfs_free_path(path
);
744 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
745 struct btrfs_root
*root
,
747 struct dentry
*dentry
)
749 struct btrfs_path
*path
;
750 const char *name
= dentry
->d_name
.name
;
751 int name_len
= dentry
->d_name
.len
;
753 struct extent_buffer
*leaf
;
754 struct btrfs_dir_item
*di
;
755 struct btrfs_key key
;
757 path
= btrfs_alloc_path();
763 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
773 leaf
= path
->nodes
[0];
774 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
775 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
778 btrfs_release_path(root
, path
);
780 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
781 key
.objectid
, name
, name_len
, -1);
790 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
792 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
793 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
794 dentry
->d_inode
->i_ino
,
795 dentry
->d_parent
->d_inode
->i_ino
);
797 printk("failed to delete reference to %.*s, "
798 "inode %lu parent %lu\n", name_len
, name
,
799 dentry
->d_inode
->i_ino
,
800 dentry
->d_parent
->d_inode
->i_ino
);
803 btrfs_free_path(path
);
805 dir
->i_size
-= name_len
* 2;
806 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
807 btrfs_update_inode(trans
, root
, dir
);
808 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
809 dentry
->d_inode
->i_nlink
--;
811 drop_nlink(dentry
->d_inode
);
813 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
814 dir
->i_sb
->s_dirt
= 1;
819 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
821 struct btrfs_root
*root
;
822 struct btrfs_trans_handle
*trans
;
823 struct inode
*inode
= dentry
->d_inode
;
825 unsigned long nr
= 0;
827 root
= BTRFS_I(dir
)->root
;
828 mutex_lock(&root
->fs_info
->fs_mutex
);
830 ret
= btrfs_check_free_space(root
, 1, 1);
834 trans
= btrfs_start_transaction(root
, 1);
836 btrfs_set_trans_block_group(trans
, dir
);
837 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
838 nr
= trans
->blocks_used
;
840 if (inode
->i_nlink
== 0) {
842 /* if the inode isn't linked anywhere,
843 * we don't need to worry about
846 found
= btrfs_del_ordered_inode(inode
);
848 atomic_dec(&inode
->i_count
);
852 btrfs_end_transaction(trans
, root
);
854 mutex_unlock(&root
->fs_info
->fs_mutex
);
855 btrfs_btree_balance_dirty(root
, nr
);
856 btrfs_throttle(root
);
860 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
862 struct inode
*inode
= dentry
->d_inode
;
865 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
866 struct btrfs_trans_handle
*trans
;
867 unsigned long nr
= 0;
869 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
872 mutex_lock(&root
->fs_info
->fs_mutex
);
873 ret
= btrfs_check_free_space(root
, 1, 1);
877 trans
= btrfs_start_transaction(root
, 1);
878 btrfs_set_trans_block_group(trans
, dir
);
880 /* now the directory is empty */
881 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
886 nr
= trans
->blocks_used
;
887 ret
= btrfs_end_transaction(trans
, root
);
889 mutex_unlock(&root
->fs_info
->fs_mutex
);
890 btrfs_btree_balance_dirty(root
, nr
);
891 btrfs_throttle(root
);
899 * this can truncate away extent items, csum items and directory items.
900 * It starts at a high offset and removes keys until it can't find
901 * any higher than i_size.
903 * csum items that cross the new i_size are truncated to the new size
906 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
907 struct btrfs_root
*root
,
912 struct btrfs_path
*path
;
913 struct btrfs_key key
;
914 struct btrfs_key found_key
;
916 struct extent_buffer
*leaf
;
917 struct btrfs_file_extent_item
*fi
;
918 u64 extent_start
= 0;
919 u64 extent_num_bytes
= 0;
925 int pending_del_nr
= 0;
926 int pending_del_slot
= 0;
927 int extent_type
= -1;
928 u64 mask
= root
->sectorsize
- 1;
930 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
931 path
= btrfs_alloc_path();
935 /* FIXME, add redo link to tree so we don't leak on crash */
936 key
.objectid
= inode
->i_ino
;
937 key
.offset
= (u64
)-1;
940 btrfs_init_path(path
);
942 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
947 BUG_ON(path
->slots
[0] == 0);
953 leaf
= path
->nodes
[0];
954 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
955 found_type
= btrfs_key_type(&found_key
);
957 if (found_key
.objectid
!= inode
->i_ino
)
960 if (found_type
< min_type
)
963 item_end
= found_key
.offset
;
964 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
965 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
966 struct btrfs_file_extent_item
);
967 extent_type
= btrfs_file_extent_type(leaf
, fi
);
968 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
970 btrfs_file_extent_num_bytes(leaf
, fi
);
971 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
972 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
974 item_end
+= btrfs_file_extent_inline_len(leaf
,
979 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
980 ret
= btrfs_csum_truncate(trans
, root
, path
,
984 if (item_end
< inode
->i_size
) {
985 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
986 found_type
= BTRFS_INODE_ITEM_KEY
;
987 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
988 found_type
= BTRFS_CSUM_ITEM_KEY
;
989 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
990 found_type
= BTRFS_XATTR_ITEM_KEY
;
991 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
992 found_type
= BTRFS_INODE_REF_KEY
;
993 } else if (found_type
) {
998 btrfs_set_key_type(&key
, found_type
);
1001 if (found_key
.offset
>= inode
->i_size
)
1007 /* FIXME, shrink the extent if the ref count is only 1 */
1008 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1011 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1013 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1015 u64 orig_num_bytes
=
1016 btrfs_file_extent_num_bytes(leaf
, fi
);
1017 extent_num_bytes
= inode
->i_size
-
1018 found_key
.offset
+ root
->sectorsize
- 1;
1019 extent_num_bytes
= extent_num_bytes
&
1020 ~((u64
)root
->sectorsize
- 1);
1021 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1023 num_dec
= (orig_num_bytes
-
1025 if (extent_start
!= 0)
1026 dec_i_blocks(inode
, num_dec
);
1027 btrfs_mark_buffer_dirty(leaf
);
1030 btrfs_file_extent_disk_num_bytes(leaf
,
1032 /* FIXME blocksize != 4096 */
1033 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1034 if (extent_start
!= 0) {
1036 dec_i_blocks(inode
, num_dec
);
1038 root_gen
= btrfs_header_generation(leaf
);
1039 root_owner
= btrfs_header_owner(leaf
);
1041 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1043 u32 newsize
= inode
->i_size
- found_key
.offset
;
1044 dec_i_blocks(inode
, item_end
+ 1 -
1045 found_key
.offset
- newsize
);
1047 btrfs_file_extent_calc_inline_size(newsize
);
1048 ret
= btrfs_truncate_item(trans
, root
, path
,
1052 dec_i_blocks(inode
, item_end
+ 1 -
1058 if (!pending_del_nr
) {
1059 /* no pending yet, add ourselves */
1060 pending_del_slot
= path
->slots
[0];
1062 } else if (pending_del_nr
&&
1063 path
->slots
[0] + 1 == pending_del_slot
) {
1064 /* hop on the pending chunk */
1066 pending_del_slot
= path
->slots
[0];
1068 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1074 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1077 root_gen
, inode
->i_ino
,
1078 found_key
.offset
, 0);
1082 if (path
->slots
[0] == 0) {
1085 btrfs_release_path(root
, path
);
1090 if (pending_del_nr
&&
1091 path
->slots
[0] + 1 != pending_del_slot
) {
1092 struct btrfs_key debug
;
1094 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1096 ret
= btrfs_del_items(trans
, root
, path
,
1101 btrfs_release_path(root
, path
);
1107 if (pending_del_nr
) {
1108 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1111 btrfs_release_path(root
, path
);
1112 btrfs_free_path(path
);
1113 inode
->i_sb
->s_dirt
= 1;
1117 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
1121 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1122 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1123 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1126 WARN_ON(!PageLocked(page
));
1127 set_page_extent_mapped(page
);
1129 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1130 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1131 page_end
, GFP_NOFS
);
1133 if (zero_start
!= PAGE_CACHE_SIZE
) {
1135 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
1136 flush_dcache_page(page
);
1139 set_page_dirty(page
);
1140 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1146 * taken from block_truncate_page, but does cow as it zeros out
1147 * any bytes left in the last page in the file.
1149 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1151 struct inode
*inode
= mapping
->host
;
1152 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1153 u32 blocksize
= root
->sectorsize
;
1154 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1155 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1160 if ((offset
& (blocksize
- 1)) == 0)
1164 page
= grab_cache_page(mapping
, index
);
1167 if (!PageUptodate(page
)) {
1168 ret
= btrfs_readpage(NULL
, page
);
1170 if (!PageUptodate(page
)) {
1175 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1177 ret
= btrfs_cow_one_page(inode
, page
, offset
);
1180 page_cache_release(page
);
1185 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1187 struct inode
*inode
= dentry
->d_inode
;
1190 err
= inode_change_ok(inode
, attr
);
1194 if (S_ISREG(inode
->i_mode
) &&
1195 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1196 struct btrfs_trans_handle
*trans
;
1197 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1198 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1200 u64 mask
= root
->sectorsize
- 1;
1201 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1202 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1206 if (attr
->ia_size
<= hole_start
)
1209 mutex_lock(&root
->fs_info
->fs_mutex
);
1210 err
= btrfs_check_free_space(root
, 1, 0);
1211 mutex_unlock(&root
->fs_info
->fs_mutex
);
1215 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1217 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1218 hole_size
= block_end
- hole_start
;
1220 mutex_lock(&root
->fs_info
->fs_mutex
);
1221 trans
= btrfs_start_transaction(root
, 1);
1222 btrfs_set_trans_block_group(trans
, inode
);
1223 err
= btrfs_drop_extents(trans
, root
, inode
,
1224 hole_start
, block_end
, hole_start
,
1227 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1228 err
= btrfs_insert_file_extent(trans
, root
,
1232 btrfs_drop_extent_cache(inode
, hole_start
,
1234 btrfs_check_file(root
, inode
);
1236 btrfs_end_transaction(trans
, root
);
1237 mutex_unlock(&root
->fs_info
->fs_mutex
);
1238 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1243 err
= inode_setattr(inode
, attr
);
1248 void btrfs_put_inode(struct inode
*inode
)
1252 if (!BTRFS_I(inode
)->ordered_trans
) {
1256 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_DIRTY
) ||
1257 mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1260 ret
= btrfs_del_ordered_inode(inode
);
1262 atomic_dec(&inode
->i_count
);
1266 void btrfs_delete_inode(struct inode
*inode
)
1268 struct btrfs_trans_handle
*trans
;
1269 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1273 truncate_inode_pages(&inode
->i_data
, 0);
1274 if (is_bad_inode(inode
)) {
1279 mutex_lock(&root
->fs_info
->fs_mutex
);
1280 trans
= btrfs_start_transaction(root
, 1);
1282 btrfs_set_trans_block_group(trans
, inode
);
1283 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1285 goto no_delete_lock
;
1287 nr
= trans
->blocks_used
;
1290 btrfs_end_transaction(trans
, root
);
1291 mutex_unlock(&root
->fs_info
->fs_mutex
);
1292 btrfs_btree_balance_dirty(root
, nr
);
1293 btrfs_throttle(root
);
1297 nr
= trans
->blocks_used
;
1298 btrfs_end_transaction(trans
, root
);
1299 mutex_unlock(&root
->fs_info
->fs_mutex
);
1300 btrfs_btree_balance_dirty(root
, nr
);
1301 btrfs_throttle(root
);
1307 * this returns the key found in the dir entry in the location pointer.
1308 * If no dir entries were found, location->objectid is 0.
1310 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1311 struct btrfs_key
*location
)
1313 const char *name
= dentry
->d_name
.name
;
1314 int namelen
= dentry
->d_name
.len
;
1315 struct btrfs_dir_item
*di
;
1316 struct btrfs_path
*path
;
1317 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1320 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1321 location
->objectid
= dir
->i_ino
;
1322 location
->type
= BTRFS_INODE_ITEM_KEY
;
1323 location
->offset
= 0;
1326 path
= btrfs_alloc_path();
1329 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1330 struct btrfs_key key
;
1331 struct extent_buffer
*leaf
;
1335 key
.objectid
= dir
->i_ino
;
1336 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1338 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1342 leaf
= path
->nodes
[0];
1343 slot
= path
->slots
[0];
1344 nritems
= btrfs_header_nritems(leaf
);
1345 if (slot
>= nritems
)
1348 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1349 if (key
.objectid
!= dir
->i_ino
||
1350 key
.type
!= BTRFS_INODE_REF_KEY
) {
1353 location
->objectid
= key
.offset
;
1354 location
->type
= BTRFS_INODE_ITEM_KEY
;
1355 location
->offset
= 0;
1359 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1363 if (!di
|| IS_ERR(di
)) {
1366 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1368 btrfs_free_path(path
);
1371 location
->objectid
= 0;
1376 * when we hit a tree root in a directory, the btrfs part of the inode
1377 * needs to be changed to reflect the root directory of the tree root. This
1378 * is kind of like crossing a mount point.
1380 static int fixup_tree_root_location(struct btrfs_root
*root
,
1381 struct btrfs_key
*location
,
1382 struct btrfs_root
**sub_root
,
1383 struct dentry
*dentry
)
1385 struct btrfs_path
*path
;
1386 struct btrfs_root_item
*ri
;
1388 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1390 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1393 path
= btrfs_alloc_path();
1395 mutex_lock(&root
->fs_info
->fs_mutex
);
1397 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1398 dentry
->d_name
.name
,
1399 dentry
->d_name
.len
);
1400 if (IS_ERR(*sub_root
))
1401 return PTR_ERR(*sub_root
);
1403 ri
= &(*sub_root
)->root_item
;
1404 location
->objectid
= btrfs_root_dirid(ri
);
1405 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1406 location
->offset
= 0;
1408 btrfs_free_path(path
);
1409 mutex_unlock(&root
->fs_info
->fs_mutex
);
1413 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1415 struct btrfs_iget_args
*args
= p
;
1416 inode
->i_ino
= args
->ino
;
1417 BTRFS_I(inode
)->root
= args
->root
;
1418 BTRFS_I(inode
)->delalloc_bytes
= 0;
1419 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1420 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1421 inode
->i_mapping
, GFP_NOFS
);
1422 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1423 inode
->i_mapping
, GFP_NOFS
);
1424 atomic_set(&BTRFS_I(inode
)->ordered_writeback
, 0);
1428 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1430 struct btrfs_iget_args
*args
= opaque
;
1431 return (args
->ino
== inode
->i_ino
&&
1432 args
->root
== BTRFS_I(inode
)->root
);
1435 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1438 struct btrfs_iget_args args
;
1439 args
.ino
= objectid
;
1440 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1445 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1448 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1449 struct btrfs_root
*root
)
1451 struct inode
*inode
;
1452 struct btrfs_iget_args args
;
1453 args
.ino
= objectid
;
1456 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1457 btrfs_init_locked_inode
,
1462 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1463 struct nameidata
*nd
)
1465 struct inode
* inode
;
1466 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1467 struct btrfs_root
*root
= bi
->root
;
1468 struct btrfs_root
*sub_root
= root
;
1469 struct btrfs_key location
;
1472 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1473 return ERR_PTR(-ENAMETOOLONG
);
1475 mutex_lock(&root
->fs_info
->fs_mutex
);
1476 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1477 mutex_unlock(&root
->fs_info
->fs_mutex
);
1480 return ERR_PTR(ret
);
1483 if (location
.objectid
) {
1484 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1487 return ERR_PTR(ret
);
1489 return ERR_PTR(-ENOENT
);
1490 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1493 return ERR_PTR(-EACCES
);
1494 if (inode
->i_state
& I_NEW
) {
1495 /* the inode and parent dir are two different roots */
1496 if (sub_root
!= root
) {
1498 sub_root
->inode
= inode
;
1500 BTRFS_I(inode
)->root
= sub_root
;
1501 memcpy(&BTRFS_I(inode
)->location
, &location
,
1503 btrfs_read_locked_inode(inode
);
1504 unlock_new_inode(inode
);
1507 return d_splice_alias(inode
, dentry
);
1510 static unsigned char btrfs_filetype_table
[] = {
1511 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1514 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1516 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1517 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1518 struct btrfs_item
*item
;
1519 struct btrfs_dir_item
*di
;
1520 struct btrfs_key key
;
1521 struct btrfs_key found_key
;
1522 struct btrfs_path
*path
;
1525 struct extent_buffer
*leaf
;
1528 unsigned char d_type
;
1533 int key_type
= BTRFS_DIR_INDEX_KEY
;
1538 /* FIXME, use a real flag for deciding about the key type */
1539 if (root
->fs_info
->tree_root
== root
)
1540 key_type
= BTRFS_DIR_ITEM_KEY
;
1542 /* special case for "." */
1543 if (filp
->f_pos
== 0) {
1544 over
= filldir(dirent
, ".", 1,
1552 mutex_lock(&root
->fs_info
->fs_mutex
);
1553 key
.objectid
= inode
->i_ino
;
1554 path
= btrfs_alloc_path();
1557 /* special case for .., just use the back ref */
1558 if (filp
->f_pos
== 1) {
1559 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1561 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1563 leaf
= path
->nodes
[0];
1564 slot
= path
->slots
[0];
1565 nritems
= btrfs_header_nritems(leaf
);
1566 if (slot
>= nritems
) {
1567 btrfs_release_path(root
, path
);
1568 goto read_dir_items
;
1570 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1571 btrfs_release_path(root
, path
);
1572 if (found_key
.objectid
!= key
.objectid
||
1573 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1574 goto read_dir_items
;
1575 over
= filldir(dirent
, "..", 2,
1576 2, found_key
.offset
, DT_DIR
);
1583 btrfs_set_key_type(&key
, key_type
);
1584 key
.offset
= filp
->f_pos
;
1586 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1591 leaf
= path
->nodes
[0];
1592 nritems
= btrfs_header_nritems(leaf
);
1593 slot
= path
->slots
[0];
1594 if (advance
|| slot
>= nritems
) {
1595 if (slot
>= nritems
-1) {
1596 ret
= btrfs_next_leaf(root
, path
);
1599 leaf
= path
->nodes
[0];
1600 nritems
= btrfs_header_nritems(leaf
);
1601 slot
= path
->slots
[0];
1608 item
= btrfs_item_nr(leaf
, slot
);
1609 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1611 if (found_key
.objectid
!= key
.objectid
)
1613 if (btrfs_key_type(&found_key
) != key_type
)
1615 if (found_key
.offset
< filp
->f_pos
)
1618 filp
->f_pos
= found_key
.offset
;
1620 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1622 di_total
= btrfs_item_size(leaf
, item
);
1623 while(di_cur
< di_total
) {
1624 struct btrfs_key location
;
1626 name_len
= btrfs_dir_name_len(leaf
, di
);
1627 if (name_len
< 32) {
1628 name_ptr
= tmp_name
;
1630 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1633 read_extent_buffer(leaf
, name_ptr
,
1634 (unsigned long)(di
+ 1), name_len
);
1636 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1637 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1638 over
= filldir(dirent
, name_ptr
, name_len
,
1643 if (name_ptr
!= tmp_name
)
1648 di_len
= btrfs_dir_name_len(leaf
, di
) +
1649 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1651 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1654 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1655 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1661 btrfs_release_path(root
, path
);
1662 btrfs_free_path(path
);
1663 mutex_unlock(&root
->fs_info
->fs_mutex
);
1667 int btrfs_write_inode(struct inode
*inode
, int wait
)
1669 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1670 struct btrfs_trans_handle
*trans
;
1674 mutex_lock(&root
->fs_info
->fs_mutex
);
1675 trans
= btrfs_start_transaction(root
, 1);
1676 btrfs_set_trans_block_group(trans
, inode
);
1677 ret
= btrfs_commit_transaction(trans
, root
);
1678 mutex_unlock(&root
->fs_info
->fs_mutex
);
1684 * This is somewhat expensive, updating the tree every time the
1685 * inode changes. But, it is most likely to find the inode in cache.
1686 * FIXME, needs more benchmarking...there are no reasons other than performance
1687 * to keep or drop this code.
1689 void btrfs_dirty_inode(struct inode
*inode
)
1691 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1692 struct btrfs_trans_handle
*trans
;
1694 mutex_lock(&root
->fs_info
->fs_mutex
);
1695 trans
= btrfs_start_transaction(root
, 1);
1696 btrfs_set_trans_block_group(trans
, inode
);
1697 btrfs_update_inode(trans
, root
, inode
);
1698 btrfs_end_transaction(trans
, root
);
1699 mutex_unlock(&root
->fs_info
->fs_mutex
);
1702 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1703 struct btrfs_root
*root
,
1704 const char *name
, int name_len
,
1707 struct btrfs_block_group_cache
*group
,
1710 struct inode
*inode
;
1711 struct btrfs_inode_item
*inode_item
;
1712 struct btrfs_block_group_cache
*new_inode_group
;
1713 struct btrfs_key
*location
;
1714 struct btrfs_path
*path
;
1715 struct btrfs_inode_ref
*ref
;
1716 struct btrfs_key key
[2];
1722 path
= btrfs_alloc_path();
1725 inode
= new_inode(root
->fs_info
->sb
);
1727 return ERR_PTR(-ENOMEM
);
1729 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1730 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1731 inode
->i_mapping
, GFP_NOFS
);
1732 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1733 inode
->i_mapping
, GFP_NOFS
);
1734 atomic_set(&BTRFS_I(inode
)->ordered_writeback
, 0);
1735 BTRFS_I(inode
)->delalloc_bytes
= 0;
1736 BTRFS_I(inode
)->root
= root
;
1742 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1743 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1744 if (!new_inode_group
) {
1745 printk("find_block group failed\n");
1746 new_inode_group
= group
;
1748 BTRFS_I(inode
)->block_group
= new_inode_group
;
1749 BTRFS_I(inode
)->flags
= 0;
1751 key
[0].objectid
= objectid
;
1752 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1755 key
[1].objectid
= objectid
;
1756 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1757 key
[1].offset
= ref_objectid
;
1759 sizes
[0] = sizeof(struct btrfs_inode_item
);
1760 sizes
[1] = name_len
+ sizeof(*ref
);
1762 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1766 if (objectid
> root
->highest_inode
)
1767 root
->highest_inode
= objectid
;
1769 inode
->i_uid
= current
->fsuid
;
1770 inode
->i_gid
= current
->fsgid
;
1771 inode
->i_mode
= mode
;
1772 inode
->i_ino
= objectid
;
1773 inode
->i_blocks
= 0;
1774 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1775 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1776 struct btrfs_inode_item
);
1777 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1779 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1780 struct btrfs_inode_ref
);
1781 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1782 ptr
= (unsigned long)(ref
+ 1);
1783 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1785 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1786 btrfs_free_path(path
);
1788 location
= &BTRFS_I(inode
)->location
;
1789 location
->objectid
= objectid
;
1790 location
->offset
= 0;
1791 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1793 insert_inode_hash(inode
);
1796 btrfs_free_path(path
);
1797 return ERR_PTR(ret
);
1800 static inline u8
btrfs_inode_type(struct inode
*inode
)
1802 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1805 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1806 struct dentry
*dentry
, struct inode
*inode
,
1810 struct btrfs_key key
;
1811 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1812 struct inode
*parent_inode
;
1814 key
.objectid
= inode
->i_ino
;
1815 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1818 ret
= btrfs_insert_dir_item(trans
, root
,
1819 dentry
->d_name
.name
, dentry
->d_name
.len
,
1820 dentry
->d_parent
->d_inode
->i_ino
,
1821 &key
, btrfs_inode_type(inode
));
1824 ret
= btrfs_insert_inode_ref(trans
, root
,
1825 dentry
->d_name
.name
,
1828 dentry
->d_parent
->d_inode
->i_ino
);
1830 parent_inode
= dentry
->d_parent
->d_inode
;
1831 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1832 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1833 ret
= btrfs_update_inode(trans
, root
,
1834 dentry
->d_parent
->d_inode
);
1839 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1840 struct dentry
*dentry
, struct inode
*inode
,
1843 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1845 d_instantiate(dentry
, inode
);
1853 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1854 int mode
, dev_t rdev
)
1856 struct btrfs_trans_handle
*trans
;
1857 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1858 struct inode
*inode
= NULL
;
1862 unsigned long nr
= 0;
1864 if (!new_valid_dev(rdev
))
1867 mutex_lock(&root
->fs_info
->fs_mutex
);
1868 err
= btrfs_check_free_space(root
, 1, 0);
1872 trans
= btrfs_start_transaction(root
, 1);
1873 btrfs_set_trans_block_group(trans
, dir
);
1875 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1881 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1883 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1884 BTRFS_I(dir
)->block_group
, mode
);
1885 err
= PTR_ERR(inode
);
1889 btrfs_set_trans_block_group(trans
, inode
);
1890 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1894 inode
->i_op
= &btrfs_special_inode_operations
;
1895 init_special_inode(inode
, inode
->i_mode
, rdev
);
1896 btrfs_update_inode(trans
, root
, inode
);
1898 dir
->i_sb
->s_dirt
= 1;
1899 btrfs_update_inode_block_group(trans
, inode
);
1900 btrfs_update_inode_block_group(trans
, dir
);
1902 nr
= trans
->blocks_used
;
1903 btrfs_end_transaction(trans
, root
);
1905 mutex_unlock(&root
->fs_info
->fs_mutex
);
1908 inode_dec_link_count(inode
);
1911 btrfs_btree_balance_dirty(root
, nr
);
1912 btrfs_throttle(root
);
1916 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1917 int mode
, struct nameidata
*nd
)
1919 struct btrfs_trans_handle
*trans
;
1920 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1921 struct inode
*inode
= NULL
;
1924 unsigned long nr
= 0;
1927 mutex_lock(&root
->fs_info
->fs_mutex
);
1928 err
= btrfs_check_free_space(root
, 1, 0);
1931 trans
= btrfs_start_transaction(root
, 1);
1932 btrfs_set_trans_block_group(trans
, dir
);
1934 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1940 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1942 dentry
->d_parent
->d_inode
->i_ino
,
1943 objectid
, BTRFS_I(dir
)->block_group
, mode
);
1944 err
= PTR_ERR(inode
);
1948 btrfs_set_trans_block_group(trans
, inode
);
1949 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1953 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1954 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1955 inode
->i_fop
= &btrfs_file_operations
;
1956 inode
->i_op
= &btrfs_file_inode_operations
;
1957 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1958 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1959 inode
->i_mapping
, GFP_NOFS
);
1960 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1961 inode
->i_mapping
, GFP_NOFS
);
1962 BTRFS_I(inode
)->delalloc_bytes
= 0;
1963 atomic_set(&BTRFS_I(inode
)->ordered_writeback
, 0);
1964 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1966 dir
->i_sb
->s_dirt
= 1;
1967 btrfs_update_inode_block_group(trans
, inode
);
1968 btrfs_update_inode_block_group(trans
, dir
);
1970 nr
= trans
->blocks_used
;
1971 btrfs_end_transaction(trans
, root
);
1973 mutex_unlock(&root
->fs_info
->fs_mutex
);
1976 inode_dec_link_count(inode
);
1979 btrfs_btree_balance_dirty(root
, nr
);
1980 btrfs_throttle(root
);
1984 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1985 struct dentry
*dentry
)
1987 struct btrfs_trans_handle
*trans
;
1988 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1989 struct inode
*inode
= old_dentry
->d_inode
;
1990 unsigned long nr
= 0;
1994 if (inode
->i_nlink
== 0)
1997 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2002 mutex_lock(&root
->fs_info
->fs_mutex
);
2003 err
= btrfs_check_free_space(root
, 1, 0);
2006 trans
= btrfs_start_transaction(root
, 1);
2008 btrfs_set_trans_block_group(trans
, dir
);
2009 atomic_inc(&inode
->i_count
);
2010 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2015 dir
->i_sb
->s_dirt
= 1;
2016 btrfs_update_inode_block_group(trans
, dir
);
2017 err
= btrfs_update_inode(trans
, root
, inode
);
2022 nr
= trans
->blocks_used
;
2023 btrfs_end_transaction(trans
, root
);
2025 mutex_unlock(&root
->fs_info
->fs_mutex
);
2028 inode_dec_link_count(inode
);
2031 btrfs_btree_balance_dirty(root
, nr
);
2032 btrfs_throttle(root
);
2036 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2038 struct inode
*inode
= NULL
;
2039 struct btrfs_trans_handle
*trans
;
2040 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2042 int drop_on_err
= 0;
2044 unsigned long nr
= 1;
2046 mutex_lock(&root
->fs_info
->fs_mutex
);
2047 err
= btrfs_check_free_space(root
, 1, 0);
2051 trans
= btrfs_start_transaction(root
, 1);
2052 btrfs_set_trans_block_group(trans
, dir
);
2054 if (IS_ERR(trans
)) {
2055 err
= PTR_ERR(trans
);
2059 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2065 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2067 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2068 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2069 if (IS_ERR(inode
)) {
2070 err
= PTR_ERR(inode
);
2075 inode
->i_op
= &btrfs_dir_inode_operations
;
2076 inode
->i_fop
= &btrfs_dir_file_operations
;
2077 btrfs_set_trans_block_group(trans
, inode
);
2080 err
= btrfs_update_inode(trans
, root
, inode
);
2084 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2088 d_instantiate(dentry
, inode
);
2090 dir
->i_sb
->s_dirt
= 1;
2091 btrfs_update_inode_block_group(trans
, inode
);
2092 btrfs_update_inode_block_group(trans
, dir
);
2095 nr
= trans
->blocks_used
;
2096 btrfs_end_transaction(trans
, root
);
2099 mutex_unlock(&root
->fs_info
->fs_mutex
);
2102 btrfs_btree_balance_dirty(root
, nr
);
2103 btrfs_throttle(root
);
2107 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2108 struct extent_map
*existing
,
2109 struct extent_map
*em
)
2114 int real_blocks
= existing
->block_start
< EXTENT_MAP_LAST_BYTE
;
2116 if (real_blocks
&& em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
2119 if (!real_blocks
&& em
->block_start
!= existing
->block_start
)
2122 new_end
= max(existing
->start
+ existing
->len
, em
->start
+ em
->len
);
2124 if (existing
->start
>= em
->start
) {
2125 if (em
->start
+ em
->len
< existing
->start
)
2128 start_diff
= existing
->start
- em
->start
;
2129 if (real_blocks
&& em
->block_start
+ start_diff
!=
2130 existing
->block_start
)
2133 em
->len
= new_end
- em
->start
;
2135 remove_extent_mapping(em_tree
, existing
);
2136 /* free for the tree */
2137 free_extent_map(existing
);
2138 ret
= add_extent_mapping(em_tree
, em
);
2140 } else if (em
->start
> existing
->start
) {
2142 if (existing
->start
+ existing
->len
< em
->start
)
2145 start_diff
= em
->start
- existing
->start
;
2146 if (real_blocks
&& existing
->block_start
+ start_diff
!=
2150 remove_extent_mapping(em_tree
, existing
);
2151 em
->block_start
= existing
->block_start
;
2152 em
->start
= existing
->start
;
2153 em
->len
= new_end
- existing
->start
;
2154 free_extent_map(existing
);
2156 ret
= add_extent_mapping(em_tree
, em
);
2163 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2164 existing
->start
, existing
->len
, existing
->block_start
,
2165 em
->start
, em
->len
, em
->block_start
);
2169 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2170 size_t pg_offset
, u64 start
, u64 len
,
2176 u64 extent_start
= 0;
2178 u64 objectid
= inode
->i_ino
;
2180 struct btrfs_path
*path
;
2181 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2182 struct btrfs_file_extent_item
*item
;
2183 struct extent_buffer
*leaf
;
2184 struct btrfs_key found_key
;
2185 struct extent_map
*em
= NULL
;
2186 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2187 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2188 struct btrfs_trans_handle
*trans
= NULL
;
2190 path
= btrfs_alloc_path();
2192 mutex_lock(&root
->fs_info
->fs_mutex
);
2195 spin_lock(&em_tree
->lock
);
2196 em
= lookup_extent_mapping(em_tree
, start
, len
);
2198 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2199 spin_unlock(&em_tree
->lock
);
2202 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2203 free_extent_map(em
);
2204 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2205 free_extent_map(em
);
2209 em
= alloc_extent_map(GFP_NOFS
);
2215 em
->start
= EXTENT_MAP_HOLE
;
2217 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2218 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2219 objectid
, start
, trans
!= NULL
);
2226 if (path
->slots
[0] == 0)
2231 leaf
= path
->nodes
[0];
2232 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2233 struct btrfs_file_extent_item
);
2234 /* are we inside the extent that was found? */
2235 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2236 found_type
= btrfs_key_type(&found_key
);
2237 if (found_key
.objectid
!= objectid
||
2238 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2242 found_type
= btrfs_file_extent_type(leaf
, item
);
2243 extent_start
= found_key
.offset
;
2244 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2245 extent_end
= extent_start
+
2246 btrfs_file_extent_num_bytes(leaf
, item
);
2248 if (start
< extent_start
|| start
>= extent_end
) {
2250 if (start
< extent_start
) {
2251 if (start
+ len
<= extent_start
)
2253 em
->len
= extent_end
- extent_start
;
2259 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2261 em
->start
= extent_start
;
2262 em
->len
= extent_end
- extent_start
;
2263 em
->block_start
= EXTENT_MAP_HOLE
;
2266 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2267 em
->block_start
= bytenr
;
2268 em
->start
= extent_start
;
2269 em
->len
= extent_end
- extent_start
;
2271 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2276 size_t extent_offset
;
2279 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2281 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2282 ~((u64
)root
->sectorsize
- 1);
2283 if (start
< extent_start
|| start
>= extent_end
) {
2285 if (start
< extent_start
) {
2286 if (start
+ len
<= extent_start
)
2288 em
->len
= extent_end
- extent_start
;
2294 em
->block_start
= EXTENT_MAP_INLINE
;
2297 em
->start
= extent_start
;
2302 page_start
= page_offset(page
) + pg_offset
;
2303 extent_offset
= page_start
- extent_start
;
2304 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2305 size
- extent_offset
);
2306 em
->start
= extent_start
+ extent_offset
;
2307 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2308 ~((u64
)root
->sectorsize
- 1);
2310 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2311 if (create
== 0 && !PageUptodate(page
)) {
2312 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2314 flush_dcache_page(page
);
2315 } else if (create
&& PageUptodate(page
)) {
2318 free_extent_map(em
);
2320 btrfs_release_path(root
, path
);
2321 trans
= btrfs_start_transaction(root
, 1);
2324 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2326 btrfs_mark_buffer_dirty(leaf
);
2329 set_extent_uptodate(io_tree
, em
->start
,
2330 extent_map_end(em
) - 1, GFP_NOFS
);
2333 printk("unkknown found_type %d\n", found_type
);
2340 em
->block_start
= EXTENT_MAP_HOLE
;
2342 btrfs_release_path(root
, path
);
2343 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2344 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2350 spin_lock(&em_tree
->lock
);
2351 ret
= add_extent_mapping(em_tree
, em
);
2352 /* it is possible that someone inserted the extent into the tree
2353 * while we had the lock dropped. It is also possible that
2354 * an overlapping map exists in the tree
2356 if (ret
== -EEXIST
) {
2357 struct extent_map
*existing
;
2358 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2359 if (existing
&& (existing
->start
> start
||
2360 existing
->start
+ existing
->len
<= start
)) {
2361 free_extent_map(existing
);
2365 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2368 err
= merge_extent_mapping(em_tree
, existing
,
2370 free_extent_map(existing
);
2372 free_extent_map(em
);
2377 printk("failing to insert %Lu %Lu\n",
2379 free_extent_map(em
);
2383 free_extent_map(em
);
2387 spin_unlock(&em_tree
->lock
);
2389 btrfs_free_path(path
);
2391 ret
= btrfs_end_transaction(trans
, root
);
2395 mutex_unlock(&root
->fs_info
->fs_mutex
);
2397 free_extent_map(em
);
2399 return ERR_PTR(err
);
2404 #if 0 /* waiting for O_DIRECT reads */
2405 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2406 struct buffer_head
*bh_result
, int create
)
2408 struct extent_map
*em
;
2409 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2410 struct btrfs_multi_bio
*multi
= NULL
;
2411 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2417 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2419 if (!em
|| IS_ERR(em
))
2422 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2426 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2431 len
= em
->start
+ em
->len
- start
;
2432 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2434 if (em
->block_start
== EXTENT_MAP_HOLE
||
2435 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2436 bh_result
->b_size
= len
;
2440 logical
= start
- em
->start
;
2441 logical
= em
->block_start
+ logical
;
2444 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2445 logical
, &map_length
, &multi
, 0);
2447 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2448 bh_result
->b_size
= min(map_length
, len
);
2450 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2451 set_buffer_mapped(bh_result
);
2454 free_extent_map(em
);
2459 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2460 const struct iovec
*iov
, loff_t offset
,
2461 unsigned long nr_segs
)
2465 struct file
*file
= iocb
->ki_filp
;
2466 struct inode
*inode
= file
->f_mapping
->host
;
2471 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2472 offset
, nr_segs
, btrfs_get_block
, NULL
);
2476 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2478 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2481 int btrfs_readpage(struct file
*file
, struct page
*page
)
2483 struct extent_io_tree
*tree
;
2484 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2485 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2488 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2490 struct extent_io_tree
*tree
;
2493 if (current
->flags
& PF_MEMALLOC
) {
2494 redirty_page_for_writepage(wbc
, page
);
2498 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2499 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2502 static int btrfs_writepages(struct address_space
*mapping
,
2503 struct writeback_control
*wbc
)
2505 struct extent_io_tree
*tree
;
2506 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2507 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2511 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2512 struct list_head
*pages
, unsigned nr_pages
)
2514 struct extent_io_tree
*tree
;
2515 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2516 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2520 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2522 struct extent_io_tree
*tree
;
2523 struct extent_map_tree
*map
;
2526 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2527 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2528 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2530 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
2531 ClearPagePrivate(page
);
2532 set_page_private(page
, 0);
2533 page_cache_release(page
);
2538 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2540 struct extent_io_tree
*tree
;
2542 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2543 extent_invalidatepage(tree
, page
, offset
);
2544 btrfs_releasepage(page
, GFP_NOFS
);
2545 if (PagePrivate(page
)) {
2546 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
2547 ClearPagePrivate(page
);
2548 set_page_private(page
, 0);
2549 page_cache_release(page
);
2554 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2555 * called from a page fault handler when a page is first dirtied. Hence we must
2556 * be careful to check for EOF conditions here. We set the page up correctly
2557 * for a written page which means we get ENOSPC checking when writing into
2558 * holes and correct delalloc and unwritten extent mapping on filesystems that
2559 * support these features.
2561 * We are not allowed to take the i_mutex here so we have to play games to
2562 * protect against truncate races as the page could now be beyond EOF. Because
2563 * vmtruncate() writes the inode size before removing pages, once we have the
2564 * page lock we can determine safely if the page is beyond EOF. If it is not
2565 * beyond EOF, then the page is guaranteed safe against truncation until we
2568 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2570 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2571 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2577 mutex_lock(&root
->fs_info
->fs_mutex
);
2578 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2579 mutex_unlock(&root
->fs_info
->fs_mutex
);
2586 wait_on_page_writeback(page
);
2587 size
= i_size_read(inode
);
2588 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2590 if ((page
->mapping
!= inode
->i_mapping
) ||
2591 (page_start
> size
)) {
2592 /* page got truncated out from underneath us */
2596 /* page is wholly or partially inside EOF */
2597 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2598 end
= size
& ~PAGE_CACHE_MASK
;
2600 end
= PAGE_CACHE_SIZE
;
2602 ret
= btrfs_cow_one_page(inode
, page
, end
);
2610 static void btrfs_truncate(struct inode
*inode
)
2612 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2614 struct btrfs_trans_handle
*trans
;
2617 if (!S_ISREG(inode
->i_mode
))
2619 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2622 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2624 mutex_lock(&root
->fs_info
->fs_mutex
);
2625 trans
= btrfs_start_transaction(root
, 1);
2626 btrfs_set_trans_block_group(trans
, inode
);
2628 /* FIXME, add redo link to tree so we don't leak on crash */
2629 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2630 BTRFS_EXTENT_DATA_KEY
);
2631 btrfs_update_inode(trans
, root
, inode
);
2632 nr
= trans
->blocks_used
;
2634 ret
= btrfs_end_transaction(trans
, root
);
2636 mutex_unlock(&root
->fs_info
->fs_mutex
);
2637 btrfs_btree_balance_dirty(root
, nr
);
2638 btrfs_throttle(root
);
2641 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2644 struct btrfs_trans_handle
*trans
;
2645 struct btrfs_key key
;
2646 struct btrfs_root_item root_item
;
2647 struct btrfs_inode_item
*inode_item
;
2648 struct extent_buffer
*leaf
;
2649 struct btrfs_root
*new_root
= root
;
2650 struct inode
*inode
;
2655 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2656 unsigned long nr
= 1;
2658 mutex_lock(&root
->fs_info
->fs_mutex
);
2659 ret
= btrfs_check_free_space(root
, 1, 0);
2663 trans
= btrfs_start_transaction(root
, 1);
2666 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2671 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2672 objectid
, trans
->transid
, 0, 0,
2675 return PTR_ERR(leaf
);
2677 btrfs_set_header_nritems(leaf
, 0);
2678 btrfs_set_header_level(leaf
, 0);
2679 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2680 btrfs_set_header_generation(leaf
, trans
->transid
);
2681 btrfs_set_header_owner(leaf
, objectid
);
2683 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2684 (unsigned long)btrfs_header_fsid(leaf
),
2686 btrfs_mark_buffer_dirty(leaf
);
2688 inode_item
= &root_item
.inode
;
2689 memset(inode_item
, 0, sizeof(*inode_item
));
2690 inode_item
->generation
= cpu_to_le64(1);
2691 inode_item
->size
= cpu_to_le64(3);
2692 inode_item
->nlink
= cpu_to_le32(1);
2693 inode_item
->nblocks
= cpu_to_le64(1);
2694 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2696 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2697 btrfs_set_root_level(&root_item
, 0);
2698 btrfs_set_root_refs(&root_item
, 1);
2699 btrfs_set_root_used(&root_item
, 0);
2701 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2702 root_item
.drop_level
= 0;
2704 free_extent_buffer(leaf
);
2707 btrfs_set_root_dirid(&root_item
, new_dirid
);
2709 key
.objectid
= objectid
;
2711 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2712 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2718 * insert the directory item
2720 key
.offset
= (u64
)-1;
2721 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2722 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2723 name
, namelen
, dir
->i_ino
, &key
,
2728 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2729 name
, namelen
, objectid
,
2730 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2734 ret
= btrfs_commit_transaction(trans
, root
);
2738 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2741 trans
= btrfs_start_transaction(new_root
, 1);
2744 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2746 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2749 inode
->i_op
= &btrfs_dir_inode_operations
;
2750 inode
->i_fop
= &btrfs_dir_file_operations
;
2751 new_root
->inode
= inode
;
2753 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2757 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2761 nr
= trans
->blocks_used
;
2762 err
= btrfs_commit_transaction(trans
, new_root
);
2766 mutex_unlock(&root
->fs_info
->fs_mutex
);
2767 btrfs_btree_balance_dirty(root
, nr
);
2768 btrfs_throttle(root
);
2772 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2774 struct btrfs_pending_snapshot
*pending_snapshot
;
2775 struct btrfs_trans_handle
*trans
;
2778 unsigned long nr
= 0;
2780 if (!root
->ref_cows
)
2783 mutex_lock(&root
->fs_info
->fs_mutex
);
2784 ret
= btrfs_check_free_space(root
, 1, 0);
2788 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2789 if (!pending_snapshot
) {
2793 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
2794 if (!pending_snapshot
->name
) {
2796 kfree(pending_snapshot
);
2799 memcpy(pending_snapshot
->name
, name
, namelen
);
2800 pending_snapshot
->name
[namelen
] = '\0';
2801 trans
= btrfs_start_transaction(root
, 1);
2803 pending_snapshot
->root
= root
;
2804 list_add(&pending_snapshot
->list
,
2805 &trans
->transaction
->pending_snapshots
);
2806 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2807 err
= btrfs_commit_transaction(trans
, root
);
2810 mutex_unlock(&root
->fs_info
->fs_mutex
);
2811 btrfs_btree_balance_dirty(root
, nr
);
2812 btrfs_throttle(root
);
2816 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2817 struct file_ra_state
*ra
, struct file
*file
,
2818 pgoff_t offset
, pgoff_t last_index
)
2820 pgoff_t req_size
= last_index
- offset
+ 1;
2822 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2823 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2826 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2827 return offset
+ req_size
;
2831 int btrfs_defrag_file(struct file
*file
) {
2832 struct inode
*inode
= fdentry(file
)->d_inode
;
2833 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2834 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2836 unsigned long last_index
;
2837 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
2838 unsigned long total_read
= 0;
2844 mutex_lock(&root
->fs_info
->fs_mutex
);
2845 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2846 mutex_unlock(&root
->fs_info
->fs_mutex
);
2850 mutex_lock(&inode
->i_mutex
);
2851 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2852 for (i
= 0; i
<= last_index
; i
++) {
2853 if (total_read
% ra_pages
== 0) {
2854 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
2855 min(last_index
, i
+ ra_pages
- 1));
2858 page
= grab_cache_page(inode
->i_mapping
, i
);
2861 if (!PageUptodate(page
)) {
2862 btrfs_readpage(NULL
, page
);
2864 if (!PageUptodate(page
)) {
2866 page_cache_release(page
);
2871 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2872 ClearPageDirty(page
);
2874 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
2876 wait_on_page_writeback(page
);
2877 set_page_extent_mapped(page
);
2879 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2880 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2882 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2883 set_extent_delalloc(io_tree
, page_start
,
2884 page_end
, GFP_NOFS
);
2886 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2887 set_page_dirty(page
);
2889 page_cache_release(page
);
2890 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2894 mutex_unlock(&inode
->i_mutex
);
2898 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2903 struct btrfs_ioctl_vol_args
*vol_args
;
2904 struct btrfs_trans_handle
*trans
;
2905 struct btrfs_device
*device
= NULL
;
2907 char *devstr
= NULL
;
2912 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2917 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2921 namelen
= strlen(vol_args
->name
);
2922 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2927 mutex_lock(&root
->fs_info
->fs_mutex
);
2928 sizestr
= vol_args
->name
;
2929 devstr
= strchr(sizestr
, ':');
2932 sizestr
= devstr
+ 1;
2934 devstr
= vol_args
->name
;
2935 devid
= simple_strtoull(devstr
, &end
, 10);
2936 printk("resizing devid %Lu\n", devid
);
2938 device
= btrfs_find_device(root
, devid
, NULL
);
2940 printk("resizer unable to find device %Lu\n", devid
);
2944 if (!strcmp(sizestr
, "max"))
2945 new_size
= device
->bdev
->bd_inode
->i_size
;
2947 if (sizestr
[0] == '-') {
2950 } else if (sizestr
[0] == '+') {
2954 new_size
= btrfs_parse_size(sizestr
);
2955 if (new_size
== 0) {
2961 old_size
= device
->total_bytes
;
2964 if (new_size
> old_size
) {
2968 new_size
= old_size
- new_size
;
2969 } else if (mod
> 0) {
2970 new_size
= old_size
+ new_size
;
2973 if (new_size
< 256 * 1024 * 1024) {
2977 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
2982 do_div(new_size
, root
->sectorsize
);
2983 new_size
*= root
->sectorsize
;
2985 printk("new size for %s is %llu\n", device
->name
, (unsigned long long)new_size
);
2987 if (new_size
> old_size
) {
2988 trans
= btrfs_start_transaction(root
, 1);
2989 ret
= btrfs_grow_device(trans
, device
, new_size
);
2990 btrfs_commit_transaction(trans
, root
);
2992 ret
= btrfs_shrink_device(device
, new_size
);
2996 mutex_unlock(&root
->fs_info
->fs_mutex
);
3002 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
3005 struct btrfs_ioctl_vol_args
*vol_args
;
3006 struct btrfs_dir_item
*di
;
3007 struct btrfs_path
*path
;
3012 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
3017 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
3022 namelen
= strlen(vol_args
->name
);
3023 if (namelen
> BTRFS_VOL_NAME_MAX
) {
3027 if (strchr(vol_args
->name
, '/')) {
3032 path
= btrfs_alloc_path();
3038 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
3039 mutex_lock(&root
->fs_info
->fs_mutex
);
3040 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
3042 vol_args
->name
, namelen
, 0);
3043 mutex_unlock(&root
->fs_info
->fs_mutex
);
3044 btrfs_free_path(path
);
3046 if (di
&& !IS_ERR(di
)) {
3056 if (root
== root
->fs_info
->tree_root
)
3057 ret
= create_subvol(root
, vol_args
->name
, namelen
);
3059 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
3065 static int btrfs_ioctl_defrag(struct file
*file
)
3067 struct inode
*inode
= fdentry(file
)->d_inode
;
3068 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3070 switch (inode
->i_mode
& S_IFMT
) {
3072 mutex_lock(&root
->fs_info
->fs_mutex
);
3073 btrfs_defrag_root(root
, 0);
3074 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
3075 mutex_unlock(&root
->fs_info
->fs_mutex
);
3078 btrfs_defrag_file(file
);
3085 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
3087 struct btrfs_ioctl_vol_args
*vol_args
;
3090 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
3095 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
3099 ret
= btrfs_init_new_device(root
, vol_args
->name
);
3106 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
3108 struct btrfs_ioctl_vol_args
*vol_args
;
3111 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
3116 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
3120 ret
= btrfs_rm_device(root
, vol_args
->name
);
3127 int dup_item_to_inode(struct btrfs_trans_handle
*trans
,
3128 struct btrfs_root
*root
,
3129 struct btrfs_path
*path
,
3130 struct extent_buffer
*leaf
,
3132 struct btrfs_key
*key
,
3136 int len
= btrfs_item_size_nr(leaf
, slot
);
3137 struct btrfs_key ckey
= *key
;
3140 dup
= kmalloc(len
, GFP_NOFS
);
3144 read_extent_buffer(leaf
, dup
, btrfs_item_ptr_offset(leaf
, slot
), len
);
3145 btrfs_release_path(root
, path
);
3147 ckey
.objectid
= destino
;
3148 ret
= btrfs_insert_item(trans
, root
, &ckey
, dup
, len
);
3153 long btrfs_ioctl_clone(struct file
*file
, unsigned long src_fd
)
3155 struct inode
*inode
= fdentry(file
)->d_inode
;
3156 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3157 struct file
*src_file
;
3159 struct btrfs_trans_handle
*trans
;
3162 struct btrfs_path
*path
;
3163 struct btrfs_key key
;
3164 struct extent_buffer
*leaf
;
3168 src_file
= fget(src_fd
);
3171 src
= src_file
->f_dentry
->d_inode
;
3174 if (src
->i_sb
!= inode
->i_sb
)
3178 mutex_lock(&inode
->i_mutex
);
3179 mutex_lock(&src
->i_mutex
);
3181 mutex_lock(&src
->i_mutex
);
3182 mutex_lock(&inode
->i_mutex
);
3189 /* do any pending delalloc/csum calc on src, one way or
3190 another, and lock file content */
3192 filemap_write_and_wait(src
->i_mapping
);
3193 lock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
3194 if (BTRFS_I(src
)->delalloc_bytes
== 0)
3196 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
3199 mutex_lock(&root
->fs_info
->fs_mutex
);
3200 trans
= btrfs_start_transaction(root
, 0);
3201 path
= btrfs_alloc_path();
3207 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3208 key
.objectid
= src
->i_ino
;
3214 * note the key will change type as we walk through the
3217 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
3221 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
3222 ret
= btrfs_next_leaf(root
, path
);
3228 leaf
= path
->nodes
[0];
3229 slot
= path
->slots
[0];
3230 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3231 nritems
= btrfs_header_nritems(leaf
);
3233 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
3234 key
.objectid
!= src
->i_ino
)
3237 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
3238 struct btrfs_file_extent_item
*extent
;
3241 extent
= btrfs_item_ptr(leaf
, slot
,
3242 struct btrfs_file_extent_item
);
3243 found_type
= btrfs_file_extent_type(leaf
, extent
);
3244 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
3245 u64 len
= btrfs_file_extent_num_bytes(leaf
,
3247 u64 ds
= btrfs_file_extent_disk_bytenr(leaf
,
3249 u64 dl
= btrfs_file_extent_disk_num_bytes(leaf
,
3251 u64 off
= btrfs_file_extent_offset(leaf
,
3253 btrfs_insert_file_extent(trans
, root
,
3256 /* ds == 0 means there's a hole */
3258 btrfs_inc_extent_ref(trans
, root
,
3260 root
->root_key
.objectid
,
3264 pos
= key
.offset
+ len
;
3265 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
3266 ret
= dup_item_to_inode(trans
, root
, path
,
3271 pos
= key
.offset
+ btrfs_item_size_nr(leaf
,
3274 } else if (btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
3275 ret
= dup_item_to_inode(trans
, root
, path
, leaf
,
3276 slot
, &key
, inode
->i_ino
);
3282 btrfs_release_path(root
, path
);
3287 btrfs_free_path(path
);
3289 inode
->i_blocks
= src
->i_blocks
;
3290 i_size_write(inode
, src
->i_size
);
3291 btrfs_update_inode(trans
, root
, inode
);
3293 unlock_extent(&BTRFS_I(src
)->io_tree
, 0, (u64
)-1, GFP_NOFS
);
3295 btrfs_end_transaction(trans
, root
);
3296 mutex_unlock(&root
->fs_info
->fs_mutex
);
3299 mutex_unlock(&src
->i_mutex
);
3300 mutex_unlock(&inode
->i_mutex
);
3306 long btrfs_ioctl(struct file
*file
, unsigned int
3307 cmd
, unsigned long arg
)
3309 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3312 case BTRFS_IOC_SNAP_CREATE
:
3313 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
3314 case BTRFS_IOC_DEFRAG
:
3315 return btrfs_ioctl_defrag(file
);
3316 case BTRFS_IOC_RESIZE
:
3317 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
3318 case BTRFS_IOC_ADD_DEV
:
3319 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
3320 case BTRFS_IOC_RM_DEV
:
3321 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
3322 case BTRFS_IOC_BALANCE
:
3323 return btrfs_balance(root
->fs_info
->dev_root
);
3324 case BTRFS_IOC_CLONE
:
3325 return btrfs_ioctl_clone(file
, arg
);
3332 * Called inside transaction, so use GFP_NOFS
3334 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3336 struct btrfs_inode
*ei
;
3338 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3342 ei
->ordered_trans
= 0;
3343 return &ei
->vfs_inode
;
3346 void btrfs_destroy_inode(struct inode
*inode
)
3348 WARN_ON(!list_empty(&inode
->i_dentry
));
3349 WARN_ON(inode
->i_data
.nrpages
);
3351 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
3352 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3355 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3356 static void init_once(struct kmem_cache
* cachep
, void *foo
)
3358 static void init_once(void * foo
, struct kmem_cache
* cachep
,
3359 unsigned long flags
)
3362 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3364 inode_init_once(&ei
->vfs_inode
);
3367 void btrfs_destroy_cachep(void)
3369 if (btrfs_inode_cachep
)
3370 kmem_cache_destroy(btrfs_inode_cachep
);
3371 if (btrfs_trans_handle_cachep
)
3372 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3373 if (btrfs_transaction_cachep
)
3374 kmem_cache_destroy(btrfs_transaction_cachep
);
3375 if (btrfs_bit_radix_cachep
)
3376 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3377 if (btrfs_path_cachep
)
3378 kmem_cache_destroy(btrfs_path_cachep
);
3381 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3382 unsigned long extra_flags
,
3383 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3384 void (*ctor
)(struct kmem_cache
*, void *)
3386 void (*ctor
)(void *, struct kmem_cache
*,
3391 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3392 SLAB_MEM_SPREAD
| extra_flags
), ctor
3393 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3399 int btrfs_init_cachep(void)
3401 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3402 sizeof(struct btrfs_inode
),
3404 if (!btrfs_inode_cachep
)
3406 btrfs_trans_handle_cachep
=
3407 btrfs_cache_create("btrfs_trans_handle_cache",
3408 sizeof(struct btrfs_trans_handle
),
3410 if (!btrfs_trans_handle_cachep
)
3412 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3413 sizeof(struct btrfs_transaction
),
3415 if (!btrfs_transaction_cachep
)
3417 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3418 sizeof(struct btrfs_path
),
3420 if (!btrfs_path_cachep
)
3422 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3423 SLAB_DESTROY_BY_RCU
, NULL
);
3424 if (!btrfs_bit_radix_cachep
)
3428 btrfs_destroy_cachep();
3432 static int btrfs_getattr(struct vfsmount
*mnt
,
3433 struct dentry
*dentry
, struct kstat
*stat
)
3435 struct inode
*inode
= dentry
->d_inode
;
3436 generic_fillattr(inode
, stat
);
3437 stat
->blksize
= PAGE_CACHE_SIZE
;
3438 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3442 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3443 struct inode
* new_dir
,struct dentry
*new_dentry
)
3445 struct btrfs_trans_handle
*trans
;
3446 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3447 struct inode
*new_inode
= new_dentry
->d_inode
;
3448 struct inode
*old_inode
= old_dentry
->d_inode
;
3449 struct timespec ctime
= CURRENT_TIME
;
3450 struct btrfs_path
*path
;
3453 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3454 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3458 mutex_lock(&root
->fs_info
->fs_mutex
);
3459 ret
= btrfs_check_free_space(root
, 1, 0);
3463 trans
= btrfs_start_transaction(root
, 1);
3465 btrfs_set_trans_block_group(trans
, new_dir
);
3466 path
= btrfs_alloc_path();
3472 old_dentry
->d_inode
->i_nlink
++;
3473 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3474 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3475 old_inode
->i_ctime
= ctime
;
3477 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3482 new_inode
->i_ctime
= CURRENT_TIME
;
3483 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3487 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3492 btrfs_free_path(path
);
3493 btrfs_end_transaction(trans
, root
);
3495 mutex_unlock(&root
->fs_info
->fs_mutex
);
3499 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3500 const char *symname
)
3502 struct btrfs_trans_handle
*trans
;
3503 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3504 struct btrfs_path
*path
;
3505 struct btrfs_key key
;
3506 struct inode
*inode
= NULL
;
3513 struct btrfs_file_extent_item
*ei
;
3514 struct extent_buffer
*leaf
;
3515 unsigned long nr
= 0;
3517 name_len
= strlen(symname
) + 1;
3518 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3519 return -ENAMETOOLONG
;
3521 mutex_lock(&root
->fs_info
->fs_mutex
);
3522 err
= btrfs_check_free_space(root
, 1, 0);
3526 trans
= btrfs_start_transaction(root
, 1);
3527 btrfs_set_trans_block_group(trans
, dir
);
3529 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3535 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
3537 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3538 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3539 err
= PTR_ERR(inode
);
3543 btrfs_set_trans_block_group(trans
, inode
);
3544 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3548 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3549 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3550 inode
->i_fop
= &btrfs_file_operations
;
3551 inode
->i_op
= &btrfs_file_inode_operations
;
3552 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3553 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3554 inode
->i_mapping
, GFP_NOFS
);
3555 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3556 inode
->i_mapping
, GFP_NOFS
);
3557 BTRFS_I(inode
)->delalloc_bytes
= 0;
3558 atomic_set(&BTRFS_I(inode
)->ordered_writeback
, 0);
3559 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3561 dir
->i_sb
->s_dirt
= 1;
3562 btrfs_update_inode_block_group(trans
, inode
);
3563 btrfs_update_inode_block_group(trans
, dir
);
3567 path
= btrfs_alloc_path();
3569 key
.objectid
= inode
->i_ino
;
3571 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3572 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3573 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3579 leaf
= path
->nodes
[0];
3580 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3581 struct btrfs_file_extent_item
);
3582 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3583 btrfs_set_file_extent_type(leaf
, ei
,
3584 BTRFS_FILE_EXTENT_INLINE
);
3585 ptr
= btrfs_file_extent_inline_start(ei
);
3586 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3587 btrfs_mark_buffer_dirty(leaf
);
3588 btrfs_free_path(path
);
3590 inode
->i_op
= &btrfs_symlink_inode_operations
;
3591 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3592 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3593 inode
->i_size
= name_len
- 1;
3594 err
= btrfs_update_inode(trans
, root
, inode
);
3599 nr
= trans
->blocks_used
;
3600 btrfs_end_transaction(trans
, root
);
3602 mutex_unlock(&root
->fs_info
->fs_mutex
);
3604 inode_dec_link_count(inode
);
3607 btrfs_btree_balance_dirty(root
, nr
);
3608 btrfs_throttle(root
);
3612 static int btrfs_permission(struct inode
*inode
, int mask
,
3613 struct nameidata
*nd
)
3615 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3617 return generic_permission(inode
, mask
, NULL
);
3620 static struct inode_operations btrfs_dir_inode_operations
= {
3621 .lookup
= btrfs_lookup
,
3622 .create
= btrfs_create
,
3623 .unlink
= btrfs_unlink
,
3625 .mkdir
= btrfs_mkdir
,
3626 .rmdir
= btrfs_rmdir
,
3627 .rename
= btrfs_rename
,
3628 .symlink
= btrfs_symlink
,
3629 .setattr
= btrfs_setattr
,
3630 .mknod
= btrfs_mknod
,
3631 .setxattr
= generic_setxattr
,
3632 .getxattr
= generic_getxattr
,
3633 .listxattr
= btrfs_listxattr
,
3634 .removexattr
= generic_removexattr
,
3635 .permission
= btrfs_permission
,
3637 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3638 .lookup
= btrfs_lookup
,
3639 .permission
= btrfs_permission
,
3641 static struct file_operations btrfs_dir_file_operations
= {
3642 .llseek
= generic_file_llseek
,
3643 .read
= generic_read_dir
,
3644 .readdir
= btrfs_readdir
,
3645 .unlocked_ioctl
= btrfs_ioctl
,
3646 #ifdef CONFIG_COMPAT
3647 .compat_ioctl
= btrfs_ioctl
,
3651 static struct extent_io_ops btrfs_extent_io_ops
= {
3652 .fill_delalloc
= run_delalloc_range
,
3653 .submit_bio_hook
= btrfs_submit_bio_hook
,
3654 .merge_bio_hook
= btrfs_merge_bio_hook
,
3655 .readpage_io_hook
= btrfs_readpage_io_hook
,
3656 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3657 .readpage_io_failed_hook
= btrfs_readpage_io_failed_hook
,
3658 .set_bit_hook
= btrfs_set_bit_hook
,
3659 .clear_bit_hook
= btrfs_clear_bit_hook
,
3662 static struct address_space_operations btrfs_aops
= {
3663 .readpage
= btrfs_readpage
,
3664 .writepage
= btrfs_writepage
,
3665 .writepages
= btrfs_writepages
,
3666 .readpages
= btrfs_readpages
,
3667 .sync_page
= block_sync_page
,
3669 .direct_IO
= btrfs_direct_IO
,
3670 .invalidatepage
= btrfs_invalidatepage
,
3671 .releasepage
= btrfs_releasepage
,
3672 .set_page_dirty
= __set_page_dirty_nobuffers
,
3675 static struct address_space_operations btrfs_symlink_aops
= {
3676 .readpage
= btrfs_readpage
,
3677 .writepage
= btrfs_writepage
,
3678 .invalidatepage
= btrfs_invalidatepage
,
3679 .releasepage
= btrfs_releasepage
,
3682 static struct inode_operations btrfs_file_inode_operations
= {
3683 .truncate
= btrfs_truncate
,
3684 .getattr
= btrfs_getattr
,
3685 .setattr
= btrfs_setattr
,
3686 .setxattr
= generic_setxattr
,
3687 .getxattr
= generic_getxattr
,
3688 .listxattr
= btrfs_listxattr
,
3689 .removexattr
= generic_removexattr
,
3690 .permission
= btrfs_permission
,
3692 static struct inode_operations btrfs_special_inode_operations
= {
3693 .getattr
= btrfs_getattr
,
3694 .setattr
= btrfs_setattr
,
3695 .permission
= btrfs_permission
,
3697 static struct inode_operations btrfs_symlink_inode_operations
= {
3698 .readlink
= generic_readlink
,
3699 .follow_link
= page_follow_link_light
,
3700 .put_link
= page_put_link
,
3701 .permission
= btrfs_permission
,