2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/module.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args {
44 struct btrfs_root *root;
47 static struct inode_operations btrfs_dir_inode_operations;
48 static struct inode_operations btrfs_symlink_inode_operations;
49 static struct inode_operations btrfs_dir_ro_inode_operations;
50 static struct inode_operations btrfs_file_inode_operations;
51 static struct address_space_operations btrfs_aops;
52 static struct address_space_operations btrfs_symlink_aops;
53 static struct file_operations btrfs_dir_file_operations;
55 static struct kmem_cache *btrfs_inode_cachep;
56 struct kmem_cache *btrfs_trans_handle_cachep;
57 struct kmem_cache *btrfs_transaction_cachep;
58 struct kmem_cache *btrfs_bit_radix_cachep;
59 struct kmem_cache *btrfs_path_cachep;
62 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
63 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
64 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
65 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
66 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
67 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
68 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
69 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
72 void btrfs_read_locked_inode(struct inode *inode)
74 struct btrfs_path *path;
75 struct btrfs_inode_item *inode_item;
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_key location;
78 u64 alloc_group_block;
81 path = btrfs_alloc_path();
83 mutex_lock(&root->fs_info->fs_mutex);
85 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
86 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
88 btrfs_free_path(path);
91 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
93 struct btrfs_inode_item);
95 inode->i_mode = btrfs_inode_mode(inode_item);
96 inode->i_nlink = btrfs_inode_nlink(inode_item);
97 inode->i_uid = btrfs_inode_uid(inode_item);
98 inode->i_gid = btrfs_inode_gid(inode_item);
99 inode->i_size = btrfs_inode_size(inode_item);
100 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
101 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
102 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
103 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
104 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
106 inode->i_blocks = btrfs_inode_nblocks(inode_item);
107 inode->i_generation = btrfs_inode_generation(inode_item);
108 alloc_group_block = btrfs_inode_block_group(inode_item);
109 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
112 btrfs_free_path(path);
115 mutex_unlock(&root->fs_info->fs_mutex);
117 switch (inode->i_mode & S_IFMT) {
120 init_special_inode(inode, inode->i_mode,
121 btrfs_inode_rdev(inode_item));
125 inode->i_mapping->a_ops = &btrfs_aops;
126 inode->i_fop = &btrfs_file_operations;
127 inode->i_op = &btrfs_file_inode_operations;
130 inode->i_fop = &btrfs_dir_file_operations;
131 if (root == root->fs_info->tree_root)
132 inode->i_op = &btrfs_dir_ro_inode_operations;
134 inode->i_op = &btrfs_dir_inode_operations;
137 inode->i_op = &btrfs_symlink_inode_operations;
138 inode->i_mapping->a_ops = &btrfs_symlink_aops;
144 btrfs_release_path(root, path);
145 btrfs_free_path(path);
146 mutex_unlock(&root->fs_info->fs_mutex);
147 make_bad_inode(inode);
150 static void fill_inode_item(struct btrfs_inode_item *item,
153 btrfs_set_inode_uid(item, inode->i_uid);
154 btrfs_set_inode_gid(item, inode->i_gid);
155 btrfs_set_inode_size(item, inode->i_size);
156 btrfs_set_inode_mode(item, inode->i_mode);
157 btrfs_set_inode_nlink(item, inode->i_nlink);
158 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
159 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
160 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
161 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
162 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
163 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
164 btrfs_set_inode_nblocks(item, inode->i_blocks);
165 btrfs_set_inode_generation(item, inode->i_generation);
166 btrfs_set_inode_block_group(item,
167 BTRFS_I(inode)->block_group->key.objectid);
170 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root,
174 struct btrfs_inode_item *inode_item;
175 struct btrfs_path *path;
178 path = btrfs_alloc_path();
180 ret = btrfs_lookup_inode(trans, root, path,
181 &BTRFS_I(inode)->location, 1);
188 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
190 struct btrfs_inode_item);
192 fill_inode_item(inode_item, inode);
193 btrfs_mark_buffer_dirty(path->nodes[0]);
196 btrfs_release_path(root, path);
197 btrfs_free_path(path);
202 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
203 struct btrfs_root *root,
205 struct dentry *dentry)
207 struct btrfs_path *path;
208 const char *name = dentry->d_name.name;
209 int name_len = dentry->d_name.len;
212 struct btrfs_dir_item *di;
214 path = btrfs_alloc_path();
220 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
230 objectid = btrfs_disk_key_objectid(&di->location);
231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
234 btrfs_release_path(root, path);
236 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
237 objectid, name, name_len, -1);
246 ret = btrfs_delete_one_dir_name(trans, root, path, di);
248 dentry->d_inode->i_ctime = dir->i_ctime;
250 btrfs_free_path(path);
252 dir->i_size -= name_len * 2;
253 btrfs_update_inode(trans, root, dir);
254 drop_nlink(dentry->d_inode);
255 ret = btrfs_update_inode(trans, root, dentry->d_inode);
256 dir->i_sb->s_dirt = 1;
261 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
263 struct btrfs_root *root;
264 struct btrfs_trans_handle *trans;
267 root = BTRFS_I(dir)->root;
268 mutex_lock(&root->fs_info->fs_mutex);
269 trans = btrfs_start_transaction(root, 1);
270 btrfs_set_trans_block_group(trans, dir);
271 ret = btrfs_unlink_trans(trans, root, dir, dentry);
272 btrfs_end_transaction(trans, root);
273 mutex_unlock(&root->fs_info->fs_mutex);
274 btrfs_btree_balance_dirty(root);
278 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
280 struct inode *inode = dentry->d_inode;
283 struct btrfs_root *root = BTRFS_I(dir)->root;
284 struct btrfs_path *path;
285 struct btrfs_key key;
286 struct btrfs_trans_handle *trans;
287 struct btrfs_key found_key;
289 struct btrfs_leaf *leaf;
290 char *goodnames = "..";
292 path = btrfs_alloc_path();
294 mutex_lock(&root->fs_info->fs_mutex);
295 trans = btrfs_start_transaction(root, 1);
296 btrfs_set_trans_block_group(trans, dir);
297 key.objectid = inode->i_ino;
298 key.offset = (u64)-1;
301 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
307 if (path->slots[0] == 0) {
312 leaf = btrfs_buffer_leaf(path->nodes[0]);
313 btrfs_disk_key_to_cpu(&found_key,
314 &leaf->items[path->slots[0]].key);
315 found_type = btrfs_key_type(&found_key);
316 if (found_key.objectid != inode->i_ino) {
320 if ((found_type != BTRFS_DIR_ITEM_KEY &&
321 found_type != BTRFS_DIR_INDEX_KEY) ||
322 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
323 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
327 ret = btrfs_del_item(trans, root, path);
330 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
332 btrfs_release_path(root, path);
335 btrfs_release_path(root, path);
337 /* now the directory is empty */
338 err = btrfs_unlink_trans(trans, root, dir, dentry);
343 btrfs_release_path(root, path);
344 btrfs_free_path(path);
345 mutex_unlock(&root->fs_info->fs_mutex);
346 ret = btrfs_end_transaction(trans, root);
347 btrfs_btree_balance_dirty(root);
353 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
354 struct btrfs_root *root,
357 struct btrfs_path *path;
362 path = btrfs_alloc_path();
364 ret = btrfs_lookup_inode(trans, root, path,
365 &BTRFS_I(inode)->location, -1);
369 ret = btrfs_del_item(trans, root, path);
370 btrfs_free_path(path);
375 * truncates go from a high offset to a low offset. So, walk
376 * from hi to lo in the node and issue readas. Stop when you find
377 * keys from a different objectid
379 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
382 struct btrfs_node *node;
392 node = btrfs_buffer_node(path->nodes[1]);
393 slot = path->slots[1];
396 nritems = btrfs_header_nritems(&node->header);
397 for (i = slot - 1; i >= 0; i--) {
398 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
399 if (item_objectid != objectid)
401 blocknr = btrfs_node_blockptr(node, i);
402 ret = readahead_tree_block(root, blocknr);
409 * this can truncate away extent items, csum items and directory items.
410 * It starts at a high offset and removes keys until it can't find
411 * any higher than i_size.
413 * csum items that cross the new i_size are truncated to the new size
416 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
417 struct btrfs_root *root,
421 struct btrfs_path *path;
422 struct btrfs_key key;
423 struct btrfs_disk_key *found_key;
425 struct btrfs_leaf *leaf;
426 struct btrfs_file_extent_item *fi;
427 u64 extent_start = 0;
428 u64 extent_num_blocks = 0;
433 path = btrfs_alloc_path();
435 /* FIXME, add redo link to tree so we don't leak on crash */
436 key.objectid = inode->i_ino;
437 key.offset = (u64)-1;
440 btrfs_init_path(path);
442 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
447 BUG_ON(path->slots[0] == 0);
450 reada_truncate(root, path, inode->i_ino);
451 leaf = btrfs_buffer_leaf(path->nodes[0]);
452 found_key = &leaf->items[path->slots[0]].key;
453 found_type = btrfs_disk_key_type(found_key);
455 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
457 if (found_type != BTRFS_CSUM_ITEM_KEY &&
458 found_type != BTRFS_DIR_ITEM_KEY &&
459 found_type != BTRFS_DIR_INDEX_KEY &&
460 found_type != BTRFS_EXTENT_DATA_KEY)
463 item_end = btrfs_disk_key_offset(found_key);
464 if (found_type == BTRFS_EXTENT_DATA_KEY) {
465 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
467 struct btrfs_file_extent_item);
468 if (btrfs_file_extent_type(fi) !=
469 BTRFS_FILE_EXTENT_INLINE) {
470 item_end += btrfs_file_extent_num_blocks(fi) <<
474 if (found_type == BTRFS_CSUM_ITEM_KEY) {
475 ret = btrfs_csum_truncate(trans, root, path,
479 if (item_end < inode->i_size) {
481 btrfs_set_key_type(&key, found_type - 1);
486 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
492 /* FIXME, shrink the extent if the ref count is only 1 */
493 if (found_type == BTRFS_EXTENT_DATA_KEY &&
494 btrfs_file_extent_type(fi) !=
495 BTRFS_FILE_EXTENT_INLINE) {
498 u64 orig_num_blocks =
499 btrfs_file_extent_num_blocks(fi);
500 extent_num_blocks = inode->i_size -
501 btrfs_disk_key_offset(found_key) +
503 extent_num_blocks >>= inode->i_blkbits;
504 btrfs_set_file_extent_num_blocks(fi,
506 inode->i_blocks -= (orig_num_blocks -
507 extent_num_blocks) << 3;
508 mark_buffer_dirty(path->nodes[0]);
511 btrfs_file_extent_disk_blocknr(fi);
513 btrfs_file_extent_disk_num_blocks(fi);
514 /* FIXME blocksize != 4096 */
515 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
516 if (extent_start != 0) {
518 inode->i_blocks -= num_dec;
523 ret = btrfs_del_item(trans, root, path);
529 btrfs_release_path(root, path);
531 ret = btrfs_free_extent(trans, root, extent_start,
532 extent_num_blocks, 0);
538 btrfs_release_path(root, path);
539 btrfs_free_path(path);
540 inode->i_sb->s_dirt = 1;
545 * taken from block_truncate_page, but does cow as it zeros out
546 * any bytes left in the last page in the file.
548 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
550 struct inode *inode = mapping->host;
551 unsigned blocksize = 1 << inode->i_blkbits;
552 pgoff_t index = from >> PAGE_CACHE_SHIFT;
553 unsigned offset = from & (PAGE_CACHE_SIZE-1);
557 struct btrfs_root *root = BTRFS_I(inode)->root;
559 struct btrfs_key ins;
560 struct btrfs_trans_handle *trans;
562 if ((offset & (blocksize - 1)) == 0)
566 page = grab_cache_page(mapping, index);
570 if (!PageUptodate(page)) {
571 ret = btrfs_readpage(NULL, page);
573 if (!PageUptodate(page)) {
578 mutex_lock(&root->fs_info->fs_mutex);
579 trans = btrfs_start_transaction(root, 1);
580 btrfs_set_trans_block_group(trans, inode);
582 ret = btrfs_drop_extents(trans, root, inode,
583 page->index << PAGE_CACHE_SHIFT,
584 (page->index + 1) << PAGE_CACHE_SHIFT,
588 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
589 alloc_hint, (u64)-1, &ins, 1);
592 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
593 page->index << PAGE_CACHE_SHIFT,
597 SetPageChecked(page);
599 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
600 flush_dcache_page(page);
601 ret = btrfs_csum_file_block(trans, root, inode->i_ino,
602 page->index << PAGE_CACHE_SHIFT,
603 kaddr, PAGE_CACHE_SIZE);
605 btrfs_end_transaction(trans, root);
606 mutex_unlock(&root->fs_info->fs_mutex);
608 set_page_dirty(page);
610 page_cache_release(page);
615 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
617 struct inode *inode = dentry->d_inode;
620 err = inode_change_ok(inode, attr);
624 if (S_ISREG(inode->i_mode) &&
625 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
626 struct btrfs_trans_handle *trans;
627 struct btrfs_root *root = BTRFS_I(inode)->root;
628 u64 mask = root->blocksize - 1;
629 u64 pos = (inode->i_size + mask) & ~mask;
632 if (attr->ia_size <= pos)
635 btrfs_truncate_page(inode->i_mapping, inode->i_size);
637 hole_size = (attr->ia_size - pos + mask) & ~mask;
638 hole_size >>= inode->i_blkbits;
640 mutex_lock(&root->fs_info->fs_mutex);
641 trans = btrfs_start_transaction(root, 1);
642 btrfs_set_trans_block_group(trans, inode);
643 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
644 pos, 0, 0, hole_size);
645 btrfs_end_transaction(trans, root);
646 mutex_unlock(&root->fs_info->fs_mutex);
651 err = inode_setattr(inode, attr);
655 void btrfs_delete_inode(struct inode *inode)
657 struct btrfs_trans_handle *trans;
658 struct btrfs_root *root = BTRFS_I(inode)->root;
661 truncate_inode_pages(&inode->i_data, 0);
662 if (is_bad_inode(inode)) {
666 mutex_lock(&root->fs_info->fs_mutex);
667 trans = btrfs_start_transaction(root, 1);
668 btrfs_set_trans_block_group(trans, inode);
669 ret = btrfs_truncate_in_trans(trans, root, inode);
672 ret = btrfs_free_inode(trans, root, inode);
675 btrfs_end_transaction(trans, root);
676 mutex_unlock(&root->fs_info->fs_mutex);
677 btrfs_btree_balance_dirty(root);
681 btrfs_end_transaction(trans, root);
682 mutex_unlock(&root->fs_info->fs_mutex);
683 btrfs_btree_balance_dirty(root);
689 * this returns the key found in the dir entry in the location pointer.
690 * If no dir entries were found, location->objectid is 0.
692 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
693 struct btrfs_key *location)
695 const char *name = dentry->d_name.name;
696 int namelen = dentry->d_name.len;
697 struct btrfs_dir_item *di;
698 struct btrfs_path *path;
699 struct btrfs_root *root = BTRFS_I(dir)->root;
702 path = btrfs_alloc_path();
704 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
706 if (!di || IS_ERR(di)) {
707 location->objectid = 0;
711 btrfs_disk_key_to_cpu(location, &di->location);
713 btrfs_release_path(root, path);
714 btrfs_free_path(path);
719 * when we hit a tree root in a directory, the btrfs part of the inode
720 * needs to be changed to reflect the root directory of the tree root. This
721 * is kind of like crossing a mount point.
723 static int fixup_tree_root_location(struct btrfs_root *root,
724 struct btrfs_key *location,
725 struct btrfs_root **sub_root)
727 struct btrfs_path *path;
728 struct btrfs_root_item *ri;
730 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
732 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
735 path = btrfs_alloc_path();
737 mutex_lock(&root->fs_info->fs_mutex);
739 *sub_root = btrfs_read_fs_root(root->fs_info, location);
740 if (IS_ERR(*sub_root))
741 return PTR_ERR(*sub_root);
743 ri = &(*sub_root)->root_item;
744 location->objectid = btrfs_root_dirid(ri);
746 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
747 location->offset = 0;
749 btrfs_free_path(path);
750 mutex_unlock(&root->fs_info->fs_mutex);
754 static int btrfs_init_locked_inode(struct inode *inode, void *p)
756 struct btrfs_iget_args *args = p;
757 inode->i_ino = args->ino;
758 BTRFS_I(inode)->root = args->root;
762 static int btrfs_find_actor(struct inode *inode, void *opaque)
764 struct btrfs_iget_args *args = opaque;
765 return (args->ino == inode->i_ino &&
766 args->root == BTRFS_I(inode)->root);
769 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
770 struct btrfs_root *root)
773 struct btrfs_iget_args args;
777 inode = iget5_locked(s, objectid, btrfs_find_actor,
778 btrfs_init_locked_inode,
783 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
784 struct nameidata *nd)
786 struct inode * inode;
787 struct btrfs_inode *bi = BTRFS_I(dir);
788 struct btrfs_root *root = bi->root;
789 struct btrfs_root *sub_root = root;
790 struct btrfs_key location;
793 if (dentry->d_name.len > BTRFS_NAME_LEN)
794 return ERR_PTR(-ENAMETOOLONG);
795 mutex_lock(&root->fs_info->fs_mutex);
796 ret = btrfs_inode_by_name(dir, dentry, &location);
797 mutex_unlock(&root->fs_info->fs_mutex);
801 if (location.objectid) {
802 ret = fixup_tree_root_location(root, &location, &sub_root);
806 return ERR_PTR(-ENOENT);
807 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
810 return ERR_PTR(-EACCES);
811 if (inode->i_state & I_NEW) {
812 /* the inode and parent dir are two different roots */
813 if (sub_root != root) {
815 sub_root->inode = inode;
817 BTRFS_I(inode)->root = sub_root;
818 memcpy(&BTRFS_I(inode)->location, &location,
820 btrfs_read_locked_inode(inode);
821 unlock_new_inode(inode);
824 return d_splice_alias(inode, dentry);
828 * readahead one full node of leaves as long as their keys include
829 * the objectid supplied
831 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
834 struct btrfs_node *node;
844 node = btrfs_buffer_node(path->nodes[1]);
845 slot = path->slots[1];
846 nritems = btrfs_header_nritems(&node->header);
847 for (i = slot + 1; i < nritems; i++) {
848 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
849 if (item_objectid != objectid)
851 blocknr = btrfs_node_blockptr(node, i);
852 ret = readahead_tree_block(root, blocknr);
857 static unsigned char btrfs_filetype_table[] = {
858 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
861 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
863 struct inode *inode = filp->f_path.dentry->d_inode;
864 struct btrfs_root *root = BTRFS_I(inode)->root;
865 struct btrfs_item *item;
866 struct btrfs_dir_item *di;
867 struct btrfs_key key;
868 struct btrfs_path *path;
871 struct btrfs_leaf *leaf;
874 unsigned char d_type;
879 int key_type = BTRFS_DIR_INDEX_KEY;
881 /* FIXME, use a real flag for deciding about the key type */
882 if (root->fs_info->tree_root == root)
883 key_type = BTRFS_DIR_ITEM_KEY;
884 mutex_lock(&root->fs_info->fs_mutex);
885 key.objectid = inode->i_ino;
887 btrfs_set_key_type(&key, key_type);
888 key.offset = filp->f_pos;
889 path = btrfs_alloc_path();
890 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
894 reada_leaves(root, path, inode->i_ino);
896 leaf = btrfs_buffer_leaf(path->nodes[0]);
897 nritems = btrfs_header_nritems(&leaf->header);
898 slot = path->slots[0];
899 if (advance || slot >= nritems) {
900 if (slot >= nritems -1) {
901 reada_leaves(root, path, inode->i_ino);
902 ret = btrfs_next_leaf(root, path);
905 leaf = btrfs_buffer_leaf(path->nodes[0]);
906 nritems = btrfs_header_nritems(&leaf->header);
907 slot = path->slots[0];
914 item = leaf->items + slot;
915 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
917 if (btrfs_disk_key_type(&item->key) != key_type)
919 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
921 filp->f_pos = btrfs_disk_key_offset(&item->key);
923 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
925 di_total = btrfs_item_size(leaf->items + slot);
926 while(di_cur < di_total) {
927 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
928 over = filldir(dirent, (const char *)(di + 1),
929 btrfs_dir_name_len(di),
930 btrfs_disk_key_offset(&item->key),
931 btrfs_disk_key_objectid(&di->location),
935 di_len = btrfs_dir_name_len(di) + sizeof(*di);
937 di = (struct btrfs_dir_item *)((char *)di + di_len);
944 btrfs_release_path(root, path);
945 btrfs_free_path(path);
946 mutex_unlock(&root->fs_info->fs_mutex);
950 int btrfs_write_inode(struct inode *inode, int wait)
952 struct btrfs_root *root = BTRFS_I(inode)->root;
953 struct btrfs_trans_handle *trans;
957 mutex_lock(&root->fs_info->fs_mutex);
958 trans = btrfs_start_transaction(root, 1);
959 btrfs_set_trans_block_group(trans, inode);
960 ret = btrfs_commit_transaction(trans, root);
961 mutex_unlock(&root->fs_info->fs_mutex);
967 * This is somewhat expensive, updating the tree every time the
968 * inode changes. But, it is most likely to find the inode in cache.
969 * FIXME, needs more benchmarking...there are no reasons other than performance
970 * to keep or drop this code.
972 void btrfs_dirty_inode(struct inode *inode)
974 struct btrfs_root *root = BTRFS_I(inode)->root;
975 struct btrfs_trans_handle *trans;
977 mutex_lock(&root->fs_info->fs_mutex);
978 trans = btrfs_start_transaction(root, 1);
979 btrfs_set_trans_block_group(trans, inode);
980 btrfs_update_inode(trans, root, inode);
981 btrfs_end_transaction(trans, root);
982 mutex_unlock(&root->fs_info->fs_mutex);
985 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
988 struct btrfs_block_group_cache *group,
992 struct btrfs_inode_item inode_item;
993 struct btrfs_key *location;
997 inode = new_inode(root->fs_info->sb);
999 return ERR_PTR(-ENOMEM);
1001 BTRFS_I(inode)->root = root;
1006 group = btrfs_find_block_group(root, group, 0, 0, owner);
1007 BTRFS_I(inode)->block_group = group;
1009 inode->i_uid = current->fsuid;
1010 inode->i_gid = current->fsgid;
1011 inode->i_mode = mode;
1012 inode->i_ino = objectid;
1013 inode->i_blocks = 0;
1014 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1015 fill_inode_item(&inode_item, inode);
1016 location = &BTRFS_I(inode)->location;
1017 location->objectid = objectid;
1018 location->flags = 0;
1019 location->offset = 0;
1020 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1022 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1024 return ERR_PTR(ret);
1025 insert_inode_hash(inode);
1029 static inline u8 btrfs_inode_type(struct inode *inode)
1031 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1034 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1035 struct dentry *dentry, struct inode *inode)
1038 struct btrfs_key key;
1039 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1040 key.objectid = inode->i_ino;
1042 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1045 ret = btrfs_insert_dir_item(trans, root,
1046 dentry->d_name.name, dentry->d_name.len,
1047 dentry->d_parent->d_inode->i_ino,
1048 &key, btrfs_inode_type(inode));
1050 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
1051 ret = btrfs_update_inode(trans, root,
1052 dentry->d_parent->d_inode);
1057 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1058 struct dentry *dentry, struct inode *inode)
1060 int err = btrfs_add_link(trans, dentry, inode);
1062 d_instantiate(dentry, inode);
1070 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1071 int mode, struct nameidata *nd)
1073 struct btrfs_trans_handle *trans;
1074 struct btrfs_root *root = BTRFS_I(dir)->root;
1075 struct inode *inode;
1080 mutex_lock(&root->fs_info->fs_mutex);
1081 trans = btrfs_start_transaction(root, 1);
1082 btrfs_set_trans_block_group(trans, dir);
1084 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1090 inode = btrfs_new_inode(trans, root, objectid,
1091 BTRFS_I(dir)->block_group, mode);
1092 err = PTR_ERR(inode);
1096 btrfs_set_trans_block_group(trans, inode);
1097 err = btrfs_add_nondir(trans, dentry, inode);
1101 inode->i_mapping->a_ops = &btrfs_aops;
1102 inode->i_fop = &btrfs_file_operations;
1103 inode->i_op = &btrfs_file_inode_operations;
1105 dir->i_sb->s_dirt = 1;
1106 btrfs_update_inode_block_group(trans, inode);
1107 btrfs_update_inode_block_group(trans, dir);
1109 btrfs_end_transaction(trans, root);
1110 mutex_unlock(&root->fs_info->fs_mutex);
1113 inode_dec_link_count(inode);
1116 btrfs_btree_balance_dirty(root);
1120 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1121 struct dentry *dentry)
1123 struct btrfs_trans_handle *trans;
1124 struct btrfs_root *root = BTRFS_I(dir)->root;
1125 struct inode *inode = old_dentry->d_inode;
1129 if (inode->i_nlink == 0)
1133 mutex_lock(&root->fs_info->fs_mutex);
1134 trans = btrfs_start_transaction(root, 1);
1135 btrfs_set_trans_block_group(trans, dir);
1136 atomic_inc(&inode->i_count);
1137 err = btrfs_add_nondir(trans, dentry, inode);
1140 dir->i_sb->s_dirt = 1;
1141 btrfs_update_inode_block_group(trans, dir);
1142 err = btrfs_update_inode(trans, root, inode);
1146 btrfs_end_transaction(trans, root);
1147 mutex_unlock(&root->fs_info->fs_mutex);
1150 inode_dec_link_count(inode);
1153 btrfs_btree_balance_dirty(root);
1157 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root,
1159 u64 objectid, u64 dirid)
1163 struct btrfs_key key;
1168 key.objectid = objectid;
1171 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1173 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1174 &key, BTRFS_FT_DIR);
1177 key.objectid = dirid;
1178 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1179 &key, BTRFS_FT_DIR);
1186 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1188 struct inode *inode;
1189 struct btrfs_trans_handle *trans;
1190 struct btrfs_root *root = BTRFS_I(dir)->root;
1192 int drop_on_err = 0;
1195 mutex_lock(&root->fs_info->fs_mutex);
1196 trans = btrfs_start_transaction(root, 1);
1197 btrfs_set_trans_block_group(trans, dir);
1198 if (IS_ERR(trans)) {
1199 err = PTR_ERR(trans);
1203 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1209 inode = btrfs_new_inode(trans, root, objectid,
1210 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1211 if (IS_ERR(inode)) {
1212 err = PTR_ERR(inode);
1216 inode->i_op = &btrfs_dir_inode_operations;
1217 inode->i_fop = &btrfs_dir_file_operations;
1218 btrfs_set_trans_block_group(trans, inode);
1220 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1225 err = btrfs_update_inode(trans, root, inode);
1228 err = btrfs_add_link(trans, dentry, inode);
1231 d_instantiate(dentry, inode);
1233 dir->i_sb->s_dirt = 1;
1234 btrfs_update_inode_block_group(trans, inode);
1235 btrfs_update_inode_block_group(trans, dir);
1238 btrfs_end_transaction(trans, root);
1240 mutex_unlock(&root->fs_info->fs_mutex);
1243 btrfs_btree_balance_dirty(root);
1248 * FIBMAP and others want to pass in a fake buffer head. They need to
1249 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1250 * any packed file data into the fake bh
1252 #define BTRFS_GET_BLOCK_NO_CREATE 0
1253 #define BTRFS_GET_BLOCK_CREATE 1
1254 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1257 * FIXME create==1 doe not work.
1259 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1260 struct buffer_head *result, int create)
1265 u64 extent_start = 0;
1267 u64 objectid = inode->i_ino;
1270 struct btrfs_path *path;
1271 struct btrfs_root *root = BTRFS_I(inode)->root;
1272 struct btrfs_file_extent_item *item;
1273 struct btrfs_leaf *leaf;
1274 struct btrfs_disk_key *found_key;
1275 struct btrfs_trans_handle *trans = NULL;
1277 path = btrfs_alloc_path();
1279 if (create & BTRFS_GET_BLOCK_CREATE) {
1281 * danger!, this only works if the page is properly up
1284 trans = btrfs_start_transaction(root, 1);
1289 ret = btrfs_drop_extents(trans, root, inode,
1290 iblock << inode->i_blkbits,
1291 (iblock + 1) << inode->i_blkbits,
1296 ret = btrfs_lookup_file_extent(NULL, root, path,
1298 iblock << inode->i_blkbits, 0);
1305 if (path->slots[0] == 0) {
1306 btrfs_release_path(root, path);
1312 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1313 struct btrfs_file_extent_item);
1314 leaf = btrfs_buffer_leaf(path->nodes[0]);
1315 blocknr = btrfs_file_extent_disk_blocknr(item);
1316 blocknr += btrfs_file_extent_offset(item);
1318 /* are we inside the extent that was found? */
1319 found_key = &leaf->items[path->slots[0]].key;
1320 found_type = btrfs_disk_key_type(found_key);
1321 if (btrfs_disk_key_objectid(found_key) != objectid ||
1322 found_type != BTRFS_EXTENT_DATA_KEY) {
1327 found_type = btrfs_file_extent_type(item);
1328 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1329 if (found_type == BTRFS_FILE_EXTENT_REG) {
1330 extent_start = extent_start >> inode->i_blkbits;
1331 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1333 if (btrfs_file_extent_disk_blocknr(item) == 0)
1335 if (iblock >= extent_start && iblock < extent_end) {
1336 btrfs_map_bh_to_logical(root, result, blocknr +
1337 iblock - extent_start);
1340 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1345 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1349 size = btrfs_file_extent_inline_len(leaf->items +
1351 extent_end = (extent_start + size) >> inode->i_blkbits;
1352 extent_start >>= inode->i_blkbits;
1353 if (iblock < extent_start || iblock > extent_end) {
1356 ptr = btrfs_file_extent_inline_start(item);
1357 map = kmap(result->b_page);
1358 memcpy(map, ptr, size);
1359 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1360 flush_dcache_page(result->b_page);
1361 kunmap(result->b_page);
1362 set_buffer_uptodate(result);
1363 SetPageChecked(result->b_page);
1364 btrfs_map_bh_to_logical(root, result, 0);
1367 if (create & BTRFS_GET_BLOCK_CREATE) {
1368 struct btrfs_key ins;
1369 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1370 1, alloc_hint, (u64)-1,
1376 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1377 iblock << inode->i_blkbits,
1378 ins.objectid, ins.offset,
1384 btrfs_map_bh_to_logical(root, result, ins.objectid);
1388 ret = btrfs_end_transaction(trans, root);
1392 btrfs_free_path(path);
1396 int btrfs_get_block(struct inode *inode, sector_t iblock,
1397 struct buffer_head *result, int create)
1400 struct btrfs_root *root = BTRFS_I(inode)->root;
1401 mutex_lock(&root->fs_info->fs_mutex);
1402 err = btrfs_get_block_lock(inode, iblock, result, create);
1403 mutex_unlock(&root->fs_info->fs_mutex);
1407 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1408 struct buffer_head *result, int create)
1411 struct btrfs_root *root = BTRFS_I(inode)->root;
1412 struct page *page = result->b_page;
1413 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1414 struct btrfs_csum_item *item;
1415 struct btrfs_path *path = NULL;
1417 mutex_lock(&root->fs_info->fs_mutex);
1418 ret = btrfs_get_block_lock(inode, iblock, result, create);
1422 path = btrfs_alloc_path();
1423 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1425 ret = PTR_ERR(item);
1426 /* a csum that isn't present is a preallocated region. */
1427 if (ret == -ENOENT || ret == -EFBIG)
1429 result->b_private = NULL;
1432 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1435 btrfs_free_path(path);
1436 mutex_unlock(&root->fs_info->fs_mutex);
1440 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1441 struct buffer_head *result, int create)
1443 struct btrfs_root *root = BTRFS_I(inode)->root;
1444 mutex_lock(&root->fs_info->fs_mutex);
1445 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1446 mutex_unlock(&root->fs_info->fs_mutex);
1450 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1452 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1455 static int btrfs_prepare_write(struct file *file, struct page *page,
1456 unsigned from, unsigned to)
1458 return block_prepare_write(page, from, to, btrfs_get_block);
1461 static void buffer_io_error(struct buffer_head *bh)
1463 char b[BDEVNAME_SIZE];
1465 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1466 bdevname(bh->b_bdev, b),
1467 (unsigned long long)bh->b_blocknr);
1471 * I/O completion handler for block_read_full_page() - pages
1472 * which come unlocked at the end of I/O.
1474 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1476 unsigned long flags;
1477 struct buffer_head *first;
1478 struct buffer_head *tmp;
1480 int page_uptodate = 1;
1481 struct inode *inode;
1484 BUG_ON(!buffer_async_read(bh));
1487 inode = page->mapping->host;
1490 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1491 if (bh->b_private) {
1492 char csum[BTRFS_CRC32_SIZE];
1493 kaddr = kmap_atomic(page, KM_IRQ0);
1494 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1497 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1499 offset = (page->index << PAGE_CACHE_SHIFT) +
1501 printk("btrfs csum failed ino %lu off %llu\n",
1502 page->mapping->host->i_ino,
1503 (unsigned long long)offset);
1504 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1505 flush_dcache_page(page);
1507 kunmap_atomic(kaddr, KM_IRQ0);
1509 set_buffer_uptodate(bh);
1511 clear_buffer_uptodate(bh);
1512 if (printk_ratelimit())
1513 buffer_io_error(bh);
1518 * Be _very_ careful from here on. Bad things can happen if
1519 * two buffer heads end IO at almost the same time and both
1520 * decide that the page is now completely done.
1522 first = page_buffers(page);
1523 local_irq_save(flags);
1524 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1525 clear_buffer_async_read(bh);
1529 if (!buffer_uptodate(tmp))
1531 if (buffer_async_read(tmp)) {
1532 BUG_ON(!buffer_locked(tmp));
1535 tmp = tmp->b_this_page;
1536 } while (tmp != bh);
1537 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1538 local_irq_restore(flags);
1541 * If none of the buffers had errors and they are all
1542 * uptodate then we can set the page uptodate.
1544 if (page_uptodate && !PageError(page))
1545 SetPageUptodate(page);
1550 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1551 local_irq_restore(flags);
1556 * Generic "read page" function for block devices that have the normal
1557 * get_block functionality. This is most of the block device filesystems.
1558 * Reads the page asynchronously --- the unlock_buffer() and
1559 * set/clear_buffer_uptodate() functions propagate buffer state into the
1560 * page struct once IO has completed.
1562 int btrfs_readpage(struct file *file, struct page *page)
1564 struct inode *inode = page->mapping->host;
1565 sector_t iblock, lblock;
1566 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1567 unsigned int blocksize;
1569 int fully_mapped = 1;
1571 BUG_ON(!PageLocked(page));
1572 blocksize = 1 << inode->i_blkbits;
1573 if (!page_has_buffers(page))
1574 create_empty_buffers(page, blocksize, 0);
1575 head = page_buffers(page);
1577 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1578 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1584 if (buffer_uptodate(bh))
1587 if (!buffer_mapped(bh)) {
1591 if (iblock < lblock) {
1592 WARN_ON(bh->b_size != blocksize);
1593 err = btrfs_get_block_csum(inode, iblock,
1598 if (!buffer_mapped(bh)) {
1599 void *kaddr = kmap_atomic(page, KM_USER0);
1600 memset(kaddr + i * blocksize, 0, blocksize);
1601 flush_dcache_page(page);
1602 kunmap_atomic(kaddr, KM_USER0);
1604 set_buffer_uptodate(bh);
1608 * get_block() might have updated the buffer
1611 if (buffer_uptodate(bh))
1615 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1618 SetPageMappedToDisk(page);
1622 * All buffers are uptodate - we can set the page uptodate
1623 * as well. But not if get_block() returned an error.
1625 if (!PageError(page))
1626 SetPageUptodate(page);
1631 /* Stage two: lock the buffers */
1632 for (i = 0; i < nr; i++) {
1635 bh->b_end_io = btrfs_end_buffer_async_read;
1636 set_buffer_async_read(bh);
1640 * Stage 3: start the IO. Check for uptodateness
1641 * inside the buffer lock in case another process reading
1642 * the underlying blockdev brought it uptodate (the sct fix).
1644 for (i = 0; i < nr; i++) {
1646 if (buffer_uptodate(bh))
1647 btrfs_end_buffer_async_read(bh, 1);
1649 submit_bh(READ, bh);
1655 * Aside from a tiny bit of packed file data handling, this is the
1656 * same as the generic code.
1658 * While block_write_full_page is writing back the dirty buffers under
1659 * the page lock, whoever dirtied the buffers may decide to clean them
1660 * again at any time. We handle that by only looking at the buffer
1661 * state inside lock_buffer().
1663 * If block_write_full_page() is called for regular writeback
1664 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1665 * locked buffer. This only can happen if someone has written the buffer
1666 * directly, with submit_bh(). At the address_space level PageWriteback
1667 * prevents this contention from occurring.
1669 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1670 struct writeback_control *wbc)
1674 sector_t last_block;
1675 struct buffer_head *bh, *head;
1676 const unsigned blocksize = 1 << inode->i_blkbits;
1677 int nr_underway = 0;
1678 struct btrfs_root *root = BTRFS_I(inode)->root;
1680 BUG_ON(!PageLocked(page));
1682 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1684 /* no csumming allowed when from PF_MEMALLOC */
1685 if (current->flags & PF_MEMALLOC) {
1686 redirty_page_for_writepage(wbc, page);
1691 if (!page_has_buffers(page)) {
1692 create_empty_buffers(page, blocksize,
1693 (1 << BH_Dirty)|(1 << BH_Uptodate));
1697 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1698 * here, and the (potentially unmapped) buffers may become dirty at
1699 * any time. If a buffer becomes dirty here after we've inspected it
1700 * then we just miss that fact, and the page stays dirty.
1702 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1703 * handle that here by just cleaning them.
1706 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1707 head = page_buffers(page);
1711 * Get all the dirty buffers mapped to disk addresses and
1712 * handle any aliases from the underlying blockdev's mapping.
1715 if (block > last_block) {
1717 * mapped buffers outside i_size will occur, because
1718 * this page can be outside i_size when there is a
1719 * truncate in progress.
1722 * The buffer was zeroed by block_write_full_page()
1724 clear_buffer_dirty(bh);
1725 set_buffer_uptodate(bh);
1726 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1727 WARN_ON(bh->b_size != blocksize);
1728 err = btrfs_get_block(inode, block, bh, 0);
1732 if (buffer_new(bh)) {
1733 /* blockdev mappings never come here */
1734 clear_buffer_new(bh);
1737 bh = bh->b_this_page;
1739 } while (bh != head);
1742 if (!buffer_mapped(bh))
1745 * If it's a fully non-blocking write attempt and we cannot
1746 * lock the buffer then redirty the page. Note that this can
1747 * potentially cause a busy-wait loop from pdflush and kswapd
1748 * activity, but those code paths have their own higher-level
1751 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1753 } else if (test_set_buffer_locked(bh)) {
1754 redirty_page_for_writepage(wbc, page);
1757 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1758 struct btrfs_trans_handle *trans;
1760 u64 off = page->index << PAGE_CACHE_SHIFT;
1763 off += bh_offset(bh);
1764 mutex_lock(&root->fs_info->fs_mutex);
1765 trans = btrfs_start_transaction(root, 1);
1766 btrfs_set_trans_block_group(trans, inode);
1768 btrfs_csum_file_block(trans, root, inode->i_ino,
1769 off, kaddr + bh_offset(bh),
1772 ret = btrfs_end_transaction(trans, root);
1774 mutex_unlock(&root->fs_info->fs_mutex);
1775 mark_buffer_async_write(bh);
1779 } while ((bh = bh->b_this_page) != head);
1782 * The page and its buffers are protected by PageWriteback(), so we can
1783 * drop the bh refcounts early.
1785 BUG_ON(PageWriteback(page));
1786 set_page_writeback(page);
1789 struct buffer_head *next = bh->b_this_page;
1790 if (buffer_async_write(bh)) {
1791 submit_bh(WRITE, bh);
1795 } while (bh != head);
1800 if (nr_underway == 0) {
1802 * The page was marked dirty, but the buffers were
1803 * clean. Someone wrote them back by hand with
1804 * ll_rw_block/submit_bh. A rare case.
1808 if (!buffer_uptodate(bh)) {
1812 bh = bh->b_this_page;
1813 } while (bh != head);
1815 SetPageUptodate(page);
1816 end_page_writeback(page);
1822 * ENOSPC, or some other error. We may already have added some
1823 * blocks to the file, so we need to write these out to avoid
1824 * exposing stale data.
1825 * The page is currently locked and not marked for writeback
1828 /* Recovery: lock and submit the mapped buffers */
1830 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1832 mark_buffer_async_write(bh);
1835 * The buffer may have been set dirty during
1836 * attachment to a dirty page.
1838 clear_buffer_dirty(bh);
1840 } while ((bh = bh->b_this_page) != head);
1842 BUG_ON(PageWriteback(page));
1843 set_page_writeback(page);
1845 struct buffer_head *next = bh->b_this_page;
1846 if (buffer_async_write(bh)) {
1847 clear_buffer_dirty(bh);
1848 submit_bh(WRITE, bh);
1852 } while (bh != head);
1857 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1859 struct inode * const inode = page->mapping->host;
1860 loff_t i_size = i_size_read(inode);
1861 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1865 /* Is the page fully inside i_size? */
1866 if (page->index < end_index)
1867 return __btrfs_write_full_page(inode, page, wbc);
1869 /* Is the page fully outside i_size? (truncate in progress) */
1870 offset = i_size & (PAGE_CACHE_SIZE-1);
1871 if (page->index >= end_index+1 || !offset) {
1873 * The page may have dirty, unmapped buffers. For example,
1874 * they may have been added in ext3_writepage(). Make them
1875 * freeable here, so the page does not leak.
1877 block_invalidatepage(page, 0);
1879 return 0; /* don't care */
1883 * The page straddles i_size. It must be zeroed out on each and every
1884 * writepage invokation because it may be mmapped. "A file is mapped
1885 * in multiples of the page size. For a file that is not a multiple of
1886 * the page size, the remaining memory is zeroed when mapped, and
1887 * writes to that region are not written out to the file."
1889 kaddr = kmap_atomic(page, KM_USER0);
1890 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1891 flush_dcache_page(page);
1892 kunmap_atomic(kaddr, KM_USER0);
1893 return __btrfs_write_full_page(inode, page, wbc);
1897 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1898 * called from a page fault handler when a page is first dirtied. Hence we must
1899 * be careful to check for EOF conditions here. We set the page up correctly
1900 * for a written page which means we get ENOSPC checking when writing into
1901 * holes and correct delalloc and unwritten extent mapping on filesystems that
1902 * support these features.
1904 * We are not allowed to take the i_mutex here so we have to play games to
1905 * protect against truncate races as the page could now be beyond EOF. Because
1906 * vmtruncate() writes the inode size before removing pages, once we have the
1907 * page lock we can determine safely if the page is beyond EOF. If it is not
1908 * beyond EOF, then the page is guaranteed safe against truncation until we
1911 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1913 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1919 wait_on_page_writeback(page);
1920 size = i_size_read(inode);
1921 if ((page->mapping != inode->i_mapping) ||
1922 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1923 /* page got truncated out from underneath us */
1927 /* page is wholly or partially inside EOF */
1928 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1929 end = size & ~PAGE_CACHE_MASK;
1931 end = PAGE_CACHE_SIZE;
1933 ret = btrfs_prepare_write(NULL, page, 0, end);
1935 ret = btrfs_commit_write(NULL, page, 0, end);
1942 static void btrfs_truncate(struct inode *inode)
1944 struct btrfs_root *root = BTRFS_I(inode)->root;
1946 struct btrfs_trans_handle *trans;
1948 if (!S_ISREG(inode->i_mode))
1950 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1953 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1955 mutex_lock(&root->fs_info->fs_mutex);
1956 trans = btrfs_start_transaction(root, 1);
1957 btrfs_set_trans_block_group(trans, inode);
1959 /* FIXME, add redo link to tree so we don't leak on crash */
1960 ret = btrfs_truncate_in_trans(trans, root, inode);
1961 btrfs_update_inode(trans, root, inode);
1962 ret = btrfs_end_transaction(trans, root);
1964 mutex_unlock(&root->fs_info->fs_mutex);
1965 btrfs_btree_balance_dirty(root);
1968 int btrfs_commit_write(struct file *file, struct page *page,
1969 unsigned from, unsigned to)
1971 struct inode *inode = page->mapping->host;
1972 struct buffer_head *bh;
1973 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1975 SetPageUptodate(page);
1976 bh = page_buffers(page);
1977 set_buffer_uptodate(bh);
1978 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1979 set_page_dirty(page);
1981 if (pos > inode->i_size) {
1982 i_size_write(inode, pos);
1983 mark_inode_dirty(inode);
1988 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1990 struct btrfs_trans_handle *trans;
1991 struct btrfs_key key;
1992 struct btrfs_root_item root_item;
1993 struct btrfs_inode_item *inode_item;
1994 struct buffer_head *subvol;
1995 struct btrfs_leaf *leaf;
1996 struct btrfs_root *new_root;
1997 struct inode *inode;
2002 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2004 mutex_lock(&root->fs_info->fs_mutex);
2005 trans = btrfs_start_transaction(root, 1);
2008 subvol = btrfs_alloc_free_block(trans, root, 0);
2010 return PTR_ERR(subvol);
2011 leaf = btrfs_buffer_leaf(subvol);
2012 btrfs_set_header_nritems(&leaf->header, 0);
2013 btrfs_set_header_level(&leaf->header, 0);
2014 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2015 btrfs_set_header_generation(&leaf->header, trans->transid);
2016 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2017 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2018 sizeof(leaf->header.fsid));
2019 mark_buffer_dirty(subvol);
2021 inode_item = &root_item.inode;
2022 memset(inode_item, 0, sizeof(*inode_item));
2023 btrfs_set_inode_generation(inode_item, 1);
2024 btrfs_set_inode_size(inode_item, 3);
2025 btrfs_set_inode_nlink(inode_item, 1);
2026 btrfs_set_inode_nblocks(inode_item, 1);
2027 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2029 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2030 btrfs_set_root_refs(&root_item, 1);
2034 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2039 btrfs_set_root_dirid(&root_item, new_dirid);
2041 key.objectid = objectid;
2044 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2045 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2051 * insert the directory item
2053 key.offset = (u64)-1;
2054 dir = root->fs_info->sb->s_root->d_inode;
2055 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2056 name, namelen, dir->i_ino, &key,
2061 ret = btrfs_commit_transaction(trans, root);
2065 new_root = btrfs_read_fs_root(root->fs_info, &key);
2068 trans = btrfs_start_transaction(new_root, 1);
2071 inode = btrfs_new_inode(trans, new_root, new_dirid,
2072 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2075 inode->i_op = &btrfs_dir_inode_operations;
2076 inode->i_fop = &btrfs_dir_file_operations;
2077 new_root->inode = inode;
2079 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2085 ret = btrfs_update_inode(trans, new_root, inode);
2089 err = btrfs_commit_transaction(trans, root);
2093 mutex_unlock(&root->fs_info->fs_mutex);
2094 btrfs_btree_balance_dirty(root);
2098 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2100 struct btrfs_trans_handle *trans;
2101 struct btrfs_key key;
2102 struct btrfs_root_item new_root_item;
2107 if (!root->ref_cows)
2110 mutex_lock(&root->fs_info->fs_mutex);
2111 trans = btrfs_start_transaction(root, 1);
2114 ret = btrfs_update_inode(trans, root, root->inode);
2118 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2123 memcpy(&new_root_item, &root->root_item,
2124 sizeof(new_root_item));
2126 key.objectid = objectid;
2129 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2130 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2132 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2138 * insert the directory item
2140 key.offset = (u64)-1;
2141 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2143 root->fs_info->sb->s_root->d_inode->i_ino,
2144 &key, BTRFS_FT_DIR);
2149 ret = btrfs_inc_root_ref(trans, root);
2154 err = btrfs_commit_transaction(trans, root);
2157 mutex_unlock(&root->fs_info->fs_mutex);
2158 btrfs_btree_balance_dirty(root);
2162 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2163 cmd, unsigned long arg)
2165 struct btrfs_root *root = BTRFS_I(inode)->root;
2166 struct btrfs_ioctl_vol_args vol_args;
2168 struct btrfs_dir_item *di;
2170 struct btrfs_path *path;
2174 case BTRFS_IOC_SNAP_CREATE:
2175 if (copy_from_user(&vol_args,
2176 (struct btrfs_ioctl_vol_args __user *)arg,
2179 namelen = strlen(vol_args.name);
2180 if (namelen > BTRFS_VOL_NAME_MAX)
2182 if (strchr(vol_args.name, '/'))
2184 path = btrfs_alloc_path();
2187 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2188 mutex_lock(&root->fs_info->fs_mutex);
2189 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2191 vol_args.name, namelen, 0);
2192 mutex_unlock(&root->fs_info->fs_mutex);
2193 btrfs_free_path(path);
2194 if (di && !IS_ERR(di))
2199 if (root == root->fs_info->tree_root)
2200 ret = create_subvol(root, vol_args.name, namelen);
2202 ret = create_snapshot(root, vol_args.name, namelen);
2210 #ifdef CONFIG_COMPAT
2211 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2214 struct inode *inode = file->f_path.dentry->d_inode;
2217 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2225 * Called inside transaction, so use GFP_NOFS
2227 struct inode *btrfs_alloc_inode(struct super_block *sb)
2229 struct btrfs_inode *ei;
2231 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2234 return &ei->vfs_inode;
2237 void btrfs_destroy_inode(struct inode *inode)
2239 WARN_ON(!list_empty(&inode->i_dentry));
2240 WARN_ON(inode->i_data.nrpages);
2242 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2245 static void init_once(void * foo, struct kmem_cache * cachep,
2246 unsigned long flags)
2248 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2250 inode_init_once(&ei->vfs_inode);
2253 void btrfs_destroy_cachep(void)
2255 if (btrfs_inode_cachep)
2256 kmem_cache_destroy(btrfs_inode_cachep);
2257 if (btrfs_trans_handle_cachep)
2258 kmem_cache_destroy(btrfs_trans_handle_cachep);
2259 if (btrfs_transaction_cachep)
2260 kmem_cache_destroy(btrfs_transaction_cachep);
2261 if (btrfs_bit_radix_cachep)
2262 kmem_cache_destroy(btrfs_bit_radix_cachep);
2263 if (btrfs_path_cachep)
2264 kmem_cache_destroy(btrfs_path_cachep);
2267 int btrfs_init_cachep(void)
2269 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2270 sizeof(struct btrfs_inode),
2271 0, (SLAB_RECLAIM_ACCOUNT|
2274 if (!btrfs_inode_cachep)
2276 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2277 sizeof(struct btrfs_trans_handle),
2278 0, (SLAB_RECLAIM_ACCOUNT|
2281 if (!btrfs_trans_handle_cachep)
2283 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2284 sizeof(struct btrfs_transaction),
2285 0, (SLAB_RECLAIM_ACCOUNT|
2288 if (!btrfs_transaction_cachep)
2290 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2291 sizeof(struct btrfs_transaction),
2292 0, (SLAB_RECLAIM_ACCOUNT|
2295 if (!btrfs_path_cachep)
2297 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2299 0, (SLAB_RECLAIM_ACCOUNT|
2301 SLAB_DESTROY_BY_RCU),
2303 if (!btrfs_bit_radix_cachep)
2307 btrfs_destroy_cachep();
2311 static int btrfs_getattr(struct vfsmount *mnt,
2312 struct dentry *dentry, struct kstat *stat)
2314 struct inode *inode = dentry->d_inode;
2315 generic_fillattr(inode, stat);
2316 stat->blksize = 256 * 1024;
2320 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2321 struct inode * new_dir,struct dentry *new_dentry)
2323 struct btrfs_trans_handle *trans;
2324 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2325 struct inode *new_inode = new_dentry->d_inode;
2326 struct inode *old_inode = old_dentry->d_inode;
2327 struct timespec ctime = CURRENT_TIME;
2328 struct btrfs_path *path;
2329 struct btrfs_dir_item *di;
2332 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2333 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2336 mutex_lock(&root->fs_info->fs_mutex);
2337 trans = btrfs_start_transaction(root, 1);
2338 btrfs_set_trans_block_group(trans, new_dir);
2339 path = btrfs_alloc_path();
2345 old_dentry->d_inode->i_nlink++;
2346 old_dir->i_ctime = old_dir->i_mtime = ctime;
2347 new_dir->i_ctime = new_dir->i_mtime = ctime;
2348 old_inode->i_ctime = ctime;
2349 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2350 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2352 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2362 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2363 ret = btrfs_del_item(trans, root, path);
2367 btrfs_release_path(root, path);
2369 di = btrfs_lookup_dir_index_item(trans, root, path,
2381 ret = btrfs_del_item(trans, root, path);
2385 btrfs_release_path(root, path);
2387 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2388 old_inode->i_ino, location,
2395 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2400 new_inode->i_ctime = CURRENT_TIME;
2401 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2404 if (S_ISDIR(new_inode->i_mode))
2405 clear_nlink(new_inode);
2407 drop_nlink(new_inode);
2408 ret = btrfs_update_inode(trans, root, new_inode);
2412 ret = btrfs_add_link(trans, new_dentry, old_inode);
2417 btrfs_free_path(path);
2418 btrfs_end_transaction(trans, root);
2419 mutex_unlock(&root->fs_info->fs_mutex);
2423 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2424 const char *symname)
2426 struct btrfs_trans_handle *trans;
2427 struct btrfs_root *root = BTRFS_I(dir)->root;
2428 struct btrfs_path *path;
2429 struct btrfs_key key;
2430 struct inode *inode;
2437 struct btrfs_file_extent_item *ei;
2439 name_len = strlen(symname) + 1;
2440 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2441 return -ENAMETOOLONG;
2442 mutex_lock(&root->fs_info->fs_mutex);
2443 trans = btrfs_start_transaction(root, 1);
2444 btrfs_set_trans_block_group(trans, dir);
2446 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2452 inode = btrfs_new_inode(trans, root, objectid,
2453 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2454 err = PTR_ERR(inode);
2458 btrfs_set_trans_block_group(trans, inode);
2459 err = btrfs_add_nondir(trans, dentry, inode);
2463 inode->i_mapping->a_ops = &btrfs_aops;
2464 inode->i_fop = &btrfs_file_operations;
2465 inode->i_op = &btrfs_file_inode_operations;
2467 dir->i_sb->s_dirt = 1;
2468 btrfs_update_inode_block_group(trans, inode);
2469 btrfs_update_inode_block_group(trans, dir);
2473 path = btrfs_alloc_path();
2475 key.objectid = inode->i_ino;
2478 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2479 datasize = btrfs_file_extent_calc_inline_size(name_len);
2480 err = btrfs_insert_empty_item(trans, root, path, &key,
2486 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2487 path->slots[0], struct btrfs_file_extent_item);
2488 btrfs_set_file_extent_generation(ei, trans->transid);
2489 btrfs_set_file_extent_type(ei,
2490 BTRFS_FILE_EXTENT_INLINE);
2491 ptr = btrfs_file_extent_inline_start(ei);
2492 btrfs_memcpy(root, path->nodes[0]->b_data,
2493 ptr, symname, name_len);
2494 mark_buffer_dirty(path->nodes[0]);
2495 btrfs_free_path(path);
2496 inode->i_op = &btrfs_symlink_inode_operations;
2497 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2498 inode->i_size = name_len - 1;
2499 err = btrfs_update_inode(trans, root, inode);
2504 btrfs_end_transaction(trans, root);
2505 mutex_unlock(&root->fs_info->fs_mutex);
2507 inode_dec_link_count(inode);
2510 btrfs_btree_balance_dirty(root);
2514 static struct inode_operations btrfs_dir_inode_operations = {
2515 .lookup = btrfs_lookup,
2516 .create = btrfs_create,
2517 .unlink = btrfs_unlink,
2519 .mkdir = btrfs_mkdir,
2520 .rmdir = btrfs_rmdir,
2521 .rename = btrfs_rename,
2522 .symlink = btrfs_symlink,
2523 .setattr = btrfs_setattr,
2526 static struct inode_operations btrfs_dir_ro_inode_operations = {
2527 .lookup = btrfs_lookup,
2530 static struct file_operations btrfs_dir_file_operations = {
2531 .llseek = generic_file_llseek,
2532 .read = generic_read_dir,
2533 .readdir = btrfs_readdir,
2534 .ioctl = btrfs_ioctl,
2535 #ifdef CONFIG_COMPAT
2536 .compat_ioctl = btrfs_compat_ioctl,
2540 static struct address_space_operations btrfs_aops = {
2541 .readpage = btrfs_readpage,
2542 .writepage = btrfs_writepage,
2543 .sync_page = block_sync_page,
2544 .prepare_write = btrfs_prepare_write,
2545 .commit_write = btrfs_commit_write,
2549 static struct address_space_operations btrfs_symlink_aops = {
2550 .readpage = btrfs_readpage,
2551 .writepage = btrfs_writepage,
2554 static struct inode_operations btrfs_file_inode_operations = {
2555 .truncate = btrfs_truncate,
2556 .getattr = btrfs_getattr,
2557 .setattr = btrfs_setattr,
2560 static struct inode_operations btrfs_symlink_inode_operations = {
2561 .readlink = generic_readlink,
2562 .follow_link = page_follow_link_light,
2563 .put_link = page_put_link,