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>
15 #include <linux/compat.h>
18 #include "transaction.h"
19 #include "btrfs_inode.h"
21 #include "print-tree.h"
23 struct btrfs_iget_args {
25 struct btrfs_root *root;
28 static struct inode_operations btrfs_dir_inode_operations;
29 static struct inode_operations btrfs_symlink_inode_operations;
30 static struct inode_operations btrfs_dir_ro_inode_operations;
31 static struct inode_operations btrfs_file_inode_operations;
32 static struct address_space_operations btrfs_aops;
33 static struct address_space_operations btrfs_symlink_aops;
34 static struct file_operations btrfs_dir_file_operations;
36 static struct kmem_cache *btrfs_inode_cachep;
37 struct kmem_cache *btrfs_trans_handle_cachep;
38 struct kmem_cache *btrfs_transaction_cachep;
39 struct kmem_cache *btrfs_bit_radix_cachep;
40 struct kmem_cache *btrfs_path_cachep;
43 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
44 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
45 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
46 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
47 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
48 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
49 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
50 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
53 void btrfs_read_locked_inode(struct inode *inode)
55 struct btrfs_path *path;
56 struct btrfs_inode_item *inode_item;
57 struct btrfs_root *root = BTRFS_I(inode)->root;
58 struct btrfs_key location;
59 u64 alloc_group_block;
62 path = btrfs_alloc_path();
64 btrfs_init_path(path);
65 mutex_lock(&root->fs_info->fs_mutex);
67 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
68 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
70 btrfs_free_path(path);
73 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
75 struct btrfs_inode_item);
77 inode->i_mode = btrfs_inode_mode(inode_item);
78 inode->i_nlink = btrfs_inode_nlink(inode_item);
79 inode->i_uid = btrfs_inode_uid(inode_item);
80 inode->i_gid = btrfs_inode_gid(inode_item);
81 inode->i_size = btrfs_inode_size(inode_item);
82 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
83 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
84 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
85 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
86 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
87 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
88 inode->i_blocks = btrfs_inode_nblocks(inode_item);
89 inode->i_generation = btrfs_inode_generation(inode_item);
90 alloc_group_block = btrfs_inode_block_group(inode_item);
91 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
94 btrfs_free_path(path);
97 mutex_unlock(&root->fs_info->fs_mutex);
99 switch (inode->i_mode & S_IFMT) {
102 init_special_inode(inode, inode->i_mode,
103 btrfs_inode_rdev(inode_item));
107 inode->i_mapping->a_ops = &btrfs_aops;
108 inode->i_fop = &btrfs_file_operations;
109 inode->i_op = &btrfs_file_inode_operations;
112 inode->i_fop = &btrfs_dir_file_operations;
113 if (root == root->fs_info->tree_root)
114 inode->i_op = &btrfs_dir_ro_inode_operations;
116 inode->i_op = &btrfs_dir_inode_operations;
119 inode->i_op = &btrfs_symlink_inode_operations;
120 inode->i_mapping->a_ops = &btrfs_symlink_aops;
126 btrfs_release_path(root, path);
127 btrfs_free_path(path);
128 mutex_unlock(&root->fs_info->fs_mutex);
129 make_bad_inode(inode);
132 static void fill_inode_item(struct btrfs_inode_item *item,
135 btrfs_set_inode_uid(item, inode->i_uid);
136 btrfs_set_inode_gid(item, inode->i_gid);
137 btrfs_set_inode_size(item, inode->i_size);
138 btrfs_set_inode_mode(item, inode->i_mode);
139 btrfs_set_inode_nlink(item, inode->i_nlink);
140 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
141 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
142 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
143 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
144 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
145 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
146 btrfs_set_inode_nblocks(item, inode->i_blocks);
147 btrfs_set_inode_generation(item, inode->i_generation);
148 btrfs_set_inode_block_group(item,
149 BTRFS_I(inode)->block_group->key.objectid);
152 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
153 struct btrfs_root *root,
156 struct btrfs_inode_item *inode_item;
157 struct btrfs_path *path;
160 path = btrfs_alloc_path();
162 btrfs_init_path(path);
163 ret = btrfs_lookup_inode(trans, root, path,
164 &BTRFS_I(inode)->location, 1);
171 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
173 struct btrfs_inode_item);
175 fill_inode_item(inode_item, inode);
176 btrfs_mark_buffer_dirty(path->nodes[0]);
179 btrfs_release_path(root, path);
180 btrfs_free_path(path);
185 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
186 struct btrfs_root *root,
188 struct dentry *dentry)
190 struct btrfs_path *path;
191 const char *name = dentry->d_name.name;
192 int name_len = dentry->d_name.len;
195 struct btrfs_dir_item *di;
197 path = btrfs_alloc_path();
199 btrfs_init_path(path);
200 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
210 objectid = btrfs_disk_key_objectid(&di->location);
211 ret = btrfs_delete_one_dir_name(trans, root, path, di);
213 btrfs_release_path(root, path);
215 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
216 objectid, name, name_len, -1);
225 ret = btrfs_delete_one_dir_name(trans, root, path, di);
228 dentry->d_inode->i_ctime = dir->i_ctime;
230 btrfs_free_path(path);
232 dir->i_size -= name_len * 2;
233 btrfs_update_inode(trans, root, dir);
234 drop_nlink(dentry->d_inode);
235 btrfs_update_inode(trans, root, dentry->d_inode);
236 dir->i_sb->s_dirt = 1;
241 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
243 struct btrfs_root *root;
244 struct btrfs_trans_handle *trans;
247 root = BTRFS_I(dir)->root;
248 mutex_lock(&root->fs_info->fs_mutex);
249 trans = btrfs_start_transaction(root, 1);
250 btrfs_set_trans_block_group(trans, dir);
251 ret = btrfs_unlink_trans(trans, root, dir, dentry);
252 btrfs_end_transaction(trans, root);
253 mutex_unlock(&root->fs_info->fs_mutex);
254 btrfs_btree_balance_dirty(root);
258 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
260 struct inode *inode = dentry->d_inode;
263 struct btrfs_root *root = BTRFS_I(dir)->root;
264 struct btrfs_path *path;
265 struct btrfs_key key;
266 struct btrfs_trans_handle *trans;
267 struct btrfs_key found_key;
269 struct btrfs_leaf *leaf;
270 char *goodnames = "..";
272 path = btrfs_alloc_path();
274 btrfs_init_path(path);
275 mutex_lock(&root->fs_info->fs_mutex);
276 trans = btrfs_start_transaction(root, 1);
277 btrfs_set_trans_block_group(trans, dir);
278 key.objectid = inode->i_ino;
279 key.offset = (u64)-1;
282 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
288 if (path->slots[0] == 0) {
293 leaf = btrfs_buffer_leaf(path->nodes[0]);
294 btrfs_disk_key_to_cpu(&found_key,
295 &leaf->items[path->slots[0]].key);
296 found_type = btrfs_key_type(&found_key);
297 if (found_key.objectid != inode->i_ino) {
301 if ((found_type != BTRFS_DIR_ITEM_KEY &&
302 found_type != BTRFS_DIR_INDEX_KEY) ||
303 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
304 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
308 ret = btrfs_del_item(trans, root, path);
311 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
313 btrfs_release_path(root, path);
316 btrfs_release_path(root, path);
318 /* now the directory is empty */
319 err = btrfs_unlink_trans(trans, root, dir, dentry);
324 btrfs_release_path(root, path);
325 btrfs_free_path(path);
326 mutex_unlock(&root->fs_info->fs_mutex);
327 ret = btrfs_end_transaction(trans, root);
328 btrfs_btree_balance_dirty(root);
334 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
335 struct btrfs_root *root,
338 struct btrfs_path *path;
343 path = btrfs_alloc_path();
345 btrfs_init_path(path);
346 ret = btrfs_lookup_inode(trans, root, path,
347 &BTRFS_I(inode)->location, -1);
349 ret = btrfs_del_item(trans, root, path);
351 btrfs_free_path(path);
356 * truncates go from a high offset to a low offset. So, walk
357 * from hi to lo in the node and issue readas. Stop when you find
358 * keys from a different objectid
360 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
363 struct btrfs_node *node;
373 node = btrfs_buffer_node(path->nodes[1]);
374 slot = path->slots[1];
377 nritems = btrfs_header_nritems(&node->header);
378 for (i = slot - 1; i >= 0; i--) {
379 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
380 if (item_objectid != objectid)
382 blocknr = btrfs_node_blockptr(node, i);
383 ret = readahead_tree_block(root, blocknr);
390 * this can truncate away extent items, csum items and directory items.
391 * It starts at a high offset and removes keys until it can't find
392 * any higher than i_size.
394 * csum items that cross the new i_size are truncated to the new size
397 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root,
402 struct btrfs_path *path;
403 struct btrfs_key key;
404 struct btrfs_disk_key *found_key;
406 struct btrfs_leaf *leaf;
407 struct btrfs_file_extent_item *fi;
408 u64 extent_start = 0;
409 u64 extent_num_blocks = 0;
414 path = btrfs_alloc_path();
416 /* FIXME, add redo link to tree so we don't leak on crash */
417 key.objectid = inode->i_ino;
418 key.offset = (u64)-1;
421 btrfs_init_path(path);
423 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
428 BUG_ON(path->slots[0] == 0);
431 reada_truncate(root, path, inode->i_ino);
432 leaf = btrfs_buffer_leaf(path->nodes[0]);
433 found_key = &leaf->items[path->slots[0]].key;
434 found_type = btrfs_disk_key_type(found_key);
436 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
438 if (found_type != BTRFS_CSUM_ITEM_KEY &&
439 found_type != BTRFS_DIR_ITEM_KEY &&
440 found_type != BTRFS_DIR_INDEX_KEY &&
441 found_type != BTRFS_EXTENT_DATA_KEY)
444 item_end = btrfs_disk_key_offset(found_key);
445 if (found_type == BTRFS_EXTENT_DATA_KEY) {
446 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
448 struct btrfs_file_extent_item);
449 if (btrfs_file_extent_type(fi) !=
450 BTRFS_FILE_EXTENT_INLINE) {
451 item_end += btrfs_file_extent_num_blocks(fi) <<
455 if (found_type == BTRFS_CSUM_ITEM_KEY) {
456 ret = btrfs_csum_truncate(trans, root, path,
460 if (item_end < inode->i_size) {
462 btrfs_set_key_type(&key, found_type - 1);
467 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
473 /* FIXME, shrink the extent if the ref count is only 1 */
474 if (found_type == BTRFS_EXTENT_DATA_KEY &&
475 btrfs_file_extent_type(fi) !=
476 BTRFS_FILE_EXTENT_INLINE) {
479 u64 orig_num_blocks =
480 btrfs_file_extent_num_blocks(fi);
481 extent_num_blocks = inode->i_size -
482 btrfs_disk_key_offset(found_key) +
484 extent_num_blocks >>= inode->i_blkbits;
485 btrfs_set_file_extent_num_blocks(fi,
487 inode->i_blocks -= (orig_num_blocks -
488 extent_num_blocks) << 3;
489 mark_buffer_dirty(path->nodes[0]);
492 btrfs_file_extent_disk_blocknr(fi);
494 btrfs_file_extent_disk_num_blocks(fi);
495 /* FIXME blocksize != 4096 */
496 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
497 if (extent_start != 0) {
499 inode->i_blocks -= num_dec;
504 ret = btrfs_del_item(trans, root, path);
509 btrfs_release_path(root, path);
511 ret = btrfs_free_extent(trans, root, extent_start,
512 extent_num_blocks, 0);
518 btrfs_release_path(root, path);
519 btrfs_free_path(path);
520 inode->i_sb->s_dirt = 1;
525 * taken from block_truncate_page, but does cow as it zeros out
526 * any bytes left in the last page in the file.
528 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
530 struct inode *inode = mapping->host;
531 unsigned blocksize = 1 << inode->i_blkbits;
532 pgoff_t index = from >> PAGE_CACHE_SHIFT;
533 unsigned offset = from & (PAGE_CACHE_SIZE-1);
537 struct btrfs_root *root = BTRFS_I(inode)->root;
539 struct btrfs_key ins;
540 struct btrfs_trans_handle *trans;
542 if ((offset & (blocksize - 1)) == 0)
546 page = grab_cache_page(mapping, index);
550 if (!PageUptodate(page)) {
551 ret = mpage_readpage(page, btrfs_get_block);
553 if (!PageUptodate(page)) {
558 mutex_lock(&root->fs_info->fs_mutex);
559 trans = btrfs_start_transaction(root, 1);
560 btrfs_set_trans_block_group(trans, inode);
562 ret = btrfs_drop_extents(trans, root, inode,
563 page->index << PAGE_CACHE_SHIFT,
564 (page->index + 1) << PAGE_CACHE_SHIFT,
567 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
568 alloc_hint, (u64)-1, &ins, 1);
570 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
571 page->index << PAGE_CACHE_SHIFT,
574 SetPageChecked(page);
576 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
577 flush_dcache_page(page);
578 btrfs_csum_file_block(trans, root, inode->i_ino,
579 page->index << PAGE_CACHE_SHIFT,
580 kaddr, PAGE_CACHE_SIZE);
582 btrfs_end_transaction(trans, root);
583 mutex_unlock(&root->fs_info->fs_mutex);
585 set_page_dirty(page);
587 page_cache_release(page);
592 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
594 struct inode *inode = dentry->d_inode;
597 err = inode_change_ok(inode, attr);
601 if (S_ISREG(inode->i_mode) &&
602 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
603 struct btrfs_trans_handle *trans;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 u64 mask = root->blocksize - 1;
606 u64 pos = (inode->i_size + mask) & ~mask;
609 if (attr->ia_size <= pos)
612 btrfs_truncate_page(inode->i_mapping, inode->i_size);
614 hole_size = (attr->ia_size - pos + mask) & ~mask;
615 hole_size >>= inode->i_blkbits;
617 mutex_lock(&root->fs_info->fs_mutex);
618 trans = btrfs_start_transaction(root, 1);
619 btrfs_set_trans_block_group(trans, inode);
620 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
621 pos, 0, 0, hole_size);
623 btrfs_end_transaction(trans, root);
624 mutex_unlock(&root->fs_info->fs_mutex);
627 err = inode_setattr(inode, attr);
631 void btrfs_delete_inode(struct inode *inode)
633 struct btrfs_trans_handle *trans;
634 struct btrfs_root *root = BTRFS_I(inode)->root;
637 truncate_inode_pages(&inode->i_data, 0);
638 if (is_bad_inode(inode)) {
642 mutex_lock(&root->fs_info->fs_mutex);
643 trans = btrfs_start_transaction(root, 1);
644 btrfs_set_trans_block_group(trans, inode);
645 ret = btrfs_truncate_in_trans(trans, root, inode);
647 btrfs_free_inode(trans, root, inode);
648 btrfs_end_transaction(trans, root);
649 mutex_unlock(&root->fs_info->fs_mutex);
650 btrfs_btree_balance_dirty(root);
657 * this returns the key found in the dir entry in the location pointer.
658 * If no dir entries were found, location->objectid is 0.
660 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
661 struct btrfs_key *location)
663 const char *name = dentry->d_name.name;
664 int namelen = dentry->d_name.len;
665 struct btrfs_dir_item *di;
666 struct btrfs_path *path;
667 struct btrfs_root *root = BTRFS_I(dir)->root;
670 path = btrfs_alloc_path();
672 btrfs_init_path(path);
673 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
675 if (!di || IS_ERR(di)) {
676 location->objectid = 0;
680 btrfs_disk_key_to_cpu(location, &di->location);
682 btrfs_release_path(root, path);
683 btrfs_free_path(path);
688 * when we hit a tree root in a directory, the btrfs part of the inode
689 * needs to be changed to reflect the root directory of the tree root. This
690 * is kind of like crossing a mount point.
692 static int fixup_tree_root_location(struct btrfs_root *root,
693 struct btrfs_key *location,
694 struct btrfs_root **sub_root)
696 struct btrfs_path *path;
697 struct btrfs_root_item *ri;
699 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
701 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
704 path = btrfs_alloc_path();
706 mutex_lock(&root->fs_info->fs_mutex);
708 *sub_root = btrfs_read_fs_root(root->fs_info, location);
709 if (IS_ERR(*sub_root))
710 return PTR_ERR(*sub_root);
712 ri = &(*sub_root)->root_item;
713 location->objectid = btrfs_root_dirid(ri);
715 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
716 location->offset = 0;
718 btrfs_free_path(path);
719 mutex_unlock(&root->fs_info->fs_mutex);
723 static int btrfs_init_locked_inode(struct inode *inode, void *p)
725 struct btrfs_iget_args *args = p;
726 inode->i_ino = args->ino;
727 BTRFS_I(inode)->root = args->root;
731 static int btrfs_find_actor(struct inode *inode, void *opaque)
733 struct btrfs_iget_args *args = opaque;
734 return (args->ino == inode->i_ino &&
735 args->root == BTRFS_I(inode)->root);
738 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
739 struct btrfs_root *root)
742 struct btrfs_iget_args args;
746 inode = iget5_locked(s, objectid, btrfs_find_actor,
747 btrfs_init_locked_inode,
752 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
753 struct nameidata *nd)
755 struct inode * inode;
756 struct btrfs_inode *bi = BTRFS_I(dir);
757 struct btrfs_root *root = bi->root;
758 struct btrfs_root *sub_root = root;
759 struct btrfs_key location;
762 if (dentry->d_name.len > BTRFS_NAME_LEN)
763 return ERR_PTR(-ENAMETOOLONG);
764 mutex_lock(&root->fs_info->fs_mutex);
765 ret = btrfs_inode_by_name(dir, dentry, &location);
766 mutex_unlock(&root->fs_info->fs_mutex);
770 if (location.objectid) {
771 ret = fixup_tree_root_location(root, &location, &sub_root);
775 return ERR_PTR(-ENOENT);
776 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
779 return ERR_PTR(-EACCES);
780 if (inode->i_state & I_NEW) {
781 /* the inode and parent dir are two different roots */
782 if (sub_root != root) {
784 sub_root->inode = inode;
786 BTRFS_I(inode)->root = sub_root;
787 memcpy(&BTRFS_I(inode)->location, &location,
789 btrfs_read_locked_inode(inode);
790 unlock_new_inode(inode);
793 return d_splice_alias(inode, dentry);
797 * readahead one full node of leaves as long as their keys include
798 * the objectid supplied
800 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
803 struct btrfs_node *node;
813 node = btrfs_buffer_node(path->nodes[1]);
814 slot = path->slots[1];
815 nritems = btrfs_header_nritems(&node->header);
816 for (i = slot + 1; i < nritems; i++) {
817 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
818 if (item_objectid != objectid)
820 blocknr = btrfs_node_blockptr(node, i);
821 ret = readahead_tree_block(root, blocknr);
826 static unsigned char btrfs_filetype_table[] = {
827 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
830 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
832 struct inode *inode = filp->f_path.dentry->d_inode;
833 struct btrfs_root *root = BTRFS_I(inode)->root;
834 struct btrfs_item *item;
835 struct btrfs_dir_item *di;
836 struct btrfs_key key;
837 struct btrfs_path *path;
840 struct btrfs_leaf *leaf;
843 unsigned char d_type;
848 int key_type = BTRFS_DIR_INDEX_KEY;
850 /* FIXME, use a real flag for deciding about the key type */
851 if (root->fs_info->tree_root == root)
852 key_type = BTRFS_DIR_ITEM_KEY;
853 mutex_lock(&root->fs_info->fs_mutex);
854 key.objectid = inode->i_ino;
856 btrfs_set_key_type(&key, key_type);
857 key.offset = filp->f_pos;
858 path = btrfs_alloc_path();
859 btrfs_init_path(path);
860 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
864 reada_leaves(root, path, inode->i_ino);
866 leaf = btrfs_buffer_leaf(path->nodes[0]);
867 nritems = btrfs_header_nritems(&leaf->header);
868 slot = path->slots[0];
869 if (advance || slot >= nritems) {
870 if (slot >= nritems -1) {
871 reada_leaves(root, path, inode->i_ino);
872 ret = btrfs_next_leaf(root, path);
875 leaf = btrfs_buffer_leaf(path->nodes[0]);
876 nritems = btrfs_header_nritems(&leaf->header);
877 slot = path->slots[0];
884 item = leaf->items + slot;
885 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
887 if (btrfs_disk_key_type(&item->key) != key_type)
889 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
891 filp->f_pos = btrfs_disk_key_offset(&item->key);
893 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
895 di_total = btrfs_item_size(leaf->items + slot);
896 while(di_cur < di_total) {
897 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
898 over = filldir(dirent, (const char *)(di + 1),
899 btrfs_dir_name_len(di),
900 btrfs_disk_key_offset(&item->key),
901 btrfs_disk_key_objectid(&di->location),
905 di_len = btrfs_dir_name_len(di) + sizeof(*di);
907 di = (struct btrfs_dir_item *)((char *)di + di_len);
914 btrfs_release_path(root, path);
915 btrfs_free_path(path);
916 mutex_unlock(&root->fs_info->fs_mutex);
920 int btrfs_write_inode(struct inode *inode, int wait)
922 struct btrfs_root *root = BTRFS_I(inode)->root;
923 struct btrfs_trans_handle *trans;
927 mutex_lock(&root->fs_info->fs_mutex);
928 trans = btrfs_start_transaction(root, 1);
929 btrfs_set_trans_block_group(trans, inode);
930 ret = btrfs_commit_transaction(trans, root);
931 mutex_unlock(&root->fs_info->fs_mutex);
937 * This is somewhat expense, updating the tree every time the
938 * inode changes. But, it is most likely to find the inode in cache.
939 * FIXME, needs more benchmarking...there are no reasons other than performance
940 * to keep or drop this code.
942 void btrfs_dirty_inode(struct inode *inode)
944 struct btrfs_root *root = BTRFS_I(inode)->root;
945 struct btrfs_trans_handle *trans;
947 mutex_lock(&root->fs_info->fs_mutex);
948 trans = btrfs_start_transaction(root, 1);
949 btrfs_set_trans_block_group(trans, inode);
950 btrfs_update_inode(trans, root, inode);
951 btrfs_end_transaction(trans, root);
952 mutex_unlock(&root->fs_info->fs_mutex);
953 btrfs_btree_balance_dirty(root);
956 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
957 struct btrfs_root *root,
959 struct btrfs_block_group_cache *group,
963 struct btrfs_inode_item inode_item;
964 struct btrfs_key *location;
968 inode = new_inode(root->fs_info->sb);
970 return ERR_PTR(-ENOMEM);
972 BTRFS_I(inode)->root = root;
977 group = btrfs_find_block_group(root, group, 0, 0, owner);
978 BTRFS_I(inode)->block_group = group;
980 inode->i_uid = current->fsuid;
981 inode->i_gid = current->fsgid;
982 inode->i_mode = mode;
983 inode->i_ino = objectid;
985 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
986 fill_inode_item(&inode_item, inode);
987 location = &BTRFS_I(inode)->location;
988 location->objectid = objectid;
990 location->offset = 0;
991 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
993 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
996 insert_inode_hash(inode);
1000 static inline u8 btrfs_inode_type(struct inode *inode)
1002 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1005 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1006 struct dentry *dentry, struct inode *inode)
1009 struct btrfs_key key;
1010 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1011 key.objectid = inode->i_ino;
1013 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1016 ret = btrfs_insert_dir_item(trans, root,
1017 dentry->d_name.name, dentry->d_name.len,
1018 dentry->d_parent->d_inode->i_ino,
1019 &key, btrfs_inode_type(inode));
1021 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
1022 ret = btrfs_update_inode(trans, root,
1023 dentry->d_parent->d_inode);
1028 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1029 struct dentry *dentry, struct inode *inode)
1031 int err = btrfs_add_link(trans, dentry, inode);
1033 d_instantiate(dentry, inode);
1041 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1042 int mode, struct nameidata *nd)
1044 struct btrfs_trans_handle *trans;
1045 struct btrfs_root *root = BTRFS_I(dir)->root;
1046 struct inode *inode;
1051 mutex_lock(&root->fs_info->fs_mutex);
1052 trans = btrfs_start_transaction(root, 1);
1053 btrfs_set_trans_block_group(trans, dir);
1055 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1061 inode = btrfs_new_inode(trans, root, objectid,
1062 BTRFS_I(dir)->block_group, mode);
1063 err = PTR_ERR(inode);
1067 btrfs_set_trans_block_group(trans, inode);
1068 err = btrfs_add_nondir(trans, dentry, inode);
1072 inode->i_mapping->a_ops = &btrfs_aops;
1073 inode->i_fop = &btrfs_file_operations;
1074 inode->i_op = &btrfs_file_inode_operations;
1076 dir->i_sb->s_dirt = 1;
1077 btrfs_update_inode_block_group(trans, inode);
1078 btrfs_update_inode_block_group(trans, dir);
1080 btrfs_end_transaction(trans, root);
1081 mutex_unlock(&root->fs_info->fs_mutex);
1084 inode_dec_link_count(inode);
1087 btrfs_btree_balance_dirty(root);
1091 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1092 struct dentry *dentry)
1094 struct btrfs_trans_handle *trans;
1095 struct btrfs_root *root = BTRFS_I(dir)->root;
1096 struct inode *inode = old_dentry->d_inode;
1100 if (inode->i_nlink == 0)
1104 mutex_lock(&root->fs_info->fs_mutex);
1105 trans = btrfs_start_transaction(root, 1);
1106 btrfs_set_trans_block_group(trans, dir);
1107 atomic_inc(&inode->i_count);
1108 err = btrfs_add_nondir(trans, dentry, inode);
1111 dir->i_sb->s_dirt = 1;
1112 btrfs_update_inode_block_group(trans, dir);
1113 btrfs_update_inode(trans, root, inode);
1115 btrfs_end_transaction(trans, root);
1116 mutex_unlock(&root->fs_info->fs_mutex);
1119 inode_dec_link_count(inode);
1122 btrfs_btree_balance_dirty(root);
1126 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1127 struct btrfs_root *root,
1128 u64 objectid, u64 dirid)
1132 struct btrfs_key key;
1137 key.objectid = objectid;
1140 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1142 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1143 &key, BTRFS_FT_DIR);
1146 key.objectid = dirid;
1147 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1148 &key, BTRFS_FT_DIR);
1155 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1157 struct inode *inode;
1158 struct btrfs_trans_handle *trans;
1159 struct btrfs_root *root = BTRFS_I(dir)->root;
1161 int drop_on_err = 0;
1164 mutex_lock(&root->fs_info->fs_mutex);
1165 trans = btrfs_start_transaction(root, 1);
1166 btrfs_set_trans_block_group(trans, dir);
1167 if (IS_ERR(trans)) {
1168 err = PTR_ERR(trans);
1172 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1178 inode = btrfs_new_inode(trans, root, objectid,
1179 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1180 if (IS_ERR(inode)) {
1181 err = PTR_ERR(inode);
1185 inode->i_op = &btrfs_dir_inode_operations;
1186 inode->i_fop = &btrfs_dir_file_operations;
1187 btrfs_set_trans_block_group(trans, inode);
1189 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1194 err = btrfs_update_inode(trans, root, inode);
1197 err = btrfs_add_link(trans, dentry, inode);
1200 d_instantiate(dentry, inode);
1202 dir->i_sb->s_dirt = 1;
1203 btrfs_update_inode_block_group(trans, inode);
1204 btrfs_update_inode_block_group(trans, dir);
1207 btrfs_end_transaction(trans, root);
1209 mutex_unlock(&root->fs_info->fs_mutex);
1212 btrfs_btree_balance_dirty(root);
1217 * FIBMAP and others want to pass in a fake buffer head. They need to
1218 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1219 * any packed file data into the fake bh
1221 #define BTRFS_GET_BLOCK_NO_CREATE 0
1222 #define BTRFS_GET_BLOCK_CREATE 1
1223 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1226 * FIXME create==1 doe not work.
1228 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1229 struct buffer_head *result, int create)
1234 u64 extent_start = 0;
1236 u64 objectid = inode->i_ino;
1239 struct btrfs_path *path;
1240 struct btrfs_root *root = BTRFS_I(inode)->root;
1241 struct btrfs_file_extent_item *item;
1242 struct btrfs_leaf *leaf;
1243 struct btrfs_disk_key *found_key;
1244 struct btrfs_trans_handle *trans = NULL;
1246 path = btrfs_alloc_path();
1248 btrfs_init_path(path);
1249 if (create & BTRFS_GET_BLOCK_CREATE) {
1251 /* this almost but not quite works */
1252 trans = btrfs_start_transaction(root, 1);
1257 ret = btrfs_drop_extents(trans, root, inode,
1258 iblock << inode->i_blkbits,
1259 (iblock + 1) << inode->i_blkbits,
1264 ret = btrfs_lookup_file_extent(NULL, root, path,
1266 iblock << inode->i_blkbits, 0);
1273 if (path->slots[0] == 0) {
1274 btrfs_release_path(root, path);
1280 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1281 struct btrfs_file_extent_item);
1282 leaf = btrfs_buffer_leaf(path->nodes[0]);
1283 blocknr = btrfs_file_extent_disk_blocknr(item);
1284 blocknr += btrfs_file_extent_offset(item);
1286 /* are we inside the extent that was found? */
1287 found_key = &leaf->items[path->slots[0]].key;
1288 found_type = btrfs_disk_key_type(found_key);
1289 if (btrfs_disk_key_objectid(found_key) != objectid ||
1290 found_type != BTRFS_EXTENT_DATA_KEY) {
1295 found_type = btrfs_file_extent_type(item);
1296 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1297 if (found_type == BTRFS_FILE_EXTENT_REG) {
1298 extent_start = extent_start >> inode->i_blkbits;
1299 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1301 if (btrfs_file_extent_disk_blocknr(item) == 0)
1303 if (iblock >= extent_start && iblock < extent_end) {
1304 btrfs_map_bh_to_logical(root, result, blocknr +
1305 iblock - extent_start);
1308 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1313 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1317 size = btrfs_file_extent_inline_len(leaf->items +
1319 extent_end = (extent_start + size) >> inode->i_blkbits;
1320 extent_start >>= inode->i_blkbits;
1321 if (iblock < extent_start || iblock > extent_end) {
1324 ptr = btrfs_file_extent_inline_start(item);
1325 map = kmap(result->b_page);
1326 memcpy(map, ptr, size);
1327 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1328 flush_dcache_page(result->b_page);
1329 kunmap(result->b_page);
1330 set_buffer_uptodate(result);
1331 SetPageChecked(result->b_page);
1332 btrfs_map_bh_to_logical(root, result, 0);
1335 if (create & BTRFS_GET_BLOCK_CREATE) {
1336 struct btrfs_key ins;
1337 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1338 1, alloc_hint, (u64)-1,
1341 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1342 iblock << inode->i_blkbits,
1343 ins.objectid, ins.offset,
1346 SetPageChecked(result->b_page);
1347 btrfs_map_bh_to_logical(root, result, ins.objectid);
1351 err = btrfs_end_transaction(trans, root);
1352 btrfs_free_path(path);
1356 int btrfs_get_block(struct inode *inode, sector_t iblock,
1357 struct buffer_head *result, int create)
1360 struct btrfs_root *root = BTRFS_I(inode)->root;
1361 mutex_lock(&root->fs_info->fs_mutex);
1362 err = btrfs_get_block_lock(inode, iblock, result, create);
1363 mutex_unlock(&root->fs_info->fs_mutex);
1367 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1368 struct buffer_head *result, int create)
1370 struct btrfs_root *root = BTRFS_I(inode)->root;
1371 mutex_lock(&root->fs_info->fs_mutex);
1372 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1373 mutex_unlock(&root->fs_info->fs_mutex);
1377 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1379 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1382 static int btrfs_prepare_write(struct file *file, struct page *page,
1383 unsigned from, unsigned to)
1385 return block_prepare_write(page, from, to, btrfs_get_block);
1388 static int btrfs_readpage(struct file *file, struct page *page)
1390 return mpage_readpage(page, btrfs_get_block);
1394 * Aside from a tiny bit of packed file data handling, this is the
1395 * same as the generic code.
1397 * While block_write_full_page is writing back the dirty buffers under
1398 * the page lock, whoever dirtied the buffers may decide to clean them
1399 * again at any time. We handle that by only looking at the buffer
1400 * state inside lock_buffer().
1402 * If block_write_full_page() is called for regular writeback
1403 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1404 * locked buffer. This only can happen if someone has written the buffer
1405 * directly, with submit_bh(). At the address_space level PageWriteback
1406 * prevents this contention from occurring.
1408 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1409 struct writeback_control *wbc)
1413 sector_t last_block;
1414 struct buffer_head *bh, *head;
1415 const unsigned blocksize = 1 << inode->i_blkbits;
1416 int nr_underway = 0;
1418 BUG_ON(!PageLocked(page));
1420 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1422 if (!page_has_buffers(page)) {
1423 create_empty_buffers(page, blocksize,
1424 (1 << BH_Dirty)|(1 << BH_Uptodate));
1428 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1429 * here, and the (potentially unmapped) buffers may become dirty at
1430 * any time. If a buffer becomes dirty here after we've inspected it
1431 * then we just miss that fact, and the page stays dirty.
1433 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1434 * handle that here by just cleaning them.
1437 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1438 head = page_buffers(page);
1442 * Get all the dirty buffers mapped to disk addresses and
1443 * handle any aliases from the underlying blockdev's mapping.
1446 if (block > last_block) {
1448 * mapped buffers outside i_size will occur, because
1449 * this page can be outside i_size when there is a
1450 * truncate in progress.
1453 * The buffer was zeroed by block_write_full_page()
1455 clear_buffer_dirty(bh);
1456 set_buffer_uptodate(bh);
1457 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1458 WARN_ON(bh->b_size != blocksize);
1459 err = btrfs_get_block(inode, block, bh, 0);
1463 if (buffer_new(bh)) {
1464 /* blockdev mappings never come here */
1465 clear_buffer_new(bh);
1468 bh = bh->b_this_page;
1470 } while (bh != head);
1473 if (!buffer_mapped(bh))
1476 * If it's a fully non-blocking write attempt and we cannot
1477 * lock the buffer then redirty the page. Note that this can
1478 * potentially cause a busy-wait loop from pdflush and kswapd
1479 * activity, but those code paths have their own higher-level
1482 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1484 } else if (test_set_buffer_locked(bh)) {
1485 redirty_page_for_writepage(wbc, page);
1488 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1489 mark_buffer_async_write(bh);
1493 } while ((bh = bh->b_this_page) != head);
1496 * The page and its buffers are protected by PageWriteback(), so we can
1497 * drop the bh refcounts early.
1499 BUG_ON(PageWriteback(page));
1500 set_page_writeback(page);
1503 struct buffer_head *next = bh->b_this_page;
1504 if (buffer_async_write(bh)) {
1505 submit_bh(WRITE, bh);
1509 } while (bh != head);
1514 if (nr_underway == 0) {
1516 * The page was marked dirty, but the buffers were
1517 * clean. Someone wrote them back by hand with
1518 * ll_rw_block/submit_bh. A rare case.
1522 if (!buffer_uptodate(bh)) {
1526 bh = bh->b_this_page;
1527 } while (bh != head);
1529 SetPageUptodate(page);
1530 end_page_writeback(page);
1536 * ENOSPC, or some other error. We may already have added some
1537 * blocks to the file, so we need to write these out to avoid
1538 * exposing stale data.
1539 * The page is currently locked and not marked for writeback
1542 /* Recovery: lock and submit the mapped buffers */
1544 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1546 mark_buffer_async_write(bh);
1549 * The buffer may have been set dirty during
1550 * attachment to a dirty page.
1552 clear_buffer_dirty(bh);
1554 } while ((bh = bh->b_this_page) != head);
1556 BUG_ON(PageWriteback(page));
1557 set_page_writeback(page);
1559 struct buffer_head *next = bh->b_this_page;
1560 if (buffer_async_write(bh)) {
1561 clear_buffer_dirty(bh);
1562 submit_bh(WRITE, bh);
1566 } while (bh != head);
1571 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1573 struct inode * const inode = page->mapping->host;
1574 loff_t i_size = i_size_read(inode);
1575 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1579 /* Is the page fully inside i_size? */
1580 if (page->index < end_index)
1581 return __btrfs_write_full_page(inode, page, wbc);
1583 /* Is the page fully outside i_size? (truncate in progress) */
1584 offset = i_size & (PAGE_CACHE_SIZE-1);
1585 if (page->index >= end_index+1 || !offset) {
1587 * The page may have dirty, unmapped buffers. For example,
1588 * they may have been added in ext3_writepage(). Make them
1589 * freeable here, so the page does not leak.
1591 block_invalidatepage(page, 0);
1593 return 0; /* don't care */
1597 * The page straddles i_size. It must be zeroed out on each and every
1598 * writepage invokation because it may be mmapped. "A file is mapped
1599 * in multiples of the page size. For a file that is not a multiple of
1600 * the page size, the remaining memory is zeroed when mapped, and
1601 * writes to that region are not written out to the file."
1603 kaddr = kmap_atomic(page, KM_USER0);
1604 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1605 flush_dcache_page(page);
1606 kunmap_atomic(kaddr, KM_USER0);
1607 return __btrfs_write_full_page(inode, page, wbc);
1610 static void btrfs_truncate(struct inode *inode)
1612 struct btrfs_root *root = BTRFS_I(inode)->root;
1614 struct btrfs_trans_handle *trans;
1616 if (!S_ISREG(inode->i_mode))
1618 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1621 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1623 mutex_lock(&root->fs_info->fs_mutex);
1624 trans = btrfs_start_transaction(root, 1);
1625 btrfs_set_trans_block_group(trans, inode);
1627 /* FIXME, add redo link to tree so we don't leak on crash */
1628 ret = btrfs_truncate_in_trans(trans, root, inode);
1630 btrfs_update_inode(trans, root, inode);
1631 ret = btrfs_end_transaction(trans, root);
1633 mutex_unlock(&root->fs_info->fs_mutex);
1634 btrfs_btree_balance_dirty(root);
1637 int btrfs_commit_write(struct file *file, struct page *page,
1638 unsigned from, unsigned to)
1640 struct inode *inode = page->mapping->host;
1641 struct buffer_head *bh;
1642 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1644 SetPageUptodate(page);
1645 bh = page_buffers(page);
1646 set_buffer_uptodate(bh);
1647 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1648 set_page_dirty(page);
1650 if (pos > inode->i_size) {
1651 i_size_write(inode, pos);
1652 mark_inode_dirty(inode);
1657 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1659 struct btrfs_trans_handle *trans;
1660 struct btrfs_key key;
1661 struct btrfs_root_item root_item;
1662 struct btrfs_inode_item *inode_item;
1663 struct buffer_head *subvol;
1664 struct btrfs_leaf *leaf;
1665 struct btrfs_root *new_root;
1666 struct inode *inode;
1670 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1672 mutex_lock(&root->fs_info->fs_mutex);
1673 trans = btrfs_start_transaction(root, 1);
1676 subvol = btrfs_alloc_free_block(trans, root, 0);
1679 leaf = btrfs_buffer_leaf(subvol);
1680 btrfs_set_header_nritems(&leaf->header, 0);
1681 btrfs_set_header_level(&leaf->header, 0);
1682 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1683 btrfs_set_header_generation(&leaf->header, trans->transid);
1684 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
1685 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1686 sizeof(leaf->header.fsid));
1687 mark_buffer_dirty(subvol);
1689 inode_item = &root_item.inode;
1690 memset(inode_item, 0, sizeof(*inode_item));
1691 btrfs_set_inode_generation(inode_item, 1);
1692 btrfs_set_inode_size(inode_item, 3);
1693 btrfs_set_inode_nlink(inode_item, 1);
1694 btrfs_set_inode_nblocks(inode_item, 1);
1695 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1697 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1698 btrfs_set_root_refs(&root_item, 1);
1702 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1706 btrfs_set_root_dirid(&root_item, new_dirid);
1708 key.objectid = objectid;
1711 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1712 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1717 * insert the directory item
1719 key.offset = (u64)-1;
1720 dir = root->fs_info->sb->s_root->d_inode;
1721 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1722 name, namelen, dir->i_ino, &key,
1726 ret = btrfs_commit_transaction(trans, root);
1729 new_root = btrfs_read_fs_root(root->fs_info, &key);
1732 trans = btrfs_start_transaction(new_root, 1);
1735 inode = btrfs_new_inode(trans, new_root, new_dirid,
1736 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
1737 inode->i_op = &btrfs_dir_inode_operations;
1738 inode->i_fop = &btrfs_dir_file_operations;
1740 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1745 ret = btrfs_update_inode(trans, new_root, inode);
1748 ret = btrfs_commit_transaction(trans, new_root);
1753 mutex_unlock(&root->fs_info->fs_mutex);
1754 btrfs_btree_balance_dirty(root);
1758 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1760 struct btrfs_trans_handle *trans;
1761 struct btrfs_key key;
1762 struct btrfs_root_item new_root_item;
1766 if (!root->ref_cows)
1769 mutex_lock(&root->fs_info->fs_mutex);
1770 trans = btrfs_start_transaction(root, 1);
1773 ret = btrfs_update_inode(trans, root, root->inode);
1776 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1780 memcpy(&new_root_item, &root->root_item,
1781 sizeof(new_root_item));
1783 key.objectid = objectid;
1786 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1787 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
1789 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1794 * insert the directory item
1796 key.offset = (u64)-1;
1797 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1799 root->fs_info->sb->s_root->d_inode->i_ino,
1800 &key, BTRFS_FT_DIR);
1804 ret = btrfs_inc_root_ref(trans, root);
1807 ret = btrfs_commit_transaction(trans, root);
1809 mutex_unlock(&root->fs_info->fs_mutex);
1810 btrfs_btree_balance_dirty(root);
1814 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1815 cmd, unsigned long arg)
1817 struct btrfs_root *root = BTRFS_I(inode)->root;
1818 struct btrfs_ioctl_vol_args vol_args;
1820 struct btrfs_dir_item *di;
1822 struct btrfs_path *path;
1826 case BTRFS_IOC_SNAP_CREATE:
1827 if (copy_from_user(&vol_args,
1828 (struct btrfs_ioctl_vol_args __user *)arg,
1831 namelen = strlen(vol_args.name);
1832 if (namelen > BTRFS_VOL_NAME_MAX)
1834 path = btrfs_alloc_path();
1837 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
1838 mutex_lock(&root->fs_info->fs_mutex);
1839 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
1841 vol_args.name, namelen, 0);
1842 mutex_unlock(&root->fs_info->fs_mutex);
1843 btrfs_free_path(path);
1844 if (di && !IS_ERR(di))
1847 if (root == root->fs_info->tree_root)
1848 ret = create_subvol(root, vol_args.name, namelen);
1850 ret = create_snapshot(root, vol_args.name, namelen);
1859 #ifdef CONFIG_COMPAT
1860 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
1863 struct inode *inode = file->f_path.dentry->d_inode;
1866 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
1874 * Called inside transaction, so use GFP_NOFS
1876 struct inode *btrfs_alloc_inode(struct super_block *sb)
1878 struct btrfs_inode *ei;
1880 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1883 return &ei->vfs_inode;
1886 void btrfs_destroy_inode(struct inode *inode)
1888 WARN_ON(!list_empty(&inode->i_dentry));
1889 WARN_ON(inode->i_data.nrpages);
1891 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1894 static void init_once(void * foo, struct kmem_cache * cachep,
1895 unsigned long flags)
1897 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1899 inode_init_once(&ei->vfs_inode);
1902 void btrfs_destroy_cachep(void)
1904 if (btrfs_inode_cachep)
1905 kmem_cache_destroy(btrfs_inode_cachep);
1906 if (btrfs_trans_handle_cachep)
1907 kmem_cache_destroy(btrfs_trans_handle_cachep);
1908 if (btrfs_transaction_cachep)
1909 kmem_cache_destroy(btrfs_transaction_cachep);
1910 if (btrfs_bit_radix_cachep)
1911 kmem_cache_destroy(btrfs_bit_radix_cachep);
1912 if (btrfs_path_cachep)
1913 kmem_cache_destroy(btrfs_path_cachep);
1916 int btrfs_init_cachep(void)
1918 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1919 sizeof(struct btrfs_inode),
1920 0, (SLAB_RECLAIM_ACCOUNT|
1923 if (!btrfs_inode_cachep)
1925 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1926 sizeof(struct btrfs_trans_handle),
1927 0, (SLAB_RECLAIM_ACCOUNT|
1930 if (!btrfs_trans_handle_cachep)
1932 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1933 sizeof(struct btrfs_transaction),
1934 0, (SLAB_RECLAIM_ACCOUNT|
1937 if (!btrfs_transaction_cachep)
1939 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1940 sizeof(struct btrfs_transaction),
1941 0, (SLAB_RECLAIM_ACCOUNT|
1944 if (!btrfs_path_cachep)
1946 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1948 0, (SLAB_RECLAIM_ACCOUNT|
1950 SLAB_DESTROY_BY_RCU),
1952 if (!btrfs_bit_radix_cachep)
1956 btrfs_destroy_cachep();
1960 static int btrfs_getattr(struct vfsmount *mnt,
1961 struct dentry *dentry, struct kstat *stat)
1963 struct inode *inode = dentry->d_inode;
1964 generic_fillattr(inode, stat);
1965 stat->blksize = 256 * 1024;
1969 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
1970 struct inode * new_dir,struct dentry *new_dentry)
1972 struct btrfs_trans_handle *trans;
1973 struct btrfs_root *root = BTRFS_I(old_dir)->root;
1974 struct inode *new_inode = new_dentry->d_inode;
1975 struct inode *old_inode = old_dentry->d_inode;
1976 struct timespec ctime = CURRENT_TIME;
1977 struct btrfs_path *path;
1978 struct btrfs_dir_item *di;
1981 if (S_ISDIR(old_inode->i_mode) && new_inode &&
1982 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1985 mutex_lock(&root->fs_info->fs_mutex);
1986 trans = btrfs_start_transaction(root, 1);
1987 btrfs_set_trans_block_group(trans, new_dir);
1988 path = btrfs_alloc_path();
1994 old_dentry->d_inode->i_nlink++;
1995 old_dir->i_ctime = old_dir->i_mtime = ctime;
1996 new_dir->i_ctime = new_dir->i_mtime = ctime;
1997 old_inode->i_ctime = ctime;
1998 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
1999 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2001 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2011 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2012 ret = btrfs_del_item(trans, root, path);
2017 btrfs_release_path(root, path);
2019 di = btrfs_lookup_dir_index_item(trans, root, path,
2031 ret = btrfs_del_item(trans, root, path);
2036 btrfs_release_path(root, path);
2038 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2039 old_inode->i_ino, location,
2046 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2051 new_inode->i_ctime = CURRENT_TIME;
2052 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2055 if (S_ISDIR(new_inode->i_mode))
2056 clear_nlink(new_inode);
2058 drop_nlink(new_inode);
2059 btrfs_update_inode(trans, root, new_inode);
2061 ret = btrfs_add_link(trans, new_dentry, old_inode);
2066 btrfs_free_path(path);
2067 btrfs_end_transaction(trans, root);
2068 mutex_unlock(&root->fs_info->fs_mutex);
2072 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2073 const char *symname)
2075 struct btrfs_trans_handle *trans;
2076 struct btrfs_root *root = BTRFS_I(dir)->root;
2077 struct btrfs_path *path;
2078 struct btrfs_key key;
2079 struct inode *inode;
2086 struct btrfs_file_extent_item *ei;
2088 name_len = strlen(symname) + 1;
2089 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2090 return -ENAMETOOLONG;
2091 mutex_lock(&root->fs_info->fs_mutex);
2092 trans = btrfs_start_transaction(root, 1);
2093 btrfs_set_trans_block_group(trans, dir);
2095 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2101 inode = btrfs_new_inode(trans, root, objectid,
2102 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2103 err = PTR_ERR(inode);
2107 btrfs_set_trans_block_group(trans, inode);
2108 err = btrfs_add_nondir(trans, dentry, inode);
2112 inode->i_mapping->a_ops = &btrfs_aops;
2113 inode->i_fop = &btrfs_file_operations;
2114 inode->i_op = &btrfs_file_inode_operations;
2116 dir->i_sb->s_dirt = 1;
2117 btrfs_update_inode_block_group(trans, inode);
2118 btrfs_update_inode_block_group(trans, dir);
2122 path = btrfs_alloc_path();
2124 key.objectid = inode->i_ino;
2127 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2128 datasize = btrfs_file_extent_calc_inline_size(name_len);
2129 err = btrfs_insert_empty_item(trans, root, path, &key,
2132 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2133 path->slots[0], struct btrfs_file_extent_item);
2134 btrfs_set_file_extent_generation(ei, trans->transid);
2135 btrfs_set_file_extent_type(ei,
2136 BTRFS_FILE_EXTENT_INLINE);
2137 ptr = btrfs_file_extent_inline_start(ei);
2138 btrfs_memcpy(root, path->nodes[0]->b_data,
2139 ptr, symname, name_len);
2140 mark_buffer_dirty(path->nodes[0]);
2141 btrfs_free_path(path);
2142 inode->i_op = &btrfs_symlink_inode_operations;
2143 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2144 inode->i_size = name_len - 1;
2145 btrfs_update_inode(trans, root, inode);
2149 btrfs_end_transaction(trans, root);
2150 mutex_unlock(&root->fs_info->fs_mutex);
2153 inode_dec_link_count(inode);
2156 btrfs_btree_balance_dirty(root);
2160 static struct inode_operations btrfs_dir_inode_operations = {
2161 .lookup = btrfs_lookup,
2162 .create = btrfs_create,
2163 .unlink = btrfs_unlink,
2165 .mkdir = btrfs_mkdir,
2166 .rmdir = btrfs_rmdir,
2167 .rename = btrfs_rename,
2168 .symlink = btrfs_symlink,
2169 .setattr = btrfs_setattr,
2172 static struct inode_operations btrfs_dir_ro_inode_operations = {
2173 .lookup = btrfs_lookup,
2176 static struct file_operations btrfs_dir_file_operations = {
2177 .llseek = generic_file_llseek,
2178 .read = generic_read_dir,
2179 .readdir = btrfs_readdir,
2180 .ioctl = btrfs_ioctl,
2181 #ifdef CONFIG_COMPAT
2182 .compat_ioctl = btrfs_compat_ioctl,
2186 static struct address_space_operations btrfs_aops = {
2187 .readpage = btrfs_readpage,
2188 .writepage = btrfs_writepage,
2189 .sync_page = block_sync_page,
2190 .prepare_write = btrfs_prepare_write,
2191 .commit_write = btrfs_commit_write,
2195 static struct address_space_operations btrfs_symlink_aops = {
2196 .readpage = btrfs_readpage,
2197 .writepage = btrfs_writepage,
2200 static struct inode_operations btrfs_file_inode_operations = {
2201 .truncate = btrfs_truncate,
2202 .getattr = btrfs_getattr,
2203 .setattr = btrfs_setattr,
2206 static struct inode_operations btrfs_symlink_inode_operations = {
2207 .readlink = generic_readlink,
2208 .follow_link = page_follow_link_light,
2209 .put_link = page_put_link,