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 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
408 dentry
->d_inode
->i_ino
,
409 dentry
->d_parent
->d_inode
->i_ino
);
411 printk("failed to delete reference to %.*s, "
412 "inode %lu parent %lu\n", name_len
, name
,
413 dentry
->d_inode
->i_ino
,
414 dentry
->d_parent
->d_inode
->i_ino
);
417 btrfs_free_path(path
);
419 dir
->i_size
-= name_len
* 2;
420 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
421 btrfs_update_inode(trans
, root
, dir
);
422 drop_nlink(dentry
->d_inode
);
423 ret
= btrfs_update_inode(trans
, root
, dentry
->d_inode
);
424 dir
->i_sb
->s_dirt
= 1;
429 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
431 struct btrfs_root
*root
;
432 struct btrfs_trans_handle
*trans
;
436 root
= BTRFS_I(dir
)->root
;
437 mutex_lock(&root
->fs_info
->fs_mutex
);
438 trans
= btrfs_start_transaction(root
, 1);
440 btrfs_set_trans_block_group(trans
, dir
);
441 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
442 nr
= trans
->blocks_used
;
444 btrfs_end_transaction(trans
, root
);
445 mutex_unlock(&root
->fs_info
->fs_mutex
);
446 btrfs_btree_balance_dirty(root
, nr
);
451 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
453 struct inode
*inode
= dentry
->d_inode
;
456 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
457 struct btrfs_trans_handle
*trans
;
460 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
463 mutex_lock(&root
->fs_info
->fs_mutex
);
464 trans
= btrfs_start_transaction(root
, 1);
465 btrfs_set_trans_block_group(trans
, dir
);
467 /* now the directory is empty */
468 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
473 nr
= trans
->blocks_used
;
474 ret
= btrfs_end_transaction(trans
, root
);
475 mutex_unlock(&root
->fs_info
->fs_mutex
);
476 btrfs_btree_balance_dirty(root
, nr
);
483 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
484 struct btrfs_root
*root
,
487 struct btrfs_path
*path
;
492 path
= btrfs_alloc_path();
494 ret
= btrfs_lookup_inode(trans
, root
, path
,
495 &BTRFS_I(inode
)->location
, -1);
499 ret
= btrfs_del_item(trans
, root
, path
);
500 btrfs_free_path(path
);
505 * this can truncate away extent items, csum items and directory items.
506 * It starts at a high offset and removes keys until it can't find
507 * any higher than i_size.
509 * csum items that cross the new i_size are truncated to the new size
512 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
513 struct btrfs_root
*root
,
517 struct btrfs_path
*path
;
518 struct btrfs_key key
;
519 struct btrfs_key found_key
;
521 struct extent_buffer
*leaf
;
522 struct btrfs_file_extent_item
*fi
;
523 u64 extent_start
= 0;
524 u64 extent_num_bytes
= 0;
530 int extent_type
= -1;
532 btrfs_drop_extent_cache(inode
, inode
->i_size
, (u64
)-1);
533 path
= btrfs_alloc_path();
537 /* FIXME, add redo link to tree so we don't leak on crash */
538 key
.objectid
= inode
->i_ino
;
539 key
.offset
= (u64
)-1;
543 btrfs_init_path(path
);
545 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
550 BUG_ON(path
->slots
[0] == 0);
553 leaf
= path
->nodes
[0];
554 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
555 found_type
= btrfs_key_type(&found_key
);
557 if (found_key
.objectid
!= inode
->i_ino
)
560 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
561 found_type
!= BTRFS_DIR_ITEM_KEY
&&
562 found_type
!= BTRFS_DIR_INDEX_KEY
&&
563 found_type
!= BTRFS_EXTENT_DATA_KEY
)
566 item_end
= found_key
.offset
;
567 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
568 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
569 struct btrfs_file_extent_item
);
570 extent_type
= btrfs_file_extent_type(leaf
, fi
);
571 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
573 btrfs_file_extent_num_bytes(leaf
, fi
);
574 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
575 struct btrfs_item
*item
= btrfs_item_nr(leaf
,
577 item_end
+= btrfs_file_extent_inline_len(leaf
,
582 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
583 ret
= btrfs_csum_truncate(trans
, root
, path
,
587 if (item_end
< inode
->i_size
) {
588 if (found_type
== BTRFS_DIR_ITEM_KEY
) {
589 found_type
= BTRFS_INODE_ITEM_KEY
;
590 } else if (found_type
== BTRFS_EXTENT_ITEM_KEY
) {
591 found_type
= BTRFS_CSUM_ITEM_KEY
;
592 } else if (found_type
) {
597 btrfs_set_key_type(&key
, found_type
);
598 btrfs_release_path(root
, path
);
601 if (found_key
.offset
>= inode
->i_size
)
607 /* FIXME, shrink the extent if the ref count is only 1 */
608 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
611 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
613 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
616 btrfs_file_extent_num_bytes(leaf
, fi
);
617 extent_num_bytes
= inode
->i_size
-
618 found_key
.offset
+ root
->sectorsize
- 1;
619 btrfs_set_file_extent_num_bytes(leaf
, fi
,
621 num_dec
= (orig_num_bytes
-
622 extent_num_bytes
) >> 9;
623 if (extent_start
!= 0) {
624 inode
->i_blocks
-= num_dec
;
626 btrfs_mark_buffer_dirty(leaf
);
629 btrfs_file_extent_disk_num_bytes(leaf
,
631 /* FIXME blocksize != 4096 */
632 num_dec
= btrfs_file_extent_num_bytes(leaf
,
634 if (extent_start
!= 0) {
636 inode
->i_blocks
-= num_dec
;
638 root_gen
= btrfs_header_generation(leaf
);
639 root_owner
= btrfs_header_owner(leaf
);
641 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
&&
643 u32 newsize
= inode
->i_size
- found_key
.offset
;
644 newsize
= btrfs_file_extent_calc_inline_size(newsize
);
645 ret
= btrfs_truncate_item(trans
, root
, path
,
651 ret
= btrfs_del_item(trans
, root
, path
);
657 btrfs_release_path(root
, path
);
659 ret
= btrfs_free_extent(trans
, root
, extent_start
,
662 root_gen
, inode
->i_ino
,
663 found_key
.offset
, 0);
669 btrfs_release_path(root
, path
);
670 btrfs_free_path(path
);
671 inode
->i_sb
->s_dirt
= 1;
675 static int btrfs_cow_one_page(struct inode
*inode
, struct page
*page
,
680 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
681 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
682 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
684 set_page_extent_mapped(page
);
686 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
687 set_extent_delalloc(&BTRFS_I(inode
)->extent_tree
, page_start
,
689 if (zero_start
!= PAGE_CACHE_SIZE
) {
691 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
692 flush_dcache_page(page
);
695 set_page_dirty(page
);
696 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
702 * taken from block_truncate_page, but does cow as it zeros out
703 * any bytes left in the last page in the file.
705 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
707 struct inode
*inode
= mapping
->host
;
708 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
709 u32 blocksize
= root
->sectorsize
;
710 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
711 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
716 if ((offset
& (blocksize
- 1)) == 0)
719 down_read(&root
->snap_sem
);
721 page
= grab_cache_page(mapping
, index
);
724 if (!PageUptodate(page
)) {
725 ret
= btrfs_readpage(NULL
, page
);
727 if (!PageUptodate(page
)) {
732 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
734 ret
= btrfs_cow_one_page(inode
, page
, offset
);
737 page_cache_release(page
);
738 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
743 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
745 struct inode
*inode
= dentry
->d_inode
;
748 err
= inode_change_ok(inode
, attr
);
752 if (S_ISREG(inode
->i_mode
) &&
753 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
754 struct btrfs_trans_handle
*trans
;
755 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
756 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
758 u64 mask
= root
->sectorsize
- 1;
759 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
760 u64 block_end
= attr
->ia_size
| mask
;
764 if (attr
->ia_size
<= pos
)
767 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
769 lock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
770 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
772 mutex_lock(&root
->fs_info
->fs_mutex
);
773 trans
= btrfs_start_transaction(root
, 1);
774 btrfs_set_trans_block_group(trans
, inode
);
775 err
= btrfs_drop_extents(trans
, root
, inode
,
776 pos
, pos
+ hole_size
, pos
,
779 if (alloc_hint
!= EXTENT_MAP_INLINE
) {
780 err
= btrfs_insert_file_extent(trans
, root
,
782 pos
, 0, 0, hole_size
);
784 btrfs_end_transaction(trans
, root
);
785 mutex_unlock(&root
->fs_info
->fs_mutex
);
786 unlock_extent(em_tree
, pos
, block_end
, GFP_NOFS
);
791 err
= inode_setattr(inode
, attr
);
795 void btrfs_delete_inode(struct inode
*inode
)
797 struct btrfs_trans_handle
*trans
;
798 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
802 truncate_inode_pages(&inode
->i_data
, 0);
803 if (is_bad_inode(inode
)) {
808 mutex_lock(&root
->fs_info
->fs_mutex
);
809 trans
= btrfs_start_transaction(root
, 1);
811 btrfs_set_trans_block_group(trans
, inode
);
812 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
815 ret
= btrfs_delete_xattrs(trans
, root
, inode
);
818 ret
= btrfs_free_inode(trans
, root
, inode
);
821 nr
= trans
->blocks_used
;
823 btrfs_end_transaction(trans
, root
);
824 mutex_unlock(&root
->fs_info
->fs_mutex
);
825 btrfs_btree_balance_dirty(root
, nr
);
829 nr
= trans
->blocks_used
;
830 btrfs_end_transaction(trans
, root
);
831 mutex_unlock(&root
->fs_info
->fs_mutex
);
832 btrfs_btree_balance_dirty(root
, nr
);
838 * this returns the key found in the dir entry in the location pointer.
839 * If no dir entries were found, location->objectid is 0.
841 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
842 struct btrfs_key
*location
)
844 const char *name
= dentry
->d_name
.name
;
845 int namelen
= dentry
->d_name
.len
;
846 struct btrfs_dir_item
*di
;
847 struct btrfs_path
*path
;
848 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
851 if (namelen
== 1 && strcmp(name
, ".") == 0) {
852 location
->objectid
= dir
->i_ino
;
853 location
->type
= BTRFS_INODE_ITEM_KEY
;
854 location
->offset
= 0;
857 path
= btrfs_alloc_path();
860 if (namelen
== 1 && strcmp(name
, "..") == 0) {
861 struct btrfs_key key
;
862 struct extent_buffer
*leaf
;
866 key
.objectid
= dir
->i_ino
;
867 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
869 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
873 leaf
= path
->nodes
[0];
874 slot
= path
->slots
[0];
875 nritems
= btrfs_header_nritems(leaf
);
879 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
880 if (key
.objectid
!= dir
->i_ino
||
881 key
.type
!= BTRFS_INODE_REF_KEY
) {
884 location
->objectid
= key
.offset
;
885 location
->type
= BTRFS_INODE_ITEM_KEY
;
886 location
->offset
= 0;
890 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
894 if (!di
|| IS_ERR(di
)) {
897 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
899 btrfs_free_path(path
);
902 location
->objectid
= 0;
907 * when we hit a tree root in a directory, the btrfs part of the inode
908 * needs to be changed to reflect the root directory of the tree root. This
909 * is kind of like crossing a mount point.
911 static int fixup_tree_root_location(struct btrfs_root
*root
,
912 struct btrfs_key
*location
,
913 struct btrfs_root
**sub_root
,
914 struct dentry
*dentry
)
916 struct btrfs_path
*path
;
917 struct btrfs_root_item
*ri
;
919 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
921 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
924 path
= btrfs_alloc_path();
926 mutex_lock(&root
->fs_info
->fs_mutex
);
928 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
,
931 if (IS_ERR(*sub_root
))
932 return PTR_ERR(*sub_root
);
934 ri
= &(*sub_root
)->root_item
;
935 location
->objectid
= btrfs_root_dirid(ri
);
936 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
937 location
->offset
= 0;
939 btrfs_free_path(path
);
940 mutex_unlock(&root
->fs_info
->fs_mutex
);
944 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
946 struct btrfs_iget_args
*args
= p
;
947 inode
->i_ino
= args
->ino
;
948 BTRFS_I(inode
)->root
= args
->root
;
949 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
950 inode
->i_mapping
, GFP_NOFS
);
954 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
956 struct btrfs_iget_args
*args
= opaque
;
957 return (args
->ino
== inode
->i_ino
&&
958 args
->root
== BTRFS_I(inode
)->root
);
961 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
962 struct btrfs_root
*root
)
965 struct btrfs_iget_args args
;
969 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
970 btrfs_init_locked_inode
,
975 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
976 struct nameidata
*nd
)
978 struct inode
* inode
;
979 struct btrfs_inode
*bi
= BTRFS_I(dir
);
980 struct btrfs_root
*root
= bi
->root
;
981 struct btrfs_root
*sub_root
= root
;
982 struct btrfs_key location
;
985 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
986 return ERR_PTR(-ENAMETOOLONG
);
988 mutex_lock(&root
->fs_info
->fs_mutex
);
989 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
990 mutex_unlock(&root
->fs_info
->fs_mutex
);
996 if (location
.objectid
) {
997 ret
= fixup_tree_root_location(root
, &location
, &sub_root
,
1000 return ERR_PTR(ret
);
1002 return ERR_PTR(-ENOENT
);
1003 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
1006 return ERR_PTR(-EACCES
);
1007 if (inode
->i_state
& I_NEW
) {
1008 /* the inode and parent dir are two different roots */
1009 if (sub_root
!= root
) {
1011 sub_root
->inode
= inode
;
1013 BTRFS_I(inode
)->root
= sub_root
;
1014 memcpy(&BTRFS_I(inode
)->location
, &location
,
1016 btrfs_read_locked_inode(inode
);
1017 unlock_new_inode(inode
);
1020 return d_splice_alias(inode
, dentry
);
1023 static unsigned char btrfs_filetype_table
[] = {
1024 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
1027 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1029 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1030 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1031 struct btrfs_item
*item
;
1032 struct btrfs_dir_item
*di
;
1033 struct btrfs_key key
;
1034 struct btrfs_key found_key
;
1035 struct btrfs_path
*path
;
1038 struct extent_buffer
*leaf
;
1041 unsigned char d_type
;
1046 int key_type
= BTRFS_DIR_INDEX_KEY
;
1051 /* FIXME, use a real flag for deciding about the key type */
1052 if (root
->fs_info
->tree_root
== root
)
1053 key_type
= BTRFS_DIR_ITEM_KEY
;
1055 /* special case for "." */
1056 if (filp
->f_pos
== 0) {
1057 over
= filldir(dirent
, ".", 1,
1065 mutex_lock(&root
->fs_info
->fs_mutex
);
1066 key
.objectid
= inode
->i_ino
;
1067 path
= btrfs_alloc_path();
1070 /* special case for .., just use the back ref */
1071 if (filp
->f_pos
== 1) {
1072 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
1074 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1076 leaf
= path
->nodes
[0];
1077 slot
= path
->slots
[0];
1078 nritems
= btrfs_header_nritems(leaf
);
1079 if (slot
>= nritems
) {
1080 btrfs_release_path(root
, path
);
1081 goto read_dir_items
;
1083 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1084 btrfs_release_path(root
, path
);
1085 if (found_key
.objectid
!= key
.objectid
||
1086 found_key
.type
!= BTRFS_INODE_REF_KEY
)
1087 goto read_dir_items
;
1088 over
= filldir(dirent
, "..", 2,
1089 2, found_key
.offset
, DT_DIR
);
1096 btrfs_set_key_type(&key
, key_type
);
1097 key
.offset
= filp
->f_pos
;
1099 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1104 leaf
= path
->nodes
[0];
1105 nritems
= btrfs_header_nritems(leaf
);
1106 slot
= path
->slots
[0];
1107 if (advance
|| slot
>= nritems
) {
1108 if (slot
>= nritems
-1) {
1109 ret
= btrfs_next_leaf(root
, path
);
1112 leaf
= path
->nodes
[0];
1113 nritems
= btrfs_header_nritems(leaf
);
1114 slot
= path
->slots
[0];
1121 item
= btrfs_item_nr(leaf
, slot
);
1122 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
1124 if (found_key
.objectid
!= key
.objectid
)
1126 if (btrfs_key_type(&found_key
) != key_type
)
1128 if (found_key
.offset
< filp
->f_pos
)
1131 filp
->f_pos
= found_key
.offset
;
1133 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
1135 di_total
= btrfs_item_size(leaf
, item
);
1136 while(di_cur
< di_total
) {
1137 struct btrfs_key location
;
1139 name_len
= btrfs_dir_name_len(leaf
, di
);
1140 if (name_len
< 32) {
1141 name_ptr
= tmp_name
;
1143 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
1146 read_extent_buffer(leaf
, name_ptr
,
1147 (unsigned long)(di
+ 1), name_len
);
1149 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
1150 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
1152 over
= filldir(dirent
, name_ptr
, name_len
,
1157 if (name_ptr
!= tmp_name
)
1162 di_len
= btrfs_dir_name_len(leaf
, di
) +
1163 btrfs_dir_data_len(leaf
, di
) +sizeof(*di
);
1165 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
1172 btrfs_release_path(root
, path
);
1173 btrfs_free_path(path
);
1174 mutex_unlock(&root
->fs_info
->fs_mutex
);
1178 int btrfs_write_inode(struct inode
*inode
, int wait
)
1180 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1181 struct btrfs_trans_handle
*trans
;
1185 mutex_lock(&root
->fs_info
->fs_mutex
);
1186 trans
= btrfs_start_transaction(root
, 1);
1187 btrfs_set_trans_block_group(trans
, inode
);
1188 ret
= btrfs_commit_transaction(trans
, root
);
1189 mutex_unlock(&root
->fs_info
->fs_mutex
);
1195 * This is somewhat expensive, updating the tree every time the
1196 * inode changes. But, it is most likely to find the inode in cache.
1197 * FIXME, needs more benchmarking...there are no reasons other than performance
1198 * to keep or drop this code.
1200 void btrfs_dirty_inode(struct inode
*inode
)
1202 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1203 struct btrfs_trans_handle
*trans
;
1205 mutex_lock(&root
->fs_info
->fs_mutex
);
1206 trans
= btrfs_start_transaction(root
, 1);
1207 btrfs_set_trans_block_group(trans
, inode
);
1208 btrfs_update_inode(trans
, root
, inode
);
1209 btrfs_end_transaction(trans
, root
);
1210 mutex_unlock(&root
->fs_info
->fs_mutex
);
1213 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
1214 struct btrfs_root
*root
,
1216 struct btrfs_block_group_cache
*group
,
1219 struct inode
*inode
;
1220 struct btrfs_inode_item
*inode_item
;
1221 struct btrfs_key
*location
;
1222 struct btrfs_path
*path
;
1226 path
= btrfs_alloc_path();
1229 inode
= new_inode(root
->fs_info
->sb
);
1231 return ERR_PTR(-ENOMEM
);
1233 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1234 inode
->i_mapping
, GFP_NOFS
);
1235 BTRFS_I(inode
)->root
= root
;
1241 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
1242 BTRFS_I(inode
)->block_group
= group
;
1244 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
1248 inode
->i_uid
= current
->fsuid
;
1249 inode
->i_gid
= current
->fsgid
;
1250 inode
->i_mode
= mode
;
1251 inode
->i_ino
= objectid
;
1252 inode
->i_blocks
= 0;
1253 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1254 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1255 struct btrfs_inode_item
);
1256 fill_inode_item(path
->nodes
[0], inode_item
, inode
);
1257 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1258 btrfs_free_path(path
);
1260 location
= &BTRFS_I(inode
)->location
;
1261 location
->objectid
= objectid
;
1262 location
->offset
= 0;
1263 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1265 insert_inode_hash(inode
);
1268 btrfs_free_path(path
);
1269 return ERR_PTR(ret
);
1272 static inline u8
btrfs_inode_type(struct inode
*inode
)
1274 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
1277 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1278 struct dentry
*dentry
, struct inode
*inode
)
1281 struct btrfs_key key
;
1282 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1283 struct inode
*parent_inode
;
1285 key
.objectid
= inode
->i_ino
;
1286 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1289 ret
= btrfs_insert_dir_item(trans
, root
,
1290 dentry
->d_name
.name
, dentry
->d_name
.len
,
1291 dentry
->d_parent
->d_inode
->i_ino
,
1292 &key
, btrfs_inode_type(inode
));
1294 ret
= btrfs_insert_inode_ref(trans
, root
,
1295 dentry
->d_name
.name
,
1298 dentry
->d_parent
->d_inode
->i_ino
);
1299 parent_inode
= dentry
->d_parent
->d_inode
;
1300 parent_inode
->i_size
+= dentry
->d_name
.len
* 2;
1301 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
1302 ret
= btrfs_update_inode(trans
, root
,
1303 dentry
->d_parent
->d_inode
);
1308 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1309 struct dentry
*dentry
, struct inode
*inode
)
1311 int err
= btrfs_add_link(trans
, dentry
, inode
);
1313 d_instantiate(dentry
, inode
);
1321 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
1322 int mode
, dev_t rdev
)
1324 struct btrfs_trans_handle
*trans
;
1325 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1326 struct inode
*inode
;
1332 if (!new_valid_dev(rdev
))
1335 mutex_lock(&root
->fs_info
->fs_mutex
);
1336 trans
= btrfs_start_transaction(root
, 1);
1337 btrfs_set_trans_block_group(trans
, dir
);
1339 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1345 inode
= btrfs_new_inode(trans
, root
, objectid
,
1346 BTRFS_I(dir
)->block_group
, mode
);
1347 err
= PTR_ERR(inode
);
1351 btrfs_set_trans_block_group(trans
, inode
);
1352 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1356 inode
->i_op
= &btrfs_special_inode_operations
;
1357 init_special_inode(inode
, inode
->i_mode
, rdev
);
1358 btrfs_update_inode(trans
, root
, inode
);
1360 dir
->i_sb
->s_dirt
= 1;
1361 btrfs_update_inode_block_group(trans
, inode
);
1362 btrfs_update_inode_block_group(trans
, dir
);
1364 nr
= trans
->blocks_used
;
1365 btrfs_end_transaction(trans
, root
);
1366 mutex_unlock(&root
->fs_info
->fs_mutex
);
1369 inode_dec_link_count(inode
);
1372 btrfs_btree_balance_dirty(root
, nr
);
1376 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1377 int mode
, struct nameidata
*nd
)
1379 struct btrfs_trans_handle
*trans
;
1380 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1381 struct inode
*inode
;
1387 mutex_lock(&root
->fs_info
->fs_mutex
);
1388 trans
= btrfs_start_transaction(root
, 1);
1389 btrfs_set_trans_block_group(trans
, dir
);
1391 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1397 inode
= btrfs_new_inode(trans
, root
, objectid
,
1398 BTRFS_I(dir
)->block_group
, mode
);
1399 err
= PTR_ERR(inode
);
1403 btrfs_set_trans_block_group(trans
, inode
);
1404 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1408 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1409 inode
->i_fop
= &btrfs_file_operations
;
1410 inode
->i_op
= &btrfs_file_inode_operations
;
1411 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
1412 inode
->i_mapping
, GFP_NOFS
);
1413 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
1415 dir
->i_sb
->s_dirt
= 1;
1416 btrfs_update_inode_block_group(trans
, inode
);
1417 btrfs_update_inode_block_group(trans
, dir
);
1419 nr
= trans
->blocks_used
;
1420 btrfs_end_transaction(trans
, root
);
1421 mutex_unlock(&root
->fs_info
->fs_mutex
);
1424 inode_dec_link_count(inode
);
1427 btrfs_btree_balance_dirty(root
, nr
);
1431 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1432 struct dentry
*dentry
)
1434 struct btrfs_trans_handle
*trans
;
1435 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1436 struct inode
*inode
= old_dentry
->d_inode
;
1441 if (inode
->i_nlink
== 0)
1445 mutex_lock(&root
->fs_info
->fs_mutex
);
1446 trans
= btrfs_start_transaction(root
, 1);
1448 btrfs_set_trans_block_group(trans
, dir
);
1449 atomic_inc(&inode
->i_count
);
1450 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1455 dir
->i_sb
->s_dirt
= 1;
1456 btrfs_update_inode_block_group(trans
, dir
);
1457 err
= btrfs_update_inode(trans
, root
, inode
);
1462 nr
= trans
->blocks_used
;
1463 btrfs_end_transaction(trans
, root
);
1464 mutex_unlock(&root
->fs_info
->fs_mutex
);
1467 inode_dec_link_count(inode
);
1470 btrfs_btree_balance_dirty(root
, nr
);
1474 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1476 struct inode
*inode
;
1477 struct btrfs_trans_handle
*trans
;
1478 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1480 int drop_on_err
= 0;
1482 unsigned long nr
= 1;
1484 mutex_lock(&root
->fs_info
->fs_mutex
);
1485 trans
= btrfs_start_transaction(root
, 1);
1486 btrfs_set_trans_block_group(trans
, dir
);
1488 if (IS_ERR(trans
)) {
1489 err
= PTR_ERR(trans
);
1493 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1499 inode
= btrfs_new_inode(trans
, root
, objectid
,
1500 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1501 if (IS_ERR(inode
)) {
1502 err
= PTR_ERR(inode
);
1507 inode
->i_op
= &btrfs_dir_inode_operations
;
1508 inode
->i_fop
= &btrfs_dir_file_operations
;
1509 btrfs_set_trans_block_group(trans
, inode
);
1512 err
= btrfs_update_inode(trans
, root
, inode
);
1516 err
= btrfs_add_link(trans
, dentry
, inode
);
1520 d_instantiate(dentry
, inode
);
1522 dir
->i_sb
->s_dirt
= 1;
1523 btrfs_update_inode_block_group(trans
, inode
);
1524 btrfs_update_inode_block_group(trans
, dir
);
1527 nr
= trans
->blocks_used
;
1528 btrfs_end_transaction(trans
, root
);
1531 mutex_unlock(&root
->fs_info
->fs_mutex
);
1534 btrfs_btree_balance_dirty(root
, nr
);
1538 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
1539 size_t page_offset
, u64 start
, u64 end
,
1545 u64 extent_start
= 0;
1547 u64 objectid
= inode
->i_ino
;
1549 int failed_insert
= 0;
1550 struct btrfs_path
*path
;
1551 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1552 struct btrfs_file_extent_item
*item
;
1553 struct extent_buffer
*leaf
;
1554 struct btrfs_key found_key
;
1555 struct extent_map
*em
= NULL
;
1556 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
1557 struct btrfs_trans_handle
*trans
= NULL
;
1559 path
= btrfs_alloc_path();
1561 mutex_lock(&root
->fs_info
->fs_mutex
);
1564 em
= lookup_extent_mapping(em_tree
, start
, end
);
1569 em
= alloc_extent_map(GFP_NOFS
);
1574 em
->start
= EXTENT_MAP_HOLE
;
1575 em
->end
= EXTENT_MAP_HOLE
;
1577 em
->bdev
= inode
->i_sb
->s_bdev
;
1578 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1579 objectid
, start
, trans
!= NULL
);
1586 if (path
->slots
[0] == 0)
1591 leaf
= path
->nodes
[0];
1592 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
1593 struct btrfs_file_extent_item
);
1594 /* are we inside the extent that was found? */
1595 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1596 found_type
= btrfs_key_type(&found_key
);
1597 if (found_key
.objectid
!= objectid
||
1598 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1602 found_type
= btrfs_file_extent_type(leaf
, item
);
1603 extent_start
= found_key
.offset
;
1604 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1605 extent_end
= extent_start
+
1606 btrfs_file_extent_num_bytes(leaf
, item
);
1608 if (start
< extent_start
|| start
>= extent_end
) {
1610 if (start
< extent_start
) {
1611 if (end
< extent_start
)
1613 em
->end
= extent_end
- 1;
1619 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
1621 em
->start
= extent_start
;
1622 em
->end
= extent_end
- 1;
1623 em
->block_start
= EXTENT_MAP_HOLE
;
1624 em
->block_end
= EXTENT_MAP_HOLE
;
1627 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
1628 em
->block_start
= bytenr
;
1629 em
->block_end
= em
->block_start
+
1630 btrfs_file_extent_num_bytes(leaf
, item
) - 1;
1631 em
->start
= extent_start
;
1632 em
->end
= extent_end
- 1;
1634 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1638 size_t extent_offset
;
1641 size
= btrfs_file_extent_inline_len(leaf
, btrfs_item_nr(leaf
,
1643 extent_end
= (extent_start
+ size
- 1) |
1644 ((u64
)root
->sectorsize
- 1);
1645 if (start
< extent_start
|| start
>= extent_end
) {
1647 if (start
< extent_start
) {
1648 if (end
< extent_start
)
1650 em
->end
= extent_end
;
1656 em
->block_start
= EXTENT_MAP_INLINE
;
1657 em
->block_end
= EXTENT_MAP_INLINE
;
1660 em
->start
= extent_start
;
1661 em
->end
= extent_start
+ size
- 1;
1665 extent_offset
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) -
1666 extent_start
+ page_offset
;
1667 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- page_offset
,
1668 size
- extent_offset
);
1669 em
->start
= extent_start
+ extent_offset
;
1670 em
->end
= (em
->start
+ copy_size
-1) |
1671 ((u64
)root
->sectorsize
-1);
1673 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
1674 if (create
== 0 && !PageUptodate(page
)) {
1675 read_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1677 flush_dcache_page(page
);
1678 } else if (create
&& PageUptodate(page
)) {
1681 free_extent_map(em
);
1683 btrfs_release_path(root
, path
);
1684 trans
= btrfs_start_transaction(root
, 1);
1687 write_extent_buffer(leaf
, map
+ page_offset
, ptr
,
1689 btrfs_mark_buffer_dirty(leaf
);
1692 set_extent_uptodate(em_tree
, em
->start
, em
->end
, GFP_NOFS
);
1695 printk("unkknown found_type %d\n", found_type
);
1702 em
->block_start
= EXTENT_MAP_HOLE
;
1703 em
->block_end
= EXTENT_MAP_HOLE
;
1705 btrfs_release_path(root
, path
);
1706 if (em
->start
> start
|| em
->end
< start
) {
1707 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em
->start
, em
->end
, start
, end
);
1711 ret
= add_extent_mapping(em_tree
, em
);
1712 if (ret
== -EEXIST
) {
1713 free_extent_map(em
);
1716 if (failed_insert
> 5) {
1717 printk("failing to insert %Lu %Lu\n", start
, end
);
1725 btrfs_free_path(path
);
1727 ret
= btrfs_end_transaction(trans
, root
);
1731 mutex_unlock(&root
->fs_info
->fs_mutex
);
1733 free_extent_map(em
);
1735 return ERR_PTR(err
);
1740 static sector_t
btrfs_bmap(struct address_space
*mapping
, sector_t iblock
)
1742 return extent_bmap(mapping
, iblock
, btrfs_get_extent
);
1745 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1746 unsigned from
, unsigned to
)
1748 return extent_prepare_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1749 page
->mapping
->host
, page
, from
, to
,
1753 int btrfs_readpage(struct file
*file
, struct page
*page
)
1755 struct extent_map_tree
*tree
;
1756 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1757 return extent_read_full_page(tree
, page
, btrfs_get_extent
);
1759 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1761 struct extent_map_tree
*tree
;
1764 if (current
->flags
& PF_MEMALLOC
) {
1765 redirty_page_for_writepage(wbc
, page
);
1769 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1770 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
1773 static int btrfs_writepages(struct address_space
*mapping
,
1774 struct writeback_control
*wbc
)
1776 struct extent_map_tree
*tree
;
1777 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1778 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
1782 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1783 struct list_head
*pages
, unsigned nr_pages
)
1785 struct extent_map_tree
*tree
;
1786 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
1787 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
1791 static int btrfs_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
1793 struct extent_map_tree
*tree
;
1796 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1797 ret
= try_release_extent_mapping(tree
, page
);
1799 ClearPagePrivate(page
);
1800 set_page_private(page
, 0);
1801 page_cache_release(page
);
1806 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
1808 struct extent_map_tree
*tree
;
1810 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
1811 extent_invalidatepage(tree
, page
, offset
);
1812 btrfs_releasepage(page
, GFP_NOFS
);
1816 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1817 * called from a page fault handler when a page is first dirtied. Hence we must
1818 * be careful to check for EOF conditions here. We set the page up correctly
1819 * for a written page which means we get ENOSPC checking when writing into
1820 * holes and correct delalloc and unwritten extent mapping on filesystems that
1821 * support these features.
1823 * We are not allowed to take the i_mutex here so we have to play games to
1824 * protect against truncate races as the page could now be beyond EOF. Because
1825 * vmtruncate() writes the inode size before removing pages, once we have the
1826 * page lock we can determine safely if the page is beyond EOF. If it is not
1827 * beyond EOF, then the page is guaranteed safe against truncation until we
1830 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct page
*page
)
1832 struct inode
*inode
= vma
->vm_file
->f_path
.dentry
->d_inode
;
1838 down_read(&BTRFS_I(inode
)->root
->snap_sem
);
1840 wait_on_page_writeback(page
);
1841 size
= i_size_read(inode
);
1842 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1844 if ((page
->mapping
!= inode
->i_mapping
) ||
1845 (page_start
> size
)) {
1846 /* page got truncated out from underneath us */
1850 /* page is wholly or partially inside EOF */
1851 if (page_start
+ PAGE_CACHE_SIZE
> size
)
1852 end
= size
& ~PAGE_CACHE_MASK
;
1854 end
= PAGE_CACHE_SIZE
;
1856 ret
= btrfs_cow_one_page(inode
, page
, end
);
1859 up_read(&BTRFS_I(inode
)->root
->snap_sem
);
1864 static void btrfs_truncate(struct inode
*inode
)
1866 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1868 struct btrfs_trans_handle
*trans
;
1871 if (!S_ISREG(inode
->i_mode
))
1873 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1876 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1878 mutex_lock(&root
->fs_info
->fs_mutex
);
1879 trans
= btrfs_start_transaction(root
, 1);
1880 btrfs_set_trans_block_group(trans
, inode
);
1882 /* FIXME, add redo link to tree so we don't leak on crash */
1883 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1884 btrfs_update_inode(trans
, root
, inode
);
1885 nr
= trans
->blocks_used
;
1887 ret
= btrfs_end_transaction(trans
, root
);
1889 mutex_unlock(&root
->fs_info
->fs_mutex
);
1890 btrfs_btree_balance_dirty(root
, nr
);
1893 int btrfs_commit_write(struct file
*file
, struct page
*page
,
1894 unsigned from
, unsigned to
)
1896 return extent_commit_write(&BTRFS_I(page
->mapping
->host
)->extent_tree
,
1897 page
->mapping
->host
, page
, from
, to
);
1900 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1902 struct btrfs_trans_handle
*trans
;
1903 struct btrfs_key key
;
1904 struct btrfs_root_item root_item
;
1905 struct btrfs_inode_item
*inode_item
;
1906 struct extent_buffer
*leaf
;
1907 struct btrfs_root
*new_root
;
1908 struct inode
*inode
;
1913 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1914 unsigned long nr
= 1;
1916 mutex_lock(&root
->fs_info
->fs_mutex
);
1917 trans
= btrfs_start_transaction(root
, 1);
1920 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1925 leaf
= __btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
1926 objectid
, trans
->transid
, 0, 0,
1929 return PTR_ERR(leaf
);
1931 btrfs_set_header_nritems(leaf
, 0);
1932 btrfs_set_header_level(leaf
, 0);
1933 btrfs_set_header_bytenr(leaf
, leaf
->start
);
1934 btrfs_set_header_generation(leaf
, trans
->transid
);
1935 btrfs_set_header_owner(leaf
, objectid
);
1937 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
1938 (unsigned long)btrfs_header_fsid(leaf
),
1940 btrfs_mark_buffer_dirty(leaf
);
1942 inode_item
= &root_item
.inode
;
1943 memset(inode_item
, 0, sizeof(*inode_item
));
1944 inode_item
->generation
= cpu_to_le64(1);
1945 inode_item
->size
= cpu_to_le64(3);
1946 inode_item
->nlink
= cpu_to_le32(1);
1947 inode_item
->nblocks
= cpu_to_le64(1);
1948 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
1950 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
1951 btrfs_set_root_level(&root_item
, 0);
1952 btrfs_set_root_refs(&root_item
, 1);
1953 btrfs_set_root_used(&root_item
, 0);
1955 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
1956 root_item
.drop_level
= 0;
1958 free_extent_buffer(leaf
);
1961 btrfs_set_root_dirid(&root_item
, new_dirid
);
1963 key
.objectid
= objectid
;
1965 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1966 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1972 * insert the directory item
1974 key
.offset
= (u64
)-1;
1975 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
1976 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1977 name
, namelen
, dir
->i_ino
, &key
,
1982 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
1983 name
, namelen
, objectid
,
1984 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
1988 ret
= btrfs_commit_transaction(trans
, root
);
1992 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
1995 trans
= btrfs_start_transaction(new_root
, 1);
1998 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
1999 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2002 inode
->i_op
= &btrfs_dir_inode_operations
;
2003 inode
->i_fop
= &btrfs_dir_file_operations
;
2004 new_root
->inode
= inode
;
2006 ret
= btrfs_insert_inode_ref(trans
, new_root
, "..", 2, new_dirid
,
2010 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2014 nr
= trans
->blocks_used
;
2015 err
= btrfs_commit_transaction(trans
, root
);
2019 mutex_unlock(&root
->fs_info
->fs_mutex
);
2020 btrfs_btree_balance_dirty(root
, nr
);
2024 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2026 struct btrfs_trans_handle
*trans
;
2027 struct btrfs_key key
;
2028 struct btrfs_root_item new_root_item
;
2029 struct extent_buffer
*tmp
;
2035 if (!root
->ref_cows
)
2038 down_write(&root
->snap_sem
);
2039 freeze_bdev(root
->fs_info
->sb
->s_bdev
);
2040 thaw_bdev(root
->fs_info
->sb
->s_bdev
, root
->fs_info
->sb
);
2042 mutex_lock(&root
->fs_info
->fs_mutex
);
2043 trans
= btrfs_start_transaction(root
, 1);
2046 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2050 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2055 memcpy(&new_root_item
, &root
->root_item
,
2056 sizeof(new_root_item
));
2058 key
.objectid
= objectid
;
2060 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2061 extent_buffer_get(root
->node
);
2062 btrfs_cow_block(trans
, root
, root
->node
, NULL
, 0, &tmp
);
2063 free_extent_buffer(tmp
);
2064 btrfs_set_root_bytenr(&new_root_item
, root
->node
->start
);
2065 btrfs_set_root_level(&new_root_item
, btrfs_header_level(root
->node
));
2066 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2072 * insert the directory item
2074 key
.offset
= (u64
)-1;
2075 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2077 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2078 &key
, BTRFS_FT_DIR
);
2083 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
2084 name
, namelen
, objectid
,
2085 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
);
2090 ret
= btrfs_inc_root_ref(trans
, root
, objectid
);
2094 nr
= trans
->blocks_used
;
2095 err
= btrfs_commit_transaction(trans
, root
);
2100 mutex_unlock(&root
->fs_info
->fs_mutex
);
2101 up_write(&root
->snap_sem
);
2102 btrfs_btree_balance_dirty(root
, nr
);
2106 static unsigned long force_ra(struct address_space
*mapping
,
2107 struct file_ra_state
*ra
, struct file
*file
,
2108 pgoff_t offset
, pgoff_t last_index
)
2112 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2113 req_size
= last_index
- offset
+ 1;
2114 offset
= page_cache_readahead(mapping
, ra
, file
, offset
, req_size
);
2117 req_size
= min(last_index
- offset
+ 1, (pgoff_t
)128);
2118 page_cache_sync_readahead(mapping
, ra
, file
, offset
, req_size
);
2119 return offset
+ req_size
;
2123 int btrfs_defrag_file(struct file
*file
) {
2124 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2125 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2127 unsigned long last_index
;
2128 unsigned long ra_index
= 0;
2133 mutex_lock(&inode
->i_mutex
);
2134 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
2135 for (i
= 0; i
<= last_index
; i
++) {
2136 if (i
== ra_index
) {
2137 ra_index
= force_ra(inode
->i_mapping
, &file
->f_ra
,
2138 file
, ra_index
, last_index
);
2140 page
= grab_cache_page(inode
->i_mapping
, i
);
2143 if (!PageUptodate(page
)) {
2144 btrfs_readpage(NULL
, page
);
2146 if (!PageUptodate(page
)) {
2148 page_cache_release(page
);
2152 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2153 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2155 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2156 set_extent_delalloc(em_tree
, page_start
,
2157 page_end
, GFP_NOFS
);
2158 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2159 set_page_dirty(page
);
2161 page_cache_release(page
);
2162 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2166 mutex_unlock(&inode
->i_mutex
);
2170 static int btrfs_ioctl_snap_create(struct btrfs_root
*root
, void __user
*arg
)
2172 struct btrfs_ioctl_vol_args vol_args
;
2173 struct btrfs_dir_item
*di
;
2174 struct btrfs_path
*path
;
2178 if (copy_from_user(&vol_args
, arg
, sizeof(vol_args
)))
2181 namelen
= strlen(vol_args
.name
);
2182 if (namelen
> BTRFS_VOL_NAME_MAX
)
2184 if (strchr(vol_args
.name
, '/'))
2187 path
= btrfs_alloc_path();
2191 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2192 mutex_lock(&root
->fs_info
->fs_mutex
);
2193 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2195 vol_args
.name
, namelen
, 0);
2196 mutex_unlock(&root
->fs_info
->fs_mutex
);
2197 btrfs_free_path(path
);
2198 if (di
&& !IS_ERR(di
))
2203 if (root
== root
->fs_info
->tree_root
)
2204 return create_subvol(root
, vol_args
.name
, namelen
);
2205 return create_snapshot(root
, vol_args
.name
, namelen
);
2208 static int btrfs_ioctl_defrag(struct file
*file
)
2210 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2211 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2213 switch (inode
->i_mode
& S_IFMT
) {
2215 mutex_lock(&root
->fs_info
->fs_mutex
);
2216 btrfs_defrag_root(root
, 0);
2217 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2218 mutex_unlock(&root
->fs_info
->fs_mutex
);
2221 btrfs_defrag_file(file
);
2228 long btrfs_ioctl(struct file
*file
, unsigned int
2229 cmd
, unsigned long arg
)
2231 struct btrfs_root
*root
= BTRFS_I(file
->f_path
.dentry
->d_inode
)->root
;
2234 case BTRFS_IOC_SNAP_CREATE
:
2235 return btrfs_ioctl_snap_create(root
, (void __user
*)arg
);
2236 case BTRFS_IOC_DEFRAG
:
2237 return btrfs_ioctl_defrag(file
);
2244 * Called inside transaction, so use GFP_NOFS
2246 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2248 struct btrfs_inode
*ei
;
2250 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2254 return &ei
->vfs_inode
;
2257 void btrfs_destroy_inode(struct inode
*inode
)
2259 WARN_ON(!list_empty(&inode
->i_dentry
));
2260 WARN_ON(inode
->i_data
.nrpages
);
2262 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2265 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2266 static void init_once(struct kmem_cache
* cachep
, void *foo
)
2268 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2269 unsigned long flags
)
2272 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2274 inode_init_once(&ei
->vfs_inode
);
2277 void btrfs_destroy_cachep(void)
2279 if (btrfs_inode_cachep
)
2280 kmem_cache_destroy(btrfs_inode_cachep
);
2281 if (btrfs_trans_handle_cachep
)
2282 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2283 if (btrfs_transaction_cachep
)
2284 kmem_cache_destroy(btrfs_transaction_cachep
);
2285 if (btrfs_bit_radix_cachep
)
2286 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2287 if (btrfs_path_cachep
)
2288 kmem_cache_destroy(btrfs_path_cachep
);
2291 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
2292 unsigned long extra_flags
,
2293 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2294 void (*ctor
)(struct kmem_cache
*, void *)
2296 void (*ctor
)(void *, struct kmem_cache
*,
2301 return kmem_cache_create(name
, size
, 0, (SLAB_RECLAIM_ACCOUNT
|
2302 SLAB_MEM_SPREAD
| extra_flags
), ctor
2303 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2309 int btrfs_init_cachep(void)
2311 btrfs_inode_cachep
= btrfs_cache_create("btrfs_inode_cache",
2312 sizeof(struct btrfs_inode
),
2314 if (!btrfs_inode_cachep
)
2316 btrfs_trans_handle_cachep
=
2317 btrfs_cache_create("btrfs_trans_handle_cache",
2318 sizeof(struct btrfs_trans_handle
),
2320 if (!btrfs_trans_handle_cachep
)
2322 btrfs_transaction_cachep
= btrfs_cache_create("btrfs_transaction_cache",
2323 sizeof(struct btrfs_transaction
),
2325 if (!btrfs_transaction_cachep
)
2327 btrfs_path_cachep
= btrfs_cache_create("btrfs_path_cache",
2328 sizeof(struct btrfs_path
),
2330 if (!btrfs_path_cachep
)
2332 btrfs_bit_radix_cachep
= btrfs_cache_create("btrfs_radix", 256,
2333 SLAB_DESTROY_BY_RCU
, NULL
);
2334 if (!btrfs_bit_radix_cachep
)
2338 btrfs_destroy_cachep();
2342 static int btrfs_getattr(struct vfsmount
*mnt
,
2343 struct dentry
*dentry
, struct kstat
*stat
)
2345 struct inode
*inode
= dentry
->d_inode
;
2346 generic_fillattr(inode
, stat
);
2347 stat
->blksize
= 256 * 1024;
2351 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2352 struct inode
* new_dir
,struct dentry
*new_dentry
)
2354 struct btrfs_trans_handle
*trans
;
2355 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2356 struct inode
*new_inode
= new_dentry
->d_inode
;
2357 struct inode
*old_inode
= old_dentry
->d_inode
;
2358 struct timespec ctime
= CURRENT_TIME
;
2359 struct btrfs_path
*path
;
2362 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2363 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2367 mutex_lock(&root
->fs_info
->fs_mutex
);
2368 trans
= btrfs_start_transaction(root
, 1);
2370 btrfs_set_trans_block_group(trans
, new_dir
);
2371 path
= btrfs_alloc_path();
2377 old_dentry
->d_inode
->i_nlink
++;
2378 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2379 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2380 old_inode
->i_ctime
= ctime
;
2382 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2387 new_inode
->i_ctime
= CURRENT_TIME
;
2388 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2392 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2397 btrfs_free_path(path
);
2398 btrfs_end_transaction(trans
, root
);
2399 mutex_unlock(&root
->fs_info
->fs_mutex
);
2403 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2404 const char *symname
)
2406 struct btrfs_trans_handle
*trans
;
2407 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2408 struct btrfs_path
*path
;
2409 struct btrfs_key key
;
2410 struct inode
*inode
;
2417 struct btrfs_file_extent_item
*ei
;
2418 struct extent_buffer
*leaf
;
2421 name_len
= strlen(symname
) + 1;
2422 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2423 return -ENAMETOOLONG
;
2424 mutex_lock(&root
->fs_info
->fs_mutex
);
2425 trans
= btrfs_start_transaction(root
, 1);
2426 btrfs_set_trans_block_group(trans
, dir
);
2428 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2434 inode
= btrfs_new_inode(trans
, root
, objectid
,
2435 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2436 err
= PTR_ERR(inode
);
2440 btrfs_set_trans_block_group(trans
, inode
);
2441 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2445 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2446 inode
->i_fop
= &btrfs_file_operations
;
2447 inode
->i_op
= &btrfs_file_inode_operations
;
2448 extent_map_tree_init(&BTRFS_I(inode
)->extent_tree
,
2449 inode
->i_mapping
, GFP_NOFS
);
2450 BTRFS_I(inode
)->extent_tree
.ops
= &btrfs_extent_map_ops
;
2452 dir
->i_sb
->s_dirt
= 1;
2453 btrfs_update_inode_block_group(trans
, inode
);
2454 btrfs_update_inode_block_group(trans
, dir
);
2458 path
= btrfs_alloc_path();
2460 key
.objectid
= inode
->i_ino
;
2462 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2463 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2464 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2470 leaf
= path
->nodes
[0];
2471 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
2472 struct btrfs_file_extent_item
);
2473 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
2474 btrfs_set_file_extent_type(leaf
, ei
,
2475 BTRFS_FILE_EXTENT_INLINE
);
2476 ptr
= btrfs_file_extent_inline_start(ei
);
2477 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
2478 btrfs_mark_buffer_dirty(leaf
);
2479 btrfs_free_path(path
);
2481 inode
->i_op
= &btrfs_symlink_inode_operations
;
2482 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2483 inode
->i_size
= name_len
- 1;
2484 err
= btrfs_update_inode(trans
, root
, inode
);
2489 nr
= trans
->blocks_used
;
2490 btrfs_end_transaction(trans
, root
);
2491 mutex_unlock(&root
->fs_info
->fs_mutex
);
2493 inode_dec_link_count(inode
);
2496 btrfs_btree_balance_dirty(root
, nr
);
2500 static struct inode_operations btrfs_dir_inode_operations
= {
2501 .lookup
= btrfs_lookup
,
2502 .create
= btrfs_create
,
2503 .unlink
= btrfs_unlink
,
2505 .mkdir
= btrfs_mkdir
,
2506 .rmdir
= btrfs_rmdir
,
2507 .rename
= btrfs_rename
,
2508 .symlink
= btrfs_symlink
,
2509 .setattr
= btrfs_setattr
,
2510 .mknod
= btrfs_mknod
,
2511 .setxattr
= generic_setxattr
,
2512 .getxattr
= generic_getxattr
,
2513 .listxattr
= btrfs_listxattr
,
2514 .removexattr
= generic_removexattr
,
2517 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2518 .lookup
= btrfs_lookup
,
2521 static struct file_operations btrfs_dir_file_operations
= {
2522 .llseek
= generic_file_llseek
,
2523 .read
= generic_read_dir
,
2524 .readdir
= btrfs_readdir
,
2525 .unlocked_ioctl
= btrfs_ioctl
,
2526 #ifdef CONFIG_COMPAT
2527 .compat_ioctl
= btrfs_ioctl
,
2531 static struct extent_map_ops btrfs_extent_map_ops
= {
2532 .fill_delalloc
= run_delalloc_range
,
2533 .writepage_io_hook
= btrfs_writepage_io_hook
,
2534 .readpage_io_hook
= btrfs_readpage_io_hook
,
2535 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
2538 static struct address_space_operations btrfs_aops
= {
2539 .readpage
= btrfs_readpage
,
2540 .writepage
= btrfs_writepage
,
2541 .writepages
= btrfs_writepages
,
2542 .readpages
= btrfs_readpages
,
2543 .sync_page
= block_sync_page
,
2544 .prepare_write
= btrfs_prepare_write
,
2545 .commit_write
= btrfs_commit_write
,
2547 .invalidatepage
= btrfs_invalidatepage
,
2548 .releasepage
= btrfs_releasepage
,
2549 .set_page_dirty
= __set_page_dirty_nobuffers
,
2552 static struct address_space_operations btrfs_symlink_aops
= {
2553 .readpage
= btrfs_readpage
,
2554 .writepage
= btrfs_writepage
,
2555 .invalidatepage
= btrfs_invalidatepage
,
2556 .releasepage
= btrfs_releasepage
,
2559 static struct inode_operations btrfs_file_inode_operations
= {
2560 .truncate
= btrfs_truncate
,
2561 .getattr
= btrfs_getattr
,
2562 .setattr
= btrfs_setattr
,
2563 .setxattr
= generic_setxattr
,
2564 .getxattr
= generic_getxattr
,
2565 .listxattr
= btrfs_listxattr
,
2566 .removexattr
= generic_removexattr
,
2569 static struct inode_operations btrfs_special_inode_operations
= {
2570 .getattr
= btrfs_getattr
,
2571 .setattr
= btrfs_setattr
,
2574 static struct inode_operations btrfs_symlink_inode_operations
= {
2575 .readlink
= generic_readlink
,
2576 .follow_link
= page_follow_link_light
,
2577 .put_link
= page_put_link
,