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>
16 #include "transaction.h"
17 #include "btrfs_inode.h"
20 void btrfs_fsinfo_release(struct kobject
*obj
)
22 struct btrfs_fs_info
*fsinfo
= container_of(obj
,
23 struct btrfs_fs_info
, kobj
);
27 struct kobj_type btrfs_fsinfo_ktype
= {
28 .release
= btrfs_fsinfo_release
,
31 struct btrfs_iget_args
{
33 struct btrfs_root
*root
;
36 decl_subsys(btrfs
, &btrfs_fsinfo_ktype
, NULL
);
38 #define BTRFS_SUPER_MAGIC 0x9123682E
40 static struct inode_operations btrfs_dir_inode_operations
;
41 static struct inode_operations btrfs_dir_ro_inode_operations
;
42 static struct super_operations btrfs_super_ops
;
43 static struct file_operations btrfs_dir_file_operations
;
44 static struct inode_operations btrfs_file_inode_operations
;
45 static struct address_space_operations btrfs_aops
;
46 static struct file_operations btrfs_file_operations
;
48 static void btrfs_read_locked_inode(struct inode
*inode
)
50 struct btrfs_path
*path
;
51 struct btrfs_inode_item
*inode_item
;
52 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
53 struct btrfs_key location
;
56 path
= btrfs_alloc_path();
58 btrfs_init_path(path
);
59 mutex_lock(&root
->fs_info
->fs_mutex
);
61 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
62 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
64 btrfs_free_path(path
);
67 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
69 struct btrfs_inode_item
);
71 inode
->i_mode
= btrfs_inode_mode(inode_item
);
72 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
73 inode
->i_uid
= btrfs_inode_uid(inode_item
);
74 inode
->i_gid
= btrfs_inode_gid(inode_item
);
75 inode
->i_size
= btrfs_inode_size(inode_item
);
76 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
77 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
78 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
79 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
80 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
81 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
82 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
83 inode
->i_generation
= btrfs_inode_generation(inode_item
);
85 btrfs_free_path(path
);
88 mutex_unlock(&root
->fs_info
->fs_mutex
);
90 switch (inode
->i_mode
& S_IFMT
) {
93 init_special_inode(inode
, inode
->i_mode
,
94 btrfs_inode_rdev(inode_item
));
98 inode
->i_mapping
->a_ops
= &btrfs_aops
;
99 inode
->i_fop
= &btrfs_file_operations
;
100 inode
->i_op
= &btrfs_file_inode_operations
;
103 inode
->i_fop
= &btrfs_dir_file_operations
;
104 if (root
== root
->fs_info
->tree_root
)
105 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
107 inode
->i_op
= &btrfs_dir_inode_operations
;
110 // inode->i_op = &page_symlink_inode_operations;
116 btrfs_release_path(root
, path
);
117 btrfs_free_path(path
);
118 mutex_unlock(&root
->fs_info
->fs_mutex
);
119 make_bad_inode(inode
);
122 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
123 struct btrfs_root
*root
,
125 struct dentry
*dentry
)
127 struct btrfs_path
*path
;
128 const char *name
= dentry
->d_name
.name
;
129 int name_len
= dentry
->d_name
.len
;
132 struct btrfs_dir_item
*di
;
134 path
= btrfs_alloc_path();
136 btrfs_init_path(path
);
137 ret
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
145 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
146 struct btrfs_dir_item
);
147 objectid
= btrfs_disk_key_objectid(&di
->location
);
149 ret
= btrfs_del_item(trans
, root
, path
);
152 btrfs_release_path(root
, path
);
153 ret
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
156 ret
= btrfs_del_item(trans
, root
, path
);
158 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
160 btrfs_release_path(root
, path
);
161 btrfs_free_path(path
);
163 inode_dec_link_count(dentry
->d_inode
);
164 dir
->i_size
-= name_len
* 2;
165 mark_inode_dirty(dir
);
170 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
172 struct btrfs_root
*root
;
173 struct btrfs_trans_handle
*trans
;
176 root
= BTRFS_I(dir
)->root
;
177 mutex_lock(&root
->fs_info
->fs_mutex
);
178 trans
= btrfs_start_transaction(root
, 1);
179 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
180 btrfs_end_transaction(trans
, root
);
181 mutex_unlock(&root
->fs_info
->fs_mutex
);
185 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
187 struct inode
*inode
= dentry
->d_inode
;
190 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
191 struct btrfs_path
*path
;
192 struct btrfs_key key
;
193 struct btrfs_trans_handle
*trans
;
194 struct btrfs_key found_key
;
196 struct btrfs_leaf
*leaf
;
197 char *goodnames
= "..";
199 path
= btrfs_alloc_path();
201 btrfs_init_path(path
);
202 mutex_lock(&root
->fs_info
->fs_mutex
);
203 trans
= btrfs_start_transaction(root
, 1);
204 key
.objectid
= inode
->i_ino
;
205 key
.offset
= (u64
)-1;
208 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
214 if (path
->slots
[0] == 0) {
219 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
220 btrfs_disk_key_to_cpu(&found_key
,
221 &leaf
->items
[path
->slots
[0]].key
);
222 found_type
= btrfs_key_type(&found_key
);
223 if (found_key
.objectid
!= inode
->i_ino
) {
227 if ((found_type
!= BTRFS_DIR_ITEM_KEY
&&
228 found_type
!= BTRFS_DIR_INDEX_KEY
) ||
229 (!btrfs_match_dir_item_name(root
, path
, goodnames
, 2) &&
230 !btrfs_match_dir_item_name(root
, path
, goodnames
, 1))) {
234 ret
= btrfs_del_item(trans
, root
, path
);
237 if (found_type
== BTRFS_DIR_ITEM_KEY
&& found_key
.offset
== 1)
239 btrfs_release_path(root
, path
);
242 btrfs_release_path(root
, path
);
244 /* now the directory is empty */
245 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
250 btrfs_release_path(root
, path
);
251 btrfs_free_path(path
);
252 mutex_unlock(&root
->fs_info
->fs_mutex
);
253 ret
= btrfs_end_transaction(trans
, root
);
259 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
260 struct btrfs_root
*root
,
263 struct btrfs_path
*path
;
268 path
= btrfs_alloc_path();
270 btrfs_init_path(path
);
271 ret
= btrfs_lookup_inode(trans
, root
, path
,
272 &BTRFS_I(inode
)->location
, -1);
274 ret
= btrfs_del_item(trans
, root
, path
);
276 btrfs_free_path(path
);
280 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
281 struct btrfs_root
*root
,
285 struct btrfs_path
*path
;
286 struct btrfs_key key
;
287 struct btrfs_disk_key
*found_key
;
288 struct btrfs_leaf
*leaf
;
289 struct btrfs_file_extent_item
*fi
= NULL
;
290 u64 extent_start
= 0;
291 u64 extent_num_blocks
= 0;
294 path
= btrfs_alloc_path();
296 /* FIXME, add redo link to tree so we don't leak on crash */
297 key
.objectid
= inode
->i_ino
;
298 key
.offset
= (u64
)-1;
301 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
304 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
306 btrfs_init_path(path
);
307 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
312 BUG_ON(path
->slots
[0] == 0);
315 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
316 found_key
= &leaf
->items
[path
->slots
[0]].key
;
317 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
319 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
320 btrfs_disk_key_type(found_key
) != BTRFS_INLINE_DATA_KEY
&&
321 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
323 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
325 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
326 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
328 struct btrfs_file_extent_item
);
329 extent_start
= btrfs_file_extent_disk_blocknr(fi
);
331 btrfs_file_extent_disk_num_blocks(fi
);
333 btrfs_file_extent_num_blocks(fi
) >> 9;
338 ret
= btrfs_del_item(trans
, root
, path
);
340 btrfs_release_path(root
, path
);
342 ret
= btrfs_free_extent(trans
, root
, extent_start
,
343 extent_num_blocks
, 0);
349 btrfs_release_path(root
, path
);
350 btrfs_free_path(path
);
354 static void btrfs_delete_inode(struct inode
*inode
)
356 struct btrfs_trans_handle
*trans
;
357 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
360 truncate_inode_pages(&inode
->i_data
, 0);
361 if (is_bad_inode(inode
)) {
365 mutex_lock(&root
->fs_info
->fs_mutex
);
366 trans
= btrfs_start_transaction(root
, 1);
367 if (S_ISREG(inode
->i_mode
)) {
368 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
371 btrfs_free_inode(trans
, root
, inode
);
372 btrfs_end_transaction(trans
, root
);
373 mutex_unlock(&root
->fs_info
->fs_mutex
);
379 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
380 struct btrfs_key
*location
)
382 const char *name
= dentry
->d_name
.name
;
383 int namelen
= dentry
->d_name
.len
;
384 struct btrfs_dir_item
*di
;
385 struct btrfs_path
*path
;
386 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
389 path
= btrfs_alloc_path();
391 btrfs_init_path(path
);
392 ret
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
394 if (ret
|| !btrfs_match_dir_item_name(root
, path
, name
, namelen
)) {
395 location
->objectid
= 0;
399 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
400 struct btrfs_dir_item
);
401 btrfs_disk_key_to_cpu(location
, &di
->location
);
403 btrfs_release_path(root
, path
);
404 btrfs_free_path(path
);
408 int fixup_tree_root_location(struct btrfs_root
*root
,
409 struct btrfs_key
*location
,
410 struct btrfs_root
**sub_root
)
412 struct btrfs_path
*path
;
413 struct btrfs_root_item
*ri
;
415 if (btrfs_key_type(location
) != BTRFS_ROOT_ITEM_KEY
)
417 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
420 path
= btrfs_alloc_path();
422 mutex_lock(&root
->fs_info
->fs_mutex
);
424 *sub_root
= btrfs_read_fs_root(root
->fs_info
, location
);
425 if (IS_ERR(*sub_root
))
426 return PTR_ERR(*sub_root
);
428 ri
= &(*sub_root
)->root_item
;
429 location
->objectid
= btrfs_root_dirid(ri
);
431 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
432 location
->offset
= 0;
434 btrfs_free_path(path
);
435 mutex_unlock(&root
->fs_info
->fs_mutex
);
439 int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
441 struct btrfs_iget_args
*args
= p
;
442 inode
->i_ino
= args
->ino
;
443 BTRFS_I(inode
)->root
= args
->root
;
447 int btrfs_find_actor(struct inode
*inode
, void *opaque
)
449 struct btrfs_iget_args
*args
= opaque
;
450 return (args
->ino
== inode
->i_ino
&&
451 args
->root
== BTRFS_I(inode
)->root
);
454 struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 objectid
,
455 struct btrfs_root
*root
)
458 struct btrfs_iget_args args
;
462 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
463 btrfs_init_locked_inode
,
468 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
469 struct nameidata
*nd
)
471 struct inode
* inode
;
472 struct btrfs_inode
*bi
= BTRFS_I(dir
);
473 struct btrfs_root
*root
= bi
->root
;
474 struct btrfs_root
*sub_root
= root
;
475 struct btrfs_key location
;
478 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
479 return ERR_PTR(-ENAMETOOLONG
);
480 mutex_lock(&root
->fs_info
->fs_mutex
);
481 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
482 mutex_unlock(&root
->fs_info
->fs_mutex
);
486 if (location
.objectid
) {
487 ret
= fixup_tree_root_location(root
, &location
, &sub_root
);
491 return ERR_PTR(-ENOENT
);
492 inode
= btrfs_iget_locked(dir
->i_sb
, location
.objectid
,
495 return ERR_PTR(-EACCES
);
496 if (inode
->i_state
& I_NEW
) {
497 if (sub_root
!= root
) {
498 printk("adding new root for inode %lu root %p (found %p)\n", inode
->i_ino
, sub_root
, BTRFS_I(inode
)->root
);
500 sub_root
->inode
= inode
;
502 BTRFS_I(inode
)->root
= sub_root
;
503 memcpy(&BTRFS_I(inode
)->location
, &location
,
505 btrfs_read_locked_inode(inode
);
506 unlock_new_inode(inode
);
509 return d_splice_alias(inode
, dentry
);
512 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
514 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
515 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
516 struct btrfs_item
*item
;
517 struct btrfs_dir_item
*di
;
518 struct btrfs_key key
;
519 struct btrfs_path
*path
;
522 struct btrfs_leaf
*leaf
;
525 unsigned char d_type
= DT_UNKNOWN
;
527 int key_type
= BTRFS_DIR_INDEX_KEY
;
529 /* FIXME, use a real flag for deciding about the key type */
530 if (root
->fs_info
->tree_root
== root
)
531 key_type
= BTRFS_DIR_ITEM_KEY
;
532 mutex_lock(&root
->fs_info
->fs_mutex
);
533 key
.objectid
= inode
->i_ino
;
535 btrfs_set_key_type(&key
, key_type
);
536 key
.offset
= filp
->f_pos
;
537 path
= btrfs_alloc_path();
538 btrfs_init_path(path
);
539 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
544 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
545 nritems
= btrfs_header_nritems(&leaf
->header
);
546 slot
= path
->slots
[0];
547 if (advance
|| slot
>= nritems
) {
548 if (slot
>= nritems
-1) {
549 ret
= btrfs_next_leaf(root
, path
);
552 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
553 nritems
= btrfs_header_nritems(&leaf
->header
);
554 slot
= path
->slots
[0];
561 item
= leaf
->items
+ slot
;
562 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
564 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
565 btrfs_disk_key_offset(&item
->key
) > root
->highest_inode
)
567 if (btrfs_disk_key_type(&item
->key
) != key_type
)
569 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
571 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
573 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
574 over
= filldir(dirent
, (const char *)(di
+ 1),
575 btrfs_dir_name_len(di
),
576 btrfs_disk_key_offset(&item
->key
),
577 btrfs_disk_key_objectid(&di
->location
), d_type
);
585 btrfs_release_path(root
, path
);
586 btrfs_free_path(path
);
587 mutex_unlock(&root
->fs_info
->fs_mutex
);
591 static void btrfs_put_super (struct super_block
* sb
)
593 struct btrfs_root
*root
= btrfs_sb(sb
);
596 ret
= close_ctree(root
);
598 printk("close ctree returns %d\n", ret
);
600 sb
->s_fs_info
= NULL
;
603 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
605 struct inode
* inode
;
606 struct dentry
* root_dentry
;
607 struct btrfs_super_block
*disk_super
;
608 struct btrfs_root
*tree_root
;
609 struct btrfs_inode
*bi
;
611 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
612 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
613 sb
->s_op
= &btrfs_super_ops
;
616 tree_root
= open_ctree(sb
);
619 printk("btrfs: open_ctree failed\n");
622 sb
->s_fs_info
= tree_root
;
623 disk_super
= tree_root
->fs_info
->disk_super
;
624 printk("read in super total blocks %Lu root %Lu\n",
625 btrfs_super_total_blocks(disk_super
),
626 btrfs_super_root_dir(disk_super
));
628 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
631 bi
->location
.objectid
= inode
->i_ino
;
632 bi
->location
.offset
= 0;
633 bi
->location
.flags
= 0;
634 bi
->root
= tree_root
;
635 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
639 if (inode
->i_state
& I_NEW
) {
640 btrfs_read_locked_inode(inode
);
641 unlock_new_inode(inode
);
644 root_dentry
= d_alloc_root(inode
);
649 sb
->s_root
= root_dentry
;
654 static void fill_inode_item(struct btrfs_inode_item
*item
,
657 btrfs_set_inode_uid(item
, inode
->i_uid
);
658 btrfs_set_inode_gid(item
, inode
->i_gid
);
659 btrfs_set_inode_size(item
, inode
->i_size
);
660 btrfs_set_inode_mode(item
, inode
->i_mode
);
661 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
662 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
663 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
664 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
665 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
666 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
667 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
668 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
669 btrfs_set_inode_generation(item
, inode
->i_generation
);
672 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
673 struct btrfs_root
*root
,
676 struct btrfs_inode_item
*inode_item
;
677 struct btrfs_path
*path
;
680 path
= btrfs_alloc_path();
682 btrfs_init_path(path
);
683 ret
= btrfs_lookup_inode(trans
, root
, path
,
684 &BTRFS_I(inode
)->location
, 1);
691 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
693 struct btrfs_inode_item
);
695 fill_inode_item(inode_item
, inode
);
696 btrfs_mark_buffer_dirty(path
->nodes
[0]);
699 btrfs_release_path(root
, path
);
700 btrfs_free_path(path
);
704 static int btrfs_write_inode(struct inode
*inode
, int wait
)
706 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
707 struct btrfs_trans_handle
*trans
;
710 mutex_lock(&root
->fs_info
->fs_mutex
);
711 trans
= btrfs_start_transaction(root
, 1);
712 ret
= btrfs_update_inode(trans
, root
, inode
);
714 btrfs_commit_transaction(trans
, root
);
716 btrfs_end_transaction(trans
, root
);
717 mutex_unlock(&root
->fs_info
->fs_mutex
);
721 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
722 struct btrfs_root
*root
,
723 u64 objectid
, int mode
)
726 struct btrfs_inode_item inode_item
;
727 struct btrfs_key
*location
;
730 inode
= new_inode(root
->fs_info
->sb
);
732 return ERR_PTR(-ENOMEM
);
734 BTRFS_I(inode
)->root
= root
;
736 inode
->i_uid
= current
->fsuid
;
737 inode
->i_gid
= current
->fsgid
;
738 inode
->i_mode
= mode
;
739 inode
->i_ino
= objectid
;
741 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
742 fill_inode_item(&inode_item
, inode
);
743 location
= &BTRFS_I(inode
)->location
;
744 location
->objectid
= objectid
;
746 location
->offset
= 0;
747 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
749 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
752 insert_inode_hash(inode
);
756 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
757 struct dentry
*dentry
, struct inode
*inode
)
760 struct btrfs_key key
;
761 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
762 key
.objectid
= inode
->i_ino
;
764 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
767 ret
= btrfs_insert_dir_item(trans
, root
,
768 dentry
->d_name
.name
, dentry
->d_name
.len
,
769 dentry
->d_parent
->d_inode
->i_ino
,
772 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
773 ret
= btrfs_update_inode(trans
, root
,
774 dentry
->d_parent
->d_inode
);
779 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
780 struct dentry
*dentry
, struct inode
*inode
)
782 int err
= btrfs_add_link(trans
, dentry
, inode
);
784 d_instantiate(dentry
, inode
);
792 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
793 int mode
, struct nameidata
*nd
)
795 struct btrfs_trans_handle
*trans
;
796 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
802 mutex_lock(&root
->fs_info
->fs_mutex
);
803 trans
= btrfs_start_transaction(root
, 1);
805 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
811 inode
= btrfs_new_inode(trans
, root
, objectid
, mode
);
812 err
= PTR_ERR(inode
);
815 // FIXME mark the inode dirty
816 err
= btrfs_add_nondir(trans
, dentry
, inode
);
820 inode
->i_mapping
->a_ops
= &btrfs_aops
;
821 inode
->i_fop
= &btrfs_file_operations
;
822 inode
->i_op
= &btrfs_file_inode_operations
;
824 dir
->i_sb
->s_dirt
= 1;
826 btrfs_end_transaction(trans
, root
);
827 mutex_unlock(&root
->fs_info
->fs_mutex
);
830 inode_dec_link_count(inode
);
836 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
837 struct btrfs_root
*root
,
838 u64 objectid
, u64 dirid
)
842 struct btrfs_key key
;
847 key
.objectid
= objectid
;
850 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
852 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
856 key
.objectid
= dirid
;
857 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
865 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
868 struct btrfs_trans_handle
*trans
;
869 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
874 mutex_lock(&root
->fs_info
->fs_mutex
);
875 trans
= btrfs_start_transaction(root
, 1);
877 err
= PTR_ERR(trans
);
881 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
887 inode
= btrfs_new_inode(trans
, root
, objectid
, S_IFDIR
| mode
);
889 err
= PTR_ERR(inode
);
893 inode
->i_op
= &btrfs_dir_inode_operations
;
894 inode
->i_fop
= &btrfs_dir_file_operations
;
896 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
901 err
= btrfs_update_inode(trans
, root
, inode
);
904 err
= btrfs_add_link(trans
, dentry
, inode
);
907 d_instantiate(dentry
, inode
);
911 btrfs_end_transaction(trans
, root
);
913 mutex_unlock(&root
->fs_info
->fs_mutex
);
919 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
921 struct btrfs_trans_handle
*trans
;
922 struct btrfs_root
*root
;
928 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
931 filemap_write_and_wait(root
->fs_info
->btree_inode
->i_mapping
);
932 mutex_lock(&root
->fs_info
->fs_mutex
);
933 trans
= btrfs_start_transaction(root
, 1);
934 ret
= btrfs_commit_transaction(trans
, root
);
937 printk("btrfs sync_fs\n");
938 mutex_unlock(&root
->fs_info
->fs_mutex
);
943 static int btrfs_get_block_inline(struct inode
*inode
, sector_t iblock
,
944 struct buffer_head
*result
, int create
)
946 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
947 struct btrfs_path
*path
;
948 struct btrfs_key key
;
949 struct btrfs_leaf
*leaf
;
950 int num_bytes
= result
->b_size
;
960 path
= btrfs_alloc_path();
967 pos
= iblock
<< inode
->i_blkbits
;
968 key
.objectid
= inode
->i_ino
;
970 btrfs_set_key_type(&key
, BTRFS_INLINE_DATA_KEY
);
971 ptr
= kmap(result
->b_page
);
973 ptr
+= (pos
& (PAGE_CACHE_SIZE
-1));
976 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
984 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
985 item_size
= btrfs_item_size(leaf
->items
+ path
->slots
[0]);
986 copy_size
= min(num_bytes
, item_size
);
987 data_ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
988 WARN_ON(safe_ptr
+ PAGE_CACHE_SIZE
< ptr
+ copy_size
);
989 memcpy(ptr
, data_ptr
, copy_size
);
991 num_bytes
-= copy_size
;
992 WARN_ON(num_bytes
< 0);
994 btrfs_release_path(root
, path
);
995 if (num_bytes
!= 0) {
996 if (pos
>= i_size_read(inode
))
997 memset(ptr
, 0, num_bytes
);
1001 set_buffer_uptodate(result
);
1002 map_bh(result
, inode
->i_sb
, 0);
1005 btrfs_free_path(path
);
1006 kunmap(result
->b_page
);
1011 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1012 struct buffer_head
*result
, int create
)
1017 u64 extent_start
= 0;
1019 u64 objectid
= inode
->i_ino
;
1020 struct btrfs_path
*path
;
1021 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1022 struct btrfs_trans_handle
*trans
= NULL
;
1023 struct btrfs_file_extent_item
*item
;
1024 struct btrfs_leaf
*leaf
;
1025 struct btrfs_disk_key
*found_key
;
1027 path
= btrfs_alloc_path();
1029 btrfs_init_path(path
);
1031 trans
= btrfs_start_transaction(root
, 1);
1033 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1035 iblock
<< inode
->i_blkbits
, create
);
1042 if (path
->slots
[0] == 0) {
1043 btrfs_release_path(root
, path
);
1049 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1050 struct btrfs_file_extent_item
);
1051 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1052 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1053 blocknr
+= btrfs_file_extent_offset(item
);
1055 /* exact match found, use it, FIXME, deal with extents
1056 * other than the page size
1060 BUG_ON(btrfs_file_extent_disk_num_blocks(item
) != 1);
1062 btrfs_file_extent_generation(item
) != trans
->transid
) {
1063 struct btrfs_key ins
;
1064 ret
= btrfs_alloc_extent(trans
, root
, 1,
1065 blocknr
, (u64
)-1, &ins
);
1067 btrfs_set_file_extent_disk_blocknr(item
, ins
.objectid
);
1068 mark_buffer_dirty(path
->nodes
[0]);
1069 ret
= btrfs_free_extent(trans
, root
,
1072 blocknr
= ins
.objectid
;
1075 map_bh(result
, inode
->i_sb
, blocknr
);
1079 /* are we inside the extent that was found? */
1080 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1081 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1082 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
) {
1085 btrfs_release_path(root
, path
);
1089 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1090 extent_start
= extent_start
>> inode
->i_blkbits
;
1091 extent_start
+= btrfs_file_extent_offset(item
);
1092 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1093 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1095 map_bh(result
, inode
->i_sb
, blocknr
+ iblock
- extent_start
);
1099 /* ok, create a new extent */
1104 ret
= btrfs_alloc_file_extent(trans
, root
, objectid
,
1105 iblock
<< inode
->i_blkbits
,
1106 1, extent_end
, &blocknr
);
1111 inode
->i_blocks
+= inode
->i_sb
->s_blocksize
>> 9;
1112 set_buffer_new(result
);
1113 map_bh(result
, inode
->i_sb
, blocknr
);
1116 btrfs_release_path(root
, path
);
1117 btrfs_free_path(path
);
1119 btrfs_end_transaction(trans
, root
);
1123 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1124 struct buffer_head
*result
, int create
)
1127 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1128 mutex_lock(&root
->fs_info
->fs_mutex
);
1129 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1130 // err = btrfs_get_block_inline(inode, iblock, result, create);
1131 mutex_unlock(&root
->fs_info
->fs_mutex
);
1135 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1136 unsigned from
, unsigned to
)
1138 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
1140 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1141 unsigned from
, unsigned to
)
1143 return nobh_commit_write(file
, page
, from
, to
);
1146 static void btrfs_write_super(struct super_block
*sb
)
1148 btrfs_sync_fs(sb
, 1);
1151 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1153 return mpage_readpage(page
, btrfs_get_block
);
1156 static int btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
1157 struct list_head
*pages
, unsigned nr_pages
)
1159 return mpage_readpages(mapping
, pages
, nr_pages
, btrfs_get_block
);
1162 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1164 return nobh_writepage(page
, btrfs_get_block
, wbc
);
1167 static void btrfs_truncate(struct inode
*inode
)
1169 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1171 struct btrfs_trans_handle
*trans
;
1173 if (!S_ISREG(inode
->i_mode
))
1175 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1178 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
1180 /* FIXME, add redo link to tree so we don't leak on crash */
1181 mutex_lock(&root
->fs_info
->fs_mutex
);
1182 trans
= btrfs_start_transaction(root
, 1);
1183 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1185 ret
= btrfs_end_transaction(trans
, root
);
1187 mutex_unlock(&root
->fs_info
->fs_mutex
);
1188 mark_inode_dirty(inode
);
1191 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1192 struct page
**prepared_pages
,
1193 const char __user
* buf
)
1195 long page_fault
= 0;
1197 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1199 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1200 size_t count
= min_t(size_t,
1201 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1202 struct page
*page
= prepared_pages
[i
];
1203 fault_in_pages_readable(buf
, count
);
1205 /* Copy data from userspace to the current page */
1207 page_fault
= __copy_from_user(page_address(page
) + offset
,
1209 /* Flush processor's dcache for this page */
1210 flush_dcache_page(page
);
1213 write_bytes
-= count
;
1218 return page_fault
? -EFAULT
: 0;
1221 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1224 for (i
= 0; i
< num_pages
; i
++) {
1227 unlock_page(pages
[i
]);
1228 mark_page_accessed(pages
[i
]);
1229 page_cache_release(pages
[i
]);
1232 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1233 struct btrfs_root
*root
,
1235 struct page
**pages
,
1245 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1247 for (i
= 0; i
< num_pages
; i
++) {
1248 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1249 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1250 /* FIXME, one block at a time */
1252 mutex_lock(&root
->fs_info
->fs_mutex
);
1253 trans
= btrfs_start_transaction(root
, 1);
1254 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1255 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1256 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1258 SetPageChecked(pages
[i
]);
1259 ret
= btrfs_end_transaction(trans
, root
);
1261 mutex_unlock(&root
->fs_info
->fs_mutex
);
1263 ret
= nobh_commit_write(file
, pages
[i
], offset
,
1264 offset
+ this_write
);
1270 WARN_ON(this_write
> write_bytes
);
1271 write_bytes
-= this_write
;
1277 static int prepare_pages(struct btrfs_trans_handle
*trans
,
1278 struct btrfs_root
*root
,
1280 struct page
**pages
,
1283 unsigned long first_index
,
1284 unsigned long last_index
,
1288 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1289 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1294 loff_t isize
= i_size_read(inode
);
1296 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1298 for (i
= 0; i
< num_pages
; i
++) {
1299 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1302 goto failed_release
;
1304 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1305 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1306 if (!PageUptodate(pages
[i
]) &&
1307 (pages
[i
]->index
== first_index
||
1308 pages
[i
]->index
== last_index
) && pos
< isize
) {
1309 ret
= mpage_readpage(pages
[i
], btrfs_get_block
);
1311 lock_page(pages
[i
]);
1313 ret
= nobh_prepare_write(pages
[i
], offset
,
1314 offset
+ this_write
,
1319 goto failed_truncate
;
1321 WARN_ON(this_write
> write_bytes
);
1322 write_bytes
-= this_write
;
1327 btrfs_drop_pages(pages
, num_pages
);
1331 btrfs_drop_pages(pages
, num_pages
);
1333 vmtruncate(inode
, isize
);
1337 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1338 size_t count
, loff_t
*ppos
)
1341 size_t num_written
= 0;
1344 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1345 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1346 struct page
*pages
[1];
1347 unsigned long first_index
;
1348 unsigned long last_index
;
1350 if (file
->f_flags
& O_DIRECT
)
1354 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1355 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1356 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1361 err
= remove_suid(file
->f_path
.dentry
);
1364 file_update_time(file
);
1365 mutex_lock(&inode
->i_mutex
);
1366 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1367 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
1369 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1370 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1371 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1373 ret
= prepare_pages(NULL
, root
, file
, pages
, num_pages
,
1374 pos
, first_index
, last_index
, write_bytes
);
1376 ret
= btrfs_copy_from_user(pos
, num_pages
,
1377 write_bytes
, pages
, buf
);
1380 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1381 num_pages
, pos
, write_bytes
);
1383 btrfs_drop_pages(pages
, num_pages
);
1386 count
-= write_bytes
;
1388 num_written
+= write_bytes
;
1390 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1393 mutex_unlock(&inode
->i_mutex
);
1396 current
->backing_dev_info
= NULL
;
1397 return num_written
? num_written
: err
;
1401 static ssize_t
inline_one_page(struct btrfs_root
*root
, struct inode
*inode
,
1402 struct page
*page
, loff_t pos
,
1403 size_t offset
, size_t write_bytes
)
1405 struct btrfs_path
*path
;
1406 struct btrfs_trans_handle
*trans
;
1407 struct btrfs_key key
;
1408 struct btrfs_leaf
*leaf
;
1409 struct btrfs_key found_key
;
1411 size_t copy_size
= 0;
1414 size_t num_written
= 0;
1416 path
= btrfs_alloc_path();
1418 mutex_lock(&root
->fs_info
->fs_mutex
);
1419 trans
= btrfs_start_transaction(root
, 1);
1420 key
.objectid
= inode
->i_ino
;
1422 btrfs_set_key_type(&key
, BTRFS_INLINE_DATA_KEY
);
1426 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1432 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1433 btrfs_disk_key_to_cpu(&found_key
,
1434 &leaf
->items
[path
->slots
[0]].key
);
1435 copy_size
= btrfs_item_size(leaf
->items
+ path
->slots
[0]);
1436 dst
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
1437 copy_size
= min(write_bytes
, copy_size
);
1440 int slot
= path
->slots
[0];
1444 // FIXME find max key
1445 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1446 btrfs_disk_key_to_cpu(&found_key
,
1447 &leaf
->items
[slot
].key
);
1448 if (found_key
.objectid
!= inode
->i_ino
)
1450 if (btrfs_key_type(&found_key
) != BTRFS_INLINE_DATA_KEY
)
1452 copy_size
= btrfs_item_size(leaf
->items
+ slot
);
1453 if (found_key
.offset
+ copy_size
<= pos
)
1455 dst
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
1456 dst
+= pos
- found_key
.offset
;
1457 copy_size
= copy_size
- (pos
- found_key
.offset
);
1458 BUG_ON(copy_size
< 0);
1459 copy_size
= min(write_bytes
, copy_size
);
1460 WARN_ON(copy_size
== 0);
1464 btrfs_release_path(root
, path
);
1465 copy_size
= min(write_bytes
,
1466 (size_t)BTRFS_LEAF_DATA_SIZE(root
) -
1467 sizeof(struct btrfs_item
) * 4);
1468 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, copy_size
);
1470 dst
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1471 path
->slots
[0], char);
1473 WARN_ON(copy_size
== 0);
1474 WARN_ON(dst
+ copy_size
>
1475 btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1476 path
->slots
[0], char) +
1477 btrfs_item_size(btrfs_buffer_leaf(path
->nodes
[0])->items
+
1479 btrfs_memcpy(root
, path
->nodes
[0]->b_data
, dst
,
1480 page_address(page
) + offset
, copy_size
);
1481 mark_buffer_dirty(path
->nodes
[0]);
1482 btrfs_release_path(root
, path
);
1484 offset
+= copy_size
;
1485 num_written
+= copy_size
;
1486 write_bytes
-= copy_size
;
1490 btrfs_free_path(path
);
1491 ret
= btrfs_end_transaction(trans
, root
);
1493 mutex_unlock(&root
->fs_info
->fs_mutex
);
1494 return num_written
? num_written
: err
;
1497 static ssize_t
btrfs_file_inline_write(struct file
*file
,
1498 const char __user
*buf
,
1499 size_t count
, loff_t
*ppos
)
1502 size_t num_written
= 0;
1505 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1506 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1507 unsigned long page_index
;
1509 if (file
->f_flags
& O_DIRECT
)
1513 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1514 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1515 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1520 err
= remove_suid(file
->f_path
.dentry
);
1523 file_update_time(file
);
1524 mutex_lock(&inode
->i_mutex
);
1526 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1527 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1530 page_index
= pos
>> PAGE_CACHE_SHIFT
;
1531 page
= grab_cache_page(inode
->i_mapping
, page_index
);
1532 if (!PageUptodate(page
)) {
1533 ret
= mpage_readpage(page
, btrfs_get_block
);
1537 ret
= btrfs_copy_from_user(pos
, 1,
1538 write_bytes
, &page
, buf
);
1540 write_bytes
= inline_one_page(root
, inode
, page
, pos
,
1541 offset
, write_bytes
);
1542 SetPageUptodate(page
);
1543 if (write_bytes
> 0 && pos
+ write_bytes
> inode
->i_size
) {
1544 i_size_write(inode
, pos
+ write_bytes
);
1545 mark_inode_dirty(inode
);
1547 page_cache_release(page
);
1549 if (write_bytes
< 0)
1552 count
-= write_bytes
;
1554 num_written
+= write_bytes
;
1556 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1560 mutex_unlock(&inode
->i_mutex
);
1563 current
->backing_dev_info
= NULL
;
1564 return num_written
? num_written
: err
;
1568 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1569 unsigned long offset
, unsigned long size
)
1572 unsigned long left
, count
= desc
->count
;
1573 struct inode
*inode
= page
->mapping
->host
;
1578 if (!PageChecked(page
)) {
1579 /* FIXME, do it per block */
1580 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1581 int ret
= btrfs_csum_verify_file_block(root
,
1582 page
->mapping
->host
->i_ino
,
1583 page
->index
<< PAGE_CACHE_SHIFT
,
1584 kmap(page
), PAGE_CACHE_SIZE
);
1586 printk("failed to verify ino %lu page %lu\n",
1587 page
->mapping
->host
->i_ino
,
1589 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
1591 SetPageChecked(page
);
1595 * Faults on the destination of a read are common, so do it before
1598 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1599 kaddr
= kmap_atomic(page
, KM_USER0
);
1600 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1601 kaddr
+ offset
, size
);
1602 kunmap_atomic(kaddr
, KM_USER0
);
1607 /* Do it the slow way */
1609 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1614 desc
->error
= -EFAULT
;
1617 desc
->count
= count
- size
;
1618 desc
->written
+= size
;
1619 desc
->arg
.buf
+= size
;
1624 * btrfs_file_aio_read - filesystem read routine
1625 * @iocb: kernel I/O control block
1626 * @iov: io vector request
1627 * @nr_segs: number of segments in the iovec
1628 * @pos: current file position
1630 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1631 unsigned long nr_segs
, loff_t pos
)
1633 struct file
*filp
= iocb
->ki_filp
;
1637 loff_t
*ppos
= &iocb
->ki_pos
;
1640 for (seg
= 0; seg
< nr_segs
; seg
++) {
1641 const struct iovec
*iv
= &iov
[seg
];
1644 * If any segment has a negative length, or the cumulative
1645 * length ever wraps negative then return -EINVAL.
1647 count
+= iv
->iov_len
;
1648 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1650 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1655 count
-= iv
->iov_len
; /* This segment is no good */
1660 for (seg
= 0; seg
< nr_segs
; seg
++) {
1661 read_descriptor_t desc
;
1664 desc
.arg
.buf
= iov
[seg
].iov_base
;
1665 desc
.count
= iov
[seg
].iov_len
;
1666 if (desc
.count
== 0)
1669 do_generic_file_read(filp
, ppos
, &desc
,
1671 retval
+= desc
.written
;
1673 retval
= retval
?: desc
.error
;
1681 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1683 struct btrfs_trans_handle
*trans
;
1684 struct btrfs_key key
;
1685 struct btrfs_root_item root_item
;
1686 struct btrfs_inode_item
*inode_item
;
1687 struct buffer_head
*subvol
;
1688 struct btrfs_leaf
*leaf
;
1689 struct btrfs_root
*new_root
;
1690 struct inode
*inode
;
1693 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1695 mutex_lock(&root
->fs_info
->fs_mutex
);
1696 trans
= btrfs_start_transaction(root
, 1);
1699 subvol
= btrfs_alloc_free_block(trans
, root
);
1700 leaf
= btrfs_buffer_leaf(subvol
);
1701 btrfs_set_header_nritems(&leaf
->header
, 0);
1702 btrfs_set_header_level(&leaf
->header
, 0);
1703 btrfs_set_header_blocknr(&leaf
->header
, subvol
->b_blocknr
);
1704 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
1705 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1706 sizeof(leaf
->header
.fsid
));
1708 inode_item
= &root_item
.inode
;
1709 memset(inode_item
, 0, sizeof(*inode_item
));
1710 btrfs_set_inode_generation(inode_item
, 1);
1711 btrfs_set_inode_size(inode_item
, 3);
1712 btrfs_set_inode_nlink(inode_item
, 1);
1713 btrfs_set_inode_nblocks(inode_item
, 1);
1714 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
1716 btrfs_set_root_blocknr(&root_item
, subvol
->b_blocknr
);
1717 btrfs_set_root_refs(&root_item
, 1);
1719 mark_buffer_dirty(subvol
);
1723 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1727 btrfs_set_root_dirid(&root_item
, new_dirid
);
1729 key
.objectid
= objectid
;
1732 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1733 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1738 * insert the directory item
1740 key
.offset
= (u64
)-1;
1741 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1743 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1747 ret
= btrfs_commit_transaction(trans
, root
);
1750 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
1753 trans
= btrfs_start_transaction(new_root
, 1);
1756 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
, S_IFDIR
| 0700);
1757 inode
->i_op
= &btrfs_dir_inode_operations
;
1758 inode
->i_fop
= &btrfs_dir_file_operations
;
1760 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1765 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1768 ret
= btrfs_commit_transaction(trans
, new_root
);
1773 mutex_unlock(&root
->fs_info
->fs_mutex
);
1777 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1779 struct btrfs_trans_handle
*trans
;
1780 struct btrfs_key key
;
1781 struct btrfs_root_item new_root_item
;
1785 if (!root
->ref_cows
)
1788 mutex_lock(&root
->fs_info
->fs_mutex
);
1789 trans
= btrfs_start_transaction(root
, 1);
1792 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
1795 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1799 memcpy(&new_root_item
, &root
->root_item
,
1800 sizeof(new_root_item
));
1802 key
.objectid
= objectid
;
1805 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1806 btrfs_set_root_blocknr(&new_root_item
, root
->node
->b_blocknr
);
1808 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1812 printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen
, name
, key
.objectid
, key
.offset
, key
.flags
);
1815 * insert the directory item
1817 key
.offset
= (u64
)-1;
1818 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1820 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1825 ret
= btrfs_inc_root_ref(trans
, root
);
1828 ret
= btrfs_commit_transaction(trans
, root
);
1830 mutex_unlock(&root
->fs_info
->fs_mutex
);
1834 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
1835 cmd
, unsigned long arg
)
1837 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1838 struct btrfs_ioctl_vol_args vol_args
;
1841 struct btrfs_path
*path
;
1845 case BTRFS_IOC_SNAP_CREATE
:
1846 if (copy_from_user(&vol_args
,
1847 (struct btrfs_ioctl_vol_args __user
*)arg
,
1850 namelen
= strlen(vol_args
.name
);
1851 if (namelen
> BTRFS_VOL_NAME_MAX
)
1853 path
= btrfs_alloc_path();
1856 root_dirid
= btrfs_root_dirid(
1857 &root
->fs_info
->tree_root
->root_item
);
1858 mutex_lock(&root
->fs_info
->fs_mutex
);
1859 ret
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
1861 vol_args
.name
, namelen
, 0);
1862 mutex_unlock(&root
->fs_info
->fs_mutex
);
1866 if (root
== root
->fs_info
->tree_root
)
1867 ret
= create_subvol(root
, vol_args
.name
, namelen
);
1869 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
1878 static struct kmem_cache
*btrfs_inode_cachep
;
1879 struct kmem_cache
*btrfs_trans_handle_cachep
;
1880 struct kmem_cache
*btrfs_transaction_cachep
;
1881 struct kmem_cache
*btrfs_bit_radix_cachep
;
1882 struct kmem_cache
*btrfs_path_cachep
;
1885 * Called inside transaction, so use GFP_NOFS
1887 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
1889 struct btrfs_inode
*ei
;
1891 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
1894 return &ei
->vfs_inode
;
1897 static void btrfs_destroy_inode(struct inode
*inode
)
1899 WARN_ON(!list_empty(&inode
->i_dentry
));
1900 WARN_ON(inode
->i_data
.nrpages
);
1902 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
1905 static void init_once(void * foo
, struct kmem_cache
* cachep
,
1906 unsigned long flags
)
1908 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
1910 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
1911 SLAB_CTOR_CONSTRUCTOR
) {
1912 inode_init_once(&ei
->vfs_inode
);
1916 static int init_inodecache(void)
1918 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
1919 sizeof(struct btrfs_inode
),
1920 0, (SLAB_RECLAIM_ACCOUNT
|
1923 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
1924 sizeof(struct btrfs_trans_handle
),
1925 0, (SLAB_RECLAIM_ACCOUNT
|
1928 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
1929 sizeof(struct btrfs_transaction
),
1930 0, (SLAB_RECLAIM_ACCOUNT
|
1933 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
1934 sizeof(struct btrfs_transaction
),
1935 0, (SLAB_RECLAIM_ACCOUNT
|
1938 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
1940 0, (SLAB_RECLAIM_ACCOUNT
|
1942 SLAB_DESTROY_BY_RCU
),
1944 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
1945 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
1950 static void destroy_inodecache(void)
1952 kmem_cache_destroy(btrfs_inode_cachep
);
1953 kmem_cache_destroy(btrfs_trans_handle_cachep
);
1954 kmem_cache_destroy(btrfs_transaction_cachep
);
1955 kmem_cache_destroy(btrfs_bit_radix_cachep
);
1956 kmem_cache_destroy(btrfs_path_cachep
);
1959 static int btrfs_get_sb(struct file_system_type
*fs_type
,
1960 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1962 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
1963 btrfs_fill_super
, mnt
);
1966 static struct file_system_type btrfs_fs_type
= {
1967 .owner
= THIS_MODULE
,
1969 .get_sb
= btrfs_get_sb
,
1970 .kill_sb
= kill_block_super
,
1971 .fs_flags
= FS_REQUIRES_DEV
,
1974 static struct super_operations btrfs_super_ops
= {
1975 .statfs
= simple_statfs
,
1976 .delete_inode
= btrfs_delete_inode
,
1977 .put_super
= btrfs_put_super
,
1978 .read_inode
= btrfs_read_locked_inode
,
1979 .write_super
= btrfs_write_super
,
1980 .sync_fs
= btrfs_sync_fs
,
1981 .write_inode
= btrfs_write_inode
,
1982 .alloc_inode
= btrfs_alloc_inode
,
1983 .destroy_inode
= btrfs_destroy_inode
,
1986 static struct inode_operations btrfs_dir_inode_operations
= {
1987 .lookup
= btrfs_lookup
,
1988 .create
= btrfs_create
,
1989 .unlink
= btrfs_unlink
,
1990 .mkdir
= btrfs_mkdir
,
1991 .rmdir
= btrfs_rmdir
,
1994 static struct inode_operations btrfs_dir_ro_inode_operations
= {
1995 .lookup
= btrfs_lookup
,
1998 static struct file_operations btrfs_dir_file_operations
= {
1999 .llseek
= generic_file_llseek
,
2000 .read
= generic_read_dir
,
2001 .readdir
= btrfs_readdir
,
2002 .ioctl
= btrfs_ioctl
,
2005 static struct address_space_operations btrfs_aops
= {
2006 .readpage
= btrfs_readpage
,
2007 .readpages
= btrfs_readpages
,
2008 .writepage
= btrfs_writepage
,
2009 .sync_page
= block_sync_page
,
2010 .prepare_write
= btrfs_prepare_write
,
2011 .commit_write
= btrfs_commit_write
,
2014 static struct inode_operations btrfs_file_inode_operations
= {
2015 .truncate
= btrfs_truncate
,
2018 static struct file_operations btrfs_file_operations
= {
2019 .llseek
= generic_file_llseek
,
2020 .read
= do_sync_read
,
2021 .aio_read
= btrfs_file_aio_read
,
2022 .write
= btrfs_file_write
,
2023 .mmap
= generic_file_mmap
,
2024 .open
= generic_file_open
,
2025 .ioctl
= btrfs_ioctl
,
2028 static int __init
init_btrfs_fs(void)
2031 printk("btrfs loaded!\n");
2032 err
= init_inodecache();
2035 kset_set_kset_s(&btrfs_subsys
, fs_subsys
);
2036 err
= subsystem_register(&btrfs_subsys
);
2039 return register_filesystem(&btrfs_fs_type
);
2041 destroy_inodecache();
2045 static void __exit
exit_btrfs_fs(void)
2047 destroy_inodecache();
2048 unregister_filesystem(&btrfs_fs_type
);
2049 subsystem_unregister(&btrfs_subsys
);
2050 printk("btrfs unloaded\n");
2053 module_init(init_btrfs_fs
)
2054 module_exit(exit_btrfs_fs
)
2056 MODULE_LICENSE("GPL");