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>
14 #include <linux/statfs.h>
17 #include "transaction.h"
18 #include "btrfs_inode.h"
20 #include "print-tree.h"
22 struct btrfs_iget_args {
24 struct btrfs_root *root;
27 #define BTRFS_SUPER_MAGIC 0x9123682E
29 static struct inode_operations btrfs_dir_inode_operations;
30 static struct inode_operations btrfs_symlink_inode_operations;
31 static struct inode_operations btrfs_dir_ro_inode_operations;
32 static struct super_operations btrfs_super_ops;
33 static struct file_operations btrfs_dir_file_operations;
34 static struct inode_operations btrfs_file_inode_operations;
35 static struct address_space_operations btrfs_aops;
36 static struct address_space_operations btrfs_symlink_aops;
37 static struct file_operations btrfs_file_operations;
39 static int drop_extents(struct btrfs_trans_handle *trans,
40 struct btrfs_root *root,
42 u64 start, u64 end, u64 *hint_block);
43 static int btrfs_get_block(struct inode *inode, sector_t iblock,
44 struct buffer_head *result, int create);
48 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
49 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
50 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
51 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
52 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
53 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
54 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
55 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
58 static void btrfs_read_locked_inode(struct inode *inode)
60 struct btrfs_path *path;
61 struct btrfs_inode_item *inode_item;
62 struct btrfs_root *root = BTRFS_I(inode)->root;
63 struct btrfs_key location;
64 struct btrfs_block_group_cache *alloc_group;
65 u64 alloc_group_block;
68 path = btrfs_alloc_path();
70 btrfs_init_path(path);
71 mutex_lock(&root->fs_info->fs_mutex);
73 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
74 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
76 btrfs_free_path(path);
79 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
81 struct btrfs_inode_item);
83 inode->i_mode = btrfs_inode_mode(inode_item);
84 inode->i_nlink = btrfs_inode_nlink(inode_item);
85 inode->i_uid = btrfs_inode_uid(inode_item);
86 inode->i_gid = btrfs_inode_gid(inode_item);
87 inode->i_size = btrfs_inode_size(inode_item);
88 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
89 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
90 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
91 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
92 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
93 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
94 inode->i_blocks = btrfs_inode_nblocks(inode_item);
95 inode->i_generation = btrfs_inode_generation(inode_item);
96 alloc_group_block = btrfs_inode_block_group(inode_item);
97 ret = radix_tree_gang_lookup(&root->fs_info->block_group_radix,
98 (void **)&alloc_group,
99 alloc_group_block, 1);
101 BTRFS_I(inode)->block_group = alloc_group;
103 btrfs_free_path(path);
106 mutex_unlock(&root->fs_info->fs_mutex);
108 switch (inode->i_mode & S_IFMT) {
111 init_special_inode(inode, inode->i_mode,
112 btrfs_inode_rdev(inode_item));
116 inode->i_mapping->a_ops = &btrfs_aops;
117 inode->i_fop = &btrfs_file_operations;
118 inode->i_op = &btrfs_file_inode_operations;
121 inode->i_fop = &btrfs_dir_file_operations;
122 if (root == root->fs_info->tree_root)
123 inode->i_op = &btrfs_dir_ro_inode_operations;
125 inode->i_op = &btrfs_dir_inode_operations;
128 inode->i_op = &btrfs_symlink_inode_operations;
129 inode->i_mapping->a_ops = &btrfs_symlink_aops;
135 btrfs_release_path(root, path);
136 btrfs_free_path(path);
137 mutex_unlock(&root->fs_info->fs_mutex);
138 make_bad_inode(inode);
141 static void fill_inode_item(struct btrfs_inode_item *item,
144 btrfs_set_inode_uid(item, inode->i_uid);
145 btrfs_set_inode_gid(item, inode->i_gid);
146 btrfs_set_inode_size(item, inode->i_size);
147 btrfs_set_inode_mode(item, inode->i_mode);
148 btrfs_set_inode_nlink(item, inode->i_nlink);
149 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
150 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
151 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
152 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
153 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
154 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
155 btrfs_set_inode_nblocks(item, inode->i_blocks);
156 btrfs_set_inode_generation(item, inode->i_generation);
157 btrfs_set_inode_block_group(item,
158 BTRFS_I(inode)->block_group->key.objectid);
161 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
162 struct btrfs_root *root,
165 struct btrfs_inode_item *inode_item;
166 struct btrfs_path *path;
169 path = btrfs_alloc_path();
171 btrfs_init_path(path);
172 ret = btrfs_lookup_inode(trans, root, path,
173 &BTRFS_I(inode)->location, 1);
180 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
182 struct btrfs_inode_item);
184 fill_inode_item(inode_item, inode);
185 btrfs_mark_buffer_dirty(path->nodes[0]);
188 btrfs_release_path(root, path);
189 btrfs_free_path(path);
194 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
195 struct btrfs_root *root,
197 struct dentry *dentry)
199 struct btrfs_path *path;
200 const char *name = dentry->d_name.name;
201 int name_len = dentry->d_name.len;
204 struct btrfs_dir_item *di;
206 path = btrfs_alloc_path();
208 btrfs_init_path(path);
209 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
219 objectid = btrfs_disk_key_objectid(&di->location);
220 ret = btrfs_delete_one_dir_name(trans, root, path, di);
222 btrfs_release_path(root, path);
224 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
225 objectid, name, name_len, -1);
234 ret = btrfs_delete_one_dir_name(trans, root, path, di);
237 dentry->d_inode->i_ctime = dir->i_ctime;
239 btrfs_free_path(path);
241 dir->i_size -= name_len * 2;
242 btrfs_update_inode(trans, root, dir);
243 drop_nlink(dentry->d_inode);
244 btrfs_update_inode(trans, root, dentry->d_inode);
245 dir->i_sb->s_dirt = 1;
250 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
252 struct btrfs_root *root;
253 struct btrfs_trans_handle *trans;
256 root = BTRFS_I(dir)->root;
257 mutex_lock(&root->fs_info->fs_mutex);
258 trans = btrfs_start_transaction(root, 1);
259 btrfs_set_trans_block_group(trans, dir);
260 ret = btrfs_unlink_trans(trans, root, dir, dentry);
261 btrfs_end_transaction(trans, root);
262 mutex_unlock(&root->fs_info->fs_mutex);
263 btrfs_btree_balance_dirty(root);
267 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
269 struct inode *inode = dentry->d_inode;
272 struct btrfs_root *root = BTRFS_I(dir)->root;
273 struct btrfs_path *path;
274 struct btrfs_key key;
275 struct btrfs_trans_handle *trans;
276 struct btrfs_key found_key;
278 struct btrfs_leaf *leaf;
279 char *goodnames = "..";
281 path = btrfs_alloc_path();
283 btrfs_init_path(path);
284 mutex_lock(&root->fs_info->fs_mutex);
285 trans = btrfs_start_transaction(root, 1);
286 btrfs_set_trans_block_group(trans, dir);
287 key.objectid = inode->i_ino;
288 key.offset = (u64)-1;
291 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
297 if (path->slots[0] == 0) {
302 leaf = btrfs_buffer_leaf(path->nodes[0]);
303 btrfs_disk_key_to_cpu(&found_key,
304 &leaf->items[path->slots[0]].key);
305 found_type = btrfs_key_type(&found_key);
306 if (found_key.objectid != inode->i_ino) {
310 if ((found_type != BTRFS_DIR_ITEM_KEY &&
311 found_type != BTRFS_DIR_INDEX_KEY) ||
312 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
313 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
317 ret = btrfs_del_item(trans, root, path);
320 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
322 btrfs_release_path(root, path);
325 btrfs_release_path(root, path);
327 /* now the directory is empty */
328 err = btrfs_unlink_trans(trans, root, dir, dentry);
333 btrfs_release_path(root, path);
334 btrfs_free_path(path);
335 mutex_unlock(&root->fs_info->fs_mutex);
336 ret = btrfs_end_transaction(trans, root);
337 btrfs_btree_balance_dirty(root);
343 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
344 struct btrfs_root *root,
347 struct btrfs_path *path;
352 path = btrfs_alloc_path();
354 btrfs_init_path(path);
355 ret = btrfs_lookup_inode(trans, root, path,
356 &BTRFS_I(inode)->location, -1);
358 ret = btrfs_del_item(trans, root, path);
360 btrfs_free_path(path);
364 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
367 struct btrfs_node *node;
377 node = btrfs_buffer_node(path->nodes[1]);
378 slot = path->slots[1];
381 nritems = btrfs_header_nritems(&node->header);
382 for (i = slot - 1; i >= 0; i--) {
383 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
384 if (item_objectid != objectid)
386 blocknr = btrfs_node_blockptr(node, i);
387 ret = readahead_tree_block(root, blocknr);
393 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root,
398 struct btrfs_path *path;
399 struct btrfs_key key;
400 struct btrfs_disk_key *found_key;
402 struct btrfs_leaf *leaf;
403 struct btrfs_file_extent_item *fi;
404 u64 extent_start = 0;
405 u64 extent_num_blocks = 0;
410 path = btrfs_alloc_path();
412 /* FIXME, add redo link to tree so we don't leak on crash */
413 key.objectid = inode->i_ino;
414 key.offset = (u64)-1;
417 btrfs_init_path(path);
419 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
424 BUG_ON(path->slots[0] == 0);
427 reada_truncate(root, path, inode->i_ino);
428 leaf = btrfs_buffer_leaf(path->nodes[0]);
429 found_key = &leaf->items[path->slots[0]].key;
430 found_type = btrfs_disk_key_type(found_key);
431 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
433 if (found_type != BTRFS_CSUM_ITEM_KEY &&
434 found_type != BTRFS_DIR_ITEM_KEY &&
435 found_type != BTRFS_DIR_INDEX_KEY &&
436 found_type != BTRFS_EXTENT_DATA_KEY)
438 item_end = btrfs_disk_key_offset(found_key);
439 if (found_type == BTRFS_EXTENT_DATA_KEY) {
440 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
442 struct btrfs_file_extent_item);
443 if (btrfs_file_extent_type(fi) !=
444 BTRFS_FILE_EXTENT_INLINE) {
445 item_end += btrfs_file_extent_num_blocks(fi) <<
449 if (found_type == BTRFS_CSUM_ITEM_KEY) {
450 ret = btrfs_csum_truncate(trans, root, path,
454 if (item_end < inode->i_size) {
456 btrfs_set_key_type(&key, found_type - 1);
461 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
467 if (found_type == BTRFS_EXTENT_DATA_KEY &&
468 btrfs_file_extent_type(fi) !=
469 BTRFS_FILE_EXTENT_INLINE) {
472 u64 orig_num_blocks =
473 btrfs_file_extent_num_blocks(fi);
474 extent_num_blocks = inode->i_size -
475 btrfs_disk_key_offset(found_key) +
477 extent_num_blocks >>= inode->i_blkbits;
478 btrfs_set_file_extent_num_blocks(fi,
480 inode->i_blocks -= (orig_num_blocks -
481 extent_num_blocks) << 3;
482 mark_buffer_dirty(path->nodes[0]);
485 btrfs_file_extent_disk_blocknr(fi);
487 btrfs_file_extent_disk_num_blocks(fi);
488 /* FIXME blocksize != 4096 */
489 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
490 if (extent_start != 0) {
492 inode->i_blocks -= num_dec;
497 ret = btrfs_del_item(trans, root, path);
502 btrfs_release_path(root, path);
504 ret = btrfs_free_extent(trans, root, extent_start,
505 extent_num_blocks, 0);
511 btrfs_release_path(root, path);
512 btrfs_free_path(path);
513 inode->i_sb->s_dirt = 1;
517 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
519 struct inode *inode = mapping->host;
520 unsigned blocksize = 1 << inode->i_blkbits;
521 pgoff_t index = from >> PAGE_CACHE_SHIFT;
522 unsigned offset = from & (PAGE_CACHE_SIZE-1);
526 struct btrfs_root *root = BTRFS_I(inode)->root;
528 struct btrfs_key ins;
529 struct btrfs_trans_handle *trans;
531 if ((offset & (blocksize - 1)) == 0)
535 page = grab_cache_page(mapping, index);
539 if (!PageUptodate(page)) {
540 ret = mpage_readpage(page, btrfs_get_block);
542 if (!PageUptodate(page)) {
547 mutex_lock(&root->fs_info->fs_mutex);
548 trans = btrfs_start_transaction(root, 1);
549 btrfs_set_trans_block_group(trans, inode);
551 ret = drop_extents(trans, root, inode, page->index << PAGE_CACHE_SHIFT,
552 (page->index + 1) << PAGE_CACHE_SHIFT, &alloc_hint);
554 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
555 alloc_hint, (u64)-1, &ins, 1);
557 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
558 page->index << PAGE_CACHE_SHIFT,
561 SetPageChecked(page);
563 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
564 flush_dcache_page(page);
565 btrfs_csum_file_block(trans, root, inode->i_ino,
566 page->index << PAGE_CACHE_SHIFT,
567 kaddr, PAGE_CACHE_SIZE);
569 btrfs_end_transaction(trans, root);
570 mutex_unlock(&root->fs_info->fs_mutex);
572 set_page_dirty(page);
574 page_cache_release(page);
579 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
581 struct inode *inode = dentry->d_inode;
584 err = inode_change_ok(inode, attr);
588 if (S_ISREG(inode->i_mode) &&
589 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
590 struct btrfs_trans_handle *trans;
591 struct btrfs_root *root = BTRFS_I(inode)->root;
592 u64 mask = root->blocksize - 1;
593 u64 pos = (inode->i_size + mask) & ~mask;
596 if (attr->ia_size <= pos)
599 btrfs_truncate_page(inode->i_mapping, inode->i_size);
601 hole_size = (attr->ia_size - pos + mask) & ~mask;
602 hole_size >>= inode->i_blkbits;
604 mutex_lock(&root->fs_info->fs_mutex);
605 trans = btrfs_start_transaction(root, 1);
606 btrfs_set_trans_block_group(trans, inode);
607 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
608 pos, 0, 0, hole_size);
610 btrfs_end_transaction(trans, root);
611 mutex_unlock(&root->fs_info->fs_mutex);
614 err = inode_setattr(inode, attr);
618 static void btrfs_delete_inode(struct inode *inode)
620 struct btrfs_trans_handle *trans;
621 struct btrfs_root *root = BTRFS_I(inode)->root;
624 truncate_inode_pages(&inode->i_data, 0);
625 if (is_bad_inode(inode)) {
629 mutex_lock(&root->fs_info->fs_mutex);
630 trans = btrfs_start_transaction(root, 1);
631 btrfs_set_trans_block_group(trans, inode);
632 ret = btrfs_truncate_in_trans(trans, root, inode);
634 btrfs_free_inode(trans, root, inode);
635 btrfs_end_transaction(trans, root);
636 mutex_unlock(&root->fs_info->fs_mutex);
637 btrfs_btree_balance_dirty(root);
643 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
644 struct btrfs_key *location)
646 const char *name = dentry->d_name.name;
647 int namelen = dentry->d_name.len;
648 struct btrfs_dir_item *di;
649 struct btrfs_path *path;
650 struct btrfs_root *root = BTRFS_I(dir)->root;
653 path = btrfs_alloc_path();
655 btrfs_init_path(path);
656 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
658 if (!di || IS_ERR(di)) {
659 location->objectid = 0;
663 btrfs_disk_key_to_cpu(location, &di->location);
665 btrfs_release_path(root, path);
666 btrfs_free_path(path);
670 static int fixup_tree_root_location(struct btrfs_root *root,
671 struct btrfs_key *location,
672 struct btrfs_root **sub_root)
674 struct btrfs_path *path;
675 struct btrfs_root_item *ri;
677 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
679 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
682 path = btrfs_alloc_path();
684 mutex_lock(&root->fs_info->fs_mutex);
686 *sub_root = btrfs_read_fs_root(root->fs_info, location);
687 if (IS_ERR(*sub_root))
688 return PTR_ERR(*sub_root);
690 ri = &(*sub_root)->root_item;
691 location->objectid = btrfs_root_dirid(ri);
693 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
694 location->offset = 0;
696 btrfs_free_path(path);
697 mutex_unlock(&root->fs_info->fs_mutex);
701 static int btrfs_init_locked_inode(struct inode *inode, void *p)
703 struct btrfs_iget_args *args = p;
704 inode->i_ino = args->ino;
705 BTRFS_I(inode)->root = args->root;
709 static int btrfs_find_actor(struct inode *inode, void *opaque)
711 struct btrfs_iget_args *args = opaque;
712 return (args->ino == inode->i_ino &&
713 args->root == BTRFS_I(inode)->root);
716 static struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
717 struct btrfs_root *root)
720 struct btrfs_iget_args args;
724 inode = iget5_locked(s, objectid, btrfs_find_actor,
725 btrfs_init_locked_inode,
730 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
731 struct nameidata *nd)
733 struct inode * inode;
734 struct btrfs_inode *bi = BTRFS_I(dir);
735 struct btrfs_root *root = bi->root;
736 struct btrfs_root *sub_root = root;
737 struct btrfs_key location;
740 if (dentry->d_name.len > BTRFS_NAME_LEN)
741 return ERR_PTR(-ENAMETOOLONG);
742 mutex_lock(&root->fs_info->fs_mutex);
743 ret = btrfs_inode_by_name(dir, dentry, &location);
744 mutex_unlock(&root->fs_info->fs_mutex);
748 if (location.objectid) {
749 ret = fixup_tree_root_location(root, &location, &sub_root);
753 return ERR_PTR(-ENOENT);
754 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
757 return ERR_PTR(-EACCES);
758 if (inode->i_state & I_NEW) {
759 if (sub_root != root) {
760 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
762 sub_root->inode = inode;
764 BTRFS_I(inode)->root = sub_root;
765 memcpy(&BTRFS_I(inode)->location, &location,
767 btrfs_read_locked_inode(inode);
768 unlock_new_inode(inode);
771 return d_splice_alias(inode, dentry);
774 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
777 struct btrfs_node *node;
787 node = btrfs_buffer_node(path->nodes[1]);
788 slot = path->slots[1];
789 nritems = btrfs_header_nritems(&node->header);
790 for (i = slot + 1; i < nritems; i++) {
791 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
792 if (item_objectid != objectid)
794 blocknr = btrfs_node_blockptr(node, i);
795 ret = readahead_tree_block(root, blocknr);
800 static unsigned char btrfs_filetype_table[] = {
801 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
804 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
806 struct inode *inode = filp->f_path.dentry->d_inode;
807 struct btrfs_root *root = BTRFS_I(inode)->root;
808 struct btrfs_item *item;
809 struct btrfs_dir_item *di;
810 struct btrfs_key key;
811 struct btrfs_path *path;
814 struct btrfs_leaf *leaf;
817 unsigned char d_type;
822 int key_type = BTRFS_DIR_INDEX_KEY;
824 /* FIXME, use a real flag for deciding about the key type */
825 if (root->fs_info->tree_root == root)
826 key_type = BTRFS_DIR_ITEM_KEY;
827 mutex_lock(&root->fs_info->fs_mutex);
828 key.objectid = inode->i_ino;
830 btrfs_set_key_type(&key, key_type);
831 key.offset = filp->f_pos;
832 path = btrfs_alloc_path();
833 btrfs_init_path(path);
834 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
838 reada_leaves(root, path, inode->i_ino);
840 leaf = btrfs_buffer_leaf(path->nodes[0]);
841 nritems = btrfs_header_nritems(&leaf->header);
842 slot = path->slots[0];
843 if (advance || slot >= nritems) {
844 if (slot >= nritems -1) {
845 reada_leaves(root, path, inode->i_ino);
846 ret = btrfs_next_leaf(root, path);
849 leaf = btrfs_buffer_leaf(path->nodes[0]);
850 nritems = btrfs_header_nritems(&leaf->header);
851 slot = path->slots[0];
858 item = leaf->items + slot;
859 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
861 if (btrfs_disk_key_type(&item->key) != key_type)
863 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
865 filp->f_pos = btrfs_disk_key_offset(&item->key);
867 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
869 di_total = btrfs_item_size(leaf->items + slot);
870 while(di_cur < di_total) {
871 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
872 over = filldir(dirent, (const char *)(di + 1),
873 btrfs_dir_name_len(di),
874 btrfs_disk_key_offset(&item->key),
875 btrfs_disk_key_objectid(&di->location),
879 di_len = btrfs_dir_name_len(di) + sizeof(*di);
881 di = (struct btrfs_dir_item *)((char *)di + di_len);
888 btrfs_release_path(root, path);
889 btrfs_free_path(path);
890 mutex_unlock(&root->fs_info->fs_mutex);
894 static void btrfs_put_super (struct super_block * sb)
896 struct btrfs_root *root = btrfs_sb(sb);
899 ret = close_ctree(root);
901 printk("close ctree returns %d\n", ret);
903 sb->s_fs_info = NULL;
906 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
908 struct inode * inode;
909 struct dentry * root_dentry;
910 struct btrfs_super_block *disk_super;
911 struct btrfs_root *tree_root;
912 struct btrfs_inode *bi;
914 sb->s_maxbytes = MAX_LFS_FILESIZE;
915 sb->s_magic = BTRFS_SUPER_MAGIC;
916 sb->s_op = &btrfs_super_ops;
919 tree_root = open_ctree(sb);
922 printk("btrfs: open_ctree failed\n");
925 sb->s_fs_info = tree_root;
926 disk_super = tree_root->fs_info->disk_super;
927 printk("read in super total blocks %Lu root %Lu\n",
928 btrfs_super_total_blocks(disk_super),
929 btrfs_super_root_dir(disk_super));
931 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
934 bi->location.objectid = inode->i_ino;
935 bi->location.offset = 0;
936 bi->location.flags = 0;
937 bi->root = tree_root;
938 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
942 if (inode->i_state & I_NEW) {
943 btrfs_read_locked_inode(inode);
944 unlock_new_inode(inode);
947 root_dentry = d_alloc_root(inode);
952 sb->s_root = root_dentry;
957 static int btrfs_write_inode(struct inode *inode, int wait)
959 struct btrfs_root *root = BTRFS_I(inode)->root;
960 struct btrfs_trans_handle *trans;
964 mutex_lock(&root->fs_info->fs_mutex);
965 trans = btrfs_start_transaction(root, 1);
966 btrfs_set_trans_block_group(trans, inode);
967 ret = btrfs_commit_transaction(trans, root);
968 mutex_unlock(&root->fs_info->fs_mutex);
973 static void btrfs_dirty_inode(struct inode *inode)
975 struct btrfs_root *root = BTRFS_I(inode)->root;
976 struct btrfs_trans_handle *trans;
978 mutex_lock(&root->fs_info->fs_mutex);
979 trans = btrfs_start_transaction(root, 1);
980 btrfs_set_trans_block_group(trans, inode);
981 btrfs_update_inode(trans, root, inode);
982 btrfs_end_transaction(trans, root);
983 mutex_unlock(&root->fs_info->fs_mutex);
984 btrfs_btree_balance_dirty(root);
987 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
990 struct btrfs_block_group_cache *group,
994 struct btrfs_inode_item inode_item;
995 struct btrfs_key *location;
999 inode = new_inode(root->fs_info->sb);
1001 return ERR_PTR(-ENOMEM);
1003 BTRFS_I(inode)->root = root;
1008 group = btrfs_find_block_group(root, group, 0, 0, owner);
1009 BTRFS_I(inode)->block_group = group;
1011 inode->i_uid = current->fsuid;
1012 inode->i_gid = current->fsgid;
1013 inode->i_mode = mode;
1014 inode->i_ino = objectid;
1015 inode->i_blocks = 0;
1016 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1017 fill_inode_item(&inode_item, inode);
1018 location = &BTRFS_I(inode)->location;
1019 location->objectid = objectid;
1020 location->flags = 0;
1021 location->offset = 0;
1022 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1024 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1027 insert_inode_hash(inode);
1031 static inline u8 btrfs_inode_type(struct inode *inode)
1033 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1036 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1037 struct dentry *dentry, struct inode *inode)
1040 struct btrfs_key key;
1041 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1042 key.objectid = inode->i_ino;
1044 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1047 ret = btrfs_insert_dir_item(trans, root,
1048 dentry->d_name.name, dentry->d_name.len,
1049 dentry->d_parent->d_inode->i_ino,
1050 &key, btrfs_inode_type(inode));
1052 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
1053 ret = btrfs_update_inode(trans, root,
1054 dentry->d_parent->d_inode);
1059 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1060 struct dentry *dentry, struct inode *inode)
1062 int err = btrfs_add_link(trans, dentry, inode);
1064 d_instantiate(dentry, inode);
1072 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1073 int mode, struct nameidata *nd)
1075 struct btrfs_trans_handle *trans;
1076 struct btrfs_root *root = BTRFS_I(dir)->root;
1077 struct inode *inode;
1082 mutex_lock(&root->fs_info->fs_mutex);
1083 trans = btrfs_start_transaction(root, 1);
1084 btrfs_set_trans_block_group(trans, dir);
1086 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1092 inode = btrfs_new_inode(trans, root, objectid,
1093 BTRFS_I(dir)->block_group, mode);
1094 err = PTR_ERR(inode);
1098 btrfs_set_trans_block_group(trans, inode);
1099 err = btrfs_add_nondir(trans, dentry, inode);
1103 inode->i_mapping->a_ops = &btrfs_aops;
1104 inode->i_fop = &btrfs_file_operations;
1105 inode->i_op = &btrfs_file_inode_operations;
1107 dir->i_sb->s_dirt = 1;
1108 btrfs_update_inode_block_group(trans, inode);
1109 btrfs_update_inode_block_group(trans, dir);
1111 btrfs_end_transaction(trans, root);
1112 mutex_unlock(&root->fs_info->fs_mutex);
1115 inode_dec_link_count(inode);
1118 btrfs_btree_balance_dirty(root);
1122 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1123 struct dentry *dentry)
1125 struct btrfs_trans_handle *trans;
1126 struct btrfs_root *root = BTRFS_I(dir)->root;
1127 struct inode *inode = old_dentry->d_inode;
1131 if (inode->i_nlink == 0)
1135 mutex_lock(&root->fs_info->fs_mutex);
1136 trans = btrfs_start_transaction(root, 1);
1137 btrfs_set_trans_block_group(trans, dir);
1138 atomic_inc(&inode->i_count);
1139 err = btrfs_add_nondir(trans, dentry, inode);
1142 dir->i_sb->s_dirt = 1;
1143 btrfs_update_inode_block_group(trans, dir);
1144 btrfs_update_inode(trans, root, inode);
1146 btrfs_end_transaction(trans, root);
1147 mutex_unlock(&root->fs_info->fs_mutex);
1150 inode_dec_link_count(inode);
1153 btrfs_btree_balance_dirty(root);
1157 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root,
1159 u64 objectid, u64 dirid)
1163 struct btrfs_key key;
1168 key.objectid = objectid;
1171 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1173 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1174 &key, BTRFS_FT_DIR);
1177 key.objectid = dirid;
1178 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1179 &key, BTRFS_FT_DIR);
1186 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1188 struct inode *inode;
1189 struct btrfs_trans_handle *trans;
1190 struct btrfs_root *root = BTRFS_I(dir)->root;
1192 int drop_on_err = 0;
1195 mutex_lock(&root->fs_info->fs_mutex);
1196 trans = btrfs_start_transaction(root, 1);
1197 btrfs_set_trans_block_group(trans, dir);
1198 if (IS_ERR(trans)) {
1199 err = PTR_ERR(trans);
1203 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1209 inode = btrfs_new_inode(trans, root, objectid,
1210 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1211 if (IS_ERR(inode)) {
1212 err = PTR_ERR(inode);
1216 inode->i_op = &btrfs_dir_inode_operations;
1217 inode->i_fop = &btrfs_dir_file_operations;
1218 btrfs_set_trans_block_group(trans, inode);
1220 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1225 err = btrfs_update_inode(trans, root, inode);
1228 err = btrfs_add_link(trans, dentry, inode);
1231 d_instantiate(dentry, inode);
1233 dir->i_sb->s_dirt = 1;
1234 btrfs_update_inode_block_group(trans, inode);
1235 btrfs_update_inode_block_group(trans, dir);
1238 btrfs_end_transaction(trans, root);
1240 mutex_unlock(&root->fs_info->fs_mutex);
1243 btrfs_btree_balance_dirty(root);
1247 static int btrfs_sync_file(struct file *file,
1248 struct dentry *dentry, int datasync)
1250 struct inode *inode = dentry->d_inode;
1251 struct btrfs_root *root = BTRFS_I(inode)->root;
1253 struct btrfs_trans_handle *trans;
1255 mutex_lock(&root->fs_info->fs_mutex);
1256 trans = btrfs_start_transaction(root, 1);
1261 ret = btrfs_commit_transaction(trans, root);
1262 mutex_unlock(&root->fs_info->fs_mutex);
1264 return ret > 0 ? EIO : ret;
1267 static int btrfs_sync_fs(struct super_block *sb, int wait)
1269 struct btrfs_trans_handle *trans;
1270 struct btrfs_root *root;
1272 root = btrfs_sb(sb);
1276 filemap_flush(root->fs_info->btree_inode->i_mapping);
1279 mutex_lock(&root->fs_info->fs_mutex);
1280 trans = btrfs_start_transaction(root, 1);
1281 ret = btrfs_commit_transaction(trans, root);
1284 printk("btrfs sync_fs\n");
1285 mutex_unlock(&root->fs_info->fs_mutex);
1289 #define BTRFS_GET_BLOCK_NO_CREATE 0
1290 #define BTRFS_GET_BLOCK_CREATE 1
1291 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1293 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1294 struct buffer_head *result, int create)
1299 u64 extent_start = 0;
1301 u64 objectid = inode->i_ino;
1304 struct btrfs_path *path;
1305 struct btrfs_root *root = BTRFS_I(inode)->root;
1306 struct btrfs_file_extent_item *item;
1307 struct btrfs_leaf *leaf;
1308 struct btrfs_disk_key *found_key;
1309 struct btrfs_trans_handle *trans = NULL;
1311 path = btrfs_alloc_path();
1313 btrfs_init_path(path);
1314 if (create & BTRFS_GET_BLOCK_CREATE) {
1316 /* this almost but not quite works */
1317 trans = btrfs_start_transaction(root, 1);
1322 ret = drop_extents(trans, root, inode,
1323 iblock << inode->i_blkbits,
1324 (iblock + 1) << inode->i_blkbits,
1329 ret = btrfs_lookup_file_extent(NULL, root, path,
1331 iblock << inode->i_blkbits, 0);
1338 if (path->slots[0] == 0) {
1339 btrfs_release_path(root, path);
1345 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1346 struct btrfs_file_extent_item);
1347 leaf = btrfs_buffer_leaf(path->nodes[0]);
1348 blocknr = btrfs_file_extent_disk_blocknr(item);
1349 blocknr += btrfs_file_extent_offset(item);
1351 /* are we inside the extent that was found? */
1352 found_key = &leaf->items[path->slots[0]].key;
1353 found_type = btrfs_disk_key_type(found_key);
1354 if (btrfs_disk_key_objectid(found_key) != objectid ||
1355 found_type != BTRFS_EXTENT_DATA_KEY) {
1360 found_type = btrfs_file_extent_type(item);
1361 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1362 if (found_type == BTRFS_FILE_EXTENT_REG) {
1363 extent_start = extent_start >> inode->i_blkbits;
1364 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1366 if (btrfs_file_extent_disk_blocknr(item) == 0)
1368 if (iblock >= extent_start && iblock < extent_end) {
1369 btrfs_map_bh_to_logical(root, result, blocknr +
1370 iblock - extent_start);
1373 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1378 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1382 size = btrfs_file_extent_inline_len(leaf->items +
1384 extent_end = (extent_start + size) >> inode->i_blkbits;
1385 extent_start >>= inode->i_blkbits;
1386 if (iblock < extent_start || iblock > extent_end) {
1389 ptr = btrfs_file_extent_inline_start(item);
1390 map = kmap(result->b_page);
1391 memcpy(map, ptr, size);
1392 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1393 flush_dcache_page(result->b_page);
1394 kunmap(result->b_page);
1395 set_buffer_uptodate(result);
1396 SetPageChecked(result->b_page);
1397 btrfs_map_bh_to_logical(root, result, 0);
1400 if (create & BTRFS_GET_BLOCK_CREATE) {
1401 struct btrfs_key ins;
1402 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1403 1, alloc_hint, (u64)-1,
1406 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1407 iblock << inode->i_blkbits,
1408 ins.objectid, ins.offset,
1411 SetPageChecked(result->b_page);
1412 btrfs_map_bh_to_logical(root, result, ins.objectid);
1416 err = btrfs_end_transaction(trans, root);
1417 btrfs_free_path(path);
1421 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1422 struct buffer_head *result, int create)
1425 struct btrfs_root *root = BTRFS_I(inode)->root;
1426 mutex_lock(&root->fs_info->fs_mutex);
1427 err = btrfs_get_block_lock(inode, iblock, result, create);
1428 mutex_unlock(&root->fs_info->fs_mutex);
1432 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1433 struct buffer_head *result, int create)
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
1436 mutex_lock(&root->fs_info->fs_mutex);
1437 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1438 mutex_unlock(&root->fs_info->fs_mutex);
1442 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1444 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1447 static int btrfs_prepare_write(struct file *file, struct page *page,
1448 unsigned from, unsigned to)
1450 return block_prepare_write(page, from, to, btrfs_get_block);
1453 static void btrfs_write_super(struct super_block *sb)
1455 btrfs_sync_fs(sb, 1);
1458 static int btrfs_readpage(struct file *file, struct page *page)
1460 return mpage_readpage(page, btrfs_get_block);
1464 * While block_write_full_page is writing back the dirty buffers under
1465 * the page lock, whoever dirtied the buffers may decide to clean them
1466 * again at any time. We handle that by only looking at the buffer
1467 * state inside lock_buffer().
1469 * If block_write_full_page() is called for regular writeback
1470 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1471 * locked buffer. This only can happen if someone has written the buffer
1472 * directly, with submit_bh(). At the address_space level PageWriteback
1473 * prevents this contention from occurring.
1475 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1476 struct writeback_control *wbc)
1480 sector_t last_block;
1481 struct buffer_head *bh, *head;
1482 const unsigned blocksize = 1 << inode->i_blkbits;
1483 int nr_underway = 0;
1485 BUG_ON(!PageLocked(page));
1487 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1489 if (!page_has_buffers(page)) {
1490 create_empty_buffers(page, blocksize,
1491 (1 << BH_Dirty)|(1 << BH_Uptodate));
1495 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1496 * here, and the (potentially unmapped) buffers may become dirty at
1497 * any time. If a buffer becomes dirty here after we've inspected it
1498 * then we just miss that fact, and the page stays dirty.
1500 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1501 * handle that here by just cleaning them.
1504 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1505 head = page_buffers(page);
1509 * Get all the dirty buffers mapped to disk addresses and
1510 * handle any aliases from the underlying blockdev's mapping.
1513 if (block > last_block) {
1515 * mapped buffers outside i_size will occur, because
1516 * this page can be outside i_size when there is a
1517 * truncate in progress.
1520 * The buffer was zeroed by block_write_full_page()
1522 clear_buffer_dirty(bh);
1523 set_buffer_uptodate(bh);
1524 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1525 WARN_ON(bh->b_size != blocksize);
1526 err = btrfs_get_block(inode, block, bh, 0);
1528 printk("writepage going to recovery err %d\n", err);
1531 if (buffer_new(bh)) {
1532 /* blockdev mappings never come here */
1533 clear_buffer_new(bh);
1536 bh = bh->b_this_page;
1538 } while (bh != head);
1541 if (!buffer_mapped(bh))
1544 * If it's a fully non-blocking write attempt and we cannot
1545 * lock the buffer then redirty the page. Note that this can
1546 * potentially cause a busy-wait loop from pdflush and kswapd
1547 * activity, but those code paths have their own higher-level
1550 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1552 } else if (test_set_buffer_locked(bh)) {
1553 redirty_page_for_writepage(wbc, page);
1556 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1557 mark_buffer_async_write(bh);
1561 } while ((bh = bh->b_this_page) != head);
1564 * The page and its buffers are protected by PageWriteback(), so we can
1565 * drop the bh refcounts early.
1567 BUG_ON(PageWriteback(page));
1568 set_page_writeback(page);
1571 struct buffer_head *next = bh->b_this_page;
1572 if (buffer_async_write(bh)) {
1573 submit_bh(WRITE, bh);
1577 } while (bh != head);
1582 if (nr_underway == 0) {
1584 * The page was marked dirty, but the buffers were
1585 * clean. Someone wrote them back by hand with
1586 * ll_rw_block/submit_bh. A rare case.
1590 if (!buffer_uptodate(bh)) {
1594 bh = bh->b_this_page;
1595 } while (bh != head);
1597 SetPageUptodate(page);
1598 end_page_writeback(page);
1604 * ENOSPC, or some other error. We may already have added some
1605 * blocks to the file, so we need to write these out to avoid
1606 * exposing stale data.
1607 * The page is currently locked and not marked for writeback
1610 /* Recovery: lock and submit the mapped buffers */
1612 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1614 mark_buffer_async_write(bh);
1617 * The buffer may have been set dirty during
1618 * attachment to a dirty page.
1620 clear_buffer_dirty(bh);
1622 } while ((bh = bh->b_this_page) != head);
1624 BUG_ON(PageWriteback(page));
1625 set_page_writeback(page);
1627 struct buffer_head *next = bh->b_this_page;
1628 if (buffer_async_write(bh)) {
1629 clear_buffer_dirty(bh);
1630 submit_bh(WRITE, bh);
1634 } while (bh != head);
1640 * The generic ->writepage function for buffer-backed address_spaces
1642 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1644 struct inode * const inode = page->mapping->host;
1645 loff_t i_size = i_size_read(inode);
1646 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1650 /* Is the page fully inside i_size? */
1651 if (page->index < end_index)
1652 return __btrfs_write_full_page(inode, page, wbc);
1654 /* Is the page fully outside i_size? (truncate in progress) */
1655 offset = i_size & (PAGE_CACHE_SIZE-1);
1656 if (page->index >= end_index+1 || !offset) {
1658 * The page may have dirty, unmapped buffers. For example,
1659 * they may have been added in ext3_writepage(). Make them
1660 * freeable here, so the page does not leak.
1662 block_invalidatepage(page, 0);
1664 return 0; /* don't care */
1668 * The page straddles i_size. It must be zeroed out on each and every
1669 * writepage invokation because it may be mmapped. "A file is mapped
1670 * in multiples of the page size. For a file that is not a multiple of
1671 * the page size, the remaining memory is zeroed when mapped, and
1672 * writes to that region are not written out to the file."
1674 kaddr = kmap_atomic(page, KM_USER0);
1675 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1676 flush_dcache_page(page);
1677 kunmap_atomic(kaddr, KM_USER0);
1678 return __btrfs_write_full_page(inode, page, wbc);
1681 static void btrfs_truncate(struct inode *inode)
1683 struct btrfs_root *root = BTRFS_I(inode)->root;
1685 struct btrfs_trans_handle *trans;
1687 if (!S_ISREG(inode->i_mode))
1689 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1692 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1694 mutex_lock(&root->fs_info->fs_mutex);
1695 trans = btrfs_start_transaction(root, 1);
1696 btrfs_set_trans_block_group(trans, inode);
1698 /* FIXME, add redo link to tree so we don't leak on crash */
1699 ret = btrfs_truncate_in_trans(trans, root, inode);
1701 btrfs_update_inode(trans, root, inode);
1702 ret = btrfs_end_transaction(trans, root);
1704 mutex_unlock(&root->fs_info->fs_mutex);
1705 btrfs_btree_balance_dirty(root);
1708 static int btrfs_commit_write(struct file *file, struct page *page,
1709 unsigned from, unsigned to)
1711 struct inode *inode = page->mapping->host;
1712 struct buffer_head *bh;
1713 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1715 SetPageUptodate(page);
1716 bh = page_buffers(page);
1717 set_buffer_uptodate(bh);
1718 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1719 set_page_dirty(page);
1721 if (pos > inode->i_size) {
1722 i_size_write(inode, pos);
1723 mark_inode_dirty(inode);
1728 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1729 struct page **prepared_pages,
1730 const char __user * buf)
1732 long page_fault = 0;
1734 int offset = pos & (PAGE_CACHE_SIZE - 1);
1736 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1737 size_t count = min_t(size_t,
1738 PAGE_CACHE_SIZE - offset, write_bytes);
1739 struct page *page = prepared_pages[i];
1740 fault_in_pages_readable(buf, count);
1742 /* Copy data from userspace to the current page */
1744 page_fault = __copy_from_user(page_address(page) + offset,
1746 /* Flush processor's dcache for this page */
1747 flush_dcache_page(page);
1750 write_bytes -= count;
1755 return page_fault ? -EFAULT : 0;
1758 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1761 for (i = 0; i < num_pages; i++) {
1764 unlock_page(pages[i]);
1765 mark_page_accessed(pages[i]);
1766 page_cache_release(pages[i]);
1769 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1770 struct btrfs_root *root,
1772 struct page **pages,
1782 struct inode *inode = file->f_path.dentry->d_inode;
1783 struct buffer_head *bh;
1784 struct btrfs_file_extent_item *ei;
1786 for (i = 0; i < num_pages; i++) {
1787 offset = pos & (PAGE_CACHE_SIZE -1);
1788 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1789 /* FIXME, one block at a time */
1791 mutex_lock(&root->fs_info->fs_mutex);
1792 trans = btrfs_start_transaction(root, 1);
1793 btrfs_set_trans_block_group(trans, inode);
1795 bh = page_buffers(pages[i]);
1796 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
1797 struct btrfs_key key;
1798 struct btrfs_path *path;
1802 path = btrfs_alloc_path();
1804 key.objectid = inode->i_ino;
1805 key.offset = pages[i]->index << PAGE_CACHE_SHIFT;
1807 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
1808 BUG_ON(write_bytes >= PAGE_CACHE_SIZE);
1810 btrfs_file_extent_calc_inline_size(write_bytes);
1811 ret = btrfs_insert_empty_item(trans, root, path, &key,
1814 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1815 path->slots[0], struct btrfs_file_extent_item);
1816 btrfs_set_file_extent_generation(ei, trans->transid);
1817 btrfs_set_file_extent_type(ei,
1818 BTRFS_FILE_EXTENT_INLINE);
1819 ptr = btrfs_file_extent_inline_start(ei);
1820 btrfs_memcpy(root, path->nodes[0]->b_data,
1821 ptr, bh->b_data, offset + write_bytes);
1822 mark_buffer_dirty(path->nodes[0]);
1823 btrfs_free_path(path);
1824 } else if (buffer_mapped(bh)) {
1825 btrfs_csum_file_block(trans, root, inode->i_ino,
1826 pages[i]->index << PAGE_CACHE_SHIFT,
1827 kmap(pages[i]), PAGE_CACHE_SIZE);
1830 SetPageChecked(pages[i]);
1831 // btrfs_update_inode_block_group(trans, inode);
1832 ret = btrfs_end_transaction(trans, root);
1834 mutex_unlock(&root->fs_info->fs_mutex);
1836 ret = btrfs_commit_write(file, pages[i], offset,
1837 offset + this_write);
1843 WARN_ON(this_write > write_bytes);
1844 write_bytes -= this_write;
1850 static int drop_extents(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 struct inode *inode,
1853 u64 start, u64 end, u64 *hint_block)
1856 struct btrfs_key key;
1857 struct btrfs_leaf *leaf;
1859 struct btrfs_file_extent_item *extent;
1862 struct btrfs_file_extent_item old;
1863 struct btrfs_path *path;
1864 u64 search_start = start;
1870 path = btrfs_alloc_path();
1874 btrfs_release_path(root, path);
1875 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1880 if (path->slots[0] == 0) {
1891 leaf = btrfs_buffer_leaf(path->nodes[0]);
1892 slot = path->slots[0];
1893 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1894 if (key.offset >= end || key.objectid != inode->i_ino) {
1898 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) {
1902 extent = btrfs_item_ptr(leaf, slot,
1903 struct btrfs_file_extent_item);
1904 found_type = btrfs_file_extent_type(extent);
1905 if (found_type == BTRFS_FILE_EXTENT_REG) {
1906 extent_end = key.offset +
1907 (btrfs_file_extent_num_blocks(extent) <<
1910 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1912 extent_end = key.offset +
1913 btrfs_file_extent_inline_len(leaf->items + slot);
1916 if (!found_extent && !found_inline) {
1921 if (search_start >= extent_end) {
1927 u64 mask = root->blocksize - 1;
1928 search_start = (extent_end + mask) & ~mask;
1930 search_start = extent_end;
1932 if (end < extent_end && end >= key.offset) {
1935 btrfs_file_extent_disk_blocknr(extent);
1936 u64 disk_num_blocks =
1937 btrfs_file_extent_disk_num_blocks(extent);
1938 memcpy(&old, extent, sizeof(old));
1939 if (disk_blocknr != 0) {
1940 ret = btrfs_inc_extent_ref(trans, root,
1941 disk_blocknr, disk_num_blocks);
1945 WARN_ON(found_inline);
1949 if (start > key.offset) {
1952 /* truncate existing extent */
1954 WARN_ON(start & (root->blocksize - 1));
1956 new_num = (start - key.offset) >>
1958 old_num = btrfs_file_extent_num_blocks(extent);
1960 btrfs_file_extent_disk_blocknr(extent);
1961 if (btrfs_file_extent_disk_blocknr(extent)) {
1963 (old_num - new_num) << 3;
1965 btrfs_set_file_extent_num_blocks(extent,
1967 mark_buffer_dirty(path->nodes[0]);
1973 u64 disk_blocknr = 0;
1974 u64 disk_num_blocks = 0;
1975 u64 extent_num_blocks = 0;
1978 btrfs_file_extent_disk_blocknr(extent);
1980 btrfs_file_extent_disk_num_blocks(extent);
1982 btrfs_file_extent_num_blocks(extent);
1984 btrfs_file_extent_disk_blocknr(extent);
1986 ret = btrfs_del_item(trans, root, path);
1988 btrfs_release_path(root, path);
1990 if (found_extent && disk_blocknr != 0) {
1991 inode->i_blocks -= extent_num_blocks << 3;
1992 ret = btrfs_free_extent(trans, root,
1994 disk_num_blocks, 0);
1998 if (!bookend && search_start >= end) {
2005 if (bookend && found_extent) {
2006 /* create bookend */
2007 struct btrfs_key ins;
2008 ins.objectid = inode->i_ino;
2011 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
2013 btrfs_release_path(root, path);
2014 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2017 extent = btrfs_item_ptr(
2018 btrfs_buffer_leaf(path->nodes[0]),
2020 struct btrfs_file_extent_item);
2021 btrfs_set_file_extent_disk_blocknr(extent,
2022 btrfs_file_extent_disk_blocknr(&old));
2023 btrfs_set_file_extent_disk_num_blocks(extent,
2024 btrfs_file_extent_disk_num_blocks(&old));
2026 btrfs_set_file_extent_offset(extent,
2027 btrfs_file_extent_offset(&old) +
2028 ((end - key.offset) >> inode->i_blkbits));
2029 WARN_ON(btrfs_file_extent_num_blocks(&old) <
2030 (extent_end - end) >> inode->i_blkbits);
2031 btrfs_set_file_extent_num_blocks(extent,
2032 (extent_end - end) >> inode->i_blkbits);
2034 btrfs_set_file_extent_type(extent,
2035 BTRFS_FILE_EXTENT_REG);
2036 btrfs_set_file_extent_generation(extent,
2037 btrfs_file_extent_generation(&old));
2038 btrfs_mark_buffer_dirty(path->nodes[0]);
2039 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
2041 btrfs_file_extent_num_blocks(extent) << 3;
2048 btrfs_free_path(path);
2052 static int prepare_pages(struct btrfs_root *root,
2054 struct page **pages,
2057 unsigned long first_index,
2058 unsigned long last_index,
2060 u64 alloc_extent_start)
2063 unsigned long index = pos >> PAGE_CACHE_SHIFT;
2064 struct inode *inode = file->f_path.dentry->d_inode;
2068 struct buffer_head *bh;
2069 struct buffer_head *head;
2070 loff_t isize = i_size_read(inode);
2072 memset(pages, 0, num_pages * sizeof(struct page *));
2074 for (i = 0; i < num_pages; i++) {
2075 pages[i] = grab_cache_page(inode->i_mapping, index + i);
2078 goto failed_release;
2080 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
2081 wait_on_page_writeback(pages[i]);
2082 offset = pos & (PAGE_CACHE_SIZE -1);
2083 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
2084 if (!page_has_buffers(pages[i])) {
2085 create_empty_buffers(pages[i],
2086 root->fs_info->sb->s_blocksize,
2087 (1 << BH_Uptodate));
2089 head = page_buffers(pages[i]);
2092 err = btrfs_map_bh_to_logical(root, bh,
2093 alloc_extent_start);
2096 goto failed_truncate;
2097 bh = bh->b_this_page;
2098 if (alloc_extent_start)
2099 alloc_extent_start++;
2100 } while (bh != head);
2102 WARN_ON(this_write > write_bytes);
2103 write_bytes -= this_write;
2108 btrfs_drop_pages(pages, num_pages);
2112 btrfs_drop_pages(pages, num_pages);
2114 vmtruncate(inode, isize);
2118 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
2119 size_t count, loff_t *ppos)
2122 size_t num_written = 0;
2125 struct inode *inode = file->f_path.dentry->d_inode;
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2127 struct page *pages[8];
2128 struct page *pinned[2];
2129 unsigned long first_index;
2130 unsigned long last_index;
2133 u64 alloc_extent_start;
2135 struct btrfs_trans_handle *trans;
2136 struct btrfs_key ins;
2139 if (file->f_flags & O_DIRECT)
2142 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
2143 current->backing_dev_info = inode->i_mapping->backing_dev_info;
2144 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
2149 err = remove_suid(file->f_path.dentry);
2152 file_update_time(file);
2154 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
2155 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
2158 mutex_lock(&inode->i_mutex);
2159 first_index = pos >> PAGE_CACHE_SHIFT;
2160 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
2162 if ((pos & (PAGE_CACHE_SIZE - 1))) {
2163 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
2164 if (!PageUptodate(pinned[0])) {
2165 ret = mpage_readpage(pinned[0], btrfs_get_block);
2167 wait_on_page_locked(pinned[0]);
2169 unlock_page(pinned[0]);
2172 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
2173 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
2174 if (!PageUptodate(pinned[1])) {
2175 ret = mpage_readpage(pinned[1], btrfs_get_block);
2177 wait_on_page_locked(pinned[1]);
2179 unlock_page(pinned[1]);
2183 mutex_lock(&root->fs_info->fs_mutex);
2184 trans = btrfs_start_transaction(root, 1);
2187 mutex_unlock(&root->fs_info->fs_mutex);
2190 btrfs_set_trans_block_group(trans, inode);
2191 /* FIXME blocksize != 4096 */
2192 inode->i_blocks += num_blocks << 3;
2194 if (start_pos < inode->i_size) {
2195 /* FIXME blocksize != pagesize */
2196 ret = drop_extents(trans, root, inode,
2198 (pos + count + root->blocksize -1) &
2199 ~((u64)root->blocksize - 1), &hint_block);
2202 if (inode->i_size < start_pos) {
2203 u64 last_pos_in_file;
2205 u64 mask = root->blocksize - 1;
2206 last_pos_in_file = (inode->i_size + mask) & ~mask;
2207 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
2208 hole_size >>= inode->i_blkbits;
2209 if (last_pos_in_file < start_pos) {
2210 ret = btrfs_insert_file_extent(trans, root,
2217 if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size ||
2218 pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
2219 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
2220 num_blocks, hint_block, (u64)-1,
2223 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
2224 start_pos, ins.objectid, ins.offset,
2232 alloc_extent_start = ins.objectid;
2233 // btrfs_update_inode_block_group(trans, inode);
2234 ret = btrfs_end_transaction(trans, root);
2235 mutex_unlock(&root->fs_info->fs_mutex);
2238 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
2239 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
2240 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
2243 memset(pages, 0, sizeof(pages));
2244 ret = prepare_pages(root, file, pages, num_pages,
2245 pos, first_index, last_index,
2246 write_bytes, alloc_extent_start);
2249 /* FIXME blocks != pagesize */
2250 if (alloc_extent_start)
2251 alloc_extent_start += num_pages;
2252 ret = btrfs_copy_from_user(pos, num_pages,
2253 write_bytes, pages, buf);
2256 ret = dirty_and_release_pages(NULL, root, file, pages,
2257 num_pages, pos, write_bytes);
2259 btrfs_drop_pages(pages, num_pages);
2262 count -= write_bytes;
2264 num_written += write_bytes;
2266 balance_dirty_pages_ratelimited(inode->i_mapping);
2267 btrfs_btree_balance_dirty(root);
2271 mutex_unlock(&inode->i_mutex);
2274 page_cache_release(pinned[0]);
2276 page_cache_release(pinned[1]);
2278 current->backing_dev_info = NULL;
2279 mark_inode_dirty(inode);
2280 return num_written ? num_written : err;
2283 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
2284 unsigned long offset, unsigned long size)
2287 unsigned long left, count = desc->count;
2288 struct inode *inode = page->mapping->host;
2293 if (!PageChecked(page)) {
2294 /* FIXME, do it per block */
2295 struct btrfs_root *root = BTRFS_I(inode)->root;
2297 struct buffer_head *bh;
2299 if (page_has_buffers(page)) {
2300 bh = page_buffers(page);
2301 if (!buffer_mapped(bh)) {
2302 SetPageChecked(page);
2307 ret = btrfs_csum_verify_file_block(root,
2308 page->mapping->host->i_ino,
2309 page->index << PAGE_CACHE_SHIFT,
2310 kmap(page), PAGE_CACHE_SIZE);
2312 if (ret != -ENOENT) {
2313 printk("failed to verify ino %lu page %lu ret %d\n",
2314 page->mapping->host->i_ino,
2316 memset(page_address(page), 1, PAGE_CACHE_SIZE);
2317 flush_dcache_page(page);
2320 SetPageChecked(page);
2325 * Faults on the destination of a read are common, so do it before
2328 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
2329 kaddr = kmap_atomic(page, KM_USER0);
2330 left = __copy_to_user_inatomic(desc->arg.buf,
2331 kaddr + offset, size);
2332 kunmap_atomic(kaddr, KM_USER0);
2337 /* Do it the slow way */
2339 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
2344 desc->error = -EFAULT;
2347 desc->count = count - size;
2348 desc->written += size;
2349 desc->arg.buf += size;
2354 * btrfs_file_aio_read - filesystem read routine
2355 * @iocb: kernel I/O control block
2356 * @iov: io vector request
2357 * @nr_segs: number of segments in the iovec
2358 * @pos: current file position
2360 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2361 unsigned long nr_segs, loff_t pos)
2363 struct file *filp = iocb->ki_filp;
2367 loff_t *ppos = &iocb->ki_pos;
2370 for (seg = 0; seg < nr_segs; seg++) {
2371 const struct iovec *iv = &iov[seg];
2374 * If any segment has a negative length, or the cumulative
2375 * length ever wraps negative then return -EINVAL.
2377 count += iv->iov_len;
2378 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
2380 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
2385 count -= iv->iov_len; /* This segment is no good */
2390 for (seg = 0; seg < nr_segs; seg++) {
2391 read_descriptor_t desc;
2394 desc.arg.buf = iov[seg].iov_base;
2395 desc.count = iov[seg].iov_len;
2396 if (desc.count == 0)
2399 do_generic_file_read(filp, ppos, &desc,
2401 retval += desc.written;
2403 retval = retval ?: desc.error;
2411 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2413 struct btrfs_trans_handle *trans;
2414 struct btrfs_key key;
2415 struct btrfs_root_item root_item;
2416 struct btrfs_inode_item *inode_item;
2417 struct buffer_head *subvol;
2418 struct btrfs_leaf *leaf;
2419 struct btrfs_root *new_root;
2420 struct inode *inode;
2424 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2426 mutex_lock(&root->fs_info->fs_mutex);
2427 trans = btrfs_start_transaction(root, 1);
2430 subvol = btrfs_alloc_free_block(trans, root, 0);
2433 leaf = btrfs_buffer_leaf(subvol);
2434 btrfs_set_header_nritems(&leaf->header, 0);
2435 btrfs_set_header_level(&leaf->header, 0);
2436 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2437 btrfs_set_header_generation(&leaf->header, trans->transid);
2438 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2439 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2440 sizeof(leaf->header.fsid));
2441 mark_buffer_dirty(subvol);
2443 inode_item = &root_item.inode;
2444 memset(inode_item, 0, sizeof(*inode_item));
2445 btrfs_set_inode_generation(inode_item, 1);
2446 btrfs_set_inode_size(inode_item, 3);
2447 btrfs_set_inode_nlink(inode_item, 1);
2448 btrfs_set_inode_nblocks(inode_item, 1);
2449 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2451 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2452 btrfs_set_root_refs(&root_item, 1);
2456 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2460 btrfs_set_root_dirid(&root_item, new_dirid);
2462 key.objectid = objectid;
2465 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2466 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2471 * insert the directory item
2473 key.offset = (u64)-1;
2474 dir = root->fs_info->sb->s_root->d_inode;
2475 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2476 name, namelen, dir->i_ino, &key,
2480 ret = btrfs_commit_transaction(trans, root);
2483 new_root = btrfs_read_fs_root(root->fs_info, &key);
2486 trans = btrfs_start_transaction(new_root, 1);
2489 inode = btrfs_new_inode(trans, new_root, new_dirid,
2490 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2491 inode->i_op = &btrfs_dir_inode_operations;
2492 inode->i_fop = &btrfs_dir_file_operations;
2494 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2499 ret = btrfs_update_inode(trans, new_root, inode);
2502 ret = btrfs_commit_transaction(trans, new_root);
2507 mutex_unlock(&root->fs_info->fs_mutex);
2508 btrfs_btree_balance_dirty(root);
2512 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2514 struct btrfs_trans_handle *trans;
2515 struct btrfs_key key;
2516 struct btrfs_root_item new_root_item;
2520 if (!root->ref_cows)
2523 mutex_lock(&root->fs_info->fs_mutex);
2524 trans = btrfs_start_transaction(root, 1);
2527 ret = btrfs_update_inode(trans, root, root->inode);
2530 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2534 memcpy(&new_root_item, &root->root_item,
2535 sizeof(new_root_item));
2537 key.objectid = objectid;
2540 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2541 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2543 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2548 * insert the directory item
2550 key.offset = (u64)-1;
2551 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2553 root->fs_info->sb->s_root->d_inode->i_ino,
2554 &key, BTRFS_FT_DIR);
2558 ret = btrfs_inc_root_ref(trans, root);
2561 ret = btrfs_commit_transaction(trans, root);
2563 mutex_unlock(&root->fs_info->fs_mutex);
2564 btrfs_btree_balance_dirty(root);
2568 static int add_disk(struct btrfs_root *root, char *name, int namelen)
2570 struct block_device *bdev;
2571 struct btrfs_path *path;
2572 struct super_block *sb = root->fs_info->sb;
2573 struct btrfs_root *dev_root = root->fs_info->dev_root;
2574 struct btrfs_trans_handle *trans;
2575 struct btrfs_device_item *dev_item;
2576 struct btrfs_key key;
2583 printk("adding disk %s\n", name);
2584 path = btrfs_alloc_path();
2587 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2588 bdev = open_bdev_excl(name, O_RDWR, sb);
2590 ret = PTR_ERR(bdev);
2591 printk("open bdev excl failed ret %d\n", ret);
2594 set_blocksize(bdev, sb->s_blocksize);
2595 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2596 key.objectid = num_blocks;
2597 key.offset = new_blocks;
2599 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2601 mutex_lock(&dev_root->fs_info->fs_mutex);
2602 trans = btrfs_start_transaction(dev_root, 1);
2603 item_size = sizeof(*dev_item) + namelen;
2604 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2605 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2607 printk("insert failed %d\n", ret);
2608 close_bdev_excl(bdev);
2613 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2614 path->slots[0], struct btrfs_device_item);
2615 btrfs_set_device_pathlen(dev_item, namelen);
2616 memcpy(dev_item + 1, name, namelen);
2618 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2619 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2620 btrfs_set_device_id(dev_item, device_id);
2621 mark_buffer_dirty(path->nodes[0]);
2623 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2627 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2628 num_blocks + new_blocks);
2629 i_size_write(root->fs_info->btree_inode,
2630 (num_blocks + new_blocks) <<
2631 root->fs_info->btree_inode->i_blkbits);
2635 ret = btrfs_commit_transaction(trans, dev_root);
2637 mutex_unlock(&root->fs_info->fs_mutex);
2639 btrfs_free_path(path);
2640 btrfs_btree_balance_dirty(root);
2645 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2646 cmd, unsigned long arg)
2648 struct btrfs_root *root = BTRFS_I(inode)->root;
2649 struct btrfs_ioctl_vol_args vol_args;
2651 struct btrfs_dir_item *di;
2653 struct btrfs_path *path;
2657 case BTRFS_IOC_SNAP_CREATE:
2658 if (copy_from_user(&vol_args,
2659 (struct btrfs_ioctl_vol_args __user *)arg,
2662 namelen = strlen(vol_args.name);
2663 if (namelen > BTRFS_VOL_NAME_MAX)
2665 path = btrfs_alloc_path();
2668 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2669 mutex_lock(&root->fs_info->fs_mutex);
2670 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2672 vol_args.name, namelen, 0);
2673 mutex_unlock(&root->fs_info->fs_mutex);
2674 btrfs_free_path(path);
2675 if (di && !IS_ERR(di))
2678 if (root == root->fs_info->tree_root)
2679 ret = create_subvol(root, vol_args.name, namelen);
2681 ret = create_snapshot(root, vol_args.name, namelen);
2684 case BTRFS_IOC_ADD_DISK:
2685 if (copy_from_user(&vol_args,
2686 (struct btrfs_ioctl_vol_args __user *)arg,
2689 namelen = strlen(vol_args.name);
2690 if (namelen > BTRFS_VOL_NAME_MAX)
2692 vol_args.name[namelen] = '\0';
2693 ret = add_disk(root, vol_args.name, namelen);
2701 static struct kmem_cache *btrfs_inode_cachep;
2702 struct kmem_cache *btrfs_trans_handle_cachep;
2703 struct kmem_cache *btrfs_transaction_cachep;
2704 struct kmem_cache *btrfs_bit_radix_cachep;
2705 struct kmem_cache *btrfs_path_cachep;
2708 * Called inside transaction, so use GFP_NOFS
2710 static struct inode *btrfs_alloc_inode(struct super_block *sb)
2712 struct btrfs_inode *ei;
2714 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2717 return &ei->vfs_inode;
2720 static void btrfs_destroy_inode(struct inode *inode)
2722 WARN_ON(!list_empty(&inode->i_dentry));
2723 WARN_ON(inode->i_data.nrpages);
2725 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2728 static void init_once(void * foo, struct kmem_cache * cachep,
2729 unsigned long flags)
2731 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2733 if ((flags & (SLAB_CTOR_CONSTRUCTOR)) ==
2734 SLAB_CTOR_CONSTRUCTOR) {
2735 inode_init_once(&ei->vfs_inode);
2739 static int init_inodecache(void)
2741 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2742 sizeof(struct btrfs_inode),
2743 0, (SLAB_RECLAIM_ACCOUNT|
2746 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2747 sizeof(struct btrfs_trans_handle),
2748 0, (SLAB_RECLAIM_ACCOUNT|
2751 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2752 sizeof(struct btrfs_transaction),
2753 0, (SLAB_RECLAIM_ACCOUNT|
2756 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2757 sizeof(struct btrfs_transaction),
2758 0, (SLAB_RECLAIM_ACCOUNT|
2761 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2763 0, (SLAB_RECLAIM_ACCOUNT|
2765 SLAB_DESTROY_BY_RCU),
2767 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2768 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2773 static void destroy_inodecache(void)
2775 kmem_cache_destroy(btrfs_inode_cachep);
2776 kmem_cache_destroy(btrfs_trans_handle_cachep);
2777 kmem_cache_destroy(btrfs_transaction_cachep);
2778 kmem_cache_destroy(btrfs_bit_radix_cachep);
2779 kmem_cache_destroy(btrfs_path_cachep);
2782 static int btrfs_get_sb(struct file_system_type *fs_type,
2783 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2785 return get_sb_bdev(fs_type, flags, dev_name, data,
2786 btrfs_fill_super, mnt);
2789 static int btrfs_getattr(struct vfsmount *mnt,
2790 struct dentry *dentry, struct kstat *stat)
2792 struct inode *inode = dentry->d_inode;
2793 generic_fillattr(inode, stat);
2794 stat->blksize = 256 * 1024;
2798 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2800 struct btrfs_root *root = btrfs_sb(dentry->d_sb);
2801 struct btrfs_super_block *disk_super = root->fs_info->disk_super;
2803 buf->f_namelen = BTRFS_NAME_LEN;
2804 buf->f_blocks = btrfs_super_total_blocks(disk_super);
2805 buf->f_bfree = buf->f_blocks - btrfs_super_blocks_used(disk_super);
2806 buf->f_bavail = buf->f_bfree;
2807 buf->f_bsize = dentry->d_sb->s_blocksize;
2808 buf->f_type = BTRFS_SUPER_MAGIC;
2812 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2813 struct inode * new_dir,struct dentry *new_dentry)
2815 struct btrfs_trans_handle *trans;
2816 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2817 struct inode *new_inode = new_dentry->d_inode;
2818 struct inode *old_inode = old_dentry->d_inode;
2819 struct timespec ctime = CURRENT_TIME;
2820 struct btrfs_path *path;
2821 struct btrfs_dir_item *di;
2824 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2825 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2828 mutex_lock(&root->fs_info->fs_mutex);
2829 trans = btrfs_start_transaction(root, 1);
2830 btrfs_set_trans_block_group(trans, new_dir);
2831 path = btrfs_alloc_path();
2837 old_dentry->d_inode->i_nlink++;
2838 old_dir->i_ctime = old_dir->i_mtime = ctime;
2839 new_dir->i_ctime = new_dir->i_mtime = ctime;
2840 old_inode->i_ctime = ctime;
2841 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2842 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2844 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2854 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2855 ret = btrfs_del_item(trans, root, path);
2860 btrfs_release_path(root, path);
2862 di = btrfs_lookup_dir_index_item(trans, root, path,
2874 ret = btrfs_del_item(trans, root, path);
2879 btrfs_release_path(root, path);
2881 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2882 old_inode->i_ino, location,
2889 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2894 new_inode->i_ctime = CURRENT_TIME;
2895 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2898 if (S_ISDIR(new_inode->i_mode))
2899 clear_nlink(new_inode);
2901 drop_nlink(new_inode);
2902 btrfs_update_inode(trans, root, new_inode);
2904 ret = btrfs_add_link(trans, new_dentry, old_inode);
2909 btrfs_free_path(path);
2910 btrfs_end_transaction(trans, root);
2911 mutex_unlock(&root->fs_info->fs_mutex);
2915 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2916 const char *symname)
2918 struct btrfs_trans_handle *trans;
2919 struct btrfs_root *root = BTRFS_I(dir)->root;
2920 struct btrfs_path *path;
2921 struct btrfs_key key;
2922 struct inode *inode;
2929 struct btrfs_file_extent_item *ei;
2931 name_len = strlen(symname) + 1;
2932 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2933 return -ENAMETOOLONG;
2934 mutex_lock(&root->fs_info->fs_mutex);
2935 trans = btrfs_start_transaction(root, 1);
2936 btrfs_set_trans_block_group(trans, dir);
2938 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2944 inode = btrfs_new_inode(trans, root, objectid,
2945 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2946 err = PTR_ERR(inode);
2950 btrfs_set_trans_block_group(trans, inode);
2951 err = btrfs_add_nondir(trans, dentry, inode);
2955 inode->i_mapping->a_ops = &btrfs_aops;
2956 inode->i_fop = &btrfs_file_operations;
2957 inode->i_op = &btrfs_file_inode_operations;
2959 dir->i_sb->s_dirt = 1;
2960 btrfs_update_inode_block_group(trans, inode);
2961 btrfs_update_inode_block_group(trans, dir);
2965 path = btrfs_alloc_path();
2967 key.objectid = inode->i_ino;
2970 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2971 datasize = btrfs_file_extent_calc_inline_size(name_len);
2972 err = btrfs_insert_empty_item(trans, root, path, &key,
2975 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2976 path->slots[0], struct btrfs_file_extent_item);
2977 btrfs_set_file_extent_generation(ei, trans->transid);
2978 btrfs_set_file_extent_type(ei,
2979 BTRFS_FILE_EXTENT_INLINE);
2980 ptr = btrfs_file_extent_inline_start(ei);
2981 btrfs_memcpy(root, path->nodes[0]->b_data,
2982 ptr, symname, name_len);
2983 mark_buffer_dirty(path->nodes[0]);
2984 btrfs_free_path(path);
2985 inode->i_op = &btrfs_symlink_inode_operations;
2986 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2987 inode->i_size = name_len - 1;
2988 btrfs_update_inode(trans, root, inode);
2992 btrfs_end_transaction(trans, root);
2993 mutex_unlock(&root->fs_info->fs_mutex);
2996 inode_dec_link_count(inode);
2999 btrfs_btree_balance_dirty(root);
3003 static struct file_system_type btrfs_fs_type = {
3004 .owner = THIS_MODULE,
3006 .get_sb = btrfs_get_sb,
3007 .kill_sb = kill_block_super,
3008 .fs_flags = FS_REQUIRES_DEV,
3011 static struct super_operations btrfs_super_ops = {
3012 .delete_inode = btrfs_delete_inode,
3013 .put_super = btrfs_put_super,
3014 .read_inode = btrfs_read_locked_inode,
3015 .write_super = btrfs_write_super,
3016 .sync_fs = btrfs_sync_fs,
3017 .write_inode = btrfs_write_inode,
3018 .dirty_inode = btrfs_dirty_inode,
3019 .alloc_inode = btrfs_alloc_inode,
3020 .destroy_inode = btrfs_destroy_inode,
3021 .statfs = btrfs_statfs,
3024 static struct inode_operations btrfs_dir_inode_operations = {
3025 .lookup = btrfs_lookup,
3026 .create = btrfs_create,
3027 .unlink = btrfs_unlink,
3029 .mkdir = btrfs_mkdir,
3030 .rmdir = btrfs_rmdir,
3031 .rename = btrfs_rename,
3032 .symlink = btrfs_symlink,
3033 .setattr = btrfs_setattr,
3036 static struct inode_operations btrfs_dir_ro_inode_operations = {
3037 .lookup = btrfs_lookup,
3040 static struct file_operations btrfs_dir_file_operations = {
3041 .llseek = generic_file_llseek,
3042 .read = generic_read_dir,
3043 .readdir = btrfs_readdir,
3044 .ioctl = btrfs_ioctl,
3047 static struct address_space_operations btrfs_aops = {
3048 .readpage = btrfs_readpage,
3049 .writepage = btrfs_writepage,
3050 .sync_page = block_sync_page,
3051 .prepare_write = btrfs_prepare_write,
3052 .commit_write = btrfs_commit_write,
3056 static struct address_space_operations btrfs_symlink_aops = {
3057 .readpage = btrfs_readpage,
3058 .writepage = btrfs_writepage,
3061 static struct inode_operations btrfs_file_inode_operations = {
3062 .truncate = btrfs_truncate,
3063 .getattr = btrfs_getattr,
3064 .setattr = btrfs_setattr,
3067 static struct file_operations btrfs_file_operations = {
3068 .llseek = generic_file_llseek,
3069 .read = do_sync_read,
3070 .aio_read = btrfs_file_aio_read,
3071 .write = btrfs_file_write,
3072 .mmap = generic_file_mmap,
3073 .open = generic_file_open,
3074 .ioctl = btrfs_ioctl,
3075 .fsync = btrfs_sync_file,
3078 static struct inode_operations btrfs_symlink_inode_operations = {
3079 .readlink = generic_readlink,
3080 .follow_link = page_follow_link_light,
3081 .put_link = page_put_link,
3084 static int __init init_btrfs_fs(void)
3087 printk("btrfs loaded!\n");
3088 err = init_inodecache();
3091 return register_filesystem(&btrfs_fs_type);
3092 destroy_inodecache();
3096 static void __exit exit_btrfs_fs(void)
3098 destroy_inodecache();
3099 unregister_filesystem(&btrfs_fs_type);
3100 printk("btrfs unloaded\n");
3103 module_init(init_btrfs_fs)
3104 module_exit(exit_btrfs_fs)
3106 MODULE_LICENSE("GPL");