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_ITEM_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_ITEM_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);
827 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
828 struct buffer_head *result, int create)
830 struct btrfs_root *root = btrfs_sb(inode->i_sb);
831 struct btrfs_path *path;
832 struct btrfs_key key;
833 struct btrfs_leaf *leaf;
834 int num_bytes = result->b_size;
844 path = btrfs_alloc_path();
851 pos = iblock << inode->i_blkbits;
852 key.objectid = inode->i_ino;
854 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
855 ptr = kmap(result->b_page);
857 ptr += (pos & (PAGE_CACHE_SIZE -1));
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
868 leaf = btrfs_buffer_leaf(path->nodes[0]);
869 item_size = btrfs_item_size(leaf->items + path->slots[0]);
870 copy_size = min(num_bytes, item_size);
871 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
872 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
873 memcpy(ptr, data_ptr, copy_size);
875 num_bytes -= copy_size;
876 WARN_ON(num_bytes < 0);
878 btrfs_release_path(root, path);
879 if (num_bytes != 0) {
880 if (pos >= i_size_read(inode))
881 memset(ptr, 0, num_bytes);
885 set_buffer_uptodate(result);
886 map_bh(result, inode->i_sb, 0);
889 btrfs_free_path(path);
890 kunmap(result->b_page);
894 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
895 struct buffer_head *result, int create)
900 u64 extent_start = 0;
902 u64 objectid = inode->i_ino;
903 struct btrfs_path *path;
904 struct btrfs_root *root = btrfs_sb(inode->i_sb);
905 struct btrfs_trans_handle *trans = NULL;
906 struct btrfs_file_extent_item *item;
907 struct btrfs_leaf *leaf;
908 struct btrfs_disk_key *found_key;
910 path = btrfs_alloc_path();
912 btrfs_init_path(path);
914 trans = btrfs_start_transaction(root, 1);
917 ret = btrfs_lookup_file_extent(trans, root, path,
919 iblock << inode->i_blkbits, 0);
926 if (path->slots[0] == 0) {
927 btrfs_release_path(root, path);
933 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
934 struct btrfs_file_extent_item);
935 leaf = btrfs_buffer_leaf(path->nodes[0]);
936 blocknr = btrfs_file_extent_disk_blocknr(item);
937 blocknr += btrfs_file_extent_offset(item);
939 /* exact match found, use it */
942 map_bh(result, inode->i_sb, blocknr);
946 /* are we inside the extent that was found? */
947 found_key = &leaf->items[path->slots[0]].key;
948 if (btrfs_disk_key_objectid(found_key) != objectid ||
949 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
952 btrfs_release_path(root, path);
956 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
957 extent_start = extent_start >> inode->i_blkbits;
958 extent_start += btrfs_file_extent_offset(item);
959 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
960 if (iblock >= extent_start && iblock < extent_end) {
962 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
966 /* ok, create a new extent */
971 ret = btrfs_alloc_file_extent(trans, root, objectid,
972 iblock << inode->i_blkbits,
973 1, extent_end, &blocknr);
978 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
979 set_buffer_new(result);
980 map_bh(result, inode->i_sb, blocknr);
983 btrfs_release_path(root, path);
984 btrfs_free_path(path);
986 btrfs_end_transaction(trans, root);
990 static int btrfs_get_block(struct inode *inode, sector_t iblock,
991 struct buffer_head *result, int create)
994 struct btrfs_root *root = btrfs_sb(inode->i_sb);
995 mutex_lock(&root->fs_info->fs_mutex);
996 // err = btrfs_get_block_lock(inode, iblock, result, create);
997 err = btrfs_get_block_inline(inode, iblock, result, create);
998 mutex_unlock(&root->fs_info->fs_mutex);
1002 static int btrfs_prepare_write(struct file *file, struct page *page,
1003 unsigned from, unsigned to)
1006 return nobh_prepare_write(page, from, to, btrfs_get_block);
1008 static int btrfs_commit_write(struct file *file, struct page *page,
1009 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;
1256 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1257 struct page *page, loff_t pos,
1258 size_t offset, size_t write_bytes)
1260 struct btrfs_path *path;
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_key key;
1263 struct btrfs_leaf *leaf;
1264 struct btrfs_key found_key;
1266 size_t copy_size = 0;
1269 size_t num_written = 0;
1271 path = btrfs_alloc_path();
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 trans = btrfs_start_transaction(root, 1);
1275 key.objectid = inode->i_ino;
1277 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1281 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1287 leaf = btrfs_buffer_leaf(path->nodes[0]);
1288 btrfs_disk_key_to_cpu(&found_key,
1289 &leaf->items[path->slots[0]].key);
1290 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1291 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1292 copy_size = min(write_bytes, copy_size);
1295 int slot = path->slots[0];
1299 // FIXME find max key
1300 leaf = btrfs_buffer_leaf(path->nodes[0]);
1301 btrfs_disk_key_to_cpu(&found_key,
1302 &leaf->items[slot].key);
1303 if (found_key.objectid != inode->i_ino)
1305 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1307 copy_size = btrfs_item_size(leaf->items + slot);
1308 if (found_key.offset + copy_size <= pos)
1310 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1311 dst += pos - found_key.offset;
1312 copy_size = copy_size - (pos - found_key.offset);
1313 BUG_ON(copy_size < 0);
1314 copy_size = min(write_bytes, copy_size);
1315 WARN_ON(copy_size == 0);
1319 btrfs_release_path(root, path);
1320 copy_size = min(write_bytes,
1321 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1322 sizeof(struct btrfs_item) * 4);
1323 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1325 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1326 path->slots[0], char);
1328 WARN_ON(copy_size == 0);
1329 WARN_ON(dst + copy_size >
1330 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1331 path->slots[0], char) +
1332 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1334 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1335 page_address(page) + offset, copy_size);
1336 mark_buffer_dirty(path->nodes[0]);
1337 btrfs_release_path(root, path);
1339 offset += copy_size;
1340 num_written += copy_size;
1341 write_bytes -= copy_size;
1345 btrfs_free_path(path);
1346 ret = btrfs_end_transaction(trans, root);
1348 mutex_unlock(&root->fs_info->fs_mutex);
1349 return num_written ? num_written : err;
1352 static ssize_t btrfs_file_inline_write(struct file *file,
1353 const char __user *buf,
1354 size_t count, loff_t *ppos)
1357 size_t num_written = 0;
1360 struct inode *inode = file->f_path.dentry->d_inode;
1361 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1362 unsigned long page_index;
1364 if (file->f_flags & O_DIRECT)
1368 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1369 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1370 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1375 err = remove_suid(file->f_path.dentry);
1378 file_update_time(file);
1379 mutex_lock(&inode->i_mutex);
1381 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1382 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1385 page_index = pos >> PAGE_CACHE_SHIFT;
1386 page = grab_cache_page(inode->i_mapping, page_index);
1387 if (!PageUptodate(page)) {
1388 ret = mpage_readpage(page, btrfs_get_block);
1392 ret = btrfs_copy_from_user(pos, 1,
1393 write_bytes, &page, buf);
1395 write_bytes = inline_one_page(root, inode, page, pos,
1396 offset, write_bytes);
1397 SetPageUptodate(page);
1398 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1399 i_size_write(inode, pos + write_bytes);
1400 mark_inode_dirty(inode);
1402 page_cache_release(page);
1404 if (write_bytes < 0)
1407 count -= write_bytes;
1409 num_written += write_bytes;
1411 balance_dirty_pages_ratelimited(inode->i_mapping);
1415 mutex_unlock(&inode->i_mutex);
1418 current->backing_dev_info = NULL;
1419 return num_written ? num_written : err;
1422 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1423 unsigned long offset, unsigned long size)
1426 unsigned long left, count = desc->count;
1431 if (!PageChecked(page)) {
1432 /* FIXME, do it per block */
1433 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1434 int ret = btrfs_csum_verify_file_block(root,
1435 page->mapping->host->i_ino,
1436 page->index << PAGE_CACHE_SHIFT,
1437 kmap(page), PAGE_CACHE_SIZE);
1439 printk("failed to verify ino %lu page %lu\n",
1440 page->mapping->host->i_ino,
1442 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1444 SetPageChecked(page);
1448 * Faults on the destination of a read are common, so do it before
1451 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1452 kaddr = kmap_atomic(page, KM_USER0);
1453 left = __copy_to_user_inatomic(desc->arg.buf,
1454 kaddr + offset, size);
1455 kunmap_atomic(kaddr, KM_USER0);
1460 /* Do it the slow way */
1462 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1467 desc->error = -EFAULT;
1470 desc->count = count - size;
1471 desc->written += size;
1472 desc->arg.buf += size;
1477 * btrfs_file_aio_read - filesystem read routine
1478 * @iocb: kernel I/O control block
1479 * @iov: io vector request
1480 * @nr_segs: number of segments in the iovec
1481 * @pos: current file position
1483 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1484 unsigned long nr_segs, loff_t pos)
1486 struct file *filp = iocb->ki_filp;
1490 loff_t *ppos = &iocb->ki_pos;
1493 for (seg = 0; seg < nr_segs; seg++) {
1494 const struct iovec *iv = &iov[seg];
1497 * If any segment has a negative length, or the cumulative
1498 * length ever wraps negative then return -EINVAL.
1500 count += iv->iov_len;
1501 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1503 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1508 count -= iv->iov_len; /* This segment is no good */
1513 for (seg = 0; seg < nr_segs; seg++) {
1514 read_descriptor_t desc;
1517 desc.arg.buf = iov[seg].iov_base;
1518 desc.count = iov[seg].iov_len;
1519 if (desc.count == 0)
1522 do_generic_file_read(filp, ppos, &desc,
1524 retval += desc.written;
1526 retval = retval ?: desc.error;
1534 static struct kmem_cache *btrfs_inode_cachep;
1535 struct kmem_cache *btrfs_trans_handle_cachep;
1536 struct kmem_cache *btrfs_transaction_cachep;
1537 struct kmem_cache *btrfs_bit_radix_cachep;
1538 struct kmem_cache *btrfs_path_cachep;
1541 * Called inside transaction, so use GFP_NOFS
1543 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1545 struct btrfs_inode *ei;
1547 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1550 ei->magic = 0xDEADBEEF;
1551 ei->magic2 = 0xDEADBEAF;
1552 return &ei->vfs_inode;
1555 static void btrfs_destroy_inode(struct inode *inode)
1557 struct btrfs_inode *ei = BTRFS_I(inode);
1558 WARN_ON(ei->magic != 0xDEADBEEF);
1559 WARN_ON(ei->magic2 != 0xDEADBEAF);
1560 WARN_ON(!list_empty(&inode->i_dentry));
1561 WARN_ON(inode->i_data.nrpages);
1565 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1568 static void init_once(void * foo, struct kmem_cache * cachep,
1569 unsigned long flags)
1571 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1573 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1574 SLAB_CTOR_CONSTRUCTOR) {
1575 inode_init_once(&ei->vfs_inode);
1579 static int init_inodecache(void)
1581 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1582 sizeof(struct btrfs_inode),
1583 0, (SLAB_RECLAIM_ACCOUNT|
1586 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1587 sizeof(struct btrfs_trans_handle),
1588 0, (SLAB_RECLAIM_ACCOUNT|
1591 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1592 sizeof(struct btrfs_transaction),
1593 0, (SLAB_RECLAIM_ACCOUNT|
1596 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1597 sizeof(struct btrfs_transaction),
1598 0, (SLAB_RECLAIM_ACCOUNT|
1601 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1603 0, (SLAB_RECLAIM_ACCOUNT|
1605 SLAB_DESTROY_BY_RCU),
1607 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1608 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1613 static void destroy_inodecache(void)
1615 kmem_cache_destroy(btrfs_inode_cachep);
1616 kmem_cache_destroy(btrfs_trans_handle_cachep);
1617 kmem_cache_destroy(btrfs_transaction_cachep);
1618 kmem_cache_destroy(btrfs_bit_radix_cachep);
1619 kmem_cache_destroy(btrfs_path_cachep);
1622 static int btrfs_get_sb(struct file_system_type *fs_type,
1623 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1625 return get_sb_bdev(fs_type, flags, dev_name, data,
1626 btrfs_fill_super, mnt);
1629 static struct file_system_type btrfs_fs_type = {
1630 .owner = THIS_MODULE,
1632 .get_sb = btrfs_get_sb,
1633 .kill_sb = kill_block_super,
1634 .fs_flags = FS_REQUIRES_DEV,
1637 static struct super_operations btrfs_super_ops = {
1638 .statfs = simple_statfs,
1639 .delete_inode = btrfs_delete_inode,
1640 .put_super = btrfs_put_super,
1641 .read_inode = btrfs_read_locked_inode,
1642 .write_super = btrfs_write_super,
1643 .sync_fs = btrfs_sync_fs,
1644 .write_inode = btrfs_write_inode,
1645 .alloc_inode = btrfs_alloc_inode,
1646 .destroy_inode = btrfs_destroy_inode,
1649 static struct inode_operations btrfs_dir_inode_operations = {
1650 .lookup = btrfs_lookup,
1651 .create = btrfs_create,
1652 .unlink = btrfs_unlink,
1653 .mkdir = btrfs_mkdir,
1654 .rmdir = btrfs_rmdir,
1657 static struct file_operations btrfs_dir_file_operations = {
1658 .llseek = generic_file_llseek,
1659 .read = generic_read_dir,
1660 .readdir = btrfs_readdir,
1663 static struct address_space_operations btrfs_aops = {
1664 .readpage = btrfs_readpage,
1665 // .readpages = btrfs_readpages,
1666 .writepage = btrfs_writepage,
1667 .sync_page = block_sync_page,
1668 .prepare_write = btrfs_prepare_write,
1669 .commit_write = btrfs_commit_write,
1672 static struct inode_operations btrfs_file_inode_operations = {
1673 .truncate = btrfs_truncate,
1676 static struct file_operations btrfs_file_operations = {
1677 .llseek = generic_file_llseek,
1678 .read = do_sync_read,
1679 .aio_read = generic_file_aio_read,
1680 .write = btrfs_file_inline_write,
1681 .mmap = generic_file_mmap,
1682 .open = generic_file_open,
1685 static int __init init_btrfs_fs(void)
1688 printk("btrfs loaded!\n");
1689 err = init_inodecache();
1692 return register_filesystem(&btrfs_fs_type);
1695 static void __exit exit_btrfs_fs(void)
1697 destroy_inodecache();
1698 unregister_filesystem(&btrfs_fs_type);
1699 printk("btrfs unloaded\n");
1702 module_init(init_btrfs_fs)
1703 module_exit(exit_btrfs_fs)
1705 MODULE_LICENSE("GPL");