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 btrfs_free_path(path
);
53 mutex_unlock(&root
->fs_info
->fs_mutex
);
54 make_bad_inode(inode
);
58 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
60 struct btrfs_inode_item
);
62 inode
->i_mode
= btrfs_inode_mode(inode_item
);
63 inode
->i_nlink
= btrfs_inode_nlink(inode_item
);
64 inode
->i_uid
= btrfs_inode_uid(inode_item
);
65 inode
->i_gid
= btrfs_inode_gid(inode_item
);
66 inode
->i_size
= btrfs_inode_size(inode_item
);
67 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(&inode_item
->atime
);
68 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->atime
);
69 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(&inode_item
->mtime
);
70 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->mtime
);
71 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(&inode_item
->ctime
);
72 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(&inode_item
->ctime
);
73 inode
->i_blocks
= btrfs_inode_nblocks(inode_item
);
74 inode
->i_generation
= btrfs_inode_generation(inode_item
);
76 btrfs_release_path(root
, path
);
77 btrfs_free_path(path
);
80 mutex_unlock(&root
->fs_info
->fs_mutex
);
82 switch (inode
->i_mode
& S_IFMT
) {
85 init_special_inode(inode
, inode
->i_mode
,
86 btrfs_inode_rdev(inode_item
));
90 inode
->i_mapping
->a_ops
= &btrfs_aops
;
91 inode
->i_fop
= &btrfs_file_operations
;
92 inode
->i_op
= &btrfs_file_inode_operations
;
95 inode
->i_op
= &btrfs_dir_inode_operations
;
96 inode
->i_fop
= &btrfs_dir_file_operations
;
99 // inode->i_op = &page_symlink_inode_operations;
106 static int btrfs_unlink_trans(struct btrfs_trans_handle
*trans
,
107 struct btrfs_root
*root
,
109 struct dentry
*dentry
)
111 struct btrfs_path
*path
;
112 const char *name
= dentry
->d_name
.name
;
113 int name_len
= dentry
->d_name
.len
;
116 struct btrfs_dir_item
*di
;
118 path
= btrfs_alloc_path();
120 btrfs_init_path(path
);
121 ret
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
129 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
130 struct btrfs_dir_item
);
131 objectid
= btrfs_dir_objectid(di
);
133 ret
= btrfs_del_item(trans
, root
, path
);
135 dentry
->d_inode
->i_ctime
= dir
->i_ctime
;
137 btrfs_release_path(root
, path
);
138 btrfs_free_path(path
);
140 inode_dec_link_count(dentry
->d_inode
);
144 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
146 struct btrfs_root
*root
;
147 struct btrfs_trans_handle
*trans
;
150 root
= btrfs_sb(dir
->i_sb
);
151 mutex_lock(&root
->fs_info
->fs_mutex
);
152 trans
= btrfs_start_transaction(root
, 1);
153 ret
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
154 btrfs_end_transaction(trans
, root
);
155 mutex_unlock(&root
->fs_info
->fs_mutex
);
159 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
161 struct inode
*inode
= dentry
->d_inode
;
164 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
165 struct btrfs_path
*path
;
166 struct btrfs_key key
;
167 struct btrfs_trans_handle
*trans
;
168 struct btrfs_disk_key
*found_key
;
169 struct btrfs_leaf
*leaf
;
171 path
= btrfs_alloc_path();
173 btrfs_init_path(path
);
174 mutex_lock(&root
->fs_info
->fs_mutex
);
175 trans
= btrfs_start_transaction(root
, 1);
176 key
.objectid
= inode
->i_ino
;
177 key
.offset
= (u64
)-1;
179 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
180 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
187 BUG_ON(path
->slots
[0] == 0);
189 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
190 found_key
= &leaf
->items
[path
->slots
[0]].key
;
191 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
) {
195 if (btrfs_disk_key_type(found_key
) != BTRFS_DIR_ITEM_KEY
||
196 btrfs_disk_key_offset(found_key
) != 2) {
200 ret
= btrfs_del_item(trans
, root
, path
);
202 btrfs_release_path(root
, path
);
204 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
213 ret
= btrfs_del_item(trans
, root
, path
);
218 btrfs_release_path(root
, path
);
220 /* now the directory is empty */
221 err
= btrfs_unlink_trans(trans
, root
, dir
, dentry
);
226 btrfs_release_path(root
, path
);
227 btrfs_free_path(path
);
228 mutex_unlock(&root
->fs_info
->fs_mutex
);
229 ret
= btrfs_end_transaction(trans
, root
);
235 static int btrfs_free_inode(struct btrfs_trans_handle
*trans
,
236 struct btrfs_root
*root
,
239 u64 objectid
= inode
->i_ino
;
240 struct btrfs_path
*path
;
241 struct btrfs_inode_map_item
*map
;
242 struct btrfs_key stat_data_key
;
247 path
= btrfs_alloc_path();
249 btrfs_init_path(path
);
250 ret
= btrfs_lookup_inode_map(trans
, root
, path
, objectid
, -1);
256 map
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
257 struct btrfs_inode_map_item
);
258 btrfs_disk_key_to_cpu(&stat_data_key
, &map
->key
);
259 ret
= btrfs_del_item(trans
, root
->fs_info
->inode_root
, path
);
261 btrfs_release_path(root
, path
);
263 ret
= btrfs_lookup_inode(trans
, root
, path
, objectid
, -1);
265 ret
= btrfs_del_item(trans
, root
, path
);
268 btrfs_release_path(root
, path
);
269 btrfs_free_path(path
);
273 static int btrfs_truncate_in_trans(struct btrfs_trans_handle
*trans
,
274 struct btrfs_root
*root
,
278 struct btrfs_path
*path
;
279 struct btrfs_key key
;
280 struct btrfs_disk_key
*found_key
;
281 struct btrfs_leaf
*leaf
;
282 struct btrfs_file_extent_item
*fi
= NULL
;
283 u64 extent_start
= 0;
284 u64 extent_num_blocks
= 0;
287 path
= btrfs_alloc_path();
289 /* FIXME, add redo link to tree so we don't leak on crash */
290 key
.objectid
= inode
->i_ino
;
291 key
.offset
= (u64
)-1;
293 btrfs_set_key_type(&key
, BTRFS_CSUM_ITEM_KEY
);
295 btrfs_init_path(path
);
296 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
301 BUG_ON(path
->slots
[0] == 0);
304 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
305 found_key
= &leaf
->items
[path
->slots
[0]].key
;
306 if (btrfs_disk_key_objectid(found_key
) != inode
->i_ino
)
308 if (btrfs_disk_key_type(found_key
) != BTRFS_CSUM_ITEM_KEY
&&
309 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
)
311 if (btrfs_disk_key_offset(found_key
) < inode
->i_size
)
313 if (btrfs_disk_key_type(found_key
) == BTRFS_EXTENT_DATA_KEY
) {
314 fi
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
316 struct btrfs_file_extent_item
);
317 extent_start
= btrfs_file_extent_disk_blocknr(fi
);
319 btrfs_file_extent_disk_num_blocks(fi
);
321 btrfs_file_extent_num_blocks(fi
) >> 9;
326 ret
= btrfs_del_item(trans
, root
, path
);
328 btrfs_release_path(root
, path
);
330 ret
= btrfs_free_extent(trans
, root
, extent_start
,
331 extent_num_blocks
, 0);
337 btrfs_release_path(root
, path
);
338 btrfs_free_path(path
);
342 static void btrfs_delete_inode(struct inode
*inode
)
344 struct btrfs_trans_handle
*trans
;
345 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
348 truncate_inode_pages(&inode
->i_data
, 0);
349 if (is_bad_inode(inode
)) {
353 mutex_lock(&root
->fs_info
->fs_mutex
);
354 trans
= btrfs_start_transaction(root
, 1);
355 if (S_ISREG(inode
->i_mode
)) {
356 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
359 btrfs_free_inode(trans
, root
, inode
);
360 btrfs_end_transaction(trans
, root
);
361 mutex_unlock(&root
->fs_info
->fs_mutex
);
367 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
370 const char *name
= dentry
->d_name
.name
;
371 int namelen
= dentry
->d_name
.len
;
372 struct btrfs_dir_item
*di
;
373 struct btrfs_path
*path
;
374 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
377 path
= btrfs_alloc_path();
379 btrfs_init_path(path
);
380 ret
= btrfs_lookup_dir_item(NULL
, root
, path
, dir
->i_ino
, name
,
382 if (ret
|| !btrfs_match_dir_item_name(root
, path
, name
, namelen
)) {
387 di
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
388 struct btrfs_dir_item
);
389 *ino
= btrfs_dir_objectid(di
);
391 btrfs_release_path(root
, path
);
392 btrfs_free_path(path
);
397 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
398 struct nameidata
*nd
)
400 struct inode
* inode
;
401 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
405 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
406 return ERR_PTR(-ENAMETOOLONG
);
407 mutex_lock(&root
->fs_info
->fs_mutex
);
408 ret
= btrfs_inode_by_name(dir
, dentry
, &ino
);
409 mutex_unlock(&root
->fs_info
->fs_mutex
);
414 inode
= iget(dir
->i_sb
, ino
);
416 return ERR_PTR(-EACCES
);
420 return d_splice_alias(inode
, dentry
);
423 static int btrfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
425 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
426 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
427 struct btrfs_item
*item
;
428 struct btrfs_dir_item
*di
;
429 struct btrfs_key key
;
430 struct btrfs_path
*path
;
433 struct btrfs_leaf
*leaf
;
436 unsigned char d_type
= DT_UNKNOWN
;
439 mutex_lock(&root
->fs_info
->fs_mutex
);
440 key
.objectid
= inode
->i_ino
;
442 btrfs_set_key_type(&key
, BTRFS_DIR_ITEM_KEY
);
443 key
.offset
= filp
->f_pos
;
444 path
= btrfs_alloc_path();
445 btrfs_init_path(path
);
446 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
452 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
453 nritems
= btrfs_header_nritems(&leaf
->header
);
454 slot
= path
->slots
[0];
455 if (advance
|| slot
>= nritems
) {
456 if (slot
>= nritems
-1) {
457 ret
= btrfs_next_leaf(root
, path
);
460 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
461 nritems
= btrfs_header_nritems(&leaf
->header
);
462 slot
= path
->slots
[0];
469 item
= leaf
->items
+ slot
;
470 if (btrfs_disk_key_objectid(&item
->key
) != key
.objectid
)
472 if (btrfs_disk_key_type(&item
->key
) != BTRFS_DIR_ITEM_KEY
)
474 if (btrfs_disk_key_offset(&item
->key
) < filp
->f_pos
)
478 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
479 over
= filldir(dirent
, (const char *)(di
+ 1),
480 btrfs_dir_name_len(di
),
481 btrfs_disk_key_offset(&item
->key
),
482 btrfs_dir_objectid(di
), d_type
);
484 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
);
487 filp
->f_pos
= btrfs_disk_key_offset(&item
->key
) + 1;
491 btrfs_release_path(root
, path
);
492 btrfs_free_path(path
);
493 mutex_unlock(&root
->fs_info
->fs_mutex
);
497 static void btrfs_put_super (struct super_block
* sb
)
499 struct btrfs_root
*root
= btrfs_sb(sb
);
502 ret
= close_ctree(root
);
504 printk("close ctree returns %d\n", ret
);
506 sb
->s_fs_info
= NULL
;
509 static int btrfs_fill_super(struct super_block
* sb
, void * data
, int silent
)
511 struct inode
* inode
;
512 struct dentry
* root_dentry
;
513 struct btrfs_super_block
*disk_super
;
514 struct btrfs_root
*root
;
516 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
517 sb
->s_magic
= BTRFS_SUPER_MAGIC
;
518 sb
->s_op
= &btrfs_super_ops
;
521 root
= open_ctree(sb
);
524 printk("btrfs: open_ctree failed\n");
527 sb
->s_fs_info
= root
;
528 disk_super
= root
->fs_info
->disk_super
;
529 printk("read in super total blocks %Lu root %Lu\n",
530 btrfs_super_total_blocks(disk_super
),
531 btrfs_super_root_dir(disk_super
));
533 inode
= iget_locked(sb
, btrfs_super_root_dir(disk_super
));
536 if (inode
->i_state
& I_NEW
) {
537 btrfs_read_locked_inode(inode
);
538 unlock_new_inode(inode
);
541 root_dentry
= d_alloc_root(inode
);
546 sb
->s_root
= root_dentry
;
551 static void fill_inode_item(struct btrfs_inode_item
*item
,
554 btrfs_set_inode_uid(item
, inode
->i_uid
);
555 btrfs_set_inode_gid(item
, inode
->i_gid
);
556 btrfs_set_inode_size(item
, inode
->i_size
);
557 btrfs_set_inode_mode(item
, inode
->i_mode
);
558 btrfs_set_inode_nlink(item
, inode
->i_nlink
);
559 btrfs_set_timespec_sec(&item
->atime
, inode
->i_atime
.tv_sec
);
560 btrfs_set_timespec_nsec(&item
->atime
, inode
->i_atime
.tv_nsec
);
561 btrfs_set_timespec_sec(&item
->mtime
, inode
->i_mtime
.tv_sec
);
562 btrfs_set_timespec_nsec(&item
->mtime
, inode
->i_mtime
.tv_nsec
);
563 btrfs_set_timespec_sec(&item
->ctime
, inode
->i_ctime
.tv_sec
);
564 btrfs_set_timespec_nsec(&item
->ctime
, inode
->i_ctime
.tv_nsec
);
565 btrfs_set_inode_nblocks(item
, inode
->i_blocks
);
566 btrfs_set_inode_generation(item
, inode
->i_generation
);
570 static int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
571 struct btrfs_root
*root
,
574 struct btrfs_inode_item
*inode_item
;
575 struct btrfs_path
*path
;
578 path
= btrfs_alloc_path();
580 btrfs_init_path(path
);
582 ret
= btrfs_lookup_inode(trans
, root
, path
, inode
->i_ino
, 1);
589 inode_item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
591 struct btrfs_inode_item
);
593 fill_inode_item(inode_item
, inode
);
594 btrfs_mark_buffer_dirty(path
->nodes
[0]);
596 btrfs_release_path(root
, path
);
597 btrfs_free_path(path
);
602 static int btrfs_write_inode(struct inode
*inode
, int wait
)
604 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
605 struct btrfs_trans_handle
*trans
;
608 mutex_lock(&root
->fs_info
->fs_mutex
);
609 trans
= btrfs_start_transaction(root
, 1);
610 ret
= btrfs_update_inode(trans
, root
, inode
);
612 btrfs_commit_transaction(trans
, root
);
614 btrfs_end_transaction(trans
, root
);
615 mutex_unlock(&root
->fs_info
->fs_mutex
);
620 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
621 struct inode
*dir
, int mode
)
624 struct btrfs_inode_item inode_item
;
625 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
626 struct btrfs_key key
;
630 inode
= new_inode(dir
->i_sb
);
632 return ERR_PTR(-ENOMEM
);
635 ret
= btrfs_find_free_objectid(trans
, root
, dir
->i_ino
, &objectid
);
638 inode
->i_uid
= current
->fsuid
;
639 inode
->i_gid
= current
->fsgid
;
640 inode
->i_mode
= mode
;
641 inode
->i_ino
= objectid
;
643 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
644 fill_inode_item(&inode_item
, inode
);
646 key
.objectid
= objectid
;
649 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
650 ret
= btrfs_insert_inode_map(trans
, root
, objectid
, &key
);
653 ret
= btrfs_insert_inode(trans
, root
, objectid
, &inode_item
);
656 insert_inode_hash(inode
);
662 static int btrfs_add_link(struct btrfs_trans_handle
*trans
,
663 struct dentry
*dentry
, struct inode
*inode
)
666 ret
= btrfs_insert_dir_item(trans
, btrfs_sb(inode
->i_sb
),
667 dentry
->d_name
.name
, dentry
->d_name
.len
,
668 dentry
->d_parent
->d_inode
->i_ino
,
671 dentry
->d_parent
->d_inode
->i_size
+= dentry
->d_name
.len
;
672 ret
= btrfs_update_inode(trans
, btrfs_sb(inode
->i_sb
),
673 dentry
->d_parent
->d_inode
);
676 check_inode(dentry
->d_parent
->d_inode
);
680 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
681 struct dentry
*dentry
, struct inode
*inode
)
683 int err
= btrfs_add_link(trans
, dentry
, inode
);
685 d_instantiate(dentry
, inode
);
694 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
695 int mode
, struct nameidata
*nd
)
697 struct btrfs_trans_handle
*trans
;
698 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
703 mutex_lock(&root
->fs_info
->fs_mutex
);
704 trans
= btrfs_start_transaction(root
, 1);
705 inode
= btrfs_new_inode(trans
, dir
, mode
);
706 err
= PTR_ERR(inode
);
709 // FIXME mark the inode dirty
710 err
= btrfs_add_nondir(trans
, dentry
, inode
);
714 inode
->i_mapping
->a_ops
= &btrfs_aops
;
715 inode
->i_fop
= &btrfs_file_operations
;
716 inode
->i_op
= &btrfs_file_inode_operations
;
718 dir
->i_sb
->s_dirt
= 1;
720 btrfs_end_transaction(trans
, root
);
721 mutex_unlock(&root
->fs_info
->fs_mutex
);
726 inode_dec_link_count(inode
);
732 static int btrfs_make_empty_dir(struct btrfs_trans_handle
*trans
,
733 struct inode
*inode
, struct inode
*dir
)
735 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
741 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 1, inode
->i_ino
,
745 ret
= btrfs_insert_dir_item(trans
, root
, buf
, 2, inode
->i_ino
,
750 ret
= btrfs_update_inode(trans
, root
, inode
);
755 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
758 struct btrfs_trans_handle
*trans
;
759 struct btrfs_root
*root
= btrfs_sb(dir
->i_sb
);
763 mutex_lock(&root
->fs_info
->fs_mutex
);
764 trans
= btrfs_start_transaction(root
, 1);
766 err
= PTR_ERR(trans
);
769 inode
= btrfs_new_inode(trans
, dir
, S_IFDIR
| mode
);
771 err
= PTR_ERR(inode
);
775 inode
->i_op
= &btrfs_dir_inode_operations
;
776 inode
->i_fop
= &btrfs_dir_file_operations
;
778 err
= btrfs_make_empty_dir(trans
, inode
, dir
);
781 err
= btrfs_add_link(trans
, dentry
, inode
);
784 d_instantiate(dentry
, inode
);
788 btrfs_end_transaction(trans
, root
);
790 mutex_unlock(&root
->fs_info
->fs_mutex
);
796 static int btrfs_sync_fs(struct super_block
*sb
, int wait
)
798 struct btrfs_trans_handle
*trans
;
799 struct btrfs_root
*root
;
805 filemap_flush(root
->fs_info
->btree_inode
->i_mapping
);
808 filemap_write_and_wait(root
->fs_info
->btree_inode
->i_mapping
);
809 mutex_lock(&root
->fs_info
->fs_mutex
);
810 trans
= btrfs_start_transaction(root
, 1);
811 ret
= btrfs_commit_transaction(trans
, root
);
814 printk("btrfs sync_fs\n");
815 mutex_unlock(&root
->fs_info
->fs_mutex
);
819 static int btrfs_get_block_lock(struct inode
*inode
, sector_t iblock
,
820 struct buffer_head
*result
, int create
)
825 u64 extent_start
= 0;
827 u64 objectid
= inode
->i_ino
;
828 struct btrfs_path
*path
;
829 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
830 struct btrfs_trans_handle
*trans
= NULL
;
831 struct btrfs_file_extent_item
*item
;
832 struct btrfs_leaf
*leaf
;
833 struct btrfs_disk_key
*found_key
;
835 path
= btrfs_alloc_path();
837 btrfs_init_path(path
);
839 trans
= btrfs_start_transaction(root
, 1);
842 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
844 iblock
<< inode
->i_blkbits
, 0);
851 if (path
->slots
[0] == 0) {
852 btrfs_release_path(root
, path
);
858 item
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]), path
->slots
[0],
859 struct btrfs_file_extent_item
);
860 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
861 blocknr
= btrfs_file_extent_disk_blocknr(item
);
862 blocknr
+= btrfs_file_extent_offset(item
);
864 /* exact match found, use it */
867 map_bh(result
, inode
->i_sb
, blocknr
);
871 /* are we inside the extent that was found? */
872 found_key
= &leaf
->items
[path
->slots
[0]].key
;
873 if (btrfs_disk_key_objectid(found_key
) != objectid
||
874 btrfs_disk_key_type(found_key
) != BTRFS_EXTENT_DATA_KEY
) {
877 btrfs_release_path(root
, path
);
881 extent_start
= btrfs_disk_key_offset(&leaf
->items
[path
->slots
[0]].key
);
882 extent_start
= extent_start
>> inode
->i_blkbits
;
883 extent_start
+= btrfs_file_extent_offset(item
);
884 extent_end
= extent_start
+ btrfs_file_extent_num_blocks(item
);
885 if (iblock
>= extent_start
&& iblock
< extent_end
) {
887 map_bh(result
, inode
->i_sb
, blocknr
+ iblock
- extent_start
);
891 /* ok, create a new extent */
896 ret
= btrfs_alloc_file_extent(trans
, root
, objectid
,
897 iblock
<< inode
->i_blkbits
,
898 1, extent_end
, &blocknr
);
903 inode
->i_blocks
+= inode
->i_sb
->s_blocksize
>> 9;
904 set_buffer_new(result
);
905 map_bh(result
, inode
->i_sb
, blocknr
);
908 btrfs_release_path(root
, path
);
909 btrfs_free_path(path
);
911 btrfs_end_transaction(trans
, root
);
915 static int btrfs_get_block(struct inode
*inode
, sector_t iblock
,
916 struct buffer_head
*result
, int create
)
919 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
920 mutex_lock(&root
->fs_info
->fs_mutex
);
921 err
= btrfs_get_block_lock(inode
, iblock
, result
, create
);
922 mutex_unlock(&root
->fs_info
->fs_mutex
);
926 static int btrfs_prepare_write(struct file
*file
, struct page
*page
,
927 unsigned from
, unsigned to
)
930 return nobh_prepare_write(page
, from
, to
, btrfs_get_block
);
932 static int btrfs_commit_write(struct file
*file
, struct page
*page
,
933 unsigned from
, unsigned to
)
936 return nobh_commit_write(file
, page
, from
, to
);
939 static void btrfs_write_super(struct super_block
*sb
)
941 btrfs_sync_fs(sb
, 1);
944 static int btrfs_readpage(struct file
*file
, struct page
*page
)
946 return mpage_readpage(page
, btrfs_get_block
);
949 static int btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
950 struct list_head
*pages
, unsigned nr_pages
)
952 return mpage_readpages(mapping
, pages
, nr_pages
, btrfs_get_block
);
955 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
957 return nobh_writepage(page
, btrfs_get_block
, wbc
);
960 static void btrfs_truncate(struct inode
*inode
)
962 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
964 struct btrfs_trans_handle
*trans
;
966 if (!S_ISREG(inode
->i_mode
))
968 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
971 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
973 /* FIXME, add redo link to tree so we don't leak on crash */
974 mutex_lock(&root
->fs_info
->fs_mutex
);
975 trans
= btrfs_start_transaction(root
, 1);
976 ret
= btrfs_truncate_in_trans(trans
, root
, inode
);
978 ret
= btrfs_end_transaction(trans
, root
);
980 mutex_unlock(&root
->fs_info
->fs_mutex
);
981 mark_inode_dirty(inode
);
984 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
985 struct page
**prepared_pages
,
986 const char __user
* buf
)
990 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
992 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
993 size_t count
= min_t(size_t,
994 PAGE_CACHE_SIZE
- offset
, write_bytes
);
995 struct page
*page
= prepared_pages
[i
];
996 fault_in_pages_readable(buf
, count
);
998 /* Copy data from userspace to the current page */
1000 page_fault
= __copy_from_user(page_address(page
) + offset
,
1002 /* Flush processor's dcache for this page */
1003 flush_dcache_page(page
);
1006 write_bytes
-= count
;
1011 return page_fault
? -EFAULT
: 0;
1014 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
1017 for (i
= 0; i
< num_pages
; i
++) {
1020 unlock_page(pages
[i
]);
1021 mark_page_accessed(pages
[i
]);
1022 page_cache_release(pages
[i
]);
1025 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
1026 struct btrfs_root
*root
,
1028 struct page
**pages
,
1038 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1040 for (i
= 0; i
< num_pages
; i
++) {
1041 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1042 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1043 /* FIXME, one block at a time */
1045 mutex_lock(&root
->fs_info
->fs_mutex
);
1046 trans
= btrfs_start_transaction(root
, 1);
1047 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
1048 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
1049 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
1051 SetPageChecked(pages
[i
]);
1052 ret
= btrfs_end_transaction(trans
, root
);
1054 mutex_unlock(&root
->fs_info
->fs_mutex
);
1056 ret
= nobh_commit_write(file
, pages
[i
], offset
,
1057 offset
+ this_write
);
1063 WARN_ON(this_write
> write_bytes
);
1064 write_bytes
-= this_write
;
1070 static int prepare_pages(struct btrfs_trans_handle
*trans
,
1071 struct btrfs_root
*root
,
1073 struct page
**pages
,
1079 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
1080 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1085 loff_t isize
= i_size_read(inode
);
1087 memset(pages
, 0, num_pages
* sizeof(struct page
*));
1089 for (i
= 0; i
< num_pages
; i
++) {
1090 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
1093 goto failed_release
;
1095 offset
= pos
& (PAGE_CACHE_SIZE
-1);
1096 this_write
= min(PAGE_CACHE_SIZE
- offset
, write_bytes
);
1097 ret
= nobh_prepare_write(pages
[i
], offset
,
1098 offset
+ this_write
,
1103 goto failed_truncate
;
1105 WARN_ON(this_write
> write_bytes
);
1106 write_bytes
-= this_write
;
1111 btrfs_drop_pages(pages
, num_pages
);
1115 btrfs_drop_pages(pages
, num_pages
);
1117 vmtruncate(inode
, isize
);
1121 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
1122 size_t count
, loff_t
*ppos
)
1125 size_t num_written
= 0;
1128 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1129 struct btrfs_root
*root
= btrfs_sb(inode
->i_sb
);
1130 struct page
*pages
[1];
1132 if (file
->f_flags
& O_DIRECT
)
1136 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1137 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
1138 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1143 err
= remove_suid(file
->f_path
.dentry
);
1146 file_update_time(file
);
1147 mutex_lock(&inode
->i_mutex
);
1149 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1150 size_t write_bytes
= min(count
, PAGE_CACHE_SIZE
- offset
);
1151 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
1153 ret
= prepare_pages(NULL
, root
, file
, pages
, num_pages
,
1156 ret
= btrfs_copy_from_user(pos
, num_pages
,
1157 write_bytes
, pages
, buf
);
1160 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
1161 num_pages
, pos
, write_bytes
);
1163 btrfs_drop_pages(pages
, num_pages
);
1166 count
-= write_bytes
;
1168 num_written
+= write_bytes
;
1170 balance_dirty_pages_ratelimited(inode
->i_mapping
);
1173 mutex_unlock(&inode
->i_mutex
);
1176 current
->backing_dev_info
= NULL
;
1177 return num_written
? num_written
: err
;
1180 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
1181 unsigned long offset
, unsigned long size
)
1184 unsigned long left
, count
= desc
->count
;
1189 if (!PageChecked(page
)) {
1190 /* FIXME, do it per block */
1191 struct btrfs_root
*root
= btrfs_sb(page
->mapping
->host
->i_sb
);
1192 int ret
= btrfs_csum_verify_file_block(root
,
1193 page
->mapping
->host
->i_ino
,
1194 page
->index
<< PAGE_CACHE_SHIFT
,
1195 kmap(page
), PAGE_CACHE_SIZE
);
1197 printk("failed to verify ino %lu page %lu\n",
1198 page
->mapping
->host
->i_ino
,
1200 memset(page_address(page
), 0, PAGE_CACHE_SIZE
);
1202 SetPageChecked(page
);
1206 * Faults on the destination of a read are common, so do it before
1209 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
1210 kaddr
= kmap_atomic(page
, KM_USER0
);
1211 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
1212 kaddr
+ offset
, size
);
1213 kunmap_atomic(kaddr
, KM_USER0
);
1218 /* Do it the slow way */
1220 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
1225 desc
->error
= -EFAULT
;
1228 desc
->count
= count
- size
;
1229 desc
->written
+= size
;
1230 desc
->arg
.buf
+= size
;
1235 * btrfs_file_aio_read - filesystem read routine
1236 * @iocb: kernel I/O control block
1237 * @iov: io vector request
1238 * @nr_segs: number of segments in the iovec
1239 * @pos: current file position
1241 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1242 unsigned long nr_segs
, loff_t pos
)
1244 struct file
*filp
= iocb
->ki_filp
;
1248 loff_t
*ppos
= &iocb
->ki_pos
;
1251 for (seg
= 0; seg
< nr_segs
; seg
++) {
1252 const struct iovec
*iv
= &iov
[seg
];
1255 * If any segment has a negative length, or the cumulative
1256 * length ever wraps negative then return -EINVAL.
1258 count
+= iv
->iov_len
;
1259 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
1261 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
1266 count
-= iv
->iov_len
; /* This segment is no good */
1271 for (seg
= 0; seg
< nr_segs
; seg
++) {
1272 read_descriptor_t desc
;
1275 desc
.arg
.buf
= iov
[seg
].iov_base
;
1276 desc
.count
= iov
[seg
].iov_len
;
1277 if (desc
.count
== 0)
1280 do_generic_file_read(filp
, ppos
, &desc
,
1282 retval
+= desc
.written
;
1284 retval
= retval
?: desc
.error
;
1292 static struct kmem_cache
*btrfs_inode_cachep
;
1293 struct kmem_cache
*btrfs_trans_handle_cachep
;
1294 struct kmem_cache
*btrfs_transaction_cachep
;
1295 struct kmem_cache
*btrfs_bit_radix_cachep
;
1296 struct kmem_cache
*btrfs_path_cachep
;
1299 * Called inside transaction, so use GFP_NOFS
1301 static struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
1303 struct btrfs_inode
*ei
;
1305 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
1308 ei
->magic
= 0xDEADBEEF;
1309 ei
->magic2
= 0xDEADBEAF;
1310 return &ei
->vfs_inode
;
1313 static void btrfs_destroy_inode(struct inode
*inode
)
1315 struct btrfs_inode
*ei
= BTRFS_I(inode
);
1316 WARN_ON(ei
->magic
!= 0xDEADBEEF);
1317 WARN_ON(ei
->magic2
!= 0xDEADBEAF);
1318 WARN_ON(!list_empty(&inode
->i_dentry
));
1319 WARN_ON(inode
->i_ino
== 1);
1320 WARN_ON(inode
->i_data
.nrpages
);
1324 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
1327 static void init_once(void * foo
, struct kmem_cache
* cachep
,
1328 unsigned long flags
)
1330 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
1332 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
1333 SLAB_CTOR_CONSTRUCTOR
) {
1334 inode_init_once(&ei
->vfs_inode
);
1338 static int init_inodecache(void)
1340 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode_cache",
1341 sizeof(struct btrfs_inode
),
1342 0, (SLAB_RECLAIM_ACCOUNT
|
1345 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle_cache",
1346 sizeof(struct btrfs_trans_handle
),
1347 0, (SLAB_RECLAIM_ACCOUNT
|
1350 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction_cache",
1351 sizeof(struct btrfs_transaction
),
1352 0, (SLAB_RECLAIM_ACCOUNT
|
1355 btrfs_path_cachep
= kmem_cache_create("btrfs_path_cache",
1356 sizeof(struct btrfs_transaction
),
1357 0, (SLAB_RECLAIM_ACCOUNT
|
1360 btrfs_bit_radix_cachep
= kmem_cache_create("btrfs_radix",
1362 0, (SLAB_RECLAIM_ACCOUNT
|
1364 SLAB_DESTROY_BY_RCU
),
1366 if (btrfs_inode_cachep
== NULL
|| btrfs_trans_handle_cachep
== NULL
||
1367 btrfs_transaction_cachep
== NULL
|| btrfs_bit_radix_cachep
== NULL
)
1372 static void destroy_inodecache(void)
1374 kmem_cache_destroy(btrfs_inode_cachep
);
1375 kmem_cache_destroy(btrfs_trans_handle_cachep
);
1376 kmem_cache_destroy(btrfs_transaction_cachep
);
1377 kmem_cache_destroy(btrfs_bit_radix_cachep
);
1378 kmem_cache_destroy(btrfs_path_cachep
);
1381 static int btrfs_get_sb(struct file_system_type
*fs_type
,
1382 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1384 return get_sb_bdev(fs_type
, flags
, dev_name
, data
,
1385 btrfs_fill_super
, mnt
);
1388 static struct file_system_type btrfs_fs_type
= {
1389 .owner
= THIS_MODULE
,
1391 .get_sb
= btrfs_get_sb
,
1392 .kill_sb
= kill_block_super
,
1393 .fs_flags
= FS_REQUIRES_DEV
,
1396 static struct super_operations btrfs_super_ops
= {
1397 .statfs
= simple_statfs
,
1398 .delete_inode
= btrfs_delete_inode
,
1399 .put_super
= btrfs_put_super
,
1400 .read_inode
= btrfs_read_locked_inode
,
1401 .write_super
= btrfs_write_super
,
1402 .sync_fs
= btrfs_sync_fs
,
1403 .write_inode
= btrfs_write_inode
,
1404 .alloc_inode
= btrfs_alloc_inode
,
1405 .destroy_inode
= btrfs_destroy_inode
,
1408 static struct inode_operations btrfs_dir_inode_operations
= {
1409 .lookup
= btrfs_lookup
,
1410 .create
= btrfs_create
,
1411 .unlink
= btrfs_unlink
,
1412 .mkdir
= btrfs_mkdir
,
1413 .rmdir
= btrfs_rmdir
,
1416 static struct file_operations btrfs_dir_file_operations
= {
1417 .llseek
= generic_file_llseek
,
1418 .read
= generic_read_dir
,
1419 .readdir
= btrfs_readdir
,
1422 static struct address_space_operations btrfs_aops
= {
1423 .readpage
= btrfs_readpage
,
1424 .readpages
= btrfs_readpages
,
1425 .writepage
= btrfs_writepage
,
1426 .sync_page
= block_sync_page
,
1427 .prepare_write
= btrfs_prepare_write
,
1428 .commit_write
= btrfs_commit_write
,
1431 static struct inode_operations btrfs_file_inode_operations
= {
1432 .truncate
= btrfs_truncate
,
1435 static struct file_operations btrfs_file_operations
= {
1436 .llseek
= generic_file_llseek
,
1437 .read
= do_sync_read
,
1438 .aio_read
= btrfs_file_aio_read
,
1439 .write
= btrfs_file_write
,
1440 .mmap
= generic_file_mmap
,
1441 .open
= generic_file_open
,
1444 static int __init
init_btrfs_fs(void)
1447 printk("btrfs loaded!\n");
1448 err
= init_inodecache();
1451 return register_filesystem(&btrfs_fs_type
);
1454 static void __exit
exit_btrfs_fs(void)
1456 destroy_inodecache();
1457 unregister_filesystem(&btrfs_fs_type
);
1458 printk("btrfs unloaded\n");
1461 module_init(init_btrfs_fs
)
1462 module_exit(exit_btrfs_fs
)
1464 MODULE_LICENSE("GPL");