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
;
407 if (!S_ISLNK(dentry
->d_inode
->i_mode
)) {
408 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
409 dentry
->d_inode
->i_ino
,
410 dentry
->d_parent
->d_inode
->i_ino
);
412 printk("failed to delete reference to %.*s, "
413 "inode %lu parent %lu\n", name_len
, name
,
414 dentry
->d_inode
->i_ino
,
415 dentry
->d_parent
->d_inode
->i_ino
);
419 btrfs_free_path(path
);
421 dir
->i_size
-= name_len
* 2;
422 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
423 btrfs_update_inode(trans
, root
, dir
);
424 drop_nlink(dentry
->d_inode
);
425 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
426 dir
->i_sb
->s_dirt
= 1;
431 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
433 struct btrfs_root
*root
;
434 struct btrfs_trans_handle
*trans
;
438 root
= BTRFS_I(dir
)->root
;
439 mutex_lock(&root
->fs_info
->fs_mutex
);
440 trans
= btrfs_start_transaction(root
, 1);
442 btrfs_set_trans_block_group(trans
, dir
);
443 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
444 nr
= trans
->blocks_used
;
446 btrfs_end_transaction(trans
, root
);
447 mutex_unlock(&root
->fs_info
->fs_mutex
);
448 btrfs_btree_balance_dirty(root
, nr
);
453 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
455 struct inode
*inode
= dentry
->d_inode
;
458 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
459 struct btrfs_trans_handle
*trans
;
462 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
465 mutex_lock(&root
->fs_info
->fs_mutex
);
466 trans
= btrfs_start_transaction(root
, 1);
467 btrfs_set_trans_block_group(trans
, dir
);
469 /* now the directory is empty */
470 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
475 nr
= trans
->blocks_used
;
476 ret
= btrfs_end_transaction(trans
, root
);
477 mutex_unlock(&root
->fs_info
->fs_mutex
);
478 btrfs_btree_balance_dirty(root
, nr
);
485 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
486 struct btrfs_root
*root
,
489 struct btrfs_path
*path
;
494 path
= btrfs_alloc_path();
496 ret
= btrfs_lookup_inode(trans
, root
, path
,
497 &BTRFS_I(inode
)->location
, -1);
501 ret
= btrfs_del_item(trans
, root
, path
);
502 btrfs_free_path(path
);
507 * this can truncate away extent items, csum items and directory items.
508 * It starts at a high offset and removes keys until it can't find
509 * any higher than i_size.
511 * csum items that cross the new i_size are truncated to the new size
514 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
515 struct btrfs_root
*root
,
519 struct btrfs_path
*path
;
520 struct btrfs_key key
;
521 struct btrfs_key found_key
;
523 struct extent_buffer
*leaf
;
524 struct btrfs_file_extent_item
*fi
;
525 u64 extent_start
= 0;
526 u64 extent_num_bytes
= 0;
532 int extent_type
= -1;
534 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
535 path
= btrfs_alloc_path();
539 /* FIXME, add redo link to tree so we don't leak on crash */
540 key
.objectid
= inode
->i_ino
;
541 key
.offset
= (u64
)-1;
545 btrfs_init_path(path
);
547 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
552 BUG_ON(path
->slots
[0] == 0);
555 leaf
= path
->nodes
[0];
556 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
557 found_type
= btrfs_key_type(&found_key
);
559 if (found_key
.objectid
!= inode
->i_ino
)
562 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
563 found_type
!= BTRFS_DIR_ITEM_KEY
&&
564 found_type
!= BTRFS_DIR_INDEX_KEY
&&
565 found_type
!= BTRFS_EXTENT_DATA_KEY
)
568 item_end
= found_key
.offset
;
569 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
570 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
571 struct btrfs_file_extent_item
);
572 extent_type
= btrfs_file_extent_type(leaf
, fi
);
573 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
575 btrfs_file_extent_num_bytes(leaf
, fi
);
576 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
577 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
579 item_end
+= btrfs_file_extent_inline_len(leaf
,
584 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
585 ret
= btrfs_csum_truncate(trans
, root
, path
,
589 if (item_end
< inode
->i_size
) {
590 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
591 found_type
= BTRFS_INODE_ITEM_KEY
;
592 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
593 found_type
= BTRFS_CSUM_ITEM_KEY
;
594 } else if (found_type
) {
599 btrfs_set_key_type(&key
, found_type
);
600 btrfs_release_path(root
, path
);
603 if (found_key
.offset
>= inode
->i_size
)
609 /* FIXME, shrink the extent if the ref count is only 1 */
610 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
613 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
615 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
618 btrfs_file_extent_num_bytes(leaf
, fi
);
619 extent_num_bytes
= inode
->i_size
-
620 found_key
.offset
+ root
->sectorsize
- 1;
621 btrfs_set_file_extent_num_bytes(leaf
, fi
,
623 num_dec
= (orig_num_bytes
-
624 extent_num_bytes
) >> 9;
625 if (extent_start
!= 0) {
626 inode
->i_blocks
-= num_dec
;
628 btrfs_mark_buffer_dirty(leaf
);
631 btrfs_file_extent_disk_num_bytes(leaf
,
633 /* FIXME blocksize != 4096 */
634 num_dec
= btrfs_file_extent_num_bytes(leaf
,
636 if (extent_start
!= 0) {
638 inode
->i_blocks
-= num_dec
;
640 root_gen
= btrfs_header_generation(leaf
);
641 root_owner
= btrfs_header_owner(leaf
);
643 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
645 u32 newsize
= inode
->i_size
- found_key
.offset
;
646 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
647 ret
= btrfs_truncate_item(trans
, root
, path
,
653 ret
= btrfs_del_item(trans
, root
, path
);
659 btrfs_release_path(root
, path
);
661 ret
= btrfs_free_extent(trans
, root
, extent_start
,
664 root_gen
, inode
->i_ino
,
665 found_key
.offset
, 0);
671 btrfs_release_path(root
, path
);
672 btrfs_free_path(path
);
673 inode
->i_sb
->s_dirt
= 1;
677 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
682 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
683 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
684 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
686 set_page_extent_mapped(page
);
688 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
689 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
691 if (zero_start
!= PAGE_CACHE_SIZE
) {
693 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
694 flush_dcache_page(page
);
697 set_page_dirty(page
);
698 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
704 * taken from block_truncate_page, but does cow as it zeros out
705 * any bytes left in the last page in the file.
707 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
709 struct inode
*inode
= mapping
->host
;
710 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
711 u32 blocksize
= root
->sectorsize
;
712 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
713 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
718 if ((offset
& (blocksize
- 1)) == 0)
721 down_read(&root
->snap_sem
);
723 page
= grab_cache_page(mapping
, index
);
726 if (!PageUptodate(page
)) {
727 ret
= btrfs_readpage(NULL
, page
);
729 if (!PageUptodate(page
)) {
734 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
736 ret
= btrfs_cow_one_page(inode
, page
, offset
);
739 page_cache_release(page
);
740 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
745 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
747 struct inode
*inode
= dentry
->d_inode
;
750 err
= inode_change_ok(inode
, attr
);
754 if (S_ISREG(inode
->i_mode
) &&
755 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
756 struct btrfs_trans_handle
*trans
;
757 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
758 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
760 u64 mask
= root
->sectorsize
- 1;
761 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
762 u64 block_end
= attr
->ia_size
| mask
;
766 if (attr
->ia_size
<= pos
)
769 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
771 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
772 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
774 mutex_lock(&root
->fs_info
->fs_mutex
);
775 trans
= btrfs_start_transaction(root
, 1);
776 btrfs_set_trans_block_group(trans
, inode
);
777 err
= btrfs_drop_extents(trans
, root
, inode
,
778 pos
, pos
+ hole_size
, pos
,
781 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
782 err
= btrfs_insert_file_extent(trans
, root
,
784 pos
, 0, 0, hole_size
);
786 btrfs_end_transaction(trans
, root
);
787 mutex_unlock(&root
->fs_info
->fs_mutex
);
788 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
793 err
= inode_setattr(inode
, attr
);
797 void btrfs_delete_inode(struct inode
*inode
)
799 struct btrfs_trans_handle
*trans
;
800 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
804 truncate_inode_pages(&inode
->i_data
, 0);
805 if (is_bad_inode(inode
)) {
810 mutex_lock(&root
->fs_info
->fs_mutex
);
811 trans
= btrfs_start_transaction(root
, 1);
813 btrfs_set_trans_block_group(trans
, inode
);
814 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
817 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
820 ret
= btrfs_free_inode(trans
, root
, inode
);
823 nr
= trans
->blocks_used
;
825 btrfs_end_transaction(trans
, root
);
826 mutex_unlock(&root
->fs_info
->fs_mutex
);
827 btrfs_btree_balance_dirty(root
, nr
);
831 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
);
840 * this returns the key found in the dir entry in the location pointer.
841 * If no dir entries were found, location->objectid is 0.
843 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
844 struct btrfs_key
*location
)
846 const char *name
= dentry
->d_name
.name
;
847 int namelen
= dentry
->d_name
.len
;
848 struct btrfs_dir_item
*di
;
849 struct btrfs_path
*path
;
850 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
853 if (namelen
== 1 && strcmp(name
, ".") == 0) {
854 location
->objectid
= dir
->i_ino
;
855 location
->type
= BTRFS_INODE_ITEM_KEY
;
856 location
->offset
= 0;
859 path
= btrfs_alloc_path();
862 if (namelen
== 1 && strcmp(name
, "..") == 0) {
863 struct btrfs_key key
;
864 struct extent_buffer
*leaf
;
868 key
.objectid
= dir
->i_ino
;
869 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
871 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
875 leaf
= path
->nodes
[0];
876 slot
= path
->slots
[0];
877 nritems
= btrfs_header_nritems(leaf
);
881 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
882 if (key
.objectid
!= dir
->i_ino
||
883 key
.type
!= BTRFS_INODE_REF_KEY
) {
886 location
->objectid
= key
.offset
;
887 location
->type
= BTRFS_INODE_ITEM_KEY
;
888 location
->offset
= 0;
892 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
896 if (!di
|| IS_ERR(di
)) {
899 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
901 btrfs_free_path(path
);
904 location
->objectid
= 0;
909 * when we hit a tree root in a directory, the btrfs part of the inode
910 * needs to be changed to reflect the root directory of the tree root. This
911 * is kind of like crossing a mount point.
913 static int fixup_tree_root_location(struct btrfs_root
*root
,
914 struct btrfs_key
*location
,
915 struct btrfs_root
**sub_root
,
916 struct dentry
*dentry
)
918 struct btrfs_path
*path
;
919 struct btrfs_root_item
*ri
;
921 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
923 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
926 path
= btrfs_alloc_path();
928 mutex_lock(&root
->fs_info
->fs_mutex
);
930 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
933 if (IS_ERR(*sub_root
))
934 return PTR_ERR(*sub_root
);
936 ri
= &(*sub_root
)->root_item
;
937 location
->objectid
= btrfs_root_dirid(ri
);
938 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
939 location
->offset
= 0;
941 btrfs_free_path(path
);
942 mutex_unlock(&root
->fs_info
->fs_mutex
);
946 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
948 struct btrfs_iget_args
*args
= p
;
949 inode
->i_ino
= args
->ino
;
950 BTRFS_I(inode
)->root
= args
->root
;
951 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
952 inode
->i_mapping
, GFP_NOFS
);
956 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
958 struct btrfs_iget_args
*args
= opaque
;
959 return (args
->ino
== inode
->i_ino
&&
960 args
->root
== BTRFS_I(inode
)->root
);
963 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
964 struct btrfs_root
*root
)
967 struct btrfs_iget_args args
;
971 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
972 btrfs_init_locked_inode
,
977 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
978 struct nameidata
*nd
)
980 struct inode
* inode
;
981 struct btrfs_inode
*bi
= BTRFS_I(dir
);
982 struct btrfs_root
*root
= bi
->root
;
983 struct btrfs_root
*sub_root
= root
;
984 struct btrfs_key location
;
987 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
988 return ERR_PTR(-ENAMETOOLONG
);
990 mutex_lock(&root
->fs_info
->fs_mutex
);
991 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
992 mutex_unlock(&root
->fs_info
->fs_mutex
);
998 if (location
.objectid
) {
999 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1002 return ERR_PTR(ret
);
1004 return ERR_PTR(-ENOENT
);
1005 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1008 return ERR_PTR(-EACCES
);
1009 if (inode
->i_state
& I_NEW
) {
1010 /* the inode and parent dir are two different roots */
1011 if (sub_root
!= root
) {
1013 sub_root
->inode
= inode
;
1015 BTRFS_I(inode
)->root
= sub_root
;
1016 memcpy(&BTRFS_I(inode
)->location
, &location
,
1018 btrfs_read_locked_inode(inode
);
1019 unlock_new_inode(inode
);
1022 return d_splice_alias(inode
, dentry
);
1025 static unsigned char btrfs_filetype_table
[] = {
1026 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1029 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1031 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1032 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1033 struct btrfs_item
*item
;
1034 struct btrfs_dir_item
*di
;
1035 struct btrfs_key key
;
1036 struct btrfs_key found_key
;
1037 struct btrfs_path
*path
;
1040 struct extent_buffer
*leaf
;
1043 unsigned char d_type
;
1048 int key_type
= BTRFS_DIR_INDEX_KEY
;
1053 /* FIXME, use a real flag for deciding about the key type */
1054 if (root
->fs_info
->tree_root
== root
)
1055 key_type
= BTRFS_DIR_ITEM_KEY
;
1057 /* special case for "." */
1058 if (filp
->f_pos
== 0) {
1059 over
= filldir(dirent
, ".", 1,
1067 mutex_lock(&root
->fs_info
->fs_mutex
);
1068 key
.objectid
= inode
->i_ino
;
1069 path
= btrfs_alloc_path();
1072 /* special case for .., just use the back ref */
1073 if (filp
->f_pos
== 1) {
1074 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1076 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1078 leaf
= path
->nodes
[0];
1079 slot
= path
->slots
[0];
1080 nritems
= btrfs_header_nritems(leaf
);
1081 if (slot
>= nritems
) {
1082 btrfs_release_path(root
, path
);
1083 goto read_dir_items
;
1085 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1086 btrfs_release_path(root
, path
);
1087 if (found_key
.objectid
!= key
.objectid
||
1088 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1089 goto read_dir_items
;
1090 over
= filldir(dirent
, "..", 2,
1091 2, found_key
.offset
, DT_DIR
);
1098 btrfs_set_key_type(&key
, key_type
);
1099 key
.offset
= filp
->f_pos
;
1101 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1106 leaf
= path
->nodes
[0];
1107 nritems
= btrfs_header_nritems(leaf
);
1108 slot
= path
->slots
[0];
1109 if (advance
|| slot
>= nritems
) {
1110 if (slot
>= nritems
-1) {
1111 ret
= btrfs_next_leaf(root
, path
);
1114 leaf
= path
->nodes
[0];
1115 nritems
= btrfs_header_nritems(leaf
);
1116 slot
= path
->slots
[0];
1123 item
= btrfs_item_nr(leaf
, slot
);
1124 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1126 if (found_key
.objectid
!= key
.objectid
)
1128 if (btrfs_key_type(&found_key
) != key_type
)
1130 if (found_key
.offset
< filp
->f_pos
)
1133 filp
->f_pos
= found_key
.offset
;
1135 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1137 di_total
= btrfs_item_size(leaf
, item
);
1138 while(di_cur
< di_total
) {
1139 struct btrfs_key location
;
1141 name_len
= btrfs_dir_name_len(leaf
, di
);
1142 if (name_len
< 32) {
1143 name_ptr
= tmp_name
;
1145 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1148 read_extent_buffer(leaf
, name_ptr
,
1149 (unsigned long)(di
+ 1), name_len
);
1151 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1152 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1154 over
= filldir(dirent
, name_ptr
, name_len
,
1159 if (name_ptr
!= tmp_name
)
1164 di_len
= btrfs_dir_name_len(leaf
, di
) +
1165 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1167 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1174 btrfs_release_path(root
, path
);
1175 btrfs_free_path(path
);
1176 mutex_unlock(&root
->fs_info
->fs_mutex
);
1180 int btrfs_write_inode(struct inode
*inode
, int wait
)
1182 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1183 struct btrfs_trans_handle
*trans
;
1187 mutex_lock(&root
->fs_info
->fs_mutex
);
1188 trans
= btrfs_start_transaction(root
, 1);
1189 btrfs_set_trans_block_group(trans
, inode
);
1190 ret
= btrfs_commit_transaction(trans
, root
);
1191 mutex_unlock(&root
->fs_info
->fs_mutex
);
1197 * This is somewhat expensive, updating the tree every time the
1198 * inode changes. But, it is most likely to find the inode in cache.
1199 * FIXME, needs more benchmarking...there are no reasons other than performance
1200 * to keep or drop this code.
1202 void btrfs_dirty_inode(struct inode
*inode
)
1204 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1205 struct btrfs_trans_handle
*trans
;
1207 mutex_lock(&root
->fs_info
->fs_mutex
);
1208 trans
= btrfs_start_transaction(root
, 1);
1209 btrfs_set_trans_block_group(trans
, inode
);
1210 btrfs_update_inode(trans
, root
, inode
);
1211 btrfs_end_transaction(trans
, root
);
1212 mutex_unlock(&root
->fs_info
->fs_mutex
);
1215 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1216 struct btrfs_root
*root
,
1218 struct btrfs_block_group_cache
*group
,
1221 struct inode
*inode
;
1222 struct btrfs_inode_item
*inode_item
;
1223 struct btrfs_key
*location
;
1224 struct btrfs_path
*path
;
1228 path
= btrfs_alloc_path();
1231 inode
= new_inode(root
->fs_info
->sb
);
1233 return ERR_PTR(-ENOMEM
);
1235 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1236 inode
->i_mapping
, GFP_NOFS
);
1237 BTRFS_I(inode
)->root
= root
;
1243 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1244 BTRFS_I(inode
)->block_group
= group
;
1246 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1250 inode
->i_uid
= current
->fsuid
;
1251 inode
->i_gid
= current
->fsgid
;
1252 inode
->i_mode
= mode
;
1253 inode
->i_ino
= objectid
;
1254 inode
->i_blocks
= 0;
1255 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1256 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1257 struct btrfs_inode_item
);
1258 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1259 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1260 btrfs_free_path(path
);
1262 location
= &BTRFS_I(inode
)->location
;
1263 location
->objectid
= objectid
;
1264 location
->offset
= 0;
1265 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1267 insert_inode_hash(inode
);
1270 btrfs_free_path(path
);
1271 return ERR_PTR(ret
);
1274 static inline u8
btrfs_inode_type(struct inode
*inode
)
1276 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1279 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1280 struct dentry
*dentry
, struct inode
*inode
)
1283 struct btrfs_key key
;
1284 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1285 struct inode
*parent_inode
;
1287 key
.objectid
= inode
->i_ino
;
1288 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1291 ret
= btrfs_insert_dir_item(trans
, root
,
1292 dentry
->d_name
.name
, dentry
->d_name
.len
,
1293 dentry
->d_parent
->d_inode
->i_ino
,
1294 &key
, btrfs_inode_type(inode
));
1296 if (!S_ISLNK(inode
->i_mode
)) {
1297 ret
= btrfs_insert_inode_ref(trans
, root
,
1298 dentry
->d_name
.name
,
1301 dentry
->d_parent
->d_inode
->i_ino
);
1303 parent_inode
= dentry
->d_parent
->d_inode
;
1304 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1305 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1306 ret
= btrfs_update_inode(trans
, root
,
1307 dentry
->d_parent
->d_inode
);
1312 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1313 struct dentry
*dentry
, struct inode
*inode
)
1315 int err
= btrfs_add_link(trans
, dentry
, inode
);
1317 d_instantiate(dentry
, inode
);
1325 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1326 int mode
, dev_t rdev
)
1328 struct btrfs_trans_handle
*trans
;
1329 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1330 struct inode
*inode
;
1336 if (!new_valid_dev(rdev
))
1339 mutex_lock(&root
->fs_info
->fs_mutex
);
1340 trans
= btrfs_start_transaction(root
, 1);
1341 btrfs_set_trans_block_group(trans
, dir
);
1343 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1349 inode
= btrfs_new_inode(trans
, root
, objectid
,
1350 BTRFS_I(dir
)->block_group
, mode
);
1351 err
= PTR_ERR(inode
);
1355 btrfs_set_trans_block_group(trans
, inode
);
1356 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1360 inode
->i_op
= &btrfs_special_inode_operations
;
1361 init_special_inode(inode
, inode
->i_mode
, rdev
);
1362 btrfs_update_inode(trans
, root
, inode
);
1364 dir
->i_sb
->s_dirt
= 1;
1365 btrfs_update_inode_block_group(trans
, inode
);
1366 btrfs_update_inode_block_group(trans
, dir
);
1368 nr
= trans
->blocks_used
;
1369 btrfs_end_transaction(trans
, root
);
1370 mutex_unlock(&root
->fs_info
->fs_mutex
);
1373 inode_dec_link_count(inode
);
1376 btrfs_btree_balance_dirty(root
, nr
);
1380 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1381 int mode
, struct nameidata
*nd
)
1383 struct btrfs_trans_handle
*trans
;
1384 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1385 struct inode
*inode
;
1391 mutex_lock(&root
->fs_info
->fs_mutex
);
1392 trans
= btrfs_start_transaction(root
, 1);
1393 btrfs_set_trans_block_group(trans
, dir
);
1395 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1401 inode
= btrfs_new_inode(trans
, root
, objectid
,
1402 BTRFS_I(dir
)->block_group
, mode
);
1403 err
= PTR_ERR(inode
);
1407 btrfs_set_trans_block_group(trans
, inode
);
1408 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1412 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1413 inode
->i_fop
= &btrfs_file_operations
;
1414 inode
->i_op
= &btrfs_file_inode_operations
;
1415 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1416 inode
->i_mapping
, GFP_NOFS
);
1417 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1419 dir
->i_sb
->s_dirt
= 1;
1420 btrfs_update_inode_block_group(trans
, inode
);
1421 btrfs_update_inode_block_group(trans
, dir
);
1423 nr
= trans
->blocks_used
;
1424 btrfs_end_transaction(trans
, root
);
1425 mutex_unlock(&root
->fs_info
->fs_mutex
);
1428 inode_dec_link_count(inode
);
1431 btrfs_btree_balance_dirty(root
, nr
);
1435 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1436 struct dentry
*dentry
)
1438 struct btrfs_trans_handle
*trans
;
1439 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1440 struct inode
*inode
= old_dentry
->d_inode
;
1445 if (inode
->i_nlink
== 0)
1449 mutex_lock(&root
->fs_info
->fs_mutex
);
1450 trans
= btrfs_start_transaction(root
, 1);
1452 btrfs_set_trans_block_group(trans
, dir
);
1453 atomic_inc(&inode
->i_count
);
1454 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1459 dir
->i_sb
->s_dirt
= 1;
1460 btrfs_update_inode_block_group(trans
, dir
);
1461 err
= btrfs_update_inode(trans
, root
, inode
);
1466 nr
= trans
->blocks_used
;
1467 btrfs_end_transaction(trans
, root
);
1468 mutex_unlock(&root
->fs_info
->fs_mutex
);
1471 inode_dec_link_count(inode
);
1474 btrfs_btree_balance_dirty(root
, nr
);
1478 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1480 struct inode
*inode
;
1481 struct btrfs_trans_handle
*trans
;
1482 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1484 int drop_on_err
= 0;
1486 unsigned long nr
= 1;
1488 mutex_lock(&root
->fs_info
->fs_mutex
);
1489 trans
= btrfs_start_transaction(root
, 1);
1490 btrfs_set_trans_block_group(trans
, dir
);
1492 if (IS_ERR(trans
)) {
1493 err
= PTR_ERR(trans
);
1497 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1503 inode
= btrfs_new_inode(trans
, root
, objectid
,
1504 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1505 if (IS_ERR(inode
)) {
1506 err
= PTR_ERR(inode
);
1511 inode
->i_op
= &btrfs_dir_inode_operations
;
1512 inode
->i_fop
= &btrfs_dir_file_operations
;
1513 btrfs_set_trans_block_group(trans
, inode
);
1516 err
= btrfs_update_inode(trans
, root
, inode
);
1520 err
= btrfs_add_link(trans
, dentry
, inode
);
1524 d_instantiate(dentry
, inode
);
1526 dir
->i_sb
->s_dirt
= 1;
1527 btrfs_update_inode_block_group(trans
, inode
);
1528 btrfs_update_inode_block_group(trans
, dir
);
1531 nr
= trans
->blocks_used
;
1532 btrfs_end_transaction(trans
, root
);
1535 mutex_unlock(&root
->fs_info
->fs_mutex
);
1538 btrfs_btree_balance_dirty(root
, nr
);
1542 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1543 size_t page_offset
, u64 start
, u64 end
,
1549 u64 extent_start
= 0;
1551 u64 objectid
= inode
->i_ino
;
1553 int failed_insert
= 0;
1554 struct btrfs_path
*path
;
1555 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1556 struct btrfs_file_extent_item
*item
;
1557 struct extent_buffer
*leaf
;
1558 struct btrfs_key found_key
;
1559 struct extent_map
*em
= NULL
;
1560 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1561 struct btrfs_trans_handle
*trans
= NULL
;
1563 path
= btrfs_alloc_path();
1565 mutex_lock(&root
->fs_info
->fs_mutex
);
1568 em
= lookup_extent_mapping(em_tree
, start
, end
);
1573 em
= alloc_extent_map(GFP_NOFS
);
1578 em
->start
= EXTENT_MAP_HOLE
;
1579 em
->end
= EXTENT_MAP_HOLE
;
1581 em
->bdev
= inode
->i_sb
->s_bdev
;
1582 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1583 objectid
, start
, trans
!= NULL
);
1590 if (path
->slots
[0] == 0)
1595 leaf
= path
->nodes
[0];
1596 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1597 struct btrfs_file_extent_item
);
1598 /* are we inside the extent that was found? */
1599 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1600 found_type
= btrfs_key_type(&found_key
);
1601 if (found_key
.objectid
!= objectid
||
1602 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1606 found_type
= btrfs_file_extent_type(leaf
, item
);
1607 extent_start
= found_key
.offset
;
1608 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1609 extent_end
= extent_start
+
1610 btrfs_file_extent_num_bytes(leaf
, item
);
1612 if (start
< extent_start
|| start
>= extent_end
) {
1614 if (start
< extent_start
) {
1615 if (end
< extent_start
)
1617 em
->end
= extent_end
- 1;
1623 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1625 em
->start
= extent_start
;
1626 em
->end
= extent_end
- 1;
1627 em
->block_start
= EXTENT_MAP_HOLE
;
1628 em
->block_end
= EXTENT_MAP_HOLE
;
1631 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1632 em
->block_start
= bytenr
;
1633 em
->block_end
= em
->block_start
+
1634 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1635 em
->start
= extent_start
;
1636 em
->end
= extent_end
- 1;
1638 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1642 size_t extent_offset
;
1645 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1647 extent_end
= (extent_start
+ size
- 1) |
1648 ((u64
)root
->sectorsize
- 1);
1649 if (start
< extent_start
|| start
>= extent_end
) {
1651 if (start
< extent_start
) {
1652 if (end
< extent_start
)
1654 em
->end
= extent_end
;
1660 em
->block_start
= EXTENT_MAP_INLINE
;
1661 em
->block_end
= EXTENT_MAP_INLINE
;
1664 em
->start
= extent_start
;
1665 em
->end
= extent_start
+ size
- 1;
1669 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1670 extent_start
+ page_offset
;
1671 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1672 size
- extent_offset
);
1673 em
->start
= extent_start
+ extent_offset
;
1674 em
->end
= (em
->start
+ copy_size
-1) |
1675 ((u64
)root
->sectorsize
-1);
1677 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1678 if (create
== 0 && !PageUptodate(page
)) {
1679 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1681 flush_dcache_page(page
);
1682 } else if (create
&& PageUptodate(page
)) {
1685 free_extent_map(em
);
1687 btrfs_release_path(root
, path
);
1688 trans
= btrfs_start_transaction(root
, 1);
1691 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1693 btrfs_mark_buffer_dirty(leaf
);
1696 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1699 printk("unkknown found_type %d\n", found_type
);
1706 em
->block_start
= EXTENT_MAP_HOLE
;
1707 em
->block_end
= EXTENT_MAP_HOLE
;
1709 btrfs_release_path(root
, path
);
1710 if (em
->start
> start
|| em
->end
< start
) {
1711 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1715 ret
= add_extent_mapping(em_tree
, em
);
1716 if (ret
== -EEXIST
) {
1717 free_extent_map(em
);
1720 if (failed_insert
> 5) {
1721 printk("failing to insert %Lu %Lu\n", start
, end
);
1729 btrfs_free_path(path
);
1731 ret
= btrfs_end_transaction(trans
, root
);
1735 mutex_unlock(&root
->fs_info
->fs_mutex
);
1737 free_extent_map(em
);
1739 return ERR_PTR(err
);
1744 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1746 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1749 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1750 unsigned from
, unsigned to
)
1752 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1753 page
->mapping
->host
, page
, from
, to
,
1757 int btrfs_readpage(struct file
*file
, struct page
*page
)
1759 struct extent_map_tree
*tree
;
1760 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1761 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1763 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1765 struct extent_map_tree
*tree
;
1768 if (current
->flags
& PF_MEMALLOC
) {
1769 redirty_page_for_writepage(wbc
, page
);
1773 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1774 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1777 static int btrfs_writepages(struct address_space
*mapping
,
1778 struct writeback_control
*wbc
)
1780 struct extent_map_tree
*tree
;
1781 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1782 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
1786 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1787 struct list_head
*pages
, unsigned nr_pages
)
1789 struct extent_map_tree
*tree
;
1790 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1791 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
1795 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1797 struct extent_map_tree
*tree
;
1800 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1801 ret
= try_release_extent_mapping(tree
, page
);
1803 ClearPagePrivate(page
);
1804 set_page_private(page
, 0);
1805 page_cache_release(page
);
1810 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1812 struct extent_map_tree
*tree
;
1814 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1815 extent_invalidatepage(tree
, page
, offset
);
1816 btrfs_releasepage(page
, GFP_NOFS
);
1820 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1821 * called from a page fault handler when a page is first dirtied. Hence we must
1822 * be careful to check for EOF conditions here. We set the page up correctly
1823 * for a written page which means we get ENOSPC checking when writing into
1824 * holes and correct delalloc and unwritten extent mapping on filesystems that
1825 * support these features.
1827 * We are not allowed to take the i_mutex here so we have to play games to
1828 * protect against truncate races as the page could now be beyond EOF. Because
1829 * vmtruncate() writes the inode size before removing pages, once we have the
1830 * page lock we can determine safely if the page is beyond EOF. If it is not
1831 * beyond EOF, then the page is guaranteed safe against truncation until we
1834 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1836 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1842 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1844 wait_on_page_writeback(page
);
1845 size
= i_size_read(inode
);
1846 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1848 if ((page
->mapping
!= inode
->i_mapping
) ||
1849 (page_start
> size
)) {
1850 /* page got truncated out from underneath us */
1854 /* page is wholly or partially inside EOF */
1855 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1856 end
= size
& ~PAGE_CACHE_MASK
;
1858 end
= PAGE_CACHE_SIZE
;
1860 ret
= btrfs_cow_one_page(inode
, page
, end
);
1863 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1868 static void btrfs_truncate(struct inode
*inode
)
1870 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1872 struct btrfs_trans_handle
*trans
;
1875 if (!S_ISREG(inode
->i_mode
))
1877 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1880 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1882 mutex_lock(&root
->fs_info
->fs_mutex
);
1883 trans
= btrfs_start_transaction(root
, 1);
1884 btrfs_set_trans_block_group(trans
, inode
);
1886 /* FIXME, add redo link to tree so we don't leak on crash */
1887 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1888 btrfs_update_inode(trans
, root
, inode
);
1889 nr
= trans
->blocks_used
;
1891 ret
= btrfs_end_transaction(trans
, root
);
1893 mutex_unlock(&root
->fs_info
->fs_mutex
);
1894 btrfs_btree_balance_dirty(root
, nr
);
1897 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1898 unsigned from
, unsigned to
)
1900 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1901 page
->mapping
->host
, page
, from
, to
);
1904 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1906 struct btrfs_trans_handle
*trans
;
1907 struct btrfs_key key
;
1908 struct btrfs_root_item root_item
;
1909 struct btrfs_inode_item
*inode_item
;
1910 struct extent_buffer
*leaf
;
1911 struct btrfs_root
*new_root
;
1912 struct inode
*inode
;
1917 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1918 unsigned long nr
= 1;
1920 mutex_lock(&root
->fs_info
->fs_mutex
);
1921 trans
= btrfs_start_transaction(root
, 1);
1924 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1929 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1930 objectid
, trans
->transid
, 0, 0,
1933 return PTR_ERR(leaf
);
1935 btrfs_set_header_nritems(leaf
, 0);
1936 btrfs_set_header_level(leaf
, 0);
1937 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1938 btrfs_set_header_generation(leaf
, trans
->transid
);
1939 btrfs_set_header_owner(leaf
, objectid
);
1941 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1942 (unsigned long)btrfs_header_fsid(leaf
),
1944 btrfs_mark_buffer_dirty(leaf
);
1946 inode_item
= &root_item
.inode
;
1947 memset(inode_item
, 0, sizeof(*inode_item
));
1948 inode_item
->generation
= cpu_to_le64(1);
1949 inode_item
->size
= cpu_to_le64(3);
1950 inode_item
->nlink
= cpu_to_le32(1);
1951 inode_item
->nblocks
= cpu_to_le64(1);
1952 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1954 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1955 btrfs_set_root_level(&root_item
, 0);
1956 btrfs_set_root_refs(&root_item
, 1);
1957 btrfs_set_root_used(&root_item
, 0);
1959 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1960 root_item
.drop_level
= 0;
1962 free_extent_buffer(leaf
);
1965 btrfs_set_root_dirid(&root_item
, new_dirid
);
1967 key
.objectid
= objectid
;
1969 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1970 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1976 * insert the directory item
1978 key
.offset
= (u64
)-1;
1979 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1980 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1981 name
, namelen
, dir
->i_ino
, &key
,
1986 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
1987 name
, namelen
, objectid
,
1988 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
1992 ret
= btrfs_commit_transaction(trans
, root
);
1996 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1999 trans
= btrfs_start_transaction(new_root
, 1);
2002 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2003 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2006 inode
->i_op
= &btrfs_dir_inode_operations
;
2007 inode
->i_fop
= &btrfs_dir_file_operations
;
2008 new_root
->inode
= inode
;
2010 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2014 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2018 nr
= trans
->blocks_used
;
2019 err
= btrfs_commit_transaction(trans
, root
);
2023 mutex_unlock(&root
->fs_info
->fs_mutex
);
2024 btrfs_btree_balance_dirty(root
, nr
);
2028 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2030 struct btrfs_trans_handle
*trans
;
2031 struct btrfs_key key
;
2032 struct btrfs_root_item new_root_item
;
2033 struct extent_buffer
*tmp
;
2039 if (!root
->ref_cows
)
2042 down_write(&root
->snap_sem
);
2043 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2044 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2046 mutex_lock(&root
->fs_info
->fs_mutex
);
2047 trans
= btrfs_start_transaction(root
, 1);
2050 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2054 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2059 memcpy(&new_root_item
, &root
->root_item
,
2060 sizeof(new_root_item
));
2062 key
.objectid
= objectid
;
2064 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2065 extent_buffer_get(root
->node
);
2066 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2067 free_extent_buffer(tmp
);
2068 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
2069 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
2070 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2076 * insert the directory item
2078 key
.offset
= (u64
)-1;
2079 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2081 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2082 &key
, BTRFS_FT_DIR
);
2087 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2088 name
, namelen
, objectid
,
2089 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2094 ret
= btrfs_inc_root_ref(trans
, root
, objectid
);
2098 nr
= trans
->blocks_used
;
2099 err
= btrfs_commit_transaction(trans
, root
);
2104 mutex_unlock(&root
->fs_info
->fs_mutex
);
2105 up_write(&root
->snap_sem
);
2106 btrfs_btree_balance_dirty(root
, nr
);
2110 static unsigned long force_ra(struct address_space
*mapping
,
2111 struct file_ra_state
*ra
, struct file
*file
,
2112 pgoff_t offset
, pgoff_t last_index
)
2116 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2117 req_size
= last_index
- offset
+ 1;
2118 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2121 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2122 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2123 return offset
+ req_size
;
2127 int btrfs_defrag_file(struct file
*file
) {
2128 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2129 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2131 unsigned long last_index
;
2132 unsigned long ra_index
= 0;
2137 mutex_lock(&inode
->i_mutex
);
2138 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2139 for (i
= 0; i
<= last_index
; i
++) {
2140 if (i
== ra_index
) {
2141 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2142 file
, ra_index
, last_index
);
2144 page
= grab_cache_page(inode
->i_mapping
, i
);
2147 if (!PageUptodate(page
)) {
2148 btrfs_readpage(NULL
, page
);
2150 if (!PageUptodate(page
)) {
2152 page_cache_release(page
);
2156 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2157 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2159 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2160 set_extent_delalloc(em_tree
, page_start
,
2161 page_end
, GFP_NOFS
);
2162 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2163 set_page_dirty(page
);
2165 page_cache_release(page
);
2166 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2170 mutex_unlock(&inode
->i_mutex
);
2174 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2176 struct btrfs_ioctl_vol_args vol_args
;
2177 struct btrfs_dir_item
*di
;
2178 struct btrfs_path
*path
;
2182 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2185 namelen
= strlen(vol_args
.name
);
2186 if (namelen
> BTRFS_VOL_NAME_MAX
)
2188 if (strchr(vol_args
.name
, '/'))
2191 path
= btrfs_alloc_path();
2195 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2196 mutex_lock(&root
->fs_info
->fs_mutex
);
2197 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2199 vol_args
.name
, namelen
, 0);
2200 mutex_unlock(&root
->fs_info
->fs_mutex
);
2201 btrfs_free_path(path
);
2202 if (di
&& !IS_ERR(di
))
2207 if (root
== root
->fs_info
->tree_root
)
2208 return create_subvol(root
, vol_args
.name
, namelen
);
2209 return create_snapshot(root
, vol_args
.name
, namelen
);
2212 static int btrfs_ioctl_defrag(struct file
*file
)
2214 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2215 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2217 switch (inode
->i_mode
& S_IFMT
) {
2219 mutex_lock(&root
->fs_info
->fs_mutex
);
2220 btrfs_defrag_root(root
, 0);
2221 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2222 mutex_unlock(&root
->fs_info
->fs_mutex
);
2225 btrfs_defrag_file(file
);
2232 long btrfs_ioctl(struct file
*file
, unsigned int
2233 cmd
, unsigned long arg
)
2235 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2238 case BTRFS_IOC_SNAP_CREATE
:
2239 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2240 case BTRFS_IOC_DEFRAG
:
2241 return btrfs_ioctl_defrag(file
);
2248 * Called inside transaction, so use GFP_NOFS
2250 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2252 struct btrfs_inode
*ei
;
2254 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2258 return &ei
->vfs_inode
;
2261 void btrfs_destroy_inode(struct inode
*inode
)
2263 WARN_ON(!list_empty(&inode
->i_dentry
));
2264 WARN_ON(inode
->i_data
.nrpages
);
2266 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2269 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2270 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2272 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2273 unsigned long flags
)
2276 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2278 inode_init_once(&ei
->vfs_inode
);
2281 void btrfs_destroy_cachep(void)
2283 if (btrfs_inode_cachep
)
2284 kmem_cache_destroy(btrfs_inode_cachep
);
2285 if (btrfs_trans_handle_cachep
)
2286 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2287 if (btrfs_transaction_cachep
)
2288 kmem_cache_destroy(btrfs_transaction_cachep
);
2289 if (btrfs_bit_radix_cachep
)
2290 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2291 if (btrfs_path_cachep
)
2292 kmem_cache_destroy(btrfs_path_cachep
);
2295 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2296 unsigned long extra_flags
,
2297 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2298 void (*ctor
)(struct kmem_cache
*, void *)
2300 void (*ctor
)(void *, struct kmem_cache
*,
2305 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2306 SLAB_MEM_SPREAD
| extra_flags
), ctor
2307 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2313 int btrfs_init_cachep(void)
2315 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2316 sizeof(struct btrfs_inode
),
2318 if (!btrfs_inode_cachep
)
2320 btrfs_trans_handle_cachep
=
2321 btrfs_cache_create("btrfs_trans_handle_cache",
2322 sizeof(struct btrfs_trans_handle
),
2324 if (!btrfs_trans_handle_cachep
)
2326 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2327 sizeof(struct btrfs_transaction
),
2329 if (!btrfs_transaction_cachep
)
2331 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2332 sizeof(struct btrfs_path
),
2334 if (!btrfs_path_cachep
)
2336 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2337 SLAB_DESTROY_BY_RCU
, NULL
);
2338 if (!btrfs_bit_radix_cachep
)
2342 btrfs_destroy_cachep();
2346 static int btrfs_getattr(struct vfsmount
*mnt
,
2347 struct dentry
*dentry
, struct kstat
*stat
)
2349 struct inode
*inode
= dentry
->d_inode
;
2350 generic_fillattr(inode
, stat
);
2351 stat
->blksize
= 256 * 1024;
2355 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2356 struct inode
* new_dir
,struct dentry
*new_dentry
)
2358 struct btrfs_trans_handle
*trans
;
2359 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2360 struct inode
*new_inode
= new_dentry
->d_inode
;
2361 struct inode
*old_inode
= old_dentry
->d_inode
;
2362 struct timespec ctime
= CURRENT_TIME
;
2363 struct btrfs_path
*path
;
2366 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2367 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2371 mutex_lock(&root
->fs_info
->fs_mutex
);
2372 trans
= btrfs_start_transaction(root
, 1);
2374 btrfs_set_trans_block_group(trans
, new_dir
);
2375 path
= btrfs_alloc_path();
2381 old_dentry
->d_inode
->i_nlink
++;
2382 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2383 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2384 old_inode
->i_ctime
= ctime
;
2386 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2391 new_inode
->i_ctime
= CURRENT_TIME
;
2392 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2396 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2401 btrfs_free_path(path
);
2402 btrfs_end_transaction(trans
, root
);
2403 mutex_unlock(&root
->fs_info
->fs_mutex
);
2407 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2408 const char *symname
)
2410 struct btrfs_trans_handle
*trans
;
2411 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2412 struct btrfs_path
*path
;
2413 struct btrfs_key key
;
2414 struct inode
*inode
;
2421 struct btrfs_file_extent_item
*ei
;
2422 struct extent_buffer
*leaf
;
2425 name_len
= strlen(symname
) + 1;
2426 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2427 return -ENAMETOOLONG
;
2428 mutex_lock(&root
->fs_info
->fs_mutex
);
2429 trans
= btrfs_start_transaction(root
, 1);
2430 btrfs_set_trans_block_group(trans
, dir
);
2432 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2438 inode
= btrfs_new_inode(trans
, root
, objectid
,
2439 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2440 err
= PTR_ERR(inode
);
2444 btrfs_set_trans_block_group(trans
, inode
);
2445 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2449 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2450 inode
->i_fop
= &btrfs_file_operations
;
2451 inode
->i_op
= &btrfs_file_inode_operations
;
2452 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2453 inode
->i_mapping
, GFP_NOFS
);
2454 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2456 dir
->i_sb
->s_dirt
= 1;
2457 btrfs_update_inode_block_group(trans
, inode
);
2458 btrfs_update_inode_block_group(trans
, dir
);
2462 path
= btrfs_alloc_path();
2464 key
.objectid
= inode
->i_ino
;
2466 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2467 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2468 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2474 leaf
= path
->nodes
[0];
2475 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2476 struct btrfs_file_extent_item
);
2477 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2478 btrfs_set_file_extent_type(leaf
, ei
,
2479 BTRFS_FILE_EXTENT_INLINE
);
2480 ptr
= btrfs_file_extent_inline_start(ei
);
2481 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2482 btrfs_mark_buffer_dirty(leaf
);
2483 btrfs_free_path(path
);
2485 inode
->i_op
= &btrfs_symlink_inode_operations
;
2486 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2487 inode
->i_size
= name_len
- 1;
2488 err
= btrfs_update_inode(trans
, root
, inode
);
2493 nr
= trans
->blocks_used
;
2494 btrfs_end_transaction(trans
, root
);
2495 mutex_unlock(&root
->fs_info
->fs_mutex
);
2497 inode_dec_link_count(inode
);
2500 btrfs_btree_balance_dirty(root
, nr
);
2504 static struct inode_operations btrfs_dir_inode_operations
= {
2505 .lookup
= btrfs_lookup
,
2506 .create
= btrfs_create
,
2507 .unlink
= btrfs_unlink
,
2509 .mkdir
= btrfs_mkdir
,
2510 .rmdir
= btrfs_rmdir
,
2511 .rename
= btrfs_rename
,
2512 .symlink
= btrfs_symlink
,
2513 .setattr
= btrfs_setattr
,
2514 .mknod
= btrfs_mknod
,
2515 .setxattr
= generic_setxattr
,
2516 .getxattr
= generic_getxattr
,
2517 .listxattr
= btrfs_listxattr
,
2518 .removexattr
= generic_removexattr
,
2521 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2522 .lookup
= btrfs_lookup
,
2525 static struct file_operations btrfs_dir_file_operations
= {
2526 .llseek
= generic_file_llseek
,
2527 .read
= generic_read_dir
,
2528 .readdir
= btrfs_readdir
,
2529 .unlocked_ioctl
= btrfs_ioctl
,
2530 #ifdef CONFIG_COMPAT
2531 .compat_ioctl
= btrfs_ioctl
,
2535 static struct extent_map_ops btrfs_extent_map_ops
= {
2536 .fill_delalloc
= run_delalloc_range
,
2537 .writepage_io_hook
= btrfs_writepage_io_hook
,
2538 .readpage_io_hook
= btrfs_readpage_io_hook
,
2539 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2542 static struct address_space_operations btrfs_aops
= {
2543 .readpage
= btrfs_readpage
,
2544 .writepage
= btrfs_writepage
,
2545 .writepages
= btrfs_writepages
,
2546 .readpages
= btrfs_readpages
,
2547 .sync_page
= block_sync_page
,
2548 .prepare_write
= btrfs_prepare_write
,
2549 .commit_write
= btrfs_commit_write
,
2551 .invalidatepage
= btrfs_invalidatepage
,
2552 .releasepage
= btrfs_releasepage
,
2553 .set_page_dirty
= __set_page_dirty_nobuffers
,
2556 static struct address_space_operations btrfs_symlink_aops
= {
2557 .readpage
= btrfs_readpage
,
2558 .writepage
= btrfs_writepage
,
2559 .invalidatepage
= btrfs_invalidatepage
,
2560 .releasepage
= btrfs_releasepage
,
2563 static struct inode_operations btrfs_file_inode_operations
= {
2564 .truncate
= btrfs_truncate
,
2565 .getattr
= btrfs_getattr
,
2566 .setattr
= btrfs_setattr
,
2567 .setxattr
= generic_setxattr
,
2568 .getxattr
= generic_getxattr
,
2569 .listxattr
= btrfs_listxattr
,
2570 .removexattr
= generic_removexattr
,
2573 static struct inode_operations btrfs_special_inode_operations
= {
2574 .getattr
= btrfs_getattr
,
2575 .setattr
= btrfs_setattr
,
2578 static struct inode_operations btrfs_symlink_inode_operations
= {
2579 .readlink
= generic_readlink
,
2580 .follow_link
= page_follow_link_light
,
2581 .put_link
= page_put_link
,