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/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.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_special_inode_operations;
51 static struct inode_operations btrfs_file_inode_operations;
52 static struct address_space_operations btrfs_aops;
53 static struct address_space_operations btrfs_symlink_aops;
54 static struct file_operations btrfs_dir_file_operations;
56 static struct kmem_cache *btrfs_inode_cachep;
57 struct kmem_cache *btrfs_trans_handle_cachep;
58 struct kmem_cache *btrfs_transaction_cachep;
59 struct kmem_cache *btrfs_bit_radix_cachep;
60 struct kmem_cache *btrfs_path_cachep;
63 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
64 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
65 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
66 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
67 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
68 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
69 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
70 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73 void btrfs_read_locked_inode(struct inode *inode)
75 struct btrfs_path *path;
76 struct btrfs_inode_item *inode_item;
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_key location;
79 u64 alloc_group_block;
83 path = btrfs_alloc_path();
85 mutex_lock(&root->fs_info->fs_mutex);
87 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
88 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
90 btrfs_free_path(path);
93 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
95 struct btrfs_inode_item);
97 inode->i_mode = btrfs_inode_mode(inode_item);
98 inode->i_nlink = btrfs_inode_nlink(inode_item);
99 inode->i_uid = btrfs_inode_uid(inode_item);
100 inode->i_gid = btrfs_inode_gid(inode_item);
101 inode->i_size = btrfs_inode_size(inode_item);
102 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
103 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
104 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
105 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
106 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
107 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
108 inode->i_blocks = btrfs_inode_nblocks(inode_item);
109 inode->i_generation = btrfs_inode_generation(inode_item);
111 rdev = btrfs_inode_rdev(inode_item);
112 alloc_group_block = btrfs_inode_block_group(inode_item);
113 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
116 btrfs_free_path(path);
119 mutex_unlock(&root->fs_info->fs_mutex);
121 switch (inode->i_mode & S_IFMT) {
123 inode->i_mapping->a_ops = &btrfs_aops;
124 inode->i_fop = &btrfs_file_operations;
125 inode->i_op = &btrfs_file_inode_operations;
128 inode->i_fop = &btrfs_dir_file_operations;
129 if (root == root->fs_info->tree_root)
130 inode->i_op = &btrfs_dir_ro_inode_operations;
132 inode->i_op = &btrfs_dir_inode_operations;
135 inode->i_op = &btrfs_symlink_inode_operations;
136 inode->i_mapping->a_ops = &btrfs_symlink_aops;
139 init_special_inode(inode, inode->i_mode, rdev);
145 btrfs_release_path(root, path);
146 btrfs_free_path(path);
147 mutex_unlock(&root->fs_info->fs_mutex);
148 make_bad_inode(inode);
151 static void fill_inode_item(struct btrfs_inode_item *item,
154 btrfs_set_inode_uid(item, inode->i_uid);
155 btrfs_set_inode_gid(item, inode->i_gid);
156 btrfs_set_inode_size(item, inode->i_size);
157 btrfs_set_inode_mode(item, inode->i_mode);
158 btrfs_set_inode_nlink(item, inode->i_nlink);
159 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
160 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
161 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
162 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
163 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
164 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
165 btrfs_set_inode_nblocks(item, inode->i_blocks);
166 btrfs_set_inode_generation(item, inode->i_generation);
167 btrfs_set_inode_rdev(item, inode->i_rdev);
168 btrfs_set_inode_block_group(item,
169 BTRFS_I(inode)->block_group->key.objectid);
172 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
173 struct btrfs_root *root,
176 struct btrfs_inode_item *inode_item;
177 struct btrfs_path *path;
180 path = btrfs_alloc_path();
182 ret = btrfs_lookup_inode(trans, root, path,
183 &BTRFS_I(inode)->location, 1);
190 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
192 struct btrfs_inode_item);
194 fill_inode_item(inode_item, inode);
195 btrfs_mark_buffer_dirty(path->nodes[0]);
198 btrfs_release_path(root, path);
199 btrfs_free_path(path);
204 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
205 struct btrfs_root *root,
207 struct dentry *dentry)
209 struct btrfs_path *path;
210 const char *name = dentry->d_name.name;
211 int name_len = dentry->d_name.len;
214 struct btrfs_dir_item *di;
216 path = btrfs_alloc_path();
222 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
232 objectid = btrfs_disk_key_objectid(&di->location);
233 ret = btrfs_delete_one_dir_name(trans, root, path, di);
236 btrfs_release_path(root, path);
238 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
239 objectid, name, name_len, -1);
248 ret = btrfs_delete_one_dir_name(trans, root, path, di);
250 dentry->d_inode->i_ctime = dir->i_ctime;
252 btrfs_free_path(path);
254 dir->i_size -= name_len * 2;
255 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
256 btrfs_update_inode(trans, root, dir);
257 drop_nlink(dentry->d_inode);
258 ret = btrfs_update_inode(trans, root, dentry->d_inode);
259 dir->i_sb->s_dirt = 1;
264 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
266 struct btrfs_root *root;
267 struct btrfs_trans_handle *trans;
270 root = BTRFS_I(dir)->root;
271 mutex_lock(&root->fs_info->fs_mutex);
272 trans = btrfs_start_transaction(root, 1);
273 btrfs_set_trans_block_group(trans, dir);
274 ret = btrfs_unlink_trans(trans, root, dir, dentry);
275 btrfs_end_transaction(trans, root);
276 mutex_unlock(&root->fs_info->fs_mutex);
277 btrfs_btree_balance_dirty(root);
281 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
283 struct inode *inode = dentry->d_inode;
286 struct btrfs_root *root = BTRFS_I(dir)->root;
287 struct btrfs_path *path;
288 struct btrfs_key key;
289 struct btrfs_trans_handle *trans;
290 struct btrfs_key found_key;
292 struct btrfs_leaf *leaf;
293 char *goodnames = "..";
295 path = btrfs_alloc_path();
297 mutex_lock(&root->fs_info->fs_mutex);
298 trans = btrfs_start_transaction(root, 1);
299 btrfs_set_trans_block_group(trans, dir);
300 key.objectid = inode->i_ino;
301 key.offset = (u64)-1;
304 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
310 if (path->slots[0] == 0) {
315 leaf = btrfs_buffer_leaf(path->nodes[0]);
316 btrfs_disk_key_to_cpu(&found_key,
317 &leaf->items[path->slots[0]].key);
318 found_type = btrfs_key_type(&found_key);
319 if (found_key.objectid != inode->i_ino) {
323 if ((found_type != BTRFS_DIR_ITEM_KEY &&
324 found_type != BTRFS_DIR_INDEX_KEY) ||
325 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
326 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
330 ret = btrfs_del_item(trans, root, path);
333 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
335 btrfs_release_path(root, path);
338 btrfs_release_path(root, path);
340 /* now the directory is empty */
341 err = btrfs_unlink_trans(trans, root, dir, dentry);
346 btrfs_release_path(root, path);
347 btrfs_free_path(path);
348 mutex_unlock(&root->fs_info->fs_mutex);
349 ret = btrfs_end_transaction(trans, root);
350 btrfs_btree_balance_dirty(root);
356 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root,
360 struct btrfs_path *path;
365 path = btrfs_alloc_path();
367 ret = btrfs_lookup_inode(trans, root, path,
368 &BTRFS_I(inode)->location, -1);
372 ret = btrfs_del_item(trans, root, path);
373 btrfs_free_path(path);
378 * truncates go from a high offset to a low offset. So, walk
379 * from hi to lo in the node and issue readas. Stop when you find
380 * keys from a different objectid
382 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
385 struct btrfs_node *node;
395 node = btrfs_buffer_node(path->nodes[1]);
396 slot = path->slots[1];
399 nritems = btrfs_header_nritems(&node->header);
400 for (i = slot - 1; i >= 0; i--) {
401 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
402 if (item_objectid != objectid)
404 blocknr = btrfs_node_blockptr(node, i);
405 ret = readahead_tree_block(root, blocknr);
412 * this can truncate away extent items, csum items and directory items.
413 * It starts at a high offset and removes keys until it can't find
414 * any higher than i_size.
416 * csum items that cross the new i_size are truncated to the new size
419 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
420 struct btrfs_root *root,
424 struct btrfs_path *path;
425 struct btrfs_key key;
426 struct btrfs_disk_key *found_key;
428 struct btrfs_leaf *leaf;
429 struct btrfs_file_extent_item *fi;
430 u64 extent_start = 0;
431 u64 extent_num_blocks = 0;
436 path = btrfs_alloc_path();
438 /* FIXME, add redo link to tree so we don't leak on crash */
439 key.objectid = inode->i_ino;
440 key.offset = (u64)-1;
443 btrfs_init_path(path);
445 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
450 BUG_ON(path->slots[0] == 0);
453 reada_truncate(root, path, inode->i_ino);
454 leaf = btrfs_buffer_leaf(path->nodes[0]);
455 found_key = &leaf->items[path->slots[0]].key;
456 found_type = btrfs_disk_key_type(found_key);
458 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
460 if (found_type != BTRFS_CSUM_ITEM_KEY &&
461 found_type != BTRFS_DIR_ITEM_KEY &&
462 found_type != BTRFS_DIR_INDEX_KEY &&
463 found_type != BTRFS_EXTENT_DATA_KEY)
466 item_end = btrfs_disk_key_offset(found_key);
467 if (found_type == BTRFS_EXTENT_DATA_KEY) {
468 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
470 struct btrfs_file_extent_item);
471 if (btrfs_file_extent_type(fi) !=
472 BTRFS_FILE_EXTENT_INLINE) {
473 item_end += btrfs_file_extent_num_blocks(fi) <<
477 if (found_type == BTRFS_CSUM_ITEM_KEY) {
478 ret = btrfs_csum_truncate(trans, root, path,
482 if (item_end < inode->i_size) {
484 btrfs_set_key_type(&key, found_type - 1);
489 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
495 /* FIXME, shrink the extent if the ref count is only 1 */
496 if (found_type == BTRFS_EXTENT_DATA_KEY &&
497 btrfs_file_extent_type(fi) !=
498 BTRFS_FILE_EXTENT_INLINE) {
501 u64 orig_num_blocks =
502 btrfs_file_extent_num_blocks(fi);
503 extent_num_blocks = inode->i_size -
504 btrfs_disk_key_offset(found_key) +
506 extent_num_blocks >>= inode->i_blkbits;
507 btrfs_set_file_extent_num_blocks(fi,
509 inode->i_blocks -= (orig_num_blocks -
510 extent_num_blocks) << 3;
511 btrfs_mark_buffer_dirty(path->nodes[0]);
514 btrfs_file_extent_disk_blocknr(fi);
516 btrfs_file_extent_disk_num_blocks(fi);
517 /* FIXME blocksize != 4096 */
518 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
519 if (extent_start != 0) {
521 inode->i_blocks -= num_dec;
526 ret = btrfs_del_item(trans, root, path);
532 btrfs_release_path(root, path);
534 ret = btrfs_free_extent(trans, root, extent_start,
535 extent_num_blocks, 0);
541 btrfs_release_path(root, path);
542 btrfs_free_path(path);
543 inode->i_sb->s_dirt = 1;
548 * taken from block_truncate_page, but does cow as it zeros out
549 * any bytes left in the last page in the file.
551 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
553 struct inode *inode = mapping->host;
554 unsigned blocksize = 1 << inode->i_blkbits;
555 pgoff_t index = from >> PAGE_CACHE_SHIFT;
556 unsigned offset = from & (PAGE_CACHE_SIZE-1);
560 struct btrfs_root *root = BTRFS_I(inode)->root;
562 struct btrfs_key ins;
563 struct btrfs_trans_handle *trans;
565 if ((offset & (blocksize - 1)) == 0)
569 page = grab_cache_page(mapping, index);
573 if (!PageUptodate(page)) {
574 ret = btrfs_readpage(NULL, page);
576 if (!PageUptodate(page)) {
581 mutex_lock(&root->fs_info->fs_mutex);
582 trans = btrfs_start_transaction(root, 1);
583 btrfs_set_trans_block_group(trans, inode);
585 ret = btrfs_drop_extents(trans, root, inode,
586 page->index << PAGE_CACHE_SHIFT,
587 (page->index + 1) << PAGE_CACHE_SHIFT,
591 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
592 alloc_hint, (u64)-1, &ins, 1);
595 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
596 page->index << PAGE_CACHE_SHIFT,
600 SetPageChecked(page);
602 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
603 flush_dcache_page(page);
604 ret = btrfs_csum_file_block(trans, root, inode->i_ino,
605 page->index << PAGE_CACHE_SHIFT,
606 kaddr, PAGE_CACHE_SIZE);
608 btrfs_end_transaction(trans, root);
609 mutex_unlock(&root->fs_info->fs_mutex);
611 set_page_dirty(page);
613 page_cache_release(page);
618 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
620 struct inode *inode = dentry->d_inode;
623 err = inode_change_ok(inode, attr);
627 if (S_ISREG(inode->i_mode) &&
628 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
629 struct btrfs_trans_handle *trans;
630 struct btrfs_root *root = BTRFS_I(inode)->root;
631 u64 mask = root->blocksize - 1;
632 u64 pos = (inode->i_size + mask) & ~mask;
635 if (attr->ia_size <= pos)
638 btrfs_truncate_page(inode->i_mapping, inode->i_size);
640 hole_size = (attr->ia_size - pos + mask) & ~mask;
641 hole_size >>= inode->i_blkbits;
643 mutex_lock(&root->fs_info->fs_mutex);
644 trans = btrfs_start_transaction(root, 1);
645 btrfs_set_trans_block_group(trans, inode);
646 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
647 pos, 0, 0, hole_size);
648 btrfs_end_transaction(trans, root);
649 mutex_unlock(&root->fs_info->fs_mutex);
654 err = inode_setattr(inode, attr);
658 void btrfs_delete_inode(struct inode *inode)
660 struct btrfs_trans_handle *trans;
661 struct btrfs_root *root = BTRFS_I(inode)->root;
664 truncate_inode_pages(&inode->i_data, 0);
665 if (is_bad_inode(inode)) {
669 mutex_lock(&root->fs_info->fs_mutex);
670 trans = btrfs_start_transaction(root, 1);
671 btrfs_set_trans_block_group(trans, inode);
672 ret = btrfs_truncate_in_trans(trans, root, inode);
675 ret = btrfs_free_inode(trans, root, inode);
678 btrfs_end_transaction(trans, root);
679 mutex_unlock(&root->fs_info->fs_mutex);
680 btrfs_btree_balance_dirty(root);
684 btrfs_end_transaction(trans, root);
685 mutex_unlock(&root->fs_info->fs_mutex);
686 btrfs_btree_balance_dirty(root);
692 * this returns the key found in the dir entry in the location pointer.
693 * If no dir entries were found, location->objectid is 0.
695 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
696 struct btrfs_key *location)
698 const char *name = dentry->d_name.name;
699 int namelen = dentry->d_name.len;
700 struct btrfs_dir_item *di;
701 struct btrfs_path *path;
702 struct btrfs_root *root = BTRFS_I(dir)->root;
705 path = btrfs_alloc_path();
707 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
709 if (!di || IS_ERR(di)) {
710 location->objectid = 0;
714 btrfs_disk_key_to_cpu(location, &di->location);
716 btrfs_release_path(root, path);
717 btrfs_free_path(path);
722 * when we hit a tree root in a directory, the btrfs part of the inode
723 * needs to be changed to reflect the root directory of the tree root. This
724 * is kind of like crossing a mount point.
726 static int fixup_tree_root_location(struct btrfs_root *root,
727 struct btrfs_key *location,
728 struct btrfs_root **sub_root)
730 struct btrfs_path *path;
731 struct btrfs_root_item *ri;
733 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
735 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
738 path = btrfs_alloc_path();
740 mutex_lock(&root->fs_info->fs_mutex);
742 *sub_root = btrfs_read_fs_root(root->fs_info, location);
743 if (IS_ERR(*sub_root))
744 return PTR_ERR(*sub_root);
746 ri = &(*sub_root)->root_item;
747 location->objectid = btrfs_root_dirid(ri);
749 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
750 location->offset = 0;
752 btrfs_free_path(path);
753 mutex_unlock(&root->fs_info->fs_mutex);
757 static int btrfs_init_locked_inode(struct inode *inode, void *p)
759 struct btrfs_iget_args *args = p;
760 inode->i_ino = args->ino;
761 BTRFS_I(inode)->root = args->root;
765 static int btrfs_find_actor(struct inode *inode, void *opaque)
767 struct btrfs_iget_args *args = opaque;
768 return (args->ino == inode->i_ino &&
769 args->root == BTRFS_I(inode)->root);
772 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
773 struct btrfs_root *root)
776 struct btrfs_iget_args args;
780 inode = iget5_locked(s, objectid, btrfs_find_actor,
781 btrfs_init_locked_inode,
786 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
787 struct nameidata *nd)
789 struct inode * inode;
790 struct btrfs_inode *bi = BTRFS_I(dir);
791 struct btrfs_root *root = bi->root;
792 struct btrfs_root *sub_root = root;
793 struct btrfs_key location;
796 if (dentry->d_name.len > BTRFS_NAME_LEN)
797 return ERR_PTR(-ENAMETOOLONG);
798 mutex_lock(&root->fs_info->fs_mutex);
799 ret = btrfs_inode_by_name(dir, dentry, &location);
800 mutex_unlock(&root->fs_info->fs_mutex);
804 if (location.objectid) {
805 ret = fixup_tree_root_location(root, &location, &sub_root);
809 return ERR_PTR(-ENOENT);
810 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
813 return ERR_PTR(-EACCES);
814 if (inode->i_state & I_NEW) {
815 /* the inode and parent dir are two different roots */
816 if (sub_root != root) {
818 sub_root->inode = inode;
820 BTRFS_I(inode)->root = sub_root;
821 memcpy(&BTRFS_I(inode)->location, &location,
823 btrfs_read_locked_inode(inode);
824 unlock_new_inode(inode);
827 return d_splice_alias(inode, dentry);
831 * readahead one full node of leaves as long as their keys include
832 * the objectid supplied
834 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
837 struct btrfs_node *node;
847 node = btrfs_buffer_node(path->nodes[1]);
848 slot = path->slots[1];
849 nritems = btrfs_header_nritems(&node->header);
850 for (i = slot + 1; i < nritems; i++) {
851 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
852 if (item_objectid != objectid)
854 blocknr = btrfs_node_blockptr(node, i);
855 ret = readahead_tree_block(root, blocknr);
860 static unsigned char btrfs_filetype_table[] = {
861 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
864 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
866 struct inode *inode = filp->f_path.dentry->d_inode;
867 struct btrfs_root *root = BTRFS_I(inode)->root;
868 struct btrfs_item *item;
869 struct btrfs_dir_item *di;
870 struct btrfs_key key;
871 struct btrfs_path *path;
874 struct btrfs_leaf *leaf;
877 unsigned char d_type;
882 int key_type = BTRFS_DIR_INDEX_KEY;
884 /* FIXME, use a real flag for deciding about the key type */
885 if (root->fs_info->tree_root == root)
886 key_type = BTRFS_DIR_ITEM_KEY;
887 mutex_lock(&root->fs_info->fs_mutex);
888 key.objectid = inode->i_ino;
890 btrfs_set_key_type(&key, key_type);
891 key.offset = filp->f_pos;
892 path = btrfs_alloc_path();
893 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
897 reada_leaves(root, path, inode->i_ino);
899 leaf = btrfs_buffer_leaf(path->nodes[0]);
900 nritems = btrfs_header_nritems(&leaf->header);
901 slot = path->slots[0];
902 if (advance || slot >= nritems) {
903 if (slot >= nritems -1) {
904 reada_leaves(root, path, inode->i_ino);
905 ret = btrfs_next_leaf(root, path);
908 leaf = btrfs_buffer_leaf(path->nodes[0]);
909 nritems = btrfs_header_nritems(&leaf->header);
910 slot = path->slots[0];
917 item = leaf->items + slot;
918 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
920 if (btrfs_disk_key_type(&item->key) != key_type)
922 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
924 filp->f_pos = btrfs_disk_key_offset(&item->key);
926 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
928 di_total = btrfs_item_size(leaf->items + slot);
929 while(di_cur < di_total) {
930 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
931 over = filldir(dirent, (const char *)(di + 1),
932 btrfs_dir_name_len(di),
933 btrfs_disk_key_offset(&item->key),
934 btrfs_disk_key_objectid(&di->location),
938 di_len = btrfs_dir_name_len(di) + sizeof(*di);
940 di = (struct btrfs_dir_item *)((char *)di + di_len);
947 btrfs_release_path(root, path);
948 btrfs_free_path(path);
949 mutex_unlock(&root->fs_info->fs_mutex);
953 int btrfs_write_inode(struct inode *inode, int wait)
955 struct btrfs_root *root = BTRFS_I(inode)->root;
956 struct btrfs_trans_handle *trans;
960 mutex_lock(&root->fs_info->fs_mutex);
961 trans = btrfs_start_transaction(root, 1);
962 btrfs_set_trans_block_group(trans, inode);
963 ret = btrfs_commit_transaction(trans, root);
964 mutex_unlock(&root->fs_info->fs_mutex);
970 * This is somewhat expensive, updating the tree every time the
971 * inode changes. But, it is most likely to find the inode in cache.
972 * FIXME, needs more benchmarking...there are no reasons other than performance
973 * to keep or drop this code.
975 void btrfs_dirty_inode(struct inode *inode)
977 struct btrfs_root *root = BTRFS_I(inode)->root;
978 struct btrfs_trans_handle *trans;
980 mutex_lock(&root->fs_info->fs_mutex);
981 trans = btrfs_start_transaction(root, 1);
982 btrfs_set_trans_block_group(trans, inode);
983 btrfs_update_inode(trans, root, inode);
984 btrfs_end_transaction(trans, root);
985 mutex_unlock(&root->fs_info->fs_mutex);
988 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
989 struct btrfs_root *root,
991 struct btrfs_block_group_cache *group,
995 struct btrfs_inode_item inode_item;
996 struct btrfs_key *location;
1000 inode = new_inode(root->fs_info->sb);
1002 return ERR_PTR(-ENOMEM);
1004 BTRFS_I(inode)->root = root;
1009 group = btrfs_find_block_group(root, group, 0, 0, owner);
1010 BTRFS_I(inode)->block_group = group;
1012 inode->i_uid = current->fsuid;
1013 inode->i_gid = current->fsgid;
1014 inode->i_mode = mode;
1015 inode->i_ino = objectid;
1016 inode->i_blocks = 0;
1017 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1018 fill_inode_item(&inode_item, inode);
1019 location = &BTRFS_I(inode)->location;
1020 location->objectid = objectid;
1021 location->flags = 0;
1022 location->offset = 0;
1023 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1025 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1027 return ERR_PTR(ret);
1028 insert_inode_hash(inode);
1032 static inline u8 btrfs_inode_type(struct inode *inode)
1034 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1037 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1038 struct dentry *dentry, struct inode *inode)
1041 struct btrfs_key key;
1042 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1043 struct inode *parent_inode;
1044 key.objectid = inode->i_ino;
1046 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1049 ret = btrfs_insert_dir_item(trans, root,
1050 dentry->d_name.name, dentry->d_name.len,
1051 dentry->d_parent->d_inode->i_ino,
1052 &key, btrfs_inode_type(inode));
1054 parent_inode = dentry->d_parent->d_inode;
1055 parent_inode->i_size += dentry->d_name.len * 2;
1056 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1057 ret = btrfs_update_inode(trans, root,
1058 dentry->d_parent->d_inode);
1063 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1064 struct dentry *dentry, struct inode *inode)
1066 int err = btrfs_add_link(trans, dentry, inode);
1068 d_instantiate(dentry, inode);
1076 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1077 int mode, dev_t rdev)
1079 struct btrfs_trans_handle *trans;
1080 struct btrfs_root *root = BTRFS_I(dir)->root;
1081 struct inode *inode;
1086 if (!new_valid_dev(rdev))
1089 mutex_lock(&root->fs_info->fs_mutex);
1090 trans = btrfs_start_transaction(root, 1);
1091 btrfs_set_trans_block_group(trans, dir);
1093 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1099 inode = btrfs_new_inode(trans, root, objectid,
1100 BTRFS_I(dir)->block_group, mode);
1101 err = PTR_ERR(inode);
1105 btrfs_set_trans_block_group(trans, inode);
1106 err = btrfs_add_nondir(trans, dentry, inode);
1110 inode->i_op = &btrfs_special_inode_operations;
1111 init_special_inode(inode, inode->i_mode, rdev);
1113 dir->i_sb->s_dirt = 1;
1114 btrfs_update_inode_block_group(trans, inode);
1115 btrfs_update_inode_block_group(trans, dir);
1117 btrfs_end_transaction(trans, root);
1118 mutex_unlock(&root->fs_info->fs_mutex);
1121 inode_dec_link_count(inode);
1124 btrfs_btree_balance_dirty(root);
1128 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1129 int mode, struct nameidata *nd)
1131 struct btrfs_trans_handle *trans;
1132 struct btrfs_root *root = BTRFS_I(dir)->root;
1133 struct inode *inode;
1138 mutex_lock(&root->fs_info->fs_mutex);
1139 trans = btrfs_start_transaction(root, 1);
1140 btrfs_set_trans_block_group(trans, dir);
1142 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1148 inode = btrfs_new_inode(trans, root, objectid,
1149 BTRFS_I(dir)->block_group, mode);
1150 err = PTR_ERR(inode);
1154 btrfs_set_trans_block_group(trans, inode);
1155 err = btrfs_add_nondir(trans, dentry, inode);
1159 inode->i_mapping->a_ops = &btrfs_aops;
1160 inode->i_fop = &btrfs_file_operations;
1161 inode->i_op = &btrfs_file_inode_operations;
1163 dir->i_sb->s_dirt = 1;
1164 btrfs_update_inode_block_group(trans, inode);
1165 btrfs_update_inode_block_group(trans, dir);
1167 btrfs_end_transaction(trans, root);
1168 mutex_unlock(&root->fs_info->fs_mutex);
1171 inode_dec_link_count(inode);
1174 btrfs_btree_balance_dirty(root);
1178 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1179 struct dentry *dentry)
1181 struct btrfs_trans_handle *trans;
1182 struct btrfs_root *root = BTRFS_I(dir)->root;
1183 struct inode *inode = old_dentry->d_inode;
1187 if (inode->i_nlink == 0)
1191 mutex_lock(&root->fs_info->fs_mutex);
1192 trans = btrfs_start_transaction(root, 1);
1193 btrfs_set_trans_block_group(trans, dir);
1194 atomic_inc(&inode->i_count);
1195 err = btrfs_add_nondir(trans, dentry, inode);
1198 dir->i_sb->s_dirt = 1;
1199 btrfs_update_inode_block_group(trans, dir);
1200 err = btrfs_update_inode(trans, root, inode);
1204 btrfs_end_transaction(trans, root);
1205 mutex_unlock(&root->fs_info->fs_mutex);
1208 inode_dec_link_count(inode);
1211 btrfs_btree_balance_dirty(root);
1215 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 u64 objectid, u64 dirid)
1221 struct btrfs_key key;
1226 key.objectid = objectid;
1229 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1231 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1232 &key, BTRFS_FT_DIR);
1235 key.objectid = dirid;
1236 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1237 &key, BTRFS_FT_DIR);
1244 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1246 struct inode *inode;
1247 struct btrfs_trans_handle *trans;
1248 struct btrfs_root *root = BTRFS_I(dir)->root;
1250 int drop_on_err = 0;
1253 mutex_lock(&root->fs_info->fs_mutex);
1254 trans = btrfs_start_transaction(root, 1);
1255 btrfs_set_trans_block_group(trans, dir);
1256 if (IS_ERR(trans)) {
1257 err = PTR_ERR(trans);
1261 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1267 inode = btrfs_new_inode(trans, root, objectid,
1268 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1269 if (IS_ERR(inode)) {
1270 err = PTR_ERR(inode);
1274 inode->i_op = &btrfs_dir_inode_operations;
1275 inode->i_fop = &btrfs_dir_file_operations;
1276 btrfs_set_trans_block_group(trans, inode);
1278 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1283 err = btrfs_update_inode(trans, root, inode);
1286 err = btrfs_add_link(trans, dentry, inode);
1289 d_instantiate(dentry, inode);
1291 dir->i_sb->s_dirt = 1;
1292 btrfs_update_inode_block_group(trans, inode);
1293 btrfs_update_inode_block_group(trans, dir);
1296 btrfs_end_transaction(trans, root);
1298 mutex_unlock(&root->fs_info->fs_mutex);
1301 btrfs_btree_balance_dirty(root);
1306 * FIBMAP and others want to pass in a fake buffer head. They need to
1307 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1308 * any packed file data into the fake bh
1310 #define BTRFS_GET_BLOCK_NO_CREATE 0
1311 #define BTRFS_GET_BLOCK_CREATE 1
1312 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1315 * FIXME create==1 doe not work.
1317 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1318 struct buffer_head *result, int create)
1323 u64 extent_start = 0;
1325 u64 objectid = inode->i_ino;
1328 struct btrfs_path *path;
1329 struct btrfs_root *root = BTRFS_I(inode)->root;
1330 struct btrfs_file_extent_item *item;
1331 struct btrfs_leaf *leaf;
1332 struct btrfs_disk_key *found_key;
1333 struct btrfs_trans_handle *trans = NULL;
1335 path = btrfs_alloc_path();
1337 if (create & BTRFS_GET_BLOCK_CREATE) {
1339 * danger!, this only works if the page is properly up
1342 trans = btrfs_start_transaction(root, 1);
1347 ret = btrfs_drop_extents(trans, root, inode,
1348 iblock << inode->i_blkbits,
1349 (iblock + 1) << inode->i_blkbits,
1354 ret = btrfs_lookup_file_extent(NULL, root, path,
1356 iblock << inode->i_blkbits, 0);
1363 if (path->slots[0] == 0) {
1364 btrfs_release_path(root, path);
1370 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1371 struct btrfs_file_extent_item);
1372 leaf = btrfs_buffer_leaf(path->nodes[0]);
1373 blocknr = btrfs_file_extent_disk_blocknr(item);
1374 blocknr += btrfs_file_extent_offset(item);
1376 /* are we inside the extent that was found? */
1377 found_key = &leaf->items[path->slots[0]].key;
1378 found_type = btrfs_disk_key_type(found_key);
1379 if (btrfs_disk_key_objectid(found_key) != objectid ||
1380 found_type != BTRFS_EXTENT_DATA_KEY) {
1385 found_type = btrfs_file_extent_type(item);
1386 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1387 if (found_type == BTRFS_FILE_EXTENT_REG) {
1388 extent_start = extent_start >> inode->i_blkbits;
1389 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1391 if (btrfs_file_extent_disk_blocknr(item) == 0)
1393 if (iblock >= extent_start && iblock < extent_end) {
1394 btrfs_map_bh_to_logical(root, result, blocknr +
1395 iblock - extent_start);
1398 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1403 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1407 size = btrfs_file_extent_inline_len(leaf->items +
1409 extent_end = (extent_start + size) >> inode->i_blkbits;
1410 extent_start >>= inode->i_blkbits;
1411 if (iblock < extent_start || iblock > extent_end) {
1414 ptr = btrfs_file_extent_inline_start(item);
1415 map = kmap(result->b_page);
1416 memcpy(map, ptr, size);
1417 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1418 flush_dcache_page(result->b_page);
1419 kunmap(result->b_page);
1420 set_buffer_uptodate(result);
1421 SetPageChecked(result->b_page);
1422 btrfs_map_bh_to_logical(root, result, 0);
1425 if (create & BTRFS_GET_BLOCK_CREATE) {
1426 struct btrfs_key ins;
1427 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1428 1, alloc_hint, (u64)-1,
1434 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1435 iblock << inode->i_blkbits,
1436 ins.objectid, ins.offset,
1442 btrfs_map_bh_to_logical(root, result, ins.objectid);
1446 ret = btrfs_end_transaction(trans, root);
1450 btrfs_free_path(path);
1454 int btrfs_get_block(struct inode *inode, sector_t iblock,
1455 struct buffer_head *result, int create)
1458 struct btrfs_root *root = BTRFS_I(inode)->root;
1459 mutex_lock(&root->fs_info->fs_mutex);
1460 err = btrfs_get_block_lock(inode, iblock, result, create);
1461 mutex_unlock(&root->fs_info->fs_mutex);
1465 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1466 struct buffer_head *result, int create)
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct page *page = result->b_page;
1471 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1472 struct btrfs_csum_item *item;
1473 struct btrfs_path *path = NULL;
1475 mutex_lock(&root->fs_info->fs_mutex);
1476 ret = btrfs_get_block_lock(inode, iblock, result, create);
1480 path = btrfs_alloc_path();
1481 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1483 ret = PTR_ERR(item);
1484 /* a csum that isn't present is a preallocated region. */
1485 if (ret == -ENOENT || ret == -EFBIG)
1487 result->b_private = NULL;
1490 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1493 btrfs_free_path(path);
1494 mutex_unlock(&root->fs_info->fs_mutex);
1498 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1499 struct buffer_head *result, int create)
1501 struct btrfs_root *root = BTRFS_I(inode)->root;
1502 mutex_lock(&root->fs_info->fs_mutex);
1503 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1504 mutex_unlock(&root->fs_info->fs_mutex);
1508 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1510 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1513 static int btrfs_prepare_write(struct file *file, struct page *page,
1514 unsigned from, unsigned to)
1516 return block_prepare_write(page, from, to, btrfs_get_block);
1519 static void buffer_io_error(struct buffer_head *bh)
1521 char b[BDEVNAME_SIZE];
1523 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1524 bdevname(bh->b_bdev, b),
1525 (unsigned long long)bh->b_blocknr);
1529 * I/O completion handler for block_read_full_page() - pages
1530 * which come unlocked at the end of I/O.
1532 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1534 unsigned long flags;
1535 struct buffer_head *first;
1536 struct buffer_head *tmp;
1538 int page_uptodate = 1;
1539 struct inode *inode;
1542 BUG_ON(!buffer_async_read(bh));
1545 inode = page->mapping->host;
1548 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1549 if (bh->b_private) {
1550 char csum[BTRFS_CRC32_SIZE];
1551 kaddr = kmap_atomic(page, KM_IRQ0);
1552 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1555 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1557 offset = (page->index << PAGE_CACHE_SHIFT) +
1559 printk("btrfs csum failed ino %lu off %llu\n",
1560 page->mapping->host->i_ino,
1561 (unsigned long long)offset);
1562 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1563 flush_dcache_page(page);
1565 kunmap_atomic(kaddr, KM_IRQ0);
1567 set_buffer_uptodate(bh);
1569 clear_buffer_uptodate(bh);
1570 if (printk_ratelimit())
1571 buffer_io_error(bh);
1576 * Be _very_ careful from here on. Bad things can happen if
1577 * two buffer heads end IO at almost the same time and both
1578 * decide that the page is now completely done.
1580 first = page_buffers(page);
1581 local_irq_save(flags);
1582 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1583 clear_buffer_async_read(bh);
1587 if (!buffer_uptodate(tmp))
1589 if (buffer_async_read(tmp)) {
1590 BUG_ON(!buffer_locked(tmp));
1593 tmp = tmp->b_this_page;
1594 } while (tmp != bh);
1595 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1596 local_irq_restore(flags);
1599 * If none of the buffers had errors and they are all
1600 * uptodate then we can set the page uptodate.
1602 if (page_uptodate && !PageError(page))
1603 SetPageUptodate(page);
1608 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1609 local_irq_restore(flags);
1614 * Generic "read page" function for block devices that have the normal
1615 * get_block functionality. This is most of the block device filesystems.
1616 * Reads the page asynchronously --- the unlock_buffer() and
1617 * set/clear_buffer_uptodate() functions propagate buffer state into the
1618 * page struct once IO has completed.
1620 int btrfs_readpage(struct file *file, struct page *page)
1622 struct inode *inode = page->mapping->host;
1623 sector_t iblock, lblock;
1624 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1625 unsigned int blocksize;
1627 int fully_mapped = 1;
1629 BUG_ON(!PageLocked(page));
1630 blocksize = 1 << inode->i_blkbits;
1631 if (!page_has_buffers(page))
1632 create_empty_buffers(page, blocksize, 0);
1633 head = page_buffers(page);
1635 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1636 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1642 if (buffer_uptodate(bh))
1645 if (!buffer_mapped(bh)) {
1649 if (iblock < lblock) {
1650 WARN_ON(bh->b_size != blocksize);
1651 err = btrfs_get_block_csum(inode, iblock,
1656 if (!buffer_mapped(bh)) {
1657 void *kaddr = kmap_atomic(page, KM_USER0);
1658 memset(kaddr + i * blocksize, 0, blocksize);
1659 flush_dcache_page(page);
1660 kunmap_atomic(kaddr, KM_USER0);
1662 set_buffer_uptodate(bh);
1666 * get_block() might have updated the buffer
1669 if (buffer_uptodate(bh))
1673 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1676 SetPageMappedToDisk(page);
1680 * All buffers are uptodate - we can set the page uptodate
1681 * as well. But not if get_block() returned an error.
1683 if (!PageError(page))
1684 SetPageUptodate(page);
1689 /* Stage two: lock the buffers */
1690 for (i = 0; i < nr; i++) {
1693 bh->b_end_io = btrfs_end_buffer_async_read;
1694 set_buffer_async_read(bh);
1698 * Stage 3: start the IO. Check for uptodateness
1699 * inside the buffer lock in case another process reading
1700 * the underlying blockdev brought it uptodate (the sct fix).
1702 for (i = 0; i < nr; i++) {
1704 if (buffer_uptodate(bh))
1705 btrfs_end_buffer_async_read(bh, 1);
1707 submit_bh(READ, bh);
1713 * Aside from a tiny bit of packed file data handling, this is the
1714 * same as the generic code.
1716 * While block_write_full_page is writing back the dirty buffers under
1717 * the page lock, whoever dirtied the buffers may decide to clean them
1718 * again at any time. We handle that by only looking at the buffer
1719 * state inside lock_buffer().
1721 * If block_write_full_page() is called for regular writeback
1722 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1723 * locked buffer. This only can happen if someone has written the buffer
1724 * directly, with submit_bh(). At the address_space level PageWriteback
1725 * prevents this contention from occurring.
1727 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1728 struct writeback_control *wbc)
1732 sector_t last_block;
1733 struct buffer_head *bh, *head;
1734 const unsigned blocksize = 1 << inode->i_blkbits;
1735 int nr_underway = 0;
1736 struct btrfs_root *root = BTRFS_I(inode)->root;
1738 BUG_ON(!PageLocked(page));
1740 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1742 /* no csumming allowed when from PF_MEMALLOC */
1743 if (current->flags & PF_MEMALLOC) {
1744 redirty_page_for_writepage(wbc, page);
1749 if (!page_has_buffers(page)) {
1750 create_empty_buffers(page, blocksize,
1751 (1 << BH_Dirty)|(1 << BH_Uptodate));
1755 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1756 * here, and the (potentially unmapped) buffers may become dirty at
1757 * any time. If a buffer becomes dirty here after we've inspected it
1758 * then we just miss that fact, and the page stays dirty.
1760 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1761 * handle that here by just cleaning them.
1764 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1765 head = page_buffers(page);
1769 * Get all the dirty buffers mapped to disk addresses and
1770 * handle any aliases from the underlying blockdev's mapping.
1773 if (block > last_block) {
1775 * mapped buffers outside i_size will occur, because
1776 * this page can be outside i_size when there is a
1777 * truncate in progress.
1780 * The buffer was zeroed by block_write_full_page()
1782 clear_buffer_dirty(bh);
1783 set_buffer_uptodate(bh);
1784 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1785 WARN_ON(bh->b_size != blocksize);
1786 err = btrfs_get_block(inode, block, bh, 0);
1790 if (buffer_new(bh)) {
1791 /* blockdev mappings never come here */
1792 clear_buffer_new(bh);
1795 bh = bh->b_this_page;
1797 } while (bh != head);
1800 if (!buffer_mapped(bh))
1803 * If it's a fully non-blocking write attempt and we cannot
1804 * lock the buffer then redirty the page. Note that this can
1805 * potentially cause a busy-wait loop from pdflush and kswapd
1806 * activity, but those code paths have their own higher-level
1809 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1811 } else if (test_set_buffer_locked(bh)) {
1812 redirty_page_for_writepage(wbc, page);
1815 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1816 struct btrfs_trans_handle *trans;
1818 u64 off = page->index << PAGE_CACHE_SHIFT;
1821 off += bh_offset(bh);
1822 mutex_lock(&root->fs_info->fs_mutex);
1823 trans = btrfs_start_transaction(root, 1);
1824 btrfs_set_trans_block_group(trans, inode);
1826 btrfs_csum_file_block(trans, root, inode->i_ino,
1827 off, kaddr + bh_offset(bh),
1830 ret = btrfs_end_transaction(trans, root);
1832 mutex_unlock(&root->fs_info->fs_mutex);
1833 mark_buffer_async_write(bh);
1837 } while ((bh = bh->b_this_page) != head);
1840 * The page and its buffers are protected by PageWriteback(), so we can
1841 * drop the bh refcounts early.
1843 BUG_ON(PageWriteback(page));
1844 set_page_writeback(page);
1847 struct buffer_head *next = bh->b_this_page;
1848 if (buffer_async_write(bh)) {
1849 submit_bh(WRITE, bh);
1853 } while (bh != head);
1858 if (nr_underway == 0) {
1860 * The page was marked dirty, but the buffers were
1861 * clean. Someone wrote them back by hand with
1862 * ll_rw_block/submit_bh. A rare case.
1866 if (!buffer_uptodate(bh)) {
1870 bh = bh->b_this_page;
1871 } while (bh != head);
1873 SetPageUptodate(page);
1874 end_page_writeback(page);
1880 * ENOSPC, or some other error. We may already have added some
1881 * blocks to the file, so we need to write these out to avoid
1882 * exposing stale data.
1883 * The page is currently locked and not marked for writeback
1886 /* Recovery: lock and submit the mapped buffers */
1888 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1890 mark_buffer_async_write(bh);
1893 * The buffer may have been set dirty during
1894 * attachment to a dirty page.
1896 clear_buffer_dirty(bh);
1898 } while ((bh = bh->b_this_page) != head);
1900 BUG_ON(PageWriteback(page));
1901 set_page_writeback(page);
1903 struct buffer_head *next = bh->b_this_page;
1904 if (buffer_async_write(bh)) {
1905 clear_buffer_dirty(bh);
1906 submit_bh(WRITE, bh);
1910 } while (bh != head);
1915 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1917 struct inode * const inode = page->mapping->host;
1918 loff_t i_size = i_size_read(inode);
1919 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1923 /* Is the page fully inside i_size? */
1924 if (page->index < end_index)
1925 return __btrfs_write_full_page(inode, page, wbc);
1927 /* Is the page fully outside i_size? (truncate in progress) */
1928 offset = i_size & (PAGE_CACHE_SIZE-1);
1929 if (page->index >= end_index+1 || !offset) {
1931 * The page may have dirty, unmapped buffers. For example,
1932 * they may have been added in ext3_writepage(). Make them
1933 * freeable here, so the page does not leak.
1935 block_invalidatepage(page, 0);
1937 return 0; /* don't care */
1941 * The page straddles i_size. It must be zeroed out on each and every
1942 * writepage invokation because it may be mmapped. "A file is mapped
1943 * in multiples of the page size. For a file that is not a multiple of
1944 * the page size, the remaining memory is zeroed when mapped, and
1945 * writes to that region are not written out to the file."
1947 kaddr = kmap_atomic(page, KM_USER0);
1948 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1949 flush_dcache_page(page);
1950 kunmap_atomic(kaddr, KM_USER0);
1951 return __btrfs_write_full_page(inode, page, wbc);
1955 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1956 * called from a page fault handler when a page is first dirtied. Hence we must
1957 * be careful to check for EOF conditions here. We set the page up correctly
1958 * for a written page which means we get ENOSPC checking when writing into
1959 * holes and correct delalloc and unwritten extent mapping on filesystems that
1960 * support these features.
1962 * We are not allowed to take the i_mutex here so we have to play games to
1963 * protect against truncate races as the page could now be beyond EOF. Because
1964 * vmtruncate() writes the inode size before removing pages, once we have the
1965 * page lock we can determine safely if the page is beyond EOF. If it is not
1966 * beyond EOF, then the page is guaranteed safe against truncation until we
1969 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1971 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1977 wait_on_page_writeback(page);
1978 size = i_size_read(inode);
1979 if ((page->mapping != inode->i_mapping) ||
1980 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1981 /* page got truncated out from underneath us */
1985 /* page is wholly or partially inside EOF */
1986 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1987 end = size & ~PAGE_CACHE_MASK;
1989 end = PAGE_CACHE_SIZE;
1991 ret = btrfs_prepare_write(NULL, page, 0, end);
1993 ret = btrfs_commit_write(NULL, page, 0, end);
2000 static void btrfs_truncate(struct inode *inode)
2002 struct btrfs_root *root = BTRFS_I(inode)->root;
2004 struct btrfs_trans_handle *trans;
2006 if (!S_ISREG(inode->i_mode))
2008 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2011 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2013 mutex_lock(&root->fs_info->fs_mutex);
2014 trans = btrfs_start_transaction(root, 1);
2015 btrfs_set_trans_block_group(trans, inode);
2017 /* FIXME, add redo link to tree so we don't leak on crash */
2018 ret = btrfs_truncate_in_trans(trans, root, inode);
2019 btrfs_update_inode(trans, root, inode);
2020 ret = btrfs_end_transaction(trans, root);
2022 mutex_unlock(&root->fs_info->fs_mutex);
2023 btrfs_btree_balance_dirty(root);
2026 int btrfs_commit_write(struct file *file, struct page *page,
2027 unsigned from, unsigned to)
2029 struct inode *inode = page->mapping->host;
2030 struct buffer_head *bh;
2031 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2033 SetPageUptodate(page);
2034 bh = page_buffers(page);
2035 set_buffer_uptodate(bh);
2036 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2037 set_page_dirty(page);
2039 if (pos > inode->i_size) {
2040 i_size_write(inode, pos);
2041 mark_inode_dirty(inode);
2046 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2048 struct btrfs_trans_handle *trans;
2049 struct btrfs_key key;
2050 struct btrfs_root_item root_item;
2051 struct btrfs_inode_item *inode_item;
2052 struct buffer_head *subvol;
2053 struct btrfs_leaf *leaf;
2054 struct btrfs_root *new_root;
2055 struct inode *inode;
2060 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2062 mutex_lock(&root->fs_info->fs_mutex);
2063 trans = btrfs_start_transaction(root, 1);
2066 subvol = btrfs_alloc_free_block(trans, root, 0);
2068 return PTR_ERR(subvol);
2069 leaf = btrfs_buffer_leaf(subvol);
2070 btrfs_set_header_nritems(&leaf->header, 0);
2071 btrfs_set_header_level(&leaf->header, 0);
2072 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2073 btrfs_set_header_generation(&leaf->header, trans->transid);
2074 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2075 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2076 sizeof(leaf->header.fsid));
2077 btrfs_mark_buffer_dirty(subvol);
2079 inode_item = &root_item.inode;
2080 memset(inode_item, 0, sizeof(*inode_item));
2081 btrfs_set_inode_generation(inode_item, 1);
2082 btrfs_set_inode_size(inode_item, 3);
2083 btrfs_set_inode_nlink(inode_item, 1);
2084 btrfs_set_inode_nblocks(inode_item, 1);
2085 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2087 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2088 btrfs_set_root_refs(&root_item, 1);
2089 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2090 root_item.drop_level = 0;
2094 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2099 btrfs_set_root_dirid(&root_item, new_dirid);
2101 key.objectid = objectid;
2104 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2105 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2111 * insert the directory item
2113 key.offset = (u64)-1;
2114 dir = root->fs_info->sb->s_root->d_inode;
2115 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2116 name, namelen, dir->i_ino, &key,
2121 ret = btrfs_commit_transaction(trans, root);
2125 new_root = btrfs_read_fs_root(root->fs_info, &key);
2128 trans = btrfs_start_transaction(new_root, 1);
2131 inode = btrfs_new_inode(trans, new_root, new_dirid,
2132 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2135 inode->i_op = &btrfs_dir_inode_operations;
2136 inode->i_fop = &btrfs_dir_file_operations;
2137 new_root->inode = inode;
2139 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2145 ret = btrfs_update_inode(trans, new_root, inode);
2149 err = btrfs_commit_transaction(trans, root);
2153 mutex_unlock(&root->fs_info->fs_mutex);
2154 btrfs_btree_balance_dirty(root);
2158 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2160 struct btrfs_trans_handle *trans;
2161 struct btrfs_key key;
2162 struct btrfs_root_item new_root_item;
2167 if (!root->ref_cows)
2170 mutex_lock(&root->fs_info->fs_mutex);
2171 trans = btrfs_start_transaction(root, 1);
2174 ret = btrfs_update_inode(trans, root, root->inode);
2178 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2183 memcpy(&new_root_item, &root->root_item,
2184 sizeof(new_root_item));
2186 key.objectid = objectid;
2189 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2190 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2192 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2198 * insert the directory item
2200 key.offset = (u64)-1;
2201 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2203 root->fs_info->sb->s_root->d_inode->i_ino,
2204 &key, BTRFS_FT_DIR);
2209 ret = btrfs_inc_root_ref(trans, root);
2214 err = btrfs_commit_transaction(trans, root);
2217 mutex_unlock(&root->fs_info->fs_mutex);
2218 btrfs_btree_balance_dirty(root);
2222 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2223 cmd, unsigned long arg)
2225 struct btrfs_root *root = BTRFS_I(inode)->root;
2226 struct btrfs_ioctl_vol_args vol_args;
2228 struct btrfs_dir_item *di;
2230 struct btrfs_path *path;
2234 case BTRFS_IOC_SNAP_CREATE:
2235 if (copy_from_user(&vol_args,
2236 (struct btrfs_ioctl_vol_args __user *)arg,
2239 namelen = strlen(vol_args.name);
2240 if (namelen > BTRFS_VOL_NAME_MAX)
2242 if (strchr(vol_args.name, '/'))
2244 path = btrfs_alloc_path();
2247 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2248 mutex_lock(&root->fs_info->fs_mutex);
2249 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2251 vol_args.name, namelen, 0);
2252 mutex_unlock(&root->fs_info->fs_mutex);
2253 btrfs_free_path(path);
2254 if (di && !IS_ERR(di))
2259 if (root == root->fs_info->tree_root)
2260 ret = create_subvol(root, vol_args.name, namelen);
2262 ret = create_snapshot(root, vol_args.name, namelen);
2270 #ifdef CONFIG_COMPAT
2271 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2274 struct inode *inode = file->f_path.dentry->d_inode;
2277 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2285 * Called inside transaction, so use GFP_NOFS
2287 struct inode *btrfs_alloc_inode(struct super_block *sb)
2289 struct btrfs_inode *ei;
2291 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2294 return &ei->vfs_inode;
2297 void btrfs_destroy_inode(struct inode *inode)
2299 WARN_ON(!list_empty(&inode->i_dentry));
2300 WARN_ON(inode->i_data.nrpages);
2302 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2305 static void init_once(void * foo, struct kmem_cache * cachep,
2306 unsigned long flags)
2308 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2310 inode_init_once(&ei->vfs_inode);
2313 void btrfs_destroy_cachep(void)
2315 if (btrfs_inode_cachep)
2316 kmem_cache_destroy(btrfs_inode_cachep);
2317 if (btrfs_trans_handle_cachep)
2318 kmem_cache_destroy(btrfs_trans_handle_cachep);
2319 if (btrfs_transaction_cachep)
2320 kmem_cache_destroy(btrfs_transaction_cachep);
2321 if (btrfs_bit_radix_cachep)
2322 kmem_cache_destroy(btrfs_bit_radix_cachep);
2323 if (btrfs_path_cachep)
2324 kmem_cache_destroy(btrfs_path_cachep);
2327 static struct kmem_cache *cache_create(const char *name, size_t size,
2328 unsigned long extra_flags,
2329 void (*ctor)(void *, struct kmem_cache *,
2332 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2333 SLAB_MEM_SPREAD | extra_flags), ctor
2334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2340 int btrfs_init_cachep(void)
2342 btrfs_inode_cachep = cache_create("btrfs_inode_cache",
2343 sizeof(struct btrfs_inode),
2345 if (!btrfs_inode_cachep)
2347 btrfs_trans_handle_cachep = cache_create("btrfs_trans_handle_cache",
2348 sizeof(struct btrfs_trans_handle),
2350 if (!btrfs_trans_handle_cachep)
2352 btrfs_transaction_cachep = cache_create("btrfs_transaction_cache",
2353 sizeof(struct btrfs_transaction),
2355 if (!btrfs_transaction_cachep)
2357 btrfs_path_cachep = cache_create("btrfs_path_cache",
2358 sizeof(struct btrfs_transaction),
2360 if (!btrfs_path_cachep)
2362 btrfs_bit_radix_cachep = cache_create("btrfs_radix", 256,
2363 SLAB_DESTROY_BY_RCU, NULL);
2364 if (!btrfs_bit_radix_cachep)
2368 btrfs_destroy_cachep();
2372 static int btrfs_getattr(struct vfsmount *mnt,
2373 struct dentry *dentry, struct kstat *stat)
2375 struct inode *inode = dentry->d_inode;
2376 generic_fillattr(inode, stat);
2377 stat->blksize = 256 * 1024;
2381 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2382 struct inode * new_dir,struct dentry *new_dentry)
2384 struct btrfs_trans_handle *trans;
2385 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2386 struct inode *new_inode = new_dentry->d_inode;
2387 struct inode *old_inode = old_dentry->d_inode;
2388 struct timespec ctime = CURRENT_TIME;
2389 struct btrfs_path *path;
2390 struct btrfs_dir_item *di;
2393 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2394 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2397 mutex_lock(&root->fs_info->fs_mutex);
2398 trans = btrfs_start_transaction(root, 1);
2399 btrfs_set_trans_block_group(trans, new_dir);
2400 path = btrfs_alloc_path();
2406 old_dentry->d_inode->i_nlink++;
2407 old_dir->i_ctime = old_dir->i_mtime = ctime;
2408 new_dir->i_ctime = new_dir->i_mtime = ctime;
2409 old_inode->i_ctime = ctime;
2410 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2411 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2413 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2423 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2424 ret = btrfs_del_item(trans, root, path);
2428 btrfs_release_path(root, path);
2430 di = btrfs_lookup_dir_index_item(trans, root, path,
2442 ret = btrfs_del_item(trans, root, path);
2446 btrfs_release_path(root, path);
2448 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2449 old_inode->i_ino, location,
2456 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2461 new_inode->i_ctime = CURRENT_TIME;
2462 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2465 if (S_ISDIR(new_inode->i_mode))
2466 clear_nlink(new_inode);
2468 drop_nlink(new_inode);
2469 ret = btrfs_update_inode(trans, root, new_inode);
2473 ret = btrfs_add_link(trans, new_dentry, old_inode);
2478 btrfs_free_path(path);
2479 btrfs_end_transaction(trans, root);
2480 mutex_unlock(&root->fs_info->fs_mutex);
2484 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2485 const char *symname)
2487 struct btrfs_trans_handle *trans;
2488 struct btrfs_root *root = BTRFS_I(dir)->root;
2489 struct btrfs_path *path;
2490 struct btrfs_key key;
2491 struct inode *inode;
2498 struct btrfs_file_extent_item *ei;
2500 name_len = strlen(symname) + 1;
2501 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2502 return -ENAMETOOLONG;
2503 mutex_lock(&root->fs_info->fs_mutex);
2504 trans = btrfs_start_transaction(root, 1);
2505 btrfs_set_trans_block_group(trans, dir);
2507 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2513 inode = btrfs_new_inode(trans, root, objectid,
2514 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2515 err = PTR_ERR(inode);
2519 btrfs_set_trans_block_group(trans, inode);
2520 err = btrfs_add_nondir(trans, dentry, inode);
2524 inode->i_mapping->a_ops = &btrfs_aops;
2525 inode->i_fop = &btrfs_file_operations;
2526 inode->i_op = &btrfs_file_inode_operations;
2528 dir->i_sb->s_dirt = 1;
2529 btrfs_update_inode_block_group(trans, inode);
2530 btrfs_update_inode_block_group(trans, dir);
2534 path = btrfs_alloc_path();
2536 key.objectid = inode->i_ino;
2539 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2540 datasize = btrfs_file_extent_calc_inline_size(name_len);
2541 err = btrfs_insert_empty_item(trans, root, path, &key,
2547 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2548 path->slots[0], struct btrfs_file_extent_item);
2549 btrfs_set_file_extent_generation(ei, trans->transid);
2550 btrfs_set_file_extent_type(ei,
2551 BTRFS_FILE_EXTENT_INLINE);
2552 ptr = btrfs_file_extent_inline_start(ei);
2553 btrfs_memcpy(root, path->nodes[0]->b_data,
2554 ptr, symname, name_len);
2555 btrfs_mark_buffer_dirty(path->nodes[0]);
2556 btrfs_free_path(path);
2557 inode->i_op = &btrfs_symlink_inode_operations;
2558 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2559 inode->i_size = name_len - 1;
2560 err = btrfs_update_inode(trans, root, inode);
2565 btrfs_end_transaction(trans, root);
2566 mutex_unlock(&root->fs_info->fs_mutex);
2568 inode_dec_link_count(inode);
2571 btrfs_btree_balance_dirty(root);
2575 static struct inode_operations btrfs_dir_inode_operations = {
2576 .lookup = btrfs_lookup,
2577 .create = btrfs_create,
2578 .unlink = btrfs_unlink,
2580 .mkdir = btrfs_mkdir,
2581 .rmdir = btrfs_rmdir,
2582 .rename = btrfs_rename,
2583 .symlink = btrfs_symlink,
2584 .setattr = btrfs_setattr,
2585 .mknod = btrfs_mknod,
2588 static struct inode_operations btrfs_dir_ro_inode_operations = {
2589 .lookup = btrfs_lookup,
2592 static struct file_operations btrfs_dir_file_operations = {
2593 .llseek = generic_file_llseek,
2594 .read = generic_read_dir,
2595 .readdir = btrfs_readdir,
2596 .ioctl = btrfs_ioctl,
2597 #ifdef CONFIG_COMPAT
2598 .compat_ioctl = btrfs_compat_ioctl,
2602 static struct address_space_operations btrfs_aops = {
2603 .readpage = btrfs_readpage,
2604 .writepage = btrfs_writepage,
2605 .sync_page = block_sync_page,
2606 .prepare_write = btrfs_prepare_write,
2607 .commit_write = btrfs_commit_write,
2611 static struct address_space_operations btrfs_symlink_aops = {
2612 .readpage = btrfs_readpage,
2613 .writepage = btrfs_writepage,
2616 static struct inode_operations btrfs_file_inode_operations = {
2617 .truncate = btrfs_truncate,
2618 .getattr = btrfs_getattr,
2619 .setattr = btrfs_setattr,
2622 static struct inode_operations btrfs_special_inode_operations = {
2623 .getattr = btrfs_getattr,
2624 .setattr = btrfs_setattr,
2627 static struct inode_operations btrfs_symlink_inode_operations = {
2628 .readlink = generic_readlink,
2629 .follow_link = page_follow_link_light,
2630 .put_link = page_put_link,