2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/module.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
41 struct btrfs_iget_args {
43 struct btrfs_root *root;
46 static struct inode_operations btrfs_dir_inode_operations;
47 static struct inode_operations btrfs_symlink_inode_operations;
48 static struct inode_operations btrfs_dir_ro_inode_operations;
49 static struct inode_operations btrfs_file_inode_operations;
50 static struct address_space_operations btrfs_aops;
51 static struct address_space_operations btrfs_symlink_aops;
52 static struct file_operations btrfs_dir_file_operations;
54 static struct kmem_cache *btrfs_inode_cachep;
55 struct kmem_cache *btrfs_trans_handle_cachep;
56 struct kmem_cache *btrfs_transaction_cachep;
57 struct kmem_cache *btrfs_bit_radix_cachep;
58 struct kmem_cache *btrfs_path_cachep;
61 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
62 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
63 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
64 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
65 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
66 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
67 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
68 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
71 void btrfs_read_locked_inode(struct inode *inode)
73 struct btrfs_path *path;
74 struct btrfs_inode_item *inode_item;
75 struct btrfs_root *root = BTRFS_I(inode)->root;
76 struct btrfs_key location;
77 u64 alloc_group_block;
80 path = btrfs_alloc_path();
82 mutex_lock(&root->fs_info->fs_mutex);
84 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
85 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
87 btrfs_free_path(path);
90 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
92 struct btrfs_inode_item);
94 inode->i_mode = btrfs_inode_mode(inode_item);
95 inode->i_nlink = btrfs_inode_nlink(inode_item);
96 inode->i_uid = btrfs_inode_uid(inode_item);
97 inode->i_gid = btrfs_inode_gid(inode_item);
98 inode->i_size = btrfs_inode_size(inode_item);
99 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
100 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
101 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
102 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
103 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
104 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
105 inode->i_blocks = btrfs_inode_nblocks(inode_item);
106 inode->i_generation = btrfs_inode_generation(inode_item);
107 alloc_group_block = btrfs_inode_block_group(inode_item);
108 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
111 btrfs_free_path(path);
114 mutex_unlock(&root->fs_info->fs_mutex);
116 switch (inode->i_mode & S_IFMT) {
119 init_special_inode(inode, inode->i_mode,
120 btrfs_inode_rdev(inode_item));
124 inode->i_mapping->a_ops = &btrfs_aops;
125 inode->i_fop = &btrfs_file_operations;
126 inode->i_op = &btrfs_file_inode_operations;
129 inode->i_fop = &btrfs_dir_file_operations;
130 if (root == root->fs_info->tree_root)
131 inode->i_op = &btrfs_dir_ro_inode_operations;
133 inode->i_op = &btrfs_dir_inode_operations;
136 inode->i_op = &btrfs_symlink_inode_operations;
137 inode->i_mapping->a_ops = &btrfs_symlink_aops;
143 btrfs_release_path(root, path);
144 btrfs_free_path(path);
145 mutex_unlock(&root->fs_info->fs_mutex);
146 make_bad_inode(inode);
149 static void fill_inode_item(struct btrfs_inode_item *item,
152 btrfs_set_inode_uid(item, inode->i_uid);
153 btrfs_set_inode_gid(item, inode->i_gid);
154 btrfs_set_inode_size(item, inode->i_size);
155 btrfs_set_inode_mode(item, inode->i_mode);
156 btrfs_set_inode_nlink(item, inode->i_nlink);
157 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
158 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
159 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
160 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
161 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
162 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
163 btrfs_set_inode_nblocks(item, inode->i_blocks);
164 btrfs_set_inode_generation(item, inode->i_generation);
165 btrfs_set_inode_block_group(item,
166 BTRFS_I(inode)->block_group->key.objectid);
169 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
170 struct btrfs_root *root,
173 struct btrfs_inode_item *inode_item;
174 struct btrfs_path *path;
177 path = btrfs_alloc_path();
179 ret = btrfs_lookup_inode(trans, root, path,
180 &BTRFS_I(inode)->location, 1);
187 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
189 struct btrfs_inode_item);
191 fill_inode_item(inode_item, inode);
192 btrfs_mark_buffer_dirty(path->nodes[0]);
195 btrfs_release_path(root, path);
196 btrfs_free_path(path);
201 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root,
204 struct dentry *dentry)
206 struct btrfs_path *path;
207 const char *name = dentry->d_name.name;
208 int name_len = dentry->d_name.len;
211 struct btrfs_dir_item *di;
213 path = btrfs_alloc_path();
215 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
225 objectid = btrfs_disk_key_objectid(&di->location);
226 ret = btrfs_delete_one_dir_name(trans, root, path, di);
228 btrfs_release_path(root, path);
230 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
231 objectid, name, name_len, -1);
240 ret = btrfs_delete_one_dir_name(trans, root, path, di);
243 dentry->d_inode->i_ctime = dir->i_ctime;
245 btrfs_free_path(path);
247 dir->i_size -= name_len * 2;
248 btrfs_update_inode(trans, root, dir);
249 drop_nlink(dentry->d_inode);
250 btrfs_update_inode(trans, root, dentry->d_inode);
251 dir->i_sb->s_dirt = 1;
256 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
258 struct btrfs_root *root;
259 struct btrfs_trans_handle *trans;
262 root = BTRFS_I(dir)->root;
263 mutex_lock(&root->fs_info->fs_mutex);
264 trans = btrfs_start_transaction(root, 1);
265 btrfs_set_trans_block_group(trans, dir);
266 ret = btrfs_unlink_trans(trans, root, dir, dentry);
267 btrfs_end_transaction(trans, root);
268 mutex_unlock(&root->fs_info->fs_mutex);
269 btrfs_btree_balance_dirty(root);
273 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
275 struct inode *inode = dentry->d_inode;
278 struct btrfs_root *root = BTRFS_I(dir)->root;
279 struct btrfs_path *path;
280 struct btrfs_key key;
281 struct btrfs_trans_handle *trans;
282 struct btrfs_key found_key;
284 struct btrfs_leaf *leaf;
285 char *goodnames = "..";
287 path = btrfs_alloc_path();
289 mutex_lock(&root->fs_info->fs_mutex);
290 trans = btrfs_start_transaction(root, 1);
291 btrfs_set_trans_block_group(trans, dir);
292 key.objectid = inode->i_ino;
293 key.offset = (u64)-1;
296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
302 if (path->slots[0] == 0) {
307 leaf = btrfs_buffer_leaf(path->nodes[0]);
308 btrfs_disk_key_to_cpu(&found_key,
309 &leaf->items[path->slots[0]].key);
310 found_type = btrfs_key_type(&found_key);
311 if (found_key.objectid != inode->i_ino) {
315 if ((found_type != BTRFS_DIR_ITEM_KEY &&
316 found_type != BTRFS_DIR_INDEX_KEY) ||
317 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
318 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
322 ret = btrfs_del_item(trans, root, path);
325 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
327 btrfs_release_path(root, path);
330 btrfs_release_path(root, path);
332 /* now the directory is empty */
333 err = btrfs_unlink_trans(trans, root, dir, dentry);
338 btrfs_release_path(root, path);
339 btrfs_free_path(path);
340 mutex_unlock(&root->fs_info->fs_mutex);
341 ret = btrfs_end_transaction(trans, root);
342 btrfs_btree_balance_dirty(root);
348 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
349 struct btrfs_root *root,
352 struct btrfs_path *path;
357 path = btrfs_alloc_path();
359 ret = btrfs_lookup_inode(trans, root, path,
360 &BTRFS_I(inode)->location, -1);
362 ret = btrfs_del_item(trans, root, path);
364 btrfs_free_path(path);
369 * truncates go from a high offset to a low offset. So, walk
370 * from hi to lo in the node and issue readas. Stop when you find
371 * keys from a different objectid
373 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
376 struct btrfs_node *node;
386 node = btrfs_buffer_node(path->nodes[1]);
387 slot = path->slots[1];
390 nritems = btrfs_header_nritems(&node->header);
391 for (i = slot - 1; i >= 0; i--) {
392 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
393 if (item_objectid != objectid)
395 blocknr = btrfs_node_blockptr(node, i);
396 ret = readahead_tree_block(root, blocknr);
403 * this can truncate away extent items, csum items and directory items.
404 * It starts at a high offset and removes keys until it can't find
405 * any higher than i_size.
407 * csum items that cross the new i_size are truncated to the new size
410 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
411 struct btrfs_root *root,
415 struct btrfs_path *path;
416 struct btrfs_key key;
417 struct btrfs_disk_key *found_key;
419 struct btrfs_leaf *leaf;
420 struct btrfs_file_extent_item *fi;
421 u64 extent_start = 0;
422 u64 extent_num_blocks = 0;
427 path = btrfs_alloc_path();
429 /* FIXME, add redo link to tree so we don't leak on crash */
430 key.objectid = inode->i_ino;
431 key.offset = (u64)-1;
434 btrfs_init_path(path);
436 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
441 BUG_ON(path->slots[0] == 0);
444 reada_truncate(root, path, inode->i_ino);
445 leaf = btrfs_buffer_leaf(path->nodes[0]);
446 found_key = &leaf->items[path->slots[0]].key;
447 found_type = btrfs_disk_key_type(found_key);
449 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
451 if (found_type != BTRFS_CSUM_ITEM_KEY &&
452 found_type != BTRFS_DIR_ITEM_KEY &&
453 found_type != BTRFS_DIR_INDEX_KEY &&
454 found_type != BTRFS_EXTENT_DATA_KEY)
457 item_end = btrfs_disk_key_offset(found_key);
458 if (found_type == BTRFS_EXTENT_DATA_KEY) {
459 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
461 struct btrfs_file_extent_item);
462 if (btrfs_file_extent_type(fi) !=
463 BTRFS_FILE_EXTENT_INLINE) {
464 item_end += btrfs_file_extent_num_blocks(fi) <<
468 if (found_type == BTRFS_CSUM_ITEM_KEY) {
469 ret = btrfs_csum_truncate(trans, root, path,
473 if (item_end < inode->i_size) {
475 btrfs_set_key_type(&key, found_type - 1);
480 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
486 /* FIXME, shrink the extent if the ref count is only 1 */
487 if (found_type == BTRFS_EXTENT_DATA_KEY &&
488 btrfs_file_extent_type(fi) !=
489 BTRFS_FILE_EXTENT_INLINE) {
492 u64 orig_num_blocks =
493 btrfs_file_extent_num_blocks(fi);
494 extent_num_blocks = inode->i_size -
495 btrfs_disk_key_offset(found_key) +
497 extent_num_blocks >>= inode->i_blkbits;
498 btrfs_set_file_extent_num_blocks(fi,
500 inode->i_blocks -= (orig_num_blocks -
501 extent_num_blocks) << 3;
502 mark_buffer_dirty(path->nodes[0]);
505 btrfs_file_extent_disk_blocknr(fi);
507 btrfs_file_extent_disk_num_blocks(fi);
508 /* FIXME blocksize != 4096 */
509 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
510 if (extent_start != 0) {
512 inode->i_blocks -= num_dec;
517 ret = btrfs_del_item(trans, root, path);
522 btrfs_release_path(root, path);
524 ret = btrfs_free_extent(trans, root, extent_start,
525 extent_num_blocks, 0);
531 btrfs_release_path(root, path);
532 btrfs_free_path(path);
533 inode->i_sb->s_dirt = 1;
538 * taken from block_truncate_page, but does cow as it zeros out
539 * any bytes left in the last page in the file.
541 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
543 struct inode *inode = mapping->host;
544 unsigned blocksize = 1 << inode->i_blkbits;
545 pgoff_t index = from >> PAGE_CACHE_SHIFT;
546 unsigned offset = from & (PAGE_CACHE_SIZE-1);
550 struct btrfs_root *root = BTRFS_I(inode)->root;
552 struct btrfs_key ins;
553 struct btrfs_trans_handle *trans;
555 if ((offset & (blocksize - 1)) == 0)
559 page = grab_cache_page(mapping, index);
563 if (!PageUptodate(page)) {
564 ret = mpage_readpage(page, btrfs_get_block);
566 if (!PageUptodate(page)) {
571 mutex_lock(&root->fs_info->fs_mutex);
572 trans = btrfs_start_transaction(root, 1);
573 btrfs_set_trans_block_group(trans, inode);
575 ret = btrfs_drop_extents(trans, root, inode,
576 page->index << PAGE_CACHE_SHIFT,
577 (page->index + 1) << PAGE_CACHE_SHIFT,
580 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
581 alloc_hint, (u64)-1, &ins, 1);
583 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
584 page->index << PAGE_CACHE_SHIFT,
587 SetPageChecked(page);
589 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
590 flush_dcache_page(page);
591 btrfs_csum_file_block(trans, root, inode->i_ino,
592 page->index << PAGE_CACHE_SHIFT,
593 kaddr, PAGE_CACHE_SIZE);
595 btrfs_end_transaction(trans, root);
596 mutex_unlock(&root->fs_info->fs_mutex);
598 set_page_dirty(page);
600 page_cache_release(page);
605 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
607 struct inode *inode = dentry->d_inode;
610 err = inode_change_ok(inode, attr);
614 if (S_ISREG(inode->i_mode) &&
615 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
616 struct btrfs_trans_handle *trans;
617 struct btrfs_root *root = BTRFS_I(inode)->root;
618 u64 mask = root->blocksize - 1;
619 u64 pos = (inode->i_size + mask) & ~mask;
622 if (attr->ia_size <= pos)
625 btrfs_truncate_page(inode->i_mapping, inode->i_size);
627 hole_size = (attr->ia_size - pos + mask) & ~mask;
628 hole_size >>= inode->i_blkbits;
630 mutex_lock(&root->fs_info->fs_mutex);
631 trans = btrfs_start_transaction(root, 1);
632 btrfs_set_trans_block_group(trans, inode);
633 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
634 pos, 0, 0, hole_size);
636 btrfs_end_transaction(trans, root);
637 mutex_unlock(&root->fs_info->fs_mutex);
640 err = inode_setattr(inode, attr);
644 void btrfs_delete_inode(struct inode *inode)
646 struct btrfs_trans_handle *trans;
647 struct btrfs_root *root = BTRFS_I(inode)->root;
650 truncate_inode_pages(&inode->i_data, 0);
651 if (is_bad_inode(inode)) {
655 mutex_lock(&root->fs_info->fs_mutex);
656 trans = btrfs_start_transaction(root, 1);
657 btrfs_set_trans_block_group(trans, inode);
658 ret = btrfs_truncate_in_trans(trans, root, inode);
660 btrfs_free_inode(trans, root, inode);
661 btrfs_end_transaction(trans, root);
662 mutex_unlock(&root->fs_info->fs_mutex);
663 btrfs_btree_balance_dirty(root);
670 * this returns the key found in the dir entry in the location pointer.
671 * If no dir entries were found, location->objectid is 0.
673 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
674 struct btrfs_key *location)
676 const char *name = dentry->d_name.name;
677 int namelen = dentry->d_name.len;
678 struct btrfs_dir_item *di;
679 struct btrfs_path *path;
680 struct btrfs_root *root = BTRFS_I(dir)->root;
683 path = btrfs_alloc_path();
685 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
687 if (!di || IS_ERR(di)) {
688 location->objectid = 0;
692 btrfs_disk_key_to_cpu(location, &di->location);
694 btrfs_release_path(root, path);
695 btrfs_free_path(path);
700 * when we hit a tree root in a directory, the btrfs part of the inode
701 * needs to be changed to reflect the root directory of the tree root. This
702 * is kind of like crossing a mount point.
704 static int fixup_tree_root_location(struct btrfs_root *root,
705 struct btrfs_key *location,
706 struct btrfs_root **sub_root)
708 struct btrfs_path *path;
709 struct btrfs_root_item *ri;
711 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
713 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
716 path = btrfs_alloc_path();
718 mutex_lock(&root->fs_info->fs_mutex);
720 *sub_root = btrfs_read_fs_root(root->fs_info, location);
721 if (IS_ERR(*sub_root))
722 return PTR_ERR(*sub_root);
724 ri = &(*sub_root)->root_item;
725 location->objectid = btrfs_root_dirid(ri);
727 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
728 location->offset = 0;
730 btrfs_free_path(path);
731 mutex_unlock(&root->fs_info->fs_mutex);
735 static int btrfs_init_locked_inode(struct inode *inode, void *p)
737 struct btrfs_iget_args *args = p;
738 inode->i_ino = args->ino;
739 BTRFS_I(inode)->root = args->root;
743 static int btrfs_find_actor(struct inode *inode, void *opaque)
745 struct btrfs_iget_args *args = opaque;
746 return (args->ino == inode->i_ino &&
747 args->root == BTRFS_I(inode)->root);
750 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
751 struct btrfs_root *root)
754 struct btrfs_iget_args args;
758 inode = iget5_locked(s, objectid, btrfs_find_actor,
759 btrfs_init_locked_inode,
764 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
765 struct nameidata *nd)
767 struct inode * inode;
768 struct btrfs_inode *bi = BTRFS_I(dir);
769 struct btrfs_root *root = bi->root;
770 struct btrfs_root *sub_root = root;
771 struct btrfs_key location;
774 if (dentry->d_name.len > BTRFS_NAME_LEN)
775 return ERR_PTR(-ENAMETOOLONG);
776 mutex_lock(&root->fs_info->fs_mutex);
777 ret = btrfs_inode_by_name(dir, dentry, &location);
778 mutex_unlock(&root->fs_info->fs_mutex);
782 if (location.objectid) {
783 ret = fixup_tree_root_location(root, &location, &sub_root);
787 return ERR_PTR(-ENOENT);
788 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
791 return ERR_PTR(-EACCES);
792 if (inode->i_state & I_NEW) {
793 /* the inode and parent dir are two different roots */
794 if (sub_root != root) {
796 sub_root->inode = inode;
798 BTRFS_I(inode)->root = sub_root;
799 memcpy(&BTRFS_I(inode)->location, &location,
801 btrfs_read_locked_inode(inode);
802 unlock_new_inode(inode);
805 return d_splice_alias(inode, dentry);
809 * readahead one full node of leaves as long as their keys include
810 * the objectid supplied
812 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
815 struct btrfs_node *node;
825 node = btrfs_buffer_node(path->nodes[1]);
826 slot = path->slots[1];
827 nritems = btrfs_header_nritems(&node->header);
828 for (i = slot + 1; i < nritems; i++) {
829 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
830 if (item_objectid != objectid)
832 blocknr = btrfs_node_blockptr(node, i);
833 ret = readahead_tree_block(root, blocknr);
838 static unsigned char btrfs_filetype_table[] = {
839 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
842 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
844 struct inode *inode = filp->f_path.dentry->d_inode;
845 struct btrfs_root *root = BTRFS_I(inode)->root;
846 struct btrfs_item *item;
847 struct btrfs_dir_item *di;
848 struct btrfs_key key;
849 struct btrfs_path *path;
852 struct btrfs_leaf *leaf;
855 unsigned char d_type;
860 int key_type = BTRFS_DIR_INDEX_KEY;
862 /* FIXME, use a real flag for deciding about the key type */
863 if (root->fs_info->tree_root == root)
864 key_type = BTRFS_DIR_ITEM_KEY;
865 mutex_lock(&root->fs_info->fs_mutex);
866 key.objectid = inode->i_ino;
868 btrfs_set_key_type(&key, key_type);
869 key.offset = filp->f_pos;
870 path = btrfs_alloc_path();
871 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
875 reada_leaves(root, path, inode->i_ino);
877 leaf = btrfs_buffer_leaf(path->nodes[0]);
878 nritems = btrfs_header_nritems(&leaf->header);
879 slot = path->slots[0];
880 if (advance || slot >= nritems) {
881 if (slot >= nritems -1) {
882 reada_leaves(root, path, inode->i_ino);
883 ret = btrfs_next_leaf(root, path);
886 leaf = btrfs_buffer_leaf(path->nodes[0]);
887 nritems = btrfs_header_nritems(&leaf->header);
888 slot = path->slots[0];
895 item = leaf->items + slot;
896 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
898 if (btrfs_disk_key_type(&item->key) != key_type)
900 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
902 filp->f_pos = btrfs_disk_key_offset(&item->key);
904 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
906 di_total = btrfs_item_size(leaf->items + slot);
907 while(di_cur < di_total) {
908 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
909 over = filldir(dirent, (const char *)(di + 1),
910 btrfs_dir_name_len(di),
911 btrfs_disk_key_offset(&item->key),
912 btrfs_disk_key_objectid(&di->location),
916 di_len = btrfs_dir_name_len(di) + sizeof(*di);
918 di = (struct btrfs_dir_item *)((char *)di + di_len);
925 btrfs_release_path(root, path);
926 btrfs_free_path(path);
927 mutex_unlock(&root->fs_info->fs_mutex);
931 int btrfs_write_inode(struct inode *inode, int wait)
933 struct btrfs_root *root = BTRFS_I(inode)->root;
934 struct btrfs_trans_handle *trans;
938 mutex_lock(&root->fs_info->fs_mutex);
939 trans = btrfs_start_transaction(root, 1);
940 btrfs_set_trans_block_group(trans, inode);
941 ret = btrfs_commit_transaction(trans, root);
942 mutex_unlock(&root->fs_info->fs_mutex);
948 * This is somewhat expense, updating the tree every time the
949 * inode changes. But, it is most likely to find the inode in cache.
950 * FIXME, needs more benchmarking...there are no reasons other than performance
951 * to keep or drop this code.
953 void btrfs_dirty_inode(struct inode *inode)
955 struct btrfs_root *root = BTRFS_I(inode)->root;
956 struct btrfs_trans_handle *trans;
958 mutex_lock(&root->fs_info->fs_mutex);
959 trans = btrfs_start_transaction(root, 1);
960 btrfs_set_trans_block_group(trans, inode);
961 btrfs_update_inode(trans, root, inode);
962 btrfs_end_transaction(trans, root);
963 mutex_unlock(&root->fs_info->fs_mutex);
964 btrfs_btree_balance_dirty(root);
967 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
968 struct btrfs_root *root,
970 struct btrfs_block_group_cache *group,
974 struct btrfs_inode_item inode_item;
975 struct btrfs_key *location;
979 inode = new_inode(root->fs_info->sb);
981 return ERR_PTR(-ENOMEM);
983 BTRFS_I(inode)->root = root;
988 group = btrfs_find_block_group(root, group, 0, 0, owner);
989 BTRFS_I(inode)->block_group = group;
991 inode->i_uid = current->fsuid;
992 inode->i_gid = current->fsgid;
993 inode->i_mode = mode;
994 inode->i_ino = objectid;
996 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
997 fill_inode_item(&inode_item, inode);
998 location = &BTRFS_I(inode)->location;
999 location->objectid = objectid;
1000 location->flags = 0;
1001 location->offset = 0;
1002 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1004 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1007 insert_inode_hash(inode);
1011 static inline u8 btrfs_inode_type(struct inode *inode)
1013 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1016 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1017 struct dentry *dentry, struct inode *inode)
1020 struct btrfs_key key;
1021 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1022 key.objectid = inode->i_ino;
1024 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1027 ret = btrfs_insert_dir_item(trans, root,
1028 dentry->d_name.name, dentry->d_name.len,
1029 dentry->d_parent->d_inode->i_ino,
1030 &key, btrfs_inode_type(inode));
1032 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
1033 ret = btrfs_update_inode(trans, root,
1034 dentry->d_parent->d_inode);
1039 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1040 struct dentry *dentry, struct inode *inode)
1042 int err = btrfs_add_link(trans, dentry, inode);
1044 d_instantiate(dentry, inode);
1052 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1053 int mode, struct nameidata *nd)
1055 struct btrfs_trans_handle *trans;
1056 struct btrfs_root *root = BTRFS_I(dir)->root;
1057 struct inode *inode;
1062 mutex_lock(&root->fs_info->fs_mutex);
1063 trans = btrfs_start_transaction(root, 1);
1064 btrfs_set_trans_block_group(trans, dir);
1066 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1072 inode = btrfs_new_inode(trans, root, objectid,
1073 BTRFS_I(dir)->block_group, mode);
1074 err = PTR_ERR(inode);
1078 btrfs_set_trans_block_group(trans, inode);
1079 err = btrfs_add_nondir(trans, dentry, inode);
1083 inode->i_mapping->a_ops = &btrfs_aops;
1084 inode->i_fop = &btrfs_file_operations;
1085 inode->i_op = &btrfs_file_inode_operations;
1087 dir->i_sb->s_dirt = 1;
1088 btrfs_update_inode_block_group(trans, inode);
1089 btrfs_update_inode_block_group(trans, dir);
1091 btrfs_end_transaction(trans, root);
1092 mutex_unlock(&root->fs_info->fs_mutex);
1095 inode_dec_link_count(inode);
1098 btrfs_btree_balance_dirty(root);
1102 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1103 struct dentry *dentry)
1105 struct btrfs_trans_handle *trans;
1106 struct btrfs_root *root = BTRFS_I(dir)->root;
1107 struct inode *inode = old_dentry->d_inode;
1111 if (inode->i_nlink == 0)
1115 mutex_lock(&root->fs_info->fs_mutex);
1116 trans = btrfs_start_transaction(root, 1);
1117 btrfs_set_trans_block_group(trans, dir);
1118 atomic_inc(&inode->i_count);
1119 err = btrfs_add_nondir(trans, dentry, inode);
1122 dir->i_sb->s_dirt = 1;
1123 btrfs_update_inode_block_group(trans, dir);
1124 btrfs_update_inode(trans, root, inode);
1126 btrfs_end_transaction(trans, root);
1127 mutex_unlock(&root->fs_info->fs_mutex);
1130 inode_dec_link_count(inode);
1133 btrfs_btree_balance_dirty(root);
1137 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1138 struct btrfs_root *root,
1139 u64 objectid, u64 dirid)
1143 struct btrfs_key key;
1148 key.objectid = objectid;
1151 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1153 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1154 &key, BTRFS_FT_DIR);
1157 key.objectid = dirid;
1158 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1159 &key, BTRFS_FT_DIR);
1166 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1168 struct inode *inode;
1169 struct btrfs_trans_handle *trans;
1170 struct btrfs_root *root = BTRFS_I(dir)->root;
1172 int drop_on_err = 0;
1175 mutex_lock(&root->fs_info->fs_mutex);
1176 trans = btrfs_start_transaction(root, 1);
1177 btrfs_set_trans_block_group(trans, dir);
1178 if (IS_ERR(trans)) {
1179 err = PTR_ERR(trans);
1183 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1189 inode = btrfs_new_inode(trans, root, objectid,
1190 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1191 if (IS_ERR(inode)) {
1192 err = PTR_ERR(inode);
1196 inode->i_op = &btrfs_dir_inode_operations;
1197 inode->i_fop = &btrfs_dir_file_operations;
1198 btrfs_set_trans_block_group(trans, inode);
1200 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1205 err = btrfs_update_inode(trans, root, inode);
1208 err = btrfs_add_link(trans, dentry, inode);
1211 d_instantiate(dentry, inode);
1213 dir->i_sb->s_dirt = 1;
1214 btrfs_update_inode_block_group(trans, inode);
1215 btrfs_update_inode_block_group(trans, dir);
1218 btrfs_end_transaction(trans, root);
1220 mutex_unlock(&root->fs_info->fs_mutex);
1223 btrfs_btree_balance_dirty(root);
1228 * FIBMAP and others want to pass in a fake buffer head. They need to
1229 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1230 * any packed file data into the fake bh
1232 #define BTRFS_GET_BLOCK_NO_CREATE 0
1233 #define BTRFS_GET_BLOCK_CREATE 1
1234 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1237 * FIXME create==1 doe not work.
1239 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1240 struct buffer_head *result, int create)
1245 u64 extent_start = 0;
1247 u64 objectid = inode->i_ino;
1250 struct btrfs_path *path;
1251 struct btrfs_root *root = BTRFS_I(inode)->root;
1252 struct btrfs_file_extent_item *item;
1253 struct btrfs_leaf *leaf;
1254 struct btrfs_disk_key *found_key;
1255 struct btrfs_trans_handle *trans = NULL;
1257 path = btrfs_alloc_path();
1259 if (create & BTRFS_GET_BLOCK_CREATE) {
1261 /* this almost but not quite works */
1262 trans = btrfs_start_transaction(root, 1);
1267 ret = btrfs_drop_extents(trans, root, inode,
1268 iblock << inode->i_blkbits,
1269 (iblock + 1) << inode->i_blkbits,
1274 ret = btrfs_lookup_file_extent(NULL, root, path,
1276 iblock << inode->i_blkbits, 0);
1283 if (path->slots[0] == 0) {
1284 btrfs_release_path(root, path);
1290 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1291 struct btrfs_file_extent_item);
1292 leaf = btrfs_buffer_leaf(path->nodes[0]);
1293 blocknr = btrfs_file_extent_disk_blocknr(item);
1294 blocknr += btrfs_file_extent_offset(item);
1296 /* are we inside the extent that was found? */
1297 found_key = &leaf->items[path->slots[0]].key;
1298 found_type = btrfs_disk_key_type(found_key);
1299 if (btrfs_disk_key_objectid(found_key) != objectid ||
1300 found_type != BTRFS_EXTENT_DATA_KEY) {
1305 found_type = btrfs_file_extent_type(item);
1306 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1307 if (found_type == BTRFS_FILE_EXTENT_REG) {
1308 extent_start = extent_start >> inode->i_blkbits;
1309 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1311 if (btrfs_file_extent_disk_blocknr(item) == 0)
1313 if (iblock >= extent_start && iblock < extent_end) {
1314 btrfs_map_bh_to_logical(root, result, blocknr +
1315 iblock - extent_start);
1318 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1323 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1327 size = btrfs_file_extent_inline_len(leaf->items +
1329 extent_end = (extent_start + size) >> inode->i_blkbits;
1330 extent_start >>= inode->i_blkbits;
1331 if (iblock < extent_start || iblock > extent_end) {
1334 ptr = btrfs_file_extent_inline_start(item);
1335 map = kmap(result->b_page);
1336 memcpy(map, ptr, size);
1337 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1338 flush_dcache_page(result->b_page);
1339 kunmap(result->b_page);
1340 set_buffer_uptodate(result);
1341 SetPageChecked(result->b_page);
1342 btrfs_map_bh_to_logical(root, result, 0);
1345 if (create & BTRFS_GET_BLOCK_CREATE) {
1346 struct btrfs_key ins;
1347 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1348 1, alloc_hint, (u64)-1,
1351 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1352 iblock << inode->i_blkbits,
1353 ins.objectid, ins.offset,
1356 SetPageChecked(result->b_page);
1357 btrfs_map_bh_to_logical(root, result, ins.objectid);
1361 err = btrfs_end_transaction(trans, root);
1362 btrfs_free_path(path);
1366 int btrfs_get_block(struct inode *inode, sector_t iblock,
1367 struct buffer_head *result, int create)
1370 struct btrfs_root *root = BTRFS_I(inode)->root;
1371 mutex_lock(&root->fs_info->fs_mutex);
1372 err = btrfs_get_block_lock(inode, iblock, result, create);
1373 mutex_unlock(&root->fs_info->fs_mutex);
1377 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1378 struct buffer_head *result, int create)
1380 struct btrfs_root *root = BTRFS_I(inode)->root;
1381 mutex_lock(&root->fs_info->fs_mutex);
1382 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1383 mutex_unlock(&root->fs_info->fs_mutex);
1387 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1389 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1392 static int btrfs_prepare_write(struct file *file, struct page *page,
1393 unsigned from, unsigned to)
1395 return block_prepare_write(page, from, to, btrfs_get_block);
1398 static int btrfs_readpage(struct file *file, struct page *page)
1400 return mpage_readpage(page, btrfs_get_block);
1404 * Aside from a tiny bit of packed file data handling, this is the
1405 * same as the generic code.
1407 * While block_write_full_page is writing back the dirty buffers under
1408 * the page lock, whoever dirtied the buffers may decide to clean them
1409 * again at any time. We handle that by only looking at the buffer
1410 * state inside lock_buffer().
1412 * If block_write_full_page() is called for regular writeback
1413 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1414 * locked buffer. This only can happen if someone has written the buffer
1415 * directly, with submit_bh(). At the address_space level PageWriteback
1416 * prevents this contention from occurring.
1418 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1419 struct writeback_control *wbc)
1423 sector_t last_block;
1424 struct buffer_head *bh, *head;
1425 const unsigned blocksize = 1 << inode->i_blkbits;
1426 int nr_underway = 0;
1428 BUG_ON(!PageLocked(page));
1430 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1432 if (!page_has_buffers(page)) {
1433 create_empty_buffers(page, blocksize,
1434 (1 << BH_Dirty)|(1 << BH_Uptodate));
1438 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1439 * here, and the (potentially unmapped) buffers may become dirty at
1440 * any time. If a buffer becomes dirty here after we've inspected it
1441 * then we just miss that fact, and the page stays dirty.
1443 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1444 * handle that here by just cleaning them.
1447 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1448 head = page_buffers(page);
1452 * Get all the dirty buffers mapped to disk addresses and
1453 * handle any aliases from the underlying blockdev's mapping.
1456 if (block > last_block) {
1458 * mapped buffers outside i_size will occur, because
1459 * this page can be outside i_size when there is a
1460 * truncate in progress.
1463 * The buffer was zeroed by block_write_full_page()
1465 clear_buffer_dirty(bh);
1466 set_buffer_uptodate(bh);
1467 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1468 WARN_ON(bh->b_size != blocksize);
1469 err = btrfs_get_block(inode, block, bh, 0);
1473 if (buffer_new(bh)) {
1474 /* blockdev mappings never come here */
1475 clear_buffer_new(bh);
1478 bh = bh->b_this_page;
1480 } while (bh != head);
1483 if (!buffer_mapped(bh))
1486 * If it's a fully non-blocking write attempt and we cannot
1487 * lock the buffer then redirty the page. Note that this can
1488 * potentially cause a busy-wait loop from pdflush and kswapd
1489 * activity, but those code paths have their own higher-level
1492 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1494 } else if (test_set_buffer_locked(bh)) {
1495 redirty_page_for_writepage(wbc, page);
1498 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1499 mark_buffer_async_write(bh);
1503 } while ((bh = bh->b_this_page) != head);
1506 * The page and its buffers are protected by PageWriteback(), so we can
1507 * drop the bh refcounts early.
1509 BUG_ON(PageWriteback(page));
1510 set_page_writeback(page);
1513 struct buffer_head *next = bh->b_this_page;
1514 if (buffer_async_write(bh)) {
1515 submit_bh(WRITE, bh);
1519 } while (bh != head);
1524 if (nr_underway == 0) {
1526 * The page was marked dirty, but the buffers were
1527 * clean. Someone wrote them back by hand with
1528 * ll_rw_block/submit_bh. A rare case.
1532 if (!buffer_uptodate(bh)) {
1536 bh = bh->b_this_page;
1537 } while (bh != head);
1539 SetPageUptodate(page);
1540 end_page_writeback(page);
1546 * ENOSPC, or some other error. We may already have added some
1547 * blocks to the file, so we need to write these out to avoid
1548 * exposing stale data.
1549 * The page is currently locked and not marked for writeback
1552 /* Recovery: lock and submit the mapped buffers */
1554 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1556 mark_buffer_async_write(bh);
1559 * The buffer may have been set dirty during
1560 * attachment to a dirty page.
1562 clear_buffer_dirty(bh);
1564 } while ((bh = bh->b_this_page) != head);
1566 BUG_ON(PageWriteback(page));
1567 set_page_writeback(page);
1569 struct buffer_head *next = bh->b_this_page;
1570 if (buffer_async_write(bh)) {
1571 clear_buffer_dirty(bh);
1572 submit_bh(WRITE, bh);
1576 } while (bh != head);
1581 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1583 struct inode * const inode = page->mapping->host;
1584 loff_t i_size = i_size_read(inode);
1585 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1589 /* Is the page fully inside i_size? */
1590 if (page->index < end_index)
1591 return __btrfs_write_full_page(inode, page, wbc);
1593 /* Is the page fully outside i_size? (truncate in progress) */
1594 offset = i_size & (PAGE_CACHE_SIZE-1);
1595 if (page->index >= end_index+1 || !offset) {
1597 * The page may have dirty, unmapped buffers. For example,
1598 * they may have been added in ext3_writepage(). Make them
1599 * freeable here, so the page does not leak.
1601 block_invalidatepage(page, 0);
1603 return 0; /* don't care */
1607 * The page straddles i_size. It must be zeroed out on each and every
1608 * writepage invokation because it may be mmapped. "A file is mapped
1609 * in multiples of the page size. For a file that is not a multiple of
1610 * the page size, the remaining memory is zeroed when mapped, and
1611 * writes to that region are not written out to the file."
1613 kaddr = kmap_atomic(page, KM_USER0);
1614 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1615 flush_dcache_page(page);
1616 kunmap_atomic(kaddr, KM_USER0);
1617 return __btrfs_write_full_page(inode, page, wbc);
1620 static void btrfs_truncate(struct inode *inode)
1622 struct btrfs_root *root = BTRFS_I(inode)->root;
1624 struct btrfs_trans_handle *trans;
1626 if (!S_ISREG(inode->i_mode))
1628 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1631 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1633 mutex_lock(&root->fs_info->fs_mutex);
1634 trans = btrfs_start_transaction(root, 1);
1635 btrfs_set_trans_block_group(trans, inode);
1637 /* FIXME, add redo link to tree so we don't leak on crash */
1638 ret = btrfs_truncate_in_trans(trans, root, inode);
1640 btrfs_update_inode(trans, root, inode);
1641 ret = btrfs_end_transaction(trans, root);
1643 mutex_unlock(&root->fs_info->fs_mutex);
1644 btrfs_btree_balance_dirty(root);
1647 int btrfs_commit_write(struct file *file, struct page *page,
1648 unsigned from, unsigned to)
1650 struct inode *inode = page->mapping->host;
1651 struct buffer_head *bh;
1652 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1654 SetPageUptodate(page);
1655 bh = page_buffers(page);
1656 set_buffer_uptodate(bh);
1657 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1658 set_page_dirty(page);
1660 if (pos > inode->i_size) {
1661 i_size_write(inode, pos);
1662 mark_inode_dirty(inode);
1667 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1669 struct btrfs_trans_handle *trans;
1670 struct btrfs_key key;
1671 struct btrfs_root_item root_item;
1672 struct btrfs_inode_item *inode_item;
1673 struct buffer_head *subvol;
1674 struct btrfs_leaf *leaf;
1675 struct btrfs_root *new_root;
1676 struct inode *inode;
1680 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1682 mutex_lock(&root->fs_info->fs_mutex);
1683 trans = btrfs_start_transaction(root, 1);
1686 subvol = btrfs_alloc_free_block(trans, root, 0);
1689 leaf = btrfs_buffer_leaf(subvol);
1690 btrfs_set_header_nritems(&leaf->header, 0);
1691 btrfs_set_header_level(&leaf->header, 0);
1692 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1693 btrfs_set_header_generation(&leaf->header, trans->transid);
1694 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
1695 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1696 sizeof(leaf->header.fsid));
1697 mark_buffer_dirty(subvol);
1699 inode_item = &root_item.inode;
1700 memset(inode_item, 0, sizeof(*inode_item));
1701 btrfs_set_inode_generation(inode_item, 1);
1702 btrfs_set_inode_size(inode_item, 3);
1703 btrfs_set_inode_nlink(inode_item, 1);
1704 btrfs_set_inode_nblocks(inode_item, 1);
1705 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1707 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1708 btrfs_set_root_refs(&root_item, 1);
1712 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1716 btrfs_set_root_dirid(&root_item, new_dirid);
1718 key.objectid = objectid;
1721 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1722 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1727 * insert the directory item
1729 key.offset = (u64)-1;
1730 dir = root->fs_info->sb->s_root->d_inode;
1731 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1732 name, namelen, dir->i_ino, &key,
1736 ret = btrfs_commit_transaction(trans, root);
1739 new_root = btrfs_read_fs_root(root->fs_info, &key);
1742 trans = btrfs_start_transaction(new_root, 1);
1745 inode = btrfs_new_inode(trans, new_root, new_dirid,
1746 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
1747 inode->i_op = &btrfs_dir_inode_operations;
1748 inode->i_fop = &btrfs_dir_file_operations;
1749 new_root->inode = inode;
1751 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1756 ret = btrfs_update_inode(trans, new_root, inode);
1759 ret = btrfs_commit_transaction(trans, new_root);
1762 mutex_unlock(&root->fs_info->fs_mutex);
1763 btrfs_btree_balance_dirty(root);
1767 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1769 struct btrfs_trans_handle *trans;
1770 struct btrfs_key key;
1771 struct btrfs_root_item new_root_item;
1775 if (!root->ref_cows)
1778 mutex_lock(&root->fs_info->fs_mutex);
1779 trans = btrfs_start_transaction(root, 1);
1782 ret = btrfs_update_inode(trans, root, root->inode);
1785 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1789 memcpy(&new_root_item, &root->root_item,
1790 sizeof(new_root_item));
1792 key.objectid = objectid;
1795 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1796 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
1798 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1803 * insert the directory item
1805 key.offset = (u64)-1;
1806 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1808 root->fs_info->sb->s_root->d_inode->i_ino,
1809 &key, BTRFS_FT_DIR);
1813 ret = btrfs_inc_root_ref(trans, root);
1816 ret = btrfs_commit_transaction(trans, root);
1818 mutex_unlock(&root->fs_info->fs_mutex);
1819 btrfs_btree_balance_dirty(root);
1823 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1824 cmd, unsigned long arg)
1826 struct btrfs_root *root = BTRFS_I(inode)->root;
1827 struct btrfs_ioctl_vol_args vol_args;
1829 struct btrfs_dir_item *di;
1831 struct btrfs_path *path;
1835 case BTRFS_IOC_SNAP_CREATE:
1836 if (copy_from_user(&vol_args,
1837 (struct btrfs_ioctl_vol_args __user *)arg,
1840 namelen = strlen(vol_args.name);
1841 if (namelen > BTRFS_VOL_NAME_MAX)
1843 if (strchr(vol_args.name, '/'))
1845 path = btrfs_alloc_path();
1848 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
1849 mutex_lock(&root->fs_info->fs_mutex);
1850 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
1852 vol_args.name, namelen, 0);
1853 mutex_unlock(&root->fs_info->fs_mutex);
1854 btrfs_free_path(path);
1855 if (di && !IS_ERR(di))
1858 if (root == root->fs_info->tree_root)
1859 ret = create_subvol(root, vol_args.name, namelen);
1861 ret = create_snapshot(root, vol_args.name, namelen);
1870 #ifdef CONFIG_COMPAT
1871 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
1874 struct inode *inode = file->f_path.dentry->d_inode;
1877 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
1885 * Called inside transaction, so use GFP_NOFS
1887 struct inode *btrfs_alloc_inode(struct super_block *sb)
1889 struct btrfs_inode *ei;
1891 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1894 return &ei->vfs_inode;
1897 void btrfs_destroy_inode(struct inode *inode)
1899 WARN_ON(!list_empty(&inode->i_dentry));
1900 WARN_ON(inode->i_data.nrpages);
1902 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1905 static void init_once(void * foo, struct kmem_cache * cachep,
1906 unsigned long flags)
1908 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1910 inode_init_once(&ei->vfs_inode);
1913 void btrfs_destroy_cachep(void)
1915 if (btrfs_inode_cachep)
1916 kmem_cache_destroy(btrfs_inode_cachep);
1917 if (btrfs_trans_handle_cachep)
1918 kmem_cache_destroy(btrfs_trans_handle_cachep);
1919 if (btrfs_transaction_cachep)
1920 kmem_cache_destroy(btrfs_transaction_cachep);
1921 if (btrfs_bit_radix_cachep)
1922 kmem_cache_destroy(btrfs_bit_radix_cachep);
1923 if (btrfs_path_cachep)
1924 kmem_cache_destroy(btrfs_path_cachep);
1927 int btrfs_init_cachep(void)
1929 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1930 sizeof(struct btrfs_inode),
1931 0, (SLAB_RECLAIM_ACCOUNT|
1934 if (!btrfs_inode_cachep)
1936 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1937 sizeof(struct btrfs_trans_handle),
1938 0, (SLAB_RECLAIM_ACCOUNT|
1941 if (!btrfs_trans_handle_cachep)
1943 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1944 sizeof(struct btrfs_transaction),
1945 0, (SLAB_RECLAIM_ACCOUNT|
1948 if (!btrfs_transaction_cachep)
1950 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1951 sizeof(struct btrfs_transaction),
1952 0, (SLAB_RECLAIM_ACCOUNT|
1955 if (!btrfs_path_cachep)
1957 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1959 0, (SLAB_RECLAIM_ACCOUNT|
1961 SLAB_DESTROY_BY_RCU),
1963 if (!btrfs_bit_radix_cachep)
1967 btrfs_destroy_cachep();
1971 static int btrfs_getattr(struct vfsmount *mnt,
1972 struct dentry *dentry, struct kstat *stat)
1974 struct inode *inode = dentry->d_inode;
1975 generic_fillattr(inode, stat);
1976 stat->blksize = 256 * 1024;
1980 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
1981 struct inode * new_dir,struct dentry *new_dentry)
1983 struct btrfs_trans_handle *trans;
1984 struct btrfs_root *root = BTRFS_I(old_dir)->root;
1985 struct inode *new_inode = new_dentry->d_inode;
1986 struct inode *old_inode = old_dentry->d_inode;
1987 struct timespec ctime = CURRENT_TIME;
1988 struct btrfs_path *path;
1989 struct btrfs_dir_item *di;
1992 if (S_ISDIR(old_inode->i_mode) && new_inode &&
1993 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1996 mutex_lock(&root->fs_info->fs_mutex);
1997 trans = btrfs_start_transaction(root, 1);
1998 btrfs_set_trans_block_group(trans, new_dir);
1999 path = btrfs_alloc_path();
2005 old_dentry->d_inode->i_nlink++;
2006 old_dir->i_ctime = old_dir->i_mtime = ctime;
2007 new_dir->i_ctime = new_dir->i_mtime = ctime;
2008 old_inode->i_ctime = ctime;
2009 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2010 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2012 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2022 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2023 ret = btrfs_del_item(trans, root, path);
2028 btrfs_release_path(root, path);
2030 di = btrfs_lookup_dir_index_item(trans, root, path,
2042 ret = btrfs_del_item(trans, root, path);
2047 btrfs_release_path(root, path);
2049 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2050 old_inode->i_ino, location,
2057 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2062 new_inode->i_ctime = CURRENT_TIME;
2063 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2066 if (S_ISDIR(new_inode->i_mode))
2067 clear_nlink(new_inode);
2069 drop_nlink(new_inode);
2070 btrfs_update_inode(trans, root, new_inode);
2072 ret = btrfs_add_link(trans, new_dentry, old_inode);
2077 btrfs_free_path(path);
2078 btrfs_end_transaction(trans, root);
2079 mutex_unlock(&root->fs_info->fs_mutex);
2083 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2084 const char *symname)
2086 struct btrfs_trans_handle *trans;
2087 struct btrfs_root *root = BTRFS_I(dir)->root;
2088 struct btrfs_path *path;
2089 struct btrfs_key key;
2090 struct inode *inode;
2097 struct btrfs_file_extent_item *ei;
2099 name_len = strlen(symname) + 1;
2100 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2101 return -ENAMETOOLONG;
2102 mutex_lock(&root->fs_info->fs_mutex);
2103 trans = btrfs_start_transaction(root, 1);
2104 btrfs_set_trans_block_group(trans, dir);
2106 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2112 inode = btrfs_new_inode(trans, root, objectid,
2113 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2114 err = PTR_ERR(inode);
2118 btrfs_set_trans_block_group(trans, inode);
2119 err = btrfs_add_nondir(trans, dentry, inode);
2123 inode->i_mapping->a_ops = &btrfs_aops;
2124 inode->i_fop = &btrfs_file_operations;
2125 inode->i_op = &btrfs_file_inode_operations;
2127 dir->i_sb->s_dirt = 1;
2128 btrfs_update_inode_block_group(trans, inode);
2129 btrfs_update_inode_block_group(trans, dir);
2133 path = btrfs_alloc_path();
2135 key.objectid = inode->i_ino;
2138 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2139 datasize = btrfs_file_extent_calc_inline_size(name_len);
2140 err = btrfs_insert_empty_item(trans, root, path, &key,
2143 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2144 path->slots[0], struct btrfs_file_extent_item);
2145 btrfs_set_file_extent_generation(ei, trans->transid);
2146 btrfs_set_file_extent_type(ei,
2147 BTRFS_FILE_EXTENT_INLINE);
2148 ptr = btrfs_file_extent_inline_start(ei);
2149 btrfs_memcpy(root, path->nodes[0]->b_data,
2150 ptr, symname, name_len);
2151 mark_buffer_dirty(path->nodes[0]);
2152 btrfs_free_path(path);
2153 inode->i_op = &btrfs_symlink_inode_operations;
2154 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2155 inode->i_size = name_len - 1;
2156 btrfs_update_inode(trans, root, inode);
2160 btrfs_end_transaction(trans, root);
2161 mutex_unlock(&root->fs_info->fs_mutex);
2164 inode_dec_link_count(inode);
2167 btrfs_btree_balance_dirty(root);
2171 static struct inode_operations btrfs_dir_inode_operations = {
2172 .lookup = btrfs_lookup,
2173 .create = btrfs_create,
2174 .unlink = btrfs_unlink,
2176 .mkdir = btrfs_mkdir,
2177 .rmdir = btrfs_rmdir,
2178 .rename = btrfs_rename,
2179 .symlink = btrfs_symlink,
2180 .setattr = btrfs_setattr,
2183 static struct inode_operations btrfs_dir_ro_inode_operations = {
2184 .lookup = btrfs_lookup,
2187 static struct file_operations btrfs_dir_file_operations = {
2188 .llseek = generic_file_llseek,
2189 .read = generic_read_dir,
2190 .readdir = btrfs_readdir,
2191 .ioctl = btrfs_ioctl,
2192 #ifdef CONFIG_COMPAT
2193 .compat_ioctl = btrfs_compat_ioctl,
2197 static struct address_space_operations btrfs_aops = {
2198 .readpage = btrfs_readpage,
2199 .writepage = btrfs_writepage,
2200 .sync_page = block_sync_page,
2201 .prepare_write = btrfs_prepare_write,
2202 .commit_write = btrfs_commit_write,
2206 static struct address_space_operations btrfs_symlink_aops = {
2207 .readpage = btrfs_readpage,
2208 .writepage = btrfs_writepage,
2211 static struct inode_operations btrfs_file_inode_operations = {
2212 .truncate = btrfs_truncate,
2213 .getattr = btrfs_getattr,
2214 .setattr = btrfs_setattr,
2217 static struct inode_operations btrfs_symlink_inode_operations = {
2218 .readlink = generic_readlink,
2219 .follow_link = page_follow_link_light,
2220 .put_link = page_put_link,