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>
39 #include <linux/posix_acl.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
47 #include "ordered-data.h"
52 struct btrfs_iget_args
{
54 struct btrfs_root
*root
;
57 static struct inode_operations btrfs_dir_inode_operations
;
58 static struct inode_operations btrfs_symlink_inode_operations
;
59 static struct inode_operations btrfs_dir_ro_inode_operations
;
60 static struct inode_operations btrfs_special_inode_operations
;
61 static struct inode_operations btrfs_file_inode_operations
;
62 static struct address_space_operations btrfs_aops
;
63 static struct address_space_operations btrfs_symlink_aops
;
64 static struct file_operations btrfs_dir_file_operations
;
65 static struct extent_io_ops btrfs_extent_io_ops
;
67 static struct kmem_cache
*btrfs_inode_cachep
;
68 struct kmem_cache
*btrfs_trans_handle_cachep
;
69 struct kmem_cache
*btrfs_transaction_cachep
;
70 struct kmem_cache
*btrfs_bit_radix_cachep
;
71 struct kmem_cache
*btrfs_path_cachep
;
74 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
75 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
76 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
77 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
78 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
79 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
80 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
81 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
84 static void btrfs_truncate(struct inode
*inode
);
87 * a very lame attempt at stopping writes when the FS is 85% full. There
88 * are countless ways this is incorrect, but it is better than nothing.
90 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
99 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
100 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
101 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
109 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
111 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
116 * when extent_io.c finds a delayed allocation range in the file,
117 * the call backs end up in this code. The basic idea is to
118 * allocate extents on disk for the range, and create ordered data structs
119 * in ram to track those extents.
121 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
123 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
124 struct btrfs_trans_handle
*trans
;
128 u64 blocksize
= root
->sectorsize
;
130 struct btrfs_key ins
;
131 struct extent_map
*em
;
132 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
135 trans
= btrfs_join_transaction(root
, 1);
137 btrfs_set_trans_block_group(trans
, inode
);
139 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
140 num_bytes
= max(blocksize
, num_bytes
);
141 orig_num_bytes
= num_bytes
;
143 if (alloc_hint
== EXTENT_MAP_INLINE
)
146 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
147 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
148 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
149 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
151 while(num_bytes
> 0) {
152 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
153 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
154 root
->sectorsize
, 0, alloc_hint
,
160 em
= alloc_extent_map(GFP_NOFS
);
162 em
->len
= ins
.offset
;
163 em
->block_start
= ins
.objectid
;
164 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
165 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
166 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
168 spin_lock(&em_tree
->lock
);
169 ret
= add_extent_mapping(em_tree
, em
);
170 spin_unlock(&em_tree
->lock
);
171 if (ret
!= -EEXIST
) {
175 btrfs_drop_extent_cache(inode
, start
,
176 start
+ ins
.offset
- 1, 0);
178 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
180 cur_alloc_size
= ins
.offset
;
181 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
184 if (num_bytes
< cur_alloc_size
) {
185 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
189 num_bytes
-= cur_alloc_size
;
190 alloc_hint
= ins
.objectid
+ ins
.offset
;
191 start
+= cur_alloc_size
;
194 btrfs_end_transaction(trans
, root
);
199 * when nowcow writeback call back. This checks for snapshots or COW copies
200 * of the extents that exist in the file, and COWs the file as required.
202 * If no cow copies or snapshots exist, we write directly to the existing
205 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
212 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
213 struct btrfs_block_group_cache
*block_group
;
214 struct btrfs_trans_handle
*trans
;
215 struct extent_buffer
*leaf
;
217 struct btrfs_path
*path
;
218 struct btrfs_file_extent_item
*item
;
221 struct btrfs_key found_key
;
223 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
224 path
= btrfs_alloc_path();
226 trans
= btrfs_join_transaction(root
, 1);
229 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
230 inode
->i_ino
, start
, 0);
237 if (path
->slots
[0] == 0)
242 leaf
= path
->nodes
[0];
243 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
244 struct btrfs_file_extent_item
);
246 /* are we inside the extent that was found? */
247 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
248 found_type
= btrfs_key_type(&found_key
);
249 if (found_key
.objectid
!= inode
->i_ino
||
250 found_type
!= BTRFS_EXTENT_DATA_KEY
)
253 found_type
= btrfs_file_extent_type(leaf
, item
);
254 extent_start
= found_key
.offset
;
255 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
256 u64 extent_num_bytes
;
258 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
259 extent_end
= extent_start
+ extent_num_bytes
;
262 if (loops
&& start
!= extent_start
)
265 if (start
< extent_start
|| start
>= extent_end
)
268 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
272 if (btrfs_cross_ref_exists(trans
, root
, &found_key
, bytenr
))
275 * we may be called by the resizer, make sure we're inside
276 * the limits of the FS
278 block_group
= btrfs_lookup_block_group(root
->fs_info
,
280 if (!block_group
|| block_group
->ro
)
283 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
284 extent_num_bytes
= min(end
+ 1, extent_end
) - start
;
285 ret
= btrfs_add_ordered_extent(inode
, start
, bytenr
,
286 extent_num_bytes
, 1);
292 btrfs_release_path(root
, path
);
300 btrfs_end_transaction(trans
, root
);
301 btrfs_free_path(path
);
302 return cow_file_range(inode
, start
, end
);
306 btrfs_end_transaction(trans
, root
);
307 btrfs_free_path(path
);
312 * extent_io.c call back to do delayed allocation processing
314 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
316 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
319 if (btrfs_test_opt(root
, NODATACOW
) ||
320 btrfs_test_flag(inode
, NODATACOW
))
321 ret
= run_delalloc_nocow(inode
, start
, end
);
323 ret
= cow_file_range(inode
, start
, end
);
329 * extent_io.c set_bit_hook, used to track delayed allocation
330 * bytes in this file, and to maintain the list of inodes that
331 * have pending delalloc work to be done.
333 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
334 unsigned long old
, unsigned long bits
)
337 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
338 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
339 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
340 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
341 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
342 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
343 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
344 &root
->fs_info
->delalloc_inodes
);
346 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
352 * extent_io.c clear_bit_hook, see set_bit_hook for why
354 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
355 unsigned long old
, unsigned long bits
)
357 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
358 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
361 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
362 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
363 printk("warning: delalloc account %Lu %Lu\n",
364 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
365 root
->fs_info
->delalloc_bytes
= 0;
366 BTRFS_I(inode
)->delalloc_bytes
= 0;
368 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
369 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
371 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
372 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
373 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
375 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
381 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
382 * we don't create bios that span stripes or chunks
384 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
385 size_t size
, struct bio
*bio
)
387 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
388 struct btrfs_mapping_tree
*map_tree
;
389 u64 logical
= (u64
)bio
->bi_sector
<< 9;
394 length
= bio
->bi_size
;
395 map_tree
= &root
->fs_info
->mapping_tree
;
397 ret
= btrfs_map_block(map_tree
, READ
, logical
,
398 &map_length
, NULL
, 0);
400 if (map_length
< length
+ size
) {
407 * in order to insert checksums into the metadata in large chunks,
408 * we wait until bio submission time. All the pages in the bio are
409 * checksummed and sums are attached onto the ordered extent record.
411 * At IO completion time the cums attached on the ordered extent record
412 * are inserted into the btree
414 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
417 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
420 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
423 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
427 * extent_io.c submission hook. This does the right thing for csum calculation on write,
428 * or reading the csums from the tree before a read
430 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
433 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
436 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
439 if (btrfs_test_opt(root
, NODATASUM
) ||
440 btrfs_test_flag(inode
, NODATASUM
)) {
444 if (!(rw
& (1 << BIO_RW
))) {
445 btrfs_lookup_bio_sums(root
, inode
, bio
);
448 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
449 inode
, rw
, bio
, mirror_num
,
450 __btrfs_submit_bio_hook
);
452 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
456 * given a list of ordered sums record them in the inode. This happens
457 * at IO completion time based on sums calculated at bio submission time.
459 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
460 struct inode
*inode
, u64 file_offset
,
461 struct list_head
*list
)
463 struct list_head
*cur
;
464 struct btrfs_ordered_sum
*sum
;
466 btrfs_set_trans_block_group(trans
, inode
);
467 list_for_each(cur
, list
) {
468 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
469 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
475 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
)
477 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
481 /* see btrfs_writepage_start_hook for details on why this is required */
482 struct btrfs_writepage_fixup
{
484 struct btrfs_work work
;
487 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
489 struct btrfs_writepage_fixup
*fixup
;
490 struct btrfs_ordered_extent
*ordered
;
496 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
500 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
501 ClearPageChecked(page
);
505 inode
= page
->mapping
->host
;
506 page_start
= page_offset(page
);
507 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
509 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
511 /* already ordered? We're done */
512 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
513 EXTENT_ORDERED
, 0)) {
517 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
519 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
522 btrfs_start_ordered_extent(inode
, ordered
, 1);
526 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
527 ClearPageChecked(page
);
529 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
532 page_cache_release(page
);
536 * There are a few paths in the higher layers of the kernel that directly
537 * set the page dirty bit without asking the filesystem if it is a
538 * good idea. This causes problems because we want to make sure COW
539 * properly happens and the data=ordered rules are followed.
541 * In our case any range that doesn't have the EXTENT_ORDERED bit set
542 * hasn't been properly setup for IO. We kick off an async process
543 * to fix it up. The async helper will wait for ordered extents, set
544 * the delalloc bit and make it safe to write the page.
546 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
548 struct inode
*inode
= page
->mapping
->host
;
549 struct btrfs_writepage_fixup
*fixup
;
550 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
553 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
558 if (PageChecked(page
))
561 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
565 SetPageChecked(page
);
566 page_cache_get(page
);
567 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
569 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
573 /* as ordered data IO finishes, this gets called so we can finish
574 * an ordered extent if the range of bytes in the file it covers are
577 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
579 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
580 struct btrfs_trans_handle
*trans
;
581 struct btrfs_ordered_extent
*ordered_extent
;
582 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
583 struct btrfs_file_extent_item
*extent_item
;
584 struct btrfs_path
*path
= NULL
;
585 struct extent_buffer
*leaf
;
587 struct list_head list
;
588 struct btrfs_key ins
;
591 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
595 trans
= btrfs_join_transaction(root
, 1);
597 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
598 BUG_ON(!ordered_extent
);
599 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
))
602 path
= btrfs_alloc_path();
605 lock_extent(io_tree
, ordered_extent
->file_offset
,
606 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
609 INIT_LIST_HEAD(&list
);
611 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
613 ret
= btrfs_drop_extents(trans
, root
, inode
,
614 ordered_extent
->file_offset
,
615 ordered_extent
->file_offset
+
617 ordered_extent
->file_offset
, &alloc_hint
);
620 ins
.objectid
= inode
->i_ino
;
621 ins
.offset
= ordered_extent
->file_offset
;
622 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
623 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
624 sizeof(*extent_item
));
626 leaf
= path
->nodes
[0];
627 extent_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
628 struct btrfs_file_extent_item
);
629 btrfs_set_file_extent_generation(leaf
, extent_item
, trans
->transid
);
630 btrfs_set_file_extent_type(leaf
, extent_item
, BTRFS_FILE_EXTENT_REG
);
631 btrfs_set_file_extent_disk_bytenr(leaf
, extent_item
,
632 ordered_extent
->start
);
633 btrfs_set_file_extent_disk_num_bytes(leaf
, extent_item
,
634 ordered_extent
->len
);
635 btrfs_set_file_extent_offset(leaf
, extent_item
, 0);
636 btrfs_set_file_extent_num_bytes(leaf
, extent_item
,
637 ordered_extent
->len
);
638 btrfs_mark_buffer_dirty(leaf
);
640 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
641 ordered_extent
->file_offset
+
642 ordered_extent
->len
- 1, 0);
643 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
645 ins
.objectid
= ordered_extent
->start
;
646 ins
.offset
= ordered_extent
->len
;
647 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
648 ret
= btrfs_alloc_reserved_extent(trans
, root
, leaf
->start
,
649 root
->root_key
.objectid
,
650 trans
->transid
, inode
->i_ino
, &ins
);
652 btrfs_release_path(root
, path
);
654 inode_add_bytes(inode
, ordered_extent
->len
);
655 unlock_extent(io_tree
, ordered_extent
->file_offset
,
656 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
659 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
660 &ordered_extent
->list
);
662 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
663 btrfs_ordered_update_i_size(inode
, ordered_extent
);
664 btrfs_update_inode(trans
, root
, inode
);
665 btrfs_remove_ordered_extent(inode
, ordered_extent
);
666 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
669 btrfs_put_ordered_extent(ordered_extent
);
670 /* once for the tree */
671 btrfs_put_ordered_extent(ordered_extent
);
673 btrfs_end_transaction(trans
, root
);
675 btrfs_free_path(path
);
679 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
680 struct extent_state
*state
, int uptodate
)
682 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
686 * When IO fails, either with EIO or csum verification fails, we
687 * try other mirrors that might have a good copy of the data. This
688 * io_failure_record is used to record state as we go through all the
689 * mirrors. If another mirror has good data, the page is set up to date
690 * and things continue. If a good mirror can't be found, the original
691 * bio end_io callback is called to indicate things have failed.
693 struct io_failure_record
{
701 int btrfs_io_failed_hook(struct bio
*failed_bio
,
702 struct page
*page
, u64 start
, u64 end
,
703 struct extent_state
*state
)
705 struct io_failure_record
*failrec
= NULL
;
707 struct extent_map
*em
;
708 struct inode
*inode
= page
->mapping
->host
;
709 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
710 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
717 ret
= get_state_private(failure_tree
, start
, &private);
719 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
722 failrec
->start
= start
;
723 failrec
->len
= end
- start
+ 1;
724 failrec
->last_mirror
= 0;
726 spin_lock(&em_tree
->lock
);
727 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
728 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
732 spin_unlock(&em_tree
->lock
);
734 if (!em
|| IS_ERR(em
)) {
738 logical
= start
- em
->start
;
739 logical
= em
->block_start
+ logical
;
740 failrec
->logical
= logical
;
742 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
743 EXTENT_DIRTY
, GFP_NOFS
);
744 set_state_private(failure_tree
, start
,
745 (u64
)(unsigned long)failrec
);
747 failrec
= (struct io_failure_record
*)(unsigned long)private;
749 num_copies
= btrfs_num_copies(
750 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
751 failrec
->logical
, failrec
->len
);
752 failrec
->last_mirror
++;
754 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
755 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
758 if (state
&& state
->start
!= failrec
->start
)
760 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
762 if (!state
|| failrec
->last_mirror
> num_copies
) {
763 set_state_private(failure_tree
, failrec
->start
, 0);
764 clear_extent_bits(failure_tree
, failrec
->start
,
765 failrec
->start
+ failrec
->len
- 1,
766 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
770 bio
= bio_alloc(GFP_NOFS
, 1);
771 bio
->bi_private
= state
;
772 bio
->bi_end_io
= failed_bio
->bi_end_io
;
773 bio
->bi_sector
= failrec
->logical
>> 9;
774 bio
->bi_bdev
= failed_bio
->bi_bdev
;
776 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
777 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
782 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
783 failrec
->last_mirror
);
788 * each time an IO finishes, we do a fast check in the IO failure tree
789 * to see if we need to process or clean up an io_failure_record
791 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
795 struct io_failure_record
*failure
;
799 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
800 (u64
)-1, 1, EXTENT_DIRTY
)) {
801 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
802 start
, &private_failure
);
804 failure
= (struct io_failure_record
*)(unsigned long)
806 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
808 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
810 failure
->start
+ failure
->len
- 1,
811 EXTENT_DIRTY
| EXTENT_LOCKED
,
820 * when reads are done, we need to check csums to verify the data is correct
821 * if there's a match, we allow the bio to finish. If not, we go through
822 * the io_failure_record routines to find good copies
824 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
825 struct extent_state
*state
)
827 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
828 struct inode
*inode
= page
->mapping
->host
;
829 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
831 u64
private = ~(u32
)0;
833 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
837 if (btrfs_test_opt(root
, NODATASUM
) ||
838 btrfs_test_flag(inode
, NODATASUM
))
840 if (state
&& state
->start
== start
) {
841 private = state
->private;
844 ret
= get_state_private(io_tree
, start
, &private);
846 local_irq_save(flags
);
847 kaddr
= kmap_atomic(page
, KM_IRQ0
);
851 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
852 btrfs_csum_final(csum
, (char *)&csum
);
853 if (csum
!= private) {
856 kunmap_atomic(kaddr
, KM_IRQ0
);
857 local_irq_restore(flags
);
859 /* if the io failure tree for this inode is non-empty,
860 * check to see if we've recovered from a failed IO
862 btrfs_clean_io_failures(inode
, start
);
866 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
867 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
869 memset(kaddr
+ offset
, 1, end
- start
+ 1);
870 flush_dcache_page(page
);
871 kunmap_atomic(kaddr
, KM_IRQ0
);
872 local_irq_restore(flags
);
879 * This creates an orphan entry for the given inode in case something goes
880 * wrong in the middle of an unlink/truncate.
882 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
884 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
887 spin_lock(&root
->list_lock
);
889 /* already on the orphan list, we're good */
890 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
891 spin_unlock(&root
->list_lock
);
895 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
897 spin_unlock(&root
->list_lock
);
900 * insert an orphan item to track this unlinked/truncated file
902 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
908 * We have done the truncate/delete so we can go ahead and remove the orphan
909 * item for this particular inode.
911 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
913 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
916 spin_lock(&root
->list_lock
);
918 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
919 spin_unlock(&root
->list_lock
);
923 list_del_init(&BTRFS_I(inode
)->i_orphan
);
925 spin_unlock(&root
->list_lock
);
929 spin_unlock(&root
->list_lock
);
931 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
937 * this cleans up any orphans that may be left on the list from the last use
940 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
942 struct btrfs_path
*path
;
943 struct extent_buffer
*leaf
;
944 struct btrfs_item
*item
;
945 struct btrfs_key key
, found_key
;
946 struct btrfs_trans_handle
*trans
;
948 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
950 /* don't do orphan cleanup if the fs is readonly. */
951 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
954 path
= btrfs_alloc_path();
959 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
960 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
961 key
.offset
= (u64
)-1;
965 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
967 printk(KERN_ERR
"Error searching slot for orphan: %d"
973 * if ret == 0 means we found what we were searching for, which
974 * is weird, but possible, so only screw with path if we didnt
975 * find the key and see if we have stuff that matches
978 if (path
->slots
[0] == 0)
983 /* pull out the item */
984 leaf
= path
->nodes
[0];
985 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
986 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
988 /* make sure the item matches what we want */
989 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
991 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
994 /* release the path since we're done with it */
995 btrfs_release_path(root
, path
);
998 * this is where we are basically btrfs_lookup, without the
999 * crossing root thing. we store the inode number in the
1000 * offset of the orphan item.
1002 inode
= btrfs_iget_locked(root
->fs_info
->sb
,
1003 found_key
.offset
, root
);
1007 if (inode
->i_state
& I_NEW
) {
1008 BTRFS_I(inode
)->root
= root
;
1010 /* have to set the location manually */
1011 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
1012 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
1013 BTRFS_I(inode
)->location
.offset
= 0;
1015 btrfs_read_locked_inode(inode
);
1016 unlock_new_inode(inode
);
1020 * add this inode to the orphan list so btrfs_orphan_del does
1021 * the proper thing when we hit it
1023 spin_lock(&root
->list_lock
);
1024 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
1025 spin_unlock(&root
->list_lock
);
1028 * if this is a bad inode, means we actually succeeded in
1029 * removing the inode, but not the orphan record, which means
1030 * we need to manually delete the orphan since iput will just
1031 * do a destroy_inode
1033 if (is_bad_inode(inode
)) {
1034 trans
= btrfs_start_transaction(root
, 1);
1035 btrfs_orphan_del(trans
, inode
);
1036 btrfs_end_transaction(trans
, root
);
1041 /* if we have links, this was a truncate, lets do that */
1042 if (inode
->i_nlink
) {
1044 btrfs_truncate(inode
);
1049 /* this will do delete_inode and everything for us */
1054 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
1056 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
1058 btrfs_free_path(path
);
1062 * read an inode from the btree into the in-memory inode
1064 void btrfs_read_locked_inode(struct inode
*inode
)
1066 struct btrfs_path
*path
;
1067 struct extent_buffer
*leaf
;
1068 struct btrfs_inode_item
*inode_item
;
1069 struct btrfs_timespec
*tspec
;
1070 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1071 struct btrfs_key location
;
1072 u64 alloc_group_block
;
1076 path
= btrfs_alloc_path();
1078 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
1080 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
1084 leaf
= path
->nodes
[0];
1085 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1086 struct btrfs_inode_item
);
1088 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
1089 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
1090 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
1091 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
1092 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
1094 tspec
= btrfs_inode_atime(inode_item
);
1095 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1096 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1098 tspec
= btrfs_inode_mtime(inode_item
);
1099 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1100 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1102 tspec
= btrfs_inode_ctime(inode_item
);
1103 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1104 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1106 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
1107 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
1108 inode
->i_generation
= BTRFS_I(inode
)->generation
;
1110 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
1112 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1114 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
1115 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
1117 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
1118 if (!BTRFS_I(inode
)->block_group
) {
1119 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
1121 BTRFS_BLOCK_GROUP_METADATA
, 0);
1123 btrfs_free_path(path
);
1126 switch (inode
->i_mode
& S_IFMT
) {
1128 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1129 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1130 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1131 inode
->i_fop
= &btrfs_file_operations
;
1132 inode
->i_op
= &btrfs_file_inode_operations
;
1135 inode
->i_fop
= &btrfs_dir_file_operations
;
1136 if (root
== root
->fs_info
->tree_root
)
1137 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
1139 inode
->i_op
= &btrfs_dir_inode_operations
;
1142 inode
->i_op
= &btrfs_symlink_inode_operations
;
1143 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
1144 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1147 init_special_inode(inode
, inode
->i_mode
, rdev
);
1153 btrfs_free_path(path
);
1154 make_bad_inode(inode
);
1158 * given a leaf and an inode, copy the inode fields into the leaf
1160 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
1161 struct extent_buffer
*leaf
,
1162 struct btrfs_inode_item
*item
,
1163 struct inode
*inode
)
1165 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
1166 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
1167 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
1168 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
1169 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
1171 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
1172 inode
->i_atime
.tv_sec
);
1173 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
1174 inode
->i_atime
.tv_nsec
);
1176 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
1177 inode
->i_mtime
.tv_sec
);
1178 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
1179 inode
->i_mtime
.tv_nsec
);
1181 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
1182 inode
->i_ctime
.tv_sec
);
1183 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
1184 inode
->i_ctime
.tv_nsec
);
1186 btrfs_set_inode_nbytes(leaf
, item
, inode_get_bytes(inode
));
1187 btrfs_set_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
);
1188 btrfs_set_inode_transid(leaf
, item
, trans
->transid
);
1189 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
1190 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
1191 btrfs_set_inode_block_group(leaf
, item
,
1192 BTRFS_I(inode
)->block_group
->key
.objectid
);
1196 * copy everything in the in-memory inode into the btree.
1198 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
1199 struct btrfs_root
*root
,
1200 struct inode
*inode
)
1202 struct btrfs_inode_item
*inode_item
;
1203 struct btrfs_path
*path
;
1204 struct extent_buffer
*leaf
;
1207 path
= btrfs_alloc_path();
1209 ret
= btrfs_lookup_inode(trans
, root
, path
,
1210 &BTRFS_I(inode
)->location
, 1);
1217 leaf
= path
->nodes
[0];
1218 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1219 struct btrfs_inode_item
);
1221 fill_inode_item(trans
, leaf
, inode_item
, inode
);
1222 btrfs_mark_buffer_dirty(leaf
);
1223 btrfs_set_inode_last_trans(trans
, inode
);
1226 btrfs_free_path(path
);
1232 * unlink helper that gets used here in inode.c and in the tree logging
1233 * recovery code. It remove a link in a directory with a given name, and
1234 * also drops the back refs in the inode to the directory
1236 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
1237 struct btrfs_root
*root
,
1238 struct inode
*dir
, struct inode
*inode
,
1239 const char *name
, int name_len
)
1241 struct btrfs_path
*path
;
1243 struct extent_buffer
*leaf
;
1244 struct btrfs_dir_item
*di
;
1245 struct btrfs_key key
;
1248 path
= btrfs_alloc_path();
1254 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1255 name
, name_len
, -1);
1264 leaf
= path
->nodes
[0];
1265 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1266 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1269 btrfs_release_path(root
, path
);
1271 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1273 dir
->i_ino
, &index
);
1275 printk("failed to delete reference to %.*s, "
1276 "inode %lu parent %lu\n", name_len
, name
,
1277 inode
->i_ino
, dir
->i_ino
);
1281 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1282 index
, name
, name_len
, -1);
1291 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1292 btrfs_release_path(root
, path
);
1294 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
1296 BUG_ON(ret
!= 0 && ret
!= -ENOENT
);
1298 BTRFS_I(dir
)->log_dirty_trans
= trans
->transid
;
1300 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
1304 btrfs_free_path(path
);
1308 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1309 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1310 btrfs_update_inode(trans
, root
, dir
);
1311 btrfs_drop_nlink(inode
);
1312 ret
= btrfs_update_inode(trans
, root
, inode
);
1313 dir
->i_sb
->s_dirt
= 1;
1318 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1320 struct btrfs_root
*root
;
1321 struct btrfs_trans_handle
*trans
;
1322 struct inode
*inode
= dentry
->d_inode
;
1324 unsigned long nr
= 0;
1326 root
= BTRFS_I(dir
)->root
;
1328 ret
= btrfs_check_free_space(root
, 1, 1);
1332 trans
= btrfs_start_transaction(root
, 1);
1334 btrfs_set_trans_block_group(trans
, dir
);
1335 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
1336 dentry
->d_name
.name
, dentry
->d_name
.len
);
1338 if (inode
->i_nlink
== 0)
1339 ret
= btrfs_orphan_add(trans
, inode
);
1341 nr
= trans
->blocks_used
;
1343 btrfs_end_transaction_throttle(trans
, root
);
1345 btrfs_btree_balance_dirty(root
, nr
);
1349 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1351 struct inode
*inode
= dentry
->d_inode
;
1354 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1355 struct btrfs_trans_handle
*trans
;
1356 unsigned long nr
= 0;
1358 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1362 ret
= btrfs_check_free_space(root
, 1, 1);
1366 trans
= btrfs_start_transaction(root
, 1);
1367 btrfs_set_trans_block_group(trans
, dir
);
1369 err
= btrfs_orphan_add(trans
, inode
);
1373 /* now the directory is empty */
1374 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
1375 dentry
->d_name
.name
, dentry
->d_name
.len
);
1377 btrfs_i_size_write(inode
, 0);
1381 nr
= trans
->blocks_used
;
1382 ret
= btrfs_end_transaction_throttle(trans
, root
);
1384 btrfs_btree_balance_dirty(root
, nr
);
1392 * when truncating bytes in a file, it is possible to avoid reading
1393 * the leaves that contain only checksum items. This can be the
1394 * majority of the IO required to delete a large file, but it must
1395 * be done carefully.
1397 * The keys in the level just above the leaves are checked to make sure
1398 * the lowest key in a given leaf is a csum key, and starts at an offset
1399 * after the new size.
1401 * Then the key for the next leaf is checked to make sure it also has
1402 * a checksum item for the same file. If it does, we know our target leaf
1403 * contains only checksum items, and it can be safely freed without reading
1406 * This is just an optimization targeted at large files. It may do
1407 * nothing. It will return 0 unless things went badly.
1409 static noinline
int drop_csum_leaves(struct btrfs_trans_handle
*trans
,
1410 struct btrfs_root
*root
,
1411 struct btrfs_path
*path
,
1412 struct inode
*inode
, u64 new_size
)
1414 struct btrfs_key key
;
1417 struct btrfs_key found_key
;
1418 struct btrfs_key other_key
;
1420 path
->lowest_level
= 1;
1421 key
.objectid
= inode
->i_ino
;
1422 key
.type
= BTRFS_CSUM_ITEM_KEY
;
1423 key
.offset
= new_size
;
1425 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1429 if (path
->nodes
[1] == NULL
) {
1434 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, path
->slots
[1]);
1435 nritems
= btrfs_header_nritems(path
->nodes
[1]);
1440 if (path
->slots
[1] >= nritems
)
1443 /* did we find a key greater than anything we want to delete? */
1444 if (found_key
.objectid
> inode
->i_ino
||
1445 (found_key
.objectid
== inode
->i_ino
&& found_key
.type
> key
.type
))
1448 /* we check the next key in the node to make sure the leave contains
1449 * only checksum items. This comparison doesn't work if our
1450 * leaf is the last one in the node
1452 if (path
->slots
[1] + 1 >= nritems
) {
1454 /* search forward from the last key in the node, this
1455 * will bring us into the next node in the tree
1457 btrfs_node_key_to_cpu(path
->nodes
[1], &found_key
, nritems
- 1);
1459 /* unlikely, but we inc below, so check to be safe */
1460 if (found_key
.offset
== (u64
)-1)
1463 /* search_forward needs a path with locks held, do the
1464 * search again for the original key. It is possible
1465 * this will race with a balance and return a path that
1466 * we could modify, but this drop is just an optimization
1467 * and is allowed to miss some leaves.
1469 btrfs_release_path(root
, path
);
1472 /* setup a max key for search_forward */
1473 other_key
.offset
= (u64
)-1;
1474 other_key
.type
= key
.type
;
1475 other_key
.objectid
= key
.objectid
;
1477 path
->keep_locks
= 1;
1478 ret
= btrfs_search_forward(root
, &found_key
, &other_key
,
1480 path
->keep_locks
= 0;
1481 if (ret
|| found_key
.objectid
!= key
.objectid
||
1482 found_key
.type
!= key
.type
) {
1487 key
.offset
= found_key
.offset
;
1488 btrfs_release_path(root
, path
);
1493 /* we know there's one more slot after us in the tree,
1494 * read that key so we can verify it is also a checksum item
1496 btrfs_node_key_to_cpu(path
->nodes
[1], &other_key
, path
->slots
[1] + 1);
1498 if (found_key
.objectid
< inode
->i_ino
)
1501 if (found_key
.type
!= key
.type
|| found_key
.offset
< new_size
)
1505 * if the key for the next leaf isn't a csum key from this objectid,
1506 * we can't be sure there aren't good items inside this leaf.
1509 if (other_key
.objectid
!= inode
->i_ino
|| other_key
.type
!= key
.type
)
1513 * it is safe to delete this leaf, it contains only
1514 * csum items from this inode at an offset >= new_size
1516 ret
= btrfs_del_leaf(trans
, root
, path
,
1517 btrfs_node_blockptr(path
->nodes
[1],
1522 btrfs_release_path(root
, path
);
1524 if (other_key
.objectid
== inode
->i_ino
&&
1525 other_key
.type
== key
.type
&& other_key
.offset
> key
.offset
) {
1526 key
.offset
= other_key
.offset
;
1532 /* fixup any changes we've made to the path */
1533 path
->lowest_level
= 0;
1534 path
->keep_locks
= 0;
1535 btrfs_release_path(root
, path
);
1540 * this can truncate away extent items, csum items and directory items.
1541 * It starts at a high offset and removes keys until it can't find
1542 * any higher than new_size
1544 * csum items that cross the new i_size are truncated to the new size
1547 * min_type is the minimum key type to truncate down to. If set to 0, this
1548 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1550 noinline
int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
1551 struct btrfs_root
*root
,
1552 struct inode
*inode
,
1553 u64 new_size
, u32 min_type
)
1556 struct btrfs_path
*path
;
1557 struct btrfs_key key
;
1558 struct btrfs_key found_key
;
1560 struct extent_buffer
*leaf
;
1561 struct btrfs_file_extent_item
*fi
;
1562 u64 extent_start
= 0;
1563 u64 extent_num_bytes
= 0;
1569 int pending_del_nr
= 0;
1570 int pending_del_slot
= 0;
1571 int extent_type
= -1;
1572 u64 mask
= root
->sectorsize
- 1;
1575 btrfs_drop_extent_cache(inode
, new_size
& (~mask
), (u64
)-1, 0);
1576 path
= btrfs_alloc_path();
1580 /* FIXME, add redo link to tree so we don't leak on crash */
1581 key
.objectid
= inode
->i_ino
;
1582 key
.offset
= (u64
)-1;
1585 btrfs_init_path(path
);
1587 ret
= drop_csum_leaves(trans
, root
, path
, inode
, new_size
);
1591 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1596 /* there are no items in the tree for us to truncate, we're
1599 if (path
->slots
[0] == 0) {
1608 leaf
= path
->nodes
[0];
1609 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1610 found_type
= btrfs_key_type(&found_key
);
1612 if (found_key
.objectid
!= inode
->i_ino
)
1615 if (found_type
< min_type
)
1618 item_end
= found_key
.offset
;
1619 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1620 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1621 struct btrfs_file_extent_item
);
1622 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1623 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1625 btrfs_file_extent_num_bytes(leaf
, fi
);
1626 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1627 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1629 item_end
+= btrfs_file_extent_inline_len(leaf
,
1634 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1635 ret
= btrfs_csum_truncate(trans
, root
, path
,
1639 if (item_end
< new_size
) {
1640 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1641 found_type
= BTRFS_INODE_ITEM_KEY
;
1642 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1643 found_type
= BTRFS_CSUM_ITEM_KEY
;
1644 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1645 found_type
= BTRFS_XATTR_ITEM_KEY
;
1646 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1647 found_type
= BTRFS_INODE_REF_KEY
;
1648 } else if (found_type
) {
1653 btrfs_set_key_type(&key
, found_type
);
1656 if (found_key
.offset
>= new_size
)
1662 /* FIXME, shrink the extent if the ref count is only 1 */
1663 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1666 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1668 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1670 u64 orig_num_bytes
=
1671 btrfs_file_extent_num_bytes(leaf
, fi
);
1672 extent_num_bytes
= new_size
-
1673 found_key
.offset
+ root
->sectorsize
- 1;
1674 extent_num_bytes
= extent_num_bytes
&
1675 ~((u64
)root
->sectorsize
- 1);
1676 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1678 num_dec
= (orig_num_bytes
-
1680 if (root
->ref_cows
&& extent_start
!= 0)
1681 inode_sub_bytes(inode
, num_dec
);
1682 btrfs_mark_buffer_dirty(leaf
);
1685 btrfs_file_extent_disk_num_bytes(leaf
,
1687 /* FIXME blocksize != 4096 */
1688 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1689 if (extent_start
!= 0) {
1692 inode_sub_bytes(inode
, num_dec
);
1694 root_gen
= btrfs_header_generation(leaf
);
1695 root_owner
= btrfs_header_owner(leaf
);
1697 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1699 u32 size
= new_size
- found_key
.offset
;
1701 if (root
->ref_cows
) {
1702 inode_sub_bytes(inode
, item_end
+ 1 -
1706 btrfs_file_extent_calc_inline_size(size
);
1707 ret
= btrfs_truncate_item(trans
, root
, path
,
1710 } else if (root
->ref_cows
) {
1711 inode_sub_bytes(inode
, item_end
+ 1 -
1717 if (!pending_del_nr
) {
1718 /* no pending yet, add ourselves */
1719 pending_del_slot
= path
->slots
[0];
1721 } else if (pending_del_nr
&&
1722 path
->slots
[0] + 1 == pending_del_slot
) {
1723 /* hop on the pending chunk */
1725 pending_del_slot
= path
->slots
[0];
1727 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1733 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1735 leaf
->start
, root_owner
,
1736 root_gen
, inode
->i_ino
, 0);
1740 if (path
->slots
[0] == 0) {
1743 btrfs_release_path(root
, path
);
1748 if (pending_del_nr
&&
1749 path
->slots
[0] + 1 != pending_del_slot
) {
1750 struct btrfs_key debug
;
1752 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1754 ret
= btrfs_del_items(trans
, root
, path
,
1759 btrfs_release_path(root
, path
);
1765 if (pending_del_nr
) {
1766 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1769 btrfs_free_path(path
);
1770 inode
->i_sb
->s_dirt
= 1;
1775 * taken from block_truncate_page, but does cow as it zeros out
1776 * any bytes left in the last page in the file.
1778 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1780 struct inode
*inode
= mapping
->host
;
1781 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1782 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1783 struct btrfs_ordered_extent
*ordered
;
1785 u32 blocksize
= root
->sectorsize
;
1786 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1787 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1793 if ((offset
& (blocksize
- 1)) == 0)
1798 page
= grab_cache_page(mapping
, index
);
1802 page_start
= page_offset(page
);
1803 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1805 if (!PageUptodate(page
)) {
1806 ret
= btrfs_readpage(NULL
, page
);
1808 if (page
->mapping
!= mapping
) {
1810 page_cache_release(page
);
1813 if (!PageUptodate(page
)) {
1818 wait_on_page_writeback(page
);
1820 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1821 set_page_extent_mapped(page
);
1823 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1825 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1827 page_cache_release(page
);
1828 btrfs_start_ordered_extent(inode
, ordered
, 1);
1829 btrfs_put_ordered_extent(ordered
);
1833 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
1835 if (offset
!= PAGE_CACHE_SIZE
) {
1837 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1838 flush_dcache_page(page
);
1841 ClearPageChecked(page
);
1842 set_page_dirty(page
);
1843 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1847 page_cache_release(page
);
1852 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1854 struct inode
*inode
= dentry
->d_inode
;
1857 err
= inode_change_ok(inode
, attr
);
1861 if (S_ISREG(inode
->i_mode
) &&
1862 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1863 struct btrfs_trans_handle
*trans
;
1864 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1865 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1867 u64 mask
= root
->sectorsize
- 1;
1868 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1869 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1873 if (attr
->ia_size
<= hole_start
)
1876 err
= btrfs_check_free_space(root
, 1, 0);
1880 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1882 hole_size
= block_end
- hole_start
;
1884 struct btrfs_ordered_extent
*ordered
;
1885 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1887 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1888 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
1890 unlock_extent(io_tree
, hole_start
,
1891 block_end
- 1, GFP_NOFS
);
1892 btrfs_put_ordered_extent(ordered
);
1898 trans
= btrfs_start_transaction(root
, 1);
1899 btrfs_set_trans_block_group(trans
, inode
);
1900 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1901 err
= btrfs_drop_extents(trans
, root
, inode
,
1902 hole_start
, block_end
, hole_start
,
1905 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1906 err
= btrfs_insert_file_extent(trans
, root
,
1910 btrfs_drop_extent_cache(inode
, hole_start
,
1912 btrfs_check_file(root
, inode
);
1914 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1915 btrfs_end_transaction(trans
, root
);
1916 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1921 err
= inode_setattr(inode
, attr
);
1923 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
1924 err
= btrfs_acl_chmod(inode
);
1929 void btrfs_delete_inode(struct inode
*inode
)
1931 struct btrfs_trans_handle
*trans
;
1932 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1936 truncate_inode_pages(&inode
->i_data
, 0);
1937 if (is_bad_inode(inode
)) {
1938 btrfs_orphan_del(NULL
, inode
);
1941 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1943 btrfs_i_size_write(inode
, 0);
1944 trans
= btrfs_start_transaction(root
, 1);
1946 btrfs_set_trans_block_group(trans
, inode
);
1947 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, inode
->i_size
, 0);
1949 btrfs_orphan_del(NULL
, inode
);
1950 goto no_delete_lock
;
1953 btrfs_orphan_del(trans
, inode
);
1955 nr
= trans
->blocks_used
;
1958 btrfs_end_transaction(trans
, root
);
1959 btrfs_btree_balance_dirty(root
, nr
);
1963 nr
= trans
->blocks_used
;
1964 btrfs_end_transaction(trans
, root
);
1965 btrfs_btree_balance_dirty(root
, nr
);
1971 * this returns the key found in the dir entry in the location pointer.
1972 * If no dir entries were found, location->objectid is 0.
1974 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1975 struct btrfs_key
*location
)
1977 const char *name
= dentry
->d_name
.name
;
1978 int namelen
= dentry
->d_name
.len
;
1979 struct btrfs_dir_item
*di
;
1980 struct btrfs_path
*path
;
1981 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1984 path
= btrfs_alloc_path();
1987 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1991 if (!di
|| IS_ERR(di
)) {
1994 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1996 btrfs_free_path(path
);
1999 location
->objectid
= 0;
2004 * when we hit a tree root in a directory, the btrfs part of the inode
2005 * needs to be changed to reflect the root directory of the tree root. This
2006 * is kind of like crossing a mount point.
2008 static int fixup_tree_root_location(struct btrfs_root
*root
,
2009 struct btrfs_key
*location
,
2010 struct btrfs_root
**sub_root
,
2011 struct dentry
*dentry
)
2013 struct btrfs_root_item
*ri
;
2015 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
2017 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
2020 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
2021 dentry
->d_name
.name
,
2022 dentry
->d_name
.len
);
2023 if (IS_ERR(*sub_root
))
2024 return PTR_ERR(*sub_root
);
2026 ri
= &(*sub_root
)->root_item
;
2027 location
->objectid
= btrfs_root_dirid(ri
);
2028 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2029 location
->offset
= 0;
2034 static noinline
void init_btrfs_i(struct inode
*inode
)
2036 struct btrfs_inode
*bi
= BTRFS_I(inode
);
2039 bi
->i_default_acl
= NULL
;
2043 bi
->logged_trans
= 0;
2044 bi
->delalloc_bytes
= 0;
2045 bi
->disk_i_size
= 0;
2047 bi
->index_cnt
= (u64
)-1;
2048 bi
->log_dirty_trans
= 0;
2049 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2050 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2051 inode
->i_mapping
, GFP_NOFS
);
2052 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2053 inode
->i_mapping
, GFP_NOFS
);
2054 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
2055 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2056 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2057 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2058 mutex_init(&BTRFS_I(inode
)->log_mutex
);
2061 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
2063 struct btrfs_iget_args
*args
= p
;
2064 inode
->i_ino
= args
->ino
;
2065 init_btrfs_i(inode
);
2066 BTRFS_I(inode
)->root
= args
->root
;
2070 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
2072 struct btrfs_iget_args
*args
= opaque
;
2073 return (args
->ino
== inode
->i_ino
&&
2074 args
->root
== BTRFS_I(inode
)->root
);
2077 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
2078 struct btrfs_root
*root
, int wait
)
2080 struct inode
*inode
;
2081 struct btrfs_iget_args args
;
2082 args
.ino
= objectid
;
2086 inode
= ilookup5(s
, objectid
, btrfs_find_actor
,
2089 inode
= ilookup5_nowait(s
, objectid
, btrfs_find_actor
,
2095 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
2096 struct btrfs_root
*root
)
2098 struct inode
*inode
;
2099 struct btrfs_iget_args args
;
2100 args
.ino
= objectid
;
2103 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
2104 btrfs_init_locked_inode
,
2109 /* Get an inode object given its location and corresponding root.
2110 * Returns in *is_new if the inode was read from disk
2112 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
2113 struct btrfs_root
*root
, int *is_new
)
2115 struct inode
*inode
;
2117 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
2119 return ERR_PTR(-EACCES
);
2121 if (inode
->i_state
& I_NEW
) {
2122 BTRFS_I(inode
)->root
= root
;
2123 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
2124 btrfs_read_locked_inode(inode
);
2125 unlock_new_inode(inode
);
2136 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
2137 struct nameidata
*nd
)
2139 struct inode
* inode
;
2140 struct btrfs_inode
*bi
= BTRFS_I(dir
);
2141 struct btrfs_root
*root
= bi
->root
;
2142 struct btrfs_root
*sub_root
= root
;
2143 struct btrfs_key location
;
2144 int ret
, new, do_orphan
= 0;
2146 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
2147 return ERR_PTR(-ENAMETOOLONG
);
2149 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
2152 return ERR_PTR(ret
);
2155 if (location
.objectid
) {
2156 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
2159 return ERR_PTR(ret
);
2161 return ERR_PTR(-ENOENT
);
2162 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, &new);
2164 return ERR_CAST(inode
);
2166 /* the inode and parent dir are two different roots */
2167 if (new && root
!= sub_root
) {
2169 sub_root
->inode
= inode
;
2174 if (unlikely(do_orphan
))
2175 btrfs_orphan_cleanup(sub_root
);
2177 return d_splice_alias(inode
, dentry
);
2180 static unsigned char btrfs_filetype_table
[] = {
2181 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
2184 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
2187 struct inode
*inode
= filp
->f_dentry
->d_inode
;
2188 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2189 struct btrfs_item
*item
;
2190 struct btrfs_dir_item
*di
;
2191 struct btrfs_key key
;
2192 struct btrfs_key found_key
;
2193 struct btrfs_path
*path
;
2196 struct extent_buffer
*leaf
;
2199 unsigned char d_type
;
2204 int key_type
= BTRFS_DIR_INDEX_KEY
;
2209 /* FIXME, use a real flag for deciding about the key type */
2210 if (root
->fs_info
->tree_root
== root
)
2211 key_type
= BTRFS_DIR_ITEM_KEY
;
2213 /* special case for "." */
2214 if (filp
->f_pos
== 0) {
2215 over
= filldir(dirent
, ".", 1,
2222 /* special case for .., just use the back ref */
2223 if (filp
->f_pos
== 1) {
2224 u64 pino
= parent_ino(filp
->f_path
.dentry
);
2225 over
= filldir(dirent
, "..", 2,
2232 path
= btrfs_alloc_path();
2235 btrfs_set_key_type(&key
, key_type
);
2236 key
.offset
= filp
->f_pos
;
2237 key
.objectid
= inode
->i_ino
;
2239 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2245 leaf
= path
->nodes
[0];
2246 nritems
= btrfs_header_nritems(leaf
);
2247 slot
= path
->slots
[0];
2248 if (advance
|| slot
>= nritems
) {
2249 if (slot
>= nritems
- 1) {
2250 ret
= btrfs_next_leaf(root
, path
);
2253 leaf
= path
->nodes
[0];
2254 nritems
= btrfs_header_nritems(leaf
);
2255 slot
= path
->slots
[0];
2262 item
= btrfs_item_nr(leaf
, slot
);
2263 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2265 if (found_key
.objectid
!= key
.objectid
)
2267 if (btrfs_key_type(&found_key
) != key_type
)
2269 if (found_key
.offset
< filp
->f_pos
)
2272 filp
->f_pos
= found_key
.offset
;
2274 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
2276 di_total
= btrfs_item_size(leaf
, item
);
2278 while (di_cur
< di_total
) {
2279 struct btrfs_key location
;
2281 name_len
= btrfs_dir_name_len(leaf
, di
);
2282 if (name_len
<= sizeof(tmp_name
)) {
2283 name_ptr
= tmp_name
;
2285 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
2291 read_extent_buffer(leaf
, name_ptr
,
2292 (unsigned long)(di
+ 1), name_len
);
2294 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
2295 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
2296 over
= filldir(dirent
, name_ptr
, name_len
,
2297 found_key
.offset
, location
.objectid
,
2300 if (name_ptr
!= tmp_name
)
2306 di_len
= btrfs_dir_name_len(leaf
, di
) +
2307 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
2309 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
2313 /* Reached end of directory/root. Bump pos past the last item. */
2314 if (key_type
== BTRFS_DIR_INDEX_KEY
)
2315 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
2321 btrfs_free_path(path
);
2325 int btrfs_write_inode(struct inode
*inode
, int wait
)
2327 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2328 struct btrfs_trans_handle
*trans
;
2331 if (root
->fs_info
->closing
> 1)
2335 trans
= btrfs_join_transaction(root
, 1);
2336 btrfs_set_trans_block_group(trans
, inode
);
2337 ret
= btrfs_commit_transaction(trans
, root
);
2343 * This is somewhat expensive, updating the tree every time the
2344 * inode changes. But, it is most likely to find the inode in cache.
2345 * FIXME, needs more benchmarking...there are no reasons other than performance
2346 * to keep or drop this code.
2348 void btrfs_dirty_inode(struct inode
*inode
)
2350 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2351 struct btrfs_trans_handle
*trans
;
2353 trans
= btrfs_join_transaction(root
, 1);
2354 btrfs_set_trans_block_group(trans
, inode
);
2355 btrfs_update_inode(trans
, root
, inode
);
2356 btrfs_end_transaction(trans
, root
);
2360 * find the highest existing sequence number in a directory
2361 * and then set the in-memory index_cnt variable to reflect
2362 * free sequence numbers
2364 static int btrfs_set_inode_index_count(struct inode
*inode
)
2366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2367 struct btrfs_key key
, found_key
;
2368 struct btrfs_path
*path
;
2369 struct extent_buffer
*leaf
;
2372 key
.objectid
= inode
->i_ino
;
2373 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
2374 key
.offset
= (u64
)-1;
2376 path
= btrfs_alloc_path();
2380 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2383 /* FIXME: we should be able to handle this */
2389 * MAGIC NUMBER EXPLANATION:
2390 * since we search a directory based on f_pos we have to start at 2
2391 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2392 * else has to start at 2
2394 if (path
->slots
[0] == 0) {
2395 BTRFS_I(inode
)->index_cnt
= 2;
2401 leaf
= path
->nodes
[0];
2402 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2404 if (found_key
.objectid
!= inode
->i_ino
||
2405 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
2406 BTRFS_I(inode
)->index_cnt
= 2;
2410 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
2412 btrfs_free_path(path
);
2417 * helper to find a free sequence number in a given directory. This current
2418 * code is very simple, later versions will do smarter things in the btree
2420 static int btrfs_set_inode_index(struct inode
*dir
, struct inode
*inode
,
2425 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
2426 ret
= btrfs_set_inode_index_count(dir
);
2432 *index
= BTRFS_I(dir
)->index_cnt
;
2433 BTRFS_I(dir
)->index_cnt
++;
2438 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
2439 struct btrfs_root
*root
,
2441 const char *name
, int name_len
,
2444 struct btrfs_block_group_cache
*group
,
2445 int mode
, u64
*index
)
2447 struct inode
*inode
;
2448 struct btrfs_inode_item
*inode_item
;
2449 struct btrfs_block_group_cache
*new_inode_group
;
2450 struct btrfs_key
*location
;
2451 struct btrfs_path
*path
;
2452 struct btrfs_inode_ref
*ref
;
2453 struct btrfs_key key
[2];
2459 path
= btrfs_alloc_path();
2462 inode
= new_inode(root
->fs_info
->sb
);
2464 return ERR_PTR(-ENOMEM
);
2467 ret
= btrfs_set_inode_index(dir
, inode
, index
);
2469 return ERR_PTR(ret
);
2472 * index_cnt is ignored for everything but a dir,
2473 * btrfs_get_inode_index_count has an explanation for the magic
2476 init_btrfs_i(inode
);
2477 BTRFS_I(inode
)->index_cnt
= 2;
2478 BTRFS_I(inode
)->root
= root
;
2479 BTRFS_I(inode
)->generation
= trans
->transid
;
2485 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
2486 BTRFS_BLOCK_GROUP_METADATA
, owner
);
2487 if (!new_inode_group
) {
2488 printk("find_block group failed\n");
2489 new_inode_group
= group
;
2491 BTRFS_I(inode
)->block_group
= new_inode_group
;
2493 key
[0].objectid
= objectid
;
2494 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
2497 key
[1].objectid
= objectid
;
2498 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
2499 key
[1].offset
= ref_objectid
;
2501 sizes
[0] = sizeof(struct btrfs_inode_item
);
2502 sizes
[1] = name_len
+ sizeof(*ref
);
2504 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
2508 if (objectid
> root
->highest_inode
)
2509 root
->highest_inode
= objectid
;
2511 inode
->i_uid
= current
->fsuid
;
2512 inode
->i_gid
= current
->fsgid
;
2513 inode
->i_mode
= mode
;
2514 inode
->i_ino
= objectid
;
2515 inode_set_bytes(inode
, 0);
2516 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2517 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2518 struct btrfs_inode_item
);
2519 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
2521 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2522 struct btrfs_inode_ref
);
2523 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
2524 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
2525 ptr
= (unsigned long)(ref
+ 1);
2526 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
2528 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2529 btrfs_free_path(path
);
2531 location
= &BTRFS_I(inode
)->location
;
2532 location
->objectid
= objectid
;
2533 location
->offset
= 0;
2534 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2536 insert_inode_hash(inode
);
2540 BTRFS_I(dir
)->index_cnt
--;
2541 btrfs_free_path(path
);
2542 return ERR_PTR(ret
);
2545 static inline u8
btrfs_inode_type(struct inode
*inode
)
2547 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2551 * utility function to add 'inode' into 'parent_inode' with
2552 * a give name and a given sequence number.
2553 * if 'add_backref' is true, also insert a backref from the
2554 * inode to the parent directory.
2556 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2557 struct inode
*parent_inode
, struct inode
*inode
,
2558 const char *name
, int name_len
, int add_backref
, u64 index
)
2561 struct btrfs_key key
;
2562 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
2564 key
.objectid
= inode
->i_ino
;
2565 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2568 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
2569 parent_inode
->i_ino
,
2570 &key
, btrfs_inode_type(inode
),
2574 ret
= btrfs_insert_inode_ref(trans
, root
,
2577 parent_inode
->i_ino
,
2580 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2582 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2583 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
2588 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2589 struct dentry
*dentry
, struct inode
*inode
,
2590 int backref
, u64 index
)
2592 int err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
2593 inode
, dentry
->d_name
.name
,
2594 dentry
->d_name
.len
, backref
, index
);
2596 d_instantiate(dentry
, inode
);
2604 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2605 int mode
, dev_t rdev
)
2607 struct btrfs_trans_handle
*trans
;
2608 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2609 struct inode
*inode
= NULL
;
2613 unsigned long nr
= 0;
2616 if (!new_valid_dev(rdev
))
2619 err
= btrfs_check_free_space(root
, 1, 0);
2623 trans
= btrfs_start_transaction(root
, 1);
2624 btrfs_set_trans_block_group(trans
, dir
);
2626 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2632 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2634 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2635 BTRFS_I(dir
)->block_group
, mode
, &index
);
2636 err
= PTR_ERR(inode
);
2640 err
= btrfs_init_acl(inode
, dir
);
2646 btrfs_set_trans_block_group(trans
, inode
);
2647 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2651 inode
->i_op
= &btrfs_special_inode_operations
;
2652 init_special_inode(inode
, inode
->i_mode
, rdev
);
2653 btrfs_update_inode(trans
, root
, inode
);
2655 dir
->i_sb
->s_dirt
= 1;
2656 btrfs_update_inode_block_group(trans
, inode
);
2657 btrfs_update_inode_block_group(trans
, dir
);
2659 nr
= trans
->blocks_used
;
2660 btrfs_end_transaction_throttle(trans
, root
);
2663 inode_dec_link_count(inode
);
2666 btrfs_btree_balance_dirty(root
, nr
);
2670 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2671 int mode
, struct nameidata
*nd
)
2673 struct btrfs_trans_handle
*trans
;
2674 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2675 struct inode
*inode
= NULL
;
2678 unsigned long nr
= 0;
2682 err
= btrfs_check_free_space(root
, 1, 0);
2685 trans
= btrfs_start_transaction(root
, 1);
2686 btrfs_set_trans_block_group(trans
, dir
);
2688 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2694 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2696 dentry
->d_parent
->d_inode
->i_ino
,
2697 objectid
, BTRFS_I(dir
)->block_group
, mode
,
2699 err
= PTR_ERR(inode
);
2703 err
= btrfs_init_acl(inode
, dir
);
2709 btrfs_set_trans_block_group(trans
, inode
);
2710 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2714 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2715 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2716 inode
->i_fop
= &btrfs_file_operations
;
2717 inode
->i_op
= &btrfs_file_inode_operations
;
2718 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2720 dir
->i_sb
->s_dirt
= 1;
2721 btrfs_update_inode_block_group(trans
, inode
);
2722 btrfs_update_inode_block_group(trans
, dir
);
2724 nr
= trans
->blocks_used
;
2725 btrfs_end_transaction_throttle(trans
, root
);
2728 inode_dec_link_count(inode
);
2731 btrfs_btree_balance_dirty(root
, nr
);
2735 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2736 struct dentry
*dentry
)
2738 struct btrfs_trans_handle
*trans
;
2739 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2740 struct inode
*inode
= old_dentry
->d_inode
;
2742 unsigned long nr
= 0;
2746 if (inode
->i_nlink
== 0)
2749 btrfs_inc_nlink(inode
);
2750 err
= btrfs_check_free_space(root
, 1, 0);
2753 err
= btrfs_set_inode_index(dir
, inode
, &index
);
2757 trans
= btrfs_start_transaction(root
, 1);
2759 btrfs_set_trans_block_group(trans
, dir
);
2760 atomic_inc(&inode
->i_count
);
2762 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
2767 dir
->i_sb
->s_dirt
= 1;
2768 btrfs_update_inode_block_group(trans
, dir
);
2769 err
= btrfs_update_inode(trans
, root
, inode
);
2774 nr
= trans
->blocks_used
;
2775 btrfs_end_transaction_throttle(trans
, root
);
2778 inode_dec_link_count(inode
);
2781 btrfs_btree_balance_dirty(root
, nr
);
2785 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2787 struct inode
*inode
= NULL
;
2788 struct btrfs_trans_handle
*trans
;
2789 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2791 int drop_on_err
= 0;
2794 unsigned long nr
= 1;
2796 err
= btrfs_check_free_space(root
, 1, 0);
2800 trans
= btrfs_start_transaction(root
, 1);
2801 btrfs_set_trans_block_group(trans
, dir
);
2803 if (IS_ERR(trans
)) {
2804 err
= PTR_ERR(trans
);
2808 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2814 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2816 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2817 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
2819 if (IS_ERR(inode
)) {
2820 err
= PTR_ERR(inode
);
2826 err
= btrfs_init_acl(inode
, dir
);
2830 inode
->i_op
= &btrfs_dir_inode_operations
;
2831 inode
->i_fop
= &btrfs_dir_file_operations
;
2832 btrfs_set_trans_block_group(trans
, inode
);
2834 btrfs_i_size_write(inode
, 0);
2835 err
= btrfs_update_inode(trans
, root
, inode
);
2839 err
= btrfs_add_link(trans
, dentry
->d_parent
->d_inode
,
2840 inode
, dentry
->d_name
.name
,
2841 dentry
->d_name
.len
, 0, index
);
2845 d_instantiate(dentry
, inode
);
2847 dir
->i_sb
->s_dirt
= 1;
2848 btrfs_update_inode_block_group(trans
, inode
);
2849 btrfs_update_inode_block_group(trans
, dir
);
2852 nr
= trans
->blocks_used
;
2853 btrfs_end_transaction_throttle(trans
, root
);
2858 btrfs_btree_balance_dirty(root
, nr
);
2862 /* helper for btfs_get_extent. Given an existing extent in the tree,
2863 * and an extent that you want to insert, deal with overlap and insert
2864 * the new extent into the tree.
2866 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2867 struct extent_map
*existing
,
2868 struct extent_map
*em
,
2869 u64 map_start
, u64 map_len
)
2873 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2874 start_diff
= map_start
- em
->start
;
2875 em
->start
= map_start
;
2877 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2878 em
->block_start
+= start_diff
;
2879 return add_extent_mapping(em_tree
, em
);
2883 * a bit scary, this does extent mapping from logical file offset to the disk.
2884 * the ugly parts come from merging extents from the disk with the
2885 * in-ram representation. This gets more complex because of the data=ordered code,
2886 * where the in-ram extents might be locked pending data=ordered completion.
2888 * This also copies inline extents directly into the page.
2890 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2891 size_t pg_offset
, u64 start
, u64 len
,
2897 u64 extent_start
= 0;
2899 u64 objectid
= inode
->i_ino
;
2901 struct btrfs_path
*path
= NULL
;
2902 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2903 struct btrfs_file_extent_item
*item
;
2904 struct extent_buffer
*leaf
;
2905 struct btrfs_key found_key
;
2906 struct extent_map
*em
= NULL
;
2907 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2908 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2909 struct btrfs_trans_handle
*trans
= NULL
;
2912 spin_lock(&em_tree
->lock
);
2913 em
= lookup_extent_mapping(em_tree
, start
, len
);
2915 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2916 spin_unlock(&em_tree
->lock
);
2919 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2920 free_extent_map(em
);
2921 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2922 free_extent_map(em
);
2926 em
= alloc_extent_map(GFP_NOFS
);
2931 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2932 em
->start
= EXTENT_MAP_HOLE
;
2936 path
= btrfs_alloc_path();
2940 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2941 objectid
, start
, trans
!= NULL
);
2948 if (path
->slots
[0] == 0)
2953 leaf
= path
->nodes
[0];
2954 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2955 struct btrfs_file_extent_item
);
2956 /* are we inside the extent that was found? */
2957 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2958 found_type
= btrfs_key_type(&found_key
);
2959 if (found_key
.objectid
!= objectid
||
2960 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2964 found_type
= btrfs_file_extent_type(leaf
, item
);
2965 extent_start
= found_key
.offset
;
2966 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2967 extent_end
= extent_start
+
2968 btrfs_file_extent_num_bytes(leaf
, item
);
2970 if (start
< extent_start
|| start
>= extent_end
) {
2972 if (start
< extent_start
) {
2973 if (start
+ len
<= extent_start
)
2975 em
->len
= extent_end
- extent_start
;
2981 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2983 em
->start
= extent_start
;
2984 em
->len
= extent_end
- extent_start
;
2985 em
->block_start
= EXTENT_MAP_HOLE
;
2988 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2989 em
->block_start
= bytenr
;
2990 em
->start
= extent_start
;
2991 em
->len
= extent_end
- extent_start
;
2993 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2998 size_t extent_offset
;
3001 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
3003 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
3004 ~((u64
)root
->sectorsize
- 1);
3005 if (start
< extent_start
|| start
>= extent_end
) {
3007 if (start
< extent_start
) {
3008 if (start
+ len
<= extent_start
)
3010 em
->len
= extent_end
- extent_start
;
3016 em
->block_start
= EXTENT_MAP_INLINE
;
3019 em
->start
= extent_start
;
3024 page_start
= page_offset(page
) + pg_offset
;
3025 extent_offset
= page_start
- extent_start
;
3026 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
3027 size
- extent_offset
);
3028 em
->start
= extent_start
+ extent_offset
;
3029 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
3030 ~((u64
)root
->sectorsize
- 1);
3032 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
3033 if (create
== 0 && !PageUptodate(page
)) {
3034 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
3036 flush_dcache_page(page
);
3037 } else if (create
&& PageUptodate(page
)) {
3040 free_extent_map(em
);
3042 btrfs_release_path(root
, path
);
3043 trans
= btrfs_join_transaction(root
, 1);
3046 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
3048 btrfs_mark_buffer_dirty(leaf
);
3051 set_extent_uptodate(io_tree
, em
->start
,
3052 extent_map_end(em
) - 1, GFP_NOFS
);
3055 printk("unkknown found_type %d\n", found_type
);
3062 em
->block_start
= EXTENT_MAP_HOLE
;
3064 btrfs_release_path(root
, path
);
3065 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
3066 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
3072 spin_lock(&em_tree
->lock
);
3073 ret
= add_extent_mapping(em_tree
, em
);
3074 /* it is possible that someone inserted the extent into the tree
3075 * while we had the lock dropped. It is also possible that
3076 * an overlapping map exists in the tree
3078 if (ret
== -EEXIST
) {
3079 struct extent_map
*existing
;
3083 existing
= lookup_extent_mapping(em_tree
, start
, len
);
3084 if (existing
&& (existing
->start
> start
||
3085 existing
->start
+ existing
->len
<= start
)) {
3086 free_extent_map(existing
);
3090 existing
= lookup_extent_mapping(em_tree
, em
->start
,
3093 err
= merge_extent_mapping(em_tree
, existing
,
3096 free_extent_map(existing
);
3098 free_extent_map(em
);
3103 printk("failing to insert %Lu %Lu\n",
3105 free_extent_map(em
);
3109 free_extent_map(em
);
3114 spin_unlock(&em_tree
->lock
);
3117 btrfs_free_path(path
);
3119 ret
= btrfs_end_transaction(trans
, root
);
3125 free_extent_map(em
);
3127 return ERR_PTR(err
);
3132 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
3133 const struct iovec
*iov
, loff_t offset
,
3134 unsigned long nr_segs
)
3139 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
3141 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
3144 int btrfs_readpage(struct file
*file
, struct page
*page
)
3146 struct extent_io_tree
*tree
;
3147 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3148 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
3151 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
3153 struct extent_io_tree
*tree
;
3156 if (current
->flags
& PF_MEMALLOC
) {
3157 redirty_page_for_writepage(wbc
, page
);
3161 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3162 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
3165 int btrfs_writepages(struct address_space
*mapping
,
3166 struct writeback_control
*wbc
)
3168 struct extent_io_tree
*tree
;
3169 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3170 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
3174 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
3175 struct list_head
*pages
, unsigned nr_pages
)
3177 struct extent_io_tree
*tree
;
3178 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3179 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
3182 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
3184 struct extent_io_tree
*tree
;
3185 struct extent_map_tree
*map
;
3188 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3189 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
3190 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
3192 ClearPagePrivate(page
);
3193 set_page_private(page
, 0);
3194 page_cache_release(page
);
3199 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
3201 if (PageWriteback(page
) || PageDirty(page
))
3203 return __btrfs_releasepage(page
, gfp_flags
);
3206 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
3208 struct extent_io_tree
*tree
;
3209 struct btrfs_ordered_extent
*ordered
;
3210 u64 page_start
= page_offset(page
);
3211 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3213 wait_on_page_writeback(page
);
3214 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3216 btrfs_releasepage(page
, GFP_NOFS
);
3220 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3221 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
3225 * IO on this page will never be started, so we need
3226 * to account for any ordered extents now
3228 clear_extent_bit(tree
, page_start
, page_end
,
3229 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3230 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
3231 btrfs_finish_ordered_io(page
->mapping
->host
,
3232 page_start
, page_end
);
3233 btrfs_put_ordered_extent(ordered
);
3234 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3236 clear_extent_bit(tree
, page_start
, page_end
,
3237 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3240 __btrfs_releasepage(page
, GFP_NOFS
);
3242 ClearPageChecked(page
);
3243 if (PagePrivate(page
)) {
3244 ClearPagePrivate(page
);
3245 set_page_private(page
, 0);
3246 page_cache_release(page
);
3251 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3252 * called from a page fault handler when a page is first dirtied. Hence we must
3253 * be careful to check for EOF conditions here. We set the page up correctly
3254 * for a written page which means we get ENOSPC checking when writing into
3255 * holes and correct delalloc and unwritten extent mapping on filesystems that
3256 * support these features.
3258 * We are not allowed to take the i_mutex here so we have to play games to
3259 * protect against truncate races as the page could now be beyond EOF. Because
3260 * vmtruncate() writes the inode size before removing pages, once we have the
3261 * page lock we can determine safely if the page is beyond EOF. If it is not
3262 * beyond EOF, then the page is guaranteed safe against truncation until we
3265 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
3267 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
3268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3269 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3270 struct btrfs_ordered_extent
*ordered
;
3272 unsigned long zero_start
;
3278 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
3285 size
= i_size_read(inode
);
3286 page_start
= page_offset(page
);
3287 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3289 if ((page
->mapping
!= inode
->i_mapping
) ||
3290 (page_start
>= size
)) {
3291 /* page got truncated out from underneath us */
3294 wait_on_page_writeback(page
);
3296 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3297 set_page_extent_mapped(page
);
3300 * we can't set the delalloc bits if there are pending ordered
3301 * extents. Drop our locks and wait for them to finish
3303 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3305 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3307 btrfs_start_ordered_extent(inode
, ordered
, 1);
3308 btrfs_put_ordered_extent(ordered
);
3312 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3315 /* page is wholly or partially inside EOF */
3316 if (page_start
+ PAGE_CACHE_SIZE
> size
)
3317 zero_start
= size
& ~PAGE_CACHE_MASK
;
3319 zero_start
= PAGE_CACHE_SIZE
;
3321 if (zero_start
!= PAGE_CACHE_SIZE
) {
3323 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
3324 flush_dcache_page(page
);
3327 ClearPageChecked(page
);
3328 set_page_dirty(page
);
3329 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3337 static void btrfs_truncate(struct inode
*inode
)
3339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3341 struct btrfs_trans_handle
*trans
;
3343 u64 mask
= root
->sectorsize
- 1;
3345 if (!S_ISREG(inode
->i_mode
))
3347 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3350 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3351 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
3353 trans
= btrfs_start_transaction(root
, 1);
3354 btrfs_set_trans_block_group(trans
, inode
);
3355 btrfs_i_size_write(inode
, inode
->i_size
);
3357 ret
= btrfs_orphan_add(trans
, inode
);
3360 /* FIXME, add redo link to tree so we don't leak on crash */
3361 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, inode
->i_size
,
3362 BTRFS_EXTENT_DATA_KEY
);
3363 btrfs_update_inode(trans
, root
, inode
);
3365 ret
= btrfs_orphan_del(trans
, inode
);
3369 nr
= trans
->blocks_used
;
3370 ret
= btrfs_end_transaction_throttle(trans
, root
);
3372 btrfs_btree_balance_dirty(root
, nr
);
3376 * Invalidate a single dcache entry at the root of the filesystem.
3377 * Needed after creation of snapshot or subvolume.
3379 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
3382 struct dentry
*alias
, *entry
;
3385 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
3389 /* change me if btrfs ever gets a d_hash operation */
3390 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
3391 entry
= d_lookup(alias
, &qstr
);
3394 d_invalidate(entry
);
3401 * create a new subvolume directory/inode (helper for the ioctl).
3403 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
3404 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
3405 struct btrfs_block_group_cache
*block_group
)
3407 struct inode
*inode
;
3410 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
3411 new_dirid
, block_group
, S_IFDIR
| 0700, &index
);
3413 return PTR_ERR(inode
);
3414 inode
->i_op
= &btrfs_dir_inode_operations
;
3415 inode
->i_fop
= &btrfs_dir_file_operations
;
3416 new_root
->inode
= inode
;
3419 btrfs_i_size_write(inode
, 0);
3421 return btrfs_update_inode(trans
, new_root
, inode
);
3424 /* helper function for file defrag and space balancing. This
3425 * forces readahead on a given range of bytes in an inode
3427 unsigned long btrfs_force_ra(struct address_space
*mapping
,
3428 struct file_ra_state
*ra
, struct file
*file
,
3429 pgoff_t offset
, pgoff_t last_index
)
3431 pgoff_t req_size
= last_index
- offset
+ 1;
3433 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
3434 return offset
+ req_size
;
3437 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3439 struct btrfs_inode
*ei
;
3441 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3445 ei
->logged_trans
= 0;
3446 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
3447 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
3448 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
3449 INIT_LIST_HEAD(&ei
->i_orphan
);
3450 return &ei
->vfs_inode
;
3453 void btrfs_destroy_inode(struct inode
*inode
)
3455 struct btrfs_ordered_extent
*ordered
;
3456 WARN_ON(!list_empty(&inode
->i_dentry
));
3457 WARN_ON(inode
->i_data
.nrpages
);
3459 if (BTRFS_I(inode
)->i_acl
&&
3460 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
3461 posix_acl_release(BTRFS_I(inode
)->i_acl
);
3462 if (BTRFS_I(inode
)->i_default_acl
&&
3463 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
3464 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
3466 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
3467 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
3468 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
3469 " list\n", inode
->i_ino
);
3472 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
3475 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
3479 printk("found ordered extent %Lu %Lu\n",
3480 ordered
->file_offset
, ordered
->len
);
3481 btrfs_remove_ordered_extent(inode
, ordered
);
3482 btrfs_put_ordered_extent(ordered
);
3483 btrfs_put_ordered_extent(ordered
);
3486 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
3487 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3490 static void init_once(void *foo
)
3492 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3494 inode_init_once(&ei
->vfs_inode
);
3497 void btrfs_destroy_cachep(void)
3499 if (btrfs_inode_cachep
)
3500 kmem_cache_destroy(btrfs_inode_cachep
);
3501 if (btrfs_trans_handle_cachep
)
3502 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3503 if (btrfs_transaction_cachep
)
3504 kmem_cache_destroy(btrfs_transaction_cachep
);
3505 if (btrfs_bit_radix_cachep
)
3506 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3507 if (btrfs_path_cachep
)
3508 kmem_cache_destroy(btrfs_path_cachep
);
3511 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3512 unsigned long extra_flags
,
3513 void (*ctor
)(void *))
3515 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3516 SLAB_MEM_SPREAD
| extra_flags
), ctor
);
3519 int btrfs_init_cachep(void)
3521 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3522 sizeof(struct btrfs_inode
),
3524 if (!btrfs_inode_cachep
)
3526 btrfs_trans_handle_cachep
=
3527 btrfs_cache_create("btrfs_trans_handle_cache",
3528 sizeof(struct btrfs_trans_handle
),
3530 if (!btrfs_trans_handle_cachep
)
3532 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3533 sizeof(struct btrfs_transaction
),
3535 if (!btrfs_transaction_cachep
)
3537 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3538 sizeof(struct btrfs_path
),
3540 if (!btrfs_path_cachep
)
3542 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3543 SLAB_DESTROY_BY_RCU
, NULL
);
3544 if (!btrfs_bit_radix_cachep
)
3548 btrfs_destroy_cachep();
3552 static int btrfs_getattr(struct vfsmount
*mnt
,
3553 struct dentry
*dentry
, struct kstat
*stat
)
3555 struct inode
*inode
= dentry
->d_inode
;
3556 generic_fillattr(inode
, stat
);
3557 stat
->blksize
= PAGE_CACHE_SIZE
;
3558 stat
->blocks
= (inode_get_bytes(inode
) +
3559 BTRFS_I(inode
)->delalloc_bytes
) >> 9;
3563 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3564 struct inode
* new_dir
,struct dentry
*new_dentry
)
3566 struct btrfs_trans_handle
*trans
;
3567 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3568 struct inode
*new_inode
= new_dentry
->d_inode
;
3569 struct inode
*old_inode
= old_dentry
->d_inode
;
3570 struct timespec ctime
= CURRENT_TIME
;
3574 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3575 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3579 ret
= btrfs_check_free_space(root
, 1, 0);
3583 trans
= btrfs_start_transaction(root
, 1);
3585 btrfs_set_trans_block_group(trans
, new_dir
);
3587 btrfs_inc_nlink(old_dentry
->d_inode
);
3588 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3589 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3590 old_inode
->i_ctime
= ctime
;
3592 ret
= btrfs_unlink_inode(trans
, root
, old_dir
, old_dentry
->d_inode
,
3593 old_dentry
->d_name
.name
,
3594 old_dentry
->d_name
.len
);
3599 new_inode
->i_ctime
= CURRENT_TIME
;
3600 ret
= btrfs_unlink_inode(trans
, root
, new_dir
,
3601 new_dentry
->d_inode
,
3602 new_dentry
->d_name
.name
,
3603 new_dentry
->d_name
.len
);
3606 if (new_inode
->i_nlink
== 0) {
3607 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
3613 ret
= btrfs_set_inode_index(new_dir
, old_inode
, &index
);
3617 ret
= btrfs_add_link(trans
, new_dentry
->d_parent
->d_inode
,
3618 old_inode
, new_dentry
->d_name
.name
,
3619 new_dentry
->d_name
.len
, 1, index
);
3624 btrfs_end_transaction_throttle(trans
, root
);
3630 * some fairly slow code that needs optimization. This walks the list
3631 * of all the inodes with pending delalloc and forces them to disk.
3633 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
)
3635 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
3636 struct btrfs_inode
*binode
;
3637 struct inode
*inode
;
3638 unsigned long flags
;
3640 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3641 while(!list_empty(head
)) {
3642 binode
= list_entry(head
->next
, struct btrfs_inode
,
3644 inode
= igrab(&binode
->vfs_inode
);
3646 list_del_init(&binode
->delalloc_inodes
);
3647 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3649 filemap_flush(inode
->i_mapping
);
3653 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3655 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3657 /* the filemap_flush will queue IO into the worker threads, but
3658 * we have to make sure the IO is actually started and that
3659 * ordered extents get created before we return
3661 atomic_inc(&root
->fs_info
->async_submit_draining
);
3662 while(atomic_read(&root
->fs_info
->nr_async_submits
)) {
3663 wait_event(root
->fs_info
->async_submit_wait
,
3664 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0));
3666 atomic_dec(&root
->fs_info
->async_submit_draining
);
3670 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3671 const char *symname
)
3673 struct btrfs_trans_handle
*trans
;
3674 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3675 struct btrfs_path
*path
;
3676 struct btrfs_key key
;
3677 struct inode
*inode
= NULL
;
3685 struct btrfs_file_extent_item
*ei
;
3686 struct extent_buffer
*leaf
;
3687 unsigned long nr
= 0;
3689 name_len
= strlen(symname
) + 1;
3690 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3691 return -ENAMETOOLONG
;
3693 err
= btrfs_check_free_space(root
, 1, 0);
3697 trans
= btrfs_start_transaction(root
, 1);
3698 btrfs_set_trans_block_group(trans
, dir
);
3700 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3706 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3708 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3709 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
3711 err
= PTR_ERR(inode
);
3715 err
= btrfs_init_acl(inode
, dir
);
3721 btrfs_set_trans_block_group(trans
, inode
);
3722 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
3726 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3727 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3728 inode
->i_fop
= &btrfs_file_operations
;
3729 inode
->i_op
= &btrfs_file_inode_operations
;
3730 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3732 dir
->i_sb
->s_dirt
= 1;
3733 btrfs_update_inode_block_group(trans
, inode
);
3734 btrfs_update_inode_block_group(trans
, dir
);
3738 path
= btrfs_alloc_path();
3740 key
.objectid
= inode
->i_ino
;
3742 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3743 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3744 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3750 leaf
= path
->nodes
[0];
3751 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3752 struct btrfs_file_extent_item
);
3753 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3754 btrfs_set_file_extent_type(leaf
, ei
,
3755 BTRFS_FILE_EXTENT_INLINE
);
3756 ptr
= btrfs_file_extent_inline_start(ei
);
3757 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3758 btrfs_mark_buffer_dirty(leaf
);
3759 btrfs_free_path(path
);
3761 inode
->i_op
= &btrfs_symlink_inode_operations
;
3762 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3763 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3764 btrfs_i_size_write(inode
, name_len
- 1);
3765 err
= btrfs_update_inode(trans
, root
, inode
);
3770 nr
= trans
->blocks_used
;
3771 btrfs_end_transaction_throttle(trans
, root
);
3774 inode_dec_link_count(inode
);
3777 btrfs_btree_balance_dirty(root
, nr
);
3781 static int btrfs_set_page_dirty(struct page
*page
)
3783 return __set_page_dirty_nobuffers(page
);
3786 static int btrfs_permission(struct inode
*inode
, int mask
)
3788 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3790 return generic_permission(inode
, mask
, btrfs_check_acl
);
3793 static struct inode_operations btrfs_dir_inode_operations
= {
3794 .lookup
= btrfs_lookup
,
3795 .create
= btrfs_create
,
3796 .unlink
= btrfs_unlink
,
3798 .mkdir
= btrfs_mkdir
,
3799 .rmdir
= btrfs_rmdir
,
3800 .rename
= btrfs_rename
,
3801 .symlink
= btrfs_symlink
,
3802 .setattr
= btrfs_setattr
,
3803 .mknod
= btrfs_mknod
,
3804 .setxattr
= btrfs_setxattr
,
3805 .getxattr
= btrfs_getxattr
,
3806 .listxattr
= btrfs_listxattr
,
3807 .removexattr
= btrfs_removexattr
,
3808 .permission
= btrfs_permission
,
3810 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3811 .lookup
= btrfs_lookup
,
3812 .permission
= btrfs_permission
,
3814 static struct file_operations btrfs_dir_file_operations
= {
3815 .llseek
= generic_file_llseek
,
3816 .read
= generic_read_dir
,
3817 .readdir
= btrfs_real_readdir
,
3818 .unlocked_ioctl
= btrfs_ioctl
,
3819 #ifdef CONFIG_COMPAT
3820 .compat_ioctl
= btrfs_ioctl
,
3822 .release
= btrfs_release_file
,
3823 .fsync
= btrfs_sync_file
,
3826 static struct extent_io_ops btrfs_extent_io_ops
= {
3827 .fill_delalloc
= run_delalloc_range
,
3828 .submit_bio_hook
= btrfs_submit_bio_hook
,
3829 .merge_bio_hook
= btrfs_merge_bio_hook
,
3830 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3831 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3832 .writepage_start_hook
= btrfs_writepage_start_hook
,
3833 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3834 .set_bit_hook
= btrfs_set_bit_hook
,
3835 .clear_bit_hook
= btrfs_clear_bit_hook
,
3838 static struct address_space_operations btrfs_aops
= {
3839 .readpage
= btrfs_readpage
,
3840 .writepage
= btrfs_writepage
,
3841 .writepages
= btrfs_writepages
,
3842 .readpages
= btrfs_readpages
,
3843 .sync_page
= block_sync_page
,
3845 .direct_IO
= btrfs_direct_IO
,
3846 .invalidatepage
= btrfs_invalidatepage
,
3847 .releasepage
= btrfs_releasepage
,
3848 .set_page_dirty
= btrfs_set_page_dirty
,
3851 static struct address_space_operations btrfs_symlink_aops
= {
3852 .readpage
= btrfs_readpage
,
3853 .writepage
= btrfs_writepage
,
3854 .invalidatepage
= btrfs_invalidatepage
,
3855 .releasepage
= btrfs_releasepage
,
3858 static struct inode_operations btrfs_file_inode_operations
= {
3859 .truncate
= btrfs_truncate
,
3860 .getattr
= btrfs_getattr
,
3861 .setattr
= btrfs_setattr
,
3862 .setxattr
= btrfs_setxattr
,
3863 .getxattr
= btrfs_getxattr
,
3864 .listxattr
= btrfs_listxattr
,
3865 .removexattr
= btrfs_removexattr
,
3866 .permission
= btrfs_permission
,
3868 static struct inode_operations btrfs_special_inode_operations
= {
3869 .getattr
= btrfs_getattr
,
3870 .setattr
= btrfs_setattr
,
3871 .permission
= btrfs_permission
,
3872 .setxattr
= btrfs_setxattr
,
3873 .getxattr
= btrfs_getxattr
,
3874 .listxattr
= btrfs_listxattr
,
3875 .removexattr
= btrfs_removexattr
,
3877 static struct inode_operations btrfs_symlink_inode_operations
= {
3878 .readlink
= generic_readlink
,
3879 .follow_link
= page_follow_link_light
,
3880 .put_link
= page_put_link
,
3881 .permission
= btrfs_permission
,