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 mutex_lock(&root
->fs_info
->fs_mutex
);
124 trans
= btrfs_start_transaction(root
, 1);
125 btrfs_set_trans_block_group(trans
, inode
);
127 btrfs_csum_file_block(trans
, root
, inode
, inode
->i_ino
,
128 start
, kaddr
+ offset
, end
- start
+ 1);
130 ret
= btrfs_end_transaction(trans
, root
);
132 mutex_unlock(&root
->fs_info
->fs_mutex
);
136 int btrfs_readpage_io_hook(struct page
*page
, u64 start
, u64 end
)
139 struct inode
*inode
= page
->mapping
->host
;
140 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
141 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
142 struct btrfs_csum_item
*item
;
143 struct btrfs_path
*path
= NULL
;
146 mutex_lock(&root
->fs_info
->fs_mutex
);
147 path
= btrfs_alloc_path();
148 item
= btrfs_lookup_csum(NULL
, root
, path
, inode
->i_ino
, start
, 0);
151 /* a csum that isn't present is a preallocated region. */
152 if (ret
== -ENOENT
|| ret
== -EFBIG
)
157 read_extent_buffer(path
->nodes
[0], &csum
, (unsigned long)item
,
159 set_state_private(em_tree
, start
, csum
);
162 btrfs_free_path(path
);
163 mutex_unlock(&root
->fs_info
->fs_mutex
);
167 int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
)
169 size_t offset
= start
- ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
170 struct inode
*inode
= page
->mapping
->host
;
171 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
175 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
179 ret
= get_state_private(em_tree
, start
, &private);
180 local_irq_save(flags
);
181 kaddr
= kmap_atomic(page
, KM_IRQ0
);
185 csum
= btrfs_csum_data(root
, kaddr
+ offset
, csum
, end
- start
+ 1);
186 btrfs_csum_final(csum
, (char *)&csum
);
187 if (csum
!= private) {
190 kunmap_atomic(kaddr
, KM_IRQ0
);
191 local_irq_restore(flags
);
195 printk("btrfs csum failed ino %lu off %llu\n",
196 page
->mapping
->host
->i_ino
, (unsigned long long)start
);
197 memset(kaddr
+ offset
, 1, end
- start
+ 1);
198 flush_dcache_page(page
);
199 kunmap_atomic(kaddr
, KM_IRQ0
);
200 local_irq_restore(flags
);
204 void btrfs_read_locked_inode(struct inode
*inode
)
206 struct btrfs_path
*path
;
207 struct extent_buffer
*leaf
;
208 struct btrfs_inode_item
*inode_item
;
209 struct btrfs_inode_timespec
*tspec
;
210 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
211 struct btrfs_key location
;
212 u64 alloc_group_block
;
216 path
= btrfs_alloc_path();
218 mutex_lock(&root
->fs_info
->fs_mutex
);
220 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
221 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
225 leaf
= path
->nodes
[0];
226 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
227 struct btrfs_inode_item
);
229 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
230 inode
->i_nlink
= btrfs_inode_nlink(leaf
, inode_item
);
231 inode
->i_uid
= btrfs_inode_uid(leaf
, inode_item
);
232 inode
->i_gid
= btrfs_inode_gid(leaf
, inode_item
);
233 inode
->i_size
= btrfs_inode_size(leaf
, inode_item
);
235 tspec
= btrfs_inode_atime(inode_item
);
236 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
237 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
239 tspec
= btrfs_inode_mtime(inode_item
);
240 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
241 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
243 tspec
= btrfs_inode_ctime(inode_item
);
244 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
245 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
247 inode
->i_blocks
= btrfs_inode_nblocks(leaf
, inode_item
);
248 inode
->i_generation
= btrfs_inode_generation(leaf
, inode_item
);
250 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
252 alloc_group_block
= btrfs_inode_block_group(leaf
, inode_item
);
253 BTRFS_I(inode
)->block_group
= btrfs_lookup_block_group(root
->fs_info
,
256 btrfs_free_path(path
);
259 mutex_unlock(&root
->fs_info
->fs_mutex
);
261 switch (inode
->i_mode
& S_IFMT
) {
263 inode
->i_mapping
->a_ops
= &btrfs_aops
;
264 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
265 inode
->i_fop
= &btrfs_file_operations
;
266 inode
->i_op
= &btrfs_file_inode_operations
;
269 inode
->i_fop
= &btrfs_dir_file_operations
;
270 if (root
== root
->fs_info
->tree_root
)
271 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
273 inode
->i_op
= &btrfs_dir_inode_operations
;
276 inode
->i_op
= &btrfs_symlink_inode_operations
;
277 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
280 init_special_inode(inode
, inode
->i_mode
, rdev
);
286 btrfs_release_path(root
, path
);
287 btrfs_free_path(path
);
288 mutex_unlock(&root
->fs_info
->fs_mutex
);
289 make_bad_inode(inode
);
292 static void fill_inode_item(struct extent_buffer
*leaf
,
293 struct btrfs_inode_item
*item
,
296 btrfs_set_inode_uid(leaf
, item
, inode
->i_uid
);
297 btrfs_set_inode_gid(leaf
, item
, inode
->i_gid
);
298 btrfs_set_inode_size(leaf
, item
, inode
->i_size
);
299 btrfs_set_inode_mode(leaf
, item
, inode
->i_mode
);
300 btrfs_set_inode_nlink(leaf
, item
, inode
->i_nlink
);
302 btrfs_set_timespec_sec(leaf
, btrfs_inode_atime(item
),
303 inode
->i_atime
.tv_sec
);
304 btrfs_set_timespec_nsec(leaf
, btrfs_inode_atime(item
),
305 inode
->i_atime
.tv_nsec
);
307 btrfs_set_timespec_sec(leaf
, btrfs_inode_mtime(item
),
308 inode
->i_mtime
.tv_sec
);
309 btrfs_set_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
310 inode
->i_mtime
.tv_nsec
);
312 btrfs_set_timespec_sec(leaf
, btrfs_inode_ctime(item
),
313 inode
->i_ctime
.tv_sec
);
314 btrfs_set_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
315 inode
->i_ctime
.tv_nsec
);
317 btrfs_set_inode_nblocks(leaf
, item
, inode
->i_blocks
);
318 btrfs_set_inode_generation(leaf
, item
, inode
->i_generation
);
319 btrfs_set_inode_rdev(leaf
, item
, inode
->i_rdev
);
320 btrfs_set_inode_block_group(leaf
, item
,
321 BTRFS_I(inode
)->block_group
->key
.objectid
);
324 int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
325 struct btrfs_root
*root
,
328 struct btrfs_inode_item
*inode_item
;
329 struct btrfs_path
*path
;
330 struct extent_buffer
*leaf
;
333 path
= btrfs_alloc_path();
335 ret
= btrfs_lookup_inode(trans
, root
, path
,
336 &BTRFS_I(inode
)->location
, 1);
343 leaf
= path
->nodes
[0];
344 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
345 struct btrfs_inode_item
);
347 fill_inode_item(leaf
, inode_item
, inode
);
348 btrfs_mark_buffer_dirty(leaf
);
349 btrfs_set_inode_last_trans(trans
, inode
);
352 btrfs_release_path(root
, path
);
353 btrfs_free_path(path
);
358 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
359 struct btrfs_root
*root
,
361 struct dentry
*dentry
)
363 struct btrfs_path
*path
;
364 const char *name
= dentry
->d_name
.name
;
365 int name_len
= dentry
->d_name
.len
;
367 struct extent_buffer
*leaf
;
368 struct btrfs_dir_item
*di
;
369 struct btrfs_key key
;
371 path
= btrfs_alloc_path();
377 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
387 leaf
= path
->nodes
[0];
388 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
389 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
392 btrfs_release_path(root
, path
);
394 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
395 key
.objectid
, name
, name_len
, -1);
404 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
406 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
408 btrfs_free_path(path
);
410 dir
->i_size
-= name_len
* 2;
411 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
412 btrfs_update_inode(trans
, root
, dir
);
413 drop_nlink(dentry
->d_inode
);
414 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
415 dir
->i_sb
->s_dirt
= 1;
420 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
422 struct btrfs_root
*root
;
423 struct btrfs_trans_handle
*trans
;
427 root
= BTRFS_I(dir
)->root
;
428 mutex_lock(&root
->fs_info
->fs_mutex
);
429 trans
= btrfs_start_transaction(root
, 1);
431 btrfs_set_trans_block_group(trans
, dir
);
432 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
433 nr
= trans
->blocks_used
;
435 btrfs_end_transaction(trans
, root
);
436 mutex_unlock(&root
->fs_info
->fs_mutex
);
437 btrfs_btree_balance_dirty(root
, nr
);
442 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
444 struct inode
*inode
= dentry
->d_inode
;
447 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
448 struct btrfs_path
*path
;
449 struct btrfs_key key
;
450 struct btrfs_trans_handle
*trans
;
451 struct btrfs_key found_key
;
453 struct extent_buffer
*leaf
;
454 char *goodnames
= "..";
457 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
460 path
= btrfs_alloc_path();
462 mutex_lock(&root
->fs_info
->fs_mutex
);
463 trans
= btrfs_start_transaction(root
, 1);
465 btrfs_set_trans_block_group(trans
, dir
);
466 key
.objectid
= inode
->i_ino
;
467 key
.offset
= (u64
)-1;
470 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
476 if (path
->slots
[0] == 0) {
481 leaf
= path
->nodes
[0];
482 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
483 found_type
= btrfs_key_type(&found_key
);
484 if (found_key
.objectid
!= inode
->i_ino
) {
488 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
489 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
490 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
491 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
495 ret
= btrfs_del_item(trans
, root
, path
);
498 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
500 btrfs_release_path(root
, path
);
503 btrfs_release_path(root
, path
);
505 /* now the directory is empty */
506 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
511 btrfs_release_path(root
, path
);
512 btrfs_free_path(path
);
513 nr
= trans
->blocks_used
;
514 ret
= btrfs_end_transaction(trans
, root
);
515 mutex_unlock(&root
->fs_info
->fs_mutex
);
516 btrfs_btree_balance_dirty(root
, nr
);
522 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
523 struct btrfs_root
*root
,
526 struct btrfs_path
*path
;
531 path
= btrfs_alloc_path();
533 ret
= btrfs_lookup_inode(trans
, root
, path
,
534 &BTRFS_I(inode
)->location
, -1);
538 ret
= btrfs_del_item(trans
, root
, path
);
539 btrfs_free_path(path
);
544 * this can truncate away extent items, csum items and directory items.
545 * It starts at a high offset and removes keys until it can't find
546 * any higher than i_size.
548 * csum items that cross the new i_size are truncated to the new size
551 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
552 struct btrfs_root
*root
,
556 struct btrfs_path
*path
;
557 struct btrfs_key key
;
558 struct btrfs_key found_key
;
560 struct extent_buffer
*leaf
;
561 struct btrfs_file_extent_item
*fi
;
562 u64 extent_start
= 0;
563 u64 extent_num_bytes
= 0;
569 int extent_type
= -1;
571 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
572 path
= btrfs_alloc_path();
576 /* FIXME, add redo link to tree so we don't leak on crash */
577 key
.objectid
= inode
->i_ino
;
578 key
.offset
= (u64
)-1;
582 btrfs_init_path(path
);
584 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
589 BUG_ON(path
->slots
[0] == 0);
592 leaf
= path
->nodes
[0];
593 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
594 found_type
= btrfs_key_type(&found_key
);
596 if (found_key
.objectid
!= inode
->i_ino
)
599 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
600 found_type
!= BTRFS_DIR_ITEM_KEY
&&
601 found_type
!= BTRFS_DIR_INDEX_KEY
&&
602 found_type
!= BTRFS_EXTENT_DATA_KEY
)
605 item_end
= found_key
.offset
;
606 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
607 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
608 struct btrfs_file_extent_item
);
609 extent_type
= btrfs_file_extent_type(leaf
, fi
);
610 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
612 btrfs_file_extent_num_bytes(leaf
, fi
);
613 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
614 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
616 item_end
+= btrfs_file_extent_inline_len(leaf
,
621 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
622 ret
= btrfs_csum_truncate(trans
, root
, path
,
626 if (item_end
< inode
->i_size
) {
627 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
628 found_type
= BTRFS_INODE_ITEM_KEY
;
629 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
630 found_type
= BTRFS_CSUM_ITEM_KEY
;
631 } else if (found_type
) {
636 btrfs_set_key_type(&key
, found_type
);
637 btrfs_release_path(root
, path
);
640 if (found_key
.offset
>= inode
->i_size
)
646 /* FIXME, shrink the extent if the ref count is only 1 */
647 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
650 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
652 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
655 btrfs_file_extent_num_bytes(leaf
, fi
);
656 extent_num_bytes
= inode
->i_size
-
657 found_key
.offset
+ root
->sectorsize
- 1;
658 btrfs_set_file_extent_num_bytes(leaf
, fi
,
660 num_dec
= (orig_num_bytes
-
661 extent_num_bytes
) >> 9;
662 if (extent_start
!= 0) {
663 inode
->i_blocks
-= num_dec
;
665 btrfs_mark_buffer_dirty(leaf
);
668 btrfs_file_extent_disk_num_bytes(leaf
,
670 /* FIXME blocksize != 4096 */
671 num_dec
= btrfs_file_extent_num_bytes(leaf
,
673 if (extent_start
!= 0) {
675 inode
->i_blocks
-= num_dec
;
677 root_gen
= btrfs_header_generation(leaf
);
678 root_owner
= btrfs_header_owner(leaf
);
680 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
682 u32 newsize
= inode
->i_size
- found_key
.offset
;
683 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
684 ret
= btrfs_truncate_item(trans
, root
, path
,
690 ret
= btrfs_del_item(trans
, root
, path
);
696 btrfs_release_path(root
, path
);
698 ret
= btrfs_free_extent(trans
, root
, extent_start
,
701 root_gen
, inode
->i_ino
,
702 found_key
.offset
, 0);
708 btrfs_release_path(root
, path
);
709 btrfs_free_path(path
);
710 inode
->i_sb
->s_dirt
= 1;
714 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
719 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
720 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
721 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
723 set_page_extent_mapped(page
);
725 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
726 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
728 if (zero_start
!= PAGE_CACHE_SIZE
) {
730 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
731 flush_dcache_page(page
);
734 set_page_dirty(page
);
735 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
741 * taken from block_truncate_page, but does cow as it zeros out
742 * any bytes left in the last page in the file.
744 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
746 struct inode
*inode
= mapping
->host
;
747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
748 u32 blocksize
= root
->sectorsize
;
749 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
750 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
755 if ((offset
& (blocksize
- 1)) == 0)
758 down_read(&root
->snap_sem
);
760 page
= grab_cache_page(mapping
, index
);
763 if (!PageUptodate(page
)) {
764 ret
= btrfs_readpage(NULL
, page
);
766 if (!PageUptodate(page
)) {
771 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
773 ret
= btrfs_cow_one_page(inode
, page
, offset
);
776 page_cache_release(page
);
777 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
782 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
784 struct inode
*inode
= dentry
->d_inode
;
787 err
= inode_change_ok(inode
, attr
);
791 if (S_ISREG(inode
->i_mode
) &&
792 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
793 struct btrfs_trans_handle
*trans
;
794 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
795 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
797 u64 mask
= root
->sectorsize
- 1;
798 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
799 u64 block_end
= attr
->ia_size
| mask
;
803 if (attr
->ia_size
<= pos
)
806 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
808 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
809 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
811 mutex_lock(&root
->fs_info
->fs_mutex
);
812 trans
= btrfs_start_transaction(root
, 1);
813 btrfs_set_trans_block_group(trans
, inode
);
814 err
= btrfs_drop_extents(trans
, root
, inode
,
815 pos
, pos
+ hole_size
, pos
,
818 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
819 err
= btrfs_insert_file_extent(trans
, root
,
821 pos
, 0, 0, hole_size
);
823 btrfs_end_transaction(trans
, root
);
824 mutex_unlock(&root
->fs_info
->fs_mutex
);
825 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
830 err
= inode_setattr(inode
, attr
);
834 void btrfs_delete_inode(struct inode
*inode
)
836 struct btrfs_trans_handle
*trans
;
837 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
841 truncate_inode_pages(&inode
->i_data
, 0);
842 if (is_bad_inode(inode
)) {
847 mutex_lock(&root
->fs_info
->fs_mutex
);
848 trans
= btrfs_start_transaction(root
, 1);
850 btrfs_set_trans_block_group(trans
, inode
);
851 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
854 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
857 ret
= btrfs_free_inode(trans
, root
, inode
);
860 nr
= trans
->blocks_used
;
862 btrfs_end_transaction(trans
, root
);
863 mutex_unlock(&root
->fs_info
->fs_mutex
);
864 btrfs_btree_balance_dirty(root
, nr
);
868 nr
= trans
->blocks_used
;
869 btrfs_end_transaction(trans
, root
);
870 mutex_unlock(&root
->fs_info
->fs_mutex
);
871 btrfs_btree_balance_dirty(root
, nr
);
877 * this returns the key found in the dir entry in the location pointer.
878 * If no dir entries were found, location->objectid is 0.
880 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
881 struct btrfs_key
*location
)
883 const char *name
= dentry
->d_name
.name
;
884 int namelen
= dentry
->d_name
.len
;
885 struct btrfs_dir_item
*di
;
886 struct btrfs_path
*path
;
887 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
890 path
= btrfs_alloc_path();
892 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
896 if (!di
|| IS_ERR(di
)) {
897 location
->objectid
= 0;
900 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
902 btrfs_release_path(root
, path
);
903 btrfs_free_path(path
);
908 * when we hit a tree root in a directory, the btrfs part of the inode
909 * needs to be changed to reflect the root directory of the tree root. This
910 * is kind of like crossing a mount point.
912 static int fixup_tree_root_location(struct btrfs_root
*root
,
913 struct btrfs_key
*location
,
914 struct btrfs_root
**sub_root
,
915 struct dentry
*dentry
)
917 struct btrfs_path
*path
;
918 struct btrfs_root_item
*ri
;
920 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
922 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
925 path
= btrfs_alloc_path();
927 mutex_lock(&root
->fs_info
->fs_mutex
);
929 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
932 if (IS_ERR(*sub_root
))
933 return PTR_ERR(*sub_root
);
935 ri
= &(*sub_root
)->root_item
;
936 location
->objectid
= btrfs_root_dirid(ri
);
937 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
938 location
->offset
= 0;
940 btrfs_free_path(path
);
941 mutex_unlock(&root
->fs_info
->fs_mutex
);
945 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
947 struct btrfs_iget_args
*args
= p
;
948 inode
->i_ino
= args
->ino
;
949 BTRFS_I(inode
)->root
= args
->root
;
950 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
951 inode
->i_mapping
, GFP_NOFS
);
955 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
957 struct btrfs_iget_args
*args
= opaque
;
958 return (args
->ino
== inode
->i_ino
&&
959 args
->root
== BTRFS_I(inode
)->root
);
962 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
963 struct btrfs_root
*root
)
966 struct btrfs_iget_args args
;
970 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
971 btrfs_init_locked_inode
,
976 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
977 struct nameidata
*nd
)
979 struct inode
* inode
;
980 struct btrfs_inode
*bi
= BTRFS_I(dir
);
981 struct btrfs_root
*root
= bi
->root
;
982 struct btrfs_root
*sub_root
= root
;
983 struct btrfs_key location
;
986 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
987 return ERR_PTR(-ENAMETOOLONG
);
989 mutex_lock(&root
->fs_info
->fs_mutex
);
990 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
991 mutex_unlock(&root
->fs_info
->fs_mutex
);
997 if (location
.objectid
) {
998 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1001 return ERR_PTR(ret
);
1003 return ERR_PTR(-ENOENT
);
1004 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1007 return ERR_PTR(-EACCES
);
1008 if (inode
->i_state
& I_NEW
) {
1009 /* the inode and parent dir are two different roots */
1010 if (sub_root
!= root
) {
1012 sub_root
->inode
= inode
;
1014 BTRFS_I(inode
)->root
= sub_root
;
1015 memcpy(&BTRFS_I(inode
)->location
, &location
,
1017 btrfs_read_locked_inode(inode
);
1018 unlock_new_inode(inode
);
1021 return d_splice_alias(inode
, dentry
);
1024 static unsigned char btrfs_filetype_table
[] = {
1025 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1028 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1030 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1031 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1032 struct btrfs_item
*item
;
1033 struct btrfs_dir_item
*di
;
1034 struct btrfs_key key
;
1035 struct btrfs_key found_key
;
1036 struct btrfs_path
*path
;
1039 struct extent_buffer
*leaf
;
1042 unsigned char d_type
;
1047 int key_type
= BTRFS_DIR_INDEX_KEY
;
1052 /* FIXME, use a real flag for deciding about the key type */
1053 if (root
->fs_info
->tree_root
== root
)
1054 key_type
= BTRFS_DIR_ITEM_KEY
;
1056 mutex_lock(&root
->fs_info
->fs_mutex
);
1057 key
.objectid
= inode
->i_ino
;
1058 btrfs_set_key_type(&key
, key_type
);
1059 key
.offset
= filp
->f_pos
;
1061 path
= btrfs_alloc_path();
1063 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1068 leaf
= path
->nodes
[0];
1069 nritems
= btrfs_header_nritems(leaf
);
1070 slot
= path
->slots
[0];
1071 if (advance
|| slot
>= nritems
) {
1072 if (slot
>= nritems
-1) {
1073 ret
= btrfs_next_leaf(root
, path
);
1076 leaf
= path
->nodes
[0];
1077 nritems
= btrfs_header_nritems(leaf
);
1078 slot
= path
->slots
[0];
1085 item
= btrfs_item_nr(leaf
, slot
);
1086 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1088 if (found_key
.objectid
!= key
.objectid
)
1090 if (btrfs_key_type(&found_key
) != key_type
)
1092 if (found_key
.offset
< filp
->f_pos
)
1095 filp
->f_pos
= found_key
.offset
;
1097 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1099 di_total
= btrfs_item_size(leaf
, item
);
1100 while(di_cur
< di_total
) {
1101 struct btrfs_key location
;
1103 name_len
= btrfs_dir_name_len(leaf
, di
);
1104 if (name_len
< 32) {
1105 name_ptr
= tmp_name
;
1107 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1110 read_extent_buffer(leaf
, name_ptr
,
1111 (unsigned long)(di
+ 1), name_len
);
1113 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1114 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1116 over
= filldir(dirent
, name_ptr
, name_len
,
1121 if (name_ptr
!= tmp_name
)
1126 di_len
= btrfs_dir_name_len(leaf
, di
) +
1127 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1129 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1136 btrfs_release_path(root
, path
);
1137 btrfs_free_path(path
);
1138 mutex_unlock(&root
->fs_info
->fs_mutex
);
1142 int btrfs_write_inode(struct inode
*inode
, int wait
)
1144 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1145 struct btrfs_trans_handle
*trans
;
1149 mutex_lock(&root
->fs_info
->fs_mutex
);
1150 trans
= btrfs_start_transaction(root
, 1);
1151 btrfs_set_trans_block_group(trans
, inode
);
1152 ret
= btrfs_commit_transaction(trans
, root
);
1153 mutex_unlock(&root
->fs_info
->fs_mutex
);
1159 * This is somewhat expensive, updating the tree every time the
1160 * inode changes. But, it is most likely to find the inode in cache.
1161 * FIXME, needs more benchmarking...there are no reasons other than performance
1162 * to keep or drop this code.
1164 void btrfs_dirty_inode(struct inode
*inode
)
1166 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1167 struct btrfs_trans_handle
*trans
;
1169 mutex_lock(&root
->fs_info
->fs_mutex
);
1170 trans
= btrfs_start_transaction(root
, 1);
1171 btrfs_set_trans_block_group(trans
, inode
);
1172 btrfs_update_inode(trans
, root
, inode
);
1173 btrfs_end_transaction(trans
, root
);
1174 mutex_unlock(&root
->fs_info
->fs_mutex
);
1177 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1178 struct btrfs_root
*root
,
1180 struct btrfs_block_group_cache
*group
,
1183 struct inode
*inode
;
1184 struct btrfs_inode_item
*inode_item
;
1185 struct btrfs_key
*location
;
1186 struct btrfs_path
*path
;
1190 path
= btrfs_alloc_path();
1193 inode
= new_inode(root
->fs_info
->sb
);
1195 return ERR_PTR(-ENOMEM
);
1197 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1198 inode
->i_mapping
, GFP_NOFS
);
1199 BTRFS_I(inode
)->root
= root
;
1205 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1206 BTRFS_I(inode
)->block_group
= group
;
1208 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1212 inode
->i_uid
= current
->fsuid
;
1213 inode
->i_gid
= current
->fsgid
;
1214 inode
->i_mode
= mode
;
1215 inode
->i_ino
= objectid
;
1216 inode
->i_blocks
= 0;
1217 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1218 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1219 struct btrfs_inode_item
);
1220 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1221 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1222 btrfs_free_path(path
);
1224 location
= &BTRFS_I(inode
)->location
;
1225 location
->objectid
= objectid
;
1226 location
->offset
= 0;
1227 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1229 insert_inode_hash(inode
);
1232 btrfs_free_path(path
);
1233 return ERR_PTR(ret
);
1236 static inline u8
btrfs_inode_type(struct inode
*inode
)
1238 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1241 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1242 struct dentry
*dentry
, struct inode
*inode
)
1245 struct btrfs_key key
;
1246 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1247 struct inode
*parent_inode
;
1249 key
.objectid
= inode
->i_ino
;
1250 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1253 ret
= btrfs_insert_dir_item(trans
, root
,
1254 dentry
->d_name
.name
, dentry
->d_name
.len
,
1255 dentry
->d_parent
->d_inode
->i_ino
,
1256 &key
, btrfs_inode_type(inode
));
1258 parent_inode
= dentry
->d_parent
->d_inode
;
1259 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1260 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1261 ret
= btrfs_update_inode(trans
, root
,
1262 dentry
->d_parent
->d_inode
);
1267 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1268 struct dentry
*dentry
, struct inode
*inode
)
1270 int err
= btrfs_add_link(trans
, dentry
, inode
);
1272 d_instantiate(dentry
, inode
);
1280 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1281 int mode
, dev_t rdev
)
1283 struct btrfs_trans_handle
*trans
;
1284 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1285 struct inode
*inode
;
1291 if (!new_valid_dev(rdev
))
1294 mutex_lock(&root
->fs_info
->fs_mutex
);
1295 trans
= btrfs_start_transaction(root
, 1);
1296 btrfs_set_trans_block_group(trans
, dir
);
1298 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1304 inode
= btrfs_new_inode(trans
, root
, objectid
,
1305 BTRFS_I(dir
)->block_group
, mode
);
1306 err
= PTR_ERR(inode
);
1310 btrfs_set_trans_block_group(trans
, inode
);
1311 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1315 inode
->i_op
= &btrfs_special_inode_operations
;
1316 init_special_inode(inode
, inode
->i_mode
, rdev
);
1317 btrfs_update_inode(trans
, root
, inode
);
1319 dir
->i_sb
->s_dirt
= 1;
1320 btrfs_update_inode_block_group(trans
, inode
);
1321 btrfs_update_inode_block_group(trans
, dir
);
1323 nr
= trans
->blocks_used
;
1324 btrfs_end_transaction(trans
, root
);
1325 mutex_unlock(&root
->fs_info
->fs_mutex
);
1328 inode_dec_link_count(inode
);
1331 btrfs_btree_balance_dirty(root
, nr
);
1335 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1336 int mode
, struct nameidata
*nd
)
1338 struct btrfs_trans_handle
*trans
;
1339 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1340 struct inode
*inode
;
1346 mutex_lock(&root
->fs_info
->fs_mutex
);
1347 trans
= btrfs_start_transaction(root
, 1);
1348 btrfs_set_trans_block_group(trans
, dir
);
1350 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1356 inode
= btrfs_new_inode(trans
, root
, objectid
,
1357 BTRFS_I(dir
)->block_group
, mode
);
1358 err
= PTR_ERR(inode
);
1362 btrfs_set_trans_block_group(trans
, inode
);
1363 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1367 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1368 inode
->i_fop
= &btrfs_file_operations
;
1369 inode
->i_op
= &btrfs_file_inode_operations
;
1370 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1371 inode
->i_mapping
, GFP_NOFS
);
1372 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1374 dir
->i_sb
->s_dirt
= 1;
1375 btrfs_update_inode_block_group(trans
, inode
);
1376 btrfs_update_inode_block_group(trans
, dir
);
1378 nr
= trans
->blocks_used
;
1379 btrfs_end_transaction(trans
, root
);
1380 mutex_unlock(&root
->fs_info
->fs_mutex
);
1383 inode_dec_link_count(inode
);
1386 btrfs_btree_balance_dirty(root
, nr
);
1390 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1391 struct dentry
*dentry
)
1393 struct btrfs_trans_handle
*trans
;
1394 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1395 struct inode
*inode
= old_dentry
->d_inode
;
1400 if (inode
->i_nlink
== 0)
1404 mutex_lock(&root
->fs_info
->fs_mutex
);
1405 trans
= btrfs_start_transaction(root
, 1);
1407 btrfs_set_trans_block_group(trans
, dir
);
1408 atomic_inc(&inode
->i_count
);
1409 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1414 dir
->i_sb
->s_dirt
= 1;
1415 btrfs_update_inode_block_group(trans
, dir
);
1416 err
= btrfs_update_inode(trans
, root
, inode
);
1421 nr
= trans
->blocks_used
;
1422 btrfs_end_transaction(trans
, root
);
1423 mutex_unlock(&root
->fs_info
->fs_mutex
);
1426 inode_dec_link_count(inode
);
1429 btrfs_btree_balance_dirty(root
, nr
);
1433 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1434 struct btrfs_root
*root
,
1435 u64 objectid
, u64 dirid
)
1439 struct btrfs_key key
;
1444 key
.objectid
= objectid
;
1446 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1448 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1449 &key
, BTRFS_FT_DIR
);
1453 key
.objectid
= dirid
;
1454 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1455 &key
, BTRFS_FT_DIR
);
1462 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1464 struct inode
*inode
;
1465 struct btrfs_trans_handle
*trans
;
1466 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1468 int drop_on_err
= 0;
1470 unsigned long nr
= 1;
1472 mutex_lock(&root
->fs_info
->fs_mutex
);
1473 trans
= btrfs_start_transaction(root
, 1);
1474 btrfs_set_trans_block_group(trans
, dir
);
1476 if (IS_ERR(trans
)) {
1477 err
= PTR_ERR(trans
);
1481 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1487 inode
= btrfs_new_inode(trans
, root
, objectid
,
1488 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1489 if (IS_ERR(inode
)) {
1490 err
= PTR_ERR(inode
);
1495 inode
->i_op
= &btrfs_dir_inode_operations
;
1496 inode
->i_fop
= &btrfs_dir_file_operations
;
1497 btrfs_set_trans_block_group(trans
, inode
);
1499 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1504 err
= btrfs_update_inode(trans
, root
, inode
);
1508 err
= btrfs_add_link(trans
, dentry
, inode
);
1512 d_instantiate(dentry
, inode
);
1514 dir
->i_sb
->s_dirt
= 1;
1515 btrfs_update_inode_block_group(trans
, inode
);
1516 btrfs_update_inode_block_group(trans
, dir
);
1519 nr
= trans
->blocks_used
;
1520 btrfs_end_transaction(trans
, root
);
1523 mutex_unlock(&root
->fs_info
->fs_mutex
);
1526 btrfs_btree_balance_dirty(root
, nr
);
1530 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1531 size_t page_offset
, u64 start
, u64 end
,
1537 u64 extent_start
= 0;
1539 u64 objectid
= inode
->i_ino
;
1541 int failed_insert
= 0;
1542 struct btrfs_path
*path
;
1543 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1544 struct btrfs_file_extent_item
*item
;
1545 struct extent_buffer
*leaf
;
1546 struct btrfs_key found_key
;
1547 struct extent_map
*em
= NULL
;
1548 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1549 struct btrfs_trans_handle
*trans
= NULL
;
1551 path
= btrfs_alloc_path();
1553 mutex_lock(&root
->fs_info
->fs_mutex
);
1556 em
= lookup_extent_mapping(em_tree
, start
, end
);
1561 em
= alloc_extent_map(GFP_NOFS
);
1566 em
->start
= EXTENT_MAP_HOLE
;
1567 em
->end
= EXTENT_MAP_HOLE
;
1569 em
->bdev
= inode
->i_sb
->s_bdev
;
1570 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1571 objectid
, start
, trans
!= NULL
);
1578 if (path
->slots
[0] == 0)
1583 leaf
= path
->nodes
[0];
1584 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1585 struct btrfs_file_extent_item
);
1586 /* are we inside the extent that was found? */
1587 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1588 found_type
= btrfs_key_type(&found_key
);
1589 if (found_key
.objectid
!= objectid
||
1590 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1594 found_type
= btrfs_file_extent_type(leaf
, item
);
1595 extent_start
= found_key
.offset
;
1596 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1597 extent_end
= extent_start
+
1598 btrfs_file_extent_num_bytes(leaf
, item
);
1600 if (start
< extent_start
|| start
>= extent_end
) {
1602 if (start
< extent_start
) {
1603 if (end
< extent_start
)
1605 em
->end
= extent_end
- 1;
1611 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1613 em
->start
= extent_start
;
1614 em
->end
= extent_end
- 1;
1615 em
->block_start
= EXTENT_MAP_HOLE
;
1616 em
->block_end
= EXTENT_MAP_HOLE
;
1619 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1620 em
->block_start
= bytenr
;
1621 em
->block_end
= em
->block_start
+
1622 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1623 em
->start
= extent_start
;
1624 em
->end
= extent_end
- 1;
1626 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1630 size_t extent_offset
;
1633 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1635 extent_end
= (extent_start
+ size
- 1) |
1636 ((u64
)root
->sectorsize
- 1);
1637 if (start
< extent_start
|| start
>= extent_end
) {
1639 if (start
< extent_start
) {
1640 if (end
< extent_start
)
1642 em
->end
= extent_end
;
1648 em
->block_start
= EXTENT_MAP_INLINE
;
1649 em
->block_end
= EXTENT_MAP_INLINE
;
1652 em
->start
= extent_start
;
1653 em
->end
= extent_start
+ size
- 1;
1657 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1658 extent_start
+ page_offset
;
1659 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1660 size
- extent_offset
);
1661 em
->start
= extent_start
+ extent_offset
;
1662 em
->end
= (em
->start
+ copy_size
-1) |
1663 ((u64
)root
->sectorsize
-1);
1665 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1666 if (create
== 0 && !PageUptodate(page
)) {
1667 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1669 flush_dcache_page(page
);
1670 } else if (create
&& PageUptodate(page
)) {
1673 free_extent_map(em
);
1675 btrfs_release_path(root
, path
);
1676 trans
= btrfs_start_transaction(root
, 1);
1679 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1681 btrfs_mark_buffer_dirty(leaf
);
1684 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1687 printk("unkknown found_type %d\n", found_type
);
1694 em
->block_start
= EXTENT_MAP_HOLE
;
1695 em
->block_end
= EXTENT_MAP_HOLE
;
1697 btrfs_release_path(root
, path
);
1698 if (em
->start
> start
|| em
->end
< start
) {
1699 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1703 ret
= add_extent_mapping(em_tree
, em
);
1704 if (ret
== -EEXIST
) {
1705 free_extent_map(em
);
1708 if (failed_insert
> 5) {
1709 printk("failing to insert %Lu %Lu\n", start
, end
);
1717 btrfs_free_path(path
);
1719 ret
= btrfs_end_transaction(trans
, root
);
1723 mutex_unlock(&root
->fs_info
->fs_mutex
);
1725 free_extent_map(em
);
1727 return ERR_PTR(err
);
1732 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1734 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1737 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1738 unsigned from
, unsigned to
)
1740 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1741 page
->mapping
->host
, page
, from
, to
,
1745 int btrfs_readpage(struct file
*file
, struct page
*page
)
1747 struct extent_map_tree
*tree
;
1748 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1749 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1751 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1753 struct extent_map_tree
*tree
;
1756 if (current
->flags
& PF_MEMALLOC
) {
1757 redirty_page_for_writepage(wbc
, page
);
1761 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1762 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1765 static int btrfs_writepages(struct address_space
*mapping
,
1766 struct writeback_control
*wbc
)
1768 struct extent_map_tree
*tree
;
1769 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1770 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
1774 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1775 struct list_head
*pages
, unsigned nr_pages
)
1777 struct extent_map_tree
*tree
;
1778 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1779 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
1783 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1785 struct extent_map_tree
*tree
;
1788 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1789 ret
= try_release_extent_mapping(tree
, page
);
1791 ClearPagePrivate(page
);
1792 set_page_private(page
, 0);
1793 page_cache_release(page
);
1798 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1800 struct extent_map_tree
*tree
;
1802 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1803 extent_invalidatepage(tree
, page
, offset
);
1804 btrfs_releasepage(page
, GFP_NOFS
);
1808 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1809 * called from a page fault handler when a page is first dirtied. Hence we must
1810 * be careful to check for EOF conditions here. We set the page up correctly
1811 * for a written page which means we get ENOSPC checking when writing into
1812 * holes and correct delalloc and unwritten extent mapping on filesystems that
1813 * support these features.
1815 * We are not allowed to take the i_mutex here so we have to play games to
1816 * protect against truncate races as the page could now be beyond EOF. Because
1817 * vmtruncate() writes the inode size before removing pages, once we have the
1818 * page lock we can determine safely if the page is beyond EOF. If it is not
1819 * beyond EOF, then the page is guaranteed safe against truncation until we
1822 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1824 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1830 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1832 wait_on_page_writeback(page
);
1833 size
= i_size_read(inode
);
1834 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1836 if ((page
->mapping
!= inode
->i_mapping
) ||
1837 (page_start
> size
)) {
1838 /* page got truncated out from underneath us */
1842 /* page is wholly or partially inside EOF */
1843 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1844 end
= size
& ~PAGE_CACHE_MASK
;
1846 end
= PAGE_CACHE_SIZE
;
1848 ret
= btrfs_cow_one_page(inode
, page
, end
);
1851 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1856 static void btrfs_truncate(struct inode
*inode
)
1858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1860 struct btrfs_trans_handle
*trans
;
1863 if (!S_ISREG(inode
->i_mode
))
1865 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1868 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1870 mutex_lock(&root
->fs_info
->fs_mutex
);
1871 trans
= btrfs_start_transaction(root
, 1);
1872 btrfs_set_trans_block_group(trans
, inode
);
1874 /* FIXME, add redo link to tree so we don't leak on crash */
1875 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1876 btrfs_update_inode(trans
, root
, inode
);
1877 nr
= trans
->blocks_used
;
1879 ret
= btrfs_end_transaction(trans
, root
);
1881 mutex_unlock(&root
->fs_info
->fs_mutex
);
1882 btrfs_btree_balance_dirty(root
, nr
);
1885 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1886 unsigned from
, unsigned to
)
1888 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1889 page
->mapping
->host
, page
, from
, to
);
1892 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1894 struct btrfs_trans_handle
*trans
;
1895 struct btrfs_key key
;
1896 struct btrfs_root_item root_item
;
1897 struct btrfs_inode_item
*inode_item
;
1898 struct extent_buffer
*leaf
;
1899 struct btrfs_root
*new_root
;
1900 struct inode
*inode
;
1905 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1906 unsigned long nr
= 1;
1908 mutex_lock(&root
->fs_info
->fs_mutex
);
1909 trans
= btrfs_start_transaction(root
, 1);
1912 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1917 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1918 objectid
, trans
->transid
, 0, 0,
1921 return PTR_ERR(leaf
);
1923 btrfs_set_header_nritems(leaf
, 0);
1924 btrfs_set_header_level(leaf
, 0);
1925 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1926 btrfs_set_header_generation(leaf
, trans
->transid
);
1927 btrfs_set_header_owner(leaf
, objectid
);
1929 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1930 (unsigned long)btrfs_header_fsid(leaf
),
1932 btrfs_mark_buffer_dirty(leaf
);
1934 inode_item
= &root_item
.inode
;
1935 memset(inode_item
, 0, sizeof(*inode_item
));
1936 inode_item
->generation
= cpu_to_le64(1);
1937 inode_item
->size
= cpu_to_le64(3);
1938 inode_item
->nlink
= cpu_to_le32(1);
1939 inode_item
->nblocks
= cpu_to_le64(1);
1940 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1942 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1943 btrfs_set_root_level(&root_item
, 0);
1944 btrfs_set_root_refs(&root_item
, 1);
1945 btrfs_set_root_used(&root_item
, 0);
1947 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1948 root_item
.drop_level
= 0;
1950 free_extent_buffer(leaf
);
1953 btrfs_set_root_dirid(&root_item
, new_dirid
);
1955 key
.objectid
= objectid
;
1957 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1958 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1964 * insert the directory item
1966 key
.offset
= (u64
)-1;
1967 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1968 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1969 name
, namelen
, dir
->i_ino
, &key
,
1974 ret
= btrfs_commit_transaction(trans
, root
);
1978 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1981 trans
= btrfs_start_transaction(new_root
, 1);
1984 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1985 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
1988 inode
->i_op
= &btrfs_dir_inode_operations
;
1989 inode
->i_fop
= &btrfs_dir_file_operations
;
1990 new_root
->inode
= inode
;
1992 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1998 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2002 nr
= trans
->blocks_used
;
2003 err
= btrfs_commit_transaction(trans
, root
);
2007 mutex_unlock(&root
->fs_info
->fs_mutex
);
2008 btrfs_btree_balance_dirty(root
, nr
);
2012 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2014 struct btrfs_trans_handle
*trans
;
2015 struct btrfs_key key
;
2016 struct btrfs_root_item new_root_item
;
2017 struct extent_buffer
*tmp
;
2023 if (!root
->ref_cows
)
2026 down_write(&root
->snap_sem
);
2027 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2028 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2030 mutex_lock(&root
->fs_info
->fs_mutex
);
2031 trans
= btrfs_start_transaction(root
, 1);
2034 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2038 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2043 memcpy(&new_root_item
, &root
->root_item
,
2044 sizeof(new_root_item
));
2046 key
.objectid
= objectid
;
2048 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2049 extent_buffer_get(root
->node
);
2050 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2051 free_extent_buffer(tmp
);
2052 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
2053 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
2054 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2060 * insert the directory item
2062 key
.offset
= (u64
)-1;
2063 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2065 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2066 &key
, BTRFS_FT_DIR
);
2071 ret
= btrfs_inc_root_ref(trans
, root
, objectid
);
2075 nr
= trans
->blocks_used
;
2076 err
= btrfs_commit_transaction(trans
, root
);
2081 mutex_unlock(&root
->fs_info
->fs_mutex
);
2082 up_write(&root
->snap_sem
);
2083 btrfs_btree_balance_dirty(root
, nr
);
2087 static unsigned long force_ra(struct address_space
*mapping
,
2088 struct file_ra_state
*ra
, struct file
*file
,
2089 pgoff_t offset
, pgoff_t last_index
)
2093 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2094 req_size
= last_index
- offset
+ 1;
2095 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2098 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2099 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2100 return offset
+ req_size
;
2104 int btrfs_defrag_file(struct file
*file
) {
2105 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2106 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2108 unsigned long last_index
;
2109 unsigned long ra_index
= 0;
2114 mutex_lock(&inode
->i_mutex
);
2115 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2116 for (i
= 0; i
<= last_index
; i
++) {
2117 if (i
== ra_index
) {
2118 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2119 file
, ra_index
, last_index
);
2121 page
= grab_cache_page(inode
->i_mapping
, i
);
2124 if (!PageUptodate(page
)) {
2125 btrfs_readpage(NULL
, page
);
2127 if (!PageUptodate(page
)) {
2129 page_cache_release(page
);
2133 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2134 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2136 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2137 set_extent_delalloc(em_tree
, page_start
,
2138 page_end
, GFP_NOFS
);
2139 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2140 set_page_dirty(page
);
2142 page_cache_release(page
);
2143 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2147 mutex_unlock(&inode
->i_mutex
);
2151 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2153 struct btrfs_ioctl_vol_args vol_args
;
2154 struct btrfs_dir_item
*di
;
2155 struct btrfs_path
*path
;
2159 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2162 namelen
= strlen(vol_args
.name
);
2163 if (namelen
> BTRFS_VOL_NAME_MAX
)
2165 if (strchr(vol_args
.name
, '/'))
2168 path
= btrfs_alloc_path();
2172 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2173 mutex_lock(&root
->fs_info
->fs_mutex
);
2174 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2176 vol_args
.name
, namelen
, 0);
2177 mutex_unlock(&root
->fs_info
->fs_mutex
);
2178 btrfs_free_path(path
);
2179 if (di
&& !IS_ERR(di
))
2184 if (root
== root
->fs_info
->tree_root
)
2185 return create_subvol(root
, vol_args
.name
, namelen
);
2186 return create_snapshot(root
, vol_args
.name
, namelen
);
2189 static int btrfs_ioctl_defrag(struct file
*file
)
2191 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2192 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2194 switch (inode
->i_mode
& S_IFMT
) {
2196 mutex_lock(&root
->fs_info
->fs_mutex
);
2197 btrfs_defrag_root(root
, 0);
2198 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2199 mutex_unlock(&root
->fs_info
->fs_mutex
);
2202 btrfs_defrag_file(file
);
2209 long btrfs_ioctl(struct file
*file
, unsigned int
2210 cmd
, unsigned long arg
)
2212 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2215 case BTRFS_IOC_SNAP_CREATE
:
2216 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2217 case BTRFS_IOC_DEFRAG
:
2218 return btrfs_ioctl_defrag(file
);
2225 * Called inside transaction, so use GFP_NOFS
2227 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2229 struct btrfs_inode
*ei
;
2231 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2235 return &ei
->vfs_inode
;
2238 void btrfs_destroy_inode(struct inode
*inode
)
2240 WARN_ON(!list_empty(&inode
->i_dentry
));
2241 WARN_ON(inode
->i_data
.nrpages
);
2243 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2246 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2247 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2249 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2250 unsigned long flags
)
2253 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2255 inode_init_once(&ei
->vfs_inode
);
2258 void btrfs_destroy_cachep(void)
2260 if (btrfs_inode_cachep
)
2261 kmem_cache_destroy(btrfs_inode_cachep
);
2262 if (btrfs_trans_handle_cachep
)
2263 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2264 if (btrfs_transaction_cachep
)
2265 kmem_cache_destroy(btrfs_transaction_cachep
);
2266 if (btrfs_bit_radix_cachep
)
2267 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2268 if (btrfs_path_cachep
)
2269 kmem_cache_destroy(btrfs_path_cachep
);
2272 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2273 unsigned long extra_flags
,
2274 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2275 void (*ctor
)(struct kmem_cache
*, void *)
2277 void (*ctor
)(void *, struct kmem_cache
*,
2282 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2283 SLAB_MEM_SPREAD
| extra_flags
), ctor
2284 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2290 int btrfs_init_cachep(void)
2292 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2293 sizeof(struct btrfs_inode
),
2295 if (!btrfs_inode_cachep
)
2297 btrfs_trans_handle_cachep
=
2298 btrfs_cache_create("btrfs_trans_handle_cache",
2299 sizeof(struct btrfs_trans_handle
),
2301 if (!btrfs_trans_handle_cachep
)
2303 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2304 sizeof(struct btrfs_transaction
),
2306 if (!btrfs_transaction_cachep
)
2308 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2309 sizeof(struct btrfs_path
),
2311 if (!btrfs_path_cachep
)
2313 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2314 SLAB_DESTROY_BY_RCU
, NULL
);
2315 if (!btrfs_bit_radix_cachep
)
2319 btrfs_destroy_cachep();
2323 static int btrfs_getattr(struct vfsmount
*mnt
,
2324 struct dentry
*dentry
, struct kstat
*stat
)
2326 struct inode
*inode
= dentry
->d_inode
;
2327 generic_fillattr(inode
, stat
);
2328 stat
->blksize
= 256 * 1024;
2332 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2333 struct inode
* new_dir
,struct dentry
*new_dentry
)
2335 struct btrfs_trans_handle
*trans
;
2336 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2337 struct inode
*new_inode
= new_dentry
->d_inode
;
2338 struct inode
*old_inode
= old_dentry
->d_inode
;
2339 struct timespec ctime
= CURRENT_TIME
;
2340 struct btrfs_path
*path
;
2341 struct btrfs_dir_item
*di
;
2344 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2345 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2349 mutex_lock(&root
->fs_info
->fs_mutex
);
2350 trans
= btrfs_start_transaction(root
, 1);
2352 btrfs_set_trans_block_group(trans
, new_dir
);
2353 path
= btrfs_alloc_path();
2359 old_dentry
->d_inode
->i_nlink
++;
2360 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2361 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2362 old_inode
->i_ctime
= ctime
;
2364 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2365 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2366 struct btrfs_key old_parent_key
;
2367 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2377 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &old_parent_key
);
2378 ret
= btrfs_del_item(trans
, root
, path
);
2382 btrfs_release_path(root
, path
);
2384 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2386 old_parent_key
.objectid
,
2396 ret
= btrfs_del_item(trans
, root
, path
);
2400 btrfs_release_path(root
, path
);
2402 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2403 old_inode
->i_ino
, location
,
2410 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2415 new_inode
->i_ctime
= CURRENT_TIME
;
2416 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2420 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2425 btrfs_free_path(path
);
2426 btrfs_end_transaction(trans
, root
);
2427 mutex_unlock(&root
->fs_info
->fs_mutex
);
2431 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2432 const char *symname
)
2434 struct btrfs_trans_handle
*trans
;
2435 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2436 struct btrfs_path
*path
;
2437 struct btrfs_key key
;
2438 struct inode
*inode
;
2445 struct btrfs_file_extent_item
*ei
;
2446 struct extent_buffer
*leaf
;
2449 name_len
= strlen(symname
) + 1;
2450 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2451 return -ENAMETOOLONG
;
2452 mutex_lock(&root
->fs_info
->fs_mutex
);
2453 trans
= btrfs_start_transaction(root
, 1);
2454 btrfs_set_trans_block_group(trans
, dir
);
2456 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2462 inode
= btrfs_new_inode(trans
, root
, objectid
,
2463 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2464 err
= PTR_ERR(inode
);
2468 btrfs_set_trans_block_group(trans
, inode
);
2469 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2473 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2474 inode
->i_fop
= &btrfs_file_operations
;
2475 inode
->i_op
= &btrfs_file_inode_operations
;
2476 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2477 inode
->i_mapping
, GFP_NOFS
);
2478 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2480 dir
->i_sb
->s_dirt
= 1;
2481 btrfs_update_inode_block_group(trans
, inode
);
2482 btrfs_update_inode_block_group(trans
, dir
);
2486 path
= btrfs_alloc_path();
2488 key
.objectid
= inode
->i_ino
;
2490 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2491 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2492 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2498 leaf
= path
->nodes
[0];
2499 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2500 struct btrfs_file_extent_item
);
2501 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2502 btrfs_set_file_extent_type(leaf
, ei
,
2503 BTRFS_FILE_EXTENT_INLINE
);
2504 ptr
= btrfs_file_extent_inline_start(ei
);
2505 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2506 btrfs_mark_buffer_dirty(leaf
);
2507 btrfs_free_path(path
);
2509 inode
->i_op
= &btrfs_symlink_inode_operations
;
2510 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2511 inode
->i_size
= name_len
- 1;
2512 err
= btrfs_update_inode(trans
, root
, inode
);
2517 nr
= trans
->blocks_used
;
2518 btrfs_end_transaction(trans
, root
);
2519 mutex_unlock(&root
->fs_info
->fs_mutex
);
2521 inode_dec_link_count(inode
);
2524 btrfs_btree_balance_dirty(root
, nr
);
2528 static struct inode_operations btrfs_dir_inode_operations
= {
2529 .lookup
= btrfs_lookup
,
2530 .create
= btrfs_create
,
2531 .unlink
= btrfs_unlink
,
2533 .mkdir
= btrfs_mkdir
,
2534 .rmdir
= btrfs_rmdir
,
2535 .rename
= btrfs_rename
,
2536 .symlink
= btrfs_symlink
,
2537 .setattr
= btrfs_setattr
,
2538 .mknod
= btrfs_mknod
,
2539 .setxattr
= generic_setxattr
,
2540 .getxattr
= generic_getxattr
,
2541 .listxattr
= btrfs_listxattr
,
2542 .removexattr
= generic_removexattr
,
2545 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2546 .lookup
= btrfs_lookup
,
2549 static struct file_operations btrfs_dir_file_operations
= {
2550 .llseek
= generic_file_llseek
,
2551 .read
= generic_read_dir
,
2552 .readdir
= btrfs_readdir
,
2553 .unlocked_ioctl
= btrfs_ioctl
,
2554 #ifdef CONFIG_COMPAT
2555 .compat_ioctl
= btrfs_ioctl
,
2559 static struct extent_map_ops btrfs_extent_map_ops
= {
2560 .fill_delalloc
= run_delalloc_range
,
2561 .writepage_io_hook
= btrfs_writepage_io_hook
,
2562 .readpage_io_hook
= btrfs_readpage_io_hook
,
2563 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2566 static struct address_space_operations btrfs_aops
= {
2567 .readpage
= btrfs_readpage
,
2568 .writepage
= btrfs_writepage
,
2569 .writepages
= btrfs_writepages
,
2570 .readpages
= btrfs_readpages
,
2571 .sync_page
= block_sync_page
,
2572 .prepare_write
= btrfs_prepare_write
,
2573 .commit_write
= btrfs_commit_write
,
2575 .invalidatepage
= btrfs_invalidatepage
,
2576 .releasepage
= btrfs_releasepage
,
2577 .set_page_dirty
= __set_page_dirty_nobuffers
,
2580 static struct address_space_operations btrfs_symlink_aops
= {
2581 .readpage
= btrfs_readpage
,
2582 .writepage
= btrfs_writepage
,
2583 .invalidatepage
= btrfs_invalidatepage
,
2584 .releasepage
= btrfs_releasepage
,
2587 static struct inode_operations btrfs_file_inode_operations
= {
2588 .truncate
= btrfs_truncate
,
2589 .getattr
= btrfs_getattr
,
2590 .setattr
= btrfs_setattr
,
2591 .setxattr
= generic_setxattr
,
2592 .getxattr
= generic_getxattr
,
2593 .listxattr
= btrfs_listxattr
,
2594 .removexattr
= generic_removexattr
,
2597 static struct inode_operations btrfs_special_inode_operations
= {
2598 .getattr
= btrfs_getattr
,
2599 .setattr
= btrfs_setattr
,
2602 static struct inode_operations btrfs_symlink_inode_operations
= {
2603 .readlink
= generic_readlink
,
2604 .follow_link
= page_follow_link_light
,
2605 .put_link
= page_put_link
,