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_ITEM_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 (btrfs_disk_key_type(&item
->key
) != key_type
)
566 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
568 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
570 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
571 over
= filldir(dirent
, (const char *)(di
+ 1),
572 btrfs_dir_name_len(di
),
573 btrfs_disk_key_offset(&item
->key
),
574 btrfs_disk_key_objectid(&di
->location
), d_type
);
582 btrfs_release_path(root
, path
);
583 btrfs_free_path(path
);
584 mutex_unlock(&root
->fs_info
->fs_mutex
);
588 static void btrfs_put_super (struct super_block
* sb
)
590 struct btrfs_root
*root
= btrfs_sb(sb
);
593 ret
= close_ctree(root
);
595 printk("close ctree returns %d\n", ret
);
597 sb
->s_fs_info
= NULL
;
600 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
602 struct inode
* inode
;
603 struct dentry
* root_dentry
;
604 struct btrfs_super_block
*disk_super
;
605 struct btrfs_root
*tree_root
;
606 struct btrfs_inode
*bi
;
608 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
609 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
610 sb
->s_op
= &btrfs_super_ops
;
613 tree_root
= open_ctree(sb
);
616 printk("btrfs: open_ctree failed\n");
619 sb
->s_fs_info
= tree_root
;
620 disk_super
= tree_root
->fs_info
->disk_super
;
621 printk("read in super total blocks %Lu root %Lu\n",
622 btrfs_super_total_blocks(disk_super
),
623 btrfs_super_root_dir(disk_super
));
625 inode
= btrfs_iget_locked(sb
, btrfs_super_root_dir(disk_super
),
628 bi
->location
.objectid
= inode
->i_ino
;
629 bi
->location
.offset
= 0;
630 bi
->location
.flags
= 0;
631 bi
->root
= tree_root
;
632 btrfs_set_key_type(&bi
->location
, BTRFS_INODE_ITEM_KEY
);
636 if (inode
->i_state
& I_NEW
) {
637 btrfs_read_locked_inode(inode
);
638 unlock_new_inode(inode
);
641 root_dentry
= d_alloc_root(inode
);
646 sb
->s_root
= root_dentry
;
651 static void fill_inode_item(struct btrfs_inode_item
*item
,
654 btrfs_set_inode_uid(item
, inode
->i_uid
);
655 btrfs_set_inode_gid(item
, inode
->i_gid
);
656 btrfs_set_inode_size(item
, inode
->i_size
);
657 btrfs_set_inode_mode(item
, inode
->i_mode
);
658 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
659 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
660 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
661 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
662 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
663 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
664 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
665 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
666 btrfs_set_inode_generation(item
, inode
->i_generation
);
669 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
670 struct btrfs_root
*root
,
673 struct btrfs_inode_item
*inode_item
;
674 struct btrfs_path
*path
;
677 path
= btrfs_alloc_path();
679 btrfs_init_path(path
);
680 ret
= btrfs_lookup_inode(trans
, root
, path
,
681 &BTRFS_I(inode
)->location
, 1);
688 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
690 struct btrfs_inode_item
);
692 fill_inode_item(inode_item
, inode
);
693 btrfs_mark_buffer_dirty(path
->nodes
[0]);
696 btrfs_release_path(root
, path
);
697 btrfs_free_path(path
);
701 static int btrfs_write_inode(struct inode
*inode
, int wait
)
703 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
704 struct btrfs_trans_handle
*trans
;
707 mutex_lock(&root
->fs_info
->fs_mutex
);
708 trans
= btrfs_start_transaction(root
, 1);
709 ret
= btrfs_update_inode(trans
, root
, inode
);
711 btrfs_commit_transaction(trans
, root
);
713 btrfs_end_transaction(trans
, root
);
714 mutex_unlock(&root
->fs_info
->fs_mutex
);
718 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
719 struct btrfs_root
*root
,
720 u64 objectid
, int mode
)
723 struct btrfs_inode_item inode_item
;
724 struct btrfs_key
*location
;
727 inode
= new_inode(root
->fs_info
->sb
);
729 return ERR_PTR(-ENOMEM
);
731 BTRFS_I(inode
)->root
= root
;
733 inode
->i_uid
= current
->fsuid
;
734 inode
->i_gid
= current
->fsgid
;
735 inode
->i_mode
= mode
;
736 inode
->i_ino
= objectid
;
738 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
739 fill_inode_item(&inode_item
, inode
);
740 location
= &BTRFS_I(inode
)->location
;
741 location
->objectid
= objectid
;
743 location
->offset
= 0;
744 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
746 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
749 insert_inode_hash(inode
);
753 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
754 struct dentry
*dentry
, struct inode
*inode
)
757 struct btrfs_key key
;
758 struct btrfs_root
*root
= BTRFS_I(dentry
->d_parent
->d_inode
)->root
;
759 key
.objectid
= inode
->i_ino
;
761 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
764 ret
= btrfs_insert_dir_item(trans
, root
,
765 dentry
->d_name
.name
, dentry
->d_name
.len
,
766 dentry
->d_parent
->d_inode
->i_ino
,
769 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
* 2;
770 ret
= btrfs_update_inode(trans
, root
,
771 dentry
->d_parent
->d_inode
);
776 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
777 struct dentry
*dentry
, struct inode
*inode
)
779 int err
= btrfs_add_link(trans
, dentry
, inode
);
781 d_instantiate(dentry
, inode
);
789 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
790 int mode
, struct nameidata
*nd
)
792 struct btrfs_trans_handle
*trans
;
793 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
799 mutex_lock(&root
->fs_info
->fs_mutex
);
800 trans
= btrfs_start_transaction(root
, 1);
802 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
808 inode
= btrfs_new_inode(trans
, root
, objectid
, mode
);
809 err
= PTR_ERR(inode
);
812 // FIXME mark the inode dirty
813 err
= btrfs_add_nondir(trans
, dentry
, inode
);
817 inode
->i_mapping
->a_ops
= &btrfs_aops
;
818 inode
->i_fop
= &btrfs_file_operations
;
819 inode
->i_op
= &btrfs_file_inode_operations
;
821 dir
->i_sb
->s_dirt
= 1;
823 btrfs_end_transaction(trans
, root
);
824 mutex_unlock(&root
->fs_info
->fs_mutex
);
827 inode_dec_link_count(inode
);
833 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
834 struct btrfs_root
*root
,
835 u64 objectid
, u64 dirid
)
839 struct btrfs_key key
;
844 key
.objectid
= objectid
;
847 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
849 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, objectid
,
853 key
.objectid
= dirid
;
854 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, objectid
,
862 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
865 struct btrfs_trans_handle
*trans
;
866 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
871 mutex_lock(&root
->fs_info
->fs_mutex
);
872 trans
= btrfs_start_transaction(root
, 1);
874 err
= PTR_ERR(trans
);
878 err
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
884 inode
= btrfs_new_inode(trans
, root
, objectid
, S_IFDIR
| mode
);
886 err
= PTR_ERR(inode
);
890 inode
->i_op
= &btrfs_dir_inode_operations
;
891 inode
->i_fop
= &btrfs_dir_file_operations
;
893 err
= btrfs_make_empty_dir(trans
, root
, inode
->i_ino
, dir
->i_ino
);
898 err
= btrfs_update_inode(trans
, root
, inode
);
901 err
= btrfs_add_link(trans
, dentry
, inode
);
904 d_instantiate(dentry
, inode
);
908 btrfs_end_transaction(trans
, root
);
910 mutex_unlock(&root
->fs_info
->fs_mutex
);
916 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
918 struct btrfs_trans_handle
*trans
;
919 struct btrfs_root
*root
;
925 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
928 filemap_write_and_wait(root
->fs_info
->btree_inode
->i_mapping
);
929 mutex_lock(&root
->fs_info
->fs_mutex
);
930 trans
= btrfs_start_transaction(root
, 1);
931 ret
= btrfs_commit_transaction(trans
, root
);
934 printk("btrfs sync_fs\n");
935 mutex_unlock(&root
->fs_info
->fs_mutex
);
940 static int btrfs_get_block_inline(struct inode
*inode
, sector_t iblock
,
941 struct buffer_head
*result
, int create
)
943 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
944 struct btrfs_path
*path
;
945 struct btrfs_key key
;
946 struct btrfs_leaf
*leaf
;
947 int num_bytes
= result
->b_size
;
957 path
= btrfs_alloc_path();
964 pos
= iblock
<< inode
->i_blkbits
;
965 key
.objectid
= inode
->i_ino
;
967 btrfs_set_key_type(&key
, BTRFS_INLINE_DATA_KEY
);
968 ptr
= kmap(result
->b_page
);
970 ptr
+= (pos
& (PAGE_CACHE_SIZE
-1));
973 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
981 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
982 item_size
= btrfs_item_size(leaf
->items
+ path
->slots
[0]);
983 copy_size
= min(num_bytes
, item_size
);
984 data_ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
985 WARN_ON(safe_ptr
+ PAGE_CACHE_SIZE
< ptr
+ copy_size
);
986 memcpy(ptr
, data_ptr
, copy_size
);
988 num_bytes
-= copy_size
;
989 WARN_ON(num_bytes
< 0);
991 btrfs_release_path(root
, path
);
992 if (num_bytes
!= 0) {
993 if (pos
>= i_size_read(inode
))
994 memset(ptr
, 0, num_bytes
);
998 set_buffer_uptodate(result
);
999 map_bh(result
, inode
->i_sb
, 0);
1002 btrfs_free_path(path
);
1003 kunmap(result
->b_page
);
1008 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
1009 struct buffer_head
*result
, int create
)
1014 u64 extent_start
= 0;
1016 u64 objectid
= inode
->i_ino
;
1017 struct btrfs_path
*path
;
1018 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1019 struct btrfs_trans_handle
*trans
= NULL
;
1020 struct btrfs_file_extent_item
*item
;
1021 struct btrfs_leaf
*leaf
;
1022 struct btrfs_disk_key
*found_key
;
1024 path
= btrfs_alloc_path();
1026 btrfs_init_path(path
);
1028 trans
= btrfs_start_transaction(root
, 1);
1032 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
1034 iblock
<< inode
->i_blkbits
, create
);
1041 if (path
->slots
[0] == 0) {
1042 btrfs_release_path(root
, path
);
1048 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
1049 struct btrfs_file_extent_item
);
1050 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1051 blocknr
= btrfs_file_extent_disk_blocknr(item
);
1052 blocknr
+= btrfs_file_extent_offset(item
);
1054 /* exact match found, use it, FIXME, deal with extents
1055 * other than the page size
1057 if (0 && ret
== 0) {
1060 btrfs_file_extent_generation(item
) != trans
->transid
) {
1061 struct btrfs_key ins
;
1062 ret
= btrfs_alloc_extent(trans
, root
, 1,
1063 blocknr
, (u64
)-1, &ins
);
1065 btrfs_set_file_extent_disk_blocknr(item
, ins
.objectid
);
1066 mark_buffer_dirty(path
->nodes
[0]);
1067 ret
= btrfs_free_extent(trans
, root
,
1070 blocknr
= ins
.objectid
;
1073 btrfs_map_bh_to_logical(root
, result
, blocknr
);
1077 /* are we inside the extent that was found? */
1078 found_key
= &leaf
->items
[path
->slots
[0]].key
;
1079 if (btrfs_disk_key_objectid(found_key
) != objectid
||
1080 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
) {
1083 btrfs_release_path(root
, path
);
1087 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
1088 extent_start
= extent_start
>> inode
->i_blkbits
;
1089 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
1090 if (iblock
>= extent_start
&& iblock
< extent_end
) {
1092 btrfs_map_bh_to_logical(root
, result
, blocknr
+ iblock
-
1097 /* ok, create a new extent */
1103 ret
= btrfs_alloc_file_extent(trans
, root
, objectid
,
1104 iblock
<< inode
->i_blkbits
,
1105 1, extent_end
, &blocknr
);
1110 inode
->i_blocks
+= inode
->i_sb
->s_blocksize
>> 9;
1111 set_buffer_new(result
);
1112 map_bh(result
, inode
->i_sb
, blocknr
);
1114 btrfs_map_bh_to_logical(root
, result
, blocknr
);
1117 btrfs_release_path(root
, path
);
1118 btrfs_free_path(path
);
1120 btrfs_end_transaction(trans
, root
);
1124 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
1125 struct buffer_head
*result
, int create
)
1128 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1129 mutex_lock(&root
->fs_info
->fs_mutex
);
1130 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
1131 // err = btrfs_get_block_inline(inode, iblock, result, create);
1132 mutex_unlock(&root
->fs_info
->fs_mutex
);
1136 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
1137 unsigned from
, unsigned to
)
1139 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
1141 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
1142 unsigned from
, unsigned to
)
1144 return nobh_commit_write(file
, page
, from
, to
);
1147 static void btrfs_write_super(struct super_block
*sb
)
1149 btrfs_sync_fs(sb
, 1);
1152 static int btrfs_readpage(struct file
*file
, struct page
*page
)
1154 return mpage_readpage(page
, btrfs_get_block
);
1157 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
1159 return nobh_writepage(page
, btrfs_get_block
, wbc
);
1162 static void btrfs_truncate(struct inode
*inode
)
1164 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1166 struct btrfs_trans_handle
*trans
;
1168 if (!S_ISREG(inode
->i_mode
))
1170 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1173 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
1175 /* FIXME, add redo link to tree so we don't leak on crash */
1176 mutex_lock(&root
->fs_info
->fs_mutex
);
1177 trans
= btrfs_start_transaction(root
, 1);
1178 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
1180 ret
= btrfs_end_transaction(trans
, root
);
1182 mutex_unlock(&root
->fs_info
->fs_mutex
);
1183 mark_inode_dirty(inode
);
1186 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
1187 struct page
**prepared_pages
,
1188 const char __user
* buf
)
1190 long page_fault
= 0;
1192 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1194 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
1195 size_t count
= min_t(size_t,
1196 PAGE_CACHE_SIZE
- offset
, write_bytes
);
1197 struct page
*page
= prepared_pages
[i
];
1198 fault_in_pages_readable(buf
, count
);
1200 /* Copy data from userspace to the current page */
1202 page_fault
= __copy_from_user(page_address(page
) + offset
,
1204 /* Flush processor's dcache for this page */
1205 flush_dcache_page(page
);
1208 write_bytes
-= count
;
1213 return page_fault
? -EFAULT
: 0;
1216 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1219 for (i
= 0; i
< num_pages
; i
++) {
1222 unlock_page(pages
[i
]);
1223 mark_page_accessed(pages
[i
]);
1224 page_cache_release(pages
[i
]);
1227 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1228 struct btrfs_root
*root
,
1230 struct page
**pages
,
1240 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1242 for (i
= 0; i
< num_pages
; i
++) {
1243 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1244 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1245 /* FIXME, one block at a time */
1247 mutex_lock(&root
->fs_info
->fs_mutex
);
1248 trans
= btrfs_start_transaction(root
, 1);
1249 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1250 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1251 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1253 SetPageChecked(pages
[i
]);
1254 ret
= btrfs_end_transaction(trans
, root
);
1256 mutex_unlock(&root
->fs_info
->fs_mutex
);
1258 ret
= nobh_commit_write(file
, pages
[i
], offset
,
1259 offset
+ this_write
);
1265 WARN_ON(this_write
> write_bytes
);
1266 write_bytes
-= this_write
;
1272 static int drop_extents(struct btrfs_trans_handle
*trans
,
1273 struct btrfs_root
*root
,
1274 struct inode
*inode
,
1278 struct btrfs_key key
;
1279 struct btrfs_leaf
*leaf
;
1281 struct btrfs_file_extent_item
*extent
;
1284 struct btrfs_file_extent_item old
;
1285 struct btrfs_path
*path
;
1286 u64 search_start
= start
;
1289 path
= btrfs_alloc_path();
1293 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
1298 if (path
->slots
[0] == 0) {
1307 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1308 slot
= path
->slots
[0];
1309 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
1311 extent
= btrfs_item_ptr(leaf
, slot
,
1312 struct btrfs_file_extent_item
);
1313 extent_end
= key
.offset
+
1314 (btrfs_file_extent_num_blocks(extent
) <<
1316 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
1320 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1323 if (end
< extent_end
&& end
>= key
.offset
) {
1324 memcpy(&old
, extent
, sizeof(old
));
1325 ret
= btrfs_inc_extent_ref(trans
, root
,
1326 btrfs_file_extent_disk_blocknr(&old
),
1327 btrfs_file_extent_disk_num_blocks(&old
));
1332 if (start
> key
.offset
) {
1334 /* truncate existing extent */
1336 WARN_ON(start
& (root
->blocksize
- 1));
1337 new_num
= (start
- key
.offset
) >> inode
->i_blkbits
;
1338 btrfs_set_file_extent_num_blocks(extent
, new_num
);
1339 mark_buffer_dirty(path
->nodes
[0]);
1343 u64 disk_num_blocks
;
1344 disk_blocknr
= btrfs_file_extent_disk_blocknr(extent
);
1346 btrfs_file_extent_disk_num_blocks(extent
);
1347 search_start
= key
.offset
+
1348 (btrfs_file_extent_num_blocks(extent
) <<
1350 ret
= btrfs_del_item(trans
, root
, path
);
1352 btrfs_release_path(root
, path
);
1354 ret
= btrfs_free_extent(trans
, root
, disk_blocknr
,
1355 disk_num_blocks
, 0);
1358 if (!bookend
&& search_start
>= end
) {
1366 /* create bookend */
1367 struct btrfs_key ins
;
1368 ins
.objectid
= inode
->i_ino
;
1371 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
1373 btrfs_release_path(root
, path
);
1374 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
1377 extent
= btrfs_item_ptr(
1378 btrfs_buffer_leaf(path
->nodes
[0]),
1380 struct btrfs_file_extent_item
);
1381 btrfs_set_file_extent_disk_blocknr(extent
,
1382 btrfs_file_extent_disk_blocknr(&old
));
1383 btrfs_set_file_extent_disk_num_blocks(extent
,
1384 btrfs_file_extent_disk_num_blocks(&old
));
1386 btrfs_set_file_extent_offset(extent
,
1387 btrfs_file_extent_offset(&old
) +
1388 ((end
- key
.offset
) >> inode
->i_blkbits
));
1389 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
1390 (end
- key
.offset
) >> inode
->i_blkbits
);
1391 btrfs_set_file_extent_num_blocks(extent
,
1392 btrfs_file_extent_num_blocks(&old
) -
1393 ((end
- key
.offset
) >> inode
->i_blkbits
));
1395 btrfs_set_file_extent_generation(extent
,
1396 btrfs_file_extent_generation(&old
));
1397 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1402 if (slot
>= btrfs_header_nritems(&leaf
->header
) - 1) {
1403 ret
= btrfs_next_leaf(root
, path
);
1414 btrfs_free_path(path
);
1418 static int prepare_pages(struct btrfs_root
*root
,
1420 struct page
**pages
,
1423 unsigned long first_index
,
1424 unsigned long last_index
,
1426 u64 alloc_extent_start
)
1429 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1430 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1434 struct buffer_head
*bh
;
1435 struct buffer_head
*head
;
1436 loff_t isize
= i_size_read(inode
);
1438 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1440 for (i
= 0; i
< num_pages
; i
++) {
1441 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1444 goto failed_release
;
1446 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1447 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1449 if ((pages
[i
]->index
== first_index
||
1450 pages
[i
]->index
== last_index
) && pos
< isize
&&
1451 !PageUptodate(pages
[i
])) {
1452 ret
= mpage_readpage(pages
[i
], btrfs_get_block
);
1454 lock_page(pages
[i
]);
1457 create_empty_buffers(pages
[i
], root
->fs_info
->sb
->s_blocksize
,
1458 (1 << BH_Uptodate
));
1459 head
= page_buffers(pages
[i
]);
1462 err
= btrfs_map_bh_to_logical(root
, bh
,
1463 alloc_extent_start
);
1466 goto failed_truncate
;
1467 bh
= bh
->b_this_page
;
1468 alloc_extent_start
++;
1469 } while (bh
!= head
);
1471 WARN_ON(this_write
> write_bytes
);
1472 write_bytes
-= this_write
;
1477 btrfs_drop_pages(pages
, num_pages
);
1481 btrfs_drop_pages(pages
, num_pages
);
1483 vmtruncate(inode
, isize
);
1487 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1488 size_t count
, loff_t
*ppos
)
1491 size_t num_written
= 0;
1494 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1495 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1496 struct page
*pages
[8];
1497 unsigned long first_index
;
1498 unsigned long last_index
;
1501 u64 alloc_extent_start
;
1502 struct btrfs_trans_handle
*trans
;
1503 struct btrfs_key ins
;
1505 if (file
->f_flags
& O_DIRECT
)
1509 start_pos
= pos
& ~(root
->blocksize
- 1);
1511 if (start_pos
!= pos
)
1513 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
1516 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1517 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1518 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1523 err
= remove_suid(file
->f_path
.dentry
);
1526 file_update_time(file
);
1527 mutex_lock(&inode
->i_mutex
);
1528 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1529 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
1531 mutex_lock(&root
->fs_info
->fs_mutex
);
1532 trans
= btrfs_start_transaction(root
, 1);
1535 mutex_unlock(&root
->fs_info
->fs_mutex
);
1538 if (start_pos
< inode
->i_size
) {
1539 ret
= drop_extents(trans
, root
, inode
,
1541 (pos
+ count
+ root
->blocksize
-1) &
1542 ~(root
->blocksize
- 1));
1544 ret
= btrfs_alloc_extent(trans
, root
, num_blocks
, 1,
1547 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
1548 start_pos
, ins
.objectid
, ins
.offset
);
1550 alloc_extent_start
= ins
.objectid
;
1551 ret
= btrfs_end_transaction(trans
, root
);
1552 mutex_unlock(&root
->fs_info
->fs_mutex
);
1555 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1556 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1557 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1560 memset(pages
, 0, sizeof(pages
));
1561 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1562 pos
, first_index
, last_index
,
1563 write_bytes
, alloc_extent_start
);
1566 /* FIXME blocks != pagesize */
1567 alloc_extent_start
+= num_pages
;
1568 ret
= btrfs_copy_from_user(pos
, num_pages
,
1569 write_bytes
, pages
, buf
);
1572 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1573 num_pages
, pos
, write_bytes
);
1575 btrfs_drop_pages(pages
, num_pages
);
1578 count
-= write_bytes
;
1580 num_written
+= write_bytes
;
1582 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1586 mutex_unlock(&inode
->i_mutex
);
1589 current
->backing_dev_info
= NULL
;
1590 return num_written
? num_written
: err
;
1594 static ssize_t
inline_one_page(struct btrfs_root
*root
, struct inode
*inode
,
1595 struct page
*page
, loff_t pos
,
1596 size_t offset
, size_t write_bytes
)
1598 struct btrfs_path
*path
;
1599 struct btrfs_trans_handle
*trans
;
1600 struct btrfs_key key
;
1601 struct btrfs_leaf
*leaf
;
1602 struct btrfs_key found_key
;
1604 size_t copy_size
= 0;
1607 size_t num_written
= 0;
1609 path
= btrfs_alloc_path();
1611 mutex_lock(&root
->fs_info
->fs_mutex
);
1612 trans
= btrfs_start_transaction(root
, 1);
1613 key
.objectid
= inode
->i_ino
;
1615 btrfs_set_key_type(&key
, BTRFS_INLINE_DATA_KEY
);
1619 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1625 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1626 btrfs_disk_key_to_cpu(&found_key
,
1627 &leaf
->items
[path
->slots
[0]].key
);
1628 copy_size
= btrfs_item_size(leaf
->items
+ path
->slots
[0]);
1629 dst
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
1630 copy_size
= min(write_bytes
, copy_size
);
1633 int slot
= path
->slots
[0];
1637 // FIXME find max key
1638 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
1639 btrfs_disk_key_to_cpu(&found_key
,
1640 &leaf
->items
[slot
].key
);
1641 if (found_key
.objectid
!= inode
->i_ino
)
1643 if (btrfs_key_type(&found_key
) != BTRFS_INLINE_DATA_KEY
)
1645 copy_size
= btrfs_item_size(leaf
->items
+ slot
);
1646 if (found_key
.offset
+ copy_size
<= pos
)
1648 dst
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
1649 dst
+= pos
- found_key
.offset
;
1650 copy_size
= copy_size
- (pos
- found_key
.offset
);
1651 BUG_ON(copy_size
< 0);
1652 copy_size
= min(write_bytes
, copy_size
);
1653 WARN_ON(copy_size
== 0);
1657 btrfs_release_path(root
, path
);
1658 copy_size
= min(write_bytes
,
1659 (size_t)BTRFS_LEAF_DATA_SIZE(root
) -
1660 sizeof(struct btrfs_item
) * 4);
1661 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, copy_size
);
1663 dst
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1664 path
->slots
[0], char);
1666 WARN_ON(copy_size
== 0);
1667 WARN_ON(dst
+ copy_size
>
1668 btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
1669 path
->slots
[0], char) +
1670 btrfs_item_size(btrfs_buffer_leaf(path
->nodes
[0])->items
+
1672 btrfs_memcpy(root
, path
->nodes
[0]->b_data
, dst
,
1673 page_address(page
) + offset
, copy_size
);
1674 mark_buffer_dirty(path
->nodes
[0]);
1675 btrfs_release_path(root
, path
);
1677 offset
+= copy_size
;
1678 num_written
+= copy_size
;
1679 write_bytes
-= copy_size
;
1683 btrfs_free_path(path
);
1684 ret
= btrfs_end_transaction(trans
, root
);
1686 mutex_unlock(&root
->fs_info
->fs_mutex
);
1687 return num_written
? num_written
: err
;
1690 static ssize_t
btrfs_file_inline_write(struct file
*file
,
1691 const char __user
*buf
,
1692 size_t count
, loff_t
*ppos
)
1695 size_t num_written
= 0;
1698 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1699 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1700 unsigned long page_index
;
1702 if (file
->f_flags
& O_DIRECT
)
1706 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1707 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1708 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1713 err
= remove_suid(file
->f_path
.dentry
);
1716 file_update_time(file
);
1717 mutex_lock(&inode
->i_mutex
);
1719 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1720 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1723 page_index
= pos
>> PAGE_CACHE_SHIFT
;
1724 page
= grab_cache_page(inode
->i_mapping
, page_index
);
1725 if (!PageUptodate(page
)) {
1726 ret
= mpage_readpage(page
, btrfs_get_block
);
1730 ret
= btrfs_copy_from_user(pos
, 1,
1731 write_bytes
, &page
, buf
);
1733 write_bytes
= inline_one_page(root
, inode
, page
, pos
,
1734 offset
, write_bytes
);
1735 SetPageUptodate(page
);
1736 if (write_bytes
> 0 && pos
+ write_bytes
> inode
->i_size
) {
1737 i_size_write(inode
, pos
+ write_bytes
);
1738 mark_inode_dirty(inode
);
1740 page_cache_release(page
);
1742 if (write_bytes
< 0)
1745 count
-= write_bytes
;
1747 num_written
+= write_bytes
;
1749 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1753 mutex_unlock(&inode
->i_mutex
);
1756 current
->backing_dev_info
= NULL
;
1757 return num_written
? num_written
: err
;
1761 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1762 unsigned long offset
, unsigned long size
)
1765 unsigned long left
, count
= desc
->count
;
1766 struct inode
*inode
= page
->mapping
->host
;
1771 if (!PageChecked(page
)) {
1772 /* FIXME, do it per block */
1773 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1774 int ret
= btrfs_csum_verify_file_block(root
,
1775 page
->mapping
->host
->i_ino
,
1776 page
->index
<< PAGE_CACHE_SHIFT
,
1777 kmap(page
), PAGE_CACHE_SIZE
);
1779 printk("failed to verify ino %lu page %lu\n",
1780 page
->mapping
->host
->i_ino
,
1782 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
1784 SetPageChecked(page
);
1788 * Faults on the destination of a read are common, so do it before
1791 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1792 kaddr
= kmap_atomic(page
, KM_USER0
);
1793 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1794 kaddr
+ offset
, size
);
1795 kunmap_atomic(kaddr
, KM_USER0
);
1800 /* Do it the slow way */
1802 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1807 desc
->error
= -EFAULT
;
1810 desc
->count
= count
- size
;
1811 desc
->written
+= size
;
1812 desc
->arg
.buf
+= size
;
1817 * btrfs_file_aio_read - filesystem read routine
1818 * @iocb: kernel I/O control block
1819 * @iov: io vector request
1820 * @nr_segs: number of segments in the iovec
1821 * @pos: current file position
1823 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1824 unsigned long nr_segs
, loff_t pos
)
1826 struct file
*filp
= iocb
->ki_filp
;
1830 loff_t
*ppos
= &iocb
->ki_pos
;
1833 for (seg
= 0; seg
< nr_segs
; seg
++) {
1834 const struct iovec
*iv
= &iov
[seg
];
1837 * If any segment has a negative length, or the cumulative
1838 * length ever wraps negative then return -EINVAL.
1840 count
+= iv
->iov_len
;
1841 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1843 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1848 count
-= iv
->iov_len
; /* This segment is no good */
1853 for (seg
= 0; seg
< nr_segs
; seg
++) {
1854 read_descriptor_t desc
;
1857 desc
.arg
.buf
= iov
[seg
].iov_base
;
1858 desc
.count
= iov
[seg
].iov_len
;
1859 if (desc
.count
== 0)
1862 do_generic_file_read(filp
, ppos
, &desc
,
1864 retval
+= desc
.written
;
1866 retval
= retval
?: desc
.error
;
1874 static int create_subvol(struct btrfs_root
*root
, char *name
, int namelen
)
1876 struct btrfs_trans_handle
*trans
;
1877 struct btrfs_key key
;
1878 struct btrfs_root_item root_item
;
1879 struct btrfs_inode_item
*inode_item
;
1880 struct buffer_head
*subvol
;
1881 struct btrfs_leaf
*leaf
;
1882 struct btrfs_root
*new_root
;
1883 struct inode
*inode
;
1886 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
1888 mutex_lock(&root
->fs_info
->fs_mutex
);
1889 trans
= btrfs_start_transaction(root
, 1);
1892 subvol
= btrfs_alloc_free_block(trans
, root
);
1893 leaf
= btrfs_buffer_leaf(subvol
);
1894 btrfs_set_header_nritems(&leaf
->header
, 0);
1895 btrfs_set_header_level(&leaf
->header
, 0);
1896 btrfs_set_header_blocknr(&leaf
->header
, bh_blocknr(subvol
));
1897 btrfs_set_header_generation(&leaf
->header
, trans
->transid
);
1898 memcpy(leaf
->header
.fsid
, root
->fs_info
->disk_super
->fsid
,
1899 sizeof(leaf
->header
.fsid
));
1901 inode_item
= &root_item
.inode
;
1902 memset(inode_item
, 0, sizeof(*inode_item
));
1903 btrfs_set_inode_generation(inode_item
, 1);
1904 btrfs_set_inode_size(inode_item
, 3);
1905 btrfs_set_inode_nlink(inode_item
, 1);
1906 btrfs_set_inode_nblocks(inode_item
, 1);
1907 btrfs_set_inode_mode(inode_item
, S_IFDIR
| 0755);
1909 btrfs_set_root_blocknr(&root_item
, bh_blocknr(subvol
));
1910 btrfs_set_root_refs(&root_item
, 1);
1912 mark_buffer_dirty(subvol
);
1916 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1920 btrfs_set_root_dirid(&root_item
, new_dirid
);
1922 key
.objectid
= objectid
;
1925 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1926 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
1931 * insert the directory item
1933 key
.offset
= (u64
)-1;
1934 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
1936 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
1940 ret
= btrfs_commit_transaction(trans
, root
);
1943 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
);
1946 trans
= btrfs_start_transaction(new_root
, 1);
1949 inode
= btrfs_new_inode(trans
, new_root
, new_dirid
, S_IFDIR
| 0700);
1950 inode
->i_op
= &btrfs_dir_inode_operations
;
1951 inode
->i_fop
= &btrfs_dir_file_operations
;
1953 ret
= btrfs_make_empty_dir(trans
, new_root
, new_dirid
, new_dirid
);
1958 ret
= btrfs_update_inode(trans
, new_root
, inode
);
1961 ret
= btrfs_commit_transaction(trans
, new_root
);
1966 mutex_unlock(&root
->fs_info
->fs_mutex
);
1970 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
1972 struct btrfs_trans_handle
*trans
;
1973 struct btrfs_key key
;
1974 struct btrfs_root_item new_root_item
;
1978 if (!root
->ref_cows
)
1981 mutex_lock(&root
->fs_info
->fs_mutex
);
1982 trans
= btrfs_start_transaction(root
, 1);
1985 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
1988 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
1992 memcpy(&new_root_item
, &root
->root_item
,
1993 sizeof(new_root_item
));
1995 key
.objectid
= objectid
;
1998 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
1999 btrfs_set_root_blocknr(&new_root_item
, bh_blocknr(root
->node
));
2001 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
2006 * insert the directory item
2008 key
.offset
= (u64
)-1;
2009 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
2011 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2016 ret
= btrfs_inc_root_ref(trans
, root
);
2019 ret
= btrfs_commit_transaction(trans
, root
);
2021 mutex_unlock(&root
->fs_info
->fs_mutex
);
2025 static int add_disk(struct btrfs_root
*root
, char *name
, int namelen
)
2027 struct block_device
*bdev
;
2028 struct btrfs_path
*path
;
2029 struct super_block
*sb
= root
->fs_info
->sb
;
2030 struct btrfs_root
*dev_root
= root
->fs_info
->dev_root
;
2031 struct btrfs_trans_handle
*trans
;
2032 struct btrfs_device_item
*dev_item
;
2033 struct btrfs_key key
;
2040 printk("adding disk %s\n", name
);
2041 path
= btrfs_alloc_path();
2044 num_blocks
= btrfs_super_total_blocks(root
->fs_info
->disk_super
);
2045 bdev
= open_bdev_excl(name
, O_RDWR
, sb
);
2047 ret
= PTR_ERR(bdev
);
2048 printk("open bdev excl failed ret %d\n", ret
);
2051 set_blocksize(bdev
, sb
->s_blocksize
);
2052 new_blocks
= bdev
->bd_inode
->i_size
>> sb
->s_blocksize_bits
;
2053 key
.objectid
= num_blocks
;
2054 key
.offset
= new_blocks
;
2056 btrfs_set_key_type(&key
, BTRFS_DEV_ITEM_KEY
);
2058 mutex_lock(&dev_root
->fs_info
->fs_mutex
);
2059 trans
= btrfs_start_transaction(dev_root
, 1);
2060 item_size
= sizeof(*dev_item
) + namelen
;
2061 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks
, new_blocks
, key
.flags
, item_size
);
2062 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
, &key
, item_size
);
2064 printk("insert failed %d\n", ret
);
2065 close_bdev_excl(bdev
);
2070 dev_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
2071 path
->slots
[0], struct btrfs_device_item
);
2072 btrfs_set_device_pathlen(dev_item
, namelen
);
2073 memcpy(dev_item
+ 1, name
, namelen
);
2075 device_id
= btrfs_super_last_device_id(root
->fs_info
->disk_super
) + 1;
2076 btrfs_set_super_last_device_id(root
->fs_info
->disk_super
, device_id
);
2077 btrfs_set_device_id(dev_item
, device_id
);
2078 mark_buffer_dirty(path
->nodes
[0]);
2080 ret
= btrfs_insert_dev_radix(root
, bdev
, device_id
, num_blocks
,
2084 btrfs_set_super_total_blocks(root
->fs_info
->disk_super
,
2085 num_blocks
+ new_blocks
);
2086 i_size_write(root
->fs_info
->btree_inode
,
2087 (num_blocks
+ new_blocks
) <<
2088 root
->fs_info
->btree_inode
->i_blkbits
);
2092 ret
= btrfs_commit_transaction(trans
, dev_root
);
2094 mutex_unlock(&root
->fs_info
->fs_mutex
);
2096 btrfs_free_path(path
);
2101 static int btrfs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int
2102 cmd
, unsigned long arg
)
2104 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2105 struct btrfs_ioctl_vol_args vol_args
;
2108 struct btrfs_path
*path
;
2112 case BTRFS_IOC_SNAP_CREATE
:
2113 if (copy_from_user(&vol_args
,
2114 (struct btrfs_ioctl_vol_args __user
*)arg
,
2117 namelen
= strlen(vol_args
.name
);
2118 if (namelen
> BTRFS_VOL_NAME_MAX
)
2120 path
= btrfs_alloc_path();
2123 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
2124 mutex_lock(&root
->fs_info
->fs_mutex
);
2125 ret
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
2127 vol_args
.name
, namelen
, 0);
2128 mutex_unlock(&root
->fs_info
->fs_mutex
);
2129 btrfs_free_path(path
);
2133 if (root
== root
->fs_info
->tree_root
)
2134 ret
= create_subvol(root
, vol_args
.name
, namelen
);
2136 ret
= create_snapshot(root
, vol_args
.name
, namelen
);
2139 case BTRFS_IOC_ADD_DISK
:
2140 if (copy_from_user(&vol_args
,
2141 (struct btrfs_ioctl_vol_args __user
*)arg
,
2144 namelen
= strlen(vol_args
.name
);
2145 if (namelen
> BTRFS_VOL_NAME_MAX
)
2147 vol_args
.name
[namelen
] = '\0';
2148 ret
= add_disk(root
, vol_args
.name
, namelen
);
2156 static struct kmem_cache
*btrfs_inode_cachep
;
2157 struct kmem_cache
*btrfs_trans_handle_cachep
;
2158 struct kmem_cache
*btrfs_transaction_cachep
;
2159 struct kmem_cache
*btrfs_bit_radix_cachep
;
2160 struct kmem_cache
*btrfs_path_cachep
;
2163 * Called inside transaction, so use GFP_NOFS
2165 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
2167 struct btrfs_inode
*ei
;
2169 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
2172 return &ei
->vfs_inode
;
2175 static void btrfs_destroy_inode(struct inode
*inode
)
2177 WARN_ON(!list_empty(&inode
->i_dentry
));
2178 WARN_ON(inode
->i_data
.nrpages
);
2180 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
2183 static void init_once(void * foo
, struct kmem_cache
* cachep
,
2184 unsigned long flags
)
2186 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
2188 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
2189 SLAB_CTOR_CONSTRUCTOR
) {
2190 inode_init_once(&ei
->vfs_inode
);
2194 static int init_inodecache(void)
2196 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
2197 sizeof(struct btrfs_inode
),
2198 0, (SLAB_RECLAIM_ACCOUNT
|
2201 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
2202 sizeof(struct btrfs_trans_handle
),
2203 0, (SLAB_RECLAIM_ACCOUNT
|
2206 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
2207 sizeof(struct btrfs_transaction
),
2208 0, (SLAB_RECLAIM_ACCOUNT
|
2211 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
2212 sizeof(struct btrfs_transaction
),
2213 0, (SLAB_RECLAIM_ACCOUNT
|
2216 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
2218 0, (SLAB_RECLAIM_ACCOUNT
|
2220 SLAB_DESTROY_BY_RCU
),
2222 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
2223 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
2228 static void destroy_inodecache(void)
2230 kmem_cache_destroy(btrfs_inode_cachep
);
2231 kmem_cache_destroy(btrfs_trans_handle_cachep
);
2232 kmem_cache_destroy(btrfs_transaction_cachep
);
2233 kmem_cache_destroy(btrfs_bit_radix_cachep
);
2234 kmem_cache_destroy(btrfs_path_cachep
);
2237 static int btrfs_get_sb(struct file_system_type
*fs_type
,
2238 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
2240 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
2241 btrfs_fill_super
, mnt
);
2244 static struct file_system_type btrfs_fs_type
= {
2245 .owner
= THIS_MODULE
,
2247 .get_sb
= btrfs_get_sb
,
2248 .kill_sb
= kill_block_super
,
2249 .fs_flags
= FS_REQUIRES_DEV
,
2252 static struct super_operations btrfs_super_ops
= {
2253 .statfs
= simple_statfs
,
2254 .delete_inode
= btrfs_delete_inode
,
2255 .put_super
= btrfs_put_super
,
2256 .read_inode
= btrfs_read_locked_inode
,
2257 .write_super
= btrfs_write_super
,
2258 .sync_fs
= btrfs_sync_fs
,
2259 .write_inode
= btrfs_write_inode
,
2260 .alloc_inode
= btrfs_alloc_inode
,
2261 .destroy_inode
= btrfs_destroy_inode
,
2264 static struct inode_operations btrfs_dir_inode_operations
= {
2265 .lookup
= btrfs_lookup
,
2266 .create
= btrfs_create
,
2267 .unlink
= btrfs_unlink
,
2268 .mkdir
= btrfs_mkdir
,
2269 .rmdir
= btrfs_rmdir
,
2272 static struct inode_operations btrfs_dir_ro_inode_operations
= {
2273 .lookup
= btrfs_lookup
,
2276 static struct file_operations btrfs_dir_file_operations
= {
2277 .llseek
= generic_file_llseek
,
2278 .read
= generic_read_dir
,
2279 .readdir
= btrfs_readdir
,
2280 .ioctl
= btrfs_ioctl
,
2283 static struct address_space_operations btrfs_aops
= {
2284 .readpage
= btrfs_readpage
,
2285 .writepage
= btrfs_writepage
,
2286 .sync_page
= block_sync_page
,
2287 .prepare_write
= btrfs_prepare_write
,
2288 .commit_write
= btrfs_commit_write
,
2291 static struct inode_operations btrfs_file_inode_operations
= {
2292 .truncate
= btrfs_truncate
,
2295 static struct file_operations btrfs_file_operations
= {
2296 .llseek
= generic_file_llseek
,
2297 .read
= do_sync_read
,
2298 .aio_read
= btrfs_file_aio_read
,
2299 .write
= btrfs_file_write
,
2300 .mmap
= generic_file_mmap
,
2301 .open
= generic_file_open
,
2302 .ioctl
= btrfs_ioctl
,
2305 static int __init
init_btrfs_fs(void)
2308 printk("btrfs loaded!\n");
2309 err
= init_inodecache();
2312 kset_set_kset_s(&btrfs_subsys
, fs_subsys
);
2313 err
= subsystem_register(&btrfs_subsys
);
2316 return register_filesystem(&btrfs_fs_type
);
2318 destroy_inodecache();
2322 static void __exit
exit_btrfs_fs(void)
2324 destroy_inodecache();
2325 unregister_filesystem(&btrfs_fs_type
);
2326 subsystem_unregister(&btrfs_subsys
);
2327 printk("btrfs unloaded\n");
2330 module_init(init_btrfs_fs
)
2331 module_exit(exit_btrfs_fs
)
2333 MODULE_LICENSE("GPL");