1 #include <linux/module.h>
2 #include <linux/buffer_head.h>
4 #include <linux/pagemap.h>
5 #include <linux/highmem.h>
6 #include <linux/time.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/smp_lock.h>
10 #include <linux/backing-dev.h>
11 #include <linux/mpage.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include <linux/statfs.h>
17 #include "transaction.h"
18 #include "btrfs_inode.h"
20 #include "print-tree.h"
22 struct btrfs_iget_args
{
24 struct btrfs_root
*root
;
27 #define BTRFS_SUPER_MAGIC 0x9123682E
29 static struct inode_operations btrfs_dir_inode_operations
;
30 static struct inode_operations btrfs_symlink_inode_operations
;
31 static struct inode_operations btrfs_dir_ro_inode_operations
;
32 static struct super_operations btrfs_super_ops
;
33 static struct file_operations btrfs_dir_file_operations
;
34 static struct inode_operations btrfs_file_inode_operations
;
35 static struct address_space_operations btrfs_aops
;
36 static struct address_space_operations btrfs_symlink_aops
;
37 static struct file_operations btrfs_file_operations
;
39 static int drop_extents(struct btrfs_trans_handle
*trans
,
40 struct btrfs_root
*root
,
42 u64 start
, u64 end
, u64
*hint_block
);
43 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
44 struct buffer_head
*result
, int create
);
46 static void btrfs_read_locked_inode(struct inode
*inode
)
48 struct btrfs_path
*path
;
49 struct btrfs_inode_item
*inode_item
;
50 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
51 struct btrfs_key location
;
52 struct btrfs_block_group_cache
*alloc_group
;
53 u64 alloc_group_block
;
56 path
= btrfs_alloc_path();
58 btrfs_init_path(path
);
59 mutex_lock(&root
->fs_info
->fs_mutex
);
61 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
62 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
64 btrfs_free_path(path
);
67 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
69 struct btrfs_inode_item
);
71 inode
->i_mode
= btrfs_inode_mode(inode_item
);
72 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
73 inode
->i_uid
= btrfs_inode_uid(inode_item
);
74 inode
->i_gid
= btrfs_inode_gid(inode_item
);
75 inode
->i_size
= btrfs_inode_size(inode_item
);
76 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
77 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
78 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
79 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
80 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
81 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
82 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
83 inode
->i_generation
= btrfs_inode_generation(inode_item
);
84 alloc_group_block
= btrfs_inode_block_group(inode_item
);
85 ret
= radix_tree_gang_lookup(&root
->fs_info
->block_group_radix
,
86 (void **)&alloc_group
,
87 alloc_group_block
, 1);
89 BTRFS_I(inode
)->block_group
= alloc_group
;
91 btrfs_free_path(path
);
94 mutex_unlock(&root
->fs_info
->fs_mutex
);
96 switch (inode
->i_mode
& S_IFMT
) {
99 init_special_inode(inode
, inode
->i_mode
,
100 btrfs_inode_rdev(inode_item
));
104 inode
->i_mapping
->a_ops
= &btrfs_aops
;
105 inode
->i_fop
= &btrfs_file_operations
;
106 inode
->i_op
= &btrfs_file_inode_operations
;
109 inode
->i_fop
= &btrfs_dir_file_operations
;
110 if (root
== root
->fs_info
->tree_root
)
111 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
113 inode
->i_op
= &btrfs_dir_inode_operations
;
116 inode
->i_op
= &btrfs_symlink_inode_operations
;
117 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
123 btrfs_release_path(root
, path
);
124 btrfs_free_path(path
);
125 mutex_unlock(&root
->fs_info
->fs_mutex
);
126 make_bad_inode(inode
);
129 static void fill_inode_item(struct btrfs_inode_item
*item
,
132 btrfs_set_inode_uid(item
, inode
->i_uid
);
133 btrfs_set_inode_gid(item
, inode
->i_gid
);
134 btrfs_set_inode_size(item
, inode
->i_size
);
135 btrfs_set_inode_mode(item
, inode
->i_mode
);
136 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
137 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
138 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
139 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
140 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
141 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
142 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
143 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
144 btrfs_set_inode_generation(item
, inode
->i_generation
);
145 btrfs_set_inode_block_group(item
,
146 BTRFS_I(inode
)->block_group
->key
.objectid
);
149 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
150 struct btrfs_root
*root
,
153 struct btrfs_inode_item
*inode_item
;
154 struct btrfs_path
*path
;
157 path
= btrfs_alloc_path();
159 btrfs_init_path(path
);
160 ret
= btrfs_lookup_inode(trans
, root
, path
,
161 &BTRFS_I(inode
)->location
, 1);
168 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
170 struct btrfs_inode_item
);
172 fill_inode_item(inode_item
, inode
);
173 btrfs_mark_buffer_dirty(path
->nodes
[0]);
176 btrfs_release_path(root
, path
);
177 btrfs_free_path(path
);
182 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
183 struct btrfs_root
*root
,
185 struct dentry
*dentry
)
187 struct btrfs_path
*path
;
188 const char *name
= dentry
->d_name
.name
;
189 int name_len
= dentry
->d_name
.len
;
192 struct btrfs_dir_item
*di
;
194 path
= btrfs_alloc_path();
196 btrfs_init_path(path
);
197 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
207 objectid
= btrfs_disk_key_objectid(&di
->location
);
208 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
210 btrfs_release_path(root
, path
);
212 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
213 objectid
, name
, name_len
, -1);
222 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
225 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
227 btrfs_free_path(path
);
229 dir
->i_size
-= name_len
* 2;
230 btrfs_update_inode(trans
, root
, dir
);
231 drop_nlink(dentry
->d_inode
);
232 btrfs_update_inode(trans
, root
, dentry
->d_inode
);
233 dir
->i_sb
->s_dirt
= 1;
238 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
240 struct btrfs_root
*root
;
241 struct btrfs_trans_handle
*trans
;
244 root
= BTRFS_I(dir
)->root
;
245 mutex_lock(&root
->fs_info
->fs_mutex
);
246 trans
= btrfs_start_transaction(root
, 1);
247 btrfs_set_trans_block_group(trans
, dir
);
248 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
249 btrfs_end_transaction(trans
, root
);
250 mutex_unlock(&root
->fs_info
->fs_mutex
);
251 btrfs_btree_balance_dirty(root
);
255 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
257 struct inode
*inode
= dentry
->d_inode
;
260 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
261 struct btrfs_path
*path
;
262 struct btrfs_key key
;
263 struct btrfs_trans_handle
*trans
;
264 struct btrfs_key found_key
;
266 struct btrfs_leaf
*leaf
;
267 char *goodnames
= "..";
269 path
= btrfs_alloc_path();
271 btrfs_init_path(path
);
272 mutex_lock(&root
->fs_info
->fs_mutex
);
273 trans
= btrfs_start_transaction(root
, 1);
274 btrfs_set_trans_block_group(trans
, dir
);
275 key
.objectid
= inode
->i_ino
;
276 key
.offset
= (u64
)-1;
279 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
285 if (path
->slots
[0] == 0) {
290 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
291 btrfs_disk_key_to_cpu(&found_key
,
292 &leaf
->items
[path
->slots
[0]].key
);
293 found_type
= btrfs_key_type(&found_key
);
294 if (found_key
.objectid
!= inode
->i_ino
) {
298 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
299 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
300 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
301 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
305 ret
= btrfs_del_item(trans
, root
, path
);
308 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
310 btrfs_release_path(root
, path
);
313 btrfs_release_path(root
, path
);
315 /* now the directory is empty */
316 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
321 btrfs_release_path(root
, path
);
322 btrfs_free_path(path
);
323 mutex_unlock(&root
->fs_info
->fs_mutex
);
324 ret
= btrfs_end_transaction(trans
, root
);
325 btrfs_btree_balance_dirty(root
);
331 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
332 struct btrfs_root
*root
,
335 struct btrfs_path
*path
;
340 path
= btrfs_alloc_path();
342 btrfs_init_path(path
);
343 ret
= btrfs_lookup_inode(trans
, root
, path
,
344 &BTRFS_I(inode
)->location
, -1);
346 ret
= btrfs_del_item(trans
, root
, path
);
348 btrfs_free_path(path
);
352 static void reada_truncate(struct btrfs_root
*root
, struct btrfs_path
*path
,
355 struct btrfs_node
*node
;
365 node
= btrfs_buffer_node(path
->nodes
[1]);
366 slot
= path
->slots
[1];
369 nritems
= btrfs_header_nritems(&node
->header
);
370 for (i
= slot
- 1; i
>= 0; i
--) {
371 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
372 if (item_objectid
!= objectid
)
374 blocknr
= btrfs_node_blockptr(node
, i
);
375 ret
= readahead_tree_block(root
, blocknr
);
381 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
382 struct btrfs_root
*root
,
386 struct btrfs_path
*path
;
387 struct btrfs_key key
;
388 struct btrfs_disk_key
*found_key
;
390 struct btrfs_leaf
*leaf
;
391 struct btrfs_file_extent_item
*fi
;
392 u64 extent_start
= 0;
393 u64 extent_num_blocks
= 0;
398 path
= btrfs_alloc_path();
400 /* FIXME, add redo link to tree so we don't leak on crash */
401 key
.objectid
= inode
->i_ino
;
402 key
.offset
= (u64
)-1;
405 btrfs_init_path(path
);
407 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
412 BUG_ON(path
->slots
[0] == 0);
415 reada_truncate(root
, path
, inode
->i_ino
);
416 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
417 found_key
= &leaf
->items
[path
->slots
[0]].key
;
418 found_type
= btrfs_disk_key_type(found_key
);
419 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
421 if (found_type
!= BTRFS_CSUM_ITEM_KEY
&&
422 found_type
!= BTRFS_DIR_ITEM_KEY
&&
423 found_type
!= BTRFS_DIR_INDEX_KEY
&&
424 found_type
!= BTRFS_EXTENT_DATA_KEY
)
426 item_end
= btrfs_disk_key_offset(found_key
);
427 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
428 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
430 struct btrfs_file_extent_item
);
431 if (btrfs_file_extent_type(fi
) !=
432 BTRFS_FILE_EXTENT_INLINE
) {
433 item_end
+= btrfs_file_extent_num_blocks(fi
) <<
437 if (found_type
== BTRFS_CSUM_ITEM_KEY
) {
438 ret
= btrfs_csum_truncate(trans
, root
, path
,
442 if (item_end
< inode
->i_size
) {
444 btrfs_set_key_type(&key
, found_type
- 1);
449 if (btrfs_disk_key_offset(found_key
) >= inode
->i_size
)
455 if (found_type
== BTRFS_EXTENT_DATA_KEY
&&
456 btrfs_file_extent_type(fi
) !=
457 BTRFS_FILE_EXTENT_INLINE
) {
460 u64 orig_num_blocks
=
461 btrfs_file_extent_num_blocks(fi
);
462 extent_num_blocks
= inode
->i_size
-
463 btrfs_disk_key_offset(found_key
) +
465 extent_num_blocks
>>= inode
->i_blkbits
;
466 btrfs_set_file_extent_num_blocks(fi
,
468 inode
->i_blocks
-= (orig_num_blocks
-
469 extent_num_blocks
) << 3;
470 mark_buffer_dirty(path
->nodes
[0]);
473 btrfs_file_extent_disk_blocknr(fi
);
475 btrfs_file_extent_disk_num_blocks(fi
);
476 /* FIXME blocksize != 4096 */
477 num_dec
= btrfs_file_extent_num_blocks(fi
) << 3;
478 if (extent_start
!= 0) {
480 inode
->i_blocks
-= num_dec
;
485 ret
= btrfs_del_item(trans
, root
, path
);
490 btrfs_release_path(root
, path
);
492 ret
= btrfs_free_extent(trans
, root
, extent_start
,
493 extent_num_blocks
, 0);
499 btrfs_release_path(root
, path
);
500 btrfs_free_path(path
);
501 inode
->i_sb
->s_dirt
= 1;
505 static int btrfs_truncate_page(struct address_space
*mapping
, loff_t from
)
507 struct inode
*inode
= mapping
->host
;
508 unsigned blocksize
= 1 << inode
->i_blkbits
;
509 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
510 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
514 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
516 struct btrfs_key ins
;
517 struct btrfs_trans_handle
*trans
;
519 if ((offset
& (blocksize
- 1)) == 0)
523 page
= grab_cache_page(mapping
, index
);
527 if (!PageUptodate(page
)) {
528 ret
= mpage_readpage(page
, btrfs_get_block
);
530 if (!PageUptodate(page
)) {
535 mutex_lock(&root
->fs_info
->fs_mutex
);
536 trans
= btrfs_start_transaction(root
, 1);
537 btrfs_set_trans_block_group(trans
, inode
);
539 ret
= drop_extents(trans
, root
, inode
, page
->index
<< PAGE_CACHE_SHIFT
,
540 (page
->index
+ 1) << PAGE_CACHE_SHIFT
, &alloc_hint
);
542 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
, 1,
543 alloc_hint
, (u64
)-1, &ins
, 1);
545 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
546 page
->index
<< PAGE_CACHE_SHIFT
,
549 SetPageChecked(page
);
551 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
552 flush_dcache_page(page
);
553 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
554 page
->index
<< PAGE_CACHE_SHIFT
,
555 kaddr
, PAGE_CACHE_SIZE
);
557 btrfs_end_transaction(trans
, root
);
558 mutex_unlock(&root
->fs_info
->fs_mutex
);
560 set_page_dirty(page
);
562 page_cache_release(page
);
567 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
569 struct inode
*inode
= dentry
->d_inode
;
572 err
= inode_change_ok(inode
, attr
);
576 if (S_ISREG(inode
->i_mode
) &&
577 attr
->ia_valid
& ATTR_SIZE
&& attr
->ia_size
> inode
->i_size
) {
578 struct btrfs_trans_handle
*trans
;
579 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
580 u64 mask
= root
->blocksize
- 1;
581 u64 pos
= (inode
->i_size
+ mask
) & ~mask
;
584 if (attr
->ia_size
<= pos
)
587 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
589 hole_size
= (attr
->ia_size
- pos
+ mask
) & ~mask
;
590 hole_size
>>= inode
->i_blkbits
;
592 mutex_lock(&root
->fs_info
->fs_mutex
);
593 trans
= btrfs_start_transaction(root
, 1);
594 btrfs_set_trans_block_group(trans
, inode
);
595 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
596 pos
, 0, 0, hole_size
);
598 btrfs_end_transaction(trans
, root
);
599 mutex_unlock(&root
->fs_info
->fs_mutex
);
602 err
= inode_setattr(inode
, attr
);
606 static void btrfs_delete_inode(struct inode
*inode
)
608 struct btrfs_trans_handle
*trans
;
609 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
612 truncate_inode_pages(&inode
->i_data
, 0);
613 if (is_bad_inode(inode
)) {
617 mutex_lock(&root
->fs_info
->fs_mutex
);
618 trans
= btrfs_start_transaction(root
, 1);
619 btrfs_set_trans_block_group(trans
, inode
);
620 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
622 btrfs_free_inode(trans
, root
, inode
);
623 btrfs_end_transaction(trans
, root
);
624 mutex_unlock(&root
->fs_info
->fs_mutex
);
625 btrfs_btree_balance_dirty(root
);
631 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
632 struct btrfs_key
*location
)
634 const char *name
= dentry
->d_name
.name
;
635 int namelen
= dentry
->d_name
.len
;
636 struct btrfs_dir_item
*di
;
637 struct btrfs_path
*path
;
638 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
641 path
= btrfs_alloc_path();
643 btrfs_init_path(path
);
644 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
646 if (!di
|| IS_ERR(di
)) {
647 location
->objectid
= 0;
651 btrfs_disk_key_to_cpu(location
, &di
->location
);
653 btrfs_release_path(root
, path
);
654 btrfs_free_path(path
);
658 static int fixup_tree_root_location(struct btrfs_root
*root
,
659 struct btrfs_key
*location
,
660 struct btrfs_root
**sub_root
)
662 struct btrfs_path
*path
;
663 struct btrfs_root_item
*ri
;
665 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
667 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
670 path
= btrfs_alloc_path();
672 mutex_lock(&root
->fs_info
->fs_mutex
);
674 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
675 if (IS_ERR(*sub_root
))
676 return PTR_ERR(*sub_root
);
678 ri
= &(*sub_root
)->root_item
;
679 location
->objectid
= btrfs_root_dirid(ri
);
681 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
682 location
->offset
= 0;
684 btrfs_free_path(path
);
685 mutex_unlock(&root
->fs_info
->fs_mutex
);
689 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
691 struct btrfs_iget_args
*args
= p
;
692 inode
->i_ino
= args
->ino
;
693 BTRFS_I(inode
)->root
= args
->root
;
697 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
699 struct btrfs_iget_args
*args
= opaque
;
700 return (args
->ino
== inode
->i_ino
&&
701 args
->root
== BTRFS_I(inode
)->root
);
704 static struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
705 struct btrfs_root
*root
)
708 struct btrfs_iget_args args
;
712 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
713 btrfs_init_locked_inode
,
718 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
719 struct nameidata
*nd
)
721 struct inode
* inode
;
722 struct btrfs_inode
*bi
= BTRFS_I(dir
);
723 struct btrfs_root
*root
= bi
->root
;
724 struct btrfs_root
*sub_root
= root
;
725 struct btrfs_key location
;
728 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
729 return ERR_PTR(-ENAMETOOLONG
);
730 mutex_lock(&root
->fs_info
->fs_mutex
);
731 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
732 mutex_unlock(&root
->fs_info
->fs_mutex
);
736 if (location
.objectid
) {
737 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
741 return ERR_PTR(-ENOENT
);
742 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
745 return ERR_PTR(-EACCES
);
746 if (inode
->i_state
& I_NEW
) {
747 if (sub_root
!= root
) {
748 printk("adding new root for inode %lu root %p (found %p)\n", inode
->i_ino
, sub_root
, BTRFS_I(inode
)->root
);
750 sub_root
->inode
= inode
;
752 BTRFS_I(inode
)->root
= sub_root
;
753 memcpy(&BTRFS_I(inode
)->location
, &location
,
755 btrfs_read_locked_inode(inode
);
756 unlock_new_inode(inode
);
759 return d_splice_alias(inode
, dentry
);
762 static void reada_leaves(struct btrfs_root
*root
, struct btrfs_path
*path
,
765 struct btrfs_node
*node
;
775 node
= btrfs_buffer_node(path
->nodes
[1]);
776 slot
= path
->slots
[1];
777 nritems
= btrfs_header_nritems(&node
->header
);
778 for (i
= slot
+ 1; i
< nritems
; i
++) {
779 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
780 if (item_objectid
!= objectid
)
782 blocknr
= btrfs_node_blockptr(node
, i
);
783 ret
= readahead_tree_block(root
, blocknr
);
789 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
791 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
792 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
793 struct btrfs_item
*item
;
794 struct btrfs_dir_item
*di
;
795 struct btrfs_key key
;
796 struct btrfs_path
*path
;
799 struct btrfs_leaf
*leaf
;
802 unsigned char d_type
= DT_UNKNOWN
;
807 int key_type
= BTRFS_DIR_INDEX_KEY
;
809 /* FIXME, use a real flag for deciding about the key type */
810 if (root
->fs_info
->tree_root
== root
)
811 key_type
= BTRFS_DIR_ITEM_KEY
;
812 mutex_lock(&root
->fs_info
->fs_mutex
);
813 key
.objectid
= inode
->i_ino
;
815 btrfs_set_key_type(&key
, key_type
);
816 key
.offset
= filp
->f_pos
;
817 path
= btrfs_alloc_path();
818 btrfs_init_path(path
);
819 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
823 reada_leaves(root
, path
, inode
->i_ino
);
825 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
826 nritems
= btrfs_header_nritems(&leaf
->header
);
827 slot
= path
->slots
[0];
828 if (advance
|| slot
>= nritems
) {
829 if (slot
>= nritems
-1) {
830 reada_leaves(root
, path
, inode
->i_ino
);
831 ret
= btrfs_next_leaf(root
, path
);
834 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
835 nritems
= btrfs_header_nritems(&leaf
->header
);
836 slot
= path
->slots
[0];
843 item
= leaf
->items
+ slot
;
844 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
846 if (btrfs_disk_key_type(&item
->key
) != key_type
)
848 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
850 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
852 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
854 di_total
= btrfs_item_size(leaf
->items
+ slot
);
855 while(di_cur
< di_total
) {
856 over
= filldir(dirent
, (const char *)(di
+ 1),
857 btrfs_dir_name_len(di
),
858 btrfs_disk_key_offset(&item
->key
),
859 btrfs_disk_key_objectid(&di
->location
),
863 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
865 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
872 btrfs_release_path(root
, path
);
873 btrfs_free_path(path
);
874 mutex_unlock(&root
->fs_info
->fs_mutex
);
878 static void btrfs_put_super (struct super_block
* sb
)
880 struct btrfs_root
*root
= btrfs_sb(sb
);
883 ret
= close_ctree(root
);
885 printk("close ctree returns %d\n", ret
);
887 sb
->s_fs_info
= NULL
;
890 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
892 struct inode
* inode
;
893 struct dentry
* root_dentry
;
894 struct btrfs_super_block
*disk_super
;
895 struct btrfs_root
*tree_root
;
896 struct btrfs_inode
*bi
;
898 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
899 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
900 sb
->s_op
= &btrfs_super_ops
;
903 tree_root
= open_ctree(sb
);
906 printk("btrfs: open_ctree failed\n");
909 sb
->s_fs_info
= tree_root
;
910 disk_super
= tree_root
->fs_info
->disk_super
;
911 printk("read in super total blocks %Lu root %Lu\n",
912 btrfs_super_total_blocks(disk_super
),
913 btrfs_super_root_dir(disk_super
));
915 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
918 bi
->location
.objectid
= inode
->i_ino
;
919 bi
->location
.offset
= 0;
920 bi
->location
.flags
= 0;
921 bi
->root
= tree_root
;
922 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
926 if (inode
->i_state
& I_NEW
) {
927 btrfs_read_locked_inode(inode
);
928 unlock_new_inode(inode
);
931 root_dentry
= d_alloc_root(inode
);
936 sb
->s_root
= root_dentry
;
941 static int btrfs_write_inode(struct inode
*inode
, int wait
)
943 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
944 struct btrfs_trans_handle
*trans
;
948 mutex_lock(&root
->fs_info
->fs_mutex
);
949 trans
= btrfs_start_transaction(root
, 1);
950 btrfs_set_trans_block_group(trans
, inode
);
951 ret
= btrfs_commit_transaction(trans
, root
);
952 mutex_unlock(&root
->fs_info
->fs_mutex
);
957 static void btrfs_dirty_inode(struct inode
*inode
)
959 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
960 struct btrfs_trans_handle
*trans
;
962 mutex_lock(&root
->fs_info
->fs_mutex
);
963 trans
= btrfs_start_transaction(root
, 1);
964 btrfs_set_trans_block_group(trans
, inode
);
965 btrfs_update_inode(trans
, root
, inode
);
966 btrfs_end_transaction(trans
, root
);
967 mutex_unlock(&root
->fs_info
->fs_mutex
);
968 btrfs_btree_balance_dirty(root
);
971 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
972 struct btrfs_root
*root
,
974 struct btrfs_block_group_cache
*group
,
978 struct btrfs_inode_item inode_item
;
979 struct btrfs_key
*location
;
983 inode
= new_inode(root
->fs_info
->sb
);
985 return ERR_PTR(-ENOMEM
);
987 BTRFS_I(inode
)->root
= root
;
992 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
993 BTRFS_I(inode
)->block_group
= group
;
995 inode
->i_uid
= current
->fsuid
;
996 inode
->i_gid
= current
->fsgid
;
997 inode
->i_mode
= mode
;
998 inode
->i_ino
= objectid
;
1000 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1001 fill_inode_item(&inode_item
, inode
);
1002 location
= &BTRFS_I(inode
)->location
;
1003 location
->objectid
= objectid
;
1004 location
->flags
= 0;
1005 location
->offset
= 0;
1006 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
1008 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
1011 insert_inode_hash(inode
);
1015 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
1016 struct dentry
*dentry
, struct inode
*inode
)
1019 struct btrfs_key key
;
1020 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
1021 key
.objectid
= inode
->i_ino
;
1023 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1026 ret
= btrfs_insert_dir_item(trans
, root
,
1027 dentry
->d_name
.name
, dentry
->d_name
.len
,
1028 dentry
->d_parent
->d_inode
->i_ino
,
1031 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
1032 ret
= btrfs_update_inode(trans
, root
,
1033 dentry
->d_parent
->d_inode
);
1038 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
1039 struct dentry
*dentry
, struct inode
*inode
)
1041 int err
= btrfs_add_link(trans
, dentry
, inode
);
1043 d_instantiate(dentry
, inode
);
1051 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
1052 int mode
, struct nameidata
*nd
)
1054 struct btrfs_trans_handle
*trans
;
1055 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1056 struct inode
*inode
;
1061 mutex_lock(&root
->fs_info
->fs_mutex
);
1062 trans
= btrfs_start_transaction(root
, 1);
1063 btrfs_set_trans_block_group(trans
, dir
);
1065 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1071 inode
= btrfs_new_inode(trans
, root
, objectid
,
1072 BTRFS_I(dir
)->block_group
, mode
);
1073 err
= PTR_ERR(inode
);
1077 btrfs_set_trans_block_group(trans
, inode
);
1078 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1082 inode
->i_mapping
->a_ops
= &btrfs_aops
;
1083 inode
->i_fop
= &btrfs_file_operations
;
1084 inode
->i_op
= &btrfs_file_inode_operations
;
1086 dir
->i_sb
->s_dirt
= 1;
1087 btrfs_update_inode_block_group(trans
, inode
);
1088 btrfs_update_inode_block_group(trans
, dir
);
1090 btrfs_end_transaction(trans
, root
);
1091 mutex_unlock(&root
->fs_info
->fs_mutex
);
1094 inode_dec_link_count(inode
);
1097 btrfs_btree_balance_dirty(root
);
1101 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
1102 struct dentry
*dentry
)
1104 struct btrfs_trans_handle
*trans
;
1105 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1106 struct inode
*inode
= old_dentry
->d_inode
;
1110 if (inode
->i_nlink
== 0)
1114 mutex_lock(&root
->fs_info
->fs_mutex
);
1115 trans
= btrfs_start_transaction(root
, 1);
1116 btrfs_set_trans_block_group(trans
, dir
);
1117 atomic_inc(&inode
->i_count
);
1118 err
= btrfs_add_nondir(trans
, dentry
, inode
);
1121 dir
->i_sb
->s_dirt
= 1;
1122 btrfs_update_inode_block_group(trans
, dir
);
1123 btrfs_update_inode(trans
, root
, inode
);
1125 btrfs_end_transaction(trans
, root
);
1126 mutex_unlock(&root
->fs_info
->fs_mutex
);
1129 inode_dec_link_count(inode
);
1132 btrfs_btree_balance_dirty(root
);
1136 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
1137 struct btrfs_root
*root
,
1138 u64 objectid
, u64 dirid
)
1142 struct btrfs_key key
;
1147 key
.objectid
= objectid
;
1150 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
1152 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
1156 key
.objectid
= dirid
;
1157 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
1165 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1167 struct inode
*inode
;
1168 struct btrfs_trans_handle
*trans
;
1169 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1171 int drop_on_err
= 0;
1174 mutex_lock(&root
->fs_info
->fs_mutex
);
1175 trans
= btrfs_start_transaction(root
, 1);
1176 btrfs_set_trans_block_group(trans
, dir
);
1177 if (IS_ERR(trans
)) {
1178 err
= PTR_ERR(trans
);
1182 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1188 inode
= btrfs_new_inode(trans
, root
, objectid
,
1189 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1190 if (IS_ERR(inode
)) {
1191 err
= PTR_ERR(inode
);
1195 inode
->i_op
= &btrfs_dir_inode_operations
;
1196 inode
->i_fop
= &btrfs_dir_file_operations
;
1197 btrfs_set_trans_block_group(trans
, inode
);
1199 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1204 err
= btrfs_update_inode(trans
, root
, inode
);
1207 err
= btrfs_add_link(trans
, dentry
, inode
);
1210 d_instantiate(dentry
, inode
);
1212 dir
->i_sb
->s_dirt
= 1;
1213 btrfs_update_inode_block_group(trans
, inode
);
1214 btrfs_update_inode_block_group(trans
, dir
);
1217 btrfs_end_transaction(trans
, root
);
1219 mutex_unlock(&root
->fs_info
->fs_mutex
);
1222 btrfs_btree_balance_dirty(root
);
1226 static int btrfs_sync_file(struct file
*file
,
1227 struct dentry
*dentry
, int datasync
)
1229 struct inode
*inode
= dentry
->d_inode
;
1230 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1232 struct btrfs_trans_handle
*trans
;
1234 mutex_lock(&root
->fs_info
->fs_mutex
);
1235 trans
= btrfs_start_transaction(root
, 1);
1240 ret
= btrfs_commit_transaction(trans
, root
);
1241 mutex_unlock(&root
->fs_info
->fs_mutex
);
1243 return ret
> 0 ? EIO
: ret
;
1246 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
1248 struct btrfs_trans_handle
*trans
;
1249 struct btrfs_root
*root
;
1251 root
= btrfs_sb(sb
);
1255 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
1258 mutex_lock(&root
->fs_info
->fs_mutex
);
1259 trans
= btrfs_start_transaction(root
, 1);
1260 ret
= btrfs_commit_transaction(trans
, root
);
1263 printk("btrfs sync_fs\n");
1264 mutex_unlock(&root
->fs_info
->fs_mutex
);
1268 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1269 struct buffer_head
*result
, int create
)
1274 u64 extent_start
= 0;
1276 u64 objectid
= inode
->i_ino
;
1279 struct btrfs_path
*path
;
1280 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1281 struct btrfs_file_extent_item
*item
;
1282 struct btrfs_leaf
*leaf
;
1283 struct btrfs_disk_key
*found_key
;
1284 struct btrfs_trans_handle
*trans
= NULL
;
1286 path
= btrfs_alloc_path();
1288 btrfs_init_path(path
);
1291 /* this almost but not quite works */
1292 trans
= btrfs_start_transaction(root
, 1);
1297 ret
= drop_extents(trans
, root
, inode
,
1298 iblock
<< inode
->i_blkbits
,
1299 (iblock
+ 1) << inode
->i_blkbits
,
1304 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1306 iblock
<< inode
->i_blkbits
, 0);
1313 if (path
->slots
[0] == 0) {
1314 btrfs_release_path(root
, path
);
1320 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1321 struct btrfs_file_extent_item
);
1322 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1323 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1324 blocknr
+= btrfs_file_extent_offset(item
);
1326 /* are we inside the extent that was found? */
1327 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1328 found_type
= btrfs_disk_key_type(found_key
);
1329 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1330 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1335 found_type
= btrfs_file_extent_type(item
);
1336 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1337 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1338 extent_start
= extent_start
>> inode
->i_blkbits
;
1339 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1341 if (btrfs_file_extent_disk_blocknr(item
) == 0)
1343 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1344 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1345 iblock
- extent_start
);
1348 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1352 size
= btrfs_file_extent_inline_len(leaf
->items
+
1354 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1355 extent_start
>>= inode
->i_blkbits
;
1356 if (iblock
< extent_start
|| iblock
> extent_end
) {
1359 ptr
= btrfs_file_extent_inline_start(item
);
1360 map
= kmap(result
->b_page
);
1361 memcpy(map
, ptr
, size
);
1362 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1363 flush_dcache_page(result
->b_page
);
1364 kunmap(result
->b_page
);
1365 set_buffer_uptodate(result
);
1366 SetPageChecked(result
->b_page
);
1367 btrfs_map_bh_to_logical(root
, result
, 0);
1371 struct btrfs_key ins
;
1372 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1373 1, alloc_hint
, (u64
)-1,
1376 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1377 iblock
<< inode
->i_blkbits
,
1378 ins
.objectid
, ins
.offset
,
1381 SetPageChecked(result
->b_page
);
1382 btrfs_map_bh_to_logical(root
, result
, ins
.objectid
);
1386 err
= btrfs_end_transaction(trans
, root
);
1387 btrfs_free_path(path
);
1391 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1392 struct buffer_head
*result
, int create
)
1395 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1396 mutex_lock(&root
->fs_info
->fs_mutex
);
1397 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1398 mutex_unlock(&root
->fs_info
->fs_mutex
);
1402 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1403 unsigned from
, unsigned to
)
1405 return block_prepare_write(page
, from
, to
, btrfs_get_block
);
1408 static void btrfs_write_super(struct super_block
*sb
)
1410 btrfs_sync_fs(sb
, 1);
1413 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1415 return mpage_readpage(page
, btrfs_get_block
);
1419 * While block_write_full_page is writing back the dirty buffers under
1420 * the page lock, whoever dirtied the buffers may decide to clean them
1421 * again at any time. We handle that by only looking at the buffer
1422 * state inside lock_buffer().
1424 * If block_write_full_page() is called for regular writeback
1425 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1426 * locked buffer. This only can happen if someone has written the buffer
1427 * directly, with submit_bh(). At the address_space level PageWriteback
1428 * prevents this contention from occurring.
1430 static int __btrfs_write_full_page(struct inode
*inode
, struct page
*page
,
1431 struct writeback_control
*wbc
)
1435 sector_t last_block
;
1436 struct buffer_head
*bh
, *head
;
1437 const unsigned blocksize
= 1 << inode
->i_blkbits
;
1438 int nr_underway
= 0;
1440 BUG_ON(!PageLocked(page
));
1442 last_block
= (i_size_read(inode
) - 1) >> inode
->i_blkbits
;
1444 if (!page_has_buffers(page
)) {
1445 create_empty_buffers(page
, blocksize
,
1446 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
1450 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1451 * here, and the (potentially unmapped) buffers may become dirty at
1452 * any time. If a buffer becomes dirty here after we've inspected it
1453 * then we just miss that fact, and the page stays dirty.
1455 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1456 * handle that here by just cleaning them.
1459 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1460 head
= page_buffers(page
);
1464 * Get all the dirty buffers mapped to disk addresses and
1465 * handle any aliases from the underlying blockdev's mapping.
1468 if (block
> last_block
) {
1470 * mapped buffers outside i_size will occur, because
1471 * this page can be outside i_size when there is a
1472 * truncate in progress.
1475 * The buffer was zeroed by block_write_full_page()
1477 clear_buffer_dirty(bh
);
1478 set_buffer_uptodate(bh
);
1479 } else if (!buffer_mapped(bh
) && buffer_dirty(bh
)) {
1480 WARN_ON(bh
->b_size
!= blocksize
);
1481 err
= btrfs_get_block(inode
, block
, bh
, 0);
1483 printk("writepage going to recovery err %d\n", err
);
1486 if (buffer_new(bh
)) {
1487 /* blockdev mappings never come here */
1488 clear_buffer_new(bh
);
1491 bh
= bh
->b_this_page
;
1493 } while (bh
!= head
);
1496 if (!buffer_mapped(bh
))
1499 * If it's a fully non-blocking write attempt and we cannot
1500 * lock the buffer then redirty the page. Note that this can
1501 * potentially cause a busy-wait loop from pdflush and kswapd
1502 * activity, but those code paths have their own higher-level
1505 if (wbc
->sync_mode
!= WB_SYNC_NONE
|| !wbc
->nonblocking
) {
1507 } else if (test_set_buffer_locked(bh
)) {
1508 redirty_page_for_writepage(wbc
, page
);
1511 if (test_clear_buffer_dirty(bh
) && bh
->b_blocknr
!= 0) {
1512 mark_buffer_async_write(bh
);
1516 } while ((bh
= bh
->b_this_page
) != head
);
1519 * The page and its buffers are protected by PageWriteback(), so we can
1520 * drop the bh refcounts early.
1522 BUG_ON(PageWriteback(page
));
1523 set_page_writeback(page
);
1526 struct buffer_head
*next
= bh
->b_this_page
;
1527 if (buffer_async_write(bh
)) {
1528 submit_bh(WRITE
, bh
);
1532 } while (bh
!= head
);
1537 if (nr_underway
== 0) {
1539 * The page was marked dirty, but the buffers were
1540 * clean. Someone wrote them back by hand with
1541 * ll_rw_block/submit_bh. A rare case.
1545 if (!buffer_uptodate(bh
)) {
1549 bh
= bh
->b_this_page
;
1550 } while (bh
!= head
);
1552 SetPageUptodate(page
);
1553 end_page_writeback(page
);
1559 * ENOSPC, or some other error. We may already have added some
1560 * blocks to the file, so we need to write these out to avoid
1561 * exposing stale data.
1562 * The page is currently locked and not marked for writeback
1565 /* Recovery: lock and submit the mapped buffers */
1567 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
1569 mark_buffer_async_write(bh
);
1572 * The buffer may have been set dirty during
1573 * attachment to a dirty page.
1575 clear_buffer_dirty(bh
);
1577 } while ((bh
= bh
->b_this_page
) != head
);
1579 BUG_ON(PageWriteback(page
));
1580 set_page_writeback(page
);
1582 struct buffer_head
*next
= bh
->b_this_page
;
1583 if (buffer_async_write(bh
)) {
1584 clear_buffer_dirty(bh
);
1585 submit_bh(WRITE
, bh
);
1589 } while (bh
!= head
);
1595 * The generic ->writepage function for buffer-backed address_spaces
1597 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1599 struct inode
* const inode
= page
->mapping
->host
;
1600 loff_t i_size
= i_size_read(inode
);
1601 const pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1605 /* Is the page fully inside i_size? */
1606 if (page
->index
< end_index
)
1607 return __btrfs_write_full_page(inode
, page
, wbc
);
1609 /* Is the page fully outside i_size? (truncate in progress) */
1610 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
1611 if (page
->index
>= end_index
+1 || !offset
) {
1613 * The page may have dirty, unmapped buffers. For example,
1614 * they may have been added in ext3_writepage(). Make them
1615 * freeable here, so the page does not leak.
1617 block_invalidatepage(page
, 0);
1619 return 0; /* don't care */
1623 * The page straddles i_size. It must be zeroed out on each and every
1624 * writepage invokation because it may be mmapped. "A file is mapped
1625 * in multiples of the page size. For a file that is not a multiple of
1626 * the page size, the remaining memory is zeroed when mapped, and
1627 * writes to that region are not written out to the file."
1629 kaddr
= kmap_atomic(page
, KM_USER0
);
1630 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1631 flush_dcache_page(page
);
1632 kunmap_atomic(kaddr
, KM_USER0
);
1633 return __btrfs_write_full_page(inode
, page
, wbc
);
1636 static void btrfs_truncate(struct inode
*inode
)
1638 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1640 struct btrfs_trans_handle
*trans
;
1642 if (!S_ISREG(inode
->i_mode
))
1644 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1647 btrfs_truncate_page(inode
->i_mapping
, inode
->i_size
);
1649 mutex_lock(&root
->fs_info
->fs_mutex
);
1650 trans
= btrfs_start_transaction(root
, 1);
1651 btrfs_set_trans_block_group(trans
, inode
);
1653 /* FIXME, add redo link to tree so we don't leak on crash */
1654 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1656 btrfs_update_inode(trans
, root
, inode
);
1657 ret
= btrfs_end_transaction(trans
, root
);
1659 mutex_unlock(&root
->fs_info
->fs_mutex
);
1660 btrfs_btree_balance_dirty(root
);
1663 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1664 unsigned from
, unsigned to
)
1666 struct inode
*inode
= page
->mapping
->host
;
1667 struct buffer_head
*bh
;
1668 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1670 SetPageUptodate(page
);
1671 bh
= page_buffers(page
);
1672 set_buffer_uptodate(bh
);
1673 if (buffer_mapped(bh
) && bh
->b_blocknr
!= 0) {
1674 set_page_dirty(page
);
1676 if (pos
> inode
->i_size
) {
1677 i_size_write(inode
, pos
);
1678 mark_inode_dirty(inode
);
1683 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1684 struct page
**prepared_pages
,
1685 const char __user
* buf
)
1687 long page_fault
= 0;
1689 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1691 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1692 size_t count
= min_t(size_t,
1693 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1694 struct page
*page
= prepared_pages
[i
];
1695 fault_in_pages_readable(buf
, count
);
1697 /* Copy data from userspace to the current page */
1699 page_fault
= __copy_from_user(page_address(page
) + offset
,
1701 /* Flush processor's dcache for this page */
1702 flush_dcache_page(page
);
1705 write_bytes
-= count
;
1710 return page_fault
? -EFAULT
: 0;
1713 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1716 for (i
= 0; i
< num_pages
; i
++) {
1719 unlock_page(pages
[i
]);
1720 mark_page_accessed(pages
[i
]);
1721 page_cache_release(pages
[i
]);
1724 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1725 struct btrfs_root
*root
,
1727 struct page
**pages
,
1737 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1738 struct buffer_head
*bh
;
1739 struct btrfs_file_extent_item
*ei
;
1741 for (i
= 0; i
< num_pages
; i
++) {
1742 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1743 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1744 /* FIXME, one block at a time */
1746 mutex_lock(&root
->fs_info
->fs_mutex
);
1747 trans
= btrfs_start_transaction(root
, 1);
1748 btrfs_set_trans_block_group(trans
, inode
);
1750 bh
= page_buffers(pages
[i
]);
1751 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
1752 struct btrfs_key key
;
1753 struct btrfs_path
*path
;
1757 path
= btrfs_alloc_path();
1759 key
.objectid
= inode
->i_ino
;
1760 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
1762 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
1763 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
1765 btrfs_file_extent_calc_inline_size(write_bytes
);
1766 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1769 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1770 path
->slots
[0], struct btrfs_file_extent_item
);
1771 btrfs_set_file_extent_generation(ei
, trans
->transid
);
1772 btrfs_set_file_extent_type(ei
,
1773 BTRFS_FILE_EXTENT_INLINE
);
1774 ptr
= btrfs_file_extent_inline_start(ei
);
1775 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
1776 ptr
, bh
->b_data
, offset
+ write_bytes
);
1777 mark_buffer_dirty(path
->nodes
[0]);
1778 btrfs_free_path(path
);
1779 } else if (buffer_mapped(bh
)) {
1780 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1781 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1782 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1785 SetPageChecked(pages
[i
]);
1786 // btrfs_update_inode_block_group(trans, inode);
1787 ret
= btrfs_end_transaction(trans
, root
);
1789 mutex_unlock(&root
->fs_info
->fs_mutex
);
1791 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
1792 offset
+ this_write
);
1798 WARN_ON(this_write
> write_bytes
);
1799 write_bytes
-= this_write
;
1805 static int drop_extents(struct btrfs_trans_handle
*trans
,
1806 struct btrfs_root
*root
,
1807 struct inode
*inode
,
1808 u64 start
, u64 end
, u64
*hint_block
)
1811 struct btrfs_key key
;
1812 struct btrfs_leaf
*leaf
;
1814 struct btrfs_file_extent_item
*extent
;
1817 struct btrfs_file_extent_item old
;
1818 struct btrfs_path
*path
;
1819 u64 search_start
= start
;
1825 path
= btrfs_alloc_path();
1829 btrfs_release_path(root
, path
);
1830 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1835 if (path
->slots
[0] == 0) {
1846 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1847 slot
= path
->slots
[0];
1848 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
1849 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
1853 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
1857 extent
= btrfs_item_ptr(leaf
, slot
,
1858 struct btrfs_file_extent_item
);
1859 found_type
= btrfs_file_extent_type(extent
);
1860 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1861 extent_end
= key
.offset
+
1862 (btrfs_file_extent_num_blocks(extent
) <<
1865 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1867 extent_end
= key
.offset
+
1868 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
1871 if (!found_extent
&& !found_inline
) {
1876 if (search_start
>= extent_end
) {
1882 u64 mask
= root
->blocksize
- 1;
1883 search_start
= (extent_end
+ mask
) & ~mask
;
1885 search_start
= extent_end
;
1887 if (end
< extent_end
&& end
>= key
.offset
) {
1890 btrfs_file_extent_disk_blocknr(extent
);
1891 u64 disk_num_blocks
=
1892 btrfs_file_extent_disk_num_blocks(extent
);
1893 memcpy(&old
, extent
, sizeof(old
));
1894 if (disk_blocknr
!= 0) {
1895 ret
= btrfs_inc_extent_ref(trans
, root
,
1896 disk_blocknr
, disk_num_blocks
);
1900 WARN_ON(found_inline
);
1904 if (start
> key
.offset
) {
1907 /* truncate existing extent */
1909 WARN_ON(start
& (root
->blocksize
- 1));
1911 new_num
= (start
- key
.offset
) >>
1913 old_num
= btrfs_file_extent_num_blocks(extent
);
1915 btrfs_file_extent_disk_blocknr(extent
);
1916 if (btrfs_file_extent_disk_blocknr(extent
)) {
1918 (old_num
- new_num
) << 3;
1920 btrfs_set_file_extent_num_blocks(extent
,
1922 mark_buffer_dirty(path
->nodes
[0]);
1928 u64 disk_blocknr
= 0;
1929 u64 disk_num_blocks
= 0;
1930 u64 extent_num_blocks
= 0;
1933 btrfs_file_extent_disk_blocknr(extent
);
1935 btrfs_file_extent_disk_num_blocks(extent
);
1937 btrfs_file_extent_num_blocks(extent
);
1939 btrfs_file_extent_disk_blocknr(extent
);
1941 ret
= btrfs_del_item(trans
, root
, path
);
1943 btrfs_release_path(root
, path
);
1945 if (found_extent
&& disk_blocknr
!= 0) {
1946 inode
->i_blocks
-= extent_num_blocks
<< 3;
1947 ret
= btrfs_free_extent(trans
, root
,
1949 disk_num_blocks
, 0);
1953 if (!bookend
&& search_start
>= end
) {
1960 if (bookend
&& found_extent
) {
1961 /* create bookend */
1962 struct btrfs_key ins
;
1963 ins
.objectid
= inode
->i_ino
;
1966 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
1968 btrfs_release_path(root
, path
);
1969 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1972 extent
= btrfs_item_ptr(
1973 btrfs_buffer_leaf(path
->nodes
[0]),
1975 struct btrfs_file_extent_item
);
1976 btrfs_set_file_extent_disk_blocknr(extent
,
1977 btrfs_file_extent_disk_blocknr(&old
));
1978 btrfs_set_file_extent_disk_num_blocks(extent
,
1979 btrfs_file_extent_disk_num_blocks(&old
));
1981 btrfs_set_file_extent_offset(extent
,
1982 btrfs_file_extent_offset(&old
) +
1983 ((end
- key
.offset
) >> inode
->i_blkbits
));
1984 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
1985 (extent_end
- end
) >> inode
->i_blkbits
);
1986 btrfs_set_file_extent_num_blocks(extent
,
1987 (extent_end
- end
) >> inode
->i_blkbits
);
1989 btrfs_set_file_extent_type(extent
,
1990 BTRFS_FILE_EXTENT_REG
);
1991 btrfs_set_file_extent_generation(extent
,
1992 btrfs_file_extent_generation(&old
));
1993 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1994 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
1996 btrfs_file_extent_num_blocks(extent
) << 3;
2003 btrfs_free_path(path
);
2007 static int prepare_pages(struct btrfs_root
*root
,
2009 struct page
**pages
,
2012 unsigned long first_index
,
2013 unsigned long last_index
,
2015 u64 alloc_extent_start
)
2018 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
2019 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2023 struct buffer_head
*bh
;
2024 struct buffer_head
*head
;
2025 loff_t isize
= i_size_read(inode
);
2027 memset(pages
, 0, num_pages
* sizeof(struct page
*));
2029 for (i
= 0; i
< num_pages
; i
++) {
2030 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
2033 goto failed_release
;
2035 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
2036 wait_on_page_writeback(pages
[i
]);
2037 offset
= pos
& (PAGE_CACHE_SIZE
-1);
2038 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
2039 if (!page_has_buffers(pages
[i
])) {
2040 create_empty_buffers(pages
[i
],
2041 root
->fs_info
->sb
->s_blocksize
,
2042 (1 << BH_Uptodate
));
2044 head
= page_buffers(pages
[i
]);
2047 err
= btrfs_map_bh_to_logical(root
, bh
,
2048 alloc_extent_start
);
2051 goto failed_truncate
;
2052 bh
= bh
->b_this_page
;
2053 if (alloc_extent_start
)
2054 alloc_extent_start
++;
2055 } while (bh
!= head
);
2057 WARN_ON(this_write
> write_bytes
);
2058 write_bytes
-= this_write
;
2063 btrfs_drop_pages(pages
, num_pages
);
2067 btrfs_drop_pages(pages
, num_pages
);
2069 vmtruncate(inode
, isize
);
2073 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
2074 size_t count
, loff_t
*ppos
)
2077 size_t num_written
= 0;
2080 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
2081 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2082 struct page
*pages
[8];
2083 struct page
*pinned
[2];
2084 unsigned long first_index
;
2085 unsigned long last_index
;
2088 u64 alloc_extent_start
;
2090 struct btrfs_trans_handle
*trans
;
2091 struct btrfs_key ins
;
2094 if (file
->f_flags
& O_DIRECT
)
2097 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2098 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
2099 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2104 err
= remove_suid(file
->f_path
.dentry
);
2107 file_update_time(file
);
2109 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
2110 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
2113 mutex_lock(&inode
->i_mutex
);
2114 first_index
= pos
>> PAGE_CACHE_SHIFT
;
2115 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
2117 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
2118 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
2119 if (!PageUptodate(pinned
[0])) {
2120 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
2122 wait_on_page_locked(pinned
[0]);
2124 unlock_page(pinned
[0]);
2127 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
2128 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
2129 if (!PageUptodate(pinned
[1])) {
2130 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
2132 wait_on_page_locked(pinned
[1]);
2134 unlock_page(pinned
[1]);
2138 mutex_lock(&root
->fs_info
->fs_mutex
);
2139 trans
= btrfs_start_transaction(root
, 1);
2142 mutex_unlock(&root
->fs_info
->fs_mutex
);
2145 btrfs_set_trans_block_group(trans
, inode
);
2146 /* FIXME blocksize != 4096 */
2147 inode
->i_blocks
+= num_blocks
<< 3;
2149 if (start_pos
< inode
->i_size
) {
2150 /* FIXME blocksize != pagesize */
2151 ret
= drop_extents(trans
, root
, inode
,
2153 (pos
+ count
+ root
->blocksize
-1) &
2154 ~((u64
)root
->blocksize
- 1), &hint_block
);
2157 if (inode
->i_size
< start_pos
) {
2158 u64 last_pos_in_file
;
2160 u64 mask
= root
->blocksize
- 1;
2161 last_pos_in_file
= (inode
->i_size
+ mask
) & ~mask
;
2162 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
2163 hole_size
>>= inode
->i_blkbits
;
2164 if (last_pos_in_file
< start_pos
) {
2165 ret
= btrfs_insert_file_extent(trans
, root
,
2172 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
2173 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
2174 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
2175 num_blocks
, hint_block
, (u64
)-1,
2178 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
2179 start_pos
, ins
.objectid
, ins
.offset
,
2187 alloc_extent_start
= ins
.objectid
;
2188 // btrfs_update_inode_block_group(trans, inode);
2189 ret
= btrfs_end_transaction(trans
, root
);
2190 mutex_unlock(&root
->fs_info
->fs_mutex
);
2193 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
2194 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
2195 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
2198 memset(pages
, 0, sizeof(pages
));
2199 ret
= prepare_pages(root
, file
, pages
, num_pages
,
2200 pos
, first_index
, last_index
,
2201 write_bytes
, alloc_extent_start
);
2204 /* FIXME blocks != pagesize */
2205 if (alloc_extent_start
)
2206 alloc_extent_start
+= num_pages
;
2207 ret
= btrfs_copy_from_user(pos
, num_pages
,
2208 write_bytes
, pages
, buf
);
2211 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
2212 num_pages
, pos
, write_bytes
);
2214 btrfs_drop_pages(pages
, num_pages
);
2217 count
-= write_bytes
;
2219 num_written
+= write_bytes
;
2221 balance_dirty_pages_ratelimited(inode
->i_mapping
);
2222 btrfs_btree_balance_dirty(root
);
2226 mutex_unlock(&inode
->i_mutex
);
2229 page_cache_release(pinned
[0]);
2231 page_cache_release(pinned
[1]);
2233 current
->backing_dev_info
= NULL
;
2234 mark_inode_dirty(inode
);
2235 return num_written
? num_written
: err
;
2238 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
2239 unsigned long offset
, unsigned long size
)
2242 unsigned long left
, count
= desc
->count
;
2243 struct inode
*inode
= page
->mapping
->host
;
2248 if (!PageChecked(page
)) {
2249 /* FIXME, do it per block */
2250 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2252 struct buffer_head
*bh
;
2254 if (page_has_buffers(page
)) {
2255 bh
= page_buffers(page
);
2256 if (!buffer_mapped(bh
)) {
2257 SetPageChecked(page
);
2262 ret
= btrfs_csum_verify_file_block(root
,
2263 page
->mapping
->host
->i_ino
,
2264 page
->index
<< PAGE_CACHE_SHIFT
,
2265 kmap(page
), PAGE_CACHE_SIZE
);
2267 if (ret
!= -ENOENT
) {
2268 printk("failed to verify ino %lu page %lu ret %d\n",
2269 page
->mapping
->host
->i_ino
,
2271 memset(page_address(page
), 1, PAGE_CACHE_SIZE
);
2272 flush_dcache_page(page
);
2275 SetPageChecked(page
);
2280 * Faults on the destination of a read are common, so do it before
2283 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
2284 kaddr
= kmap_atomic(page
, KM_USER0
);
2285 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
2286 kaddr
+ offset
, size
);
2287 kunmap_atomic(kaddr
, KM_USER0
);
2292 /* Do it the slow way */
2294 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
2299 desc
->error
= -EFAULT
;
2302 desc
->count
= count
- size
;
2303 desc
->written
+= size
;
2304 desc
->arg
.buf
+= size
;
2309 * btrfs_file_aio_read - filesystem read routine
2310 * @iocb: kernel I/O control block
2311 * @iov: io vector request
2312 * @nr_segs: number of segments in the iovec
2313 * @pos: current file position
2315 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2316 unsigned long nr_segs
, loff_t pos
)
2318 struct file
*filp
= iocb
->ki_filp
;
2322 loff_t
*ppos
= &iocb
->ki_pos
;
2325 for (seg
= 0; seg
< nr_segs
; seg
++) {
2326 const struct iovec
*iv
= &iov
[seg
];
2329 * If any segment has a negative length, or the cumulative
2330 * length ever wraps negative then return -EINVAL.
2332 count
+= iv
->iov_len
;
2333 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
2335 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
2340 count
-= iv
->iov_len
; /* This segment is no good */
2345 for (seg
= 0; seg
< nr_segs
; seg
++) {
2346 read_descriptor_t desc
;
2349 desc
.arg
.buf
= iov
[seg
].iov_base
;
2350 desc
.count
= iov
[seg
].iov_len
;
2351 if (desc
.count
== 0)
2354 do_generic_file_read(filp
, ppos
, &desc
,
2356 retval
+= desc
.written
;
2358 retval
= retval
?: desc
.error
;
2366 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
2368 struct btrfs_trans_handle
*trans
;
2369 struct btrfs_key key
;
2370 struct btrfs_root_item root_item
;
2371 struct btrfs_inode_item
*inode_item
;
2372 struct buffer_head
*subvol
;
2373 struct btrfs_leaf
*leaf
;
2374 struct btrfs_root
*new_root
;
2375 struct inode
*inode
;
2379 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2381 mutex_lock(&root
->fs_info
->fs_mutex
);
2382 trans
= btrfs_start_transaction(root
, 1);
2385 subvol
= btrfs_alloc_free_block(trans
, root
, 0);
2388 leaf
= btrfs_buffer_leaf(subvol
);
2389 btrfs_set_header_nritems(&leaf
->header
, 0);
2390 btrfs_set_header_level(&leaf
->header
, 0);
2391 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
2392 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
2393 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
2394 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
2395 sizeof(leaf
->header
.fsid
));
2396 mark_buffer_dirty(subvol
);
2398 inode_item
= &root_item
.inode
;
2399 memset(inode_item
, 0, sizeof(*inode_item
));
2400 btrfs_set_inode_generation(inode_item
, 1);
2401 btrfs_set_inode_size(inode_item
, 3);
2402 btrfs_set_inode_nlink(inode_item
, 1);
2403 btrfs_set_inode_nblocks(inode_item
, 1);
2404 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
2406 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
2407 btrfs_set_root_refs(&root_item
, 1);
2411 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2415 btrfs_set_root_dirid(&root_item
, new_dirid
);
2417 key
.objectid
= objectid
;
2420 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2421 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2426 * insert the directory item
2428 key
.offset
= (u64
)-1;
2429 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2430 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2431 name
, namelen
, dir
->i_ino
, &key
, 0);
2434 ret
= btrfs_commit_transaction(trans
, root
);
2437 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2440 trans
= btrfs_start_transaction(new_root
, 1);
2443 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2444 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2445 inode
->i_op
= &btrfs_dir_inode_operations
;
2446 inode
->i_fop
= &btrfs_dir_file_operations
;
2448 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
2453 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2456 ret
= btrfs_commit_transaction(trans
, new_root
);
2461 mutex_unlock(&root
->fs_info
->fs_mutex
);
2462 btrfs_btree_balance_dirty(root
);
2466 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2468 struct btrfs_trans_handle
*trans
;
2469 struct btrfs_key key
;
2470 struct btrfs_root_item new_root_item
;
2474 if (!root
->ref_cows
)
2477 mutex_lock(&root
->fs_info
->fs_mutex
);
2478 trans
= btrfs_start_transaction(root
, 1);
2481 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2484 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2488 memcpy(&new_root_item
, &root
->root_item
,
2489 sizeof(new_root_item
));
2491 key
.objectid
= objectid
;
2494 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2495 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2497 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2502 * insert the directory item
2504 key
.offset
= (u64
)-1;
2505 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2507 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2512 ret
= btrfs_inc_root_ref(trans
, root
);
2515 ret
= btrfs_commit_transaction(trans
, root
);
2517 mutex_unlock(&root
->fs_info
->fs_mutex
);
2518 btrfs_btree_balance_dirty(root
);
2522 static int add_disk(struct btrfs_root
*root
, char *name
, int namelen
)
2524 struct block_device
*bdev
;
2525 struct btrfs_path
*path
;
2526 struct super_block
*sb
= root
->fs_info
->sb
;
2527 struct btrfs_root
*dev_root
= root
->fs_info
->dev_root
;
2528 struct btrfs_trans_handle
*trans
;
2529 struct btrfs_device_item
*dev_item
;
2530 struct btrfs_key key
;
2537 printk("adding disk %s\n", name
);
2538 path
= btrfs_alloc_path();
2541 num_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
2542 bdev
= open_bdev_excl(name
, O_RDWR
, sb
);
2544 ret
= PTR_ERR(bdev
);
2545 printk("open bdev excl failed ret %d\n", ret
);
2548 set_blocksize(bdev
, sb
->s_blocksize
);
2549 new_blocks
= bdev
->bd_inode
->i_size
>> sb
->s_blocksize_bits
;
2550 key
.objectid
= num_blocks
;
2551 key
.offset
= new_blocks
;
2553 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
2555 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
2556 trans
= btrfs_start_transaction(dev_root
, 1);
2557 item_size
= sizeof(*dev_item
) + namelen
;
2558 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks
, new_blocks
, key
.flags
, item_size
);
2559 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
, &key
, item_size
);
2561 printk("insert failed %d\n", ret
);
2562 close_bdev_excl(bdev
);
2567 dev_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2568 path
->slots
[0], struct btrfs_device_item
);
2569 btrfs_set_device_pathlen(dev_item
, namelen
);
2570 memcpy(dev_item
+ 1, name
, namelen
);
2572 device_id
= btrfs_super_last_device_id(root
->fs_info
->disk_super
) + 1;
2573 btrfs_set_super_last_device_id(root
->fs_info
->disk_super
, device_id
);
2574 btrfs_set_device_id(dev_item
, device_id
);
2575 mark_buffer_dirty(path
->nodes
[0]);
2577 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
, num_blocks
,
2581 btrfs_set_super_total_blocks(root
->fs_info
->disk_super
,
2582 num_blocks
+ new_blocks
);
2583 i_size_write(root
->fs_info
->btree_inode
,
2584 (num_blocks
+ new_blocks
) <<
2585 root
->fs_info
->btree_inode
->i_blkbits
);
2589 ret
= btrfs_commit_transaction(trans
, dev_root
);
2591 mutex_unlock(&root
->fs_info
->fs_mutex
);
2593 btrfs_free_path(path
);
2594 btrfs_btree_balance_dirty(root
);
2599 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2600 cmd
, unsigned long arg
)
2602 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2603 struct btrfs_ioctl_vol_args vol_args
;
2605 struct btrfs_dir_item
*di
;
2607 struct btrfs_path
*path
;
2611 case BTRFS_IOC_SNAP_CREATE
:
2612 if (copy_from_user(&vol_args
,
2613 (struct btrfs_ioctl_vol_args __user
*)arg
,
2616 namelen
= strlen(vol_args
.name
);
2617 if (namelen
> BTRFS_VOL_NAME_MAX
)
2619 path
= btrfs_alloc_path();
2622 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2623 mutex_lock(&root
->fs_info
->fs_mutex
);
2624 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2626 vol_args
.name
, namelen
, 0);
2627 mutex_unlock(&root
->fs_info
->fs_mutex
);
2628 btrfs_free_path(path
);
2629 if (di
&& !IS_ERR(di
))
2632 if (root
== root
->fs_info
->tree_root
)
2633 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2635 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2638 case BTRFS_IOC_ADD_DISK
:
2639 if (copy_from_user(&vol_args
,
2640 (struct btrfs_ioctl_vol_args __user
*)arg
,
2643 namelen
= strlen(vol_args
.name
);
2644 if (namelen
> BTRFS_VOL_NAME_MAX
)
2646 vol_args
.name
[namelen
] = '\0';
2647 ret
= add_disk(root
, vol_args
.name
, namelen
);
2655 static struct kmem_cache
*btrfs_inode_cachep
;
2656 struct kmem_cache
*btrfs_trans_handle_cachep
;
2657 struct kmem_cache
*btrfs_transaction_cachep
;
2658 struct kmem_cache
*btrfs_bit_radix_cachep
;
2659 struct kmem_cache
*btrfs_path_cachep
;
2662 * Called inside transaction, so use GFP_NOFS
2664 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2666 struct btrfs_inode
*ei
;
2668 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2671 return &ei
->vfs_inode
;
2674 static void btrfs_destroy_inode(struct inode
*inode
)
2676 WARN_ON(!list_empty(&inode
->i_dentry
));
2677 WARN_ON(inode
->i_data
.nrpages
);
2679 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2682 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2683 unsigned long flags
)
2685 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2687 if ((flags
& (SLAB_CTOR_CONSTRUCTOR
)) ==
2688 SLAB_CTOR_CONSTRUCTOR
) {
2689 inode_init_once(&ei
->vfs_inode
);
2693 static int init_inodecache(void)
2695 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
2696 sizeof(struct btrfs_inode
),
2697 0, (SLAB_RECLAIM_ACCOUNT
|
2700 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
2701 sizeof(struct btrfs_trans_handle
),
2702 0, (SLAB_RECLAIM_ACCOUNT
|
2705 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
2706 sizeof(struct btrfs_transaction
),
2707 0, (SLAB_RECLAIM_ACCOUNT
|
2710 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
2711 sizeof(struct btrfs_transaction
),
2712 0, (SLAB_RECLAIM_ACCOUNT
|
2715 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
2717 0, (SLAB_RECLAIM_ACCOUNT
|
2719 SLAB_DESTROY_BY_RCU
),
2721 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
2722 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
2727 static void destroy_inodecache(void)
2729 kmem_cache_destroy(btrfs_inode_cachep
);
2730 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2731 kmem_cache_destroy(btrfs_transaction_cachep
);
2732 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2733 kmem_cache_destroy(btrfs_path_cachep
);
2736 static int btrfs_get_sb(struct file_system_type
*fs_type
,
2737 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2739 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
2740 btrfs_fill_super
, mnt
);
2743 static int btrfs_getattr(struct vfsmount
*mnt
,
2744 struct dentry
*dentry
, struct kstat
*stat
)
2746 struct inode
*inode
= dentry
->d_inode
;
2747 generic_fillattr(inode
, stat
);
2748 stat
->blksize
= 256 * 1024;
2752 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2754 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
2755 struct btrfs_super_block
*disk_super
= root
->fs_info
->disk_super
;
2757 buf
->f_namelen
= BTRFS_NAME_LEN
;
2758 buf
->f_blocks
= btrfs_super_total_blocks(disk_super
);
2759 buf
->f_bfree
= buf
->f_blocks
- btrfs_super_blocks_used(disk_super
);
2760 buf
->f_bavail
= buf
->f_bfree
;
2761 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2762 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2766 static int btrfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
,
2767 struct inode
* new_dir
,struct dentry
*new_dentry
)
2769 struct btrfs_trans_handle
*trans
;
2770 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
2771 struct inode
*new_inode
= new_dentry
->d_inode
;
2772 struct inode
*old_inode
= old_dentry
->d_inode
;
2773 struct timespec ctime
= CURRENT_TIME
;
2774 struct btrfs_path
*path
;
2775 struct btrfs_dir_item
*di
;
2778 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
2779 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
) {
2782 mutex_lock(&root
->fs_info
->fs_mutex
);
2783 trans
= btrfs_start_transaction(root
, 1);
2784 btrfs_set_trans_block_group(trans
, new_dir
);
2785 path
= btrfs_alloc_path();
2791 old_dentry
->d_inode
->i_nlink
++;
2792 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
2793 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
2794 old_inode
->i_ctime
= ctime
;
2795 if (S_ISDIR(old_inode
->i_mode
) && old_dir
!= new_dir
) {
2796 struct btrfs_key
*location
= &BTRFS_I(new_dir
)->location
;
2798 di
= btrfs_lookup_dir_item(trans
, root
, path
, old_inode
->i_ino
,
2808 old_parent_oid
= btrfs_disk_key_objectid(&di
->location
);
2809 ret
= btrfs_del_item(trans
, root
, path
);
2814 btrfs_release_path(root
, path
);
2816 di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
2828 ret
= btrfs_del_item(trans
, root
, path
);
2833 btrfs_release_path(root
, path
);
2835 ret
= btrfs_insert_dir_item(trans
, root
, "..", 2,
2836 old_inode
->i_ino
, location
, 0);
2842 ret
= btrfs_unlink_trans(trans
, root
, old_dir
, old_dentry
);
2847 new_inode
->i_ctime
= CURRENT_TIME
;
2848 ret
= btrfs_unlink_trans(trans
, root
, new_dir
, new_dentry
);
2851 if (S_ISDIR(new_inode
->i_mode
))
2852 clear_nlink(new_inode
);
2854 drop_nlink(new_inode
);
2855 btrfs_update_inode(trans
, root
, new_inode
);
2857 ret
= btrfs_add_link(trans
, new_dentry
, old_inode
);
2862 btrfs_free_path(path
);
2863 btrfs_end_transaction(trans
, root
);
2864 mutex_unlock(&root
->fs_info
->fs_mutex
);
2868 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
2869 const char *symname
)
2871 struct btrfs_trans_handle
*trans
;
2872 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2873 struct btrfs_path
*path
;
2874 struct btrfs_key key
;
2875 struct inode
*inode
;
2882 struct btrfs_file_extent_item
*ei
;
2884 name_len
= strlen(symname
) + 1;
2885 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
2886 return -ENAMETOOLONG
;
2887 mutex_lock(&root
->fs_info
->fs_mutex
);
2888 trans
= btrfs_start_transaction(root
, 1);
2889 btrfs_set_trans_block_group(trans
, dir
);
2891 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
2897 inode
= btrfs_new_inode(trans
, root
, objectid
,
2898 BTRFS_I(dir
)->block_group
, S_IFLNK
|S_IRWXUGO
);
2899 err
= PTR_ERR(inode
);
2903 btrfs_set_trans_block_group(trans
, inode
);
2904 err
= btrfs_add_nondir(trans
, dentry
, inode
);
2908 inode
->i_mapping
->a_ops
= &btrfs_aops
;
2909 inode
->i_fop
= &btrfs_file_operations
;
2910 inode
->i_op
= &btrfs_file_inode_operations
;
2912 dir
->i_sb
->s_dirt
= 1;
2913 btrfs_update_inode_block_group(trans
, inode
);
2914 btrfs_update_inode_block_group(trans
, dir
);
2918 path
= btrfs_alloc_path();
2920 key
.objectid
= inode
->i_ino
;
2923 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
2924 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
2925 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2928 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2929 path
->slots
[0], struct btrfs_file_extent_item
);
2930 btrfs_set_file_extent_generation(ei
, trans
->transid
);
2931 btrfs_set_file_extent_type(ei
,
2932 BTRFS_FILE_EXTENT_INLINE
);
2933 ptr
= btrfs_file_extent_inline_start(ei
);
2934 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
2935 ptr
, symname
, name_len
);
2936 mark_buffer_dirty(path
->nodes
[0]);
2937 btrfs_free_path(path
);
2938 inode
->i_op
= &btrfs_symlink_inode_operations
;
2939 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
2940 inode
->i_size
= name_len
- 1;
2941 btrfs_update_inode(trans
, root
, inode
);
2945 btrfs_end_transaction(trans
, root
);
2946 mutex_unlock(&root
->fs_info
->fs_mutex
);
2949 inode_dec_link_count(inode
);
2952 btrfs_btree_balance_dirty(root
);
2956 static struct file_system_type btrfs_fs_type
= {
2957 .owner
= THIS_MODULE
,
2959 .get_sb
= btrfs_get_sb
,
2960 .kill_sb
= kill_block_super
,
2961 .fs_flags
= FS_REQUIRES_DEV
,
2964 static struct super_operations btrfs_super_ops
= {
2965 .delete_inode
= btrfs_delete_inode
,
2966 .put_super
= btrfs_put_super
,
2967 .read_inode
= btrfs_read_locked_inode
,
2968 .write_super
= btrfs_write_super
,
2969 .sync_fs
= btrfs_sync_fs
,
2970 .write_inode
= btrfs_write_inode
,
2971 .dirty_inode
= btrfs_dirty_inode
,
2972 .alloc_inode
= btrfs_alloc_inode
,
2973 .destroy_inode
= btrfs_destroy_inode
,
2974 .statfs
= btrfs_statfs
,
2977 static struct inode_operations btrfs_dir_inode_operations
= {
2978 .lookup
= btrfs_lookup
,
2979 .create
= btrfs_create
,
2980 .unlink
= btrfs_unlink
,
2982 .mkdir
= btrfs_mkdir
,
2983 .rmdir
= btrfs_rmdir
,
2984 .rename
= btrfs_rename
,
2985 .symlink
= btrfs_symlink
,
2986 .setattr
= btrfs_setattr
,
2989 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2990 .lookup
= btrfs_lookup
,
2993 static struct file_operations btrfs_dir_file_operations
= {
2994 .llseek
= generic_file_llseek
,
2995 .read
= generic_read_dir
,
2996 .readdir
= btrfs_readdir
,
2997 .ioctl
= btrfs_ioctl
,
3000 static struct address_space_operations btrfs_aops
= {
3001 .readpage
= btrfs_readpage
,
3002 .writepage
= btrfs_writepage
,
3003 .sync_page
= block_sync_page
,
3004 .prepare_write
= btrfs_prepare_write
,
3005 .commit_write
= btrfs_commit_write
,
3008 static struct address_space_operations btrfs_symlink_aops
= {
3009 .readpage
= btrfs_readpage
,
3010 .writepage
= btrfs_writepage
,
3013 static struct inode_operations btrfs_file_inode_operations
= {
3014 .truncate
= btrfs_truncate
,
3015 .getattr
= btrfs_getattr
,
3016 .setattr
= btrfs_setattr
,
3019 static struct file_operations btrfs_file_operations
= {
3020 .llseek
= generic_file_llseek
,
3021 .read
= do_sync_read
,
3022 .aio_read
= btrfs_file_aio_read
,
3023 .write
= btrfs_file_write
,
3024 .mmap
= generic_file_mmap
,
3025 .open
= generic_file_open
,
3026 .ioctl
= btrfs_ioctl
,
3027 .fsync
= btrfs_sync_file
,
3030 static struct inode_operations btrfs_symlink_inode_operations
= {
3031 .readlink
= generic_readlink
,
3032 .follow_link
= page_follow_link_light
,
3033 .put_link
= page_put_link
,
3036 static int __init
init_btrfs_fs(void)
3039 printk("btrfs loaded!\n");
3040 err
= init_inodecache();
3043 return register_filesystem(&btrfs_fs_type
);
3044 destroy_inodecache();
3048 static void __exit
exit_btrfs_fs(void)
3050 destroy_inodecache();
3051 unregister_filesystem(&btrfs_fs_type
);
3052 printk("btrfs unloaded\n");
3055 module_init(init_btrfs_fs
)
3056 module_exit(exit_btrfs_fs
)
3058 MODULE_LICENSE("GPL");