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 static int run_delalloc_range(struct inode
*inode
, u64 start
, u64 end
)
77 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
78 struct btrfs_trans_handle
*trans
;
83 u64 blocksize
= root
->sectorsize
;
85 mutex_lock(&root
->fs_info
->fs_mutex
);
86 trans
= btrfs_start_transaction(root
, 1);
87 btrfs_set_trans_block_group(trans
, inode
);
89 num_bytes
= (end
- start
+ blocksize
) & ~(blocksize
- 1);
90 ret
= btrfs_drop_extents(trans
, root
, inode
,
91 start
, start
+ num_bytes
, start
, &alloc_hint
);
93 if (alloc_hint
== EXTENT_MAP_INLINE
)
96 ret
= btrfs_alloc_extent(trans
, root
, num_bytes
,
97 root
->root_key
.objectid
, trans
->transid
,
98 inode
->i_ino
, start
, 0,
99 alloc_hint
, (u64
)-1, &ins
, 1);
104 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
105 start
, ins
.objectid
, ins
.offset
,
108 btrfs_end_transaction(trans
, root
);
109 mutex_unlock(&root
->fs_info
->fs_mutex
);
113 int btrfs_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
115 struct inode
*inode
= page
->mapping
->host
;
116 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
117 struct btrfs_trans_handle
*trans
;
120 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
121 size_t offset
= start
- page_start
;
123 if (btrfs_test_opt(root
, NODATASUM
))
126 mutex_lock(&root
->fs_info
->fs_mutex
);
127 trans
= btrfs_start_transaction(root
, 1);
128 btrfs_set_trans_block_group(trans
, inode
);
130 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
131 start
, kaddr
+ offset
, end
- start
+ 1);
133 ret
= btrfs_end_transaction(trans
, root
);
135 mutex_unlock(&root
->fs_info
->fs_mutex
);
139 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
142 struct inode
*inode
= page
->mapping
->host
;
143 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
144 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
145 struct btrfs_csum_item
*item
;
146 struct btrfs_path
*path
= NULL
;
149 if (btrfs_test_opt(root
, NODATASUM
))
152 mutex_lock(&root
->fs_info
->fs_mutex
);
153 path
= btrfs_alloc_path();
154 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
157 /* a csum that isn't present is a preallocated region. */
158 if (ret
== -ENOENT
|| ret
== -EFBIG
)
163 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
165 set_state_private(em_tree
, start
, csum
);
168 btrfs_free_path(path
);
169 mutex_unlock(&root
->fs_info
->fs_mutex
);
173 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
175 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
176 struct inode
*inode
= page
->mapping
->host
;
177 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
181 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
185 if (btrfs_test_opt(root
, NODATASUM
))
188 ret
= get_state_private(em_tree
, start
, &private);
189 local_irq_save(flags
);
190 kaddr
= kmap_atomic(page
, KM_IRQ0
);
194 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
195 btrfs_csum_final(csum
, (char *)&csum
);
196 if (csum
!= private) {
199 kunmap_atomic(kaddr
, KM_IRQ0
);
200 local_irq_restore(flags
);
204 printk("btrfs csum failed ino %lu off %llu\n",
205 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
206 memset(kaddr
+ offset
, 1, end
- start
+ 1);
207 flush_dcache_page(page
);
208 kunmap_atomic(kaddr
, KM_IRQ0
);
209 local_irq_restore(flags
);
213 void btrfs_read_locked_inode(struct inode
*inode
)
215 struct btrfs_path
*path
;
216 struct extent_buffer
*leaf
;
217 struct btrfs_inode_item
*inode_item
;
218 struct btrfs_inode_timespec
*tspec
;
219 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
220 struct btrfs_key location
;
221 u64 alloc_group_block
;
225 path
= btrfs_alloc_path();
227 mutex_lock(&root
->fs_info
->fs_mutex
);
229 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
230 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
234 leaf
= path
->nodes
[0];
235 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
236 struct btrfs_inode_item
);
238 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
239 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
240 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
241 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
242 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
244 tspec
= btrfs_inode_atime(inode_item
);
245 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
246 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
248 tspec
= btrfs_inode_mtime(inode_item
);
249 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
250 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
252 tspec
= btrfs_inode_ctime(inode_item
);
253 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
254 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
256 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
257 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
259 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
261 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
262 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
265 btrfs_free_path(path
);
268 mutex_unlock(&root
->fs_info
->fs_mutex
);
270 switch (inode
->i_mode
& S_IFMT
) {
272 inode
->i_mapping
->a_ops
= &btrfs_aops
;
273 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
274 inode
->i_fop
= &btrfs_file_operations
;
275 inode
->i_op
= &btrfs_file_inode_operations
;
278 inode
->i_fop
= &btrfs_dir_file_operations
;
279 if (root
== root
->fs_info
->tree_root
)
280 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
282 inode
->i_op
= &btrfs_dir_inode_operations
;
285 inode
->i_op
= &btrfs_symlink_inode_operations
;
286 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
289 init_special_inode(inode
, inode
->i_mode
, rdev
);
295 btrfs_release_path(root
, path
);
296 btrfs_free_path(path
);
297 mutex_unlock(&root
->fs_info
->fs_mutex
);
298 make_bad_inode(inode
);
301 static void fill_inode_item(struct extent_buffer
*leaf
,
302 struct btrfs_inode_item
*item
,
305 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
306 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
307 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
308 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
309 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
311 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
312 inode
->i_atime
.tv_sec
);
313 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
314 inode
->i_atime
.tv_nsec
);
316 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
317 inode
->i_mtime
.tv_sec
);
318 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
319 inode
->i_mtime
.tv_nsec
);
321 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
322 inode
->i_ctime
.tv_sec
);
323 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
324 inode
->i_ctime
.tv_nsec
);
326 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
327 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
328 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
329 btrfs_set_inode_block_group(leaf
, item
,
330 BTRFS_I(inode
)->block_group
->key
.objectid
);
333 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
334 struct btrfs_root
*root
,
337 struct btrfs_inode_item
*inode_item
;
338 struct btrfs_path
*path
;
339 struct extent_buffer
*leaf
;
342 path
= btrfs_alloc_path();
344 ret
= btrfs_lookup_inode(trans
, root
, path
,
345 &BTRFS_I(inode
)->location
, 1);
352 leaf
= path
->nodes
[0];
353 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
354 struct btrfs_inode_item
);
356 fill_inode_item(leaf
, inode_item
, inode
);
357 btrfs_mark_buffer_dirty(leaf
);
358 btrfs_set_inode_last_trans(trans
, inode
);
361 btrfs_release_path(root
, path
);
362 btrfs_free_path(path
);
367 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
368 struct btrfs_root
*root
,
370 struct dentry
*dentry
)
372 struct btrfs_path
*path
;
373 const char *name
= dentry
->d_name
.name
;
374 int name_len
= dentry
->d_name
.len
;
376 struct extent_buffer
*leaf
;
377 struct btrfs_dir_item
*di
;
378 struct btrfs_key key
;
380 path
= btrfs_alloc_path();
386 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
396 leaf
= path
->nodes
[0];
397 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
398 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
401 btrfs_release_path(root
, path
);
403 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
404 key
.objectid
, name
, name_len
, -1);
413 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
415 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
416 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
417 dentry
->d_inode
->i_ino
,
418 dentry
->d_parent
->d_inode
->i_ino
);
420 printk("failed to delete reference to %.*s, "
421 "inode %lu parent %lu\n", name_len
, name
,
422 dentry
->d_inode
->i_ino
,
423 dentry
->d_parent
->d_inode
->i_ino
);
426 btrfs_free_path(path
);
428 dir
->i_size
-= name_len
* 2;
429 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
430 btrfs_update_inode(trans
, root
, dir
);
431 drop_nlink(dentry
->d_inode
);
432 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
433 dir
->i_sb
->s_dirt
= 1;
438 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
440 struct btrfs_root
*root
;
441 struct btrfs_trans_handle
*trans
;
445 root
= BTRFS_I(dir
)->root
;
446 mutex_lock(&root
->fs_info
->fs_mutex
);
447 trans
= btrfs_start_transaction(root
, 1);
449 btrfs_set_trans_block_group(trans
, dir
);
450 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
451 nr
= trans
->blocks_used
;
453 btrfs_end_transaction(trans
, root
);
454 mutex_unlock(&root
->fs_info
->fs_mutex
);
455 btrfs_btree_balance_dirty(root
, nr
);
460 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
462 struct inode
*inode
= dentry
->d_inode
;
465 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
466 struct btrfs_trans_handle
*trans
;
469 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
472 mutex_lock(&root
->fs_info
->fs_mutex
);
473 trans
= btrfs_start_transaction(root
, 1);
474 btrfs_set_trans_block_group(trans
, dir
);
476 /* now the directory is empty */
477 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
482 nr
= trans
->blocks_used
;
483 ret
= btrfs_end_transaction(trans
, root
);
484 mutex_unlock(&root
->fs_info
->fs_mutex
);
485 btrfs_btree_balance_dirty(root
, nr
);
492 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
493 struct btrfs_root
*root
,
496 struct btrfs_path
*path
;
501 path
= btrfs_alloc_path();
503 ret
= btrfs_lookup_inode(trans
, root
, path
,
504 &BTRFS_I(inode
)->location
, -1);
508 ret
= btrfs_del_item(trans
, root
, path
);
509 btrfs_free_path(path
);
514 * this can truncate away extent items, csum items and directory items.
515 * It starts at a high offset and removes keys until it can't find
516 * any higher than i_size.
518 * csum items that cross the new i_size are truncated to the new size
521 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
522 struct btrfs_root
*root
,
526 struct btrfs_path
*path
;
527 struct btrfs_key key
;
528 struct btrfs_key found_key
;
530 struct extent_buffer
*leaf
;
531 struct btrfs_file_extent_item
*fi
;
532 u64 extent_start
= 0;
533 u64 extent_num_bytes
= 0;
539 int extent_type
= -1;
541 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
542 path
= btrfs_alloc_path();
546 /* FIXME, add redo link to tree so we don't leak on crash */
547 key
.objectid
= inode
->i_ino
;
548 key
.offset
= (u64
)-1;
552 btrfs_init_path(path
);
554 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
559 BUG_ON(path
->slots
[0] == 0);
562 leaf
= path
->nodes
[0];
563 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
564 found_type
= btrfs_key_type(&found_key
);
566 if (found_key
.objectid
!= inode
->i_ino
)
569 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
570 found_type
!= BTRFS_DIR_ITEM_KEY
&&
571 found_type
!= BTRFS_DIR_INDEX_KEY
&&
572 found_type
!= BTRFS_EXTENT_DATA_KEY
)
575 item_end
= found_key
.offset
;
576 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
577 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
578 struct btrfs_file_extent_item
);
579 extent_type
= btrfs_file_extent_type(leaf
, fi
);
580 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
582 btrfs_file_extent_num_bytes(leaf
, fi
);
583 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
584 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
586 item_end
+= btrfs_file_extent_inline_len(leaf
,
591 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
592 ret
= btrfs_csum_truncate(trans
, root
, path
,
596 if (item_end
< inode
->i_size
) {
597 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
598 found_type
= BTRFS_INODE_ITEM_KEY
;
599 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
600 found_type
= BTRFS_CSUM_ITEM_KEY
;
601 } else if (found_type
) {
606 btrfs_set_key_type(&key
, found_type
);
607 btrfs_release_path(root
, path
);
610 if (found_key
.offset
>= inode
->i_size
)
616 /* FIXME, shrink the extent if the ref count is only 1 */
617 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
620 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
622 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
625 btrfs_file_extent_num_bytes(leaf
, fi
);
626 extent_num_bytes
= inode
->i_size
-
627 found_key
.offset
+ root
->sectorsize
- 1;
628 btrfs_set_file_extent_num_bytes(leaf
, fi
,
630 num_dec
= (orig_num_bytes
-
631 extent_num_bytes
) >> 9;
632 if (extent_start
!= 0) {
633 inode
->i_blocks
-= num_dec
;
635 btrfs_mark_buffer_dirty(leaf
);
638 btrfs_file_extent_disk_num_bytes(leaf
,
640 /* FIXME blocksize != 4096 */
641 num_dec
= btrfs_file_extent_num_bytes(leaf
,
643 if (extent_start
!= 0) {
645 inode
->i_blocks
-= num_dec
;
647 root_gen
= btrfs_header_generation(leaf
);
648 root_owner
= btrfs_header_owner(leaf
);
650 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
652 u32 newsize
= inode
->i_size
- found_key
.offset
;
653 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
654 ret
= btrfs_truncate_item(trans
, root
, path
,
660 ret
= btrfs_del_item(trans
, root
, path
);
666 btrfs_release_path(root
, path
);
668 ret
= btrfs_free_extent(trans
, root
, extent_start
,
671 root_gen
, inode
->i_ino
,
672 found_key
.offset
, 0);
678 btrfs_release_path(root
, path
);
679 btrfs_free_path(path
);
680 inode
->i_sb
->s_dirt
= 1;
684 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
689 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
690 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
691 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
693 set_page_extent_mapped(page
);
695 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
696 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
698 if (zero_start
!= PAGE_CACHE_SIZE
) {
700 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
701 flush_dcache_page(page
);
704 set_page_dirty(page
);
705 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
711 * taken from block_truncate_page, but does cow as it zeros out
712 * any bytes left in the last page in the file.
714 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
716 struct inode
*inode
= mapping
->host
;
717 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
718 u32 blocksize
= root
->sectorsize
;
719 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
720 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
725 if ((offset
& (blocksize
- 1)) == 0)
728 down_read(&root
->snap_sem
);
730 page
= grab_cache_page(mapping
, index
);
733 if (!PageUptodate(page
)) {
734 ret
= btrfs_readpage(NULL
, page
);
736 if (!PageUptodate(page
)) {
741 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
743 ret
= btrfs_cow_one_page(inode
, page
, offset
);
746 page_cache_release(page
);
747 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
752 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
754 struct inode
*inode
= dentry
->d_inode
;
757 err
= inode_change_ok(inode
, attr
);
761 if (S_ISREG(inode
->i_mode
) &&
762 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
763 struct btrfs_trans_handle
*trans
;
764 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
765 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
767 u64 mask
= root
->sectorsize
- 1;
768 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
769 u64 block_end
= attr
->ia_size
| mask
;
773 if (attr
->ia_size
<= pos
)
776 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
778 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
779 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
781 mutex_lock(&root
->fs_info
->fs_mutex
);
782 trans
= btrfs_start_transaction(root
, 1);
783 btrfs_set_trans_block_group(trans
, inode
);
784 err
= btrfs_drop_extents(trans
, root
, inode
,
785 pos
, pos
+ hole_size
, pos
,
788 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
789 err
= btrfs_insert_file_extent(trans
, root
,
791 pos
, 0, 0, hole_size
);
793 btrfs_end_transaction(trans
, root
);
794 mutex_unlock(&root
->fs_info
->fs_mutex
);
795 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
800 err
= inode_setattr(inode
, attr
);
804 void btrfs_delete_inode(struct inode
*inode
)
806 struct btrfs_trans_handle
*trans
;
807 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
811 truncate_inode_pages(&inode
->i_data
, 0);
812 if (is_bad_inode(inode
)) {
817 mutex_lock(&root
->fs_info
->fs_mutex
);
818 trans
= btrfs_start_transaction(root
, 1);
820 btrfs_set_trans_block_group(trans
, inode
);
821 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
824 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
827 ret
= btrfs_free_inode(trans
, root
, inode
);
830 nr
= trans
->blocks_used
;
832 btrfs_end_transaction(trans
, root
);
833 mutex_unlock(&root
->fs_info
->fs_mutex
);
834 btrfs_btree_balance_dirty(root
, nr
);
838 nr
= trans
->blocks_used
;
839 btrfs_end_transaction(trans
, root
);
840 mutex_unlock(&root
->fs_info
->fs_mutex
);
841 btrfs_btree_balance_dirty(root
, nr
);
847 * this returns the key found in the dir entry in the location pointer.
848 * If no dir entries were found, location->objectid is 0.
850 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
851 struct btrfs_key
*location
)
853 const char *name
= dentry
->d_name
.name
;
854 int namelen
= dentry
->d_name
.len
;
855 struct btrfs_dir_item
*di
;
856 struct btrfs_path
*path
;
857 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
860 if (namelen
== 1 && strcmp(name
, ".") == 0) {
861 location
->objectid
= dir
->i_ino
;
862 location
->type
= BTRFS_INODE_ITEM_KEY
;
863 location
->offset
= 0;
866 path
= btrfs_alloc_path();
869 if (namelen
== 2 && strcmp(name
, "..") == 0) {
870 struct btrfs_key key
;
871 struct extent_buffer
*leaf
;
875 key
.objectid
= dir
->i_ino
;
876 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
878 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
882 leaf
= path
->nodes
[0];
883 slot
= path
->slots
[0];
884 nritems
= btrfs_header_nritems(leaf
);
888 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
889 if (key
.objectid
!= dir
->i_ino
||
890 key
.type
!= BTRFS_INODE_REF_KEY
) {
893 location
->objectid
= key
.offset
;
894 location
->type
= BTRFS_INODE_ITEM_KEY
;
895 location
->offset
= 0;
899 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
903 if (!di
|| IS_ERR(di
)) {
906 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
908 btrfs_free_path(path
);
911 location
->objectid
= 0;
916 * when we hit a tree root in a directory, the btrfs part of the inode
917 * needs to be changed to reflect the root directory of the tree root. This
918 * is kind of like crossing a mount point.
920 static int fixup_tree_root_location(struct btrfs_root
*root
,
921 struct btrfs_key
*location
,
922 struct btrfs_root
**sub_root
,
923 struct dentry
*dentry
)
925 struct btrfs_path
*path
;
926 struct btrfs_root_item
*ri
;
928 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
930 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
933 path
= btrfs_alloc_path();
935 mutex_lock(&root
->fs_info
->fs_mutex
);
937 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
940 if (IS_ERR(*sub_root
))
941 return PTR_ERR(*sub_root
);
943 ri
= &(*sub_root
)->root_item
;
944 location
->objectid
= btrfs_root_dirid(ri
);
945 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
946 location
->offset
= 0;
948 btrfs_free_path(path
);
949 mutex_unlock(&root
->fs_info
->fs_mutex
);
953 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
955 struct btrfs_iget_args
*args
= p
;
956 inode
->i_ino
= args
->ino
;
957 BTRFS_I(inode
)->root
= args
->root
;
958 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
959 inode
->i_mapping
, GFP_NOFS
);
963 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
965 struct btrfs_iget_args
*args
= opaque
;
966 return (args
->ino
== inode
->i_ino
&&
967 args
->root
== BTRFS_I(inode
)->root
);
970 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
971 struct btrfs_root
*root
)
974 struct btrfs_iget_args args
;
978 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
979 btrfs_init_locked_inode
,
984 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
985 struct nameidata
*nd
)
987 struct inode
* inode
;
988 struct btrfs_inode
*bi
= BTRFS_I(dir
);
989 struct btrfs_root
*root
= bi
->root
;
990 struct btrfs_root
*sub_root
= root
;
991 struct btrfs_key location
;
994 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
995 return ERR_PTR(-ENAMETOOLONG
);
997 mutex_lock(&root
->fs_info
->fs_mutex
);
998 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
999 mutex_unlock(&root
->fs_info
->fs_mutex
);
1002 return ERR_PTR(ret
);
1005 if (location
.objectid
) {
1006 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1009 return ERR_PTR(ret
);
1011 return ERR_PTR(-ENOENT
);
1012 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1015 return ERR_PTR(-EACCES
);
1016 if (inode
->i_state
& I_NEW
) {
1017 /* the inode and parent dir are two different roots */
1018 if (sub_root
!= root
) {
1020 sub_root
->inode
= inode
;
1022 BTRFS_I(inode
)->root
= sub_root
;
1023 memcpy(&BTRFS_I(inode
)->location
, &location
,
1025 btrfs_read_locked_inode(inode
);
1026 unlock_new_inode(inode
);
1029 return d_splice_alias(inode
, dentry
);
1032 static unsigned char btrfs_filetype_table
[] = {
1033 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1036 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1038 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1039 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1040 struct btrfs_item
*item
;
1041 struct btrfs_dir_item
*di
;
1042 struct btrfs_key key
;
1043 struct btrfs_key found_key
;
1044 struct btrfs_path
*path
;
1047 struct extent_buffer
*leaf
;
1050 unsigned char d_type
;
1055 int key_type
= BTRFS_DIR_INDEX_KEY
;
1060 /* FIXME, use a real flag for deciding about the key type */
1061 if (root
->fs_info
->tree_root
== root
)
1062 key_type
= BTRFS_DIR_ITEM_KEY
;
1064 /* special case for "." */
1065 if (filp
->f_pos
== 0) {
1066 over
= filldir(dirent
, ".", 1,
1074 mutex_lock(&root
->fs_info
->fs_mutex
);
1075 key
.objectid
= inode
->i_ino
;
1076 path
= btrfs_alloc_path();
1079 /* special case for .., just use the back ref */
1080 if (filp
->f_pos
== 1) {
1081 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1083 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
) {
1089 btrfs_release_path(root
, path
);
1090 goto read_dir_items
;
1092 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1093 btrfs_release_path(root
, path
);
1094 if (found_key
.objectid
!= key
.objectid
||
1095 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1096 goto read_dir_items
;
1097 over
= filldir(dirent
, "..", 2,
1098 2, found_key
.offset
, DT_DIR
);
1105 btrfs_set_key_type(&key
, key_type
);
1106 key
.offset
= filp
->f_pos
;
1108 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1113 leaf
= path
->nodes
[0];
1114 nritems
= btrfs_header_nritems(leaf
);
1115 slot
= path
->slots
[0];
1116 if (advance
|| slot
>= nritems
) {
1117 if (slot
>= nritems
-1) {
1118 ret
= btrfs_next_leaf(root
, path
);
1121 leaf
= path
->nodes
[0];
1122 nritems
= btrfs_header_nritems(leaf
);
1123 slot
= path
->slots
[0];
1130 item
= btrfs_item_nr(leaf
, slot
);
1131 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1133 if (found_key
.objectid
!= key
.objectid
)
1135 if (btrfs_key_type(&found_key
) != key_type
)
1137 if (found_key
.offset
< filp
->f_pos
)
1140 filp
->f_pos
= found_key
.offset
;
1142 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1144 di_total
= btrfs_item_size(leaf
, item
);
1145 while(di_cur
< di_total
) {
1146 struct btrfs_key location
;
1148 name_len
= btrfs_dir_name_len(leaf
, di
);
1149 if (name_len
< 32) {
1150 name_ptr
= tmp_name
;
1152 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1155 read_extent_buffer(leaf
, name_ptr
,
1156 (unsigned long)(di
+ 1), name_len
);
1158 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1159 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1161 over
= filldir(dirent
, name_ptr
, name_len
,
1166 if (name_ptr
!= tmp_name
)
1171 di_len
= btrfs_dir_name_len(leaf
, di
) +
1172 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1174 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1181 btrfs_release_path(root
, path
);
1182 btrfs_free_path(path
);
1183 mutex_unlock(&root
->fs_info
->fs_mutex
);
1187 int btrfs_write_inode(struct inode
*inode
, int wait
)
1189 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1190 struct btrfs_trans_handle
*trans
;
1194 mutex_lock(&root
->fs_info
->fs_mutex
);
1195 trans
= btrfs_start_transaction(root
, 1);
1196 btrfs_set_trans_block_group(trans
, inode
);
1197 ret
= btrfs_commit_transaction(trans
, root
);
1198 mutex_unlock(&root
->fs_info
->fs_mutex
);
1204 * This is somewhat expensive, updating the tree every time the
1205 * inode changes. But, it is most likely to find the inode in cache.
1206 * FIXME, needs more benchmarking...there are no reasons other than performance
1207 * to keep or drop this code.
1209 void btrfs_dirty_inode(struct inode
*inode
)
1211 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1212 struct btrfs_trans_handle
*trans
;
1214 mutex_lock(&root
->fs_info
->fs_mutex
);
1215 trans
= btrfs_start_transaction(root
, 1);
1216 btrfs_set_trans_block_group(trans
, inode
);
1217 btrfs_update_inode(trans
, root
, inode
);
1218 btrfs_end_transaction(trans
, root
);
1219 mutex_unlock(&root
->fs_info
->fs_mutex
);
1222 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1223 struct btrfs_root
*root
,
1225 struct btrfs_block_group_cache
*group
,
1228 struct inode
*inode
;
1229 struct btrfs_inode_item
*inode_item
;
1230 struct btrfs_key
*location
;
1231 struct btrfs_path
*path
;
1235 path
= btrfs_alloc_path();
1238 inode
= new_inode(root
->fs_info
->sb
);
1240 return ERR_PTR(-ENOMEM
);
1242 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1243 inode
->i_mapping
, GFP_NOFS
);
1244 BTRFS_I(inode
)->root
= root
;
1250 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1251 BTRFS_I(inode
)->block_group
= group
;
1253 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1257 inode
->i_uid
= current
->fsuid
;
1258 inode
->i_gid
= current
->fsgid
;
1259 inode
->i_mode
= mode
;
1260 inode
->i_ino
= objectid
;
1261 inode
->i_blocks
= 0;
1262 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1263 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1264 struct btrfs_inode_item
);
1265 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1266 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1267 btrfs_free_path(path
);
1269 location
= &BTRFS_I(inode
)->location
;
1270 location
->objectid
= objectid
;
1271 location
->offset
= 0;
1272 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1274 insert_inode_hash(inode
);
1277 btrfs_free_path(path
);
1278 return ERR_PTR(ret
);
1281 static inline u8
btrfs_inode_type(struct inode
*inode
)
1283 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1286 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1287 struct dentry
*dentry
, struct inode
*inode
)
1290 struct btrfs_key key
;
1291 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1292 struct inode
*parent_inode
;
1294 key
.objectid
= inode
->i_ino
;
1295 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1298 ret
= btrfs_insert_dir_item(trans
, root
,
1299 dentry
->d_name
.name
, dentry
->d_name
.len
,
1300 dentry
->d_parent
->d_inode
->i_ino
,
1301 &key
, btrfs_inode_type(inode
));
1303 ret
= btrfs_insert_inode_ref(trans
, root
,
1304 dentry
->d_name
.name
,
1307 dentry
->d_parent
->d_inode
->i_ino
);
1308 parent_inode
= dentry
->d_parent
->d_inode
;
1309 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1310 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1311 ret
= btrfs_update_inode(trans
, root
,
1312 dentry
->d_parent
->d_inode
);
1317 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1318 struct dentry
*dentry
, struct inode
*inode
)
1320 int err
= btrfs_add_link(trans
, dentry
, inode
);
1322 d_instantiate(dentry
, inode
);
1330 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1331 int mode
, dev_t rdev
)
1333 struct btrfs_trans_handle
*trans
;
1334 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1335 struct inode
*inode
;
1341 if (!new_valid_dev(rdev
))
1344 mutex_lock(&root
->fs_info
->fs_mutex
);
1345 trans
= btrfs_start_transaction(root
, 1);
1346 btrfs_set_trans_block_group(trans
, dir
);
1348 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1354 inode
= btrfs_new_inode(trans
, root
, objectid
,
1355 BTRFS_I(dir
)->block_group
, mode
);
1356 err
= PTR_ERR(inode
);
1360 btrfs_set_trans_block_group(trans
, inode
);
1361 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1365 inode
->i_op
= &btrfs_special_inode_operations
;
1366 init_special_inode(inode
, inode
->i_mode
, rdev
);
1367 btrfs_update_inode(trans
, root
, inode
);
1369 dir
->i_sb
->s_dirt
= 1;
1370 btrfs_update_inode_block_group(trans
, inode
);
1371 btrfs_update_inode_block_group(trans
, dir
);
1373 nr
= trans
->blocks_used
;
1374 btrfs_end_transaction(trans
, root
);
1375 mutex_unlock(&root
->fs_info
->fs_mutex
);
1378 inode_dec_link_count(inode
);
1381 btrfs_btree_balance_dirty(root
, nr
);
1385 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1386 int mode
, struct nameidata
*nd
)
1388 struct btrfs_trans_handle
*trans
;
1389 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1390 struct inode
*inode
;
1396 mutex_lock(&root
->fs_info
->fs_mutex
);
1397 trans
= btrfs_start_transaction(root
, 1);
1398 btrfs_set_trans_block_group(trans
, dir
);
1400 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1406 inode
= btrfs_new_inode(trans
, root
, objectid
,
1407 BTRFS_I(dir
)->block_group
, mode
);
1408 err
= PTR_ERR(inode
);
1412 btrfs_set_trans_block_group(trans
, inode
);
1413 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1417 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1418 inode
->i_fop
= &btrfs_file_operations
;
1419 inode
->i_op
= &btrfs_file_inode_operations
;
1420 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1421 inode
->i_mapping
, GFP_NOFS
);
1422 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1424 dir
->i_sb
->s_dirt
= 1;
1425 btrfs_update_inode_block_group(trans
, inode
);
1426 btrfs_update_inode_block_group(trans
, dir
);
1428 nr
= trans
->blocks_used
;
1429 btrfs_end_transaction(trans
, root
);
1430 mutex_unlock(&root
->fs_info
->fs_mutex
);
1433 inode_dec_link_count(inode
);
1436 btrfs_btree_balance_dirty(root
, nr
);
1440 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1441 struct dentry
*dentry
)
1443 struct btrfs_trans_handle
*trans
;
1444 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1445 struct inode
*inode
= old_dentry
->d_inode
;
1450 if (inode
->i_nlink
== 0)
1454 mutex_lock(&root
->fs_info
->fs_mutex
);
1455 trans
= btrfs_start_transaction(root
, 1);
1457 btrfs_set_trans_block_group(trans
, dir
);
1458 atomic_inc(&inode
->i_count
);
1459 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1464 dir
->i_sb
->s_dirt
= 1;
1465 btrfs_update_inode_block_group(trans
, dir
);
1466 err
= btrfs_update_inode(trans
, root
, inode
);
1471 nr
= trans
->blocks_used
;
1472 btrfs_end_transaction(trans
, root
);
1473 mutex_unlock(&root
->fs_info
->fs_mutex
);
1476 inode_dec_link_count(inode
);
1479 btrfs_btree_balance_dirty(root
, nr
);
1483 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1485 struct inode
*inode
;
1486 struct btrfs_trans_handle
*trans
;
1487 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1489 int drop_on_err
= 0;
1491 unsigned long nr
= 1;
1493 mutex_lock(&root
->fs_info
->fs_mutex
);
1494 trans
= btrfs_start_transaction(root
, 1);
1495 btrfs_set_trans_block_group(trans
, dir
);
1497 if (IS_ERR(trans
)) {
1498 err
= PTR_ERR(trans
);
1502 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1508 inode
= btrfs_new_inode(trans
, root
, objectid
,
1509 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1510 if (IS_ERR(inode
)) {
1511 err
= PTR_ERR(inode
);
1516 inode
->i_op
= &btrfs_dir_inode_operations
;
1517 inode
->i_fop
= &btrfs_dir_file_operations
;
1518 btrfs_set_trans_block_group(trans
, inode
);
1521 err
= btrfs_update_inode(trans
, root
, inode
);
1525 err
= btrfs_add_link(trans
, dentry
, inode
);
1529 d_instantiate(dentry
, inode
);
1531 dir
->i_sb
->s_dirt
= 1;
1532 btrfs_update_inode_block_group(trans
, inode
);
1533 btrfs_update_inode_block_group(trans
, dir
);
1536 nr
= trans
->blocks_used
;
1537 btrfs_end_transaction(trans
, root
);
1540 mutex_unlock(&root
->fs_info
->fs_mutex
);
1543 btrfs_btree_balance_dirty(root
, nr
);
1547 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1548 size_t page_offset
, u64 start
, u64 end
,
1554 u64 extent_start
= 0;
1556 u64 objectid
= inode
->i_ino
;
1558 int failed_insert
= 0;
1559 struct btrfs_path
*path
;
1560 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1561 struct btrfs_file_extent_item
*item
;
1562 struct extent_buffer
*leaf
;
1563 struct btrfs_key found_key
;
1564 struct extent_map
*em
= NULL
;
1565 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1566 struct btrfs_trans_handle
*trans
= NULL
;
1568 path
= btrfs_alloc_path();
1570 mutex_lock(&root
->fs_info
->fs_mutex
);
1573 em
= lookup_extent_mapping(em_tree
, start
, end
);
1578 em
= alloc_extent_map(GFP_NOFS
);
1583 em
->start
= EXTENT_MAP_HOLE
;
1584 em
->end
= EXTENT_MAP_HOLE
;
1586 em
->bdev
= inode
->i_sb
->s_bdev
;
1587 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1588 objectid
, start
, trans
!= NULL
);
1595 if (path
->slots
[0] == 0)
1600 leaf
= path
->nodes
[0];
1601 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1602 struct btrfs_file_extent_item
);
1603 /* are we inside the extent that was found? */
1604 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1605 found_type
= btrfs_key_type(&found_key
);
1606 if (found_key
.objectid
!= objectid
||
1607 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1611 found_type
= btrfs_file_extent_type(leaf
, item
);
1612 extent_start
= found_key
.offset
;
1613 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1614 extent_end
= extent_start
+
1615 btrfs_file_extent_num_bytes(leaf
, item
);
1617 if (start
< extent_start
|| start
>= extent_end
) {
1619 if (start
< extent_start
) {
1620 if (end
< extent_start
)
1622 em
->end
= extent_end
- 1;
1628 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1630 em
->start
= extent_start
;
1631 em
->end
= extent_end
- 1;
1632 em
->block_start
= EXTENT_MAP_HOLE
;
1633 em
->block_end
= EXTENT_MAP_HOLE
;
1636 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1637 em
->block_start
= bytenr
;
1638 em
->block_end
= em
->block_start
+
1639 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1640 em
->start
= extent_start
;
1641 em
->end
= extent_end
- 1;
1643 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1647 size_t extent_offset
;
1650 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1652 extent_end
= (extent_start
+ size
- 1) |
1653 ((u64
)root
->sectorsize
- 1);
1654 if (start
< extent_start
|| start
>= extent_end
) {
1656 if (start
< extent_start
) {
1657 if (end
< extent_start
)
1659 em
->end
= extent_end
;
1665 em
->block_start
= EXTENT_MAP_INLINE
;
1666 em
->block_end
= EXTENT_MAP_INLINE
;
1669 em
->start
= extent_start
;
1670 em
->end
= extent_start
+ size
- 1;
1674 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1675 extent_start
+ page_offset
;
1676 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1677 size
- extent_offset
);
1678 em
->start
= extent_start
+ extent_offset
;
1679 em
->end
= (em
->start
+ copy_size
-1) |
1680 ((u64
)root
->sectorsize
-1);
1682 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1683 if (create
== 0 && !PageUptodate(page
)) {
1684 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1686 flush_dcache_page(page
);
1687 } else if (create
&& PageUptodate(page
)) {
1690 free_extent_map(em
);
1692 btrfs_release_path(root
, path
);
1693 trans
= btrfs_start_transaction(root
, 1);
1696 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1698 btrfs_mark_buffer_dirty(leaf
);
1701 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1704 printk("unkknown found_type %d\n", found_type
);
1711 em
->block_start
= EXTENT_MAP_HOLE
;
1712 em
->block_end
= EXTENT_MAP_HOLE
;
1714 btrfs_release_path(root
, path
);
1715 if (em
->start
> start
|| em
->end
< start
) {
1716 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1720 ret
= add_extent_mapping(em_tree
, em
);
1721 if (ret
== -EEXIST
) {
1722 free_extent_map(em
);
1725 if (failed_insert
> 5) {
1726 printk("failing to insert %Lu %Lu\n", start
, end
);
1734 btrfs_free_path(path
);
1736 ret
= btrfs_end_transaction(trans
, root
);
1740 mutex_unlock(&root
->fs_info
->fs_mutex
);
1742 free_extent_map(em
);
1744 return ERR_PTR(err
);
1749 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1751 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1754 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1755 unsigned from
, unsigned to
)
1757 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1758 page
->mapping
->host
, page
, from
, to
,
1762 int btrfs_readpage(struct file
*file
, struct page
*page
)
1764 struct extent_map_tree
*tree
;
1765 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1766 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1768 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1770 struct extent_map_tree
*tree
;
1773 if (current
->flags
& PF_MEMALLOC
) {
1774 redirty_page_for_writepage(wbc
, page
);
1778 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1779 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1782 static int btrfs_writepages(struct address_space
*mapping
,
1783 struct writeback_control
*wbc
)
1785 struct extent_map_tree
*tree
;
1786 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1787 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
1791 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1792 struct list_head
*pages
, unsigned nr_pages
)
1794 struct extent_map_tree
*tree
;
1795 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1796 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
1800 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1802 struct extent_map_tree
*tree
;
1805 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1806 ret
= try_release_extent_mapping(tree
, page
);
1808 ClearPagePrivate(page
);
1809 set_page_private(page
, 0);
1810 page_cache_release(page
);
1815 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1817 struct extent_map_tree
*tree
;
1819 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1820 extent_invalidatepage(tree
, page
, offset
);
1821 btrfs_releasepage(page
, GFP_NOFS
);
1825 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1826 * called from a page fault handler when a page is first dirtied. Hence we must
1827 * be careful to check for EOF conditions here. We set the page up correctly
1828 * for a written page which means we get ENOSPC checking when writing into
1829 * holes and correct delalloc and unwritten extent mapping on filesystems that
1830 * support these features.
1832 * We are not allowed to take the i_mutex here so we have to play games to
1833 * protect against truncate races as the page could now be beyond EOF. Because
1834 * vmtruncate() writes the inode size before removing pages, once we have the
1835 * page lock we can determine safely if the page is beyond EOF. If it is not
1836 * beyond EOF, then the page is guaranteed safe against truncation until we
1839 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1841 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1847 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1849 wait_on_page_writeback(page
);
1850 size
= i_size_read(inode
);
1851 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1853 if ((page
->mapping
!= inode
->i_mapping
) ||
1854 (page_start
> size
)) {
1855 /* page got truncated out from underneath us */
1859 /* page is wholly or partially inside EOF */
1860 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1861 end
= size
& ~PAGE_CACHE_MASK
;
1863 end
= PAGE_CACHE_SIZE
;
1865 ret
= btrfs_cow_one_page(inode
, page
, end
);
1868 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1873 static void btrfs_truncate(struct inode
*inode
)
1875 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1877 struct btrfs_trans_handle
*trans
;
1880 if (!S_ISREG(inode
->i_mode
))
1882 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1885 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1887 mutex_lock(&root
->fs_info
->fs_mutex
);
1888 trans
= btrfs_start_transaction(root
, 1);
1889 btrfs_set_trans_block_group(trans
, inode
);
1891 /* FIXME, add redo link to tree so we don't leak on crash */
1892 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1893 btrfs_update_inode(trans
, root
, inode
);
1894 nr
= trans
->blocks_used
;
1896 ret
= btrfs_end_transaction(trans
, root
);
1898 mutex_unlock(&root
->fs_info
->fs_mutex
);
1899 btrfs_btree_balance_dirty(root
, nr
);
1902 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1903 unsigned from
, unsigned to
)
1905 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1906 struct inode
*inode
= page
->mapping
->host
;
1908 btrfs_cow_one_page(inode
, page
, PAGE_CACHE_SIZE
);
1910 set_page_extent_mapped(page
);
1911 set_page_dirty(page
);
1913 if (pos
> inode
->i_size
) {
1914 i_size_write(inode
, pos
);
1915 mark_inode_dirty(inode
);
1920 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1922 struct btrfs_trans_handle
*trans
;
1923 struct btrfs_key key
;
1924 struct btrfs_root_item root_item
;
1925 struct btrfs_inode_item
*inode_item
;
1926 struct extent_buffer
*leaf
;
1927 struct btrfs_root
*new_root
;
1928 struct inode
*inode
;
1933 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1934 unsigned long nr
= 1;
1936 mutex_lock(&root
->fs_info
->fs_mutex
);
1937 trans
= btrfs_start_transaction(root
, 1);
1940 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1945 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1946 objectid
, trans
->transid
, 0, 0,
1949 return PTR_ERR(leaf
);
1951 btrfs_set_header_nritems(leaf
, 0);
1952 btrfs_set_header_level(leaf
, 0);
1953 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1954 btrfs_set_header_generation(leaf
, trans
->transid
);
1955 btrfs_set_header_owner(leaf
, objectid
);
1957 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1958 (unsigned long)btrfs_header_fsid(leaf
),
1960 btrfs_mark_buffer_dirty(leaf
);
1962 inode_item
= &root_item
.inode
;
1963 memset(inode_item
, 0, sizeof(*inode_item
));
1964 inode_item
->generation
= cpu_to_le64(1);
1965 inode_item
->size
= cpu_to_le64(3);
1966 inode_item
->nlink
= cpu_to_le32(1);
1967 inode_item
->nblocks
= cpu_to_le64(1);
1968 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1970 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1971 btrfs_set_root_level(&root_item
, 0);
1972 btrfs_set_root_refs(&root_item
, 1);
1973 btrfs_set_root_used(&root_item
, 0);
1975 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1976 root_item
.drop_level
= 0;
1978 free_extent_buffer(leaf
);
1981 btrfs_set_root_dirid(&root_item
, new_dirid
);
1983 key
.objectid
= objectid
;
1985 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1986 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1992 * insert the directory item
1994 key
.offset
= (u64
)-1;
1995 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1996 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1997 name
, namelen
, dir
->i_ino
, &key
,
2002 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2003 name
, namelen
, objectid
,
2004 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2008 ret
= btrfs_commit_transaction(trans
, root
);
2012 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
2015 trans
= btrfs_start_transaction(new_root
, 1);
2018 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2019 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2022 inode
->i_op
= &btrfs_dir_inode_operations
;
2023 inode
->i_fop
= &btrfs_dir_file_operations
;
2024 new_root
->inode
= inode
;
2026 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2030 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2034 nr
= trans
->blocks_used
;
2035 err
= btrfs_commit_transaction(trans
, root
);
2039 mutex_unlock(&root
->fs_info
->fs_mutex
);
2040 btrfs_btree_balance_dirty(root
, nr
);
2044 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2046 struct btrfs_trans_handle
*trans
;
2047 struct btrfs_key key
;
2048 struct btrfs_root_item new_root_item
;
2049 struct extent_buffer
*tmp
;
2055 if (!root
->ref_cows
)
2058 down_write(&root
->snap_sem
);
2059 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2060 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2062 mutex_lock(&root
->fs_info
->fs_mutex
);
2063 trans
= btrfs_start_transaction(root
, 1);
2066 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2070 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2075 memcpy(&new_root_item
, &root
->root_item
,
2076 sizeof(new_root_item
));
2078 key
.objectid
= objectid
;
2080 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2081 extent_buffer_get(root
->node
);
2082 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2083 free_extent_buffer(tmp
);
2084 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
2085 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
2086 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2092 * insert the directory item
2094 key
.offset
= (u64
)-1;
2095 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2097 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2098 &key
, BTRFS_FT_DIR
);
2103 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2104 name
, namelen
, objectid
,
2105 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2110 ret
= btrfs_inc_root_ref(trans
, root
, objectid
);
2114 nr
= trans
->blocks_used
;
2115 err
= btrfs_commit_transaction(trans
, root
);
2120 mutex_unlock(&root
->fs_info
->fs_mutex
);
2121 up_write(&root
->snap_sem
);
2122 btrfs_btree_balance_dirty(root
, nr
);
2126 static unsigned long force_ra(struct address_space
*mapping
,
2127 struct file_ra_state
*ra
, struct file
*file
,
2128 pgoff_t offset
, pgoff_t last_index
)
2132 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2133 req_size
= last_index
- offset
+ 1;
2134 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2137 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2138 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2139 return offset
+ req_size
;
2143 int btrfs_defrag_file(struct file
*file
) {
2144 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2145 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2147 unsigned long last_index
;
2148 unsigned long ra_index
= 0;
2153 mutex_lock(&inode
->i_mutex
);
2154 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2155 for (i
= 0; i
<= last_index
; i
++) {
2156 if (i
== ra_index
) {
2157 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2158 file
, ra_index
, last_index
);
2160 page
= grab_cache_page(inode
->i_mapping
, i
);
2163 if (!PageUptodate(page
)) {
2164 btrfs_readpage(NULL
, page
);
2166 if (!PageUptodate(page
)) {
2168 page_cache_release(page
);
2172 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2173 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2175 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2176 set_extent_delalloc(em_tree
, page_start
,
2177 page_end
, GFP_NOFS
);
2178 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2179 set_page_dirty(page
);
2181 page_cache_release(page
);
2182 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2186 mutex_unlock(&inode
->i_mutex
);
2190 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2192 struct btrfs_ioctl_vol_args vol_args
;
2193 struct btrfs_dir_item
*di
;
2194 struct btrfs_path
*path
;
2198 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2201 namelen
= strlen(vol_args
.name
);
2202 if (namelen
> BTRFS_VOL_NAME_MAX
)
2204 if (strchr(vol_args
.name
, '/'))
2207 path
= btrfs_alloc_path();
2211 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2212 mutex_lock(&root
->fs_info
->fs_mutex
);
2213 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2215 vol_args
.name
, namelen
, 0);
2216 mutex_unlock(&root
->fs_info
->fs_mutex
);
2217 btrfs_free_path(path
);
2218 if (di
&& !IS_ERR(di
))
2223 if (root
== root
->fs_info
->tree_root
)
2224 return create_subvol(root
, vol_args
.name
, namelen
);
2225 return create_snapshot(root
, vol_args
.name
, namelen
);
2228 static int btrfs_ioctl_defrag(struct file
*file
)
2230 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2231 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2233 switch (inode
->i_mode
& S_IFMT
) {
2235 mutex_lock(&root
->fs_info
->fs_mutex
);
2236 btrfs_defrag_root(root
, 0);
2237 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2238 mutex_unlock(&root
->fs_info
->fs_mutex
);
2241 btrfs_defrag_file(file
);
2248 long btrfs_ioctl(struct file
*file
, unsigned int
2249 cmd
, unsigned long arg
)
2251 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2254 case BTRFS_IOC_SNAP_CREATE
:
2255 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2256 case BTRFS_IOC_DEFRAG
:
2257 return btrfs_ioctl_defrag(file
);
2264 * Called inside transaction, so use GFP_NOFS
2266 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2268 struct btrfs_inode
*ei
;
2270 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2274 return &ei
->vfs_inode
;
2277 void btrfs_destroy_inode(struct inode
*inode
)
2279 WARN_ON(!list_empty(&inode
->i_dentry
));
2280 WARN_ON(inode
->i_data
.nrpages
);
2282 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2285 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2286 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2288 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2289 unsigned long flags
)
2292 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2294 inode_init_once(&ei
->vfs_inode
);
2297 void btrfs_destroy_cachep(void)
2299 if (btrfs_inode_cachep
)
2300 kmem_cache_destroy(btrfs_inode_cachep
);
2301 if (btrfs_trans_handle_cachep
)
2302 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2303 if (btrfs_transaction_cachep
)
2304 kmem_cache_destroy(btrfs_transaction_cachep
);
2305 if (btrfs_bit_radix_cachep
)
2306 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2307 if (btrfs_path_cachep
)
2308 kmem_cache_destroy(btrfs_path_cachep
);
2311 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2312 unsigned long extra_flags
,
2313 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2314 void (*ctor
)(struct kmem_cache
*, void *)
2316 void (*ctor
)(void *, struct kmem_cache
*,
2321 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2322 SLAB_MEM_SPREAD
| extra_flags
), ctor
2323 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2329 int btrfs_init_cachep(void)
2331 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2332 sizeof(struct btrfs_inode
),
2334 if (!btrfs_inode_cachep
)
2336 btrfs_trans_handle_cachep
=
2337 btrfs_cache_create("btrfs_trans_handle_cache",
2338 sizeof(struct btrfs_trans_handle
),
2340 if (!btrfs_trans_handle_cachep
)
2342 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2343 sizeof(struct btrfs_transaction
),
2345 if (!btrfs_transaction_cachep
)
2347 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2348 sizeof(struct btrfs_path
),
2350 if (!btrfs_path_cachep
)
2352 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2353 SLAB_DESTROY_BY_RCU
, NULL
);
2354 if (!btrfs_bit_radix_cachep
)
2358 btrfs_destroy_cachep();
2362 static int btrfs_getattr(struct vfsmount
*mnt
,
2363 struct dentry
*dentry
, struct kstat
*stat
)
2365 struct inode
*inode
= dentry
->d_inode
;
2366 generic_fillattr(inode
, stat
);
2367 stat
->blksize
= 256 * 1024;
2371 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2372 struct inode
* new_dir
,struct dentry
*new_dentry
)
2374 struct btrfs_trans_handle
*trans
;
2375 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2376 struct inode
*new_inode
= new_dentry
->d_inode
;
2377 struct inode
*old_inode
= old_dentry
->d_inode
;
2378 struct timespec ctime
= CURRENT_TIME
;
2379 struct btrfs_path
*path
;
2382 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2383 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2387 mutex_lock(&root
->fs_info
->fs_mutex
);
2388 trans
= btrfs_start_transaction(root
, 1);
2390 btrfs_set_trans_block_group(trans
, new_dir
);
2391 path
= btrfs_alloc_path();
2397 old_dentry
->d_inode
->i_nlink
++;
2398 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2399 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2400 old_inode
->i_ctime
= ctime
;
2402 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2407 new_inode
->i_ctime
= CURRENT_TIME
;
2408 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2412 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2417 btrfs_free_path(path
);
2418 btrfs_end_transaction(trans
, root
);
2419 mutex_unlock(&root
->fs_info
->fs_mutex
);
2423 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2424 const char *symname
)
2426 struct btrfs_trans_handle
*trans
;
2427 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2428 struct btrfs_path
*path
;
2429 struct btrfs_key key
;
2430 struct inode
*inode
;
2437 struct btrfs_file_extent_item
*ei
;
2438 struct extent_buffer
*leaf
;
2441 name_len
= strlen(symname
) + 1;
2442 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2443 return -ENAMETOOLONG
;
2444 mutex_lock(&root
->fs_info
->fs_mutex
);
2445 trans
= btrfs_start_transaction(root
, 1);
2446 btrfs_set_trans_block_group(trans
, dir
);
2448 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2454 inode
= btrfs_new_inode(trans
, root
, objectid
,
2455 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2456 err
= PTR_ERR(inode
);
2460 btrfs_set_trans_block_group(trans
, inode
);
2461 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2465 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2466 inode
->i_fop
= &btrfs_file_operations
;
2467 inode
->i_op
= &btrfs_file_inode_operations
;
2468 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2469 inode
->i_mapping
, GFP_NOFS
);
2470 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2472 dir
->i_sb
->s_dirt
= 1;
2473 btrfs_update_inode_block_group(trans
, inode
);
2474 btrfs_update_inode_block_group(trans
, dir
);
2478 path
= btrfs_alloc_path();
2480 key
.objectid
= inode
->i_ino
;
2482 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2483 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2484 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2490 leaf
= path
->nodes
[0];
2491 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2492 struct btrfs_file_extent_item
);
2493 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2494 btrfs_set_file_extent_type(leaf
, ei
,
2495 BTRFS_FILE_EXTENT_INLINE
);
2496 ptr
= btrfs_file_extent_inline_start(ei
);
2497 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2498 btrfs_mark_buffer_dirty(leaf
);
2499 btrfs_free_path(path
);
2501 inode
->i_op
= &btrfs_symlink_inode_operations
;
2502 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2503 inode
->i_size
= name_len
- 1;
2504 err
= btrfs_update_inode(trans
, root
, inode
);
2509 nr
= trans
->blocks_used
;
2510 btrfs_end_transaction(trans
, root
);
2511 mutex_unlock(&root
->fs_info
->fs_mutex
);
2513 inode_dec_link_count(inode
);
2516 btrfs_btree_balance_dirty(root
, nr
);
2520 static struct inode_operations btrfs_dir_inode_operations
= {
2521 .lookup
= btrfs_lookup
,
2522 .create
= btrfs_create
,
2523 .unlink
= btrfs_unlink
,
2525 .mkdir
= btrfs_mkdir
,
2526 .rmdir
= btrfs_rmdir
,
2527 .rename
= btrfs_rename
,
2528 .symlink
= btrfs_symlink
,
2529 .setattr
= btrfs_setattr
,
2530 .mknod
= btrfs_mknod
,
2531 .setxattr
= generic_setxattr
,
2532 .getxattr
= generic_getxattr
,
2533 .listxattr
= btrfs_listxattr
,
2534 .removexattr
= generic_removexattr
,
2537 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2538 .lookup
= btrfs_lookup
,
2541 static struct file_operations btrfs_dir_file_operations
= {
2542 .llseek
= generic_file_llseek
,
2543 .read
= generic_read_dir
,
2544 .readdir
= btrfs_readdir
,
2545 .unlocked_ioctl
= btrfs_ioctl
,
2546 #ifdef CONFIG_COMPAT
2547 .compat_ioctl
= btrfs_ioctl
,
2551 static struct extent_map_ops btrfs_extent_map_ops
= {
2552 .fill_delalloc
= run_delalloc_range
,
2553 .writepage_io_hook
= btrfs_writepage_io_hook
,
2554 .readpage_io_hook
= btrfs_readpage_io_hook
,
2555 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2558 static struct address_space_operations btrfs_aops
= {
2559 .readpage
= btrfs_readpage
,
2560 .writepage
= btrfs_writepage
,
2561 .writepages
= btrfs_writepages
,
2562 .readpages
= btrfs_readpages
,
2563 .sync_page
= block_sync_page
,
2564 .prepare_write
= btrfs_prepare_write
,
2565 .commit_write
= btrfs_commit_write
,
2567 .invalidatepage
= btrfs_invalidatepage
,
2568 .releasepage
= btrfs_releasepage
,
2569 .set_page_dirty
= __set_page_dirty_nobuffers
,
2572 static struct address_space_operations btrfs_symlink_aops
= {
2573 .readpage
= btrfs_readpage
,
2574 .writepage
= btrfs_writepage
,
2575 .invalidatepage
= btrfs_invalidatepage
,
2576 .releasepage
= btrfs_releasepage
,
2579 static struct inode_operations btrfs_file_inode_operations
= {
2580 .truncate
= btrfs_truncate
,
2581 .getattr
= btrfs_getattr
,
2582 .setattr
= btrfs_setattr
,
2583 .setxattr
= generic_setxattr
,
2584 .getxattr
= generic_getxattr
,
2585 .listxattr
= btrfs_listxattr
,
2586 .removexattr
= generic_removexattr
,
2589 static struct inode_operations btrfs_special_inode_operations
= {
2590 .getattr
= btrfs_getattr
,
2591 .setattr
= btrfs_setattr
,
2594 static struct inode_operations btrfs_symlink_inode_operations
= {
2595 .readlink
= generic_readlink
,
2596 .follow_link
= page_follow_link_light
,
2597 .put_link
= page_put_link
,