2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
41 struct btrfs_iget_args {
43 struct btrfs_root *root;
46 static struct inode_operations btrfs_dir_inode_operations;
47 static struct inode_operations btrfs_symlink_inode_operations;
48 static struct inode_operations btrfs_dir_ro_inode_operations;
49 static struct inode_operations btrfs_file_inode_operations;
50 static struct address_space_operations btrfs_aops;
51 static struct address_space_operations btrfs_symlink_aops;
52 static struct file_operations btrfs_dir_file_operations;
54 static struct kmem_cache *btrfs_inode_cachep;
55 struct kmem_cache *btrfs_trans_handle_cachep;
56 struct kmem_cache *btrfs_transaction_cachep;
57 struct kmem_cache *btrfs_bit_radix_cachep;
58 struct kmem_cache *btrfs_path_cachep;
61 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
62 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
63 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
64 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
65 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
66 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
67 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
68 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
71 void btrfs_read_locked_inode(struct inode *inode)
73 struct btrfs_path *path;
74 struct btrfs_inode_item *inode_item;
75 struct btrfs_root *root = BTRFS_I(inode)->root;
76 struct btrfs_key location;
77 u64 alloc_group_block;
80 path = btrfs_alloc_path();
82 mutex_lock(&root->fs_info->fs_mutex);
84 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
85 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
87 btrfs_free_path(path);
90 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
92 struct btrfs_inode_item);
94 inode->i_mode = btrfs_inode_mode(inode_item);
95 inode->i_nlink = btrfs_inode_nlink(inode_item);
96 inode->i_uid = btrfs_inode_uid(inode_item);
97 inode->i_gid = btrfs_inode_gid(inode_item);
98 inode->i_size = btrfs_inode_size(inode_item);
99 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
100 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
101 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
102 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
103 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
104 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
105 inode->i_blocks = btrfs_inode_nblocks(inode_item);
106 inode->i_generation = btrfs_inode_generation(inode_item);
107 alloc_group_block = btrfs_inode_block_group(inode_item);
108 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
111 btrfs_free_path(path);
114 mutex_unlock(&root->fs_info->fs_mutex);
116 switch (inode->i_mode & S_IFMT) {
119 init_special_inode(inode, inode->i_mode,
120 btrfs_inode_rdev(inode_item));
124 inode->i_mapping->a_ops = &btrfs_aops;
125 inode->i_fop = &btrfs_file_operations;
126 inode->i_op = &btrfs_file_inode_operations;
129 inode->i_fop = &btrfs_dir_file_operations;
130 if (root == root->fs_info->tree_root)
131 inode->i_op = &btrfs_dir_ro_inode_operations;
133 inode->i_op = &btrfs_dir_inode_operations;
136 inode->i_op = &btrfs_symlink_inode_operations;
137 inode->i_mapping->a_ops = &btrfs_symlink_aops;
143 btrfs_release_path(root, path);
144 btrfs_free_path(path);
145 mutex_unlock(&root->fs_info->fs_mutex);
146 make_bad_inode(inode);
149 static void fill_inode_item(struct btrfs_inode_item *item,
152 btrfs_set_inode_uid(item, inode->i_uid);
153 btrfs_set_inode_gid(item, inode->i_gid);
154 btrfs_set_inode_size(item, inode->i_size);
155 btrfs_set_inode_mode(item, inode->i_mode);
156 btrfs_set_inode_nlink(item, inode->i_nlink);
157 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
158 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
159 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
160 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
161 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
162 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
163 btrfs_set_inode_nblocks(item, inode->i_blocks);
164 btrfs_set_inode_generation(item, inode->i_generation);
165 btrfs_set_inode_block_group(item,
166 BTRFS_I(inode)->block_group->key.objectid);
169 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
170 struct btrfs_root *root,
173 struct btrfs_inode_item *inode_item;
174 struct btrfs_path *path;
177 path = btrfs_alloc_path();
179 ret = btrfs_lookup_inode(trans, root, path,
180 &BTRFS_I(inode)->location, 1);
187 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
189 struct btrfs_inode_item);
191 fill_inode_item(inode_item, inode);
192 btrfs_mark_buffer_dirty(path->nodes[0]);
195 btrfs_release_path(root, path);
196 btrfs_free_path(path);
201 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root,
204 struct dentry *dentry)
206 struct btrfs_path *path;
207 const char *name = dentry->d_name.name;
208 int name_len = dentry->d_name.len;
211 struct btrfs_dir_item *di;
213 path = btrfs_alloc_path();
219 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
229 objectid = btrfs_disk_key_objectid(&di->location);
230 ret = btrfs_delete_one_dir_name(trans, root, path, di);
233 btrfs_release_path(root, path);
235 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
236 objectid, name, name_len, -1);
245 ret = btrfs_delete_one_dir_name(trans, root, path, di);
247 dentry->d_inode->i_ctime = dir->i_ctime;
249 btrfs_free_path(path);
251 dir->i_size -= name_len * 2;
252 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
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 btrfs_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 struct inode *parent_inode;
1041 key.objectid = inode->i_ino;
1043 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1046 ret = btrfs_insert_dir_item(trans, root,
1047 dentry->d_name.name, dentry->d_name.len,
1048 dentry->d_parent->d_inode->i_ino,
1049 &key, btrfs_inode_type(inode));
1051 parent_inode = dentry->d_parent->d_inode;
1052 parent_inode->i_size += dentry->d_name.len * 2;
1053 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1054 ret = btrfs_update_inode(trans, root,
1055 dentry->d_parent->d_inode);
1060 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1061 struct dentry *dentry, struct inode *inode)
1063 int err = btrfs_add_link(trans, dentry, inode);
1065 d_instantiate(dentry, inode);
1073 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1074 int mode, struct nameidata *nd)
1076 struct btrfs_trans_handle *trans;
1077 struct btrfs_root *root = BTRFS_I(dir)->root;
1078 struct inode *inode;
1083 mutex_lock(&root->fs_info->fs_mutex);
1084 trans = btrfs_start_transaction(root, 1);
1085 btrfs_set_trans_block_group(trans, dir);
1087 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1093 inode = btrfs_new_inode(trans, root, objectid,
1094 BTRFS_I(dir)->block_group, mode);
1095 err = PTR_ERR(inode);
1099 btrfs_set_trans_block_group(trans, inode);
1100 err = btrfs_add_nondir(trans, dentry, inode);
1104 inode->i_mapping->a_ops = &btrfs_aops;
1105 inode->i_fop = &btrfs_file_operations;
1106 inode->i_op = &btrfs_file_inode_operations;
1108 dir->i_sb->s_dirt = 1;
1109 btrfs_update_inode_block_group(trans, inode);
1110 btrfs_update_inode_block_group(trans, dir);
1112 btrfs_end_transaction(trans, root);
1113 mutex_unlock(&root->fs_info->fs_mutex);
1116 inode_dec_link_count(inode);
1119 btrfs_btree_balance_dirty(root);
1123 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1124 struct dentry *dentry)
1126 struct btrfs_trans_handle *trans;
1127 struct btrfs_root *root = BTRFS_I(dir)->root;
1128 struct inode *inode = old_dentry->d_inode;
1132 if (inode->i_nlink == 0)
1136 mutex_lock(&root->fs_info->fs_mutex);
1137 trans = btrfs_start_transaction(root, 1);
1138 btrfs_set_trans_block_group(trans, dir);
1139 atomic_inc(&inode->i_count);
1140 err = btrfs_add_nondir(trans, dentry, inode);
1143 dir->i_sb->s_dirt = 1;
1144 btrfs_update_inode_block_group(trans, dir);
1145 err = btrfs_update_inode(trans, root, inode);
1149 btrfs_end_transaction(trans, root);
1150 mutex_unlock(&root->fs_info->fs_mutex);
1153 inode_dec_link_count(inode);
1156 btrfs_btree_balance_dirty(root);
1160 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 u64 objectid, u64 dirid)
1166 struct btrfs_key key;
1171 key.objectid = objectid;
1174 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1176 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1177 &key, BTRFS_FT_DIR);
1180 key.objectid = dirid;
1181 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1182 &key, BTRFS_FT_DIR);
1189 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1191 struct inode *inode;
1192 struct btrfs_trans_handle *trans;
1193 struct btrfs_root *root = BTRFS_I(dir)->root;
1195 int drop_on_err = 0;
1198 mutex_lock(&root->fs_info->fs_mutex);
1199 trans = btrfs_start_transaction(root, 1);
1200 btrfs_set_trans_block_group(trans, dir);
1201 if (IS_ERR(trans)) {
1202 err = PTR_ERR(trans);
1206 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1212 inode = btrfs_new_inode(trans, root, objectid,
1213 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1214 if (IS_ERR(inode)) {
1215 err = PTR_ERR(inode);
1219 inode->i_op = &btrfs_dir_inode_operations;
1220 inode->i_fop = &btrfs_dir_file_operations;
1221 btrfs_set_trans_block_group(trans, inode);
1223 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1228 err = btrfs_update_inode(trans, root, inode);
1231 err = btrfs_add_link(trans, dentry, inode);
1234 d_instantiate(dentry, inode);
1236 dir->i_sb->s_dirt = 1;
1237 btrfs_update_inode_block_group(trans, inode);
1238 btrfs_update_inode_block_group(trans, dir);
1241 btrfs_end_transaction(trans, root);
1243 mutex_unlock(&root->fs_info->fs_mutex);
1246 btrfs_btree_balance_dirty(root);
1251 * FIBMAP and others want to pass in a fake buffer head. They need to
1252 * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
1253 * any packed file data into the fake bh
1255 #define BTRFS_GET_BLOCK_NO_CREATE 0
1256 #define BTRFS_GET_BLOCK_CREATE 1
1257 #define BTRFS_GET_BLOCK_NO_DIRECT 2
1260 * FIXME create==1 doe not work.
1262 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1263 struct buffer_head *result, int create)
1268 u64 extent_start = 0;
1270 u64 objectid = inode->i_ino;
1273 struct btrfs_path *path;
1274 struct btrfs_root *root = BTRFS_I(inode)->root;
1275 struct btrfs_file_extent_item *item;
1276 struct btrfs_leaf *leaf;
1277 struct btrfs_disk_key *found_key;
1278 struct btrfs_trans_handle *trans = NULL;
1280 path = btrfs_alloc_path();
1282 if (create & BTRFS_GET_BLOCK_CREATE) {
1284 * danger!, this only works if the page is properly up
1287 trans = btrfs_start_transaction(root, 1);
1292 ret = btrfs_drop_extents(trans, root, inode,
1293 iblock << inode->i_blkbits,
1294 (iblock + 1) << inode->i_blkbits,
1299 ret = btrfs_lookup_file_extent(NULL, root, path,
1301 iblock << inode->i_blkbits, 0);
1308 if (path->slots[0] == 0) {
1309 btrfs_release_path(root, path);
1315 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1316 struct btrfs_file_extent_item);
1317 leaf = btrfs_buffer_leaf(path->nodes[0]);
1318 blocknr = btrfs_file_extent_disk_blocknr(item);
1319 blocknr += btrfs_file_extent_offset(item);
1321 /* are we inside the extent that was found? */
1322 found_key = &leaf->items[path->slots[0]].key;
1323 found_type = btrfs_disk_key_type(found_key);
1324 if (btrfs_disk_key_objectid(found_key) != objectid ||
1325 found_type != BTRFS_EXTENT_DATA_KEY) {
1330 found_type = btrfs_file_extent_type(item);
1331 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1332 if (found_type == BTRFS_FILE_EXTENT_REG) {
1333 extent_start = extent_start >> inode->i_blkbits;
1334 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1336 if (btrfs_file_extent_disk_blocknr(item) == 0)
1338 if (iblock >= extent_start && iblock < extent_end) {
1339 btrfs_map_bh_to_logical(root, result, blocknr +
1340 iblock - extent_start);
1343 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1348 if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
1352 size = btrfs_file_extent_inline_len(leaf->items +
1354 extent_end = (extent_start + size) >> inode->i_blkbits;
1355 extent_start >>= inode->i_blkbits;
1356 if (iblock < extent_start || iblock > extent_end) {
1359 ptr = btrfs_file_extent_inline_start(item);
1360 map = kmap(result->b_page);
1361 memcpy(map, ptr, size);
1362 memset(map + size, 0, PAGE_CACHE_SIZE - size);
1363 flush_dcache_page(result->b_page);
1364 kunmap(result->b_page);
1365 set_buffer_uptodate(result);
1366 SetPageChecked(result->b_page);
1367 btrfs_map_bh_to_logical(root, result, 0);
1370 if (create & BTRFS_GET_BLOCK_CREATE) {
1371 struct btrfs_key ins;
1372 ret = btrfs_alloc_extent(trans, root, inode->i_ino,
1373 1, alloc_hint, (u64)-1,
1379 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1380 iblock << inode->i_blkbits,
1381 ins.objectid, ins.offset,
1387 btrfs_map_bh_to_logical(root, result, ins.objectid);
1391 ret = btrfs_end_transaction(trans, root);
1395 btrfs_free_path(path);
1399 int btrfs_get_block(struct inode *inode, sector_t iblock,
1400 struct buffer_head *result, int create)
1403 struct btrfs_root *root = BTRFS_I(inode)->root;
1404 mutex_lock(&root->fs_info->fs_mutex);
1405 err = btrfs_get_block_lock(inode, iblock, result, create);
1406 mutex_unlock(&root->fs_info->fs_mutex);
1410 static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
1411 struct buffer_head *result, int create)
1414 struct btrfs_root *root = BTRFS_I(inode)->root;
1415 struct page *page = result->b_page;
1416 u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
1417 struct btrfs_csum_item *item;
1418 struct btrfs_path *path = NULL;
1420 mutex_lock(&root->fs_info->fs_mutex);
1421 ret = btrfs_get_block_lock(inode, iblock, result, create);
1425 path = btrfs_alloc_path();
1426 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
1428 ret = PTR_ERR(item);
1429 /* a csum that isn't present is a preallocated region. */
1430 if (ret == -ENOENT || ret == -EFBIG)
1432 result->b_private = NULL;
1435 memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
1438 btrfs_free_path(path);
1439 mutex_unlock(&root->fs_info->fs_mutex);
1443 static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1444 struct buffer_head *result, int create)
1446 struct btrfs_root *root = BTRFS_I(inode)->root;
1447 mutex_lock(&root->fs_info->fs_mutex);
1448 btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
1449 mutex_unlock(&root->fs_info->fs_mutex);
1453 static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1455 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1458 static int btrfs_prepare_write(struct file *file, struct page *page,
1459 unsigned from, unsigned to)
1461 return block_prepare_write(page, from, to, btrfs_get_block);
1464 static void buffer_io_error(struct buffer_head *bh)
1466 char b[BDEVNAME_SIZE];
1468 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
1469 bdevname(bh->b_bdev, b),
1470 (unsigned long long)bh->b_blocknr);
1474 * I/O completion handler for block_read_full_page() - pages
1475 * which come unlocked at the end of I/O.
1477 static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
1479 unsigned long flags;
1480 struct buffer_head *first;
1481 struct buffer_head *tmp;
1483 int page_uptodate = 1;
1484 struct inode *inode;
1487 BUG_ON(!buffer_async_read(bh));
1490 inode = page->mapping->host;
1493 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1494 if (bh->b_private) {
1495 char csum[BTRFS_CRC32_SIZE];
1496 kaddr = kmap_atomic(page, KM_IRQ0);
1497 ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
1500 if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
1502 offset = (page->index << PAGE_CACHE_SHIFT) +
1504 printk("btrfs csum failed ino %lu off %llu\n",
1505 page->mapping->host->i_ino,
1506 (unsigned long long)offset);
1507 memset(kaddr + bh_offset(bh), 1, bh->b_size);
1508 flush_dcache_page(page);
1510 kunmap_atomic(kaddr, KM_IRQ0);
1512 set_buffer_uptodate(bh);
1514 clear_buffer_uptodate(bh);
1515 if (printk_ratelimit())
1516 buffer_io_error(bh);
1521 * Be _very_ careful from here on. Bad things can happen if
1522 * two buffer heads end IO at almost the same time and both
1523 * decide that the page is now completely done.
1525 first = page_buffers(page);
1526 local_irq_save(flags);
1527 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
1528 clear_buffer_async_read(bh);
1532 if (!buffer_uptodate(tmp))
1534 if (buffer_async_read(tmp)) {
1535 BUG_ON(!buffer_locked(tmp));
1538 tmp = tmp->b_this_page;
1539 } while (tmp != bh);
1540 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1541 local_irq_restore(flags);
1544 * If none of the buffers had errors and they are all
1545 * uptodate then we can set the page uptodate.
1547 if (page_uptodate && !PageError(page))
1548 SetPageUptodate(page);
1553 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
1554 local_irq_restore(flags);
1559 * Generic "read page" function for block devices that have the normal
1560 * get_block functionality. This is most of the block device filesystems.
1561 * Reads the page asynchronously --- the unlock_buffer() and
1562 * set/clear_buffer_uptodate() functions propagate buffer state into the
1563 * page struct once IO has completed.
1565 int btrfs_readpage(struct file *file, struct page *page)
1567 struct inode *inode = page->mapping->host;
1568 sector_t iblock, lblock;
1569 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
1570 unsigned int blocksize;
1572 int fully_mapped = 1;
1574 BUG_ON(!PageLocked(page));
1575 blocksize = 1 << inode->i_blkbits;
1576 if (!page_has_buffers(page))
1577 create_empty_buffers(page, blocksize, 0);
1578 head = page_buffers(page);
1580 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1581 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
1587 if (buffer_uptodate(bh))
1590 if (!buffer_mapped(bh)) {
1594 if (iblock < lblock) {
1595 WARN_ON(bh->b_size != blocksize);
1596 err = btrfs_get_block_csum(inode, iblock,
1601 if (!buffer_mapped(bh)) {
1602 void *kaddr = kmap_atomic(page, KM_USER0);
1603 memset(kaddr + i * blocksize, 0, blocksize);
1604 flush_dcache_page(page);
1605 kunmap_atomic(kaddr, KM_USER0);
1607 set_buffer_uptodate(bh);
1611 * get_block() might have updated the buffer
1614 if (buffer_uptodate(bh))
1618 } while (i++, iblock++, (bh = bh->b_this_page) != head);
1621 SetPageMappedToDisk(page);
1625 * All buffers are uptodate - we can set the page uptodate
1626 * as well. But not if get_block() returned an error.
1628 if (!PageError(page))
1629 SetPageUptodate(page);
1634 /* Stage two: lock the buffers */
1635 for (i = 0; i < nr; i++) {
1638 bh->b_end_io = btrfs_end_buffer_async_read;
1639 set_buffer_async_read(bh);
1643 * Stage 3: start the IO. Check for uptodateness
1644 * inside the buffer lock in case another process reading
1645 * the underlying blockdev brought it uptodate (the sct fix).
1647 for (i = 0; i < nr; i++) {
1649 if (buffer_uptodate(bh))
1650 btrfs_end_buffer_async_read(bh, 1);
1652 submit_bh(READ, bh);
1658 * Aside from a tiny bit of packed file data handling, this is the
1659 * same as the generic code.
1661 * While block_write_full_page is writing back the dirty buffers under
1662 * the page lock, whoever dirtied the buffers may decide to clean them
1663 * again at any time. We handle that by only looking at the buffer
1664 * state inside lock_buffer().
1666 * If block_write_full_page() is called for regular writeback
1667 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1668 * locked buffer. This only can happen if someone has written the buffer
1669 * directly, with submit_bh(). At the address_space level PageWriteback
1670 * prevents this contention from occurring.
1672 static int __btrfs_write_full_page(struct inode *inode, struct page *page,
1673 struct writeback_control *wbc)
1677 sector_t last_block;
1678 struct buffer_head *bh, *head;
1679 const unsigned blocksize = 1 << inode->i_blkbits;
1680 int nr_underway = 0;
1681 struct btrfs_root *root = BTRFS_I(inode)->root;
1683 BUG_ON(!PageLocked(page));
1685 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1687 /* no csumming allowed when from PF_MEMALLOC */
1688 if (current->flags & PF_MEMALLOC) {
1689 redirty_page_for_writepage(wbc, page);
1694 if (!page_has_buffers(page)) {
1695 create_empty_buffers(page, blocksize,
1696 (1 << BH_Dirty)|(1 << BH_Uptodate));
1700 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1701 * here, and the (potentially unmapped) buffers may become dirty at
1702 * any time. If a buffer becomes dirty here after we've inspected it
1703 * then we just miss that fact, and the page stays dirty.
1705 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1706 * handle that here by just cleaning them.
1709 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1710 head = page_buffers(page);
1714 * Get all the dirty buffers mapped to disk addresses and
1715 * handle any aliases from the underlying blockdev's mapping.
1718 if (block > last_block) {
1720 * mapped buffers outside i_size will occur, because
1721 * this page can be outside i_size when there is a
1722 * truncate in progress.
1725 * The buffer was zeroed by block_write_full_page()
1727 clear_buffer_dirty(bh);
1728 set_buffer_uptodate(bh);
1729 } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
1730 WARN_ON(bh->b_size != blocksize);
1731 err = btrfs_get_block(inode, block, bh, 0);
1735 if (buffer_new(bh)) {
1736 /* blockdev mappings never come here */
1737 clear_buffer_new(bh);
1740 bh = bh->b_this_page;
1742 } while (bh != head);
1745 if (!buffer_mapped(bh))
1748 * If it's a fully non-blocking write attempt and we cannot
1749 * lock the buffer then redirty the page. Note that this can
1750 * potentially cause a busy-wait loop from pdflush and kswapd
1751 * activity, but those code paths have their own higher-level
1754 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1756 } else if (test_set_buffer_locked(bh)) {
1757 redirty_page_for_writepage(wbc, page);
1760 if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
1761 struct btrfs_trans_handle *trans;
1763 u64 off = page->index << PAGE_CACHE_SHIFT;
1766 off += bh_offset(bh);
1767 mutex_lock(&root->fs_info->fs_mutex);
1768 trans = btrfs_start_transaction(root, 1);
1769 btrfs_set_trans_block_group(trans, inode);
1771 btrfs_csum_file_block(trans, root, inode->i_ino,
1772 off, kaddr + bh_offset(bh),
1775 ret = btrfs_end_transaction(trans, root);
1777 mutex_unlock(&root->fs_info->fs_mutex);
1778 mark_buffer_async_write(bh);
1782 } while ((bh = bh->b_this_page) != head);
1785 * The page and its buffers are protected by PageWriteback(), so we can
1786 * drop the bh refcounts early.
1788 BUG_ON(PageWriteback(page));
1789 set_page_writeback(page);
1792 struct buffer_head *next = bh->b_this_page;
1793 if (buffer_async_write(bh)) {
1794 submit_bh(WRITE, bh);
1798 } while (bh != head);
1803 if (nr_underway == 0) {
1805 * The page was marked dirty, but the buffers were
1806 * clean. Someone wrote them back by hand with
1807 * ll_rw_block/submit_bh. A rare case.
1811 if (!buffer_uptodate(bh)) {
1815 bh = bh->b_this_page;
1816 } while (bh != head);
1818 SetPageUptodate(page);
1819 end_page_writeback(page);
1825 * ENOSPC, or some other error. We may already have added some
1826 * blocks to the file, so we need to write these out to avoid
1827 * exposing stale data.
1828 * The page is currently locked and not marked for writeback
1831 /* Recovery: lock and submit the mapped buffers */
1833 if (buffer_mapped(bh) && buffer_dirty(bh)) {
1835 mark_buffer_async_write(bh);
1838 * The buffer may have been set dirty during
1839 * attachment to a dirty page.
1841 clear_buffer_dirty(bh);
1843 } while ((bh = bh->b_this_page) != head);
1845 BUG_ON(PageWriteback(page));
1846 set_page_writeback(page);
1848 struct buffer_head *next = bh->b_this_page;
1849 if (buffer_async_write(bh)) {
1850 clear_buffer_dirty(bh);
1851 submit_bh(WRITE, bh);
1855 } while (bh != head);
1860 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1862 struct inode * const inode = page->mapping->host;
1863 loff_t i_size = i_size_read(inode);
1864 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
1868 /* Is the page fully inside i_size? */
1869 if (page->index < end_index)
1870 return __btrfs_write_full_page(inode, page, wbc);
1872 /* Is the page fully outside i_size? (truncate in progress) */
1873 offset = i_size & (PAGE_CACHE_SIZE-1);
1874 if (page->index >= end_index+1 || !offset) {
1876 * The page may have dirty, unmapped buffers. For example,
1877 * they may have been added in ext3_writepage(). Make them
1878 * freeable here, so the page does not leak.
1880 block_invalidatepage(page, 0);
1882 return 0; /* don't care */
1886 * The page straddles i_size. It must be zeroed out on each and every
1887 * writepage invokation because it may be mmapped. "A file is mapped
1888 * in multiples of the page size. For a file that is not a multiple of
1889 * the page size, the remaining memory is zeroed when mapped, and
1890 * writes to that region are not written out to the file."
1892 kaddr = kmap_atomic(page, KM_USER0);
1893 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1894 flush_dcache_page(page);
1895 kunmap_atomic(kaddr, KM_USER0);
1896 return __btrfs_write_full_page(inode, page, wbc);
1900 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1901 * called from a page fault handler when a page is first dirtied. Hence we must
1902 * be careful to check for EOF conditions here. We set the page up correctly
1903 * for a written page which means we get ENOSPC checking when writing into
1904 * holes and correct delalloc and unwritten extent mapping on filesystems that
1905 * support these features.
1907 * We are not allowed to take the i_mutex here so we have to play games to
1908 * protect against truncate races as the page could now be beyond EOF. Because
1909 * vmtruncate() writes the inode size before removing pages, once we have the
1910 * page lock we can determine safely if the page is beyond EOF. If it is not
1911 * beyond EOF, then the page is guaranteed safe against truncation until we
1914 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1916 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1922 wait_on_page_writeback(page);
1923 size = i_size_read(inode);
1924 if ((page->mapping != inode->i_mapping) ||
1925 ((page->index << PAGE_CACHE_SHIFT) > size)) {
1926 /* page got truncated out from underneath us */
1930 /* page is wholly or partially inside EOF */
1931 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
1932 end = size & ~PAGE_CACHE_MASK;
1934 end = PAGE_CACHE_SIZE;
1936 ret = btrfs_prepare_write(NULL, page, 0, end);
1938 ret = btrfs_commit_write(NULL, page, 0, end);
1945 static void btrfs_truncate(struct inode *inode)
1947 struct btrfs_root *root = BTRFS_I(inode)->root;
1949 struct btrfs_trans_handle *trans;
1951 if (!S_ISREG(inode->i_mode))
1953 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1956 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1958 mutex_lock(&root->fs_info->fs_mutex);
1959 trans = btrfs_start_transaction(root, 1);
1960 btrfs_set_trans_block_group(trans, inode);
1962 /* FIXME, add redo link to tree so we don't leak on crash */
1963 ret = btrfs_truncate_in_trans(trans, root, inode);
1964 btrfs_update_inode(trans, root, inode);
1965 ret = btrfs_end_transaction(trans, root);
1967 mutex_unlock(&root->fs_info->fs_mutex);
1968 btrfs_btree_balance_dirty(root);
1971 int btrfs_commit_write(struct file *file, struct page *page,
1972 unsigned from, unsigned to)
1974 struct inode *inode = page->mapping->host;
1975 struct buffer_head *bh;
1976 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1978 SetPageUptodate(page);
1979 bh = page_buffers(page);
1980 set_buffer_uptodate(bh);
1981 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
1982 set_page_dirty(page);
1984 if (pos > inode->i_size) {
1985 i_size_write(inode, pos);
1986 mark_inode_dirty(inode);
1991 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1993 struct btrfs_trans_handle *trans;
1994 struct btrfs_key key;
1995 struct btrfs_root_item root_item;
1996 struct btrfs_inode_item *inode_item;
1997 struct buffer_head *subvol;
1998 struct btrfs_leaf *leaf;
1999 struct btrfs_root *new_root;
2000 struct inode *inode;
2005 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2007 mutex_lock(&root->fs_info->fs_mutex);
2008 trans = btrfs_start_transaction(root, 1);
2011 subvol = btrfs_alloc_free_block(trans, root, 0);
2013 return PTR_ERR(subvol);
2014 leaf = btrfs_buffer_leaf(subvol);
2015 btrfs_set_header_nritems(&leaf->header, 0);
2016 btrfs_set_header_level(&leaf->header, 0);
2017 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
2018 btrfs_set_header_generation(&leaf->header, trans->transid);
2019 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
2020 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
2021 sizeof(leaf->header.fsid));
2022 btrfs_mark_buffer_dirty(subvol);
2024 inode_item = &root_item.inode;
2025 memset(inode_item, 0, sizeof(*inode_item));
2026 btrfs_set_inode_generation(inode_item, 1);
2027 btrfs_set_inode_size(inode_item, 3);
2028 btrfs_set_inode_nlink(inode_item, 1);
2029 btrfs_set_inode_nblocks(inode_item, 1);
2030 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
2032 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
2033 btrfs_set_root_refs(&root_item, 1);
2034 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2035 root_item.drop_level = 0;
2039 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2044 btrfs_set_root_dirid(&root_item, new_dirid);
2046 key.objectid = objectid;
2049 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2050 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2056 * insert the directory item
2058 key.offset = (u64)-1;
2059 dir = root->fs_info->sb->s_root->d_inode;
2060 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2061 name, namelen, dir->i_ino, &key,
2066 ret = btrfs_commit_transaction(trans, root);
2070 new_root = btrfs_read_fs_root(root->fs_info, &key);
2073 trans = btrfs_start_transaction(new_root, 1);
2076 inode = btrfs_new_inode(trans, new_root, new_dirid,
2077 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2080 inode->i_op = &btrfs_dir_inode_operations;
2081 inode->i_fop = &btrfs_dir_file_operations;
2082 new_root->inode = inode;
2084 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
2090 ret = btrfs_update_inode(trans, new_root, inode);
2094 err = btrfs_commit_transaction(trans, root);
2098 mutex_unlock(&root->fs_info->fs_mutex);
2099 btrfs_btree_balance_dirty(root);
2103 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2105 struct btrfs_trans_handle *trans;
2106 struct btrfs_key key;
2107 struct btrfs_root_item new_root_item;
2112 if (!root->ref_cows)
2115 mutex_lock(&root->fs_info->fs_mutex);
2116 trans = btrfs_start_transaction(root, 1);
2119 ret = btrfs_update_inode(trans, root, root->inode);
2123 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2128 memcpy(&new_root_item, &root->root_item,
2129 sizeof(new_root_item));
2131 key.objectid = objectid;
2134 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2135 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2137 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2143 * insert the directory item
2145 key.offset = (u64)-1;
2146 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2148 root->fs_info->sb->s_root->d_inode->i_ino,
2149 &key, BTRFS_FT_DIR);
2154 ret = btrfs_inc_root_ref(trans, root);
2159 err = btrfs_commit_transaction(trans, root);
2162 mutex_unlock(&root->fs_info->fs_mutex);
2163 btrfs_btree_balance_dirty(root);
2167 int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2168 cmd, unsigned long arg)
2170 struct btrfs_root *root = BTRFS_I(inode)->root;
2171 struct btrfs_ioctl_vol_args vol_args;
2173 struct btrfs_dir_item *di;
2175 struct btrfs_path *path;
2179 case BTRFS_IOC_SNAP_CREATE:
2180 if (copy_from_user(&vol_args,
2181 (struct btrfs_ioctl_vol_args __user *)arg,
2184 namelen = strlen(vol_args.name);
2185 if (namelen > BTRFS_VOL_NAME_MAX)
2187 if (strchr(vol_args.name, '/'))
2189 path = btrfs_alloc_path();
2192 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2193 mutex_lock(&root->fs_info->fs_mutex);
2194 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2196 vol_args.name, namelen, 0);
2197 mutex_unlock(&root->fs_info->fs_mutex);
2198 btrfs_free_path(path);
2199 if (di && !IS_ERR(di))
2204 if (root == root->fs_info->tree_root)
2205 ret = create_subvol(root, vol_args.name, namelen);
2207 ret = create_snapshot(root, vol_args.name, namelen);
2215 #ifdef CONFIG_COMPAT
2216 long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
2219 struct inode *inode = file->f_path.dentry->d_inode;
2222 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
2230 * Called inside transaction, so use GFP_NOFS
2232 struct inode *btrfs_alloc_inode(struct super_block *sb)
2234 struct btrfs_inode *ei;
2236 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2239 return &ei->vfs_inode;
2242 void btrfs_destroy_inode(struct inode *inode)
2244 WARN_ON(!list_empty(&inode->i_dentry));
2245 WARN_ON(inode->i_data.nrpages);
2247 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2250 static void init_once(void * foo, struct kmem_cache * cachep,
2251 unsigned long flags)
2253 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2255 inode_init_once(&ei->vfs_inode);
2258 void btrfs_destroy_cachep(void)
2260 if (btrfs_inode_cachep)
2261 kmem_cache_destroy(btrfs_inode_cachep);
2262 if (btrfs_trans_handle_cachep)
2263 kmem_cache_destroy(btrfs_trans_handle_cachep);
2264 if (btrfs_transaction_cachep)
2265 kmem_cache_destroy(btrfs_transaction_cachep);
2266 if (btrfs_bit_radix_cachep)
2267 kmem_cache_destroy(btrfs_bit_radix_cachep);
2268 if (btrfs_path_cachep)
2269 kmem_cache_destroy(btrfs_path_cachep);
2272 int btrfs_init_cachep(void)
2274 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2275 sizeof(struct btrfs_inode),
2276 0, (SLAB_RECLAIM_ACCOUNT|
2279 if (!btrfs_inode_cachep)
2281 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2282 sizeof(struct btrfs_trans_handle),
2283 0, (SLAB_RECLAIM_ACCOUNT|
2286 if (!btrfs_trans_handle_cachep)
2288 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2289 sizeof(struct btrfs_transaction),
2290 0, (SLAB_RECLAIM_ACCOUNT|
2293 if (!btrfs_transaction_cachep)
2295 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2296 sizeof(struct btrfs_transaction),
2297 0, (SLAB_RECLAIM_ACCOUNT|
2300 if (!btrfs_path_cachep)
2302 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2304 0, (SLAB_RECLAIM_ACCOUNT|
2306 SLAB_DESTROY_BY_RCU),
2308 if (!btrfs_bit_radix_cachep)
2312 btrfs_destroy_cachep();
2316 static int btrfs_getattr(struct vfsmount *mnt,
2317 struct dentry *dentry, struct kstat *stat)
2319 struct inode *inode = dentry->d_inode;
2320 generic_fillattr(inode, stat);
2321 stat->blksize = 256 * 1024;
2325 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2326 struct inode * new_dir,struct dentry *new_dentry)
2328 struct btrfs_trans_handle *trans;
2329 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2330 struct inode *new_inode = new_dentry->d_inode;
2331 struct inode *old_inode = old_dentry->d_inode;
2332 struct timespec ctime = CURRENT_TIME;
2333 struct btrfs_path *path;
2334 struct btrfs_dir_item *di;
2337 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2338 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2341 mutex_lock(&root->fs_info->fs_mutex);
2342 trans = btrfs_start_transaction(root, 1);
2343 btrfs_set_trans_block_group(trans, new_dir);
2344 path = btrfs_alloc_path();
2350 old_dentry->d_inode->i_nlink++;
2351 old_dir->i_ctime = old_dir->i_mtime = ctime;
2352 new_dir->i_ctime = new_dir->i_mtime = ctime;
2353 old_inode->i_ctime = ctime;
2354 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2355 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2357 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2367 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2368 ret = btrfs_del_item(trans, root, path);
2372 btrfs_release_path(root, path);
2374 di = btrfs_lookup_dir_index_item(trans, root, path,
2386 ret = btrfs_del_item(trans, root, path);
2390 btrfs_release_path(root, path);
2392 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2393 old_inode->i_ino, location,
2400 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2405 new_inode->i_ctime = CURRENT_TIME;
2406 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2409 if (S_ISDIR(new_inode->i_mode))
2410 clear_nlink(new_inode);
2412 drop_nlink(new_inode);
2413 ret = btrfs_update_inode(trans, root, new_inode);
2417 ret = btrfs_add_link(trans, new_dentry, old_inode);
2422 btrfs_free_path(path);
2423 btrfs_end_transaction(trans, root);
2424 mutex_unlock(&root->fs_info->fs_mutex);
2428 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2429 const char *symname)
2431 struct btrfs_trans_handle *trans;
2432 struct btrfs_root *root = BTRFS_I(dir)->root;
2433 struct btrfs_path *path;
2434 struct btrfs_key key;
2435 struct inode *inode;
2442 struct btrfs_file_extent_item *ei;
2444 name_len = strlen(symname) + 1;
2445 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2446 return -ENAMETOOLONG;
2447 mutex_lock(&root->fs_info->fs_mutex);
2448 trans = btrfs_start_transaction(root, 1);
2449 btrfs_set_trans_block_group(trans, dir);
2451 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2457 inode = btrfs_new_inode(trans, root, objectid,
2458 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2459 err = PTR_ERR(inode);
2463 btrfs_set_trans_block_group(trans, inode);
2464 err = btrfs_add_nondir(trans, dentry, inode);
2468 inode->i_mapping->a_ops = &btrfs_aops;
2469 inode->i_fop = &btrfs_file_operations;
2470 inode->i_op = &btrfs_file_inode_operations;
2472 dir->i_sb->s_dirt = 1;
2473 btrfs_update_inode_block_group(trans, inode);
2474 btrfs_update_inode_block_group(trans, dir);
2478 path = btrfs_alloc_path();
2480 key.objectid = inode->i_ino;
2483 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2484 datasize = btrfs_file_extent_calc_inline_size(name_len);
2485 err = btrfs_insert_empty_item(trans, root, path, &key,
2491 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2492 path->slots[0], struct btrfs_file_extent_item);
2493 btrfs_set_file_extent_generation(ei, trans->transid);
2494 btrfs_set_file_extent_type(ei,
2495 BTRFS_FILE_EXTENT_INLINE);
2496 ptr = btrfs_file_extent_inline_start(ei);
2497 btrfs_memcpy(root, path->nodes[0]->b_data,
2498 ptr, symname, name_len);
2499 btrfs_mark_buffer_dirty(path->nodes[0]);
2500 btrfs_free_path(path);
2501 inode->i_op = &btrfs_symlink_inode_operations;
2502 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2503 inode->i_size = name_len - 1;
2504 err = btrfs_update_inode(trans, root, inode);
2509 btrfs_end_transaction(trans, root);
2510 mutex_unlock(&root->fs_info->fs_mutex);
2512 inode_dec_link_count(inode);
2515 btrfs_btree_balance_dirty(root);
2519 static struct inode_operations btrfs_dir_inode_operations = {
2520 .lookup = btrfs_lookup,
2521 .create = btrfs_create,
2522 .unlink = btrfs_unlink,
2524 .mkdir = btrfs_mkdir,
2525 .rmdir = btrfs_rmdir,
2526 .rename = btrfs_rename,
2527 .symlink = btrfs_symlink,
2528 .setattr = btrfs_setattr,
2531 static struct inode_operations btrfs_dir_ro_inode_operations = {
2532 .lookup = btrfs_lookup,
2535 static struct file_operations btrfs_dir_file_operations = {
2536 .llseek = generic_file_llseek,
2537 .read = generic_read_dir,
2538 .readdir = btrfs_readdir,
2539 .ioctl = btrfs_ioctl,
2540 #ifdef CONFIG_COMPAT
2541 .compat_ioctl = btrfs_compat_ioctl,
2545 static struct address_space_operations btrfs_aops = {
2546 .readpage = btrfs_readpage,
2547 .writepage = btrfs_writepage,
2548 .sync_page = block_sync_page,
2549 .prepare_write = btrfs_prepare_write,
2550 .commit_write = btrfs_commit_write,
2554 static struct address_space_operations btrfs_symlink_aops = {
2555 .readpage = btrfs_readpage,
2556 .writepage = btrfs_writepage,
2559 static struct inode_operations btrfs_file_inode_operations = {
2560 .truncate = btrfs_truncate,
2561 .getattr = btrfs_getattr,
2562 .setattr = btrfs_setattr,
2565 static struct inode_operations btrfs_symlink_inode_operations = {
2566 .readlink = generic_readlink,
2567 .follow_link = page_follow_link_light,
2568 .put_link = page_put_link,