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
);
90 spin_lock(&root
->fs_info
->delalloc_lock
);
91 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
93 spin_unlock(&root
->fs_info
->delalloc_lock
);
97 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
99 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
100 struct btrfs_trans_handle
*trans
;
104 u64 blocksize
= root
->sectorsize
;
105 struct btrfs_key ins
;
108 trans
= btrfs_start_transaction(root
, 1);
110 btrfs_set_trans_block_group(trans
, inode
);
112 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
113 num_bytes
= max(blocksize
, num_bytes
);
114 ret
= btrfs_drop_extents(trans
, root
, inode
,
115 start
, start
+ num_bytes
, start
, &alloc_hint
);
117 if (alloc_hint
== EXTENT_MAP_INLINE
)
120 while(num_bytes
> 0) {
121 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
122 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
123 root
->root_key
.objectid
,
125 inode
->i_ino
, start
, 0,
126 alloc_hint
, (u64
)-1, &ins
, 1);
131 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
132 start
, ins
.objectid
, ins
.offset
,
134 num_bytes
-= cur_alloc_size
;
135 alloc_hint
= ins
.objectid
+ ins
.offset
;
136 start
+= cur_alloc_size
;
138 btrfs_add_ordered_inode(inode
);
140 btrfs_end_transaction(trans
, root
);
144 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
152 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
153 struct extent_buffer
*leaf
;
155 struct btrfs_path
*path
;
156 struct btrfs_file_extent_item
*item
;
159 struct btrfs_key found_key
;
161 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
162 path
= btrfs_alloc_path();
165 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
166 inode
->i_ino
, start
, 0);
168 btrfs_free_path(path
);
174 if (path
->slots
[0] == 0)
179 leaf
= path
->nodes
[0];
180 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
181 struct btrfs_file_extent_item
);
183 /* are we inside the extent that was found? */
184 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
185 found_type
= btrfs_key_type(&found_key
);
186 if (found_key
.objectid
!= inode
->i_ino
||
187 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
191 found_type
= btrfs_file_extent_type(leaf
, item
);
192 extent_start
= found_key
.offset
;
193 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
194 u64 extent_num_bytes
;
196 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
197 extent_end
= extent_start
+ extent_num_bytes
;
200 if (loops
&& start
!= extent_start
)
203 if (start
< extent_start
|| start
>= extent_end
)
206 cow_end
= min(end
, extent_end
- 1);
207 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
212 * we may be called by the resizer, make sure we're inside
213 * the limits of the FS
215 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
218 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
228 btrfs_free_path(path
);
231 btrfs_release_path(root
, path
);
236 cow_file_range(inode
, start
, cow_end
);
241 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
243 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
246 mutex_lock(&root
->fs_info
->fs_mutex
);
247 if (btrfs_test_opt(root
, NODATACOW
) ||
248 btrfs_test_flag(inode
, NODATACOW
))
249 ret
= run_delalloc_nocow(inode
, start
, end
);
251 ret
= cow_file_range(inode
, start
, end
);
253 spin_lock(&root
->fs_info
->delalloc_lock
);
254 num_bytes
= end
+ 1 - start
;
255 if (root
->fs_info
->delalloc_bytes
< num_bytes
) {
256 printk("delalloc accounting error total %llu sub %llu\n",
257 root
->fs_info
->delalloc_bytes
, num_bytes
);
259 root
->fs_info
->delalloc_bytes
-= num_bytes
;
261 spin_unlock(&root
->fs_info
->delalloc_lock
);
263 mutex_unlock(&root
->fs_info
->fs_mutex
);
267 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
269 struct inode
*inode
= page
->mapping
->host
;
270 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
271 struct btrfs_trans_handle
*trans
;
274 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
275 size_t offset
= start
- page_start
;
276 if (btrfs_test_opt(root
, NODATASUM
) ||
277 btrfs_test_flag(inode
, NODATASUM
))
279 mutex_lock(&root
->fs_info
->fs_mutex
);
280 trans
= btrfs_start_transaction(root
, 1);
281 btrfs_set_trans_block_group(trans
, inode
);
283 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
284 start
, kaddr
+ offset
, end
- start
+ 1);
286 ret
= btrfs_end_transaction(trans
, root
);
288 mutex_unlock(&root
->fs_info
->fs_mutex
);
292 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
295 struct inode
*inode
= page
->mapping
->host
;
296 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
297 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
298 struct btrfs_csum_item
*item
;
299 struct btrfs_path
*path
= NULL
;
301 if (btrfs_test_opt(root
, NODATASUM
) ||
302 btrfs_test_flag(inode
, NODATASUM
))
304 mutex_lock(&root
->fs_info
->fs_mutex
);
305 path
= btrfs_alloc_path();
306 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
309 /* a csum that isn't present is a preallocated region. */
310 if (ret
== -ENOENT
|| ret
== -EFBIG
)
315 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
317 set_state_private(em_tree
, start
, csum
);
320 btrfs_free_path(path
);
321 mutex_unlock(&root
->fs_info
->fs_mutex
);
325 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
327 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
328 struct inode
*inode
= page
->mapping
->host
;
329 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
333 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
336 if (btrfs_test_opt(root
, NODATASUM
) ||
337 btrfs_test_flag(inode
, NODATASUM
))
339 ret
= get_state_private(em_tree
, start
, &private);
340 local_irq_save(flags
);
341 kaddr
= kmap_atomic(page
, KM_IRQ0
);
345 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
346 btrfs_csum_final(csum
, (char *)&csum
);
347 if (csum
!= private) {
350 kunmap_atomic(kaddr
, KM_IRQ0
);
351 local_irq_restore(flags
);
355 printk("btrfs csum failed ino %lu off %llu\n",
356 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
357 memset(kaddr
+ offset
, 1, end
- start
+ 1);
358 flush_dcache_page(page
);
359 kunmap_atomic(kaddr
, KM_IRQ0
);
360 local_irq_restore(flags
);
364 void btrfs_read_locked_inode(struct inode
*inode
)
366 struct btrfs_path
*path
;
367 struct extent_buffer
*leaf
;
368 struct btrfs_inode_item
*inode_item
;
369 struct btrfs_inode_timespec
*tspec
;
370 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
371 struct btrfs_key location
;
372 u64 alloc_group_block
;
376 path
= btrfs_alloc_path();
378 mutex_lock(&root
->fs_info
->fs_mutex
);
379 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
381 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
385 leaf
= path
->nodes
[0];
386 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
387 struct btrfs_inode_item
);
389 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
390 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
391 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
392 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
393 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
395 tspec
= btrfs_inode_atime(inode_item
);
396 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
397 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
399 tspec
= btrfs_inode_mtime(inode_item
);
400 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
401 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
403 tspec
= btrfs_inode_ctime(inode_item
);
404 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
405 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
407 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
408 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
410 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
412 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
413 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
415 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
416 if (!BTRFS_I(inode
)->block_group
) {
417 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
420 btrfs_free_path(path
);
423 mutex_unlock(&root
->fs_info
->fs_mutex
);
425 switch (inode
->i_mode
& S_IFMT
) {
427 inode
->i_mapping
->a_ops
= &btrfs_aops
;
428 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
429 inode
->i_fop
= &btrfs_file_operations
;
430 inode
->i_op
= &btrfs_file_inode_operations
;
433 inode
->i_fop
= &btrfs_dir_file_operations
;
434 if (root
== root
->fs_info
->tree_root
)
435 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
437 inode
->i_op
= &btrfs_dir_inode_operations
;
440 inode
->i_op
= &btrfs_symlink_inode_operations
;
441 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
444 init_special_inode(inode
, inode
->i_mode
, rdev
);
450 btrfs_release_path(root
, path
);
451 btrfs_free_path(path
);
452 mutex_unlock(&root
->fs_info
->fs_mutex
);
453 make_bad_inode(inode
);
456 static void fill_inode_item(struct extent_buffer
*leaf
,
457 struct btrfs_inode_item
*item
,
460 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
461 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
462 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
463 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
464 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
466 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
467 inode
->i_atime
.tv_sec
);
468 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
469 inode
->i_atime
.tv_nsec
);
471 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
472 inode
->i_mtime
.tv_sec
);
473 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
474 inode
->i_mtime
.tv_nsec
);
476 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
477 inode
->i_ctime
.tv_sec
);
478 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
479 inode
->i_ctime
.tv_nsec
);
481 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
482 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
483 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
484 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
485 btrfs_set_inode_block_group(leaf
, item
,
486 BTRFS_I(inode
)->block_group
->key
.objectid
);
489 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
490 struct btrfs_root
*root
,
493 struct btrfs_inode_item
*inode_item
;
494 struct btrfs_path
*path
;
495 struct extent_buffer
*leaf
;
498 path
= btrfs_alloc_path();
500 ret
= btrfs_lookup_inode(trans
, root
, path
,
501 &BTRFS_I(inode
)->location
, 1);
508 leaf
= path
->nodes
[0];
509 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
510 struct btrfs_inode_item
);
512 fill_inode_item(leaf
, inode_item
, inode
);
513 btrfs_mark_buffer_dirty(leaf
);
514 btrfs_set_inode_last_trans(trans
, inode
);
517 btrfs_release_path(root
, path
);
518 btrfs_free_path(path
);
523 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
524 struct btrfs_root
*root
,
526 struct dentry
*dentry
)
528 struct btrfs_path
*path
;
529 const char *name
= dentry
->d_name
.name
;
530 int name_len
= dentry
->d_name
.len
;
532 struct extent_buffer
*leaf
;
533 struct btrfs_dir_item
*di
;
534 struct btrfs_key key
;
536 path
= btrfs_alloc_path();
542 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
552 leaf
= path
->nodes
[0];
553 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
554 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
557 btrfs_release_path(root
, path
);
559 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
560 key
.objectid
, name
, name_len
, -1);
569 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
571 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
572 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
573 dentry
->d_inode
->i_ino
,
574 dentry
->d_parent
->d_inode
->i_ino
);
576 printk("failed to delete reference to %.*s, "
577 "inode %lu parent %lu\n", name_len
, name
,
578 dentry
->d_inode
->i_ino
,
579 dentry
->d_parent
->d_inode
->i_ino
);
582 btrfs_free_path(path
);
584 dir
->i_size
-= name_len
* 2;
585 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
586 btrfs_update_inode(trans
, root
, dir
);
587 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
588 dentry
->d_inode
->i_nlink
--;
590 drop_nlink(dentry
->d_inode
);
592 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
593 dir
->i_sb
->s_dirt
= 1;
598 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
600 struct btrfs_root
*root
;
601 struct btrfs_trans_handle
*trans
;
603 unsigned long nr
= 0;
605 root
= BTRFS_I(dir
)->root
;
606 mutex_lock(&root
->fs_info
->fs_mutex
);
608 ret
= btrfs_check_free_space(root
, 1, 1);
612 trans
= btrfs_start_transaction(root
, 1);
614 btrfs_set_trans_block_group(trans
, dir
);
615 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
616 nr
= trans
->blocks_used
;
618 btrfs_end_transaction(trans
, root
);
620 mutex_unlock(&root
->fs_info
->fs_mutex
);
621 btrfs_btree_balance_dirty(root
, nr
);
622 btrfs_throttle(root
);
626 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
628 struct inode
*inode
= dentry
->d_inode
;
631 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
632 struct btrfs_trans_handle
*trans
;
633 unsigned long nr
= 0;
635 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
638 mutex_lock(&root
->fs_info
->fs_mutex
);
639 ret
= btrfs_check_free_space(root
, 1, 1);
643 trans
= btrfs_start_transaction(root
, 1);
644 btrfs_set_trans_block_group(trans
, dir
);
646 /* now the directory is empty */
647 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
652 nr
= trans
->blocks_used
;
653 ret
= btrfs_end_transaction(trans
, root
);
655 mutex_unlock(&root
->fs_info
->fs_mutex
);
656 btrfs_btree_balance_dirty(root
, nr
);
657 btrfs_throttle(root
);
664 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
665 struct btrfs_root
*root
,
668 struct btrfs_path
*path
;
673 path
= btrfs_alloc_path();
675 ret
= btrfs_lookup_inode(trans
, root
, path
,
676 &BTRFS_I(inode
)->location
, -1);
680 ret
= btrfs_del_item(trans
, root
, path
);
681 btrfs_free_path(path
);
686 * this can truncate away extent items, csum items and directory items.
687 * It starts at a high offset and removes keys until it can't find
688 * any higher than i_size.
690 * csum items that cross the new i_size are truncated to the new size
693 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
694 struct btrfs_root
*root
,
698 struct btrfs_path
*path
;
699 struct btrfs_key key
;
700 struct btrfs_key found_key
;
702 struct extent_buffer
*leaf
;
703 struct btrfs_file_extent_item
*fi
;
704 u64 extent_start
= 0;
705 u64 extent_num_bytes
= 0;
711 int extent_type
= -1;
713 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
714 path
= btrfs_alloc_path();
718 /* FIXME, add redo link to tree so we don't leak on crash */
719 key
.objectid
= inode
->i_ino
;
720 key
.offset
= (u64
)-1;
724 btrfs_init_path(path
);
726 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
731 BUG_ON(path
->slots
[0] == 0);
734 leaf
= path
->nodes
[0];
735 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
736 found_type
= btrfs_key_type(&found_key
);
738 if (found_key
.objectid
!= inode
->i_ino
)
741 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
742 found_type
!= BTRFS_DIR_ITEM_KEY
&&
743 found_type
!= BTRFS_DIR_INDEX_KEY
&&
744 found_type
!= BTRFS_EXTENT_DATA_KEY
)
747 item_end
= found_key
.offset
;
748 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
749 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
750 struct btrfs_file_extent_item
);
751 extent_type
= btrfs_file_extent_type(leaf
, fi
);
752 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
754 btrfs_file_extent_num_bytes(leaf
, fi
);
755 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
756 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
758 item_end
+= btrfs_file_extent_inline_len(leaf
,
763 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
764 ret
= btrfs_csum_truncate(trans
, root
, path
,
768 if (item_end
< inode
->i_size
) {
769 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
770 found_type
= BTRFS_INODE_ITEM_KEY
;
771 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
772 found_type
= BTRFS_CSUM_ITEM_KEY
;
773 } else if (found_type
) {
778 btrfs_set_key_type(&key
, found_type
);
779 btrfs_release_path(root
, path
);
782 if (found_key
.offset
>= inode
->i_size
)
788 /* FIXME, shrink the extent if the ref count is only 1 */
789 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
792 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
794 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
797 btrfs_file_extent_num_bytes(leaf
, fi
);
798 extent_num_bytes
= inode
->i_size
-
799 found_key
.offset
+ root
->sectorsize
- 1;
800 btrfs_set_file_extent_num_bytes(leaf
, fi
,
802 num_dec
= (orig_num_bytes
-
803 extent_num_bytes
) >> 9;
804 if (extent_start
!= 0) {
805 inode
->i_blocks
-= num_dec
;
807 btrfs_mark_buffer_dirty(leaf
);
810 btrfs_file_extent_disk_num_bytes(leaf
,
812 /* FIXME blocksize != 4096 */
813 num_dec
= btrfs_file_extent_num_bytes(leaf
,
815 if (extent_start
!= 0) {
817 inode
->i_blocks
-= num_dec
;
819 root_gen
= btrfs_header_generation(leaf
);
820 root_owner
= btrfs_header_owner(leaf
);
822 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
824 u32 newsize
= inode
->i_size
- found_key
.offset
;
825 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
826 ret
= btrfs_truncate_item(trans
, root
, path
,
832 ret
= btrfs_del_item(trans
, root
, path
);
838 btrfs_release_path(root
, path
);
840 ret
= btrfs_free_extent(trans
, root
, extent_start
,
843 root_gen
, inode
->i_ino
,
844 found_key
.offset
, 0);
850 btrfs_release_path(root
, path
);
851 btrfs_free_path(path
);
852 inode
->i_sb
->s_dirt
= 1;
856 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
860 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
862 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
863 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
864 u64 existing_delalloc
;
868 WARN_ON(!PageLocked(page
));
869 set_page_extent_mapped(page
);
871 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
872 delalloc_start
= page_start
;
873 existing_delalloc
= count_range_bits(&BTRFS_I(inode
)->extent_tree
,
874 &delalloc_start
, page_end
,
875 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
876 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
879 spin_lock(&root
->fs_info
->delalloc_lock
);
880 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
- existing_delalloc
;
881 spin_unlock(&root
->fs_info
->delalloc_lock
);
883 if (zero_start
!= PAGE_CACHE_SIZE
) {
885 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
886 flush_dcache_page(page
);
889 set_page_dirty(page
);
890 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
896 * taken from block_truncate_page, but does cow as it zeros out
897 * any bytes left in the last page in the file.
899 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
901 struct inode
*inode
= mapping
->host
;
902 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
903 u32 blocksize
= root
->sectorsize
;
904 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
905 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
910 if ((offset
& (blocksize
- 1)) == 0)
914 page
= grab_cache_page(mapping
, index
);
917 if (!PageUptodate(page
)) {
918 ret
= btrfs_readpage(NULL
, page
);
920 if (!PageUptodate(page
)) {
925 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
927 ret
= btrfs_cow_one_page(inode
, page
, offset
);
930 page_cache_release(page
);
935 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
937 struct inode
*inode
= dentry
->d_inode
;
940 err
= inode_change_ok(inode
, attr
);
944 if (S_ISREG(inode
->i_mode
) &&
945 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
946 struct btrfs_trans_handle
*trans
;
947 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
948 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
950 u64 mask
= root
->sectorsize
- 1;
951 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
952 u64 block_end
= attr
->ia_size
| mask
;
956 if (attr
->ia_size
<= pos
)
959 mutex_lock(&root
->fs_info
->fs_mutex
);
960 err
= btrfs_check_free_space(root
, 1, 0);
961 mutex_unlock(&root
->fs_info
->fs_mutex
);
965 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
967 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
968 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
970 mutex_lock(&root
->fs_info
->fs_mutex
);
971 trans
= btrfs_start_transaction(root
, 1);
972 btrfs_set_trans_block_group(trans
, inode
);
973 err
= btrfs_drop_extents(trans
, root
, inode
,
974 pos
, pos
+ hole_size
, pos
,
977 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
978 err
= btrfs_insert_file_extent(trans
, root
,
980 pos
, 0, 0, hole_size
);
982 btrfs_end_transaction(trans
, root
);
983 mutex_unlock(&root
->fs_info
->fs_mutex
);
984 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
989 err
= inode_setattr(inode
, attr
);
994 void btrfs_drop_inode(struct inode
*inode
)
996 if (!BTRFS_I(inode
)->ordered_trans
|| inode
->i_nlink
) {
997 generic_drop_inode(inode
);
1000 /* FIXME, make sure this delete actually ends up in the transaction */
1001 btrfs_del_ordered_inode(inode
);
1002 generic_drop_inode(inode
);
1005 void btrfs_delete_inode(struct inode
*inode
)
1007 struct btrfs_trans_handle
*trans
;
1008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1012 truncate_inode_pages(&inode
->i_data
, 0);
1013 if (is_bad_inode(inode
)) {
1018 mutex_lock(&root
->fs_info
->fs_mutex
);
1019 trans
= btrfs_start_transaction(root
, 1);
1021 btrfs_set_trans_block_group(trans
, inode
);
1022 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1024 goto no_delete_lock
;
1025 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
1027 goto no_delete_lock
;
1028 ret
= btrfs_free_inode(trans
, root
, inode
);
1030 goto no_delete_lock
;
1031 nr
= trans
->blocks_used
;
1033 btrfs_end_transaction(trans
, root
);
1034 mutex_unlock(&root
->fs_info
->fs_mutex
);
1035 btrfs_btree_balance_dirty(root
, nr
);
1036 btrfs_throttle(root
);
1040 nr
= trans
->blocks_used
;
1041 btrfs_end_transaction(trans
, root
);
1042 mutex_unlock(&root
->fs_info
->fs_mutex
);
1043 btrfs_btree_balance_dirty(root
, nr
);
1044 btrfs_throttle(root
);
1050 * this returns the key found in the dir entry in the location pointer.
1051 * If no dir entries were found, location->objectid is 0.
1053 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1054 struct btrfs_key
*location
)
1056 const char *name
= dentry
->d_name
.name
;
1057 int namelen
= dentry
->d_name
.len
;
1058 struct btrfs_dir_item
*di
;
1059 struct btrfs_path
*path
;
1060 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1063 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1064 location
->objectid
= dir
->i_ino
;
1065 location
->type
= BTRFS_INODE_ITEM_KEY
;
1066 location
->offset
= 0;
1069 path
= btrfs_alloc_path();
1072 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1073 struct btrfs_key key
;
1074 struct extent_buffer
*leaf
;
1078 key
.objectid
= dir
->i_ino
;
1079 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1081 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1085 leaf
= path
->nodes
[0];
1086 slot
= path
->slots
[0];
1087 nritems
= btrfs_header_nritems(leaf
);
1088 if (slot
>= nritems
)
1091 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1092 if (key
.objectid
!= dir
->i_ino
||
1093 key
.type
!= BTRFS_INODE_REF_KEY
) {
1096 location
->objectid
= key
.offset
;
1097 location
->type
= BTRFS_INODE_ITEM_KEY
;
1098 location
->offset
= 0;
1102 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1106 if (!di
|| IS_ERR(di
)) {
1109 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1111 btrfs_free_path(path
);
1114 location
->objectid
= 0;
1119 * when we hit a tree root in a directory, the btrfs part of the inode
1120 * needs to be changed to reflect the root directory of the tree root. This
1121 * is kind of like crossing a mount point.
1123 static int fixup_tree_root_location(struct btrfs_root
*root
,
1124 struct btrfs_key
*location
,
1125 struct btrfs_root
**sub_root
,
1126 struct dentry
*dentry
)
1128 struct btrfs_path
*path
;
1129 struct btrfs_root_item
*ri
;
1131 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1133 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1136 path
= btrfs_alloc_path();
1138 mutex_lock(&root
->fs_info
->fs_mutex
);
1140 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1141 dentry
->d_name
.name
,
1142 dentry
->d_name
.len
);
1143 if (IS_ERR(*sub_root
))
1144 return PTR_ERR(*sub_root
);
1146 ri
= &(*sub_root
)->root_item
;
1147 location
->objectid
= btrfs_root_dirid(ri
);
1148 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1149 location
->offset
= 0;
1151 btrfs_free_path(path
);
1152 mutex_unlock(&root
->fs_info
->fs_mutex
);
1156 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1158 struct btrfs_iget_args
*args
= p
;
1159 inode
->i_ino
= args
->ino
;
1160 BTRFS_I(inode
)->root
= args
->root
;
1161 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1162 inode
->i_mapping
, GFP_NOFS
);
1166 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1168 struct btrfs_iget_args
*args
= opaque
;
1169 return (args
->ino
== inode
->i_ino
&&
1170 args
->root
== BTRFS_I(inode
)->root
);
1173 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1176 struct btrfs_iget_args args
;
1177 args
.ino
= objectid
;
1178 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1183 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1186 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1187 struct btrfs_root
*root
)
1189 struct inode
*inode
;
1190 struct btrfs_iget_args args
;
1191 args
.ino
= objectid
;
1194 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1195 btrfs_init_locked_inode
,
1200 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1201 struct nameidata
*nd
)
1203 struct inode
* inode
;
1204 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1205 struct btrfs_root
*root
= bi
->root
;
1206 struct btrfs_root
*sub_root
= root
;
1207 struct btrfs_key location
;
1210 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1211 return ERR_PTR(-ENAMETOOLONG
);
1213 mutex_lock(&root
->fs_info
->fs_mutex
);
1214 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1215 mutex_unlock(&root
->fs_info
->fs_mutex
);
1218 return ERR_PTR(ret
);
1221 if (location
.objectid
) {
1222 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1225 return ERR_PTR(ret
);
1227 return ERR_PTR(-ENOENT
);
1228 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1231 return ERR_PTR(-EACCES
);
1232 if (inode
->i_state
& I_NEW
) {
1233 /* the inode and parent dir are two different roots */
1234 if (sub_root
!= root
) {
1236 sub_root
->inode
= inode
;
1238 BTRFS_I(inode
)->root
= sub_root
;
1239 memcpy(&BTRFS_I(inode
)->location
, &location
,
1241 btrfs_read_locked_inode(inode
);
1242 unlock_new_inode(inode
);
1245 return d_splice_alias(inode
, dentry
);
1248 static unsigned char btrfs_filetype_table
[] = {
1249 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1252 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1254 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1255 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1256 struct btrfs_item
*item
;
1257 struct btrfs_dir_item
*di
;
1258 struct btrfs_key key
;
1259 struct btrfs_key found_key
;
1260 struct btrfs_path
*path
;
1263 struct extent_buffer
*leaf
;
1266 unsigned char d_type
;
1271 int key_type
= BTRFS_DIR_INDEX_KEY
;
1276 /* FIXME, use a real flag for deciding about the key type */
1277 if (root
->fs_info
->tree_root
== root
)
1278 key_type
= BTRFS_DIR_ITEM_KEY
;
1280 /* special case for "." */
1281 if (filp
->f_pos
== 0) {
1282 over
= filldir(dirent
, ".", 1,
1290 mutex_lock(&root
->fs_info
->fs_mutex
);
1291 key
.objectid
= inode
->i_ino
;
1292 path
= btrfs_alloc_path();
1295 /* special case for .., just use the back ref */
1296 if (filp
->f_pos
== 1) {
1297 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1299 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1301 leaf
= path
->nodes
[0];
1302 slot
= path
->slots
[0];
1303 nritems
= btrfs_header_nritems(leaf
);
1304 if (slot
>= nritems
) {
1305 btrfs_release_path(root
, path
);
1306 goto read_dir_items
;
1308 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1309 btrfs_release_path(root
, path
);
1310 if (found_key
.objectid
!= key
.objectid
||
1311 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1312 goto read_dir_items
;
1313 over
= filldir(dirent
, "..", 2,
1314 2, found_key
.offset
, DT_DIR
);
1321 btrfs_set_key_type(&key
, key_type
);
1322 key
.offset
= filp
->f_pos
;
1324 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1329 leaf
= path
->nodes
[0];
1330 nritems
= btrfs_header_nritems(leaf
);
1331 slot
= path
->slots
[0];
1332 if (advance
|| slot
>= nritems
) {
1333 if (slot
>= nritems
-1) {
1334 ret
= btrfs_next_leaf(root
, path
);
1337 leaf
= path
->nodes
[0];
1338 nritems
= btrfs_header_nritems(leaf
);
1339 slot
= path
->slots
[0];
1346 item
= btrfs_item_nr(leaf
, slot
);
1347 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1349 if (found_key
.objectid
!= key
.objectid
)
1351 if (btrfs_key_type(&found_key
) != key_type
)
1353 if (found_key
.offset
< filp
->f_pos
)
1356 filp
->f_pos
= found_key
.offset
;
1358 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1360 di_total
= btrfs_item_size(leaf
, item
);
1361 while(di_cur
< di_total
) {
1362 struct btrfs_key location
;
1364 name_len
= btrfs_dir_name_len(leaf
, di
);
1365 if (name_len
< 32) {
1366 name_ptr
= tmp_name
;
1368 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1371 read_extent_buffer(leaf
, name_ptr
,
1372 (unsigned long)(di
+ 1), name_len
);
1374 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1375 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1376 over
= filldir(dirent
, name_ptr
, name_len
,
1381 if (name_ptr
!= tmp_name
)
1386 di_len
= btrfs_dir_name_len(leaf
, di
) +
1387 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1389 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1396 btrfs_release_path(root
, path
);
1397 btrfs_free_path(path
);
1398 mutex_unlock(&root
->fs_info
->fs_mutex
);
1402 int btrfs_write_inode(struct inode
*inode
, int wait
)
1404 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1405 struct btrfs_trans_handle
*trans
;
1409 mutex_lock(&root
->fs_info
->fs_mutex
);
1410 trans
= btrfs_start_transaction(root
, 1);
1411 btrfs_set_trans_block_group(trans
, inode
);
1412 ret
= btrfs_commit_transaction(trans
, root
);
1413 mutex_unlock(&root
->fs_info
->fs_mutex
);
1419 * This is somewhat expensive, updating the tree every time the
1420 * inode changes. But, it is most likely to find the inode in cache.
1421 * FIXME, needs more benchmarking...there are no reasons other than performance
1422 * to keep or drop this code.
1424 void btrfs_dirty_inode(struct inode
*inode
)
1426 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1427 struct btrfs_trans_handle
*trans
;
1429 mutex_lock(&root
->fs_info
->fs_mutex
);
1430 trans
= btrfs_start_transaction(root
, 1);
1431 btrfs_set_trans_block_group(trans
, inode
);
1432 btrfs_update_inode(trans
, root
, inode
);
1433 btrfs_end_transaction(trans
, root
);
1434 mutex_unlock(&root
->fs_info
->fs_mutex
);
1437 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1438 struct btrfs_root
*root
,
1440 struct btrfs_block_group_cache
*group
,
1443 struct inode
*inode
;
1444 struct btrfs_inode_item
*inode_item
;
1445 struct btrfs_key
*location
;
1446 struct btrfs_path
*path
;
1450 path
= btrfs_alloc_path();
1453 inode
= new_inode(root
->fs_info
->sb
);
1455 return ERR_PTR(-ENOMEM
);
1457 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1458 inode
->i_mapping
, GFP_NOFS
);
1459 BTRFS_I(inode
)->root
= root
;
1465 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1466 BTRFS_I(inode
)->block_group
= group
;
1467 BTRFS_I(inode
)->flags
= 0;
1468 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1472 inode
->i_uid
= current
->fsuid
;
1473 inode
->i_gid
= current
->fsgid
;
1474 inode
->i_mode
= mode
;
1475 inode
->i_ino
= objectid
;
1476 inode
->i_blocks
= 0;
1477 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1478 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1479 struct btrfs_inode_item
);
1480 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1481 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1482 btrfs_free_path(path
);
1484 location
= &BTRFS_I(inode
)->location
;
1485 location
->objectid
= objectid
;
1486 location
->offset
= 0;
1487 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1489 insert_inode_hash(inode
);
1492 btrfs_free_path(path
);
1493 return ERR_PTR(ret
);
1496 static inline u8
btrfs_inode_type(struct inode
*inode
)
1498 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1501 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1502 struct dentry
*dentry
, struct inode
*inode
)
1505 struct btrfs_key key
;
1506 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1507 struct inode
*parent_inode
;
1509 key
.objectid
= inode
->i_ino
;
1510 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1513 ret
= btrfs_insert_dir_item(trans
, root
,
1514 dentry
->d_name
.name
, dentry
->d_name
.len
,
1515 dentry
->d_parent
->d_inode
->i_ino
,
1516 &key
, btrfs_inode_type(inode
));
1518 ret
= btrfs_insert_inode_ref(trans
, root
,
1519 dentry
->d_name
.name
,
1522 dentry
->d_parent
->d_inode
->i_ino
);
1523 parent_inode
= dentry
->d_parent
->d_inode
;
1524 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1525 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1526 ret
= btrfs_update_inode(trans
, root
,
1527 dentry
->d_parent
->d_inode
);
1532 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1533 struct dentry
*dentry
, struct inode
*inode
)
1535 int err
= btrfs_add_link(trans
, dentry
, inode
);
1537 d_instantiate(dentry
, inode
);
1545 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1546 int mode
, dev_t rdev
)
1548 struct btrfs_trans_handle
*trans
;
1549 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1550 struct inode
*inode
= NULL
;
1554 unsigned long nr
= 0;
1556 if (!new_valid_dev(rdev
))
1559 mutex_lock(&root
->fs_info
->fs_mutex
);
1560 err
= btrfs_check_free_space(root
, 1, 0);
1564 trans
= btrfs_start_transaction(root
, 1);
1565 btrfs_set_trans_block_group(trans
, dir
);
1567 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1573 inode
= btrfs_new_inode(trans
, root
, objectid
,
1574 BTRFS_I(dir
)->block_group
, mode
);
1575 err
= PTR_ERR(inode
);
1579 btrfs_set_trans_block_group(trans
, inode
);
1580 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1584 inode
->i_op
= &btrfs_special_inode_operations
;
1585 init_special_inode(inode
, inode
->i_mode
, rdev
);
1586 btrfs_update_inode(trans
, root
, inode
);
1588 dir
->i_sb
->s_dirt
= 1;
1589 btrfs_update_inode_block_group(trans
, inode
);
1590 btrfs_update_inode_block_group(trans
, dir
);
1592 nr
= trans
->blocks_used
;
1593 btrfs_end_transaction(trans
, root
);
1595 mutex_unlock(&root
->fs_info
->fs_mutex
);
1598 inode_dec_link_count(inode
);
1601 btrfs_btree_balance_dirty(root
, nr
);
1602 btrfs_throttle(root
);
1606 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1607 int mode
, struct nameidata
*nd
)
1609 struct btrfs_trans_handle
*trans
;
1610 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1611 struct inode
*inode
= NULL
;
1614 unsigned long nr
= 0;
1617 mutex_lock(&root
->fs_info
->fs_mutex
);
1618 err
= btrfs_check_free_space(root
, 1, 0);
1621 trans
= btrfs_start_transaction(root
, 1);
1622 btrfs_set_trans_block_group(trans
, dir
);
1624 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1630 inode
= btrfs_new_inode(trans
, root
, objectid
,
1631 BTRFS_I(dir
)->block_group
, mode
);
1632 err
= PTR_ERR(inode
);
1636 btrfs_set_trans_block_group(trans
, inode
);
1637 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1641 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1642 inode
->i_fop
= &btrfs_file_operations
;
1643 inode
->i_op
= &btrfs_file_inode_operations
;
1644 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1645 inode
->i_mapping
, GFP_NOFS
);
1646 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1648 dir
->i_sb
->s_dirt
= 1;
1649 btrfs_update_inode_block_group(trans
, inode
);
1650 btrfs_update_inode_block_group(trans
, dir
);
1652 nr
= trans
->blocks_used
;
1653 btrfs_end_transaction(trans
, root
);
1655 mutex_unlock(&root
->fs_info
->fs_mutex
);
1658 inode_dec_link_count(inode
);
1661 btrfs_btree_balance_dirty(root
, nr
);
1662 btrfs_throttle(root
);
1666 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1667 struct dentry
*dentry
)
1669 struct btrfs_trans_handle
*trans
;
1670 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1671 struct inode
*inode
= old_dentry
->d_inode
;
1672 unsigned long nr
= 0;
1676 if (inode
->i_nlink
== 0)
1679 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1684 mutex_lock(&root
->fs_info
->fs_mutex
);
1685 err
= btrfs_check_free_space(root
, 1, 0);
1688 trans
= btrfs_start_transaction(root
, 1);
1690 btrfs_set_trans_block_group(trans
, dir
);
1691 atomic_inc(&inode
->i_count
);
1692 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1697 dir
->i_sb
->s_dirt
= 1;
1698 btrfs_update_inode_block_group(trans
, dir
);
1699 err
= btrfs_update_inode(trans
, root
, inode
);
1704 nr
= trans
->blocks_used
;
1705 btrfs_end_transaction(trans
, root
);
1707 mutex_unlock(&root
->fs_info
->fs_mutex
);
1710 inode_dec_link_count(inode
);
1713 btrfs_btree_balance_dirty(root
, nr
);
1714 btrfs_throttle(root
);
1718 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1720 struct inode
*inode
;
1721 struct btrfs_trans_handle
*trans
;
1722 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1724 int drop_on_err
= 0;
1726 unsigned long nr
= 1;
1728 mutex_lock(&root
->fs_info
->fs_mutex
);
1729 err
= btrfs_check_free_space(root
, 1, 0);
1733 trans
= btrfs_start_transaction(root
, 1);
1734 btrfs_set_trans_block_group(trans
, dir
);
1736 if (IS_ERR(trans
)) {
1737 err
= PTR_ERR(trans
);
1741 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1747 inode
= btrfs_new_inode(trans
, root
, objectid
,
1748 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1749 if (IS_ERR(inode
)) {
1750 err
= PTR_ERR(inode
);
1755 inode
->i_op
= &btrfs_dir_inode_operations
;
1756 inode
->i_fop
= &btrfs_dir_file_operations
;
1757 btrfs_set_trans_block_group(trans
, inode
);
1760 err
= btrfs_update_inode(trans
, root
, inode
);
1764 err
= btrfs_add_link(trans
, dentry
, inode
);
1768 d_instantiate(dentry
, inode
);
1770 dir
->i_sb
->s_dirt
= 1;
1771 btrfs_update_inode_block_group(trans
, inode
);
1772 btrfs_update_inode_block_group(trans
, dir
);
1775 nr
= trans
->blocks_used
;
1776 btrfs_end_transaction(trans
, root
);
1779 mutex_unlock(&root
->fs_info
->fs_mutex
);
1782 btrfs_btree_balance_dirty(root
, nr
);
1783 btrfs_throttle(root
);
1787 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1788 size_t page_offset
, u64 start
, u64 end
,
1794 u64 extent_start
= 0;
1796 u64 objectid
= inode
->i_ino
;
1798 int failed_insert
= 0;
1799 struct btrfs_path
*path
;
1800 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1801 struct btrfs_file_extent_item
*item
;
1802 struct extent_buffer
*leaf
;
1803 struct btrfs_key found_key
;
1804 struct extent_map
*em
= NULL
;
1805 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1806 struct btrfs_trans_handle
*trans
= NULL
;
1808 path
= btrfs_alloc_path();
1810 mutex_lock(&root
->fs_info
->fs_mutex
);
1813 em
= lookup_extent_mapping(em_tree
, start
, end
);
1815 if (em
->start
> start
) {
1816 printk("get_extent start %Lu em start %Lu\n",
1823 em
= alloc_extent_map(GFP_NOFS
);
1828 em
->start
= EXTENT_MAP_HOLE
;
1829 em
->end
= EXTENT_MAP_HOLE
;
1831 em
->bdev
= inode
->i_sb
->s_bdev
;
1832 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1833 objectid
, start
, trans
!= NULL
);
1840 if (path
->slots
[0] == 0)
1845 leaf
= path
->nodes
[0];
1846 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1847 struct btrfs_file_extent_item
);
1848 /* are we inside the extent that was found? */
1849 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1850 found_type
= btrfs_key_type(&found_key
);
1851 if (found_key
.objectid
!= objectid
||
1852 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1856 found_type
= btrfs_file_extent_type(leaf
, item
);
1857 extent_start
= found_key
.offset
;
1858 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1859 extent_end
= extent_start
+
1860 btrfs_file_extent_num_bytes(leaf
, item
);
1862 if (start
< extent_start
|| start
>= extent_end
) {
1864 if (start
< extent_start
) {
1865 if (end
< extent_start
)
1867 em
->end
= extent_end
- 1;
1873 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1875 em
->start
= extent_start
;
1876 em
->end
= extent_end
- 1;
1877 em
->block_start
= EXTENT_MAP_HOLE
;
1878 em
->block_end
= EXTENT_MAP_HOLE
;
1881 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1882 em
->block_start
= bytenr
;
1883 em
->block_end
= em
->block_start
+
1884 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1885 em
->start
= extent_start
;
1886 em
->end
= extent_end
- 1;
1888 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1892 size_t extent_offset
;
1895 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1897 extent_end
= (extent_start
+ size
- 1) |
1898 ((u64
)root
->sectorsize
- 1);
1899 if (start
< extent_start
|| start
>= extent_end
) {
1901 if (start
< extent_start
) {
1902 if (end
< extent_start
)
1904 em
->end
= extent_end
;
1910 em
->block_start
= EXTENT_MAP_INLINE
;
1911 em
->block_end
= EXTENT_MAP_INLINE
;
1914 em
->start
= extent_start
;
1915 em
->end
= extent_start
+ size
- 1;
1919 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1920 extent_start
+ page_offset
;
1921 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1922 size
- extent_offset
);
1923 em
->start
= extent_start
+ extent_offset
;
1924 em
->end
= (em
->start
+ copy_size
-1) |
1925 ((u64
)root
->sectorsize
-1);
1927 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1928 if (create
== 0 && !PageUptodate(page
)) {
1929 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1931 flush_dcache_page(page
);
1932 } else if (create
&& PageUptodate(page
)) {
1935 free_extent_map(em
);
1937 btrfs_release_path(root
, path
);
1938 trans
= btrfs_start_transaction(root
, 1);
1941 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1943 btrfs_mark_buffer_dirty(leaf
);
1946 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1949 printk("unkknown found_type %d\n", found_type
);
1956 em
->block_start
= EXTENT_MAP_HOLE
;
1957 em
->block_end
= EXTENT_MAP_HOLE
;
1959 btrfs_release_path(root
, path
);
1960 if (em
->start
> start
|| em
->end
< start
) {
1961 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1965 ret
= add_extent_mapping(em_tree
, em
);
1966 if (ret
== -EEXIST
) {
1967 free_extent_map(em
);
1969 if (0 && failed_insert
== 1) {
1970 btrfs_drop_extent_cache(inode
, start
, end
);
1973 if (failed_insert
> 5) {
1974 printk("failing to insert %Lu %Lu\n", start
, end
);
1982 btrfs_free_path(path
);
1984 ret
= btrfs_end_transaction(trans
, root
);
1988 mutex_unlock(&root
->fs_info
->fs_mutex
);
1990 free_extent_map(em
);
1992 return ERR_PTR(err
);
1997 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1999 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2002 int btrfs_readpage(struct file
*file
, struct page
*page
)
2004 struct extent_map_tree
*tree
;
2005 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2006 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2009 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2011 struct extent_map_tree
*tree
;
2014 if (current
->flags
& PF_MEMALLOC
) {
2015 redirty_page_for_writepage(wbc
, page
);
2019 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2020 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2023 static int btrfs_writepages(struct address_space
*mapping
,
2024 struct writeback_control
*wbc
)
2026 struct extent_map_tree
*tree
;
2027 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
2028 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2032 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2033 struct list_head
*pages
, unsigned nr_pages
)
2035 struct extent_map_tree
*tree
;
2036 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
2037 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2041 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
2043 struct extent_map_tree
*tree
;
2046 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2047 ret
= try_release_extent_mapping(tree
, page
);
2049 ClearPagePrivate(page
);
2050 set_page_private(page
, 0);
2051 page_cache_release(page
);
2056 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2058 struct extent_map_tree
*tree
;
2060 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2061 extent_invalidatepage(tree
, page
, offset
);
2062 btrfs_releasepage(page
, GFP_NOFS
);
2066 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2067 * called from a page fault handler when a page is first dirtied. Hence we must
2068 * be careful to check for EOF conditions here. We set the page up correctly
2069 * for a written page which means we get ENOSPC checking when writing into
2070 * holes and correct delalloc and unwritten extent mapping on filesystems that
2071 * support these features.
2073 * We are not allowed to take the i_mutex here so we have to play games to
2074 * protect against truncate races as the page could now be beyond EOF. Because
2075 * vmtruncate() writes the inode size before removing pages, once we have the
2076 * page lock we can determine safely if the page is beyond EOF. If it is not
2077 * beyond EOF, then the page is guaranteed safe against truncation until we
2080 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2082 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2083 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2089 mutex_lock(&root
->fs_info
->fs_mutex
);
2090 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2091 mutex_unlock(&root
->fs_info
->fs_mutex
);
2098 wait_on_page_writeback(page
);
2099 size
= i_size_read(inode
);
2100 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2102 if ((page
->mapping
!= inode
->i_mapping
) ||
2103 (page_start
> size
)) {
2104 /* page got truncated out from underneath us */
2108 /* page is wholly or partially inside EOF */
2109 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2110 end
= size
& ~PAGE_CACHE_MASK
;
2112 end
= PAGE_CACHE_SIZE
;
2114 ret
= btrfs_cow_one_page(inode
, page
, end
);
2122 static void btrfs_truncate(struct inode
*inode
)
2124 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2126 struct btrfs_trans_handle
*trans
;
2129 if (!S_ISREG(inode
->i_mode
))
2131 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2134 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2136 mutex_lock(&root
->fs_info
->fs_mutex
);
2137 trans
= btrfs_start_transaction(root
, 1);
2138 btrfs_set_trans_block_group(trans
, inode
);
2140 /* FIXME, add redo link to tree so we don't leak on crash */
2141 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
2142 btrfs_update_inode(trans
, root
, inode
);
2143 nr
= trans
->blocks_used
;
2145 ret
= btrfs_end_transaction(trans
, root
);
2147 mutex_unlock(&root
->fs_info
->fs_mutex
);
2148 btrfs_btree_balance_dirty(root
, nr
);
2149 btrfs_throttle(root
);
2152 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2155 struct btrfs_trans_handle
*trans
;
2156 struct btrfs_key key
;
2157 struct btrfs_root_item root_item
;
2158 struct btrfs_inode_item
*inode_item
;
2159 struct extent_buffer
*leaf
;
2160 struct btrfs_root
*new_root
= root
;
2161 struct inode
*inode
;
2166 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2167 unsigned long nr
= 1;
2169 mutex_lock(&root
->fs_info
->fs_mutex
);
2170 ret
= btrfs_check_free_space(root
, 1, 0);
2174 trans
= btrfs_start_transaction(root
, 1);
2177 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2182 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2183 objectid
, trans
->transid
, 0, 0,
2186 return PTR_ERR(leaf
);
2188 btrfs_set_header_nritems(leaf
, 0);
2189 btrfs_set_header_level(leaf
, 0);
2190 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2191 btrfs_set_header_generation(leaf
, trans
->transid
);
2192 btrfs_set_header_owner(leaf
, objectid
);
2194 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2195 (unsigned long)btrfs_header_fsid(leaf
),
2197 btrfs_mark_buffer_dirty(leaf
);
2199 inode_item
= &root_item
.inode
;
2200 memset(inode_item
, 0, sizeof(*inode_item
));
2201 inode_item
->generation
= cpu_to_le64(1);
2202 inode_item
->size
= cpu_to_le64(3);
2203 inode_item
->nlink
= cpu_to_le32(1);
2204 inode_item
->nblocks
= cpu_to_le64(1);
2205 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2207 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2208 btrfs_set_root_level(&root_item
, 0);
2209 btrfs_set_root_refs(&root_item
, 1);
2210 btrfs_set_root_used(&root_item
, 0);
2212 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2213 root_item
.drop_level
= 0;
2215 free_extent_buffer(leaf
);
2218 btrfs_set_root_dirid(&root_item
, new_dirid
);
2220 key
.objectid
= objectid
;
2222 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2223 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2229 * insert the directory item
2231 key
.offset
= (u64
)-1;
2232 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2233 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2234 name
, namelen
, dir
->i_ino
, &key
,
2239 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2240 name
, namelen
, objectid
,
2241 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2245 ret
= btrfs_commit_transaction(trans
, root
);
2249 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2252 trans
= btrfs_start_transaction(new_root
, 1);
2255 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2256 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2259 inode
->i_op
= &btrfs_dir_inode_operations
;
2260 inode
->i_fop
= &btrfs_dir_file_operations
;
2261 new_root
->inode
= inode
;
2263 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2267 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2271 nr
= trans
->blocks_used
;
2272 err
= btrfs_commit_transaction(trans
, new_root
);
2276 mutex_unlock(&root
->fs_info
->fs_mutex
);
2277 btrfs_btree_balance_dirty(root
, nr
);
2278 btrfs_throttle(root
);
2282 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2284 struct btrfs_pending_snapshot
*pending_snapshot
;
2285 struct btrfs_trans_handle
*trans
;
2288 unsigned long nr
= 0;
2290 if (!root
->ref_cows
)
2293 mutex_lock(&root
->fs_info
->fs_mutex
);
2294 ret
= btrfs_check_free_space(root
, 1, 0);
2298 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2299 if (!pending_snapshot
) {
2303 pending_snapshot
->name
= kstrndup(name
, namelen
, GFP_NOFS
);
2304 if (!pending_snapshot
->name
) {
2306 kfree(pending_snapshot
);
2309 trans
= btrfs_start_transaction(root
, 1);
2312 pending_snapshot
->root
= root
;
2313 list_add(&pending_snapshot
->list
,
2314 &trans
->transaction
->pending_snapshots
);
2315 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2316 err
= btrfs_commit_transaction(trans
, root
);
2319 mutex_unlock(&root
->fs_info
->fs_mutex
);
2320 btrfs_btree_balance_dirty(root
, nr
);
2321 btrfs_throttle(root
);
2325 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2326 struct file_ra_state
*ra
, struct file
*file
,
2327 pgoff_t offset
, pgoff_t last_index
)
2331 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2332 req_size
= last_index
- offset
+ 1;
2333 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2336 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2337 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2338 return offset
+ req_size
;
2342 int btrfs_defrag_file(struct file
*file
) {
2343 struct inode
*inode
= fdentry(file
)->d_inode
;
2344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2345 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2347 unsigned long last_index
;
2348 unsigned long ra_index
= 0;
2352 u64 existing_delalloc
;
2356 mutex_lock(&root
->fs_info
->fs_mutex
);
2357 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2358 mutex_unlock(&root
->fs_info
->fs_mutex
);
2362 mutex_lock(&inode
->i_mutex
);
2363 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2364 for (i
= 0; i
<= last_index
; i
++) {
2365 if (i
== ra_index
) {
2366 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2368 file
, ra_index
, last_index
);
2370 page
= grab_cache_page(inode
->i_mapping
, i
);
2373 if (!PageUptodate(page
)) {
2374 btrfs_readpage(NULL
, page
);
2376 if (!PageUptodate(page
)) {
2378 page_cache_release(page
);
2382 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2383 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2385 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2386 delalloc_start
= page_start
;
2388 count_range_bits(&BTRFS_I(inode
)->extent_tree
,
2389 &delalloc_start
, page_end
,
2390 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
2391 set_extent_delalloc(em_tree
, page_start
,
2392 page_end
, GFP_NOFS
);
2394 spin_lock(&root
->fs_info
->delalloc_lock
);
2395 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
-
2397 spin_unlock(&root
->fs_info
->delalloc_lock
);
2399 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2400 set_page_dirty(page
);
2402 page_cache_release(page
);
2403 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2407 mutex_unlock(&inode
->i_mutex
);
2411 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2415 struct btrfs_ioctl_vol_args
*vol_args
;
2416 struct btrfs_trans_handle
*trans
;
2422 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2427 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2431 namelen
= strlen(vol_args
->name
);
2432 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2437 sizestr
= vol_args
->name
;
2438 if (!strcmp(sizestr
, "max"))
2439 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2441 if (sizestr
[0] == '-') {
2444 } else if (sizestr
[0] == '+') {
2448 new_size
= btrfs_parse_size(sizestr
);
2449 if (new_size
== 0) {
2455 mutex_lock(&root
->fs_info
->fs_mutex
);
2456 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2459 if (new_size
> old_size
) {
2463 new_size
= old_size
- new_size
;
2464 } else if (mod
> 0) {
2465 new_size
= old_size
+ new_size
;
2468 if (new_size
< 256 * 1024 * 1024) {
2472 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2477 do_div(new_size
, root
->sectorsize
);
2478 new_size
*= root
->sectorsize
;
2480 printk("new size is %Lu\n", new_size
);
2481 if (new_size
> old_size
) {
2482 trans
= btrfs_start_transaction(root
, 1);
2483 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2484 btrfs_commit_transaction(trans
, root
);
2486 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2490 mutex_unlock(&root
->fs_info
->fs_mutex
);
2496 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
2499 struct btrfs_ioctl_vol_args
*vol_args
;
2500 struct btrfs_dir_item
*di
;
2501 struct btrfs_path
*path
;
2506 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2511 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2516 namelen
= strlen(vol_args
->name
);
2517 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2521 if (strchr(vol_args
->name
, '/')) {
2526 path
= btrfs_alloc_path();
2532 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2533 mutex_lock(&root
->fs_info
->fs_mutex
);
2534 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2536 vol_args
->name
, namelen
, 0);
2537 mutex_unlock(&root
->fs_info
->fs_mutex
);
2538 btrfs_free_path(path
);
2540 if (di
&& !IS_ERR(di
)) {
2550 if (root
== root
->fs_info
->tree_root
)
2551 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2553 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2559 static int btrfs_ioctl_defrag(struct file
*file
)
2561 struct inode
*inode
= fdentry(file
)->d_inode
;
2562 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2564 switch (inode
->i_mode
& S_IFMT
) {
2566 mutex_lock(&root
->fs_info
->fs_mutex
);
2567 btrfs_defrag_root(root
, 0);
2568 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2569 mutex_unlock(&root
->fs_info
->fs_mutex
);
2572 btrfs_defrag_file(file
);
2579 long btrfs_ioctl(struct file
*file
, unsigned int
2580 cmd
, unsigned long arg
)
2582 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2585 case BTRFS_IOC_SNAP_CREATE
:
2586 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2587 case BTRFS_IOC_DEFRAG
:
2588 return btrfs_ioctl_defrag(file
);
2589 case BTRFS_IOC_RESIZE
:
2590 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2597 * Called inside transaction, so use GFP_NOFS
2599 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2601 struct btrfs_inode
*ei
;
2603 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2607 ei
->ordered_trans
= 0;
2608 return &ei
->vfs_inode
;
2611 void btrfs_destroy_inode(struct inode
*inode
)
2613 WARN_ON(!list_empty(&inode
->i_dentry
));
2614 WARN_ON(inode
->i_data
.nrpages
);
2616 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
2617 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2620 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2621 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2623 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2624 unsigned long flags
)
2627 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2629 inode_init_once(&ei
->vfs_inode
);
2632 void btrfs_destroy_cachep(void)
2634 if (btrfs_inode_cachep
)
2635 kmem_cache_destroy(btrfs_inode_cachep
);
2636 if (btrfs_trans_handle_cachep
)
2637 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2638 if (btrfs_transaction_cachep
)
2639 kmem_cache_destroy(btrfs_transaction_cachep
);
2640 if (btrfs_bit_radix_cachep
)
2641 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2642 if (btrfs_path_cachep
)
2643 kmem_cache_destroy(btrfs_path_cachep
);
2646 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2647 unsigned long extra_flags
,
2648 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2649 void (*ctor
)(struct kmem_cache
*, void *)
2651 void (*ctor
)(void *, struct kmem_cache
*,
2656 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2657 SLAB_MEM_SPREAD
| extra_flags
), ctor
2658 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2664 int btrfs_init_cachep(void)
2666 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2667 sizeof(struct btrfs_inode
),
2669 if (!btrfs_inode_cachep
)
2671 btrfs_trans_handle_cachep
=
2672 btrfs_cache_create("btrfs_trans_handle_cache",
2673 sizeof(struct btrfs_trans_handle
),
2675 if (!btrfs_trans_handle_cachep
)
2677 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2678 sizeof(struct btrfs_transaction
),
2680 if (!btrfs_transaction_cachep
)
2682 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2683 sizeof(struct btrfs_path
),
2685 if (!btrfs_path_cachep
)
2687 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2688 SLAB_DESTROY_BY_RCU
, NULL
);
2689 if (!btrfs_bit_radix_cachep
)
2693 btrfs_destroy_cachep();
2697 static int btrfs_getattr(struct vfsmount
*mnt
,
2698 struct dentry
*dentry
, struct kstat
*stat
)
2700 struct inode
*inode
= dentry
->d_inode
;
2701 generic_fillattr(inode
, stat
);
2702 stat
->blksize
= PAGE_CACHE_SIZE
;
2706 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2707 struct inode
* new_dir
,struct dentry
*new_dentry
)
2709 struct btrfs_trans_handle
*trans
;
2710 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2711 struct inode
*new_inode
= new_dentry
->d_inode
;
2712 struct inode
*old_inode
= old_dentry
->d_inode
;
2713 struct timespec ctime
= CURRENT_TIME
;
2714 struct btrfs_path
*path
;
2717 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2718 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2722 mutex_lock(&root
->fs_info
->fs_mutex
);
2723 ret
= btrfs_check_free_space(root
, 1, 0);
2727 trans
= btrfs_start_transaction(root
, 1);
2729 btrfs_set_trans_block_group(trans
, new_dir
);
2730 path
= btrfs_alloc_path();
2736 old_dentry
->d_inode
->i_nlink
++;
2737 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2738 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2739 old_inode
->i_ctime
= ctime
;
2741 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2746 new_inode
->i_ctime
= CURRENT_TIME
;
2747 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2751 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2756 btrfs_free_path(path
);
2757 btrfs_end_transaction(trans
, root
);
2759 mutex_unlock(&root
->fs_info
->fs_mutex
);
2763 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2764 const char *symname
)
2766 struct btrfs_trans_handle
*trans
;
2767 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2768 struct btrfs_path
*path
;
2769 struct btrfs_key key
;
2770 struct inode
*inode
= NULL
;
2777 struct btrfs_file_extent_item
*ei
;
2778 struct extent_buffer
*leaf
;
2779 unsigned long nr
= 0;
2781 name_len
= strlen(symname
) + 1;
2782 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2783 return -ENAMETOOLONG
;
2785 mutex_lock(&root
->fs_info
->fs_mutex
);
2786 err
= btrfs_check_free_space(root
, 1, 0);
2790 trans
= btrfs_start_transaction(root
, 1);
2791 btrfs_set_trans_block_group(trans
, dir
);
2793 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2799 inode
= btrfs_new_inode(trans
, root
, objectid
,
2800 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2801 err
= PTR_ERR(inode
);
2805 btrfs_set_trans_block_group(trans
, inode
);
2806 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2810 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2811 inode
->i_fop
= &btrfs_file_operations
;
2812 inode
->i_op
= &btrfs_file_inode_operations
;
2813 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2814 inode
->i_mapping
, GFP_NOFS
);
2815 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2817 dir
->i_sb
->s_dirt
= 1;
2818 btrfs_update_inode_block_group(trans
, inode
);
2819 btrfs_update_inode_block_group(trans
, dir
);
2823 path
= btrfs_alloc_path();
2825 key
.objectid
= inode
->i_ino
;
2827 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2828 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2829 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2835 leaf
= path
->nodes
[0];
2836 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2837 struct btrfs_file_extent_item
);
2838 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2839 btrfs_set_file_extent_type(leaf
, ei
,
2840 BTRFS_FILE_EXTENT_INLINE
);
2841 ptr
= btrfs_file_extent_inline_start(ei
);
2842 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2843 btrfs_mark_buffer_dirty(leaf
);
2844 btrfs_free_path(path
);
2846 inode
->i_op
= &btrfs_symlink_inode_operations
;
2847 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2848 inode
->i_size
= name_len
- 1;
2849 err
= btrfs_update_inode(trans
, root
, inode
);
2854 nr
= trans
->blocks_used
;
2855 btrfs_end_transaction(trans
, root
);
2857 mutex_unlock(&root
->fs_info
->fs_mutex
);
2859 inode_dec_link_count(inode
);
2862 btrfs_btree_balance_dirty(root
, nr
);
2863 btrfs_throttle(root
);
2866 static int btrfs_permission(struct inode
*inode
, int mask
,
2867 struct nameidata
*nd
)
2869 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
2871 return generic_permission(inode
, mask
, NULL
);
2874 static struct inode_operations btrfs_dir_inode_operations
= {
2875 .lookup
= btrfs_lookup
,
2876 .create
= btrfs_create
,
2877 .unlink
= btrfs_unlink
,
2879 .mkdir
= btrfs_mkdir
,
2880 .rmdir
= btrfs_rmdir
,
2881 .rename
= btrfs_rename
,
2882 .symlink
= btrfs_symlink
,
2883 .setattr
= btrfs_setattr
,
2884 .mknod
= btrfs_mknod
,
2885 .setxattr
= generic_setxattr
,
2886 .getxattr
= generic_getxattr
,
2887 .listxattr
= btrfs_listxattr
,
2888 .removexattr
= generic_removexattr
,
2889 .permission
= btrfs_permission
,
2891 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2892 .lookup
= btrfs_lookup
,
2893 .permission
= btrfs_permission
,
2895 static struct file_operations btrfs_dir_file_operations
= {
2896 .llseek
= generic_file_llseek
,
2897 .read
= generic_read_dir
,
2898 .readdir
= btrfs_readdir
,
2899 .unlocked_ioctl
= btrfs_ioctl
,
2900 #ifdef CONFIG_COMPAT
2901 .compat_ioctl
= btrfs_ioctl
,
2905 static struct extent_map_ops btrfs_extent_map_ops
= {
2906 .fill_delalloc
= run_delalloc_range
,
2907 .writepage_io_hook
= btrfs_writepage_io_hook
,
2908 .readpage_io_hook
= btrfs_readpage_io_hook
,
2909 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2912 static struct address_space_operations btrfs_aops
= {
2913 .readpage
= btrfs_readpage
,
2914 .writepage
= btrfs_writepage
,
2915 .writepages
= btrfs_writepages
,
2916 .readpages
= btrfs_readpages
,
2917 .sync_page
= block_sync_page
,
2919 .invalidatepage
= btrfs_invalidatepage
,
2920 .releasepage
= btrfs_releasepage
,
2921 .set_page_dirty
= __set_page_dirty_nobuffers
,
2924 static struct address_space_operations btrfs_symlink_aops
= {
2925 .readpage
= btrfs_readpage
,
2926 .writepage
= btrfs_writepage
,
2927 .invalidatepage
= btrfs_invalidatepage
,
2928 .releasepage
= btrfs_releasepage
,
2931 static struct inode_operations btrfs_file_inode_operations
= {
2932 .truncate
= btrfs_truncate
,
2933 .getattr
= btrfs_getattr
,
2934 .setattr
= btrfs_setattr
,
2935 .setxattr
= generic_setxattr
,
2936 .getxattr
= generic_getxattr
,
2937 .listxattr
= btrfs_listxattr
,
2938 .removexattr
= generic_removexattr
,
2939 .permission
= btrfs_permission
,
2941 static struct inode_operations btrfs_special_inode_operations
= {
2942 .getattr
= btrfs_getattr
,
2943 .setattr
= btrfs_setattr
,
2944 .permission
= btrfs_permission
,
2946 static struct inode_operations btrfs_symlink_inode_operations
= {
2947 .readlink
= generic_readlink
,
2948 .follow_link
= page_follow_link_light
,
2949 .put_link
= page_put_link
,
2950 .permission
= btrfs_permission
,