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/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
45 struct btrfs_iget_args
{
47 struct btrfs_root
*root
;
50 static struct inode_operations btrfs_dir_inode_operations
;
51 static struct inode_operations btrfs_symlink_inode_operations
;
52 static struct inode_operations btrfs_dir_ro_inode_operations
;
53 static struct inode_operations btrfs_special_inode_operations
;
54 static struct inode_operations btrfs_file_inode_operations
;
55 static struct address_space_operations btrfs_aops
;
56 static struct address_space_operations btrfs_symlink_aops
;
57 static struct file_operations btrfs_dir_file_operations
;
58 static struct extent_io_ops btrfs_extent_io_ops
;
60 static struct kmem_cache
*btrfs_inode_cachep
;
61 struct kmem_cache
*btrfs_trans_handle_cachep
;
62 struct kmem_cache
*btrfs_transaction_cachep
;
63 struct kmem_cache
*btrfs_bit_radix_cachep
;
64 struct kmem_cache
*btrfs_path_cachep
;
67 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
68 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
69 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
70 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
71 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
72 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
73 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
74 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
77 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
80 u64 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
81 u64 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
92 spin_lock(&root
->fs_info
->delalloc_lock
);
93 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
95 spin_unlock(&root
->fs_info
->delalloc_lock
);
99 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
101 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
102 struct btrfs_trans_handle
*trans
;
106 u64 blocksize
= root
->sectorsize
;
107 u64 orig_start
= start
;
109 struct btrfs_key ins
;
112 trans
= btrfs_start_transaction(root
, 1);
114 btrfs_set_trans_block_group(trans
, inode
);
116 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
117 num_bytes
= max(blocksize
, num_bytes
);
118 ret
= btrfs_drop_extents(trans
, root
, inode
,
119 start
, start
+ num_bytes
, start
, &alloc_hint
);
120 orig_num_bytes
= num_bytes
;
122 if (alloc_hint
== EXTENT_MAP_INLINE
)
125 while(num_bytes
> 0) {
126 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
127 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
129 root
->root_key
.objectid
,
131 inode
->i_ino
, start
, 0,
132 alloc_hint
, (u64
)-1, &ins
, 1);
137 cur_alloc_size
= ins
.offset
;
138 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
139 start
, ins
.objectid
, ins
.offset
,
141 inode
->i_blocks
+= ins
.offset
>> 9;
142 btrfs_check_file(root
, inode
);
143 num_bytes
-= cur_alloc_size
;
144 alloc_hint
= ins
.objectid
+ ins
.offset
;
145 start
+= cur_alloc_size
;
147 btrfs_drop_extent_cache(inode
, orig_start
,
148 orig_start
+ orig_num_bytes
- 1);
149 btrfs_add_ordered_inode(inode
);
150 btrfs_update_inode(trans
, root
, inode
);
152 btrfs_end_transaction(trans
, root
);
156 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
164 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
165 struct extent_buffer
*leaf
;
167 struct btrfs_path
*path
;
168 struct btrfs_file_extent_item
*item
;
171 struct btrfs_key found_key
;
173 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
174 path
= btrfs_alloc_path();
177 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
178 inode
->i_ino
, start
, 0);
180 btrfs_free_path(path
);
186 if (path
->slots
[0] == 0)
191 leaf
= path
->nodes
[0];
192 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
193 struct btrfs_file_extent_item
);
195 /* are we inside the extent that was found? */
196 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
197 found_type
= btrfs_key_type(&found_key
);
198 if (found_key
.objectid
!= inode
->i_ino
||
199 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
203 found_type
= btrfs_file_extent_type(leaf
, item
);
204 extent_start
= found_key
.offset
;
205 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
206 u64 extent_num_bytes
;
208 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
209 extent_end
= extent_start
+ extent_num_bytes
;
212 if (loops
&& start
!= extent_start
)
215 if (start
< extent_start
|| start
>= extent_end
)
218 cow_end
= min(end
, extent_end
- 1);
219 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
224 * we may be called by the resizer, make sure we're inside
225 * the limits of the FS
227 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
230 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
240 btrfs_free_path(path
);
243 btrfs_release_path(root
, path
);
248 cow_file_range(inode
, start
, cow_end
);
253 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
255 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
257 mutex_lock(&root
->fs_info
->fs_mutex
);
258 if (btrfs_test_opt(root
, NODATACOW
) ||
259 btrfs_test_flag(inode
, NODATACOW
))
260 ret
= run_delalloc_nocow(inode
, start
, end
);
262 ret
= cow_file_range(inode
, start
, end
);
264 mutex_unlock(&root
->fs_info
->fs_mutex
);
268 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
269 unsigned long old
, unsigned long bits
)
271 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
272 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
273 spin_lock(&root
->fs_info
->delalloc_lock
);
274 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
275 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
276 spin_unlock(&root
->fs_info
->delalloc_lock
);
281 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
282 unsigned long old
, unsigned long bits
)
284 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
285 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
286 spin_lock(&root
->fs_info
->delalloc_lock
);
287 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
288 printk("warning: delalloc account %Lu %Lu\n",
289 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
290 root
->fs_info
->delalloc_bytes
= 0;
291 BTRFS_I(inode
)->delalloc_bytes
= 0;
293 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
294 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
296 spin_unlock(&root
->fs_info
->delalloc_lock
);
301 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
302 size_t size
, struct bio
*bio
)
304 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
305 struct btrfs_mapping_tree
*map_tree
;
306 u64 logical
= bio
->bi_sector
<< 9;
309 struct bio_vec
*bvec
;
313 bio_for_each_segment(bvec
, bio
, i
) {
314 length
+= bvec
->bv_len
;
316 map_tree
= &root
->fs_info
->mapping_tree
;
318 ret
= btrfs_map_block(map_tree
, READ
, logical
,
319 &map_length
, NULL
, 0);
321 if (map_length
< length
+ size
) {
327 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
330 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
331 struct btrfs_trans_handle
*trans
;
335 ret
= btrfs_csum_one_bio(root
, bio
, &sums
);
338 mutex_lock(&root
->fs_info
->fs_mutex
);
339 trans
= btrfs_start_transaction(root
, 1);
341 btrfs_set_trans_block_group(trans
, inode
);
342 btrfs_csum_file_blocks(trans
, root
, inode
, bio
, sums
);
344 ret
= btrfs_end_transaction(trans
, root
);
346 mutex_unlock(&root
->fs_info
->fs_mutex
);
350 return btrfs_map_bio(root
, rw
, bio
, mirror_num
);
353 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
359 if (!(rw
& (1 << BIO_RW
))) {
360 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
365 if (btrfs_test_opt(root
, NODATASUM
) ||
366 btrfs_test_flag(inode
, NODATASUM
)) {
370 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
371 inode
, rw
, bio
, mirror_num
,
372 __btrfs_submit_bio_hook
);
374 return btrfs_map_bio(root
, rw
, bio
, mirror_num
);
377 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
380 struct inode
*inode
= page
->mapping
->host
;
381 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
382 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
383 struct btrfs_csum_item
*item
;
384 struct btrfs_path
*path
= NULL
;
387 if (btrfs_test_opt(root
, NODATASUM
) ||
388 btrfs_test_flag(inode
, NODATASUM
))
391 mutex_lock(&root
->fs_info
->fs_mutex
);
392 path
= btrfs_alloc_path();
393 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
396 /* a csum that isn't present is a preallocated region. */
397 if (ret
== -ENOENT
|| ret
== -EFBIG
)
400 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
, start
);
403 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
405 set_state_private(io_tree
, start
, csum
);
408 btrfs_free_path(path
);
409 mutex_unlock(&root
->fs_info
->fs_mutex
);
413 struct io_failure_record
{
421 int btrfs_readpage_io_failed_hook(struct bio
*failed_bio
,
422 struct page
*page
, u64 start
, u64 end
,
423 struct extent_state
*state
)
425 struct io_failure_record
*failrec
= NULL
;
427 struct extent_map
*em
;
428 struct inode
*inode
= page
->mapping
->host
;
429 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
435 ret
= get_state_private(failure_tree
, start
, &private);
437 size_t pg_offset
= start
- page_offset(page
);
438 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
441 failrec
->start
= start
;
442 failrec
->len
= end
- start
+ 1;
443 failrec
->last_mirror
= 0;
445 em
= btrfs_get_extent(inode
, NULL
, pg_offset
, start
,
448 if (!em
|| IS_ERR(em
)) {
452 logical
= start
- em
->start
;
453 logical
= em
->block_start
+ logical
;
454 failrec
->logical
= logical
;
456 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
457 EXTENT_DIRTY
, GFP_NOFS
);
458 set_state_private(failure_tree
, start
,
459 (u64
)(unsigned long)failrec
);
461 failrec
= (struct io_failure_record
*)(unsigned long)private;
463 num_copies
= btrfs_num_copies(
464 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
465 failrec
->logical
, failrec
->len
);
466 failrec
->last_mirror
++;
468 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
469 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
472 if (state
&& state
->start
!= failrec
->start
)
474 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
476 if (!state
|| failrec
->last_mirror
> num_copies
) {
477 set_state_private(failure_tree
, failrec
->start
, 0);
478 clear_extent_bits(failure_tree
, failrec
->start
,
479 failrec
->start
+ failrec
->len
- 1,
480 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
484 bio
= bio_alloc(GFP_NOFS
, 1);
485 bio
->bi_private
= state
;
486 bio
->bi_end_io
= failed_bio
->bi_end_io
;
487 bio
->bi_sector
= failrec
->logical
>> 9;
488 bio
->bi_bdev
= failed_bio
->bi_bdev
;
489 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
490 btrfs_submit_bio_hook(inode
, READ
, bio
, failrec
->last_mirror
);
494 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
495 struct extent_state
*state
)
497 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
498 struct inode
*inode
= page
->mapping
->host
;
499 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
501 u64
private = ~(u32
)0;
503 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
507 if (btrfs_test_opt(root
, NODATASUM
) ||
508 btrfs_test_flag(inode
, NODATASUM
))
510 if (state
&& state
->start
== start
) {
511 private = state
->private;
514 ret
= get_state_private(io_tree
, start
, &private);
516 local_irq_save(flags
);
517 kaddr
= kmap_atomic(page
, KM_IRQ0
);
521 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
522 btrfs_csum_final(csum
, (char *)&csum
);
523 if (csum
!= private) {
526 kunmap_atomic(kaddr
, KM_IRQ0
);
527 local_irq_restore(flags
);
529 /* if the io failure tree for this inode is non-empty,
530 * check to see if we've recovered from a failed IO
533 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
534 (u64
)-1, 1, EXTENT_DIRTY
)) {
536 struct io_failure_record
*failure
;
537 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
538 start
, &private_failure
);
540 failure
= (struct io_failure_record
*)(unsigned long)
542 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
544 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
546 failure
->start
+ failure
->len
- 1,
547 EXTENT_DIRTY
| EXTENT_LOCKED
,
555 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
556 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
558 memset(kaddr
+ offset
, 1, end
- start
+ 1);
559 flush_dcache_page(page
);
560 kunmap_atomic(kaddr
, KM_IRQ0
);
561 local_irq_restore(flags
);
565 void btrfs_read_locked_inode(struct inode
*inode
)
567 struct btrfs_path
*path
;
568 struct extent_buffer
*leaf
;
569 struct btrfs_inode_item
*inode_item
;
570 struct btrfs_timespec
*tspec
;
571 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
572 struct btrfs_key location
;
573 u64 alloc_group_block
;
577 path
= btrfs_alloc_path();
579 mutex_lock(&root
->fs_info
->fs_mutex
);
580 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
582 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
586 leaf
= path
->nodes
[0];
587 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
588 struct btrfs_inode_item
);
590 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
591 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
592 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
593 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
594 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
596 tspec
= btrfs_inode_atime(inode_item
);
597 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
598 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
600 tspec
= btrfs_inode_mtime(inode_item
);
601 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
602 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
604 tspec
= btrfs_inode_ctime(inode_item
);
605 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
606 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
608 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
609 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
611 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
613 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
614 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
616 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
617 if (!BTRFS_I(inode
)->block_group
) {
618 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
620 BTRFS_BLOCK_GROUP_METADATA
, 0);
622 btrfs_free_path(path
);
625 mutex_unlock(&root
->fs_info
->fs_mutex
);
627 switch (inode
->i_mode
& S_IFMT
) {
629 inode
->i_mapping
->a_ops
= &btrfs_aops
;
630 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
631 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
632 inode
->i_fop
= &btrfs_file_operations
;
633 inode
->i_op
= &btrfs_file_inode_operations
;
636 inode
->i_fop
= &btrfs_dir_file_operations
;
637 if (root
== root
->fs_info
->tree_root
)
638 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
640 inode
->i_op
= &btrfs_dir_inode_operations
;
643 inode
->i_op
= &btrfs_symlink_inode_operations
;
644 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
645 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
648 init_special_inode(inode
, inode
->i_mode
, rdev
);
654 btrfs_release_path(root
, path
);
655 btrfs_free_path(path
);
656 mutex_unlock(&root
->fs_info
->fs_mutex
);
657 make_bad_inode(inode
);
660 static void fill_inode_item(struct extent_buffer
*leaf
,
661 struct btrfs_inode_item
*item
,
664 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
665 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
666 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
667 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
668 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
670 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
671 inode
->i_atime
.tv_sec
);
672 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
673 inode
->i_atime
.tv_nsec
);
675 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
676 inode
->i_mtime
.tv_sec
);
677 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
678 inode
->i_mtime
.tv_nsec
);
680 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
681 inode
->i_ctime
.tv_sec
);
682 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
683 inode
->i_ctime
.tv_nsec
);
685 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
686 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
687 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
688 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
689 btrfs_set_inode_block_group(leaf
, item
,
690 BTRFS_I(inode
)->block_group
->key
.objectid
);
693 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
694 struct btrfs_root
*root
,
697 struct btrfs_inode_item
*inode_item
;
698 struct btrfs_path
*path
;
699 struct extent_buffer
*leaf
;
702 path
= btrfs_alloc_path();
704 ret
= btrfs_lookup_inode(trans
, root
, path
,
705 &BTRFS_I(inode
)->location
, 1);
712 leaf
= path
->nodes
[0];
713 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
714 struct btrfs_inode_item
);
716 fill_inode_item(leaf
, inode_item
, inode
);
717 btrfs_mark_buffer_dirty(leaf
);
718 btrfs_set_inode_last_trans(trans
, inode
);
721 btrfs_release_path(root
, path
);
722 btrfs_free_path(path
);
727 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
728 struct btrfs_root
*root
,
730 struct dentry
*dentry
)
732 struct btrfs_path
*path
;
733 const char *name
= dentry
->d_name
.name
;
734 int name_len
= dentry
->d_name
.len
;
736 struct extent_buffer
*leaf
;
737 struct btrfs_dir_item
*di
;
738 struct btrfs_key key
;
740 path
= btrfs_alloc_path();
746 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
756 leaf
= path
->nodes
[0];
757 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
758 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
761 btrfs_release_path(root
, path
);
763 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
764 key
.objectid
, name
, name_len
, -1);
773 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
775 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
776 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
777 dentry
->d_inode
->i_ino
,
778 dentry
->d_parent
->d_inode
->i_ino
);
780 printk("failed to delete reference to %.*s, "
781 "inode %lu parent %lu\n", name_len
, name
,
782 dentry
->d_inode
->i_ino
,
783 dentry
->d_parent
->d_inode
->i_ino
);
786 btrfs_free_path(path
);
788 dir
->i_size
-= name_len
* 2;
789 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
790 btrfs_update_inode(trans
, root
, dir
);
791 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
792 dentry
->d_inode
->i_nlink
--;
794 drop_nlink(dentry
->d_inode
);
796 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
797 dir
->i_sb
->s_dirt
= 1;
802 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
804 struct btrfs_root
*root
;
805 struct btrfs_trans_handle
*trans
;
806 struct inode
*inode
= dentry
->d_inode
;
808 unsigned long nr
= 0;
810 root
= BTRFS_I(dir
)->root
;
811 mutex_lock(&root
->fs_info
->fs_mutex
);
813 ret
= btrfs_check_free_space(root
, 1, 1);
817 trans
= btrfs_start_transaction(root
, 1);
819 btrfs_set_trans_block_group(trans
, dir
);
820 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
821 nr
= trans
->blocks_used
;
823 if (inode
->i_nlink
== 0) {
825 /* if the inode isn't linked anywhere,
826 * we don't need to worry about
829 found
= btrfs_del_ordered_inode(inode
);
831 atomic_dec(&inode
->i_count
);
835 btrfs_end_transaction(trans
, root
);
837 mutex_unlock(&root
->fs_info
->fs_mutex
);
838 btrfs_btree_balance_dirty(root
, nr
);
839 btrfs_throttle(root
);
843 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
845 struct inode
*inode
= dentry
->d_inode
;
848 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
849 struct btrfs_trans_handle
*trans
;
850 unsigned long nr
= 0;
852 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
855 mutex_lock(&root
->fs_info
->fs_mutex
);
856 ret
= btrfs_check_free_space(root
, 1, 1);
860 trans
= btrfs_start_transaction(root
, 1);
861 btrfs_set_trans_block_group(trans
, dir
);
863 /* now the directory is empty */
864 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
869 nr
= trans
->blocks_used
;
870 ret
= btrfs_end_transaction(trans
, root
);
872 mutex_unlock(&root
->fs_info
->fs_mutex
);
873 btrfs_btree_balance_dirty(root
, nr
);
874 btrfs_throttle(root
);
882 * this can truncate away extent items, csum items and directory items.
883 * It starts at a high offset and removes keys until it can't find
884 * any higher than i_size.
886 * csum items that cross the new i_size are truncated to the new size
889 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
890 struct btrfs_root
*root
,
895 struct btrfs_path
*path
;
896 struct btrfs_key key
;
897 struct btrfs_key found_key
;
899 struct extent_buffer
*leaf
;
900 struct btrfs_file_extent_item
*fi
;
901 u64 extent_start
= 0;
902 u64 extent_num_bytes
= 0;
908 int pending_del_nr
= 0;
909 int pending_del_slot
= 0;
910 int extent_type
= -1;
912 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
913 path
= btrfs_alloc_path();
917 /* FIXME, add redo link to tree so we don't leak on crash */
918 key
.objectid
= inode
->i_ino
;
919 key
.offset
= (u64
)-1;
922 btrfs_init_path(path
);
924 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
929 BUG_ON(path
->slots
[0] == 0);
935 leaf
= path
->nodes
[0];
936 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
937 found_type
= btrfs_key_type(&found_key
);
939 if (found_key
.objectid
!= inode
->i_ino
)
942 if (found_type
< min_type
)
945 item_end
= found_key
.offset
;
946 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
947 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
948 struct btrfs_file_extent_item
);
949 extent_type
= btrfs_file_extent_type(leaf
, fi
);
950 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
952 btrfs_file_extent_num_bytes(leaf
, fi
);
953 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
954 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
956 item_end
+= btrfs_file_extent_inline_len(leaf
,
961 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
962 ret
= btrfs_csum_truncate(trans
, root
, path
,
966 if (item_end
< inode
->i_size
) {
967 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
968 found_type
= BTRFS_INODE_ITEM_KEY
;
969 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
970 found_type
= BTRFS_CSUM_ITEM_KEY
;
971 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
972 found_type
= BTRFS_XATTR_ITEM_KEY
;
973 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
974 found_type
= BTRFS_INODE_REF_KEY
;
975 } else if (found_type
) {
980 btrfs_set_key_type(&key
, found_type
);
983 if (found_key
.offset
>= inode
->i_size
)
989 /* FIXME, shrink the extent if the ref count is only 1 */
990 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
993 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
995 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
998 btrfs_file_extent_num_bytes(leaf
, fi
);
999 extent_num_bytes
= inode
->i_size
-
1000 found_key
.offset
+ root
->sectorsize
- 1;
1001 extent_num_bytes
= extent_num_bytes
&
1002 ~((u64
)root
->sectorsize
- 1);
1003 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1005 num_dec
= (orig_num_bytes
-
1007 if (extent_start
!= 0)
1008 dec_i_blocks(inode
, num_dec
);
1009 btrfs_mark_buffer_dirty(leaf
);
1012 btrfs_file_extent_disk_num_bytes(leaf
,
1014 /* FIXME blocksize != 4096 */
1015 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1016 if (extent_start
!= 0) {
1018 dec_i_blocks(inode
, num_dec
);
1020 root_gen
= btrfs_header_generation(leaf
);
1021 root_owner
= btrfs_header_owner(leaf
);
1023 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1025 u32 newsize
= inode
->i_size
- found_key
.offset
;
1026 dec_i_blocks(inode
, item_end
+ 1 -
1027 found_key
.offset
- newsize
);
1029 btrfs_file_extent_calc_inline_size(newsize
);
1030 ret
= btrfs_truncate_item(trans
, root
, path
,
1034 dec_i_blocks(inode
, item_end
+ 1 -
1040 if (!pending_del_nr
) {
1041 /* no pending yet, add ourselves */
1042 pending_del_slot
= path
->slots
[0];
1044 } else if (pending_del_nr
&&
1045 path
->slots
[0] + 1 == pending_del_slot
) {
1046 /* hop on the pending chunk */
1048 pending_del_slot
= path
->slots
[0];
1050 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1056 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1059 root_gen
, inode
->i_ino
,
1060 found_key
.offset
, 0);
1064 if (path
->slots
[0] == 0) {
1067 btrfs_release_path(root
, path
);
1072 if (pending_del_nr
&&
1073 path
->slots
[0] + 1 != pending_del_slot
) {
1074 struct btrfs_key debug
;
1076 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1078 ret
= btrfs_del_items(trans
, root
, path
,
1083 btrfs_release_path(root
, path
);
1089 if (pending_del_nr
) {
1090 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1093 btrfs_release_path(root
, path
);
1094 btrfs_free_path(path
);
1095 inode
->i_sb
->s_dirt
= 1;
1099 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
1103 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1104 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1105 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1108 WARN_ON(!PageLocked(page
));
1109 set_page_extent_mapped(page
);
1111 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1112 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1113 page_end
, GFP_NOFS
);
1115 if (zero_start
!= PAGE_CACHE_SIZE
) {
1117 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
1118 flush_dcache_page(page
);
1121 set_page_dirty(page
);
1122 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1128 * taken from block_truncate_page, but does cow as it zeros out
1129 * any bytes left in the last page in the file.
1131 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1133 struct inode
*inode
= mapping
->host
;
1134 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1135 u32 blocksize
= root
->sectorsize
;
1136 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1137 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1142 if ((offset
& (blocksize
- 1)) == 0)
1146 page
= grab_cache_page(mapping
, index
);
1149 if (!PageUptodate(page
)) {
1150 ret
= btrfs_readpage(NULL
, page
);
1152 if (!PageUptodate(page
)) {
1157 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1159 ret
= btrfs_cow_one_page(inode
, page
, offset
);
1162 page_cache_release(page
);
1167 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1169 struct inode
*inode
= dentry
->d_inode
;
1172 err
= inode_change_ok(inode
, attr
);
1176 if (S_ISREG(inode
->i_mode
) &&
1177 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1178 struct btrfs_trans_handle
*trans
;
1179 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1180 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1182 u64 mask
= root
->sectorsize
- 1;
1183 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1184 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1188 if (attr
->ia_size
<= hole_start
)
1191 mutex_lock(&root
->fs_info
->fs_mutex
);
1192 err
= btrfs_check_free_space(root
, 1, 0);
1193 mutex_unlock(&root
->fs_info
->fs_mutex
);
1197 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1199 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1200 hole_size
= block_end
- hole_start
;
1202 mutex_lock(&root
->fs_info
->fs_mutex
);
1203 trans
= btrfs_start_transaction(root
, 1);
1204 btrfs_set_trans_block_group(trans
, inode
);
1205 err
= btrfs_drop_extents(trans
, root
, inode
,
1206 hole_start
, block_end
, hole_start
,
1209 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1210 err
= btrfs_insert_file_extent(trans
, root
,
1214 btrfs_drop_extent_cache(inode
, hole_start
,
1216 btrfs_check_file(root
, inode
);
1218 btrfs_end_transaction(trans
, root
);
1219 mutex_unlock(&root
->fs_info
->fs_mutex
);
1220 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1225 err
= inode_setattr(inode
, attr
);
1230 void btrfs_put_inode(struct inode
*inode
)
1234 if (!BTRFS_I(inode
)->ordered_trans
) {
1238 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_DIRTY
) ||
1239 mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1242 ret
= btrfs_del_ordered_inode(inode
);
1244 atomic_dec(&inode
->i_count
);
1248 void btrfs_delete_inode(struct inode
*inode
)
1250 struct btrfs_trans_handle
*trans
;
1251 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1255 truncate_inode_pages(&inode
->i_data
, 0);
1256 if (is_bad_inode(inode
)) {
1261 mutex_lock(&root
->fs_info
->fs_mutex
);
1262 trans
= btrfs_start_transaction(root
, 1);
1264 btrfs_set_trans_block_group(trans
, inode
);
1265 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1267 goto no_delete_lock
;
1269 nr
= trans
->blocks_used
;
1272 btrfs_end_transaction(trans
, root
);
1273 mutex_unlock(&root
->fs_info
->fs_mutex
);
1274 btrfs_btree_balance_dirty(root
, nr
);
1275 btrfs_throttle(root
);
1279 nr
= trans
->blocks_used
;
1280 btrfs_end_transaction(trans
, root
);
1281 mutex_unlock(&root
->fs_info
->fs_mutex
);
1282 btrfs_btree_balance_dirty(root
, nr
);
1283 btrfs_throttle(root
);
1289 * this returns the key found in the dir entry in the location pointer.
1290 * If no dir entries were found, location->objectid is 0.
1292 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1293 struct btrfs_key
*location
)
1295 const char *name
= dentry
->d_name
.name
;
1296 int namelen
= dentry
->d_name
.len
;
1297 struct btrfs_dir_item
*di
;
1298 struct btrfs_path
*path
;
1299 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1302 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1303 location
->objectid
= dir
->i_ino
;
1304 location
->type
= BTRFS_INODE_ITEM_KEY
;
1305 location
->offset
= 0;
1308 path
= btrfs_alloc_path();
1311 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1312 struct btrfs_key key
;
1313 struct extent_buffer
*leaf
;
1317 key
.objectid
= dir
->i_ino
;
1318 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1320 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1324 leaf
= path
->nodes
[0];
1325 slot
= path
->slots
[0];
1326 nritems
= btrfs_header_nritems(leaf
);
1327 if (slot
>= nritems
)
1330 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1331 if (key
.objectid
!= dir
->i_ino
||
1332 key
.type
!= BTRFS_INODE_REF_KEY
) {
1335 location
->objectid
= key
.offset
;
1336 location
->type
= BTRFS_INODE_ITEM_KEY
;
1337 location
->offset
= 0;
1341 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1345 if (!di
|| IS_ERR(di
)) {
1348 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1350 btrfs_free_path(path
);
1353 location
->objectid
= 0;
1358 * when we hit a tree root in a directory, the btrfs part of the inode
1359 * needs to be changed to reflect the root directory of the tree root. This
1360 * is kind of like crossing a mount point.
1362 static int fixup_tree_root_location(struct btrfs_root
*root
,
1363 struct btrfs_key
*location
,
1364 struct btrfs_root
**sub_root
,
1365 struct dentry
*dentry
)
1367 struct btrfs_path
*path
;
1368 struct btrfs_root_item
*ri
;
1370 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1372 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1375 path
= btrfs_alloc_path();
1377 mutex_lock(&root
->fs_info
->fs_mutex
);
1379 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1380 dentry
->d_name
.name
,
1381 dentry
->d_name
.len
);
1382 if (IS_ERR(*sub_root
))
1383 return PTR_ERR(*sub_root
);
1385 ri
= &(*sub_root
)->root_item
;
1386 location
->objectid
= btrfs_root_dirid(ri
);
1387 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1388 location
->offset
= 0;
1390 btrfs_free_path(path
);
1391 mutex_unlock(&root
->fs_info
->fs_mutex
);
1395 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1397 struct btrfs_iget_args
*args
= p
;
1398 inode
->i_ino
= args
->ino
;
1399 BTRFS_I(inode
)->root
= args
->root
;
1400 BTRFS_I(inode
)->delalloc_bytes
= 0;
1401 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1402 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1403 inode
->i_mapping
, GFP_NOFS
);
1404 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1405 inode
->i_mapping
, GFP_NOFS
);
1409 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1411 struct btrfs_iget_args
*args
= opaque
;
1412 return (args
->ino
== inode
->i_ino
&&
1413 args
->root
== BTRFS_I(inode
)->root
);
1416 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1419 struct btrfs_iget_args args
;
1420 args
.ino
= objectid
;
1421 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1426 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1429 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1430 struct btrfs_root
*root
)
1432 struct inode
*inode
;
1433 struct btrfs_iget_args args
;
1434 args
.ino
= objectid
;
1437 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1438 btrfs_init_locked_inode
,
1443 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1444 struct nameidata
*nd
)
1446 struct inode
* inode
;
1447 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1448 struct btrfs_root
*root
= bi
->root
;
1449 struct btrfs_root
*sub_root
= root
;
1450 struct btrfs_key location
;
1453 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1454 return ERR_PTR(-ENAMETOOLONG
);
1456 mutex_lock(&root
->fs_info
->fs_mutex
);
1457 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1458 mutex_unlock(&root
->fs_info
->fs_mutex
);
1461 return ERR_PTR(ret
);
1464 if (location
.objectid
) {
1465 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1468 return ERR_PTR(ret
);
1470 return ERR_PTR(-ENOENT
);
1471 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1474 return ERR_PTR(-EACCES
);
1475 if (inode
->i_state
& I_NEW
) {
1476 /* the inode and parent dir are two different roots */
1477 if (sub_root
!= root
) {
1479 sub_root
->inode
= inode
;
1481 BTRFS_I(inode
)->root
= sub_root
;
1482 memcpy(&BTRFS_I(inode
)->location
, &location
,
1484 btrfs_read_locked_inode(inode
);
1485 unlock_new_inode(inode
);
1488 return d_splice_alias(inode
, dentry
);
1491 static unsigned char btrfs_filetype_table
[] = {
1492 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1495 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1497 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1498 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1499 struct btrfs_item
*item
;
1500 struct btrfs_dir_item
*di
;
1501 struct btrfs_key key
;
1502 struct btrfs_key found_key
;
1503 struct btrfs_path
*path
;
1506 struct extent_buffer
*leaf
;
1509 unsigned char d_type
;
1514 int key_type
= BTRFS_DIR_INDEX_KEY
;
1519 /* FIXME, use a real flag for deciding about the key type */
1520 if (root
->fs_info
->tree_root
== root
)
1521 key_type
= BTRFS_DIR_ITEM_KEY
;
1523 /* special case for "." */
1524 if (filp
->f_pos
== 0) {
1525 over
= filldir(dirent
, ".", 1,
1533 mutex_lock(&root
->fs_info
->fs_mutex
);
1534 key
.objectid
= inode
->i_ino
;
1535 path
= btrfs_alloc_path();
1538 /* special case for .., just use the back ref */
1539 if (filp
->f_pos
== 1) {
1540 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1542 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1544 leaf
= path
->nodes
[0];
1545 slot
= path
->slots
[0];
1546 nritems
= btrfs_header_nritems(leaf
);
1547 if (slot
>= nritems
) {
1548 btrfs_release_path(root
, path
);
1549 goto read_dir_items
;
1551 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1552 btrfs_release_path(root
, path
);
1553 if (found_key
.objectid
!= key
.objectid
||
1554 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1555 goto read_dir_items
;
1556 over
= filldir(dirent
, "..", 2,
1557 2, found_key
.offset
, DT_DIR
);
1564 btrfs_set_key_type(&key
, key_type
);
1565 key
.offset
= filp
->f_pos
;
1567 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1572 leaf
= path
->nodes
[0];
1573 nritems
= btrfs_header_nritems(leaf
);
1574 slot
= path
->slots
[0];
1575 if (advance
|| slot
>= nritems
) {
1576 if (slot
>= nritems
-1) {
1577 ret
= btrfs_next_leaf(root
, path
);
1580 leaf
= path
->nodes
[0];
1581 nritems
= btrfs_header_nritems(leaf
);
1582 slot
= path
->slots
[0];
1589 item
= btrfs_item_nr(leaf
, slot
);
1590 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1592 if (found_key
.objectid
!= key
.objectid
)
1594 if (btrfs_key_type(&found_key
) != key_type
)
1596 if (found_key
.offset
< filp
->f_pos
)
1599 filp
->f_pos
= found_key
.offset
;
1601 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1603 di_total
= btrfs_item_size(leaf
, item
);
1604 while(di_cur
< di_total
) {
1605 struct btrfs_key location
;
1607 name_len
= btrfs_dir_name_len(leaf
, di
);
1608 if (name_len
< 32) {
1609 name_ptr
= tmp_name
;
1611 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1614 read_extent_buffer(leaf
, name_ptr
,
1615 (unsigned long)(di
+ 1), name_len
);
1617 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1618 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1619 over
= filldir(dirent
, name_ptr
, name_len
,
1624 if (name_ptr
!= tmp_name
)
1629 di_len
= btrfs_dir_name_len(leaf
, di
) +
1630 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1632 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1635 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1636 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1642 btrfs_release_path(root
, path
);
1643 btrfs_free_path(path
);
1644 mutex_unlock(&root
->fs_info
->fs_mutex
);
1648 int btrfs_write_inode(struct inode
*inode
, int wait
)
1650 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1651 struct btrfs_trans_handle
*trans
;
1655 mutex_lock(&root
->fs_info
->fs_mutex
);
1656 trans
= btrfs_start_transaction(root
, 1);
1657 btrfs_set_trans_block_group(trans
, inode
);
1658 ret
= btrfs_commit_transaction(trans
, root
);
1659 mutex_unlock(&root
->fs_info
->fs_mutex
);
1665 * This is somewhat expensive, updating the tree every time the
1666 * inode changes. But, it is most likely to find the inode in cache.
1667 * FIXME, needs more benchmarking...there are no reasons other than performance
1668 * to keep or drop this code.
1670 void btrfs_dirty_inode(struct inode
*inode
)
1672 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1673 struct btrfs_trans_handle
*trans
;
1675 mutex_lock(&root
->fs_info
->fs_mutex
);
1676 trans
= btrfs_start_transaction(root
, 1);
1677 btrfs_set_trans_block_group(trans
, inode
);
1678 btrfs_update_inode(trans
, root
, inode
);
1679 btrfs_end_transaction(trans
, root
);
1680 mutex_unlock(&root
->fs_info
->fs_mutex
);
1683 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1684 struct btrfs_root
*root
,
1685 const char *name
, int name_len
,
1688 struct btrfs_block_group_cache
*group
,
1691 struct inode
*inode
;
1692 struct btrfs_inode_item
*inode_item
;
1693 struct btrfs_block_group_cache
*new_inode_group
;
1694 struct btrfs_key
*location
;
1695 struct btrfs_path
*path
;
1696 struct btrfs_inode_ref
*ref
;
1697 struct btrfs_key key
[2];
1703 path
= btrfs_alloc_path();
1706 inode
= new_inode(root
->fs_info
->sb
);
1708 return ERR_PTR(-ENOMEM
);
1710 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1711 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1712 inode
->i_mapping
, GFP_NOFS
);
1713 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1714 inode
->i_mapping
, GFP_NOFS
);
1715 BTRFS_I(inode
)->delalloc_bytes
= 0;
1716 BTRFS_I(inode
)->root
= root
;
1722 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1723 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1724 if (!new_inode_group
) {
1725 printk("find_block group failed\n");
1726 new_inode_group
= group
;
1728 BTRFS_I(inode
)->block_group
= new_inode_group
;
1729 BTRFS_I(inode
)->flags
= 0;
1731 key
[0].objectid
= objectid
;
1732 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1735 key
[1].objectid
= objectid
;
1736 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1737 key
[1].offset
= ref_objectid
;
1739 sizes
[0] = sizeof(struct btrfs_inode_item
);
1740 sizes
[1] = name_len
+ sizeof(*ref
);
1742 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1746 if (objectid
> root
->highest_inode
)
1747 root
->highest_inode
= objectid
;
1749 inode
->i_uid
= current
->fsuid
;
1750 inode
->i_gid
= current
->fsgid
;
1751 inode
->i_mode
= mode
;
1752 inode
->i_ino
= objectid
;
1753 inode
->i_blocks
= 0;
1754 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1755 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1756 struct btrfs_inode_item
);
1757 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1759 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1760 struct btrfs_inode_ref
);
1761 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1762 ptr
= (unsigned long)(ref
+ 1);
1763 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1765 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1766 btrfs_free_path(path
);
1768 location
= &BTRFS_I(inode
)->location
;
1769 location
->objectid
= objectid
;
1770 location
->offset
= 0;
1771 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1773 insert_inode_hash(inode
);
1776 btrfs_free_path(path
);
1777 return ERR_PTR(ret
);
1780 static inline u8
btrfs_inode_type(struct inode
*inode
)
1782 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1785 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1786 struct dentry
*dentry
, struct inode
*inode
,
1790 struct btrfs_key key
;
1791 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1792 struct inode
*parent_inode
;
1794 key
.objectid
= inode
->i_ino
;
1795 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1798 ret
= btrfs_insert_dir_item(trans
, root
,
1799 dentry
->d_name
.name
, dentry
->d_name
.len
,
1800 dentry
->d_parent
->d_inode
->i_ino
,
1801 &key
, btrfs_inode_type(inode
));
1804 ret
= btrfs_insert_inode_ref(trans
, root
,
1805 dentry
->d_name
.name
,
1808 dentry
->d_parent
->d_inode
->i_ino
);
1810 parent_inode
= dentry
->d_parent
->d_inode
;
1811 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1812 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1813 ret
= btrfs_update_inode(trans
, root
,
1814 dentry
->d_parent
->d_inode
);
1819 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1820 struct dentry
*dentry
, struct inode
*inode
,
1823 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1825 d_instantiate(dentry
, inode
);
1833 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1834 int mode
, dev_t rdev
)
1836 struct btrfs_trans_handle
*trans
;
1837 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1838 struct inode
*inode
= NULL
;
1842 unsigned long nr
= 0;
1844 if (!new_valid_dev(rdev
))
1847 mutex_lock(&root
->fs_info
->fs_mutex
);
1848 err
= btrfs_check_free_space(root
, 1, 0);
1852 trans
= btrfs_start_transaction(root
, 1);
1853 btrfs_set_trans_block_group(trans
, dir
);
1855 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1861 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1863 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1864 BTRFS_I(dir
)->block_group
, mode
);
1865 err
= PTR_ERR(inode
);
1869 btrfs_set_trans_block_group(trans
, inode
);
1870 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1874 inode
->i_op
= &btrfs_special_inode_operations
;
1875 init_special_inode(inode
, inode
->i_mode
, rdev
);
1876 btrfs_update_inode(trans
, root
, inode
);
1878 dir
->i_sb
->s_dirt
= 1;
1879 btrfs_update_inode_block_group(trans
, inode
);
1880 btrfs_update_inode_block_group(trans
, dir
);
1882 nr
= trans
->blocks_used
;
1883 btrfs_end_transaction(trans
, root
);
1885 mutex_unlock(&root
->fs_info
->fs_mutex
);
1888 inode_dec_link_count(inode
);
1891 btrfs_btree_balance_dirty(root
, nr
);
1892 btrfs_throttle(root
);
1896 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1897 int mode
, struct nameidata
*nd
)
1899 struct btrfs_trans_handle
*trans
;
1900 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1901 struct inode
*inode
= NULL
;
1904 unsigned long nr
= 0;
1907 mutex_lock(&root
->fs_info
->fs_mutex
);
1908 err
= btrfs_check_free_space(root
, 1, 0);
1911 trans
= btrfs_start_transaction(root
, 1);
1912 btrfs_set_trans_block_group(trans
, dir
);
1914 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1920 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1922 dentry
->d_parent
->d_inode
->i_ino
,
1923 objectid
, BTRFS_I(dir
)->block_group
, mode
);
1924 err
= PTR_ERR(inode
);
1928 btrfs_set_trans_block_group(trans
, inode
);
1929 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1933 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1934 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1935 inode
->i_fop
= &btrfs_file_operations
;
1936 inode
->i_op
= &btrfs_file_inode_operations
;
1937 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1938 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1939 inode
->i_mapping
, GFP_NOFS
);
1940 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1941 inode
->i_mapping
, GFP_NOFS
);
1942 BTRFS_I(inode
)->delalloc_bytes
= 0;
1943 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1945 dir
->i_sb
->s_dirt
= 1;
1946 btrfs_update_inode_block_group(trans
, inode
);
1947 btrfs_update_inode_block_group(trans
, dir
);
1949 nr
= trans
->blocks_used
;
1950 btrfs_end_transaction(trans
, root
);
1952 mutex_unlock(&root
->fs_info
->fs_mutex
);
1955 inode_dec_link_count(inode
);
1958 btrfs_btree_balance_dirty(root
, nr
);
1959 btrfs_throttle(root
);
1963 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1964 struct dentry
*dentry
)
1966 struct btrfs_trans_handle
*trans
;
1967 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1968 struct inode
*inode
= old_dentry
->d_inode
;
1969 unsigned long nr
= 0;
1973 if (inode
->i_nlink
== 0)
1976 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1981 mutex_lock(&root
->fs_info
->fs_mutex
);
1982 err
= btrfs_check_free_space(root
, 1, 0);
1985 trans
= btrfs_start_transaction(root
, 1);
1987 btrfs_set_trans_block_group(trans
, dir
);
1988 atomic_inc(&inode
->i_count
);
1989 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
1994 dir
->i_sb
->s_dirt
= 1;
1995 btrfs_update_inode_block_group(trans
, dir
);
1996 err
= btrfs_update_inode(trans
, root
, inode
);
2001 nr
= trans
->blocks_used
;
2002 btrfs_end_transaction(trans
, root
);
2004 mutex_unlock(&root
->fs_info
->fs_mutex
);
2007 inode_dec_link_count(inode
);
2010 btrfs_btree_balance_dirty(root
, nr
);
2011 btrfs_throttle(root
);
2015 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2017 struct inode
*inode
;
2018 struct btrfs_trans_handle
*trans
;
2019 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2021 int drop_on_err
= 0;
2023 unsigned long nr
= 1;
2025 mutex_lock(&root
->fs_info
->fs_mutex
);
2026 err
= btrfs_check_free_space(root
, 1, 0);
2030 trans
= btrfs_start_transaction(root
, 1);
2031 btrfs_set_trans_block_group(trans
, dir
);
2033 if (IS_ERR(trans
)) {
2034 err
= PTR_ERR(trans
);
2038 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2044 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2046 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2047 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2048 if (IS_ERR(inode
)) {
2049 err
= PTR_ERR(inode
);
2054 inode
->i_op
= &btrfs_dir_inode_operations
;
2055 inode
->i_fop
= &btrfs_dir_file_operations
;
2056 btrfs_set_trans_block_group(trans
, inode
);
2059 err
= btrfs_update_inode(trans
, root
, inode
);
2063 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2067 d_instantiate(dentry
, inode
);
2069 dir
->i_sb
->s_dirt
= 1;
2070 btrfs_update_inode_block_group(trans
, inode
);
2071 btrfs_update_inode_block_group(trans
, dir
);
2074 nr
= trans
->blocks_used
;
2075 btrfs_end_transaction(trans
, root
);
2078 mutex_unlock(&root
->fs_info
->fs_mutex
);
2081 btrfs_btree_balance_dirty(root
, nr
);
2082 btrfs_throttle(root
);
2086 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2087 size_t pg_offset
, u64 start
, u64 len
,
2093 u64 extent_start
= 0;
2095 u64 objectid
= inode
->i_ino
;
2097 struct btrfs_path
*path
;
2098 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2099 struct btrfs_file_extent_item
*item
;
2100 struct extent_buffer
*leaf
;
2101 struct btrfs_key found_key
;
2102 struct extent_map
*em
= NULL
;
2103 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2104 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2105 struct btrfs_trans_handle
*trans
= NULL
;
2107 path
= btrfs_alloc_path();
2109 mutex_lock(&root
->fs_info
->fs_mutex
);
2112 spin_lock(&em_tree
->lock
);
2113 em
= lookup_extent_mapping(em_tree
, start
, len
);
2114 spin_unlock(&em_tree
->lock
);
2117 if (em
->start
> start
) {
2118 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2119 start
, len
, em
->start
, em
->len
);
2122 if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2123 free_extent_map(em
);
2127 em
= alloc_extent_map(GFP_NOFS
);
2133 em
->start
= EXTENT_MAP_HOLE
;
2135 em
->bdev
= inode
->i_sb
->s_bdev
;
2136 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2137 objectid
, start
, trans
!= NULL
);
2144 if (path
->slots
[0] == 0)
2149 leaf
= path
->nodes
[0];
2150 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2151 struct btrfs_file_extent_item
);
2152 /* are we inside the extent that was found? */
2153 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2154 found_type
= btrfs_key_type(&found_key
);
2155 if (found_key
.objectid
!= objectid
||
2156 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2160 found_type
= btrfs_file_extent_type(leaf
, item
);
2161 extent_start
= found_key
.offset
;
2162 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2163 extent_end
= extent_start
+
2164 btrfs_file_extent_num_bytes(leaf
, item
);
2166 if (start
< extent_start
|| start
>= extent_end
) {
2168 if (start
< extent_start
) {
2169 if (start
+ len
<= extent_start
)
2171 em
->len
= extent_end
- extent_start
;
2177 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2179 em
->start
= extent_start
;
2180 em
->len
= extent_end
- extent_start
;
2181 em
->block_start
= EXTENT_MAP_HOLE
;
2184 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2185 em
->block_start
= bytenr
;
2186 em
->start
= extent_start
;
2187 em
->len
= extent_end
- extent_start
;
2189 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2194 size_t extent_offset
;
2197 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2199 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2200 ~((u64
)root
->sectorsize
- 1);
2201 if (start
< extent_start
|| start
>= extent_end
) {
2203 if (start
< extent_start
) {
2204 if (start
+ len
<= extent_start
)
2206 em
->len
= extent_end
- extent_start
;
2212 em
->block_start
= EXTENT_MAP_INLINE
;
2215 em
->start
= extent_start
;
2220 page_start
= page_offset(page
) + pg_offset
;
2221 extent_offset
= page_start
- extent_start
;
2222 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2223 size
- extent_offset
);
2224 em
->start
= extent_start
+ extent_offset
;
2225 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2226 ~((u64
)root
->sectorsize
- 1);
2228 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2229 if (create
== 0 && !PageUptodate(page
)) {
2230 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2232 flush_dcache_page(page
);
2233 } else if (create
&& PageUptodate(page
)) {
2236 free_extent_map(em
);
2238 btrfs_release_path(root
, path
);
2239 trans
= btrfs_start_transaction(root
, 1);
2242 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2244 btrfs_mark_buffer_dirty(leaf
);
2247 set_extent_uptodate(io_tree
, em
->start
,
2248 extent_map_end(em
) - 1, GFP_NOFS
);
2251 printk("unkknown found_type %d\n", found_type
);
2258 em
->block_start
= EXTENT_MAP_HOLE
;
2260 btrfs_release_path(root
, path
);
2261 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2262 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2268 spin_lock(&em_tree
->lock
);
2269 ret
= add_extent_mapping(em_tree
, em
);
2270 if (ret
== -EEXIST
) {
2271 free_extent_map(em
);
2272 em
= lookup_extent_mapping(em_tree
, start
, len
);
2275 printk("failing to insert %Lu %Lu\n", start
, len
);
2278 spin_unlock(&em_tree
->lock
);
2280 btrfs_free_path(path
);
2282 ret
= btrfs_end_transaction(trans
, root
);
2286 mutex_unlock(&root
->fs_info
->fs_mutex
);
2288 free_extent_map(em
);
2290 return ERR_PTR(err
);
2295 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2296 struct buffer_head
*bh_result
, int create
)
2298 struct extent_map
*em
;
2299 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2300 struct btrfs_multi_bio
*multi
= NULL
;
2301 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2307 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2309 if (!em
|| IS_ERR(em
))
2312 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2315 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2320 if (em
->block_start
== EXTENT_MAP_HOLE
||
2321 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2325 len
= em
->start
+ em
->len
- start
;
2326 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2328 logical
= start
- em
->start
;
2329 logical
= em
->block_start
+ logical
;
2332 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2333 logical
, &map_length
, &multi
, 0);
2335 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2336 bh_result
->b_size
= min(map_length
, len
);
2337 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2338 set_buffer_mapped(bh_result
);
2341 free_extent_map(em
);
2345 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2346 const struct iovec
*iov
, loff_t offset
,
2347 unsigned long nr_segs
)
2349 struct file
*file
= iocb
->ki_filp
;
2350 struct inode
*inode
= file
->f_mapping
->host
;
2355 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2356 offset
, nr_segs
, btrfs_get_block
, NULL
);
2359 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2361 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2364 int btrfs_readpage(struct file
*file
, struct page
*page
)
2366 struct extent_io_tree
*tree
;
2367 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2368 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2371 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2373 struct extent_io_tree
*tree
;
2376 if (current
->flags
& PF_MEMALLOC
) {
2377 redirty_page_for_writepage(wbc
, page
);
2381 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2382 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2385 static int btrfs_writepages(struct address_space
*mapping
,
2386 struct writeback_control
*wbc
)
2388 struct extent_io_tree
*tree
;
2389 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2390 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2394 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2395 struct list_head
*pages
, unsigned nr_pages
)
2397 struct extent_io_tree
*tree
;
2398 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2399 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2403 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2405 struct extent_io_tree
*tree
;
2406 struct extent_map_tree
*map
;
2409 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2410 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2411 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2413 ClearPagePrivate(page
);
2414 set_page_private(page
, 0);
2415 page_cache_release(page
);
2420 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2422 struct extent_io_tree
*tree
;
2424 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2425 extent_invalidatepage(tree
, page
, offset
);
2426 btrfs_releasepage(page
, GFP_NOFS
);
2430 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2431 * called from a page fault handler when a page is first dirtied. Hence we must
2432 * be careful to check for EOF conditions here. We set the page up correctly
2433 * for a written page which means we get ENOSPC checking when writing into
2434 * holes and correct delalloc and unwritten extent mapping on filesystems that
2435 * support these features.
2437 * We are not allowed to take the i_mutex here so we have to play games to
2438 * protect against truncate races as the page could now be beyond EOF. Because
2439 * vmtruncate() writes the inode size before removing pages, once we have the
2440 * page lock we can determine safely if the page is beyond EOF. If it is not
2441 * beyond EOF, then the page is guaranteed safe against truncation until we
2444 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2446 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2447 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2453 mutex_lock(&root
->fs_info
->fs_mutex
);
2454 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2455 mutex_unlock(&root
->fs_info
->fs_mutex
);
2462 wait_on_page_writeback(page
);
2463 size
= i_size_read(inode
);
2464 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2466 if ((page
->mapping
!= inode
->i_mapping
) ||
2467 (page_start
> size
)) {
2468 /* page got truncated out from underneath us */
2472 /* page is wholly or partially inside EOF */
2473 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2474 end
= size
& ~PAGE_CACHE_MASK
;
2476 end
= PAGE_CACHE_SIZE
;
2478 ret
= btrfs_cow_one_page(inode
, page
, end
);
2486 static void btrfs_truncate(struct inode
*inode
)
2488 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2490 struct btrfs_trans_handle
*trans
;
2493 if (!S_ISREG(inode
->i_mode
))
2495 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2498 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2500 mutex_lock(&root
->fs_info
->fs_mutex
);
2501 trans
= btrfs_start_transaction(root
, 1);
2502 btrfs_set_trans_block_group(trans
, inode
);
2504 /* FIXME, add redo link to tree so we don't leak on crash */
2505 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2506 BTRFS_EXTENT_DATA_KEY
);
2507 btrfs_update_inode(trans
, root
, inode
);
2508 nr
= trans
->blocks_used
;
2510 ret
= btrfs_end_transaction(trans
, root
);
2512 mutex_unlock(&root
->fs_info
->fs_mutex
);
2513 btrfs_btree_balance_dirty(root
, nr
);
2514 btrfs_throttle(root
);
2517 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2520 struct btrfs_trans_handle
*trans
;
2521 struct btrfs_key key
;
2522 struct btrfs_root_item root_item
;
2523 struct btrfs_inode_item
*inode_item
;
2524 struct extent_buffer
*leaf
;
2525 struct btrfs_root
*new_root
= root
;
2526 struct inode
*inode
;
2531 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2532 unsigned long nr
= 1;
2534 mutex_lock(&root
->fs_info
->fs_mutex
);
2535 ret
= btrfs_check_free_space(root
, 1, 0);
2539 trans
= btrfs_start_transaction(root
, 1);
2542 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2547 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2548 objectid
, trans
->transid
, 0, 0,
2551 return PTR_ERR(leaf
);
2553 btrfs_set_header_nritems(leaf
, 0);
2554 btrfs_set_header_level(leaf
, 0);
2555 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2556 btrfs_set_header_generation(leaf
, trans
->transid
);
2557 btrfs_set_header_owner(leaf
, objectid
);
2559 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2560 (unsigned long)btrfs_header_fsid(leaf
),
2562 btrfs_mark_buffer_dirty(leaf
);
2564 inode_item
= &root_item
.inode
;
2565 memset(inode_item
, 0, sizeof(*inode_item
));
2566 inode_item
->generation
= cpu_to_le64(1);
2567 inode_item
->size
= cpu_to_le64(3);
2568 inode_item
->nlink
= cpu_to_le32(1);
2569 inode_item
->nblocks
= cpu_to_le64(1);
2570 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2572 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2573 btrfs_set_root_level(&root_item
, 0);
2574 btrfs_set_root_refs(&root_item
, 1);
2575 btrfs_set_root_used(&root_item
, 0);
2577 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2578 root_item
.drop_level
= 0;
2580 free_extent_buffer(leaf
);
2583 btrfs_set_root_dirid(&root_item
, new_dirid
);
2585 key
.objectid
= objectid
;
2587 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2588 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2594 * insert the directory item
2596 key
.offset
= (u64
)-1;
2597 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2598 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2599 name
, namelen
, dir
->i_ino
, &key
,
2604 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2605 name
, namelen
, objectid
,
2606 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2610 ret
= btrfs_commit_transaction(trans
, root
);
2614 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2617 trans
= btrfs_start_transaction(new_root
, 1);
2620 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2622 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2625 inode
->i_op
= &btrfs_dir_inode_operations
;
2626 inode
->i_fop
= &btrfs_dir_file_operations
;
2627 new_root
->inode
= inode
;
2629 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2633 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2637 nr
= trans
->blocks_used
;
2638 err
= btrfs_commit_transaction(trans
, new_root
);
2642 mutex_unlock(&root
->fs_info
->fs_mutex
);
2643 btrfs_btree_balance_dirty(root
, nr
);
2644 btrfs_throttle(root
);
2648 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2650 struct btrfs_pending_snapshot
*pending_snapshot
;
2651 struct btrfs_trans_handle
*trans
;
2654 unsigned long nr
= 0;
2656 if (!root
->ref_cows
)
2659 mutex_lock(&root
->fs_info
->fs_mutex
);
2660 ret
= btrfs_check_free_space(root
, 1, 0);
2664 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2665 if (!pending_snapshot
) {
2669 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
2670 if (!pending_snapshot
->name
) {
2672 kfree(pending_snapshot
);
2675 memcpy(pending_snapshot
->name
, name
, namelen
);
2676 pending_snapshot
->name
[namelen
] = '\0';
2677 trans
= btrfs_start_transaction(root
, 1);
2679 pending_snapshot
->root
= root
;
2680 list_add(&pending_snapshot
->list
,
2681 &trans
->transaction
->pending_snapshots
);
2682 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2683 err
= btrfs_commit_transaction(trans
, root
);
2686 mutex_unlock(&root
->fs_info
->fs_mutex
);
2687 btrfs_btree_balance_dirty(root
, nr
);
2688 btrfs_throttle(root
);
2692 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2693 struct file_ra_state
*ra
, struct file
*file
,
2694 pgoff_t offset
, pgoff_t last_index
)
2698 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2699 req_size
= last_index
- offset
+ 1;
2700 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2703 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2704 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2705 return offset
+ req_size
;
2709 int btrfs_defrag_file(struct file
*file
) {
2710 struct inode
*inode
= fdentry(file
)->d_inode
;
2711 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2712 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2714 unsigned long last_index
;
2715 unsigned long ra_index
= 0;
2721 mutex_lock(&root
->fs_info
->fs_mutex
);
2722 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2723 mutex_unlock(&root
->fs_info
->fs_mutex
);
2727 mutex_lock(&inode
->i_mutex
);
2728 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2729 for (i
= 0; i
<= last_index
; i
++) {
2730 if (i
== ra_index
) {
2731 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2733 file
, ra_index
, last_index
);
2735 page
= grab_cache_page(inode
->i_mapping
, i
);
2738 if (!PageUptodate(page
)) {
2739 btrfs_readpage(NULL
, page
);
2741 if (!PageUptodate(page
)) {
2743 page_cache_release(page
);
2747 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2748 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2750 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2751 set_extent_delalloc(io_tree
, page_start
,
2752 page_end
, GFP_NOFS
);
2754 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2755 set_page_dirty(page
);
2757 page_cache_release(page
);
2758 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2762 mutex_unlock(&inode
->i_mutex
);
2766 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2770 struct btrfs_ioctl_vol_args
*vol_args
;
2771 struct btrfs_trans_handle
*trans
;
2777 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2782 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2786 namelen
= strlen(vol_args
->name
);
2787 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2792 sizestr
= vol_args
->name
;
2793 if (!strcmp(sizestr
, "max"))
2794 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2796 if (sizestr
[0] == '-') {
2799 } else if (sizestr
[0] == '+') {
2803 new_size
= btrfs_parse_size(sizestr
);
2804 if (new_size
== 0) {
2810 mutex_lock(&root
->fs_info
->fs_mutex
);
2811 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2814 if (new_size
> old_size
) {
2818 new_size
= old_size
- new_size
;
2819 } else if (mod
> 0) {
2820 new_size
= old_size
+ new_size
;
2823 if (new_size
< 256 * 1024 * 1024) {
2827 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2832 do_div(new_size
, root
->sectorsize
);
2833 new_size
*= root
->sectorsize
;
2835 printk("new size is %Lu\n", new_size
);
2836 if (new_size
> old_size
) {
2837 trans
= btrfs_start_transaction(root
, 1);
2838 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2839 btrfs_commit_transaction(trans
, root
);
2841 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2845 mutex_unlock(&root
->fs_info
->fs_mutex
);
2851 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
2854 struct btrfs_ioctl_vol_args
*vol_args
;
2855 struct btrfs_dir_item
*di
;
2856 struct btrfs_path
*path
;
2861 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2866 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2871 namelen
= strlen(vol_args
->name
);
2872 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2876 if (strchr(vol_args
->name
, '/')) {
2881 path
= btrfs_alloc_path();
2887 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2888 mutex_lock(&root
->fs_info
->fs_mutex
);
2889 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2891 vol_args
->name
, namelen
, 0);
2892 mutex_unlock(&root
->fs_info
->fs_mutex
);
2893 btrfs_free_path(path
);
2895 if (di
&& !IS_ERR(di
)) {
2905 if (root
== root
->fs_info
->tree_root
)
2906 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2908 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2914 static int btrfs_ioctl_defrag(struct file
*file
)
2916 struct inode
*inode
= fdentry(file
)->d_inode
;
2917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2919 switch (inode
->i_mode
& S_IFMT
) {
2921 mutex_lock(&root
->fs_info
->fs_mutex
);
2922 btrfs_defrag_root(root
, 0);
2923 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2924 mutex_unlock(&root
->fs_info
->fs_mutex
);
2927 btrfs_defrag_file(file
);
2934 long btrfs_ioctl(struct file
*file
, unsigned int
2935 cmd
, unsigned long arg
)
2937 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2940 case BTRFS_IOC_SNAP_CREATE
:
2941 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2942 case BTRFS_IOC_DEFRAG
:
2943 return btrfs_ioctl_defrag(file
);
2944 case BTRFS_IOC_RESIZE
:
2945 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2952 * Called inside transaction, so use GFP_NOFS
2954 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2956 struct btrfs_inode
*ei
;
2958 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2962 ei
->ordered_trans
= 0;
2963 return &ei
->vfs_inode
;
2966 void btrfs_destroy_inode(struct inode
*inode
)
2968 WARN_ON(!list_empty(&inode
->i_dentry
));
2969 WARN_ON(inode
->i_data
.nrpages
);
2971 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2972 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2975 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2976 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2978 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2979 unsigned long flags
)
2982 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2984 inode_init_once(&ei
->vfs_inode
);
2987 void btrfs_destroy_cachep(void)
2989 if (btrfs_inode_cachep
)
2990 kmem_cache_destroy(btrfs_inode_cachep
);
2991 if (btrfs_trans_handle_cachep
)
2992 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2993 if (btrfs_transaction_cachep
)
2994 kmem_cache_destroy(btrfs_transaction_cachep
);
2995 if (btrfs_bit_radix_cachep
)
2996 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2997 if (btrfs_path_cachep
)
2998 kmem_cache_destroy(btrfs_path_cachep
);
3001 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3002 unsigned long extra_flags
,
3003 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3004 void (*ctor
)(struct kmem_cache
*, void *)
3006 void (*ctor
)(void *, struct kmem_cache
*,
3011 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3012 SLAB_MEM_SPREAD
| extra_flags
), ctor
3013 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3019 int btrfs_init_cachep(void)
3021 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3022 sizeof(struct btrfs_inode
),
3024 if (!btrfs_inode_cachep
)
3026 btrfs_trans_handle_cachep
=
3027 btrfs_cache_create("btrfs_trans_handle_cache",
3028 sizeof(struct btrfs_trans_handle
),
3030 if (!btrfs_trans_handle_cachep
)
3032 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3033 sizeof(struct btrfs_transaction
),
3035 if (!btrfs_transaction_cachep
)
3037 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3038 sizeof(struct btrfs_path
),
3040 if (!btrfs_path_cachep
)
3042 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3043 SLAB_DESTROY_BY_RCU
, NULL
);
3044 if (!btrfs_bit_radix_cachep
)
3048 btrfs_destroy_cachep();
3052 static int btrfs_getattr(struct vfsmount
*mnt
,
3053 struct dentry
*dentry
, struct kstat
*stat
)
3055 struct inode
*inode
= dentry
->d_inode
;
3056 generic_fillattr(inode
, stat
);
3057 stat
->blksize
= PAGE_CACHE_SIZE
;
3058 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3062 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3063 struct inode
* new_dir
,struct dentry
*new_dentry
)
3065 struct btrfs_trans_handle
*trans
;
3066 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3067 struct inode
*new_inode
= new_dentry
->d_inode
;
3068 struct inode
*old_inode
= old_dentry
->d_inode
;
3069 struct timespec ctime
= CURRENT_TIME
;
3070 struct btrfs_path
*path
;
3073 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3074 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3078 mutex_lock(&root
->fs_info
->fs_mutex
);
3079 ret
= btrfs_check_free_space(root
, 1, 0);
3083 trans
= btrfs_start_transaction(root
, 1);
3085 btrfs_set_trans_block_group(trans
, new_dir
);
3086 path
= btrfs_alloc_path();
3092 old_dentry
->d_inode
->i_nlink
++;
3093 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3094 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3095 old_inode
->i_ctime
= ctime
;
3097 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3102 new_inode
->i_ctime
= CURRENT_TIME
;
3103 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3107 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
3112 btrfs_free_path(path
);
3113 btrfs_end_transaction(trans
, root
);
3115 mutex_unlock(&root
->fs_info
->fs_mutex
);
3119 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3120 const char *symname
)
3122 struct btrfs_trans_handle
*trans
;
3123 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3124 struct btrfs_path
*path
;
3125 struct btrfs_key key
;
3126 struct inode
*inode
= NULL
;
3133 struct btrfs_file_extent_item
*ei
;
3134 struct extent_buffer
*leaf
;
3135 unsigned long nr
= 0;
3137 name_len
= strlen(symname
) + 1;
3138 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3139 return -ENAMETOOLONG
;
3141 mutex_lock(&root
->fs_info
->fs_mutex
);
3142 err
= btrfs_check_free_space(root
, 1, 0);
3146 trans
= btrfs_start_transaction(root
, 1);
3147 btrfs_set_trans_block_group(trans
, dir
);
3149 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3155 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
3157 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3158 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
3159 err
= PTR_ERR(inode
);
3163 btrfs_set_trans_block_group(trans
, inode
);
3164 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
3168 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3169 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3170 inode
->i_fop
= &btrfs_file_operations
;
3171 inode
->i_op
= &btrfs_file_inode_operations
;
3172 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3173 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3174 inode
->i_mapping
, GFP_NOFS
);
3175 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3176 inode
->i_mapping
, GFP_NOFS
);
3177 BTRFS_I(inode
)->delalloc_bytes
= 0;
3178 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3180 dir
->i_sb
->s_dirt
= 1;
3181 btrfs_update_inode_block_group(trans
, inode
);
3182 btrfs_update_inode_block_group(trans
, dir
);
3186 path
= btrfs_alloc_path();
3188 key
.objectid
= inode
->i_ino
;
3190 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3191 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3192 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3198 leaf
= path
->nodes
[0];
3199 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3200 struct btrfs_file_extent_item
);
3201 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3202 btrfs_set_file_extent_type(leaf
, ei
,
3203 BTRFS_FILE_EXTENT_INLINE
);
3204 ptr
= btrfs_file_extent_inline_start(ei
);
3205 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3206 btrfs_mark_buffer_dirty(leaf
);
3207 btrfs_free_path(path
);
3209 inode
->i_op
= &btrfs_symlink_inode_operations
;
3210 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3211 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3212 inode
->i_size
= name_len
- 1;
3213 err
= btrfs_update_inode(trans
, root
, inode
);
3218 nr
= trans
->blocks_used
;
3219 btrfs_end_transaction(trans
, root
);
3221 mutex_unlock(&root
->fs_info
->fs_mutex
);
3223 inode_dec_link_count(inode
);
3226 btrfs_btree_balance_dirty(root
, nr
);
3227 btrfs_throttle(root
);
3231 static int btrfs_permission(struct inode
*inode
, int mask
,
3232 struct nameidata
*nd
)
3234 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3236 return generic_permission(inode
, mask
, NULL
);
3239 static struct inode_operations btrfs_dir_inode_operations
= {
3240 .lookup
= btrfs_lookup
,
3241 .create
= btrfs_create
,
3242 .unlink
= btrfs_unlink
,
3244 .mkdir
= btrfs_mkdir
,
3245 .rmdir
= btrfs_rmdir
,
3246 .rename
= btrfs_rename
,
3247 .symlink
= btrfs_symlink
,
3248 .setattr
= btrfs_setattr
,
3249 .mknod
= btrfs_mknod
,
3250 .setxattr
= generic_setxattr
,
3251 .getxattr
= generic_getxattr
,
3252 .listxattr
= btrfs_listxattr
,
3253 .removexattr
= generic_removexattr
,
3254 .permission
= btrfs_permission
,
3256 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3257 .lookup
= btrfs_lookup
,
3258 .permission
= btrfs_permission
,
3260 static struct file_operations btrfs_dir_file_operations
= {
3261 .llseek
= generic_file_llseek
,
3262 .read
= generic_read_dir
,
3263 .readdir
= btrfs_readdir
,
3264 .unlocked_ioctl
= btrfs_ioctl
,
3265 #ifdef CONFIG_COMPAT
3266 .compat_ioctl
= btrfs_ioctl
,
3270 static struct extent_io_ops btrfs_extent_io_ops
= {
3271 .fill_delalloc
= run_delalloc_range
,
3272 .submit_bio_hook
= btrfs_submit_bio_hook
,
3273 .merge_bio_hook
= btrfs_merge_bio_hook
,
3274 .readpage_io_hook
= btrfs_readpage_io_hook
,
3275 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3276 .readpage_io_failed_hook
= btrfs_readpage_io_failed_hook
,
3277 .set_bit_hook
= btrfs_set_bit_hook
,
3278 .clear_bit_hook
= btrfs_clear_bit_hook
,
3281 static struct address_space_operations btrfs_aops
= {
3282 .readpage
= btrfs_readpage
,
3283 .writepage
= btrfs_writepage
,
3284 .writepages
= btrfs_writepages
,
3285 .readpages
= btrfs_readpages
,
3286 .sync_page
= block_sync_page
,
3288 .direct_IO
= btrfs_direct_IO
,
3289 .invalidatepage
= btrfs_invalidatepage
,
3290 .releasepage
= btrfs_releasepage
,
3291 .set_page_dirty
= __set_page_dirty_nobuffers
,
3294 static struct address_space_operations btrfs_symlink_aops
= {
3295 .readpage
= btrfs_readpage
,
3296 .writepage
= btrfs_writepage
,
3297 .invalidatepage
= btrfs_invalidatepage
,
3298 .releasepage
= btrfs_releasepage
,
3301 static struct inode_operations btrfs_file_inode_operations
= {
3302 .truncate
= btrfs_truncate
,
3303 .getattr
= btrfs_getattr
,
3304 .setattr
= btrfs_setattr
,
3305 .setxattr
= generic_setxattr
,
3306 .getxattr
= generic_getxattr
,
3307 .listxattr
= btrfs_listxattr
,
3308 .removexattr
= generic_removexattr
,
3309 .permission
= btrfs_permission
,
3311 static struct inode_operations btrfs_special_inode_operations
= {
3312 .getattr
= btrfs_getattr
,
3313 .setattr
= btrfs_setattr
,
3314 .permission
= btrfs_permission
,
3316 static struct inode_operations btrfs_symlink_inode_operations
= {
3317 .readlink
= generic_readlink
,
3318 .follow_link
= page_follow_link_light
,
3319 .put_link
= page_put_link
,
3320 .permission
= btrfs_permission
,