2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
37 #include "transaction.h"
38 #include "btrfs_inode.h"
40 #include "print-tree.h"
42 struct btrfs_iget_args {
44 struct btrfs_root *root;
47 static struct inode_operations btrfs_dir_inode_operations;
48 static struct inode_operations btrfs_symlink_inode_operations;
49 static struct inode_operations btrfs_dir_ro_inode_operations;
50 static struct inode_operations btrfs_special_inode_operations;
51 static struct inode_operations btrfs_file_inode_operations;
52 static struct address_space_operations btrfs_aops;
53 static struct address_space_operations btrfs_symlink_aops;
54 static struct file_operations btrfs_dir_file_operations;
55 static struct extent_map_ops btrfs_extent_map_ops;
57 static struct kmem_cache *btrfs_inode_cachep;
58 struct kmem_cache *btrfs_trans_handle_cachep;
59 struct kmem_cache *btrfs_transaction_cachep;
60 struct kmem_cache *btrfs_bit_radix_cachep;
61 struct kmem_cache *btrfs_path_cachep;
64 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
65 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
66 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
67 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
68 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
69 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
70 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
71 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
74 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_trans_handle *trans;
82 u64 blocksize = 1 << inode->i_blkbits;
84 mutex_lock(&root->fs_info->fs_mutex);
85 trans = btrfs_start_transaction(root, 1);
86 btrfs_set_trans_block_group(trans, inode);
88 num_blocks = (end - start + blocksize) & ~(blocksize - 1);
89 ret = btrfs_drop_extents(trans, root, inode,
90 start, start + num_blocks, &alloc_hint);
91 num_blocks = num_blocks >> inode->i_blkbits;
92 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_blocks, 0,
93 alloc_hint, (u64)-1, &ins, 1);
98 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
99 start, ins.objectid, ins.offset,
102 btrfs_end_transaction(trans, root);
103 mutex_unlock(&root->fs_info->fs_mutex);
107 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
109 struct inode *inode = page->mapping->host;
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
114 u64 page_start = page->index << PAGE_CACHE_SHIFT;
115 size_t offset = start - page_start;
117 mutex_lock(&root->fs_info->fs_mutex);
118 trans = btrfs_start_transaction(root, 1);
119 btrfs_set_trans_block_group(trans, inode);
121 btrfs_csum_file_block(trans, root, inode->i_ino,
122 start, kaddr + offset, end - start + 1);
124 ret = btrfs_end_transaction(trans, root);
126 mutex_unlock(&root->fs_info->fs_mutex);
130 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
133 struct inode *inode = page->mapping->host;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
136 struct btrfs_csum_item *item;
137 struct btrfs_path *path = NULL;
140 mutex_lock(&root->fs_info->fs_mutex);
141 path = btrfs_alloc_path();
142 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret == -ENOENT || ret == -EFBIG)
151 memcpy((char *)&private, &item->csum, BTRFS_CRC32_SIZE);
152 set_state_private(em_tree, start, private);
155 btrfs_free_path(path);
156 mutex_unlock(&root->fs_info->fs_mutex);
160 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
162 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
163 struct inode *inode = page->mapping->host;
164 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
169 ret = get_state_private(em_tree, start, &private);
170 kaddr = kmap_atomic(page, KM_IRQ0);
175 struct btrfs_root *root = BTRFS_I(inode)->root;
176 char csum[BTRFS_CRC32_SIZE];
177 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
179 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
183 kunmap_atomic(kaddr, KM_IRQ0);
187 printk("btrfs csum failed ino %lu off %llu\n",
188 page->mapping->host->i_ino, (unsigned long long)start);
189 memset(kaddr + offset, 1, end - start + 1); flush_dcache_page(page);
190 kunmap_atomic(kaddr, KM_IRQ0);
194 void btrfs_read_locked_inode(struct inode *inode)
196 struct btrfs_path *path;
197 struct extent_buffer *leaf;
198 struct btrfs_inode_item *inode_item;
199 struct btrfs_inode_timespec *tspec;
200 struct btrfs_root *root = BTRFS_I(inode)->root;
201 struct btrfs_key location;
202 u64 alloc_group_block;
206 path = btrfs_alloc_path();
208 mutex_lock(&root->fs_info->fs_mutex);
210 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
211 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
215 leaf = path->nodes[0];
216 inode_item = btrfs_item_ptr(leaf, path->slots[0],
217 struct btrfs_inode_item);
219 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
220 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
221 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
222 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
223 inode->i_size = btrfs_inode_size(leaf, inode_item);
225 tspec = btrfs_inode_atime(inode_item);
226 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
227 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
229 tspec = btrfs_inode_mtime(inode_item);
230 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
231 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
233 tspec = btrfs_inode_ctime(inode_item);
234 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
235 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
237 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
238 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
240 rdev = btrfs_inode_rdev(leaf, inode_item);
242 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
243 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
246 btrfs_free_path(path);
249 mutex_unlock(&root->fs_info->fs_mutex);
251 switch (inode->i_mode & S_IFMT) {
253 inode->i_mapping->a_ops = &btrfs_aops;
254 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
255 inode->i_fop = &btrfs_file_operations;
256 inode->i_op = &btrfs_file_inode_operations;
259 inode->i_fop = &btrfs_dir_file_operations;
260 if (root == root->fs_info->tree_root)
261 inode->i_op = &btrfs_dir_ro_inode_operations;
263 inode->i_op = &btrfs_dir_inode_operations;
266 inode->i_op = &btrfs_symlink_inode_operations;
267 inode->i_mapping->a_ops = &btrfs_symlink_aops;
270 init_special_inode(inode, inode->i_mode, rdev);
276 btrfs_release_path(root, path);
277 btrfs_free_path(path);
278 mutex_unlock(&root->fs_info->fs_mutex);
279 make_bad_inode(inode);
282 static void fill_inode_item(struct extent_buffer *leaf,
283 struct btrfs_inode_item *item,
286 btrfs_set_inode_uid(leaf, item, inode->i_uid);
287 btrfs_set_inode_gid(leaf, item, inode->i_gid);
288 btrfs_set_inode_size(leaf, item, inode->i_size);
289 btrfs_set_inode_mode(leaf, item, inode->i_mode);
290 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
292 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
293 inode->i_atime.tv_sec);
294 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
295 inode->i_atime.tv_nsec);
297 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
298 inode->i_mtime.tv_sec);
299 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
300 inode->i_mtime.tv_nsec);
302 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
303 inode->i_ctime.tv_sec);
304 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
305 inode->i_ctime.tv_nsec);
307 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
308 btrfs_set_inode_generation(leaf, item, inode->i_generation);
309 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
310 btrfs_set_inode_block_group(leaf, item,
311 BTRFS_I(inode)->block_group->key.objectid);
314 int btrfs_update_inode(struct btrfs_trans_handle *trans,
315 struct btrfs_root *root,
318 struct btrfs_inode_item *inode_item;
319 struct btrfs_path *path;
320 struct extent_buffer *leaf;
323 path = btrfs_alloc_path();
325 ret = btrfs_lookup_inode(trans, root, path,
326 &BTRFS_I(inode)->location, 1);
333 leaf = path->nodes[0];
334 inode_item = btrfs_item_ptr(leaf, path->slots[0],
335 struct btrfs_inode_item);
337 fill_inode_item(leaf, inode_item, inode);
338 btrfs_mark_buffer_dirty(leaf);
339 btrfs_set_inode_last_trans(trans, inode);
342 btrfs_release_path(root, path);
343 btrfs_free_path(path);
348 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
349 struct btrfs_root *root,
351 struct dentry *dentry)
353 struct btrfs_path *path;
354 const char *name = dentry->d_name.name;
355 int name_len = dentry->d_name.len;
357 struct extent_buffer *leaf;
358 struct btrfs_dir_item *di;
359 struct btrfs_key key;
361 path = btrfs_alloc_path();
367 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
377 leaf = path->nodes[0];
378 btrfs_dir_item_key_to_cpu(leaf, di, &key);
379 ret = btrfs_delete_one_dir_name(trans, root, path, di);
382 btrfs_release_path(root, path);
384 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
385 key.objectid, name, name_len, -1);
394 ret = btrfs_delete_one_dir_name(trans, root, path, di);
396 dentry->d_inode->i_ctime = dir->i_ctime;
398 btrfs_free_path(path);
400 dir->i_size -= name_len * 2;
401 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
402 btrfs_update_inode(trans, root, dir);
403 drop_nlink(dentry->d_inode);
404 ret = btrfs_update_inode(trans, root, dentry->d_inode);
405 dir->i_sb->s_dirt = 1;
410 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
412 struct btrfs_root *root;
413 struct btrfs_trans_handle *trans;
417 root = BTRFS_I(dir)->root;
418 mutex_lock(&root->fs_info->fs_mutex);
419 trans = btrfs_start_transaction(root, 1);
421 btrfs_set_trans_block_group(trans, dir);
422 ret = btrfs_unlink_trans(trans, root, dir, dentry);
423 nr = trans->blocks_used;
425 btrfs_end_transaction(trans, root);
426 mutex_unlock(&root->fs_info->fs_mutex);
427 btrfs_btree_balance_dirty(root, nr);
432 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
434 struct inode *inode = dentry->d_inode;
437 struct btrfs_root *root = BTRFS_I(dir)->root;
438 struct btrfs_path *path;
439 struct btrfs_key key;
440 struct btrfs_trans_handle *trans;
441 struct btrfs_key found_key;
443 struct extent_buffer *leaf;
444 char *goodnames = "..";
447 path = btrfs_alloc_path();
449 mutex_lock(&root->fs_info->fs_mutex);
450 trans = btrfs_start_transaction(root, 1);
452 btrfs_set_trans_block_group(trans, dir);
453 key.objectid = inode->i_ino;
454 key.offset = (u64)-1;
457 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
463 if (path->slots[0] == 0) {
468 leaf = path->nodes[0];
469 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
470 found_type = btrfs_key_type(&found_key);
471 if (found_key.objectid != inode->i_ino) {
475 if ((found_type != BTRFS_DIR_ITEM_KEY &&
476 found_type != BTRFS_DIR_INDEX_KEY) ||
477 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
478 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
482 ret = btrfs_del_item(trans, root, path);
485 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
487 btrfs_release_path(root, path);
490 btrfs_release_path(root, path);
492 /* now the directory is empty */
493 err = btrfs_unlink_trans(trans, root, dir, dentry);
498 btrfs_release_path(root, path);
499 btrfs_free_path(path);
500 mutex_unlock(&root->fs_info->fs_mutex);
501 nr = trans->blocks_used;
502 ret = btrfs_end_transaction(trans, root);
503 btrfs_btree_balance_dirty(root, nr);
509 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
510 struct btrfs_root *root,
513 struct btrfs_path *path;
518 path = btrfs_alloc_path();
520 ret = btrfs_lookup_inode(trans, root, path,
521 &BTRFS_I(inode)->location, -1);
525 ret = btrfs_del_item(trans, root, path);
526 btrfs_free_path(path);
531 * this can truncate away extent items, csum items and directory items.
532 * It starts at a high offset and removes keys until it can't find
533 * any higher than i_size.
535 * csum items that cross the new i_size are truncated to the new size
538 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
543 struct btrfs_path *path;
544 struct btrfs_key key;
545 struct btrfs_key found_key;
547 struct extent_buffer *leaf;
548 struct btrfs_file_extent_item *fi;
549 u64 extent_start = 0;
550 u64 extent_num_blocks = 0;
555 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
556 path = btrfs_alloc_path();
560 /* FIXME, add redo link to tree so we don't leak on crash */
561 key.objectid = inode->i_ino;
562 key.offset = (u64)-1;
566 btrfs_init_path(path);
568 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
573 BUG_ON(path->slots[0] == 0);
576 leaf = path->nodes[0];
577 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
578 found_type = btrfs_key_type(&found_key);
580 if (found_key.objectid != inode->i_ino)
583 if (found_type != BTRFS_CSUM_ITEM_KEY &&
584 found_type != BTRFS_DIR_ITEM_KEY &&
585 found_type != BTRFS_DIR_INDEX_KEY &&
586 found_type != BTRFS_EXTENT_DATA_KEY)
589 item_end = found_key.offset;
590 if (found_type == BTRFS_EXTENT_DATA_KEY) {
591 fi = btrfs_item_ptr(leaf, path->slots[0],
592 struct btrfs_file_extent_item);
593 if (btrfs_file_extent_type(leaf, fi) !=
594 BTRFS_FILE_EXTENT_INLINE) {
596 btrfs_file_extent_num_blocks(leaf, fi) <<
600 if (found_type == BTRFS_CSUM_ITEM_KEY) {
601 ret = btrfs_csum_truncate(trans, root, path,
605 if (item_end < inode->i_size) {
606 if (found_type == BTRFS_DIR_ITEM_KEY) {
607 found_type = BTRFS_INODE_ITEM_KEY;
608 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
609 found_type = BTRFS_CSUM_ITEM_KEY;
610 } else if (found_type) {
615 btrfs_set_key_type(&key, found_type);
618 if (found_key.offset >= inode->i_size)
624 /* FIXME, shrink the extent if the ref count is only 1 */
625 if (found_type == BTRFS_EXTENT_DATA_KEY &&
626 btrfs_file_extent_type(leaf, fi) !=
627 BTRFS_FILE_EXTENT_INLINE) {
629 extent_start = btrfs_file_extent_disk_blocknr(leaf, fi);
631 u64 orig_num_blocks =
632 btrfs_file_extent_num_blocks(leaf, fi);
633 extent_num_blocks = inode->i_size -
634 found_key.offset + root->sectorsize - 1;
635 extent_num_blocks >>= inode->i_blkbits;
636 btrfs_set_file_extent_num_blocks(leaf, fi,
638 num_dec = (orig_num_blocks -
639 extent_num_blocks) << 3;
640 if (extent_start != 0) {
641 inode->i_blocks -= num_dec;
643 btrfs_mark_buffer_dirty(leaf);
646 btrfs_file_extent_disk_num_blocks(leaf,
648 /* FIXME blocksize != 4096 */
649 num_dec = btrfs_file_extent_num_blocks(leaf,
651 if (extent_start != 0) {
653 inode->i_blocks -= num_dec;
658 ret = btrfs_del_item(trans, root, path);
664 btrfs_release_path(root, path);
666 ret = btrfs_free_extent(trans, root, extent_start,
667 extent_num_blocks, 0);
673 btrfs_release_path(root, path);
674 btrfs_free_path(path);
675 inode->i_sb->s_dirt = 1;
679 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
684 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
685 u64 page_start = page->index << PAGE_CACHE_SHIFT;
686 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
688 set_page_extent_mapped(page);
690 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
691 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
693 if (zero_start != PAGE_CACHE_SIZE) {
695 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
696 flush_dcache_page(page);
699 set_page_dirty(page);
700 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
706 * taken from block_truncate_page, but does cow as it zeros out
707 * any bytes left in the last page in the file.
709 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
711 struct inode *inode = mapping->host;
712 unsigned blocksize = 1 << inode->i_blkbits;
713 pgoff_t index = from >> PAGE_CACHE_SHIFT;
714 unsigned offset = from & (PAGE_CACHE_SIZE-1);
719 if ((offset & (blocksize - 1)) == 0)
722 down_read(&BTRFS_I(inode)->root->snap_sem);
724 page = grab_cache_page(mapping, index);
727 if (!PageUptodate(page)) {
728 ret = btrfs_readpage(NULL, page);
730 if (!PageUptodate(page)) {
735 page_start = page->index << PAGE_CACHE_SHIFT;
737 ret = btrfs_cow_one_page(inode, page, offset);
740 page_cache_release(page);
741 up_read(&BTRFS_I(inode)->root->snap_sem);
746 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
748 struct inode *inode = dentry->d_inode;
751 err = inode_change_ok(inode, attr);
755 if (S_ISREG(inode->i_mode) &&
756 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
757 struct btrfs_trans_handle *trans;
758 struct btrfs_root *root = BTRFS_I(inode)->root;
759 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
761 u64 mask = root->sectorsize - 1;
762 u64 pos = (inode->i_size + mask) & ~mask;
763 u64 block_end = attr->ia_size | mask;
767 if (attr->ia_size <= pos)
770 btrfs_truncate_page(inode->i_mapping, inode->i_size);
772 lock_extent(em_tree, pos, block_end, GFP_NOFS);
773 hole_size = (attr->ia_size - pos + mask) & ~mask;
775 mutex_lock(&root->fs_info->fs_mutex);
776 trans = btrfs_start_transaction(root, 1);
777 btrfs_set_trans_block_group(trans, inode);
778 err = btrfs_drop_extents(trans, root, inode,
779 pos, pos + hole_size, &alloc_hint);
781 hole_size >>= inode->i_blkbits;
783 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
784 pos, 0, 0, hole_size);
785 btrfs_end_transaction(trans, root);
786 mutex_unlock(&root->fs_info->fs_mutex);
787 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
792 err = inode_setattr(inode, attr);
796 void btrfs_delete_inode(struct inode *inode)
798 struct btrfs_trans_handle *trans;
799 struct btrfs_root *root = BTRFS_I(inode)->root;
803 truncate_inode_pages(&inode->i_data, 0);
804 if (is_bad_inode(inode)) {
809 mutex_lock(&root->fs_info->fs_mutex);
810 trans = btrfs_start_transaction(root, 1);
812 btrfs_set_trans_block_group(trans, inode);
813 ret = btrfs_truncate_in_trans(trans, root, inode);
816 ret = btrfs_free_inode(trans, root, inode);
819 nr = trans->blocks_used;
821 btrfs_end_transaction(trans, root);
822 mutex_unlock(&root->fs_info->fs_mutex);
823 btrfs_btree_balance_dirty(root, nr);
827 nr = trans->blocks_used;
828 btrfs_end_transaction(trans, root);
829 mutex_unlock(&root->fs_info->fs_mutex);
830 btrfs_btree_balance_dirty(root, nr);
836 * this returns the key found in the dir entry in the location pointer.
837 * If no dir entries were found, location->objectid is 0.
839 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
840 struct btrfs_key *location)
842 const char *name = dentry->d_name.name;
843 int namelen = dentry->d_name.len;
844 struct btrfs_dir_item *di;
845 struct btrfs_path *path;
846 struct btrfs_root *root = BTRFS_I(dir)->root;
849 path = btrfs_alloc_path();
851 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
853 if (!di || IS_ERR(di)) {
854 location->objectid = 0;
858 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
860 btrfs_release_path(root, path);
861 btrfs_free_path(path);
866 * when we hit a tree root in a directory, the btrfs part of the inode
867 * needs to be changed to reflect the root directory of the tree root. This
868 * is kind of like crossing a mount point.
870 static int fixup_tree_root_location(struct btrfs_root *root,
871 struct btrfs_key *location,
872 struct btrfs_root **sub_root,
873 struct dentry *dentry)
875 struct btrfs_path *path;
876 struct btrfs_root_item *ri;
878 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
880 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
883 path = btrfs_alloc_path();
885 mutex_lock(&root->fs_info->fs_mutex);
887 *sub_root = btrfs_read_fs_root(root->fs_info, location,
890 if (IS_ERR(*sub_root))
891 return PTR_ERR(*sub_root);
893 ri = &(*sub_root)->root_item;
894 location->objectid = btrfs_root_dirid(ri);
895 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
896 location->offset = 0;
898 btrfs_free_path(path);
899 mutex_unlock(&root->fs_info->fs_mutex);
903 static int btrfs_init_locked_inode(struct inode *inode, void *p)
905 struct btrfs_iget_args *args = p;
906 inode->i_ino = args->ino;
907 BTRFS_I(inode)->root = args->root;
908 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
909 inode->i_mapping, GFP_NOFS);
913 static int btrfs_find_actor(struct inode *inode, void *opaque)
915 struct btrfs_iget_args *args = opaque;
916 return (args->ino == inode->i_ino &&
917 args->root == BTRFS_I(inode)->root);
920 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
921 struct btrfs_root *root)
924 struct btrfs_iget_args args;
928 inode = iget5_locked(s, objectid, btrfs_find_actor,
929 btrfs_init_locked_inode,
934 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
935 struct nameidata *nd)
937 struct inode * inode;
938 struct btrfs_inode *bi = BTRFS_I(dir);
939 struct btrfs_root *root = bi->root;
940 struct btrfs_root *sub_root = root;
941 struct btrfs_key location;
944 if (dentry->d_name.len > BTRFS_NAME_LEN)
945 return ERR_PTR(-ENAMETOOLONG);
947 mutex_lock(&root->fs_info->fs_mutex);
948 ret = btrfs_inode_by_name(dir, dentry, &location);
949 mutex_unlock(&root->fs_info->fs_mutex);
955 if (location.objectid) {
956 ret = fixup_tree_root_location(root, &location, &sub_root,
961 return ERR_PTR(-ENOENT);
962 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
965 return ERR_PTR(-EACCES);
966 if (inode->i_state & I_NEW) {
967 /* the inode and parent dir are two different roots */
968 if (sub_root != root) {
970 sub_root->inode = inode;
972 BTRFS_I(inode)->root = sub_root;
973 memcpy(&BTRFS_I(inode)->location, &location,
975 btrfs_read_locked_inode(inode);
976 unlock_new_inode(inode);
979 return d_splice_alias(inode, dentry);
982 static unsigned char btrfs_filetype_table[] = {
983 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
986 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
988 struct inode *inode = filp->f_path.dentry->d_inode;
989 struct btrfs_root *root = BTRFS_I(inode)->root;
990 struct btrfs_item *item;
991 struct btrfs_dir_item *di;
992 struct btrfs_key key;
993 struct btrfs_key found_key;
994 struct btrfs_path *path;
997 struct extent_buffer *leaf;
1000 unsigned char d_type;
1005 int key_type = BTRFS_DIR_INDEX_KEY;
1010 /* FIXME, use a real flag for deciding about the key type */
1011 if (root->fs_info->tree_root == root)
1012 key_type = BTRFS_DIR_ITEM_KEY;
1014 mutex_lock(&root->fs_info->fs_mutex);
1015 key.objectid = inode->i_ino;
1016 btrfs_set_key_type(&key, key_type);
1017 key.offset = filp->f_pos;
1019 path = btrfs_alloc_path();
1021 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1026 leaf = path->nodes[0];
1027 nritems = btrfs_header_nritems(leaf);
1028 slot = path->slots[0];
1029 if (advance || slot >= nritems) {
1030 if (slot >= nritems -1) {
1031 ret = btrfs_next_leaf(root, path);
1034 leaf = path->nodes[0];
1035 nritems = btrfs_header_nritems(leaf);
1036 slot = path->slots[0];
1043 item = btrfs_item_nr(leaf, slot);
1044 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1046 if (found_key.objectid != key.objectid)
1048 if (btrfs_key_type(&found_key) != key_type)
1050 if (found_key.offset < filp->f_pos)
1053 filp->f_pos = found_key.offset;
1055 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1057 di_total = btrfs_item_size(leaf, item);
1058 while(di_cur < di_total) {
1059 struct btrfs_key location;
1061 name_len = btrfs_dir_name_len(leaf, di);
1062 if (name_len < 32) {
1063 name_ptr = tmp_name;
1065 name_ptr = kmalloc(name_len, GFP_NOFS);
1068 read_extent_buffer(leaf, name_ptr,
1069 (unsigned long)(di + 1), name_len);
1071 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1072 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1074 over = filldir(dirent, name_ptr, name_len,
1079 if (name_ptr != tmp_name)
1084 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
1086 di = (struct btrfs_dir_item *)((char *)di + di_len);
1093 btrfs_release_path(root, path);
1094 btrfs_free_path(path);
1095 mutex_unlock(&root->fs_info->fs_mutex);
1099 int btrfs_write_inode(struct inode *inode, int wait)
1101 struct btrfs_root *root = BTRFS_I(inode)->root;
1102 struct btrfs_trans_handle *trans;
1106 mutex_lock(&root->fs_info->fs_mutex);
1107 trans = btrfs_start_transaction(root, 1);
1108 btrfs_set_trans_block_group(trans, inode);
1109 ret = btrfs_commit_transaction(trans, root);
1110 mutex_unlock(&root->fs_info->fs_mutex);
1116 * This is somewhat expensive, updating the tree every time the
1117 * inode changes. But, it is most likely to find the inode in cache.
1118 * FIXME, needs more benchmarking...there are no reasons other than performance
1119 * to keep or drop this code.
1121 void btrfs_dirty_inode(struct inode *inode)
1123 struct btrfs_root *root = BTRFS_I(inode)->root;
1124 struct btrfs_trans_handle *trans;
1126 mutex_lock(&root->fs_info->fs_mutex);
1127 trans = btrfs_start_transaction(root, 1);
1128 btrfs_set_trans_block_group(trans, inode);
1129 btrfs_update_inode(trans, root, inode);
1130 btrfs_end_transaction(trans, root);
1131 mutex_unlock(&root->fs_info->fs_mutex);
1134 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1135 struct btrfs_root *root,
1137 struct btrfs_block_group_cache *group,
1140 struct inode *inode;
1141 struct btrfs_inode_item *inode_item;
1142 struct btrfs_key *location;
1143 struct btrfs_path *path;
1147 path = btrfs_alloc_path();
1150 inode = new_inode(root->fs_info->sb);
1152 return ERR_PTR(-ENOMEM);
1154 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1155 inode->i_mapping, GFP_NOFS);
1156 BTRFS_I(inode)->root = root;
1162 group = btrfs_find_block_group(root, group, 0, 0, owner);
1163 BTRFS_I(inode)->block_group = group;
1165 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1169 inode->i_uid = current->fsuid;
1170 inode->i_gid = current->fsgid;
1171 inode->i_mode = mode;
1172 inode->i_ino = objectid;
1173 inode->i_blocks = 0;
1174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1175 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1176 struct btrfs_inode_item);
1177 fill_inode_item(path->nodes[0], inode_item, inode);
1178 btrfs_mark_buffer_dirty(path->nodes[0]);
1179 btrfs_free_path(path);
1181 location = &BTRFS_I(inode)->location;
1182 location->objectid = objectid;
1183 location->offset = 0;
1184 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1186 insert_inode_hash(inode);
1189 btrfs_free_path(path);
1190 return ERR_PTR(ret);
1193 static inline u8 btrfs_inode_type(struct inode *inode)
1195 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1198 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1199 struct dentry *dentry, struct inode *inode)
1202 struct btrfs_key key;
1203 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1204 struct inode *parent_inode;
1206 key.objectid = inode->i_ino;
1207 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1210 ret = btrfs_insert_dir_item(trans, root,
1211 dentry->d_name.name, dentry->d_name.len,
1212 dentry->d_parent->d_inode->i_ino,
1213 &key, btrfs_inode_type(inode));
1215 parent_inode = dentry->d_parent->d_inode;
1216 parent_inode->i_size += dentry->d_name.len * 2;
1217 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1218 ret = btrfs_update_inode(trans, root,
1219 dentry->d_parent->d_inode);
1224 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1225 struct dentry *dentry, struct inode *inode)
1227 int err = btrfs_add_link(trans, dentry, inode);
1229 d_instantiate(dentry, inode);
1237 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1238 int mode, dev_t rdev)
1240 struct btrfs_trans_handle *trans;
1241 struct btrfs_root *root = BTRFS_I(dir)->root;
1242 struct inode *inode;
1248 if (!new_valid_dev(rdev))
1251 mutex_lock(&root->fs_info->fs_mutex);
1252 trans = btrfs_start_transaction(root, 1);
1253 btrfs_set_trans_block_group(trans, dir);
1255 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1261 inode = btrfs_new_inode(trans, root, objectid,
1262 BTRFS_I(dir)->block_group, mode);
1263 err = PTR_ERR(inode);
1267 btrfs_set_trans_block_group(trans, inode);
1268 err = btrfs_add_nondir(trans, dentry, inode);
1272 inode->i_op = &btrfs_special_inode_operations;
1273 init_special_inode(inode, inode->i_mode, rdev);
1274 btrfs_update_inode(trans, root, inode);
1276 dir->i_sb->s_dirt = 1;
1277 btrfs_update_inode_block_group(trans, inode);
1278 btrfs_update_inode_block_group(trans, dir);
1280 nr = trans->blocks_used;
1281 btrfs_end_transaction(trans, root);
1282 mutex_unlock(&root->fs_info->fs_mutex);
1285 inode_dec_link_count(inode);
1288 btrfs_btree_balance_dirty(root, nr);
1292 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1293 int mode, struct nameidata *nd)
1295 struct btrfs_trans_handle *trans;
1296 struct btrfs_root *root = BTRFS_I(dir)->root;
1297 struct inode *inode;
1303 mutex_lock(&root->fs_info->fs_mutex);
1304 trans = btrfs_start_transaction(root, 1);
1305 btrfs_set_trans_block_group(trans, dir);
1307 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1313 inode = btrfs_new_inode(trans, root, objectid,
1314 BTRFS_I(dir)->block_group, mode);
1315 err = PTR_ERR(inode);
1319 btrfs_set_trans_block_group(trans, inode);
1320 err = btrfs_add_nondir(trans, dentry, inode);
1324 inode->i_mapping->a_ops = &btrfs_aops;
1325 inode->i_fop = &btrfs_file_operations;
1326 inode->i_op = &btrfs_file_inode_operations;
1327 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1328 inode->i_mapping, GFP_NOFS);
1329 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1331 dir->i_sb->s_dirt = 1;
1332 btrfs_update_inode_block_group(trans, inode);
1333 btrfs_update_inode_block_group(trans, dir);
1335 nr = trans->blocks_used;
1336 btrfs_end_transaction(trans, root);
1337 mutex_unlock(&root->fs_info->fs_mutex);
1340 inode_dec_link_count(inode);
1343 btrfs_btree_balance_dirty(root, nr);
1347 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1348 struct dentry *dentry)
1350 struct btrfs_trans_handle *trans;
1351 struct btrfs_root *root = BTRFS_I(dir)->root;
1352 struct inode *inode = old_dentry->d_inode;
1357 if (inode->i_nlink == 0)
1361 mutex_lock(&root->fs_info->fs_mutex);
1362 trans = btrfs_start_transaction(root, 1);
1364 btrfs_set_trans_block_group(trans, dir);
1365 atomic_inc(&inode->i_count);
1366 err = btrfs_add_nondir(trans, dentry, inode);
1371 dir->i_sb->s_dirt = 1;
1372 btrfs_update_inode_block_group(trans, dir);
1373 err = btrfs_update_inode(trans, root, inode);
1378 nr = trans->blocks_used;
1379 btrfs_end_transaction(trans, root);
1380 mutex_unlock(&root->fs_info->fs_mutex);
1383 inode_dec_link_count(inode);
1386 btrfs_btree_balance_dirty(root, nr);
1390 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1391 struct btrfs_root *root,
1392 u64 objectid, u64 dirid)
1396 struct btrfs_key key;
1401 key.objectid = objectid;
1403 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1405 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1406 &key, BTRFS_FT_DIR);
1410 key.objectid = dirid;
1411 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1412 &key, BTRFS_FT_DIR);
1419 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1421 struct inode *inode;
1422 struct btrfs_trans_handle *trans;
1423 struct btrfs_root *root = BTRFS_I(dir)->root;
1425 int drop_on_err = 0;
1427 unsigned long nr = 1;
1429 mutex_lock(&root->fs_info->fs_mutex);
1430 trans = btrfs_start_transaction(root, 1);
1431 btrfs_set_trans_block_group(trans, dir);
1433 if (IS_ERR(trans)) {
1434 err = PTR_ERR(trans);
1438 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1444 inode = btrfs_new_inode(trans, root, objectid,
1445 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1446 if (IS_ERR(inode)) {
1447 err = PTR_ERR(inode);
1452 inode->i_op = &btrfs_dir_inode_operations;
1453 inode->i_fop = &btrfs_dir_file_operations;
1454 btrfs_set_trans_block_group(trans, inode);
1456 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1461 err = btrfs_update_inode(trans, root, inode);
1465 err = btrfs_add_link(trans, dentry, inode);
1469 d_instantiate(dentry, inode);
1471 dir->i_sb->s_dirt = 1;
1472 btrfs_update_inode_block_group(trans, inode);
1473 btrfs_update_inode_block_group(trans, dir);
1476 nr = trans->blocks_used;
1477 btrfs_end_transaction(trans, root);
1480 mutex_unlock(&root->fs_info->fs_mutex);
1483 btrfs_btree_balance_dirty(root, nr);
1487 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1488 size_t page_offset, u64 start, u64 end,
1494 u64 extent_start = 0;
1496 u64 objectid = inode->i_ino;
1498 int failed_insert = 0;
1499 struct btrfs_path *path;
1500 struct btrfs_root *root = BTRFS_I(inode)->root;
1501 struct btrfs_file_extent_item *item;
1502 struct extent_buffer *leaf;
1503 struct btrfs_key found_key;
1504 struct extent_map *em = NULL;
1505 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1506 struct btrfs_trans_handle *trans = NULL;
1508 path = btrfs_alloc_path();
1510 mutex_lock(&root->fs_info->fs_mutex);
1513 em = lookup_extent_mapping(em_tree, start, end);
1518 em = alloc_extent_map(GFP_NOFS);
1523 em->start = EXTENT_MAP_HOLE;
1524 em->end = EXTENT_MAP_HOLE;
1526 em->bdev = inode->i_sb->s_bdev;
1527 ret = btrfs_lookup_file_extent(NULL, root, path,
1528 objectid, start, 0);
1535 if (path->slots[0] == 0)
1540 leaf = path->nodes[0];
1541 item = btrfs_item_ptr(leaf, path->slots[0],
1542 struct btrfs_file_extent_item);
1544 blocknr = btrfs_file_extent_disk_blocknr(leaf, item);
1545 blocknr += btrfs_file_extent_offset(leaf, item);
1547 /* are we inside the extent that was found? */
1548 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1549 found_type = btrfs_key_type(&found_key);
1550 if (found_key.objectid != objectid ||
1551 found_type != BTRFS_EXTENT_DATA_KEY) {
1555 found_type = btrfs_file_extent_type(leaf, item);
1556 extent_start = found_key.offset;
1557 if (found_type == BTRFS_FILE_EXTENT_REG) {
1558 extent_end = extent_start +
1559 (btrfs_file_extent_num_blocks(leaf, item) <<
1562 if (start < extent_start || start >= extent_end) {
1564 if (start < extent_start) {
1565 if (end < extent_start)
1567 em->end = extent_end - 1;
1573 if (btrfs_file_extent_disk_blocknr(leaf, item) == 0) {
1574 em->start = extent_start;
1575 em->end = extent_end - 1;
1576 em->block_start = EXTENT_MAP_HOLE;
1577 em->block_end = EXTENT_MAP_HOLE;
1580 em->block_start = blocknr << inode->i_blkbits;
1581 em->block_end = em->block_start +
1582 (btrfs_file_extent_num_blocks(leaf, item) <<
1583 inode->i_blkbits) - 1;
1584 em->start = extent_start;
1585 em->end = extent_end - 1;
1587 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1592 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1595 extent_end = extent_start | ((u64)root->sectorsize - 1);
1596 if (start < extent_start || start >= extent_end) {
1598 if (start < extent_start) {
1599 if (end < extent_start)
1601 em->end = extent_end;
1608 em->block_start = EXTENT_MAP_INLINE;
1609 em->block_end = EXTENT_MAP_INLINE;
1610 em->start = extent_start;
1611 em->end = extent_end;
1617 ptr = btrfs_file_extent_inline_start(item);
1619 read_extent_buffer(leaf, map + page_offset, ptr, size);
1620 memset(map + page_offset + size, 0,
1621 root->sectorsize - (page_offset + size));
1622 flush_dcache_page(page);
1624 set_extent_uptodate(em_tree, extent_start,
1625 extent_end, GFP_NOFS);
1628 printk("unkknown found_type %d\n", found_type);
1635 em->block_start = EXTENT_MAP_HOLE;
1636 em->block_end = EXTENT_MAP_HOLE;
1638 btrfs_release_path(root, path);
1639 if (em->start > start || em->end < start) {
1640 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1644 ret = add_extent_mapping(em_tree, em);
1645 if (ret == -EEXIST) {
1646 free_extent_map(em);
1649 if (failed_insert > 5) {
1650 printk("failing to insert %Lu %Lu\n", start, end);
1658 btrfs_free_path(path);
1660 ret = btrfs_end_transaction(trans, root);
1664 mutex_unlock(&root->fs_info->fs_mutex);
1666 free_extent_map(em);
1668 return ERR_PTR(err);
1673 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1675 return extent_bmap(mapping, iblock, btrfs_get_extent);
1678 static int btrfs_prepare_write(struct file *file, struct page *page,
1679 unsigned from, unsigned to)
1681 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1682 page->mapping->host, page, from, to,
1686 int btrfs_readpage(struct file *file, struct page *page)
1688 struct extent_map_tree *tree;
1689 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1690 return extent_read_full_page(tree, page, btrfs_get_extent);
1692 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1694 struct extent_map_tree *tree;
1697 if (current->flags & PF_MEMALLOC) {
1698 redirty_page_for_writepage(wbc, page);
1702 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1703 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1706 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1708 struct extent_map_tree *tree;
1711 if (page->private != 1) {
1713 return try_to_free_buffers(page);
1715 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1716 ret = try_release_extent_mapping(tree, page);
1718 ClearPagePrivate(page);
1719 set_page_private(page, 0);
1720 page_cache_release(page);
1725 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1727 struct extent_map_tree *tree;
1729 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1730 extent_invalidatepage(tree, page, offset);
1731 btrfs_releasepage(page, GFP_NOFS);
1735 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1736 * called from a page fault handler when a page is first dirtied. Hence we must
1737 * be careful to check for EOF conditions here. We set the page up correctly
1738 * for a written page which means we get ENOSPC checking when writing into
1739 * holes and correct delalloc and unwritten extent mapping on filesystems that
1740 * support these features.
1742 * We are not allowed to take the i_mutex here so we have to play games to
1743 * protect against truncate races as the page could now be beyond EOF. Because
1744 * vmtruncate() writes the inode size before removing pages, once we have the
1745 * page lock we can determine safely if the page is beyond EOF. If it is not
1746 * beyond EOF, then the page is guaranteed safe against truncation until we
1749 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1751 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1757 down_read(&BTRFS_I(inode)->root->snap_sem);
1759 wait_on_page_writeback(page);
1760 size = i_size_read(inode);
1761 page_start = page->index << PAGE_CACHE_SHIFT;
1763 if ((page->mapping != inode->i_mapping) ||
1764 (page_start > size)) {
1765 /* page got truncated out from underneath us */
1769 /* page is wholly or partially inside EOF */
1770 if (page_start + PAGE_CACHE_SIZE > size)
1771 end = size & ~PAGE_CACHE_MASK;
1773 end = PAGE_CACHE_SIZE;
1775 ret = btrfs_cow_one_page(inode, page, end);
1778 up_read(&BTRFS_I(inode)->root->snap_sem);
1783 static void btrfs_truncate(struct inode *inode)
1785 struct btrfs_root *root = BTRFS_I(inode)->root;
1787 struct btrfs_trans_handle *trans;
1790 if (!S_ISREG(inode->i_mode))
1792 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1795 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1797 mutex_lock(&root->fs_info->fs_mutex);
1798 trans = btrfs_start_transaction(root, 1);
1799 btrfs_set_trans_block_group(trans, inode);
1801 /* FIXME, add redo link to tree so we don't leak on crash */
1802 ret = btrfs_truncate_in_trans(trans, root, inode);
1803 btrfs_update_inode(trans, root, inode);
1804 nr = trans->blocks_used;
1806 ret = btrfs_end_transaction(trans, root);
1808 mutex_unlock(&root->fs_info->fs_mutex);
1809 btrfs_btree_balance_dirty(root, nr);
1812 int btrfs_commit_write(struct file *file, struct page *page,
1813 unsigned from, unsigned to)
1815 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1816 page->mapping->host, page, from, to);
1819 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1821 struct btrfs_trans_handle *trans;
1822 struct btrfs_key key;
1823 struct btrfs_root_item root_item;
1824 struct btrfs_inode_item *inode_item;
1825 struct extent_buffer *leaf;
1826 struct btrfs_root *new_root;
1827 struct inode *inode;
1832 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1833 unsigned long nr = 1;
1835 mutex_lock(&root->fs_info->fs_mutex);
1836 trans = btrfs_start_transaction(root, 1);
1839 leaf = btrfs_alloc_free_block(trans, root, 0, 0);
1841 return PTR_ERR(leaf);
1843 btrfs_set_header_nritems(leaf, 0);
1844 btrfs_set_header_level(leaf, 0);
1845 btrfs_set_header_blocknr(leaf, extent_buffer_blocknr(leaf));
1846 btrfs_set_header_generation(leaf, trans->transid);
1847 btrfs_set_header_owner(leaf, root->root_key.objectid);
1848 write_extent_buffer(leaf, root->fs_info->fsid,
1849 (unsigned long)btrfs_header_fsid(leaf),
1851 btrfs_mark_buffer_dirty(leaf);
1853 inode_item = &root_item.inode;
1854 memset(inode_item, 0, sizeof(*inode_item));
1855 inode_item->generation = cpu_to_le64(1);
1856 inode_item->size = cpu_to_le64(3);
1857 inode_item->nlink = cpu_to_le32(1);
1858 inode_item->nblocks = cpu_to_le64(1);
1859 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1861 btrfs_set_root_blocknr(&root_item, extent_buffer_blocknr(leaf));
1862 btrfs_set_root_refs(&root_item, 1);
1863 btrfs_set_root_used(&root_item, 0);
1865 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1866 root_item.drop_level = 0;
1868 free_extent_buffer(leaf);
1871 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1876 btrfs_set_root_dirid(&root_item, new_dirid);
1878 key.objectid = objectid;
1880 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1881 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1887 * insert the directory item
1889 key.offset = (u64)-1;
1890 dir = root->fs_info->sb->s_root->d_inode;
1891 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1892 name, namelen, dir->i_ino, &key,
1897 ret = btrfs_commit_transaction(trans, root);
1901 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
1904 trans = btrfs_start_transaction(new_root, 1);
1907 inode = btrfs_new_inode(trans, new_root, new_dirid,
1908 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
1911 inode->i_op = &btrfs_dir_inode_operations;
1912 inode->i_fop = &btrfs_dir_file_operations;
1913 new_root->inode = inode;
1915 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1921 ret = btrfs_update_inode(trans, new_root, inode);
1925 nr = trans->blocks_used;
1926 err = btrfs_commit_transaction(trans, root);
1930 mutex_unlock(&root->fs_info->fs_mutex);
1931 btrfs_btree_balance_dirty(root, nr);
1935 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1937 struct btrfs_trans_handle *trans;
1938 struct btrfs_key key;
1939 struct btrfs_root_item new_root_item;
1940 struct extent_buffer *tmp;
1946 if (!root->ref_cows)
1949 down_write(&root->snap_sem);
1950 freeze_bdev(root->fs_info->sb->s_bdev);
1951 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
1953 mutex_lock(&root->fs_info->fs_mutex);
1954 trans = btrfs_start_transaction(root, 1);
1957 ret = btrfs_update_inode(trans, root, root->inode);
1961 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1966 memcpy(&new_root_item, &root->root_item,
1967 sizeof(new_root_item));
1969 key.objectid = objectid;
1971 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1973 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
1974 btrfs_set_root_blocknr(&new_root_item,
1975 extent_buffer_blocknr(root->node));
1977 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1983 * insert the directory item
1985 key.offset = (u64)-1;
1986 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1988 root->fs_info->sb->s_root->d_inode->i_ino,
1989 &key, BTRFS_FT_DIR);
1994 ret = btrfs_inc_root_ref(trans, root);
1998 nr = trans->blocks_used;
1999 err = btrfs_commit_transaction(trans, root);
2004 mutex_unlock(&root->fs_info->fs_mutex);
2005 up_write(&root->snap_sem);
2006 btrfs_btree_balance_dirty(root, nr);
2010 static unsigned long force_ra(struct address_space *mapping,
2011 struct file_ra_state *ra, struct file *file,
2012 pgoff_t offset, pgoff_t last_index)
2016 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2017 req_size = last_index - offset + 1;
2018 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2021 req_size = min(last_index - offset + 1, (pgoff_t)128);
2022 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2023 return offset + req_size;
2027 int btrfs_defrag_file(struct file *file) {
2028 struct inode *inode = file->f_path.dentry->d_inode;
2029 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2031 unsigned long last_index;
2032 unsigned long ra_index = 0;
2037 mutex_lock(&inode->i_mutex);
2038 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2039 for (i = 0; i <= last_index; i++) {
2040 if (i == ra_index) {
2041 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2042 file, ra_index, last_index);
2044 page = grab_cache_page(inode->i_mapping, i);
2047 if (!PageUptodate(page)) {
2048 btrfs_readpage(NULL, page);
2050 if (!PageUptodate(page)) {
2052 page_cache_release(page);
2056 page_start = page->index << PAGE_CACHE_SHIFT;
2057 page_end = page_start + PAGE_CACHE_SIZE - 1;
2059 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2060 set_extent_delalloc(em_tree, page_start,
2061 page_end, GFP_NOFS);
2062 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2063 set_page_dirty(page);
2065 page_cache_release(page);
2066 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2070 mutex_unlock(&inode->i_mutex);
2074 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2076 struct btrfs_ioctl_vol_args vol_args;
2077 struct btrfs_dir_item *di;
2078 struct btrfs_path *path;
2082 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2085 namelen = strlen(vol_args.name);
2086 if (namelen > BTRFS_VOL_NAME_MAX)
2088 if (strchr(vol_args.name, '/'))
2091 path = btrfs_alloc_path();
2095 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2096 mutex_lock(&root->fs_info->fs_mutex);
2097 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2099 vol_args.name, namelen, 0);
2100 mutex_unlock(&root->fs_info->fs_mutex);
2101 btrfs_free_path(path);
2102 if (di && !IS_ERR(di))
2107 if (root == root->fs_info->tree_root)
2108 return create_subvol(root, vol_args.name, namelen);
2109 return create_snapshot(root, vol_args.name, namelen);
2112 static int btrfs_ioctl_defrag(struct file *file)
2114 struct inode *inode = file->f_path.dentry->d_inode;
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2117 switch (inode->i_mode & S_IFMT) {
2119 mutex_lock(&root->fs_info->fs_mutex);
2120 btrfs_defrag_root(root, 0);
2121 btrfs_defrag_root(root->fs_info->extent_root, 0);
2122 mutex_unlock(&root->fs_info->fs_mutex);
2125 btrfs_defrag_file(file);
2132 long btrfs_ioctl(struct file *file, unsigned int
2133 cmd, unsigned long arg)
2135 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
2138 case BTRFS_IOC_SNAP_CREATE:
2139 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2140 case BTRFS_IOC_DEFRAG:
2141 return btrfs_ioctl_defrag(file);
2148 * Called inside transaction, so use GFP_NOFS
2150 struct inode *btrfs_alloc_inode(struct super_block *sb)
2152 struct btrfs_inode *ei;
2154 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2158 return &ei->vfs_inode;
2161 void btrfs_destroy_inode(struct inode *inode)
2163 WARN_ON(!list_empty(&inode->i_dentry));
2164 WARN_ON(inode->i_data.nrpages);
2166 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2169 static void init_once(void * foo, struct kmem_cache * cachep,
2170 unsigned long flags)
2172 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2174 inode_init_once(&ei->vfs_inode);
2177 void btrfs_destroy_cachep(void)
2179 if (btrfs_inode_cachep)
2180 kmem_cache_destroy(btrfs_inode_cachep);
2181 if (btrfs_trans_handle_cachep)
2182 kmem_cache_destroy(btrfs_trans_handle_cachep);
2183 if (btrfs_transaction_cachep)
2184 kmem_cache_destroy(btrfs_transaction_cachep);
2185 if (btrfs_bit_radix_cachep)
2186 kmem_cache_destroy(btrfs_bit_radix_cachep);
2187 if (btrfs_path_cachep)
2188 kmem_cache_destroy(btrfs_path_cachep);
2191 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2192 unsigned long extra_flags,
2193 void (*ctor)(void *, struct kmem_cache *,
2196 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2197 SLAB_MEM_SPREAD | extra_flags), ctor
2198 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2204 int btrfs_init_cachep(void)
2206 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2207 sizeof(struct btrfs_inode),
2209 if (!btrfs_inode_cachep)
2211 btrfs_trans_handle_cachep =
2212 btrfs_cache_create("btrfs_trans_handle_cache",
2213 sizeof(struct btrfs_trans_handle),
2215 if (!btrfs_trans_handle_cachep)
2217 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2218 sizeof(struct btrfs_transaction),
2220 if (!btrfs_transaction_cachep)
2222 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2223 sizeof(struct btrfs_path),
2225 if (!btrfs_path_cachep)
2227 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2228 SLAB_DESTROY_BY_RCU, NULL);
2229 if (!btrfs_bit_radix_cachep)
2233 btrfs_destroy_cachep();
2237 static int btrfs_getattr(struct vfsmount *mnt,
2238 struct dentry *dentry, struct kstat *stat)
2240 struct inode *inode = dentry->d_inode;
2241 generic_fillattr(inode, stat);
2242 stat->blksize = 256 * 1024;
2246 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2247 struct inode * new_dir,struct dentry *new_dentry)
2249 struct btrfs_trans_handle *trans;
2250 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2251 struct inode *new_inode = new_dentry->d_inode;
2252 struct inode *old_inode = old_dentry->d_inode;
2253 struct timespec ctime = CURRENT_TIME;
2254 struct btrfs_path *path;
2255 struct btrfs_dir_item *di;
2258 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2259 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2263 mutex_lock(&root->fs_info->fs_mutex);
2264 trans = btrfs_start_transaction(root, 1);
2266 btrfs_set_trans_block_group(trans, new_dir);
2267 path = btrfs_alloc_path();
2273 old_dentry->d_inode->i_nlink++;
2274 old_dir->i_ctime = old_dir->i_mtime = ctime;
2275 new_dir->i_ctime = new_dir->i_mtime = ctime;
2276 old_inode->i_ctime = ctime;
2278 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2279 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2280 struct btrfs_key old_parent_key;
2281 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2291 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
2292 ret = btrfs_del_item(trans, root, path);
2296 btrfs_release_path(root, path);
2298 di = btrfs_lookup_dir_index_item(trans, root, path,
2300 old_parent_key.objectid,
2310 ret = btrfs_del_item(trans, root, path);
2314 btrfs_release_path(root, path);
2316 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2317 old_inode->i_ino, location,
2324 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2329 new_inode->i_ctime = CURRENT_TIME;
2330 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2334 ret = btrfs_add_link(trans, new_dentry, old_inode);
2339 btrfs_free_path(path);
2340 btrfs_end_transaction(trans, root);
2341 mutex_unlock(&root->fs_info->fs_mutex);
2345 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2346 const char *symname)
2348 struct btrfs_trans_handle *trans;
2349 struct btrfs_root *root = BTRFS_I(dir)->root;
2350 struct btrfs_path *path;
2351 struct btrfs_key key;
2352 struct inode *inode;
2359 struct btrfs_file_extent_item *ei;
2360 struct extent_buffer *leaf;
2363 name_len = strlen(symname) + 1;
2364 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2365 return -ENAMETOOLONG;
2366 mutex_lock(&root->fs_info->fs_mutex);
2367 trans = btrfs_start_transaction(root, 1);
2368 btrfs_set_trans_block_group(trans, dir);
2370 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2376 inode = btrfs_new_inode(trans, root, objectid,
2377 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2378 err = PTR_ERR(inode);
2382 btrfs_set_trans_block_group(trans, inode);
2383 err = btrfs_add_nondir(trans, dentry, inode);
2387 inode->i_mapping->a_ops = &btrfs_aops;
2388 inode->i_fop = &btrfs_file_operations;
2389 inode->i_op = &btrfs_file_inode_operations;
2390 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2391 inode->i_mapping, GFP_NOFS);
2392 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2394 dir->i_sb->s_dirt = 1;
2395 btrfs_update_inode_block_group(trans, inode);
2396 btrfs_update_inode_block_group(trans, dir);
2400 path = btrfs_alloc_path();
2402 key.objectid = inode->i_ino;
2404 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2405 datasize = btrfs_file_extent_calc_inline_size(name_len);
2406 err = btrfs_insert_empty_item(trans, root, path, &key,
2412 leaf = path->nodes[0];
2413 ei = btrfs_item_ptr(leaf, path->slots[0],
2414 struct btrfs_file_extent_item);
2415 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2416 btrfs_set_file_extent_type(leaf, ei,
2417 BTRFS_FILE_EXTENT_INLINE);
2418 ptr = btrfs_file_extent_inline_start(ei);
2419 write_extent_buffer(leaf, symname, ptr, name_len);
2420 btrfs_mark_buffer_dirty(leaf);
2421 btrfs_free_path(path);
2423 inode->i_op = &btrfs_symlink_inode_operations;
2424 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2425 inode->i_size = name_len - 1;
2426 err = btrfs_update_inode(trans, root, inode);
2431 nr = trans->blocks_used;
2432 btrfs_end_transaction(trans, root);
2433 mutex_unlock(&root->fs_info->fs_mutex);
2435 inode_dec_link_count(inode);
2438 btrfs_btree_balance_dirty(root, nr);
2442 static struct inode_operations btrfs_dir_inode_operations = {
2443 .lookup = btrfs_lookup,
2444 .create = btrfs_create,
2445 .unlink = btrfs_unlink,
2447 .mkdir = btrfs_mkdir,
2448 .rmdir = btrfs_rmdir,
2449 .rename = btrfs_rename,
2450 .symlink = btrfs_symlink,
2451 .setattr = btrfs_setattr,
2452 .mknod = btrfs_mknod,
2455 static struct inode_operations btrfs_dir_ro_inode_operations = {
2456 .lookup = btrfs_lookup,
2459 static struct file_operations btrfs_dir_file_operations = {
2460 .llseek = generic_file_llseek,
2461 .read = generic_read_dir,
2462 .readdir = btrfs_readdir,
2463 .unlocked_ioctl = btrfs_ioctl,
2464 #ifdef CONFIG_COMPAT
2465 .compat_ioctl = btrfs_ioctl,
2469 static struct extent_map_ops btrfs_extent_map_ops = {
2470 .fill_delalloc = run_delalloc_range,
2471 .writepage_io_hook = btrfs_writepage_io_hook,
2472 .readpage_io_hook = btrfs_readpage_io_hook,
2473 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2476 static struct address_space_operations btrfs_aops = {
2477 .readpage = btrfs_readpage,
2478 .writepage = btrfs_writepage,
2479 .sync_page = block_sync_page,
2480 .prepare_write = btrfs_prepare_write,
2481 .commit_write = btrfs_commit_write,
2483 .invalidatepage = btrfs_invalidatepage,
2484 .releasepage = btrfs_releasepage,
2485 .set_page_dirty = __set_page_dirty_nobuffers,
2488 static struct address_space_operations btrfs_symlink_aops = {
2489 .readpage = btrfs_readpage,
2490 .writepage = btrfs_writepage,
2491 .invalidatepage = btrfs_invalidatepage,
2492 .releasepage = btrfs_releasepage,
2495 static struct inode_operations btrfs_file_inode_operations = {
2496 .truncate = btrfs_truncate,
2497 .getattr = btrfs_getattr,
2498 .setattr = btrfs_setattr,
2501 static struct inode_operations btrfs_special_inode_operations = {
2502 .getattr = btrfs_getattr,
2503 .setattr = btrfs_setattr,
2506 static struct inode_operations btrfs_symlink_inode_operations = {
2507 .readlink = generic_readlink,
2508 .follow_link = page_follow_link_light,
2509 .put_link = page_put_link,