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 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
254 btrfs_update_inode(trans, root, dir);
255 drop_nlink(dentry->d_inode);
256 ret = btrfs_update_inode(trans, root, dentry->d_inode);
257 dir->i_sb->s_dirt = 1;
262 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
264 struct btrfs_root *root;
265 struct btrfs_trans_handle *trans;
268 root = BTRFS_I(dir)->root;
269 mutex_lock(&root->fs_info->fs_mutex);
270 trans = btrfs_start_transaction(root, 1);
271 btrfs_set_trans_block_group(trans, dir);
272 ret = btrfs_unlink_trans(trans, root, dir, dentry);
273 btrfs_end_transaction(trans, root);
274 mutex_unlock(&root->fs_info->fs_mutex);
275 btrfs_btree_balance_dirty(root);
279 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
281 struct inode *inode = dentry->d_inode;
284 struct btrfs_root *root = BTRFS_I(dir)->root;
285 struct btrfs_path *path;
286 struct btrfs_key key;
287 struct btrfs_trans_handle *trans;
288 struct btrfs_key found_key;
290 struct btrfs_leaf *leaf;
291 char *goodnames = "..";
293 path = btrfs_alloc_path();
295 mutex_lock(&root->fs_info->fs_mutex);
296 trans = btrfs_start_transaction(root, 1);
297 btrfs_set_trans_block_group(trans, dir);
298 key.objectid = inode->i_ino;
299 key.offset = (u64)-1;
302 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
308 if (path->slots[0] == 0) {
313 leaf = btrfs_buffer_leaf(path->nodes[0]);
314 btrfs_disk_key_to_cpu(&found_key,
315 &leaf->items[path->slots[0]].key);
316 found_type = btrfs_key_type(&found_key);
317 if (found_key.objectid != inode->i_ino) {
321 if ((found_type != BTRFS_DIR_ITEM_KEY &&
322 found_type != BTRFS_DIR_INDEX_KEY) ||
323 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
324 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
328 ret = btrfs_del_item(trans, root, path);
331 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
333 btrfs_release_path(root, path);
336 btrfs_release_path(root, path);
338 /* now the directory is empty */
339 err = btrfs_unlink_trans(trans, root, dir, dentry);
344 btrfs_release_path(root, path);
345 btrfs_free_path(path);
346 mutex_unlock(&root->fs_info->fs_mutex);
347 ret = btrfs_end_transaction(trans, root);
348 btrfs_btree_balance_dirty(root);
354 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
355 struct btrfs_root *root,
358 struct btrfs_path *path;
363 path = btrfs_alloc_path();
365 ret = btrfs_lookup_inode(trans, root, path,
366 &BTRFS_I(inode)->location, -1);
370 ret = btrfs_del_item(trans, root, path);
371 btrfs_free_path(path);
376 * truncates go from a high offset to a low offset. So, walk
377 * from hi to lo in the node and issue readas. Stop when you find
378 * keys from a different objectid
380 static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
383 struct btrfs_node *node;
393 node = btrfs_buffer_node(path->nodes[1]);
394 slot = path->slots[1];
397 nritems = btrfs_header_nritems(&node->header);
398 for (i = slot - 1; i >= 0; i--) {
399 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
400 if (item_objectid != objectid)
402 blocknr = btrfs_node_blockptr(node, i);
403 ret = readahead_tree_block(root, blocknr);
410 * this can truncate away extent items, csum items and directory items.
411 * It starts at a high offset and removes keys until it can't find
412 * any higher than i_size.
414 * csum items that cross the new i_size are truncated to the new size
417 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
418 struct btrfs_root *root,
422 struct btrfs_path *path;
423 struct btrfs_key key;
424 struct btrfs_disk_key *found_key;
426 struct btrfs_leaf *leaf;
427 struct btrfs_file_extent_item *fi;
428 u64 extent_start = 0;
429 u64 extent_num_blocks = 0;
434 path = btrfs_alloc_path();
436 /* FIXME, add redo link to tree so we don't leak on crash */
437 key.objectid = inode->i_ino;
438 key.offset = (u64)-1;
441 btrfs_init_path(path);
443 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
448 BUG_ON(path->slots[0] == 0);
451 reada_truncate(root, path, inode->i_ino);
452 leaf = btrfs_buffer_leaf(path->nodes[0]);
453 found_key = &leaf->items[path->slots[0]].key;
454 found_type = btrfs_disk_key_type(found_key);
456 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
458 if (found_type != BTRFS_CSUM_ITEM_KEY &&
459 found_type != BTRFS_DIR_ITEM_KEY &&
460 found_type != BTRFS_DIR_INDEX_KEY &&
461 found_type != BTRFS_EXTENT_DATA_KEY)
464 item_end = btrfs_disk_key_offset(found_key);
465 if (found_type == BTRFS_EXTENT_DATA_KEY) {
466 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
468 struct btrfs_file_extent_item);
469 if (btrfs_file_extent_type(fi) !=
470 BTRFS_FILE_EXTENT_INLINE) {
471 item_end += btrfs_file_extent_num_blocks(fi) <<
475 if (found_type == BTRFS_CSUM_ITEM_KEY) {
476 ret = btrfs_csum_truncate(trans, root, path,
480 if (item_end < inode->i_size) {
482 btrfs_set_key_type(&key, found_type - 1);
487 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
493 /* FIXME, shrink the extent if the ref count is only 1 */
494 if (found_type == BTRFS_EXTENT_DATA_KEY &&
495 btrfs_file_extent_type(fi) !=
496 BTRFS_FILE_EXTENT_INLINE) {
499 u64 orig_num_blocks =
500 btrfs_file_extent_num_blocks(fi);
501 extent_num_blocks = inode->i_size -
502 btrfs_disk_key_offset(found_key) +
504 extent_num_blocks >>= inode->i_blkbits;
505 btrfs_set_file_extent_num_blocks(fi,
507 inode->i_blocks -= (orig_num_blocks -
508 extent_num_blocks) << 3;
509 mark_buffer_dirty(path->nodes[0]);
512 btrfs_file_extent_disk_blocknr(fi);
514 btrfs_file_extent_disk_num_blocks(fi);
515 /* FIXME blocksize != 4096 */
516 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
517 if (extent_start != 0) {
519 inode->i_blocks -= num_dec;
524 ret = btrfs_del_item(trans, root, path);
530 btrfs_release_path(root, path);
532 ret = btrfs_free_extent(trans, root, extent_start,
533 extent_num_blocks, 0);
539 btrfs_release_path(root, path);
540 btrfs_free_path(path);
541 inode->i_sb->s_dirt = 1;
546 * taken from block_truncate_page, but does cow as it zeros out
547 * any bytes left in the last page in the file.
549 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
551 struct inode *inode = mapping->host;
552 unsigned blocksize = 1 << inode->i_blkbits;
553 pgoff_t index = from >> PAGE_CACHE_SHIFT;
554 unsigned offset = from & (PAGE_CACHE_SIZE-1);
558 struct btrfs_root *root = BTRFS_I(inode)->root;
560 struct btrfs_key ins;
561 struct btrfs_trans_handle *trans;
563 if ((offset & (blocksize - 1)) == 0)
567 page = grab_cache_page(mapping, index);
571 if (!PageUptodate(page)) {
572 ret = btrfs_readpage(NULL, page);
574 if (!PageUptodate(page)) {
579 mutex_lock(&root->fs_info->fs_mutex);
580 trans = btrfs_start_transaction(root, 1);
581 btrfs_set_trans_block_group(trans, inode);
583 ret = btrfs_drop_extents(trans, root, inode,
584 page->index << PAGE_CACHE_SHIFT,
585 (page->index + 1) << PAGE_CACHE_SHIFT,
589 ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
590 alloc_hint, (u64)-1, &ins, 1);
593 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
594 page->index << PAGE_CACHE_SHIFT,
598 SetPageChecked(page);
600 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
601 flush_dcache_page(page);
602 ret = btrfs_csum_file_block(trans, root, inode->i_ino,
603 page->index << PAGE_CACHE_SHIFT,
604 kaddr, PAGE_CACHE_SIZE);
606 btrfs_end_transaction(trans, root);
607 mutex_unlock(&root->fs_info->fs_mutex);
609 set_page_dirty(page);
611 page_cache_release(page);
616 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
618 struct inode *inode = dentry->d_inode;
621 err = inode_change_ok(inode, attr);
625 if (S_ISREG(inode->i_mode) &&
626 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
627 struct btrfs_trans_handle *trans;
628 struct btrfs_root *root = BTRFS_I(inode)->root;
629 u64 mask = root->blocksize - 1;
630 u64 pos = (inode->i_size + mask) & ~mask;
633 if (attr->ia_size <= pos)
636 btrfs_truncate_page(inode->i_mapping, inode->i_size);
638 hole_size = (attr->ia_size - pos + mask) & ~mask;
639 hole_size >>= inode->i_blkbits;
641 mutex_lock(&root->fs_info->fs_mutex);
642 trans = btrfs_start_transaction(root, 1);
643 btrfs_set_trans_block_group(trans, inode);
644 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
645 pos, 0, 0, hole_size);
646 btrfs_end_transaction(trans, root);
647 mutex_unlock(&root->fs_info->fs_mutex);
652 err = inode_setattr(inode, attr);
656 void btrfs_delete_inode(struct inode *inode)
658 struct btrfs_trans_handle *trans;
659 struct btrfs_root *root = BTRFS_I(inode)->root;
662 truncate_inode_pages(&inode->i_data, 0);
663 if (is_bad_inode(inode)) {
667 mutex_lock(&root->fs_info->fs_mutex);
668 trans = btrfs_start_transaction(root, 1);
669 btrfs_set_trans_block_group(trans, inode);
670 ret = btrfs_truncate_in_trans(trans, root, inode);
673 ret = btrfs_free_inode(trans, root, inode);
676 btrfs_end_transaction(trans, root);
677 mutex_unlock(&root->fs_info->fs_mutex);
678 btrfs_btree_balance_dirty(root);
682 btrfs_end_transaction(trans, root);
683 mutex_unlock(&root->fs_info->fs_mutex);
684 btrfs_btree_balance_dirty(root);
690 * this returns the key found in the dir entry in the location pointer.
691 * If no dir entries were found, location->objectid is 0.
693 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
694 struct btrfs_key *location)
696 const char *name = dentry->d_name.name;
697 int namelen = dentry->d_name.len;
698 struct btrfs_dir_item *di;
699 struct btrfs_path *path;
700 struct btrfs_root *root = BTRFS_I(dir)->root;
703 path = btrfs_alloc_path();
705 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
707 if (!di || IS_ERR(di)) {
708 location->objectid = 0;
712 btrfs_disk_key_to_cpu(location, &di->location);
714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
720 * when we hit a tree root in a directory, the btrfs part of the inode
721 * needs to be changed to reflect the root directory of the tree root. This
722 * is kind of like crossing a mount point.
724 static int fixup_tree_root_location(struct btrfs_root *root,
725 struct btrfs_key *location,
726 struct btrfs_root **sub_root)
728 struct btrfs_path *path;
729 struct btrfs_root_item *ri;
731 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
733 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
736 path = btrfs_alloc_path();
738 mutex_lock(&root->fs_info->fs_mutex);
740 *sub_root = btrfs_read_fs_root(root->fs_info, location);
741 if (IS_ERR(*sub_root))
742 return PTR_ERR(*sub_root);
744 ri = &(*sub_root)->root_item;
745 location->objectid = btrfs_root_dirid(ri);
747 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
748 location->offset = 0;
750 btrfs_free_path(path);
751 mutex_unlock(&root->fs_info->fs_mutex);
755 static int btrfs_init_locked_inode(struct inode *inode, void *p)
757 struct btrfs_iget_args *args = p;
758 inode->i_ino = args->ino;
759 BTRFS_I(inode)->root = args->root;
763 static int btrfs_find_actor(struct inode *inode, void *opaque)
765 struct btrfs_iget_args *args = opaque;
766 return (args->ino == inode->i_ino &&
767 args->root == BTRFS_I(inode)->root);
770 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
771 struct btrfs_root *root)
774 struct btrfs_iget_args args;
778 inode = iget5_locked(s, objectid, btrfs_find_actor,
779 btrfs_init_locked_inode,
784 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
785 struct nameidata *nd)
787 struct inode * inode;
788 struct btrfs_inode *bi = BTRFS_I(dir);
789 struct btrfs_root *root = bi->root;
790 struct btrfs_root *sub_root = root;
791 struct btrfs_key location;
794 if (dentry->d_name.len > BTRFS_NAME_LEN)
795 return ERR_PTR(-ENAMETOOLONG);
796 mutex_lock(&root->fs_info->fs_mutex);
797 ret = btrfs_inode_by_name(dir, dentry, &location);
798 mutex_unlock(&root->fs_info->fs_mutex);
802 if (location.objectid) {
803 ret = fixup_tree_root_location(root, &location, &sub_root);
807 return ERR_PTR(-ENOENT);
808 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
811 return ERR_PTR(-EACCES);
812 if (inode->i_state & I_NEW) {
813 /* the inode and parent dir are two different roots */
814 if (sub_root != root) {
816 sub_root->inode = inode;
818 BTRFS_I(inode)->root = sub_root;
819 memcpy(&BTRFS_I(inode)->location, &location,
821 btrfs_read_locked_inode(inode);
822 unlock_new_inode(inode);
825 return d_splice_alias(inode, dentry);
829 * readahead one full node of leaves as long as their keys include
830 * the objectid supplied
832 static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
835 struct btrfs_node *node;
845 node = btrfs_buffer_node(path->nodes[1]);
846 slot = path->slots[1];
847 nritems = btrfs_header_nritems(&node->header);
848 for (i = slot + 1; i < nritems; i++) {
849 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
850 if (item_objectid != objectid)
852 blocknr = btrfs_node_blockptr(node, i);
853 ret = readahead_tree_block(root, blocknr);
858 static unsigned char btrfs_filetype_table[] = {
859 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
862 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
864 struct inode *inode = filp->f_path.dentry->d_inode;
865 struct btrfs_root *root = BTRFS_I(inode)->root;
866 struct btrfs_item *item;
867 struct btrfs_dir_item *di;
868 struct btrfs_key key;
869 struct btrfs_path *path;
872 struct btrfs_leaf *leaf;
875 unsigned char d_type;
880 int key_type = BTRFS_DIR_INDEX_KEY;
882 /* FIXME, use a real flag for deciding about the key type */
883 if (root->fs_info->tree_root == root)
884 key_type = BTRFS_DIR_ITEM_KEY;
885 mutex_lock(&root->fs_info->fs_mutex);
886 key.objectid = inode->i_ino;
888 btrfs_set_key_type(&key, key_type);
889 key.offset = filp->f_pos;
890 path = btrfs_alloc_path();
891 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
895 reada_leaves(root, path, inode->i_ino);
897 leaf = btrfs_buffer_leaf(path->nodes[0]);
898 nritems = btrfs_header_nritems(&leaf->header);
899 slot = path->slots[0];
900 if (advance || slot >= nritems) {
901 if (slot >= nritems -1) {
902 reada_leaves(root, path, inode->i_ino);
903 ret = btrfs_next_leaf(root, path);
906 leaf = btrfs_buffer_leaf(path->nodes[0]);
907 nritems = btrfs_header_nritems(&leaf->header);
908 slot = path->slots[0];
915 item = leaf->items + slot;
916 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
918 if (btrfs_disk_key_type(&item->key) != key_type)
920 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
922 filp->f_pos = btrfs_disk_key_offset(&item->key);
924 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
926 di_total = btrfs_item_size(leaf->items + slot);
927 while(di_cur < di_total) {
928 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
929 over = filldir(dirent, (const char *)(di + 1),
930 btrfs_dir_name_len(di),
931 btrfs_disk_key_offset(&item->key),
932 btrfs_disk_key_objectid(&di->location),
936 di_len = btrfs_dir_name_len(di) + sizeof(*di);
938 di = (struct btrfs_dir_item *)((char *)di + di_len);
945 btrfs_release_path(root, path);
946 btrfs_free_path(path);
947 mutex_unlock(&root->fs_info->fs_mutex);
951 int btrfs_write_inode(struct inode *inode, int wait)
953 struct btrfs_root *root = BTRFS_I(inode)->root;
954 struct btrfs_trans_handle *trans;
958 mutex_lock(&root->fs_info->fs_mutex);
959 trans = btrfs_start_transaction(root, 1);
960 btrfs_set_trans_block_group(trans, inode);
961 ret = btrfs_commit_transaction(trans, root);
962 mutex_unlock(&root->fs_info->fs_mutex);
968 * This is somewhat expensive, updating the tree every time the
969 * inode changes. But, it is most likely to find the inode in cache.
970 * FIXME, needs more benchmarking...there are no reasons other than performance
971 * to keep or drop this code.
973 void btrfs_dirty_inode(struct inode *inode)
975 struct btrfs_root *root = BTRFS_I(inode)->root;
976 struct btrfs_trans_handle *trans;
978 mutex_lock(&root->fs_info->fs_mutex);
979 trans = btrfs_start_transaction(root, 1);
980 btrfs_set_trans_block_group(trans, inode);
981 btrfs_update_inode(trans, root, inode);
982 btrfs_end_transaction(trans, root);
983 mutex_unlock(&root->fs_info->fs_mutex);
986 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
989 struct btrfs_block_group_cache *group,
993 struct btrfs_inode_item inode_item;
994 struct btrfs_key *location;
998 inode = new_inode(root->fs_info->sb);
1000 return ERR_PTR(-ENOMEM);
1002 BTRFS_I(inode)->root = root;
1007 group = btrfs_find_block_group(root, group, 0, 0, owner);
1008 BTRFS_I(inode)->block_group = group;
1010 inode->i_uid = current->fsuid;
1011 inode->i_gid = current->fsgid;
1012 inode->i_mode = mode;
1013 inode->i_ino = objectid;
1014 inode->i_blocks = 0;
1015 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1016 fill_inode_item(&inode_item, inode);
1017 location = &BTRFS_I(inode)->location;
1018 location->objectid = objectid;
1019 location->flags = 0;
1020 location->offset = 0;
1021 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1023 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
1025 return ERR_PTR(ret);
1026 insert_inode_hash(inode);
1030 static inline u8 btrfs_inode_type(struct inode *inode)
1032 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1035 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1036 struct dentry *dentry, struct inode *inode)
1039 struct btrfs_key key;
1040 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1041 struct inode *parent_inode;
1042 key.objectid = inode->i_ino;
1044 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1047 ret = btrfs_insert_dir_item(trans, root,
1048 dentry->d_name.name, dentry->d_name.len,
1049 dentry->d_parent->d_inode->i_ino,
1050 &key, btrfs_inode_type(inode));
1052 parent_inode = dentry->d_parent->d_inode;
1053 parent_inode->i_size += dentry->d_name.len * 2;
1054 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1055 ret = btrfs_update_inode(trans, root,
1056 dentry->d_parent->d_inode);
1061 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1062 struct dentry *dentry, struct inode *inode)
1064 int err = btrfs_add_link(trans, dentry, inode);
1066 d_instantiate(dentry, inode);
1074 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1075 int mode, struct nameidata *nd)
1077 struct btrfs_trans_handle *trans;
1078 struct btrfs_root *root = BTRFS_I(dir)->root;
1079 struct inode *inode;
1084 mutex_lock(&root->fs_info->fs_mutex);
1085 trans = btrfs_start_transaction(root, 1);
1086 btrfs_set_trans_block_group(trans, dir);
1088 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1094 inode = btrfs_new_inode(trans, root, objectid,
1095 BTRFS_I(dir)->block_group, mode);
1096 err = PTR_ERR(inode);
1100 btrfs_set_trans_block_group(trans, inode);
1101 err = btrfs_add_nondir(trans, dentry, inode);
1105 inode->i_mapping->a_ops = &btrfs_aops;
1106 inode->i_fop = &btrfs_file_operations;
1107 inode->i_op = &btrfs_file_inode_operations;
1109 dir->i_sb->s_dirt = 1;
1110 btrfs_update_inode_block_group(trans, inode);
1111 btrfs_update_inode_block_group(trans, dir);
1113 btrfs_end_transaction(trans, root);
1114 mutex_unlock(&root->fs_info->fs_mutex);
1117 inode_dec_link_count(inode);
1120 btrfs_btree_balance_dirty(root);
1124 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1125 struct dentry *dentry)
1127 struct btrfs_trans_handle *trans;
1128 struct btrfs_root *root = BTRFS_I(dir)->root;
1129 struct inode *inode = old_dentry->d_inode;
1133 if (inode->i_nlink == 0)
1137 mutex_lock(&root->fs_info->fs_mutex);
1138 trans = btrfs_start_transaction(root, 1);
1139 btrfs_set_trans_block_group(trans, dir);
1140 atomic_inc(&inode->i_count);
1141 err = btrfs_add_nondir(trans, dentry, inode);
1144 dir->i_sb->s_dirt = 1;
1145 btrfs_update_inode_block_group(trans, dir);
1146 err = btrfs_update_inode(trans, root, inode);
1150 btrfs_end_transaction(trans, root);
1151 mutex_unlock(&root->fs_info->fs_mutex);
1154 inode_dec_link_count(inode);
1157 btrfs_btree_balance_dirty(root);
1161 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1162 struct btrfs_root *root,
1163 u64 objectid, u64 dirid)
1167 struct btrfs_key key;
1172 key.objectid = objectid;
1175 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1177 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1178 &key, BTRFS_FT_DIR);
1181 key.objectid = dirid;
1182 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1183 &key, BTRFS_FT_DIR);
1190 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1192 struct inode *inode;
1193 struct btrfs_trans_handle *trans;
1194 struct btrfs_root *root = BTRFS_I(dir)->root;
1196 int drop_on_err = 0;
1199 mutex_lock(&root->fs_info->fs_mutex);
1200 trans = btrfs_start_transaction(root, 1);
1201 btrfs_set_trans_block_group(trans, dir);
1202 if (IS_ERR(trans)) {
1203 err = PTR_ERR(trans);
1207 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1213 inode = btrfs_new_inode(trans, root, objectid,
1214 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1215 if (IS_ERR(inode)) {
1216 err = PTR_ERR(inode);
1220 inode->i_op = &btrfs_dir_inode_operations;
1221 inode->i_fop = &btrfs_dir_file_operations;
1222 btrfs_set_trans_block_group(trans, inode);
1224 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1229 err = btrfs_update_inode(trans, root, inode);
1232 err = btrfs_add_link(trans, dentry, inode);
1235 d_instantiate(dentry, inode);
1237 dir->i_sb->s_dirt = 1;
1238 btrfs_update_inode_block_group(trans, inode);
1239 btrfs_update_inode_block_group(trans, dir);
1242 btrfs_end_transaction(trans, root);
1244 mutex_unlock(&root->fs_info->fs_mutex);
1247 btrfs_btree_balance_dirty(root);
1252 * FIBMAP and others want to pass in a fake buffer head. They need to
1253 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1254 * any packed file data into the fake bh
1256 #define BTRFS_GET_BLOCK_NO_CREATE 0
1257 #define BTRFS_GET_BLOCK_CREATE 1
1258 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1261 * FIXME create==1 doe not work.
1263 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1264 struct buffer_head *result, int create)
1269 u64 extent_start = 0;
1271 u64 objectid = inode->i_ino;
1274 struct btrfs_path *path;
1275 struct btrfs_root *root = BTRFS_I(inode)->root;
1276 struct btrfs_file_extent_item *item;
1277 struct btrfs_leaf *leaf;
1278 struct btrfs_disk_key *found_key;
1279 struct btrfs_trans_handle *trans = NULL;
1281 path = btrfs_alloc_path();
1283 if (create & BTRFS_GET_BLOCK_CREATE) {
1285 * danger!, this only works if the page is properly up
1288 trans = btrfs_start_transaction(root, 1);
1293 ret = btrfs_drop_extents(trans, root, inode,
1294 iblock << inode->i_blkbits,
1295 (iblock + 1) << inode->i_blkbits,
1300 ret = btrfs_lookup_file_extent(NULL, root, path,
1302 iblock << inode->i_blkbits, 0);
1309 if (path->slots[0] == 0) {
1310 btrfs_release_path(root, path);
1316 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1317 struct btrfs_file_extent_item);
1318 leaf = btrfs_buffer_leaf(path->nodes[0]);
1319 blocknr = btrfs_file_extent_disk_blocknr(item);
1320 blocknr += btrfs_file_extent_offset(item);
1322 /* are we inside the extent that was found? */
1323 found_key = &leaf->items[path->slots[0]].key;
1324 found_type = btrfs_disk_key_type(found_key);
1325 if (btrfs_disk_key_objectid(found_key) != objectid ||
1326 found_type != BTRFS_EXTENT_DATA_KEY) {
1331 found_type = btrfs_file_extent_type(item);
1332 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1333 if (found_type == BTRFS_FILE_EXTENT_REG) {
1334 extent_start = extent_start >> inode->i_blkbits;
1335 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1337 if (btrfs_file_extent_disk_blocknr(item) == 0)
1339 if (iblock >= extent_start && iblock < extent_end) {
1340 btrfs_map_bh_to_logical(root, result, blocknr +
1341 iblock - extent_start);
1344 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1349 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1353 size = btrfs_file_extent_inline_len(leaf->items +
1355 extent_end = (extent_start + size) >> inode->i_blkbits;
1356 extent_start >>= inode->i_blkbits;
1357 if (iblock < extent_start || iblock > extent_end) {
1360 ptr = btrfs_file_extent_inline_start(item);
1361 map = kmap(result->b_page);
1362 memcpy(map, ptr, size);
1363 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1364 flush_dcache_page(result->b_page);
1365 kunmap(result->b_page);
1366 set_buffer_uptodate(result);
1367 SetPageChecked(result->b_page);
1368 btrfs_map_bh_to_logical(root, result, 0);
1371 if (create & BTRFS_GET_BLOCK_CREATE) {
1372 struct btrfs_key ins;
1373 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1374 1, alloc_hint, (u64)-1,
1380 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1381 iblock << inode->i_blkbits,
1382 ins.objectid, ins.offset,
1388 btrfs_map_bh_to_logical(root, result, ins.objectid);
1392 ret = btrfs_end_transaction(trans, root);
1396 btrfs_free_path(path);
1400 int btrfs_get_block(struct inode *inode, sector_t iblock,
1401 struct buffer_head *result, int create)
1404 struct btrfs_root *root = BTRFS_I(inode)->root;
1405 mutex_lock(&root->fs_info->fs_mutex);
1406 err = btrfs_get_block_lock(inode, iblock, result, create);
1407 mutex_unlock(&root->fs_info->fs_mutex);
1411 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1412 struct buffer_head *result, int create)
1415 struct btrfs_root *root = BTRFS_I(inode)->root;
1416 struct page *page = result->b_page;
1417 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1418 struct btrfs_csum_item *item;
1419 struct btrfs_path *path = NULL;
1421 mutex_lock(&root->fs_info->fs_mutex);
1422 ret = btrfs_get_block_lock(inode, iblock, result, create);
1426 path = btrfs_alloc_path();
1427 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1429 ret = PTR_ERR(item);
1430 /* a csum that isn't present is a preallocated region. */
1431 if (ret == -ENOENT || ret == -EFBIG)
1433 result->b_private = NULL;
1436 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1439 btrfs_free_path(path);
1440 mutex_unlock(&root->fs_info->fs_mutex);
1444 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1445 struct buffer_head *result, int create)
1447 struct btrfs_root *root = BTRFS_I(inode)->root;
1448 mutex_lock(&root->fs_info->fs_mutex);
1449 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1450 mutex_unlock(&root->fs_info->fs_mutex);
1454 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1456 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1459 static int btrfs_prepare_write(struct file *file, struct page *page,
1460 unsigned from, unsigned to)
1462 return block_prepare_write(page, from, to, btrfs_get_block);
1465 static void buffer_io_error(struct buffer_head *bh)
1467 char b[BDEVNAME_SIZE];
1469 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1470 bdevname(bh->b_bdev, b),
1471 (unsigned long long)bh->b_blocknr);
1475 * I/O completion handler for block_read_full_page() - pages
1476 * which come unlocked at the end of I/O.
1478 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1480 unsigned long flags;
1481 struct buffer_head *first;
1482 struct buffer_head *tmp;
1484 int page_uptodate = 1;
1485 struct inode *inode;
1488 BUG_ON(!buffer_async_read(bh));
1491 inode = page->mapping->host;
1494 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1495 if (bh->b_private) {
1496 char csum[BTRFS_CRC32_SIZE];
1497 kaddr = kmap_atomic(page, KM_IRQ0);
1498 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1501 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1503 offset = (page->index << PAGE_CACHE_SHIFT) +
1505 printk("btrfs csum failed ino %lu off %llu\n",
1506 page->mapping->host->i_ino,
1507 (unsigned long long)offset);
1508 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1509 flush_dcache_page(page);
1511 kunmap_atomic(kaddr, KM_IRQ0);
1513 set_buffer_uptodate(bh);
1515 clear_buffer_uptodate(bh);
1516 if (printk_ratelimit())
1517 buffer_io_error(bh);
1522 * Be _very_ careful from here on. Bad things can happen if
1523 * two buffer heads end IO at almost the same time and both
1524 * decide that the page is now completely done.
1526 first = page_buffers(page);
1527 local_irq_save(flags);
1528 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1529 clear_buffer_async_read(bh);
1533 if (!buffer_uptodate(tmp))
1535 if (buffer_async_read(tmp)) {
1536 BUG_ON(!buffer_locked(tmp));
1539 tmp = tmp->b_this_page;
1540 } while (tmp != bh);
1541 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1542 local_irq_restore(flags);
1545 * If none of the buffers had errors and they are all
1546 * uptodate then we can set the page uptodate.
1548 if (page_uptodate && !PageError(page))
1549 SetPageUptodate(page);
1554 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1555 local_irq_restore(flags);
1560 * Generic "read page" function for block devices that have the normal
1561 * get_block functionality. This is most of the block device filesystems.
1562 * Reads the page asynchronously --- the unlock_buffer() and
1563 * set/clear_buffer_uptodate() functions propagate buffer state into the
1564 * page struct once IO has completed.
1566 int btrfs_readpage(struct file *file, struct page *page)
1568 struct inode *inode = page->mapping->host;
1569 sector_t iblock, lblock;
1570 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1571 unsigned int blocksize;
1573 int fully_mapped = 1;
1575 BUG_ON(!PageLocked(page));
1576 blocksize = 1 << inode->i_blkbits;
1577 if (!page_has_buffers(page))
1578 create_empty_buffers(page, blocksize, 0);
1579 head = page_buffers(page);
1581 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1582 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1588 if (buffer_uptodate(bh))
1591 if (!buffer_mapped(bh)) {
1595 if (iblock < lblock) {
1596 WARN_ON(bh->b_size != blocksize);
1597 err = btrfs_get_block_csum(inode, iblock,
1602 if (!buffer_mapped(bh)) {
1603 void *kaddr = kmap_atomic(page, KM_USER0);
1604 memset(kaddr + i * blocksize, 0, blocksize);
1605 flush_dcache_page(page);
1606 kunmap_atomic(kaddr, KM_USER0);
1608 set_buffer_uptodate(bh);
1612 * get_block() might have updated the buffer
1615 if (buffer_uptodate(bh))
1619 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1622 SetPageMappedToDisk(page);
1626 * All buffers are uptodate - we can set the page uptodate
1627 * as well. But not if get_block() returned an error.
1629 if (!PageError(page))
1630 SetPageUptodate(page);
1635 /* Stage two: lock the buffers */
1636 for (i = 0; i < nr; i++) {
1639 bh->b_end_io = btrfs_end_buffer_async_read;
1640 set_buffer_async_read(bh);
1644 * Stage 3: start the IO. Check for uptodateness
1645 * inside the buffer lock in case another process reading
1646 * the underlying blockdev brought it uptodate (the sct fix).
1648 for (i = 0; i < nr; i++) {
1650 if (buffer_uptodate(bh))
1651 btrfs_end_buffer_async_read(bh, 1);
1653 submit_bh(READ, bh);
1659 * Aside from a tiny bit of packed file data handling, this is the
1660 * same as the generic code.
1662 * While block_write_full_page is writing back the dirty buffers under
1663 * the page lock, whoever dirtied the buffers may decide to clean them
1664 * again at any time. We handle that by only looking at the buffer
1665 * state inside lock_buffer().
1667 * If block_write_full_page() is called for regular writeback
1668 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1669 * locked buffer. This only can happen if someone has written the buffer
1670 * directly, with submit_bh(). At the address_space level PageWriteback
1671 * prevents this contention from occurring.
1673 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1674 struct writeback_control *wbc)
1678 sector_t last_block;
1679 struct buffer_head *bh, *head;
1680 const unsigned blocksize = 1 << inode->i_blkbits;
1681 int nr_underway = 0;
1682 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 BUG_ON(!PageLocked(page));
1686 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1688 /* no csumming allowed when from PF_MEMALLOC */
1689 if (current->flags & PF_MEMALLOC) {
1690 redirty_page_for_writepage(wbc, page);
1695 if (!page_has_buffers(page)) {
1696 create_empty_buffers(page, blocksize,
1697 (1 << BH_Dirty)|(1 << BH_Uptodate));
1701 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1702 * here, and the (potentially unmapped) buffers may become dirty at
1703 * any time. If a buffer becomes dirty here after we've inspected it
1704 * then we just miss that fact, and the page stays dirty.
1706 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1707 * handle that here by just cleaning them.
1710 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1711 head = page_buffers(page);
1715 * Get all the dirty buffers mapped to disk addresses and
1716 * handle any aliases from the underlying blockdev's mapping.
1719 if (block > last_block) {
1721 * mapped buffers outside i_size will occur, because
1722 * this page can be outside i_size when there is a
1723 * truncate in progress.
1726 * The buffer was zeroed by block_write_full_page()
1728 clear_buffer_dirty(bh);
1729 set_buffer_uptodate(bh);
1730 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1731 WARN_ON(bh->b_size != blocksize);
1732 err = btrfs_get_block(inode, block, bh, 0);
1736 if (buffer_new(bh)) {
1737 /* blockdev mappings never come here */
1738 clear_buffer_new(bh);
1741 bh = bh->b_this_page;
1743 } while (bh != head);
1746 if (!buffer_mapped(bh))
1749 * If it's a fully non-blocking write attempt and we cannot
1750 * lock the buffer then redirty the page. Note that this can
1751 * potentially cause a busy-wait loop from pdflush and kswapd
1752 * activity, but those code paths have their own higher-level
1755 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1757 } else if (test_set_buffer_locked(bh)) {
1758 redirty_page_for_writepage(wbc, page);
1761 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1762 struct btrfs_trans_handle *trans;
1764 u64 off = page->index << PAGE_CACHE_SHIFT;
1767 off += bh_offset(bh);
1768 mutex_lock(&root->fs_info->fs_mutex);
1769 trans = btrfs_start_transaction(root, 1);
1770 btrfs_set_trans_block_group(trans, inode);
1772 btrfs_csum_file_block(trans, root, inode->i_ino,
1773 off, kaddr + bh_offset(bh),
1776 ret = btrfs_end_transaction(trans, root);
1778 mutex_unlock(&root->fs_info->fs_mutex);
1779 mark_buffer_async_write(bh);
1783 } while ((bh = bh->b_this_page) != head);
1786 * The page and its buffers are protected by PageWriteback(), so we can
1787 * drop the bh refcounts early.
1789 BUG_ON(PageWriteback(page));
1790 set_page_writeback(page);
1793 struct buffer_head *next = bh->b_this_page;
1794 if (buffer_async_write(bh)) {
1795 submit_bh(WRITE, bh);
1799 } while (bh != head);
1804 if (nr_underway == 0) {
1806 * The page was marked dirty, but the buffers were
1807 * clean. Someone wrote them back by hand with
1808 * ll_rw_block/submit_bh. A rare case.
1812 if (!buffer_uptodate(bh)) {
1816 bh = bh->b_this_page;
1817 } while (bh != head);
1819 SetPageUptodate(page);
1820 end_page_writeback(page);
1826 * ENOSPC, or some other error. We may already have added some
1827 * blocks to the file, so we need to write these out to avoid
1828 * exposing stale data.
1829 * The page is currently locked and not marked for writeback
1832 /* Recovery: lock and submit the mapped buffers */
1834 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1836 mark_buffer_async_write(bh);
1839 * The buffer may have been set dirty during
1840 * attachment to a dirty page.
1842 clear_buffer_dirty(bh);
1844 } while ((bh = bh->b_this_page) != head);
1846 BUG_ON(PageWriteback(page));
1847 set_page_writeback(page);
1849 struct buffer_head *next = bh->b_this_page;
1850 if (buffer_async_write(bh)) {
1851 clear_buffer_dirty(bh);
1852 submit_bh(WRITE, bh);
1856 } while (bh != head);
1861 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1863 struct inode * const inode = page->mapping->host;
1864 loff_t i_size = i_size_read(inode);
1865 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1869 /* Is the page fully inside i_size? */
1870 if (page->index < end_index)
1871 return __btrfs_write_full_page(inode, page, wbc);
1873 /* Is the page fully outside i_size? (truncate in progress) */
1874 offset = i_size & (PAGE_CACHE_SIZE-1);
1875 if (page->index >= end_index+1 || !offset) {
1877 * The page may have dirty, unmapped buffers. For example,
1878 * they may have been added in ext3_writepage(). Make them
1879 * freeable here, so the page does not leak.
1881 block_invalidatepage(page, 0);
1883 return 0; /* don't care */
1887 * The page straddles i_size. It must be zeroed out on each and every
1888 * writepage invokation because it may be mmapped. "A file is mapped
1889 * in multiples of the page size. For a file that is not a multiple of
1890 * the page size, the remaining memory is zeroed when mapped, and
1891 * writes to that region are not written out to the file."
1893 kaddr = kmap_atomic(page, KM_USER0);
1894 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1895 flush_dcache_page(page);
1896 kunmap_atomic(kaddr, KM_USER0);
1897 return __btrfs_write_full_page(inode, page, wbc);
1901 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1902 * called from a page fault handler when a page is first dirtied. Hence we must
1903 * be careful to check for EOF conditions here. We set the page up correctly
1904 * for a written page which means we get ENOSPC checking when writing into
1905 * holes and correct delalloc and unwritten extent mapping on filesystems that
1906 * support these features.
1908 * We are not allowed to take the i_mutex here so we have to play games to
1909 * protect against truncate races as the page could now be beyond EOF. Because
1910 * vmtruncate() writes the inode size before removing pages, once we have the
1911 * page lock we can determine safely if the page is beyond EOF. If it is not
1912 * beyond EOF, then the page is guaranteed safe against truncation until we
1915 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1917 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1923 wait_on_page_writeback(page);
1924 size = i_size_read(inode);
1925 if ((page->mapping != inode->i_mapping) ||
1926 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1927 /* page got truncated out from underneath us */
1931 /* page is wholly or partially inside EOF */
1932 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1933 end = size & ~PAGE_CACHE_MASK;
1935 end = PAGE_CACHE_SIZE;
1937 ret = btrfs_prepare_write(NULL, page, 0, end);
1939 ret = btrfs_commit_write(NULL, page, 0, end);
1946 static void btrfs_truncate(struct inode *inode)
1948 struct btrfs_root *root = BTRFS_I(inode)->root;
1950 struct btrfs_trans_handle *trans;
1952 if (!S_ISREG(inode->i_mode))
1954 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1957 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1959 mutex_lock(&root->fs_info->fs_mutex);
1960 trans = btrfs_start_transaction(root, 1);
1961 btrfs_set_trans_block_group(trans, inode);
1963 /* FIXME, add redo link to tree so we don't leak on crash */
1964 ret = btrfs_truncate_in_trans(trans, root, inode);
1965 btrfs_update_inode(trans, root, inode);
1966 ret = btrfs_end_transaction(trans, root);
1968 mutex_unlock(&root->fs_info->fs_mutex);
1969 btrfs_btree_balance_dirty(root);
1972 int btrfs_commit_write(struct file *file, struct page *page,
1973 unsigned from, unsigned to)
1975 struct inode *inode = page->mapping->host;
1976 struct buffer_head *bh;
1977 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1979 SetPageUptodate(page);
1980 bh = page_buffers(page);
1981 set_buffer_uptodate(bh);
1982 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1983 set_page_dirty(page);
1985 if (pos > inode->i_size) {
1986 i_size_write(inode, pos);
1987 mark_inode_dirty(inode);
1992 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1994 struct btrfs_trans_handle *trans;
1995 struct btrfs_key key;
1996 struct btrfs_root_item root_item;
1997 struct btrfs_inode_item *inode_item;
1998 struct buffer_head *subvol;
1999 struct btrfs_leaf *leaf;
2000 struct btrfs_root *new_root;
2001 struct inode *inode;
2006 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2008 mutex_lock(&root->fs_info->fs_mutex);
2009 trans = btrfs_start_transaction(root, 1);
2012 subvol = btrfs_alloc_free_block(trans, root, 0);
2014 return PTR_ERR(subvol);
2015 leaf = btrfs_buffer_leaf(subvol);
2016 btrfs_set_header_nritems(&leaf->header, 0);
2017 btrfs_set_header_level(&leaf->header, 0);
2018 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2019 btrfs_set_header_generation(&leaf->header, trans->transid);
2020 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2021 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2022 sizeof(leaf->header.fsid));
2023 mark_buffer_dirty(subvol);
2025 inode_item = &root_item.inode;
2026 memset(inode_item, 0, sizeof(*inode_item));
2027 btrfs_set_inode_generation(inode_item, 1);
2028 btrfs_set_inode_size(inode_item, 3);
2029 btrfs_set_inode_nlink(inode_item, 1);
2030 btrfs_set_inode_nblocks(inode_item, 1);
2031 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2033 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2034 btrfs_set_root_refs(&root_item, 1);
2035 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2036 root_item.drop_level = 0;
2040 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2045 btrfs_set_root_dirid(&root_item, new_dirid);
2047 key.objectid = objectid;
2050 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2051 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2057 * insert the directory item
2059 key.offset = (u64)-1;
2060 dir = root->fs_info->sb->s_root->d_inode;
2061 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2062 name, namelen, dir->i_ino, &key,
2067 ret = btrfs_commit_transaction(trans, root);
2071 new_root = btrfs_read_fs_root(root->fs_info, &key);
2074 trans = btrfs_start_transaction(new_root, 1);
2077 inode = btrfs_new_inode(trans, new_root, new_dirid,
2078 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2081 inode->i_op = &btrfs_dir_inode_operations;
2082 inode->i_fop = &btrfs_dir_file_operations;
2083 new_root->inode = inode;
2085 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2091 ret = btrfs_update_inode(trans, new_root, inode);
2095 err = btrfs_commit_transaction(trans, root);
2099 mutex_unlock(&root->fs_info->fs_mutex);
2100 btrfs_btree_balance_dirty(root);
2104 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2106 struct btrfs_trans_handle *trans;
2107 struct btrfs_key key;
2108 struct btrfs_root_item new_root_item;
2113 if (!root->ref_cows)
2116 mutex_lock(&root->fs_info->fs_mutex);
2117 trans = btrfs_start_transaction(root, 1);
2120 ret = btrfs_update_inode(trans, root, root->inode);
2124 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2129 memcpy(&new_root_item, &root->root_item,
2130 sizeof(new_root_item));
2132 key.objectid = objectid;
2135 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2136 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2138 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2144 * insert the directory item
2146 key.offset = (u64)-1;
2147 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2149 root->fs_info->sb->s_root->d_inode->i_ino,
2150 &key, BTRFS_FT_DIR);
2155 ret = btrfs_inc_root_ref(trans, root);
2160 err = btrfs_commit_transaction(trans, root);
2163 mutex_unlock(&root->fs_info->fs_mutex);
2164 btrfs_btree_balance_dirty(root);
2168 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2169 cmd, unsigned long arg)
2171 struct btrfs_root *root = BTRFS_I(inode)->root;
2172 struct btrfs_ioctl_vol_args vol_args;
2174 struct btrfs_dir_item *di;
2176 struct btrfs_path *path;
2180 case BTRFS_IOC_SNAP_CREATE:
2181 if (copy_from_user(&vol_args,
2182 (struct btrfs_ioctl_vol_args __user *)arg,
2185 namelen = strlen(vol_args.name);
2186 if (namelen > BTRFS_VOL_NAME_MAX)
2188 if (strchr(vol_args.name, '/'))
2190 path = btrfs_alloc_path();
2193 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2194 mutex_lock(&root->fs_info->fs_mutex);
2195 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2197 vol_args.name, namelen, 0);
2198 mutex_unlock(&root->fs_info->fs_mutex);
2199 btrfs_free_path(path);
2200 if (di && !IS_ERR(di))
2205 if (root == root->fs_info->tree_root)
2206 ret = create_subvol(root, vol_args.name, namelen);
2208 ret = create_snapshot(root, vol_args.name, namelen);
2216 #ifdef CONFIG_COMPAT
2217 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2220 struct inode *inode = file->f_path.dentry->d_inode;
2223 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2231 * Called inside transaction, so use GFP_NOFS
2233 struct inode *btrfs_alloc_inode(struct super_block *sb)
2235 struct btrfs_inode *ei;
2237 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2240 return &ei->vfs_inode;
2243 void btrfs_destroy_inode(struct inode *inode)
2245 WARN_ON(!list_empty(&inode->i_dentry));
2246 WARN_ON(inode->i_data.nrpages);
2248 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2251 static void init_once(void * foo, struct kmem_cache * cachep,
2252 unsigned long flags)
2254 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2256 inode_init_once(&ei->vfs_inode);
2259 void btrfs_destroy_cachep(void)
2261 if (btrfs_inode_cachep)
2262 kmem_cache_destroy(btrfs_inode_cachep);
2263 if (btrfs_trans_handle_cachep)
2264 kmem_cache_destroy(btrfs_trans_handle_cachep);
2265 if (btrfs_transaction_cachep)
2266 kmem_cache_destroy(btrfs_transaction_cachep);
2267 if (btrfs_bit_radix_cachep)
2268 kmem_cache_destroy(btrfs_bit_radix_cachep);
2269 if (btrfs_path_cachep)
2270 kmem_cache_destroy(btrfs_path_cachep);
2273 int btrfs_init_cachep(void)
2275 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2276 sizeof(struct btrfs_inode),
2277 0, (SLAB_RECLAIM_ACCOUNT|
2280 if (!btrfs_inode_cachep)
2282 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2283 sizeof(struct btrfs_trans_handle),
2284 0, (SLAB_RECLAIM_ACCOUNT|
2287 if (!btrfs_trans_handle_cachep)
2289 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2290 sizeof(struct btrfs_transaction),
2291 0, (SLAB_RECLAIM_ACCOUNT|
2294 if (!btrfs_transaction_cachep)
2296 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2297 sizeof(struct btrfs_transaction),
2298 0, (SLAB_RECLAIM_ACCOUNT|
2301 if (!btrfs_path_cachep)
2303 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2305 0, (SLAB_RECLAIM_ACCOUNT|
2307 SLAB_DESTROY_BY_RCU),
2309 if (!btrfs_bit_radix_cachep)
2313 btrfs_destroy_cachep();
2317 static int btrfs_getattr(struct vfsmount *mnt,
2318 struct dentry *dentry, struct kstat *stat)
2320 struct inode *inode = dentry->d_inode;
2321 generic_fillattr(inode, stat);
2322 stat->blksize = 256 * 1024;
2326 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2327 struct inode * new_dir,struct dentry *new_dentry)
2329 struct btrfs_trans_handle *trans;
2330 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2331 struct inode *new_inode = new_dentry->d_inode;
2332 struct inode *old_inode = old_dentry->d_inode;
2333 struct timespec ctime = CURRENT_TIME;
2334 struct btrfs_path *path;
2335 struct btrfs_dir_item *di;
2338 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2339 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2342 mutex_lock(&root->fs_info->fs_mutex);
2343 trans = btrfs_start_transaction(root, 1);
2344 btrfs_set_trans_block_group(trans, new_dir);
2345 path = btrfs_alloc_path();
2351 old_dentry->d_inode->i_nlink++;
2352 old_dir->i_ctime = old_dir->i_mtime = ctime;
2353 new_dir->i_ctime = new_dir->i_mtime = ctime;
2354 old_inode->i_ctime = ctime;
2355 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2356 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2358 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2368 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2369 ret = btrfs_del_item(trans, root, path);
2373 btrfs_release_path(root, path);
2375 di = btrfs_lookup_dir_index_item(trans, root, path,
2387 ret = btrfs_del_item(trans, root, path);
2391 btrfs_release_path(root, path);
2393 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2394 old_inode->i_ino, location,
2401 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2406 new_inode->i_ctime = CURRENT_TIME;
2407 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2410 if (S_ISDIR(new_inode->i_mode))
2411 clear_nlink(new_inode);
2413 drop_nlink(new_inode);
2414 ret = btrfs_update_inode(trans, root, new_inode);
2418 ret = btrfs_add_link(trans, new_dentry, old_inode);
2423 btrfs_free_path(path);
2424 btrfs_end_transaction(trans, root);
2425 mutex_unlock(&root->fs_info->fs_mutex);
2429 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2430 const char *symname)
2432 struct btrfs_trans_handle *trans;
2433 struct btrfs_root *root = BTRFS_I(dir)->root;
2434 struct btrfs_path *path;
2435 struct btrfs_key key;
2436 struct inode *inode;
2443 struct btrfs_file_extent_item *ei;
2445 name_len = strlen(symname) + 1;
2446 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2447 return -ENAMETOOLONG;
2448 mutex_lock(&root->fs_info->fs_mutex);
2449 trans = btrfs_start_transaction(root, 1);
2450 btrfs_set_trans_block_group(trans, dir);
2452 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2458 inode = btrfs_new_inode(trans, root, objectid,
2459 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2460 err = PTR_ERR(inode);
2464 btrfs_set_trans_block_group(trans, inode);
2465 err = btrfs_add_nondir(trans, dentry, inode);
2469 inode->i_mapping->a_ops = &btrfs_aops;
2470 inode->i_fop = &btrfs_file_operations;
2471 inode->i_op = &btrfs_file_inode_operations;
2473 dir->i_sb->s_dirt = 1;
2474 btrfs_update_inode_block_group(trans, inode);
2475 btrfs_update_inode_block_group(trans, dir);
2479 path = btrfs_alloc_path();
2481 key.objectid = inode->i_ino;
2484 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2485 datasize = btrfs_file_extent_calc_inline_size(name_len);
2486 err = btrfs_insert_empty_item(trans, root, path, &key,
2492 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2493 path->slots[0], struct btrfs_file_extent_item);
2494 btrfs_set_file_extent_generation(ei, trans->transid);
2495 btrfs_set_file_extent_type(ei,
2496 BTRFS_FILE_EXTENT_INLINE);
2497 ptr = btrfs_file_extent_inline_start(ei);
2498 btrfs_memcpy(root, path->nodes[0]->b_data,
2499 ptr, symname, name_len);
2500 mark_buffer_dirty(path->nodes[0]);
2501 btrfs_free_path(path);
2502 inode->i_op = &btrfs_symlink_inode_operations;
2503 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2504 inode->i_size = name_len - 1;
2505 err = btrfs_update_inode(trans, root, inode);
2510 btrfs_end_transaction(trans, root);
2511 mutex_unlock(&root->fs_info->fs_mutex);
2513 inode_dec_link_count(inode);
2516 btrfs_btree_balance_dirty(root);
2520 static struct inode_operations btrfs_dir_inode_operations = {
2521 .lookup = btrfs_lookup,
2522 .create = btrfs_create,
2523 .unlink = btrfs_unlink,
2525 .mkdir = btrfs_mkdir,
2526 .rmdir = btrfs_rmdir,
2527 .rename = btrfs_rename,
2528 .symlink = btrfs_symlink,
2529 .setattr = btrfs_setattr,
2532 static struct inode_operations btrfs_dir_ro_inode_operations = {
2533 .lookup = btrfs_lookup,
2536 static struct file_operations btrfs_dir_file_operations = {
2537 .llseek = generic_file_llseek,
2538 .read = generic_read_dir,
2539 .readdir = btrfs_readdir,
2540 .ioctl = btrfs_ioctl,
2541 #ifdef CONFIG_COMPAT
2542 .compat_ioctl = btrfs_compat_ioctl,
2546 static struct address_space_operations btrfs_aops = {
2547 .readpage = btrfs_readpage,
2548 .writepage = btrfs_writepage,
2549 .sync_page = block_sync_page,
2550 .prepare_write = btrfs_prepare_write,
2551 .commit_write = btrfs_commit_write,
2555 static struct address_space_operations btrfs_symlink_aops = {
2556 .readpage = btrfs_readpage,
2557 .writepage = btrfs_writepage,
2560 static struct inode_operations btrfs_file_inode_operations = {
2561 .truncate = btrfs_truncate,
2562 .getattr = btrfs_getattr,
2563 .setattr = btrfs_setattr,
2566 static struct inode_operations btrfs_symlink_inode_operations = {
2567 .readlink = generic_readlink,
2568 .follow_link = page_follow_link_light,
2569 .put_link = page_put_link,