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"
21 static void btrfs_fsinfo_release(struct kobject
*obj
)
23 struct btrfs_fs_info
*fsinfo
= container_of(obj
,
24 struct btrfs_fs_info
, kobj
);
28 static struct kobj_type btrfs_fsinfo_ktype
= {
29 .release
= btrfs_fsinfo_release
,
32 struct btrfs_iget_args
{
34 struct btrfs_root
*root
;
37 decl_subsys(btrfs
, &btrfs_fsinfo_ktype
, NULL
);
39 #define BTRFS_SUPER_MAGIC 0x9123682E
41 static struct inode_operations btrfs_dir_inode_operations
;
42 static struct inode_operations btrfs_dir_ro_inode_operations
;
43 static struct super_operations btrfs_super_ops
;
44 static struct file_operations btrfs_dir_file_operations
;
45 static struct inode_operations btrfs_file_inode_operations
;
46 static struct address_space_operations btrfs_aops
;
47 static struct file_operations btrfs_file_operations
;
49 static void btrfs_read_locked_inode(struct inode
*inode
)
51 struct btrfs_path
*path
;
52 struct btrfs_inode_item
*inode_item
;
53 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
54 struct btrfs_key location
;
55 struct btrfs_block_group_cache
*alloc_group
;
56 u64 alloc_group_block
;
59 path
= btrfs_alloc_path();
61 btrfs_init_path(path
);
62 mutex_lock(&root
->fs_info
->fs_mutex
);
64 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
65 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
67 btrfs_free_path(path
);
70 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
72 struct btrfs_inode_item
);
74 inode
->i_mode
= btrfs_inode_mode(inode_item
);
75 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
76 inode
->i_uid
= btrfs_inode_uid(inode_item
);
77 inode
->i_gid
= btrfs_inode_gid(inode_item
);
78 inode
->i_size
= btrfs_inode_size(inode_item
);
79 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
80 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
81 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
82 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
83 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
84 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
85 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
86 inode
->i_generation
= btrfs_inode_generation(inode_item
);
87 alloc_group_block
= btrfs_inode_block_group(inode_item
);
88 ret
= radix_tree_gang_lookup(&root
->fs_info
->block_group_radix
,
89 (void **)&alloc_group
,
90 alloc_group_block
, 1);
92 BTRFS_I(inode
)->block_group
= alloc_group
;
94 btrfs_free_path(path
);
97 mutex_unlock(&root
->fs_info
->fs_mutex
);
99 switch (inode
->i_mode
& S_IFMT
) {
102 init_special_inode(inode
, inode
->i_mode
,
103 btrfs_inode_rdev(inode_item
));
107 inode
->i_mapping
->a_ops
= &btrfs_aops
;
108 inode
->i_fop
= &btrfs_file_operations
;
109 inode
->i_op
= &btrfs_file_inode_operations
;
112 inode
->i_fop
= &btrfs_dir_file_operations
;
113 if (root
== root
->fs_info
->tree_root
)
114 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
116 inode
->i_op
= &btrfs_dir_inode_operations
;
119 // inode->i_op = &page_symlink_inode_operations;
125 btrfs_release_path(root
, path
);
126 btrfs_free_path(path
);
127 mutex_unlock(&root
->fs_info
->fs_mutex
);
128 make_bad_inode(inode
);
131 static void fill_inode_item(struct btrfs_inode_item
*item
,
134 btrfs_set_inode_uid(item
, inode
->i_uid
);
135 btrfs_set_inode_gid(item
, inode
->i_gid
);
136 btrfs_set_inode_size(item
, inode
->i_size
);
137 btrfs_set_inode_mode(item
, inode
->i_mode
);
138 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
139 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
140 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
141 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
142 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
143 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
144 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
145 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
146 btrfs_set_inode_generation(item
, inode
->i_generation
);
147 btrfs_set_inode_block_group(item
,
148 BTRFS_I(inode
)->block_group
->key
.objectid
);
151 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
152 struct btrfs_root
*root
,
155 struct btrfs_inode_item
*inode_item
;
156 struct btrfs_path
*path
;
159 path
= btrfs_alloc_path();
161 btrfs_init_path(path
);
162 ret
= btrfs_lookup_inode(trans
, root
, path
,
163 &BTRFS_I(inode
)->location
, 1);
170 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
172 struct btrfs_inode_item
);
174 fill_inode_item(inode_item
, inode
);
175 btrfs_mark_buffer_dirty(path
->nodes
[0]);
178 btrfs_release_path(root
, path
);
179 btrfs_free_path(path
);
184 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
185 struct btrfs_root
*root
,
187 struct dentry
*dentry
)
189 struct btrfs_path
*path
;
190 const char *name
= dentry
->d_name
.name
;
191 int name_len
= dentry
->d_name
.len
;
194 struct btrfs_dir_item
*di
;
196 path
= btrfs_alloc_path();
198 btrfs_init_path(path
);
199 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
209 objectid
= btrfs_disk_key_objectid(&di
->location
);
210 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
212 btrfs_release_path(root
, path
);
214 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
215 objectid
, name
, name_len
, -1);
224 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
227 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
229 btrfs_free_path(path
);
231 dir
->i_size
-= name_len
* 2;
232 btrfs_update_inode(trans
, root
, dir
);
233 drop_nlink(dentry
->d_inode
);
234 btrfs_update_inode(trans
, root
, dentry
->d_inode
);
235 dir
->i_sb
->s_dirt
= 1;
240 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
242 struct btrfs_root
*root
;
243 struct btrfs_trans_handle
*trans
;
246 root
= BTRFS_I(dir
)->root
;
247 mutex_lock(&root
->fs_info
->fs_mutex
);
248 trans
= btrfs_start_transaction(root
, 1);
249 btrfs_set_trans_block_group(trans
, dir
);
250 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
251 btrfs_end_transaction(trans
, root
);
252 mutex_unlock(&root
->fs_info
->fs_mutex
);
253 btrfs_btree_balance_dirty(root
);
257 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
259 struct inode
*inode
= dentry
->d_inode
;
262 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
263 struct btrfs_path
*path
;
264 struct btrfs_key key
;
265 struct btrfs_trans_handle
*trans
;
266 struct btrfs_key found_key
;
268 struct btrfs_leaf
*leaf
;
269 char *goodnames
= "..";
271 path
= btrfs_alloc_path();
273 btrfs_init_path(path
);
274 mutex_lock(&root
->fs_info
->fs_mutex
);
275 trans
= btrfs_start_transaction(root
, 1);
276 btrfs_set_trans_block_group(trans
, dir
);
277 key
.objectid
= inode
->i_ino
;
278 key
.offset
= (u64
)-1;
281 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
287 if (path
->slots
[0] == 0) {
292 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
293 btrfs_disk_key_to_cpu(&found_key
,
294 &leaf
->items
[path
->slots
[0]].key
);
295 found_type
= btrfs_key_type(&found_key
);
296 if (found_key
.objectid
!= inode
->i_ino
) {
300 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
301 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
302 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
303 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
307 ret
= btrfs_del_item(trans
, root
, path
);
310 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
312 btrfs_release_path(root
, path
);
315 btrfs_release_path(root
, path
);
317 /* now the directory is empty */
318 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
323 btrfs_release_path(root
, path
);
324 btrfs_free_path(path
);
325 mutex_unlock(&root
->fs_info
->fs_mutex
);
326 ret
= btrfs_end_transaction(trans
, root
);
327 btrfs_btree_balance_dirty(root
);
333 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
334 struct btrfs_root
*root
,
337 struct btrfs_path
*path
;
342 path
= btrfs_alloc_path();
344 btrfs_init_path(path
);
345 ret
= btrfs_lookup_inode(trans
, root
, path
,
346 &BTRFS_I(inode
)->location
, -1);
348 ret
= btrfs_del_item(trans
, root
, path
);
350 btrfs_free_path(path
);
354 static void reada_truncate(struct btrfs_root
*root
, struct btrfs_path
*path
,
357 struct btrfs_node
*node
;
367 node
= btrfs_buffer_node(path
->nodes
[1]);
368 slot
= path
->slots
[1];
371 nritems
= btrfs_header_nritems(&node
->header
);
372 for (i
= slot
- 1; i
>= 0; i
--) {
373 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
374 if (item_objectid
!= objectid
)
376 blocknr
= btrfs_node_blockptr(node
, i
);
377 ret
= readahead_tree_block(root
, blocknr
);
383 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
384 struct btrfs_root
*root
,
388 struct btrfs_path
*path
;
389 struct btrfs_key key
;
390 struct btrfs_disk_key
*found_key
;
391 struct btrfs_leaf
*leaf
;
392 struct btrfs_file_extent_item
*fi
= NULL
;
393 u64 extent_start
= 0;
394 u64 extent_num_blocks
= 0;
397 path
= btrfs_alloc_path();
399 /* FIXME, add redo link to tree so we don't leak on crash */
400 key
.objectid
= inode
->i_ino
;
401 key
.offset
= (u64
)-1;
404 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
407 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
409 btrfs_init_path(path
);
410 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
415 BUG_ON(path
->slots
[0] == 0);
418 reada_truncate(root
, path
, inode
->i_ino
);
419 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
420 found_key
= &leaf
->items
[path
->slots
[0]].key
;
421 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
423 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
424 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
426 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
429 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
430 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
432 struct btrfs_file_extent_item
);
433 if (btrfs_file_extent_type(fi
) !=
434 BTRFS_FILE_EXTENT_INLINE
) {
436 btrfs_file_extent_disk_blocknr(fi
);
438 btrfs_file_extent_disk_num_blocks(fi
);
439 /* FIXME blocksize != 4096 */
441 btrfs_file_extent_num_blocks(fi
) << 3;
445 ret
= btrfs_del_item(trans
, root
, path
);
447 btrfs_release_path(root
, path
);
449 ret
= btrfs_free_extent(trans
, root
, extent_start
,
450 extent_num_blocks
, 0);
456 btrfs_release_path(root
, path
);
457 btrfs_free_path(path
);
458 inode
->i_sb
->s_dirt
= 1;
462 static void btrfs_delete_inode(struct inode
*inode
)
464 struct btrfs_trans_handle
*trans
;
465 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
468 truncate_inode_pages(&inode
->i_data
, 0);
469 if (is_bad_inode(inode
)) {
473 mutex_lock(&root
->fs_info
->fs_mutex
);
474 trans
= btrfs_start_transaction(root
, 1);
475 btrfs_set_trans_block_group(trans
, inode
);
476 if (S_ISREG(inode
->i_mode
)) {
477 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
480 btrfs_free_inode(trans
, root
, inode
);
481 btrfs_end_transaction(trans
, root
);
482 mutex_unlock(&root
->fs_info
->fs_mutex
);
483 btrfs_btree_balance_dirty(root
);
489 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
490 struct btrfs_key
*location
)
492 const char *name
= dentry
->d_name
.name
;
493 int namelen
= dentry
->d_name
.len
;
494 struct btrfs_dir_item
*di
;
495 struct btrfs_path
*path
;
496 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
499 path
= btrfs_alloc_path();
501 btrfs_init_path(path
);
502 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
504 if (!di
|| IS_ERR(di
)) {
505 location
->objectid
= 0;
509 btrfs_disk_key_to_cpu(location
, &di
->location
);
511 btrfs_release_path(root
, path
);
512 btrfs_free_path(path
);
516 static int fixup_tree_root_location(struct btrfs_root
*root
,
517 struct btrfs_key
*location
,
518 struct btrfs_root
**sub_root
)
520 struct btrfs_path
*path
;
521 struct btrfs_root_item
*ri
;
523 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
525 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
528 path
= btrfs_alloc_path();
530 mutex_lock(&root
->fs_info
->fs_mutex
);
532 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
533 if (IS_ERR(*sub_root
))
534 return PTR_ERR(*sub_root
);
536 ri
= &(*sub_root
)->root_item
;
537 location
->objectid
= btrfs_root_dirid(ri
);
539 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
540 location
->offset
= 0;
542 btrfs_free_path(path
);
543 mutex_unlock(&root
->fs_info
->fs_mutex
);
547 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
549 struct btrfs_iget_args
*args
= p
;
550 inode
->i_ino
= args
->ino
;
551 BTRFS_I(inode
)->root
= args
->root
;
555 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
557 struct btrfs_iget_args
*args
= opaque
;
558 return (args
->ino
== inode
->i_ino
&&
559 args
->root
== BTRFS_I(inode
)->root
);
562 static struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
563 struct btrfs_root
*root
)
566 struct btrfs_iget_args args
;
570 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
571 btrfs_init_locked_inode
,
576 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
577 struct nameidata
*nd
)
579 struct inode
* inode
;
580 struct btrfs_inode
*bi
= BTRFS_I(dir
);
581 struct btrfs_root
*root
= bi
->root
;
582 struct btrfs_root
*sub_root
= root
;
583 struct btrfs_key location
;
586 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
587 return ERR_PTR(-ENAMETOOLONG
);
588 mutex_lock(&root
->fs_info
->fs_mutex
);
589 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
590 mutex_unlock(&root
->fs_info
->fs_mutex
);
594 if (location
.objectid
) {
595 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
599 return ERR_PTR(-ENOENT
);
600 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
603 return ERR_PTR(-EACCES
);
604 if (inode
->i_state
& I_NEW
) {
605 if (sub_root
!= root
) {
606 printk("adding new root for inode %lu root %p (found %p)\n", inode
->i_ino
, sub_root
, BTRFS_I(inode
)->root
);
608 sub_root
->inode
= inode
;
610 BTRFS_I(inode
)->root
= sub_root
;
611 memcpy(&BTRFS_I(inode
)->location
, &location
,
613 btrfs_read_locked_inode(inode
);
614 unlock_new_inode(inode
);
617 return d_splice_alias(inode
, dentry
);
620 static void reada_leaves(struct btrfs_root
*root
, struct btrfs_path
*path
,
623 struct btrfs_node
*node
;
633 node
= btrfs_buffer_node(path
->nodes
[1]);
634 slot
= path
->slots
[1];
635 nritems
= btrfs_header_nritems(&node
->header
);
636 for (i
= slot
+ 1; i
< nritems
; i
++) {
637 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
638 if (item_objectid
!= objectid
)
640 blocknr
= btrfs_node_blockptr(node
, i
);
641 ret
= readahead_tree_block(root
, blocknr
);
647 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
649 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
650 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
651 struct btrfs_item
*item
;
652 struct btrfs_dir_item
*di
;
653 struct btrfs_key key
;
654 struct btrfs_path
*path
;
657 struct btrfs_leaf
*leaf
;
660 unsigned char d_type
= DT_UNKNOWN
;
665 int key_type
= BTRFS_DIR_INDEX_KEY
;
667 /* FIXME, use a real flag for deciding about the key type */
668 if (root
->fs_info
->tree_root
== root
)
669 key_type
= BTRFS_DIR_ITEM_KEY
;
670 mutex_lock(&root
->fs_info
->fs_mutex
);
671 key
.objectid
= inode
->i_ino
;
673 btrfs_set_key_type(&key
, key_type
);
674 key
.offset
= filp
->f_pos
;
675 path
= btrfs_alloc_path();
676 btrfs_init_path(path
);
677 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
681 reada_leaves(root
, path
, inode
->i_ino
);
683 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
684 nritems
= btrfs_header_nritems(&leaf
->header
);
685 slot
= path
->slots
[0];
686 if (advance
|| slot
>= nritems
) {
687 if (slot
>= nritems
-1) {
688 reada_leaves(root
, path
, inode
->i_ino
);
689 ret
= btrfs_next_leaf(root
, path
);
692 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
693 nritems
= btrfs_header_nritems(&leaf
->header
);
694 slot
= path
->slots
[0];
701 item
= leaf
->items
+ slot
;
702 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
704 if (btrfs_disk_key_type(&item
->key
) != key_type
)
706 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
708 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
710 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
712 di_total
= btrfs_item_size(leaf
->items
+ slot
);
713 while(di_cur
< di_total
) {
714 over
= filldir(dirent
, (const char *)(di
+ 1),
715 btrfs_dir_name_len(di
),
716 btrfs_disk_key_offset(&item
->key
),
717 btrfs_disk_key_objectid(&di
->location
),
721 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
723 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
730 btrfs_release_path(root
, path
);
731 btrfs_free_path(path
);
732 mutex_unlock(&root
->fs_info
->fs_mutex
);
736 static void btrfs_put_super (struct super_block
* sb
)
738 struct btrfs_root
*root
= btrfs_sb(sb
);
741 ret
= close_ctree(root
);
743 printk("close ctree returns %d\n", ret
);
745 sb
->s_fs_info
= NULL
;
748 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
750 struct inode
* inode
;
751 struct dentry
* root_dentry
;
752 struct btrfs_super_block
*disk_super
;
753 struct btrfs_root
*tree_root
;
754 struct btrfs_inode
*bi
;
756 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
757 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
758 sb
->s_op
= &btrfs_super_ops
;
761 tree_root
= open_ctree(sb
);
764 printk("btrfs: open_ctree failed\n");
767 sb
->s_fs_info
= tree_root
;
768 disk_super
= tree_root
->fs_info
->disk_super
;
769 printk("read in super total blocks %Lu root %Lu\n",
770 btrfs_super_total_blocks(disk_super
),
771 btrfs_super_root_dir(disk_super
));
773 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
776 bi
->location
.objectid
= inode
->i_ino
;
777 bi
->location
.offset
= 0;
778 bi
->location
.flags
= 0;
779 bi
->root
= tree_root
;
780 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
784 if (inode
->i_state
& I_NEW
) {
785 btrfs_read_locked_inode(inode
);
786 unlock_new_inode(inode
);
789 root_dentry
= d_alloc_root(inode
);
794 sb
->s_root
= root_dentry
;
799 static int btrfs_write_inode(struct inode
*inode
, int wait
)
801 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
802 struct btrfs_trans_handle
*trans
;
806 mutex_lock(&root
->fs_info
->fs_mutex
);
807 trans
= btrfs_start_transaction(root
, 1);
808 btrfs_set_trans_block_group(trans
, inode
);
809 ret
= btrfs_commit_transaction(trans
, root
);
810 mutex_unlock(&root
->fs_info
->fs_mutex
);
815 static void btrfs_dirty_inode(struct inode
*inode
)
817 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
818 struct btrfs_trans_handle
*trans
;
820 mutex_lock(&root
->fs_info
->fs_mutex
);
821 trans
= btrfs_start_transaction(root
, 1);
822 btrfs_set_trans_block_group(trans
, inode
);
823 btrfs_update_inode(trans
, root
, inode
);
824 btrfs_end_transaction(trans
, root
);
825 mutex_unlock(&root
->fs_info
->fs_mutex
);
826 btrfs_btree_balance_dirty(root
);
829 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
830 struct btrfs_root
*root
,
832 struct btrfs_block_group_cache
*group
,
836 struct btrfs_inode_item inode_item
;
837 struct btrfs_key
*location
;
841 inode
= new_inode(root
->fs_info
->sb
);
843 return ERR_PTR(-ENOMEM
);
845 BTRFS_I(inode
)->root
= root
;
850 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
851 BTRFS_I(inode
)->block_group
= group
;
853 inode
->i_uid
= current
->fsuid
;
854 inode
->i_gid
= current
->fsgid
;
855 inode
->i_mode
= mode
;
856 inode
->i_ino
= objectid
;
858 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
859 fill_inode_item(&inode_item
, inode
);
860 location
= &BTRFS_I(inode
)->location
;
861 location
->objectid
= objectid
;
863 location
->offset
= 0;
864 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
866 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
869 insert_inode_hash(inode
);
873 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
874 struct dentry
*dentry
, struct inode
*inode
)
877 struct btrfs_key key
;
878 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
879 key
.objectid
= inode
->i_ino
;
881 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
884 ret
= btrfs_insert_dir_item(trans
, root
,
885 dentry
->d_name
.name
, dentry
->d_name
.len
,
886 dentry
->d_parent
->d_inode
->i_ino
,
889 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
890 ret
= btrfs_update_inode(trans
, root
,
891 dentry
->d_parent
->d_inode
);
896 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
897 struct dentry
*dentry
, struct inode
*inode
)
899 int err
= btrfs_add_link(trans
, dentry
, inode
);
901 d_instantiate(dentry
, inode
);
909 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
910 int mode
, struct nameidata
*nd
)
912 struct btrfs_trans_handle
*trans
;
913 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
919 mutex_lock(&root
->fs_info
->fs_mutex
);
920 trans
= btrfs_start_transaction(root
, 1);
921 btrfs_set_trans_block_group(trans
, dir
);
923 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
929 inode
= btrfs_new_inode(trans
, root
, objectid
,
930 BTRFS_I(dir
)->block_group
, mode
);
931 err
= PTR_ERR(inode
);
935 btrfs_set_trans_block_group(trans
, inode
);
936 err
= btrfs_add_nondir(trans
, dentry
, inode
);
940 inode
->i_mapping
->a_ops
= &btrfs_aops
;
941 inode
->i_fop
= &btrfs_file_operations
;
942 inode
->i_op
= &btrfs_file_inode_operations
;
944 dir
->i_sb
->s_dirt
= 1;
945 btrfs_update_inode_block_group(trans
, inode
);
946 btrfs_update_inode_block_group(trans
, dir
);
948 btrfs_end_transaction(trans
, root
);
949 mutex_unlock(&root
->fs_info
->fs_mutex
);
952 inode_dec_link_count(inode
);
955 btrfs_btree_balance_dirty(root
);
959 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
960 struct btrfs_root
*root
,
961 u64 objectid
, u64 dirid
)
965 struct btrfs_key key
;
970 key
.objectid
= objectid
;
973 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
975 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
979 key
.objectid
= dirid
;
980 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
988 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
991 struct btrfs_trans_handle
*trans
;
992 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
997 mutex_lock(&root
->fs_info
->fs_mutex
);
998 trans
= btrfs_start_transaction(root
, 1);
999 btrfs_set_trans_block_group(trans
, dir
);
1000 if (IS_ERR(trans
)) {
1001 err
= PTR_ERR(trans
);
1005 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
1011 inode
= btrfs_new_inode(trans
, root
, objectid
,
1012 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1013 if (IS_ERR(inode
)) {
1014 err
= PTR_ERR(inode
);
1018 inode
->i_op
= &btrfs_dir_inode_operations
;
1019 inode
->i_fop
= &btrfs_dir_file_operations
;
1020 btrfs_set_trans_block_group(trans
, inode
);
1022 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1027 err
= btrfs_update_inode(trans
, root
, inode
);
1030 err
= btrfs_add_link(trans
, dentry
, inode
);
1033 d_instantiate(dentry
, inode
);
1035 dir
->i_sb
->s_dirt
= 1;
1036 btrfs_update_inode_block_group(trans
, inode
);
1037 btrfs_update_inode_block_group(trans
, dir
);
1040 btrfs_end_transaction(trans
, root
);
1042 mutex_unlock(&root
->fs_info
->fs_mutex
);
1045 btrfs_btree_balance_dirty(root
);
1049 static int btrfs_sync_file(struct file
*file
,
1050 struct dentry
*dentry
, int datasync
)
1052 struct inode
*inode
= dentry
->d_inode
;
1053 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1055 struct btrfs_trans_handle
*trans
;
1057 mutex_lock(&root
->fs_info
->fs_mutex
);
1058 trans
= btrfs_start_transaction(root
, 1);
1063 ret
= btrfs_commit_transaction(trans
, root
);
1064 mutex_unlock(&root
->fs_info
->fs_mutex
);
1066 return ret
> 0 ? EIO
: ret
;
1069 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
1071 struct btrfs_trans_handle
*trans
;
1072 struct btrfs_root
*root
;
1074 root
= btrfs_sb(sb
);
1078 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
1081 mutex_lock(&root
->fs_info
->fs_mutex
);
1082 trans
= btrfs_start_transaction(root
, 1);
1083 ret
= btrfs_commit_transaction(trans
, root
);
1086 printk("btrfs sync_fs\n");
1087 mutex_unlock(&root
->fs_info
->fs_mutex
);
1091 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1092 struct buffer_head
*result
, int create
)
1097 u64 extent_start
= 0;
1099 u64 objectid
= inode
->i_ino
;
1101 struct btrfs_path
*path
;
1102 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1103 struct btrfs_file_extent_item
*item
;
1104 struct btrfs_leaf
*leaf
;
1105 struct btrfs_disk_key
*found_key
;
1107 path
= btrfs_alloc_path();
1109 btrfs_init_path(path
);
1114 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1116 iblock
<< inode
->i_blkbits
, 0);
1123 if (path
->slots
[0] == 0) {
1124 btrfs_release_path(root
, path
);
1130 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1131 struct btrfs_file_extent_item
);
1132 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1133 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1134 blocknr
+= btrfs_file_extent_offset(item
);
1136 /* are we inside the extent that was found? */
1137 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1138 found_type
= btrfs_disk_key_type(found_key
);
1139 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1140 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1145 found_type
= btrfs_file_extent_type(item
);
1146 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1147 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1148 extent_start
= extent_start
>> inode
->i_blkbits
;
1149 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1150 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1152 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1153 iblock
- extent_start
);
1156 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1160 size
= btrfs_file_extent_inline_len(leaf
->items
+
1162 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1163 extent_start
>>= inode
->i_blkbits
;
1164 if (iblock
< extent_start
|| iblock
> extent_end
) {
1167 ptr
= btrfs_file_extent_inline_start(item
);
1168 map
= kmap(result
->b_page
);
1169 memcpy(map
, ptr
, size
);
1170 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1171 flush_dcache_page(result
->b_page
);
1172 kunmap(result
->b_page
);
1173 set_buffer_uptodate(result
);
1174 SetPageChecked(result
->b_page
);
1175 btrfs_map_bh_to_logical(root
, result
, 0);
1178 btrfs_free_path(path
);
1182 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1183 struct buffer_head
*result
, int create
)
1186 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1187 mutex_lock(&root
->fs_info
->fs_mutex
);
1188 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1189 mutex_unlock(&root
->fs_info
->fs_mutex
);
1193 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1194 unsigned from
, unsigned to
)
1196 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
1199 static void btrfs_write_super(struct super_block
*sb
)
1201 btrfs_sync_fs(sb
, 1);
1204 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1206 return mpage_readpage(page
, btrfs_get_block
);
1210 * While block_write_full_page is writing back the dirty buffers under
1211 * the page lock, whoever dirtied the buffers may decide to clean them
1212 * again at any time. We handle that by only looking at the buffer
1213 * state inside lock_buffer().
1215 * If block_write_full_page() is called for regular writeback
1216 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1217 * locked buffer. This only can happen if someone has written the buffer
1218 * directly, with submit_bh(). At the address_space level PageWriteback
1219 * prevents this contention from occurring.
1221 static int __btrfs_write_full_page(struct inode
*inode
, struct page
*page
,
1222 struct writeback_control
*wbc
)
1226 sector_t last_block
;
1227 struct buffer_head
*bh
, *head
;
1228 const unsigned blocksize
= 1 << inode
->i_blkbits
;
1229 int nr_underway
= 0;
1231 BUG_ON(!PageLocked(page
));
1233 last_block
= (i_size_read(inode
) - 1) >> inode
->i_blkbits
;
1235 if (!page_has_buffers(page
)) {
1236 create_empty_buffers(page
, blocksize
,
1237 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
1241 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1242 * here, and the (potentially unmapped) buffers may become dirty at
1243 * any time. If a buffer becomes dirty here after we've inspected it
1244 * then we just miss that fact, and the page stays dirty.
1246 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1247 * handle that here by just cleaning them.
1250 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1251 head
= page_buffers(page
);
1255 * Get all the dirty buffers mapped to disk addresses and
1256 * handle any aliases from the underlying blockdev's mapping.
1259 if (block
> last_block
) {
1261 * mapped buffers outside i_size will occur, because
1262 * this page can be outside i_size when there is a
1263 * truncate in progress.
1266 * The buffer was zeroed by block_write_full_page()
1268 clear_buffer_dirty(bh
);
1269 set_buffer_uptodate(bh
);
1270 } else if (!buffer_mapped(bh
) && buffer_dirty(bh
)) {
1271 WARN_ON(bh
->b_size
!= blocksize
);
1272 err
= btrfs_get_block(inode
, block
, bh
, 0);
1274 printk("writepage going to recovery err %d\n", err
);
1277 if (buffer_new(bh
)) {
1278 /* blockdev mappings never come here */
1279 clear_buffer_new(bh
);
1282 bh
= bh
->b_this_page
;
1284 } while (bh
!= head
);
1287 if (!buffer_mapped(bh
))
1290 * If it's a fully non-blocking write attempt and we cannot
1291 * lock the buffer then redirty the page. Note that this can
1292 * potentially cause a busy-wait loop from pdflush and kswapd
1293 * activity, but those code paths have their own higher-level
1296 if (wbc
->sync_mode
!= WB_SYNC_NONE
|| !wbc
->nonblocking
) {
1298 } else if (test_set_buffer_locked(bh
)) {
1299 redirty_page_for_writepage(wbc
, page
);
1302 if (test_clear_buffer_dirty(bh
) && bh
->b_blocknr
!= 0) {
1303 mark_buffer_async_write(bh
);
1307 } while ((bh
= bh
->b_this_page
) != head
);
1310 * The page and its buffers are protected by PageWriteback(), so we can
1311 * drop the bh refcounts early.
1313 BUG_ON(PageWriteback(page
));
1314 set_page_writeback(page
);
1317 struct buffer_head
*next
= bh
->b_this_page
;
1318 if (buffer_async_write(bh
)) {
1319 submit_bh(WRITE
, bh
);
1323 } while (bh
!= head
);
1328 if (nr_underway
== 0) {
1330 * The page was marked dirty, but the buffers were
1331 * clean. Someone wrote them back by hand with
1332 * ll_rw_block/submit_bh. A rare case.
1336 if (!buffer_uptodate(bh
)) {
1340 bh
= bh
->b_this_page
;
1341 } while (bh
!= head
);
1343 SetPageUptodate(page
);
1344 end_page_writeback(page
);
1350 * ENOSPC, or some other error. We may already have added some
1351 * blocks to the file, so we need to write these out to avoid
1352 * exposing stale data.
1353 * The page is currently locked and not marked for writeback
1356 /* Recovery: lock and submit the mapped buffers */
1358 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
1360 mark_buffer_async_write(bh
);
1363 * The buffer may have been set dirty during
1364 * attachment to a dirty page.
1366 clear_buffer_dirty(bh
);
1368 } while ((bh
= bh
->b_this_page
) != head
);
1370 BUG_ON(PageWriteback(page
));
1371 set_page_writeback(page
);
1373 struct buffer_head
*next
= bh
->b_this_page
;
1374 if (buffer_async_write(bh
)) {
1375 clear_buffer_dirty(bh
);
1376 submit_bh(WRITE
, bh
);
1380 } while (bh
!= head
);
1386 * The generic ->writepage function for buffer-backed address_spaces
1388 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1390 struct inode
* const inode
= page
->mapping
->host
;
1391 loff_t i_size
= i_size_read(inode
);
1392 const pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1396 /* Is the page fully inside i_size? */
1397 if (page
->index
< end_index
)
1398 return __btrfs_write_full_page(inode
, page
, wbc
);
1400 /* Is the page fully outside i_size? (truncate in progress) */
1401 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
1402 if (page
->index
>= end_index
+1 || !offset
) {
1404 * The page may have dirty, unmapped buffers. For example,
1405 * they may have been added in ext3_writepage(). Make them
1406 * freeable here, so the page does not leak.
1408 block_invalidatepage(page
, 0);
1410 return 0; /* don't care */
1414 * The page straddles i_size. It must be zeroed out on each and every
1415 * writepage invokation because it may be mmapped. "A file is mapped
1416 * in multiples of the page size. For a file that is not a multiple of
1417 * the page size, the remaining memory is zeroed when mapped, and
1418 * writes to that region are not written out to the file."
1420 kaddr
= kmap_atomic(page
, KM_USER0
);
1421 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1422 flush_dcache_page(page
);
1423 kunmap_atomic(kaddr
, KM_USER0
);
1424 return __btrfs_write_full_page(inode
, page
, wbc
);
1427 static void btrfs_truncate(struct inode
*inode
)
1429 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1431 struct btrfs_trans_handle
*trans
;
1433 if (!S_ISREG(inode
->i_mode
))
1435 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1438 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
1440 /* FIXME, add redo link to tree so we don't leak on crash */
1441 mutex_lock(&root
->fs_info
->fs_mutex
);
1442 trans
= btrfs_start_transaction(root
, 1);
1443 btrfs_set_trans_block_group(trans
, inode
);
1444 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1446 btrfs_update_inode(trans
, root
, inode
);
1447 ret
= btrfs_end_transaction(trans
, root
);
1449 mutex_unlock(&root
->fs_info
->fs_mutex
);
1450 btrfs_btree_balance_dirty(root
);
1454 * Make sure any changes to nobh_commit_write() are reflected in
1455 * nobh_truncate_page(), since it doesn't call commit_write().
1457 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1458 unsigned from
, unsigned to
)
1460 struct inode
*inode
= page
->mapping
->host
;
1461 struct buffer_head
*bh
;
1462 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1464 SetPageUptodate(page
);
1465 bh
= page_buffers(page
);
1466 if (buffer_mapped(bh
) && bh
->b_blocknr
!= 0) {
1467 set_page_dirty(page
);
1469 if (pos
> inode
->i_size
) {
1470 i_size_write(inode
, pos
);
1471 mark_inode_dirty(inode
);
1476 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1477 struct page
**prepared_pages
,
1478 const char __user
* buf
)
1480 long page_fault
= 0;
1482 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1484 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1485 size_t count
= min_t(size_t,
1486 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1487 struct page
*page
= prepared_pages
[i
];
1488 fault_in_pages_readable(buf
, count
);
1490 /* Copy data from userspace to the current page */
1492 page_fault
= __copy_from_user(page_address(page
) + offset
,
1494 /* Flush processor's dcache for this page */
1495 flush_dcache_page(page
);
1498 write_bytes
-= count
;
1503 return page_fault
? -EFAULT
: 0;
1506 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1509 for (i
= 0; i
< num_pages
; i
++) {
1512 unlock_page(pages
[i
]);
1513 mark_page_accessed(pages
[i
]);
1514 page_cache_release(pages
[i
]);
1517 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1518 struct btrfs_root
*root
,
1520 struct page
**pages
,
1530 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1531 struct buffer_head
*bh
;
1532 struct btrfs_file_extent_item
*ei
;
1534 for (i
= 0; i
< num_pages
; i
++) {
1535 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1536 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1537 /* FIXME, one block at a time */
1539 mutex_lock(&root
->fs_info
->fs_mutex
);
1540 trans
= btrfs_start_transaction(root
, 1);
1541 btrfs_set_trans_block_group(trans
, inode
);
1543 bh
= page_buffers(pages
[i
]);
1544 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
1545 struct btrfs_key key
;
1546 struct btrfs_path
*path
;
1550 path
= btrfs_alloc_path();
1552 key
.objectid
= inode
->i_ino
;
1553 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
1555 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
1556 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
1558 btrfs_file_extent_calc_inline_size(write_bytes
);
1559 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1562 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1563 path
->slots
[0], struct btrfs_file_extent_item
);
1564 btrfs_set_file_extent_generation(ei
, trans
->transid
);
1565 btrfs_set_file_extent_type(ei
,
1566 BTRFS_FILE_EXTENT_INLINE
);
1567 ptr
= btrfs_file_extent_inline_start(ei
);
1568 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
1569 ptr
, bh
->b_data
, offset
+ write_bytes
);
1570 mark_buffer_dirty(path
->nodes
[0]);
1571 btrfs_free_path(path
);
1573 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1574 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1575 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1578 SetPageChecked(pages
[i
]);
1579 // btrfs_update_inode_block_group(trans, inode);
1580 ret
= btrfs_end_transaction(trans
, root
);
1582 mutex_unlock(&root
->fs_info
->fs_mutex
);
1584 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
1585 offset
+ this_write
);
1591 WARN_ON(this_write
> write_bytes
);
1592 write_bytes
-= this_write
;
1598 static int drop_extents(struct btrfs_trans_handle
*trans
,
1599 struct btrfs_root
*root
,
1600 struct inode
*inode
,
1601 u64 start
, u64 end
, u64
*hint_block
)
1604 struct btrfs_key key
;
1605 struct btrfs_leaf
*leaf
;
1607 struct btrfs_file_extent_item
*extent
;
1610 struct btrfs_file_extent_item old
;
1611 struct btrfs_path
*path
;
1612 u64 search_start
= start
;
1618 path
= btrfs_alloc_path();
1622 btrfs_release_path(root
, path
);
1623 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1628 if (path
->slots
[0] == 0) {
1639 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1640 slot
= path
->slots
[0];
1641 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
1642 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
1646 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
1650 extent
= btrfs_item_ptr(leaf
, slot
,
1651 struct btrfs_file_extent_item
);
1652 found_type
= btrfs_file_extent_type(extent
);
1653 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1654 extent_end
= key
.offset
+
1655 (btrfs_file_extent_num_blocks(extent
) <<
1658 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1660 extent_end
= key
.offset
+
1661 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
1664 if (!found_extent
&& !found_inline
) {
1669 if (search_start
>= extent_end
) {
1674 search_start
= extent_end
;
1676 if (end
< extent_end
&& end
>= key
.offset
) {
1678 memcpy(&old
, extent
, sizeof(old
));
1679 ret
= btrfs_inc_extent_ref(trans
, root
,
1680 btrfs_file_extent_disk_blocknr(&old
),
1681 btrfs_file_extent_disk_num_blocks(&old
));
1684 WARN_ON(found_inline
);
1688 if (start
> key
.offset
) {
1691 /* truncate existing extent */
1693 WARN_ON(start
& (root
->blocksize
- 1));
1695 new_num
= (start
- key
.offset
) >>
1697 old_num
= btrfs_file_extent_num_blocks(extent
);
1699 btrfs_file_extent_disk_blocknr(extent
);
1700 inode
->i_blocks
-= (old_num
- new_num
) << 3;
1701 btrfs_set_file_extent_num_blocks(extent
,
1703 mark_buffer_dirty(path
->nodes
[0]);
1709 u64 disk_blocknr
= 0;
1710 u64 disk_num_blocks
= 0;
1711 u64 extent_num_blocks
= 0;
1714 btrfs_file_extent_disk_blocknr(extent
);
1716 btrfs_file_extent_disk_num_blocks(extent
);
1718 btrfs_file_extent_num_blocks(extent
);
1720 btrfs_file_extent_disk_blocknr(extent
);
1722 ret
= btrfs_del_item(trans
, root
, path
);
1724 btrfs_release_path(root
, path
);
1727 inode
->i_blocks
-= extent_num_blocks
<< 3;
1728 ret
= btrfs_free_extent(trans
, root
,
1730 disk_num_blocks
, 0);
1734 if (!bookend
&& search_start
>= end
) {
1741 if (bookend
&& found_extent
) {
1742 /* create bookend */
1743 struct btrfs_key ins
;
1744 ins
.objectid
= inode
->i_ino
;
1747 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
1749 btrfs_release_path(root
, path
);
1750 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1753 extent
= btrfs_item_ptr(
1754 btrfs_buffer_leaf(path
->nodes
[0]),
1756 struct btrfs_file_extent_item
);
1757 btrfs_set_file_extent_disk_blocknr(extent
,
1758 btrfs_file_extent_disk_blocknr(&old
));
1759 btrfs_set_file_extent_disk_num_blocks(extent
,
1760 btrfs_file_extent_disk_num_blocks(&old
));
1762 btrfs_set_file_extent_offset(extent
,
1763 btrfs_file_extent_offset(&old
) +
1764 ((end
- key
.offset
) >> inode
->i_blkbits
));
1765 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
1766 (end
- key
.offset
) >> inode
->i_blkbits
);
1767 btrfs_set_file_extent_num_blocks(extent
,
1768 btrfs_file_extent_num_blocks(&old
) -
1769 ((end
- key
.offset
) >> inode
->i_blkbits
));
1771 btrfs_set_file_extent_type(extent
,
1772 BTRFS_FILE_EXTENT_REG
);
1773 btrfs_set_file_extent_generation(extent
,
1774 btrfs_file_extent_generation(&old
));
1775 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1777 btrfs_file_extent_num_blocks(extent
) << 3;
1783 btrfs_free_path(path
);
1787 static int prepare_pages(struct btrfs_root
*root
,
1789 struct page
**pages
,
1792 unsigned long first_index
,
1793 unsigned long last_index
,
1795 u64 alloc_extent_start
)
1798 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1799 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1803 struct buffer_head
*bh
;
1804 struct buffer_head
*head
;
1805 loff_t isize
= i_size_read(inode
);
1807 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1809 for (i
= 0; i
< num_pages
; i
++) {
1810 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1813 goto failed_release
;
1815 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
1816 wait_on_page_writeback(pages
[i
]);
1817 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1818 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1819 if (!page_has_buffers(pages
[i
])) {
1820 create_empty_buffers(pages
[i
],
1821 root
->fs_info
->sb
->s_blocksize
,
1822 (1 << BH_Uptodate
));
1824 head
= page_buffers(pages
[i
]);
1827 err
= btrfs_map_bh_to_logical(root
, bh
,
1828 alloc_extent_start
);
1831 goto failed_truncate
;
1832 bh
= bh
->b_this_page
;
1833 if (alloc_extent_start
)
1834 alloc_extent_start
++;
1835 } while (bh
!= head
);
1837 WARN_ON(this_write
> write_bytes
);
1838 write_bytes
-= this_write
;
1843 btrfs_drop_pages(pages
, num_pages
);
1847 btrfs_drop_pages(pages
, num_pages
);
1849 vmtruncate(inode
, isize
);
1853 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1854 size_t count
, loff_t
*ppos
)
1857 size_t num_written
= 0;
1860 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1862 struct page
*pages
[8];
1863 struct page
*pinned
[2];
1864 unsigned long first_index
;
1865 unsigned long last_index
;
1868 u64 alloc_extent_start
;
1870 struct btrfs_trans_handle
*trans
;
1871 struct btrfs_key ins
;
1874 if (file
->f_flags
& O_DIRECT
)
1877 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1878 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1879 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1884 err
= remove_suid(file
->f_path
.dentry
);
1887 file_update_time(file
);
1889 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
1890 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
1893 mutex_lock(&inode
->i_mutex
);
1894 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1895 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
1897 if ((first_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1898 (pos
& (PAGE_CACHE_SIZE
- 1))) {
1899 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
1900 if (!PageUptodate(pinned
[0])) {
1901 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
1903 wait_on_page_locked(pinned
[0]);
1905 unlock_page(pinned
[0]);
1908 if (first_index
!= last_index
&&
1909 (last_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1910 pos
+ count
< inode
->i_size
&&
1911 (count
& (PAGE_CACHE_SIZE
- 1))) {
1912 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
1913 if (!PageUptodate(pinned
[1])) {
1914 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
1916 wait_on_page_locked(pinned
[1]);
1918 unlock_page(pinned
[1]);
1922 mutex_lock(&root
->fs_info
->fs_mutex
);
1923 trans
= btrfs_start_transaction(root
, 1);
1926 mutex_unlock(&root
->fs_info
->fs_mutex
);
1929 btrfs_set_trans_block_group(trans
, inode
);
1930 /* FIXME blocksize != 4096 */
1931 inode
->i_blocks
+= num_blocks
<< 3;
1933 if (start_pos
< inode
->i_size
) {
1934 /* FIXME blocksize != pagesize */
1935 ret
= drop_extents(trans
, root
, inode
,
1937 (pos
+ count
+ root
->blocksize
-1) &
1938 ~((u64
)root
->blocksize
- 1), &hint_block
);
1941 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
1942 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
1943 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1944 num_blocks
, hint_block
, (u64
)-1,
1947 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1948 start_pos
, ins
.objectid
, ins
.offset
);
1955 alloc_extent_start
= ins
.objectid
;
1956 // btrfs_update_inode_block_group(trans, inode);
1957 ret
= btrfs_end_transaction(trans
, root
);
1958 mutex_unlock(&root
->fs_info
->fs_mutex
);
1961 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1962 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1963 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1966 memset(pages
, 0, sizeof(pages
));
1967 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1968 pos
, first_index
, last_index
,
1969 write_bytes
, alloc_extent_start
);
1972 /* FIXME blocks != pagesize */
1973 if (alloc_extent_start
)
1974 alloc_extent_start
+= num_pages
;
1975 ret
= btrfs_copy_from_user(pos
, num_pages
,
1976 write_bytes
, pages
, buf
);
1979 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1980 num_pages
, pos
, write_bytes
);
1982 btrfs_drop_pages(pages
, num_pages
);
1985 count
-= write_bytes
;
1987 num_written
+= write_bytes
;
1989 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1990 btrfs_btree_balance_dirty(root
);
1994 mutex_unlock(&inode
->i_mutex
);
1997 page_cache_release(pinned
[0]);
1999 page_cache_release(pinned
[1]);
2001 current
->backing_dev_info
= NULL
;
2002 mark_inode_dirty(inode
);
2003 return num_written
? num_written
: err
;
2006 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
2007 unsigned long offset
, unsigned long size
)
2010 unsigned long left
, count
= desc
->count
;
2011 struct inode
*inode
= page
->mapping
->host
;
2016 if (!PageChecked(page
)) {
2017 /* FIXME, do it per block */
2018 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2020 int ret
= btrfs_csum_verify_file_block(root
,
2021 page
->mapping
->host
->i_ino
,
2022 page
->index
<< PAGE_CACHE_SHIFT
,
2023 kmap(page
), PAGE_CACHE_SIZE
);
2025 printk("failed to verify ino %lu page %lu\n",
2026 page
->mapping
->host
->i_ino
,
2028 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
2030 SetPageChecked(page
);
2034 * Faults on the destination of a read are common, so do it before
2037 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
2038 kaddr
= kmap_atomic(page
, KM_USER0
);
2039 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
2040 kaddr
+ offset
, size
);
2041 kunmap_atomic(kaddr
, KM_USER0
);
2046 /* Do it the slow way */
2048 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
2053 desc
->error
= -EFAULT
;
2056 desc
->count
= count
- size
;
2057 desc
->written
+= size
;
2058 desc
->arg
.buf
+= size
;
2063 * btrfs_file_aio_read - filesystem read routine
2064 * @iocb: kernel I/O control block
2065 * @iov: io vector request
2066 * @nr_segs: number of segments in the iovec
2067 * @pos: current file position
2069 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2070 unsigned long nr_segs
, loff_t pos
)
2072 struct file
*filp
= iocb
->ki_filp
;
2076 loff_t
*ppos
= &iocb
->ki_pos
;
2079 for (seg
= 0; seg
< nr_segs
; seg
++) {
2080 const struct iovec
*iv
= &iov
[seg
];
2083 * If any segment has a negative length, or the cumulative
2084 * length ever wraps negative then return -EINVAL.
2086 count
+= iv
->iov_len
;
2087 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
2089 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
2094 count
-= iv
->iov_len
; /* This segment is no good */
2099 for (seg
= 0; seg
< nr_segs
; seg
++) {
2100 read_descriptor_t desc
;
2103 desc
.arg
.buf
= iov
[seg
].iov_base
;
2104 desc
.count
= iov
[seg
].iov_len
;
2105 if (desc
.count
== 0)
2108 do_generic_file_read(filp
, ppos
, &desc
,
2110 retval
+= desc
.written
;
2112 retval
= retval
?: desc
.error
;
2120 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
2122 struct btrfs_trans_handle
*trans
;
2123 struct btrfs_key key
;
2124 struct btrfs_root_item root_item
;
2125 struct btrfs_inode_item
*inode_item
;
2126 struct buffer_head
*subvol
;
2127 struct btrfs_leaf
*leaf
;
2128 struct btrfs_root
*new_root
;
2129 struct inode
*inode
;
2133 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2135 mutex_lock(&root
->fs_info
->fs_mutex
);
2136 trans
= btrfs_start_transaction(root
, 1);
2139 subvol
= btrfs_alloc_free_block(trans
, root
, 0);
2142 leaf
= btrfs_buffer_leaf(subvol
);
2143 btrfs_set_header_nritems(&leaf
->header
, 0);
2144 btrfs_set_header_level(&leaf
->header
, 0);
2145 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
2146 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
2147 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
2148 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
2149 sizeof(leaf
->header
.fsid
));
2150 mark_buffer_dirty(subvol
);
2152 inode_item
= &root_item
.inode
;
2153 memset(inode_item
, 0, sizeof(*inode_item
));
2154 btrfs_set_inode_generation(inode_item
, 1);
2155 btrfs_set_inode_size(inode_item
, 3);
2156 btrfs_set_inode_nlink(inode_item
, 1);
2157 btrfs_set_inode_nblocks(inode_item
, 1);
2158 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
2160 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
2161 btrfs_set_root_refs(&root_item
, 1);
2165 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2169 btrfs_set_root_dirid(&root_item
, new_dirid
);
2171 key
.objectid
= objectid
;
2174 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2175 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2180 * insert the directory item
2182 key
.offset
= (u64
)-1;
2183 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2184 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2185 name
, namelen
, dir
->i_ino
, &key
, 0);
2188 ret
= btrfs_commit_transaction(trans
, root
);
2191 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2194 trans
= btrfs_start_transaction(new_root
, 1);
2197 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2198 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2199 inode
->i_op
= &btrfs_dir_inode_operations
;
2200 inode
->i_fop
= &btrfs_dir_file_operations
;
2202 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
2207 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2210 ret
= btrfs_commit_transaction(trans
, new_root
);
2215 mutex_unlock(&root
->fs_info
->fs_mutex
);
2216 btrfs_btree_balance_dirty(root
);
2220 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2222 struct btrfs_trans_handle
*trans
;
2223 struct btrfs_key key
;
2224 struct btrfs_root_item new_root_item
;
2228 if (!root
->ref_cows
)
2231 mutex_lock(&root
->fs_info
->fs_mutex
);
2232 trans
= btrfs_start_transaction(root
, 1);
2235 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2238 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2242 memcpy(&new_root_item
, &root
->root_item
,
2243 sizeof(new_root_item
));
2245 key
.objectid
= objectid
;
2248 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2249 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2251 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2256 * insert the directory item
2258 key
.offset
= (u64
)-1;
2259 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2261 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2266 ret
= btrfs_inc_root_ref(trans
, root
);
2269 ret
= btrfs_commit_transaction(trans
, root
);
2271 mutex_unlock(&root
->fs_info
->fs_mutex
);
2272 btrfs_btree_balance_dirty(root
);
2276 static int add_disk(struct btrfs_root
*root
, char *name
, int namelen
)
2278 struct block_device
*bdev
;
2279 struct btrfs_path
*path
;
2280 struct super_block
*sb
= root
->fs_info
->sb
;
2281 struct btrfs_root
*dev_root
= root
->fs_info
->dev_root
;
2282 struct btrfs_trans_handle
*trans
;
2283 struct btrfs_device_item
*dev_item
;
2284 struct btrfs_key key
;
2291 printk("adding disk %s\n", name
);
2292 path
= btrfs_alloc_path();
2295 num_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
2296 bdev
= open_bdev_excl(name
, O_RDWR
, sb
);
2298 ret
= PTR_ERR(bdev
);
2299 printk("open bdev excl failed ret %d\n", ret
);
2302 set_blocksize(bdev
, sb
->s_blocksize
);
2303 new_blocks
= bdev
->bd_inode
->i_size
>> sb
->s_blocksize_bits
;
2304 key
.objectid
= num_blocks
;
2305 key
.offset
= new_blocks
;
2307 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
2309 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
2310 trans
= btrfs_start_transaction(dev_root
, 1);
2311 item_size
= sizeof(*dev_item
) + namelen
;
2312 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks
, new_blocks
, key
.flags
, item_size
);
2313 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
, &key
, item_size
);
2315 printk("insert failed %d\n", ret
);
2316 close_bdev_excl(bdev
);
2321 dev_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2322 path
->slots
[0], struct btrfs_device_item
);
2323 btrfs_set_device_pathlen(dev_item
, namelen
);
2324 memcpy(dev_item
+ 1, name
, namelen
);
2326 device_id
= btrfs_super_last_device_id(root
->fs_info
->disk_super
) + 1;
2327 btrfs_set_super_last_device_id(root
->fs_info
->disk_super
, device_id
);
2328 btrfs_set_device_id(dev_item
, device_id
);
2329 mark_buffer_dirty(path
->nodes
[0]);
2331 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
, num_blocks
,
2335 btrfs_set_super_total_blocks(root
->fs_info
->disk_super
,
2336 num_blocks
+ new_blocks
);
2337 i_size_write(root
->fs_info
->btree_inode
,
2338 (num_blocks
+ new_blocks
) <<
2339 root
->fs_info
->btree_inode
->i_blkbits
);
2343 ret
= btrfs_commit_transaction(trans
, dev_root
);
2345 mutex_unlock(&root
->fs_info
->fs_mutex
);
2347 btrfs_free_path(path
);
2348 btrfs_btree_balance_dirty(root
);
2353 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2354 cmd
, unsigned long arg
)
2356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2357 struct btrfs_ioctl_vol_args vol_args
;
2359 struct btrfs_dir_item
*di
;
2361 struct btrfs_path
*path
;
2365 case BTRFS_IOC_SNAP_CREATE
:
2366 if (copy_from_user(&vol_args
,
2367 (struct btrfs_ioctl_vol_args __user
*)arg
,
2370 namelen
= strlen(vol_args
.name
);
2371 if (namelen
> BTRFS_VOL_NAME_MAX
)
2373 path
= btrfs_alloc_path();
2376 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2377 mutex_lock(&root
->fs_info
->fs_mutex
);
2378 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2380 vol_args
.name
, namelen
, 0);
2381 mutex_unlock(&root
->fs_info
->fs_mutex
);
2382 btrfs_free_path(path
);
2383 if (di
&& !IS_ERR(di
))
2386 if (root
== root
->fs_info
->tree_root
)
2387 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2389 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2392 case BTRFS_IOC_ADD_DISK
:
2393 if (copy_from_user(&vol_args
,
2394 (struct btrfs_ioctl_vol_args __user
*)arg
,
2397 namelen
= strlen(vol_args
.name
);
2398 if (namelen
> BTRFS_VOL_NAME_MAX
)
2400 vol_args
.name
[namelen
] = '\0';
2401 ret
= add_disk(root
, vol_args
.name
, namelen
);
2409 static struct kmem_cache
*btrfs_inode_cachep
;
2410 struct kmem_cache
*btrfs_trans_handle_cachep
;
2411 struct kmem_cache
*btrfs_transaction_cachep
;
2412 struct kmem_cache
*btrfs_bit_radix_cachep
;
2413 struct kmem_cache
*btrfs_path_cachep
;
2416 * Called inside transaction, so use GFP_NOFS
2418 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2420 struct btrfs_inode
*ei
;
2422 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2425 return &ei
->vfs_inode
;
2428 static void btrfs_destroy_inode(struct inode
*inode
)
2430 WARN_ON(!list_empty(&inode
->i_dentry
));
2431 WARN_ON(inode
->i_data
.nrpages
);
2433 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2436 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2437 unsigned long flags
)
2439 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2441 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
2442 SLAB_CTOR_CONSTRUCTOR
) {
2443 inode_init_once(&ei
->vfs_inode
);
2447 static int init_inodecache(void)
2449 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
2450 sizeof(struct btrfs_inode
),
2451 0, (SLAB_RECLAIM_ACCOUNT
|
2454 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
2455 sizeof(struct btrfs_trans_handle
),
2456 0, (SLAB_RECLAIM_ACCOUNT
|
2459 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
2460 sizeof(struct btrfs_transaction
),
2461 0, (SLAB_RECLAIM_ACCOUNT
|
2464 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
2465 sizeof(struct btrfs_transaction
),
2466 0, (SLAB_RECLAIM_ACCOUNT
|
2469 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
2471 0, (SLAB_RECLAIM_ACCOUNT
|
2473 SLAB_DESTROY_BY_RCU
),
2475 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
2476 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
2481 static void destroy_inodecache(void)
2483 kmem_cache_destroy(btrfs_inode_cachep
);
2484 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2485 kmem_cache_destroy(btrfs_transaction_cachep
);
2486 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2487 kmem_cache_destroy(btrfs_path_cachep
);
2490 static int btrfs_get_sb(struct file_system_type
*fs_type
,
2491 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2493 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
2494 btrfs_fill_super
, mnt
);
2497 static int btrfs_getattr(struct vfsmount
*mnt
,
2498 struct dentry
*dentry
, struct kstat
*stat
)
2500 struct inode
*inode
= dentry
->d_inode
;
2501 generic_fillattr(inode
, stat
);
2502 stat
->blksize
= 256 * 1024;
2506 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2508 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
2509 struct btrfs_super_block
*disk_super
= root
->fs_info
->disk_super
;
2511 buf
->f_namelen
= BTRFS_NAME_LEN
;
2512 buf
->f_blocks
= btrfs_super_total_blocks(disk_super
);
2513 buf
->f_bfree
= buf
->f_blocks
- btrfs_super_blocks_used(disk_super
);
2514 buf
->f_bavail
= buf
->f_bfree
;
2515 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2516 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2520 static struct file_system_type btrfs_fs_type
= {
2521 .owner
= THIS_MODULE
,
2523 .get_sb
= btrfs_get_sb
,
2524 .kill_sb
= kill_block_super
,
2525 .fs_flags
= FS_REQUIRES_DEV
,
2528 static struct super_operations btrfs_super_ops
= {
2529 .delete_inode
= btrfs_delete_inode
,
2530 .put_super
= btrfs_put_super
,
2531 .read_inode
= btrfs_read_locked_inode
,
2532 .write_super
= btrfs_write_super
,
2533 .sync_fs
= btrfs_sync_fs
,
2534 .write_inode
= btrfs_write_inode
,
2535 .dirty_inode
= btrfs_dirty_inode
,
2536 .alloc_inode
= btrfs_alloc_inode
,
2537 .destroy_inode
= btrfs_destroy_inode
,
2538 .statfs
= btrfs_statfs
,
2541 static struct inode_operations btrfs_dir_inode_operations
= {
2542 .lookup
= btrfs_lookup
,
2543 .create
= btrfs_create
,
2544 .unlink
= btrfs_unlink
,
2545 .mkdir
= btrfs_mkdir
,
2546 .rmdir
= btrfs_rmdir
,
2549 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2550 .lookup
= btrfs_lookup
,
2553 static struct file_operations btrfs_dir_file_operations
= {
2554 .llseek
= generic_file_llseek
,
2555 .read
= generic_read_dir
,
2556 .readdir
= btrfs_readdir
,
2557 .ioctl
= btrfs_ioctl
,
2560 static struct address_space_operations btrfs_aops
= {
2561 .readpage
= btrfs_readpage
,
2562 .writepage
= btrfs_writepage
,
2563 .sync_page
= block_sync_page
,
2564 .prepare_write
= btrfs_prepare_write
,
2565 .commit_write
= btrfs_commit_write
,
2568 static struct inode_operations btrfs_file_inode_operations
= {
2569 .truncate
= btrfs_truncate
,
2570 .getattr
= btrfs_getattr
,
2573 static struct file_operations btrfs_file_operations
= {
2574 .llseek
= generic_file_llseek
,
2575 .read
= do_sync_read
,
2576 .aio_read
= btrfs_file_aio_read
,
2577 .write
= btrfs_file_write
,
2578 .mmap
= generic_file_mmap
,
2579 .open
= generic_file_open
,
2580 .ioctl
= btrfs_ioctl
,
2581 .fsync
= btrfs_sync_file
,
2584 static int __init
init_btrfs_fs(void)
2587 printk("btrfs loaded!\n");
2588 err
= init_inodecache();
2591 kset_set_kset_s(&btrfs_subsys
, fs_subsys
);
2592 err
= subsystem_register(&btrfs_subsys
);
2595 return register_filesystem(&btrfs_fs_type
);
2597 destroy_inodecache();
2601 static void __exit
exit_btrfs_fs(void)
2603 destroy_inodecache();
2604 unregister_filesystem(&btrfs_fs_type
);
2605 subsystem_unregister(&btrfs_subsys
);
2606 printk("btrfs unloaded\n");
2609 module_init(init_btrfs_fs
)
2610 module_exit(exit_btrfs_fs
)
2612 MODULE_LICENSE("GPL");