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"
19 #define BTRFS_SUPER_MAGIC 0x9123682E
21 static struct inode_operations btrfs_dir_inode_operations
;
22 static struct super_operations btrfs_super_ops
;
23 static struct file_operations btrfs_dir_file_operations
;
24 static struct inode_operations btrfs_file_inode_operations
;
25 static struct address_space_operations btrfs_aops
;
26 static struct file_operations btrfs_file_operations
;
28 static int check_inode(struct inode
*inode
)
30 struct btrfs_inode
*ei
= BTRFS_I(inode
);
31 WARN_ON(ei
->magic
!= 0xDEADBEEF);
32 WARN_ON(ei
->magic2
!= 0xDEADBEAF);
36 static void btrfs_read_locked_inode(struct inode
*inode
)
38 struct btrfs_path
*path
;
39 struct btrfs_inode_item
*inode_item
;
40 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
43 path
= btrfs_alloc_path();
45 btrfs_init_path(path
);
46 mutex_lock(&root
->fs_info
->fs_mutex
);
49 ret
= btrfs_lookup_inode(NULL
, root
, path
, inode
->i_ino
, 0);
51 btrfs_release_path(root
, path
);
52 mutex_unlock(&root
->fs_info
->fs_mutex
);
53 make_bad_inode(inode
);
57 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
59 struct btrfs_inode_item
);
61 inode
->i_mode
= btrfs_inode_mode(inode_item
);
62 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
63 inode
->i_uid
= btrfs_inode_uid(inode_item
);
64 inode
->i_gid
= btrfs_inode_gid(inode_item
);
65 inode
->i_size
= btrfs_inode_size(inode_item
);
66 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
67 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
68 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
69 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
70 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
71 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
72 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
73 inode
->i_generation
= btrfs_inode_generation(inode_item
);
75 btrfs_release_path(root
, path
);
76 btrfs_free_path(path
);
79 mutex_unlock(&root
->fs_info
->fs_mutex
);
81 switch (inode
->i_mode
& S_IFMT
) {
84 init_special_inode(inode
, inode
->i_mode
,
85 btrfs_inode_rdev(inode_item
));
89 inode
->i_mapping
->a_ops
= &btrfs_aops
;
90 inode
->i_fop
= &btrfs_file_operations
;
91 inode
->i_op
= &btrfs_file_inode_operations
;
94 inode
->i_op
= &btrfs_dir_inode_operations
;
95 inode
->i_fop
= &btrfs_dir_file_operations
;
98 // inode->i_op = &page_symlink_inode_operations;
105 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
106 struct btrfs_root
*root
,
108 struct dentry
*dentry
)
110 struct btrfs_path
*path
;
111 const char *name
= dentry
->d_name
.name
;
112 int name_len
= dentry
->d_name
.len
;
115 struct btrfs_dir_item
*di
;
117 path
= btrfs_alloc_path();
119 btrfs_init_path(path
);
120 ret
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
128 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
129 struct btrfs_dir_item
);
130 objectid
= btrfs_dir_objectid(di
);
132 ret
= btrfs_del_item(trans
, root
, path
);
134 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
136 btrfs_release_path(root
, path
);
137 btrfs_free_path(path
);
139 inode_dec_link_count(dentry
->d_inode
);
143 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
145 struct btrfs_root
*root
;
146 struct btrfs_trans_handle
*trans
;
149 root
= btrfs_sb(dir
->i_sb
);
150 mutex_lock(&root
->fs_info
->fs_mutex
);
151 trans
= btrfs_start_transaction(root
, 1);
152 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
153 btrfs_end_transaction(trans
, root
);
154 mutex_unlock(&root
->fs_info
->fs_mutex
);
158 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
160 struct inode
*inode
= dentry
->d_inode
;
163 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
164 struct btrfs_path
*path
;
165 struct btrfs_key key
;
166 struct btrfs_trans_handle
*trans
;
167 struct btrfs_disk_key
*found_key
;
168 struct btrfs_leaf
*leaf
;
170 path
= btrfs_alloc_path();
172 btrfs_init_path(path
);
173 mutex_lock(&root
->fs_info
->fs_mutex
);
174 trans
= btrfs_start_transaction(root
, 1);
175 key
.objectid
= inode
->i_ino
;
176 key
.offset
= (u64
)-1;
178 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
179 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
186 BUG_ON(path
->slots
[0] == 0);
188 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
189 found_key
= &leaf
->items
[path
->slots
[0]].key
;
190 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
) {
194 if (btrfs_disk_key_type(found_key
) != BTRFS_DIR_ITEM_KEY
||
195 btrfs_disk_key_offset(found_key
) != 2) {
199 ret
= btrfs_del_item(trans
, root
, path
);
201 btrfs_release_path(root
, path
);
203 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
212 ret
= btrfs_del_item(trans
, root
, path
);
217 btrfs_release_path(root
, path
);
218 btrfs_free_path(path
);
220 /* now the directory is empty */
221 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
226 mutex_unlock(&root
->fs_info
->fs_mutex
);
227 ret
= btrfs_end_transaction(trans
, root
);
233 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
234 struct btrfs_root
*root
,
237 u64 objectid
= inode
->i_ino
;
238 struct btrfs_path
*path
;
239 struct btrfs_inode_map_item
*map
;
240 struct btrfs_key stat_data_key
;
245 path
= btrfs_alloc_path();
247 btrfs_init_path(path
);
248 ret
= btrfs_lookup_inode_map(trans
, root
, path
, objectid
, -1);
254 map
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
255 struct btrfs_inode_map_item
);
256 btrfs_disk_key_to_cpu(&stat_data_key
, &map
->key
);
257 ret
= btrfs_del_item(trans
, root
->fs_info
->inode_root
, path
);
259 btrfs_release_path(root
, path
);
261 ret
= btrfs_lookup_inode(trans
, root
, path
, objectid
, -1);
263 ret
= btrfs_del_item(trans
, root
, path
);
266 btrfs_release_path(root
, path
);
267 btrfs_free_path(path
);
271 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
272 struct btrfs_root
*root
,
276 struct btrfs_path
*path
;
277 struct btrfs_key key
;
278 struct btrfs_disk_key
*found_key
;
279 struct btrfs_leaf
*leaf
;
280 struct btrfs_file_extent_item
*fi
= NULL
;
281 u64 extent_start
= 0;
282 u64 extent_num_blocks
= 0;
285 path
= btrfs_alloc_path();
287 /* FIXME, add redo link to tree so we don't leak on crash */
288 key
.objectid
= inode
->i_ino
;
289 key
.offset
= (u64
)-1;
291 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
293 btrfs_init_path(path
);
294 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
299 BUG_ON(path
->slots
[0] == 0);
302 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
303 found_key
= &leaf
->items
[path
->slots
[0]].key
;
304 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
306 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
307 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
309 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
311 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
312 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
314 struct btrfs_file_extent_item
);
315 extent_start
= btrfs_file_extent_disk_blocknr(fi
);
317 btrfs_file_extent_disk_num_blocks(fi
);
319 btrfs_file_extent_num_blocks(fi
) >> 9;
324 ret
= btrfs_del_item(trans
, root
, path
);
326 btrfs_release_path(root
, path
);
328 ret
= btrfs_free_extent(trans
, root
, extent_start
,
329 extent_num_blocks
, 0);
335 btrfs_release_path(root
, path
);
336 btrfs_free_path(path
);
340 static void btrfs_delete_inode(struct inode
*inode
)
342 struct btrfs_trans_handle
*trans
;
343 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
346 truncate_inode_pages(&inode
->i_data
, 0);
347 if (is_bad_inode(inode
)) {
351 mutex_lock(&root
->fs_info
->fs_mutex
);
352 trans
= btrfs_start_transaction(root
, 1);
353 if (S_ISREG(inode
->i_mode
)) {
354 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
357 btrfs_free_inode(trans
, root
, inode
);
358 btrfs_end_transaction(trans
, root
);
359 mutex_unlock(&root
->fs_info
->fs_mutex
);
365 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
368 const char *name
= dentry
->d_name
.name
;
369 int namelen
= dentry
->d_name
.len
;
370 struct btrfs_dir_item
*di
;
371 struct btrfs_path
*path
;
372 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
375 path
= btrfs_alloc_path();
377 btrfs_init_path(path
);
378 ret
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
380 if (ret
|| !btrfs_match_dir_item_name(root
, path
, name
, namelen
)) {
385 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
386 struct btrfs_dir_item
);
387 *ino
= btrfs_dir_objectid(di
);
389 btrfs_release_path(root
, path
);
390 btrfs_free_path(path
);
395 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
396 struct nameidata
*nd
)
398 struct inode
* inode
;
399 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
403 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
404 return ERR_PTR(-ENAMETOOLONG
);
405 mutex_lock(&root
->fs_info
->fs_mutex
);
406 ret
= btrfs_inode_by_name(dir
, dentry
, &ino
);
407 mutex_unlock(&root
->fs_info
->fs_mutex
);
412 inode
= iget(dir
->i_sb
, ino
);
414 return ERR_PTR(-EACCES
);
418 return d_splice_alias(inode
, dentry
);
421 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
423 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
424 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
425 struct btrfs_item
*item
;
426 struct btrfs_dir_item
*di
;
427 struct btrfs_key key
;
428 struct btrfs_path
*path
;
431 struct btrfs_leaf
*leaf
;
434 unsigned char d_type
= DT_UNKNOWN
;
437 mutex_lock(&root
->fs_info
->fs_mutex
);
438 key
.objectid
= inode
->i_ino
;
440 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
441 key
.offset
= filp
->f_pos
;
442 path
= btrfs_alloc_path();
443 btrfs_init_path(path
);
444 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
450 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
451 nritems
= btrfs_header_nritems(&leaf
->header
);
452 slot
= path
->slots
[0];
453 if (advance
|| slot
>= nritems
) {
454 if (slot
>= nritems
-1) {
455 ret
= btrfs_next_leaf(root
, path
);
458 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
459 nritems
= btrfs_header_nritems(&leaf
->header
);
460 slot
= path
->slots
[0];
467 item
= leaf
->items
+ slot
;
468 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
470 if (btrfs_disk_key_type(&item
->key
) != BTRFS_DIR_ITEM_KEY
)
472 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
476 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
477 over
= filldir(dirent
, (const char *)(di
+ 1),
478 btrfs_dir_name_len(di
),
479 btrfs_disk_key_offset(&item
->key
),
480 btrfs_dir_objectid(di
), d_type
);
482 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
485 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
) + 1;
489 btrfs_release_path(root
, path
);
490 btrfs_free_path(path
);
491 mutex_unlock(&root
->fs_info
->fs_mutex
);
495 static void btrfs_put_super (struct super_block
* sb
)
497 struct btrfs_root
*root
= btrfs_sb(sb
);
500 ret
= close_ctree(root
);
502 printk("close ctree returns %d\n", ret
);
504 sb
->s_fs_info
= NULL
;
507 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
509 struct inode
* inode
;
510 struct dentry
* root_dentry
;
511 struct btrfs_super_block
*disk_super
;
512 struct btrfs_root
*root
;
514 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
515 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
516 sb
->s_op
= &btrfs_super_ops
;
519 root
= open_ctree(sb
);
522 printk("btrfs: open_ctree failed\n");
525 sb
->s_fs_info
= root
;
526 disk_super
= root
->fs_info
->disk_super
;
527 printk("read in super total blocks %Lu root %Lu\n",
528 btrfs_super_total_blocks(disk_super
),
529 btrfs_super_root_dir(disk_super
));
531 inode
= iget_locked(sb
, btrfs_super_root_dir(disk_super
));
534 if (inode
->i_state
& I_NEW
) {
535 btrfs_read_locked_inode(inode
);
536 unlock_new_inode(inode
);
539 root_dentry
= d_alloc_root(inode
);
544 sb
->s_root
= root_dentry
;
549 static void fill_inode_item(struct btrfs_inode_item
*item
,
552 btrfs_set_inode_uid(item
, inode
->i_uid
);
553 btrfs_set_inode_gid(item
, inode
->i_gid
);
554 btrfs_set_inode_size(item
, inode
->i_size
);
555 btrfs_set_inode_mode(item
, inode
->i_mode
);
556 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
557 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
558 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
559 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
560 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
561 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
562 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
563 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
564 btrfs_set_inode_generation(item
, inode
->i_generation
);
568 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
569 struct btrfs_root
*root
,
572 struct btrfs_inode_item
*inode_item
;
573 struct btrfs_path
*path
;
576 path
= btrfs_alloc_path();
578 btrfs_init_path(path
);
580 ret
= btrfs_lookup_inode(trans
, root
, path
, inode
->i_ino
, 1);
587 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
589 struct btrfs_inode_item
);
591 fill_inode_item(inode_item
, inode
);
592 btrfs_mark_buffer_dirty(path
->nodes
[0]);
594 btrfs_release_path(root
, path
);
595 btrfs_free_path(path
);
600 static int btrfs_write_inode(struct inode
*inode
, int wait
)
602 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
603 struct btrfs_trans_handle
*trans
;
606 mutex_lock(&root
->fs_info
->fs_mutex
);
607 trans
= btrfs_start_transaction(root
, 1);
608 ret
= btrfs_update_inode(trans
, root
, inode
);
610 btrfs_commit_transaction(trans
, root
);
612 btrfs_end_transaction(trans
, root
);
613 mutex_unlock(&root
->fs_info
->fs_mutex
);
618 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
619 struct inode
*dir
, int mode
)
622 struct btrfs_inode_item inode_item
;
623 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
624 struct btrfs_key key
;
628 inode
= new_inode(dir
->i_sb
);
630 return ERR_PTR(-ENOMEM
);
633 ret
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
636 inode
->i_uid
= current
->fsuid
;
637 inode
->i_gid
= current
->fsgid
;
638 inode
->i_mode
= mode
;
639 inode
->i_ino
= objectid
;
641 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
642 fill_inode_item(&inode_item
, inode
);
644 key
.objectid
= objectid
;
647 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
648 ret
= btrfs_insert_inode_map(trans
, root
, objectid
, &key
);
651 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
654 insert_inode_hash(inode
);
660 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
661 struct dentry
*dentry
, struct inode
*inode
)
664 ret
= btrfs_insert_dir_item(trans
, btrfs_sb(inode
->i_sb
),
665 dentry
->d_name
.name
, dentry
->d_name
.len
,
666 dentry
->d_parent
->d_inode
->i_ino
,
669 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
;
670 ret
= btrfs_update_inode(trans
, btrfs_sb(inode
->i_sb
),
671 dentry
->d_parent
->d_inode
);
674 check_inode(dentry
->d_parent
->d_inode
);
678 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
679 struct dentry
*dentry
, struct inode
*inode
)
681 int err
= btrfs_add_link(trans
, dentry
, inode
);
683 d_instantiate(dentry
, inode
);
692 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
693 int mode
, struct nameidata
*nd
)
695 struct btrfs_trans_handle
*trans
;
696 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
701 mutex_lock(&root
->fs_info
->fs_mutex
);
702 trans
= btrfs_start_transaction(root
, 1);
703 inode
= btrfs_new_inode(trans
, dir
, mode
);
704 err
= PTR_ERR(inode
);
707 // FIXME mark the inode dirty
708 err
= btrfs_add_nondir(trans
, dentry
, inode
);
712 inode
->i_mapping
->a_ops
= &btrfs_aops
;
713 inode
->i_fop
= &btrfs_file_operations
;
714 inode
->i_op
= &btrfs_file_inode_operations
;
716 dir
->i_sb
->s_dirt
= 1;
718 btrfs_end_transaction(trans
, root
);
719 mutex_unlock(&root
->fs_info
->fs_mutex
);
724 inode_dec_link_count(inode
);
730 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
731 struct inode
*inode
, struct inode
*dir
)
733 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
739 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, inode
->i_ino
,
743 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, inode
->i_ino
,
748 ret
= btrfs_update_inode(trans
, root
, inode
);
753 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
756 struct btrfs_trans_handle
*trans
;
757 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
761 mutex_lock(&root
->fs_info
->fs_mutex
);
762 trans
= btrfs_start_transaction(root
, 1);
764 err
= PTR_ERR(trans
);
767 inode
= btrfs_new_inode(trans
, dir
, S_IFDIR
| mode
);
769 err
= PTR_ERR(inode
);
773 inode
->i_op
= &btrfs_dir_inode_operations
;
774 inode
->i_fop
= &btrfs_dir_file_operations
;
776 err
= btrfs_make_empty_dir(trans
, inode
, dir
);
779 err
= btrfs_add_link(trans
, dentry
, inode
);
782 d_instantiate(dentry
, inode
);
786 btrfs_end_transaction(trans
, root
);
788 mutex_unlock(&root
->fs_info
->fs_mutex
);
794 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
796 struct btrfs_trans_handle
*trans
;
797 struct btrfs_root
*root
;
803 // filemap_flush(root->fs_info->btree_inode->i_mapping);
804 filemap_flush(root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
);
807 filemap_write_and_wait(root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
);
808 mutex_lock(&root
->fs_info
->fs_mutex
);
809 trans
= btrfs_start_transaction(root
, 1);
810 ret
= btrfs_commit_transaction(trans
, root
);
813 printk("btrfs sync_fs\n");
814 mutex_unlock(&root
->fs_info
->fs_mutex
);
818 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
819 struct buffer_head
*result
, int create
)
824 u64 extent_start
= 0;
826 u64 objectid
= inode
->i_ino
;
827 struct btrfs_path
*path
;
828 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
829 struct btrfs_trans_handle
*trans
= NULL
;
830 struct btrfs_file_extent_item
*item
;
831 struct btrfs_leaf
*leaf
;
832 struct btrfs_disk_key
*found_key
;
834 path
= btrfs_alloc_path();
836 btrfs_init_path(path
);
838 trans
= btrfs_start_transaction(root
, 1);
841 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
843 iblock
<< inode
->i_blkbits
, 0);
850 if (path
->slots
[0] == 0) {
851 btrfs_release_path(root
, path
);
857 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
858 struct btrfs_file_extent_item
);
859 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
860 blocknr
= btrfs_file_extent_disk_blocknr(item
);
861 blocknr
+= btrfs_file_extent_offset(item
);
863 /* exact match found, use it */
866 map_bh(result
, inode
->i_sb
, blocknr
);
870 /* are we inside the extent that was found? */
871 found_key
= &leaf
->items
[path
->slots
[0]].key
;
872 if (btrfs_disk_key_objectid(found_key
) != objectid
||
873 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
) {
876 btrfs_release_path(root
, path
);
880 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
881 extent_start
= extent_start
>> inode
->i_blkbits
;
882 extent_start
+= btrfs_file_extent_offset(item
);
883 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
884 if (iblock
>= extent_start
&& iblock
< extent_end
) {
886 map_bh(result
, inode
->i_sb
, blocknr
+ iblock
- extent_start
);
890 /* ok, create a new extent */
895 ret
= btrfs_alloc_file_extent(trans
, root
, objectid
,
896 iblock
<< inode
->i_blkbits
,
897 1, extent_end
, &blocknr
);
902 inode
->i_blocks
+= inode
->i_sb
->s_blocksize
>> 9;
903 set_buffer_new(result
);
904 map_bh(result
, inode
->i_sb
, blocknr
);
907 btrfs_release_path(root
, path
);
908 btrfs_free_path(path
);
910 btrfs_end_transaction(trans
, root
);
914 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
915 struct buffer_head
*result
, int create
)
918 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
919 mutex_lock(&root
->fs_info
->fs_mutex
);
920 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
921 mutex_unlock(&root
->fs_info
->fs_mutex
);
925 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
926 unsigned from
, unsigned to
)
929 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
931 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
932 unsigned from
, unsigned to
)
935 return nobh_commit_write(file
, page
, from
, to
);
938 static void btrfs_write_super(struct super_block
*sb
)
940 btrfs_sync_fs(sb
, 1);
943 static int btrfs_readpage(struct file
*file
, struct page
*page
)
945 return mpage_readpage(page
, btrfs_get_block
);
948 static int btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
949 struct list_head
*pages
, unsigned nr_pages
)
951 return mpage_readpages(mapping
, pages
, nr_pages
, btrfs_get_block
);
954 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
956 return nobh_writepage(page
, btrfs_get_block
, wbc
);
959 static void btrfs_truncate(struct inode
*inode
)
961 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
963 struct btrfs_trans_handle
*trans
;
965 if (!S_ISREG(inode
->i_mode
))
967 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
970 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
972 /* FIXME, add redo link to tree so we don't leak on crash */
973 mutex_lock(&root
->fs_info
->fs_mutex
);
974 trans
= btrfs_start_transaction(root
, 1);
975 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
977 ret
= btrfs_end_transaction(trans
, root
);
979 mutex_unlock(&root
->fs_info
->fs_mutex
);
980 mark_inode_dirty(inode
);
983 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
984 struct page
**prepared_pages
,
985 const char __user
* buf
)
989 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
991 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
992 size_t count
= min_t(size_t,
993 PAGE_CACHE_SIZE
- offset
, write_bytes
);
994 struct page
*page
= prepared_pages
[i
];
995 fault_in_pages_readable(buf
, count
);
997 /* Copy data from userspace to the current page */
999 page_fault
= __copy_from_user(page_address(page
) + offset
,
1001 /* Flush processor's dcache for this page */
1002 flush_dcache_page(page
);
1005 write_bytes
-= count
;
1010 return page_fault
? -EFAULT
: 0;
1013 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1016 for (i
= 0; i
< num_pages
; i
++) {
1019 unlock_page(pages
[i
]);
1020 mark_page_accessed(pages
[i
]);
1021 page_cache_release(pages
[i
]);
1024 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1025 struct btrfs_root
*root
,
1027 struct page
**pages
,
1037 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1039 for (i
= 0; i
< num_pages
; i
++) {
1040 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1041 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1042 /* FIXME, one block at a time */
1044 mutex_lock(&root
->fs_info
->fs_mutex
);
1045 trans
= btrfs_start_transaction(root
, 1);
1046 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1047 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1048 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1050 SetPageChecked(pages
[i
]);
1051 ret
= btrfs_end_transaction(trans
, root
);
1053 mutex_unlock(&root
->fs_info
->fs_mutex
);
1055 ret
= nobh_commit_write(file
, pages
[i
], offset
,
1056 offset
+ this_write
);
1062 WARN_ON(this_write
> write_bytes
);
1063 write_bytes
-= this_write
;
1069 static int prepare_pages(struct btrfs_trans_handle
*trans
,
1070 struct btrfs_root
*root
,
1072 struct page
**pages
,
1078 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1079 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1084 loff_t isize
= i_size_read(inode
);
1086 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1088 for (i
= 0; i
< num_pages
; i
++) {
1089 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1092 goto failed_release
;
1094 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1095 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1096 ret
= nobh_prepare_write(pages
[i
], offset
,
1097 offset
+ this_write
,
1102 goto failed_truncate
;
1104 WARN_ON(this_write
> write_bytes
);
1105 write_bytes
-= this_write
;
1110 btrfs_drop_pages(pages
, num_pages
);
1114 btrfs_drop_pages(pages
, num_pages
);
1116 vmtruncate(inode
, isize
);
1120 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1121 size_t count
, loff_t
*ppos
)
1124 size_t num_written
= 0;
1127 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1128 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
1129 struct page
*pages
[1];
1131 if (file
->f_flags
& O_DIRECT
)
1135 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1136 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1137 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1142 err
= remove_suid(file
->f_path
.dentry
);
1145 file_update_time(file
);
1146 mutex_lock(&inode
->i_mutex
);
1148 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1149 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1150 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1152 ret
= prepare_pages(NULL
, root
, file
, pages
, num_pages
,
1155 ret
= btrfs_copy_from_user(pos
, num_pages
,
1156 write_bytes
, pages
, buf
);
1159 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1160 num_pages
, pos
, write_bytes
);
1162 btrfs_drop_pages(pages
, num_pages
);
1165 count
-= write_bytes
;
1167 num_written
+= write_bytes
;
1169 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1172 mutex_unlock(&inode
->i_mutex
);
1175 current
->backing_dev_info
= NULL
;
1176 return num_written
? num_written
: err
;
1179 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1180 unsigned long offset
, unsigned long size
)
1183 unsigned long left
, count
= desc
->count
;
1188 if (!PageChecked(page
)) {
1189 /* FIXME, do it per block */
1190 struct btrfs_root
*root
= btrfs_sb(page
->mapping
->host
->i_sb
);
1191 int ret
= btrfs_csum_verify_file_block(root
,
1192 page
->mapping
->host
->i_ino
,
1193 page
->index
<< PAGE_CACHE_SHIFT
,
1194 kmap(page
), PAGE_CACHE_SIZE
);
1196 printk("failed to verify ino %lu page %lu\n",
1197 page
->mapping
->host
->i_ino
,
1199 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
1201 SetPageChecked(page
);
1205 * Faults on the destination of a read are common, so do it before
1208 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1209 kaddr
= kmap_atomic(page
, KM_USER0
);
1210 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1211 kaddr
+ offset
, size
);
1212 kunmap_atomic(kaddr
, KM_USER0
);
1217 /* Do it the slow way */
1219 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1224 desc
->error
= -EFAULT
;
1227 desc
->count
= count
- size
;
1228 desc
->written
+= size
;
1229 desc
->arg
.buf
+= size
;
1234 * btrfs_file_aio_read - filesystem read routine
1235 * @iocb: kernel I/O control block
1236 * @iov: io vector request
1237 * @nr_segs: number of segments in the iovec
1238 * @pos: current file position
1240 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1241 unsigned long nr_segs
, loff_t pos
)
1243 struct file
*filp
= iocb
->ki_filp
;
1247 loff_t
*ppos
= &iocb
->ki_pos
;
1250 for (seg
= 0; seg
< nr_segs
; seg
++) {
1251 const struct iovec
*iv
= &iov
[seg
];
1254 * If any segment has a negative length, or the cumulative
1255 * length ever wraps negative then return -EINVAL.
1257 count
+= iv
->iov_len
;
1258 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1260 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1265 count
-= iv
->iov_len
; /* This segment is no good */
1270 for (seg
= 0; seg
< nr_segs
; seg
++) {
1271 read_descriptor_t desc
;
1274 desc
.arg
.buf
= iov
[seg
].iov_base
;
1275 desc
.count
= iov
[seg
].iov_len
;
1276 if (desc
.count
== 0)
1279 do_generic_file_read(filp
, ppos
, &desc
,
1281 retval
+= desc
.written
;
1283 retval
= retval
?: desc
.error
;
1291 static struct kmem_cache
*btrfs_inode_cachep
;
1292 struct kmem_cache
*btrfs_trans_handle_cachep
;
1293 struct kmem_cache
*btrfs_transaction_cachep
;
1294 struct kmem_cache
*btrfs_bit_radix_cachep
;
1295 struct kmem_cache
*btrfs_path_cachep
;
1298 * Called inside transaction, so use GFP_NOFS
1300 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
1302 struct btrfs_inode
*ei
;
1304 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
1307 ei
->magic
= 0xDEADBEEF;
1308 ei
->magic2
= 0xDEADBEAF;
1309 return &ei
->vfs_inode
;
1312 static void btrfs_destroy_inode(struct inode
*inode
)
1314 struct btrfs_inode
*ei
= BTRFS_I(inode
);
1315 WARN_ON(ei
->magic
!= 0xDEADBEEF);
1316 WARN_ON(ei
->magic2
!= 0xDEADBEAF);
1317 WARN_ON(!list_empty(&inode
->i_dentry
));
1318 WARN_ON(inode
->i_ino
== 1);
1319 WARN_ON(inode
->i_data
.nrpages
);
1323 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
1326 static void init_once(void * foo
, struct kmem_cache
* cachep
,
1327 unsigned long flags
)
1329 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
1331 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
1332 SLAB_CTOR_CONSTRUCTOR
) {
1333 inode_init_once(&ei
->vfs_inode
);
1337 static int init_inodecache(void)
1339 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
1340 sizeof(struct btrfs_inode
),
1341 0, (SLAB_RECLAIM_ACCOUNT
|
1344 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
1345 sizeof(struct btrfs_trans_handle
),
1346 0, (SLAB_RECLAIM_ACCOUNT
|
1349 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
1350 sizeof(struct btrfs_transaction
),
1351 0, (SLAB_RECLAIM_ACCOUNT
|
1354 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
1355 sizeof(struct btrfs_transaction
),
1356 0, (SLAB_RECLAIM_ACCOUNT
|
1359 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
1361 0, (SLAB_RECLAIM_ACCOUNT
|
1363 SLAB_DESTROY_BY_RCU
),
1365 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
1366 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
1371 static void destroy_inodecache(void)
1373 kmem_cache_destroy(btrfs_inode_cachep
);
1374 kmem_cache_destroy(btrfs_trans_handle_cachep
);
1375 kmem_cache_destroy(btrfs_transaction_cachep
);
1376 kmem_cache_destroy(btrfs_bit_radix_cachep
);
1377 kmem_cache_destroy(btrfs_path_cachep
);
1380 static int btrfs_get_sb(struct file_system_type
*fs_type
,
1381 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1383 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
1384 btrfs_fill_super
, mnt
);
1387 static struct file_system_type btrfs_fs_type
= {
1388 .owner
= THIS_MODULE
,
1390 .get_sb
= btrfs_get_sb
,
1391 .kill_sb
= kill_block_super
,
1392 .fs_flags
= FS_REQUIRES_DEV
,
1395 static struct super_operations btrfs_super_ops
= {
1396 .statfs
= simple_statfs
,
1397 .delete_inode
= btrfs_delete_inode
,
1398 .put_super
= btrfs_put_super
,
1399 .read_inode
= btrfs_read_locked_inode
,
1400 .write_super
= btrfs_write_super
,
1401 .sync_fs
= btrfs_sync_fs
,
1402 .write_inode
= btrfs_write_inode
,
1403 .alloc_inode
= btrfs_alloc_inode
,
1404 .destroy_inode
= btrfs_destroy_inode
,
1407 static struct inode_operations btrfs_dir_inode_operations
= {
1408 .lookup
= btrfs_lookup
,
1409 .create
= btrfs_create
,
1410 .unlink
= btrfs_unlink
,
1411 .mkdir
= btrfs_mkdir
,
1412 .rmdir
= btrfs_rmdir
,
1415 static struct file_operations btrfs_dir_file_operations
= {
1416 .llseek
= generic_file_llseek
,
1417 .read
= generic_read_dir
,
1418 .readdir
= btrfs_readdir
,
1421 static struct address_space_operations btrfs_aops
= {
1422 .readpage
= btrfs_readpage
,
1423 .readpages
= btrfs_readpages
,
1424 .writepage
= btrfs_writepage
,
1425 .sync_page
= block_sync_page
,
1426 .prepare_write
= btrfs_prepare_write
,
1427 .commit_write
= btrfs_commit_write
,
1430 static struct inode_operations btrfs_file_inode_operations
= {
1431 .truncate
= btrfs_truncate
,
1434 static struct file_operations btrfs_file_operations
= {
1435 .llseek
= generic_file_llseek
,
1436 .read
= do_sync_read
,
1437 .aio_read
= btrfs_file_aio_read
,
1438 .write
= btrfs_file_write
,
1439 .mmap
= generic_file_mmap
,
1440 .open
= generic_file_open
,
1443 static int __init
init_btrfs_fs(void)
1446 printk("btrfs loaded!\n");
1447 err
= init_inodecache();
1450 return register_filesystem(&btrfs_fs_type
);
1453 static void __exit
exit_btrfs_fs(void)
1455 destroy_inodecache();
1456 unregister_filesystem(&btrfs_fs_type
);
1457 printk("btrfs unloaded\n");
1460 module_init(init_btrfs_fs
)
1461 module_exit(exit_btrfs_fs
)
1463 MODULE_LICENSE("GPL");