2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
46 #include "ordered-data.h"
48 struct btrfs_iget_args
{
50 struct btrfs_root
*root
;
53 static struct inode_operations btrfs_dir_inode_operations
;
54 static struct inode_operations btrfs_symlink_inode_operations
;
55 static struct inode_operations btrfs_dir_ro_inode_operations
;
56 static struct inode_operations btrfs_special_inode_operations
;
57 static struct inode_operations btrfs_file_inode_operations
;
58 static struct address_space_operations btrfs_aops
;
59 static struct address_space_operations btrfs_symlink_aops
;
60 static struct file_operations btrfs_dir_file_operations
;
61 static struct extent_io_ops btrfs_extent_io_ops
;
63 static struct kmem_cache
*btrfs_inode_cachep
;
64 struct kmem_cache
*btrfs_trans_handle_cachep
;
65 struct kmem_cache
*btrfs_transaction_cachep
;
66 struct kmem_cache
*btrfs_bit_radix_cachep
;
67 struct kmem_cache
*btrfs_path_cachep
;
70 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
71 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
72 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
73 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
74 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
75 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
76 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
77 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
80 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
89 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
90 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
91 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
99 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
101 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
105 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
107 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
108 struct btrfs_trans_handle
*trans
;
112 u64 blocksize
= root
->sectorsize
;
114 struct btrfs_key ins
;
115 struct extent_map
*em
;
116 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
119 trans
= btrfs_start_transaction(root
, 1);
121 btrfs_set_trans_block_group(trans
, inode
);
123 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
124 num_bytes
= max(blocksize
, num_bytes
);
125 orig_num_bytes
= num_bytes
;
127 if (alloc_hint
== EXTENT_MAP_INLINE
)
130 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
131 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1);
133 while(num_bytes
> 0) {
134 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
135 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
136 root
->sectorsize
, 0, 0,
142 em
= alloc_extent_map(GFP_NOFS
);
144 em
->len
= ins
.offset
;
145 em
->block_start
= ins
.objectid
;
146 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
148 spin_lock(&em_tree
->lock
);
149 ret
= add_extent_mapping(em_tree
, em
);
150 spin_unlock(&em_tree
->lock
);
151 if (ret
!= -EEXIST
) {
155 btrfs_drop_extent_cache(inode
, start
,
156 start
+ ins
.offset
- 1);
159 cur_alloc_size
= ins
.offset
;
160 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
163 if (num_bytes
< cur_alloc_size
) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
168 num_bytes
-= cur_alloc_size
;
169 alloc_hint
= ins
.objectid
+ ins
.offset
;
170 start
+= cur_alloc_size
;
173 btrfs_end_transaction(trans
, root
);
177 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
185 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
186 struct btrfs_block_group_cache
*block_group
;
187 struct extent_buffer
*leaf
;
189 struct btrfs_path
*path
;
190 struct btrfs_file_extent_item
*item
;
193 struct btrfs_key found_key
;
195 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
196 path
= btrfs_alloc_path();
199 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
200 inode
->i_ino
, start
, 0);
202 btrfs_free_path(path
);
208 if (path
->slots
[0] == 0)
213 leaf
= path
->nodes
[0];
214 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
215 struct btrfs_file_extent_item
);
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
219 found_type
= btrfs_key_type(&found_key
);
220 if (found_key
.objectid
!= inode
->i_ino
||
221 found_type
!= BTRFS_EXTENT_DATA_KEY
)
224 found_type
= btrfs_file_extent_type(leaf
, item
);
225 extent_start
= found_key
.offset
;
226 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
227 u64 extent_num_bytes
;
229 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
230 extent_end
= extent_start
+ extent_num_bytes
;
233 if (loops
&& start
!= extent_start
)
236 if (start
< extent_start
|| start
>= extent_end
)
239 cow_end
= min(end
, extent_end
- 1);
240 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
244 if (btrfs_count_snapshots_in_path(root
, path
, inode
->i_ino
,
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
253 block_group
= btrfs_lookup_block_group(root
->fs_info
,
255 if (!block_group
|| block_group
->ro
)
264 btrfs_free_path(path
);
267 btrfs_release_path(root
, path
);
272 cow_file_range(inode
, start
, end
);
277 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
279 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
282 if (btrfs_test_opt(root
, NODATACOW
) ||
283 btrfs_test_flag(inode
, NODATACOW
))
284 ret
= run_delalloc_nocow(inode
, start
, end
);
286 ret
= cow_file_range(inode
, start
, end
);
291 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
292 unsigned long old
, unsigned long bits
)
295 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
297 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
298 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
299 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
300 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
305 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
306 unsigned long old
, unsigned long bits
)
308 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
309 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
312 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
313 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
316 root
->fs_info
->delalloc_bytes
= 0;
317 BTRFS_I(inode
)->delalloc_bytes
= 0;
319 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
320 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
322 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
327 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
328 size_t size
, struct bio
*bio
)
330 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
331 struct btrfs_mapping_tree
*map_tree
;
332 u64 logical
= bio
->bi_sector
<< 9;
337 length
= bio
->bi_size
;
338 map_tree
= &root
->fs_info
->mapping_tree
;
340 ret
= btrfs_map_block(map_tree
, READ
, logical
,
341 &map_length
, NULL
, 0);
343 if (map_length
< length
+ size
) {
349 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
354 struct btrfs_ordered_sum
*sums
;
356 ret
= btrfs_csum_one_bio(root
, bio
, &sums
);
359 ret
= btrfs_add_ordered_sum(inode
, sums
);
362 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
365 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
368 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
371 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
374 if (!(rw
& (1 << BIO_RW
))) {
378 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
379 inode
, rw
, bio
, mirror_num
,
380 __btrfs_submit_bio_hook
);
382 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
385 static int add_pending_csums(struct btrfs_trans_handle
*trans
,
386 struct inode
*inode
, u64 file_offset
,
387 struct list_head
*list
)
389 struct list_head
*cur
;
390 struct btrfs_ordered_sum
*sum
;
392 btrfs_set_trans_block_group(trans
, inode
);
393 while(!list_empty(list
)) {
395 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
396 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
397 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
399 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
400 list_del(&sum
->list
);
406 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
407 struct extent_state
*state
, int uptodate
)
409 struct inode
*inode
= page
->mapping
->host
;
410 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
411 struct btrfs_trans_handle
*trans
;
412 struct btrfs_ordered_extent
*ordered_extent
;
413 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
415 struct list_head list
;
416 struct btrfs_key ins
;
419 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
424 trans
= btrfs_start_transaction(root
, 1);
426 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
427 BUG_ON(!ordered_extent
);
429 lock_extent(io_tree
, ordered_extent
->file_offset
,
430 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
433 INIT_LIST_HEAD(&list
);
435 ins
.objectid
= ordered_extent
->start
;
436 ins
.offset
= ordered_extent
->len
;
437 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
438 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
439 trans
->transid
, inode
->i_ino
,
440 ordered_extent
->file_offset
, &ins
);
442 ret
= btrfs_drop_extents(trans
, root
, inode
,
443 ordered_extent
->file_offset
,
444 ordered_extent
->file_offset
+
446 ordered_extent
->file_offset
, &alloc_hint
);
448 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
449 ordered_extent
->file_offset
,
450 ordered_extent
->start
,
452 ordered_extent
->len
, 0);
454 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
455 ordered_extent
->file_offset
+
456 ordered_extent
->len
- 1);
457 inode
->i_blocks
+= ordered_extent
->len
>> 9;
458 unlock_extent(io_tree
, ordered_extent
->file_offset
,
459 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
461 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
462 &ordered_extent
->list
);
464 btrfs_remove_ordered_extent(inode
, ordered_extent
);
466 btrfs_put_ordered_extent(ordered_extent
);
467 /* once for the tree */
468 btrfs_put_ordered_extent(ordered_extent
);
470 btrfs_update_inode(trans
, root
, inode
);
471 btrfs_end_transaction(trans
, root
);
475 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
478 struct inode
*inode
= page
->mapping
->host
;
479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
480 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
481 struct btrfs_csum_item
*item
;
482 struct btrfs_path
*path
= NULL
;
485 if (btrfs_test_opt(root
, NODATASUM
) ||
486 btrfs_test_flag(inode
, NODATASUM
))
489 path
= btrfs_alloc_path();
490 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
493 /* a csum that isn't present is a preallocated region. */
494 if (ret
== -ENOENT
|| ret
== -EFBIG
)
497 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
501 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
503 set_state_private(io_tree
, start
, csum
);
506 btrfs_free_path(path
);
510 struct io_failure_record
{
518 int btrfs_io_failed_hook(struct bio
*failed_bio
,
519 struct page
*page
, u64 start
, u64 end
,
520 struct extent_state
*state
)
522 struct io_failure_record
*failrec
= NULL
;
524 struct extent_map
*em
;
525 struct inode
*inode
= page
->mapping
->host
;
526 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
527 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
534 ret
= get_state_private(failure_tree
, start
, &private);
536 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
539 failrec
->start
= start
;
540 failrec
->len
= end
- start
+ 1;
541 failrec
->last_mirror
= 0;
543 spin_lock(&em_tree
->lock
);
544 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
545 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
549 spin_unlock(&em_tree
->lock
);
551 if (!em
|| IS_ERR(em
)) {
555 logical
= start
- em
->start
;
556 logical
= em
->block_start
+ logical
;
557 failrec
->logical
= logical
;
559 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
560 EXTENT_DIRTY
, GFP_NOFS
);
561 set_state_private(failure_tree
, start
,
562 (u64
)(unsigned long)failrec
);
564 failrec
= (struct io_failure_record
*)(unsigned long)private;
566 num_copies
= btrfs_num_copies(
567 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
568 failrec
->logical
, failrec
->len
);
569 failrec
->last_mirror
++;
571 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
572 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
575 if (state
&& state
->start
!= failrec
->start
)
577 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
579 if (!state
|| failrec
->last_mirror
> num_copies
) {
580 set_state_private(failure_tree
, failrec
->start
, 0);
581 clear_extent_bits(failure_tree
, failrec
->start
,
582 failrec
->start
+ failrec
->len
- 1,
583 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
587 bio
= bio_alloc(GFP_NOFS
, 1);
588 bio
->bi_private
= state
;
589 bio
->bi_end_io
= failed_bio
->bi_end_io
;
590 bio
->bi_sector
= failrec
->logical
>> 9;
591 bio
->bi_bdev
= failed_bio
->bi_bdev
;
593 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
594 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
599 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
600 failrec
->last_mirror
);
604 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
608 struct io_failure_record
*failure
;
612 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
613 (u64
)-1, 1, EXTENT_DIRTY
)) {
614 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
615 start
, &private_failure
);
617 failure
= (struct io_failure_record
*)(unsigned long)
619 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
621 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
623 failure
->start
+ failure
->len
- 1,
624 EXTENT_DIRTY
| EXTENT_LOCKED
,
632 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
633 struct extent_state
*state
)
635 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
636 struct inode
*inode
= page
->mapping
->host
;
637 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
639 u64
private = ~(u32
)0;
641 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
645 if (btrfs_test_opt(root
, NODATASUM
) ||
646 btrfs_test_flag(inode
, NODATASUM
))
648 if (state
&& state
->start
== start
) {
649 private = state
->private;
652 ret
= get_state_private(io_tree
, start
, &private);
654 local_irq_save(flags
);
655 kaddr
= kmap_atomic(page
, KM_IRQ0
);
659 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
660 btrfs_csum_final(csum
, (char *)&csum
);
661 if (csum
!= private) {
664 kunmap_atomic(kaddr
, KM_IRQ0
);
665 local_irq_restore(flags
);
667 /* if the io failure tree for this inode is non-empty,
668 * check to see if we've recovered from a failed IO
670 btrfs_clean_io_failures(inode
, start
);
674 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
675 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
677 memset(kaddr
+ offset
, 1, end
- start
+ 1);
678 flush_dcache_page(page
);
679 kunmap_atomic(kaddr
, KM_IRQ0
);
680 local_irq_restore(flags
);
686 void btrfs_read_locked_inode(struct inode
*inode
)
688 struct btrfs_path
*path
;
689 struct extent_buffer
*leaf
;
690 struct btrfs_inode_item
*inode_item
;
691 struct btrfs_timespec
*tspec
;
692 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
693 struct btrfs_key location
;
694 u64 alloc_group_block
;
698 path
= btrfs_alloc_path();
700 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
702 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
706 leaf
= path
->nodes
[0];
707 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
708 struct btrfs_inode_item
);
710 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
711 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
712 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
713 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
714 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
716 tspec
= btrfs_inode_atime(inode_item
);
717 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
718 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
720 tspec
= btrfs_inode_mtime(inode_item
);
721 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
722 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
724 tspec
= btrfs_inode_ctime(inode_item
);
725 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
726 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
728 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
729 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
731 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
733 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
734 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
736 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
737 if (!BTRFS_I(inode
)->block_group
) {
738 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
740 BTRFS_BLOCK_GROUP_METADATA
, 0);
742 btrfs_free_path(path
);
745 switch (inode
->i_mode
& S_IFMT
) {
747 inode
->i_mapping
->a_ops
= &btrfs_aops
;
748 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
749 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
750 inode
->i_fop
= &btrfs_file_operations
;
751 inode
->i_op
= &btrfs_file_inode_operations
;
754 inode
->i_fop
= &btrfs_dir_file_operations
;
755 if (root
== root
->fs_info
->tree_root
)
756 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
758 inode
->i_op
= &btrfs_dir_inode_operations
;
761 inode
->i_op
= &btrfs_symlink_inode_operations
;
762 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
763 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
766 init_special_inode(inode
, inode
->i_mode
, rdev
);
772 btrfs_free_path(path
);
773 make_bad_inode(inode
);
776 static void fill_inode_item(struct extent_buffer
*leaf
,
777 struct btrfs_inode_item
*item
,
780 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
781 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
782 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
783 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
784 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
786 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
787 inode
->i_atime
.tv_sec
);
788 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
789 inode
->i_atime
.tv_nsec
);
791 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
792 inode
->i_mtime
.tv_sec
);
793 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
794 inode
->i_mtime
.tv_nsec
);
796 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
797 inode
->i_ctime
.tv_sec
);
798 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
799 inode
->i_ctime
.tv_nsec
);
801 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
802 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
803 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
804 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
805 btrfs_set_inode_block_group(leaf
, item
,
806 BTRFS_I(inode
)->block_group
->key
.objectid
);
809 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
810 struct btrfs_root
*root
,
813 struct btrfs_inode_item
*inode_item
;
814 struct btrfs_path
*path
;
815 struct extent_buffer
*leaf
;
818 path
= btrfs_alloc_path();
820 ret
= btrfs_lookup_inode(trans
, root
, path
,
821 &BTRFS_I(inode
)->location
, 1);
828 leaf
= path
->nodes
[0];
829 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
830 struct btrfs_inode_item
);
832 fill_inode_item(leaf
, inode_item
, inode
);
833 btrfs_mark_buffer_dirty(leaf
);
834 btrfs_set_inode_last_trans(trans
, inode
);
837 btrfs_free_path(path
);
842 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
843 struct btrfs_root
*root
,
845 struct dentry
*dentry
)
847 struct btrfs_path
*path
;
848 const char *name
= dentry
->d_name
.name
;
849 int name_len
= dentry
->d_name
.len
;
851 struct extent_buffer
*leaf
;
852 struct btrfs_dir_item
*di
;
853 struct btrfs_key key
;
855 path
= btrfs_alloc_path();
861 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
871 leaf
= path
->nodes
[0];
872 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
873 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
876 btrfs_release_path(root
, path
);
878 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
879 key
.objectid
, name
, name_len
, -1);
888 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
889 btrfs_release_path(root
, path
);
891 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
892 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
893 dentry
->d_inode
->i_ino
,
894 dentry
->d_parent
->d_inode
->i_ino
);
896 printk("failed to delete reference to %.*s, "
897 "inode %lu parent %lu\n", name_len
, name
,
898 dentry
->d_inode
->i_ino
,
899 dentry
->d_parent
->d_inode
->i_ino
);
902 btrfs_free_path(path
);
904 dir
->i_size
-= name_len
* 2;
905 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
906 btrfs_update_inode(trans
, root
, dir
);
907 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
908 dentry
->d_inode
->i_nlink
--;
910 drop_nlink(dentry
->d_inode
);
912 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
913 dir
->i_sb
->s_dirt
= 1;
918 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
920 struct btrfs_root
*root
;
921 struct btrfs_trans_handle
*trans
;
923 unsigned long nr
= 0;
925 root
= BTRFS_I(dir
)->root
;
927 ret
= btrfs_check_free_space(root
, 1, 1);
931 trans
= btrfs_start_transaction(root
, 1);
933 btrfs_set_trans_block_group(trans
, dir
);
934 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
935 nr
= trans
->blocks_used
;
937 btrfs_end_transaction_throttle(trans
, root
);
939 btrfs_btree_balance_dirty(root
, nr
);
943 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
945 struct inode
*inode
= dentry
->d_inode
;
948 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
949 struct btrfs_trans_handle
*trans
;
950 unsigned long nr
= 0;
952 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
956 ret
= btrfs_check_free_space(root
, 1, 1);
960 trans
= btrfs_start_transaction(root
, 1);
961 btrfs_set_trans_block_group(trans
, dir
);
963 /* now the directory is empty */
964 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
969 nr
= trans
->blocks_used
;
970 ret
= btrfs_end_transaction_throttle(trans
, root
);
972 btrfs_btree_balance_dirty(root
, nr
);
980 * this can truncate away extent items, csum items and directory items.
981 * It starts at a high offset and removes keys until it can't find
982 * any higher than i_size.
984 * csum items that cross the new i_size are truncated to the new size
987 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
988 struct btrfs_root
*root
,
993 struct btrfs_path
*path
;
994 struct btrfs_key key
;
995 struct btrfs_key found_key
;
997 struct extent_buffer
*leaf
;
998 struct btrfs_file_extent_item
*fi
;
999 u64 extent_start
= 0;
1000 u64 extent_num_bytes
= 0;
1006 int pending_del_nr
= 0;
1007 int pending_del_slot
= 0;
1008 int extent_type
= -1;
1009 u64 mask
= root
->sectorsize
- 1;
1011 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1012 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1013 path
= btrfs_alloc_path();
1017 /* FIXME, add redo link to tree so we don't leak on crash */
1018 key
.objectid
= inode
->i_ino
;
1019 key
.offset
= (u64
)-1;
1022 btrfs_init_path(path
);
1024 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1029 BUG_ON(path
->slots
[0] == 0);
1035 leaf
= path
->nodes
[0];
1036 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1037 found_type
= btrfs_key_type(&found_key
);
1039 if (found_key
.objectid
!= inode
->i_ino
)
1042 if (found_type
< min_type
)
1045 item_end
= found_key
.offset
;
1046 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1047 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1048 struct btrfs_file_extent_item
);
1049 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1050 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1052 btrfs_file_extent_num_bytes(leaf
, fi
);
1053 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1054 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1056 item_end
+= btrfs_file_extent_inline_len(leaf
,
1061 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1062 ret
= btrfs_csum_truncate(trans
, root
, path
,
1066 if (item_end
< inode
->i_size
) {
1067 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1068 found_type
= BTRFS_INODE_ITEM_KEY
;
1069 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1070 found_type
= BTRFS_CSUM_ITEM_KEY
;
1071 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1072 found_type
= BTRFS_XATTR_ITEM_KEY
;
1073 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1074 found_type
= BTRFS_INODE_REF_KEY
;
1075 } else if (found_type
) {
1080 btrfs_set_key_type(&key
, found_type
);
1083 if (found_key
.offset
>= inode
->i_size
)
1089 /* FIXME, shrink the extent if the ref count is only 1 */
1090 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1093 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1095 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1097 u64 orig_num_bytes
=
1098 btrfs_file_extent_num_bytes(leaf
, fi
);
1099 extent_num_bytes
= inode
->i_size
-
1100 found_key
.offset
+ root
->sectorsize
- 1;
1101 extent_num_bytes
= extent_num_bytes
&
1102 ~((u64
)root
->sectorsize
- 1);
1103 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1105 num_dec
= (orig_num_bytes
-
1107 if (extent_start
!= 0)
1108 dec_i_blocks(inode
, num_dec
);
1109 btrfs_mark_buffer_dirty(leaf
);
1112 btrfs_file_extent_disk_num_bytes(leaf
,
1114 /* FIXME blocksize != 4096 */
1115 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1116 if (extent_start
!= 0) {
1118 dec_i_blocks(inode
, num_dec
);
1120 root_gen
= btrfs_header_generation(leaf
);
1121 root_owner
= btrfs_header_owner(leaf
);
1123 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1125 u32 newsize
= inode
->i_size
- found_key
.offset
;
1126 dec_i_blocks(inode
, item_end
+ 1 -
1127 found_key
.offset
- newsize
);
1129 btrfs_file_extent_calc_inline_size(newsize
);
1130 ret
= btrfs_truncate_item(trans
, root
, path
,
1134 dec_i_blocks(inode
, item_end
+ 1 -
1140 if (!pending_del_nr
) {
1141 /* no pending yet, add ourselves */
1142 pending_del_slot
= path
->slots
[0];
1144 } else if (pending_del_nr
&&
1145 path
->slots
[0] + 1 == pending_del_slot
) {
1146 /* hop on the pending chunk */
1148 pending_del_slot
= path
->slots
[0];
1150 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1156 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1159 root_gen
, inode
->i_ino
,
1160 found_key
.offset
, 0);
1164 if (path
->slots
[0] == 0) {
1167 btrfs_release_path(root
, path
);
1172 if (pending_del_nr
&&
1173 path
->slots
[0] + 1 != pending_del_slot
) {
1174 struct btrfs_key debug
;
1176 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1178 ret
= btrfs_del_items(trans
, root
, path
,
1183 btrfs_release_path(root
, path
);
1189 if (pending_del_nr
) {
1190 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1193 btrfs_free_path(path
);
1194 inode
->i_sb
->s_dirt
= 1;
1199 * taken from block_truncate_page, but does cow as it zeros out
1200 * any bytes left in the last page in the file.
1202 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1204 struct inode
*inode
= mapping
->host
;
1205 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1206 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1207 struct btrfs_ordered_extent
*ordered
;
1209 u32 blocksize
= root
->sectorsize
;
1210 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1211 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1217 if ((offset
& (blocksize
- 1)) == 0)
1222 page
= grab_cache_page(mapping
, index
);
1226 page_start
= page_offset(page
);
1227 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1229 if (!PageUptodate(page
)) {
1230 ret
= btrfs_readpage(NULL
, page
);
1232 if (page
->mapping
!= mapping
) {
1234 page_cache_release(page
);
1237 if (!PageUptodate(page
)) {
1242 wait_on_page_writeback(page
);
1244 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1245 set_page_extent_mapped(page
);
1247 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1249 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1251 page_cache_release(page
);
1252 btrfs_wait_ordered_extent(inode
, ordered
);
1253 btrfs_put_ordered_extent(ordered
);
1257 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
1258 page_end
, GFP_NOFS
);
1260 if (offset
!= PAGE_CACHE_SIZE
) {
1262 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1263 flush_dcache_page(page
);
1266 set_page_dirty(page
);
1267 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1270 page_cache_release(page
);
1275 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1277 struct inode
*inode
= dentry
->d_inode
;
1280 err
= inode_change_ok(inode
, attr
);
1284 if (S_ISREG(inode
->i_mode
) &&
1285 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1286 struct btrfs_trans_handle
*trans
;
1287 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1288 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1290 u64 mask
= root
->sectorsize
- 1;
1291 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1292 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1296 if (attr
->ia_size
<= hole_start
)
1299 err
= btrfs_check_free_space(root
, 1, 0);
1303 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1305 hole_size
= block_end
- hole_start
;
1306 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1307 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1309 trans
= btrfs_start_transaction(root
, 1);
1310 btrfs_set_trans_block_group(trans
, inode
);
1311 err
= btrfs_drop_extents(trans
, root
, inode
,
1312 hole_start
, block_end
, hole_start
,
1315 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1316 err
= btrfs_insert_file_extent(trans
, root
,
1320 btrfs_drop_extent_cache(inode
, hole_start
,
1322 btrfs_check_file(root
, inode
);
1324 btrfs_end_transaction(trans
, root
);
1325 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1330 err
= inode_setattr(inode
, attr
);
1335 void btrfs_delete_inode(struct inode
*inode
)
1337 struct btrfs_trans_handle
*trans
;
1338 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1342 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1343 truncate_inode_pages(&inode
->i_data
, 0);
1344 if (is_bad_inode(inode
)) {
1349 trans
= btrfs_start_transaction(root
, 1);
1351 btrfs_set_trans_block_group(trans
, inode
);
1352 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1354 goto no_delete_lock
;
1356 nr
= trans
->blocks_used
;
1359 btrfs_end_transaction(trans
, root
);
1360 btrfs_btree_balance_dirty(root
, nr
);
1364 nr
= trans
->blocks_used
;
1365 btrfs_end_transaction(trans
, root
);
1366 btrfs_btree_balance_dirty(root
, nr
);
1372 * this returns the key found in the dir entry in the location pointer.
1373 * If no dir entries were found, location->objectid is 0.
1375 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1376 struct btrfs_key
*location
)
1378 const char *name
= dentry
->d_name
.name
;
1379 int namelen
= dentry
->d_name
.len
;
1380 struct btrfs_dir_item
*di
;
1381 struct btrfs_path
*path
;
1382 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1385 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1386 location
->objectid
= dir
->i_ino
;
1387 location
->type
= BTRFS_INODE_ITEM_KEY
;
1388 location
->offset
= 0;
1391 path
= btrfs_alloc_path();
1394 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1395 struct btrfs_key key
;
1396 struct extent_buffer
*leaf
;
1400 key
.objectid
= dir
->i_ino
;
1401 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1403 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1407 leaf
= path
->nodes
[0];
1408 slot
= path
->slots
[0];
1409 nritems
= btrfs_header_nritems(leaf
);
1410 if (slot
>= nritems
)
1413 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1414 if (key
.objectid
!= dir
->i_ino
||
1415 key
.type
!= BTRFS_INODE_REF_KEY
) {
1418 location
->objectid
= key
.offset
;
1419 location
->type
= BTRFS_INODE_ITEM_KEY
;
1420 location
->offset
= 0;
1424 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1428 if (!di
|| IS_ERR(di
)) {
1431 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1433 btrfs_free_path(path
);
1436 location
->objectid
= 0;
1441 * when we hit a tree root in a directory, the btrfs part of the inode
1442 * needs to be changed to reflect the root directory of the tree root. This
1443 * is kind of like crossing a mount point.
1445 static int fixup_tree_root_location(struct btrfs_root
*root
,
1446 struct btrfs_key
*location
,
1447 struct btrfs_root
**sub_root
,
1448 struct dentry
*dentry
)
1450 struct btrfs_path
*path
;
1451 struct btrfs_root_item
*ri
;
1453 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1455 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1458 path
= btrfs_alloc_path();
1461 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1462 dentry
->d_name
.name
,
1463 dentry
->d_name
.len
);
1464 if (IS_ERR(*sub_root
))
1465 return PTR_ERR(*sub_root
);
1467 ri
= &(*sub_root
)->root_item
;
1468 location
->objectid
= btrfs_root_dirid(ri
);
1469 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1470 location
->offset
= 0;
1472 btrfs_free_path(path
);
1476 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1478 struct btrfs_iget_args
*args
= p
;
1479 inode
->i_ino
= args
->ino
;
1480 BTRFS_I(inode
)->root
= args
->root
;
1481 BTRFS_I(inode
)->delalloc_bytes
= 0;
1482 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1483 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1484 inode
->i_mapping
, GFP_NOFS
);
1485 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1486 inode
->i_mapping
, GFP_NOFS
);
1487 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1491 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1493 struct btrfs_iget_args
*args
= opaque
;
1494 return (args
->ino
== inode
->i_ino
&&
1495 args
->root
== BTRFS_I(inode
)->root
);
1498 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1501 struct btrfs_iget_args args
;
1502 args
.ino
= objectid
;
1503 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1508 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1511 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1512 struct btrfs_root
*root
)
1514 struct inode
*inode
;
1515 struct btrfs_iget_args args
;
1516 args
.ino
= objectid
;
1519 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1520 btrfs_init_locked_inode
,
1525 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1526 struct nameidata
*nd
)
1528 struct inode
* inode
;
1529 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1530 struct btrfs_root
*root
= bi
->root
;
1531 struct btrfs_root
*sub_root
= root
;
1532 struct btrfs_key location
;
1535 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1536 return ERR_PTR(-ENAMETOOLONG
);
1538 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1541 return ERR_PTR(ret
);
1544 if (location
.objectid
) {
1545 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1548 return ERR_PTR(ret
);
1550 return ERR_PTR(-ENOENT
);
1551 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1554 return ERR_PTR(-EACCES
);
1555 if (inode
->i_state
& I_NEW
) {
1556 /* the inode and parent dir are two different roots */
1557 if (sub_root
!= root
) {
1559 sub_root
->inode
= inode
;
1561 BTRFS_I(inode
)->root
= sub_root
;
1562 memcpy(&BTRFS_I(inode
)->location
, &location
,
1564 btrfs_read_locked_inode(inode
);
1565 unlock_new_inode(inode
);
1568 return d_splice_alias(inode
, dentry
);
1571 static unsigned char btrfs_filetype_table
[] = {
1572 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1575 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1577 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1578 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1579 struct btrfs_item
*item
;
1580 struct btrfs_dir_item
*di
;
1581 struct btrfs_key key
;
1582 struct btrfs_key found_key
;
1583 struct btrfs_path
*path
;
1586 struct extent_buffer
*leaf
;
1589 unsigned char d_type
;
1594 int key_type
= BTRFS_DIR_INDEX_KEY
;
1599 /* FIXME, use a real flag for deciding about the key type */
1600 if (root
->fs_info
->tree_root
== root
)
1601 key_type
= BTRFS_DIR_ITEM_KEY
;
1603 /* special case for "." */
1604 if (filp
->f_pos
== 0) {
1605 over
= filldir(dirent
, ".", 1,
1613 key
.objectid
= inode
->i_ino
;
1614 path
= btrfs_alloc_path();
1617 /* special case for .., just use the back ref */
1618 if (filp
->f_pos
== 1) {
1619 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1621 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1623 leaf
= path
->nodes
[0];
1624 slot
= path
->slots
[0];
1625 nritems
= btrfs_header_nritems(leaf
);
1626 if (slot
>= nritems
) {
1627 btrfs_release_path(root
, path
);
1628 goto read_dir_items
;
1630 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1631 btrfs_release_path(root
, path
);
1632 if (found_key
.objectid
!= key
.objectid
||
1633 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1634 goto read_dir_items
;
1635 over
= filldir(dirent
, "..", 2,
1636 2, found_key
.offset
, DT_DIR
);
1643 btrfs_set_key_type(&key
, key_type
);
1644 key
.offset
= filp
->f_pos
;
1646 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1651 leaf
= path
->nodes
[0];
1652 nritems
= btrfs_header_nritems(leaf
);
1653 slot
= path
->slots
[0];
1654 if (advance
|| slot
>= nritems
) {
1655 if (slot
>= nritems
-1) {
1656 ret
= btrfs_next_leaf(root
, path
);
1659 leaf
= path
->nodes
[0];
1660 nritems
= btrfs_header_nritems(leaf
);
1661 slot
= path
->slots
[0];
1668 item
= btrfs_item_nr(leaf
, slot
);
1669 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1671 if (found_key
.objectid
!= key
.objectid
)
1673 if (btrfs_key_type(&found_key
) != key_type
)
1675 if (found_key
.offset
< filp
->f_pos
)
1678 filp
->f_pos
= found_key
.offset
;
1680 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1682 di_total
= btrfs_item_size(leaf
, item
);
1683 while(di_cur
< di_total
) {
1684 struct btrfs_key location
;
1686 name_len
= btrfs_dir_name_len(leaf
, di
);
1687 if (name_len
< 32) {
1688 name_ptr
= tmp_name
;
1690 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1693 read_extent_buffer(leaf
, name_ptr
,
1694 (unsigned long)(di
+ 1), name_len
);
1696 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1697 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1698 over
= filldir(dirent
, name_ptr
, name_len
,
1703 if (name_ptr
!= tmp_name
)
1708 di_len
= btrfs_dir_name_len(leaf
, di
) +
1709 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1711 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1714 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1715 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1721 btrfs_free_path(path
);
1725 int btrfs_write_inode(struct inode
*inode
, int wait
)
1727 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1728 struct btrfs_trans_handle
*trans
;
1732 trans
= btrfs_start_transaction(root
, 1);
1733 btrfs_set_trans_block_group(trans
, inode
);
1734 ret
= btrfs_commit_transaction(trans
, root
);
1740 * This is somewhat expensive, updating the tree every time the
1741 * inode changes. But, it is most likely to find the inode in cache.
1742 * FIXME, needs more benchmarking...there are no reasons other than performance
1743 * to keep or drop this code.
1745 void btrfs_dirty_inode(struct inode
*inode
)
1747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1748 struct btrfs_trans_handle
*trans
;
1750 trans
= btrfs_start_transaction(root
, 1);
1751 btrfs_set_trans_block_group(trans
, inode
);
1752 btrfs_update_inode(trans
, root
, inode
);
1753 btrfs_end_transaction(trans
, root
);
1756 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1757 struct btrfs_root
*root
,
1758 const char *name
, int name_len
,
1761 struct btrfs_block_group_cache
*group
,
1764 struct inode
*inode
;
1765 struct btrfs_inode_item
*inode_item
;
1766 struct btrfs_block_group_cache
*new_inode_group
;
1767 struct btrfs_key
*location
;
1768 struct btrfs_path
*path
;
1769 struct btrfs_inode_ref
*ref
;
1770 struct btrfs_key key
[2];
1776 path
= btrfs_alloc_path();
1779 inode
= new_inode(root
->fs_info
->sb
);
1781 return ERR_PTR(-ENOMEM
);
1783 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1784 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1785 inode
->i_mapping
, GFP_NOFS
);
1786 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1787 inode
->i_mapping
, GFP_NOFS
);
1788 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1789 BTRFS_I(inode
)->delalloc_bytes
= 0;
1790 BTRFS_I(inode
)->root
= root
;
1796 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1797 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1798 if (!new_inode_group
) {
1799 printk("find_block group failed\n");
1800 new_inode_group
= group
;
1802 BTRFS_I(inode
)->block_group
= new_inode_group
;
1803 BTRFS_I(inode
)->flags
= 0;
1805 key
[0].objectid
= objectid
;
1806 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1809 key
[1].objectid
= objectid
;
1810 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1811 key
[1].offset
= ref_objectid
;
1813 sizes
[0] = sizeof(struct btrfs_inode_item
);
1814 sizes
[1] = name_len
+ sizeof(*ref
);
1816 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1820 if (objectid
> root
->highest_inode
)
1821 root
->highest_inode
= objectid
;
1823 inode
->i_uid
= current
->fsuid
;
1824 inode
->i_gid
= current
->fsgid
;
1825 inode
->i_mode
= mode
;
1826 inode
->i_ino
= objectid
;
1827 inode
->i_blocks
= 0;
1828 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1829 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1830 struct btrfs_inode_item
);
1831 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1833 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1834 struct btrfs_inode_ref
);
1835 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1836 ptr
= (unsigned long)(ref
+ 1);
1837 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1839 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1840 btrfs_free_path(path
);
1842 location
= &BTRFS_I(inode
)->location
;
1843 location
->objectid
= objectid
;
1844 location
->offset
= 0;
1845 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1847 insert_inode_hash(inode
);
1850 btrfs_free_path(path
);
1851 return ERR_PTR(ret
);
1854 static inline u8
btrfs_inode_type(struct inode
*inode
)
1856 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1859 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1860 struct dentry
*dentry
, struct inode
*inode
,
1864 struct btrfs_key key
;
1865 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1866 struct inode
*parent_inode
;
1868 key
.objectid
= inode
->i_ino
;
1869 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1872 ret
= btrfs_insert_dir_item(trans
, root
,
1873 dentry
->d_name
.name
, dentry
->d_name
.len
,
1874 dentry
->d_parent
->d_inode
->i_ino
,
1875 &key
, btrfs_inode_type(inode
));
1878 ret
= btrfs_insert_inode_ref(trans
, root
,
1879 dentry
->d_name
.name
,
1882 dentry
->d_parent
->d_inode
->i_ino
);
1884 parent_inode
= dentry
->d_parent
->d_inode
;
1885 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1886 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1887 ret
= btrfs_update_inode(trans
, root
,
1888 dentry
->d_parent
->d_inode
);
1893 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1894 struct dentry
*dentry
, struct inode
*inode
,
1897 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1899 d_instantiate(dentry
, inode
);
1907 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1908 int mode
, dev_t rdev
)
1910 struct btrfs_trans_handle
*trans
;
1911 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1912 struct inode
*inode
= NULL
;
1916 unsigned long nr
= 0;
1918 if (!new_valid_dev(rdev
))
1921 err
= btrfs_check_free_space(root
, 1, 0);
1925 trans
= btrfs_start_transaction(root
, 1);
1926 btrfs_set_trans_block_group(trans
, dir
);
1928 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1934 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1936 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1937 BTRFS_I(dir
)->block_group
, mode
);
1938 err
= PTR_ERR(inode
);
1942 btrfs_set_trans_block_group(trans
, inode
);
1943 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1947 inode
->i_op
= &btrfs_special_inode_operations
;
1948 init_special_inode(inode
, inode
->i_mode
, rdev
);
1949 btrfs_update_inode(trans
, root
, inode
);
1951 dir
->i_sb
->s_dirt
= 1;
1952 btrfs_update_inode_block_group(trans
, inode
);
1953 btrfs_update_inode_block_group(trans
, dir
);
1955 nr
= trans
->blocks_used
;
1956 btrfs_end_transaction_throttle(trans
, root
);
1959 inode_dec_link_count(inode
);
1962 btrfs_btree_balance_dirty(root
, nr
);
1966 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1967 int mode
, struct nameidata
*nd
)
1969 struct btrfs_trans_handle
*trans
;
1970 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1971 struct inode
*inode
= NULL
;
1974 unsigned long nr
= 0;
1977 err
= btrfs_check_free_space(root
, 1, 0);
1980 trans
= btrfs_start_transaction(root
, 1);
1981 btrfs_set_trans_block_group(trans
, dir
);
1983 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1989 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1991 dentry
->d_parent
->d_inode
->i_ino
,
1992 objectid
, BTRFS_I(dir
)->block_group
, mode
);
1993 err
= PTR_ERR(inode
);
1997 btrfs_set_trans_block_group(trans
, inode
);
1998 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2002 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2003 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2004 inode
->i_fop
= &btrfs_file_operations
;
2005 inode
->i_op
= &btrfs_file_inode_operations
;
2006 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2007 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2008 inode
->i_mapping
, GFP_NOFS
);
2009 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2010 inode
->i_mapping
, GFP_NOFS
);
2011 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2012 BTRFS_I(inode
)->delalloc_bytes
= 0;
2013 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2015 dir
->i_sb
->s_dirt
= 1;
2016 btrfs_update_inode_block_group(trans
, inode
);
2017 btrfs_update_inode_block_group(trans
, dir
);
2019 nr
= trans
->blocks_used
;
2020 btrfs_end_transaction_throttle(trans
, root
);
2023 inode_dec_link_count(inode
);
2026 btrfs_btree_balance_dirty(root
, nr
);
2030 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2031 struct dentry
*dentry
)
2033 struct btrfs_trans_handle
*trans
;
2034 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2035 struct inode
*inode
= old_dentry
->d_inode
;
2036 unsigned long nr
= 0;
2040 if (inode
->i_nlink
== 0)
2043 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2048 err
= btrfs_check_free_space(root
, 1, 0);
2051 trans
= btrfs_start_transaction(root
, 1);
2053 btrfs_set_trans_block_group(trans
, dir
);
2054 atomic_inc(&inode
->i_count
);
2055 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
2060 dir
->i_sb
->s_dirt
= 1;
2061 btrfs_update_inode_block_group(trans
, dir
);
2062 err
= btrfs_update_inode(trans
, root
, inode
);
2067 nr
= trans
->blocks_used
;
2068 btrfs_end_transaction_throttle(trans
, root
);
2071 inode_dec_link_count(inode
);
2074 btrfs_btree_balance_dirty(root
, nr
);
2078 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2080 struct inode
*inode
= NULL
;
2081 struct btrfs_trans_handle
*trans
;
2082 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2084 int drop_on_err
= 0;
2086 unsigned long nr
= 1;
2088 err
= btrfs_check_free_space(root
, 1, 0);
2092 trans
= btrfs_start_transaction(root
, 1);
2093 btrfs_set_trans_block_group(trans
, dir
);
2095 if (IS_ERR(trans
)) {
2096 err
= PTR_ERR(trans
);
2100 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2106 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2108 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2109 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
2110 if (IS_ERR(inode
)) {
2111 err
= PTR_ERR(inode
);
2116 inode
->i_op
= &btrfs_dir_inode_operations
;
2117 inode
->i_fop
= &btrfs_dir_file_operations
;
2118 btrfs_set_trans_block_group(trans
, inode
);
2121 err
= btrfs_update_inode(trans
, root
, inode
);
2125 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
2129 d_instantiate(dentry
, inode
);
2131 dir
->i_sb
->s_dirt
= 1;
2132 btrfs_update_inode_block_group(trans
, inode
);
2133 btrfs_update_inode_block_group(trans
, dir
);
2136 nr
= trans
->blocks_used
;
2137 btrfs_end_transaction_throttle(trans
, root
);
2142 btrfs_btree_balance_dirty(root
, nr
);
2146 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2147 struct extent_map
*existing
,
2148 struct extent_map
*em
,
2149 u64 map_start
, u64 map_len
)
2153 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2154 start_diff
= map_start
- em
->start
;
2155 em
->start
= map_start
;
2157 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2158 em
->block_start
+= start_diff
;
2159 return add_extent_mapping(em_tree
, em
);
2162 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2163 size_t pg_offset
, u64 start
, u64 len
,
2169 u64 extent_start
= 0;
2171 u64 objectid
= inode
->i_ino
;
2173 struct btrfs_path
*path
;
2174 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2175 struct btrfs_file_extent_item
*item
;
2176 struct extent_buffer
*leaf
;
2177 struct btrfs_key found_key
;
2178 struct extent_map
*em
= NULL
;
2179 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2180 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2181 struct btrfs_trans_handle
*trans
= NULL
;
2183 path
= btrfs_alloc_path();
2187 spin_lock(&em_tree
->lock
);
2188 em
= lookup_extent_mapping(em_tree
, start
, len
);
2190 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2191 spin_unlock(&em_tree
->lock
);
2194 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2195 free_extent_map(em
);
2196 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2197 free_extent_map(em
);
2201 em
= alloc_extent_map(GFP_NOFS
);
2206 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2207 em
->start
= EXTENT_MAP_HOLE
;
2209 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2210 objectid
, start
, trans
!= NULL
);
2217 if (path
->slots
[0] == 0)
2222 leaf
= path
->nodes
[0];
2223 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2224 struct btrfs_file_extent_item
);
2225 /* are we inside the extent that was found? */
2226 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2227 found_type
= btrfs_key_type(&found_key
);
2228 if (found_key
.objectid
!= objectid
||
2229 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2233 found_type
= btrfs_file_extent_type(leaf
, item
);
2234 extent_start
= found_key
.offset
;
2235 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2236 extent_end
= extent_start
+
2237 btrfs_file_extent_num_bytes(leaf
, item
);
2239 if (start
< extent_start
|| start
>= extent_end
) {
2241 if (start
< extent_start
) {
2242 if (start
+ len
<= extent_start
)
2244 em
->len
= extent_end
- extent_start
;
2250 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2252 em
->start
= extent_start
;
2253 em
->len
= extent_end
- extent_start
;
2254 em
->block_start
= EXTENT_MAP_HOLE
;
2257 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2258 em
->block_start
= bytenr
;
2259 em
->start
= extent_start
;
2260 em
->len
= extent_end
- extent_start
;
2262 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2267 size_t extent_offset
;
2270 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2272 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2273 ~((u64
)root
->sectorsize
- 1);
2274 if (start
< extent_start
|| start
>= extent_end
) {
2276 if (start
< extent_start
) {
2277 if (start
+ len
<= extent_start
)
2279 em
->len
= extent_end
- extent_start
;
2285 em
->block_start
= EXTENT_MAP_INLINE
;
2288 em
->start
= extent_start
;
2293 page_start
= page_offset(page
) + pg_offset
;
2294 extent_offset
= page_start
- extent_start
;
2295 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2296 size
- extent_offset
);
2297 em
->start
= extent_start
+ extent_offset
;
2298 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2299 ~((u64
)root
->sectorsize
- 1);
2301 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2302 if (create
== 0 && !PageUptodate(page
)) {
2303 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2305 flush_dcache_page(page
);
2306 } else if (create
&& PageUptodate(page
)) {
2309 free_extent_map(em
);
2311 btrfs_release_path(root
, path
);
2312 trans
= btrfs_start_transaction(root
, 1);
2315 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2317 btrfs_mark_buffer_dirty(leaf
);
2320 set_extent_uptodate(io_tree
, em
->start
,
2321 extent_map_end(em
) - 1, GFP_NOFS
);
2324 printk("unkknown found_type %d\n", found_type
);
2331 em
->block_start
= EXTENT_MAP_HOLE
;
2333 btrfs_release_path(root
, path
);
2334 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2335 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2341 spin_lock(&em_tree
->lock
);
2342 ret
= add_extent_mapping(em_tree
, em
);
2343 /* it is possible that someone inserted the extent into the tree
2344 * while we had the lock dropped. It is also possible that
2345 * an overlapping map exists in the tree
2347 if (ret
== -EEXIST
) {
2348 struct extent_map
*existing
;
2352 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2353 if (existing
&& (existing
->start
> start
||
2354 existing
->start
+ existing
->len
<= start
)) {
2355 free_extent_map(existing
);
2359 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2362 err
= merge_extent_mapping(em_tree
, existing
,
2365 free_extent_map(existing
);
2367 free_extent_map(em
);
2372 printk("failing to insert %Lu %Lu\n",
2374 free_extent_map(em
);
2378 free_extent_map(em
);
2383 spin_unlock(&em_tree
->lock
);
2385 btrfs_free_path(path
);
2387 ret
= btrfs_end_transaction(trans
, root
);
2393 free_extent_map(em
);
2395 return ERR_PTR(err
);
2400 #if 0 /* waiting for O_DIRECT reads */
2401 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2402 struct buffer_head
*bh_result
, int create
)
2404 struct extent_map
*em
;
2405 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2406 struct btrfs_multi_bio
*multi
= NULL
;
2407 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2413 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2415 if (!em
|| IS_ERR(em
))
2418 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2422 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2427 len
= em
->start
+ em
->len
- start
;
2428 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2430 if (em
->block_start
== EXTENT_MAP_HOLE
||
2431 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2432 bh_result
->b_size
= len
;
2436 logical
= start
- em
->start
;
2437 logical
= em
->block_start
+ logical
;
2440 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2441 logical
, &map_length
, &multi
, 0);
2443 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2444 bh_result
->b_size
= min(map_length
, len
);
2446 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2447 set_buffer_mapped(bh_result
);
2450 free_extent_map(em
);
2455 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2456 const struct iovec
*iov
, loff_t offset
,
2457 unsigned long nr_segs
)
2461 struct file
*file
= iocb
->ki_filp
;
2462 struct inode
*inode
= file
->f_mapping
->host
;
2467 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2468 offset
, nr_segs
, btrfs_get_block
, NULL
);
2472 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2474 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2477 int btrfs_readpage(struct file
*file
, struct page
*page
)
2479 struct extent_io_tree
*tree
;
2480 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2481 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2484 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2486 struct extent_io_tree
*tree
;
2489 if (current
->flags
& PF_MEMALLOC
) {
2490 redirty_page_for_writepage(wbc
, page
);
2494 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2495 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2498 static int btrfs_writepages(struct address_space
*mapping
,
2499 struct writeback_control
*wbc
)
2501 struct extent_io_tree
*tree
;
2502 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2503 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2507 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2508 struct list_head
*pages
, unsigned nr_pages
)
2510 struct extent_io_tree
*tree
;
2511 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2512 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2515 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2517 struct extent_io_tree
*tree
;
2518 struct extent_map_tree
*map
;
2521 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2522 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2523 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2525 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
2526 ClearPagePrivate(page
);
2527 set_page_private(page
, 0);
2528 page_cache_release(page
);
2533 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2535 struct btrfs_ordered_extent
*ordered
;
2537 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2540 btrfs_put_ordered_extent(ordered
);
2543 return __btrfs_releasepage(page
, gfp_flags
);
2546 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2548 struct extent_io_tree
*tree
;
2549 struct btrfs_ordered_extent
*ordered
;
2550 u64 page_start
= page_offset(page
);
2551 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2553 wait_on_page_writeback(page
);
2554 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2556 btrfs_releasepage(page
, GFP_NOFS
);
2560 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2561 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
2564 clear_extent_bit(tree
, page_start
, page_end
,
2565 EXTENT_DIRTY
| EXTENT_DELALLOC
|
2566 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
2567 btrfs_writepage_end_io_hook(page
, page_start
,
2569 btrfs_put_ordered_extent(ordered
);
2570 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2572 clear_extent_bit(tree
, page_start
, page_end
,
2573 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2576 __btrfs_releasepage(page
, GFP_NOFS
);
2578 if (PagePrivate(page
)) {
2579 invalidate_extent_lru(tree
, page_offset(page
),
2581 ClearPagePrivate(page
);
2582 set_page_private(page
, 0);
2583 page_cache_release(page
);
2588 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2589 * called from a page fault handler when a page is first dirtied. Hence we must
2590 * be careful to check for EOF conditions here. We set the page up correctly
2591 * for a written page which means we get ENOSPC checking when writing into
2592 * holes and correct delalloc and unwritten extent mapping on filesystems that
2593 * support these features.
2595 * We are not allowed to take the i_mutex here so we have to play games to
2596 * protect against truncate races as the page could now be beyond EOF. Because
2597 * vmtruncate() writes the inode size before removing pages, once we have the
2598 * page lock we can determine safely if the page is beyond EOF. If it is not
2599 * beyond EOF, then the page is guaranteed safe against truncation until we
2602 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2604 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2605 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2606 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2607 struct btrfs_ordered_extent
*ordered
;
2609 unsigned long zero_start
;
2615 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2622 size
= i_size_read(inode
);
2623 page_start
= page_offset(page
);
2624 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2626 if ((page
->mapping
!= inode
->i_mapping
) ||
2627 (page_start
>= size
)) {
2628 /* page got truncated out from underneath us */
2631 wait_on_page_writeback(page
);
2633 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2634 set_page_extent_mapped(page
);
2636 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2638 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2640 btrfs_wait_ordered_extent(inode
, ordered
);
2641 btrfs_put_ordered_extent(ordered
);
2645 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
2646 page_end
, GFP_NOFS
);
2649 /* page is wholly or partially inside EOF */
2650 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2651 zero_start
= size
& ~PAGE_CACHE_MASK
;
2653 zero_start
= PAGE_CACHE_SIZE
;
2655 if (zero_start
!= PAGE_CACHE_SIZE
) {
2657 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
2658 flush_dcache_page(page
);
2661 set_page_dirty(page
);
2662 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2670 static void btrfs_truncate(struct inode
*inode
)
2672 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2674 struct btrfs_trans_handle
*trans
;
2677 if (!S_ISREG(inode
->i_mode
))
2679 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2682 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2684 trans
= btrfs_start_transaction(root
, 1);
2685 btrfs_set_trans_block_group(trans
, inode
);
2687 /* FIXME, add redo link to tree so we don't leak on crash */
2688 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2689 BTRFS_EXTENT_DATA_KEY
);
2690 btrfs_update_inode(trans
, root
, inode
);
2691 nr
= trans
->blocks_used
;
2693 ret
= btrfs_end_transaction_throttle(trans
, root
);
2695 btrfs_btree_balance_dirty(root
, nr
);
2699 * Invalidate a single dcache entry at the root of the filesystem.
2700 * Needed after creation of snapshot or subvolume.
2702 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
2705 struct dentry
*alias
, *entry
;
2708 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
2712 /* change me if btrfs ever gets a d_hash operation */
2713 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
2714 entry
= d_lookup(alias
, &qstr
);
2717 d_invalidate(entry
);
2723 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
2724 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
2725 struct btrfs_block_group_cache
*block_group
)
2727 struct inode
*inode
;
2730 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2731 new_dirid
, block_group
, S_IFDIR
| 0700);
2733 return PTR_ERR(inode
);
2734 inode
->i_op
= &btrfs_dir_inode_operations
;
2735 inode
->i_fop
= &btrfs_dir_file_operations
;
2736 new_root
->inode
= inode
;
2738 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2743 return btrfs_update_inode(trans
, new_root
, inode
);
2746 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2747 struct file_ra_state
*ra
, struct file
*file
,
2748 pgoff_t offset
, pgoff_t last_index
)
2750 pgoff_t req_size
= last_index
- offset
+ 1;
2752 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2753 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2756 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2757 return offset
+ req_size
;
2761 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2763 struct btrfs_inode
*ei
;
2765 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2769 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
2770 return &ei
->vfs_inode
;
2773 void btrfs_destroy_inode(struct inode
*inode
)
2775 struct btrfs_ordered_extent
*ordered
;
2776 WARN_ON(!list_empty(&inode
->i_dentry
));
2777 WARN_ON(inode
->i_data
.nrpages
);
2780 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
2784 printk("found ordered extent %Lu %Lu\n",
2785 ordered
->file_offset
, ordered
->len
);
2786 btrfs_remove_ordered_extent(inode
, ordered
);
2787 btrfs_put_ordered_extent(ordered
);
2788 btrfs_put_ordered_extent(ordered
);
2791 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2792 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2795 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2796 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2798 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2799 unsigned long flags
)
2802 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2804 inode_init_once(&ei
->vfs_inode
);
2807 void btrfs_destroy_cachep(void)
2809 if (btrfs_inode_cachep
)
2810 kmem_cache_destroy(btrfs_inode_cachep
);
2811 if (btrfs_trans_handle_cachep
)
2812 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2813 if (btrfs_transaction_cachep
)
2814 kmem_cache_destroy(btrfs_transaction_cachep
);
2815 if (btrfs_bit_radix_cachep
)
2816 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2817 if (btrfs_path_cachep
)
2818 kmem_cache_destroy(btrfs_path_cachep
);
2821 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2822 unsigned long extra_flags
,
2823 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2824 void (*ctor
)(struct kmem_cache
*, void *)
2826 void (*ctor
)(void *, struct kmem_cache
*,
2831 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2832 SLAB_MEM_SPREAD
| extra_flags
), ctor
2833 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2839 int btrfs_init_cachep(void)
2841 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2842 sizeof(struct btrfs_inode
),
2844 if (!btrfs_inode_cachep
)
2846 btrfs_trans_handle_cachep
=
2847 btrfs_cache_create("btrfs_trans_handle_cache",
2848 sizeof(struct btrfs_trans_handle
),
2850 if (!btrfs_trans_handle_cachep
)
2852 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2853 sizeof(struct btrfs_transaction
),
2855 if (!btrfs_transaction_cachep
)
2857 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2858 sizeof(struct btrfs_path
),
2860 if (!btrfs_path_cachep
)
2862 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2863 SLAB_DESTROY_BY_RCU
, NULL
);
2864 if (!btrfs_bit_radix_cachep
)
2868 btrfs_destroy_cachep();
2872 static int btrfs_getattr(struct vfsmount
*mnt
,
2873 struct dentry
*dentry
, struct kstat
*stat
)
2875 struct inode
*inode
= dentry
->d_inode
;
2876 generic_fillattr(inode
, stat
);
2877 stat
->blksize
= PAGE_CACHE_SIZE
;
2878 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
2882 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2883 struct inode
* new_dir
,struct dentry
*new_dentry
)
2885 struct btrfs_trans_handle
*trans
;
2886 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2887 struct inode
*new_inode
= new_dentry
->d_inode
;
2888 struct inode
*old_inode
= old_dentry
->d_inode
;
2889 struct timespec ctime
= CURRENT_TIME
;
2892 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2893 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2897 ret
= btrfs_check_free_space(root
, 1, 0);
2901 trans
= btrfs_start_transaction(root
, 1);
2903 btrfs_set_trans_block_group(trans
, new_dir
);
2905 old_dentry
->d_inode
->i_nlink
++;
2906 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2907 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2908 old_inode
->i_ctime
= ctime
;
2910 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2915 new_inode
->i_ctime
= CURRENT_TIME
;
2916 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2920 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
2925 btrfs_end_transaction(trans
, root
);
2930 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2931 const char *symname
)
2933 struct btrfs_trans_handle
*trans
;
2934 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2935 struct btrfs_path
*path
;
2936 struct btrfs_key key
;
2937 struct inode
*inode
= NULL
;
2944 struct btrfs_file_extent_item
*ei
;
2945 struct extent_buffer
*leaf
;
2946 unsigned long nr
= 0;
2948 name_len
= strlen(symname
) + 1;
2949 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2950 return -ENAMETOOLONG
;
2952 err
= btrfs_check_free_space(root
, 1, 0);
2956 trans
= btrfs_start_transaction(root
, 1);
2957 btrfs_set_trans_block_group(trans
, dir
);
2959 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2965 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2967 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2968 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2969 err
= PTR_ERR(inode
);
2973 btrfs_set_trans_block_group(trans
, inode
);
2974 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2978 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2979 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2980 inode
->i_fop
= &btrfs_file_operations
;
2981 inode
->i_op
= &btrfs_file_inode_operations
;
2982 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2983 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2984 inode
->i_mapping
, GFP_NOFS
);
2985 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2986 inode
->i_mapping
, GFP_NOFS
);
2987 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2988 BTRFS_I(inode
)->delalloc_bytes
= 0;
2989 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2991 dir
->i_sb
->s_dirt
= 1;
2992 btrfs_update_inode_block_group(trans
, inode
);
2993 btrfs_update_inode_block_group(trans
, dir
);
2997 path
= btrfs_alloc_path();
2999 key
.objectid
= inode
->i_ino
;
3001 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3002 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3003 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3009 leaf
= path
->nodes
[0];
3010 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3011 struct btrfs_file_extent_item
);
3012 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3013 btrfs_set_file_extent_type(leaf
, ei
,
3014 BTRFS_FILE_EXTENT_INLINE
);
3015 ptr
= btrfs_file_extent_inline_start(ei
);
3016 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3017 btrfs_mark_buffer_dirty(leaf
);
3018 btrfs_free_path(path
);
3020 inode
->i_op
= &btrfs_symlink_inode_operations
;
3021 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3022 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3023 inode
->i_size
= name_len
- 1;
3024 err
= btrfs_update_inode(trans
, root
, inode
);
3029 nr
= trans
->blocks_used
;
3030 btrfs_end_transaction_throttle(trans
, root
);
3033 inode_dec_link_count(inode
);
3036 btrfs_btree_balance_dirty(root
, nr
);
3040 static int btrfs_set_page_dirty(struct page
*page
)
3042 struct inode
*inode
= page
->mapping
->host
;
3043 u64 page_start
= page_offset(page
);
3044 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3046 if (!test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
3047 EXTENT_DELALLOC
, 0)) {
3048 printk("inode %lu page %Lu not delalloc\n", inode
->i_ino
, page_offset(page
));
3051 return __set_page_dirty_nobuffers(page
);
3054 static int btrfs_permission(struct inode
*inode
, int mask
,
3055 struct nameidata
*nd
)
3057 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3059 return generic_permission(inode
, mask
, NULL
);
3062 static struct inode_operations btrfs_dir_inode_operations
= {
3063 .lookup
= btrfs_lookup
,
3064 .create
= btrfs_create
,
3065 .unlink
= btrfs_unlink
,
3067 .mkdir
= btrfs_mkdir
,
3068 .rmdir
= btrfs_rmdir
,
3069 .rename
= btrfs_rename
,
3070 .symlink
= btrfs_symlink
,
3071 .setattr
= btrfs_setattr
,
3072 .mknod
= btrfs_mknod
,
3073 .setxattr
= generic_setxattr
,
3074 .getxattr
= generic_getxattr
,
3075 .listxattr
= btrfs_listxattr
,
3076 .removexattr
= generic_removexattr
,
3077 .permission
= btrfs_permission
,
3079 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3080 .lookup
= btrfs_lookup
,
3081 .permission
= btrfs_permission
,
3083 static struct file_operations btrfs_dir_file_operations
= {
3084 .llseek
= generic_file_llseek
,
3085 .read
= generic_read_dir
,
3086 .readdir
= btrfs_readdir
,
3087 .unlocked_ioctl
= btrfs_ioctl
,
3088 #ifdef CONFIG_COMPAT
3089 .compat_ioctl
= btrfs_ioctl
,
3091 .release
= btrfs_release_file
,
3094 static struct extent_io_ops btrfs_extent_io_ops
= {
3095 .fill_delalloc
= run_delalloc_range
,
3096 .submit_bio_hook
= btrfs_submit_bio_hook
,
3097 .merge_bio_hook
= btrfs_merge_bio_hook
,
3098 .readpage_io_hook
= btrfs_readpage_io_hook
,
3099 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3100 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3101 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3102 .set_bit_hook
= btrfs_set_bit_hook
,
3103 .clear_bit_hook
= btrfs_clear_bit_hook
,
3106 static struct address_space_operations btrfs_aops
= {
3107 .readpage
= btrfs_readpage
,
3108 .writepage
= btrfs_writepage
,
3109 .writepages
= btrfs_writepages
,
3110 .readpages
= btrfs_readpages
,
3111 .sync_page
= block_sync_page
,
3113 .direct_IO
= btrfs_direct_IO
,
3114 .invalidatepage
= btrfs_invalidatepage
,
3115 .releasepage
= btrfs_releasepage
,
3116 .set_page_dirty
= btrfs_set_page_dirty
,
3119 static struct address_space_operations btrfs_symlink_aops
= {
3120 .readpage
= btrfs_readpage
,
3121 .writepage
= btrfs_writepage
,
3122 .invalidatepage
= btrfs_invalidatepage
,
3123 .releasepage
= btrfs_releasepage
,
3126 static struct inode_operations btrfs_file_inode_operations
= {
3127 .truncate
= btrfs_truncate
,
3128 .getattr
= btrfs_getattr
,
3129 .setattr
= btrfs_setattr
,
3130 .setxattr
= generic_setxattr
,
3131 .getxattr
= generic_getxattr
,
3132 .listxattr
= btrfs_listxattr
,
3133 .removexattr
= generic_removexattr
,
3134 .permission
= btrfs_permission
,
3136 static struct inode_operations btrfs_special_inode_operations
= {
3137 .getattr
= btrfs_getattr
,
3138 .setattr
= btrfs_setattr
,
3139 .permission
= btrfs_permission
,
3141 static struct inode_operations btrfs_symlink_inode_operations
= {
3142 .readlink
= generic_readlink
,
3143 .follow_link
= page_follow_link_light
,
3144 .put_link
= page_put_link
,
3145 .permission
= btrfs_permission
,