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"
49 struct btrfs_iget_args
{
51 struct btrfs_root
*root
;
54 static struct inode_operations btrfs_dir_inode_operations
;
55 static struct inode_operations btrfs_symlink_inode_operations
;
56 static struct inode_operations btrfs_dir_ro_inode_operations
;
57 static struct inode_operations btrfs_special_inode_operations
;
58 static struct inode_operations btrfs_file_inode_operations
;
59 static struct address_space_operations btrfs_aops
;
60 static struct address_space_operations btrfs_symlink_aops
;
61 static struct file_operations btrfs_dir_file_operations
;
62 static struct extent_io_ops btrfs_extent_io_ops
;
64 static struct kmem_cache
*btrfs_inode_cachep
;
65 struct kmem_cache
*btrfs_trans_handle_cachep
;
66 struct kmem_cache
*btrfs_transaction_cachep
;
67 struct kmem_cache
*btrfs_bit_radix_cachep
;
68 struct kmem_cache
*btrfs_path_cachep
;
71 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
72 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
73 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
74 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
75 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
76 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
77 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
78 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
81 static void btrfs_truncate(struct inode
*inode
);
83 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
92 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
93 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
94 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
102 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
104 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
108 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
111 struct btrfs_trans_handle
*trans
;
115 u64 blocksize
= root
->sectorsize
;
117 struct btrfs_key ins
;
118 struct extent_map
*em
;
119 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
122 trans
= btrfs_join_transaction(root
, 1);
124 btrfs_set_trans_block_group(trans
, inode
);
126 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
127 num_bytes
= max(blocksize
, num_bytes
);
128 orig_num_bytes
= num_bytes
;
130 if (alloc_hint
== EXTENT_MAP_INLINE
)
133 BUG_ON(num_bytes
> btrfs_super_total_bytes(&root
->fs_info
->super_copy
));
134 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
135 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1);
136 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
138 while(num_bytes
> 0) {
139 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
140 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
141 root
->sectorsize
, 0, 0,
147 em
= alloc_extent_map(GFP_NOFS
);
149 em
->len
= ins
.offset
;
150 em
->block_start
= ins
.objectid
;
151 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
152 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
153 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
155 spin_lock(&em_tree
->lock
);
156 ret
= add_extent_mapping(em_tree
, em
);
157 spin_unlock(&em_tree
->lock
);
158 if (ret
!= -EEXIST
) {
162 btrfs_drop_extent_cache(inode
, start
,
163 start
+ ins
.offset
- 1);
165 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
167 cur_alloc_size
= ins
.offset
;
168 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
171 if (num_bytes
< cur_alloc_size
) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes
,
176 num_bytes
-= cur_alloc_size
;
177 alloc_hint
= ins
.objectid
+ ins
.offset
;
178 start
+= cur_alloc_size
;
181 btrfs_end_transaction(trans
, root
);
185 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
192 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
193 struct btrfs_block_group_cache
*block_group
;
194 struct btrfs_trans_handle
*trans
;
195 struct extent_buffer
*leaf
;
197 struct btrfs_path
*path
;
198 struct btrfs_file_extent_item
*item
;
201 struct btrfs_key found_key
;
203 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
204 path
= btrfs_alloc_path();
206 trans
= btrfs_join_transaction(root
, 1);
209 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
210 inode
->i_ino
, start
, 0);
217 if (path
->slots
[0] == 0)
222 leaf
= path
->nodes
[0];
223 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
224 struct btrfs_file_extent_item
);
226 /* are we inside the extent that was found? */
227 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
228 found_type
= btrfs_key_type(&found_key
);
229 if (found_key
.objectid
!= inode
->i_ino
||
230 found_type
!= BTRFS_EXTENT_DATA_KEY
)
233 found_type
= btrfs_file_extent_type(leaf
, item
);
234 extent_start
= found_key
.offset
;
235 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
236 u64 extent_num_bytes
;
238 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
239 extent_end
= extent_start
+ extent_num_bytes
;
242 if (loops
&& start
!= extent_start
)
245 if (start
< extent_start
|| start
>= extent_end
)
248 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
252 if (btrfs_cross_ref_exists(trans
, root
, &found_key
, bytenr
))
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
258 block_group
= btrfs_lookup_block_group(root
->fs_info
,
260 if (!block_group
|| block_group
->ro
)
263 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
264 extent_num_bytes
= min(end
+ 1, extent_end
) - start
;
265 ret
= btrfs_add_ordered_extent(inode
, start
, bytenr
,
266 extent_num_bytes
, 1);
272 btrfs_release_path(root
, path
);
280 btrfs_end_transaction(trans
, root
);
281 btrfs_free_path(path
);
282 return cow_file_range(inode
, start
, end
);
286 btrfs_end_transaction(trans
, root
);
287 btrfs_free_path(path
);
291 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
293 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
296 if (btrfs_test_opt(root
, NODATACOW
) ||
297 btrfs_test_flag(inode
, NODATACOW
))
298 ret
= run_delalloc_nocow(inode
, start
, end
);
300 ret
= cow_file_range(inode
, start
, end
);
305 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
306 unsigned long old
, unsigned long bits
)
309 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
310 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
311 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
312 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
313 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
314 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
315 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
316 &root
->fs_info
->delalloc_inodes
);
318 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
323 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
324 unsigned long old
, unsigned long bits
)
326 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
327 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
330 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
331 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
332 printk("warning: delalloc account %Lu %Lu\n",
333 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
334 root
->fs_info
->delalloc_bytes
= 0;
335 BTRFS_I(inode
)->delalloc_bytes
= 0;
337 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
338 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
340 if (BTRFS_I(inode
)->delalloc_bytes
== 0 &&
341 !list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
342 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
344 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
349 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
350 size_t size
, struct bio
*bio
)
352 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
353 struct btrfs_mapping_tree
*map_tree
;
354 u64 logical
= bio
->bi_sector
<< 9;
359 length
= bio
->bi_size
;
360 map_tree
= &root
->fs_info
->mapping_tree
;
362 ret
= btrfs_map_block(map_tree
, READ
, logical
,
363 &map_length
, NULL
, 0);
365 if (map_length
< length
+ size
) {
371 int __btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
374 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
377 ret
= btrfs_csum_one_bio(root
, inode
, bio
);
380 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
383 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
386 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
389 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
392 if (!(rw
& (1 << BIO_RW
))) {
396 if (btrfs_test_opt(root
, NODATASUM
) ||
397 btrfs_test_flag(inode
, NODATASUM
)) {
401 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
402 inode
, rw
, bio
, mirror_num
,
403 __btrfs_submit_bio_hook
);
405 return btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
408 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
409 struct inode
*inode
, u64 file_offset
,
410 struct list_head
*list
)
412 struct list_head
*cur
;
413 struct btrfs_ordered_sum
*sum
;
415 btrfs_set_trans_block_group(trans
, inode
);
416 list_for_each(cur
, list
) {
417 sum
= list_entry(cur
, struct btrfs_ordered_sum
, list
);
418 mutex_lock(&BTRFS_I(inode
)->csum_mutex
);
419 btrfs_csum_file_blocks(trans
, BTRFS_I(inode
)->root
,
421 mutex_unlock(&BTRFS_I(inode
)->csum_mutex
);
426 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
)
428 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
432 struct btrfs_writepage_fixup
{
434 struct btrfs_work work
;
437 /* see btrfs_writepage_start_hook for details on why this is required */
438 void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
440 struct btrfs_writepage_fixup
*fixup
;
441 struct btrfs_ordered_extent
*ordered
;
447 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
451 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
452 ClearPageChecked(page
);
456 inode
= page
->mapping
->host
;
457 page_start
= page_offset(page
);
458 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
460 lock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
462 /* already ordered? We're done */
463 if (test_range_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
464 EXTENT_ORDERED
, 0)) {
468 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
470 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
,
473 btrfs_start_ordered_extent(inode
, ordered
, 1);
477 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
478 ClearPageChecked(page
);
480 unlock_extent(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, GFP_NOFS
);
483 page_cache_release(page
);
487 * There are a few paths in the higher layers of the kernel that directly
488 * set the page dirty bit without asking the filesystem if it is a
489 * good idea. This causes problems because we want to make sure COW
490 * properly happens and the data=ordered rules are followed.
492 * In our case any range that doesn't have the EXTENT_ORDERED bit set
493 * hasn't been properly setup for IO. We kick off an async process
494 * to fix it up. The async helper will wait for ordered extents, set
495 * the delalloc bit and make it safe to write the page.
497 int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
499 struct inode
*inode
= page
->mapping
->host
;
500 struct btrfs_writepage_fixup
*fixup
;
501 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
504 ret
= test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
509 if (PageChecked(page
))
512 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
516 SetPageChecked(page
);
517 page_cache_get(page
);
518 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
520 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
524 static int btrfs_finish_ordered_io(struct inode
*inode
, u64 start
, u64 end
)
526 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
527 struct btrfs_trans_handle
*trans
;
528 struct btrfs_ordered_extent
*ordered_extent
;
529 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
531 struct list_head list
;
532 struct btrfs_key ins
;
535 ret
= btrfs_dec_test_ordered_pending(inode
, start
, end
- start
+ 1);
539 trans
= btrfs_join_transaction(root
, 1);
541 ordered_extent
= btrfs_lookup_ordered_extent(inode
, start
);
542 BUG_ON(!ordered_extent
);
543 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
))
546 lock_extent(io_tree
, ordered_extent
->file_offset
,
547 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
550 INIT_LIST_HEAD(&list
);
552 ins
.objectid
= ordered_extent
->start
;
553 ins
.offset
= ordered_extent
->len
;
554 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
556 ret
= btrfs_alloc_reserved_extent(trans
, root
, root
->root_key
.objectid
,
557 trans
->transid
, inode
->i_ino
,
558 ordered_extent
->file_offset
, &ins
);
561 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
563 ret
= btrfs_drop_extents(trans
, root
, inode
,
564 ordered_extent
->file_offset
,
565 ordered_extent
->file_offset
+
567 ordered_extent
->file_offset
, &alloc_hint
);
569 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
570 ordered_extent
->file_offset
,
571 ordered_extent
->start
,
573 ordered_extent
->len
, 0);
576 btrfs_drop_extent_cache(inode
, ordered_extent
->file_offset
,
577 ordered_extent
->file_offset
+
578 ordered_extent
->len
- 1);
579 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
581 inode
->i_blocks
+= ordered_extent
->len
>> 9;
582 unlock_extent(io_tree
, ordered_extent
->file_offset
,
583 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
586 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
587 &ordered_extent
->list
);
589 btrfs_ordered_update_i_size(inode
, ordered_extent
);
590 btrfs_remove_ordered_extent(inode
, ordered_extent
);
593 btrfs_put_ordered_extent(ordered_extent
);
594 /* once for the tree */
595 btrfs_put_ordered_extent(ordered_extent
);
597 btrfs_update_inode(trans
, root
, inode
);
598 btrfs_end_transaction(trans
, root
);
602 int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
603 struct extent_state
*state
, int uptodate
)
605 return btrfs_finish_ordered_io(page
->mapping
->host
, start
, end
);
608 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
611 struct inode
*inode
= page
->mapping
->host
;
612 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
613 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
614 struct btrfs_csum_item
*item
;
615 struct btrfs_path
*path
= NULL
;
618 if (btrfs_test_opt(root
, NODATASUM
) ||
619 btrfs_test_flag(inode
, NODATASUM
))
623 * It is possible there is an ordered extent that has
624 * not yet finished for this range in the file. If so,
625 * that extent will have a csum cached, and it will insert
626 * the sum after all the blocks in the extent are fully
627 * on disk. So, look for an ordered extent and use the
628 * sum if found. We have to do this before looking in the
629 * btree because csum items are pre-inserted based on
630 * the file size. btrfs_lookup_csum might find an item
631 * that still hasn't been fully filled.
633 ret
= btrfs_find_ordered_sum(inode
, start
, &csum
);
638 path
= btrfs_alloc_path();
639 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
642 /* a csum that isn't present is a preallocated region. */
643 if (ret
== -ENOENT
|| ret
== -EFBIG
)
646 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
,
650 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
653 set_state_private(io_tree
, start
, csum
);
656 btrfs_free_path(path
);
660 struct io_failure_record
{
668 int btrfs_io_failed_hook(struct bio
*failed_bio
,
669 struct page
*page
, u64 start
, u64 end
,
670 struct extent_state
*state
)
672 struct io_failure_record
*failrec
= NULL
;
674 struct extent_map
*em
;
675 struct inode
*inode
= page
->mapping
->host
;
676 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
677 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
684 ret
= get_state_private(failure_tree
, start
, &private);
686 failrec
= kmalloc(sizeof(*failrec
), GFP_NOFS
);
689 failrec
->start
= start
;
690 failrec
->len
= end
- start
+ 1;
691 failrec
->last_mirror
= 0;
693 spin_lock(&em_tree
->lock
);
694 em
= lookup_extent_mapping(em_tree
, start
, failrec
->len
);
695 if (em
->start
> start
|| em
->start
+ em
->len
< start
) {
699 spin_unlock(&em_tree
->lock
);
701 if (!em
|| IS_ERR(em
)) {
705 logical
= start
- em
->start
;
706 logical
= em
->block_start
+ logical
;
707 failrec
->logical
= logical
;
709 set_extent_bits(failure_tree
, start
, end
, EXTENT_LOCKED
|
710 EXTENT_DIRTY
, GFP_NOFS
);
711 set_state_private(failure_tree
, start
,
712 (u64
)(unsigned long)failrec
);
714 failrec
= (struct io_failure_record
*)(unsigned long)private;
716 num_copies
= btrfs_num_copies(
717 &BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
718 failrec
->logical
, failrec
->len
);
719 failrec
->last_mirror
++;
721 spin_lock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
722 state
= find_first_extent_bit_state(&BTRFS_I(inode
)->io_tree
,
725 if (state
&& state
->start
!= failrec
->start
)
727 spin_unlock_irq(&BTRFS_I(inode
)->io_tree
.lock
);
729 if (!state
|| failrec
->last_mirror
> num_copies
) {
730 set_state_private(failure_tree
, failrec
->start
, 0);
731 clear_extent_bits(failure_tree
, failrec
->start
,
732 failrec
->start
+ failrec
->len
- 1,
733 EXTENT_LOCKED
| EXTENT_DIRTY
, GFP_NOFS
);
737 bio
= bio_alloc(GFP_NOFS
, 1);
738 bio
->bi_private
= state
;
739 bio
->bi_end_io
= failed_bio
->bi_end_io
;
740 bio
->bi_sector
= failrec
->logical
>> 9;
741 bio
->bi_bdev
= failed_bio
->bi_bdev
;
743 bio_add_page(bio
, page
, failrec
->len
, start
- page_offset(page
));
744 if (failed_bio
->bi_rw
& (1 << BIO_RW
))
749 BTRFS_I(inode
)->io_tree
.ops
->submit_bio_hook(inode
, rw
, bio
,
750 failrec
->last_mirror
);
754 int btrfs_clean_io_failures(struct inode
*inode
, u64 start
)
758 struct io_failure_record
*failure
;
762 if (count_range_bits(&BTRFS_I(inode
)->io_failure_tree
, &private,
763 (u64
)-1, 1, EXTENT_DIRTY
)) {
764 ret
= get_state_private(&BTRFS_I(inode
)->io_failure_tree
,
765 start
, &private_failure
);
767 failure
= (struct io_failure_record
*)(unsigned long)
769 set_state_private(&BTRFS_I(inode
)->io_failure_tree
,
771 clear_extent_bits(&BTRFS_I(inode
)->io_failure_tree
,
773 failure
->start
+ failure
->len
- 1,
774 EXTENT_DIRTY
| EXTENT_LOCKED
,
782 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
783 struct extent_state
*state
)
785 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
786 struct inode
*inode
= page
->mapping
->host
;
787 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
789 u64
private = ~(u32
)0;
791 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
795 if (btrfs_test_opt(root
, NODATASUM
) ||
796 btrfs_test_flag(inode
, NODATASUM
))
798 if (state
&& state
->start
== start
) {
799 private = state
->private;
802 ret
= get_state_private(io_tree
, start
, &private);
804 local_irq_save(flags
);
805 kaddr
= kmap_atomic(page
, KM_IRQ0
);
809 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
810 btrfs_csum_final(csum
, (char *)&csum
);
811 if (csum
!= private) {
814 kunmap_atomic(kaddr
, KM_IRQ0
);
815 local_irq_restore(flags
);
817 /* if the io failure tree for this inode is non-empty,
818 * check to see if we've recovered from a failed IO
820 btrfs_clean_io_failures(inode
, start
);
824 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
825 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
827 memset(kaddr
+ offset
, 1, end
- start
+ 1);
828 flush_dcache_page(page
);
829 kunmap_atomic(kaddr
, KM_IRQ0
);
830 local_irq_restore(flags
);
837 * This creates an orphan entry for the given inode in case something goes
838 * wrong in the middle of an unlink/truncate.
840 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
842 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
845 spin_lock(&root
->list_lock
);
847 /* already on the orphan list, we're good */
848 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
849 spin_unlock(&root
->list_lock
);
853 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
855 spin_unlock(&root
->list_lock
);
858 * insert an orphan item to track this unlinked/truncated file
860 ret
= btrfs_insert_orphan_item(trans
, root
, inode
->i_ino
);
866 * We have done the truncate/delete so we can go ahead and remove the orphan
867 * item for this particular inode.
869 int btrfs_orphan_del(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
871 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
874 spin_lock(&root
->list_lock
);
876 if (list_empty(&BTRFS_I(inode
)->i_orphan
)) {
877 spin_unlock(&root
->list_lock
);
881 list_del_init(&BTRFS_I(inode
)->i_orphan
);
883 spin_unlock(&root
->list_lock
);
887 spin_unlock(&root
->list_lock
);
889 ret
= btrfs_del_orphan_item(trans
, root
, inode
->i_ino
);
895 * this cleans up any orphans that may be left on the list from the last use
898 void btrfs_orphan_cleanup(struct btrfs_root
*root
)
900 struct btrfs_path
*path
;
901 struct extent_buffer
*leaf
;
902 struct btrfs_item
*item
;
903 struct btrfs_key key
, found_key
;
904 struct btrfs_trans_handle
*trans
;
906 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
908 /* don't do orphan cleanup if the fs is readonly. */
909 if (root
->inode
->i_sb
->s_flags
& MS_RDONLY
)
912 path
= btrfs_alloc_path();
917 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
918 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
919 key
.offset
= (u64
)-1;
921 trans
= btrfs_start_transaction(root
, 1);
922 btrfs_set_trans_block_group(trans
, root
->inode
);
925 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
927 printk(KERN_ERR
"Error searching slot for orphan: %d"
933 * if ret == 0 means we found what we were searching for, which
934 * is weird, but possible, so only screw with path if we didnt
935 * find the key and see if we have stuff that matches
938 if (path
->slots
[0] == 0)
943 /* pull out the item */
944 leaf
= path
->nodes
[0];
945 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
946 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
948 /* make sure the item matches what we want */
949 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
951 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
954 /* release the path since we're done with it */
955 btrfs_release_path(root
, path
);
958 * this is where we are basically btrfs_lookup, without the
959 * crossing root thing. we store the inode number in the
960 * offset of the orphan item.
962 inode
= btrfs_iget_locked(root
->inode
->i_sb
,
963 found_key
.offset
, root
);
967 if (inode
->i_state
& I_NEW
) {
968 BTRFS_I(inode
)->root
= root
;
970 /* have to set the location manually */
971 BTRFS_I(inode
)->location
.objectid
= inode
->i_ino
;
972 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
973 BTRFS_I(inode
)->location
.offset
= 0;
975 btrfs_read_locked_inode(inode
);
976 unlock_new_inode(inode
);
980 * add this inode to the orphan list so btrfs_orphan_del does
981 * the proper thing when we hit it
983 spin_lock(&root
->list_lock
);
984 list_add(&BTRFS_I(inode
)->i_orphan
, &root
->orphan_list
);
985 spin_unlock(&root
->list_lock
);
988 * if this is a bad inode, means we actually succeeded in
989 * removing the inode, but not the orphan record, which means
990 * we need to manually delete the orphan since iput will just
993 if (is_bad_inode(inode
)) {
994 btrfs_orphan_del(trans
, inode
);
999 /* if we have links, this was a truncate, lets do that */
1000 if (inode
->i_nlink
) {
1002 btrfs_truncate(inode
);
1007 /* this will do delete_inode and everything for us */
1012 printk(KERN_INFO
"btrfs: unlinked %d orphans\n", nr_unlink
);
1014 printk(KERN_INFO
"btrfs: truncated %d orphans\n", nr_truncate
);
1016 btrfs_free_path(path
);
1017 btrfs_end_transaction(trans
, root
);
1020 void btrfs_read_locked_inode(struct inode
*inode
)
1022 struct btrfs_path
*path
;
1023 struct extent_buffer
*leaf
;
1024 struct btrfs_inode_item
*inode_item
;
1025 struct btrfs_timespec
*tspec
;
1026 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1027 struct btrfs_key location
;
1028 u64 alloc_group_block
;
1032 path
= btrfs_alloc_path();
1034 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
1036 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
1040 leaf
= path
->nodes
[0];
1041 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1042 struct btrfs_inode_item
);
1044 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
1045 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
1046 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
1047 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
1048 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
1050 tspec
= btrfs_inode_atime(inode_item
);
1051 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1052 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1054 tspec
= btrfs_inode_mtime(inode_item
);
1055 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1056 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1058 tspec
= btrfs_inode_ctime(inode_item
);
1059 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
1060 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
1062 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
1063 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
1065 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
1067 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1069 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
1070 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
1072 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
1073 if (!BTRFS_I(inode
)->block_group
) {
1074 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
1076 BTRFS_BLOCK_GROUP_METADATA
, 0);
1078 btrfs_free_path(path
);
1081 switch (inode
->i_mode
& S_IFMT
) {
1083 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1084 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1085 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1086 inode
->i_fop
= &btrfs_file_operations
;
1087 inode
->i_op
= &btrfs_file_inode_operations
;
1090 inode
->i_fop
= &btrfs_dir_file_operations
;
1091 if (root
== root
->fs_info
->tree_root
)
1092 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
1094 inode
->i_op
= &btrfs_dir_inode_operations
;
1097 inode
->i_op
= &btrfs_symlink_inode_operations
;
1098 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
1099 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1102 init_special_inode(inode
, inode
->i_mode
, rdev
);
1108 btrfs_free_path(path
);
1109 make_bad_inode(inode
);
1112 static void fill_inode_item(struct extent_buffer
*leaf
,
1113 struct btrfs_inode_item
*item
,
1114 struct inode
*inode
)
1116 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
1117 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
1118 btrfs_set_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
);
1119 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
1120 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
1122 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
1123 inode
->i_atime
.tv_sec
);
1124 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
1125 inode
->i_atime
.tv_nsec
);
1127 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
1128 inode
->i_mtime
.tv_sec
);
1129 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
1130 inode
->i_mtime
.tv_nsec
);
1132 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
1133 inode
->i_ctime
.tv_sec
);
1134 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
1135 inode
->i_ctime
.tv_nsec
);
1137 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
1138 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
1139 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
1140 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
1141 btrfs_set_inode_block_group(leaf
, item
,
1142 BTRFS_I(inode
)->block_group
->key
.objectid
);
1145 int noinline
btrfs_update_inode(struct btrfs_trans_handle
*trans
,
1146 struct btrfs_root
*root
,
1147 struct inode
*inode
)
1149 struct btrfs_inode_item
*inode_item
;
1150 struct btrfs_path
*path
;
1151 struct extent_buffer
*leaf
;
1154 path
= btrfs_alloc_path();
1156 ret
= btrfs_lookup_inode(trans
, root
, path
,
1157 &BTRFS_I(inode
)->location
, 1);
1164 leaf
= path
->nodes
[0];
1165 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1166 struct btrfs_inode_item
);
1168 fill_inode_item(leaf
, inode_item
, inode
);
1169 btrfs_mark_buffer_dirty(leaf
);
1170 btrfs_set_inode_last_trans(trans
, inode
);
1173 btrfs_free_path(path
);
1178 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
1179 struct btrfs_root
*root
,
1181 struct dentry
*dentry
)
1183 struct btrfs_path
*path
;
1184 const char *name
= dentry
->d_name
.name
;
1185 int name_len
= dentry
->d_name
.len
;
1187 struct extent_buffer
*leaf
;
1188 struct btrfs_dir_item
*di
;
1189 struct btrfs_key key
;
1192 path
= btrfs_alloc_path();
1198 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
1199 name
, name_len
, -1);
1208 leaf
= path
->nodes
[0];
1209 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
1210 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1213 btrfs_release_path(root
, path
);
1215 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
1216 dentry
->d_inode
->i_ino
,
1217 dentry
->d_parent
->d_inode
->i_ino
, &index
);
1219 printk("failed to delete reference to %.*s, "
1220 "inode %lu parent %lu\n", name_len
, name
,
1221 dentry
->d_inode
->i_ino
,
1222 dentry
->d_parent
->d_inode
->i_ino
);
1226 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
1227 index
, name
, name_len
, -1);
1236 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
1237 btrfs_release_path(root
, path
);
1239 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
1241 btrfs_free_path(path
);
1243 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
1244 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
1245 btrfs_update_inode(trans
, root
, dir
);
1246 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1247 dentry
->d_inode
->i_nlink
--;
1249 drop_nlink(dentry
->d_inode
);
1251 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
1252 dir
->i_sb
->s_dirt
= 1;
1257 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1259 struct btrfs_root
*root
;
1260 struct btrfs_trans_handle
*trans
;
1261 struct inode
*inode
= dentry
->d_inode
;
1263 unsigned long nr
= 0;
1265 root
= BTRFS_I(dir
)->root
;
1267 ret
= btrfs_check_free_space(root
, 1, 1);
1271 trans
= btrfs_start_transaction(root
, 1);
1273 btrfs_set_trans_block_group(trans
, dir
);
1274 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1276 if (inode
->i_nlink
== 0)
1277 ret
= btrfs_orphan_add(trans
, inode
);
1279 nr
= trans
->blocks_used
;
1281 btrfs_end_transaction_throttle(trans
, root
);
1283 btrfs_btree_balance_dirty(root
, nr
);
1287 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1289 struct inode
*inode
= dentry
->d_inode
;
1292 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1293 struct btrfs_trans_handle
*trans
;
1294 unsigned long nr
= 0;
1296 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
1300 ret
= btrfs_check_free_space(root
, 1, 1);
1304 trans
= btrfs_start_transaction(root
, 1);
1305 btrfs_set_trans_block_group(trans
, dir
);
1307 err
= btrfs_orphan_add(trans
, inode
);
1311 /* now the directory is empty */
1312 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
1314 btrfs_i_size_write(inode
, 0);
1318 nr
= trans
->blocks_used
;
1319 ret
= btrfs_end_transaction_throttle(trans
, root
);
1321 btrfs_btree_balance_dirty(root
, nr
);
1329 * this can truncate away extent items, csum items and directory items.
1330 * It starts at a high offset and removes keys until it can't find
1331 * any higher than i_size.
1333 * csum items that cross the new i_size are truncated to the new size
1336 * min_type is the minimum key type to truncate down to. If set to 0, this
1337 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1339 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
1340 struct btrfs_root
*root
,
1341 struct inode
*inode
,
1345 struct btrfs_path
*path
;
1346 struct btrfs_key key
;
1347 struct btrfs_key found_key
;
1349 struct extent_buffer
*leaf
;
1350 struct btrfs_file_extent_item
*fi
;
1351 u64 extent_start
= 0;
1352 u64 extent_num_bytes
= 0;
1358 int pending_del_nr
= 0;
1359 int pending_del_slot
= 0;
1360 int extent_type
= -1;
1361 u64 mask
= root
->sectorsize
- 1;
1363 btrfs_drop_extent_cache(inode
, inode
->i_size
& (~mask
), (u64
)-1);
1364 path
= btrfs_alloc_path();
1368 /* FIXME, add redo link to tree so we don't leak on crash */
1369 key
.objectid
= inode
->i_ino
;
1370 key
.offset
= (u64
)-1;
1373 btrfs_init_path(path
);
1375 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1380 BUG_ON(path
->slots
[0] == 0);
1386 leaf
= path
->nodes
[0];
1387 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1388 found_type
= btrfs_key_type(&found_key
);
1390 if (found_key
.objectid
!= inode
->i_ino
)
1393 if (found_type
< min_type
)
1396 item_end
= found_key
.offset
;
1397 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1398 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1399 struct btrfs_file_extent_item
);
1400 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1401 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1403 btrfs_file_extent_num_bytes(leaf
, fi
);
1404 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1405 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
1407 item_end
+= btrfs_file_extent_inline_len(leaf
,
1412 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
1413 ret
= btrfs_csum_truncate(trans
, root
, path
,
1417 if (item_end
< inode
->i_size
) {
1418 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
1419 found_type
= BTRFS_INODE_ITEM_KEY
;
1420 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
1421 found_type
= BTRFS_CSUM_ITEM_KEY
;
1422 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
1423 found_type
= BTRFS_XATTR_ITEM_KEY
;
1424 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
1425 found_type
= BTRFS_INODE_REF_KEY
;
1426 } else if (found_type
) {
1431 btrfs_set_key_type(&key
, found_type
);
1434 if (found_key
.offset
>= inode
->i_size
)
1440 /* FIXME, shrink the extent if the ref count is only 1 */
1441 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
1444 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
1446 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1448 u64 orig_num_bytes
=
1449 btrfs_file_extent_num_bytes(leaf
, fi
);
1450 extent_num_bytes
= inode
->i_size
-
1451 found_key
.offset
+ root
->sectorsize
- 1;
1452 extent_num_bytes
= extent_num_bytes
&
1453 ~((u64
)root
->sectorsize
- 1);
1454 btrfs_set_file_extent_num_bytes(leaf
, fi
,
1456 num_dec
= (orig_num_bytes
-
1458 if (extent_start
!= 0)
1459 dec_i_blocks(inode
, num_dec
);
1460 btrfs_mark_buffer_dirty(leaf
);
1463 btrfs_file_extent_disk_num_bytes(leaf
,
1465 /* FIXME blocksize != 4096 */
1466 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
1467 if (extent_start
!= 0) {
1469 dec_i_blocks(inode
, num_dec
);
1471 root_gen
= btrfs_header_generation(leaf
);
1472 root_owner
= btrfs_header_owner(leaf
);
1474 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1476 u32 newsize
= inode
->i_size
- found_key
.offset
;
1477 dec_i_blocks(inode
, item_end
+ 1 -
1478 found_key
.offset
- newsize
);
1480 btrfs_file_extent_calc_inline_size(newsize
);
1481 ret
= btrfs_truncate_item(trans
, root
, path
,
1485 dec_i_blocks(inode
, item_end
+ 1 -
1491 if (!pending_del_nr
) {
1492 /* no pending yet, add ourselves */
1493 pending_del_slot
= path
->slots
[0];
1495 } else if (pending_del_nr
&&
1496 path
->slots
[0] + 1 == pending_del_slot
) {
1497 /* hop on the pending chunk */
1499 pending_del_slot
= path
->slots
[0];
1501 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
1507 ret
= btrfs_free_extent(trans
, root
, extent_start
,
1510 root_gen
, inode
->i_ino
,
1511 found_key
.offset
, 0);
1515 if (path
->slots
[0] == 0) {
1518 btrfs_release_path(root
, path
);
1523 if (pending_del_nr
&&
1524 path
->slots
[0] + 1 != pending_del_slot
) {
1525 struct btrfs_key debug
;
1527 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
1529 ret
= btrfs_del_items(trans
, root
, path
,
1534 btrfs_release_path(root
, path
);
1540 if (pending_del_nr
) {
1541 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
1544 btrfs_free_path(path
);
1545 inode
->i_sb
->s_dirt
= 1;
1550 * taken from block_truncate_page, but does cow as it zeros out
1551 * any bytes left in the last page in the file.
1553 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1555 struct inode
*inode
= mapping
->host
;
1556 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1557 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1558 struct btrfs_ordered_extent
*ordered
;
1560 u32 blocksize
= root
->sectorsize
;
1561 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1562 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1568 if ((offset
& (blocksize
- 1)) == 0)
1573 page
= grab_cache_page(mapping
, index
);
1577 page_start
= page_offset(page
);
1578 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
1580 if (!PageUptodate(page
)) {
1581 ret
= btrfs_readpage(NULL
, page
);
1583 if (page
->mapping
!= mapping
) {
1585 page_cache_release(page
);
1588 if (!PageUptodate(page
)) {
1593 wait_on_page_writeback(page
);
1595 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1596 set_page_extent_mapped(page
);
1598 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1600 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1602 page_cache_release(page
);
1603 btrfs_start_ordered_extent(inode
, ordered
, 1);
1604 btrfs_put_ordered_extent(ordered
);
1608 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
1610 if (offset
!= PAGE_CACHE_SIZE
) {
1612 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1613 flush_dcache_page(page
);
1616 ClearPageChecked(page
);
1617 set_page_dirty(page
);
1618 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
1622 page_cache_release(page
);
1627 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1629 struct inode
*inode
= dentry
->d_inode
;
1632 err
= inode_change_ok(inode
, attr
);
1636 if (S_ISREG(inode
->i_mode
) &&
1637 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1638 struct btrfs_trans_handle
*trans
;
1639 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1640 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1642 u64 mask
= root
->sectorsize
- 1;
1643 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1644 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1648 if (attr
->ia_size
<= hole_start
)
1651 err
= btrfs_check_free_space(root
, 1, 0);
1655 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1657 hole_size
= block_end
- hole_start
;
1658 btrfs_wait_ordered_range(inode
, hole_start
, hole_size
);
1659 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1661 trans
= btrfs_start_transaction(root
, 1);
1662 btrfs_set_trans_block_group(trans
, inode
);
1663 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
1664 err
= btrfs_drop_extents(trans
, root
, inode
,
1665 hole_start
, block_end
, hole_start
,
1668 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1669 err
= btrfs_insert_file_extent(trans
, root
,
1673 btrfs_drop_extent_cache(inode
, hole_start
,
1675 btrfs_check_file(root
, inode
);
1677 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
1678 btrfs_end_transaction(trans
, root
);
1679 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1684 err
= inode_setattr(inode
, attr
);
1686 if (!err
&& ((attr
->ia_valid
& ATTR_MODE
)))
1687 err
= btrfs_acl_chmod(inode
);
1692 void btrfs_delete_inode(struct inode
*inode
)
1694 struct btrfs_trans_handle
*trans
;
1695 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1699 truncate_inode_pages(&inode
->i_data
, 0);
1700 if (is_bad_inode(inode
)) {
1701 btrfs_orphan_del(NULL
, inode
);
1704 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1706 btrfs_i_size_write(inode
, 0);
1707 trans
= btrfs_start_transaction(root
, 1);
1709 btrfs_set_trans_block_group(trans
, inode
);
1710 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1712 btrfs_orphan_del(NULL
, inode
);
1713 goto no_delete_lock
;
1716 btrfs_orphan_del(trans
, inode
);
1718 nr
= trans
->blocks_used
;
1721 btrfs_end_transaction(trans
, root
);
1722 btrfs_btree_balance_dirty(root
, nr
);
1726 nr
= trans
->blocks_used
;
1727 btrfs_end_transaction(trans
, root
);
1728 btrfs_btree_balance_dirty(root
, nr
);
1734 * this returns the key found in the dir entry in the location pointer.
1735 * If no dir entries were found, location->objectid is 0.
1737 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1738 struct btrfs_key
*location
)
1740 const char *name
= dentry
->d_name
.name
;
1741 int namelen
= dentry
->d_name
.len
;
1742 struct btrfs_dir_item
*di
;
1743 struct btrfs_path
*path
;
1744 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1747 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1748 location
->objectid
= dir
->i_ino
;
1749 location
->type
= BTRFS_INODE_ITEM_KEY
;
1750 location
->offset
= 0;
1753 path
= btrfs_alloc_path();
1756 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1757 struct btrfs_key key
;
1758 struct extent_buffer
*leaf
;
1761 key
.objectid
= dir
->i_ino
;
1762 key
.offset
= (u64
)-1;
1763 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1764 if (ret
< 0 || path
->slots
[0] == 0)
1766 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1769 leaf
= path
->nodes
[0];
1770 slot
= path
->slots
[0] - 1;
1772 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1773 if (key
.objectid
!= dir
->i_ino
||
1774 key
.type
!= BTRFS_INODE_REF_KEY
) {
1777 location
->objectid
= key
.offset
;
1778 location
->type
= BTRFS_INODE_ITEM_KEY
;
1779 location
->offset
= 0;
1783 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1787 if (!di
|| IS_ERR(di
)) {
1790 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1792 btrfs_free_path(path
);
1795 location
->objectid
= 0;
1800 * when we hit a tree root in a directory, the btrfs part of the inode
1801 * needs to be changed to reflect the root directory of the tree root. This
1802 * is kind of like crossing a mount point.
1804 static int fixup_tree_root_location(struct btrfs_root
*root
,
1805 struct btrfs_key
*location
,
1806 struct btrfs_root
**sub_root
,
1807 struct dentry
*dentry
)
1809 struct btrfs_root_item
*ri
;
1811 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1813 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1816 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1817 dentry
->d_name
.name
,
1818 dentry
->d_name
.len
);
1819 if (IS_ERR(*sub_root
))
1820 return PTR_ERR(*sub_root
);
1822 ri
= &(*sub_root
)->root_item
;
1823 location
->objectid
= btrfs_root_dirid(ri
);
1824 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1825 location
->offset
= 0;
1830 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1832 struct btrfs_iget_args
*args
= p
;
1833 inode
->i_ino
= args
->ino
;
1834 BTRFS_I(inode
)->root
= args
->root
;
1835 BTRFS_I(inode
)->delalloc_bytes
= 0;
1836 inode
->i_mapping
->writeback_index
= 0;
1837 BTRFS_I(inode
)->disk_i_size
= 0;
1838 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1839 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1840 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1841 inode
->i_mapping
, GFP_NOFS
);
1842 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
1843 inode
->i_mapping
, GFP_NOFS
);
1844 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
1845 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
1846 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
1847 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
1851 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1853 struct btrfs_iget_args
*args
= opaque
;
1854 return (args
->ino
== inode
->i_ino
&&
1855 args
->root
== BTRFS_I(inode
)->root
);
1858 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1861 struct btrfs_iget_args args
;
1862 args
.ino
= objectid
;
1863 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1868 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1871 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1872 struct btrfs_root
*root
)
1874 struct inode
*inode
;
1875 struct btrfs_iget_args args
;
1876 args
.ino
= objectid
;
1879 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1880 btrfs_init_locked_inode
,
1885 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1886 struct nameidata
*nd
)
1888 struct inode
* inode
;
1889 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1890 struct btrfs_root
*root
= bi
->root
;
1891 struct btrfs_root
*sub_root
= root
;
1892 struct btrfs_key location
;
1893 int ret
, do_orphan
= 0;
1895 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1896 return ERR_PTR(-ENAMETOOLONG
);
1898 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1901 return ERR_PTR(ret
);
1904 if (location
.objectid
) {
1905 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1908 return ERR_PTR(ret
);
1910 return ERR_PTR(-ENOENT
);
1912 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1915 return ERR_PTR(-EACCES
);
1916 if (inode
->i_state
& I_NEW
) {
1917 /* the inode and parent dir are two different roots */
1918 if (sub_root
!= root
) {
1920 sub_root
->inode
= inode
;
1923 BTRFS_I(inode
)->root
= sub_root
;
1924 memcpy(&BTRFS_I(inode
)->location
, &location
,
1926 btrfs_read_locked_inode(inode
);
1927 unlock_new_inode(inode
);
1931 if (unlikely(do_orphan
))
1932 btrfs_orphan_cleanup(sub_root
);
1934 return d_splice_alias(inode
, dentry
);
1937 static unsigned char btrfs_filetype_table
[] = {
1938 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1941 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1943 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1944 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1945 struct btrfs_item
*item
;
1946 struct btrfs_dir_item
*di
;
1947 struct btrfs_key key
;
1948 struct btrfs_key found_key
;
1949 struct btrfs_path
*path
;
1952 struct extent_buffer
*leaf
;
1955 unsigned char d_type
;
1960 int key_type
= BTRFS_DIR_INDEX_KEY
;
1965 /* FIXME, use a real flag for deciding about the key type */
1966 if (root
->fs_info
->tree_root
== root
)
1967 key_type
= BTRFS_DIR_ITEM_KEY
;
1969 /* special case for "." */
1970 if (filp
->f_pos
== 0) {
1971 over
= filldir(dirent
, ".", 1,
1979 key
.objectid
= inode
->i_ino
;
1980 path
= btrfs_alloc_path();
1983 /* special case for .., just use the back ref */
1984 if (filp
->f_pos
== 1) {
1985 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1986 key
.offset
= (u64
)-1;
1987 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1988 if (ret
< 0 || path
->slots
[0] == 0) {
1989 btrfs_release_path(root
, path
);
1990 goto read_dir_items
;
1993 leaf
= path
->nodes
[0];
1994 slot
= path
->slots
[0] - 1;
1995 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1996 btrfs_release_path(root
, path
);
1997 if (found_key
.objectid
!= key
.objectid
||
1998 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1999 goto read_dir_items
;
2000 over
= filldir(dirent
, "..", 2,
2001 2, found_key
.offset
, DT_DIR
);
2008 btrfs_set_key_type(&key
, key_type
);
2009 key
.offset
= filp
->f_pos
;
2011 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2016 leaf
= path
->nodes
[0];
2017 nritems
= btrfs_header_nritems(leaf
);
2018 slot
= path
->slots
[0];
2019 if (advance
|| slot
>= nritems
) {
2020 if (slot
>= nritems
-1) {
2021 ret
= btrfs_next_leaf(root
, path
);
2024 leaf
= path
->nodes
[0];
2025 nritems
= btrfs_header_nritems(leaf
);
2026 slot
= path
->slots
[0];
2033 item
= btrfs_item_nr(leaf
, slot
);
2034 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2036 if (found_key
.objectid
!= key
.objectid
)
2038 if (btrfs_key_type(&found_key
) != key_type
)
2040 if (found_key
.offset
< filp
->f_pos
)
2043 filp
->f_pos
= found_key
.offset
;
2045 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
2047 di_total
= btrfs_item_size(leaf
, item
);
2048 while(di_cur
< di_total
) {
2049 struct btrfs_key location
;
2051 name_len
= btrfs_dir_name_len(leaf
, di
);
2052 if (name_len
< 32) {
2053 name_ptr
= tmp_name
;
2055 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
2058 read_extent_buffer(leaf
, name_ptr
,
2059 (unsigned long)(di
+ 1), name_len
);
2061 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
2062 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
2063 over
= filldir(dirent
, name_ptr
, name_len
,
2068 if (name_ptr
!= tmp_name
)
2073 di_len
= btrfs_dir_name_len(leaf
, di
) +
2074 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
2076 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
2079 if (key_type
== BTRFS_DIR_INDEX_KEY
)
2080 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
2086 btrfs_free_path(path
);
2090 int btrfs_write_inode(struct inode
*inode
, int wait
)
2092 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2093 struct btrfs_trans_handle
*trans
;
2096 if (root
->fs_info
->closing
> 1)
2100 trans
= btrfs_join_transaction(root
, 1);
2101 btrfs_set_trans_block_group(trans
, inode
);
2102 ret
= btrfs_commit_transaction(trans
, root
);
2108 * This is somewhat expensive, updating the tree every time the
2109 * inode changes. But, it is most likely to find the inode in cache.
2110 * FIXME, needs more benchmarking...there are no reasons other than performance
2111 * to keep or drop this code.
2113 void btrfs_dirty_inode(struct inode
*inode
)
2115 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2116 struct btrfs_trans_handle
*trans
;
2118 trans
= btrfs_join_transaction(root
, 1);
2119 btrfs_set_trans_block_group(trans
, inode
);
2120 btrfs_update_inode(trans
, root
, inode
);
2121 btrfs_end_transaction(trans
, root
);
2124 static int btrfs_set_inode_index_count(struct inode
*inode
)
2126 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2127 struct btrfs_key key
, found_key
;
2128 struct btrfs_path
*path
;
2129 struct extent_buffer
*leaf
;
2132 key
.objectid
= inode
->i_ino
;
2133 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
2134 key
.offset
= (u64
)-1;
2136 path
= btrfs_alloc_path();
2140 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2143 /* FIXME: we should be able to handle this */
2149 * MAGIC NUMBER EXPLANATION:
2150 * since we search a directory based on f_pos we have to start at 2
2151 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2152 * else has to start at 2
2154 if (path
->slots
[0] == 0) {
2155 BTRFS_I(inode
)->index_cnt
= 2;
2161 leaf
= path
->nodes
[0];
2162 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2164 if (found_key
.objectid
!= inode
->i_ino
||
2165 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
2166 BTRFS_I(inode
)->index_cnt
= 2;
2170 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
2172 btrfs_free_path(path
);
2176 static int btrfs_set_inode_index(struct inode
*dir
, struct inode
*inode
,
2181 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
2182 ret
= btrfs_set_inode_index_count(dir
);
2187 *index
= BTRFS_I(dir
)->index_cnt
;
2188 BTRFS_I(dir
)->index_cnt
++;
2193 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
2194 struct btrfs_root
*root
,
2196 const char *name
, int name_len
,
2199 struct btrfs_block_group_cache
*group
,
2200 int mode
, u64
*index
)
2202 struct inode
*inode
;
2203 struct btrfs_inode_item
*inode_item
;
2204 struct btrfs_block_group_cache
*new_inode_group
;
2205 struct btrfs_key
*location
;
2206 struct btrfs_path
*path
;
2207 struct btrfs_inode_ref
*ref
;
2208 struct btrfs_key key
[2];
2214 path
= btrfs_alloc_path();
2217 inode
= new_inode(root
->fs_info
->sb
);
2219 return ERR_PTR(-ENOMEM
);
2222 ret
= btrfs_set_inode_index(dir
, inode
, index
);
2224 return ERR_PTR(ret
);
2227 * index_cnt is ignored for everything but a dir,
2228 * btrfs_get_inode_index_count has an explanation for the magic
2231 BTRFS_I(inode
)->index_cnt
= 2;
2233 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2234 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2235 inode
->i_mapping
, GFP_NOFS
);
2236 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2237 inode
->i_mapping
, GFP_NOFS
);
2238 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2239 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
2240 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2241 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2242 BTRFS_I(inode
)->delalloc_bytes
= 0;
2243 inode
->i_mapping
->writeback_index
= 0;
2244 BTRFS_I(inode
)->disk_i_size
= 0;
2245 BTRFS_I(inode
)->root
= root
;
2251 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
2252 BTRFS_BLOCK_GROUP_METADATA
, owner
);
2253 if (!new_inode_group
) {
2254 printk("find_block group failed\n");
2255 new_inode_group
= group
;
2257 BTRFS_I(inode
)->block_group
= new_inode_group
;
2258 BTRFS_I(inode
)->flags
= 0;
2260 key
[0].objectid
= objectid
;
2261 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
2264 key
[1].objectid
= objectid
;
2265 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
2266 key
[1].offset
= ref_objectid
;
2268 sizes
[0] = sizeof(struct btrfs_inode_item
);
2269 sizes
[1] = name_len
+ sizeof(*ref
);
2271 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
2275 if (objectid
> root
->highest_inode
)
2276 root
->highest_inode
= objectid
;
2278 inode
->i_uid
= current
->fsuid
;
2279 inode
->i_gid
= current
->fsgid
;
2280 inode
->i_mode
= mode
;
2281 inode
->i_ino
= objectid
;
2282 inode
->i_blocks
= 0;
2283 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2284 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2285 struct btrfs_inode_item
);
2286 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
2288 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
2289 struct btrfs_inode_ref
);
2290 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
2291 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
2292 ptr
= (unsigned long)(ref
+ 1);
2293 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
2295 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2296 btrfs_free_path(path
);
2298 location
= &BTRFS_I(inode
)->location
;
2299 location
->objectid
= objectid
;
2300 location
->offset
= 0;
2301 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
2303 insert_inode_hash(inode
);
2307 BTRFS_I(dir
)->index_cnt
--;
2308 btrfs_free_path(path
);
2309 return ERR_PTR(ret
);
2312 static inline u8
btrfs_inode_type(struct inode
*inode
)
2314 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
2317 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
2318 struct dentry
*dentry
, struct inode
*inode
,
2319 int add_backref
, u64 index
)
2322 struct btrfs_key key
;
2323 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
2324 struct inode
*parent_inode
= dentry
->d_parent
->d_inode
;
2326 key
.objectid
= inode
->i_ino
;
2327 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
2330 ret
= btrfs_insert_dir_item(trans
, root
,
2331 dentry
->d_name
.name
, dentry
->d_name
.len
,
2332 dentry
->d_parent
->d_inode
->i_ino
,
2333 &key
, btrfs_inode_type(inode
),
2337 ret
= btrfs_insert_inode_ref(trans
, root
,
2338 dentry
->d_name
.name
,
2341 parent_inode
->i_ino
,
2344 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
2345 dentry
->d_name
.len
* 2);
2346 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
2347 ret
= btrfs_update_inode(trans
, root
,
2348 dentry
->d_parent
->d_inode
);
2353 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
2354 struct dentry
*dentry
, struct inode
*inode
,
2355 int backref
, u64 index
)
2357 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
, index
);
2359 d_instantiate(dentry
, inode
);
2367 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
2368 int mode
, dev_t rdev
)
2370 struct btrfs_trans_handle
*trans
;
2371 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2372 struct inode
*inode
= NULL
;
2376 unsigned long nr
= 0;
2379 if (!new_valid_dev(rdev
))
2382 err
= btrfs_check_free_space(root
, 1, 0);
2386 trans
= btrfs_start_transaction(root
, 1);
2387 btrfs_set_trans_block_group(trans
, dir
);
2389 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2395 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2397 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2398 BTRFS_I(dir
)->block_group
, mode
, &index
);
2399 err
= PTR_ERR(inode
);
2403 err
= btrfs_init_acl(inode
, dir
);
2409 btrfs_set_trans_block_group(trans
, inode
);
2410 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2414 inode
->i_op
= &btrfs_special_inode_operations
;
2415 init_special_inode(inode
, inode
->i_mode
, rdev
);
2416 btrfs_update_inode(trans
, root
, inode
);
2418 dir
->i_sb
->s_dirt
= 1;
2419 btrfs_update_inode_block_group(trans
, inode
);
2420 btrfs_update_inode_block_group(trans
, dir
);
2422 nr
= trans
->blocks_used
;
2423 btrfs_end_transaction_throttle(trans
, root
);
2426 inode_dec_link_count(inode
);
2429 btrfs_btree_balance_dirty(root
, nr
);
2433 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
2434 int mode
, struct nameidata
*nd
)
2436 struct btrfs_trans_handle
*trans
;
2437 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2438 struct inode
*inode
= NULL
;
2441 unsigned long nr
= 0;
2445 err
= btrfs_check_free_space(root
, 1, 0);
2448 trans
= btrfs_start_transaction(root
, 1);
2449 btrfs_set_trans_block_group(trans
, dir
);
2451 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2457 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2459 dentry
->d_parent
->d_inode
->i_ino
,
2460 objectid
, BTRFS_I(dir
)->block_group
, mode
,
2462 err
= PTR_ERR(inode
);
2466 err
= btrfs_init_acl(inode
, dir
);
2472 btrfs_set_trans_block_group(trans
, inode
);
2473 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
2477 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2478 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2479 inode
->i_fop
= &btrfs_file_operations
;
2480 inode
->i_op
= &btrfs_file_inode_operations
;
2481 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2482 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2483 inode
->i_mapping
, GFP_NOFS
);
2484 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
2485 inode
->i_mapping
, GFP_NOFS
);
2486 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
2487 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
2488 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
2489 BTRFS_I(inode
)->delalloc_bytes
= 0;
2490 BTRFS_I(inode
)->disk_i_size
= 0;
2491 inode
->i_mapping
->writeback_index
= 0;
2492 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2493 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
2495 dir
->i_sb
->s_dirt
= 1;
2496 btrfs_update_inode_block_group(trans
, inode
);
2497 btrfs_update_inode_block_group(trans
, dir
);
2499 nr
= trans
->blocks_used
;
2500 btrfs_end_transaction_throttle(trans
, root
);
2503 inode_dec_link_count(inode
);
2506 btrfs_btree_balance_dirty(root
, nr
);
2510 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
2511 struct dentry
*dentry
)
2513 struct btrfs_trans_handle
*trans
;
2514 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2515 struct inode
*inode
= old_dentry
->d_inode
;
2517 unsigned long nr
= 0;
2521 if (inode
->i_nlink
== 0)
2524 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2529 err
= btrfs_check_free_space(root
, 1, 0);
2532 err
= btrfs_set_inode_index(dir
, inode
, &index
);
2536 trans
= btrfs_start_transaction(root
, 1);
2538 btrfs_set_trans_block_group(trans
, dir
);
2539 atomic_inc(&inode
->i_count
);
2541 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1, index
);
2546 dir
->i_sb
->s_dirt
= 1;
2547 btrfs_update_inode_block_group(trans
, dir
);
2548 err
= btrfs_update_inode(trans
, root
, inode
);
2553 nr
= trans
->blocks_used
;
2554 btrfs_end_transaction_throttle(trans
, root
);
2557 inode_dec_link_count(inode
);
2560 btrfs_btree_balance_dirty(root
, nr
);
2564 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2566 struct inode
*inode
= NULL
;
2567 struct btrfs_trans_handle
*trans
;
2568 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2570 int drop_on_err
= 0;
2573 unsigned long nr
= 1;
2575 err
= btrfs_check_free_space(root
, 1, 0);
2579 trans
= btrfs_start_transaction(root
, 1);
2580 btrfs_set_trans_block_group(trans
, dir
);
2582 if (IS_ERR(trans
)) {
2583 err
= PTR_ERR(trans
);
2587 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2593 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
2595 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2596 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
,
2598 if (IS_ERR(inode
)) {
2599 err
= PTR_ERR(inode
);
2605 err
= btrfs_init_acl(inode
, dir
);
2609 inode
->i_op
= &btrfs_dir_inode_operations
;
2610 inode
->i_fop
= &btrfs_dir_file_operations
;
2611 btrfs_set_trans_block_group(trans
, inode
);
2613 btrfs_i_size_write(inode
, 0);
2614 err
= btrfs_update_inode(trans
, root
, inode
);
2618 err
= btrfs_add_link(trans
, dentry
, inode
, 0, index
);
2622 d_instantiate(dentry
, inode
);
2624 dir
->i_sb
->s_dirt
= 1;
2625 btrfs_update_inode_block_group(trans
, inode
);
2626 btrfs_update_inode_block_group(trans
, dir
);
2629 nr
= trans
->blocks_used
;
2630 btrfs_end_transaction_throttle(trans
, root
);
2635 btrfs_btree_balance_dirty(root
, nr
);
2639 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
2640 struct extent_map
*existing
,
2641 struct extent_map
*em
,
2642 u64 map_start
, u64 map_len
)
2646 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
2647 start_diff
= map_start
- em
->start
;
2648 em
->start
= map_start
;
2650 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
)
2651 em
->block_start
+= start_diff
;
2652 return add_extent_mapping(em_tree
, em
);
2655 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
2656 size_t pg_offset
, u64 start
, u64 len
,
2662 u64 extent_start
= 0;
2664 u64 objectid
= inode
->i_ino
;
2666 struct btrfs_path
*path
= NULL
;
2667 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2668 struct btrfs_file_extent_item
*item
;
2669 struct extent_buffer
*leaf
;
2670 struct btrfs_key found_key
;
2671 struct extent_map
*em
= NULL
;
2672 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2673 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2674 struct btrfs_trans_handle
*trans
= NULL
;
2677 spin_lock(&em_tree
->lock
);
2678 em
= lookup_extent_mapping(em_tree
, start
, len
);
2680 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2681 spin_unlock(&em_tree
->lock
);
2684 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
2685 free_extent_map(em
);
2686 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
2687 free_extent_map(em
);
2691 em
= alloc_extent_map(GFP_NOFS
);
2696 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
2697 em
->start
= EXTENT_MAP_HOLE
;
2701 path
= btrfs_alloc_path();
2705 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2706 objectid
, start
, trans
!= NULL
);
2713 if (path
->slots
[0] == 0)
2718 leaf
= path
->nodes
[0];
2719 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2720 struct btrfs_file_extent_item
);
2721 /* are we inside the extent that was found? */
2722 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2723 found_type
= btrfs_key_type(&found_key
);
2724 if (found_key
.objectid
!= objectid
||
2725 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2729 found_type
= btrfs_file_extent_type(leaf
, item
);
2730 extent_start
= found_key
.offset
;
2731 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2732 extent_end
= extent_start
+
2733 btrfs_file_extent_num_bytes(leaf
, item
);
2735 if (start
< extent_start
|| start
>= extent_end
) {
2737 if (start
< extent_start
) {
2738 if (start
+ len
<= extent_start
)
2740 em
->len
= extent_end
- extent_start
;
2746 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2748 em
->start
= extent_start
;
2749 em
->len
= extent_end
- extent_start
;
2750 em
->block_start
= EXTENT_MAP_HOLE
;
2753 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2754 em
->block_start
= bytenr
;
2755 em
->start
= extent_start
;
2756 em
->len
= extent_end
- extent_start
;
2758 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2763 size_t extent_offset
;
2766 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2768 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2769 ~((u64
)root
->sectorsize
- 1);
2770 if (start
< extent_start
|| start
>= extent_end
) {
2772 if (start
< extent_start
) {
2773 if (start
+ len
<= extent_start
)
2775 em
->len
= extent_end
- extent_start
;
2781 em
->block_start
= EXTENT_MAP_INLINE
;
2784 em
->start
= extent_start
;
2789 page_start
= page_offset(page
) + pg_offset
;
2790 extent_offset
= page_start
- extent_start
;
2791 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2792 size
- extent_offset
);
2793 em
->start
= extent_start
+ extent_offset
;
2794 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2795 ~((u64
)root
->sectorsize
- 1);
2797 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2798 if (create
== 0 && !PageUptodate(page
)) {
2799 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2801 flush_dcache_page(page
);
2802 } else if (create
&& PageUptodate(page
)) {
2805 free_extent_map(em
);
2807 btrfs_release_path(root
, path
);
2808 trans
= btrfs_join_transaction(root
, 1);
2811 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2813 btrfs_mark_buffer_dirty(leaf
);
2816 set_extent_uptodate(io_tree
, em
->start
,
2817 extent_map_end(em
) - 1, GFP_NOFS
);
2820 printk("unkknown found_type %d\n", found_type
);
2827 em
->block_start
= EXTENT_MAP_HOLE
;
2829 btrfs_release_path(root
, path
);
2830 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2831 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2837 spin_lock(&em_tree
->lock
);
2838 ret
= add_extent_mapping(em_tree
, em
);
2839 /* it is possible that someone inserted the extent into the tree
2840 * while we had the lock dropped. It is also possible that
2841 * an overlapping map exists in the tree
2843 if (ret
== -EEXIST
) {
2844 struct extent_map
*existing
;
2848 existing
= lookup_extent_mapping(em_tree
, start
, len
);
2849 if (existing
&& (existing
->start
> start
||
2850 existing
->start
+ existing
->len
<= start
)) {
2851 free_extent_map(existing
);
2855 existing
= lookup_extent_mapping(em_tree
, em
->start
,
2858 err
= merge_extent_mapping(em_tree
, existing
,
2861 free_extent_map(existing
);
2863 free_extent_map(em
);
2868 printk("failing to insert %Lu %Lu\n",
2870 free_extent_map(em
);
2874 free_extent_map(em
);
2879 spin_unlock(&em_tree
->lock
);
2882 btrfs_free_path(path
);
2884 ret
= btrfs_end_transaction(trans
, root
);
2890 free_extent_map(em
);
2892 return ERR_PTR(err
);
2897 #if 0 /* waiting for O_DIRECT reads */
2898 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
2899 struct buffer_head
*bh_result
, int create
)
2901 struct extent_map
*em
;
2902 u64 start
= (u64
)iblock
<< inode
->i_blkbits
;
2903 struct btrfs_multi_bio
*multi
= NULL
;
2904 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2910 em
= btrfs_get_extent(inode
, NULL
, 0, start
, bh_result
->b_size
, 0);
2912 if (!em
|| IS_ERR(em
))
2915 if (em
->start
> start
|| em
->start
+ em
->len
<= start
) {
2919 if (em
->block_start
== EXTENT_MAP_INLINE
) {
2924 len
= em
->start
+ em
->len
- start
;
2925 len
= min_t(u64
, len
, INT_LIMIT(typeof(bh_result
->b_size
)));
2927 if (em
->block_start
== EXTENT_MAP_HOLE
||
2928 em
->block_start
== EXTENT_MAP_DELALLOC
) {
2929 bh_result
->b_size
= len
;
2933 logical
= start
- em
->start
;
2934 logical
= em
->block_start
+ logical
;
2937 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
2938 logical
, &map_length
, &multi
, 0);
2940 bh_result
->b_blocknr
= multi
->stripes
[0].physical
>> inode
->i_blkbits
;
2941 bh_result
->b_size
= min(map_length
, len
);
2943 bh_result
->b_bdev
= multi
->stripes
[0].dev
->bdev
;
2944 set_buffer_mapped(bh_result
);
2947 free_extent_map(em
);
2952 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
2953 const struct iovec
*iov
, loff_t offset
,
2954 unsigned long nr_segs
)
2958 struct file
*file
= iocb
->ki_filp
;
2959 struct inode
*inode
= file
->f_mapping
->host
;
2964 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
2965 offset
, nr_segs
, btrfs_get_block
, NULL
);
2969 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2971 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2974 int btrfs_readpage(struct file
*file
, struct page
*page
)
2976 struct extent_io_tree
*tree
;
2977 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2978 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2981 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2983 struct extent_io_tree
*tree
;
2986 if (current
->flags
& PF_MEMALLOC
) {
2987 redirty_page_for_writepage(wbc
, page
);
2991 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2992 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2995 int btrfs_writepages(struct address_space
*mapping
,
2996 struct writeback_control
*wbc
)
2998 struct extent_io_tree
*tree
;
2999 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3000 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
3004 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
3005 struct list_head
*pages
, unsigned nr_pages
)
3007 struct extent_io_tree
*tree
;
3008 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
3009 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
3012 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
3014 struct extent_io_tree
*tree
;
3015 struct extent_map_tree
*map
;
3018 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3019 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
3020 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
3022 ClearPagePrivate(page
);
3023 set_page_private(page
, 0);
3024 page_cache_release(page
);
3029 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
3031 return __btrfs_releasepage(page
, gfp_flags
);
3034 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
3036 struct extent_io_tree
*tree
;
3037 struct btrfs_ordered_extent
*ordered
;
3038 u64 page_start
= page_offset(page
);
3039 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3041 wait_on_page_writeback(page
);
3042 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
3044 btrfs_releasepage(page
, GFP_NOFS
);
3048 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3049 ordered
= btrfs_lookup_ordered_extent(page
->mapping
->host
,
3053 * IO on this page will never be started, so we need
3054 * to account for any ordered extents now
3056 clear_extent_bit(tree
, page_start
, page_end
,
3057 EXTENT_DIRTY
| EXTENT_DELALLOC
|
3058 EXTENT_LOCKED
, 1, 0, GFP_NOFS
);
3059 btrfs_finish_ordered_io(page
->mapping
->host
,
3060 page_start
, page_end
);
3061 btrfs_put_ordered_extent(ordered
);
3062 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
3064 clear_extent_bit(tree
, page_start
, page_end
,
3065 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
3068 __btrfs_releasepage(page
, GFP_NOFS
);
3070 ClearPageChecked(page
);
3071 if (PagePrivate(page
)) {
3072 ClearPagePrivate(page
);
3073 set_page_private(page
, 0);
3074 page_cache_release(page
);
3079 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3080 * called from a page fault handler when a page is first dirtied. Hence we must
3081 * be careful to check for EOF conditions here. We set the page up correctly
3082 * for a written page which means we get ENOSPC checking when writing into
3083 * holes and correct delalloc and unwritten extent mapping on filesystems that
3084 * support these features.
3086 * We are not allowed to take the i_mutex here so we have to play games to
3087 * protect against truncate races as the page could now be beyond EOF. Because
3088 * vmtruncate() writes the inode size before removing pages, once we have the
3089 * page lock we can determine safely if the page is beyond EOF. If it is not
3090 * beyond EOF, then the page is guaranteed safe against truncation until we
3093 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
3095 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
3096 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3097 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3098 struct btrfs_ordered_extent
*ordered
;
3100 unsigned long zero_start
;
3106 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
3113 size
= i_size_read(inode
);
3114 page_start
= page_offset(page
);
3115 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
3117 if ((page
->mapping
!= inode
->i_mapping
) ||
3118 (page_start
>= size
)) {
3119 /* page got truncated out from underneath us */
3122 wait_on_page_writeback(page
);
3124 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3125 set_page_extent_mapped(page
);
3128 * we can't set the delalloc bits if there are pending ordered
3129 * extents. Drop our locks and wait for them to finish
3131 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
3133 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3135 btrfs_start_ordered_extent(inode
, ordered
, 1);
3136 btrfs_put_ordered_extent(ordered
);
3140 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
3143 /* page is wholly or partially inside EOF */
3144 if (page_start
+ PAGE_CACHE_SIZE
> size
)
3145 zero_start
= size
& ~PAGE_CACHE_MASK
;
3147 zero_start
= PAGE_CACHE_SIZE
;
3149 if (zero_start
!= PAGE_CACHE_SIZE
) {
3151 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
3152 flush_dcache_page(page
);
3155 ClearPageChecked(page
);
3156 set_page_dirty(page
);
3157 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
3165 static void btrfs_truncate(struct inode
*inode
)
3167 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3169 struct btrfs_trans_handle
*trans
;
3171 u64 mask
= root
->sectorsize
- 1;
3173 if (!S_ISREG(inode
->i_mode
))
3175 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3178 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3179 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
3181 trans
= btrfs_start_transaction(root
, 1);
3182 btrfs_set_trans_block_group(trans
, inode
);
3183 btrfs_i_size_write(inode
, inode
->i_size
);
3185 ret
= btrfs_orphan_add(trans
, inode
);
3188 /* FIXME, add redo link to tree so we don't leak on crash */
3189 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
3190 BTRFS_EXTENT_DATA_KEY
);
3191 btrfs_update_inode(trans
, root
, inode
);
3193 ret
= btrfs_orphan_del(trans
, inode
);
3197 nr
= trans
->blocks_used
;
3198 ret
= btrfs_end_transaction_throttle(trans
, root
);
3200 btrfs_btree_balance_dirty(root
, nr
);
3204 * Invalidate a single dcache entry at the root of the filesystem.
3205 * Needed after creation of snapshot or subvolume.
3207 void btrfs_invalidate_dcache_root(struct btrfs_root
*root
, char *name
,
3210 struct dentry
*alias
, *entry
;
3213 alias
= d_find_alias(root
->fs_info
->sb
->s_root
->d_inode
);
3217 /* change me if btrfs ever gets a d_hash operation */
3218 qstr
.hash
= full_name_hash(qstr
.name
, qstr
.len
);
3219 entry
= d_lookup(alias
, &qstr
);
3222 d_invalidate(entry
);
3228 int btrfs_create_subvol_root(struct btrfs_root
*new_root
,
3229 struct btrfs_trans_handle
*trans
, u64 new_dirid
,
3230 struct btrfs_block_group_cache
*block_group
)
3232 struct inode
*inode
;
3235 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2, new_dirid
,
3236 new_dirid
, block_group
, S_IFDIR
| 0700, &index
);
3238 return PTR_ERR(inode
);
3239 inode
->i_op
= &btrfs_dir_inode_operations
;
3240 inode
->i_fop
= &btrfs_dir_file_operations
;
3241 new_root
->inode
= inode
;
3244 btrfs_i_size_write(inode
, 0);
3246 return btrfs_update_inode(trans
, new_root
, inode
);
3249 unsigned long btrfs_force_ra(struct address_space
*mapping
,
3250 struct file_ra_state
*ra
, struct file
*file
,
3251 pgoff_t offset
, pgoff_t last_index
)
3253 pgoff_t req_size
= last_index
- offset
+ 1;
3255 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3256 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
3259 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
3260 return offset
+ req_size
;
3264 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
3266 struct btrfs_inode
*ei
;
3268 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
3272 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
3273 ei
->i_acl
= BTRFS_ACL_NOT_CACHED
;
3274 ei
->i_default_acl
= BTRFS_ACL_NOT_CACHED
;
3275 INIT_LIST_HEAD(&ei
->i_orphan
);
3276 return &ei
->vfs_inode
;
3279 void btrfs_destroy_inode(struct inode
*inode
)
3281 struct btrfs_ordered_extent
*ordered
;
3282 WARN_ON(!list_empty(&inode
->i_dentry
));
3283 WARN_ON(inode
->i_data
.nrpages
);
3285 if (BTRFS_I(inode
)->i_acl
&&
3286 BTRFS_I(inode
)->i_acl
!= BTRFS_ACL_NOT_CACHED
)
3287 posix_acl_release(BTRFS_I(inode
)->i_acl
);
3288 if (BTRFS_I(inode
)->i_default_acl
&&
3289 BTRFS_I(inode
)->i_default_acl
!= BTRFS_ACL_NOT_CACHED
)
3290 posix_acl_release(BTRFS_I(inode
)->i_default_acl
);
3292 spin_lock(&BTRFS_I(inode
)->root
->list_lock
);
3293 if (!list_empty(&BTRFS_I(inode
)->i_orphan
)) {
3294 printk(KERN_ERR
"BTRFS: inode %lu: inode still on the orphan"
3295 " list\n", inode
->i_ino
);
3298 spin_unlock(&BTRFS_I(inode
)->root
->list_lock
);
3301 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
3305 printk("found ordered extent %Lu %Lu\n",
3306 ordered
->file_offset
, ordered
->len
);
3307 btrfs_remove_ordered_extent(inode
, ordered
);
3308 btrfs_put_ordered_extent(ordered
);
3309 btrfs_put_ordered_extent(ordered
);
3312 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
3313 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
3316 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3317 static void init_once(void *foo
)
3318 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3319 static void init_once(struct kmem_cache
* cachep
, void *foo
)
3321 static void init_once(void * foo
, struct kmem_cache
* cachep
,
3322 unsigned long flags
)
3325 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
3327 inode_init_once(&ei
->vfs_inode
);
3330 void btrfs_destroy_cachep(void)
3332 if (btrfs_inode_cachep
)
3333 kmem_cache_destroy(btrfs_inode_cachep
);
3334 if (btrfs_trans_handle_cachep
)
3335 kmem_cache_destroy(btrfs_trans_handle_cachep
);
3336 if (btrfs_transaction_cachep
)
3337 kmem_cache_destroy(btrfs_transaction_cachep
);
3338 if (btrfs_bit_radix_cachep
)
3339 kmem_cache_destroy(btrfs_bit_radix_cachep
);
3340 if (btrfs_path_cachep
)
3341 kmem_cache_destroy(btrfs_path_cachep
);
3344 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
3345 unsigned long extra_flags
,
3346 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3347 void (*ctor
)(void *)
3348 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3349 void (*ctor
)(struct kmem_cache
*, void *)
3351 void (*ctor
)(void *, struct kmem_cache
*,
3356 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
3357 SLAB_MEM_SPREAD
| extra_flags
), ctor
3358 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3364 int btrfs_init_cachep(void)
3366 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
3367 sizeof(struct btrfs_inode
),
3369 if (!btrfs_inode_cachep
)
3371 btrfs_trans_handle_cachep
=
3372 btrfs_cache_create("btrfs_trans_handle_cache",
3373 sizeof(struct btrfs_trans_handle
),
3375 if (!btrfs_trans_handle_cachep
)
3377 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
3378 sizeof(struct btrfs_transaction
),
3380 if (!btrfs_transaction_cachep
)
3382 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
3383 sizeof(struct btrfs_path
),
3385 if (!btrfs_path_cachep
)
3387 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
3388 SLAB_DESTROY_BY_RCU
, NULL
);
3389 if (!btrfs_bit_radix_cachep
)
3393 btrfs_destroy_cachep();
3397 static int btrfs_getattr(struct vfsmount
*mnt
,
3398 struct dentry
*dentry
, struct kstat
*stat
)
3400 struct inode
*inode
= dentry
->d_inode
;
3401 generic_fillattr(inode
, stat
);
3402 stat
->blksize
= PAGE_CACHE_SIZE
;
3403 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
3407 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
3408 struct inode
* new_dir
,struct dentry
*new_dentry
)
3410 struct btrfs_trans_handle
*trans
;
3411 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
3412 struct inode
*new_inode
= new_dentry
->d_inode
;
3413 struct inode
*old_inode
= old_dentry
->d_inode
;
3414 struct timespec ctime
= CURRENT_TIME
;
3418 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
3419 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
3423 ret
= btrfs_check_free_space(root
, 1, 0);
3427 trans
= btrfs_start_transaction(root
, 1);
3429 btrfs_set_trans_block_group(trans
, new_dir
);
3431 old_dentry
->d_inode
->i_nlink
++;
3432 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
3433 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
3434 old_inode
->i_ctime
= ctime
;
3436 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
3441 new_inode
->i_ctime
= CURRENT_TIME
;
3442 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
3445 if (new_inode
->i_nlink
== 0) {
3446 ret
= btrfs_orphan_add(trans
, new_inode
);
3451 ret
= btrfs_set_inode_index(new_dir
, old_inode
, &index
);
3455 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1, index
);
3460 btrfs_end_transaction_throttle(trans
, root
);
3465 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
)
3467 struct list_head
*head
= &root
->fs_info
->delalloc_inodes
;
3468 struct btrfs_inode
*binode
;
3469 unsigned long flags
;
3471 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3472 while(!list_empty(head
)) {
3473 binode
= list_entry(head
->next
, struct btrfs_inode
,
3475 atomic_inc(&binode
->vfs_inode
.i_count
);
3476 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3477 filemap_write_and_wait(binode
->vfs_inode
.i_mapping
);
3478 iput(&binode
->vfs_inode
);
3479 spin_lock_irqsave(&root
->fs_info
->delalloc_lock
, flags
);
3481 spin_unlock_irqrestore(&root
->fs_info
->delalloc_lock
, flags
);
3485 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
3486 const char *symname
)
3488 struct btrfs_trans_handle
*trans
;
3489 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3490 struct btrfs_path
*path
;
3491 struct btrfs_key key
;
3492 struct inode
*inode
= NULL
;
3500 struct btrfs_file_extent_item
*ei
;
3501 struct extent_buffer
*leaf
;
3502 unsigned long nr
= 0;
3504 name_len
= strlen(symname
) + 1;
3505 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
3506 return -ENAMETOOLONG
;
3508 err
= btrfs_check_free_space(root
, 1, 0);
3512 trans
= btrfs_start_transaction(root
, 1);
3513 btrfs_set_trans_block_group(trans
, dir
);
3515 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
3521 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
3523 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
3524 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
,
3526 err
= PTR_ERR(inode
);
3530 err
= btrfs_init_acl(inode
, dir
);
3536 btrfs_set_trans_block_group(trans
, inode
);
3537 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0, index
);
3541 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3542 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3543 inode
->i_fop
= &btrfs_file_operations
;
3544 inode
->i_op
= &btrfs_file_inode_operations
;
3545 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
3546 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
3547 inode
->i_mapping
, GFP_NOFS
);
3548 extent_io_tree_init(&BTRFS_I(inode
)->io_failure_tree
,
3549 inode
->i_mapping
, GFP_NOFS
);
3550 INIT_LIST_HEAD(&BTRFS_I(inode
)->delalloc_inodes
);
3551 mutex_init(&BTRFS_I(inode
)->csum_mutex
);
3552 mutex_init(&BTRFS_I(inode
)->extent_mutex
);
3553 BTRFS_I(inode
)->delalloc_bytes
= 0;
3554 BTRFS_I(inode
)->disk_i_size
= 0;
3555 inode
->i_mapping
->writeback_index
= 0;
3556 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3557 btrfs_ordered_inode_tree_init(&BTRFS_I(inode
)->ordered_tree
);
3559 dir
->i_sb
->s_dirt
= 1;
3560 btrfs_update_inode_block_group(trans
, inode
);
3561 btrfs_update_inode_block_group(trans
, dir
);
3565 path
= btrfs_alloc_path();
3567 key
.objectid
= inode
->i_ino
;
3569 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
3570 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
3571 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3577 leaf
= path
->nodes
[0];
3578 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
3579 struct btrfs_file_extent_item
);
3580 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
3581 btrfs_set_file_extent_type(leaf
, ei
,
3582 BTRFS_FILE_EXTENT_INLINE
);
3583 ptr
= btrfs_file_extent_inline_start(ei
);
3584 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3585 btrfs_mark_buffer_dirty(leaf
);
3586 btrfs_free_path(path
);
3588 inode
->i_op
= &btrfs_symlink_inode_operations
;
3589 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3590 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3591 btrfs_i_size_write(inode
, name_len
- 1);
3592 err
= btrfs_update_inode(trans
, root
, inode
);
3597 nr
= trans
->blocks_used
;
3598 btrfs_end_transaction_throttle(trans
, root
);
3601 inode_dec_link_count(inode
);
3604 btrfs_btree_balance_dirty(root
, nr
);
3608 static int btrfs_set_page_dirty(struct page
*page
)
3610 return __set_page_dirty_nobuffers(page
);
3613 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3614 static int btrfs_permission(struct inode
*inode
, int mask
)
3616 static int btrfs_permission(struct inode
*inode
, int mask
,
3617 struct nameidata
*nd
)
3620 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3622 return generic_permission(inode
, mask
, btrfs_check_acl
);
3625 static struct inode_operations btrfs_dir_inode_operations
= {
3626 .lookup
= btrfs_lookup
,
3627 .create
= btrfs_create
,
3628 .unlink
= btrfs_unlink
,
3630 .mkdir
= btrfs_mkdir
,
3631 .rmdir
= btrfs_rmdir
,
3632 .rename
= btrfs_rename
,
3633 .symlink
= btrfs_symlink
,
3634 .setattr
= btrfs_setattr
,
3635 .mknod
= btrfs_mknod
,
3636 .setxattr
= generic_setxattr
,
3637 .getxattr
= generic_getxattr
,
3638 .listxattr
= btrfs_listxattr
,
3639 .removexattr
= generic_removexattr
,
3640 .permission
= btrfs_permission
,
3642 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3643 .lookup
= btrfs_lookup
,
3644 .permission
= btrfs_permission
,
3646 static struct file_operations btrfs_dir_file_operations
= {
3647 .llseek
= generic_file_llseek
,
3648 .read
= generic_read_dir
,
3649 .readdir
= btrfs_readdir
,
3650 .unlocked_ioctl
= btrfs_ioctl
,
3651 #ifdef CONFIG_COMPAT
3652 .compat_ioctl
= btrfs_ioctl
,
3654 .release
= btrfs_release_file
,
3657 static struct extent_io_ops btrfs_extent_io_ops
= {
3658 .fill_delalloc
= run_delalloc_range
,
3659 .submit_bio_hook
= btrfs_submit_bio_hook
,
3660 .merge_bio_hook
= btrfs_merge_bio_hook
,
3661 .readpage_io_hook
= btrfs_readpage_io_hook
,
3662 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3663 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
3664 .writepage_start_hook
= btrfs_writepage_start_hook
,
3665 .readpage_io_failed_hook
= btrfs_io_failed_hook
,
3666 .set_bit_hook
= btrfs_set_bit_hook
,
3667 .clear_bit_hook
= btrfs_clear_bit_hook
,
3670 static struct address_space_operations btrfs_aops
= {
3671 .readpage
= btrfs_readpage
,
3672 .writepage
= btrfs_writepage
,
3673 .writepages
= btrfs_writepages
,
3674 .readpages
= btrfs_readpages
,
3675 .sync_page
= block_sync_page
,
3677 .direct_IO
= btrfs_direct_IO
,
3678 .invalidatepage
= btrfs_invalidatepage
,
3679 .releasepage
= btrfs_releasepage
,
3680 .set_page_dirty
= btrfs_set_page_dirty
,
3683 static struct address_space_operations btrfs_symlink_aops
= {
3684 .readpage
= btrfs_readpage
,
3685 .writepage
= btrfs_writepage
,
3686 .invalidatepage
= btrfs_invalidatepage
,
3687 .releasepage
= btrfs_releasepage
,
3690 static struct inode_operations btrfs_file_inode_operations
= {
3691 .truncate
= btrfs_truncate
,
3692 .getattr
= btrfs_getattr
,
3693 .setattr
= btrfs_setattr
,
3694 .setxattr
= generic_setxattr
,
3695 .getxattr
= generic_getxattr
,
3696 .listxattr
= btrfs_listxattr
,
3697 .removexattr
= generic_removexattr
,
3698 .permission
= btrfs_permission
,
3700 static struct inode_operations btrfs_special_inode_operations
= {
3701 .getattr
= btrfs_getattr
,
3702 .setattr
= btrfs_setattr
,
3703 .permission
= btrfs_permission
,
3704 .setxattr
= generic_setxattr
,
3705 .getxattr
= generic_getxattr
,
3706 .listxattr
= btrfs_listxattr
,
3707 .removexattr
= generic_removexattr
,
3709 static struct inode_operations btrfs_symlink_inode_operations
= {
3710 .readlink
= generic_readlink
,
3711 .follow_link
= page_follow_link_light
,
3712 .put_link
= page_put_link
,
3713 .permission
= btrfs_permission
,