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>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
41 struct btrfs_iget_args {
43 struct btrfs_root *root;
46 static struct inode_operations btrfs_dir_inode_operations;
47 static struct inode_operations btrfs_symlink_inode_operations;
48 static struct inode_operations btrfs_dir_ro_inode_operations;
49 static struct inode_operations btrfs_special_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;
82 path = btrfs_alloc_path();
84 mutex_lock(&root->fs_info->fs_mutex);
86 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
87 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
89 btrfs_free_path(path);
92 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
94 struct btrfs_inode_item);
96 inode->i_mode = btrfs_inode_mode(inode_item);
97 inode->i_nlink = btrfs_inode_nlink(inode_item);
98 inode->i_uid = btrfs_inode_uid(inode_item);
99 inode->i_gid = btrfs_inode_gid(inode_item);
100 inode->i_size = btrfs_inode_size(inode_item);
101 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
102 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
103 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
104 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
105 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
106 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
107 inode->i_blocks = btrfs_inode_nblocks(inode_item);
108 inode->i_generation = btrfs_inode_generation(inode_item);
110 rdev = btrfs_inode_rdev(inode_item);
111 alloc_group_block = btrfs_inode_block_group(inode_item);
112 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
115 btrfs_free_path(path);
118 mutex_unlock(&root->fs_info->fs_mutex);
120 switch (inode->i_mode & S_IFMT) {
122 inode->i_mapping->a_ops = &btrfs_aops;
123 inode->i_fop = &btrfs_file_operations;
124 inode->i_op = &btrfs_file_inode_operations;
127 inode->i_fop = &btrfs_dir_file_operations;
128 if (root == root->fs_info->tree_root)
129 inode->i_op = &btrfs_dir_ro_inode_operations;
131 inode->i_op = &btrfs_dir_inode_operations;
134 inode->i_op = &btrfs_symlink_inode_operations;
135 inode->i_mapping->a_ops = &btrfs_symlink_aops;
138 init_special_inode(inode, inode->i_mode, rdev);
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_rdev(item, inode->i_rdev);
167 btrfs_set_inode_block_group(item,
168 BTRFS_I(inode)->block_group->key.objectid);
171 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root,
175 struct btrfs_inode_item *inode_item;
176 struct btrfs_path *path;
179 path = btrfs_alloc_path();
181 ret = btrfs_lookup_inode(trans, root, path,
182 &BTRFS_I(inode)->location, 1);
189 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
191 struct btrfs_inode_item);
193 fill_inode_item(inode_item, inode);
194 btrfs_mark_buffer_dirty(path->nodes[0]);
197 btrfs_release_path(root, path);
198 btrfs_free_path(path);
203 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
204 struct btrfs_root *root,
206 struct dentry *dentry)
208 struct btrfs_path *path;
209 const char *name = dentry->d_name.name;
210 int name_len = dentry->d_name.len;
213 struct btrfs_dir_item *di;
215 path = btrfs_alloc_path();
221 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
231 objectid = btrfs_disk_key_objectid(&di->location);
232 ret = btrfs_delete_one_dir_name(trans, root, path, di);
235 btrfs_release_path(root, path);
237 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
238 objectid, name, name_len, -1);
247 ret = btrfs_delete_one_dir_name(trans, root, path, di);
249 dentry->d_inode->i_ctime = dir->i_ctime;
251 btrfs_free_path(path);
253 dir->i_size -= name_len * 2;
254 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
255 btrfs_update_inode(trans, root, dir);
256 drop_nlink(dentry->d_inode);
257 ret = btrfs_update_inode(trans, root, dentry->d_inode);
258 dir->i_sb->s_dirt = 1;
263 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
265 struct btrfs_root *root;
266 struct btrfs_trans_handle *trans;
269 root = BTRFS_I(dir)->root;
270 mutex_lock(&root->fs_info->fs_mutex);
271 trans = btrfs_start_transaction(root, 1);
272 btrfs_set_trans_block_group(trans, dir);
273 ret = btrfs_unlink_trans(trans, root, dir, dentry);
274 btrfs_end_transaction(trans, root);
275 mutex_unlock(&root->fs_info->fs_mutex);
276 btrfs_btree_balance_dirty(root);
280 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
282 struct inode *inode = dentry->d_inode;
285 struct btrfs_root *root = BTRFS_I(dir)->root;
286 struct btrfs_path *path;
287 struct btrfs_key key;
288 struct btrfs_trans_handle *trans;
289 struct btrfs_key found_key;
291 struct btrfs_leaf *leaf;
292 char *goodnames = "..";
294 path = btrfs_alloc_path();
296 mutex_lock(&root->fs_info->fs_mutex);
297 trans = btrfs_start_transaction(root, 1);
298 btrfs_set_trans_block_group(trans, dir);
299 key.objectid = inode->i_ino;
300 key.offset = (u64)-1;
303 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
309 if (path->slots[0] == 0) {
314 leaf = btrfs_buffer_leaf(path->nodes[0]);
315 btrfs_disk_key_to_cpu(&found_key,
316 &leaf->items[path->slots[0]].key);
317 found_type = btrfs_key_type(&found_key);
318 if (found_key.objectid != inode->i_ino) {
322 if ((found_type != BTRFS_DIR_ITEM_KEY &&
323 found_type != BTRFS_DIR_INDEX_KEY) ||
324 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
325 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
329 ret = btrfs_del_item(trans, root, path);
332 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
334 btrfs_release_path(root, path);
337 btrfs_release_path(root, path);
339 /* now the directory is empty */
340 err = btrfs_unlink_trans(trans, root, dir, dentry);
345 btrfs_release_path(root, path);
346 btrfs_free_path(path);
347 mutex_unlock(&root->fs_info->fs_mutex);
348 ret = btrfs_end_transaction(trans, root);
349 btrfs_btree_balance_dirty(root);
355 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root,
359 struct btrfs_path *path;
364 path = btrfs_alloc_path();
366 ret = btrfs_lookup_inode(trans, root, path,
367 &BTRFS_I(inode)->location, -1);
371 ret = btrfs_del_item(trans, root, path);
372 btrfs_free_path(path);
377 * truncates go from a high offset to a low offset. So, walk
378 * from hi to lo in the node and issue readas. Stop when you find
379 * keys from a different objectid
381 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
384 struct btrfs_node *node;
394 node = btrfs_buffer_node(path->nodes[1]);
395 slot = path->slots[1];
398 nritems = btrfs_header_nritems(&node->header);
399 for (i = slot - 1; i >= 0; i--) {
400 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
401 if (item_objectid != objectid)
403 blocknr = btrfs_node_blockptr(node, i);
404 ret = readahead_tree_block(root, blocknr);
411 * this can truncate away extent items, csum items and directory items.
412 * It starts at a high offset and removes keys until it can't find
413 * any higher than i_size.
415 * csum items that cross the new i_size are truncated to the new size
418 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
419 struct btrfs_root *root,
423 struct btrfs_path *path;
424 struct btrfs_key key;
425 struct btrfs_disk_key *found_key;
427 struct btrfs_leaf *leaf;
428 struct btrfs_file_extent_item *fi;
429 u64 extent_start = 0;
430 u64 extent_num_blocks = 0;
435 path = btrfs_alloc_path();
437 /* FIXME, add redo link to tree so we don't leak on crash */
438 key.objectid = inode->i_ino;
439 key.offset = (u64)-1;
442 btrfs_init_path(path);
444 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
449 BUG_ON(path->slots[0] == 0);
452 reada_truncate(root, path, inode->i_ino);
453 leaf = btrfs_buffer_leaf(path->nodes[0]);
454 found_key = &leaf->items[path->slots[0]].key;
455 found_type = btrfs_disk_key_type(found_key);
457 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
459 if (found_type != BTRFS_CSUM_ITEM_KEY &&
460 found_type != BTRFS_DIR_ITEM_KEY &&
461 found_type != BTRFS_DIR_INDEX_KEY &&
462 found_type != BTRFS_EXTENT_DATA_KEY)
465 item_end = btrfs_disk_key_offset(found_key);
466 if (found_type == BTRFS_EXTENT_DATA_KEY) {
467 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
469 struct btrfs_file_extent_item);
470 if (btrfs_file_extent_type(fi) !=
471 BTRFS_FILE_EXTENT_INLINE) {
472 item_end += btrfs_file_extent_num_blocks(fi) <<
476 if (found_type == BTRFS_CSUM_ITEM_KEY) {
477 ret = btrfs_csum_truncate(trans, root, path,
481 if (item_end < inode->i_size) {
483 btrfs_set_key_type(&key, found_type - 1);
488 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
494 /* FIXME, shrink the extent if the ref count is only 1 */
495 if (found_type == BTRFS_EXTENT_DATA_KEY &&
496 btrfs_file_extent_type(fi) !=
497 BTRFS_FILE_EXTENT_INLINE) {
500 u64 orig_num_blocks =
501 btrfs_file_extent_num_blocks(fi);
502 extent_num_blocks = inode->i_size -
503 btrfs_disk_key_offset(found_key) +
505 extent_num_blocks >>= inode->i_blkbits;
506 btrfs_set_file_extent_num_blocks(fi,
508 inode->i_blocks -= (orig_num_blocks -
509 extent_num_blocks) << 3;
510 btrfs_mark_buffer_dirty(path->nodes[0]);
513 btrfs_file_extent_disk_blocknr(fi);
515 btrfs_file_extent_disk_num_blocks(fi);
516 /* FIXME blocksize != 4096 */
517 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
518 if (extent_start != 0) {
520 inode->i_blocks -= num_dec;
525 ret = btrfs_del_item(trans, root, path);
531 btrfs_release_path(root, path);
533 ret = btrfs_free_extent(trans, root, extent_start,
534 extent_num_blocks, 0);
540 btrfs_release_path(root, path);
541 btrfs_free_path(path);
542 inode->i_sb->s_dirt = 1;
547 * taken from block_truncate_page, but does cow as it zeros out
548 * any bytes left in the last page in the file.
550 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
552 struct inode *inode = mapping->host;
553 unsigned blocksize = 1 << inode->i_blkbits;
554 pgoff_t index = from >> PAGE_CACHE_SHIFT;
555 unsigned offset = from & (PAGE_CACHE_SIZE-1);
559 struct btrfs_root *root = BTRFS_I(inode)->root;
561 struct btrfs_key ins;
562 struct btrfs_trans_handle *trans;
564 if ((offset & (blocksize - 1)) == 0)
568 page = grab_cache_page(mapping, index);
572 if (!PageUptodate(page)) {
573 ret = btrfs_readpage(NULL, page);
575 if (!PageUptodate(page)) {
580 mutex_lock(&root->fs_info->fs_mutex);
581 trans = btrfs_start_transaction(root, 1);
582 btrfs_set_trans_block_group(trans, inode);
584 ret = btrfs_drop_extents(trans, root, inode,
585 page->index << PAGE_CACHE_SHIFT,
586 (page->index + 1) << PAGE_CACHE_SHIFT,
590 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
591 alloc_hint, (u64)-1, &ins, 1);
594 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
595 page->index << PAGE_CACHE_SHIFT,
599 SetPageChecked(page);
601 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
602 flush_dcache_page(page);
603 ret = btrfs_csum_file_block(trans, root, inode->i_ino,
604 page->index << PAGE_CACHE_SHIFT,
605 kaddr, PAGE_CACHE_SIZE);
607 btrfs_end_transaction(trans, root);
608 mutex_unlock(&root->fs_info->fs_mutex);
610 set_page_dirty(page);
612 page_cache_release(page);
617 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
619 struct inode *inode = dentry->d_inode;
622 err = inode_change_ok(inode, attr);
626 if (S_ISREG(inode->i_mode) &&
627 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
628 struct btrfs_trans_handle *trans;
629 struct btrfs_root *root = BTRFS_I(inode)->root;
630 u64 mask = root->blocksize - 1;
631 u64 pos = (inode->i_size + mask) & ~mask;
634 if (attr->ia_size <= pos)
637 btrfs_truncate_page(inode->i_mapping, inode->i_size);
639 hole_size = (attr->ia_size - pos + mask) & ~mask;
640 hole_size >>= inode->i_blkbits;
642 mutex_lock(&root->fs_info->fs_mutex);
643 trans = btrfs_start_transaction(root, 1);
644 btrfs_set_trans_block_group(trans, inode);
645 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
646 pos, 0, 0, hole_size);
647 btrfs_end_transaction(trans, root);
648 mutex_unlock(&root->fs_info->fs_mutex);
653 err = inode_setattr(inode, attr);
657 void btrfs_delete_inode(struct inode *inode)
659 struct btrfs_trans_handle *trans;
660 struct btrfs_root *root = BTRFS_I(inode)->root;
663 truncate_inode_pages(&inode->i_data, 0);
664 if (is_bad_inode(inode)) {
668 mutex_lock(&root->fs_info->fs_mutex);
669 trans = btrfs_start_transaction(root, 1);
670 btrfs_set_trans_block_group(trans, inode);
671 ret = btrfs_truncate_in_trans(trans, root, inode);
674 ret = btrfs_free_inode(trans, root, inode);
677 btrfs_end_transaction(trans, root);
678 mutex_unlock(&root->fs_info->fs_mutex);
679 btrfs_btree_balance_dirty(root);
683 btrfs_end_transaction(trans, root);
684 mutex_unlock(&root->fs_info->fs_mutex);
685 btrfs_btree_balance_dirty(root);
691 * this returns the key found in the dir entry in the location pointer.
692 * If no dir entries were found, location->objectid is 0.
694 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
695 struct btrfs_key *location)
697 const char *name = dentry->d_name.name;
698 int namelen = dentry->d_name.len;
699 struct btrfs_dir_item *di;
700 struct btrfs_path *path;
701 struct btrfs_root *root = BTRFS_I(dir)->root;
704 path = btrfs_alloc_path();
706 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
708 if (!di || IS_ERR(di)) {
709 location->objectid = 0;
713 btrfs_disk_key_to_cpu(location, &di->location);
715 btrfs_release_path(root, path);
716 btrfs_free_path(path);
721 * when we hit a tree root in a directory, the btrfs part of the inode
722 * needs to be changed to reflect the root directory of the tree root. This
723 * is kind of like crossing a mount point.
725 static int fixup_tree_root_location(struct btrfs_root *root,
726 struct btrfs_key *location,
727 struct btrfs_root **sub_root)
729 struct btrfs_path *path;
730 struct btrfs_root_item *ri;
732 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
734 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
737 path = btrfs_alloc_path();
739 mutex_lock(&root->fs_info->fs_mutex);
741 *sub_root = btrfs_read_fs_root(root->fs_info, location);
742 if (IS_ERR(*sub_root))
743 return PTR_ERR(*sub_root);
745 ri = &(*sub_root)->root_item;
746 location->objectid = btrfs_root_dirid(ri);
748 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
749 location->offset = 0;
751 btrfs_free_path(path);
752 mutex_unlock(&root->fs_info->fs_mutex);
756 static int btrfs_init_locked_inode(struct inode *inode, void *p)
758 struct btrfs_iget_args *args = p;
759 inode->i_ino = args->ino;
760 BTRFS_I(inode)->root = args->root;
764 static int btrfs_find_actor(struct inode *inode, void *opaque)
766 struct btrfs_iget_args *args = opaque;
767 return (args->ino == inode->i_ino &&
768 args->root == BTRFS_I(inode)->root);
771 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
772 struct btrfs_root *root)
775 struct btrfs_iget_args args;
779 inode = iget5_locked(s, objectid, btrfs_find_actor,
780 btrfs_init_locked_inode,
785 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
786 struct nameidata *nd)
788 struct inode * inode;
789 struct btrfs_inode *bi = BTRFS_I(dir);
790 struct btrfs_root *root = bi->root;
791 struct btrfs_root *sub_root = root;
792 struct btrfs_key location;
795 if (dentry->d_name.len > BTRFS_NAME_LEN)
796 return ERR_PTR(-ENAMETOOLONG);
797 mutex_lock(&root->fs_info->fs_mutex);
798 ret = btrfs_inode_by_name(dir, dentry, &location);
799 mutex_unlock(&root->fs_info->fs_mutex);
803 if (location.objectid) {
804 ret = fixup_tree_root_location(root, &location, &sub_root);
808 return ERR_PTR(-ENOENT);
809 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
812 return ERR_PTR(-EACCES);
813 if (inode->i_state & I_NEW) {
814 /* the inode and parent dir are two different roots */
815 if (sub_root != root) {
817 sub_root->inode = inode;
819 BTRFS_I(inode)->root = sub_root;
820 memcpy(&BTRFS_I(inode)->location, &location,
822 btrfs_read_locked_inode(inode);
823 unlock_new_inode(inode);
826 return d_splice_alias(inode, dentry);
830 * readahead one full node of leaves as long as their keys include
831 * the objectid supplied
833 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
836 struct btrfs_node *node;
846 node = btrfs_buffer_node(path->nodes[1]);
847 slot = path->slots[1];
848 nritems = btrfs_header_nritems(&node->header);
849 for (i = slot + 1; i < nritems; i++) {
850 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
851 if (item_objectid != objectid)
853 blocknr = btrfs_node_blockptr(node, i);
854 ret = readahead_tree_block(root, blocknr);
859 static unsigned char btrfs_filetype_table[] = {
860 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
863 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
865 struct inode *inode = filp->f_path.dentry->d_inode;
866 struct btrfs_root *root = BTRFS_I(inode)->root;
867 struct btrfs_item *item;
868 struct btrfs_dir_item *di;
869 struct btrfs_key key;
870 struct btrfs_path *path;
873 struct btrfs_leaf *leaf;
876 unsigned char d_type;
881 int key_type = BTRFS_DIR_INDEX_KEY;
883 /* FIXME, use a real flag for deciding about the key type */
884 if (root->fs_info->tree_root == root)
885 key_type = BTRFS_DIR_ITEM_KEY;
886 mutex_lock(&root->fs_info->fs_mutex);
887 key.objectid = inode->i_ino;
889 btrfs_set_key_type(&key, key_type);
890 key.offset = filp->f_pos;
891 path = btrfs_alloc_path();
892 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
896 reada_leaves(root, path, inode->i_ino);
898 leaf = btrfs_buffer_leaf(path->nodes[0]);
899 nritems = btrfs_header_nritems(&leaf->header);
900 slot = path->slots[0];
901 if (advance || slot >= nritems) {
902 if (slot >= nritems -1) {
903 reada_leaves(root, path, inode->i_ino);
904 ret = btrfs_next_leaf(root, path);
907 leaf = btrfs_buffer_leaf(path->nodes[0]);
908 nritems = btrfs_header_nritems(&leaf->header);
909 slot = path->slots[0];
916 item = leaf->items + slot;
917 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
919 if (btrfs_disk_key_type(&item->key) != key_type)
921 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
923 filp->f_pos = btrfs_disk_key_offset(&item->key);
925 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
927 di_total = btrfs_item_size(leaf->items + slot);
928 while(di_cur < di_total) {
929 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
930 over = filldir(dirent, (const char *)(di + 1),
931 btrfs_dir_name_len(di),
932 btrfs_disk_key_offset(&item->key),
933 btrfs_disk_key_objectid(&di->location),
937 di_len = btrfs_dir_name_len(di) + sizeof(*di);
939 di = (struct btrfs_dir_item *)((char *)di + di_len);
946 btrfs_release_path(root, path);
947 btrfs_free_path(path);
948 mutex_unlock(&root->fs_info->fs_mutex);
952 int btrfs_write_inode(struct inode *inode, int wait)
954 struct btrfs_root *root = BTRFS_I(inode)->root;
955 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 ret = btrfs_commit_transaction(trans, root);
963 mutex_unlock(&root->fs_info->fs_mutex);
969 * This is somewhat expensive, updating the tree every time the
970 * inode changes. But, it is most likely to find the inode in cache.
971 * FIXME, needs more benchmarking...there are no reasons other than performance
972 * to keep or drop this code.
974 void btrfs_dirty_inode(struct inode *inode)
976 struct btrfs_root *root = BTRFS_I(inode)->root;
977 struct btrfs_trans_handle *trans;
979 mutex_lock(&root->fs_info->fs_mutex);
980 trans = btrfs_start_transaction(root, 1);
981 btrfs_set_trans_block_group(trans, inode);
982 btrfs_update_inode(trans, root, inode);
983 btrfs_end_transaction(trans, root);
984 mutex_unlock(&root->fs_info->fs_mutex);
987 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
990 struct btrfs_block_group_cache *group,
994 struct btrfs_inode_item inode_item;
995 struct btrfs_key *location;
999 inode = new_inode(root->fs_info->sb);
1001 return ERR_PTR(-ENOMEM);
1003 BTRFS_I(inode)->root = root;
1008 group = btrfs_find_block_group(root, group, 0, 0, owner);
1009 BTRFS_I(inode)->block_group = group;
1011 inode->i_uid = current->fsuid;
1012 inode->i_gid = current->fsgid;
1013 inode->i_mode = mode;
1014 inode->i_ino = objectid;
1015 inode->i_blocks = 0;
1016 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1017 fill_inode_item(&inode_item, inode);
1018 location = &BTRFS_I(inode)->location;
1019 location->objectid = objectid;
1020 location->flags = 0;
1021 location->offset = 0;
1022 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1024 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1026 return ERR_PTR(ret);
1027 insert_inode_hash(inode);
1031 static inline u8 btrfs_inode_type(struct inode *inode)
1033 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1036 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1037 struct dentry *dentry, struct inode *inode)
1040 struct btrfs_key key;
1041 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1042 struct inode *parent_inode;
1043 key.objectid = inode->i_ino;
1045 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1048 ret = btrfs_insert_dir_item(trans, root,
1049 dentry->d_name.name, dentry->d_name.len,
1050 dentry->d_parent->d_inode->i_ino,
1051 &key, btrfs_inode_type(inode));
1053 parent_inode = dentry->d_parent->d_inode;
1054 parent_inode->i_size += dentry->d_name.len * 2;
1055 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1056 ret = btrfs_update_inode(trans, root,
1057 dentry->d_parent->d_inode);
1062 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1063 struct dentry *dentry, struct inode *inode)
1065 int err = btrfs_add_link(trans, dentry, inode);
1067 d_instantiate(dentry, inode);
1075 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1076 int mode, dev_t rdev)
1078 struct btrfs_trans_handle *trans;
1079 struct btrfs_root *root = BTRFS_I(dir)->root;
1080 struct inode *inode;
1085 if (!new_valid_dev(rdev))
1088 mutex_lock(&root->fs_info->fs_mutex);
1089 trans = btrfs_start_transaction(root, 1);
1090 btrfs_set_trans_block_group(trans, dir);
1092 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1098 inode = btrfs_new_inode(trans, root, objectid,
1099 BTRFS_I(dir)->block_group, mode);
1100 err = PTR_ERR(inode);
1104 btrfs_set_trans_block_group(trans, inode);
1105 err = btrfs_add_nondir(trans, dentry, inode);
1109 inode->i_op = &btrfs_special_inode_operations;
1110 init_special_inode(inode, inode->i_mode, rdev);
1112 dir->i_sb->s_dirt = 1;
1113 btrfs_update_inode_block_group(trans, inode);
1114 btrfs_update_inode_block_group(trans, dir);
1116 btrfs_end_transaction(trans, root);
1117 mutex_unlock(&root->fs_info->fs_mutex);
1120 inode_dec_link_count(inode);
1123 btrfs_btree_balance_dirty(root);
1127 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1128 int mode, struct nameidata *nd)
1130 struct btrfs_trans_handle *trans;
1131 struct btrfs_root *root = BTRFS_I(dir)->root;
1132 struct inode *inode;
1137 mutex_lock(&root->fs_info->fs_mutex);
1138 trans = btrfs_start_transaction(root, 1);
1139 btrfs_set_trans_block_group(trans, dir);
1141 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1147 inode = btrfs_new_inode(trans, root, objectid,
1148 BTRFS_I(dir)->block_group, mode);
1149 err = PTR_ERR(inode);
1153 btrfs_set_trans_block_group(trans, inode);
1154 err = btrfs_add_nondir(trans, dentry, inode);
1158 inode->i_mapping->a_ops = &btrfs_aops;
1159 inode->i_fop = &btrfs_file_operations;
1160 inode->i_op = &btrfs_file_inode_operations;
1162 dir->i_sb->s_dirt = 1;
1163 btrfs_update_inode_block_group(trans, inode);
1164 btrfs_update_inode_block_group(trans, dir);
1166 btrfs_end_transaction(trans, root);
1167 mutex_unlock(&root->fs_info->fs_mutex);
1170 inode_dec_link_count(inode);
1173 btrfs_btree_balance_dirty(root);
1177 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1178 struct dentry *dentry)
1180 struct btrfs_trans_handle *trans;
1181 struct btrfs_root *root = BTRFS_I(dir)->root;
1182 struct inode *inode = old_dentry->d_inode;
1186 if (inode->i_nlink == 0)
1190 mutex_lock(&root->fs_info->fs_mutex);
1191 trans = btrfs_start_transaction(root, 1);
1192 btrfs_set_trans_block_group(trans, dir);
1193 atomic_inc(&inode->i_count);
1194 err = btrfs_add_nondir(trans, dentry, inode);
1197 dir->i_sb->s_dirt = 1;
1198 btrfs_update_inode_block_group(trans, dir);
1199 err = btrfs_update_inode(trans, root, inode);
1203 btrfs_end_transaction(trans, root);
1204 mutex_unlock(&root->fs_info->fs_mutex);
1207 inode_dec_link_count(inode);
1210 btrfs_btree_balance_dirty(root);
1214 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 u64 objectid, u64 dirid)
1220 struct btrfs_key key;
1225 key.objectid = objectid;
1228 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1230 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1231 &key, BTRFS_FT_DIR);
1234 key.objectid = dirid;
1235 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1236 &key, BTRFS_FT_DIR);
1243 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1245 struct inode *inode;
1246 struct btrfs_trans_handle *trans;
1247 struct btrfs_root *root = BTRFS_I(dir)->root;
1249 int drop_on_err = 0;
1252 mutex_lock(&root->fs_info->fs_mutex);
1253 trans = btrfs_start_transaction(root, 1);
1254 btrfs_set_trans_block_group(trans, dir);
1255 if (IS_ERR(trans)) {
1256 err = PTR_ERR(trans);
1260 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1266 inode = btrfs_new_inode(trans, root, objectid,
1267 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1268 if (IS_ERR(inode)) {
1269 err = PTR_ERR(inode);
1273 inode->i_op = &btrfs_dir_inode_operations;
1274 inode->i_fop = &btrfs_dir_file_operations;
1275 btrfs_set_trans_block_group(trans, inode);
1277 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1282 err = btrfs_update_inode(trans, root, inode);
1285 err = btrfs_add_link(trans, dentry, inode);
1288 d_instantiate(dentry, inode);
1290 dir->i_sb->s_dirt = 1;
1291 btrfs_update_inode_block_group(trans, inode);
1292 btrfs_update_inode_block_group(trans, dir);
1295 btrfs_end_transaction(trans, root);
1297 mutex_unlock(&root->fs_info->fs_mutex);
1300 btrfs_btree_balance_dirty(root);
1305 * FIBMAP and others want to pass in a fake buffer head. They need to
1306 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1307 * any packed file data into the fake bh
1309 #define BTRFS_GET_BLOCK_NO_CREATE 0
1310 #define BTRFS_GET_BLOCK_CREATE 1
1311 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1314 * FIXME create==1 doe not work.
1316 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1317 struct buffer_head *result, int create)
1322 u64 extent_start = 0;
1324 u64 objectid = inode->i_ino;
1327 struct btrfs_path *path;
1328 struct btrfs_root *root = BTRFS_I(inode)->root;
1329 struct btrfs_file_extent_item *item;
1330 struct btrfs_leaf *leaf;
1331 struct btrfs_disk_key *found_key;
1332 struct btrfs_trans_handle *trans = NULL;
1334 path = btrfs_alloc_path();
1336 if (create & BTRFS_GET_BLOCK_CREATE) {
1338 * danger!, this only works if the page is properly up
1341 trans = btrfs_start_transaction(root, 1);
1346 ret = btrfs_drop_extents(trans, root, inode,
1347 iblock << inode->i_blkbits,
1348 (iblock + 1) << inode->i_blkbits,
1353 ret = btrfs_lookup_file_extent(NULL, root, path,
1355 iblock << inode->i_blkbits, 0);
1362 if (path->slots[0] == 0) {
1363 btrfs_release_path(root, path);
1369 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1370 struct btrfs_file_extent_item);
1371 leaf = btrfs_buffer_leaf(path->nodes[0]);
1372 blocknr = btrfs_file_extent_disk_blocknr(item);
1373 blocknr += btrfs_file_extent_offset(item);
1375 /* are we inside the extent that was found? */
1376 found_key = &leaf->items[path->slots[0]].key;
1377 found_type = btrfs_disk_key_type(found_key);
1378 if (btrfs_disk_key_objectid(found_key) != objectid ||
1379 found_type != BTRFS_EXTENT_DATA_KEY) {
1384 found_type = btrfs_file_extent_type(item);
1385 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1386 if (found_type == BTRFS_FILE_EXTENT_REG) {
1387 extent_start = extent_start >> inode->i_blkbits;
1388 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1390 if (btrfs_file_extent_disk_blocknr(item) == 0)
1392 if (iblock >= extent_start && iblock < extent_end) {
1393 btrfs_map_bh_to_logical(root, result, blocknr +
1394 iblock - extent_start);
1397 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1402 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1406 size = btrfs_file_extent_inline_len(leaf->items +
1408 extent_end = (extent_start + size) >> inode->i_blkbits;
1409 extent_start >>= inode->i_blkbits;
1410 if (iblock < extent_start || iblock > extent_end) {
1413 ptr = btrfs_file_extent_inline_start(item);
1414 map = kmap(result->b_page);
1415 memcpy(map, ptr, size);
1416 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1417 flush_dcache_page(result->b_page);
1418 kunmap(result->b_page);
1419 set_buffer_uptodate(result);
1420 SetPageChecked(result->b_page);
1421 btrfs_map_bh_to_logical(root, result, 0);
1424 if (create & BTRFS_GET_BLOCK_CREATE) {
1425 struct btrfs_key ins;
1426 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1427 1, alloc_hint, (u64)-1,
1433 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1434 iblock << inode->i_blkbits,
1435 ins.objectid, ins.offset,
1441 btrfs_map_bh_to_logical(root, result, ins.objectid);
1445 ret = btrfs_end_transaction(trans, root);
1449 btrfs_free_path(path);
1453 int btrfs_get_block(struct inode *inode, sector_t iblock,
1454 struct buffer_head *result, int create)
1457 struct btrfs_root *root = BTRFS_I(inode)->root;
1458 mutex_lock(&root->fs_info->fs_mutex);
1459 err = btrfs_get_block_lock(inode, iblock, result, create);
1460 mutex_unlock(&root->fs_info->fs_mutex);
1464 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1465 struct buffer_head *result, int create)
1468 struct btrfs_root *root = BTRFS_I(inode)->root;
1469 struct page *page = result->b_page;
1470 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1471 struct btrfs_csum_item *item;
1472 struct btrfs_path *path = NULL;
1474 mutex_lock(&root->fs_info->fs_mutex);
1475 ret = btrfs_get_block_lock(inode, iblock, result, create);
1479 path = btrfs_alloc_path();
1480 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1482 ret = PTR_ERR(item);
1483 /* a csum that isn't present is a preallocated region. */
1484 if (ret == -ENOENT || ret == -EFBIG)
1486 result->b_private = NULL;
1489 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1492 btrfs_free_path(path);
1493 mutex_unlock(&root->fs_info->fs_mutex);
1497 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1498 struct buffer_head *result, int create)
1500 struct btrfs_root *root = BTRFS_I(inode)->root;
1501 mutex_lock(&root->fs_info->fs_mutex);
1502 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1503 mutex_unlock(&root->fs_info->fs_mutex);
1507 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1509 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1512 static int btrfs_prepare_write(struct file *file, struct page *page,
1513 unsigned from, unsigned to)
1515 return block_prepare_write(page, from, to, btrfs_get_block);
1518 static void buffer_io_error(struct buffer_head *bh)
1520 char b[BDEVNAME_SIZE];
1522 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1523 bdevname(bh->b_bdev, b),
1524 (unsigned long long)bh->b_blocknr);
1528 * I/O completion handler for block_read_full_page() - pages
1529 * which come unlocked at the end of I/O.
1531 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1533 unsigned long flags;
1534 struct buffer_head *first;
1535 struct buffer_head *tmp;
1537 int page_uptodate = 1;
1538 struct inode *inode;
1541 BUG_ON(!buffer_async_read(bh));
1544 inode = page->mapping->host;
1547 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1548 if (bh->b_private) {
1549 char csum[BTRFS_CRC32_SIZE];
1550 kaddr = kmap_atomic(page, KM_IRQ0);
1551 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1554 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1556 offset = (page->index << PAGE_CACHE_SHIFT) +
1558 printk("btrfs csum failed ino %lu off %llu\n",
1559 page->mapping->host->i_ino,
1560 (unsigned long long)offset);
1561 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1562 flush_dcache_page(page);
1564 kunmap_atomic(kaddr, KM_IRQ0);
1566 set_buffer_uptodate(bh);
1568 clear_buffer_uptodate(bh);
1569 if (printk_ratelimit())
1570 buffer_io_error(bh);
1575 * Be _very_ careful from here on. Bad things can happen if
1576 * two buffer heads end IO at almost the same time and both
1577 * decide that the page is now completely done.
1579 first = page_buffers(page);
1580 local_irq_save(flags);
1581 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1582 clear_buffer_async_read(bh);
1586 if (!buffer_uptodate(tmp))
1588 if (buffer_async_read(tmp)) {
1589 BUG_ON(!buffer_locked(tmp));
1592 tmp = tmp->b_this_page;
1593 } while (tmp != bh);
1594 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1595 local_irq_restore(flags);
1598 * If none of the buffers had errors and they are all
1599 * uptodate then we can set the page uptodate.
1601 if (page_uptodate && !PageError(page))
1602 SetPageUptodate(page);
1607 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1608 local_irq_restore(flags);
1613 * Generic "read page" function for block devices that have the normal
1614 * get_block functionality. This is most of the block device filesystems.
1615 * Reads the page asynchronously --- the unlock_buffer() and
1616 * set/clear_buffer_uptodate() functions propagate buffer state into the
1617 * page struct once IO has completed.
1619 int btrfs_readpage(struct file *file, struct page *page)
1621 struct inode *inode = page->mapping->host;
1622 sector_t iblock, lblock;
1623 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1624 unsigned int blocksize;
1626 int fully_mapped = 1;
1628 BUG_ON(!PageLocked(page));
1629 blocksize = 1 << inode->i_blkbits;
1630 if (!page_has_buffers(page))
1631 create_empty_buffers(page, blocksize, 0);
1632 head = page_buffers(page);
1634 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1635 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1641 if (buffer_uptodate(bh))
1644 if (!buffer_mapped(bh)) {
1648 if (iblock < lblock) {
1649 WARN_ON(bh->b_size != blocksize);
1650 err = btrfs_get_block_csum(inode, iblock,
1655 if (!buffer_mapped(bh)) {
1656 void *kaddr = kmap_atomic(page, KM_USER0);
1657 memset(kaddr + i * blocksize, 0, blocksize);
1658 flush_dcache_page(page);
1659 kunmap_atomic(kaddr, KM_USER0);
1661 set_buffer_uptodate(bh);
1665 * get_block() might have updated the buffer
1668 if (buffer_uptodate(bh))
1672 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1675 SetPageMappedToDisk(page);
1679 * All buffers are uptodate - we can set the page uptodate
1680 * as well. But not if get_block() returned an error.
1682 if (!PageError(page))
1683 SetPageUptodate(page);
1688 /* Stage two: lock the buffers */
1689 for (i = 0; i < nr; i++) {
1692 bh->b_end_io = btrfs_end_buffer_async_read;
1693 set_buffer_async_read(bh);
1697 * Stage 3: start the IO. Check for uptodateness
1698 * inside the buffer lock in case another process reading
1699 * the underlying blockdev brought it uptodate (the sct fix).
1701 for (i = 0; i < nr; i++) {
1703 if (buffer_uptodate(bh))
1704 btrfs_end_buffer_async_read(bh, 1);
1706 submit_bh(READ, bh);
1712 * Aside from a tiny bit of packed file data handling, this is the
1713 * same as the generic code.
1715 * While block_write_full_page is writing back the dirty buffers under
1716 * the page lock, whoever dirtied the buffers may decide to clean them
1717 * again at any time. We handle that by only looking at the buffer
1718 * state inside lock_buffer().
1720 * If block_write_full_page() is called for regular writeback
1721 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1722 * locked buffer. This only can happen if someone has written the buffer
1723 * directly, with submit_bh(). At the address_space level PageWriteback
1724 * prevents this contention from occurring.
1726 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1727 struct writeback_control *wbc)
1731 sector_t last_block;
1732 struct buffer_head *bh, *head;
1733 const unsigned blocksize = 1 << inode->i_blkbits;
1734 int nr_underway = 0;
1735 struct btrfs_root *root = BTRFS_I(inode)->root;
1737 BUG_ON(!PageLocked(page));
1739 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1741 /* no csumming allowed when from PF_MEMALLOC */
1742 if (current->flags & PF_MEMALLOC) {
1743 redirty_page_for_writepage(wbc, page);
1748 if (!page_has_buffers(page)) {
1749 create_empty_buffers(page, blocksize,
1750 (1 << BH_Dirty)|(1 << BH_Uptodate));
1754 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1755 * here, and the (potentially unmapped) buffers may become dirty at
1756 * any time. If a buffer becomes dirty here after we've inspected it
1757 * then we just miss that fact, and the page stays dirty.
1759 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1760 * handle that here by just cleaning them.
1763 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1764 head = page_buffers(page);
1768 * Get all the dirty buffers mapped to disk addresses and
1769 * handle any aliases from the underlying blockdev's mapping.
1772 if (block > last_block) {
1774 * mapped buffers outside i_size will occur, because
1775 * this page can be outside i_size when there is a
1776 * truncate in progress.
1779 * The buffer was zeroed by block_write_full_page()
1781 clear_buffer_dirty(bh);
1782 set_buffer_uptodate(bh);
1783 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1784 WARN_ON(bh->b_size != blocksize);
1785 err = btrfs_get_block(inode, block, bh, 0);
1789 if (buffer_new(bh)) {
1790 /* blockdev mappings never come here */
1791 clear_buffer_new(bh);
1794 bh = bh->b_this_page;
1796 } while (bh != head);
1799 if (!buffer_mapped(bh))
1802 * If it's a fully non-blocking write attempt and we cannot
1803 * lock the buffer then redirty the page. Note that this can
1804 * potentially cause a busy-wait loop from pdflush and kswapd
1805 * activity, but those code paths have their own higher-level
1808 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1810 } else if (test_set_buffer_locked(bh)) {
1811 redirty_page_for_writepage(wbc, page);
1814 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1815 struct btrfs_trans_handle *trans;
1817 u64 off = page->index << PAGE_CACHE_SHIFT;
1820 off += bh_offset(bh);
1821 mutex_lock(&root->fs_info->fs_mutex);
1822 trans = btrfs_start_transaction(root, 1);
1823 btrfs_set_trans_block_group(trans, inode);
1825 btrfs_csum_file_block(trans, root, inode->i_ino,
1826 off, kaddr + bh_offset(bh),
1829 ret = btrfs_end_transaction(trans, root);
1831 mutex_unlock(&root->fs_info->fs_mutex);
1832 mark_buffer_async_write(bh);
1836 } while ((bh = bh->b_this_page) != head);
1839 * The page and its buffers are protected by PageWriteback(), so we can
1840 * drop the bh refcounts early.
1842 BUG_ON(PageWriteback(page));
1843 set_page_writeback(page);
1846 struct buffer_head *next = bh->b_this_page;
1847 if (buffer_async_write(bh)) {
1848 submit_bh(WRITE, bh);
1852 } while (bh != head);
1857 if (nr_underway == 0) {
1859 * The page was marked dirty, but the buffers were
1860 * clean. Someone wrote them back by hand with
1861 * ll_rw_block/submit_bh. A rare case.
1865 if (!buffer_uptodate(bh)) {
1869 bh = bh->b_this_page;
1870 } while (bh != head);
1872 SetPageUptodate(page);
1873 end_page_writeback(page);
1879 * ENOSPC, or some other error. We may already have added some
1880 * blocks to the file, so we need to write these out to avoid
1881 * exposing stale data.
1882 * The page is currently locked and not marked for writeback
1885 /* Recovery: lock and submit the mapped buffers */
1887 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1889 mark_buffer_async_write(bh);
1892 * The buffer may have been set dirty during
1893 * attachment to a dirty page.
1895 clear_buffer_dirty(bh);
1897 } while ((bh = bh->b_this_page) != head);
1899 BUG_ON(PageWriteback(page));
1900 set_page_writeback(page);
1902 struct buffer_head *next = bh->b_this_page;
1903 if (buffer_async_write(bh)) {
1904 clear_buffer_dirty(bh);
1905 submit_bh(WRITE, bh);
1909 } while (bh != head);
1914 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1916 struct inode * const inode = page->mapping->host;
1917 loff_t i_size = i_size_read(inode);
1918 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1922 /* Is the page fully inside i_size? */
1923 if (page->index < end_index)
1924 return __btrfs_write_full_page(inode, page, wbc);
1926 /* Is the page fully outside i_size? (truncate in progress) */
1927 offset = i_size & (PAGE_CACHE_SIZE-1);
1928 if (page->index >= end_index+1 || !offset) {
1930 * The page may have dirty, unmapped buffers. For example,
1931 * they may have been added in ext3_writepage(). Make them
1932 * freeable here, so the page does not leak.
1934 block_invalidatepage(page, 0);
1936 return 0; /* don't care */
1940 * The page straddles i_size. It must be zeroed out on each and every
1941 * writepage invokation because it may be mmapped. "A file is mapped
1942 * in multiples of the page size. For a file that is not a multiple of
1943 * the page size, the remaining memory is zeroed when mapped, and
1944 * writes to that region are not written out to the file."
1946 kaddr = kmap_atomic(page, KM_USER0);
1947 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1948 flush_dcache_page(page);
1949 kunmap_atomic(kaddr, KM_USER0);
1950 return __btrfs_write_full_page(inode, page, wbc);
1954 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1955 * called from a page fault handler when a page is first dirtied. Hence we must
1956 * be careful to check for EOF conditions here. We set the page up correctly
1957 * for a written page which means we get ENOSPC checking when writing into
1958 * holes and correct delalloc and unwritten extent mapping on filesystems that
1959 * support these features.
1961 * We are not allowed to take the i_mutex here so we have to play games to
1962 * protect against truncate races as the page could now be beyond EOF. Because
1963 * vmtruncate() writes the inode size before removing pages, once we have the
1964 * page lock we can determine safely if the page is beyond EOF. If it is not
1965 * beyond EOF, then the page is guaranteed safe against truncation until we
1968 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1970 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1976 wait_on_page_writeback(page);
1977 size = i_size_read(inode);
1978 if ((page->mapping != inode->i_mapping) ||
1979 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1980 /* page got truncated out from underneath us */
1984 /* page is wholly or partially inside EOF */
1985 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1986 end = size & ~PAGE_CACHE_MASK;
1988 end = PAGE_CACHE_SIZE;
1990 ret = btrfs_prepare_write(NULL, page, 0, end);
1992 ret = btrfs_commit_write(NULL, page, 0, end);
1999 static void btrfs_truncate(struct inode *inode)
2001 struct btrfs_root *root = BTRFS_I(inode)->root;
2003 struct btrfs_trans_handle *trans;
2005 if (!S_ISREG(inode->i_mode))
2007 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2010 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2012 mutex_lock(&root->fs_info->fs_mutex);
2013 trans = btrfs_start_transaction(root, 1);
2014 btrfs_set_trans_block_group(trans, inode);
2016 /* FIXME, add redo link to tree so we don't leak on crash */
2017 ret = btrfs_truncate_in_trans(trans, root, inode);
2018 btrfs_update_inode(trans, root, inode);
2019 ret = btrfs_end_transaction(trans, root);
2021 mutex_unlock(&root->fs_info->fs_mutex);
2022 btrfs_btree_balance_dirty(root);
2025 int btrfs_commit_write(struct file *file, struct page *page,
2026 unsigned from, unsigned to)
2028 struct inode *inode = page->mapping->host;
2029 struct buffer_head *bh;
2030 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2032 SetPageUptodate(page);
2033 bh = page_buffers(page);
2034 set_buffer_uptodate(bh);
2035 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2036 set_page_dirty(page);
2038 if (pos > inode->i_size) {
2039 i_size_write(inode, pos);
2040 mark_inode_dirty(inode);
2045 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2047 struct btrfs_trans_handle *trans;
2048 struct btrfs_key key;
2049 struct btrfs_root_item root_item;
2050 struct btrfs_inode_item *inode_item;
2051 struct buffer_head *subvol;
2052 struct btrfs_leaf *leaf;
2053 struct btrfs_root *new_root;
2054 struct inode *inode;
2059 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2061 mutex_lock(&root->fs_info->fs_mutex);
2062 trans = btrfs_start_transaction(root, 1);
2065 subvol = btrfs_alloc_free_block(trans, root, 0);
2067 return PTR_ERR(subvol);
2068 leaf = btrfs_buffer_leaf(subvol);
2069 btrfs_set_header_nritems(&leaf->header, 0);
2070 btrfs_set_header_level(&leaf->header, 0);
2071 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2072 btrfs_set_header_generation(&leaf->header, trans->transid);
2073 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2074 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2075 sizeof(leaf->header.fsid));
2076 btrfs_mark_buffer_dirty(subvol);
2078 inode_item = &root_item.inode;
2079 memset(inode_item, 0, sizeof(*inode_item));
2080 btrfs_set_inode_generation(inode_item, 1);
2081 btrfs_set_inode_size(inode_item, 3);
2082 btrfs_set_inode_nlink(inode_item, 1);
2083 btrfs_set_inode_nblocks(inode_item, 1);
2084 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2086 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2087 btrfs_set_root_refs(&root_item, 1);
2088 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2089 root_item.drop_level = 0;
2093 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2098 btrfs_set_root_dirid(&root_item, new_dirid);
2100 key.objectid = objectid;
2103 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2104 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2110 * insert the directory item
2112 key.offset = (u64)-1;
2113 dir = root->fs_info->sb->s_root->d_inode;
2114 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2115 name, namelen, dir->i_ino, &key,
2120 ret = btrfs_commit_transaction(trans, root);
2124 new_root = btrfs_read_fs_root(root->fs_info, &key);
2127 trans = btrfs_start_transaction(new_root, 1);
2130 inode = btrfs_new_inode(trans, new_root, new_dirid,
2131 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2134 inode->i_op = &btrfs_dir_inode_operations;
2135 inode->i_fop = &btrfs_dir_file_operations;
2136 new_root->inode = inode;
2138 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2144 ret = btrfs_update_inode(trans, new_root, inode);
2148 err = btrfs_commit_transaction(trans, root);
2152 mutex_unlock(&root->fs_info->fs_mutex);
2153 btrfs_btree_balance_dirty(root);
2157 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2159 struct btrfs_trans_handle *trans;
2160 struct btrfs_key key;
2161 struct btrfs_root_item new_root_item;
2166 if (!root->ref_cows)
2169 mutex_lock(&root->fs_info->fs_mutex);
2170 trans = btrfs_start_transaction(root, 1);
2173 ret = btrfs_update_inode(trans, root, root->inode);
2177 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2182 memcpy(&new_root_item, &root->root_item,
2183 sizeof(new_root_item));
2185 key.objectid = objectid;
2188 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2189 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2191 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2197 * insert the directory item
2199 key.offset = (u64)-1;
2200 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2202 root->fs_info->sb->s_root->d_inode->i_ino,
2203 &key, BTRFS_FT_DIR);
2208 ret = btrfs_inc_root_ref(trans, root);
2213 err = btrfs_commit_transaction(trans, root);
2216 mutex_unlock(&root->fs_info->fs_mutex);
2217 btrfs_btree_balance_dirty(root);
2221 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2222 cmd, unsigned long arg)
2224 struct btrfs_root *root = BTRFS_I(inode)->root;
2225 struct btrfs_ioctl_vol_args vol_args;
2227 struct btrfs_dir_item *di;
2229 struct btrfs_path *path;
2233 case BTRFS_IOC_SNAP_CREATE:
2234 if (copy_from_user(&vol_args,
2235 (struct btrfs_ioctl_vol_args __user *)arg,
2238 namelen = strlen(vol_args.name);
2239 if (namelen > BTRFS_VOL_NAME_MAX)
2241 if (strchr(vol_args.name, '/'))
2243 path = btrfs_alloc_path();
2246 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2247 mutex_lock(&root->fs_info->fs_mutex);
2248 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2250 vol_args.name, namelen, 0);
2251 mutex_unlock(&root->fs_info->fs_mutex);
2252 btrfs_free_path(path);
2253 if (di && !IS_ERR(di))
2258 if (root == root->fs_info->tree_root)
2259 ret = create_subvol(root, vol_args.name, namelen);
2261 ret = create_snapshot(root, vol_args.name, namelen);
2269 #ifdef CONFIG_COMPAT
2270 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2273 struct inode *inode = file->f_path.dentry->d_inode;
2276 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2284 * Called inside transaction, so use GFP_NOFS
2286 struct inode *btrfs_alloc_inode(struct super_block *sb)
2288 struct btrfs_inode *ei;
2290 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2293 return &ei->vfs_inode;
2296 void btrfs_destroy_inode(struct inode *inode)
2298 WARN_ON(!list_empty(&inode->i_dentry));
2299 WARN_ON(inode->i_data.nrpages);
2301 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2304 static void init_once(void * foo, struct kmem_cache * cachep,
2305 unsigned long flags)
2307 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2309 inode_init_once(&ei->vfs_inode);
2312 void btrfs_destroy_cachep(void)
2314 if (btrfs_inode_cachep)
2315 kmem_cache_destroy(btrfs_inode_cachep);
2316 if (btrfs_trans_handle_cachep)
2317 kmem_cache_destroy(btrfs_trans_handle_cachep);
2318 if (btrfs_transaction_cachep)
2319 kmem_cache_destroy(btrfs_transaction_cachep);
2320 if (btrfs_bit_radix_cachep)
2321 kmem_cache_destroy(btrfs_bit_radix_cachep);
2322 if (btrfs_path_cachep)
2323 kmem_cache_destroy(btrfs_path_cachep);
2326 int btrfs_init_cachep(void)
2328 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2329 sizeof(struct btrfs_inode),
2330 0, (SLAB_RECLAIM_ACCOUNT|
2333 if (!btrfs_inode_cachep)
2335 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2336 sizeof(struct btrfs_trans_handle),
2337 0, (SLAB_RECLAIM_ACCOUNT|
2340 if (!btrfs_trans_handle_cachep)
2342 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2343 sizeof(struct btrfs_transaction),
2344 0, (SLAB_RECLAIM_ACCOUNT|
2347 if (!btrfs_transaction_cachep)
2349 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2350 sizeof(struct btrfs_transaction),
2351 0, (SLAB_RECLAIM_ACCOUNT|
2354 if (!btrfs_path_cachep)
2356 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2358 0, (SLAB_RECLAIM_ACCOUNT|
2360 SLAB_DESTROY_BY_RCU),
2362 if (!btrfs_bit_radix_cachep)
2366 btrfs_destroy_cachep();
2370 static int btrfs_getattr(struct vfsmount *mnt,
2371 struct dentry *dentry, struct kstat *stat)
2373 struct inode *inode = dentry->d_inode;
2374 generic_fillattr(inode, stat);
2375 stat->blksize = 256 * 1024;
2379 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2380 struct inode * new_dir,struct dentry *new_dentry)
2382 struct btrfs_trans_handle *trans;
2383 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2384 struct inode *new_inode = new_dentry->d_inode;
2385 struct inode *old_inode = old_dentry->d_inode;
2386 struct timespec ctime = CURRENT_TIME;
2387 struct btrfs_path *path;
2388 struct btrfs_dir_item *di;
2391 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2392 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2395 mutex_lock(&root->fs_info->fs_mutex);
2396 trans = btrfs_start_transaction(root, 1);
2397 btrfs_set_trans_block_group(trans, new_dir);
2398 path = btrfs_alloc_path();
2404 old_dentry->d_inode->i_nlink++;
2405 old_dir->i_ctime = old_dir->i_mtime = ctime;
2406 new_dir->i_ctime = new_dir->i_mtime = ctime;
2407 old_inode->i_ctime = ctime;
2408 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2409 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2411 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2421 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2422 ret = btrfs_del_item(trans, root, path);
2426 btrfs_release_path(root, path);
2428 di = btrfs_lookup_dir_index_item(trans, root, path,
2440 ret = btrfs_del_item(trans, root, path);
2444 btrfs_release_path(root, path);
2446 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2447 old_inode->i_ino, location,
2454 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2459 new_inode->i_ctime = CURRENT_TIME;
2460 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2463 if (S_ISDIR(new_inode->i_mode))
2464 clear_nlink(new_inode);
2466 drop_nlink(new_inode);
2467 ret = btrfs_update_inode(trans, root, new_inode);
2471 ret = btrfs_add_link(trans, new_dentry, old_inode);
2476 btrfs_free_path(path);
2477 btrfs_end_transaction(trans, root);
2478 mutex_unlock(&root->fs_info->fs_mutex);
2482 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2483 const char *symname)
2485 struct btrfs_trans_handle *trans;
2486 struct btrfs_root *root = BTRFS_I(dir)->root;
2487 struct btrfs_path *path;
2488 struct btrfs_key key;
2489 struct inode *inode;
2496 struct btrfs_file_extent_item *ei;
2498 name_len = strlen(symname) + 1;
2499 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2500 return -ENAMETOOLONG;
2501 mutex_lock(&root->fs_info->fs_mutex);
2502 trans = btrfs_start_transaction(root, 1);
2503 btrfs_set_trans_block_group(trans, dir);
2505 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2511 inode = btrfs_new_inode(trans, root, objectid,
2512 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2513 err = PTR_ERR(inode);
2517 btrfs_set_trans_block_group(trans, inode);
2518 err = btrfs_add_nondir(trans, dentry, inode);
2522 inode->i_mapping->a_ops = &btrfs_aops;
2523 inode->i_fop = &btrfs_file_operations;
2524 inode->i_op = &btrfs_file_inode_operations;
2526 dir->i_sb->s_dirt = 1;
2527 btrfs_update_inode_block_group(trans, inode);
2528 btrfs_update_inode_block_group(trans, dir);
2532 path = btrfs_alloc_path();
2534 key.objectid = inode->i_ino;
2537 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2538 datasize = btrfs_file_extent_calc_inline_size(name_len);
2539 err = btrfs_insert_empty_item(trans, root, path, &key,
2545 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2546 path->slots[0], struct btrfs_file_extent_item);
2547 btrfs_set_file_extent_generation(ei, trans->transid);
2548 btrfs_set_file_extent_type(ei,
2549 BTRFS_FILE_EXTENT_INLINE);
2550 ptr = btrfs_file_extent_inline_start(ei);
2551 btrfs_memcpy(root, path->nodes[0]->b_data,
2552 ptr, symname, name_len);
2553 btrfs_mark_buffer_dirty(path->nodes[0]);
2554 btrfs_free_path(path);
2555 inode->i_op = &btrfs_symlink_inode_operations;
2556 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2557 inode->i_size = name_len - 1;
2558 err = btrfs_update_inode(trans, root, inode);
2563 btrfs_end_transaction(trans, root);
2564 mutex_unlock(&root->fs_info->fs_mutex);
2566 inode_dec_link_count(inode);
2569 btrfs_btree_balance_dirty(root);
2573 static struct inode_operations btrfs_dir_inode_operations = {
2574 .lookup = btrfs_lookup,
2575 .create = btrfs_create,
2576 .unlink = btrfs_unlink,
2578 .mkdir = btrfs_mkdir,
2579 .rmdir = btrfs_rmdir,
2580 .rename = btrfs_rename,
2581 .symlink = btrfs_symlink,
2582 .setattr = btrfs_setattr,
2583 .mknod = btrfs_mknod,
2586 static struct inode_operations btrfs_dir_ro_inode_operations = {
2587 .lookup = btrfs_lookup,
2590 static struct file_operations btrfs_dir_file_operations = {
2591 .llseek = generic_file_llseek,
2592 .read = generic_read_dir,
2593 .readdir = btrfs_readdir,
2594 .ioctl = btrfs_ioctl,
2595 #ifdef CONFIG_COMPAT
2596 .compat_ioctl = btrfs_compat_ioctl,
2600 static struct address_space_operations btrfs_aops = {
2601 .readpage = btrfs_readpage,
2602 .writepage = btrfs_writepage,
2603 .sync_page = block_sync_page,
2604 .prepare_write = btrfs_prepare_write,
2605 .commit_write = btrfs_commit_write,
2609 static struct address_space_operations btrfs_symlink_aops = {
2610 .readpage = btrfs_readpage,
2611 .writepage = btrfs_writepage,
2614 static struct inode_operations btrfs_file_inode_operations = {
2615 .truncate = btrfs_truncate,
2616 .getattr = btrfs_getattr,
2617 .setattr = btrfs_setattr,
2620 static struct inode_operations btrfs_special_inode_operations = {
2621 .getattr = btrfs_getattr,
2622 .setattr = btrfs_setattr,
2625 static struct inode_operations btrfs_symlink_inode_operations = {
2626 .readlink = generic_readlink,
2627 .follow_link = page_follow_link_light,
2628 .put_link = page_put_link,