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 struct btrfs_iget_args
{
23 struct btrfs_root
*root
;
26 #define BTRFS_SUPER_MAGIC 0x9123682E
28 static struct inode_operations btrfs_dir_inode_operations
;
29 static struct inode_operations btrfs_dir_ro_inode_operations
;
30 static struct super_operations btrfs_super_ops
;
31 static struct file_operations btrfs_dir_file_operations
;
32 static struct inode_operations btrfs_file_inode_operations
;
33 static struct address_space_operations btrfs_aops
;
34 static struct file_operations btrfs_file_operations
;
36 static void btrfs_read_locked_inode(struct inode
*inode
)
38 struct btrfs_path
*path
;
39 struct btrfs_inode_item
*inode_item
;
40 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
41 struct btrfs_key location
;
42 struct btrfs_block_group_cache
*alloc_group
;
43 u64 alloc_group_block
;
46 path
= btrfs_alloc_path();
48 btrfs_init_path(path
);
49 mutex_lock(&root
->fs_info
->fs_mutex
);
51 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
52 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
54 btrfs_free_path(path
);
57 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
59 struct btrfs_inode_item
);
61 inode
->i_mode
= btrfs_inode_mode(inode_item
);
62 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
63 inode
->i_uid
= btrfs_inode_uid(inode_item
);
64 inode
->i_gid
= btrfs_inode_gid(inode_item
);
65 inode
->i_size
= btrfs_inode_size(inode_item
);
66 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
67 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
68 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
69 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
70 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
71 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
72 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
73 inode
->i_generation
= btrfs_inode_generation(inode_item
);
74 alloc_group_block
= btrfs_inode_block_group(inode_item
);
75 ret
= radix_tree_gang_lookup(&root
->fs_info
->block_group_radix
,
76 (void **)&alloc_group
,
77 alloc_group_block
, 1);
79 BTRFS_I(inode
)->block_group
= alloc_group
;
81 btrfs_free_path(path
);
84 mutex_unlock(&root
->fs_info
->fs_mutex
);
86 switch (inode
->i_mode
& S_IFMT
) {
89 init_special_inode(inode
, inode
->i_mode
,
90 btrfs_inode_rdev(inode_item
));
94 inode
->i_mapping
->a_ops
= &btrfs_aops
;
95 inode
->i_fop
= &btrfs_file_operations
;
96 inode
->i_op
= &btrfs_file_inode_operations
;
99 inode
->i_fop
= &btrfs_dir_file_operations
;
100 if (root
== root
->fs_info
->tree_root
)
101 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
103 inode
->i_op
= &btrfs_dir_inode_operations
;
106 // inode->i_op = &page_symlink_inode_operations;
112 btrfs_release_path(root
, path
);
113 btrfs_free_path(path
);
114 mutex_unlock(&root
->fs_info
->fs_mutex
);
115 make_bad_inode(inode
);
118 static void fill_inode_item(struct btrfs_inode_item
*item
,
121 btrfs_set_inode_uid(item
, inode
->i_uid
);
122 btrfs_set_inode_gid(item
, inode
->i_gid
);
123 btrfs_set_inode_size(item
, inode
->i_size
);
124 btrfs_set_inode_mode(item
, inode
->i_mode
);
125 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
126 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
127 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
128 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
129 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
130 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
131 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
132 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
133 btrfs_set_inode_generation(item
, inode
->i_generation
);
134 btrfs_set_inode_block_group(item
,
135 BTRFS_I(inode
)->block_group
->key
.objectid
);
138 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
139 struct btrfs_root
*root
,
142 struct btrfs_inode_item
*inode_item
;
143 struct btrfs_path
*path
;
146 path
= btrfs_alloc_path();
148 btrfs_init_path(path
);
149 ret
= btrfs_lookup_inode(trans
, root
, path
,
150 &BTRFS_I(inode
)->location
, 1);
157 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
159 struct btrfs_inode_item
);
161 fill_inode_item(inode_item
, inode
);
162 btrfs_mark_buffer_dirty(path
->nodes
[0]);
165 btrfs_release_path(root
, path
);
166 btrfs_free_path(path
);
171 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
172 struct btrfs_root
*root
,
174 struct dentry
*dentry
)
176 struct btrfs_path
*path
;
177 const char *name
= dentry
->d_name
.name
;
178 int name_len
= dentry
->d_name
.len
;
181 struct btrfs_dir_item
*di
;
183 path
= btrfs_alloc_path();
185 btrfs_init_path(path
);
186 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
196 objectid
= btrfs_disk_key_objectid(&di
->location
);
197 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
199 btrfs_release_path(root
, path
);
201 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
202 objectid
, name
, name_len
, -1);
211 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
214 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
216 btrfs_free_path(path
);
218 dir
->i_size
-= name_len
* 2;
219 btrfs_update_inode(trans
, root
, dir
);
220 drop_nlink(dentry
->d_inode
);
221 btrfs_update_inode(trans
, root
, dentry
->d_inode
);
222 dir
->i_sb
->s_dirt
= 1;
227 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
229 struct btrfs_root
*root
;
230 struct btrfs_trans_handle
*trans
;
233 root
= BTRFS_I(dir
)->root
;
234 mutex_lock(&root
->fs_info
->fs_mutex
);
235 trans
= btrfs_start_transaction(root
, 1);
236 btrfs_set_trans_block_group(trans
, dir
);
237 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
238 btrfs_end_transaction(trans
, root
);
239 mutex_unlock(&root
->fs_info
->fs_mutex
);
240 btrfs_btree_balance_dirty(root
);
244 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
246 struct inode
*inode
= dentry
->d_inode
;
249 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
250 struct btrfs_path
*path
;
251 struct btrfs_key key
;
252 struct btrfs_trans_handle
*trans
;
253 struct btrfs_key found_key
;
255 struct btrfs_leaf
*leaf
;
256 char *goodnames
= "..";
258 path
= btrfs_alloc_path();
260 btrfs_init_path(path
);
261 mutex_lock(&root
->fs_info
->fs_mutex
);
262 trans
= btrfs_start_transaction(root
, 1);
263 btrfs_set_trans_block_group(trans
, dir
);
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
);
314 btrfs_btree_balance_dirty(root
);
320 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
321 struct btrfs_root
*root
,
324 struct btrfs_path
*path
;
329 path
= btrfs_alloc_path();
331 btrfs_init_path(path
);
332 ret
= btrfs_lookup_inode(trans
, root
, path
,
333 &BTRFS_I(inode
)->location
, -1);
335 ret
= btrfs_del_item(trans
, root
, path
);
337 btrfs_free_path(path
);
341 static void reada_truncate(struct btrfs_root
*root
, struct btrfs_path
*path
,
344 struct btrfs_node
*node
;
354 node
= btrfs_buffer_node(path
->nodes
[1]);
355 slot
= path
->slots
[1];
358 nritems
= btrfs_header_nritems(&node
->header
);
359 for (i
= slot
- 1; i
>= 0; i
--) {
360 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
361 if (item_objectid
!= objectid
)
363 blocknr
= btrfs_node_blockptr(node
, i
);
364 ret
= readahead_tree_block(root
, blocknr
);
370 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
371 struct btrfs_root
*root
,
375 struct btrfs_path
*path
;
376 struct btrfs_key key
;
377 struct btrfs_disk_key
*found_key
;
378 struct btrfs_leaf
*leaf
;
379 struct btrfs_file_extent_item
*fi
= NULL
;
380 u64 extent_start
= 0;
381 u64 extent_num_blocks
= 0;
384 path
= btrfs_alloc_path();
386 /* FIXME, add redo link to tree so we don't leak on crash */
387 key
.objectid
= inode
->i_ino
;
388 key
.offset
= (u64
)-1;
391 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
394 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
396 btrfs_init_path(path
);
397 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
402 BUG_ON(path
->slots
[0] == 0);
405 reada_truncate(root
, path
, inode
->i_ino
);
406 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
407 found_key
= &leaf
->items
[path
->slots
[0]].key
;
408 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
410 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
411 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
413 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
416 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
417 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
419 struct btrfs_file_extent_item
);
420 if (btrfs_file_extent_type(fi
) !=
421 BTRFS_FILE_EXTENT_INLINE
) {
423 btrfs_file_extent_disk_blocknr(fi
);
425 btrfs_file_extent_disk_num_blocks(fi
);
426 /* FIXME blocksize != 4096 */
428 btrfs_file_extent_num_blocks(fi
) << 3;
432 ret
= btrfs_del_item(trans
, root
, path
);
434 btrfs_release_path(root
, path
);
436 ret
= btrfs_free_extent(trans
, root
, extent_start
,
437 extent_num_blocks
, 0);
443 btrfs_release_path(root
, path
);
444 btrfs_free_path(path
);
445 inode
->i_sb
->s_dirt
= 1;
449 static void btrfs_delete_inode(struct inode
*inode
)
451 struct btrfs_trans_handle
*trans
;
452 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
455 truncate_inode_pages(&inode
->i_data
, 0);
456 if (is_bad_inode(inode
)) {
460 mutex_lock(&root
->fs_info
->fs_mutex
);
461 trans
= btrfs_start_transaction(root
, 1);
462 btrfs_set_trans_block_group(trans
, inode
);
463 if (S_ISREG(inode
->i_mode
)) {
464 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
467 btrfs_free_inode(trans
, root
, inode
);
468 btrfs_end_transaction(trans
, root
);
469 mutex_unlock(&root
->fs_info
->fs_mutex
);
470 btrfs_btree_balance_dirty(root
);
476 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
477 struct btrfs_key
*location
)
479 const char *name
= dentry
->d_name
.name
;
480 int namelen
= dentry
->d_name
.len
;
481 struct btrfs_dir_item
*di
;
482 struct btrfs_path
*path
;
483 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
486 path
= btrfs_alloc_path();
488 btrfs_init_path(path
);
489 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
491 if (!di
|| IS_ERR(di
)) {
492 location
->objectid
= 0;
496 btrfs_disk_key_to_cpu(location
, &di
->location
);
498 btrfs_release_path(root
, path
);
499 btrfs_free_path(path
);
503 static int fixup_tree_root_location(struct btrfs_root
*root
,
504 struct btrfs_key
*location
,
505 struct btrfs_root
**sub_root
)
507 struct btrfs_path
*path
;
508 struct btrfs_root_item
*ri
;
510 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
512 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
515 path
= btrfs_alloc_path();
517 mutex_lock(&root
->fs_info
->fs_mutex
);
519 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
520 if (IS_ERR(*sub_root
))
521 return PTR_ERR(*sub_root
);
523 ri
= &(*sub_root
)->root_item
;
524 location
->objectid
= btrfs_root_dirid(ri
);
526 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
527 location
->offset
= 0;
529 btrfs_free_path(path
);
530 mutex_unlock(&root
->fs_info
->fs_mutex
);
534 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
536 struct btrfs_iget_args
*args
= p
;
537 inode
->i_ino
= args
->ino
;
538 BTRFS_I(inode
)->root
= args
->root
;
542 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
544 struct btrfs_iget_args
*args
= opaque
;
545 return (args
->ino
== inode
->i_ino
&&
546 args
->root
== BTRFS_I(inode
)->root
);
549 static struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
550 struct btrfs_root
*root
)
553 struct btrfs_iget_args args
;
557 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
558 btrfs_init_locked_inode
,
563 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
564 struct nameidata
*nd
)
566 struct inode
* inode
;
567 struct btrfs_inode
*bi
= BTRFS_I(dir
);
568 struct btrfs_root
*root
= bi
->root
;
569 struct btrfs_root
*sub_root
= root
;
570 struct btrfs_key location
;
573 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
574 return ERR_PTR(-ENAMETOOLONG
);
575 mutex_lock(&root
->fs_info
->fs_mutex
);
576 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
577 mutex_unlock(&root
->fs_info
->fs_mutex
);
581 if (location
.objectid
) {
582 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
586 return ERR_PTR(-ENOENT
);
587 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
590 return ERR_PTR(-EACCES
);
591 if (inode
->i_state
& I_NEW
) {
592 if (sub_root
!= root
) {
593 printk("adding new root for inode %lu root %p (found %p)\n", inode
->i_ino
, sub_root
, BTRFS_I(inode
)->root
);
595 sub_root
->inode
= inode
;
597 BTRFS_I(inode
)->root
= sub_root
;
598 memcpy(&BTRFS_I(inode
)->location
, &location
,
600 btrfs_read_locked_inode(inode
);
601 unlock_new_inode(inode
);
604 return d_splice_alias(inode
, dentry
);
607 static void reada_leaves(struct btrfs_root
*root
, struct btrfs_path
*path
,
610 struct btrfs_node
*node
;
620 node
= btrfs_buffer_node(path
->nodes
[1]);
621 slot
= path
->slots
[1];
622 nritems
= btrfs_header_nritems(&node
->header
);
623 for (i
= slot
+ 1; i
< nritems
; i
++) {
624 item_objectid
= btrfs_disk_key_objectid(&node
->ptrs
[i
].key
);
625 if (item_objectid
!= objectid
)
627 blocknr
= btrfs_node_blockptr(node
, i
);
628 ret
= readahead_tree_block(root
, blocknr
);
634 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
636 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
638 struct btrfs_item
*item
;
639 struct btrfs_dir_item
*di
;
640 struct btrfs_key key
;
641 struct btrfs_path
*path
;
644 struct btrfs_leaf
*leaf
;
647 unsigned char d_type
= DT_UNKNOWN
;
652 int key_type
= BTRFS_DIR_INDEX_KEY
;
654 /* FIXME, use a real flag for deciding about the key type */
655 if (root
->fs_info
->tree_root
== root
)
656 key_type
= BTRFS_DIR_ITEM_KEY
;
657 mutex_lock(&root
->fs_info
->fs_mutex
);
658 key
.objectid
= inode
->i_ino
;
660 btrfs_set_key_type(&key
, key_type
);
661 key
.offset
= filp
->f_pos
;
662 path
= btrfs_alloc_path();
663 btrfs_init_path(path
);
664 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
668 reada_leaves(root
, path
, inode
->i_ino
);
670 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
671 nritems
= btrfs_header_nritems(&leaf
->header
);
672 slot
= path
->slots
[0];
673 if (advance
|| slot
>= nritems
) {
674 if (slot
>= nritems
-1) {
675 reada_leaves(root
, path
, inode
->i_ino
);
676 ret
= btrfs_next_leaf(root
, path
);
679 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
680 nritems
= btrfs_header_nritems(&leaf
->header
);
681 slot
= path
->slots
[0];
688 item
= leaf
->items
+ slot
;
689 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
691 if (btrfs_disk_key_type(&item
->key
) != key_type
)
693 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
695 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
697 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
699 di_total
= btrfs_item_size(leaf
->items
+ slot
);
700 while(di_cur
< di_total
) {
701 over
= filldir(dirent
, (const char *)(di
+ 1),
702 btrfs_dir_name_len(di
),
703 btrfs_disk_key_offset(&item
->key
),
704 btrfs_disk_key_objectid(&di
->location
),
708 di_len
= btrfs_dir_name_len(di
) + sizeof(*di
);
710 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
717 btrfs_release_path(root
, path
);
718 btrfs_free_path(path
);
719 mutex_unlock(&root
->fs_info
->fs_mutex
);
723 static void btrfs_put_super (struct super_block
* sb
)
725 struct btrfs_root
*root
= btrfs_sb(sb
);
728 ret
= close_ctree(root
);
730 printk("close ctree returns %d\n", ret
);
732 sb
->s_fs_info
= NULL
;
735 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
737 struct inode
* inode
;
738 struct dentry
* root_dentry
;
739 struct btrfs_super_block
*disk_super
;
740 struct btrfs_root
*tree_root
;
741 struct btrfs_inode
*bi
;
743 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
744 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
745 sb
->s_op
= &btrfs_super_ops
;
748 tree_root
= open_ctree(sb
);
751 printk("btrfs: open_ctree failed\n");
754 sb
->s_fs_info
= tree_root
;
755 disk_super
= tree_root
->fs_info
->disk_super
;
756 printk("read in super total blocks %Lu root %Lu\n",
757 btrfs_super_total_blocks(disk_super
),
758 btrfs_super_root_dir(disk_super
));
760 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
763 bi
->location
.objectid
= inode
->i_ino
;
764 bi
->location
.offset
= 0;
765 bi
->location
.flags
= 0;
766 bi
->root
= tree_root
;
767 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
771 if (inode
->i_state
& I_NEW
) {
772 btrfs_read_locked_inode(inode
);
773 unlock_new_inode(inode
);
776 root_dentry
= d_alloc_root(inode
);
781 sb
->s_root
= root_dentry
;
786 static int btrfs_write_inode(struct inode
*inode
, int wait
)
788 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
789 struct btrfs_trans_handle
*trans
;
793 mutex_lock(&root
->fs_info
->fs_mutex
);
794 trans
= btrfs_start_transaction(root
, 1);
795 btrfs_set_trans_block_group(trans
, inode
);
796 ret
= btrfs_commit_transaction(trans
, root
);
797 mutex_unlock(&root
->fs_info
->fs_mutex
);
802 static void btrfs_dirty_inode(struct inode
*inode
)
804 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
805 struct btrfs_trans_handle
*trans
;
807 mutex_lock(&root
->fs_info
->fs_mutex
);
808 trans
= btrfs_start_transaction(root
, 1);
809 btrfs_set_trans_block_group(trans
, inode
);
810 btrfs_update_inode(trans
, root
, inode
);
811 btrfs_end_transaction(trans
, root
);
812 mutex_unlock(&root
->fs_info
->fs_mutex
);
813 btrfs_btree_balance_dirty(root
);
816 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
817 struct btrfs_root
*root
,
819 struct btrfs_block_group_cache
*group
,
823 struct btrfs_inode_item inode_item
;
824 struct btrfs_key
*location
;
828 inode
= new_inode(root
->fs_info
->sb
);
830 return ERR_PTR(-ENOMEM
);
832 BTRFS_I(inode
)->root
= root
;
837 group
= btrfs_find_block_group(root
, group
, 0, 0, owner
);
838 BTRFS_I(inode
)->block_group
= group
;
840 inode
->i_uid
= current
->fsuid
;
841 inode
->i_gid
= current
->fsgid
;
842 inode
->i_mode
= mode
;
843 inode
->i_ino
= objectid
;
845 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
846 fill_inode_item(&inode_item
, inode
);
847 location
= &BTRFS_I(inode
)->location
;
848 location
->objectid
= objectid
;
850 location
->offset
= 0;
851 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
853 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
856 insert_inode_hash(inode
);
860 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
861 struct dentry
*dentry
, struct inode
*inode
)
864 struct btrfs_key key
;
865 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
866 key
.objectid
= inode
->i_ino
;
868 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
871 ret
= btrfs_insert_dir_item(trans
, root
,
872 dentry
->d_name
.name
, dentry
->d_name
.len
,
873 dentry
->d_parent
->d_inode
->i_ino
,
876 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
877 ret
= btrfs_update_inode(trans
, root
,
878 dentry
->d_parent
->d_inode
);
883 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
884 struct dentry
*dentry
, struct inode
*inode
)
886 int err
= btrfs_add_link(trans
, dentry
, inode
);
888 d_instantiate(dentry
, inode
);
896 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
897 int mode
, struct nameidata
*nd
)
899 struct btrfs_trans_handle
*trans
;
900 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
906 mutex_lock(&root
->fs_info
->fs_mutex
);
907 trans
= btrfs_start_transaction(root
, 1);
908 btrfs_set_trans_block_group(trans
, dir
);
910 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
916 inode
= btrfs_new_inode(trans
, root
, objectid
,
917 BTRFS_I(dir
)->block_group
, mode
);
918 err
= PTR_ERR(inode
);
922 btrfs_set_trans_block_group(trans
, inode
);
923 err
= btrfs_add_nondir(trans
, dentry
, inode
);
927 inode
->i_mapping
->a_ops
= &btrfs_aops
;
928 inode
->i_fop
= &btrfs_file_operations
;
929 inode
->i_op
= &btrfs_file_inode_operations
;
931 dir
->i_sb
->s_dirt
= 1;
932 btrfs_update_inode_block_group(trans
, inode
);
933 btrfs_update_inode_block_group(trans
, dir
);
935 btrfs_end_transaction(trans
, root
);
936 mutex_unlock(&root
->fs_info
->fs_mutex
);
939 inode_dec_link_count(inode
);
942 btrfs_btree_balance_dirty(root
);
946 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
947 struct btrfs_root
*root
,
948 u64 objectid
, u64 dirid
)
952 struct btrfs_key key
;
957 key
.objectid
= objectid
;
960 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
962 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
966 key
.objectid
= dirid
;
967 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
975 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
978 struct btrfs_trans_handle
*trans
;
979 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
984 mutex_lock(&root
->fs_info
->fs_mutex
);
985 trans
= btrfs_start_transaction(root
, 1);
986 btrfs_set_trans_block_group(trans
, dir
);
988 err
= PTR_ERR(trans
);
992 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
998 inode
= btrfs_new_inode(trans
, root
, objectid
,
999 BTRFS_I(dir
)->block_group
, S_IFDIR
| mode
);
1000 if (IS_ERR(inode
)) {
1001 err
= PTR_ERR(inode
);
1005 inode
->i_op
= &btrfs_dir_inode_operations
;
1006 inode
->i_fop
= &btrfs_dir_file_operations
;
1007 btrfs_set_trans_block_group(trans
, inode
);
1009 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
1014 err
= btrfs_update_inode(trans
, root
, inode
);
1017 err
= btrfs_add_link(trans
, dentry
, inode
);
1020 d_instantiate(dentry
, inode
);
1022 dir
->i_sb
->s_dirt
= 1;
1023 btrfs_update_inode_block_group(trans
, inode
);
1024 btrfs_update_inode_block_group(trans
, dir
);
1027 btrfs_end_transaction(trans
, root
);
1029 mutex_unlock(&root
->fs_info
->fs_mutex
);
1032 btrfs_btree_balance_dirty(root
);
1036 static int btrfs_sync_file(struct file
*file
,
1037 struct dentry
*dentry
, int datasync
)
1039 struct inode
*inode
= dentry
->d_inode
;
1040 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1042 struct btrfs_trans_handle
*trans
;
1044 mutex_lock(&root
->fs_info
->fs_mutex
);
1045 trans
= btrfs_start_transaction(root
, 1);
1050 ret
= btrfs_commit_transaction(trans
, root
);
1051 mutex_unlock(&root
->fs_info
->fs_mutex
);
1053 return ret
> 0 ? EIO
: ret
;
1056 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
1058 struct btrfs_trans_handle
*trans
;
1059 struct btrfs_root
*root
;
1061 root
= btrfs_sb(sb
);
1065 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
1068 mutex_lock(&root
->fs_info
->fs_mutex
);
1069 trans
= btrfs_start_transaction(root
, 1);
1070 ret
= btrfs_commit_transaction(trans
, root
);
1073 printk("btrfs sync_fs\n");
1074 mutex_unlock(&root
->fs_info
->fs_mutex
);
1078 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1079 struct buffer_head
*result
, int create
)
1084 u64 extent_start
= 0;
1086 u64 objectid
= inode
->i_ino
;
1088 struct btrfs_path
*path
;
1089 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1090 struct btrfs_file_extent_item
*item
;
1091 struct btrfs_leaf
*leaf
;
1092 struct btrfs_disk_key
*found_key
;
1094 path
= btrfs_alloc_path();
1096 btrfs_init_path(path
);
1101 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
1103 iblock
<< inode
->i_blkbits
, 0);
1110 if (path
->slots
[0] == 0) {
1111 btrfs_release_path(root
, path
);
1117 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1118 struct btrfs_file_extent_item
);
1119 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1120 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1121 blocknr
+= btrfs_file_extent_offset(item
);
1123 /* are we inside the extent that was found? */
1124 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1125 found_type
= btrfs_disk_key_type(found_key
);
1126 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1127 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
1132 found_type
= btrfs_file_extent_type(item
);
1133 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1134 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1135 extent_start
= extent_start
>> inode
->i_blkbits
;
1136 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1137 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1139 btrfs_map_bh_to_logical(root
, result
, blocknr
+
1140 iblock
- extent_start
);
1143 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1147 size
= btrfs_file_extent_inline_len(leaf
->items
+
1149 extent_end
= (extent_start
+ size
) >> inode
->i_blkbits
;
1150 extent_start
>>= inode
->i_blkbits
;
1151 if (iblock
< extent_start
|| iblock
> extent_end
) {
1154 ptr
= btrfs_file_extent_inline_start(item
);
1155 map
= kmap(result
->b_page
);
1156 memcpy(map
, ptr
, size
);
1157 memset(map
+ size
, 0, PAGE_CACHE_SIZE
- size
);
1158 flush_dcache_page(result
->b_page
);
1159 kunmap(result
->b_page
);
1160 set_buffer_uptodate(result
);
1161 SetPageChecked(result
->b_page
);
1162 btrfs_map_bh_to_logical(root
, result
, 0);
1165 btrfs_free_path(path
);
1169 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1170 struct buffer_head
*result
, int create
)
1173 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1174 mutex_lock(&root
->fs_info
->fs_mutex
);
1175 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1176 mutex_unlock(&root
->fs_info
->fs_mutex
);
1180 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1181 unsigned from
, unsigned to
)
1183 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
1186 static void btrfs_write_super(struct super_block
*sb
)
1188 btrfs_sync_fs(sb
, 1);
1191 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1193 return mpage_readpage(page
, btrfs_get_block
);
1197 * While block_write_full_page is writing back the dirty buffers under
1198 * the page lock, whoever dirtied the buffers may decide to clean them
1199 * again at any time. We handle that by only looking at the buffer
1200 * state inside lock_buffer().
1202 * If block_write_full_page() is called for regular writeback
1203 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1204 * locked buffer. This only can happen if someone has written the buffer
1205 * directly, with submit_bh(). At the address_space level PageWriteback
1206 * prevents this contention from occurring.
1208 static int __btrfs_write_full_page(struct inode
*inode
, struct page
*page
,
1209 struct writeback_control
*wbc
)
1213 sector_t last_block
;
1214 struct buffer_head
*bh
, *head
;
1215 const unsigned blocksize
= 1 << inode
->i_blkbits
;
1216 int nr_underway
= 0;
1218 BUG_ON(!PageLocked(page
));
1220 last_block
= (i_size_read(inode
) - 1) >> inode
->i_blkbits
;
1222 if (!page_has_buffers(page
)) {
1223 create_empty_buffers(page
, blocksize
,
1224 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
1228 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1229 * here, and the (potentially unmapped) buffers may become dirty at
1230 * any time. If a buffer becomes dirty here after we've inspected it
1231 * then we just miss that fact, and the page stays dirty.
1233 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1234 * handle that here by just cleaning them.
1237 block
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
1238 head
= page_buffers(page
);
1242 * Get all the dirty buffers mapped to disk addresses and
1243 * handle any aliases from the underlying blockdev's mapping.
1246 if (block
> last_block
) {
1248 * mapped buffers outside i_size will occur, because
1249 * this page can be outside i_size when there is a
1250 * truncate in progress.
1253 * The buffer was zeroed by block_write_full_page()
1255 clear_buffer_dirty(bh
);
1256 set_buffer_uptodate(bh
);
1257 } else if (!buffer_mapped(bh
) && buffer_dirty(bh
)) {
1258 WARN_ON(bh
->b_size
!= blocksize
);
1259 err
= btrfs_get_block(inode
, block
, bh
, 0);
1261 printk("writepage going to recovery err %d\n", err
);
1264 if (buffer_new(bh
)) {
1265 /* blockdev mappings never come here */
1266 clear_buffer_new(bh
);
1269 bh
= bh
->b_this_page
;
1271 } while (bh
!= head
);
1274 if (!buffer_mapped(bh
))
1277 * If it's a fully non-blocking write attempt and we cannot
1278 * lock the buffer then redirty the page. Note that this can
1279 * potentially cause a busy-wait loop from pdflush and kswapd
1280 * activity, but those code paths have their own higher-level
1283 if (wbc
->sync_mode
!= WB_SYNC_NONE
|| !wbc
->nonblocking
) {
1285 } else if (test_set_buffer_locked(bh
)) {
1286 redirty_page_for_writepage(wbc
, page
);
1289 if (test_clear_buffer_dirty(bh
) && bh
->b_blocknr
!= 0) {
1290 mark_buffer_async_write(bh
);
1294 } while ((bh
= bh
->b_this_page
) != head
);
1297 * The page and its buffers are protected by PageWriteback(), so we can
1298 * drop the bh refcounts early.
1300 BUG_ON(PageWriteback(page
));
1301 set_page_writeback(page
);
1304 struct buffer_head
*next
= bh
->b_this_page
;
1305 if (buffer_async_write(bh
)) {
1306 submit_bh(WRITE
, bh
);
1310 } while (bh
!= head
);
1315 if (nr_underway
== 0) {
1317 * The page was marked dirty, but the buffers were
1318 * clean. Someone wrote them back by hand with
1319 * ll_rw_block/submit_bh. A rare case.
1323 if (!buffer_uptodate(bh
)) {
1327 bh
= bh
->b_this_page
;
1328 } while (bh
!= head
);
1330 SetPageUptodate(page
);
1331 end_page_writeback(page
);
1337 * ENOSPC, or some other error. We may already have added some
1338 * blocks to the file, so we need to write these out to avoid
1339 * exposing stale data.
1340 * The page is currently locked and not marked for writeback
1343 /* Recovery: lock and submit the mapped buffers */
1345 if (buffer_mapped(bh
) && buffer_dirty(bh
)) {
1347 mark_buffer_async_write(bh
);
1350 * The buffer may have been set dirty during
1351 * attachment to a dirty page.
1353 clear_buffer_dirty(bh
);
1355 } while ((bh
= bh
->b_this_page
) != head
);
1357 BUG_ON(PageWriteback(page
));
1358 set_page_writeback(page
);
1360 struct buffer_head
*next
= bh
->b_this_page
;
1361 if (buffer_async_write(bh
)) {
1362 clear_buffer_dirty(bh
);
1363 submit_bh(WRITE
, bh
);
1367 } while (bh
!= head
);
1373 * The generic ->writepage function for buffer-backed address_spaces
1375 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1377 struct inode
* const inode
= page
->mapping
->host
;
1378 loff_t i_size
= i_size_read(inode
);
1379 const pgoff_t end_index
= i_size
>> PAGE_CACHE_SHIFT
;
1383 /* Is the page fully inside i_size? */
1384 if (page
->index
< end_index
)
1385 return __btrfs_write_full_page(inode
, page
, wbc
);
1387 /* Is the page fully outside i_size? (truncate in progress) */
1388 offset
= i_size
& (PAGE_CACHE_SIZE
-1);
1389 if (page
->index
>= end_index
+1 || !offset
) {
1391 * The page may have dirty, unmapped buffers. For example,
1392 * they may have been added in ext3_writepage(). Make them
1393 * freeable here, so the page does not leak.
1395 block_invalidatepage(page
, 0);
1397 return 0; /* don't care */
1401 * The page straddles i_size. It must be zeroed out on each and every
1402 * writepage invokation because it may be mmapped. "A file is mapped
1403 * in multiples of the page size. For a file that is not a multiple of
1404 * the page size, the remaining memory is zeroed when mapped, and
1405 * writes to that region are not written out to the file."
1407 kaddr
= kmap_atomic(page
, KM_USER0
);
1408 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
1409 flush_dcache_page(page
);
1410 kunmap_atomic(kaddr
, KM_USER0
);
1411 return __btrfs_write_full_page(inode
, page
, wbc
);
1414 static void btrfs_truncate(struct inode
*inode
)
1416 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1418 struct btrfs_trans_handle
*trans
;
1420 if (!S_ISREG(inode
->i_mode
))
1422 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1425 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
1427 /* FIXME, add redo link to tree so we don't leak on crash */
1428 mutex_lock(&root
->fs_info
->fs_mutex
);
1429 trans
= btrfs_start_transaction(root
, 1);
1430 btrfs_set_trans_block_group(trans
, inode
);
1431 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1433 btrfs_update_inode(trans
, root
, inode
);
1434 ret
= btrfs_end_transaction(trans
, root
);
1436 mutex_unlock(&root
->fs_info
->fs_mutex
);
1437 btrfs_btree_balance_dirty(root
);
1441 * Make sure any changes to nobh_commit_write() are reflected in
1442 * nobh_truncate_page(), since it doesn't call commit_write().
1444 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1445 unsigned from
, unsigned to
)
1447 struct inode
*inode
= page
->mapping
->host
;
1448 struct buffer_head
*bh
;
1449 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
1451 SetPageUptodate(page
);
1452 bh
= page_buffers(page
);
1453 if (buffer_mapped(bh
) && bh
->b_blocknr
!= 0) {
1454 set_page_dirty(page
);
1456 if (pos
> inode
->i_size
) {
1457 i_size_write(inode
, pos
);
1458 mark_inode_dirty(inode
);
1463 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1464 struct page
**prepared_pages
,
1465 const char __user
* buf
)
1467 long page_fault
= 0;
1469 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1471 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1472 size_t count
= min_t(size_t,
1473 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1474 struct page
*page
= prepared_pages
[i
];
1475 fault_in_pages_readable(buf
, count
);
1477 /* Copy data from userspace to the current page */
1479 page_fault
= __copy_from_user(page_address(page
) + offset
,
1481 /* Flush processor's dcache for this page */
1482 flush_dcache_page(page
);
1485 write_bytes
-= count
;
1490 return page_fault
? -EFAULT
: 0;
1493 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1496 for (i
= 0; i
< num_pages
; i
++) {
1499 unlock_page(pages
[i
]);
1500 mark_page_accessed(pages
[i
]);
1501 page_cache_release(pages
[i
]);
1504 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1505 struct btrfs_root
*root
,
1507 struct page
**pages
,
1517 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1518 struct buffer_head
*bh
;
1519 struct btrfs_file_extent_item
*ei
;
1521 for (i
= 0; i
< num_pages
; i
++) {
1522 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1523 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1524 /* FIXME, one block at a time */
1526 mutex_lock(&root
->fs_info
->fs_mutex
);
1527 trans
= btrfs_start_transaction(root
, 1);
1528 btrfs_set_trans_block_group(trans
, inode
);
1530 bh
= page_buffers(pages
[i
]);
1531 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
1532 struct btrfs_key key
;
1533 struct btrfs_path
*path
;
1537 path
= btrfs_alloc_path();
1539 key
.objectid
= inode
->i_ino
;
1540 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
1542 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
1543 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
1545 btrfs_file_extent_calc_inline_size(write_bytes
);
1546 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1549 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1550 path
->slots
[0], struct btrfs_file_extent_item
);
1551 btrfs_set_file_extent_generation(ei
, trans
->transid
);
1552 btrfs_set_file_extent_type(ei
,
1553 BTRFS_FILE_EXTENT_INLINE
);
1554 ptr
= btrfs_file_extent_inline_start(ei
);
1555 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
1556 ptr
, bh
->b_data
, offset
+ write_bytes
);
1557 mark_buffer_dirty(path
->nodes
[0]);
1558 btrfs_free_path(path
);
1560 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1561 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1562 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1565 SetPageChecked(pages
[i
]);
1566 // btrfs_update_inode_block_group(trans, inode);
1567 ret
= btrfs_end_transaction(trans
, root
);
1569 mutex_unlock(&root
->fs_info
->fs_mutex
);
1571 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
1572 offset
+ this_write
);
1578 WARN_ON(this_write
> write_bytes
);
1579 write_bytes
-= this_write
;
1585 static int drop_extents(struct btrfs_trans_handle
*trans
,
1586 struct btrfs_root
*root
,
1587 struct inode
*inode
,
1588 u64 start
, u64 end
, u64
*hint_block
)
1591 struct btrfs_key key
;
1592 struct btrfs_leaf
*leaf
;
1594 struct btrfs_file_extent_item
*extent
;
1597 struct btrfs_file_extent_item old
;
1598 struct btrfs_path
*path
;
1599 u64 search_start
= start
;
1605 path
= btrfs_alloc_path();
1609 btrfs_release_path(root
, path
);
1610 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1615 if (path
->slots
[0] == 0) {
1626 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1627 slot
= path
->slots
[0];
1628 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
1629 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
1633 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
1637 extent
= btrfs_item_ptr(leaf
, slot
,
1638 struct btrfs_file_extent_item
);
1639 found_type
= btrfs_file_extent_type(extent
);
1640 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
1641 extent_end
= key
.offset
+
1642 (btrfs_file_extent_num_blocks(extent
) <<
1645 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
1647 extent_end
= key
.offset
+
1648 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
1651 if (!found_extent
&& !found_inline
) {
1656 if (search_start
>= extent_end
) {
1661 search_start
= extent_end
;
1663 if (end
< extent_end
&& end
>= key
.offset
) {
1665 memcpy(&old
, extent
, sizeof(old
));
1666 ret
= btrfs_inc_extent_ref(trans
, root
,
1667 btrfs_file_extent_disk_blocknr(&old
),
1668 btrfs_file_extent_disk_num_blocks(&old
));
1671 WARN_ON(found_inline
);
1675 if (start
> key
.offset
) {
1678 /* truncate existing extent */
1680 WARN_ON(start
& (root
->blocksize
- 1));
1682 new_num
= (start
- key
.offset
) >>
1684 old_num
= btrfs_file_extent_num_blocks(extent
);
1686 btrfs_file_extent_disk_blocknr(extent
);
1687 inode
->i_blocks
-= (old_num
- new_num
) << 3;
1688 btrfs_set_file_extent_num_blocks(extent
,
1690 mark_buffer_dirty(path
->nodes
[0]);
1696 u64 disk_blocknr
= 0;
1697 u64 disk_num_blocks
= 0;
1698 u64 extent_num_blocks
= 0;
1701 btrfs_file_extent_disk_blocknr(extent
);
1703 btrfs_file_extent_disk_num_blocks(extent
);
1705 btrfs_file_extent_num_blocks(extent
);
1707 btrfs_file_extent_disk_blocknr(extent
);
1709 ret
= btrfs_del_item(trans
, root
, path
);
1711 btrfs_release_path(root
, path
);
1714 inode
->i_blocks
-= extent_num_blocks
<< 3;
1715 ret
= btrfs_free_extent(trans
, root
,
1717 disk_num_blocks
, 0);
1721 if (!bookend
&& search_start
>= end
) {
1728 if (bookend
&& found_extent
) {
1729 /* create bookend */
1730 struct btrfs_key ins
;
1731 ins
.objectid
= inode
->i_ino
;
1734 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
1736 btrfs_release_path(root
, path
);
1737 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1740 extent
= btrfs_item_ptr(
1741 btrfs_buffer_leaf(path
->nodes
[0]),
1743 struct btrfs_file_extent_item
);
1744 btrfs_set_file_extent_disk_blocknr(extent
,
1745 btrfs_file_extent_disk_blocknr(&old
));
1746 btrfs_set_file_extent_disk_num_blocks(extent
,
1747 btrfs_file_extent_disk_num_blocks(&old
));
1749 btrfs_set_file_extent_offset(extent
,
1750 btrfs_file_extent_offset(&old
) +
1751 ((end
- key
.offset
) >> inode
->i_blkbits
));
1752 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
1753 (end
- key
.offset
) >> inode
->i_blkbits
);
1754 btrfs_set_file_extent_num_blocks(extent
,
1755 btrfs_file_extent_num_blocks(&old
) -
1756 ((end
- key
.offset
) >> inode
->i_blkbits
));
1758 btrfs_set_file_extent_type(extent
,
1759 BTRFS_FILE_EXTENT_REG
);
1760 btrfs_set_file_extent_generation(extent
,
1761 btrfs_file_extent_generation(&old
));
1762 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1764 btrfs_file_extent_num_blocks(extent
) << 3;
1770 btrfs_free_path(path
);
1774 static int prepare_pages(struct btrfs_root
*root
,
1776 struct page
**pages
,
1779 unsigned long first_index
,
1780 unsigned long last_index
,
1782 u64 alloc_extent_start
)
1785 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1786 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1790 struct buffer_head
*bh
;
1791 struct buffer_head
*head
;
1792 loff_t isize
= i_size_read(inode
);
1794 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1796 for (i
= 0; i
< num_pages
; i
++) {
1797 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1800 goto failed_release
;
1802 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
1803 wait_on_page_writeback(pages
[i
]);
1804 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1805 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1806 if (!page_has_buffers(pages
[i
])) {
1807 create_empty_buffers(pages
[i
],
1808 root
->fs_info
->sb
->s_blocksize
,
1809 (1 << BH_Uptodate
));
1811 head
= page_buffers(pages
[i
]);
1814 err
= btrfs_map_bh_to_logical(root
, bh
,
1815 alloc_extent_start
);
1818 goto failed_truncate
;
1819 bh
= bh
->b_this_page
;
1820 if (alloc_extent_start
)
1821 alloc_extent_start
++;
1822 } while (bh
!= head
);
1824 WARN_ON(this_write
> write_bytes
);
1825 write_bytes
-= this_write
;
1830 btrfs_drop_pages(pages
, num_pages
);
1834 btrfs_drop_pages(pages
, num_pages
);
1836 vmtruncate(inode
, isize
);
1840 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1841 size_t count
, loff_t
*ppos
)
1844 size_t num_written
= 0;
1847 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1848 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1849 struct page
*pages
[8];
1850 struct page
*pinned
[2];
1851 unsigned long first_index
;
1852 unsigned long last_index
;
1855 u64 alloc_extent_start
;
1857 struct btrfs_trans_handle
*trans
;
1858 struct btrfs_key ins
;
1861 if (file
->f_flags
& O_DIRECT
)
1864 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1865 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1866 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1871 err
= remove_suid(file
->f_path
.dentry
);
1874 file_update_time(file
);
1876 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
1877 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
1880 mutex_lock(&inode
->i_mutex
);
1881 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1882 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
1884 if ((first_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1885 (pos
& (PAGE_CACHE_SIZE
- 1))) {
1886 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
1887 if (!PageUptodate(pinned
[0])) {
1888 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
1890 wait_on_page_locked(pinned
[0]);
1892 unlock_page(pinned
[0]);
1895 if (first_index
!= last_index
&&
1896 (last_index
<< PAGE_CACHE_SHIFT
) < inode
->i_size
&&
1897 pos
+ count
< inode
->i_size
&&
1898 (count
& (PAGE_CACHE_SIZE
- 1))) {
1899 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
1900 if (!PageUptodate(pinned
[1])) {
1901 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
1903 wait_on_page_locked(pinned
[1]);
1905 unlock_page(pinned
[1]);
1909 mutex_lock(&root
->fs_info
->fs_mutex
);
1910 trans
= btrfs_start_transaction(root
, 1);
1913 mutex_unlock(&root
->fs_info
->fs_mutex
);
1916 btrfs_set_trans_block_group(trans
, inode
);
1917 /* FIXME blocksize != 4096 */
1918 inode
->i_blocks
+= num_blocks
<< 3;
1920 if (start_pos
< inode
->i_size
) {
1921 /* FIXME blocksize != pagesize */
1922 ret
= drop_extents(trans
, root
, inode
,
1924 (pos
+ count
+ root
->blocksize
-1) &
1925 ~((u64
)root
->blocksize
- 1), &hint_block
);
1928 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
1929 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
1930 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
1931 num_blocks
, hint_block
, (u64
)-1,
1934 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1935 start_pos
, ins
.objectid
, ins
.offset
);
1942 alloc_extent_start
= ins
.objectid
;
1943 // btrfs_update_inode_block_group(trans, inode);
1944 ret
= btrfs_end_transaction(trans
, root
);
1945 mutex_unlock(&root
->fs_info
->fs_mutex
);
1948 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1949 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1950 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1953 memset(pages
, 0, sizeof(pages
));
1954 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1955 pos
, first_index
, last_index
,
1956 write_bytes
, alloc_extent_start
);
1959 /* FIXME blocks != pagesize */
1960 if (alloc_extent_start
)
1961 alloc_extent_start
+= num_pages
;
1962 ret
= btrfs_copy_from_user(pos
, num_pages
,
1963 write_bytes
, pages
, buf
);
1966 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1967 num_pages
, pos
, write_bytes
);
1969 btrfs_drop_pages(pages
, num_pages
);
1972 count
-= write_bytes
;
1974 num_written
+= write_bytes
;
1976 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1977 btrfs_btree_balance_dirty(root
);
1981 mutex_unlock(&inode
->i_mutex
);
1984 page_cache_release(pinned
[0]);
1986 page_cache_release(pinned
[1]);
1988 current
->backing_dev_info
= NULL
;
1989 mark_inode_dirty(inode
);
1990 return num_written
? num_written
: err
;
1993 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1994 unsigned long offset
, unsigned long size
)
1997 unsigned long left
, count
= desc
->count
;
1998 struct inode
*inode
= page
->mapping
->host
;
2003 if (!PageChecked(page
)) {
2004 /* FIXME, do it per block */
2005 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2007 int ret
= btrfs_csum_verify_file_block(root
,
2008 page
->mapping
->host
->i_ino
,
2009 page
->index
<< PAGE_CACHE_SHIFT
,
2010 kmap(page
), PAGE_CACHE_SIZE
);
2012 printk("failed to verify ino %lu page %lu\n",
2013 page
->mapping
->host
->i_ino
,
2015 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
2017 SetPageChecked(page
);
2021 * Faults on the destination of a read are common, so do it before
2024 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
2025 kaddr
= kmap_atomic(page
, KM_USER0
);
2026 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
2027 kaddr
+ offset
, size
);
2028 kunmap_atomic(kaddr
, KM_USER0
);
2033 /* Do it the slow way */
2035 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
2040 desc
->error
= -EFAULT
;
2043 desc
->count
= count
- size
;
2044 desc
->written
+= size
;
2045 desc
->arg
.buf
+= size
;
2050 * btrfs_file_aio_read - filesystem read routine
2051 * @iocb: kernel I/O control block
2052 * @iov: io vector request
2053 * @nr_segs: number of segments in the iovec
2054 * @pos: current file position
2056 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2057 unsigned long nr_segs
, loff_t pos
)
2059 struct file
*filp
= iocb
->ki_filp
;
2063 loff_t
*ppos
= &iocb
->ki_pos
;
2066 for (seg
= 0; seg
< nr_segs
; seg
++) {
2067 const struct iovec
*iv
= &iov
[seg
];
2070 * If any segment has a negative length, or the cumulative
2071 * length ever wraps negative then return -EINVAL.
2073 count
+= iv
->iov_len
;
2074 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
2076 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
2081 count
-= iv
->iov_len
; /* This segment is no good */
2086 for (seg
= 0; seg
< nr_segs
; seg
++) {
2087 read_descriptor_t desc
;
2090 desc
.arg
.buf
= iov
[seg
].iov_base
;
2091 desc
.count
= iov
[seg
].iov_len
;
2092 if (desc
.count
== 0)
2095 do_generic_file_read(filp
, ppos
, &desc
,
2097 retval
+= desc
.written
;
2099 retval
= retval
?: desc
.error
;
2107 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
2109 struct btrfs_trans_handle
*trans
;
2110 struct btrfs_key key
;
2111 struct btrfs_root_item root_item
;
2112 struct btrfs_inode_item
*inode_item
;
2113 struct buffer_head
*subvol
;
2114 struct btrfs_leaf
*leaf
;
2115 struct btrfs_root
*new_root
;
2116 struct inode
*inode
;
2120 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
2122 mutex_lock(&root
->fs_info
->fs_mutex
);
2123 trans
= btrfs_start_transaction(root
, 1);
2126 subvol
= btrfs_alloc_free_block(trans
, root
, 0);
2129 leaf
= btrfs_buffer_leaf(subvol
);
2130 btrfs_set_header_nritems(&leaf
->header
, 0);
2131 btrfs_set_header_level(&leaf
->header
, 0);
2132 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
2133 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
2134 btrfs_set_header_owner(&leaf
->header
, root
->root_key
.objectid
);
2135 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
2136 sizeof(leaf
->header
.fsid
));
2137 mark_buffer_dirty(subvol
);
2139 inode_item
= &root_item
.inode
;
2140 memset(inode_item
, 0, sizeof(*inode_item
));
2141 btrfs_set_inode_generation(inode_item
, 1);
2142 btrfs_set_inode_size(inode_item
, 3);
2143 btrfs_set_inode_nlink(inode_item
, 1);
2144 btrfs_set_inode_nblocks(inode_item
, 1);
2145 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
2147 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
2148 btrfs_set_root_refs(&root_item
, 1);
2152 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2156 btrfs_set_root_dirid(&root_item
, new_dirid
);
2158 key
.objectid
= objectid
;
2161 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2162 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2167 * insert the directory item
2169 key
.offset
= (u64
)-1;
2170 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
2171 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2172 name
, namelen
, dir
->i_ino
, &key
, 0);
2175 ret
= btrfs_commit_transaction(trans
, root
);
2178 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
2181 trans
= btrfs_start_transaction(new_root
, 1);
2184 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
,
2185 BTRFS_I(dir
)->block_group
, S_IFDIR
| 0700);
2186 inode
->i_op
= &btrfs_dir_inode_operations
;
2187 inode
->i_fop
= &btrfs_dir_file_operations
;
2189 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
2194 ret
= btrfs_update_inode(trans
, new_root
, inode
);
2197 ret
= btrfs_commit_transaction(trans
, new_root
);
2202 mutex_unlock(&root
->fs_info
->fs_mutex
);
2203 btrfs_btree_balance_dirty(root
);
2207 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
2209 struct btrfs_trans_handle
*trans
;
2210 struct btrfs_key key
;
2211 struct btrfs_root_item new_root_item
;
2215 if (!root
->ref_cows
)
2218 mutex_lock(&root
->fs_info
->fs_mutex
);
2219 trans
= btrfs_start_transaction(root
, 1);
2222 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
2225 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
2229 memcpy(&new_root_item
, &root
->root_item
,
2230 sizeof(new_root_item
));
2232 key
.objectid
= objectid
;
2235 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2236 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2238 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2243 * insert the directory item
2245 key
.offset
= (u64
)-1;
2246 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2248 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2253 ret
= btrfs_inc_root_ref(trans
, root
);
2256 ret
= btrfs_commit_transaction(trans
, root
);
2258 mutex_unlock(&root
->fs_info
->fs_mutex
);
2259 btrfs_btree_balance_dirty(root
);
2263 static int add_disk(struct btrfs_root
*root
, char *name
, int namelen
)
2265 struct block_device
*bdev
;
2266 struct btrfs_path
*path
;
2267 struct super_block
*sb
= root
->fs_info
->sb
;
2268 struct btrfs_root
*dev_root
= root
->fs_info
->dev_root
;
2269 struct btrfs_trans_handle
*trans
;
2270 struct btrfs_device_item
*dev_item
;
2271 struct btrfs_key key
;
2278 printk("adding disk %s\n", name
);
2279 path
= btrfs_alloc_path();
2282 num_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
2283 bdev
= open_bdev_excl(name
, O_RDWR
, sb
);
2285 ret
= PTR_ERR(bdev
);
2286 printk("open bdev excl failed ret %d\n", ret
);
2289 set_blocksize(bdev
, sb
->s_blocksize
);
2290 new_blocks
= bdev
->bd_inode
->i_size
>> sb
->s_blocksize_bits
;
2291 key
.objectid
= num_blocks
;
2292 key
.offset
= new_blocks
;
2294 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
2296 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
2297 trans
= btrfs_start_transaction(dev_root
, 1);
2298 item_size
= sizeof(*dev_item
) + namelen
;
2299 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks
, new_blocks
, key
.flags
, item_size
);
2300 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
, &key
, item_size
);
2302 printk("insert failed %d\n", ret
);
2303 close_bdev_excl(bdev
);
2308 dev_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2309 path
->slots
[0], struct btrfs_device_item
);
2310 btrfs_set_device_pathlen(dev_item
, namelen
);
2311 memcpy(dev_item
+ 1, name
, namelen
);
2313 device_id
= btrfs_super_last_device_id(root
->fs_info
->disk_super
) + 1;
2314 btrfs_set_super_last_device_id(root
->fs_info
->disk_super
, device_id
);
2315 btrfs_set_device_id(dev_item
, device_id
);
2316 mark_buffer_dirty(path
->nodes
[0]);
2318 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
, num_blocks
,
2322 btrfs_set_super_total_blocks(root
->fs_info
->disk_super
,
2323 num_blocks
+ new_blocks
);
2324 i_size_write(root
->fs_info
->btree_inode
,
2325 (num_blocks
+ new_blocks
) <<
2326 root
->fs_info
->btree_inode
->i_blkbits
);
2330 ret
= btrfs_commit_transaction(trans
, dev_root
);
2332 mutex_unlock(&root
->fs_info
->fs_mutex
);
2334 btrfs_free_path(path
);
2335 btrfs_btree_balance_dirty(root
);
2340 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2341 cmd
, unsigned long arg
)
2343 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2344 struct btrfs_ioctl_vol_args vol_args
;
2346 struct btrfs_dir_item
*di
;
2348 struct btrfs_path
*path
;
2352 case BTRFS_IOC_SNAP_CREATE
:
2353 if (copy_from_user(&vol_args
,
2354 (struct btrfs_ioctl_vol_args __user
*)arg
,
2357 namelen
= strlen(vol_args
.name
);
2358 if (namelen
> BTRFS_VOL_NAME_MAX
)
2360 path
= btrfs_alloc_path();
2363 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2364 mutex_lock(&root
->fs_info
->fs_mutex
);
2365 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2367 vol_args
.name
, namelen
, 0);
2368 mutex_unlock(&root
->fs_info
->fs_mutex
);
2369 btrfs_free_path(path
);
2370 if (di
&& !IS_ERR(di
))
2373 if (root
== root
->fs_info
->tree_root
)
2374 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2376 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2379 case BTRFS_IOC_ADD_DISK
:
2380 if (copy_from_user(&vol_args
,
2381 (struct btrfs_ioctl_vol_args __user
*)arg
,
2384 namelen
= strlen(vol_args
.name
);
2385 if (namelen
> BTRFS_VOL_NAME_MAX
)
2387 vol_args
.name
[namelen
] = '\0';
2388 ret
= add_disk(root
, vol_args
.name
, namelen
);
2396 static struct kmem_cache
*btrfs_inode_cachep
;
2397 struct kmem_cache
*btrfs_trans_handle_cachep
;
2398 struct kmem_cache
*btrfs_transaction_cachep
;
2399 struct kmem_cache
*btrfs_bit_radix_cachep
;
2400 struct kmem_cache
*btrfs_path_cachep
;
2403 * Called inside transaction, so use GFP_NOFS
2405 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2407 struct btrfs_inode
*ei
;
2409 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2412 return &ei
->vfs_inode
;
2415 static void btrfs_destroy_inode(struct inode
*inode
)
2417 WARN_ON(!list_empty(&inode
->i_dentry
));
2418 WARN_ON(inode
->i_data
.nrpages
);
2420 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2423 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2424 unsigned long flags
)
2426 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2428 if ((flags
& (SLAB_CTOR_CONSTRUCTOR
)) ==
2429 SLAB_CTOR_CONSTRUCTOR
) {
2430 inode_init_once(&ei
->vfs_inode
);
2434 static int init_inodecache(void)
2436 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
2437 sizeof(struct btrfs_inode
),
2438 0, (SLAB_RECLAIM_ACCOUNT
|
2441 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
2442 sizeof(struct btrfs_trans_handle
),
2443 0, (SLAB_RECLAIM_ACCOUNT
|
2446 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
2447 sizeof(struct btrfs_transaction
),
2448 0, (SLAB_RECLAIM_ACCOUNT
|
2451 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
2452 sizeof(struct btrfs_transaction
),
2453 0, (SLAB_RECLAIM_ACCOUNT
|
2456 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
2458 0, (SLAB_RECLAIM_ACCOUNT
|
2460 SLAB_DESTROY_BY_RCU
),
2462 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
2463 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
2468 static void destroy_inodecache(void)
2470 kmem_cache_destroy(btrfs_inode_cachep
);
2471 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2472 kmem_cache_destroy(btrfs_transaction_cachep
);
2473 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2474 kmem_cache_destroy(btrfs_path_cachep
);
2477 static int btrfs_get_sb(struct file_system_type
*fs_type
,
2478 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2480 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
2481 btrfs_fill_super
, mnt
);
2484 static int btrfs_getattr(struct vfsmount
*mnt
,
2485 struct dentry
*dentry
, struct kstat
*stat
)
2487 struct inode
*inode
= dentry
->d_inode
;
2488 generic_fillattr(inode
, stat
);
2489 stat
->blksize
= 256 * 1024;
2493 static int btrfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2495 struct btrfs_root
*root
= btrfs_sb(dentry
->d_sb
);
2496 struct btrfs_super_block
*disk_super
= root
->fs_info
->disk_super
;
2498 buf
->f_namelen
= BTRFS_NAME_LEN
;
2499 buf
->f_blocks
= btrfs_super_total_blocks(disk_super
);
2500 buf
->f_bfree
= buf
->f_blocks
- btrfs_super_blocks_used(disk_super
);
2501 buf
->f_bavail
= buf
->f_bfree
;
2502 buf
->f_bsize
= dentry
->d_sb
->s_blocksize
;
2503 buf
->f_type
= BTRFS_SUPER_MAGIC
;
2507 static struct file_system_type btrfs_fs_type
= {
2508 .owner
= THIS_MODULE
,
2510 .get_sb
= btrfs_get_sb
,
2511 .kill_sb
= kill_block_super
,
2512 .fs_flags
= FS_REQUIRES_DEV
,
2515 static struct super_operations btrfs_super_ops
= {
2516 .delete_inode
= btrfs_delete_inode
,
2517 .put_super
= btrfs_put_super
,
2518 .read_inode
= btrfs_read_locked_inode
,
2519 .write_super
= btrfs_write_super
,
2520 .sync_fs
= btrfs_sync_fs
,
2521 .write_inode
= btrfs_write_inode
,
2522 .dirty_inode
= btrfs_dirty_inode
,
2523 .alloc_inode
= btrfs_alloc_inode
,
2524 .destroy_inode
= btrfs_destroy_inode
,
2525 .statfs
= btrfs_statfs
,
2528 static struct inode_operations btrfs_dir_inode_operations
= {
2529 .lookup
= btrfs_lookup
,
2530 .create
= btrfs_create
,
2531 .unlink
= btrfs_unlink
,
2532 .mkdir
= btrfs_mkdir
,
2533 .rmdir
= btrfs_rmdir
,
2536 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2537 .lookup
= btrfs_lookup
,
2540 static struct file_operations btrfs_dir_file_operations
= {
2541 .llseek
= generic_file_llseek
,
2542 .read
= generic_read_dir
,
2543 .readdir
= btrfs_readdir
,
2544 .ioctl
= btrfs_ioctl
,
2547 static struct address_space_operations btrfs_aops
= {
2548 .readpage
= btrfs_readpage
,
2549 .writepage
= btrfs_writepage
,
2550 .sync_page
= block_sync_page
,
2551 .prepare_write
= btrfs_prepare_write
,
2552 .commit_write
= btrfs_commit_write
,
2555 static struct inode_operations btrfs_file_inode_operations
= {
2556 .truncate
= btrfs_truncate
,
2557 .getattr
= btrfs_getattr
,
2560 static struct file_operations btrfs_file_operations
= {
2561 .llseek
= generic_file_llseek
,
2562 .read
= do_sync_read
,
2563 .aio_read
= btrfs_file_aio_read
,
2564 .write
= btrfs_file_write
,
2565 .mmap
= generic_file_mmap
,
2566 .open
= generic_file_open
,
2567 .ioctl
= btrfs_ioctl
,
2568 .fsync
= btrfs_sync_file
,
2571 static int __init
init_btrfs_fs(void)
2574 printk("btrfs loaded!\n");
2575 err
= init_inodecache();
2578 return register_filesystem(&btrfs_fs_type
);
2579 destroy_inodecache();
2583 static void __exit
exit_btrfs_fs(void)
2585 destroy_inodecache();
2586 unregister_filesystem(&btrfs_fs_type
);
2587 printk("btrfs unloaded\n");
2590 module_init(init_btrfs_fs
)
2591 module_exit(exit_btrfs_fs
)
2593 MODULE_LICENSE("GPL");