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
);
92 spin_lock(&root
->fs_info
->delalloc_lock
);
93 if (used
+ root
->fs_info
->delalloc_bytes
+ num_required
> thresh
)
95 spin_unlock(&root
->fs_info
->delalloc_lock
);
99 static int cow_file_range(struct inode
*inode
, u64 start
, u64 end
)
101 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
102 struct btrfs_trans_handle
*trans
;
106 u64 blocksize
= root
->sectorsize
;
107 struct btrfs_key ins
;
110 trans
= btrfs_start_transaction(root
, 1);
112 btrfs_set_trans_block_group(trans
, inode
);
114 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
115 num_bytes
= max(blocksize
, num_bytes
);
116 ret
= btrfs_drop_extents(trans
, root
, inode
,
117 start
, start
+ num_bytes
, start
, &alloc_hint
);
119 if (alloc_hint
== EXTENT_MAP_INLINE
)
122 while(num_bytes
> 0) {
123 cur_alloc_size
= min(num_bytes
, root
->fs_info
->max_extent
);
124 ret
= btrfs_alloc_extent(trans
, root
, cur_alloc_size
,
125 root
->root_key
.objectid
,
127 inode
->i_ino
, start
, 0,
128 alloc_hint
, (u64
)-1, &ins
, 1);
133 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
134 start
, ins
.objectid
, ins
.offset
,
136 num_bytes
-= cur_alloc_size
;
137 alloc_hint
= ins
.objectid
+ ins
.offset
;
138 start
+= cur_alloc_size
;
140 btrfs_add_ordered_inode(inode
);
142 btrfs_end_transaction(trans
, root
);
146 static int run_delalloc_nocow(struct inode
*inode
, u64 start
, u64 end
)
154 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
155 struct extent_buffer
*leaf
;
157 struct btrfs_path
*path
;
158 struct btrfs_file_extent_item
*item
;
161 struct btrfs_key found_key
;
163 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
164 path
= btrfs_alloc_path();
167 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
168 inode
->i_ino
, start
, 0);
170 btrfs_free_path(path
);
176 if (path
->slots
[0] == 0)
181 leaf
= path
->nodes
[0];
182 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
183 struct btrfs_file_extent_item
);
185 /* are we inside the extent that was found? */
186 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
187 found_type
= btrfs_key_type(&found_key
);
188 if (found_key
.objectid
!= inode
->i_ino
||
189 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
193 found_type
= btrfs_file_extent_type(leaf
, item
);
194 extent_start
= found_key
.offset
;
195 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
196 u64 extent_num_bytes
;
198 extent_num_bytes
= btrfs_file_extent_num_bytes(leaf
, item
);
199 extent_end
= extent_start
+ extent_num_bytes
;
202 if (loops
&& start
!= extent_start
)
205 if (start
< extent_start
|| start
>= extent_end
)
208 cow_end
= min(end
, extent_end
- 1);
209 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
214 * we may be called by the resizer, make sure we're inside
215 * the limits of the FS
217 if (bytenr
+ extent_num_bytes
> total_fs_bytes
)
220 if (btrfs_count_snapshots_in_path(root
, path
, bytenr
) != 1) {
230 btrfs_free_path(path
);
233 btrfs_release_path(root
, path
);
238 cow_file_range(inode
, start
, cow_end
);
243 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
245 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
248 mutex_lock(&root
->fs_info
->fs_mutex
);
249 if (btrfs_test_opt(root
, NODATACOW
) ||
250 btrfs_test_flag(inode
, NODATACOW
))
251 ret
= run_delalloc_nocow(inode
, start
, end
);
253 ret
= cow_file_range(inode
, start
, end
);
255 spin_lock(&root
->fs_info
->delalloc_lock
);
256 num_bytes
= end
+ 1 - start
;
257 if (root
->fs_info
->delalloc_bytes
< num_bytes
) {
258 printk("delalloc accounting error total %llu sub %llu\n",
259 root
->fs_info
->delalloc_bytes
, num_bytes
);
261 root
->fs_info
->delalloc_bytes
-= num_bytes
;
263 spin_unlock(&root
->fs_info
->delalloc_lock
);
265 mutex_unlock(&root
->fs_info
->fs_mutex
);
269 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
271 struct inode
*inode
= page
->mapping
->host
;
272 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
273 struct btrfs_trans_handle
*trans
;
276 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
277 size_t offset
= start
- page_start
;
278 if (btrfs_test_opt(root
, NODATASUM
) ||
279 btrfs_test_flag(inode
, NODATASUM
))
281 mutex_lock(&root
->fs_info
->fs_mutex
);
282 trans
= btrfs_start_transaction(root
, 1);
283 btrfs_set_trans_block_group(trans
, inode
);
285 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
286 start
, kaddr
+ offset
, end
- start
+ 1);
288 ret
= btrfs_end_transaction(trans
, root
);
290 mutex_unlock(&root
->fs_info
->fs_mutex
);
294 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
297 struct inode
*inode
= page
->mapping
->host
;
298 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
299 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
300 struct btrfs_csum_item
*item
;
301 struct btrfs_path
*path
= NULL
;
303 if (btrfs_test_opt(root
, NODATASUM
) ||
304 btrfs_test_flag(inode
, NODATASUM
))
306 mutex_lock(&root
->fs_info
->fs_mutex
);
307 path
= btrfs_alloc_path();
308 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
311 /* a csum that isn't present is a preallocated region. */
312 if (ret
== -ENOENT
|| ret
== -EFBIG
)
317 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
319 set_state_private(em_tree
, start
, csum
);
322 btrfs_free_path(path
);
323 mutex_unlock(&root
->fs_info
->fs_mutex
);
327 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
329 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
330 struct inode
*inode
= page
->mapping
->host
;
331 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
335 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
338 if (btrfs_test_opt(root
, NODATASUM
) ||
339 btrfs_test_flag(inode
, NODATASUM
))
341 ret
= get_state_private(em_tree
, start
, &private);
342 local_irq_save(flags
);
343 kaddr
= kmap_atomic(page
, KM_IRQ0
);
347 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
348 btrfs_csum_final(csum
, (char *)&csum
);
349 if (csum
!= private) {
352 kunmap_atomic(kaddr
, KM_IRQ0
);
353 local_irq_restore(flags
);
357 printk("btrfs csum failed ino %lu off %llu\n",
358 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
359 memset(kaddr
+ offset
, 1, end
- start
+ 1);
360 flush_dcache_page(page
);
361 kunmap_atomic(kaddr
, KM_IRQ0
);
362 local_irq_restore(flags
);
366 void btrfs_read_locked_inode(struct inode
*inode
)
368 struct btrfs_path
*path
;
369 struct extent_buffer
*leaf
;
370 struct btrfs_inode_item
*inode_item
;
371 struct btrfs_inode_timespec
*tspec
;
372 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
373 struct btrfs_key location
;
374 u64 alloc_group_block
;
378 path
= btrfs_alloc_path();
380 mutex_lock(&root
->fs_info
->fs_mutex
);
381 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
383 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
387 leaf
= path
->nodes
[0];
388 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
389 struct btrfs_inode_item
);
391 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
392 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
393 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
394 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
395 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
397 tspec
= btrfs_inode_atime(inode_item
);
398 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
399 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
401 tspec
= btrfs_inode_mtime(inode_item
);
402 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
403 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
405 tspec
= btrfs_inode_ctime(inode_item
);
406 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
407 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
409 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
410 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
412 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
414 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
415 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
417 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
418 if (!BTRFS_I(inode
)->block_group
) {
419 BTRFS_I(inode
)->block_group
= btrfs_find_block_group(root
,
422 btrfs_free_path(path
);
425 mutex_unlock(&root
->fs_info
->fs_mutex
);
427 switch (inode
->i_mode
& S_IFMT
) {
429 inode
->i_mapping
->a_ops
= &btrfs_aops
;
430 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
431 inode
->i_fop
= &btrfs_file_operations
;
432 inode
->i_op
= &btrfs_file_inode_operations
;
435 inode
->i_fop
= &btrfs_dir_file_operations
;
436 if (root
== root
->fs_info
->tree_root
)
437 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
439 inode
->i_op
= &btrfs_dir_inode_operations
;
442 inode
->i_op
= &btrfs_symlink_inode_operations
;
443 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
446 init_special_inode(inode
, inode
->i_mode
, rdev
);
452 btrfs_release_path(root
, path
);
453 btrfs_free_path(path
);
454 mutex_unlock(&root
->fs_info
->fs_mutex
);
455 make_bad_inode(inode
);
458 static void fill_inode_item(struct extent_buffer
*leaf
,
459 struct btrfs_inode_item
*item
,
462 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
463 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
464 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
465 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
466 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
468 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
469 inode
->i_atime
.tv_sec
);
470 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
471 inode
->i_atime
.tv_nsec
);
473 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
474 inode
->i_mtime
.tv_sec
);
475 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
476 inode
->i_mtime
.tv_nsec
);
478 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
479 inode
->i_ctime
.tv_sec
);
480 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
481 inode
->i_ctime
.tv_nsec
);
483 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
484 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
485 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
486 btrfs_set_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
);
487 btrfs_set_inode_block_group(leaf
, item
,
488 BTRFS_I(inode
)->block_group
->key
.objectid
);
491 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
492 struct btrfs_root
*root
,
495 struct btrfs_inode_item
*inode_item
;
496 struct btrfs_path
*path
;
497 struct extent_buffer
*leaf
;
500 path
= btrfs_alloc_path();
502 ret
= btrfs_lookup_inode(trans
, root
, path
,
503 &BTRFS_I(inode
)->location
, 1);
510 leaf
= path
->nodes
[0];
511 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
512 struct btrfs_inode_item
);
514 fill_inode_item(leaf
, inode_item
, inode
);
515 btrfs_mark_buffer_dirty(leaf
);
516 btrfs_set_inode_last_trans(trans
, inode
);
519 btrfs_release_path(root
, path
);
520 btrfs_free_path(path
);
525 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
526 struct btrfs_root
*root
,
528 struct dentry
*dentry
)
530 struct btrfs_path
*path
;
531 const char *name
= dentry
->d_name
.name
;
532 int name_len
= dentry
->d_name
.len
;
534 struct extent_buffer
*leaf
;
535 struct btrfs_dir_item
*di
;
536 struct btrfs_key key
;
538 path
= btrfs_alloc_path();
544 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
554 leaf
= path
->nodes
[0];
555 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
556 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
559 btrfs_release_path(root
, path
);
561 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
562 key
.objectid
, name
, name_len
, -1);
571 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
573 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
574 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
575 dentry
->d_inode
->i_ino
,
576 dentry
->d_parent
->d_inode
->i_ino
);
578 printk("failed to delete reference to %.*s, "
579 "inode %lu parent %lu\n", name_len
, name
,
580 dentry
->d_inode
->i_ino
,
581 dentry
->d_parent
->d_inode
->i_ino
);
584 btrfs_free_path(path
);
586 dir
->i_size
-= name_len
* 2;
587 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
588 btrfs_update_inode(trans
, root
, dir
);
589 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
590 dentry
->d_inode
->i_nlink
--;
592 drop_nlink(dentry
->d_inode
);
594 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
595 dir
->i_sb
->s_dirt
= 1;
600 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
602 struct btrfs_root
*root
;
603 struct btrfs_trans_handle
*trans
;
604 struct inode
*inode
= dentry
->d_inode
;
606 unsigned long nr
= 0;
608 root
= BTRFS_I(dir
)->root
;
609 mutex_lock(&root
->fs_info
->fs_mutex
);
611 ret
= btrfs_check_free_space(root
, 1, 1);
615 trans
= btrfs_start_transaction(root
, 1);
617 btrfs_set_trans_block_group(trans
, dir
);
618 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
619 nr
= trans
->blocks_used
;
621 if (inode
->i_nlink
== 0) {
623 /* if the inode isn't linked anywhere,
624 * we don't need to worry about
627 found
= btrfs_del_ordered_inode(inode
);
629 atomic_dec(&inode
->i_count
);
633 btrfs_end_transaction(trans
, root
);
635 mutex_unlock(&root
->fs_info
->fs_mutex
);
636 btrfs_btree_balance_dirty(root
, nr
);
637 btrfs_throttle(root
);
641 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
643 struct inode
*inode
= dentry
->d_inode
;
646 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
647 struct btrfs_trans_handle
*trans
;
648 unsigned long nr
= 0;
650 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
653 mutex_lock(&root
->fs_info
->fs_mutex
);
654 ret
= btrfs_check_free_space(root
, 1, 1);
658 trans
= btrfs_start_transaction(root
, 1);
659 btrfs_set_trans_block_group(trans
, dir
);
661 /* now the directory is empty */
662 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
667 nr
= trans
->blocks_used
;
668 ret
= btrfs_end_transaction(trans
, root
);
670 mutex_unlock(&root
->fs_info
->fs_mutex
);
671 btrfs_btree_balance_dirty(root
, nr
);
672 btrfs_throttle(root
);
679 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
680 struct btrfs_root
*root
,
683 struct btrfs_path
*path
;
688 path
= btrfs_alloc_path();
690 ret
= btrfs_lookup_inode(trans
, root
, path
,
691 &BTRFS_I(inode
)->location
, -1);
695 ret
= btrfs_del_item(trans
, root
, path
);
696 btrfs_free_path(path
);
701 * this can truncate away extent items, csum items and directory items.
702 * It starts at a high offset and removes keys until it can't find
703 * any higher than i_size.
705 * csum items that cross the new i_size are truncated to the new size
708 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
709 struct btrfs_root
*root
,
713 struct btrfs_path
*path
;
714 struct btrfs_key key
;
715 struct btrfs_key found_key
;
717 struct extent_buffer
*leaf
;
718 struct btrfs_file_extent_item
*fi
;
719 u64 extent_start
= 0;
720 u64 extent_num_bytes
= 0;
726 int extent_type
= -1;
728 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
729 path
= btrfs_alloc_path();
733 /* FIXME, add redo link to tree so we don't leak on crash */
734 key
.objectid
= inode
->i_ino
;
735 key
.offset
= (u64
)-1;
739 btrfs_init_path(path
);
741 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
746 BUG_ON(path
->slots
[0] == 0);
749 leaf
= path
->nodes
[0];
750 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
751 found_type
= btrfs_key_type(&found_key
);
753 if (found_key
.objectid
!= inode
->i_ino
)
756 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
757 found_type
!= BTRFS_DIR_ITEM_KEY
&&
758 found_type
!= BTRFS_DIR_INDEX_KEY
&&
759 found_type
!= BTRFS_EXTENT_DATA_KEY
)
762 item_end
= found_key
.offset
;
763 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
764 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
765 struct btrfs_file_extent_item
);
766 extent_type
= btrfs_file_extent_type(leaf
, fi
);
767 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
769 btrfs_file_extent_num_bytes(leaf
, fi
);
770 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
771 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
773 item_end
+= btrfs_file_extent_inline_len(leaf
,
778 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
779 ret
= btrfs_csum_truncate(trans
, root
, path
,
783 if (item_end
< inode
->i_size
) {
784 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
785 found_type
= BTRFS_INODE_ITEM_KEY
;
786 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
787 found_type
= BTRFS_CSUM_ITEM_KEY
;
788 } else if (found_type
) {
793 btrfs_set_key_type(&key
, found_type
);
794 btrfs_release_path(root
, path
);
797 if (found_key
.offset
>= inode
->i_size
)
803 /* FIXME, shrink the extent if the ref count is only 1 */
804 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
807 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
809 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
812 btrfs_file_extent_num_bytes(leaf
, fi
);
813 extent_num_bytes
= inode
->i_size
-
814 found_key
.offset
+ root
->sectorsize
- 1;
815 btrfs_set_file_extent_num_bytes(leaf
, fi
,
817 num_dec
= (orig_num_bytes
-
818 extent_num_bytes
) >> 9;
819 if (extent_start
!= 0) {
820 inode
->i_blocks
-= num_dec
;
822 btrfs_mark_buffer_dirty(leaf
);
825 btrfs_file_extent_disk_num_bytes(leaf
,
827 /* FIXME blocksize != 4096 */
828 num_dec
= btrfs_file_extent_num_bytes(leaf
,
830 if (extent_start
!= 0) {
832 inode
->i_blocks
-= num_dec
;
834 root_gen
= btrfs_header_generation(leaf
);
835 root_owner
= btrfs_header_owner(leaf
);
837 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
839 u32 newsize
= inode
->i_size
- found_key
.offset
;
840 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
841 ret
= btrfs_truncate_item(trans
, root
, path
,
847 ret
= btrfs_del_item(trans
, root
, path
);
853 btrfs_release_path(root
, path
);
855 ret
= btrfs_free_extent(trans
, root
, extent_start
,
858 root_gen
, inode
->i_ino
,
859 found_key
.offset
, 0);
865 btrfs_release_path(root
, path
);
866 btrfs_free_path(path
);
867 inode
->i_sb
->s_dirt
= 1;
871 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
875 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
876 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
877 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
878 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
879 u64 existing_delalloc
;
883 WARN_ON(!PageLocked(page
));
884 set_page_extent_mapped(page
);
886 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
887 delalloc_start
= page_start
;
888 existing_delalloc
= count_range_bits(&BTRFS_I(inode
)->extent_tree
,
889 &delalloc_start
, page_end
,
890 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
891 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
894 spin_lock(&root
->fs_info
->delalloc_lock
);
895 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
- existing_delalloc
;
896 spin_unlock(&root
->fs_info
->delalloc_lock
);
898 if (zero_start
!= PAGE_CACHE_SIZE
) {
900 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
901 flush_dcache_page(page
);
904 set_page_dirty(page
);
905 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
911 * taken from block_truncate_page, but does cow as it zeros out
912 * any bytes left in the last page in the file.
914 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
916 struct inode
*inode
= mapping
->host
;
917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
918 u32 blocksize
= root
->sectorsize
;
919 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
920 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
925 if ((offset
& (blocksize
- 1)) == 0)
929 page
= grab_cache_page(mapping
, index
);
932 if (!PageUptodate(page
)) {
933 ret
= btrfs_readpage(NULL
, page
);
935 if (!PageUptodate(page
)) {
940 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
942 ret
= btrfs_cow_one_page(inode
, page
, offset
);
945 page_cache_release(page
);
950 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
952 struct inode
*inode
= dentry
->d_inode
;
955 err
= inode_change_ok(inode
, attr
);
959 if (S_ISREG(inode
->i_mode
) &&
960 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
961 struct btrfs_trans_handle
*trans
;
962 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
963 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
965 u64 mask
= root
->sectorsize
- 1;
966 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
967 u64 block_end
= attr
->ia_size
| mask
;
971 if (attr
->ia_size
<= pos
)
974 mutex_lock(&root
->fs_info
->fs_mutex
);
975 err
= btrfs_check_free_space(root
, 1, 0);
976 mutex_unlock(&root
->fs_info
->fs_mutex
);
980 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
982 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
983 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
985 mutex_lock(&root
->fs_info
->fs_mutex
);
986 trans
= btrfs_start_transaction(root
, 1);
987 btrfs_set_trans_block_group(trans
, inode
);
988 err
= btrfs_drop_extents(trans
, root
, inode
,
989 pos
, pos
+ hole_size
, pos
,
992 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
993 err
= btrfs_insert_file_extent(trans
, root
,
995 pos
, 0, 0, hole_size
);
997 btrfs_end_transaction(trans
, root
);
998 mutex_unlock(&root
->fs_info
->fs_mutex
);
999 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
1004 err
= inode_setattr(inode
, attr
);
1009 void btrfs_put_inode(struct inode
*inode
)
1013 if (!BTRFS_I(inode
)->ordered_trans
) {
1017 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_DIRTY
) ||
1018 mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1021 ret
= btrfs_del_ordered_inode(inode
);
1023 atomic_dec(&inode
->i_count
);
1027 void btrfs_delete_inode(struct inode
*inode
)
1029 struct btrfs_trans_handle
*trans
;
1030 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1034 truncate_inode_pages(&inode
->i_data
, 0);
1035 if (is_bad_inode(inode
)) {
1040 mutex_lock(&root
->fs_info
->fs_mutex
);
1041 trans
= btrfs_start_transaction(root
, 1);
1043 btrfs_set_trans_block_group(trans
, inode
);
1044 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1046 goto no_delete_lock
;
1047 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
1049 goto no_delete_lock
;
1050 ret
= btrfs_free_inode(trans
, root
, inode
);
1052 goto no_delete_lock
;
1053 nr
= trans
->blocks_used
;
1055 btrfs_end_transaction(trans
, root
);
1056 mutex_unlock(&root
->fs_info
->fs_mutex
);
1057 btrfs_btree_balance_dirty(root
, nr
);
1058 btrfs_throttle(root
);
1062 nr
= trans
->blocks_used
;
1063 btrfs_end_transaction(trans
, root
);
1064 mutex_unlock(&root
->fs_info
->fs_mutex
);
1065 btrfs_btree_balance_dirty(root
, nr
);
1066 btrfs_throttle(root
);
1072 * this returns the key found in the dir entry in the location pointer.
1073 * If no dir entries were found, location->objectid is 0.
1075 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
1076 struct btrfs_key
*location
)
1078 const char *name
= dentry
->d_name
.name
;
1079 int namelen
= dentry
->d_name
.len
;
1080 struct btrfs_dir_item
*di
;
1081 struct btrfs_path
*path
;
1082 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1085 if (namelen
== 1 && strcmp(name
, ".") == 0) {
1086 location
->objectid
= dir
->i_ino
;
1087 location
->type
= BTRFS_INODE_ITEM_KEY
;
1088 location
->offset
= 0;
1091 path
= btrfs_alloc_path();
1094 if (namelen
== 2 && strcmp(name
, "..") == 0) {
1095 struct btrfs_key key
;
1096 struct extent_buffer
*leaf
;
1100 key
.objectid
= dir
->i_ino
;
1101 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1103 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1107 leaf
= path
->nodes
[0];
1108 slot
= path
->slots
[0];
1109 nritems
= btrfs_header_nritems(leaf
);
1110 if (slot
>= nritems
)
1113 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1114 if (key
.objectid
!= dir
->i_ino
||
1115 key
.type
!= BTRFS_INODE_REF_KEY
) {
1118 location
->objectid
= key
.offset
;
1119 location
->type
= BTRFS_INODE_ITEM_KEY
;
1120 location
->offset
= 0;
1124 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
1128 if (!di
|| IS_ERR(di
)) {
1131 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
1133 btrfs_free_path(path
);
1136 location
->objectid
= 0;
1141 * when we hit a tree root in a directory, the btrfs part of the inode
1142 * needs to be changed to reflect the root directory of the tree root. This
1143 * is kind of like crossing a mount point.
1145 static int fixup_tree_root_location(struct btrfs_root
*root
,
1146 struct btrfs_key
*location
,
1147 struct btrfs_root
**sub_root
,
1148 struct dentry
*dentry
)
1150 struct btrfs_path
*path
;
1151 struct btrfs_root_item
*ri
;
1153 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
1155 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
1158 path
= btrfs_alloc_path();
1160 mutex_lock(&root
->fs_info
->fs_mutex
);
1162 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
1163 dentry
->d_name
.name
,
1164 dentry
->d_name
.len
);
1165 if (IS_ERR(*sub_root
))
1166 return PTR_ERR(*sub_root
);
1168 ri
= &(*sub_root
)->root_item
;
1169 location
->objectid
= btrfs_root_dirid(ri
);
1170 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1171 location
->offset
= 0;
1173 btrfs_free_path(path
);
1174 mutex_unlock(&root
->fs_info
->fs_mutex
);
1178 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
1180 struct btrfs_iget_args
*args
= p
;
1181 inode
->i_ino
= args
->ino
;
1182 BTRFS_I(inode
)->root
= args
->root
;
1183 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1184 inode
->i_mapping
, GFP_NOFS
);
1188 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
1190 struct btrfs_iget_args
*args
= opaque
;
1191 return (args
->ino
== inode
->i_ino
&&
1192 args
->root
== BTRFS_I(inode
)->root
);
1195 struct inode
*btrfs_ilookup(struct super_block
*s
, u64 objectid
,
1198 struct btrfs_iget_args args
;
1199 args
.ino
= objectid
;
1200 args
.root
= btrfs_lookup_fs_root(btrfs_sb(s
)->fs_info
, root_objectid
);
1205 return ilookup5(s
, objectid
, btrfs_find_actor
, (void *)&args
);
1208 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
1209 struct btrfs_root
*root
)
1211 struct inode
*inode
;
1212 struct btrfs_iget_args args
;
1213 args
.ino
= objectid
;
1216 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
1217 btrfs_init_locked_inode
,
1222 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
1223 struct nameidata
*nd
)
1225 struct inode
* inode
;
1226 struct btrfs_inode
*bi
= BTRFS_I(dir
);
1227 struct btrfs_root
*root
= bi
->root
;
1228 struct btrfs_root
*sub_root
= root
;
1229 struct btrfs_key location
;
1232 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
1233 return ERR_PTR(-ENAMETOOLONG
);
1235 mutex_lock(&root
->fs_info
->fs_mutex
);
1236 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
1237 mutex_unlock(&root
->fs_info
->fs_mutex
);
1240 return ERR_PTR(ret
);
1243 if (location
.objectid
) {
1244 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1247 return ERR_PTR(ret
);
1249 return ERR_PTR(-ENOENT
);
1250 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1253 return ERR_PTR(-EACCES
);
1254 if (inode
->i_state
& I_NEW
) {
1255 /* the inode and parent dir are two different roots */
1256 if (sub_root
!= root
) {
1258 sub_root
->inode
= inode
;
1260 BTRFS_I(inode
)->root
= sub_root
;
1261 memcpy(&BTRFS_I(inode
)->location
, &location
,
1263 btrfs_read_locked_inode(inode
);
1264 unlock_new_inode(inode
);
1267 return d_splice_alias(inode
, dentry
);
1270 static unsigned char btrfs_filetype_table
[] = {
1271 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1274 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1276 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1277 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1278 struct btrfs_item
*item
;
1279 struct btrfs_dir_item
*di
;
1280 struct btrfs_key key
;
1281 struct btrfs_key found_key
;
1282 struct btrfs_path
*path
;
1285 struct extent_buffer
*leaf
;
1288 unsigned char d_type
;
1293 int key_type
= BTRFS_DIR_INDEX_KEY
;
1298 /* FIXME, use a real flag for deciding about the key type */
1299 if (root
->fs_info
->tree_root
== root
)
1300 key_type
= BTRFS_DIR_ITEM_KEY
;
1302 /* special case for "." */
1303 if (filp
->f_pos
== 0) {
1304 over
= filldir(dirent
, ".", 1,
1312 mutex_lock(&root
->fs_info
->fs_mutex
);
1313 key
.objectid
= inode
->i_ino
;
1314 path
= btrfs_alloc_path();
1317 /* special case for .., just use the back ref */
1318 if (filp
->f_pos
== 1) {
1319 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1321 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1323 leaf
= path
->nodes
[0];
1324 slot
= path
->slots
[0];
1325 nritems
= btrfs_header_nritems(leaf
);
1326 if (slot
>= nritems
) {
1327 btrfs_release_path(root
, path
);
1328 goto read_dir_items
;
1330 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1331 btrfs_release_path(root
, path
);
1332 if (found_key
.objectid
!= key
.objectid
||
1333 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1334 goto read_dir_items
;
1335 over
= filldir(dirent
, "..", 2,
1336 2, found_key
.offset
, DT_DIR
);
1343 btrfs_set_key_type(&key
, key_type
);
1344 key
.offset
= filp
->f_pos
;
1346 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1351 leaf
= path
->nodes
[0];
1352 nritems
= btrfs_header_nritems(leaf
);
1353 slot
= path
->slots
[0];
1354 if (advance
|| slot
>= nritems
) {
1355 if (slot
>= nritems
-1) {
1356 ret
= btrfs_next_leaf(root
, path
);
1359 leaf
= path
->nodes
[0];
1360 nritems
= btrfs_header_nritems(leaf
);
1361 slot
= path
->slots
[0];
1368 item
= btrfs_item_nr(leaf
, slot
);
1369 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1371 if (found_key
.objectid
!= key
.objectid
)
1373 if (btrfs_key_type(&found_key
) != key_type
)
1375 if (found_key
.offset
< filp
->f_pos
)
1378 filp
->f_pos
= found_key
.offset
;
1380 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1382 di_total
= btrfs_item_size(leaf
, item
);
1383 while(di_cur
< di_total
) {
1384 struct btrfs_key location
;
1386 name_len
= btrfs_dir_name_len(leaf
, di
);
1387 if (name_len
< 32) {
1388 name_ptr
= tmp_name
;
1390 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1393 read_extent_buffer(leaf
, name_ptr
,
1394 (unsigned long)(di
+ 1), name_len
);
1396 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1397 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1398 over
= filldir(dirent
, name_ptr
, name_len
,
1403 if (name_ptr
!= tmp_name
)
1408 di_len
= btrfs_dir_name_len(leaf
, di
) +
1409 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1411 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1418 btrfs_release_path(root
, path
);
1419 btrfs_free_path(path
);
1420 mutex_unlock(&root
->fs_info
->fs_mutex
);
1424 int btrfs_write_inode(struct inode
*inode
, int wait
)
1426 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1427 struct btrfs_trans_handle
*trans
;
1431 mutex_lock(&root
->fs_info
->fs_mutex
);
1432 trans
= btrfs_start_transaction(root
, 1);
1433 btrfs_set_trans_block_group(trans
, inode
);
1434 ret
= btrfs_commit_transaction(trans
, root
);
1435 mutex_unlock(&root
->fs_info
->fs_mutex
);
1441 * This is somewhat expensive, updating the tree every time the
1442 * inode changes. But, it is most likely to find the inode in cache.
1443 * FIXME, needs more benchmarking...there are no reasons other than performance
1444 * to keep or drop this code.
1446 void btrfs_dirty_inode(struct inode
*inode
)
1448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1449 struct btrfs_trans_handle
*trans
;
1451 mutex_lock(&root
->fs_info
->fs_mutex
);
1452 trans
= btrfs_start_transaction(root
, 1);
1453 btrfs_set_trans_block_group(trans
, inode
);
1454 btrfs_update_inode(trans
, root
, inode
);
1455 btrfs_end_transaction(trans
, root
);
1456 mutex_unlock(&root
->fs_info
->fs_mutex
);
1459 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1460 struct btrfs_root
*root
,
1462 struct btrfs_block_group_cache
*group
,
1465 struct inode
*inode
;
1466 struct btrfs_inode_item
*inode_item
;
1467 struct btrfs_key
*location
;
1468 struct btrfs_path
*path
;
1472 path
= btrfs_alloc_path();
1475 inode
= new_inode(root
->fs_info
->sb
);
1477 return ERR_PTR(-ENOMEM
);
1479 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1480 inode
->i_mapping
, GFP_NOFS
);
1481 BTRFS_I(inode
)->root
= root
;
1487 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1488 BTRFS_I(inode
)->block_group
= group
;
1489 BTRFS_I(inode
)->flags
= 0;
1490 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1494 inode
->i_uid
= current
->fsuid
;
1495 inode
->i_gid
= current
->fsgid
;
1496 inode
->i_mode
= mode
;
1497 inode
->i_ino
= objectid
;
1498 inode
->i_blocks
= 0;
1499 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1500 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1501 struct btrfs_inode_item
);
1502 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1503 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1504 btrfs_free_path(path
);
1506 location
= &BTRFS_I(inode
)->location
;
1507 location
->objectid
= objectid
;
1508 location
->offset
= 0;
1509 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1511 insert_inode_hash(inode
);
1514 btrfs_free_path(path
);
1515 return ERR_PTR(ret
);
1518 static inline u8
btrfs_inode_type(struct inode
*inode
)
1520 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1523 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1524 struct dentry
*dentry
, struct inode
*inode
)
1527 struct btrfs_key key
;
1528 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1529 struct inode
*parent_inode
;
1531 key
.objectid
= inode
->i_ino
;
1532 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1535 ret
= btrfs_insert_dir_item(trans
, root
,
1536 dentry
->d_name
.name
, dentry
->d_name
.len
,
1537 dentry
->d_parent
->d_inode
->i_ino
,
1538 &key
, btrfs_inode_type(inode
));
1540 ret
= btrfs_insert_inode_ref(trans
, root
,
1541 dentry
->d_name
.name
,
1544 dentry
->d_parent
->d_inode
->i_ino
);
1545 parent_inode
= dentry
->d_parent
->d_inode
;
1546 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1547 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1548 ret
= btrfs_update_inode(trans
, root
,
1549 dentry
->d_parent
->d_inode
);
1554 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1555 struct dentry
*dentry
, struct inode
*inode
)
1557 int err
= btrfs_add_link(trans
, dentry
, inode
);
1559 d_instantiate(dentry
, inode
);
1567 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1568 int mode
, dev_t rdev
)
1570 struct btrfs_trans_handle
*trans
;
1571 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1572 struct inode
*inode
= NULL
;
1576 unsigned long nr
= 0;
1578 if (!new_valid_dev(rdev
))
1581 mutex_lock(&root
->fs_info
->fs_mutex
);
1582 err
= btrfs_check_free_space(root
, 1, 0);
1586 trans
= btrfs_start_transaction(root
, 1);
1587 btrfs_set_trans_block_group(trans
, dir
);
1589 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1595 inode
= btrfs_new_inode(trans
, root
, objectid
,
1596 BTRFS_I(dir
)->block_group
, mode
);
1597 err
= PTR_ERR(inode
);
1601 btrfs_set_trans_block_group(trans
, inode
);
1602 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1606 inode
->i_op
= &btrfs_special_inode_operations
;
1607 init_special_inode(inode
, inode
->i_mode
, rdev
);
1608 btrfs_update_inode(trans
, root
, inode
);
1610 dir
->i_sb
->s_dirt
= 1;
1611 btrfs_update_inode_block_group(trans
, inode
);
1612 btrfs_update_inode_block_group(trans
, dir
);
1614 nr
= trans
->blocks_used
;
1615 btrfs_end_transaction(trans
, root
);
1617 mutex_unlock(&root
->fs_info
->fs_mutex
);
1620 inode_dec_link_count(inode
);
1623 btrfs_btree_balance_dirty(root
, nr
);
1624 btrfs_throttle(root
);
1628 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1629 int mode
, struct nameidata
*nd
)
1631 struct btrfs_trans_handle
*trans
;
1632 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1633 struct inode
*inode
= NULL
;
1636 unsigned long nr
= 0;
1639 mutex_lock(&root
->fs_info
->fs_mutex
);
1640 err
= btrfs_check_free_space(root
, 1, 0);
1643 trans
= btrfs_start_transaction(root
, 1);
1644 btrfs_set_trans_block_group(trans
, dir
);
1646 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1652 inode
= btrfs_new_inode(trans
, root
, objectid
,
1653 BTRFS_I(dir
)->block_group
, mode
);
1654 err
= PTR_ERR(inode
);
1658 btrfs_set_trans_block_group(trans
, inode
);
1659 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1663 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1664 inode
->i_fop
= &btrfs_file_operations
;
1665 inode
->i_op
= &btrfs_file_inode_operations
;
1666 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1667 inode
->i_mapping
, GFP_NOFS
);
1668 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1670 dir
->i_sb
->s_dirt
= 1;
1671 btrfs_update_inode_block_group(trans
, inode
);
1672 btrfs_update_inode_block_group(trans
, dir
);
1674 nr
= trans
->blocks_used
;
1675 btrfs_end_transaction(trans
, root
);
1677 mutex_unlock(&root
->fs_info
->fs_mutex
);
1680 inode_dec_link_count(inode
);
1683 btrfs_btree_balance_dirty(root
, nr
);
1684 btrfs_throttle(root
);
1688 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1689 struct dentry
*dentry
)
1691 struct btrfs_trans_handle
*trans
;
1692 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1693 struct inode
*inode
= old_dentry
->d_inode
;
1694 unsigned long nr
= 0;
1698 if (inode
->i_nlink
== 0)
1701 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1706 mutex_lock(&root
->fs_info
->fs_mutex
);
1707 err
= btrfs_check_free_space(root
, 1, 0);
1710 trans
= btrfs_start_transaction(root
, 1);
1712 btrfs_set_trans_block_group(trans
, dir
);
1713 atomic_inc(&inode
->i_count
);
1714 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1719 dir
->i_sb
->s_dirt
= 1;
1720 btrfs_update_inode_block_group(trans
, dir
);
1721 err
= btrfs_update_inode(trans
, root
, inode
);
1726 nr
= trans
->blocks_used
;
1727 btrfs_end_transaction(trans
, root
);
1729 mutex_unlock(&root
->fs_info
->fs_mutex
);
1732 inode_dec_link_count(inode
);
1735 btrfs_btree_balance_dirty(root
, nr
);
1736 btrfs_throttle(root
);
1740 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1742 struct inode
*inode
;
1743 struct btrfs_trans_handle
*trans
;
1744 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1746 int drop_on_err
= 0;
1748 unsigned long nr
= 1;
1750 mutex_lock(&root
->fs_info
->fs_mutex
);
1751 err
= btrfs_check_free_space(root
, 1, 0);
1755 trans
= btrfs_start_transaction(root
, 1);
1756 btrfs_set_trans_block_group(trans
, dir
);
1758 if (IS_ERR(trans
)) {
1759 err
= PTR_ERR(trans
);
1763 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1769 inode
= btrfs_new_inode(trans
, root
, objectid
,
1770 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1771 if (IS_ERR(inode
)) {
1772 err
= PTR_ERR(inode
);
1777 inode
->i_op
= &btrfs_dir_inode_operations
;
1778 inode
->i_fop
= &btrfs_dir_file_operations
;
1779 btrfs_set_trans_block_group(trans
, inode
);
1782 err
= btrfs_update_inode(trans
, root
, inode
);
1786 err
= btrfs_add_link(trans
, dentry
, inode
);
1790 d_instantiate(dentry
, inode
);
1792 dir
->i_sb
->s_dirt
= 1;
1793 btrfs_update_inode_block_group(trans
, inode
);
1794 btrfs_update_inode_block_group(trans
, dir
);
1797 nr
= trans
->blocks_used
;
1798 btrfs_end_transaction(trans
, root
);
1801 mutex_unlock(&root
->fs_info
->fs_mutex
);
1804 btrfs_btree_balance_dirty(root
, nr
);
1805 btrfs_throttle(root
);
1809 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1810 size_t page_offset
, u64 start
, u64 end
,
1816 u64 extent_start
= 0;
1818 u64 objectid
= inode
->i_ino
;
1820 int failed_insert
= 0;
1821 struct btrfs_path
*path
;
1822 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1823 struct btrfs_file_extent_item
*item
;
1824 struct extent_buffer
*leaf
;
1825 struct btrfs_key found_key
;
1826 struct extent_map
*em
= NULL
;
1827 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1828 struct btrfs_trans_handle
*trans
= NULL
;
1830 path
= btrfs_alloc_path();
1832 mutex_lock(&root
->fs_info
->fs_mutex
);
1835 em
= lookup_extent_mapping(em_tree
, start
, end
);
1837 if (em
->start
> start
) {
1838 printk("get_extent start %Lu em start %Lu\n",
1845 em
= alloc_extent_map(GFP_NOFS
);
1850 em
->start
= EXTENT_MAP_HOLE
;
1851 em
->end
= EXTENT_MAP_HOLE
;
1853 em
->bdev
= inode
->i_sb
->s_bdev
;
1854 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1855 objectid
, start
, trans
!= NULL
);
1862 if (path
->slots
[0] == 0)
1867 leaf
= path
->nodes
[0];
1868 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1869 struct btrfs_file_extent_item
);
1870 /* are we inside the extent that was found? */
1871 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1872 found_type
= btrfs_key_type(&found_key
);
1873 if (found_key
.objectid
!= objectid
||
1874 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1878 found_type
= btrfs_file_extent_type(leaf
, item
);
1879 extent_start
= found_key
.offset
;
1880 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1881 extent_end
= extent_start
+
1882 btrfs_file_extent_num_bytes(leaf
, item
);
1884 if (start
< extent_start
|| start
>= extent_end
) {
1886 if (start
< extent_start
) {
1887 if (end
< extent_start
)
1889 em
->end
= extent_end
- 1;
1895 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1897 em
->start
= extent_start
;
1898 em
->end
= extent_end
- 1;
1899 em
->block_start
= EXTENT_MAP_HOLE
;
1900 em
->block_end
= EXTENT_MAP_HOLE
;
1903 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1904 em
->block_start
= bytenr
;
1905 em
->block_end
= em
->block_start
+
1906 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1907 em
->start
= extent_start
;
1908 em
->end
= extent_end
- 1;
1910 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1914 size_t extent_offset
;
1917 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1919 extent_end
= (extent_start
+ size
- 1) |
1920 ((u64
)root
->sectorsize
- 1);
1921 if (start
< extent_start
|| start
>= extent_end
) {
1923 if (start
< extent_start
) {
1924 if (end
< extent_start
)
1926 em
->end
= extent_end
;
1932 em
->block_start
= EXTENT_MAP_INLINE
;
1933 em
->block_end
= EXTENT_MAP_INLINE
;
1936 em
->start
= extent_start
;
1937 em
->end
= extent_start
+ size
- 1;
1941 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1942 extent_start
+ page_offset
;
1943 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1944 size
- extent_offset
);
1945 em
->start
= extent_start
+ extent_offset
;
1946 em
->end
= (em
->start
+ copy_size
-1) |
1947 ((u64
)root
->sectorsize
-1);
1949 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1950 if (create
== 0 && !PageUptodate(page
)) {
1951 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1953 flush_dcache_page(page
);
1954 } else if (create
&& PageUptodate(page
)) {
1957 free_extent_map(em
);
1959 btrfs_release_path(root
, path
);
1960 trans
= btrfs_start_transaction(root
, 1);
1963 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1965 btrfs_mark_buffer_dirty(leaf
);
1968 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1971 printk("unkknown found_type %d\n", found_type
);
1978 em
->block_start
= EXTENT_MAP_HOLE
;
1979 em
->block_end
= EXTENT_MAP_HOLE
;
1981 btrfs_release_path(root
, path
);
1982 if (em
->start
> start
|| em
->end
< start
) {
1983 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1987 ret
= add_extent_mapping(em_tree
, em
);
1988 if (ret
== -EEXIST
) {
1989 free_extent_map(em
);
1991 if (0 && failed_insert
== 1) {
1992 btrfs_drop_extent_cache(inode
, start
, end
);
1995 if (failed_insert
> 5) {
1996 printk("failing to insert %Lu %Lu\n", start
, end
);
2004 btrfs_free_path(path
);
2006 ret
= btrfs_end_transaction(trans
, root
);
2010 mutex_unlock(&root
->fs_info
->fs_mutex
);
2012 free_extent_map(em
);
2014 return ERR_PTR(err
);
2019 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
2021 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
2024 int btrfs_readpage(struct file
*file
, struct page
*page
)
2026 struct extent_map_tree
*tree
;
2027 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2028 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
2031 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
2033 struct extent_map_tree
*tree
;
2036 if (current
->flags
& PF_MEMALLOC
) {
2037 redirty_page_for_writepage(wbc
, page
);
2041 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2042 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
2045 static int btrfs_writepages(struct address_space
*mapping
,
2046 struct writeback_control
*wbc
)
2048 struct extent_map_tree
*tree
;
2049 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
2050 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
2054 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
2055 struct list_head
*pages
, unsigned nr_pages
)
2057 struct extent_map_tree
*tree
;
2058 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
2059 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
2063 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
2065 struct extent_map_tree
*tree
;
2068 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2069 ret
= try_release_extent_mapping(tree
, page
);
2071 ClearPagePrivate(page
);
2072 set_page_private(page
, 0);
2073 page_cache_release(page
);
2078 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
2080 struct extent_map_tree
*tree
;
2082 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
2083 extent_invalidatepage(tree
, page
, offset
);
2084 btrfs_releasepage(page
, GFP_NOFS
);
2088 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2089 * called from a page fault handler when a page is first dirtied. Hence we must
2090 * be careful to check for EOF conditions here. We set the page up correctly
2091 * for a written page which means we get ENOSPC checking when writing into
2092 * holes and correct delalloc and unwritten extent mapping on filesystems that
2093 * support these features.
2095 * We are not allowed to take the i_mutex here so we have to play games to
2096 * protect against truncate races as the page could now be beyond EOF. Because
2097 * vmtruncate() writes the inode size before removing pages, once we have the
2098 * page lock we can determine safely if the page is beyond EOF. If it is not
2099 * beyond EOF, then the page is guaranteed safe against truncation until we
2102 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
2104 struct inode
*inode
= fdentry(vma
->vm_file
)->d_inode
;
2105 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2111 mutex_lock(&root
->fs_info
->fs_mutex
);
2112 ret
= btrfs_check_free_space(root
, PAGE_CACHE_SIZE
, 0);
2113 mutex_unlock(&root
->fs_info
->fs_mutex
);
2120 wait_on_page_writeback(page
);
2121 size
= i_size_read(inode
);
2122 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2124 if ((page
->mapping
!= inode
->i_mapping
) ||
2125 (page_start
> size
)) {
2126 /* page got truncated out from underneath us */
2130 /* page is wholly or partially inside EOF */
2131 if (page_start
+ PAGE_CACHE_SIZE
> size
)
2132 end
= size
& ~PAGE_CACHE_MASK
;
2134 end
= PAGE_CACHE_SIZE
;
2136 ret
= btrfs_cow_one_page(inode
, page
, end
);
2144 static void btrfs_truncate(struct inode
*inode
)
2146 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2148 struct btrfs_trans_handle
*trans
;
2151 if (!S_ISREG(inode
->i_mode
))
2153 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2156 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
2158 mutex_lock(&root
->fs_info
->fs_mutex
);
2159 trans
= btrfs_start_transaction(root
, 1);
2160 btrfs_set_trans_block_group(trans
, inode
);
2162 /* FIXME, add redo link to tree so we don't leak on crash */
2163 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
2164 btrfs_update_inode(trans
, root
, inode
);
2165 nr
= trans
->blocks_used
;
2167 ret
= btrfs_end_transaction(trans
, root
);
2169 mutex_unlock(&root
->fs_info
->fs_mutex
);
2170 btrfs_btree_balance_dirty(root
, nr
);
2171 btrfs_throttle(root
);
2174 static int noinline
create_subvol(struct btrfs_root
*root
, char *name
,
2177 struct btrfs_trans_handle
*trans
;
2178 struct btrfs_key key
;
2179 struct btrfs_root_item root_item
;
2180 struct btrfs_inode_item
*inode_item
;
2181 struct extent_buffer
*leaf
;
2182 struct btrfs_root
*new_root
= root
;
2183 struct inode
*inode
;
2188 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2189 unsigned long nr
= 1;
2191 mutex_lock(&root
->fs_info
->fs_mutex
);
2192 ret
= btrfs_check_free_space(root
, 1, 0);
2196 trans
= btrfs_start_transaction(root
, 1);
2199 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2204 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2205 objectid
, trans
->transid
, 0, 0,
2208 return PTR_ERR(leaf
);
2210 btrfs_set_header_nritems(leaf
, 0);
2211 btrfs_set_header_level(leaf
, 0);
2212 btrfs_set_header_bytenr(leaf
, leaf
->start
);
2213 btrfs_set_header_generation(leaf
, trans
->transid
);
2214 btrfs_set_header_owner(leaf
, objectid
);
2216 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
2217 (unsigned long)btrfs_header_fsid(leaf
),
2219 btrfs_mark_buffer_dirty(leaf
);
2221 inode_item
= &root_item
.inode
;
2222 memset(inode_item
, 0, sizeof(*inode_item
));
2223 inode_item
->generation
= cpu_to_le64(1);
2224 inode_item
->size
= cpu_to_le64(3);
2225 inode_item
->nlink
= cpu_to_le32(1);
2226 inode_item
->nblocks
= cpu_to_le64(1);
2227 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
2229 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
2230 btrfs_set_root_level(&root_item
, 0);
2231 btrfs_set_root_refs(&root_item
, 1);
2232 btrfs_set_root_used(&root_item
, 0);
2234 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
2235 root_item
.drop_level
= 0;
2237 free_extent_buffer(leaf
);
2240 btrfs_set_root_dirid(&root_item
, new_dirid
);
2242 key
.objectid
= objectid
;
2244 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2245 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2251 * insert the directory item
2253 key
.offset
= (u64
)-1;
2254 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2255 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2256 name
, namelen
, dir
->i_ino
, &key
,
2261 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2262 name
, namelen
, objectid
,
2263 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2267 ret
= btrfs_commit_transaction(trans
, root
);
2271 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2274 trans
= btrfs_start_transaction(new_root
, 1);
2277 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2278 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2281 inode
->i_op
= &btrfs_dir_inode_operations
;
2282 inode
->i_fop
= &btrfs_dir_file_operations
;
2283 new_root
->inode
= inode
;
2285 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2289 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2293 nr
= trans
->blocks_used
;
2294 err
= btrfs_commit_transaction(trans
, new_root
);
2298 mutex_unlock(&root
->fs_info
->fs_mutex
);
2299 btrfs_btree_balance_dirty(root
, nr
);
2300 btrfs_throttle(root
);
2304 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2306 struct btrfs_pending_snapshot
*pending_snapshot
;
2307 struct btrfs_trans_handle
*trans
;
2310 unsigned long nr
= 0;
2312 if (!root
->ref_cows
)
2315 mutex_lock(&root
->fs_info
->fs_mutex
);
2316 ret
= btrfs_check_free_space(root
, 1, 0);
2320 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
2321 if (!pending_snapshot
) {
2325 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
2326 if (!pending_snapshot
->name
) {
2328 kfree(pending_snapshot
);
2331 memcpy(pending_snapshot
->name
, name
, namelen
);
2332 pending_snapshot
->name
[namelen
] = '\0';
2333 trans
= btrfs_start_transaction(root
, 1);
2335 pending_snapshot
->root
= root
;
2336 list_add(&pending_snapshot
->list
,
2337 &trans
->transaction
->pending_snapshots
);
2338 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2339 err
= btrfs_commit_transaction(trans
, root
);
2342 mutex_unlock(&root
->fs_info
->fs_mutex
);
2343 btrfs_btree_balance_dirty(root
, nr
);
2344 btrfs_throttle(root
);
2348 unsigned long btrfs_force_ra(struct address_space
*mapping
,
2349 struct file_ra_state
*ra
, struct file
*file
,
2350 pgoff_t offset
, pgoff_t last_index
)
2354 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2355 req_size
= last_index
- offset
+ 1;
2356 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2359 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2360 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2361 return offset
+ req_size
;
2365 int btrfs_defrag_file(struct file
*file
) {
2366 struct inode
*inode
= fdentry(file
)->d_inode
;
2367 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2368 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2370 unsigned long last_index
;
2371 unsigned long ra_index
= 0;
2375 u64 existing_delalloc
;
2379 mutex_lock(&root
->fs_info
->fs_mutex
);
2380 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
2381 mutex_unlock(&root
->fs_info
->fs_mutex
);
2385 mutex_lock(&inode
->i_mutex
);
2386 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2387 for (i
= 0; i
<= last_index
; i
++) {
2388 if (i
== ra_index
) {
2389 ra_index
= btrfs_force_ra(inode
->i_mapping
,
2391 file
, ra_index
, last_index
);
2393 page
= grab_cache_page(inode
->i_mapping
, i
);
2396 if (!PageUptodate(page
)) {
2397 btrfs_readpage(NULL
, page
);
2399 if (!PageUptodate(page
)) {
2401 page_cache_release(page
);
2405 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2406 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2408 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2409 delalloc_start
= page_start
;
2411 count_range_bits(&BTRFS_I(inode
)->extent_tree
,
2412 &delalloc_start
, page_end
,
2413 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
2414 set_extent_delalloc(em_tree
, page_start
,
2415 page_end
, GFP_NOFS
);
2417 spin_lock(&root
->fs_info
->delalloc_lock
);
2418 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
-
2420 spin_unlock(&root
->fs_info
->delalloc_lock
);
2422 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2423 set_page_dirty(page
);
2425 page_cache_release(page
);
2426 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2430 mutex_unlock(&inode
->i_mutex
);
2434 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
2438 struct btrfs_ioctl_vol_args
*vol_args
;
2439 struct btrfs_trans_handle
*trans
;
2445 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2450 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2454 namelen
= strlen(vol_args
->name
);
2455 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2460 sizestr
= vol_args
->name
;
2461 if (!strcmp(sizestr
, "max"))
2462 new_size
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
;
2464 if (sizestr
[0] == '-') {
2467 } else if (sizestr
[0] == '+') {
2471 new_size
= btrfs_parse_size(sizestr
);
2472 if (new_size
== 0) {
2478 mutex_lock(&root
->fs_info
->fs_mutex
);
2479 old_size
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2482 if (new_size
> old_size
) {
2486 new_size
= old_size
- new_size
;
2487 } else if (mod
> 0) {
2488 new_size
= old_size
+ new_size
;
2491 if (new_size
< 256 * 1024 * 1024) {
2495 if (new_size
> root
->fs_info
->sb
->s_bdev
->bd_inode
->i_size
) {
2500 do_div(new_size
, root
->sectorsize
);
2501 new_size
*= root
->sectorsize
;
2503 printk("new size is %Lu\n", new_size
);
2504 if (new_size
> old_size
) {
2505 trans
= btrfs_start_transaction(root
, 1);
2506 ret
= btrfs_grow_extent_tree(trans
, root
, new_size
);
2507 btrfs_commit_transaction(trans
, root
);
2509 ret
= btrfs_shrink_extent_tree(root
, new_size
);
2513 mutex_unlock(&root
->fs_info
->fs_mutex
);
2519 static int noinline
btrfs_ioctl_snap_create(struct btrfs_root
*root
,
2522 struct btrfs_ioctl_vol_args
*vol_args
;
2523 struct btrfs_dir_item
*di
;
2524 struct btrfs_path
*path
;
2529 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
2534 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
2539 namelen
= strlen(vol_args
->name
);
2540 if (namelen
> BTRFS_VOL_NAME_MAX
) {
2544 if (strchr(vol_args
->name
, '/')) {
2549 path
= btrfs_alloc_path();
2555 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2556 mutex_lock(&root
->fs_info
->fs_mutex
);
2557 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2559 vol_args
->name
, namelen
, 0);
2560 mutex_unlock(&root
->fs_info
->fs_mutex
);
2561 btrfs_free_path(path
);
2563 if (di
&& !IS_ERR(di
)) {
2573 if (root
== root
->fs_info
->tree_root
)
2574 ret
= create_subvol(root
, vol_args
->name
, namelen
);
2576 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
2582 static int btrfs_ioctl_defrag(struct file
*file
)
2584 struct inode
*inode
= fdentry(file
)->d_inode
;
2585 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2587 switch (inode
->i_mode
& S_IFMT
) {
2589 mutex_lock(&root
->fs_info
->fs_mutex
);
2590 btrfs_defrag_root(root
, 0);
2591 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2592 mutex_unlock(&root
->fs_info
->fs_mutex
);
2595 btrfs_defrag_file(file
);
2602 long btrfs_ioctl(struct file
*file
, unsigned int
2603 cmd
, unsigned long arg
)
2605 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2608 case BTRFS_IOC_SNAP_CREATE
:
2609 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2610 case BTRFS_IOC_DEFRAG
:
2611 return btrfs_ioctl_defrag(file
);
2612 case BTRFS_IOC_RESIZE
:
2613 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
2620 * Called inside transaction, so use GFP_NOFS
2622 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2624 struct btrfs_inode
*ei
;
2626 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2630 ei
->ordered_trans
= 0;
2631 return &ei
->vfs_inode
;
2634 void btrfs_destroy_inode(struct inode
*inode
)
2636 WARN_ON(!list_empty(&inode
->i_dentry
));
2637 WARN_ON(inode
->i_data
.nrpages
);
2639 btrfs_drop_extent_cache(inode
, 0, (u64
)-1);
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
= PAGE_CACHE_SIZE
;
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
);
2886 btrfs_throttle(root
);
2889 static int btrfs_permission(struct inode
*inode
, int mask
,
2890 struct nameidata
*nd
)
2892 if (btrfs_test_flag(inode
, READONLY
) && (mask
& MAY_WRITE
))
2894 return generic_permission(inode
, mask
, NULL
);
2897 static struct inode_operations btrfs_dir_inode_operations
= {
2898 .lookup
= btrfs_lookup
,
2899 .create
= btrfs_create
,
2900 .unlink
= btrfs_unlink
,
2902 .mkdir
= btrfs_mkdir
,
2903 .rmdir
= btrfs_rmdir
,
2904 .rename
= btrfs_rename
,
2905 .symlink
= btrfs_symlink
,
2906 .setattr
= btrfs_setattr
,
2907 .mknod
= btrfs_mknod
,
2908 .setxattr
= generic_setxattr
,
2909 .getxattr
= generic_getxattr
,
2910 .listxattr
= btrfs_listxattr
,
2911 .removexattr
= generic_removexattr
,
2912 .permission
= btrfs_permission
,
2914 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2915 .lookup
= btrfs_lookup
,
2916 .permission
= btrfs_permission
,
2918 static struct file_operations btrfs_dir_file_operations
= {
2919 .llseek
= generic_file_llseek
,
2920 .read
= generic_read_dir
,
2921 .readdir
= btrfs_readdir
,
2922 .unlocked_ioctl
= btrfs_ioctl
,
2923 #ifdef CONFIG_COMPAT
2924 .compat_ioctl
= btrfs_ioctl
,
2928 static struct extent_map_ops btrfs_extent_map_ops
= {
2929 .fill_delalloc
= run_delalloc_range
,
2930 .writepage_io_hook
= btrfs_writepage_io_hook
,
2931 .readpage_io_hook
= btrfs_readpage_io_hook
,
2932 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2935 static struct address_space_operations btrfs_aops
= {
2936 .readpage
= btrfs_readpage
,
2937 .writepage
= btrfs_writepage
,
2938 .writepages
= btrfs_writepages
,
2939 .readpages
= btrfs_readpages
,
2940 .sync_page
= block_sync_page
,
2942 .invalidatepage
= btrfs_invalidatepage
,
2943 .releasepage
= btrfs_releasepage
,
2944 .set_page_dirty
= __set_page_dirty_nobuffers
,
2947 static struct address_space_operations btrfs_symlink_aops
= {
2948 .readpage
= btrfs_readpage
,
2949 .writepage
= btrfs_writepage
,
2950 .invalidatepage
= btrfs_invalidatepage
,
2951 .releasepage
= btrfs_releasepage
,
2954 static struct inode_operations btrfs_file_inode_operations
= {
2955 .truncate
= btrfs_truncate
,
2956 .getattr
= btrfs_getattr
,
2957 .setattr
= btrfs_setattr
,
2958 .setxattr
= generic_setxattr
,
2959 .getxattr
= generic_getxattr
,
2960 .listxattr
= btrfs_listxattr
,
2961 .removexattr
= generic_removexattr
,
2962 .permission
= btrfs_permission
,
2964 static struct inode_operations btrfs_special_inode_operations
= {
2965 .getattr
= btrfs_getattr
,
2966 .setattr
= btrfs_setattr
,
2967 .permission
= btrfs_permission
,
2969 static struct inode_operations btrfs_symlink_inode_operations
= {
2970 .readlink
= generic_readlink
,
2971 .follow_link
= page_follow_link_light
,
2972 .put_link
= page_put_link
,
2973 .permission
= btrfs_permission
,