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>
34 #include <linux/bit_spinlock.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args {
44 struct btrfs_root *root;
47 static struct inode_operations btrfs_dir_inode_operations;
48 static struct inode_operations btrfs_symlink_inode_operations;
49 static struct inode_operations btrfs_dir_ro_inode_operations;
50 static struct inode_operations btrfs_file_inode_operations;
51 static struct address_space_operations btrfs_aops;
52 static struct address_space_operations btrfs_symlink_aops;
53 static struct file_operations btrfs_dir_file_operations;
55 static struct kmem_cache *btrfs_inode_cachep;
56 struct kmem_cache *btrfs_trans_handle_cachep;
57 struct kmem_cache *btrfs_transaction_cachep;
58 struct kmem_cache *btrfs_bit_radix_cachep;
59 struct kmem_cache *btrfs_path_cachep;
62 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
63 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
64 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
65 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
66 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
67 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
68 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
69 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
72 void btrfs_read_locked_inode(struct inode *inode)
74 struct btrfs_path *path;
75 struct btrfs_inode_item *inode_item;
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_key location;
78 u64 alloc_group_block;
81 path = btrfs_alloc_path();
83 mutex_lock(&root->fs_info->fs_mutex);
85 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
86 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
88 btrfs_free_path(path);
91 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
93 struct btrfs_inode_item);
95 inode->i_mode = btrfs_inode_mode(inode_item);
96 inode->i_nlink = btrfs_inode_nlink(inode_item);
97 inode->i_uid = btrfs_inode_uid(inode_item);
98 inode->i_gid = btrfs_inode_gid(inode_item);
99 inode->i_size = btrfs_inode_size(inode_item);
100 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
101 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
102 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
103 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
104 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
106 inode->i_blocks = btrfs_inode_nblocks(inode_item);
107 inode->i_generation = btrfs_inode_generation(inode_item);
108 alloc_group_block = btrfs_inode_block_group(inode_item);
109 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
112 btrfs_free_path(path);
115 mutex_unlock(&root->fs_info->fs_mutex);
117 switch (inode->i_mode & S_IFMT) {
120 init_special_inode(inode, inode->i_mode,
121 btrfs_inode_rdev(inode_item));
125 inode->i_mapping->a_ops = &btrfs_aops;
126 inode->i_fop = &btrfs_file_operations;
127 inode->i_op = &btrfs_file_inode_operations;
130 inode->i_fop = &btrfs_dir_file_operations;
131 if (root == root->fs_info->tree_root)
132 inode->i_op = &btrfs_dir_ro_inode_operations;
134 inode->i_op = &btrfs_dir_inode_operations;
137 inode->i_op = &btrfs_symlink_inode_operations;
138 inode->i_mapping->a_ops = &btrfs_symlink_aops;
144 btrfs_release_path(root, path);
145 btrfs_free_path(path);
146 mutex_unlock(&root->fs_info->fs_mutex);
147 make_bad_inode(inode);
150 static void fill_inode_item(struct btrfs_inode_item *item,
153 btrfs_set_inode_uid(item, inode->i_uid);
154 btrfs_set_inode_gid(item, inode->i_gid);
155 btrfs_set_inode_size(item, inode->i_size);
156 btrfs_set_inode_mode(item, inode->i_mode);
157 btrfs_set_inode_nlink(item, inode->i_nlink);
158 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
159 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
160 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
161 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
162 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
163 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
164 btrfs_set_inode_nblocks(item, inode->i_blocks);
165 btrfs_set_inode_generation(item, inode->i_generation);
166 btrfs_set_inode_block_group(item,
167 BTRFS_I(inode)->block_group->key.objectid);
170 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root,
174 struct btrfs_inode_item *inode_item;
175 struct btrfs_path *path;
178 path = btrfs_alloc_path();
180 ret = btrfs_lookup_inode(trans, root, path,
181 &BTRFS_I(inode)->location, 1);
188 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
190 struct btrfs_inode_item);
192 fill_inode_item(inode_item, inode);
193 btrfs_mark_buffer_dirty(path->nodes[0]);
196 btrfs_release_path(root, path);
197 btrfs_free_path(path);
202 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
203 struct btrfs_root *root,
205 struct dentry *dentry)
207 struct btrfs_path *path;
208 const char *name = dentry->d_name.name;
209 int name_len = dentry->d_name.len;
212 struct btrfs_dir_item *di;
214 path = btrfs_alloc_path();
216 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
226 objectid = btrfs_disk_key_objectid(&di->location);
227 ret = btrfs_delete_one_dir_name(trans, root, path, di);
229 btrfs_release_path(root, path);
231 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
232 objectid, name, name_len, -1);
241 ret = btrfs_delete_one_dir_name(trans, root, path, di);
244 dentry->d_inode->i_ctime = dir->i_ctime;
246 btrfs_free_path(path);
248 dir->i_size -= name_len * 2;
249 btrfs_update_inode(trans, root, dir);
250 drop_nlink(dentry->d_inode);
251 btrfs_update_inode(trans, root, dentry->d_inode);
252 dir->i_sb->s_dirt = 1;
257 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
259 struct btrfs_root *root;
260 struct btrfs_trans_handle *trans;
263 root = BTRFS_I(dir)->root;
264 mutex_lock(&root->fs_info->fs_mutex);
265 trans = btrfs_start_transaction(root, 1);
266 btrfs_set_trans_block_group(trans, dir);
267 ret = btrfs_unlink_trans(trans, root, dir, dentry);
268 btrfs_end_transaction(trans, root);
269 mutex_unlock(&root->fs_info->fs_mutex);
270 btrfs_btree_balance_dirty(root);
274 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
276 struct inode *inode = dentry->d_inode;
279 struct btrfs_root *root = BTRFS_I(dir)->root;
280 struct btrfs_path *path;
281 struct btrfs_key key;
282 struct btrfs_trans_handle *trans;
283 struct btrfs_key found_key;
285 struct btrfs_leaf *leaf;
286 char *goodnames = "..";
288 path = btrfs_alloc_path();
290 mutex_lock(&root->fs_info->fs_mutex);
291 trans = btrfs_start_transaction(root, 1);
292 btrfs_set_trans_block_group(trans, dir);
293 key.objectid = inode->i_ino;
294 key.offset = (u64)-1;
297 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
303 if (path->slots[0] == 0) {
308 leaf = btrfs_buffer_leaf(path->nodes[0]);
309 btrfs_disk_key_to_cpu(&found_key,
310 &leaf->items[path->slots[0]].key);
311 found_type = btrfs_key_type(&found_key);
312 if (found_key.objectid != inode->i_ino) {
316 if ((found_type != BTRFS_DIR_ITEM_KEY &&
317 found_type != BTRFS_DIR_INDEX_KEY) ||
318 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
319 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
323 ret = btrfs_del_item(trans, root, path);
326 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
328 btrfs_release_path(root, path);
331 btrfs_release_path(root, path);
333 /* now the directory is empty */
334 err = btrfs_unlink_trans(trans, root, dir, dentry);
339 btrfs_release_path(root, path);
340 btrfs_free_path(path);
341 mutex_unlock(&root->fs_info->fs_mutex);
342 ret = btrfs_end_transaction(trans, root);
343 btrfs_btree_balance_dirty(root);
349 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
350 struct btrfs_root *root,
353 struct btrfs_path *path;
358 path = btrfs_alloc_path();
360 ret = btrfs_lookup_inode(trans, root, path,
361 &BTRFS_I(inode)->location, -1);
363 ret = btrfs_del_item(trans, root, path);
365 btrfs_free_path(path);
370 * truncates go from a high offset to a low offset. So, walk
371 * from hi to lo in the node and issue readas. Stop when you find
372 * keys from a different objectid
374 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
377 struct btrfs_node *node;
387 node = btrfs_buffer_node(path->nodes[1]);
388 slot = path->slots[1];
391 nritems = btrfs_header_nritems(&node->header);
392 for (i = slot - 1; i >= 0; i--) {
393 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
394 if (item_objectid != objectid)
396 blocknr = btrfs_node_blockptr(node, i);
397 ret = readahead_tree_block(root, blocknr);
404 * this can truncate away extent items, csum items and directory items.
405 * It starts at a high offset and removes keys until it can't find
406 * any higher than i_size.
408 * csum items that cross the new i_size are truncated to the new size
411 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
412 struct btrfs_root *root,
416 struct btrfs_path *path;
417 struct btrfs_key key;
418 struct btrfs_disk_key *found_key;
420 struct btrfs_leaf *leaf;
421 struct btrfs_file_extent_item *fi;
422 u64 extent_start = 0;
423 u64 extent_num_blocks = 0;
428 path = btrfs_alloc_path();
430 /* FIXME, add redo link to tree so we don't leak on crash */
431 key.objectid = inode->i_ino;
432 key.offset = (u64)-1;
435 btrfs_init_path(path);
437 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
442 BUG_ON(path->slots[0] == 0);
445 reada_truncate(root, path, inode->i_ino);
446 leaf = btrfs_buffer_leaf(path->nodes[0]);
447 found_key = &leaf->items[path->slots[0]].key;
448 found_type = btrfs_disk_key_type(found_key);
450 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
452 if (found_type != BTRFS_CSUM_ITEM_KEY &&
453 found_type != BTRFS_DIR_ITEM_KEY &&
454 found_type != BTRFS_DIR_INDEX_KEY &&
455 found_type != BTRFS_EXTENT_DATA_KEY)
458 item_end = btrfs_disk_key_offset(found_key);
459 if (found_type == BTRFS_EXTENT_DATA_KEY) {
460 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
462 struct btrfs_file_extent_item);
463 if (btrfs_file_extent_type(fi) !=
464 BTRFS_FILE_EXTENT_INLINE) {
465 item_end += btrfs_file_extent_num_blocks(fi) <<
469 if (found_type == BTRFS_CSUM_ITEM_KEY) {
470 ret = btrfs_csum_truncate(trans, root, path,
474 if (item_end < inode->i_size) {
476 btrfs_set_key_type(&key, found_type - 1);
481 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
487 /* FIXME, shrink the extent if the ref count is only 1 */
488 if (found_type == BTRFS_EXTENT_DATA_KEY &&
489 btrfs_file_extent_type(fi) !=
490 BTRFS_FILE_EXTENT_INLINE) {
493 u64 orig_num_blocks =
494 btrfs_file_extent_num_blocks(fi);
495 extent_num_blocks = inode->i_size -
496 btrfs_disk_key_offset(found_key) +
498 extent_num_blocks >>= inode->i_blkbits;
499 btrfs_set_file_extent_num_blocks(fi,
501 inode->i_blocks -= (orig_num_blocks -
502 extent_num_blocks) << 3;
503 mark_buffer_dirty(path->nodes[0]);
506 btrfs_file_extent_disk_blocknr(fi);
508 btrfs_file_extent_disk_num_blocks(fi);
509 /* FIXME blocksize != 4096 */
510 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
511 if (extent_start != 0) {
513 inode->i_blocks -= num_dec;
518 ret = btrfs_del_item(trans, root, path);
523 btrfs_release_path(root, path);
525 ret = btrfs_free_extent(trans, root, extent_start,
526 extent_num_blocks, 0);
532 btrfs_release_path(root, path);
533 btrfs_free_path(path);
534 inode->i_sb->s_dirt = 1;
539 * taken from block_truncate_page, but does cow as it zeros out
540 * any bytes left in the last page in the file.
542 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
544 struct inode *inode = mapping->host;
545 unsigned blocksize = 1 << inode->i_blkbits;
546 pgoff_t index = from >> PAGE_CACHE_SHIFT;
547 unsigned offset = from & (PAGE_CACHE_SIZE-1);
551 struct btrfs_root *root = BTRFS_I(inode)->root;
553 struct btrfs_key ins;
554 struct btrfs_trans_handle *trans;
556 if ((offset & (blocksize - 1)) == 0)
560 page = grab_cache_page(mapping, index);
564 if (!PageUptodate(page)) {
565 ret = btrfs_readpage(NULL, page);
567 if (!PageUptodate(page)) {
572 mutex_lock(&root->fs_info->fs_mutex);
573 trans = btrfs_start_transaction(root, 1);
574 btrfs_set_trans_block_group(trans, inode);
576 ret = btrfs_drop_extents(trans, root, inode,
577 page->index << PAGE_CACHE_SHIFT,
578 (page->index + 1) << PAGE_CACHE_SHIFT,
581 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
582 alloc_hint, (u64)-1, &ins, 1);
584 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
585 page->index << PAGE_CACHE_SHIFT,
588 SetPageChecked(page);
590 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
591 flush_dcache_page(page);
592 btrfs_csum_file_block(trans, root, inode->i_ino,
593 page->index << PAGE_CACHE_SHIFT,
594 kaddr, PAGE_CACHE_SIZE);
596 btrfs_end_transaction(trans, root);
597 mutex_unlock(&root->fs_info->fs_mutex);
599 set_page_dirty(page);
601 page_cache_release(page);
606 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
608 struct inode *inode = dentry->d_inode;
611 err = inode_change_ok(inode, attr);
615 if (S_ISREG(inode->i_mode) &&
616 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
617 struct btrfs_trans_handle *trans;
618 struct btrfs_root *root = BTRFS_I(inode)->root;
619 u64 mask = root->blocksize - 1;
620 u64 pos = (inode->i_size + mask) & ~mask;
623 if (attr->ia_size <= pos)
626 btrfs_truncate_page(inode->i_mapping, inode->i_size);
628 hole_size = (attr->ia_size - pos + mask) & ~mask;
629 hole_size >>= inode->i_blkbits;
631 mutex_lock(&root->fs_info->fs_mutex);
632 trans = btrfs_start_transaction(root, 1);
633 btrfs_set_trans_block_group(trans, inode);
634 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
635 pos, 0, 0, hole_size);
637 btrfs_end_transaction(trans, root);
638 mutex_unlock(&root->fs_info->fs_mutex);
641 err = inode_setattr(inode, attr);
645 void btrfs_delete_inode(struct inode *inode)
647 struct btrfs_trans_handle *trans;
648 struct btrfs_root *root = BTRFS_I(inode)->root;
651 truncate_inode_pages(&inode->i_data, 0);
652 if (is_bad_inode(inode)) {
656 mutex_lock(&root->fs_info->fs_mutex);
657 trans = btrfs_start_transaction(root, 1);
658 btrfs_set_trans_block_group(trans, inode);
659 ret = btrfs_truncate_in_trans(trans, root, inode);
661 btrfs_free_inode(trans, root, inode);
662 btrfs_end_transaction(trans, root);
663 mutex_unlock(&root->fs_info->fs_mutex);
664 btrfs_btree_balance_dirty(root);
671 * this returns the key found in the dir entry in the location pointer.
672 * If no dir entries were found, location->objectid is 0.
674 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
675 struct btrfs_key *location)
677 const char *name = dentry->d_name.name;
678 int namelen = dentry->d_name.len;
679 struct btrfs_dir_item *di;
680 struct btrfs_path *path;
681 struct btrfs_root *root = BTRFS_I(dir)->root;
684 path = btrfs_alloc_path();
686 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
688 if (!di || IS_ERR(di)) {
689 location->objectid = 0;
693 btrfs_disk_key_to_cpu(location, &di->location);
695 btrfs_release_path(root, path);
696 btrfs_free_path(path);
701 * when we hit a tree root in a directory, the btrfs part of the inode
702 * needs to be changed to reflect the root directory of the tree root. This
703 * is kind of like crossing a mount point.
705 static int fixup_tree_root_location(struct btrfs_root *root,
706 struct btrfs_key *location,
707 struct btrfs_root **sub_root)
709 struct btrfs_path *path;
710 struct btrfs_root_item *ri;
712 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
714 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
717 path = btrfs_alloc_path();
719 mutex_lock(&root->fs_info->fs_mutex);
721 *sub_root = btrfs_read_fs_root(root->fs_info, location);
722 if (IS_ERR(*sub_root))
723 return PTR_ERR(*sub_root);
725 ri = &(*sub_root)->root_item;
726 location->objectid = btrfs_root_dirid(ri);
728 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
729 location->offset = 0;
731 btrfs_free_path(path);
732 mutex_unlock(&root->fs_info->fs_mutex);
736 static int btrfs_init_locked_inode(struct inode *inode, void *p)
738 struct btrfs_iget_args *args = p;
739 inode->i_ino = args->ino;
740 BTRFS_I(inode)->root = args->root;
744 static int btrfs_find_actor(struct inode *inode, void *opaque)
746 struct btrfs_iget_args *args = opaque;
747 return (args->ino == inode->i_ino &&
748 args->root == BTRFS_I(inode)->root);
751 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
752 struct btrfs_root *root)
755 struct btrfs_iget_args args;
759 inode = iget5_locked(s, objectid, btrfs_find_actor,
760 btrfs_init_locked_inode,
765 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
766 struct nameidata *nd)
768 struct inode * inode;
769 struct btrfs_inode *bi = BTRFS_I(dir);
770 struct btrfs_root *root = bi->root;
771 struct btrfs_root *sub_root = root;
772 struct btrfs_key location;
775 if (dentry->d_name.len > BTRFS_NAME_LEN)
776 return ERR_PTR(-ENAMETOOLONG);
777 mutex_lock(&root->fs_info->fs_mutex);
778 ret = btrfs_inode_by_name(dir, dentry, &location);
779 mutex_unlock(&root->fs_info->fs_mutex);
783 if (location.objectid) {
784 ret = fixup_tree_root_location(root, &location, &sub_root);
788 return ERR_PTR(-ENOENT);
789 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
792 return ERR_PTR(-EACCES);
793 if (inode->i_state & I_NEW) {
794 /* the inode and parent dir are two different roots */
795 if (sub_root != root) {
797 sub_root->inode = inode;
799 BTRFS_I(inode)->root = sub_root;
800 memcpy(&BTRFS_I(inode)->location, &location,
802 btrfs_read_locked_inode(inode);
803 unlock_new_inode(inode);
806 return d_splice_alias(inode, dentry);
810 * readahead one full node of leaves as long as their keys include
811 * the objectid supplied
813 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
816 struct btrfs_node *node;
826 node = btrfs_buffer_node(path->nodes[1]);
827 slot = path->slots[1];
828 nritems = btrfs_header_nritems(&node->header);
829 for (i = slot + 1; i < nritems; i++) {
830 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
831 if (item_objectid != objectid)
833 blocknr = btrfs_node_blockptr(node, i);
834 ret = readahead_tree_block(root, blocknr);
839 static unsigned char btrfs_filetype_table[] = {
840 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
843 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
845 struct inode *inode = filp->f_path.dentry->d_inode;
846 struct btrfs_root *root = BTRFS_I(inode)->root;
847 struct btrfs_item *item;
848 struct btrfs_dir_item *di;
849 struct btrfs_key key;
850 struct btrfs_path *path;
853 struct btrfs_leaf *leaf;
856 unsigned char d_type;
861 int key_type = BTRFS_DIR_INDEX_KEY;
863 /* FIXME, use a real flag for deciding about the key type */
864 if (root->fs_info->tree_root == root)
865 key_type = BTRFS_DIR_ITEM_KEY;
866 mutex_lock(&root->fs_info->fs_mutex);
867 key.objectid = inode->i_ino;
869 btrfs_set_key_type(&key, key_type);
870 key.offset = filp->f_pos;
871 path = btrfs_alloc_path();
872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
876 reada_leaves(root, path, inode->i_ino);
878 leaf = btrfs_buffer_leaf(path->nodes[0]);
879 nritems = btrfs_header_nritems(&leaf->header);
880 slot = path->slots[0];
881 if (advance || slot >= nritems) {
882 if (slot >= nritems -1) {
883 reada_leaves(root, path, inode->i_ino);
884 ret = btrfs_next_leaf(root, path);
887 leaf = btrfs_buffer_leaf(path->nodes[0]);
888 nritems = btrfs_header_nritems(&leaf->header);
889 slot = path->slots[0];
896 item = leaf->items + slot;
897 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
899 if (btrfs_disk_key_type(&item->key) != key_type)
901 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
903 filp->f_pos = btrfs_disk_key_offset(&item->key);
905 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
907 di_total = btrfs_item_size(leaf->items + slot);
908 while(di_cur < di_total) {
909 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
910 over = filldir(dirent, (const char *)(di + 1),
911 btrfs_dir_name_len(di),
912 btrfs_disk_key_offset(&item->key),
913 btrfs_disk_key_objectid(&di->location),
917 di_len = btrfs_dir_name_len(di) + sizeof(*di);
919 di = (struct btrfs_dir_item *)((char *)di + di_len);
926 btrfs_release_path(root, path);
927 btrfs_free_path(path);
928 mutex_unlock(&root->fs_info->fs_mutex);
932 int btrfs_write_inode(struct inode *inode, int wait)
934 struct btrfs_root *root = BTRFS_I(inode)->root;
935 struct btrfs_trans_handle *trans;
939 mutex_lock(&root->fs_info->fs_mutex);
940 trans = btrfs_start_transaction(root, 1);
941 btrfs_set_trans_block_group(trans, inode);
942 ret = btrfs_commit_transaction(trans, root);
943 mutex_unlock(&root->fs_info->fs_mutex);
949 * This is somewhat expense, updating the tree every time the
950 * inode changes. But, it is most likely to find the inode in cache.
951 * FIXME, needs more benchmarking...there are no reasons other than performance
952 * to keep or drop this code.
954 void btrfs_dirty_inode(struct inode *inode)
956 struct btrfs_root *root = BTRFS_I(inode)->root;
957 struct btrfs_trans_handle *trans;
959 mutex_lock(&root->fs_info->fs_mutex);
960 trans = btrfs_start_transaction(root, 1);
961 btrfs_set_trans_block_group(trans, inode);
962 btrfs_update_inode(trans, root, inode);
963 btrfs_end_transaction(trans, root);
964 mutex_unlock(&root->fs_info->fs_mutex);
965 btrfs_btree_balance_dirty(root);
968 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
969 struct btrfs_root *root,
971 struct btrfs_block_group_cache *group,
975 struct btrfs_inode_item inode_item;
976 struct btrfs_key *location;
980 inode = new_inode(root->fs_info->sb);
982 return ERR_PTR(-ENOMEM);
984 BTRFS_I(inode)->root = root;
989 group = btrfs_find_block_group(root, group, 0, 0, owner);
990 BTRFS_I(inode)->block_group = group;
992 inode->i_uid = current->fsuid;
993 inode->i_gid = current->fsgid;
994 inode->i_mode = mode;
995 inode->i_ino = objectid;
997 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
998 fill_inode_item(&inode_item, inode);
999 location = &BTRFS_I(inode)->location;
1000 location->objectid = objectid;
1001 location->flags = 0;
1002 location->offset = 0;
1003 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1005 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1008 insert_inode_hash(inode);
1012 static inline u8 btrfs_inode_type(struct inode *inode)
1014 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1017 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1018 struct dentry *dentry, struct inode *inode)
1021 struct btrfs_key key;
1022 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1023 key.objectid = inode->i_ino;
1025 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1028 ret = btrfs_insert_dir_item(trans, root,
1029 dentry->d_name.name, dentry->d_name.len,
1030 dentry->d_parent->d_inode->i_ino,
1031 &key, btrfs_inode_type(inode));
1033 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
1034 ret = btrfs_update_inode(trans, root,
1035 dentry->d_parent->d_inode);
1040 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1041 struct dentry *dentry, struct inode *inode)
1043 int err = btrfs_add_link(trans, dentry, inode);
1045 d_instantiate(dentry, inode);
1053 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1054 int mode, struct nameidata *nd)
1056 struct btrfs_trans_handle *trans;
1057 struct btrfs_root *root = BTRFS_I(dir)->root;
1058 struct inode *inode;
1063 mutex_lock(&root->fs_info->fs_mutex);
1064 trans = btrfs_start_transaction(root, 1);
1065 btrfs_set_trans_block_group(trans, dir);
1067 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1073 inode = btrfs_new_inode(trans, root, objectid,
1074 BTRFS_I(dir)->block_group, mode);
1075 err = PTR_ERR(inode);
1079 btrfs_set_trans_block_group(trans, inode);
1080 err = btrfs_add_nondir(trans, dentry, inode);
1084 inode->i_mapping->a_ops = &btrfs_aops;
1085 inode->i_fop = &btrfs_file_operations;
1086 inode->i_op = &btrfs_file_inode_operations;
1088 dir->i_sb->s_dirt = 1;
1089 btrfs_update_inode_block_group(trans, inode);
1090 btrfs_update_inode_block_group(trans, dir);
1092 btrfs_end_transaction(trans, root);
1093 mutex_unlock(&root->fs_info->fs_mutex);
1096 inode_dec_link_count(inode);
1099 btrfs_btree_balance_dirty(root);
1103 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1104 struct dentry *dentry)
1106 struct btrfs_trans_handle *trans;
1107 struct btrfs_root *root = BTRFS_I(dir)->root;
1108 struct inode *inode = old_dentry->d_inode;
1112 if (inode->i_nlink == 0)
1116 mutex_lock(&root->fs_info->fs_mutex);
1117 trans = btrfs_start_transaction(root, 1);
1118 btrfs_set_trans_block_group(trans, dir);
1119 atomic_inc(&inode->i_count);
1120 err = btrfs_add_nondir(trans, dentry, inode);
1123 dir->i_sb->s_dirt = 1;
1124 btrfs_update_inode_block_group(trans, dir);
1125 btrfs_update_inode(trans, root, inode);
1127 btrfs_end_transaction(trans, root);
1128 mutex_unlock(&root->fs_info->fs_mutex);
1131 inode_dec_link_count(inode);
1134 btrfs_btree_balance_dirty(root);
1138 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1139 struct btrfs_root *root,
1140 u64 objectid, u64 dirid)
1144 struct btrfs_key key;
1149 key.objectid = objectid;
1152 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1154 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1155 &key, BTRFS_FT_DIR);
1158 key.objectid = dirid;
1159 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1160 &key, BTRFS_FT_DIR);
1167 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1169 struct inode *inode;
1170 struct btrfs_trans_handle *trans;
1171 struct btrfs_root *root = BTRFS_I(dir)->root;
1173 int drop_on_err = 0;
1176 mutex_lock(&root->fs_info->fs_mutex);
1177 trans = btrfs_start_transaction(root, 1);
1178 btrfs_set_trans_block_group(trans, dir);
1179 if (IS_ERR(trans)) {
1180 err = PTR_ERR(trans);
1184 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1190 inode = btrfs_new_inode(trans, root, objectid,
1191 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1192 if (IS_ERR(inode)) {
1193 err = PTR_ERR(inode);
1197 inode->i_op = &btrfs_dir_inode_operations;
1198 inode->i_fop = &btrfs_dir_file_operations;
1199 btrfs_set_trans_block_group(trans, inode);
1201 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1206 err = btrfs_update_inode(trans, root, inode);
1209 err = btrfs_add_link(trans, dentry, inode);
1212 d_instantiate(dentry, inode);
1214 dir->i_sb->s_dirt = 1;
1215 btrfs_update_inode_block_group(trans, inode);
1216 btrfs_update_inode_block_group(trans, dir);
1219 btrfs_end_transaction(trans, root);
1221 mutex_unlock(&root->fs_info->fs_mutex);
1224 btrfs_btree_balance_dirty(root);
1229 * FIBMAP and others want to pass in a fake buffer head. They need to
1230 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1231 * any packed file data into the fake bh
1233 #define BTRFS_GET_BLOCK_NO_CREATE 0
1234 #define BTRFS_GET_BLOCK_CREATE 1
1235 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1238 * FIXME create==1 doe not work.
1240 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1241 struct buffer_head *result, int create)
1246 u64 extent_start = 0;
1248 u64 objectid = inode->i_ino;
1251 struct btrfs_path *path;
1252 struct btrfs_root *root = BTRFS_I(inode)->root;
1253 struct btrfs_file_extent_item *item;
1254 struct btrfs_leaf *leaf;
1255 struct btrfs_disk_key *found_key;
1256 struct btrfs_trans_handle *trans = NULL;
1258 path = btrfs_alloc_path();
1260 if (create & BTRFS_GET_BLOCK_CREATE) {
1262 * danger!, this only works if the page is properly up
1265 trans = btrfs_start_transaction(root, 1);
1270 ret = btrfs_drop_extents(trans, root, inode,
1271 iblock << inode->i_blkbits,
1272 (iblock + 1) << inode->i_blkbits,
1277 ret = btrfs_lookup_file_extent(NULL, root, path,
1279 iblock << inode->i_blkbits, 0);
1286 if (path->slots[0] == 0) {
1287 btrfs_release_path(root, path);
1293 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1294 struct btrfs_file_extent_item);
1295 leaf = btrfs_buffer_leaf(path->nodes[0]);
1296 blocknr = btrfs_file_extent_disk_blocknr(item);
1297 blocknr += btrfs_file_extent_offset(item);
1299 /* are we inside the extent that was found? */
1300 found_key = &leaf->items[path->slots[0]].key;
1301 found_type = btrfs_disk_key_type(found_key);
1302 if (btrfs_disk_key_objectid(found_key) != objectid ||
1303 found_type != BTRFS_EXTENT_DATA_KEY) {
1308 found_type = btrfs_file_extent_type(item);
1309 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1310 if (found_type == BTRFS_FILE_EXTENT_REG) {
1311 extent_start = extent_start >> inode->i_blkbits;
1312 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1314 if (btrfs_file_extent_disk_blocknr(item) == 0)
1316 if (iblock >= extent_start && iblock < extent_end) {
1317 btrfs_map_bh_to_logical(root, result, blocknr +
1318 iblock - extent_start);
1321 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1326 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1330 size = btrfs_file_extent_inline_len(leaf->items +
1332 extent_end = (extent_start + size) >> inode->i_blkbits;
1333 extent_start >>= inode->i_blkbits;
1334 if (iblock < extent_start || iblock > extent_end) {
1337 ptr = btrfs_file_extent_inline_start(item);
1338 map = kmap(result->b_page);
1339 memcpy(map, ptr, size);
1340 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1341 flush_dcache_page(result->b_page);
1342 kunmap(result->b_page);
1343 set_buffer_uptodate(result);
1344 SetPageChecked(result->b_page);
1345 btrfs_map_bh_to_logical(root, result, 0);
1348 if (create & BTRFS_GET_BLOCK_CREATE) {
1349 struct btrfs_key ins;
1350 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1351 1, alloc_hint, (u64)-1,
1354 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1355 iblock << inode->i_blkbits,
1356 ins.objectid, ins.offset,
1359 btrfs_map_bh_to_logical(root, result, ins.objectid);
1363 err = btrfs_end_transaction(trans, root);
1364 btrfs_free_path(path);
1368 int btrfs_get_block(struct inode *inode, sector_t iblock,
1369 struct buffer_head *result, int create)
1372 struct btrfs_root *root = BTRFS_I(inode)->root;
1373 mutex_lock(&root->fs_info->fs_mutex);
1374 err = btrfs_get_block_lock(inode, iblock, result, create);
1375 mutex_unlock(&root->fs_info->fs_mutex);
1379 int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1380 struct buffer_head *result, int create)
1383 struct btrfs_root *root = BTRFS_I(inode)->root;
1384 struct page *page = result->b_page;
1385 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1386 struct btrfs_csum_item *item;
1387 struct btrfs_path *path = NULL;
1389 mutex_lock(&root->fs_info->fs_mutex);
1390 ret = btrfs_get_block_lock(inode, iblock, result, create);
1394 path = btrfs_alloc_path();
1395 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1397 ret = PTR_ERR(item);
1398 /* a csum that isn't present is a preallocated region. */
1399 if (ret == -ENOENT || ret == -EFBIG)
1401 result->b_private = 0;
1404 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1405 printk("get_block_sum file %lu offset %llu csum %X\n", inode->i_ino, (unsigned long long)offset, *(int *)(&item->csum));
1408 btrfs_free_path(path);
1409 mutex_unlock(&root->fs_info->fs_mutex);
1413 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1414 struct buffer_head *result, int create)
1416 struct btrfs_root *root = BTRFS_I(inode)->root;
1417 mutex_lock(&root->fs_info->fs_mutex);
1418 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1419 mutex_unlock(&root->fs_info->fs_mutex);
1423 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1425 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1428 static int btrfs_prepare_write(struct file *file, struct page *page,
1429 unsigned from, unsigned to)
1431 return block_prepare_write(page, from, to, btrfs_get_block);
1434 static void buffer_io_error(struct buffer_head *bh)
1436 char b[BDEVNAME_SIZE];
1438 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1439 bdevname(bh->b_bdev, b),
1440 (unsigned long long)bh->b_blocknr);
1444 * I/O completion handler for block_read_full_page() - pages
1445 * which come unlocked at the end of I/O.
1447 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1449 unsigned long flags;
1450 struct buffer_head *first;
1451 struct buffer_head *tmp;
1453 int page_uptodate = 1;
1454 struct inode *inode;
1457 BUG_ON(!buffer_async_read(bh));
1460 inode = page->mapping->host;
1463 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1464 if (bh->b_private) {
1465 char csum[BTRFS_CRC32_SIZE];
1466 kaddr = kmap_atomic(page, KM_IRQ0);
1467 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1470 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1472 offset = (page->index << PAGE_CACHE_SHIFT) +
1474 printk("btrfs csum failed ino %lu off %llu\n",
1475 page->mapping->host->i_ino,
1476 (unsigned long long)offset);
1477 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1478 flush_dcache_page(page);
1479 printk("bad verify file %lu offset %llu bh_private %lX csum %X\n", inode->i_ino, (unsigned long long)offset, (unsigned long)(bh->b_private), *(int *)csum);
1481 kunmap_atomic(kaddr, KM_IRQ0);
1483 set_buffer_uptodate(bh);
1485 clear_buffer_uptodate(bh);
1486 if (printk_ratelimit())
1487 buffer_io_error(bh);
1492 * Be _very_ careful from here on. Bad things can happen if
1493 * two buffer heads end IO at almost the same time and both
1494 * decide that the page is now completely done.
1496 first = page_buffers(page);
1497 local_irq_save(flags);
1498 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1499 clear_buffer_async_read(bh);
1503 if (!buffer_uptodate(tmp))
1505 if (buffer_async_read(tmp)) {
1506 BUG_ON(!buffer_locked(tmp));
1509 tmp = tmp->b_this_page;
1510 } while (tmp != bh);
1511 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1512 local_irq_restore(flags);
1515 * If none of the buffers had errors and they are all
1516 * uptodate then we can set the page uptodate.
1518 if (page_uptodate && !PageError(page))
1519 SetPageUptodate(page);
1524 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1525 local_irq_restore(flags);
1530 * Generic "read page" function for block devices that have the normal
1531 * get_block functionality. This is most of the block device filesystems.
1532 * Reads the page asynchronously --- the unlock_buffer() and
1533 * set/clear_buffer_uptodate() functions propagate buffer state into the
1534 * page struct once IO has completed.
1536 int btrfs_readpage(struct file *file, struct page *page)
1538 struct inode *inode = page->mapping->host;
1539 sector_t iblock, lblock;
1540 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1541 unsigned int blocksize;
1543 int fully_mapped = 1;
1545 BUG_ON(!PageLocked(page));
1546 blocksize = 1 << inode->i_blkbits;
1547 if (!page_has_buffers(page))
1548 create_empty_buffers(page, blocksize, 0);
1549 head = page_buffers(page);
1551 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1552 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1558 if (buffer_uptodate(bh))
1561 if (!buffer_mapped(bh)) {
1565 if (iblock < lblock) {
1566 WARN_ON(bh->b_size != blocksize);
1567 err = btrfs_get_block_csum(inode, iblock,
1572 if (!buffer_mapped(bh)) {
1573 void *kaddr = kmap_atomic(page, KM_USER0);
1574 memset(kaddr + i * blocksize, 0, blocksize);
1575 flush_dcache_page(page);
1576 kunmap_atomic(kaddr, KM_USER0);
1578 set_buffer_uptodate(bh);
1582 * get_block() might have updated the buffer
1585 if (buffer_uptodate(bh))
1589 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1592 SetPageMappedToDisk(page);
1596 * All buffers are uptodate - we can set the page uptodate
1597 * as well. But not if get_block() returned an error.
1599 if (!PageError(page))
1600 SetPageUptodate(page);
1605 /* Stage two: lock the buffers */
1606 for (i = 0; i < nr; i++) {
1609 bh->b_end_io = btrfs_end_buffer_async_read;
1610 set_buffer_async_read(bh);
1614 * Stage 3: start the IO. Check for uptodateness
1615 * inside the buffer lock in case another process reading
1616 * the underlying blockdev brought it uptodate (the sct fix).
1618 for (i = 0; i < nr; i++) {
1620 if (buffer_uptodate(bh))
1621 btrfs_end_buffer_async_read(bh, 1);
1623 submit_bh(READ, bh);
1629 * Aside from a tiny bit of packed file data handling, this is the
1630 * same as the generic code.
1632 * While block_write_full_page is writing back the dirty buffers under
1633 * the page lock, whoever dirtied the buffers may decide to clean them
1634 * again at any time. We handle that by only looking at the buffer
1635 * state inside lock_buffer().
1637 * If block_write_full_page() is called for regular writeback
1638 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1639 * locked buffer. This only can happen if someone has written the buffer
1640 * directly, with submit_bh(). At the address_space level PageWriteback
1641 * prevents this contention from occurring.
1643 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1644 struct writeback_control *wbc)
1648 sector_t last_block;
1649 struct buffer_head *bh, *head;
1650 const unsigned blocksize = 1 << inode->i_blkbits;
1651 int nr_underway = 0;
1652 struct btrfs_root *root = BTRFS_I(inode)->root;
1654 BUG_ON(!PageLocked(page));
1656 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1658 if (!page_has_buffers(page)) {
1659 create_empty_buffers(page, blocksize,
1660 (1 << BH_Dirty)|(1 << BH_Uptodate));
1664 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1665 * here, and the (potentially unmapped) buffers may become dirty at
1666 * any time. If a buffer becomes dirty here after we've inspected it
1667 * then we just miss that fact, and the page stays dirty.
1669 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1670 * handle that here by just cleaning them.
1673 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1674 head = page_buffers(page);
1678 * Get all the dirty buffers mapped to disk addresses and
1679 * handle any aliases from the underlying blockdev's mapping.
1682 if (block > last_block) {
1684 * mapped buffers outside i_size will occur, because
1685 * this page can be outside i_size when there is a
1686 * truncate in progress.
1689 * The buffer was zeroed by block_write_full_page()
1691 clear_buffer_dirty(bh);
1692 set_buffer_uptodate(bh);
1693 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1694 WARN_ON(bh->b_size != blocksize);
1695 err = btrfs_get_block(inode, block, bh, 0);
1699 if (buffer_new(bh)) {
1700 /* blockdev mappings never come here */
1701 clear_buffer_new(bh);
1704 bh = bh->b_this_page;
1706 } while (bh != head);
1709 if (!buffer_mapped(bh))
1712 * If it's a fully non-blocking write attempt and we cannot
1713 * lock the buffer then redirty the page. Note that this can
1714 * potentially cause a busy-wait loop from pdflush and kswapd
1715 * activity, but those code paths have their own higher-level
1718 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1720 } else if (test_set_buffer_locked(bh)) {
1721 redirty_page_for_writepage(wbc, page);
1724 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1725 struct btrfs_trans_handle *trans;
1727 u64 off = page->index << PAGE_CACHE_SHIFT;
1730 off += bh_offset(bh);
1731 mutex_lock(&root->fs_info->fs_mutex);
1732 trans = btrfs_start_transaction(root, 1);
1733 btrfs_set_trans_block_group(trans, inode);
1735 ret = btrfs_csum_file_block(trans, root, inode->i_ino,
1736 off, kaddr + bh_offset(bh),
1740 ret = btrfs_end_transaction(trans, root);
1742 mutex_unlock(&root->fs_info->fs_mutex);
1743 mark_buffer_async_write(bh);
1747 } while ((bh = bh->b_this_page) != head);
1750 * The page and its buffers are protected by PageWriteback(), so we can
1751 * drop the bh refcounts early.
1753 BUG_ON(PageWriteback(page));
1754 set_page_writeback(page);
1757 struct buffer_head *next = bh->b_this_page;
1758 if (buffer_async_write(bh)) {
1759 submit_bh(WRITE, bh);
1763 } while (bh != head);
1768 if (nr_underway == 0) {
1770 * The page was marked dirty, but the buffers were
1771 * clean. Someone wrote them back by hand with
1772 * ll_rw_block/submit_bh. A rare case.
1776 if (!buffer_uptodate(bh)) {
1780 bh = bh->b_this_page;
1781 } while (bh != head);
1783 SetPageUptodate(page);
1784 end_page_writeback(page);
1790 * ENOSPC, or some other error. We may already have added some
1791 * blocks to the file, so we need to write these out to avoid
1792 * exposing stale data.
1793 * The page is currently locked and not marked for writeback
1796 /* Recovery: lock and submit the mapped buffers */
1798 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1800 mark_buffer_async_write(bh);
1803 * The buffer may have been set dirty during
1804 * attachment to a dirty page.
1806 clear_buffer_dirty(bh);
1808 } while ((bh = bh->b_this_page) != head);
1810 BUG_ON(PageWriteback(page));
1811 set_page_writeback(page);
1813 struct buffer_head *next = bh->b_this_page;
1814 if (buffer_async_write(bh)) {
1815 clear_buffer_dirty(bh);
1816 submit_bh(WRITE, bh);
1820 } while (bh != head);
1825 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1827 struct inode * const inode = page->mapping->host;
1828 loff_t i_size = i_size_read(inode);
1829 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1833 /* Is the page fully inside i_size? */
1834 if (page->index < end_index)
1835 return __btrfs_write_full_page(inode, page, wbc);
1837 /* Is the page fully outside i_size? (truncate in progress) */
1838 offset = i_size & (PAGE_CACHE_SIZE-1);
1839 if (page->index >= end_index+1 || !offset) {
1841 * The page may have dirty, unmapped buffers. For example,
1842 * they may have been added in ext3_writepage(). Make them
1843 * freeable here, so the page does not leak.
1845 block_invalidatepage(page, 0);
1847 return 0; /* don't care */
1851 * The page straddles i_size. It must be zeroed out on each and every
1852 * writepage invokation because it may be mmapped. "A file is mapped
1853 * in multiples of the page size. For a file that is not a multiple of
1854 * the page size, the remaining memory is zeroed when mapped, and
1855 * writes to that region are not written out to the file."
1857 kaddr = kmap_atomic(page, KM_USER0);
1858 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1859 flush_dcache_page(page);
1860 kunmap_atomic(kaddr, KM_USER0);
1861 return __btrfs_write_full_page(inode, page, wbc);
1865 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1866 * called from a page fault handler when a page is first dirtied. Hence we must
1867 * be careful to check for EOF conditions here. We set the page up correctly
1868 * for a written page which means we get ENOSPC checking when writing into
1869 * holes and correct delalloc and unwritten extent mapping on filesystems that
1870 * support these features.
1872 * We are not allowed to take the i_mutex here so we have to play games to
1873 * protect against truncate races as the page could now be beyond EOF. Because
1874 * vmtruncate() writes the inode size before removing pages, once we have the
1875 * page lock we can determine safely if the page is beyond EOF. If it is not
1876 * beyond EOF, then the page is guaranteed safe against truncation until we
1879 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1881 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1887 wait_on_page_writeback(page);
1888 printk("btrfs_page_mkwrite %lu %lu\n", page->mapping->host->i_ino, page->index);
1889 size = i_size_read(inode);
1890 if ((page->mapping != inode->i_mapping) ||
1891 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1892 /* page got truncated out from underneath us */
1896 /* page is wholly or partially inside EOF */
1897 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1898 end = size & ~PAGE_CACHE_MASK;
1900 end = PAGE_CACHE_SIZE;
1902 ret = btrfs_prepare_write(NULL, page, 0, end);
1904 ret = btrfs_commit_write(NULL, page, 0, end);
1911 static void btrfs_truncate(struct inode *inode)
1913 struct btrfs_root *root = BTRFS_I(inode)->root;
1915 struct btrfs_trans_handle *trans;
1917 if (!S_ISREG(inode->i_mode))
1919 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1922 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1924 mutex_lock(&root->fs_info->fs_mutex);
1925 trans = btrfs_start_transaction(root, 1);
1926 btrfs_set_trans_block_group(trans, inode);
1928 /* FIXME, add redo link to tree so we don't leak on crash */
1929 ret = btrfs_truncate_in_trans(trans, root, inode);
1931 btrfs_update_inode(trans, root, inode);
1932 ret = btrfs_end_transaction(trans, root);
1934 mutex_unlock(&root->fs_info->fs_mutex);
1935 btrfs_btree_balance_dirty(root);
1938 int btrfs_commit_write(struct file *file, struct page *page,
1939 unsigned from, unsigned to)
1941 struct inode *inode = page->mapping->host;
1942 struct buffer_head *bh;
1943 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1945 SetPageUptodate(page);
1946 bh = page_buffers(page);
1947 set_buffer_uptodate(bh);
1948 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1949 set_page_dirty(page);
1951 if (pos > inode->i_size) {
1952 i_size_write(inode, pos);
1953 mark_inode_dirty(inode);
1958 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1960 struct btrfs_trans_handle *trans;
1961 struct btrfs_key key;
1962 struct btrfs_root_item root_item;
1963 struct btrfs_inode_item *inode_item;
1964 struct buffer_head *subvol;
1965 struct btrfs_leaf *leaf;
1966 struct btrfs_root *new_root;
1967 struct inode *inode;
1971 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1973 mutex_lock(&root->fs_info->fs_mutex);
1974 trans = btrfs_start_transaction(root, 1);
1977 subvol = btrfs_alloc_free_block(trans, root, 0);
1980 leaf = btrfs_buffer_leaf(subvol);
1981 btrfs_set_header_nritems(&leaf->header, 0);
1982 btrfs_set_header_level(&leaf->header, 0);
1983 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1984 btrfs_set_header_generation(&leaf->header, trans->transid);
1985 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
1986 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1987 sizeof(leaf->header.fsid));
1988 mark_buffer_dirty(subvol);
1990 inode_item = &root_item.inode;
1991 memset(inode_item, 0, sizeof(*inode_item));
1992 btrfs_set_inode_generation(inode_item, 1);
1993 btrfs_set_inode_size(inode_item, 3);
1994 btrfs_set_inode_nlink(inode_item, 1);
1995 btrfs_set_inode_nblocks(inode_item, 1);
1996 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1998 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1999 btrfs_set_root_refs(&root_item, 1);
2003 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2007 btrfs_set_root_dirid(&root_item, new_dirid);
2009 key.objectid = objectid;
2012 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2013 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2018 * insert the directory item
2020 key.offset = (u64)-1;
2021 dir = root->fs_info->sb->s_root->d_inode;
2022 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2023 name, namelen, dir->i_ino, &key,
2027 ret = btrfs_commit_transaction(trans, root);
2030 new_root = btrfs_read_fs_root(root->fs_info, &key);
2033 trans = btrfs_start_transaction(new_root, 1);
2036 inode = btrfs_new_inode(trans, new_root, new_dirid,
2037 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2038 inode->i_op = &btrfs_dir_inode_operations;
2039 inode->i_fop = &btrfs_dir_file_operations;
2040 new_root->inode = inode;
2042 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2047 ret = btrfs_update_inode(trans, new_root, inode);
2050 ret = btrfs_commit_transaction(trans, new_root);
2053 mutex_unlock(&root->fs_info->fs_mutex);
2054 btrfs_btree_balance_dirty(root);
2058 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2060 struct btrfs_trans_handle *trans;
2061 struct btrfs_key key;
2062 struct btrfs_root_item new_root_item;
2066 if (!root->ref_cows)
2069 mutex_lock(&root->fs_info->fs_mutex);
2070 trans = btrfs_start_transaction(root, 1);
2073 ret = btrfs_update_inode(trans, root, root->inode);
2076 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2080 memcpy(&new_root_item, &root->root_item,
2081 sizeof(new_root_item));
2083 key.objectid = objectid;
2086 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2087 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2089 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2094 * insert the directory item
2096 key.offset = (u64)-1;
2097 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2099 root->fs_info->sb->s_root->d_inode->i_ino,
2100 &key, BTRFS_FT_DIR);
2104 ret = btrfs_inc_root_ref(trans, root);
2107 ret = btrfs_commit_transaction(trans, root);
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 btrfs_btree_balance_dirty(root);
2114 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2115 cmd, unsigned long arg)
2117 struct btrfs_root *root = BTRFS_I(inode)->root;
2118 struct btrfs_ioctl_vol_args vol_args;
2120 struct btrfs_dir_item *di;
2122 struct btrfs_path *path;
2126 case BTRFS_IOC_SNAP_CREATE:
2127 if (copy_from_user(&vol_args,
2128 (struct btrfs_ioctl_vol_args __user *)arg,
2131 namelen = strlen(vol_args.name);
2132 if (namelen > BTRFS_VOL_NAME_MAX)
2134 if (strchr(vol_args.name, '/'))
2136 path = btrfs_alloc_path();
2139 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2140 mutex_lock(&root->fs_info->fs_mutex);
2141 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2143 vol_args.name, namelen, 0);
2144 mutex_unlock(&root->fs_info->fs_mutex);
2145 btrfs_free_path(path);
2146 if (di && !IS_ERR(di))
2149 if (root == root->fs_info->tree_root)
2150 ret = create_subvol(root, vol_args.name, namelen);
2152 ret = create_snapshot(root, vol_args.name, namelen);
2161 #ifdef CONFIG_COMPAT
2162 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2165 struct inode *inode = file->f_path.dentry->d_inode;
2168 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2176 * Called inside transaction, so use GFP_NOFS
2178 struct inode *btrfs_alloc_inode(struct super_block *sb)
2180 struct btrfs_inode *ei;
2182 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2185 return &ei->vfs_inode;
2188 void btrfs_destroy_inode(struct inode *inode)
2190 WARN_ON(!list_empty(&inode->i_dentry));
2191 WARN_ON(inode->i_data.nrpages);
2193 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2196 static void init_once(void * foo, struct kmem_cache * cachep,
2197 unsigned long flags)
2199 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2201 inode_init_once(&ei->vfs_inode);
2204 void btrfs_destroy_cachep(void)
2206 if (btrfs_inode_cachep)
2207 kmem_cache_destroy(btrfs_inode_cachep);
2208 if (btrfs_trans_handle_cachep)
2209 kmem_cache_destroy(btrfs_trans_handle_cachep);
2210 if (btrfs_transaction_cachep)
2211 kmem_cache_destroy(btrfs_transaction_cachep);
2212 if (btrfs_bit_radix_cachep)
2213 kmem_cache_destroy(btrfs_bit_radix_cachep);
2214 if (btrfs_path_cachep)
2215 kmem_cache_destroy(btrfs_path_cachep);
2218 int btrfs_init_cachep(void)
2220 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2221 sizeof(struct btrfs_inode),
2222 0, (SLAB_RECLAIM_ACCOUNT|
2225 if (!btrfs_inode_cachep)
2227 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2228 sizeof(struct btrfs_trans_handle),
2229 0, (SLAB_RECLAIM_ACCOUNT|
2232 if (!btrfs_trans_handle_cachep)
2234 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2235 sizeof(struct btrfs_transaction),
2236 0, (SLAB_RECLAIM_ACCOUNT|
2239 if (!btrfs_transaction_cachep)
2241 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2242 sizeof(struct btrfs_transaction),
2243 0, (SLAB_RECLAIM_ACCOUNT|
2246 if (!btrfs_path_cachep)
2248 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2250 0, (SLAB_RECLAIM_ACCOUNT|
2252 SLAB_DESTROY_BY_RCU),
2254 if (!btrfs_bit_radix_cachep)
2258 btrfs_destroy_cachep();
2262 static int btrfs_getattr(struct vfsmount *mnt,
2263 struct dentry *dentry, struct kstat *stat)
2265 struct inode *inode = dentry->d_inode;
2266 generic_fillattr(inode, stat);
2267 stat->blksize = 256 * 1024;
2271 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2272 struct inode * new_dir,struct dentry *new_dentry)
2274 struct btrfs_trans_handle *trans;
2275 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2276 struct inode *new_inode = new_dentry->d_inode;
2277 struct inode *old_inode = old_dentry->d_inode;
2278 struct timespec ctime = CURRENT_TIME;
2279 struct btrfs_path *path;
2280 struct btrfs_dir_item *di;
2283 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2284 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2287 mutex_lock(&root->fs_info->fs_mutex);
2288 trans = btrfs_start_transaction(root, 1);
2289 btrfs_set_trans_block_group(trans, new_dir);
2290 path = btrfs_alloc_path();
2296 old_dentry->d_inode->i_nlink++;
2297 old_dir->i_ctime = old_dir->i_mtime = ctime;
2298 new_dir->i_ctime = new_dir->i_mtime = ctime;
2299 old_inode->i_ctime = ctime;
2300 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2301 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2303 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2313 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2314 ret = btrfs_del_item(trans, root, path);
2319 btrfs_release_path(root, path);
2321 di = btrfs_lookup_dir_index_item(trans, root, path,
2333 ret = btrfs_del_item(trans, root, path);
2338 btrfs_release_path(root, path);
2340 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2341 old_inode->i_ino, location,
2348 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2353 new_inode->i_ctime = CURRENT_TIME;
2354 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2357 if (S_ISDIR(new_inode->i_mode))
2358 clear_nlink(new_inode);
2360 drop_nlink(new_inode);
2361 btrfs_update_inode(trans, root, new_inode);
2363 ret = btrfs_add_link(trans, new_dentry, old_inode);
2368 btrfs_free_path(path);
2369 btrfs_end_transaction(trans, root);
2370 mutex_unlock(&root->fs_info->fs_mutex);
2374 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2375 const char *symname)
2377 struct btrfs_trans_handle *trans;
2378 struct btrfs_root *root = BTRFS_I(dir)->root;
2379 struct btrfs_path *path;
2380 struct btrfs_key key;
2381 struct inode *inode;
2388 struct btrfs_file_extent_item *ei;
2390 name_len = strlen(symname) + 1;
2391 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2392 return -ENAMETOOLONG;
2393 mutex_lock(&root->fs_info->fs_mutex);
2394 trans = btrfs_start_transaction(root, 1);
2395 btrfs_set_trans_block_group(trans, dir);
2397 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2403 inode = btrfs_new_inode(trans, root, objectid,
2404 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2405 err = PTR_ERR(inode);
2409 btrfs_set_trans_block_group(trans, inode);
2410 err = btrfs_add_nondir(trans, dentry, inode);
2414 inode->i_mapping->a_ops = &btrfs_aops;
2415 inode->i_fop = &btrfs_file_operations;
2416 inode->i_op = &btrfs_file_inode_operations;
2418 dir->i_sb->s_dirt = 1;
2419 btrfs_update_inode_block_group(trans, inode);
2420 btrfs_update_inode_block_group(trans, dir);
2424 path = btrfs_alloc_path();
2426 key.objectid = inode->i_ino;
2429 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2430 datasize = btrfs_file_extent_calc_inline_size(name_len);
2431 err = btrfs_insert_empty_item(trans, root, path, &key,
2434 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2435 path->slots[0], struct btrfs_file_extent_item);
2436 btrfs_set_file_extent_generation(ei, trans->transid);
2437 btrfs_set_file_extent_type(ei,
2438 BTRFS_FILE_EXTENT_INLINE);
2439 ptr = btrfs_file_extent_inline_start(ei);
2440 btrfs_memcpy(root, path->nodes[0]->b_data,
2441 ptr, symname, name_len);
2442 mark_buffer_dirty(path->nodes[0]);
2443 btrfs_free_path(path);
2444 inode->i_op = &btrfs_symlink_inode_operations;
2445 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2446 inode->i_size = name_len - 1;
2447 btrfs_update_inode(trans, root, inode);
2451 btrfs_end_transaction(trans, root);
2452 mutex_unlock(&root->fs_info->fs_mutex);
2455 inode_dec_link_count(inode);
2458 btrfs_btree_balance_dirty(root);
2462 static struct inode_operations btrfs_dir_inode_operations = {
2463 .lookup = btrfs_lookup,
2464 .create = btrfs_create,
2465 .unlink = btrfs_unlink,
2467 .mkdir = btrfs_mkdir,
2468 .rmdir = btrfs_rmdir,
2469 .rename = btrfs_rename,
2470 .symlink = btrfs_symlink,
2471 .setattr = btrfs_setattr,
2474 static struct inode_operations btrfs_dir_ro_inode_operations = {
2475 .lookup = btrfs_lookup,
2478 static struct file_operations btrfs_dir_file_operations = {
2479 .llseek = generic_file_llseek,
2480 .read = generic_read_dir,
2481 .readdir = btrfs_readdir,
2482 .ioctl = btrfs_ioctl,
2483 #ifdef CONFIG_COMPAT
2484 .compat_ioctl = btrfs_compat_ioctl,
2488 static struct address_space_operations btrfs_aops = {
2489 .readpage = btrfs_readpage,
2490 .writepage = btrfs_writepage,
2491 .sync_page = block_sync_page,
2492 .prepare_write = btrfs_prepare_write,
2493 .commit_write = btrfs_commit_write,
2497 static struct address_space_operations btrfs_symlink_aops = {
2498 .readpage = btrfs_readpage,
2499 .writepage = btrfs_writepage,
2502 static struct inode_operations btrfs_file_inode_operations = {
2503 .truncate = btrfs_truncate,
2504 .getattr = btrfs_getattr,
2505 .setattr = btrfs_setattr,
2508 static struct inode_operations btrfs_symlink_inode_operations = {
2509 .readlink = generic_readlink,
2510 .follow_link = page_follow_link_light,
2511 .put_link = page_put_link,