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);
141 dir->i_size -= name_len;
142 mark_inode_dirty(dir);
147 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
149 struct btrfs_root *root;
150 struct btrfs_trans_handle *trans;
153 root = btrfs_sb(dir->i_sb);
154 mutex_lock(&root->fs_info->fs_mutex);
155 trans = btrfs_start_transaction(root, 1);
156 ret = btrfs_unlink_trans(trans, root, dir, dentry);
157 btrfs_end_transaction(trans, root);
158 mutex_unlock(&root->fs_info->fs_mutex);
162 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
164 struct inode *inode = dentry->d_inode;
167 struct btrfs_root *root = btrfs_sb(dir->i_sb);
168 struct btrfs_path *path;
169 struct btrfs_key key;
170 struct btrfs_trans_handle *trans;
171 struct btrfs_disk_key *found_key;
172 struct btrfs_leaf *leaf;
174 path = btrfs_alloc_path();
176 btrfs_init_path(path);
177 mutex_lock(&root->fs_info->fs_mutex);
178 trans = btrfs_start_transaction(root, 1);
179 key.objectid = inode->i_ino;
180 key.offset = (u64)-1;
182 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
183 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
190 BUG_ON(path->slots[0] == 0);
192 leaf = btrfs_buffer_leaf(path->nodes[0]);
193 found_key = &leaf->items[path->slots[0]].key;
194 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
198 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
199 btrfs_disk_key_offset(found_key) != 2) {
203 ret = btrfs_del_item(trans, root, path);
205 btrfs_release_path(root, path);
207 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
216 ret = btrfs_del_item(trans, root, path);
221 btrfs_release_path(root, path);
223 /* now the directory is empty */
224 err = btrfs_unlink_trans(trans, root, dir, dentry);
229 btrfs_release_path(root, path);
230 btrfs_free_path(path);
231 mutex_unlock(&root->fs_info->fs_mutex);
232 ret = btrfs_end_transaction(trans, root);
238 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root,
242 u64 objectid = inode->i_ino;
243 struct btrfs_path *path;
244 struct btrfs_inode_map_item *map;
245 struct btrfs_key stat_data_key;
250 path = btrfs_alloc_path();
252 btrfs_init_path(path);
253 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
259 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
260 struct btrfs_inode_map_item);
261 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
262 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
264 btrfs_release_path(root, path);
266 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
268 ret = btrfs_del_item(trans, root, path);
271 btrfs_release_path(root, path);
272 btrfs_free_path(path);
276 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
277 struct btrfs_root *root,
281 struct btrfs_path *path;
282 struct btrfs_key key;
283 struct btrfs_disk_key *found_key;
284 struct btrfs_leaf *leaf;
285 struct btrfs_file_extent_item *fi = NULL;
286 u64 extent_start = 0;
287 u64 extent_num_blocks = 0;
290 path = btrfs_alloc_path();
292 /* FIXME, add redo link to tree so we don't leak on crash */
293 key.objectid = inode->i_ino;
294 key.offset = (u64)-1;
297 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
300 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
302 btrfs_init_path(path);
303 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
308 BUG_ON(path->slots[0] == 0);
311 leaf = btrfs_buffer_leaf(path->nodes[0]);
312 found_key = &leaf->items[path->slots[0]].key;
313 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
315 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
316 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
317 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
319 if (btrfs_disk_key_offset(found_key) < inode->i_size)
321 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
322 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
324 struct btrfs_file_extent_item);
325 extent_start = btrfs_file_extent_disk_blocknr(fi);
327 btrfs_file_extent_disk_num_blocks(fi);
329 btrfs_file_extent_num_blocks(fi) >> 9;
334 ret = btrfs_del_item(trans, root, path);
336 btrfs_release_path(root, path);
338 ret = btrfs_free_extent(trans, root, extent_start,
339 extent_num_blocks, 0);
345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
350 static void btrfs_delete_inode(struct inode *inode)
352 struct btrfs_trans_handle *trans;
353 struct btrfs_root *root = btrfs_sb(inode->i_sb);
356 truncate_inode_pages(&inode->i_data, 0);
357 if (is_bad_inode(inode)) {
361 mutex_lock(&root->fs_info->fs_mutex);
362 trans = btrfs_start_transaction(root, 1);
363 if (S_ISREG(inode->i_mode)) {
364 ret = btrfs_truncate_in_trans(trans, root, inode);
367 btrfs_free_inode(trans, root, inode);
368 btrfs_end_transaction(trans, root);
369 mutex_unlock(&root->fs_info->fs_mutex);
375 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
378 const char *name = dentry->d_name.name;
379 int namelen = dentry->d_name.len;
380 struct btrfs_dir_item *di;
381 struct btrfs_path *path;
382 struct btrfs_root *root = btrfs_sb(dir->i_sb);
385 path = btrfs_alloc_path();
387 btrfs_init_path(path);
388 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
390 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
395 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
396 struct btrfs_dir_item);
397 *ino = btrfs_dir_objectid(di);
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
405 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
406 struct nameidata *nd)
408 struct inode * inode;
409 struct btrfs_root *root = btrfs_sb(dir->i_sb);
413 if (dentry->d_name.len > BTRFS_NAME_LEN)
414 return ERR_PTR(-ENAMETOOLONG);
415 mutex_lock(&root->fs_info->fs_mutex);
416 ret = btrfs_inode_by_name(dir, dentry, &ino);
417 mutex_unlock(&root->fs_info->fs_mutex);
422 inode = iget(dir->i_sb, ino);
424 return ERR_PTR(-EACCES);
428 return d_splice_alias(inode, dentry);
431 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
433 struct inode *inode = filp->f_path.dentry->d_inode;
434 struct btrfs_root *root = btrfs_sb(inode->i_sb);
435 struct btrfs_item *item;
436 struct btrfs_dir_item *di;
437 struct btrfs_key key;
438 struct btrfs_path *path;
441 struct btrfs_leaf *leaf;
444 unsigned char d_type = DT_UNKNOWN;
447 mutex_lock(&root->fs_info->fs_mutex);
448 key.objectid = inode->i_ino;
450 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
451 key.offset = filp->f_pos;
452 path = btrfs_alloc_path();
453 btrfs_init_path(path);
454 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
460 leaf = btrfs_buffer_leaf(path->nodes[0]);
461 nritems = btrfs_header_nritems(&leaf->header);
462 slot = path->slots[0];
463 if (advance || slot >= nritems) {
464 if (slot >= nritems -1) {
465 ret = btrfs_next_leaf(root, path);
468 leaf = btrfs_buffer_leaf(path->nodes[0]);
469 nritems = btrfs_header_nritems(&leaf->header);
470 slot = path->slots[0];
477 item = leaf->items + slot;
478 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
480 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_INDEX_KEY)
482 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
486 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
487 over = filldir(dirent, (const char *)(di + 1),
488 btrfs_dir_name_len(di),
489 btrfs_disk_key_offset(&item->key),
490 btrfs_dir_objectid(di), d_type);
492 filp->f_pos = btrfs_disk_key_offset(&item->key);
495 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
499 btrfs_release_path(root, path);
500 btrfs_free_path(path);
501 mutex_unlock(&root->fs_info->fs_mutex);
505 static void btrfs_put_super (struct super_block * sb)
507 struct btrfs_root *root = btrfs_sb(sb);
510 ret = close_ctree(root);
512 printk("close ctree returns %d\n", ret);
514 sb->s_fs_info = NULL;
517 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
519 struct inode * inode;
520 struct dentry * root_dentry;
521 struct btrfs_super_block *disk_super;
522 struct btrfs_root *root;
524 sb->s_maxbytes = MAX_LFS_FILESIZE;
525 sb->s_magic = BTRFS_SUPER_MAGIC;
526 sb->s_op = &btrfs_super_ops;
529 root = open_ctree(sb);
532 printk("btrfs: open_ctree failed\n");
535 sb->s_fs_info = root;
536 disk_super = root->fs_info->disk_super;
537 printk("read in super total blocks %Lu root %Lu\n",
538 btrfs_super_total_blocks(disk_super),
539 btrfs_super_root_dir(disk_super));
541 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
544 if (inode->i_state & I_NEW) {
545 btrfs_read_locked_inode(inode);
546 unlock_new_inode(inode);
549 root_dentry = d_alloc_root(inode);
554 sb->s_root = root_dentry;
559 static void fill_inode_item(struct btrfs_inode_item *item,
562 btrfs_set_inode_uid(item, inode->i_uid);
563 btrfs_set_inode_gid(item, inode->i_gid);
564 btrfs_set_inode_size(item, inode->i_size);
565 btrfs_set_inode_mode(item, inode->i_mode);
566 btrfs_set_inode_nlink(item, inode->i_nlink);
567 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
568 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
569 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
570 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
571 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
572 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
573 btrfs_set_inode_nblocks(item, inode->i_blocks);
574 btrfs_set_inode_generation(item, inode->i_generation);
578 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
579 struct btrfs_root *root,
582 struct btrfs_inode_item *inode_item;
583 struct btrfs_path *path;
586 path = btrfs_alloc_path();
588 btrfs_init_path(path);
590 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
597 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
599 struct btrfs_inode_item);
601 fill_inode_item(inode_item, inode);
602 btrfs_mark_buffer_dirty(path->nodes[0]);
604 btrfs_release_path(root, path);
605 btrfs_free_path(path);
610 static int btrfs_write_inode(struct inode *inode, int wait)
612 struct btrfs_root *root = btrfs_sb(inode->i_sb);
613 struct btrfs_trans_handle *trans;
616 mutex_lock(&root->fs_info->fs_mutex);
617 trans = btrfs_start_transaction(root, 1);
618 ret = btrfs_update_inode(trans, root, inode);
620 btrfs_commit_transaction(trans, root);
622 btrfs_end_transaction(trans, root);
623 mutex_unlock(&root->fs_info->fs_mutex);
628 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
629 struct inode *dir, int mode)
632 struct btrfs_inode_item inode_item;
633 struct btrfs_root *root = btrfs_sb(dir->i_sb);
634 struct btrfs_key key;
638 inode = new_inode(dir->i_sb);
640 return ERR_PTR(-ENOMEM);
643 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
646 inode->i_uid = current->fsuid;
647 inode->i_gid = current->fsgid;
648 inode->i_mode = mode;
649 inode->i_ino = objectid;
651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
652 fill_inode_item(&inode_item, inode);
654 key.objectid = objectid;
657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
658 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
661 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
664 insert_inode_hash(inode);
670 static int btrfs_add_link(struct btrfs_trans_handle *trans,
671 struct dentry *dentry, struct inode *inode)
674 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
675 dentry->d_name.name, dentry->d_name.len,
676 dentry->d_parent->d_inode->i_ino,
679 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
680 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
681 dentry->d_parent->d_inode);
684 check_inode(dentry->d_parent->d_inode);
688 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
689 struct dentry *dentry, struct inode *inode)
691 int err = btrfs_add_link(trans, dentry, inode);
693 d_instantiate(dentry, inode);
702 static int btrfs_create(struct inode *dir, struct dentry *dentry,
703 int mode, struct nameidata *nd)
705 struct btrfs_trans_handle *trans;
706 struct btrfs_root *root = btrfs_sb(dir->i_sb);
711 mutex_lock(&root->fs_info->fs_mutex);
712 trans = btrfs_start_transaction(root, 1);
713 inode = btrfs_new_inode(trans, dir, mode);
714 err = PTR_ERR(inode);
717 // FIXME mark the inode dirty
718 err = btrfs_add_nondir(trans, dentry, inode);
722 inode->i_mapping->a_ops = &btrfs_aops;
723 inode->i_fop = &btrfs_file_operations;
724 inode->i_op = &btrfs_file_inode_operations;
726 dir->i_sb->s_dirt = 1;
728 btrfs_end_transaction(trans, root);
729 mutex_unlock(&root->fs_info->fs_mutex);
734 inode_dec_link_count(inode);
740 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
741 struct inode *inode, struct inode *dir)
743 struct btrfs_root *root = btrfs_sb(inode->i_sb);
749 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
753 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
758 ret = btrfs_update_inode(trans, root, inode);
763 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
766 struct btrfs_trans_handle *trans;
767 struct btrfs_root *root = btrfs_sb(dir->i_sb);
771 mutex_lock(&root->fs_info->fs_mutex);
772 trans = btrfs_start_transaction(root, 1);
774 err = PTR_ERR(trans);
777 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
779 err = PTR_ERR(inode);
783 inode->i_op = &btrfs_dir_inode_operations;
784 inode->i_fop = &btrfs_dir_file_operations;
786 err = btrfs_make_empty_dir(trans, inode, dir);
789 err = btrfs_add_link(trans, dentry, inode);
792 d_instantiate(dentry, inode);
796 btrfs_end_transaction(trans, root);
798 mutex_unlock(&root->fs_info->fs_mutex);
804 static int btrfs_sync_fs(struct super_block *sb, int wait)
806 struct btrfs_trans_handle *trans;
807 struct btrfs_root *root;
813 filemap_flush(root->fs_info->btree_inode->i_mapping);
816 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
817 mutex_lock(&root->fs_info->fs_mutex);
818 trans = btrfs_start_transaction(root, 1);
819 ret = btrfs_commit_transaction(trans, root);
822 printk("btrfs sync_fs\n");
823 mutex_unlock(&root->fs_info->fs_mutex);
828 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
829 struct buffer_head *result, int create)
831 struct btrfs_root *root = btrfs_sb(inode->i_sb);
832 struct btrfs_path *path;
833 struct btrfs_key key;
834 struct btrfs_leaf *leaf;
835 int num_bytes = result->b_size;
845 path = btrfs_alloc_path();
852 pos = iblock << inode->i_blkbits;
853 key.objectid = inode->i_ino;
855 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
856 ptr = kmap(result->b_page);
858 ptr += (pos & (PAGE_CACHE_SIZE -1));
861 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
869 leaf = btrfs_buffer_leaf(path->nodes[0]);
870 item_size = btrfs_item_size(leaf->items + path->slots[0]);
871 copy_size = min(num_bytes, item_size);
872 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
873 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
874 memcpy(ptr, data_ptr, copy_size);
876 num_bytes -= copy_size;
877 WARN_ON(num_bytes < 0);
879 btrfs_release_path(root, path);
880 if (num_bytes != 0) {
881 if (pos >= i_size_read(inode))
882 memset(ptr, 0, num_bytes);
886 set_buffer_uptodate(result);
887 map_bh(result, inode->i_sb, 0);
890 btrfs_free_path(path);
891 kunmap(result->b_page);
896 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
897 struct buffer_head *result, int create)
902 u64 extent_start = 0;
904 u64 objectid = inode->i_ino;
905 struct btrfs_path *path;
906 struct btrfs_root *root = btrfs_sb(inode->i_sb);
907 struct btrfs_trans_handle *trans = NULL;
908 struct btrfs_file_extent_item *item;
909 struct btrfs_leaf *leaf;
910 struct btrfs_disk_key *found_key;
912 path = btrfs_alloc_path();
914 btrfs_init_path(path);
916 trans = btrfs_start_transaction(root, 1);
919 ret = btrfs_lookup_file_extent(trans, root, path,
921 iblock << inode->i_blkbits, 0);
928 if (path->slots[0] == 0) {
929 btrfs_release_path(root, path);
935 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
936 struct btrfs_file_extent_item);
937 leaf = btrfs_buffer_leaf(path->nodes[0]);
938 blocknr = btrfs_file_extent_disk_blocknr(item);
939 blocknr += btrfs_file_extent_offset(item);
941 /* exact match found, use it */
944 map_bh(result, inode->i_sb, blocknr);
948 /* are we inside the extent that was found? */
949 found_key = &leaf->items[path->slots[0]].key;
950 if (btrfs_disk_key_objectid(found_key) != objectid ||
951 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
954 btrfs_release_path(root, path);
958 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
959 extent_start = extent_start >> inode->i_blkbits;
960 extent_start += btrfs_file_extent_offset(item);
961 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
962 if (iblock >= extent_start && iblock < extent_end) {
964 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
968 /* ok, create a new extent */
973 ret = btrfs_alloc_file_extent(trans, root, objectid,
974 iblock << inode->i_blkbits,
975 1, extent_end, &blocknr);
980 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
981 set_buffer_new(result);
982 map_bh(result, inode->i_sb, blocknr);
985 btrfs_release_path(root, path);
986 btrfs_free_path(path);
988 btrfs_end_transaction(trans, root);
992 static int btrfs_get_block(struct inode *inode, sector_t iblock,
993 struct buffer_head *result, int create)
996 struct btrfs_root *root = btrfs_sb(inode->i_sb);
997 mutex_lock(&root->fs_info->fs_mutex);
998 err = btrfs_get_block_lock(inode, iblock, result, create);
999 // err = btrfs_get_block_inline(inode, iblock, result, create);
1000 mutex_unlock(&root->fs_info->fs_mutex);
1004 static int btrfs_prepare_write(struct file *file, struct page *page,
1005 unsigned from, unsigned to)
1007 return nobh_prepare_write(page, from, to, btrfs_get_block);
1009 static int btrfs_commit_write(struct file *file, struct page *page,
1010 unsigned from, unsigned to)
1012 return nobh_commit_write(file, page, from, to);
1015 static void btrfs_write_super(struct super_block *sb)
1017 btrfs_sync_fs(sb, 1);
1020 static int btrfs_readpage(struct file *file, struct page *page)
1022 return mpage_readpage(page, btrfs_get_block);
1025 static int btrfs_readpages(struct file *file, struct address_space *mapping,
1026 struct list_head *pages, unsigned nr_pages)
1028 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1031 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1033 return nobh_writepage(page, btrfs_get_block, wbc);
1036 static void btrfs_truncate(struct inode *inode)
1038 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1040 struct btrfs_trans_handle *trans;
1042 if (!S_ISREG(inode->i_mode))
1044 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1047 nobh_truncate_page(inode->i_mapping, inode->i_size);
1049 /* FIXME, add redo link to tree so we don't leak on crash */
1050 mutex_lock(&root->fs_info->fs_mutex);
1051 trans = btrfs_start_transaction(root, 1);
1052 ret = btrfs_truncate_in_trans(trans, root, inode);
1054 ret = btrfs_end_transaction(trans, root);
1056 mutex_unlock(&root->fs_info->fs_mutex);
1057 mark_inode_dirty(inode);
1060 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1061 struct page **prepared_pages,
1062 const char __user * buf)
1064 long page_fault = 0;
1066 int offset = pos & (PAGE_CACHE_SIZE - 1);
1068 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1069 size_t count = min_t(size_t,
1070 PAGE_CACHE_SIZE - offset, write_bytes);
1071 struct page *page = prepared_pages[i];
1072 fault_in_pages_readable(buf, count);
1074 /* Copy data from userspace to the current page */
1076 page_fault = __copy_from_user(page_address(page) + offset,
1078 /* Flush processor's dcache for this page */
1079 flush_dcache_page(page);
1082 write_bytes -= count;
1087 return page_fault ? -EFAULT : 0;
1090 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1093 for (i = 0; i < num_pages; i++) {
1096 unlock_page(pages[i]);
1097 mark_page_accessed(pages[i]);
1098 page_cache_release(pages[i]);
1101 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1104 struct page **pages,
1114 struct inode *inode = file->f_path.dentry->d_inode;
1116 for (i = 0; i < num_pages; i++) {
1117 offset = pos & (PAGE_CACHE_SIZE -1);
1118 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1119 /* FIXME, one block at a time */
1121 mutex_lock(&root->fs_info->fs_mutex);
1122 trans = btrfs_start_transaction(root, 1);
1123 btrfs_csum_file_block(trans, root, inode->i_ino,
1124 pages[i]->index << PAGE_CACHE_SHIFT,
1125 kmap(pages[i]), PAGE_CACHE_SIZE);
1127 SetPageChecked(pages[i]);
1128 ret = btrfs_end_transaction(trans, root);
1130 mutex_unlock(&root->fs_info->fs_mutex);
1132 ret = nobh_commit_write(file, pages[i], offset,
1133 offset + this_write);
1139 WARN_ON(this_write > write_bytes);
1140 write_bytes -= this_write;
1146 static int prepare_pages(struct btrfs_trans_handle *trans,
1147 struct btrfs_root *root,
1149 struct page **pages,
1155 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1156 struct inode *inode = file->f_path.dentry->d_inode;
1161 loff_t isize = i_size_read(inode);
1163 memset(pages, 0, num_pages * sizeof(struct page *));
1165 for (i = 0; i < num_pages; i++) {
1166 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1169 goto failed_release;
1171 offset = pos & (PAGE_CACHE_SIZE -1);
1172 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1173 ret = nobh_prepare_write(pages[i], offset,
1174 offset + this_write,
1179 goto failed_truncate;
1181 WARN_ON(this_write > write_bytes);
1182 write_bytes -= this_write;
1187 btrfs_drop_pages(pages, num_pages);
1191 btrfs_drop_pages(pages, num_pages);
1193 vmtruncate(inode, isize);
1197 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *ppos)
1201 size_t num_written = 0;
1204 struct inode *inode = file->f_path.dentry->d_inode;
1205 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1206 struct page *pages[1];
1208 if (file->f_flags & O_DIRECT)
1212 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1213 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1214 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1219 err = remove_suid(file->f_path.dentry);
1222 file_update_time(file);
1223 mutex_lock(&inode->i_mutex);
1225 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1226 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1227 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1229 ret = prepare_pages(NULL, root, file, pages, num_pages,
1232 ret = btrfs_copy_from_user(pos, num_pages,
1233 write_bytes, pages, buf);
1236 ret = dirty_and_release_pages(NULL, root, file, pages,
1237 num_pages, pos, write_bytes);
1239 btrfs_drop_pages(pages, num_pages);
1242 count -= write_bytes;
1244 num_written += write_bytes;
1246 balance_dirty_pages_ratelimited(inode->i_mapping);
1249 mutex_unlock(&inode->i_mutex);
1252 current->backing_dev_info = NULL;
1253 return num_written ? num_written : err;
1257 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1258 struct page *page, loff_t pos,
1259 size_t offset, size_t write_bytes)
1261 struct btrfs_path *path;
1262 struct btrfs_trans_handle *trans;
1263 struct btrfs_key key;
1264 struct btrfs_leaf *leaf;
1265 struct btrfs_key found_key;
1267 size_t copy_size = 0;
1270 size_t num_written = 0;
1272 path = btrfs_alloc_path();
1274 mutex_lock(&root->fs_info->fs_mutex);
1275 trans = btrfs_start_transaction(root, 1);
1276 key.objectid = inode->i_ino;
1278 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1282 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1288 leaf = btrfs_buffer_leaf(path->nodes[0]);
1289 btrfs_disk_key_to_cpu(&found_key,
1290 &leaf->items[path->slots[0]].key);
1291 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1292 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1293 copy_size = min(write_bytes, copy_size);
1296 int slot = path->slots[0];
1300 // FIXME find max key
1301 leaf = btrfs_buffer_leaf(path->nodes[0]);
1302 btrfs_disk_key_to_cpu(&found_key,
1303 &leaf->items[slot].key);
1304 if (found_key.objectid != inode->i_ino)
1306 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1308 copy_size = btrfs_item_size(leaf->items + slot);
1309 if (found_key.offset + copy_size <= pos)
1311 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1312 dst += pos - found_key.offset;
1313 copy_size = copy_size - (pos - found_key.offset);
1314 BUG_ON(copy_size < 0);
1315 copy_size = min(write_bytes, copy_size);
1316 WARN_ON(copy_size == 0);
1320 btrfs_release_path(root, path);
1321 copy_size = min(write_bytes,
1322 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1323 sizeof(struct btrfs_item) * 4);
1324 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1326 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1327 path->slots[0], char);
1329 WARN_ON(copy_size == 0);
1330 WARN_ON(dst + copy_size >
1331 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1332 path->slots[0], char) +
1333 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1335 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1336 page_address(page) + offset, copy_size);
1337 mark_buffer_dirty(path->nodes[0]);
1338 btrfs_release_path(root, path);
1340 offset += copy_size;
1341 num_written += copy_size;
1342 write_bytes -= copy_size;
1346 btrfs_free_path(path);
1347 ret = btrfs_end_transaction(trans, root);
1349 mutex_unlock(&root->fs_info->fs_mutex);
1350 return num_written ? num_written : err;
1353 static ssize_t btrfs_file_inline_write(struct file *file,
1354 const char __user *buf,
1355 size_t count, loff_t *ppos)
1358 size_t num_written = 0;
1361 struct inode *inode = file->f_path.dentry->d_inode;
1362 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1363 unsigned long page_index;
1365 if (file->f_flags & O_DIRECT)
1369 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1370 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1371 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1376 err = remove_suid(file->f_path.dentry);
1379 file_update_time(file);
1380 mutex_lock(&inode->i_mutex);
1382 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1383 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1386 page_index = pos >> PAGE_CACHE_SHIFT;
1387 page = grab_cache_page(inode->i_mapping, page_index);
1388 if (!PageUptodate(page)) {
1389 ret = mpage_readpage(page, btrfs_get_block);
1393 ret = btrfs_copy_from_user(pos, 1,
1394 write_bytes, &page, buf);
1396 write_bytes = inline_one_page(root, inode, page, pos,
1397 offset, write_bytes);
1398 SetPageUptodate(page);
1399 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1400 i_size_write(inode, pos + write_bytes);
1401 mark_inode_dirty(inode);
1403 page_cache_release(page);
1405 if (write_bytes < 0)
1408 count -= write_bytes;
1410 num_written += write_bytes;
1412 balance_dirty_pages_ratelimited(inode->i_mapping);
1416 mutex_unlock(&inode->i_mutex);
1419 current->backing_dev_info = NULL;
1420 return num_written ? num_written : err;
1424 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1425 unsigned long offset, unsigned long size)
1428 unsigned long left, count = desc->count;
1433 if (!PageChecked(page)) {
1434 /* FIXME, do it per block */
1435 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1436 int ret = btrfs_csum_verify_file_block(root,
1437 page->mapping->host->i_ino,
1438 page->index << PAGE_CACHE_SHIFT,
1439 kmap(page), PAGE_CACHE_SIZE);
1441 printk("failed to verify ino %lu page %lu\n",
1442 page->mapping->host->i_ino,
1444 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1446 SetPageChecked(page);
1450 * Faults on the destination of a read are common, so do it before
1453 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1454 kaddr = kmap_atomic(page, KM_USER0);
1455 left = __copy_to_user_inatomic(desc->arg.buf,
1456 kaddr + offset, size);
1457 kunmap_atomic(kaddr, KM_USER0);
1462 /* Do it the slow way */
1464 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1469 desc->error = -EFAULT;
1472 desc->count = count - size;
1473 desc->written += size;
1474 desc->arg.buf += size;
1479 * btrfs_file_aio_read - filesystem read routine
1480 * @iocb: kernel I/O control block
1481 * @iov: io vector request
1482 * @nr_segs: number of segments in the iovec
1483 * @pos: current file position
1485 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1486 unsigned long nr_segs, loff_t pos)
1488 struct file *filp = iocb->ki_filp;
1492 loff_t *ppos = &iocb->ki_pos;
1495 for (seg = 0; seg < nr_segs; seg++) {
1496 const struct iovec *iv = &iov[seg];
1499 * If any segment has a negative length, or the cumulative
1500 * length ever wraps negative then return -EINVAL.
1502 count += iv->iov_len;
1503 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1505 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1510 count -= iv->iov_len; /* This segment is no good */
1515 for (seg = 0; seg < nr_segs; seg++) {
1516 read_descriptor_t desc;
1519 desc.arg.buf = iov[seg].iov_base;
1520 desc.count = iov[seg].iov_len;
1521 if (desc.count == 0)
1524 do_generic_file_read(filp, ppos, &desc,
1526 retval += desc.written;
1528 retval = retval ?: desc.error;
1536 static struct kmem_cache *btrfs_inode_cachep;
1537 struct kmem_cache *btrfs_trans_handle_cachep;
1538 struct kmem_cache *btrfs_transaction_cachep;
1539 struct kmem_cache *btrfs_bit_radix_cachep;
1540 struct kmem_cache *btrfs_path_cachep;
1543 * Called inside transaction, so use GFP_NOFS
1545 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1547 struct btrfs_inode *ei;
1549 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1552 ei->magic = 0xDEADBEEF;
1553 ei->magic2 = 0xDEADBEAF;
1554 return &ei->vfs_inode;
1557 static void btrfs_destroy_inode(struct inode *inode)
1559 struct btrfs_inode *ei = BTRFS_I(inode);
1560 WARN_ON(ei->magic != 0xDEADBEEF);
1561 WARN_ON(ei->magic2 != 0xDEADBEAF);
1562 WARN_ON(!list_empty(&inode->i_dentry));
1563 WARN_ON(inode->i_data.nrpages);
1567 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1570 static void init_once(void * foo, struct kmem_cache * cachep,
1571 unsigned long flags)
1573 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1575 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1576 SLAB_CTOR_CONSTRUCTOR) {
1577 inode_init_once(&ei->vfs_inode);
1581 static int init_inodecache(void)
1583 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1584 sizeof(struct btrfs_inode),
1585 0, (SLAB_RECLAIM_ACCOUNT|
1588 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1589 sizeof(struct btrfs_trans_handle),
1590 0, (SLAB_RECLAIM_ACCOUNT|
1593 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1594 sizeof(struct btrfs_transaction),
1595 0, (SLAB_RECLAIM_ACCOUNT|
1598 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1599 sizeof(struct btrfs_transaction),
1600 0, (SLAB_RECLAIM_ACCOUNT|
1603 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1605 0, (SLAB_RECLAIM_ACCOUNT|
1607 SLAB_DESTROY_BY_RCU),
1609 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1610 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1615 static void destroy_inodecache(void)
1617 kmem_cache_destroy(btrfs_inode_cachep);
1618 kmem_cache_destroy(btrfs_trans_handle_cachep);
1619 kmem_cache_destroy(btrfs_transaction_cachep);
1620 kmem_cache_destroy(btrfs_bit_radix_cachep);
1621 kmem_cache_destroy(btrfs_path_cachep);
1624 static int btrfs_get_sb(struct file_system_type *fs_type,
1625 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1627 return get_sb_bdev(fs_type, flags, dev_name, data,
1628 btrfs_fill_super, mnt);
1631 static struct file_system_type btrfs_fs_type = {
1632 .owner = THIS_MODULE,
1634 .get_sb = btrfs_get_sb,
1635 .kill_sb = kill_block_super,
1636 .fs_flags = FS_REQUIRES_DEV,
1639 static struct super_operations btrfs_super_ops = {
1640 .statfs = simple_statfs,
1641 .delete_inode = btrfs_delete_inode,
1642 .put_super = btrfs_put_super,
1643 .read_inode = btrfs_read_locked_inode,
1644 .write_super = btrfs_write_super,
1645 .sync_fs = btrfs_sync_fs,
1646 .write_inode = btrfs_write_inode,
1647 .alloc_inode = btrfs_alloc_inode,
1648 .destroy_inode = btrfs_destroy_inode,
1651 static struct inode_operations btrfs_dir_inode_operations = {
1652 .lookup = btrfs_lookup,
1653 .create = btrfs_create,
1654 .unlink = btrfs_unlink,
1655 .mkdir = btrfs_mkdir,
1656 .rmdir = btrfs_rmdir,
1659 static struct file_operations btrfs_dir_file_operations = {
1660 .llseek = generic_file_llseek,
1661 .read = generic_read_dir,
1662 .readdir = btrfs_readdir,
1665 static struct address_space_operations btrfs_aops = {
1666 .readpage = btrfs_readpage,
1667 .readpages = btrfs_readpages,
1668 .writepage = btrfs_writepage,
1669 .sync_page = block_sync_page,
1670 .prepare_write = btrfs_prepare_write,
1671 .commit_write = btrfs_commit_write,
1674 static struct inode_operations btrfs_file_inode_operations = {
1675 .truncate = btrfs_truncate,
1678 static struct file_operations btrfs_file_operations = {
1679 .llseek = generic_file_llseek,
1680 .read = do_sync_read,
1681 .aio_read = btrfs_file_aio_read,
1682 .write = btrfs_file_write,
1683 .mmap = generic_file_mmap,
1684 .open = generic_file_open,
1687 static int __init init_btrfs_fs(void)
1690 printk("btrfs loaded!\n");
1691 err = init_inodecache();
1694 return register_filesystem(&btrfs_fs_type);
1697 static void __exit exit_btrfs_fs(void)
1699 destroy_inodecache();
1700 unregister_filesystem(&btrfs_fs_type);
1701 printk("btrfs unloaded\n");
1704 module_init(init_btrfs_fs)
1705 module_exit(exit_btrfs_fs)
1707 MODULE_LICENSE("GPL");