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 void btrfs_fsinfo_release(struct kobject
*obj
)
23 struct btrfs_fs_info
*fsinfo
= container_of(obj
,
24 struct btrfs_fs_info
, kobj
);
28 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
;
57 path
= btrfs_alloc_path();
59 btrfs_init_path(path
);
60 mutex_lock(&root
->fs_info
->fs_mutex
);
62 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
63 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
65 btrfs_free_path(path
);
68 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
70 struct btrfs_inode_item
);
72 inode
->i_mode
= btrfs_inode_mode(inode_item
);
73 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
74 inode
->i_uid
= btrfs_inode_uid(inode_item
);
75 inode
->i_gid
= btrfs_inode_gid(inode_item
);
76 inode
->i_size
= btrfs_inode_size(inode_item
);
77 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
78 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
79 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
80 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
81 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
82 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
83 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
84 inode
->i_generation
= btrfs_inode_generation(inode_item
);
86 btrfs_free_path(path
);
89 mutex_unlock(&root
->fs_info
->fs_mutex
);
91 switch (inode
->i_mode
& S_IFMT
) {
94 init_special_inode(inode
, inode
->i_mode
,
95 btrfs_inode_rdev(inode_item
));
99 inode
->i_mapping
->a_ops
= &btrfs_aops
;
100 inode
->i_fop
= &btrfs_file_operations
;
101 inode
->i_op
= &btrfs_file_inode_operations
;
104 inode
->i_fop
= &btrfs_dir_file_operations
;
105 if (root
== root
->fs_info
->tree_root
)
106 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
108 inode
->i_op
= &btrfs_dir_inode_operations
;
111 // inode->i_op = &page_symlink_inode_operations;
117 btrfs_release_path(root
, path
);
118 btrfs_free_path(path
);
119 mutex_unlock(&root
->fs_info
->fs_mutex
);
120 make_bad_inode(inode
);
123 static void fill_inode_item(struct btrfs_inode_item
*item
,
126 btrfs_set_inode_uid(item
, inode
->i_uid
);
127 btrfs_set_inode_gid(item
, inode
->i_gid
);
128 btrfs_set_inode_size(item
, inode
->i_size
);
129 btrfs_set_inode_mode(item
, inode
->i_mode
);
130 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
131 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
132 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
133 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
134 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
135 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
136 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
137 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
138 btrfs_set_inode_generation(item
, inode
->i_generation
);
142 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
143 struct btrfs_root
*root
,
146 struct btrfs_inode_item
*inode_item
;
147 struct btrfs_path
*path
;
150 path
= btrfs_alloc_path();
152 btrfs_init_path(path
);
153 ret
= btrfs_lookup_inode(trans
, root
, path
,
154 &BTRFS_I(inode
)->location
, 1);
161 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
163 struct btrfs_inode_item
);
165 fill_inode_item(inode_item
, inode
);
166 btrfs_mark_buffer_dirty(path
->nodes
[0]);
169 btrfs_release_path(root
, path
);
170 btrfs_free_path(path
);
175 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
176 struct btrfs_root
*root
,
178 struct dentry
*dentry
)
180 struct btrfs_path
*path
;
181 const char *name
= dentry
->d_name
.name
;
182 int name_len
= dentry
->d_name
.len
;
185 struct btrfs_dir_item
*di
;
187 path
= btrfs_alloc_path();
189 btrfs_init_path(path
);
190 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
200 objectid
= btrfs_disk_key_objectid(&di
->location
);
201 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
203 btrfs_release_path(root
, path
);
205 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
206 objectid
, name
, name_len
, -1);
215 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
218 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
220 btrfs_free_path(path
);
222 dir
->i_size
-= name_len
* 2;
223 btrfs_update_inode(trans
, root
, dir
);
224 drop_nlink(dentry
->d_inode
);
225 btrfs_update_inode(trans
, root
, dentry
->d_inode
);
230 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
232 struct btrfs_root
*root
;
233 struct btrfs_trans_handle
*trans
;
236 root
= BTRFS_I(dir
)->root
;
237 mutex_lock(&root
->fs_info
->fs_mutex
);
238 trans
= btrfs_start_transaction(root
, 1);
239 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
240 btrfs_end_transaction(trans
, root
);
241 mutex_unlock(&root
->fs_info
->fs_mutex
);
245 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
247 struct inode
*inode
= dentry
->d_inode
;
250 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
251 struct btrfs_path
*path
;
252 struct btrfs_key key
;
253 struct btrfs_trans_handle
*trans
;
254 struct btrfs_key found_key
;
256 struct btrfs_leaf
*leaf
;
257 char *goodnames
= "..";
259 path
= btrfs_alloc_path();
261 btrfs_init_path(path
);
262 mutex_lock(&root
->fs_info
->fs_mutex
);
263 trans
= btrfs_start_transaction(root
, 1);
264 key
.objectid
= inode
->i_ino
;
265 key
.offset
= (u64
)-1;
268 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
274 if (path
->slots
[0] == 0) {
279 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
280 btrfs_disk_key_to_cpu(&found_key
,
281 &leaf
->items
[path
->slots
[0]].key
);
282 found_type
= btrfs_key_type(&found_key
);
283 if (found_key
.objectid
!= inode
->i_ino
) {
287 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
288 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
289 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
290 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
294 ret
= btrfs_del_item(trans
, root
, path
);
297 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
299 btrfs_release_path(root
, path
);
302 btrfs_release_path(root
, path
);
304 /* now the directory is empty */
305 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
310 btrfs_release_path(root
, path
);
311 btrfs_free_path(path
);
312 mutex_unlock(&root
->fs_info
->fs_mutex
);
313 ret
= btrfs_end_transaction(trans
, root
);
319 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
320 struct btrfs_root
*root
,
323 struct btrfs_path
*path
;
328 path
= btrfs_alloc_path();
330 btrfs_init_path(path
);
331 ret
= btrfs_lookup_inode(trans
, root
, path
,
332 &BTRFS_I(inode
)->location
, -1);
334 ret
= btrfs_del_item(trans
, root
, path
);
336 btrfs_free_path(path
);
340 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
341 struct btrfs_root
*root
,
345 struct btrfs_path
*path
;
346 struct btrfs_key key
;
347 struct btrfs_disk_key
*found_key
;
348 struct btrfs_leaf
*leaf
;
349 struct btrfs_file_extent_item
*fi
= NULL
;
350 u64 extent_start
= 0;
351 u64 extent_num_blocks
= 0;
354 path
= btrfs_alloc_path();
356 /* FIXME, add redo link to tree so we don't leak on crash */
357 key
.objectid
= inode
->i_ino
;
358 key
.offset
= (u64
)-1;
361 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
364 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
366 btrfs_init_path(path
);
367 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
372 BUG_ON(path
->slots
[0] == 0);
375 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
376 found_key
= &leaf
->items
[path
->slots
[0]].key
;
377 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
379 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
380 btrfs_disk_key_type(found_key
) != BTRFS_INLINE_DATA_KEY
&&
381 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
383 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
386 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
387 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
389 struct btrfs_file_extent_item
);
390 if (btrfs_file_extent_type(fi
) !=
391 BTRFS_FILE_EXTENT_INLINE
) {
393 btrfs_file_extent_disk_blocknr(fi
);
395 btrfs_file_extent_disk_num_blocks(fi
);
396 /* FIXME blocksize != 4096 */
398 btrfs_file_extent_num_blocks(fi
) << 3;
402 ret
= btrfs_del_item(trans
, root
, path
);
404 btrfs_release_path(root
, path
);
406 ret
= btrfs_free_extent(trans
, root
, extent_start
,
407 extent_num_blocks
, 0);
413 btrfs_release_path(root
, path
);
414 btrfs_free_path(path
);
418 static void btrfs_delete_inode(struct inode
*inode
)
420 struct btrfs_trans_handle
*trans
;
421 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
424 truncate_inode_pages(&inode
->i_data
, 0);
425 if (is_bad_inode(inode
)) {
429 mutex_lock(&root
->fs_info
->fs_mutex
);
430 trans
= btrfs_start_transaction(root
, 1);
431 if (S_ISREG(inode
->i_mode
)) {
432 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
435 btrfs_free_inode(trans
, root
, inode
);
436 btrfs_end_transaction(trans
, root
);
437 mutex_unlock(&root
->fs_info
->fs_mutex
);
443 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
444 struct btrfs_key
*location
)
446 const char *name
= dentry
->d_name
.name
;
447 int namelen
= dentry
->d_name
.len
;
448 struct btrfs_dir_item
*di
;
449 struct btrfs_path
*path
;
450 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
453 path
= btrfs_alloc_path();
455 btrfs_init_path(path
);
456 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
458 if (!di
|| IS_ERR(di
)) {
459 location
->objectid
= 0;
463 btrfs_disk_key_to_cpu(location
, &di
->location
);
465 btrfs_release_path(root
, path
);
466 btrfs_free_path(path
);
470 int fixup_tree_root_location(struct btrfs_root
*root
,
471 struct btrfs_key
*location
,
472 struct btrfs_root
**sub_root
)
474 struct btrfs_path
*path
;
475 struct btrfs_root_item
*ri
;
477 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
479 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
482 path
= btrfs_alloc_path();
484 mutex_lock(&root
->fs_info
->fs_mutex
);
486 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
487 if (IS_ERR(*sub_root
))
488 return PTR_ERR(*sub_root
);
490 ri
= &(*sub_root
)->root_item
;
491 location
->objectid
= btrfs_root_dirid(ri
);
493 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
494 location
->offset
= 0;
496 btrfs_free_path(path
);
497 mutex_unlock(&root
->fs_info
->fs_mutex
);
501 int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
503 struct btrfs_iget_args
*args
= p
;
504 inode
->i_ino
= args
->ino
;
505 BTRFS_I(inode
)->root
= args
->root
;
509 int btrfs_find_actor(struct inode
*inode
, void *opaque
)
511 struct btrfs_iget_args
*args
= opaque
;
512 return (args
->ino
== inode
->i_ino
&&
513 args
->root
== BTRFS_I(inode
)->root
);
516 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
517 struct btrfs_root
*root
)
520 struct btrfs_iget_args args
;
524 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
525 btrfs_init_locked_inode
,
530 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
531 struct nameidata
*nd
)
533 struct inode
* inode
;
534 struct btrfs_inode
*bi
= BTRFS_I(dir
);
535 struct btrfs_root
*root
= bi
->root
;
536 struct btrfs_root
*sub_root
= root
;
537 struct btrfs_key location
;
540 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
541 return ERR_PTR(-ENAMETOOLONG
);
542 mutex_lock(&root
->fs_info
->fs_mutex
);
543 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
544 mutex_unlock(&root
->fs_info
->fs_mutex
);
548 if (location
.objectid
) {
549 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
553 return ERR_PTR(-ENOENT
);
554 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
557 return ERR_PTR(-EACCES
);
558 if (inode
->i_state
& I_NEW
) {
559 if (sub_root
!= root
) {
560 printk("adding new root for inode %lu root %p (found %p)\n", inode
->i_ino
, sub_root
, BTRFS_I(inode
)->root
);
562 sub_root
->inode
= inode
;
564 BTRFS_I(inode
)->root
= sub_root
;
565 memcpy(&BTRFS_I(inode
)->location
, &location
,
567 btrfs_read_locked_inode(inode
);
568 unlock_new_inode(inode
);
571 return d_splice_alias(inode
, dentry
);
574 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
576 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
577 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
578 struct btrfs_item
*item
;
579 struct btrfs_dir_item
*di
;
580 struct btrfs_key key
;
581 struct btrfs_path
*path
;
584 struct btrfs_leaf
*leaf
;
587 unsigned char d_type
= DT_UNKNOWN
;
592 int key_type
= BTRFS_DIR_INDEX_KEY
;
594 /* FIXME, use a real flag for deciding about the key type */
595 if (root
->fs_info
->tree_root
== root
)
596 key_type
= BTRFS_DIR_ITEM_KEY
;
597 mutex_lock(&root
->fs_info
->fs_mutex
);
598 key
.objectid
= inode
->i_ino
;
600 btrfs_set_key_type(&key
, key_type
);
601 key
.offset
= filp
->f_pos
;
602 path
= btrfs_alloc_path();
603 btrfs_init_path(path
);
604 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
609 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
610 nritems
= btrfs_header_nritems(&leaf
->header
);
611 slot
= path
->slots
[0];
612 if (advance
|| slot
>= nritems
) {
613 if (slot
>= nritems
-1) {
614 ret
= btrfs_next_leaf(root
, path
);
617 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
618 nritems
= btrfs_header_nritems(&leaf
->header
);
619 slot
= path
->slots
[0];
626 item
= leaf
->items
+ slot
;
627 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
629 if (btrfs_disk_key_type(&item
->key
) != key_type
)
631 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
633 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
635 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
637 di_total
= btrfs_item_size(leaf
->items
+ slot
);
638 while(di_cur
< di_total
) {
639 over
= filldir(dirent
, (const char *)(di
+ 1),
640 btrfs_dir_name_len(di
),
641 btrfs_disk_key_offset(&item
->key
),
642 btrfs_disk_key_objectid(&di
->location
),
646 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
648 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
655 btrfs_release_path(root
, path
);
656 btrfs_free_path(path
);
657 mutex_unlock(&root
->fs_info
->fs_mutex
);
661 static void btrfs_put_super (struct super_block
* sb
)
663 struct btrfs_root
*root
= btrfs_sb(sb
);
666 ret
= close_ctree(root
);
668 printk("close ctree returns %d\n", ret
);
670 sb
->s_fs_info
= NULL
;
673 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
675 struct inode
* inode
;
676 struct dentry
* root_dentry
;
677 struct btrfs_super_block
*disk_super
;
678 struct btrfs_root
*tree_root
;
679 struct btrfs_inode
*bi
;
681 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
682 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
683 sb
->s_op
= &btrfs_super_ops
;
686 tree_root
= open_ctree(sb
);
689 printk("btrfs: open_ctree failed\n");
692 sb
->s_fs_info
= tree_root
;
693 disk_super
= tree_root
->fs_info
->disk_super
;
694 printk("read in super total blocks %Lu root %Lu\n",
695 btrfs_super_total_blocks(disk_super
),
696 btrfs_super_root_dir(disk_super
));
698 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
701 bi
->location
.objectid
= inode
->i_ino
;
702 bi
->location
.offset
= 0;
703 bi
->location
.flags
= 0;
704 bi
->root
= tree_root
;
705 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
709 if (inode
->i_state
& I_NEW
) {
710 btrfs_read_locked_inode(inode
);
711 unlock_new_inode(inode
);
714 root_dentry
= d_alloc_root(inode
);
719 sb
->s_root
= root_dentry
;
724 static int btrfs_write_inode(struct inode
*inode
, int wait
)
726 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
727 struct btrfs_trans_handle
*trans
;
731 mutex_lock(&root
->fs_info
->fs_mutex
);
732 trans
= btrfs_start_transaction(root
, 1);
733 ret
= btrfs_commit_transaction(trans
, root
);
734 mutex_unlock(&root
->fs_info
->fs_mutex
);
739 static void btrfs_dirty_inode(struct inode
*inode
)
741 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
742 struct btrfs_trans_handle
*trans
;
744 mutex_lock(&root
->fs_info
->fs_mutex
);
745 trans
= btrfs_start_transaction(root
, 1);
746 btrfs_update_inode(trans
, root
, inode
);
747 btrfs_end_transaction(trans
, root
);
748 mutex_unlock(&root
->fs_info
->fs_mutex
);
751 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
752 struct btrfs_root
*root
,
753 u64 objectid
, int mode
)
756 struct btrfs_inode_item inode_item
;
757 struct btrfs_key
*location
;
760 inode
= new_inode(root
->fs_info
->sb
);
762 return ERR_PTR(-ENOMEM
);
764 BTRFS_I(inode
)->root
= root
;
766 inode
->i_uid
= current
->fsuid
;
767 inode
->i_gid
= current
->fsgid
;
768 inode
->i_mode
= mode
;
769 inode
->i_ino
= objectid
;
771 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
772 fill_inode_item(&inode_item
, inode
);
773 location
= &BTRFS_I(inode
)->location
;
774 location
->objectid
= objectid
;
776 location
->offset
= 0;
777 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
779 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
782 insert_inode_hash(inode
);
786 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
787 struct dentry
*dentry
, struct inode
*inode
)
790 struct btrfs_key key
;
791 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
792 key
.objectid
= inode
->i_ino
;
794 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
797 ret
= btrfs_insert_dir_item(trans
, root
,
798 dentry
->d_name
.name
, dentry
->d_name
.len
,
799 dentry
->d_parent
->d_inode
->i_ino
,
802 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
803 ret
= btrfs_update_inode(trans
, root
,
804 dentry
->d_parent
->d_inode
);
809 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
810 struct dentry
*dentry
, struct inode
*inode
)
812 int err
= btrfs_add_link(trans
, dentry
, inode
);
814 d_instantiate(dentry
, inode
);
822 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
823 int mode
, struct nameidata
*nd
)
825 struct btrfs_trans_handle
*trans
;
826 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
832 mutex_lock(&root
->fs_info
->fs_mutex
);
833 trans
= btrfs_start_transaction(root
, 1);
835 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
841 inode
= btrfs_new_inode(trans
, root
, objectid
, mode
);
842 err
= PTR_ERR(inode
);
845 // FIXME mark the inode dirty
846 err
= btrfs_add_nondir(trans
, dentry
, inode
);
850 inode
->i_mapping
->a_ops
= &btrfs_aops
;
851 inode
->i_fop
= &btrfs_file_operations
;
852 inode
->i_op
= &btrfs_file_inode_operations
;
854 dir
->i_sb
->s_dirt
= 1;
856 btrfs_end_transaction(trans
, root
);
857 mutex_unlock(&root
->fs_info
->fs_mutex
);
860 inode_dec_link_count(inode
);
866 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
867 struct btrfs_root
*root
,
868 u64 objectid
, u64 dirid
)
872 struct btrfs_key key
;
877 key
.objectid
= objectid
;
880 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
882 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
886 key
.objectid
= dirid
;
887 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
895 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
898 struct btrfs_trans_handle
*trans
;
899 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
904 mutex_lock(&root
->fs_info
->fs_mutex
);
905 trans
= btrfs_start_transaction(root
, 1);
907 err
= PTR_ERR(trans
);
911 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
917 inode
= btrfs_new_inode(trans
, root
, objectid
, S_IFDIR
| mode
);
919 err
= PTR_ERR(inode
);
923 inode
->i_op
= &btrfs_dir_inode_operations
;
924 inode
->i_fop
= &btrfs_dir_file_operations
;
926 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
931 err
= btrfs_update_inode(trans
, root
, inode
);
934 err
= btrfs_add_link(trans
, dentry
, inode
);
937 d_instantiate(dentry
, inode
);
941 btrfs_end_transaction(trans
, root
);
943 mutex_unlock(&root
->fs_info
->fs_mutex
);
949 static int btrfs_sync_file(struct file
*file
,
950 struct dentry
*dentry
, int datasync
)
952 struct inode
*inode
= dentry
->d_inode
;
953 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
955 struct btrfs_trans_handle
*trans
;
957 mutex_lock(&root
->fs_info
->fs_mutex
);
958 trans
= btrfs_start_transaction(root
, 1);
963 ret
= btrfs_commit_transaction(trans
, root
);
964 mutex_unlock(&root
->fs_info
->fs_mutex
);
966 return ret
> 0 ? EIO
: ret
;
969 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
971 struct btrfs_trans_handle
*trans
;
972 struct btrfs_root
*root
;
978 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
981 filemap_write_and_wait(root
->fs_info
->btree_inode
->i_mapping
);
982 mutex_lock(&root
->fs_info
->fs_mutex
);
983 trans
= btrfs_start_transaction(root
, 1);
984 ret
= btrfs_commit_transaction(trans
, root
);
987 printk("btrfs sync_fs\n");
988 mutex_unlock(&root
->fs_info
->fs_mutex
);
992 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
993 struct buffer_head
*result
, int create
)
998 u64 extent_start
= 0;
1000 u64 objectid
= inode
->i_ino
;
1002 struct btrfs_path
*path
;
1003 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1004 struct btrfs_file_extent_item
*item
;
1005 struct btrfs_leaf
*leaf
;
1006 struct btrfs_disk_key
*found_key
;
1008 path
= btrfs_alloc_path();
1010 btrfs_init_path(path
);
1015 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1017 iblock
<< inode
->i_blkbits
, 0);
1024 if (path
->slots
[0] == 0) {
1025 btrfs_release_path(root
, path
);
1031 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1032 struct btrfs_file_extent_item
);
1033 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1034 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1035 blocknr
+= btrfs_file_extent_offset(item
);
1037 /* are we inside the extent that was found? */
1038 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1039 found_type
= btrfs_disk_key_type(found_key
);
1040 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1041 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1044 btrfs_release_path(root
, path
);
1047 found_type
= btrfs_file_extent_type(item
);
1048 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1049 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1050 extent_start
= extent_start
>> inode
->i_blkbits
;
1051 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1052 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1054 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1055 iblock
- extent_start
);
1058 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1062 size
= btrfs_file_extent_inline_len(leaf
->items
+
1064 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1065 extent_start
>>= inode
->i_blkbits
;
1066 if (iblock
< extent_start
|| iblock
> extent_end
) {
1069 ptr
= btrfs_file_extent_inline_start(item
);
1070 map
= kmap(result
->b_page
);
1071 memcpy(map
, ptr
, size
);
1072 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1073 flush_dcache_page(result
->b_page
);
1074 kunmap(result
->b_page
);
1075 set_buffer_uptodate(result
);
1076 SetPageChecked(result
->b_page
);
1077 btrfs_map_bh_to_logical(root
, result
, 0);
1080 btrfs_release_path(root
, path
);
1081 btrfs_free_path(path
);
1085 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1086 struct buffer_head
*result
, int create
)
1089 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1090 mutex_lock(&root
->fs_info
->fs_mutex
);
1091 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1092 mutex_unlock(&root
->fs_info
->fs_mutex
);
1096 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1097 unsigned from
, unsigned to
)
1099 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
1102 static void btrfs_write_super(struct super_block
*sb
)
1104 btrfs_sync_fs(sb
, 1);
1107 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1109 return mpage_readpage(page
, btrfs_get_block
);
1113 * While block_write_full_page is writing back the dirty buffers under
1114 * the page lock, whoever dirtied the buffers may decide to clean them
1115 * again at any time. We handle that by only looking at the buffer
1116 * state inside lock_buffer().
1118 * If block_write_full_page() is called for regular writeback
1119 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1120 * locked buffer. This only can happen if someone has written the buffer
1121 * directly, with submit_bh(). At the address_space level PageWriteback
1122 * prevents this contention from occurring.
1124 static int __btrfs_write_full_page(struct inode
*inode
, struct page
*page
,
1125 struct writeback_control
*wbc
)
1129 sector_t last_block
;
1130 struct buffer_head
*bh
, *head
;
1131 const unsigned blocksize
= 1 << inode
->i_blkbits
;
1132 int nr_underway
= 0;
1134 BUG_ON(!PageLocked(page
));
1136 last_block
= (i_size_read(inode
) - 1) >> inode
->i_blkbits
;
1138 if (!page_has_buffers(page
)) {
1139 create_empty_buffers(page
, blocksize
,
1140 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
1144 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1145 * here, and the (potentially unmapped) buffers may become dirty at
1146 * any time. If a buffer becomes dirty here after we've inspected it
1147 * then we just miss that fact, and the page stays dirty.
1149 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1150 * handle that here by just cleaning them.
1153 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1154 head
= page_buffers(page
);
1158 * Get all the dirty buffers mapped to disk addresses and
1159 * handle any aliases from the underlying blockdev's mapping.
1162 if (block
> last_block
) {
1164 * mapped buffers outside i_size will occur, because
1165 * this page can be outside i_size when there is a
1166 * truncate in progress.
1169 * The buffer was zeroed by block_write_full_page()
1171 clear_buffer_dirty(bh
);
1172 set_buffer_uptodate(bh
);
1173 } else if (!buffer_mapped(bh
) && buffer_dirty(bh
)) {
1174 WARN_ON(bh
->b_size
!= blocksize
);
1175 err
= btrfs_get_block(inode
, block
, bh
, 0);
1178 if (buffer_new(bh
)) {
1179 /* blockdev mappings never come here */
1180 clear_buffer_new(bh
);
1181 unmap_underlying_metadata(bh
->b_bdev
,
1185 bh
= bh
->b_this_page
;
1187 } while (bh
!= head
);
1190 if (!buffer_mapped(bh
))
1193 * If it's a fully non-blocking write attempt and we cannot
1194 * lock the buffer then redirty the page. Note that this can
1195 * potentially cause a busy-wait loop from pdflush and kswapd
1196 * activity, but those code paths have their own higher-level
1199 if (wbc
->sync_mode
!= WB_SYNC_NONE
|| !wbc
->nonblocking
) {
1201 } else if (test_set_buffer_locked(bh
)) {
1202 redirty_page_for_writepage(wbc
, page
);
1205 if (test_clear_buffer_dirty(bh
) && bh
->b_blocknr
!= 0) {
1206 mark_buffer_async_write(bh
);
1210 } while ((bh
= bh
->b_this_page
) != head
);
1213 * The page and its buffers are protected by PageWriteback(), so we can
1214 * drop the bh refcounts early.
1216 BUG_ON(PageWriteback(page
));
1217 set_page_writeback(page
);
1220 struct buffer_head
*next
= bh
->b_this_page
;
1221 if (buffer_async_write(bh
)) {
1222 submit_bh(WRITE
, bh
);
1226 } while (bh
!= head
);
1231 if (nr_underway
== 0) {
1233 * The page was marked dirty, but the buffers were
1234 * clean. Someone wrote them back by hand with
1235 * ll_rw_block/submit_bh. A rare case.
1239 if (!buffer_uptodate(bh
)) {
1243 bh
= bh
->b_this_page
;
1244 } while (bh
!= head
);
1246 SetPageUptodate(page
);
1247 end_page_writeback(page
);
1249 * The page and buffer_heads can be released at any time from
1252 wbc
->pages_skipped
++; /* We didn't write this page */
1258 * ENOSPC, or some other error. We may already have added some
1259 * blocks to the file, so we need to write these out to avoid
1260 * exposing stale data.
1261 * The page is currently locked and not marked for writeback
1264 /* Recovery: lock and submit the mapped buffers */
1266 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
1268 mark_buffer_async_write(bh
);
1271 * The buffer may have been set dirty during
1272 * attachment to a dirty page.
1274 clear_buffer_dirty(bh
);
1276 } while ((bh
= bh
->b_this_page
) != head
);
1278 BUG_ON(PageWriteback(page
));
1279 set_page_writeback(page
);
1281 struct buffer_head
*next
= bh
->b_this_page
;
1282 if (buffer_async_write(bh
)) {
1283 clear_buffer_dirty(bh
);
1284 submit_bh(WRITE
, bh
);
1288 } while (bh
!= head
);
1294 * The generic ->writepage function for buffer-backed address_spaces
1296 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1298 struct inode
* const inode
= page
->mapping
->host
;
1299 loff_t i_size
= i_size_read(inode
);
1300 const pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1304 /* Is the page fully inside i_size? */
1305 if (page
->index
< end_index
)
1306 return __btrfs_write_full_page(inode
, page
, wbc
);
1308 /* Is the page fully outside i_size? (truncate in progress) */
1309 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
1310 if (page
->index
>= end_index
+1 || !offset
) {
1312 * The page may have dirty, unmapped buffers. For example,
1313 * they may have been added in ext3_writepage(). Make them
1314 * freeable here, so the page does not leak.
1316 block_invalidatepage(page
, 0);
1318 return 0; /* don't care */
1322 * The page straddles i_size. It must be zeroed out on each and every
1323 * writepage invokation because it may be mmapped. "A file is mapped
1324 * in multiples of the page size. For a file that is not a multiple of
1325 * the page size, the remaining memory is zeroed when mapped, and
1326 * writes to that region are not written out to the file."
1328 kaddr
= kmap_atomic(page
, KM_USER0
);
1329 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1330 flush_dcache_page(page
);
1331 kunmap_atomic(kaddr
, KM_USER0
);
1332 return __btrfs_write_full_page(inode
, page
, wbc
);
1335 static void btrfs_truncate(struct inode
*inode
)
1337 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1339 struct btrfs_trans_handle
*trans
;
1341 if (!S_ISREG(inode
->i_mode
))
1343 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1346 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
1348 /* FIXME, add redo link to tree so we don't leak on crash */
1349 mutex_lock(&root
->fs_info
->fs_mutex
);
1350 trans
= btrfs_start_transaction(root
, 1);
1351 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1353 ret
= btrfs_end_transaction(trans
, root
);
1355 mutex_unlock(&root
->fs_info
->fs_mutex
);
1356 mark_inode_dirty(inode
);
1360 * Make sure any changes to nobh_commit_write() are reflected in
1361 * nobh_truncate_page(), since it doesn't call commit_write().
1363 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1364 unsigned from
, unsigned to
)
1366 struct inode
*inode
= page
->mapping
->host
;
1367 struct buffer_head
*bh
;
1368 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1370 SetPageUptodate(page
);
1371 bh
= page_buffers(page
);
1372 if (buffer_mapped(bh
) && bh
->b_blocknr
!= 0) {
1373 set_page_dirty(page
);
1375 if (pos
> inode
->i_size
) {
1376 i_size_write(inode
, pos
);
1377 mark_inode_dirty(inode
);
1382 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1383 struct page
**prepared_pages
,
1384 const char __user
* buf
)
1386 long page_fault
= 0;
1388 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1390 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1391 size_t count
= min_t(size_t,
1392 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1393 struct page
*page
= prepared_pages
[i
];
1394 fault_in_pages_readable(buf
, count
);
1396 /* Copy data from userspace to the current page */
1398 page_fault
= __copy_from_user(page_address(page
) + offset
,
1400 /* Flush processor's dcache for this page */
1401 flush_dcache_page(page
);
1404 write_bytes
-= count
;
1409 return page_fault
? -EFAULT
: 0;
1412 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1415 for (i
= 0; i
< num_pages
; i
++) {
1418 unlock_page(pages
[i
]);
1419 mark_page_accessed(pages
[i
]);
1420 page_cache_release(pages
[i
]);
1423 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1424 struct btrfs_root
*root
,
1426 struct page
**pages
,
1436 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1437 struct buffer_head
*bh
;
1438 struct btrfs_file_extent_item
*ei
;
1440 for (i
= 0; i
< num_pages
; i
++) {
1441 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1442 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1443 /* FIXME, one block at a time */
1445 mutex_lock(&root
->fs_info
->fs_mutex
);
1446 trans
= btrfs_start_transaction(root
, 1);
1448 bh
= page_buffers(pages
[i
]);
1449 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
1450 struct btrfs_key key
;
1451 struct btrfs_path
*path
;
1455 path
= btrfs_alloc_path();
1457 key
.objectid
= inode
->i_ino
;
1458 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
1460 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
1461 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
1463 btrfs_file_extent_calc_inline_size(write_bytes
);
1464 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1467 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1468 path
->slots
[0], struct btrfs_file_extent_item
);
1469 btrfs_set_file_extent_generation(ei
, trans
->transid
);
1470 btrfs_set_file_extent_type(ei
,
1471 BTRFS_FILE_EXTENT_INLINE
);
1472 ptr
= btrfs_file_extent_inline_start(ei
);
1473 memcpy(ptr
, bh
->b_data
, offset
+ write_bytes
);
1474 mark_buffer_dirty(path
->nodes
[0]);
1475 btrfs_free_path(path
);
1477 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1478 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1479 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1482 SetPageChecked(pages
[i
]);
1483 ret
= btrfs_end_transaction(trans
, root
);
1485 mutex_unlock(&root
->fs_info
->fs_mutex
);
1487 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
1488 offset
+ this_write
);
1494 WARN_ON(this_write
> write_bytes
);
1495 write_bytes
-= this_write
;
1501 static int drop_extents(struct btrfs_trans_handle
*trans
,
1502 struct btrfs_root
*root
,
1503 struct inode
*inode
,
1507 struct btrfs_key key
;
1508 struct btrfs_leaf
*leaf
;
1510 struct btrfs_file_extent_item
*extent
;
1513 struct btrfs_file_extent_item old
;
1514 struct btrfs_path
*path
;
1515 u64 search_start
= start
;
1521 path
= btrfs_alloc_path();
1525 btrfs_release_path(root
, path
);
1526 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1531 if (path
->slots
[0] == 0) {
1542 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1543 slot
= path
->slots
[0];
1544 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
1545 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
1549 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
1553 extent
= btrfs_item_ptr(leaf
, slot
,
1554 struct btrfs_file_extent_item
);
1555 found_type
= btrfs_file_extent_type(extent
);
1556 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1557 extent_end
= key
.offset
+
1558 (btrfs_file_extent_num_blocks(extent
) <<
1561 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1563 extent_end
= key
.offset
+
1564 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
1567 if (!found_extent
&& !found_inline
) {
1572 if (search_start
>= extent_end
) {
1577 search_start
= extent_end
;
1579 if (end
< extent_end
&& end
>= key
.offset
) {
1581 memcpy(&old
, extent
, sizeof(old
));
1582 ret
= btrfs_inc_extent_ref(trans
, root
,
1583 btrfs_file_extent_disk_blocknr(&old
),
1584 btrfs_file_extent_disk_num_blocks(&old
));
1587 WARN_ON(found_inline
);
1591 if (start
> key
.offset
) {
1594 /* truncate existing extent */
1596 WARN_ON(start
& (root
->blocksize
- 1));
1598 new_num
= (start
- key
.offset
) >>
1600 old_num
= btrfs_file_extent_num_blocks(extent
);
1601 inode
->i_blocks
-= (old_num
- new_num
) << 3;
1602 btrfs_set_file_extent_num_blocks(extent
,
1604 mark_buffer_dirty(path
->nodes
[0]);
1608 ret = btrfs_truncate_item(trans, root, path,
1609 start - key.offset);
1615 u64 disk_blocknr
= 0;
1616 u64 disk_num_blocks
= 0;
1617 u64 extent_num_blocks
= 0;
1620 btrfs_file_extent_disk_blocknr(extent
);
1622 btrfs_file_extent_disk_num_blocks(extent
);
1624 btrfs_file_extent_num_blocks(extent
);
1626 ret
= btrfs_del_item(trans
, root
, path
);
1628 btrfs_release_path(root
, path
);
1631 btrfs_file_extent_num_blocks(extent
) << 3;
1632 ret
= btrfs_free_extent(trans
, root
,
1634 disk_num_blocks
, 0);
1638 if (!bookend
&& search_start
>= end
) {
1645 if (bookend
&& found_extent
) {
1646 /* create bookend */
1647 struct btrfs_key ins
;
1648 ins
.objectid
= inode
->i_ino
;
1651 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
1653 btrfs_release_path(root
, path
);
1654 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1657 extent
= btrfs_item_ptr(
1658 btrfs_buffer_leaf(path
->nodes
[0]),
1660 struct btrfs_file_extent_item
);
1661 btrfs_set_file_extent_disk_blocknr(extent
,
1662 btrfs_file_extent_disk_blocknr(&old
));
1663 btrfs_set_file_extent_disk_num_blocks(extent
,
1664 btrfs_file_extent_disk_num_blocks(&old
));
1666 btrfs_set_file_extent_offset(extent
,
1667 btrfs_file_extent_offset(&old
) +
1668 ((end
- key
.offset
) >> inode
->i_blkbits
));
1669 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
1670 (end
- key
.offset
) >> inode
->i_blkbits
);
1671 btrfs_set_file_extent_num_blocks(extent
,
1672 btrfs_file_extent_num_blocks(&old
) -
1673 ((end
- key
.offset
) >> inode
->i_blkbits
));
1675 btrfs_set_file_extent_type(extent
,
1676 BTRFS_FILE_EXTENT_REG
);
1677 btrfs_set_file_extent_generation(extent
,
1678 btrfs_file_extent_generation(&old
));
1679 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1681 btrfs_file_extent_num_blocks(extent
) << 3;
1687 btrfs_free_path(path
);
1691 static int prepare_pages(struct btrfs_root
*root
,
1693 struct page
**pages
,
1696 unsigned long first_index
,
1697 unsigned long last_index
,
1699 u64 alloc_extent_start
)
1702 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1703 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1707 struct buffer_head
*bh
;
1708 struct buffer_head
*head
;
1709 loff_t isize
= i_size_read(inode
);
1711 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1713 for (i
= 0; i
< num_pages
; i
++) {
1714 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1717 goto failed_release
;
1719 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1720 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1721 create_empty_buffers(pages
[i
], root
->fs_info
->sb
->s_blocksize
,
1722 (1 << BH_Uptodate
));
1723 head
= page_buffers(pages
[i
]);
1726 err
= btrfs_map_bh_to_logical(root
, bh
,
1727 alloc_extent_start
);
1730 goto failed_truncate
;
1731 bh
= bh
->b_this_page
;
1732 if (alloc_extent_start
)
1733 alloc_extent_start
++;
1734 } while (bh
!= head
);
1736 WARN_ON(this_write
> write_bytes
);
1737 write_bytes
-= this_write
;
1742 btrfs_drop_pages(pages
, num_pages
);
1746 btrfs_drop_pages(pages
, num_pages
);
1748 vmtruncate(inode
, isize
);
1752 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1753 size_t count
, loff_t
*ppos
)
1756 size_t num_written
= 0;
1759 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1760 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1761 struct page
*pages
[8];
1762 struct page
*pinned
[2] = { NULL
, NULL
};
1763 unsigned long first_index
;
1764 unsigned long last_index
;
1767 u64 alloc_extent_start
;
1768 struct btrfs_trans_handle
*trans
;
1769 struct btrfs_key ins
;
1771 if (file
->f_flags
& O_DIRECT
)
1774 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1775 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1776 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1781 err
= remove_suid(file
->f_path
.dentry
);
1784 file_update_time(file
);
1786 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
1787 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
1790 mutex_lock(&inode
->i_mutex
);
1791 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1792 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
1794 if ((first_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1795 (pos
& (PAGE_CACHE_SIZE
- 1))) {
1796 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
1797 if (!PageUptodate(pinned
[0])) {
1798 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
1801 unlock_page(pinned
[0]);
1804 if (first_index
!= last_index
&&
1805 (last_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1806 (count
& (PAGE_CACHE_SIZE
- 1))) {
1807 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
1808 if (!PageUptodate(pinned
[1])) {
1809 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
1812 unlock_page(pinned
[1]);
1816 mutex_lock(&root
->fs_info
->fs_mutex
);
1817 trans
= btrfs_start_transaction(root
, 1);
1820 mutex_unlock(&root
->fs_info
->fs_mutex
);
1823 /* FIXME blocksize != 4096 */
1824 inode
->i_blocks
+= num_blocks
<< 3;
1825 if (start_pos
< inode
->i_size
) {
1826 /* FIXME blocksize != pagesize */
1827 ret
= drop_extents(trans
, root
, inode
,
1829 (pos
+ count
+ root
->blocksize
-1) &
1830 ~((u64
)root
->blocksize
- 1));
1833 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
1834 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
1835 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1836 num_blocks
, 1, (u64
)-1, &ins
);
1838 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1839 start_pos
, ins
.objectid
, ins
.offset
);
1846 alloc_extent_start
= ins
.objectid
;
1847 ret
= btrfs_end_transaction(trans
, root
);
1848 mutex_unlock(&root
->fs_info
->fs_mutex
);
1851 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1852 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1853 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1856 memset(pages
, 0, sizeof(pages
));
1857 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1858 pos
, first_index
, last_index
,
1859 write_bytes
, alloc_extent_start
);
1862 /* FIXME blocks != pagesize */
1863 if (alloc_extent_start
)
1864 alloc_extent_start
+= num_pages
;
1865 ret
= btrfs_copy_from_user(pos
, num_pages
,
1866 write_bytes
, pages
, buf
);
1869 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1870 num_pages
, pos
, write_bytes
);
1872 btrfs_drop_pages(pages
, num_pages
);
1875 count
-= write_bytes
;
1877 num_written
+= write_bytes
;
1879 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1883 mutex_unlock(&inode
->i_mutex
);
1886 page_cache_release(pinned
[0]);
1888 page_cache_release(pinned
[1]);
1890 current
->backing_dev_info
= NULL
;
1891 mark_inode_dirty(inode
);
1892 return num_written
? num_written
: err
;
1895 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1896 unsigned long offset
, unsigned long size
)
1899 unsigned long left
, count
= desc
->count
;
1900 struct inode
*inode
= page
->mapping
->host
;
1905 if (!PageChecked(page
)) {
1906 /* FIXME, do it per block */
1907 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1909 int ret
= btrfs_csum_verify_file_block(root
,
1910 page
->mapping
->host
->i_ino
,
1911 page
->index
<< PAGE_CACHE_SHIFT
,
1912 kmap(page
), PAGE_CACHE_SIZE
);
1914 printk("failed to verify ino %lu page %lu\n",
1915 page
->mapping
->host
->i_ino
,
1917 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
1919 SetPageChecked(page
);
1923 * Faults on the destination of a read are common, so do it before
1926 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1927 kaddr
= kmap_atomic(page
, KM_USER0
);
1928 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1929 kaddr
+ offset
, size
);
1930 kunmap_atomic(kaddr
, KM_USER0
);
1935 /* Do it the slow way */
1937 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1942 desc
->error
= -EFAULT
;
1945 desc
->count
= count
- size
;
1946 desc
->written
+= size
;
1947 desc
->arg
.buf
+= size
;
1952 * btrfs_file_aio_read - filesystem read routine
1953 * @iocb: kernel I/O control block
1954 * @iov: io vector request
1955 * @nr_segs: number of segments in the iovec
1956 * @pos: current file position
1958 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1959 unsigned long nr_segs
, loff_t pos
)
1961 struct file
*filp
= iocb
->ki_filp
;
1965 loff_t
*ppos
= &iocb
->ki_pos
;
1968 for (seg
= 0; seg
< nr_segs
; seg
++) {
1969 const struct iovec
*iv
= &iov
[seg
];
1972 * If any segment has a negative length, or the cumulative
1973 * length ever wraps negative then return -EINVAL.
1975 count
+= iv
->iov_len
;
1976 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1978 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1983 count
-= iv
->iov_len
; /* This segment is no good */
1988 for (seg
= 0; seg
< nr_segs
; seg
++) {
1989 read_descriptor_t desc
;
1992 desc
.arg
.buf
= iov
[seg
].iov_base
;
1993 desc
.count
= iov
[seg
].iov_len
;
1994 if (desc
.count
== 0)
1997 do_generic_file_read(filp
, ppos
, &desc
,
1999 retval
+= desc
.written
;
2001 retval
= retval
?: desc
.error
;
2009 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
2011 struct btrfs_trans_handle
*trans
;
2012 struct btrfs_key key
;
2013 struct btrfs_root_item root_item
;
2014 struct btrfs_inode_item
*inode_item
;
2015 struct buffer_head
*subvol
;
2016 struct btrfs_leaf
*leaf
;
2017 struct btrfs_root
*new_root
;
2018 struct inode
*inode
;
2021 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2023 mutex_lock(&root
->fs_info
->fs_mutex
);
2024 trans
= btrfs_start_transaction(root
, 1);
2027 subvol
= btrfs_alloc_free_block(trans
, root
);
2030 leaf
= btrfs_buffer_leaf(subvol
);
2031 btrfs_set_header_nritems(&leaf
->header
, 0);
2032 btrfs_set_header_level(&leaf
->header
, 0);
2033 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
2034 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
2035 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
2036 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
2037 sizeof(leaf
->header
.fsid
));
2038 mark_buffer_dirty(subvol
);
2040 inode_item
= &root_item
.inode
;
2041 memset(inode_item
, 0, sizeof(*inode_item
));
2042 btrfs_set_inode_generation(inode_item
, 1);
2043 btrfs_set_inode_size(inode_item
, 3);
2044 btrfs_set_inode_nlink(inode_item
, 1);
2045 btrfs_set_inode_nblocks(inode_item
, 1);
2046 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
2048 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
2049 btrfs_set_root_refs(&root_item
, 1);
2053 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2057 btrfs_set_root_dirid(&root_item
, new_dirid
);
2059 key
.objectid
= objectid
;
2062 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2063 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2068 * insert the directory item
2070 key
.offset
= (u64
)-1;
2071 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2073 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2077 ret
= btrfs_commit_transaction(trans
, root
);
2080 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2083 trans
= btrfs_start_transaction(new_root
, 1);
2086 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
, S_IFDIR
| 0700);
2087 inode
->i_op
= &btrfs_dir_inode_operations
;
2088 inode
->i_fop
= &btrfs_dir_file_operations
;
2090 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
2095 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2098 ret
= btrfs_commit_transaction(trans
, new_root
);
2103 mutex_unlock(&root
->fs_info
->fs_mutex
);
2107 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2109 struct btrfs_trans_handle
*trans
;
2110 struct btrfs_key key
;
2111 struct btrfs_root_item new_root_item
;
2115 if (!root
->ref_cows
)
2118 mutex_lock(&root
->fs_info
->fs_mutex
);
2119 trans
= btrfs_start_transaction(root
, 1);
2122 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2125 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2129 memcpy(&new_root_item
, &root
->root_item
,
2130 sizeof(new_root_item
));
2132 key
.objectid
= objectid
;
2135 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2136 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2138 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2143 * insert the directory item
2145 key
.offset
= (u64
)-1;
2146 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2148 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2153 ret
= btrfs_inc_root_ref(trans
, root
);
2156 ret
= btrfs_commit_transaction(trans
, root
);
2158 mutex_unlock(&root
->fs_info
->fs_mutex
);
2162 static int add_disk(struct btrfs_root
*root
, char *name
, int namelen
)
2164 struct block_device
*bdev
;
2165 struct btrfs_path
*path
;
2166 struct super_block
*sb
= root
->fs_info
->sb
;
2167 struct btrfs_root
*dev_root
= root
->fs_info
->dev_root
;
2168 struct btrfs_trans_handle
*trans
;
2169 struct btrfs_device_item
*dev_item
;
2170 struct btrfs_key key
;
2177 printk("adding disk %s\n", name
);
2178 path
= btrfs_alloc_path();
2181 num_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
2182 bdev
= open_bdev_excl(name
, O_RDWR
, sb
);
2184 ret
= PTR_ERR(bdev
);
2185 printk("open bdev excl failed ret %d\n", ret
);
2188 set_blocksize(bdev
, sb
->s_blocksize
);
2189 new_blocks
= bdev
->bd_inode
->i_size
>> sb
->s_blocksize_bits
;
2190 key
.objectid
= num_blocks
;
2191 key
.offset
= new_blocks
;
2193 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
2195 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
2196 trans
= btrfs_start_transaction(dev_root
, 1);
2197 item_size
= sizeof(*dev_item
) + namelen
;
2198 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks
, new_blocks
, key
.flags
, item_size
);
2199 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
, &key
, item_size
);
2201 printk("insert failed %d\n", ret
);
2202 close_bdev_excl(bdev
);
2207 dev_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2208 path
->slots
[0], struct btrfs_device_item
);
2209 btrfs_set_device_pathlen(dev_item
, namelen
);
2210 memcpy(dev_item
+ 1, name
, namelen
);
2212 device_id
= btrfs_super_last_device_id(root
->fs_info
->disk_super
) + 1;
2213 btrfs_set_super_last_device_id(root
->fs_info
->disk_super
, device_id
);
2214 btrfs_set_device_id(dev_item
, device_id
);
2215 mark_buffer_dirty(path
->nodes
[0]);
2217 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
, num_blocks
,
2221 btrfs_set_super_total_blocks(root
->fs_info
->disk_super
,
2222 num_blocks
+ new_blocks
);
2223 i_size_write(root
->fs_info
->btree_inode
,
2224 (num_blocks
+ new_blocks
) <<
2225 root
->fs_info
->btree_inode
->i_blkbits
);
2229 ret
= btrfs_commit_transaction(trans
, dev_root
);
2231 mutex_unlock(&root
->fs_info
->fs_mutex
);
2233 btrfs_free_path(path
);
2238 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2239 cmd
, unsigned long arg
)
2241 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2242 struct btrfs_ioctl_vol_args vol_args
;
2244 struct btrfs_dir_item
*di
;
2246 struct btrfs_path
*path
;
2250 case BTRFS_IOC_SNAP_CREATE
:
2251 if (copy_from_user(&vol_args
,
2252 (struct btrfs_ioctl_vol_args __user
*)arg
,
2255 namelen
= strlen(vol_args
.name
);
2256 if (namelen
> BTRFS_VOL_NAME_MAX
)
2258 path
= btrfs_alloc_path();
2261 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2262 mutex_lock(&root
->fs_info
->fs_mutex
);
2263 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2265 vol_args
.name
, namelen
, 0);
2266 mutex_unlock(&root
->fs_info
->fs_mutex
);
2267 btrfs_free_path(path
);
2268 if (di
&& !IS_ERR(di
))
2271 if (root
== root
->fs_info
->tree_root
)
2272 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2274 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2277 case BTRFS_IOC_ADD_DISK
:
2278 if (copy_from_user(&vol_args
,
2279 (struct btrfs_ioctl_vol_args __user
*)arg
,
2282 namelen
= strlen(vol_args
.name
);
2283 if (namelen
> BTRFS_VOL_NAME_MAX
)
2285 vol_args
.name
[namelen
] = '\0';
2286 ret
= add_disk(root
, vol_args
.name
, namelen
);
2294 static struct kmem_cache
*btrfs_inode_cachep
;
2295 struct kmem_cache
*btrfs_trans_handle_cachep
;
2296 struct kmem_cache
*btrfs_transaction_cachep
;
2297 struct kmem_cache
*btrfs_bit_radix_cachep
;
2298 struct kmem_cache
*btrfs_path_cachep
;
2301 * Called inside transaction, so use GFP_NOFS
2303 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2305 struct btrfs_inode
*ei
;
2307 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2310 return &ei
->vfs_inode
;
2313 static void btrfs_destroy_inode(struct inode
*inode
)
2315 WARN_ON(!list_empty(&inode
->i_dentry
));
2316 WARN_ON(inode
->i_data
.nrpages
);
2318 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2321 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2322 unsigned long flags
)
2324 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2326 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
2327 SLAB_CTOR_CONSTRUCTOR
) {
2328 inode_init_once(&ei
->vfs_inode
);
2332 static int init_inodecache(void)
2334 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
2335 sizeof(struct btrfs_inode
),
2336 0, (SLAB_RECLAIM_ACCOUNT
|
2339 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
2340 sizeof(struct btrfs_trans_handle
),
2341 0, (SLAB_RECLAIM_ACCOUNT
|
2344 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
2345 sizeof(struct btrfs_transaction
),
2346 0, (SLAB_RECLAIM_ACCOUNT
|
2349 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
2350 sizeof(struct btrfs_transaction
),
2351 0, (SLAB_RECLAIM_ACCOUNT
|
2354 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
2356 0, (SLAB_RECLAIM_ACCOUNT
|
2358 SLAB_DESTROY_BY_RCU
),
2360 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
2361 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
2366 static void destroy_inodecache(void)
2368 kmem_cache_destroy(btrfs_inode_cachep
);
2369 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2370 kmem_cache_destroy(btrfs_transaction_cachep
);
2371 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2372 kmem_cache_destroy(btrfs_path_cachep
);
2375 static int btrfs_get_sb(struct file_system_type
*fs_type
,
2376 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2378 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
2379 btrfs_fill_super
, mnt
);
2383 static int btrfs_getattr(struct vfsmount
*mnt
,
2384 struct dentry
*dentry
, struct kstat
*stat
)
2386 struct inode
*inode
= dentry
->d_inode
;
2387 generic_fillattr(inode
, stat
);
2388 stat
->blksize
= 256 * 1024;
2392 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2394 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
2395 struct btrfs_super_block
*disk_super
= root
->fs_info
->disk_super
;
2397 buf
->f_namelen
= BTRFS_NAME_LEN
;
2398 buf
->f_blocks
= btrfs_super_total_blocks(disk_super
);
2399 buf
->f_bfree
= buf
->f_blocks
- btrfs_super_blocks_used(disk_super
);
2400 buf
->f_bavail
= buf
->f_bfree
;
2401 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2402 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2406 static struct file_system_type btrfs_fs_type
= {
2407 .owner
= THIS_MODULE
,
2409 .get_sb
= btrfs_get_sb
,
2410 .kill_sb
= kill_block_super
,
2411 .fs_flags
= FS_REQUIRES_DEV
,
2414 static struct super_operations btrfs_super_ops
= {
2415 .delete_inode
= btrfs_delete_inode
,
2416 .put_super
= btrfs_put_super
,
2417 .read_inode
= btrfs_read_locked_inode
,
2418 .write_super
= btrfs_write_super
,
2419 .sync_fs
= btrfs_sync_fs
,
2420 .write_inode
= btrfs_write_inode
,
2421 .dirty_inode
= btrfs_dirty_inode
,
2422 .alloc_inode
= btrfs_alloc_inode
,
2423 .destroy_inode
= btrfs_destroy_inode
,
2424 .statfs
= btrfs_statfs
,
2427 static struct inode_operations btrfs_dir_inode_operations
= {
2428 .lookup
= btrfs_lookup
,
2429 .create
= btrfs_create
,
2430 .unlink
= btrfs_unlink
,
2431 .mkdir
= btrfs_mkdir
,
2432 .rmdir
= btrfs_rmdir
,
2435 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2436 .lookup
= btrfs_lookup
,
2439 static struct file_operations btrfs_dir_file_operations
= {
2440 .llseek
= generic_file_llseek
,
2441 .read
= generic_read_dir
,
2442 .readdir
= btrfs_readdir
,
2443 .ioctl
= btrfs_ioctl
,
2446 static struct address_space_operations btrfs_aops
= {
2447 .readpage
= btrfs_readpage
,
2448 .writepage
= btrfs_writepage
,
2449 .sync_page
= block_sync_page
,
2450 .prepare_write
= btrfs_prepare_write
,
2451 .commit_write
= btrfs_commit_write
,
2454 static struct inode_operations btrfs_file_inode_operations
= {
2455 .truncate
= btrfs_truncate
,
2456 .getattr
= btrfs_getattr
,
2459 static struct file_operations btrfs_file_operations
= {
2460 .llseek
= generic_file_llseek
,
2461 .read
= do_sync_read
,
2462 .aio_read
= btrfs_file_aio_read
,
2463 .write
= btrfs_file_write
,
2464 .mmap
= generic_file_mmap
,
2465 .open
= generic_file_open
,
2466 .ioctl
= btrfs_ioctl
,
2467 .fsync
= btrfs_sync_file
,
2470 static int __init
init_btrfs_fs(void)
2473 printk("btrfs loaded!\n");
2474 err
= init_inodecache();
2477 kset_set_kset_s(&btrfs_subsys
, fs_subsys
);
2478 err
= subsystem_register(&btrfs_subsys
);
2481 return register_filesystem(&btrfs_fs_type
);
2483 destroy_inodecache();
2487 static void __exit
exit_btrfs_fs(void)
2489 destroy_inodecache();
2490 unregister_filesystem(&btrfs_fs_type
);
2491 subsystem_unregister(&btrfs_subsys
);
2492 printk("btrfs unloaded\n");
2495 module_init(init_btrfs_fs
)
2496 module_exit(exit_btrfs_fs
)
2498 MODULE_LICENSE("GPL");