2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
45 struct btrfs_iget_args
{
47 struct btrfs_root
*root
;
50 static struct inode_operations btrfs_dir_inode_operations
;
51 static struct inode_operations btrfs_symlink_inode_operations
;
52 static struct inode_operations btrfs_dir_ro_inode_operations
;
53 static struct inode_operations btrfs_special_inode_operations
;
54 static struct inode_operations btrfs_file_inode_operations
;
55 static struct address_space_operations btrfs_aops
;
56 static struct address_space_operations btrfs_symlink_aops
;
57 static struct file_operations btrfs_dir_file_operations
;
58 static struct extent_io_ops btrfs_extent_io_ops
;
60 static struct kmem_cache
*btrfs_inode_cachep
;
61 struct kmem_cache
*btrfs_trans_handle_cachep
;
62 struct kmem_cache
*btrfs_transaction_cachep
;
63 struct kmem_cache
*btrfs_bit_radix_cachep
;
64 struct kmem_cache
*btrfs_path_cachep
;
67 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
68 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
69 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
70 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
71 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
72 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
73 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
74 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
77 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
80 u64 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
81 u64 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
92 spin_lock(&root
->fs_info
->delalloc_lock
);
93 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
95 spin_unlock(&root
->fs_info
->delalloc_lock
);
99 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
101 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
102 struct btrfs_trans_handle
*trans
;
106 u64 blocksize
= root
->sectorsize
;
107 u64 orig_start
= start
;
109 struct btrfs_key ins
;
112 trans
= btrfs_start_transaction(root
, 1);
114 btrfs_set_trans_block_group(trans
, inode
);
116 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
117 num_bytes
= max(blocksize
, num_bytes
);
118 ret
= btrfs_drop_extents(trans
, root
, inode
,
119 start
, start
+ num_bytes
, start
, &alloc_hint
);
120 orig_num_bytes
= num_bytes
;
122 if (alloc_hint
== EXTENT_MAP_INLINE
)
125 while(num_bytes
> 0) {
126 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
127 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
128 root
->root_key
.objectid
,
130 inode
->i_ino
, start
, 0,
131 alloc_hint
, (u64
)-1, &ins
, 1);
136 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
137 start
, ins
.objectid
, ins
.offset
,
139 inode
->i_blocks
+= ins
.offset
>> 9;
140 btrfs_check_file(root
, inode
);
141 num_bytes
-= cur_alloc_size
;
142 alloc_hint
= ins
.objectid
+ ins
.offset
;
143 start
+= cur_alloc_size
;
145 btrfs_drop_extent_cache(inode
, orig_start
,
146 orig_start
+ orig_num_bytes
- 1);
147 btrfs_add_ordered_inode(inode
);
148 btrfs_update_inode(trans
, root
, inode
);
150 btrfs_end_transaction(trans
, root
);
154 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
162 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
163 struct extent_buffer
*leaf
;
165 struct btrfs_path
*path
;
166 struct btrfs_file_extent_item
*item
;
169 struct btrfs_key found_key
;
171 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
172 path
= btrfs_alloc_path();
175 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
176 inode
->i_ino
, start
, 0);
178 btrfs_free_path(path
);
184 if (path
->slots
[0] == 0)
189 leaf
= path
->nodes
[0];
190 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
191 struct btrfs_file_extent_item
);
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
195 found_type
= btrfs_key_type(&found_key
);
196 if (found_key
.objectid
!= inode
->i_ino
||
197 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
201 found_type
= btrfs_file_extent_type(leaf
, item
);
202 extent_start
= found_key
.offset
;
203 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
204 u64 extent_num_bytes
;
206 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
207 extent_end
= extent_start
+ extent_num_bytes
;
210 if (loops
&& start
!= extent_start
)
213 if (start
< extent_start
|| start
>= extent_end
)
216 cow_end
= min(end
, extent_end
- 1);
217 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
225 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
228 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
238 btrfs_free_path(path
);
241 btrfs_release_path(root
, path
);
246 cow_file_range(inode
, start
, cow_end
);
251 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
253 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
255 mutex_lock(&root
->fs_info
->fs_mutex
);
256 if (btrfs_test_opt(root
, NODATACOW
) ||
257 btrfs_test_flag(inode
, NODATACOW
))
258 ret
= run_delalloc_nocow(inode
, start
, end
);
260 ret
= cow_file_range(inode
, start
, end
);
262 mutex_unlock(&root
->fs_info
->fs_mutex
);
266 int btrfs_set_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
267 unsigned long old
, unsigned long bits
)
269 if (!(old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
270 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
271 spin_lock(&root
->fs_info
->delalloc_lock
);
272 BTRFS_I(inode
)->delalloc_bytes
+= end
- start
+ 1;
273 root
->fs_info
->delalloc_bytes
+= end
- start
+ 1;
274 spin_unlock(&root
->fs_info
->delalloc_lock
);
279 int btrfs_clear_bit_hook(struct inode
*inode
, u64 start
, u64 end
,
280 unsigned long old
, unsigned long bits
)
282 if ((old
& EXTENT_DELALLOC
) && (bits
& EXTENT_DELALLOC
)) {
283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
284 spin_lock(&root
->fs_info
->delalloc_lock
);
285 if (end
- start
+ 1 > root
->fs_info
->delalloc_bytes
) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end
- start
+ 1, root
->fs_info
->delalloc_bytes
);
288 root
->fs_info
->delalloc_bytes
= 0;
289 BTRFS_I(inode
)->delalloc_bytes
= 0;
291 root
->fs_info
->delalloc_bytes
-= end
- start
+ 1;
292 BTRFS_I(inode
)->delalloc_bytes
-= end
- start
+ 1;
294 spin_unlock(&root
->fs_info
->delalloc_lock
);
299 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
300 size_t size
, struct bio
*bio
)
302 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
303 struct btrfs_mapping_tree
*map_tree
;
304 struct btrfs_device
*dev
;
305 u64 logical
= bio
->bi_sector
<< 9;
310 struct bio_vec
*bvec
;
314 bio_for_each_segment(bvec
, bio
, i
) {
315 length
+= bvec
->bv_len
;
317 map_tree
= &root
->fs_info
->mapping_tree
;
319 ret
= btrfs_map_block(map_tree
, READ
, 0, logical
, &physical
,
320 &map_length
, &dev
, &total_devs
);
321 if (map_length
< length
+ size
) {
327 int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
)
329 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
330 struct btrfs_trans_handle
*trans
;
337 if (btrfs_test_opt(root
, NODATASUM
) ||
338 btrfs_test_flag(inode
, NODATASUM
)) {
342 mutex_lock(&root
->fs_info
->fs_mutex
);
343 trans
= btrfs_start_transaction(root
, 1);
344 btrfs_set_trans_block_group(trans
, inode
);
345 btrfs_csum_file_blocks(trans
, root
, inode
, bio
);
346 ret
= btrfs_end_transaction(trans
, root
);
348 mutex_unlock(&root
->fs_info
->fs_mutex
);
350 return btrfs_map_bio(root
, rw
, bio
);
353 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
356 struct inode
*inode
= page
->mapping
->host
;
357 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
358 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
359 struct btrfs_csum_item
*item
;
360 struct btrfs_path
*path
= NULL
;
362 if (btrfs_test_opt(root
, NODATASUM
) ||
363 btrfs_test_flag(inode
, NODATASUM
))
365 mutex_lock(&root
->fs_info
->fs_mutex
);
366 path
= btrfs_alloc_path();
367 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
370 /* a csum that isn't present is a preallocated region. */
371 if (ret
== -ENOENT
|| ret
== -EFBIG
)
374 printk("no csum found for inode %lu start %Lu\n", inode
->i_ino
, start
);
377 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
379 set_state_private(io_tree
, start
, csum
);
382 btrfs_free_path(path
);
383 mutex_unlock(&root
->fs_info
->fs_mutex
);
387 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
388 struct extent_state
*state
)
390 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
391 struct inode
*inode
= page
->mapping
->host
;
392 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
394 u64
private = ~(u32
)0;
396 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
400 if (btrfs_test_opt(root
, NODATASUM
) ||
401 btrfs_test_flag(inode
, NODATASUM
))
403 if (state
&& state
->start
== start
) {
404 private = state
->private;
407 ret
= get_state_private(io_tree
, start
, &private);
409 local_irq_save(flags
);
410 kaddr
= kmap_atomic(page
, KM_IRQ0
);
414 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
415 btrfs_csum_final(csum
, (char *)&csum
);
416 if (csum
!= private) {
419 kunmap_atomic(kaddr
, KM_IRQ0
);
420 local_irq_restore(flags
);
424 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
425 page
->mapping
->host
->i_ino
, (unsigned long long)start
, csum
,
427 memset(kaddr
+ offset
, 1, end
- start
+ 1);
428 flush_dcache_page(page
);
429 kunmap_atomic(kaddr
, KM_IRQ0
);
430 local_irq_restore(flags
);
434 void btrfs_read_locked_inode(struct inode
*inode
)
436 struct btrfs_path
*path
;
437 struct extent_buffer
*leaf
;
438 struct btrfs_inode_item
*inode_item
;
439 struct btrfs_timespec
*tspec
;
440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
441 struct btrfs_key location
;
442 u64 alloc_group_block
;
446 path
= btrfs_alloc_path();
448 mutex_lock(&root
->fs_info
->fs_mutex
);
449 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
451 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
455 leaf
= path
->nodes
[0];
456 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
457 struct btrfs_inode_item
);
459 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
460 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
461 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
462 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
463 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
465 tspec
= btrfs_inode_atime(inode_item
);
466 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
467 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
469 tspec
= btrfs_inode_mtime(inode_item
);
470 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
471 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
473 tspec
= btrfs_inode_ctime(inode_item
);
474 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
475 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
477 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
478 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
480 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
482 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
483 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
485 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
486 if (!BTRFS_I(inode
)->block_group
) {
487 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
489 BTRFS_BLOCK_GROUP_METADATA
, 0);
491 btrfs_free_path(path
);
494 mutex_unlock(&root
->fs_info
->fs_mutex
);
496 switch (inode
->i_mode
& S_IFMT
) {
498 inode
->i_mapping
->a_ops
= &btrfs_aops
;
499 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
500 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
501 inode
->i_fop
= &btrfs_file_operations
;
502 inode
->i_op
= &btrfs_file_inode_operations
;
505 inode
->i_fop
= &btrfs_dir_file_operations
;
506 if (root
== root
->fs_info
->tree_root
)
507 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
509 inode
->i_op
= &btrfs_dir_inode_operations
;
512 inode
->i_op
= &btrfs_symlink_inode_operations
;
513 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
514 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
517 init_special_inode(inode
, inode
->i_mode
, rdev
);
523 btrfs_release_path(root
, path
);
524 btrfs_free_path(path
);
525 mutex_unlock(&root
->fs_info
->fs_mutex
);
526 make_bad_inode(inode
);
529 static void fill_inode_item(struct extent_buffer
*leaf
,
530 struct btrfs_inode_item
*item
,
533 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
534 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
535 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
536 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
537 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
539 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
540 inode
->i_atime
.tv_sec
);
541 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
542 inode
->i_atime
.tv_nsec
);
544 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
545 inode
->i_mtime
.tv_sec
);
546 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
547 inode
->i_mtime
.tv_nsec
);
549 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
550 inode
->i_ctime
.tv_sec
);
551 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
552 inode
->i_ctime
.tv_nsec
);
554 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
555 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
556 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
557 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
558 btrfs_set_inode_block_group(leaf
, item
,
559 BTRFS_I(inode
)->block_group
->key
.objectid
);
562 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
563 struct btrfs_root
*root
,
566 struct btrfs_inode_item
*inode_item
;
567 struct btrfs_path
*path
;
568 struct extent_buffer
*leaf
;
571 path
= btrfs_alloc_path();
573 ret
= btrfs_lookup_inode(trans
, root
, path
,
574 &BTRFS_I(inode
)->location
, 1);
581 leaf
= path
->nodes
[0];
582 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
583 struct btrfs_inode_item
);
585 fill_inode_item(leaf
, inode_item
, inode
);
586 btrfs_mark_buffer_dirty(leaf
);
587 btrfs_set_inode_last_trans(trans
, inode
);
590 btrfs_release_path(root
, path
);
591 btrfs_free_path(path
);
596 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
597 struct btrfs_root
*root
,
599 struct dentry
*dentry
)
601 struct btrfs_path
*path
;
602 const char *name
= dentry
->d_name
.name
;
603 int name_len
= dentry
->d_name
.len
;
605 struct extent_buffer
*leaf
;
606 struct btrfs_dir_item
*di
;
607 struct btrfs_key key
;
609 path
= btrfs_alloc_path();
615 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
625 leaf
= path
->nodes
[0];
626 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
627 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
630 btrfs_release_path(root
, path
);
632 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
633 key
.objectid
, name
, name_len
, -1);
642 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
644 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
645 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
646 dentry
->d_inode
->i_ino
,
647 dentry
->d_parent
->d_inode
->i_ino
);
649 printk("failed to delete reference to %.*s, "
650 "inode %lu parent %lu\n", name_len
, name
,
651 dentry
->d_inode
->i_ino
,
652 dentry
->d_parent
->d_inode
->i_ino
);
655 btrfs_free_path(path
);
657 dir
->i_size
-= name_len
* 2;
658 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
659 btrfs_update_inode(trans
, root
, dir
);
660 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
661 dentry
->d_inode
->i_nlink
--;
663 drop_nlink(dentry
->d_inode
);
665 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
666 dir
->i_sb
->s_dirt
= 1;
671 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
673 struct btrfs_root
*root
;
674 struct btrfs_trans_handle
*trans
;
675 struct inode
*inode
= dentry
->d_inode
;
677 unsigned long nr
= 0;
679 root
= BTRFS_I(dir
)->root
;
680 mutex_lock(&root
->fs_info
->fs_mutex
);
682 ret
= btrfs_check_free_space(root
, 1, 1);
686 trans
= btrfs_start_transaction(root
, 1);
688 btrfs_set_trans_block_group(trans
, dir
);
689 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
690 nr
= trans
->blocks_used
;
692 if (inode
->i_nlink
== 0) {
694 /* if the inode isn't linked anywhere,
695 * we don't need to worry about
698 found
= btrfs_del_ordered_inode(inode
);
700 atomic_dec(&inode
->i_count
);
704 btrfs_end_transaction(trans
, root
);
706 mutex_unlock(&root
->fs_info
->fs_mutex
);
707 btrfs_btree_balance_dirty(root
, nr
);
708 btrfs_throttle(root
);
712 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
714 struct inode
*inode
= dentry
->d_inode
;
717 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
718 struct btrfs_trans_handle
*trans
;
719 unsigned long nr
= 0;
721 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
724 mutex_lock(&root
->fs_info
->fs_mutex
);
725 ret
= btrfs_check_free_space(root
, 1, 1);
729 trans
= btrfs_start_transaction(root
, 1);
730 btrfs_set_trans_block_group(trans
, dir
);
732 /* now the directory is empty */
733 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
738 nr
= trans
->blocks_used
;
739 ret
= btrfs_end_transaction(trans
, root
);
741 mutex_unlock(&root
->fs_info
->fs_mutex
);
742 btrfs_btree_balance_dirty(root
, nr
);
743 btrfs_throttle(root
);
751 * this can truncate away extent items, csum items and directory items.
752 * It starts at a high offset and removes keys until it can't find
753 * any higher than i_size.
755 * csum items that cross the new i_size are truncated to the new size
758 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
759 struct btrfs_root
*root
,
764 struct btrfs_path
*path
;
765 struct btrfs_key key
;
766 struct btrfs_key found_key
;
768 struct extent_buffer
*leaf
;
769 struct btrfs_file_extent_item
*fi
;
770 u64 extent_start
= 0;
771 u64 extent_num_bytes
= 0;
777 int pending_del_nr
= 0;
778 int pending_del_slot
= 0;
779 int extent_type
= -1;
781 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
782 path
= btrfs_alloc_path();
786 /* FIXME, add redo link to tree so we don't leak on crash */
787 key
.objectid
= inode
->i_ino
;
788 key
.offset
= (u64
)-1;
791 btrfs_init_path(path
);
793 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
798 BUG_ON(path
->slots
[0] == 0);
804 leaf
= path
->nodes
[0];
805 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
806 found_type
= btrfs_key_type(&found_key
);
808 if (found_key
.objectid
!= inode
->i_ino
)
811 if (found_type
< min_type
)
814 item_end
= found_key
.offset
;
815 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
816 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
817 struct btrfs_file_extent_item
);
818 extent_type
= btrfs_file_extent_type(leaf
, fi
);
819 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
821 btrfs_file_extent_num_bytes(leaf
, fi
);
822 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
823 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
825 item_end
+= btrfs_file_extent_inline_len(leaf
,
830 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
831 ret
= btrfs_csum_truncate(trans
, root
, path
,
835 if (item_end
< inode
->i_size
) {
836 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
837 found_type
= BTRFS_INODE_ITEM_KEY
;
838 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
839 found_type
= BTRFS_CSUM_ITEM_KEY
;
840 } else if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
841 found_type
= BTRFS_XATTR_ITEM_KEY
;
842 } else if (found_type
== BTRFS_XATTR_ITEM_KEY
) {
843 found_type
= BTRFS_INODE_REF_KEY
;
844 } else if (found_type
) {
849 btrfs_set_key_type(&key
, found_type
);
852 if (found_key
.offset
>= inode
->i_size
)
858 /* FIXME, shrink the extent if the ref count is only 1 */
859 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
862 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
864 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
867 btrfs_file_extent_num_bytes(leaf
, fi
);
868 extent_num_bytes
= inode
->i_size
-
869 found_key
.offset
+ root
->sectorsize
- 1;
870 extent_num_bytes
= extent_num_bytes
&
871 ~((u64
)root
->sectorsize
- 1);
872 btrfs_set_file_extent_num_bytes(leaf
, fi
,
874 num_dec
= (orig_num_bytes
-
876 if (extent_start
!= 0)
877 dec_i_blocks(inode
, num_dec
);
878 btrfs_mark_buffer_dirty(leaf
);
881 btrfs_file_extent_disk_num_bytes(leaf
,
883 /* FIXME blocksize != 4096 */
884 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
885 if (extent_start
!= 0) {
887 dec_i_blocks(inode
, num_dec
);
889 root_gen
= btrfs_header_generation(leaf
);
890 root_owner
= btrfs_header_owner(leaf
);
892 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
894 u32 newsize
= inode
->i_size
- found_key
.offset
;
895 dec_i_blocks(inode
, item_end
+ 1 -
896 found_key
.offset
- newsize
);
898 btrfs_file_extent_calc_inline_size(newsize
);
899 ret
= btrfs_truncate_item(trans
, root
, path
,
903 dec_i_blocks(inode
, item_end
+ 1 -
909 if (!pending_del_nr
) {
910 /* no pending yet, add ourselves */
911 pending_del_slot
= path
->slots
[0];
913 } else if (pending_del_nr
&&
914 path
->slots
[0] + 1 == pending_del_slot
) {
915 /* hop on the pending chunk */
917 pending_del_slot
= path
->slots
[0];
919 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path
->slots
[0], pending_del_nr
, pending_del_slot
);
925 ret
= btrfs_free_extent(trans
, root
, extent_start
,
928 root_gen
, inode
->i_ino
,
929 found_key
.offset
, 0);
933 if (path
->slots
[0] == 0) {
936 btrfs_release_path(root
, path
);
941 if (pending_del_nr
&&
942 path
->slots
[0] + 1 != pending_del_slot
) {
943 struct btrfs_key debug
;
945 btrfs_item_key_to_cpu(path
->nodes
[0], &debug
,
947 ret
= btrfs_del_items(trans
, root
, path
,
952 btrfs_release_path(root
, path
);
958 if (pending_del_nr
) {
959 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
962 btrfs_release_path(root
, path
);
963 btrfs_free_path(path
);
964 inode
->i_sb
->s_dirt
= 1;
968 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
972 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
973 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
974 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
977 WARN_ON(!PageLocked(page
));
978 set_page_extent_mapped(page
);
980 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
981 set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, page_start
,
984 if (zero_start
!= PAGE_CACHE_SIZE
) {
986 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
987 flush_dcache_page(page
);
990 set_page_dirty(page
);
991 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
997 * taken from block_truncate_page, but does cow as it zeros out
998 * any bytes left in the last page in the file.
1000 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
1002 struct inode
*inode
= mapping
->host
;
1003 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1004 u32 blocksize
= root
->sectorsize
;
1005 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
1006 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
1011 if ((offset
& (blocksize
- 1)) == 0)
1015 page
= grab_cache_page(mapping
, index
);
1018 if (!PageUptodate(page
)) {
1019 ret
= btrfs_readpage(NULL
, page
);
1021 if (!PageUptodate(page
)) {
1026 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1028 ret
= btrfs_cow_one_page(inode
, page
, offset
);
1031 page_cache_release(page
);
1036 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1038 struct inode
*inode
= dentry
->d_inode
;
1041 err
= inode_change_ok(inode
, attr
);
1045 if (S_ISREG(inode
->i_mode
) &&
1046 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
1047 struct btrfs_trans_handle
*trans
;
1048 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1049 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1051 u64 mask
= root
->sectorsize
- 1;
1052 u64 hole_start
= (inode
->i_size
+ mask
) & ~mask
;
1053 u64 block_end
= (attr
->ia_size
+ mask
) & ~mask
;
1057 if (attr
->ia_size
<= hole_start
)
1060 mutex_lock(&root
->fs_info
->fs_mutex
);
1061 err
= btrfs_check_free_space(root
, 1, 0);
1062 mutex_unlock(&root
->fs_info
->fs_mutex
);
1066 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1068 lock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1069 hole_size
= block_end
- hole_start
;
1071 mutex_lock(&root
->fs_info
->fs_mutex
);
1072 trans
= btrfs_start_transaction(root
, 1);
1073 btrfs_set_trans_block_group(trans
, inode
);
1074 err
= btrfs_drop_extents(trans
, root
, inode
,
1075 hole_start
, block_end
, hole_start
,
1078 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
1079 err
= btrfs_insert_file_extent(trans
, root
,
1083 btrfs_drop_extent_cache(inode
, hole_start
,
1085 btrfs_check_file(root
, inode
);
1087 btrfs_end_transaction(trans
, root
);
1088 mutex_unlock(&root
->fs_info
->fs_mutex
);
1089 unlock_extent(io_tree
, hole_start
, block_end
- 1, GFP_NOFS
);
1094 err
= inode_setattr(inode
, attr
);
1099 void btrfs_put_inode(struct inode
*inode
)
1103 if (!BTRFS_I(inode
)->ordered_trans
) {
1107 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_DIRTY
) ||
1108 mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1111 ret
= btrfs_del_ordered_inode(inode
);
1113 atomic_dec(&inode
->i_count
);
1117 void btrfs_delete_inode(struct inode
*inode
)
1119 struct btrfs_trans_handle
*trans
;
1120 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1124 truncate_inode_pages(&inode
->i_data
, 0);
1125 if (is_bad_inode(inode
)) {
1130 mutex_lock(&root
->fs_info
->fs_mutex
);
1131 trans
= btrfs_start_transaction(root
, 1);
1133 btrfs_set_trans_block_group(trans
, inode
);
1134 ret
= btrfs_truncate_in_trans(trans
, root
, inode
, 0);
1136 goto no_delete_lock
;
1138 nr
= trans
->blocks_used
;
1141 btrfs_end_transaction(trans
, root
);
1142 mutex_unlock(&root
->fs_info
->fs_mutex
);
1143 btrfs_btree_balance_dirty(root
, nr
);
1144 btrfs_throttle(root
);
1148 nr
= trans
->blocks_used
;
1149 btrfs_end_transaction(trans
, root
);
1150 mutex_unlock(&root
->fs_info
->fs_mutex
);
1151 btrfs_btree_balance_dirty(root
, nr
);
1152 btrfs_throttle(root
);
1158 * this returns the key found in the dir entry in the location pointer.
1159 * If no dir entries were found, location->objectid is 0.
1161 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1162 struct btrfs_key
*location
)
1164 const char *name
= dentry
->d_name
.name
;
1165 int namelen
= dentry
->d_name
.len
;
1166 struct btrfs_dir_item
*di
;
1167 struct btrfs_path
*path
;
1168 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1171 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1172 location
->objectid
= dir
->i_ino
;
1173 location
->type
= BTRFS_INODE_ITEM_KEY
;
1174 location
->offset
= 0;
1177 path
= btrfs_alloc_path();
1180 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1181 struct btrfs_key key
;
1182 struct extent_buffer
*leaf
;
1186 key
.objectid
= dir
->i_ino
;
1187 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1189 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1193 leaf
= path
->nodes
[0];
1194 slot
= path
->slots
[0];
1195 nritems
= btrfs_header_nritems(leaf
);
1196 if (slot
>= nritems
)
1199 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1200 if (key
.objectid
!= dir
->i_ino
||
1201 key
.type
!= BTRFS_INODE_REF_KEY
) {
1204 location
->objectid
= key
.offset
;
1205 location
->type
= BTRFS_INODE_ITEM_KEY
;
1206 location
->offset
= 0;
1210 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1214 if (!di
|| IS_ERR(di
)) {
1217 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1219 btrfs_free_path(path
);
1222 location
->objectid
= 0;
1227 * when we hit a tree root in a directory, the btrfs part of the inode
1228 * needs to be changed to reflect the root directory of the tree root. This
1229 * is kind of like crossing a mount point.
1231 static int fixup_tree_root_location(struct btrfs_root
*root
,
1232 struct btrfs_key
*location
,
1233 struct btrfs_root
**sub_root
,
1234 struct dentry
*dentry
)
1236 struct btrfs_path
*path
;
1237 struct btrfs_root_item
*ri
;
1239 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1241 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1244 path
= btrfs_alloc_path();
1246 mutex_lock(&root
->fs_info
->fs_mutex
);
1248 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1249 dentry
->d_name
.name
,
1250 dentry
->d_name
.len
);
1251 if (IS_ERR(*sub_root
))
1252 return PTR_ERR(*sub_root
);
1254 ri
= &(*sub_root
)->root_item
;
1255 location
->objectid
= btrfs_root_dirid(ri
);
1256 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1257 location
->offset
= 0;
1259 btrfs_free_path(path
);
1260 mutex_unlock(&root
->fs_info
->fs_mutex
);
1264 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1266 struct btrfs_iget_args
*args
= p
;
1267 inode
->i_ino
= args
->ino
;
1268 BTRFS_I(inode
)->root
= args
->root
;
1269 BTRFS_I(inode
)->delalloc_bytes
= 0;
1270 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1271 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1272 inode
->i_mapping
, GFP_NOFS
);
1276 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1278 struct btrfs_iget_args
*args
= opaque
;
1279 return (args
->ino
== inode
->i_ino
&&
1280 args
->root
== BTRFS_I(inode
)->root
);
1283 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1286 struct btrfs_iget_args args
;
1287 args
.ino
= objectid
;
1288 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1293 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1296 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1297 struct btrfs_root
*root
)
1299 struct inode
*inode
;
1300 struct btrfs_iget_args args
;
1301 args
.ino
= objectid
;
1304 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1305 btrfs_init_locked_inode
,
1310 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1311 struct nameidata
*nd
)
1313 struct inode
* inode
;
1314 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1315 struct btrfs_root
*root
= bi
->root
;
1316 struct btrfs_root
*sub_root
= root
;
1317 struct btrfs_key location
;
1320 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1321 return ERR_PTR(-ENAMETOOLONG
);
1323 mutex_lock(&root
->fs_info
->fs_mutex
);
1324 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1325 mutex_unlock(&root
->fs_info
->fs_mutex
);
1328 return ERR_PTR(ret
);
1331 if (location
.objectid
) {
1332 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1335 return ERR_PTR(ret
);
1337 return ERR_PTR(-ENOENT
);
1338 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1341 return ERR_PTR(-EACCES
);
1342 if (inode
->i_state
& I_NEW
) {
1343 /* the inode and parent dir are two different roots */
1344 if (sub_root
!= root
) {
1346 sub_root
->inode
= inode
;
1348 BTRFS_I(inode
)->root
= sub_root
;
1349 memcpy(&BTRFS_I(inode
)->location
, &location
,
1351 btrfs_read_locked_inode(inode
);
1352 unlock_new_inode(inode
);
1355 return d_splice_alias(inode
, dentry
);
1358 static unsigned char btrfs_filetype_table
[] = {
1359 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1362 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1364 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1365 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1366 struct btrfs_item
*item
;
1367 struct btrfs_dir_item
*di
;
1368 struct btrfs_key key
;
1369 struct btrfs_key found_key
;
1370 struct btrfs_path
*path
;
1373 struct extent_buffer
*leaf
;
1376 unsigned char d_type
;
1381 int key_type
= BTRFS_DIR_INDEX_KEY
;
1386 /* FIXME, use a real flag for deciding about the key type */
1387 if (root
->fs_info
->tree_root
== root
)
1388 key_type
= BTRFS_DIR_ITEM_KEY
;
1390 /* special case for "." */
1391 if (filp
->f_pos
== 0) {
1392 over
= filldir(dirent
, ".", 1,
1400 mutex_lock(&root
->fs_info
->fs_mutex
);
1401 key
.objectid
= inode
->i_ino
;
1402 path
= btrfs_alloc_path();
1405 /* special case for .., just use the back ref */
1406 if (filp
->f_pos
== 1) {
1407 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1409 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1411 leaf
= path
->nodes
[0];
1412 slot
= path
->slots
[0];
1413 nritems
= btrfs_header_nritems(leaf
);
1414 if (slot
>= nritems
) {
1415 btrfs_release_path(root
, path
);
1416 goto read_dir_items
;
1418 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1419 btrfs_release_path(root
, path
);
1420 if (found_key
.objectid
!= key
.objectid
||
1421 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1422 goto read_dir_items
;
1423 over
= filldir(dirent
, "..", 2,
1424 2, found_key
.offset
, DT_DIR
);
1431 btrfs_set_key_type(&key
, key_type
);
1432 key
.offset
= filp
->f_pos
;
1434 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1439 leaf
= path
->nodes
[0];
1440 nritems
= btrfs_header_nritems(leaf
);
1441 slot
= path
->slots
[0];
1442 if (advance
|| slot
>= nritems
) {
1443 if (slot
>= nritems
-1) {
1444 ret
= btrfs_next_leaf(root
, path
);
1447 leaf
= path
->nodes
[0];
1448 nritems
= btrfs_header_nritems(leaf
);
1449 slot
= path
->slots
[0];
1456 item
= btrfs_item_nr(leaf
, slot
);
1457 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1459 if (found_key
.objectid
!= key
.objectid
)
1461 if (btrfs_key_type(&found_key
) != key_type
)
1463 if (found_key
.offset
< filp
->f_pos
)
1466 filp
->f_pos
= found_key
.offset
;
1468 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1470 di_total
= btrfs_item_size(leaf
, item
);
1471 while(di_cur
< di_total
) {
1472 struct btrfs_key location
;
1474 name_len
= btrfs_dir_name_len(leaf
, di
);
1475 if (name_len
< 32) {
1476 name_ptr
= tmp_name
;
1478 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1481 read_extent_buffer(leaf
, name_ptr
,
1482 (unsigned long)(di
+ 1), name_len
);
1484 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1485 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1486 over
= filldir(dirent
, name_ptr
, name_len
,
1491 if (name_ptr
!= tmp_name
)
1496 di_len
= btrfs_dir_name_len(leaf
, di
) +
1497 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1499 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1502 if (key_type
== BTRFS_DIR_INDEX_KEY
)
1503 filp
->f_pos
= INT_LIMIT(typeof(filp
->f_pos
));
1509 btrfs_release_path(root
, path
);
1510 btrfs_free_path(path
);
1511 mutex_unlock(&root
->fs_info
->fs_mutex
);
1515 int btrfs_write_inode(struct inode
*inode
, int wait
)
1517 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1518 struct btrfs_trans_handle
*trans
;
1522 mutex_lock(&root
->fs_info
->fs_mutex
);
1523 trans
= btrfs_start_transaction(root
, 1);
1524 btrfs_set_trans_block_group(trans
, inode
);
1525 ret
= btrfs_commit_transaction(trans
, root
);
1526 mutex_unlock(&root
->fs_info
->fs_mutex
);
1532 * This is somewhat expensive, updating the tree every time the
1533 * inode changes. But, it is most likely to find the inode in cache.
1534 * FIXME, needs more benchmarking...there are no reasons other than performance
1535 * to keep or drop this code.
1537 void btrfs_dirty_inode(struct inode
*inode
)
1539 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1540 struct btrfs_trans_handle
*trans
;
1542 mutex_lock(&root
->fs_info
->fs_mutex
);
1543 trans
= btrfs_start_transaction(root
, 1);
1544 btrfs_set_trans_block_group(trans
, inode
);
1545 btrfs_update_inode(trans
, root
, inode
);
1546 btrfs_end_transaction(trans
, root
);
1547 mutex_unlock(&root
->fs_info
->fs_mutex
);
1550 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1551 struct btrfs_root
*root
,
1552 const char *name
, int name_len
,
1555 struct btrfs_block_group_cache
*group
,
1558 struct inode
*inode
;
1559 struct btrfs_inode_item
*inode_item
;
1560 struct btrfs_block_group_cache
*new_inode_group
;
1561 struct btrfs_key
*location
;
1562 struct btrfs_path
*path
;
1563 struct btrfs_inode_ref
*ref
;
1564 struct btrfs_key key
[2];
1570 path
= btrfs_alloc_path();
1573 inode
= new_inode(root
->fs_info
->sb
);
1575 return ERR_PTR(-ENOMEM
);
1577 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1578 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1579 inode
->i_mapping
, GFP_NOFS
);
1580 BTRFS_I(inode
)->delalloc_bytes
= 0;
1581 BTRFS_I(inode
)->root
= root
;
1587 new_inode_group
= btrfs_find_block_group(root
, group
, 0,
1588 BTRFS_BLOCK_GROUP_METADATA
, owner
);
1589 if (!new_inode_group
) {
1590 printk("find_block group failed\n");
1591 new_inode_group
= group
;
1593 BTRFS_I(inode
)->block_group
= new_inode_group
;
1594 BTRFS_I(inode
)->flags
= 0;
1596 key
[0].objectid
= objectid
;
1597 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
1600 key
[1].objectid
= objectid
;
1601 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
1602 key
[1].offset
= ref_objectid
;
1604 sizes
[0] = sizeof(struct btrfs_inode_item
);
1605 sizes
[1] = name_len
+ sizeof(*ref
);
1607 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
1611 if (objectid
> root
->highest_inode
)
1612 root
->highest_inode
= objectid
;
1614 inode
->i_uid
= current
->fsuid
;
1615 inode
->i_gid
= current
->fsgid
;
1616 inode
->i_mode
= mode
;
1617 inode
->i_ino
= objectid
;
1618 inode
->i_blocks
= 0;
1619 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1620 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1621 struct btrfs_inode_item
);
1622 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1624 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1625 struct btrfs_inode_ref
);
1626 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
1627 ptr
= (unsigned long)(ref
+ 1);
1628 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
1630 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1631 btrfs_free_path(path
);
1633 location
= &BTRFS_I(inode
)->location
;
1634 location
->objectid
= objectid
;
1635 location
->offset
= 0;
1636 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1638 insert_inode_hash(inode
);
1641 btrfs_free_path(path
);
1642 return ERR_PTR(ret
);
1645 static inline u8
btrfs_inode_type(struct inode
*inode
)
1647 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1650 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1651 struct dentry
*dentry
, struct inode
*inode
,
1655 struct btrfs_key key
;
1656 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1657 struct inode
*parent_inode
;
1659 key
.objectid
= inode
->i_ino
;
1660 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1663 ret
= btrfs_insert_dir_item(trans
, root
,
1664 dentry
->d_name
.name
, dentry
->d_name
.len
,
1665 dentry
->d_parent
->d_inode
->i_ino
,
1666 &key
, btrfs_inode_type(inode
));
1669 ret
= btrfs_insert_inode_ref(trans
, root
,
1670 dentry
->d_name
.name
,
1673 dentry
->d_parent
->d_inode
->i_ino
);
1675 parent_inode
= dentry
->d_parent
->d_inode
;
1676 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1677 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1678 ret
= btrfs_update_inode(trans
, root
,
1679 dentry
->d_parent
->d_inode
);
1684 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1685 struct dentry
*dentry
, struct inode
*inode
,
1688 int err
= btrfs_add_link(trans
, dentry
, inode
, backref
);
1690 d_instantiate(dentry
, inode
);
1698 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1699 int mode
, dev_t rdev
)
1701 struct btrfs_trans_handle
*trans
;
1702 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1703 struct inode
*inode
= NULL
;
1707 unsigned long nr
= 0;
1709 if (!new_valid_dev(rdev
))
1712 mutex_lock(&root
->fs_info
->fs_mutex
);
1713 err
= btrfs_check_free_space(root
, 1, 0);
1717 trans
= btrfs_start_transaction(root
, 1);
1718 btrfs_set_trans_block_group(trans
, dir
);
1720 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1726 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1728 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1729 BTRFS_I(dir
)->block_group
, mode
);
1730 err
= PTR_ERR(inode
);
1734 btrfs_set_trans_block_group(trans
, inode
);
1735 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1739 inode
->i_op
= &btrfs_special_inode_operations
;
1740 init_special_inode(inode
, inode
->i_mode
, rdev
);
1741 btrfs_update_inode(trans
, root
, inode
);
1743 dir
->i_sb
->s_dirt
= 1;
1744 btrfs_update_inode_block_group(trans
, inode
);
1745 btrfs_update_inode_block_group(trans
, dir
);
1747 nr
= trans
->blocks_used
;
1748 btrfs_end_transaction(trans
, root
);
1750 mutex_unlock(&root
->fs_info
->fs_mutex
);
1753 inode_dec_link_count(inode
);
1756 btrfs_btree_balance_dirty(root
, nr
);
1757 btrfs_throttle(root
);
1761 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1762 int mode
, struct nameidata
*nd
)
1764 struct btrfs_trans_handle
*trans
;
1765 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1766 struct inode
*inode
= NULL
;
1769 unsigned long nr
= 0;
1772 mutex_lock(&root
->fs_info
->fs_mutex
);
1773 err
= btrfs_check_free_space(root
, 1, 0);
1776 trans
= btrfs_start_transaction(root
, 1);
1777 btrfs_set_trans_block_group(trans
, dir
);
1779 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1785 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1787 dentry
->d_parent
->d_inode
->i_ino
,
1788 objectid
, BTRFS_I(dir
)->block_group
, mode
);
1789 err
= PTR_ERR(inode
);
1793 btrfs_set_trans_block_group(trans
, inode
);
1794 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
1798 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1799 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
1800 inode
->i_fop
= &btrfs_file_operations
;
1801 inode
->i_op
= &btrfs_file_inode_operations
;
1802 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
1803 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
1804 inode
->i_mapping
, GFP_NOFS
);
1805 BTRFS_I(inode
)->delalloc_bytes
= 0;
1806 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
1808 dir
->i_sb
->s_dirt
= 1;
1809 btrfs_update_inode_block_group(trans
, inode
);
1810 btrfs_update_inode_block_group(trans
, dir
);
1812 nr
= trans
->blocks_used
;
1813 btrfs_end_transaction(trans
, root
);
1815 mutex_unlock(&root
->fs_info
->fs_mutex
);
1818 inode_dec_link_count(inode
);
1821 btrfs_btree_balance_dirty(root
, nr
);
1822 btrfs_throttle(root
);
1826 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1827 struct dentry
*dentry
)
1829 struct btrfs_trans_handle
*trans
;
1830 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1831 struct inode
*inode
= old_dentry
->d_inode
;
1832 unsigned long nr
= 0;
1836 if (inode
->i_nlink
== 0)
1839 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1844 mutex_lock(&root
->fs_info
->fs_mutex
);
1845 err
= btrfs_check_free_space(root
, 1, 0);
1848 trans
= btrfs_start_transaction(root
, 1);
1850 btrfs_set_trans_block_group(trans
, dir
);
1851 atomic_inc(&inode
->i_count
);
1852 err
= btrfs_add_nondir(trans
, dentry
, inode
, 1);
1857 dir
->i_sb
->s_dirt
= 1;
1858 btrfs_update_inode_block_group(trans
, dir
);
1859 err
= btrfs_update_inode(trans
, root
, inode
);
1864 nr
= trans
->blocks_used
;
1865 btrfs_end_transaction(trans
, root
);
1867 mutex_unlock(&root
->fs_info
->fs_mutex
);
1870 inode_dec_link_count(inode
);
1873 btrfs_btree_balance_dirty(root
, nr
);
1874 btrfs_throttle(root
);
1878 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1880 struct inode
*inode
;
1881 struct btrfs_trans_handle
*trans
;
1882 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1884 int drop_on_err
= 0;
1886 unsigned long nr
= 1;
1888 mutex_lock(&root
->fs_info
->fs_mutex
);
1889 err
= btrfs_check_free_space(root
, 1, 0);
1893 trans
= btrfs_start_transaction(root
, 1);
1894 btrfs_set_trans_block_group(trans
, dir
);
1896 if (IS_ERR(trans
)) {
1897 err
= PTR_ERR(trans
);
1901 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1907 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
1909 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
1910 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1911 if (IS_ERR(inode
)) {
1912 err
= PTR_ERR(inode
);
1917 inode
->i_op
= &btrfs_dir_inode_operations
;
1918 inode
->i_fop
= &btrfs_dir_file_operations
;
1919 btrfs_set_trans_block_group(trans
, inode
);
1922 err
= btrfs_update_inode(trans
, root
, inode
);
1926 err
= btrfs_add_link(trans
, dentry
, inode
, 0);
1930 d_instantiate(dentry
, inode
);
1932 dir
->i_sb
->s_dirt
= 1;
1933 btrfs_update_inode_block_group(trans
, inode
);
1934 btrfs_update_inode_block_group(trans
, dir
);
1937 nr
= trans
->blocks_used
;
1938 btrfs_end_transaction(trans
, root
);
1941 mutex_unlock(&root
->fs_info
->fs_mutex
);
1944 btrfs_btree_balance_dirty(root
, nr
);
1945 btrfs_throttle(root
);
1949 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1950 size_t pg_offset
, u64 start
, u64 len
,
1956 u64 extent_start
= 0;
1958 u64 objectid
= inode
->i_ino
;
1960 struct btrfs_path
*path
;
1961 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1962 struct btrfs_file_extent_item
*item
;
1963 struct extent_buffer
*leaf
;
1964 struct btrfs_key found_key
;
1965 struct extent_map
*em
= NULL
;
1966 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1967 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
1968 struct btrfs_trans_handle
*trans
= NULL
;
1970 path
= btrfs_alloc_path();
1972 mutex_lock(&root
->fs_info
->fs_mutex
);
1975 spin_lock(&em_tree
->lock
);
1976 em
= lookup_extent_mapping(em_tree
, start
, len
);
1977 spin_unlock(&em_tree
->lock
);
1980 if (em
->start
> start
) {
1981 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1982 start
, len
, em
->start
, em
->len
);
1985 if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
1986 free_extent_map(em
);
1990 em
= alloc_extent_map(GFP_NOFS
);
1996 em
->start
= EXTENT_MAP_HOLE
;
1998 em
->bdev
= inode
->i_sb
->s_bdev
;
1999 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
2000 objectid
, start
, trans
!= NULL
);
2007 if (path
->slots
[0] == 0)
2012 leaf
= path
->nodes
[0];
2013 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2014 struct btrfs_file_extent_item
);
2015 /* are we inside the extent that was found? */
2016 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2017 found_type
= btrfs_key_type(&found_key
);
2018 if (found_key
.objectid
!= objectid
||
2019 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
2023 found_type
= btrfs_file_extent_type(leaf
, item
);
2024 extent_start
= found_key
.offset
;
2025 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
2026 extent_end
= extent_start
+
2027 btrfs_file_extent_num_bytes(leaf
, item
);
2029 if (start
< extent_start
|| start
>= extent_end
) {
2031 if (start
< extent_start
) {
2032 if (start
+ len
<= extent_start
)
2034 em
->len
= extent_end
- extent_start
;
2040 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
2042 em
->start
= extent_start
;
2043 em
->len
= extent_end
- extent_start
;
2044 em
->block_start
= EXTENT_MAP_HOLE
;
2047 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
2048 em
->block_start
= bytenr
;
2049 em
->start
= extent_start
;
2050 em
->len
= extent_end
- extent_start
;
2052 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
2057 size_t extent_offset
;
2060 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
2062 extent_end
= (extent_start
+ size
+ root
->sectorsize
- 1) &
2063 ~((u64
)root
->sectorsize
- 1);
2064 if (start
< extent_start
|| start
>= extent_end
) {
2066 if (start
< extent_start
) {
2067 if (start
+ len
<= extent_start
)
2069 em
->len
= extent_end
- extent_start
;
2075 em
->block_start
= EXTENT_MAP_INLINE
;
2078 em
->start
= extent_start
;
2083 page_start
= page_offset(page
) + pg_offset
;
2084 extent_offset
= page_start
- extent_start
;
2085 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
2086 size
- extent_offset
);
2087 em
->start
= extent_start
+ extent_offset
;
2088 em
->len
= (copy_size
+ root
->sectorsize
- 1) &
2089 ~((u64
)root
->sectorsize
- 1);
2091 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
2092 if (create
== 0 && !PageUptodate(page
)) {
2093 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2095 flush_dcache_page(page
);
2096 } else if (create
&& PageUptodate(page
)) {
2099 free_extent_map(em
);
2101 btrfs_release_path(root
, path
);
2102 trans
= btrfs_start_transaction(root
, 1);
2105 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
2107 btrfs_mark_buffer_dirty(leaf
);
2110 set_extent_uptodate(io_tree
, em
->start
,
2111 extent_map_end(em
) - 1, GFP_NOFS
);
2114 printk("unkknown found_type %d\n", found_type
);
2121 em
->block_start
= EXTENT_MAP_HOLE
;
2123 btrfs_release_path(root
, path
);
2124 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
2125 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->len
, start
, len
);
2131 spin_lock(&em_tree
->lock
);
2132 ret
= add_extent_mapping(em_tree
, em
);
2133 if (ret
== -EEXIST
) {
2134 free_extent_map(em
);
2135 em
= lookup_extent_mapping(em_tree
, start
, len
);
2138 printk("failing to insert %Lu %Lu\n", start
, len
);
2141 spin_unlock(&em_tree
->lock
);
2143 btrfs_free_path(path
);
2145 ret
= btrfs_end_transaction(trans
, root
);
2149 mutex_unlock(&root
->fs_info
->fs_mutex
);
2151 free_extent_map(em
);
2153 return ERR_PTR(err
);
2158 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2160 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2163 int btrfs_readpage(struct file
*file
, struct page
*page
)
2165 struct extent_io_tree
*tree
;
2166 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2167 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2170 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2172 struct extent_io_tree
*tree
;
2175 if (current
->flags
& PF_MEMALLOC
) {
2176 redirty_page_for_writepage(wbc
, page
);
2180 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2181 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2184 static int btrfs_writepages(struct address_space
*mapping
,
2185 struct writeback_control
*wbc
)
2187 struct extent_io_tree
*tree
;
2188 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2189 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2193 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2194 struct list_head
*pages
, unsigned nr_pages
)
2196 struct extent_io_tree
*tree
;
2197 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
2198 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2202 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
2204 struct extent_io_tree
*tree
;
2205 struct extent_map_tree
*map
;
2208 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2209 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2210 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
2212 ClearPagePrivate(page
);
2213 set_page_private(page
, 0);
2214 page_cache_release(page
);
2219 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2221 struct extent_io_tree
*tree
;
2223 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
2224 extent_invalidatepage(tree
, page
, offset
);
2225 btrfs_releasepage(page
, GFP_NOFS
);
2229 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2230 * called from a page fault handler when a page is first dirtied. Hence we must
2231 * be careful to check for EOF conditions here. We set the page up correctly
2232 * for a written page which means we get ENOSPC checking when writing into
2233 * holes and correct delalloc and unwritten extent mapping on filesystems that
2234 * support these features.
2236 * We are not allowed to take the i_mutex here so we have to play games to
2237 * protect against truncate races as the page could now be beyond EOF. Because
2238 * vmtruncate() writes the inode size before removing pages, once we have the
2239 * page lock we can determine safely if the page is beyond EOF. If it is not
2240 * beyond EOF, then the page is guaranteed safe against truncation until we
2243 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2245 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2246 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2252 mutex_lock(&root
->fs_info
->fs_mutex
);
2253 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2254 mutex_unlock(&root
->fs_info
->fs_mutex
);
2261 wait_on_page_writeback(page
);
2262 size
= i_size_read(inode
);
2263 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2265 if ((page
->mapping
!= inode
->i_mapping
) ||
2266 (page_start
> size
)) {
2267 /* page got truncated out from underneath us */
2271 /* page is wholly or partially inside EOF */
2272 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2273 end
= size
& ~PAGE_CACHE_MASK
;
2275 end
= PAGE_CACHE_SIZE
;
2277 ret
= btrfs_cow_one_page(inode
, page
, end
);
2285 static void btrfs_truncate(struct inode
*inode
)
2287 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2289 struct btrfs_trans_handle
*trans
;
2292 if (!S_ISREG(inode
->i_mode
))
2294 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2297 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2299 mutex_lock(&root
->fs_info
->fs_mutex
);
2300 trans
= btrfs_start_transaction(root
, 1);
2301 btrfs_set_trans_block_group(trans
, inode
);
2303 /* FIXME, add redo link to tree so we don't leak on crash */
2304 ret
= btrfs_truncate_in_trans(trans
, root
, inode
,
2305 BTRFS_EXTENT_DATA_KEY
);
2306 btrfs_update_inode(trans
, root
, inode
);
2307 nr
= trans
->blocks_used
;
2309 ret
= btrfs_end_transaction(trans
, root
);
2311 mutex_unlock(&root
->fs_info
->fs_mutex
);
2312 btrfs_btree_balance_dirty(root
, nr
);
2313 btrfs_throttle(root
);
2316 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2319 struct btrfs_trans_handle
*trans
;
2320 struct btrfs_key key
;
2321 struct btrfs_root_item root_item
;
2322 struct btrfs_inode_item
*inode_item
;
2323 struct extent_buffer
*leaf
;
2324 struct btrfs_root
*new_root
= root
;
2325 struct inode
*inode
;
2330 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2331 unsigned long nr
= 1;
2333 mutex_lock(&root
->fs_info
->fs_mutex
);
2334 ret
= btrfs_check_free_space(root
, 1, 0);
2338 trans
= btrfs_start_transaction(root
, 1);
2341 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2346 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2347 objectid
, trans
->transid
, 0, 0,
2350 return PTR_ERR(leaf
);
2352 btrfs_set_header_nritems(leaf
, 0);
2353 btrfs_set_header_level(leaf
, 0);
2354 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2355 btrfs_set_header_generation(leaf
, trans
->transid
);
2356 btrfs_set_header_owner(leaf
, objectid
);
2358 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2359 (unsigned long)btrfs_header_fsid(leaf
),
2361 btrfs_mark_buffer_dirty(leaf
);
2363 inode_item
= &root_item
.inode
;
2364 memset(inode_item
, 0, sizeof(*inode_item
));
2365 inode_item
->generation
= cpu_to_le64(1);
2366 inode_item
->size
= cpu_to_le64(3);
2367 inode_item
->nlink
= cpu_to_le32(1);
2368 inode_item
->nblocks
= cpu_to_le64(1);
2369 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2371 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2372 btrfs_set_root_level(&root_item
, 0);
2373 btrfs_set_root_refs(&root_item
, 1);
2374 btrfs_set_root_used(&root_item
, 0);
2376 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2377 root_item
.drop_level
= 0;
2379 free_extent_buffer(leaf
);
2382 btrfs_set_root_dirid(&root_item
, new_dirid
);
2384 key
.objectid
= objectid
;
2386 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2387 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2393 * insert the directory item
2395 key
.offset
= (u64
)-1;
2396 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2397 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2398 name
, namelen
, dir
->i_ino
, &key
,
2403 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2404 name
, namelen
, objectid
,
2405 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2409 ret
= btrfs_commit_transaction(trans
, root
);
2413 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2416 trans
= btrfs_start_transaction(new_root
, 1);
2419 inode
= btrfs_new_inode(trans
, new_root
, "..", 2, new_dirid
,
2421 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2424 inode
->i_op
= &btrfs_dir_inode_operations
;
2425 inode
->i_fop
= &btrfs_dir_file_operations
;
2426 new_root
->inode
= inode
;
2428 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2432 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2436 nr
= trans
->blocks_used
;
2437 err
= btrfs_commit_transaction(trans
, new_root
);
2441 mutex_unlock(&root
->fs_info
->fs_mutex
);
2442 btrfs_btree_balance_dirty(root
, nr
);
2443 btrfs_throttle(root
);
2447 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2449 struct btrfs_pending_snapshot
*pending_snapshot
;
2450 struct btrfs_trans_handle
*trans
;
2453 unsigned long nr
= 0;
2455 if (!root
->ref_cows
)
2458 mutex_lock(&root
->fs_info
->fs_mutex
);
2459 ret
= btrfs_check_free_space(root
, 1, 0);
2463 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2464 if (!pending_snapshot
) {
2468 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
2469 if (!pending_snapshot
->name
) {
2471 kfree(pending_snapshot
);
2474 memcpy(pending_snapshot
->name
, name
, namelen
);
2475 pending_snapshot
->name
[namelen
] = '\0';
2476 trans
= btrfs_start_transaction(root
, 1);
2478 pending_snapshot
->root
= root
;
2479 list_add(&pending_snapshot
->list
,
2480 &trans
->transaction
->pending_snapshots
);
2481 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2482 err
= btrfs_commit_transaction(trans
, root
);
2485 mutex_unlock(&root
->fs_info
->fs_mutex
);
2486 btrfs_btree_balance_dirty(root
, nr
);
2487 btrfs_throttle(root
);
2491 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2492 struct file_ra_state
*ra
, struct file
*file
,
2493 pgoff_t offset
, pgoff_t last_index
)
2497 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2498 req_size
= last_index
- offset
+ 1;
2499 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2502 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2503 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2504 return offset
+ req_size
;
2508 int btrfs_defrag_file(struct file
*file
) {
2509 struct inode
*inode
= fdentry(file
)->d_inode
;
2510 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2511 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2513 unsigned long last_index
;
2514 unsigned long ra_index
= 0;
2520 mutex_lock(&root
->fs_info
->fs_mutex
);
2521 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2522 mutex_unlock(&root
->fs_info
->fs_mutex
);
2526 mutex_lock(&inode
->i_mutex
);
2527 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2528 for (i
= 0; i
<= last_index
; i
++) {
2529 if (i
== ra_index
) {
2530 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2532 file
, ra_index
, last_index
);
2534 page
= grab_cache_page(inode
->i_mapping
, i
);
2537 if (!PageUptodate(page
)) {
2538 btrfs_readpage(NULL
, page
);
2540 if (!PageUptodate(page
)) {
2542 page_cache_release(page
);
2546 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2547 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2549 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2550 set_extent_delalloc(io_tree
, page_start
,
2551 page_end
, GFP_NOFS
);
2553 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2554 set_page_dirty(page
);
2556 page_cache_release(page
);
2557 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2561 mutex_unlock(&inode
->i_mutex
);
2565 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2569 struct btrfs_ioctl_vol_args
*vol_args
;
2570 struct btrfs_trans_handle
*trans
;
2576 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2581 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2585 namelen
= strlen(vol_args
->name
);
2586 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2591 sizestr
= vol_args
->name
;
2592 if (!strcmp(sizestr
, "max"))
2593 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2595 if (sizestr
[0] == '-') {
2598 } else if (sizestr
[0] == '+') {
2602 new_size
= btrfs_parse_size(sizestr
);
2603 if (new_size
== 0) {
2609 mutex_lock(&root
->fs_info
->fs_mutex
);
2610 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2613 if (new_size
> old_size
) {
2617 new_size
= old_size
- new_size
;
2618 } else if (mod
> 0) {
2619 new_size
= old_size
+ new_size
;
2622 if (new_size
< 256 * 1024 * 1024) {
2626 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2631 do_div(new_size
, root
->sectorsize
);
2632 new_size
*= root
->sectorsize
;
2634 printk("new size is %Lu\n", new_size
);
2635 if (new_size
> old_size
) {
2636 trans
= btrfs_start_transaction(root
, 1);
2637 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2638 btrfs_commit_transaction(trans
, root
);
2640 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2644 mutex_unlock(&root
->fs_info
->fs_mutex
);
2650 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
2653 struct btrfs_ioctl_vol_args
*vol_args
;
2654 struct btrfs_dir_item
*di
;
2655 struct btrfs_path
*path
;
2660 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2665 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2670 namelen
= strlen(vol_args
->name
);
2671 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2675 if (strchr(vol_args
->name
, '/')) {
2680 path
= btrfs_alloc_path();
2686 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2687 mutex_lock(&root
->fs_info
->fs_mutex
);
2688 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2690 vol_args
->name
, namelen
, 0);
2691 mutex_unlock(&root
->fs_info
->fs_mutex
);
2692 btrfs_free_path(path
);
2694 if (di
&& !IS_ERR(di
)) {
2704 if (root
== root
->fs_info
->tree_root
)
2705 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2707 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2713 static int btrfs_ioctl_defrag(struct file
*file
)
2715 struct inode
*inode
= fdentry(file
)->d_inode
;
2716 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2718 switch (inode
->i_mode
& S_IFMT
) {
2720 mutex_lock(&root
->fs_info
->fs_mutex
);
2721 btrfs_defrag_root(root
, 0);
2722 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2723 mutex_unlock(&root
->fs_info
->fs_mutex
);
2726 btrfs_defrag_file(file
);
2733 long btrfs_ioctl(struct file
*file
, unsigned int
2734 cmd
, unsigned long arg
)
2736 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2739 case BTRFS_IOC_SNAP_CREATE
:
2740 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2741 case BTRFS_IOC_DEFRAG
:
2742 return btrfs_ioctl_defrag(file
);
2743 case BTRFS_IOC_RESIZE
:
2744 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2751 * Called inside transaction, so use GFP_NOFS
2753 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2755 struct btrfs_inode
*ei
;
2757 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2761 ei
->ordered_trans
= 0;
2762 return &ei
->vfs_inode
;
2765 void btrfs_destroy_inode(struct inode
*inode
)
2767 WARN_ON(!list_empty(&inode
->i_dentry
));
2768 WARN_ON(inode
->i_data
.nrpages
);
2770 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2771 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2774 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2775 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2777 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2778 unsigned long flags
)
2781 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2783 inode_init_once(&ei
->vfs_inode
);
2786 void btrfs_destroy_cachep(void)
2788 if (btrfs_inode_cachep
)
2789 kmem_cache_destroy(btrfs_inode_cachep
);
2790 if (btrfs_trans_handle_cachep
)
2791 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2792 if (btrfs_transaction_cachep
)
2793 kmem_cache_destroy(btrfs_transaction_cachep
);
2794 if (btrfs_bit_radix_cachep
)
2795 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2796 if (btrfs_path_cachep
)
2797 kmem_cache_destroy(btrfs_path_cachep
);
2800 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2801 unsigned long extra_flags
,
2802 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2803 void (*ctor
)(struct kmem_cache
*, void *)
2805 void (*ctor
)(void *, struct kmem_cache
*,
2810 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2811 SLAB_MEM_SPREAD
| extra_flags
), ctor
2812 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2818 int btrfs_init_cachep(void)
2820 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2821 sizeof(struct btrfs_inode
),
2823 if (!btrfs_inode_cachep
)
2825 btrfs_trans_handle_cachep
=
2826 btrfs_cache_create("btrfs_trans_handle_cache",
2827 sizeof(struct btrfs_trans_handle
),
2829 if (!btrfs_trans_handle_cachep
)
2831 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2832 sizeof(struct btrfs_transaction
),
2834 if (!btrfs_transaction_cachep
)
2836 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2837 sizeof(struct btrfs_path
),
2839 if (!btrfs_path_cachep
)
2841 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2842 SLAB_DESTROY_BY_RCU
, NULL
);
2843 if (!btrfs_bit_radix_cachep
)
2847 btrfs_destroy_cachep();
2851 static int btrfs_getattr(struct vfsmount
*mnt
,
2852 struct dentry
*dentry
, struct kstat
*stat
)
2854 struct inode
*inode
= dentry
->d_inode
;
2855 generic_fillattr(inode
, stat
);
2856 stat
->blksize
= PAGE_CACHE_SIZE
;
2857 stat
->blocks
= inode
->i_blocks
+ (BTRFS_I(inode
)->delalloc_bytes
>> 9);
2861 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2862 struct inode
* new_dir
,struct dentry
*new_dentry
)
2864 struct btrfs_trans_handle
*trans
;
2865 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2866 struct inode
*new_inode
= new_dentry
->d_inode
;
2867 struct inode
*old_inode
= old_dentry
->d_inode
;
2868 struct timespec ctime
= CURRENT_TIME
;
2869 struct btrfs_path
*path
;
2872 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2873 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2877 mutex_lock(&root
->fs_info
->fs_mutex
);
2878 ret
= btrfs_check_free_space(root
, 1, 0);
2882 trans
= btrfs_start_transaction(root
, 1);
2884 btrfs_set_trans_block_group(trans
, new_dir
);
2885 path
= btrfs_alloc_path();
2891 old_dentry
->d_inode
->i_nlink
++;
2892 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2893 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2894 old_inode
->i_ctime
= ctime
;
2896 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2901 new_inode
->i_ctime
= CURRENT_TIME
;
2902 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2906 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
, 1);
2911 btrfs_free_path(path
);
2912 btrfs_end_transaction(trans
, root
);
2914 mutex_unlock(&root
->fs_info
->fs_mutex
);
2918 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2919 const char *symname
)
2921 struct btrfs_trans_handle
*trans
;
2922 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2923 struct btrfs_path
*path
;
2924 struct btrfs_key key
;
2925 struct inode
*inode
= NULL
;
2932 struct btrfs_file_extent_item
*ei
;
2933 struct extent_buffer
*leaf
;
2934 unsigned long nr
= 0;
2936 name_len
= strlen(symname
) + 1;
2937 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2938 return -ENAMETOOLONG
;
2940 mutex_lock(&root
->fs_info
->fs_mutex
);
2941 err
= btrfs_check_free_space(root
, 1, 0);
2945 trans
= btrfs_start_transaction(root
, 1);
2946 btrfs_set_trans_block_group(trans
, dir
);
2948 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2954 inode
= btrfs_new_inode(trans
, root
, dentry
->d_name
.name
,
2956 dentry
->d_parent
->d_inode
->i_ino
, objectid
,
2957 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2958 err
= PTR_ERR(inode
);
2962 btrfs_set_trans_block_group(trans
, inode
);
2963 err
= btrfs_add_nondir(trans
, dentry
, inode
, 0);
2967 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2968 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
2969 inode
->i_fop
= &btrfs_file_operations
;
2970 inode
->i_op
= &btrfs_file_inode_operations
;
2971 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
, GFP_NOFS
);
2972 extent_io_tree_init(&BTRFS_I(inode
)->io_tree
,
2973 inode
->i_mapping
, GFP_NOFS
);
2974 BTRFS_I(inode
)->delalloc_bytes
= 0;
2975 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
2977 dir
->i_sb
->s_dirt
= 1;
2978 btrfs_update_inode_block_group(trans
, inode
);
2979 btrfs_update_inode_block_group(trans
, dir
);
2983 path
= btrfs_alloc_path();
2985 key
.objectid
= inode
->i_ino
;
2987 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2988 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2989 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2995 leaf
= path
->nodes
[0];
2996 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2997 struct btrfs_file_extent_item
);
2998 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2999 btrfs_set_file_extent_type(leaf
, ei
,
3000 BTRFS_FILE_EXTENT_INLINE
);
3001 ptr
= btrfs_file_extent_inline_start(ei
);
3002 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
3003 btrfs_mark_buffer_dirty(leaf
);
3004 btrfs_free_path(path
);
3006 inode
->i_op
= &btrfs_symlink_inode_operations
;
3007 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3008 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3009 inode
->i_size
= name_len
- 1;
3010 err
= btrfs_update_inode(trans
, root
, inode
);
3015 nr
= trans
->blocks_used
;
3016 btrfs_end_transaction(trans
, root
);
3018 mutex_unlock(&root
->fs_info
->fs_mutex
);
3020 inode_dec_link_count(inode
);
3023 btrfs_btree_balance_dirty(root
, nr
);
3024 btrfs_throttle(root
);
3027 static int btrfs_permission(struct inode
*inode
, int mask
,
3028 struct nameidata
*nd
)
3030 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
3032 return generic_permission(inode
, mask
, NULL
);
3035 static struct inode_operations btrfs_dir_inode_operations
= {
3036 .lookup
= btrfs_lookup
,
3037 .create
= btrfs_create
,
3038 .unlink
= btrfs_unlink
,
3040 .mkdir
= btrfs_mkdir
,
3041 .rmdir
= btrfs_rmdir
,
3042 .rename
= btrfs_rename
,
3043 .symlink
= btrfs_symlink
,
3044 .setattr
= btrfs_setattr
,
3045 .mknod
= btrfs_mknod
,
3046 .setxattr
= generic_setxattr
,
3047 .getxattr
= generic_getxattr
,
3048 .listxattr
= btrfs_listxattr
,
3049 .removexattr
= generic_removexattr
,
3050 .permission
= btrfs_permission
,
3052 static struct inode_operations btrfs_dir_ro_inode_operations
= {
3053 .lookup
= btrfs_lookup
,
3054 .permission
= btrfs_permission
,
3056 static struct file_operations btrfs_dir_file_operations
= {
3057 .llseek
= generic_file_llseek
,
3058 .read
= generic_read_dir
,
3059 .readdir
= btrfs_readdir
,
3060 .unlocked_ioctl
= btrfs_ioctl
,
3061 #ifdef CONFIG_COMPAT
3062 .compat_ioctl
= btrfs_ioctl
,
3066 static struct extent_io_ops btrfs_extent_io_ops
= {
3067 .fill_delalloc
= run_delalloc_range
,
3068 .submit_bio_hook
= btrfs_submit_bio_hook
,
3069 .merge_bio_hook
= btrfs_merge_bio_hook
,
3070 .readpage_io_hook
= btrfs_readpage_io_hook
,
3071 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
3072 .set_bit_hook
= btrfs_set_bit_hook
,
3073 .clear_bit_hook
= btrfs_clear_bit_hook
,
3076 static struct address_space_operations btrfs_aops
= {
3077 .readpage
= btrfs_readpage
,
3078 .writepage
= btrfs_writepage
,
3079 .writepages
= btrfs_writepages
,
3080 .readpages
= btrfs_readpages
,
3081 .sync_page
= block_sync_page
,
3083 .invalidatepage
= btrfs_invalidatepage
,
3084 .releasepage
= btrfs_releasepage
,
3085 .set_page_dirty
= __set_page_dirty_nobuffers
,
3088 static struct address_space_operations btrfs_symlink_aops
= {
3089 .readpage
= btrfs_readpage
,
3090 .writepage
= btrfs_writepage
,
3091 .invalidatepage
= btrfs_invalidatepage
,
3092 .releasepage
= btrfs_releasepage
,
3095 static struct inode_operations btrfs_file_inode_operations
= {
3096 .truncate
= btrfs_truncate
,
3097 .getattr
= btrfs_getattr
,
3098 .setattr
= btrfs_setattr
,
3099 .setxattr
= generic_setxattr
,
3100 .getxattr
= generic_getxattr
,
3101 .listxattr
= btrfs_listxattr
,
3102 .removexattr
= generic_removexattr
,
3103 .permission
= btrfs_permission
,
3105 static struct inode_operations btrfs_special_inode_operations
= {
3106 .getattr
= btrfs_getattr
,
3107 .setattr
= btrfs_setattr
,
3108 .permission
= btrfs_permission
,
3110 static struct inode_operations btrfs_symlink_inode_operations
= {
3111 .readlink
= generic_readlink
,
3112 .follow_link
= page_follow_link_light
,
3113 .put_link
= page_put_link
,
3114 .permission
= btrfs_permission
,