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/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
35 #include <linux/xattr.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "print-tree.h"
43 struct btrfs_iget_args
{
45 struct btrfs_root
*root
;
48 static struct inode_operations btrfs_dir_inode_operations
;
49 static struct inode_operations btrfs_symlink_inode_operations
;
50 static struct inode_operations btrfs_dir_ro_inode_operations
;
51 static struct inode_operations btrfs_special_inode_operations
;
52 static struct inode_operations btrfs_file_inode_operations
;
53 static struct address_space_operations btrfs_aops
;
54 static struct address_space_operations btrfs_symlink_aops
;
55 static struct file_operations btrfs_dir_file_operations
;
56 static struct extent_map_ops btrfs_extent_map_ops
;
58 static struct kmem_cache
*btrfs_inode_cachep
;
59 struct kmem_cache
*btrfs_trans_handle_cachep
;
60 struct kmem_cache
*btrfs_transaction_cachep
;
61 struct kmem_cache
*btrfs_bit_radix_cachep
;
62 struct kmem_cache
*btrfs_path_cachep
;
65 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
66 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
67 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
68 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
69 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
70 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
71 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
72 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
75 int btrfs_check_free_space(struct btrfs_root
*root
, u64 num_required
,
78 u64 total
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
79 u64 used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
84 thresh
= (total
* 90) / 100;
86 thresh
= (total
* 85) / 100;
88 spin_lock(&root
->fs_info
->delalloc_lock
);
89 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
91 spin_unlock(&root
->fs_info
->delalloc_lock
);
95 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
97 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
98 struct btrfs_trans_handle
*trans
;
102 u64 blocksize
= root
->sectorsize
;
103 struct btrfs_key ins
;
106 trans
= btrfs_start_transaction(root
, 1);
108 btrfs_set_trans_block_group(trans
, inode
);
110 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
111 num_bytes
= max(blocksize
, num_bytes
);
112 ret
= btrfs_drop_extents(trans
, root
, inode
,
113 start
, start
+ num_bytes
, start
, &alloc_hint
);
115 if (alloc_hint
== EXTENT_MAP_INLINE
)
118 while(num_bytes
> 0) {
119 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
120 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
121 root
->root_key
.objectid
,
123 inode
->i_ino
, start
, 0,
124 alloc_hint
, (u64
)-1, &ins
, 1);
129 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
130 start
, ins
.objectid
, ins
.offset
,
132 num_bytes
-= cur_alloc_size
;
133 alloc_hint
= ins
.objectid
+ ins
.offset
;
134 start
+= cur_alloc_size
;
137 btrfs_end_transaction(trans
, root
);
141 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
148 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
149 struct extent_buffer
*leaf
;
151 struct btrfs_path
*path
;
152 struct btrfs_file_extent_item
*item
;
155 struct btrfs_key found_key
;
157 path
= btrfs_alloc_path();
160 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
161 inode
->i_ino
, start
, 0);
163 btrfs_free_path(path
);
169 if (path
->slots
[0] == 0)
174 leaf
= path
->nodes
[0];
175 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
176 struct btrfs_file_extent_item
);
178 /* are we inside the extent that was found? */
179 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
180 found_type
= btrfs_key_type(&found_key
);
181 if (found_key
.objectid
!= inode
->i_ino
||
182 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
186 found_type
= btrfs_file_extent_type(leaf
, item
);
187 extent_start
= found_key
.offset
;
188 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
189 extent_end
= extent_start
+
190 btrfs_file_extent_num_bytes(leaf
, item
);
193 if (loops
&& start
!= extent_start
)
196 if (start
< extent_start
|| start
>= extent_end
)
199 cow_end
= min(end
, extent_end
- 1);
200 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
204 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
209 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
214 btrfs_free_path(path
);
217 btrfs_release_path(root
, path
);
222 cow_file_range(inode
, start
, cow_end
);
227 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
229 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
233 mutex_lock(&root
->fs_info
->fs_mutex
);
234 if (btrfs_test_opt(root
, NODATACOW
))
235 ret
= run_delalloc_nocow(inode
, start
, end
);
237 ret
= cow_file_range(inode
, start
, end
);
239 spin_lock(&root
->fs_info
->delalloc_lock
);
240 num_bytes
= end
+ 1 - start
;
241 if (root
->fs_info
->delalloc_bytes
< num_bytes
) {
242 printk("delalloc accounting error total %llu sub %llu\n",
243 root
->fs_info
->delalloc_bytes
, num_bytes
);
245 root
->fs_info
->delalloc_bytes
-= num_bytes
;
247 spin_unlock(&root
->fs_info
->delalloc_lock
);
249 mutex_unlock(&root
->fs_info
->fs_mutex
);
253 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
255 struct inode
*inode
= page
->mapping
->host
;
256 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
257 struct btrfs_trans_handle
*trans
;
260 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
261 size_t offset
= start
- page_start
;
263 if (btrfs_test_opt(root
, NODATASUM
))
266 mutex_lock(&root
->fs_info
->fs_mutex
);
267 trans
= btrfs_start_transaction(root
, 1);
268 btrfs_set_trans_block_group(trans
, inode
);
270 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
271 start
, kaddr
+ offset
, end
- start
+ 1);
273 ret
= btrfs_end_transaction(trans
, root
);
275 mutex_unlock(&root
->fs_info
->fs_mutex
);
279 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
282 struct inode
*inode
= page
->mapping
->host
;
283 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
284 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
285 struct btrfs_csum_item
*item
;
286 struct btrfs_path
*path
= NULL
;
289 if (btrfs_test_opt(root
, NODATASUM
))
292 mutex_lock(&root
->fs_info
->fs_mutex
);
293 path
= btrfs_alloc_path();
294 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
297 /* a csum that isn't present is a preallocated region. */
298 if (ret
== -ENOENT
|| ret
== -EFBIG
)
303 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
305 set_state_private(em_tree
, start
, csum
);
308 btrfs_free_path(path
);
309 mutex_unlock(&root
->fs_info
->fs_mutex
);
313 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
315 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
316 struct inode
*inode
= page
->mapping
->host
;
317 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
321 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
325 if (btrfs_test_opt(root
, NODATASUM
))
328 ret
= get_state_private(em_tree
, start
, &private);
329 local_irq_save(flags
);
330 kaddr
= kmap_atomic(page
, KM_IRQ0
);
334 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
335 btrfs_csum_final(csum
, (char *)&csum
);
336 if (csum
!= private) {
339 kunmap_atomic(kaddr
, KM_IRQ0
);
340 local_irq_restore(flags
);
344 printk("btrfs csum failed ino %lu off %llu\n",
345 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
346 memset(kaddr
+ offset
, 1, end
- start
+ 1);
347 flush_dcache_page(page
);
348 kunmap_atomic(kaddr
, KM_IRQ0
);
349 local_irq_restore(flags
);
353 void btrfs_read_locked_inode(struct inode
*inode
)
355 struct btrfs_path
*path
;
356 struct extent_buffer
*leaf
;
357 struct btrfs_inode_item
*inode_item
;
358 struct btrfs_inode_timespec
*tspec
;
359 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
360 struct btrfs_key location
;
361 u64 alloc_group_block
;
365 path
= btrfs_alloc_path();
367 mutex_lock(&root
->fs_info
->fs_mutex
);
369 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
370 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
374 leaf
= path
->nodes
[0];
375 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
376 struct btrfs_inode_item
);
378 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
379 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
380 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
381 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
382 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
384 tspec
= btrfs_inode_atime(inode_item
);
385 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
386 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
388 tspec
= btrfs_inode_mtime(inode_item
);
389 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
390 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
392 tspec
= btrfs_inode_ctime(inode_item
);
393 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
394 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
396 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
397 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
399 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
401 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
402 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
405 if (!BTRFS_I(inode
)->block_group
) {
406 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
409 btrfs_free_path(path
);
412 mutex_unlock(&root
->fs_info
->fs_mutex
);
414 switch (inode
->i_mode
& S_IFMT
) {
416 inode
->i_mapping
->a_ops
= &btrfs_aops
;
417 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
418 inode
->i_fop
= &btrfs_file_operations
;
419 inode
->i_op
= &btrfs_file_inode_operations
;
422 inode
->i_fop
= &btrfs_dir_file_operations
;
423 if (root
== root
->fs_info
->tree_root
)
424 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
426 inode
->i_op
= &btrfs_dir_inode_operations
;
429 inode
->i_op
= &btrfs_symlink_inode_operations
;
430 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
433 init_special_inode(inode
, inode
->i_mode
, rdev
);
439 btrfs_release_path(root
, path
);
440 btrfs_free_path(path
);
441 mutex_unlock(&root
->fs_info
->fs_mutex
);
442 make_bad_inode(inode
);
445 static void fill_inode_item(struct extent_buffer
*leaf
,
446 struct btrfs_inode_item
*item
,
449 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
450 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
451 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
452 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
453 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
455 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
456 inode
->i_atime
.tv_sec
);
457 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
458 inode
->i_atime
.tv_nsec
);
460 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
461 inode
->i_mtime
.tv_sec
);
462 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
463 inode
->i_mtime
.tv_nsec
);
465 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
466 inode
->i_ctime
.tv_sec
);
467 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
468 inode
->i_ctime
.tv_nsec
);
470 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
471 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
472 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
473 btrfs_set_inode_block_group(leaf
, item
,
474 BTRFS_I(inode
)->block_group
->key
.objectid
);
477 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
478 struct btrfs_root
*root
,
481 struct btrfs_inode_item
*inode_item
;
482 struct btrfs_path
*path
;
483 struct extent_buffer
*leaf
;
486 path
= btrfs_alloc_path();
488 ret
= btrfs_lookup_inode(trans
, root
, path
,
489 &BTRFS_I(inode
)->location
, 1);
496 leaf
= path
->nodes
[0];
497 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
498 struct btrfs_inode_item
);
500 fill_inode_item(leaf
, inode_item
, inode
);
501 btrfs_mark_buffer_dirty(leaf
);
502 btrfs_set_inode_last_trans(trans
, inode
);
505 btrfs_release_path(root
, path
);
506 btrfs_free_path(path
);
511 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
512 struct btrfs_root
*root
,
514 struct dentry
*dentry
)
516 struct btrfs_path
*path
;
517 const char *name
= dentry
->d_name
.name
;
518 int name_len
= dentry
->d_name
.len
;
520 struct extent_buffer
*leaf
;
521 struct btrfs_dir_item
*di
;
522 struct btrfs_key key
;
524 path
= btrfs_alloc_path();
530 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
540 leaf
= path
->nodes
[0];
541 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
542 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
545 btrfs_release_path(root
, path
);
547 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
548 key
.objectid
, name
, name_len
, -1);
557 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
559 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
560 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
561 dentry
->d_inode
->i_ino
,
562 dentry
->d_parent
->d_inode
->i_ino
);
564 printk("failed to delete reference to %.*s, "
565 "inode %lu parent %lu\n", name_len
, name
,
566 dentry
->d_inode
->i_ino
,
567 dentry
->d_parent
->d_inode
->i_ino
);
570 btrfs_free_path(path
);
572 dir
->i_size
-= name_len
* 2;
573 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
574 btrfs_update_inode(trans
, root
, dir
);
575 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
576 dentry
->d_inode
->i_nlink
--;
578 drop_nlink(dentry
->d_inode
);
580 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
581 dir
->i_sb
->s_dirt
= 1;
586 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
588 struct btrfs_root
*root
;
589 struct btrfs_trans_handle
*trans
;
591 unsigned long nr
= 0;
593 root
= BTRFS_I(dir
)->root
;
594 mutex_lock(&root
->fs_info
->fs_mutex
);
596 ret
= btrfs_check_free_space(root
, 1, 1);
600 trans
= btrfs_start_transaction(root
, 1);
602 btrfs_set_trans_block_group(trans
, dir
);
603 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
604 nr
= trans
->blocks_used
;
606 btrfs_end_transaction(trans
, root
);
608 mutex_unlock(&root
->fs_info
->fs_mutex
);
609 btrfs_btree_balance_dirty(root
, nr
);
613 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
615 struct inode
*inode
= dentry
->d_inode
;
618 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
619 struct btrfs_trans_handle
*trans
;
620 unsigned long nr
= 0;
622 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
625 mutex_lock(&root
->fs_info
->fs_mutex
);
626 ret
= btrfs_check_free_space(root
, 1, 1);
630 trans
= btrfs_start_transaction(root
, 1);
631 btrfs_set_trans_block_group(trans
, dir
);
633 /* now the directory is empty */
634 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
639 nr
= trans
->blocks_used
;
640 ret
= btrfs_end_transaction(trans
, root
);
642 mutex_unlock(&root
->fs_info
->fs_mutex
);
643 btrfs_btree_balance_dirty(root
, nr
);
650 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
651 struct btrfs_root
*root
,
654 struct btrfs_path
*path
;
659 path
= btrfs_alloc_path();
661 ret
= btrfs_lookup_inode(trans
, root
, path
,
662 &BTRFS_I(inode
)->location
, -1);
666 ret
= btrfs_del_item(trans
, root
, path
);
667 btrfs_free_path(path
);
672 * this can truncate away extent items, csum items and directory items.
673 * It starts at a high offset and removes keys until it can't find
674 * any higher than i_size.
676 * csum items that cross the new i_size are truncated to the new size
679 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
680 struct btrfs_root
*root
,
684 struct btrfs_path
*path
;
685 struct btrfs_key key
;
686 struct btrfs_key found_key
;
688 struct extent_buffer
*leaf
;
689 struct btrfs_file_extent_item
*fi
;
690 u64 extent_start
= 0;
691 u64 extent_num_bytes
= 0;
697 int extent_type
= -1;
699 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
700 path
= btrfs_alloc_path();
704 /* FIXME, add redo link to tree so we don't leak on crash */
705 key
.objectid
= inode
->i_ino
;
706 key
.offset
= (u64
)-1;
710 btrfs_init_path(path
);
712 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
717 BUG_ON(path
->slots
[0] == 0);
720 leaf
= path
->nodes
[0];
721 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
722 found_type
= btrfs_key_type(&found_key
);
724 if (found_key
.objectid
!= inode
->i_ino
)
727 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
728 found_type
!= BTRFS_DIR_ITEM_KEY
&&
729 found_type
!= BTRFS_DIR_INDEX_KEY
&&
730 found_type
!= BTRFS_EXTENT_DATA_KEY
)
733 item_end
= found_key
.offset
;
734 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
735 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
736 struct btrfs_file_extent_item
);
737 extent_type
= btrfs_file_extent_type(leaf
, fi
);
738 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
740 btrfs_file_extent_num_bytes(leaf
, fi
);
741 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
742 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
744 item_end
+= btrfs_file_extent_inline_len(leaf
,
749 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
750 ret
= btrfs_csum_truncate(trans
, root
, path
,
754 if (item_end
< inode
->i_size
) {
755 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
756 found_type
= BTRFS_INODE_ITEM_KEY
;
757 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
758 found_type
= BTRFS_CSUM_ITEM_KEY
;
759 } else if (found_type
) {
764 btrfs_set_key_type(&key
, found_type
);
765 btrfs_release_path(root
, path
);
768 if (found_key
.offset
>= inode
->i_size
)
774 /* FIXME, shrink the extent if the ref count is only 1 */
775 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
778 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
780 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
783 btrfs_file_extent_num_bytes(leaf
, fi
);
784 extent_num_bytes
= inode
->i_size
-
785 found_key
.offset
+ root
->sectorsize
- 1;
786 btrfs_set_file_extent_num_bytes(leaf
, fi
,
788 num_dec
= (orig_num_bytes
-
789 extent_num_bytes
) >> 9;
790 if (extent_start
!= 0) {
791 inode
->i_blocks
-= num_dec
;
793 btrfs_mark_buffer_dirty(leaf
);
796 btrfs_file_extent_disk_num_bytes(leaf
,
798 /* FIXME blocksize != 4096 */
799 num_dec
= btrfs_file_extent_num_bytes(leaf
,
801 if (extent_start
!= 0) {
803 inode
->i_blocks
-= num_dec
;
805 root_gen
= btrfs_header_generation(leaf
);
806 root_owner
= btrfs_header_owner(leaf
);
808 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
810 u32 newsize
= inode
->i_size
- found_key
.offset
;
811 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
812 ret
= btrfs_truncate_item(trans
, root
, path
,
818 ret
= btrfs_del_item(trans
, root
, path
);
824 btrfs_release_path(root
, path
);
826 ret
= btrfs_free_extent(trans
, root
, extent_start
,
829 root_gen
, inode
->i_ino
,
830 found_key
.offset
, 0);
836 btrfs_release_path(root
, path
);
837 btrfs_free_path(path
);
838 inode
->i_sb
->s_dirt
= 1;
842 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
846 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
847 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
848 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
849 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
850 u64 existing_delalloc
;
854 WARN_ON(!PageLocked(page
));
855 set_page_extent_mapped(page
);
857 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
858 delalloc_start
= page_start
;
859 existing_delalloc
= count_range_bits(&BTRFS_I(inode
)->extent_tree
,
860 &delalloc_start
, page_end
,
861 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
862 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
865 spin_lock(&root
->fs_info
->delalloc_lock
);
866 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
- existing_delalloc
;
867 spin_unlock(&root
->fs_info
->delalloc_lock
);
869 if (zero_start
!= PAGE_CACHE_SIZE
) {
871 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
872 flush_dcache_page(page
);
875 set_page_dirty(page
);
876 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
882 * taken from block_truncate_page, but does cow as it zeros out
883 * any bytes left in the last page in the file.
885 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
887 struct inode
*inode
= mapping
->host
;
888 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
889 u32 blocksize
= root
->sectorsize
;
890 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
891 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
896 if ((offset
& (blocksize
- 1)) == 0)
899 down_read(&root
->snap_sem
);
901 page
= grab_cache_page(mapping
, index
);
904 if (!PageUptodate(page
)) {
905 ret
= btrfs_readpage(NULL
, page
);
907 if (!PageUptodate(page
)) {
912 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
914 ret
= btrfs_cow_one_page(inode
, page
, offset
);
917 page_cache_release(page
);
918 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
923 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
925 struct inode
*inode
= dentry
->d_inode
;
928 err
= inode_change_ok(inode
, attr
);
932 if (S_ISREG(inode
->i_mode
) &&
933 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
934 struct btrfs_trans_handle
*trans
;
935 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
936 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
938 u64 mask
= root
->sectorsize
- 1;
939 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
940 u64 block_end
= attr
->ia_size
| mask
;
944 if (attr
->ia_size
<= pos
)
947 mutex_lock(&root
->fs_info
->fs_mutex
);
948 err
= btrfs_check_free_space(root
, 1, 0);
949 mutex_unlock(&root
->fs_info
->fs_mutex
);
953 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
955 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
956 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
958 mutex_lock(&root
->fs_info
->fs_mutex
);
959 trans
= btrfs_start_transaction(root
, 1);
960 btrfs_set_trans_block_group(trans
, inode
);
961 err
= btrfs_drop_extents(trans
, root
, inode
,
962 pos
, pos
+ hole_size
, pos
,
965 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
966 err
= btrfs_insert_file_extent(trans
, root
,
968 pos
, 0, 0, hole_size
);
970 btrfs_end_transaction(trans
, root
);
971 mutex_unlock(&root
->fs_info
->fs_mutex
);
972 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
977 err
= inode_setattr(inode
, attr
);
981 void btrfs_delete_inode(struct inode
*inode
)
983 struct btrfs_trans_handle
*trans
;
984 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
988 truncate_inode_pages(&inode
->i_data
, 0);
989 if (is_bad_inode(inode
)) {
994 mutex_lock(&root
->fs_info
->fs_mutex
);
995 trans
= btrfs_start_transaction(root
, 1);
997 btrfs_set_trans_block_group(trans
, inode
);
998 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1000 goto no_delete_lock
;
1001 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
1003 goto no_delete_lock
;
1004 ret
= btrfs_free_inode(trans
, root
, inode
);
1006 goto no_delete_lock
;
1007 nr
= trans
->blocks_used
;
1009 btrfs_end_transaction(trans
, root
);
1010 mutex_unlock(&root
->fs_info
->fs_mutex
);
1011 btrfs_btree_balance_dirty(root
, nr
);
1015 nr
= trans
->blocks_used
;
1016 btrfs_end_transaction(trans
, root
);
1017 mutex_unlock(&root
->fs_info
->fs_mutex
);
1018 btrfs_btree_balance_dirty(root
, nr
);
1024 * this returns the key found in the dir entry in the location pointer.
1025 * If no dir entries were found, location->objectid is 0.
1027 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1028 struct btrfs_key
*location
)
1030 const char *name
= dentry
->d_name
.name
;
1031 int namelen
= dentry
->d_name
.len
;
1032 struct btrfs_dir_item
*di
;
1033 struct btrfs_path
*path
;
1034 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1037 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1038 location
->objectid
= dir
->i_ino
;
1039 location
->type
= BTRFS_INODE_ITEM_KEY
;
1040 location
->offset
= 0;
1043 path
= btrfs_alloc_path();
1046 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1047 struct btrfs_key key
;
1048 struct extent_buffer
*leaf
;
1052 key
.objectid
= dir
->i_ino
;
1053 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1055 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1059 leaf
= path
->nodes
[0];
1060 slot
= path
->slots
[0];
1061 nritems
= btrfs_header_nritems(leaf
);
1062 if (slot
>= nritems
)
1065 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1066 if (key
.objectid
!= dir
->i_ino
||
1067 key
.type
!= BTRFS_INODE_REF_KEY
) {
1070 location
->objectid
= key
.offset
;
1071 location
->type
= BTRFS_INODE_ITEM_KEY
;
1072 location
->offset
= 0;
1076 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1080 if (!di
|| IS_ERR(di
)) {
1083 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1085 btrfs_free_path(path
);
1088 location
->objectid
= 0;
1093 * when we hit a tree root in a directory, the btrfs part of the inode
1094 * needs to be changed to reflect the root directory of the tree root. This
1095 * is kind of like crossing a mount point.
1097 static int fixup_tree_root_location(struct btrfs_root
*root
,
1098 struct btrfs_key
*location
,
1099 struct btrfs_root
**sub_root
,
1100 struct dentry
*dentry
)
1102 struct btrfs_path
*path
;
1103 struct btrfs_root_item
*ri
;
1105 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1107 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1110 path
= btrfs_alloc_path();
1112 mutex_lock(&root
->fs_info
->fs_mutex
);
1114 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1115 dentry
->d_name
.name
,
1116 dentry
->d_name
.len
);
1117 if (IS_ERR(*sub_root
))
1118 return PTR_ERR(*sub_root
);
1120 ri
= &(*sub_root
)->root_item
;
1121 location
->objectid
= btrfs_root_dirid(ri
);
1122 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1123 location
->offset
= 0;
1125 btrfs_free_path(path
);
1126 mutex_unlock(&root
->fs_info
->fs_mutex
);
1130 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1132 struct btrfs_iget_args
*args
= p
;
1133 inode
->i_ino
= args
->ino
;
1134 BTRFS_I(inode
)->root
= args
->root
;
1135 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1136 inode
->i_mapping
, GFP_NOFS
);
1140 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1142 struct btrfs_iget_args
*args
= opaque
;
1143 return (args
->ino
== inode
->i_ino
&&
1144 args
->root
== BTRFS_I(inode
)->root
);
1147 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1148 struct btrfs_root
*root
)
1150 struct inode
*inode
;
1151 struct btrfs_iget_args args
;
1152 args
.ino
= objectid
;
1155 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1156 btrfs_init_locked_inode
,
1161 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1162 struct nameidata
*nd
)
1164 struct inode
* inode
;
1165 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1166 struct btrfs_root
*root
= bi
->root
;
1167 struct btrfs_root
*sub_root
= root
;
1168 struct btrfs_key location
;
1171 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1172 return ERR_PTR(-ENAMETOOLONG
);
1174 mutex_lock(&root
->fs_info
->fs_mutex
);
1175 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1176 mutex_unlock(&root
->fs_info
->fs_mutex
);
1179 return ERR_PTR(ret
);
1182 if (location
.objectid
) {
1183 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1186 return ERR_PTR(ret
);
1188 return ERR_PTR(-ENOENT
);
1189 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1192 return ERR_PTR(-EACCES
);
1193 if (inode
->i_state
& I_NEW
) {
1194 /* the inode and parent dir are two different roots */
1195 if (sub_root
!= root
) {
1197 sub_root
->inode
= inode
;
1199 BTRFS_I(inode
)->root
= sub_root
;
1200 memcpy(&BTRFS_I(inode
)->location
, &location
,
1202 btrfs_read_locked_inode(inode
);
1203 unlock_new_inode(inode
);
1206 return d_splice_alias(inode
, dentry
);
1209 static unsigned char btrfs_filetype_table
[] = {
1210 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1213 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1215 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1216 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1217 struct btrfs_item
*item
;
1218 struct btrfs_dir_item
*di
;
1219 struct btrfs_key key
;
1220 struct btrfs_key found_key
;
1221 struct btrfs_path
*path
;
1224 struct extent_buffer
*leaf
;
1227 unsigned char d_type
;
1232 int key_type
= BTRFS_DIR_INDEX_KEY
;
1237 /* FIXME, use a real flag for deciding about the key type */
1238 if (root
->fs_info
->tree_root
== root
)
1239 key_type
= BTRFS_DIR_ITEM_KEY
;
1241 /* special case for "." */
1242 if (filp
->f_pos
== 0) {
1243 over
= filldir(dirent
, ".", 1,
1251 mutex_lock(&root
->fs_info
->fs_mutex
);
1252 key
.objectid
= inode
->i_ino
;
1253 path
= btrfs_alloc_path();
1256 /* special case for .., just use the back ref */
1257 if (filp
->f_pos
== 1) {
1258 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1260 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1262 leaf
= path
->nodes
[0];
1263 slot
= path
->slots
[0];
1264 nritems
= btrfs_header_nritems(leaf
);
1265 if (slot
>= nritems
) {
1266 btrfs_release_path(root
, path
);
1267 goto read_dir_items
;
1269 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1270 btrfs_release_path(root
, path
);
1271 if (found_key
.objectid
!= key
.objectid
||
1272 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1273 goto read_dir_items
;
1274 over
= filldir(dirent
, "..", 2,
1275 2, found_key
.offset
, DT_DIR
);
1282 btrfs_set_key_type(&key
, key_type
);
1283 key
.offset
= filp
->f_pos
;
1285 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1290 leaf
= path
->nodes
[0];
1291 nritems
= btrfs_header_nritems(leaf
);
1292 slot
= path
->slots
[0];
1293 if (advance
|| slot
>= nritems
) {
1294 if (slot
>= nritems
-1) {
1295 ret
= btrfs_next_leaf(root
, path
);
1298 leaf
= path
->nodes
[0];
1299 nritems
= btrfs_header_nritems(leaf
);
1300 slot
= path
->slots
[0];
1307 item
= btrfs_item_nr(leaf
, slot
);
1308 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1310 if (found_key
.objectid
!= key
.objectid
)
1312 if (btrfs_key_type(&found_key
) != key_type
)
1314 if (found_key
.offset
< filp
->f_pos
)
1317 filp
->f_pos
= found_key
.offset
;
1319 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1321 di_total
= btrfs_item_size(leaf
, item
);
1322 while(di_cur
< di_total
) {
1323 struct btrfs_key location
;
1325 name_len
= btrfs_dir_name_len(leaf
, di
);
1326 if (name_len
< 32) {
1327 name_ptr
= tmp_name
;
1329 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1332 read_extent_buffer(leaf
, name_ptr
,
1333 (unsigned long)(di
+ 1), name_len
);
1335 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1336 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1338 over
= filldir(dirent
, name_ptr
, name_len
,
1343 if (name_ptr
!= tmp_name
)
1348 di_len
= btrfs_dir_name_len(leaf
, di
) +
1349 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1351 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1358 btrfs_release_path(root
, path
);
1359 btrfs_free_path(path
);
1360 mutex_unlock(&root
->fs_info
->fs_mutex
);
1364 int btrfs_write_inode(struct inode
*inode
, int wait
)
1366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1367 struct btrfs_trans_handle
*trans
;
1371 mutex_lock(&root
->fs_info
->fs_mutex
);
1372 trans
= btrfs_start_transaction(root
, 1);
1373 btrfs_set_trans_block_group(trans
, inode
);
1374 ret
= btrfs_commit_transaction(trans
, root
);
1375 mutex_unlock(&root
->fs_info
->fs_mutex
);
1381 * This is somewhat expensive, updating the tree every time the
1382 * inode changes. But, it is most likely to find the inode in cache.
1383 * FIXME, needs more benchmarking...there are no reasons other than performance
1384 * to keep or drop this code.
1386 void btrfs_dirty_inode(struct inode
*inode
)
1388 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1389 struct btrfs_trans_handle
*trans
;
1391 mutex_lock(&root
->fs_info
->fs_mutex
);
1392 trans
= btrfs_start_transaction(root
, 1);
1393 btrfs_set_trans_block_group(trans
, inode
);
1394 btrfs_update_inode(trans
, root
, inode
);
1395 btrfs_end_transaction(trans
, root
);
1396 mutex_unlock(&root
->fs_info
->fs_mutex
);
1399 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1400 struct btrfs_root
*root
,
1402 struct btrfs_block_group_cache
*group
,
1405 struct inode
*inode
;
1406 struct btrfs_inode_item
*inode_item
;
1407 struct btrfs_key
*location
;
1408 struct btrfs_path
*path
;
1412 path
= btrfs_alloc_path();
1415 inode
= new_inode(root
->fs_info
->sb
);
1417 return ERR_PTR(-ENOMEM
);
1419 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1420 inode
->i_mapping
, GFP_NOFS
);
1421 BTRFS_I(inode
)->root
= root
;
1427 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1428 BTRFS_I(inode
)->block_group
= group
;
1430 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1434 inode
->i_uid
= current
->fsuid
;
1435 inode
->i_gid
= current
->fsgid
;
1436 inode
->i_mode
= mode
;
1437 inode
->i_ino
= objectid
;
1438 inode
->i_blocks
= 0;
1439 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1440 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1441 struct btrfs_inode_item
);
1442 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1443 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1444 btrfs_free_path(path
);
1446 location
= &BTRFS_I(inode
)->location
;
1447 location
->objectid
= objectid
;
1448 location
->offset
= 0;
1449 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1451 insert_inode_hash(inode
);
1454 btrfs_free_path(path
);
1455 return ERR_PTR(ret
);
1458 static inline u8
btrfs_inode_type(struct inode
*inode
)
1460 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1463 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1464 struct dentry
*dentry
, struct inode
*inode
)
1467 struct btrfs_key key
;
1468 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1469 struct inode
*parent_inode
;
1471 key
.objectid
= inode
->i_ino
;
1472 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1475 ret
= btrfs_insert_dir_item(trans
, root
,
1476 dentry
->d_name
.name
, dentry
->d_name
.len
,
1477 dentry
->d_parent
->d_inode
->i_ino
,
1478 &key
, btrfs_inode_type(inode
));
1480 ret
= btrfs_insert_inode_ref(trans
, root
,
1481 dentry
->d_name
.name
,
1484 dentry
->d_parent
->d_inode
->i_ino
);
1485 parent_inode
= dentry
->d_parent
->d_inode
;
1486 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1487 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1488 ret
= btrfs_update_inode(trans
, root
,
1489 dentry
->d_parent
->d_inode
);
1494 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1495 struct dentry
*dentry
, struct inode
*inode
)
1497 int err
= btrfs_add_link(trans
, dentry
, inode
);
1499 d_instantiate(dentry
, inode
);
1507 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1508 int mode
, dev_t rdev
)
1510 struct btrfs_trans_handle
*trans
;
1511 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1512 struct inode
*inode
= NULL
;
1516 unsigned long nr
= 0;
1518 if (!new_valid_dev(rdev
))
1521 mutex_lock(&root
->fs_info
->fs_mutex
);
1522 err
= btrfs_check_free_space(root
, 1, 0);
1526 trans
= btrfs_start_transaction(root
, 1);
1527 btrfs_set_trans_block_group(trans
, dir
);
1529 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1535 inode
= btrfs_new_inode(trans
, root
, objectid
,
1536 BTRFS_I(dir
)->block_group
, mode
);
1537 err
= PTR_ERR(inode
);
1541 btrfs_set_trans_block_group(trans
, inode
);
1542 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1546 inode
->i_op
= &btrfs_special_inode_operations
;
1547 init_special_inode(inode
, inode
->i_mode
, rdev
);
1548 btrfs_update_inode(trans
, root
, inode
);
1550 dir
->i_sb
->s_dirt
= 1;
1551 btrfs_update_inode_block_group(trans
, inode
);
1552 btrfs_update_inode_block_group(trans
, dir
);
1554 nr
= trans
->blocks_used
;
1555 btrfs_end_transaction(trans
, root
);
1557 mutex_unlock(&root
->fs_info
->fs_mutex
);
1560 inode_dec_link_count(inode
);
1563 btrfs_btree_balance_dirty(root
, nr
);
1567 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1568 int mode
, struct nameidata
*nd
)
1570 struct btrfs_trans_handle
*trans
;
1571 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1572 struct inode
*inode
= NULL
;
1575 unsigned long nr
= 0;
1578 mutex_lock(&root
->fs_info
->fs_mutex
);
1579 err
= btrfs_check_free_space(root
, 1, 0);
1582 trans
= btrfs_start_transaction(root
, 1);
1583 btrfs_set_trans_block_group(trans
, dir
);
1585 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1591 inode
= btrfs_new_inode(trans
, root
, objectid
,
1592 BTRFS_I(dir
)->block_group
, mode
);
1593 err
= PTR_ERR(inode
);
1597 btrfs_set_trans_block_group(trans
, inode
);
1598 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1602 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1603 inode
->i_fop
= &btrfs_file_operations
;
1604 inode
->i_op
= &btrfs_file_inode_operations
;
1605 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1606 inode
->i_mapping
, GFP_NOFS
);
1607 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1609 dir
->i_sb
->s_dirt
= 1;
1610 btrfs_update_inode_block_group(trans
, inode
);
1611 btrfs_update_inode_block_group(trans
, dir
);
1613 nr
= trans
->blocks_used
;
1614 btrfs_end_transaction(trans
, root
);
1616 mutex_unlock(&root
->fs_info
->fs_mutex
);
1619 inode_dec_link_count(inode
);
1622 btrfs_btree_balance_dirty(root
, nr
);
1626 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1627 struct dentry
*dentry
)
1629 struct btrfs_trans_handle
*trans
;
1630 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1631 struct inode
*inode
= old_dentry
->d_inode
;
1632 unsigned long nr
= 0;
1636 if (inode
->i_nlink
== 0)
1639 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1644 mutex_lock(&root
->fs_info
->fs_mutex
);
1645 err
= btrfs_check_free_space(root
, 1, 0);
1648 trans
= btrfs_start_transaction(root
, 1);
1650 btrfs_set_trans_block_group(trans
, dir
);
1651 atomic_inc(&inode
->i_count
);
1652 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1657 dir
->i_sb
->s_dirt
= 1;
1658 btrfs_update_inode_block_group(trans
, dir
);
1659 err
= btrfs_update_inode(trans
, root
, inode
);
1664 nr
= trans
->blocks_used
;
1665 btrfs_end_transaction(trans
, root
);
1667 mutex_unlock(&root
->fs_info
->fs_mutex
);
1670 inode_dec_link_count(inode
);
1673 btrfs_btree_balance_dirty(root
, nr
);
1677 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1679 struct inode
*inode
;
1680 struct btrfs_trans_handle
*trans
;
1681 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1683 int drop_on_err
= 0;
1685 unsigned long nr
= 1;
1687 mutex_lock(&root
->fs_info
->fs_mutex
);
1688 err
= btrfs_check_free_space(root
, 1, 0);
1692 trans
= btrfs_start_transaction(root
, 1);
1693 btrfs_set_trans_block_group(trans
, dir
);
1695 if (IS_ERR(trans
)) {
1696 err
= PTR_ERR(trans
);
1700 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1706 inode
= btrfs_new_inode(trans
, root
, objectid
,
1707 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1708 if (IS_ERR(inode
)) {
1709 err
= PTR_ERR(inode
);
1714 inode
->i_op
= &btrfs_dir_inode_operations
;
1715 inode
->i_fop
= &btrfs_dir_file_operations
;
1716 btrfs_set_trans_block_group(trans
, inode
);
1719 err
= btrfs_update_inode(trans
, root
, inode
);
1723 err
= btrfs_add_link(trans
, dentry
, inode
);
1727 d_instantiate(dentry
, inode
);
1729 dir
->i_sb
->s_dirt
= 1;
1730 btrfs_update_inode_block_group(trans
, inode
);
1731 btrfs_update_inode_block_group(trans
, dir
);
1734 nr
= trans
->blocks_used
;
1735 btrfs_end_transaction(trans
, root
);
1738 mutex_unlock(&root
->fs_info
->fs_mutex
);
1741 btrfs_btree_balance_dirty(root
, nr
);
1745 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1746 size_t page_offset
, u64 start
, u64 end
,
1752 u64 extent_start
= 0;
1754 u64 objectid
= inode
->i_ino
;
1756 int failed_insert
= 0;
1757 struct btrfs_path
*path
;
1758 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1759 struct btrfs_file_extent_item
*item
;
1760 struct extent_buffer
*leaf
;
1761 struct btrfs_key found_key
;
1762 struct extent_map
*em
= NULL
;
1763 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1764 struct btrfs_trans_handle
*trans
= NULL
;
1766 path
= btrfs_alloc_path();
1768 mutex_lock(&root
->fs_info
->fs_mutex
);
1771 em
= lookup_extent_mapping(em_tree
, start
, end
);
1776 em
= alloc_extent_map(GFP_NOFS
);
1781 em
->start
= EXTENT_MAP_HOLE
;
1782 em
->end
= EXTENT_MAP_HOLE
;
1784 em
->bdev
= inode
->i_sb
->s_bdev
;
1785 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1786 objectid
, start
, trans
!= NULL
);
1793 if (path
->slots
[0] == 0)
1798 leaf
= path
->nodes
[0];
1799 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1800 struct btrfs_file_extent_item
);
1801 /* are we inside the extent that was found? */
1802 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1803 found_type
= btrfs_key_type(&found_key
);
1804 if (found_key
.objectid
!= objectid
||
1805 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1809 found_type
= btrfs_file_extent_type(leaf
, item
);
1810 extent_start
= found_key
.offset
;
1811 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1812 extent_end
= extent_start
+
1813 btrfs_file_extent_num_bytes(leaf
, item
);
1815 if (start
< extent_start
|| start
>= extent_end
) {
1817 if (start
< extent_start
) {
1818 if (end
< extent_start
)
1820 em
->end
= extent_end
- 1;
1826 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1828 em
->start
= extent_start
;
1829 em
->end
= extent_end
- 1;
1830 em
->block_start
= EXTENT_MAP_HOLE
;
1831 em
->block_end
= EXTENT_MAP_HOLE
;
1834 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1835 em
->block_start
= bytenr
;
1836 em
->block_end
= em
->block_start
+
1837 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1838 em
->start
= extent_start
;
1839 em
->end
= extent_end
- 1;
1841 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1845 size_t extent_offset
;
1848 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1850 extent_end
= (extent_start
+ size
- 1) |
1851 ((u64
)root
->sectorsize
- 1);
1852 if (start
< extent_start
|| start
>= extent_end
) {
1854 if (start
< extent_start
) {
1855 if (end
< extent_start
)
1857 em
->end
= extent_end
;
1863 em
->block_start
= EXTENT_MAP_INLINE
;
1864 em
->block_end
= EXTENT_MAP_INLINE
;
1867 em
->start
= extent_start
;
1868 em
->end
= extent_start
+ size
- 1;
1872 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1873 extent_start
+ page_offset
;
1874 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1875 size
- extent_offset
);
1876 em
->start
= extent_start
+ extent_offset
;
1877 em
->end
= (em
->start
+ copy_size
-1) |
1878 ((u64
)root
->sectorsize
-1);
1880 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1881 if (create
== 0 && !PageUptodate(page
)) {
1882 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1884 flush_dcache_page(page
);
1885 } else if (create
&& PageUptodate(page
)) {
1888 free_extent_map(em
);
1890 btrfs_release_path(root
, path
);
1891 trans
= btrfs_start_transaction(root
, 1);
1894 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1896 btrfs_mark_buffer_dirty(leaf
);
1899 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1902 printk("unkknown found_type %d\n", found_type
);
1909 em
->block_start
= EXTENT_MAP_HOLE
;
1910 em
->block_end
= EXTENT_MAP_HOLE
;
1912 btrfs_release_path(root
, path
);
1913 if (em
->start
> start
|| em
->end
< start
) {
1914 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1918 ret
= add_extent_mapping(em_tree
, em
);
1919 if (ret
== -EEXIST
) {
1920 free_extent_map(em
);
1922 if (0 && failed_insert
== 1) {
1923 btrfs_drop_extent_cache(inode
, start
, end
);
1926 if (failed_insert
> 5) {
1927 printk("failing to insert %Lu %Lu\n", start
, end
);
1935 btrfs_free_path(path
);
1937 ret
= btrfs_end_transaction(trans
, root
);
1941 mutex_unlock(&root
->fs_info
->fs_mutex
);
1943 free_extent_map(em
);
1945 return ERR_PTR(err
);
1950 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1952 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1955 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1956 unsigned from
, unsigned to
)
1958 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1961 mutex_lock(&root
->fs_info
->fs_mutex
);
1962 err
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
1963 mutex_unlock(&root
->fs_info
->fs_mutex
);
1967 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1968 page
->mapping
->host
, page
, from
, to
,
1972 int btrfs_readpage(struct file
*file
, struct page
*page
)
1974 struct extent_map_tree
*tree
;
1975 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1976 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1979 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1981 struct extent_map_tree
*tree
;
1984 if (current
->flags
& PF_MEMALLOC
) {
1985 redirty_page_for_writepage(wbc
, page
);
1989 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1990 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1993 static int btrfs_writepages(struct address_space
*mapping
,
1994 struct writeback_control
*wbc
)
1996 struct extent_map_tree
*tree
;
1997 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1998 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2002 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2003 struct list_head
*pages
, unsigned nr_pages
)
2005 struct extent_map_tree
*tree
;
2006 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
2007 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2011 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
2013 struct extent_map_tree
*tree
;
2016 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2017 ret
= try_release_extent_mapping(tree
, page
);
2019 ClearPagePrivate(page
);
2020 set_page_private(page
, 0);
2021 page_cache_release(page
);
2026 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2028 struct extent_map_tree
*tree
;
2030 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2031 extent_invalidatepage(tree
, page
, offset
);
2032 btrfs_releasepage(page
, GFP_NOFS
);
2036 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2037 * called from a page fault handler when a page is first dirtied. Hence we must
2038 * be careful to check for EOF conditions here. We set the page up correctly
2039 * for a written page which means we get ENOSPC checking when writing into
2040 * holes and correct delalloc and unwritten extent mapping on filesystems that
2041 * support these features.
2043 * We are not allowed to take the i_mutex here so we have to play games to
2044 * protect against truncate races as the page could now be beyond EOF. Because
2045 * vmtruncate() writes the inode size before removing pages, once we have the
2046 * page lock we can determine safely if the page is beyond EOF. If it is not
2047 * beyond EOF, then the page is guaranteed safe against truncation until we
2050 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2052 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2053 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2059 mutex_lock(&root
->fs_info
->fs_mutex
);
2060 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2061 mutex_unlock(&root
->fs_info
->fs_mutex
);
2067 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
2069 wait_on_page_writeback(page
);
2070 size
= i_size_read(inode
);
2071 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2073 if ((page
->mapping
!= inode
->i_mapping
) ||
2074 (page_start
> size
)) {
2075 /* page got truncated out from underneath us */
2079 /* page is wholly or partially inside EOF */
2080 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2081 end
= size
& ~PAGE_CACHE_MASK
;
2083 end
= PAGE_CACHE_SIZE
;
2085 ret
= btrfs_cow_one_page(inode
, page
, end
);
2088 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
2094 static void btrfs_truncate(struct inode
*inode
)
2096 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2098 struct btrfs_trans_handle
*trans
;
2101 if (!S_ISREG(inode
->i_mode
))
2103 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2106 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2108 mutex_lock(&root
->fs_info
->fs_mutex
);
2109 trans
= btrfs_start_transaction(root
, 1);
2110 btrfs_set_trans_block_group(trans
, inode
);
2112 /* FIXME, add redo link to tree so we don't leak on crash */
2113 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
2114 btrfs_update_inode(trans
, root
, inode
);
2115 nr
= trans
->blocks_used
;
2117 ret
= btrfs_end_transaction(trans
, root
);
2119 mutex_unlock(&root
->fs_info
->fs_mutex
);
2120 btrfs_btree_balance_dirty(root
, nr
);
2123 int btrfs_commit_write(struct file
*file
, struct page
*page
,
2124 unsigned from
, unsigned to
)
2126 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2127 struct inode
*inode
= page
->mapping
->host
;
2129 btrfs_cow_one_page(inode
, page
, PAGE_CACHE_SIZE
);
2131 if (pos
> inode
->i_size
) {
2132 i_size_write(inode
, pos
);
2133 mark_inode_dirty(inode
);
2138 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
2140 struct btrfs_trans_handle
*trans
;
2141 struct btrfs_key key
;
2142 struct btrfs_root_item root_item
;
2143 struct btrfs_inode_item
*inode_item
;
2144 struct extent_buffer
*leaf
;
2145 struct btrfs_root
*new_root
;
2146 struct inode
*inode
;
2151 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2152 unsigned long nr
= 1;
2154 mutex_lock(&root
->fs_info
->fs_mutex
);
2155 ret
= btrfs_check_free_space(root
, 1, 0);
2159 trans
= btrfs_start_transaction(root
, 1);
2162 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2167 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2168 objectid
, trans
->transid
, 0, 0,
2171 return PTR_ERR(leaf
);
2173 btrfs_set_header_nritems(leaf
, 0);
2174 btrfs_set_header_level(leaf
, 0);
2175 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2176 btrfs_set_header_generation(leaf
, trans
->transid
);
2177 btrfs_set_header_owner(leaf
, objectid
);
2179 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2180 (unsigned long)btrfs_header_fsid(leaf
),
2182 btrfs_mark_buffer_dirty(leaf
);
2184 inode_item
= &root_item
.inode
;
2185 memset(inode_item
, 0, sizeof(*inode_item
));
2186 inode_item
->generation
= cpu_to_le64(1);
2187 inode_item
->size
= cpu_to_le64(3);
2188 inode_item
->nlink
= cpu_to_le32(1);
2189 inode_item
->nblocks
= cpu_to_le64(1);
2190 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2192 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2193 btrfs_set_root_level(&root_item
, 0);
2194 btrfs_set_root_refs(&root_item
, 1);
2195 btrfs_set_root_used(&root_item
, 0);
2197 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2198 root_item
.drop_level
= 0;
2200 free_extent_buffer(leaf
);
2203 btrfs_set_root_dirid(&root_item
, new_dirid
);
2205 key
.objectid
= objectid
;
2207 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2208 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2214 * insert the directory item
2216 key
.offset
= (u64
)-1;
2217 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2218 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2219 name
, namelen
, dir
->i_ino
, &key
,
2224 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2225 name
, namelen
, objectid
,
2226 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2230 ret
= btrfs_commit_transaction(trans
, root
);
2234 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2237 trans
= btrfs_start_transaction(new_root
, 1);
2240 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2241 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2244 inode
->i_op
= &btrfs_dir_inode_operations
;
2245 inode
->i_fop
= &btrfs_dir_file_operations
;
2246 new_root
->inode
= inode
;
2248 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2252 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2256 nr
= trans
->blocks_used
;
2257 err
= btrfs_commit_transaction(trans
, root
);
2261 mutex_unlock(&root
->fs_info
->fs_mutex
);
2262 btrfs_btree_balance_dirty(root
, nr
);
2266 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2268 struct btrfs_trans_handle
*trans
;
2269 struct btrfs_key key
;
2270 struct btrfs_root_item new_root_item
;
2271 struct extent_buffer
*tmp
;
2275 unsigned long nr
= 0;
2277 if (!root
->ref_cows
)
2280 down_write(&root
->snap_sem
);
2281 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2282 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2284 mutex_lock(&root
->fs_info
->fs_mutex
);
2285 ret
= btrfs_check_free_space(root
, 1, 0);
2289 trans
= btrfs_start_transaction(root
, 1);
2292 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2296 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2301 memcpy(&new_root_item
, &root
->root_item
,
2302 sizeof(new_root_item
));
2304 key
.objectid
= objectid
;
2306 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2308 extent_buffer_get(root
->node
);
2309 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2310 free_extent_buffer(tmp
);
2312 btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
2314 btrfs_set_root_bytenr(&new_root_item
, tmp
->start
);
2315 btrfs_set_root_level(&new_root_item
, btrfs_header_level(tmp
));
2316 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2318 free_extent_buffer(tmp
);
2323 * insert the directory item
2325 key
.offset
= (u64
)-1;
2326 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2328 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2329 &key
, BTRFS_FT_DIR
);
2334 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2335 name
, namelen
, objectid
,
2336 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2341 nr
= trans
->blocks_used
;
2342 err
= btrfs_commit_transaction(trans
, root
);
2347 mutex_unlock(&root
->fs_info
->fs_mutex
);
2348 up_write(&root
->snap_sem
);
2349 btrfs_btree_balance_dirty(root
, nr
);
2353 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2354 struct file_ra_state
*ra
, struct file
*file
,
2355 pgoff_t offset
, pgoff_t last_index
)
2359 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2360 req_size
= last_index
- offset
+ 1;
2361 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2364 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2365 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2366 return offset
+ req_size
;
2370 int btrfs_defrag_file(struct file
*file
) {
2371 struct inode
*inode
= fdentry(file
)->d_inode
;
2372 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2373 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2375 unsigned long last_index
;
2376 unsigned long ra_index
= 0;
2380 u64 existing_delalloc
;
2384 mutex_lock(&root
->fs_info
->fs_mutex
);
2385 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2386 mutex_unlock(&root
->fs_info
->fs_mutex
);
2390 mutex_lock(&inode
->i_mutex
);
2391 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2392 for (i
= 0; i
<= last_index
; i
++) {
2393 if (i
== ra_index
) {
2394 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2396 file
, ra_index
, last_index
);
2398 page
= grab_cache_page(inode
->i_mapping
, i
);
2401 if (!PageUptodate(page
)) {
2402 btrfs_readpage(NULL
, page
);
2404 if (!PageUptodate(page
)) {
2406 page_cache_release(page
);
2410 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2411 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2413 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2414 delalloc_start
= page_start
;
2416 count_range_bits(&BTRFS_I(inode
)->extent_tree
,
2417 &delalloc_start
, page_end
,
2418 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
2419 set_extent_delalloc(em_tree
, page_start
,
2420 page_end
, GFP_NOFS
);
2422 spin_lock(&root
->fs_info
->delalloc_lock
);
2423 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
-
2425 spin_unlock(&root
->fs_info
->delalloc_lock
);
2427 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2428 set_page_dirty(page
);
2430 page_cache_release(page
);
2431 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2435 mutex_unlock(&inode
->i_mutex
);
2439 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2443 struct btrfs_ioctl_vol_args
*vol_args
;
2444 struct btrfs_trans_handle
*trans
;
2450 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2455 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2459 namelen
= strlen(vol_args
->name
);
2460 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2465 sizestr
= vol_args
->name
;
2466 if (!strcmp(sizestr
, "max"))
2467 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2469 if (sizestr
[0] == '-') {
2472 } else if (sizestr
[0] == '+') {
2476 new_size
= btrfs_parse_size(sizestr
);
2477 if (new_size
== 0) {
2483 mutex_lock(&root
->fs_info
->fs_mutex
);
2484 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2487 if (new_size
> old_size
) {
2491 new_size
= old_size
- new_size
;
2492 } else if (mod
> 0) {
2493 new_size
= old_size
+ new_size
;
2496 if (new_size
< 256 * 1024 * 1024) {
2500 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2504 new_size
= (new_size
/ root
->sectorsize
) * root
->sectorsize
;
2506 printk("new size is %Lu\n", new_size
);
2507 if (new_size
> old_size
) {
2508 trans
= btrfs_start_transaction(root
, 1);
2509 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2510 btrfs_commit_transaction(trans
, root
);
2512 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2516 mutex_unlock(&root
->fs_info
->fs_mutex
);
2522 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2524 struct btrfs_ioctl_vol_args
*vol_args
;
2525 struct btrfs_dir_item
*di
;
2526 struct btrfs_path
*path
;
2531 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2536 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2541 namelen
= strlen(vol_args
->name
);
2542 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2546 if (strchr(vol_args
->name
, '/')) {
2551 path
= btrfs_alloc_path();
2557 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2558 mutex_lock(&root
->fs_info
->fs_mutex
);
2559 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2561 vol_args
->name
, namelen
, 0);
2562 mutex_unlock(&root
->fs_info
->fs_mutex
);
2563 btrfs_free_path(path
);
2565 if (di
&& !IS_ERR(di
)) {
2575 if (root
== root
->fs_info
->tree_root
)
2576 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2578 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2584 static int btrfs_ioctl_defrag(struct file
*file
)
2586 struct inode
*inode
= fdentry(file
)->d_inode
;
2587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2589 switch (inode
->i_mode
& S_IFMT
) {
2591 mutex_lock(&root
->fs_info
->fs_mutex
);
2592 btrfs_defrag_root(root
, 0);
2593 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2594 mutex_unlock(&root
->fs_info
->fs_mutex
);
2597 btrfs_defrag_file(file
);
2604 long btrfs_ioctl(struct file
*file
, unsigned int
2605 cmd
, unsigned long arg
)
2607 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2610 case BTRFS_IOC_SNAP_CREATE
:
2611 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2612 case BTRFS_IOC_DEFRAG
:
2613 return btrfs_ioctl_defrag(file
);
2614 case BTRFS_IOC_RESIZE
:
2615 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2622 * Called inside transaction, so use GFP_NOFS
2624 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2626 struct btrfs_inode
*ei
;
2628 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2632 return &ei
->vfs_inode
;
2635 void btrfs_destroy_inode(struct inode
*inode
)
2637 WARN_ON(!list_empty(&inode
->i_dentry
));
2638 WARN_ON(inode
->i_data
.nrpages
);
2640 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2643 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2644 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2646 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2647 unsigned long flags
)
2650 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2652 inode_init_once(&ei
->vfs_inode
);
2655 void btrfs_destroy_cachep(void)
2657 if (btrfs_inode_cachep
)
2658 kmem_cache_destroy(btrfs_inode_cachep
);
2659 if (btrfs_trans_handle_cachep
)
2660 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2661 if (btrfs_transaction_cachep
)
2662 kmem_cache_destroy(btrfs_transaction_cachep
);
2663 if (btrfs_bit_radix_cachep
)
2664 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2665 if (btrfs_path_cachep
)
2666 kmem_cache_destroy(btrfs_path_cachep
);
2669 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2670 unsigned long extra_flags
,
2671 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2672 void (*ctor
)(struct kmem_cache
*, void *)
2674 void (*ctor
)(void *, struct kmem_cache
*,
2679 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2680 SLAB_MEM_SPREAD
| extra_flags
), ctor
2681 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2687 int btrfs_init_cachep(void)
2689 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2690 sizeof(struct btrfs_inode
),
2692 if (!btrfs_inode_cachep
)
2694 btrfs_trans_handle_cachep
=
2695 btrfs_cache_create("btrfs_trans_handle_cache",
2696 sizeof(struct btrfs_trans_handle
),
2698 if (!btrfs_trans_handle_cachep
)
2700 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2701 sizeof(struct btrfs_transaction
),
2703 if (!btrfs_transaction_cachep
)
2705 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2706 sizeof(struct btrfs_path
),
2708 if (!btrfs_path_cachep
)
2710 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2711 SLAB_DESTROY_BY_RCU
, NULL
);
2712 if (!btrfs_bit_radix_cachep
)
2716 btrfs_destroy_cachep();
2720 static int btrfs_getattr(struct vfsmount
*mnt
,
2721 struct dentry
*dentry
, struct kstat
*stat
)
2723 struct inode
*inode
= dentry
->d_inode
;
2724 generic_fillattr(inode
, stat
);
2725 stat
->blksize
= 256 * 1024;
2729 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2730 struct inode
* new_dir
,struct dentry
*new_dentry
)
2732 struct btrfs_trans_handle
*trans
;
2733 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2734 struct inode
*new_inode
= new_dentry
->d_inode
;
2735 struct inode
*old_inode
= old_dentry
->d_inode
;
2736 struct timespec ctime
= CURRENT_TIME
;
2737 struct btrfs_path
*path
;
2740 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2741 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2745 mutex_lock(&root
->fs_info
->fs_mutex
);
2746 ret
= btrfs_check_free_space(root
, 1, 0);
2750 trans
= btrfs_start_transaction(root
, 1);
2752 btrfs_set_trans_block_group(trans
, new_dir
);
2753 path
= btrfs_alloc_path();
2759 old_dentry
->d_inode
->i_nlink
++;
2760 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2761 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2762 old_inode
->i_ctime
= ctime
;
2764 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2769 new_inode
->i_ctime
= CURRENT_TIME
;
2770 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2774 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2779 btrfs_free_path(path
);
2780 btrfs_end_transaction(trans
, root
);
2782 mutex_unlock(&root
->fs_info
->fs_mutex
);
2786 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2787 const char *symname
)
2789 struct btrfs_trans_handle
*trans
;
2790 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2791 struct btrfs_path
*path
;
2792 struct btrfs_key key
;
2793 struct inode
*inode
= NULL
;
2800 struct btrfs_file_extent_item
*ei
;
2801 struct extent_buffer
*leaf
;
2802 unsigned long nr
= 0;
2804 name_len
= strlen(symname
) + 1;
2805 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2806 return -ENAMETOOLONG
;
2808 mutex_lock(&root
->fs_info
->fs_mutex
);
2809 err
= btrfs_check_free_space(root
, 1, 0);
2813 trans
= btrfs_start_transaction(root
, 1);
2814 btrfs_set_trans_block_group(trans
, dir
);
2816 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2822 inode
= btrfs_new_inode(trans
, root
, objectid
,
2823 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2824 err
= PTR_ERR(inode
);
2828 btrfs_set_trans_block_group(trans
, inode
);
2829 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2833 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2834 inode
->i_fop
= &btrfs_file_operations
;
2835 inode
->i_op
= &btrfs_file_inode_operations
;
2836 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2837 inode
->i_mapping
, GFP_NOFS
);
2838 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2840 dir
->i_sb
->s_dirt
= 1;
2841 btrfs_update_inode_block_group(trans
, inode
);
2842 btrfs_update_inode_block_group(trans
, dir
);
2846 path
= btrfs_alloc_path();
2848 key
.objectid
= inode
->i_ino
;
2850 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2851 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2852 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2858 leaf
= path
->nodes
[0];
2859 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2860 struct btrfs_file_extent_item
);
2861 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2862 btrfs_set_file_extent_type(leaf
, ei
,
2863 BTRFS_FILE_EXTENT_INLINE
);
2864 ptr
= btrfs_file_extent_inline_start(ei
);
2865 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2866 btrfs_mark_buffer_dirty(leaf
);
2867 btrfs_free_path(path
);
2869 inode
->i_op
= &btrfs_symlink_inode_operations
;
2870 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2871 inode
->i_size
= name_len
- 1;
2872 err
= btrfs_update_inode(trans
, root
, inode
);
2877 nr
= trans
->blocks_used
;
2878 btrfs_end_transaction(trans
, root
);
2880 mutex_unlock(&root
->fs_info
->fs_mutex
);
2882 inode_dec_link_count(inode
);
2885 btrfs_btree_balance_dirty(root
, nr
);
2889 static struct inode_operations btrfs_dir_inode_operations
= {
2890 .lookup
= btrfs_lookup
,
2891 .create
= btrfs_create
,
2892 .unlink
= btrfs_unlink
,
2894 .mkdir
= btrfs_mkdir
,
2895 .rmdir
= btrfs_rmdir
,
2896 .rename
= btrfs_rename
,
2897 .symlink
= btrfs_symlink
,
2898 .setattr
= btrfs_setattr
,
2899 .mknod
= btrfs_mknod
,
2900 .setxattr
= generic_setxattr
,
2901 .getxattr
= generic_getxattr
,
2902 .listxattr
= btrfs_listxattr
,
2903 .removexattr
= generic_removexattr
,
2906 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2907 .lookup
= btrfs_lookup
,
2910 static struct file_operations btrfs_dir_file_operations
= {
2911 .llseek
= generic_file_llseek
,
2912 .read
= generic_read_dir
,
2913 .readdir
= btrfs_readdir
,
2914 .unlocked_ioctl
= btrfs_ioctl
,
2915 #ifdef CONFIG_COMPAT
2916 .compat_ioctl
= btrfs_ioctl
,
2920 static struct extent_map_ops btrfs_extent_map_ops
= {
2921 .fill_delalloc
= run_delalloc_range
,
2922 .writepage_io_hook
= btrfs_writepage_io_hook
,
2923 .readpage_io_hook
= btrfs_readpage_io_hook
,
2924 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2927 static struct address_space_operations btrfs_aops
= {
2928 .readpage
= btrfs_readpage
,
2929 .writepage
= btrfs_writepage
,
2930 .writepages
= btrfs_writepages
,
2931 .readpages
= btrfs_readpages
,
2932 .sync_page
= block_sync_page
,
2933 .prepare_write
= btrfs_prepare_write
,
2934 .commit_write
= btrfs_commit_write
,
2936 .invalidatepage
= btrfs_invalidatepage
,
2937 .releasepage
= btrfs_releasepage
,
2938 .set_page_dirty
= __set_page_dirty_nobuffers
,
2941 static struct address_space_operations btrfs_symlink_aops
= {
2942 .readpage
= btrfs_readpage
,
2943 .writepage
= btrfs_writepage
,
2944 .invalidatepage
= btrfs_invalidatepage
,
2945 .releasepage
= btrfs_releasepage
,
2948 static struct inode_operations btrfs_file_inode_operations
= {
2949 .truncate
= btrfs_truncate
,
2950 .getattr
= btrfs_getattr
,
2951 .setattr
= btrfs_setattr
,
2952 .setxattr
= generic_setxattr
,
2953 .getxattr
= generic_getxattr
,
2954 .listxattr
= btrfs_listxattr
,
2955 .removexattr
= generic_removexattr
,
2958 static struct inode_operations btrfs_special_inode_operations
= {
2959 .getattr
= btrfs_getattr
,
2960 .setattr
= btrfs_setattr
,
2963 static struct inode_operations btrfs_symlink_inode_operations
= {
2964 .readlink
= generic_readlink
,
2965 .follow_link
= page_follow_link_light
,
2966 .put_link
= page_put_link
,