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>
35 #include <linux/xattr.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "print-tree.h"
43 struct btrfs_iget_args {
45 struct btrfs_root *root;
48 static struct inode_operations btrfs_dir_inode_operations;
49 static struct inode_operations btrfs_symlink_inode_operations;
50 static struct inode_operations btrfs_dir_ro_inode_operations;
51 static struct inode_operations btrfs_special_inode_operations;
52 static struct inode_operations btrfs_file_inode_operations;
53 static struct address_space_operations btrfs_aops;
54 static struct address_space_operations btrfs_symlink_aops;
55 static struct file_operations btrfs_dir_file_operations;
56 static struct extent_io_ops btrfs_extent_io_ops;
58 static struct kmem_cache *btrfs_inode_cachep;
59 struct kmem_cache *btrfs_trans_handle_cachep;
60 struct kmem_cache *btrfs_transaction_cachep;
61 struct kmem_cache *btrfs_bit_radix_cachep;
62 struct kmem_cache *btrfs_path_cachep;
65 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
95 spin_unlock(&root->fs_info->delalloc_lock);
99 static int cow_file_range(struct inode *inode, u64 start, u64 end)
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
106 u64 blocksize = root->sectorsize;
107 u64 orig_start = start;
109 struct btrfs_key ins;
112 trans = btrfs_start_transaction(root, 1);
114 btrfs_set_trans_block_group(trans, inode);
116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
117 num_bytes = max(blocksize, num_bytes);
118 ret = btrfs_drop_extents(trans, root, inode,
119 start, start + num_bytes, start, &alloc_hint);
120 orig_num_bytes = num_bytes;
122 if (alloc_hint == EXTENT_MAP_INLINE)
125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
139 btrfs_check_file(root, inode);
140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
146 btrfs_add_ordered_inode(inode);
148 btrfs_end_transaction(trans, root);
152 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
160 struct btrfs_root *root = BTRFS_I(inode)->root;
161 struct extent_buffer *leaf;
163 struct btrfs_path *path;
164 struct btrfs_file_extent_item *item;
167 struct btrfs_key found_key;
169 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
170 path = btrfs_alloc_path();
173 ret = btrfs_lookup_file_extent(NULL, root, path,
174 inode->i_ino, start, 0);
176 btrfs_free_path(path);
182 if (path->slots[0] == 0)
187 leaf = path->nodes[0];
188 item = btrfs_item_ptr(leaf, path->slots[0],
189 struct btrfs_file_extent_item);
191 /* are we inside the extent that was found? */
192 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
193 found_type = btrfs_key_type(&found_key);
194 if (found_key.objectid != inode->i_ino ||
195 found_type != BTRFS_EXTENT_DATA_KEY) {
199 found_type = btrfs_file_extent_type(leaf, item);
200 extent_start = found_key.offset;
201 if (found_type == BTRFS_FILE_EXTENT_REG) {
202 u64 extent_num_bytes;
204 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
205 extent_end = extent_start + extent_num_bytes;
208 if (loops && start != extent_start)
211 if (start < extent_start || start >= extent_end)
214 cow_end = min(end, extent_end - 1);
215 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
220 * we may be called by the resizer, make sure we're inside
221 * the limits of the FS
223 if (bytenr + extent_num_bytes > total_fs_bytes)
226 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
236 btrfs_free_path(path);
239 btrfs_release_path(root, path);
244 cow_file_range(inode, start, cow_end);
249 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
251 struct btrfs_root *root = BTRFS_I(inode)->root;
254 mutex_lock(&root->fs_info->fs_mutex);
255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
257 ret = run_delalloc_nocow(inode, start, end);
259 ret = cow_file_range(inode, start, end);
261 spin_lock(&root->fs_info->delalloc_lock);
262 num_bytes = end + 1 - start;
263 if (root->fs_info->delalloc_bytes < num_bytes) {
264 printk("delalloc accounting error total %llu sub %llu\n",
265 root->fs_info->delalloc_bytes, num_bytes);
267 root->fs_info->delalloc_bytes -= num_bytes;
269 spin_unlock(&root->fs_info->delalloc_lock);
271 mutex_unlock(&root->fs_info->fs_mutex);
275 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
277 struct inode *inode = page->mapping->host;
278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 struct btrfs_trans_handle *trans;
282 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
283 size_t offset = start - page_start;
284 if (btrfs_test_opt(root, NODATASUM) ||
285 btrfs_test_flag(inode, NODATASUM))
287 mutex_lock(&root->fs_info->fs_mutex);
288 trans = btrfs_start_transaction(root, 1);
289 btrfs_set_trans_block_group(trans, inode);
291 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
292 start, kaddr + offset, end - start + 1);
294 ret = btrfs_end_transaction(trans, root);
296 mutex_unlock(&root->fs_info->fs_mutex);
300 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
303 struct inode *inode = page->mapping->host;
304 struct btrfs_root *root = BTRFS_I(inode)->root;
305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
306 struct btrfs_csum_item *item;
307 struct btrfs_path *path = NULL;
309 if (btrfs_test_opt(root, NODATASUM) ||
310 btrfs_test_flag(inode, NODATASUM))
312 mutex_lock(&root->fs_info->fs_mutex);
313 path = btrfs_alloc_path();
314 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
317 /* a csum that isn't present is a preallocated region. */
318 if (ret == -ENOENT || ret == -EFBIG)
321 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
324 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
326 set_state_private(io_tree, start, csum);
329 btrfs_free_path(path);
330 mutex_unlock(&root->fs_info->fs_mutex);
334 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
335 struct extent_state *state)
337 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
338 struct inode *inode = page->mapping->host;
339 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
341 u64 private = ~(u32)0;
343 struct btrfs_root *root = BTRFS_I(inode)->root;
347 if (btrfs_test_opt(root, NODATASUM) ||
348 btrfs_test_flag(inode, NODATASUM))
351 if (state->start == start) {
352 private = state->private;
355 ret = get_state_private(io_tree, start, &private);
357 local_irq_save(flags);
358 kaddr = kmap_atomic(page, KM_IRQ0);
362 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
363 btrfs_csum_final(csum, (char *)&csum);
364 if (csum != private) {
367 kunmap_atomic(kaddr, KM_IRQ0);
368 local_irq_restore(flags);
372 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
373 page->mapping->host->i_ino, (unsigned long long)start, csum,
375 memset(kaddr + offset, 1, end - start + 1);
376 flush_dcache_page(page);
377 kunmap_atomic(kaddr, KM_IRQ0);
378 local_irq_restore(flags);
382 void btrfs_read_locked_inode(struct inode *inode)
384 struct btrfs_path *path;
385 struct extent_buffer *leaf;
386 struct btrfs_inode_item *inode_item;
387 struct btrfs_inode_timespec *tspec;
388 struct btrfs_root *root = BTRFS_I(inode)->root;
389 struct btrfs_key location;
390 u64 alloc_group_block;
394 path = btrfs_alloc_path();
396 mutex_lock(&root->fs_info->fs_mutex);
397 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
399 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
403 leaf = path->nodes[0];
404 inode_item = btrfs_item_ptr(leaf, path->slots[0],
405 struct btrfs_inode_item);
407 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
408 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
409 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
410 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
411 inode->i_size = btrfs_inode_size(leaf, inode_item);
413 tspec = btrfs_inode_atime(inode_item);
414 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
415 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
417 tspec = btrfs_inode_mtime(inode_item);
418 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
419 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
421 tspec = btrfs_inode_ctime(inode_item);
422 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
423 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
425 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
426 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
428 rdev = btrfs_inode_rdev(leaf, inode_item);
430 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
431 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
433 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
434 if (!BTRFS_I(inode)->block_group) {
435 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
438 btrfs_free_path(path);
441 mutex_unlock(&root->fs_info->fs_mutex);
443 switch (inode->i_mode & S_IFMT) {
445 inode->i_mapping->a_ops = &btrfs_aops;
446 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
447 inode->i_fop = &btrfs_file_operations;
448 inode->i_op = &btrfs_file_inode_operations;
451 inode->i_fop = &btrfs_dir_file_operations;
452 if (root == root->fs_info->tree_root)
453 inode->i_op = &btrfs_dir_ro_inode_operations;
455 inode->i_op = &btrfs_dir_inode_operations;
458 inode->i_op = &btrfs_symlink_inode_operations;
459 inode->i_mapping->a_ops = &btrfs_symlink_aops;
462 init_special_inode(inode, inode->i_mode, rdev);
468 btrfs_release_path(root, path);
469 btrfs_free_path(path);
470 mutex_unlock(&root->fs_info->fs_mutex);
471 make_bad_inode(inode);
474 static void fill_inode_item(struct extent_buffer *leaf,
475 struct btrfs_inode_item *item,
478 btrfs_set_inode_uid(leaf, item, inode->i_uid);
479 btrfs_set_inode_gid(leaf, item, inode->i_gid);
480 btrfs_set_inode_size(leaf, item, inode->i_size);
481 btrfs_set_inode_mode(leaf, item, inode->i_mode);
482 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
484 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
485 inode->i_atime.tv_sec);
486 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
487 inode->i_atime.tv_nsec);
489 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
490 inode->i_mtime.tv_sec);
491 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
492 inode->i_mtime.tv_nsec);
494 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
495 inode->i_ctime.tv_sec);
496 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
497 inode->i_ctime.tv_nsec);
499 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
500 btrfs_set_inode_generation(leaf, item, inode->i_generation);
501 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
502 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
503 btrfs_set_inode_block_group(leaf, item,
504 BTRFS_I(inode)->block_group->key.objectid);
507 int btrfs_update_inode(struct btrfs_trans_handle *trans,
508 struct btrfs_root *root,
511 struct btrfs_inode_item *inode_item;
512 struct btrfs_path *path;
513 struct extent_buffer *leaf;
516 path = btrfs_alloc_path();
518 ret = btrfs_lookup_inode(trans, root, path,
519 &BTRFS_I(inode)->location, 1);
526 leaf = path->nodes[0];
527 inode_item = btrfs_item_ptr(leaf, path->slots[0],
528 struct btrfs_inode_item);
530 fill_inode_item(leaf, inode_item, inode);
531 btrfs_mark_buffer_dirty(leaf);
532 btrfs_set_inode_last_trans(trans, inode);
535 btrfs_release_path(root, path);
536 btrfs_free_path(path);
541 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root,
544 struct dentry *dentry)
546 struct btrfs_path *path;
547 const char *name = dentry->d_name.name;
548 int name_len = dentry->d_name.len;
550 struct extent_buffer *leaf;
551 struct btrfs_dir_item *di;
552 struct btrfs_key key;
554 path = btrfs_alloc_path();
560 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
570 leaf = path->nodes[0];
571 btrfs_dir_item_key_to_cpu(leaf, di, &key);
572 ret = btrfs_delete_one_dir_name(trans, root, path, di);
575 btrfs_release_path(root, path);
577 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
578 key.objectid, name, name_len, -1);
587 ret = btrfs_delete_one_dir_name(trans, root, path, di);
589 dentry->d_inode->i_ctime = dir->i_ctime;
590 ret = btrfs_del_inode_ref(trans, root, name, name_len,
591 dentry->d_inode->i_ino,
592 dentry->d_parent->d_inode->i_ino);
594 printk("failed to delete reference to %.*s, "
595 "inode %lu parent %lu\n", name_len, name,
596 dentry->d_inode->i_ino,
597 dentry->d_parent->d_inode->i_ino);
600 btrfs_free_path(path);
602 dir->i_size -= name_len * 2;
603 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
604 btrfs_update_inode(trans, root, dir);
605 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
606 dentry->d_inode->i_nlink--;
608 drop_nlink(dentry->d_inode);
610 ret = btrfs_update_inode(trans, root, dentry->d_inode);
611 dir->i_sb->s_dirt = 1;
616 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
618 struct btrfs_root *root;
619 struct btrfs_trans_handle *trans;
620 struct inode *inode = dentry->d_inode;
622 unsigned long nr = 0;
624 root = BTRFS_I(dir)->root;
625 mutex_lock(&root->fs_info->fs_mutex);
627 ret = btrfs_check_free_space(root, 1, 1);
631 trans = btrfs_start_transaction(root, 1);
633 btrfs_set_trans_block_group(trans, dir);
634 ret = btrfs_unlink_trans(trans, root, dir, dentry);
635 nr = trans->blocks_used;
637 if (inode->i_nlink == 0) {
639 /* if the inode isn't linked anywhere,
640 * we don't need to worry about
643 found = btrfs_del_ordered_inode(inode);
645 atomic_dec(&inode->i_count);
649 btrfs_end_transaction(trans, root);
651 mutex_unlock(&root->fs_info->fs_mutex);
652 btrfs_btree_balance_dirty(root, nr);
653 btrfs_throttle(root);
657 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
659 struct inode *inode = dentry->d_inode;
662 struct btrfs_root *root = BTRFS_I(dir)->root;
663 struct btrfs_trans_handle *trans;
664 unsigned long nr = 0;
666 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
669 mutex_lock(&root->fs_info->fs_mutex);
670 ret = btrfs_check_free_space(root, 1, 1);
674 trans = btrfs_start_transaction(root, 1);
675 btrfs_set_trans_block_group(trans, dir);
677 /* now the directory is empty */
678 err = btrfs_unlink_trans(trans, root, dir, dentry);
683 nr = trans->blocks_used;
684 ret = btrfs_end_transaction(trans, root);
686 mutex_unlock(&root->fs_info->fs_mutex);
687 btrfs_btree_balance_dirty(root, nr);
688 btrfs_throttle(root);
695 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
696 struct btrfs_root *root,
699 struct btrfs_path *path;
704 path = btrfs_alloc_path();
706 ret = btrfs_lookup_inode(trans, root, path,
707 &BTRFS_I(inode)->location, -1);
711 ret = btrfs_del_item(trans, root, path);
712 btrfs_free_path(path);
717 * this can truncate away extent items, csum items and directory items.
718 * It starts at a high offset and removes keys until it can't find
719 * any higher than i_size.
721 * csum items that cross the new i_size are truncated to the new size
724 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
725 struct btrfs_root *root,
729 struct btrfs_path *path;
730 struct btrfs_key key;
731 struct btrfs_key found_key;
733 struct extent_buffer *leaf;
734 struct btrfs_file_extent_item *fi;
735 u64 extent_start = 0;
736 u64 extent_num_bytes = 0;
742 int extent_type = -1;
744 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
745 path = btrfs_alloc_path();
749 /* FIXME, add redo link to tree so we don't leak on crash */
750 key.objectid = inode->i_ino;
751 key.offset = (u64)-1;
755 btrfs_init_path(path);
757 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
762 BUG_ON(path->slots[0] == 0);
765 leaf = path->nodes[0];
766 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
767 found_type = btrfs_key_type(&found_key);
769 if (found_key.objectid != inode->i_ino)
772 if (found_type != BTRFS_CSUM_ITEM_KEY &&
773 found_type != BTRFS_DIR_ITEM_KEY &&
774 found_type != BTRFS_DIR_INDEX_KEY &&
775 found_type != BTRFS_EXTENT_DATA_KEY)
778 item_end = found_key.offset;
779 if (found_type == BTRFS_EXTENT_DATA_KEY) {
780 fi = btrfs_item_ptr(leaf, path->slots[0],
781 struct btrfs_file_extent_item);
782 extent_type = btrfs_file_extent_type(leaf, fi);
783 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
785 btrfs_file_extent_num_bytes(leaf, fi);
786 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
787 struct btrfs_item *item = btrfs_item_nr(leaf,
789 item_end += btrfs_file_extent_inline_len(leaf,
794 if (found_type == BTRFS_CSUM_ITEM_KEY) {
795 ret = btrfs_csum_truncate(trans, root, path,
799 if (item_end < inode->i_size) {
800 if (found_type == BTRFS_DIR_ITEM_KEY) {
801 found_type = BTRFS_INODE_ITEM_KEY;
802 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
803 found_type = BTRFS_CSUM_ITEM_KEY;
804 } else if (found_type) {
809 btrfs_set_key_type(&key, found_type);
810 btrfs_release_path(root, path);
813 if (found_key.offset >= inode->i_size)
819 /* FIXME, shrink the extent if the ref count is only 1 */
820 if (found_type != BTRFS_EXTENT_DATA_KEY)
823 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
825 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
828 btrfs_file_extent_num_bytes(leaf, fi);
829 extent_num_bytes = inode->i_size -
830 found_key.offset + root->sectorsize - 1;
831 btrfs_set_file_extent_num_bytes(leaf, fi,
833 num_dec = (orig_num_bytes -
834 extent_num_bytes) >> 9;
835 if (extent_start != 0) {
836 inode->i_blocks -= num_dec;
838 btrfs_mark_buffer_dirty(leaf);
841 btrfs_file_extent_disk_num_bytes(leaf,
843 /* FIXME blocksize != 4096 */
844 num_dec = btrfs_file_extent_num_bytes(leaf,
846 if (extent_start != 0) {
848 inode->i_blocks -= num_dec;
850 root_gen = btrfs_header_generation(leaf);
851 root_owner = btrfs_header_owner(leaf);
853 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
855 u32 newsize = inode->i_size - found_key.offset;
856 newsize = btrfs_file_extent_calc_inline_size(newsize);
857 ret = btrfs_truncate_item(trans, root, path,
863 ret = btrfs_del_item(trans, root, path);
869 btrfs_release_path(root, path);
871 ret = btrfs_free_extent(trans, root, extent_start,
874 root_gen, inode->i_ino,
875 found_key.offset, 0);
881 btrfs_release_path(root, path);
882 btrfs_free_path(path);
883 inode->i_sb->s_dirt = 1;
887 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
892 struct btrfs_root *root = BTRFS_I(inode)->root;
893 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
894 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
895 u64 existing_delalloc;
899 WARN_ON(!PageLocked(page));
900 set_page_extent_mapped(page);
902 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
903 delalloc_start = page_start;
904 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
905 &delalloc_start, page_end,
906 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
907 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
910 spin_lock(&root->fs_info->delalloc_lock);
911 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
912 spin_unlock(&root->fs_info->delalloc_lock);
914 if (zero_start != PAGE_CACHE_SIZE) {
916 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
917 flush_dcache_page(page);
920 set_page_dirty(page);
921 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
927 * taken from block_truncate_page, but does cow as it zeros out
928 * any bytes left in the last page in the file.
930 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
932 struct inode *inode = mapping->host;
933 struct btrfs_root *root = BTRFS_I(inode)->root;
934 u32 blocksize = root->sectorsize;
935 pgoff_t index = from >> PAGE_CACHE_SHIFT;
936 unsigned offset = from & (PAGE_CACHE_SIZE-1);
941 if ((offset & (blocksize - 1)) == 0)
945 page = grab_cache_page(mapping, index);
948 if (!PageUptodate(page)) {
949 ret = btrfs_readpage(NULL, page);
951 if (!PageUptodate(page)) {
956 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
958 ret = btrfs_cow_one_page(inode, page, offset);
961 page_cache_release(page);
966 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
968 struct inode *inode = dentry->d_inode;
971 err = inode_change_ok(inode, attr);
975 if (S_ISREG(inode->i_mode) &&
976 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
977 struct btrfs_trans_handle *trans;
978 struct btrfs_root *root = BTRFS_I(inode)->root;
979 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
981 u64 mask = root->sectorsize - 1;
982 u64 pos = (inode->i_size + mask) & ~mask;
983 u64 block_end = attr->ia_size | mask;
988 if (attr->ia_size <= pos)
991 if (pos != inode->i_size)
992 hole_start = pos + root->sectorsize;
996 mutex_lock(&root->fs_info->fs_mutex);
997 err = btrfs_check_free_space(root, 1, 0);
998 mutex_unlock(&root->fs_info->fs_mutex);
1002 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1004 lock_extent(io_tree, pos, block_end, GFP_NOFS);
1005 hole_size = block_end - hole_start;
1007 mutex_lock(&root->fs_info->fs_mutex);
1008 trans = btrfs_start_transaction(root, 1);
1009 btrfs_set_trans_block_group(trans, inode);
1010 err = btrfs_drop_extents(trans, root, inode,
1011 pos, block_end, pos,
1014 if (alloc_hint != EXTENT_MAP_INLINE) {
1015 err = btrfs_insert_file_extent(trans, root,
1019 btrfs_drop_extent_cache(inode, hole_start,
1021 btrfs_check_file(root, inode);
1023 btrfs_end_transaction(trans, root);
1024 mutex_unlock(&root->fs_info->fs_mutex);
1025 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
1030 err = inode_setattr(inode, attr);
1035 void btrfs_put_inode(struct inode *inode)
1039 if (!BTRFS_I(inode)->ordered_trans) {
1043 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1044 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1047 ret = btrfs_del_ordered_inode(inode);
1049 atomic_dec(&inode->i_count);
1053 void btrfs_delete_inode(struct inode *inode)
1055 struct btrfs_trans_handle *trans;
1056 struct btrfs_root *root = BTRFS_I(inode)->root;
1060 truncate_inode_pages(&inode->i_data, 0);
1061 if (is_bad_inode(inode)) {
1066 mutex_lock(&root->fs_info->fs_mutex);
1067 trans = btrfs_start_transaction(root, 1);
1069 btrfs_set_trans_block_group(trans, inode);
1070 ret = btrfs_truncate_in_trans(trans, root, inode);
1072 goto no_delete_lock;
1073 ret = btrfs_delete_xattrs(trans, root, inode);
1075 goto no_delete_lock;
1076 ret = btrfs_free_inode(trans, root, inode);
1078 goto no_delete_lock;
1079 nr = trans->blocks_used;
1081 btrfs_end_transaction(trans, root);
1082 mutex_unlock(&root->fs_info->fs_mutex);
1083 btrfs_btree_balance_dirty(root, nr);
1084 btrfs_throttle(root);
1088 nr = trans->blocks_used;
1089 btrfs_end_transaction(trans, root);
1090 mutex_unlock(&root->fs_info->fs_mutex);
1091 btrfs_btree_balance_dirty(root, nr);
1092 btrfs_throttle(root);
1098 * this returns the key found in the dir entry in the location pointer.
1099 * If no dir entries were found, location->objectid is 0.
1101 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1102 struct btrfs_key *location)
1104 const char *name = dentry->d_name.name;
1105 int namelen = dentry->d_name.len;
1106 struct btrfs_dir_item *di;
1107 struct btrfs_path *path;
1108 struct btrfs_root *root = BTRFS_I(dir)->root;
1111 if (namelen == 1 && strcmp(name, ".") == 0) {
1112 location->objectid = dir->i_ino;
1113 location->type = BTRFS_INODE_ITEM_KEY;
1114 location->offset = 0;
1117 path = btrfs_alloc_path();
1120 if (namelen == 2 && strcmp(name, "..") == 0) {
1121 struct btrfs_key key;
1122 struct extent_buffer *leaf;
1126 key.objectid = dir->i_ino;
1127 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1133 leaf = path->nodes[0];
1134 slot = path->slots[0];
1135 nritems = btrfs_header_nritems(leaf);
1136 if (slot >= nritems)
1139 btrfs_item_key_to_cpu(leaf, &key, slot);
1140 if (key.objectid != dir->i_ino ||
1141 key.type != BTRFS_INODE_REF_KEY) {
1144 location->objectid = key.offset;
1145 location->type = BTRFS_INODE_ITEM_KEY;
1146 location->offset = 0;
1150 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1154 if (!di || IS_ERR(di)) {
1157 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1159 btrfs_free_path(path);
1162 location->objectid = 0;
1167 * when we hit a tree root in a directory, the btrfs part of the inode
1168 * needs to be changed to reflect the root directory of the tree root. This
1169 * is kind of like crossing a mount point.
1171 static int fixup_tree_root_location(struct btrfs_root *root,
1172 struct btrfs_key *location,
1173 struct btrfs_root **sub_root,
1174 struct dentry *dentry)
1176 struct btrfs_path *path;
1177 struct btrfs_root_item *ri;
1179 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1181 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1184 path = btrfs_alloc_path();
1186 mutex_lock(&root->fs_info->fs_mutex);
1188 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1189 dentry->d_name.name,
1190 dentry->d_name.len);
1191 if (IS_ERR(*sub_root))
1192 return PTR_ERR(*sub_root);
1194 ri = &(*sub_root)->root_item;
1195 location->objectid = btrfs_root_dirid(ri);
1196 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1197 location->offset = 0;
1199 btrfs_free_path(path);
1200 mutex_unlock(&root->fs_info->fs_mutex);
1204 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1206 struct btrfs_iget_args *args = p;
1207 inode->i_ino = args->ino;
1208 BTRFS_I(inode)->root = args->root;
1209 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1210 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1211 inode->i_mapping, GFP_NOFS);
1215 static int btrfs_find_actor(struct inode *inode, void *opaque)
1217 struct btrfs_iget_args *args = opaque;
1218 return (args->ino == inode->i_ino &&
1219 args->root == BTRFS_I(inode)->root);
1222 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1225 struct btrfs_iget_args args;
1226 args.ino = objectid;
1227 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1232 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1235 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1236 struct btrfs_root *root)
1238 struct inode *inode;
1239 struct btrfs_iget_args args;
1240 args.ino = objectid;
1243 inode = iget5_locked(s, objectid, btrfs_find_actor,
1244 btrfs_init_locked_inode,
1249 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1250 struct nameidata *nd)
1252 struct inode * inode;
1253 struct btrfs_inode *bi = BTRFS_I(dir);
1254 struct btrfs_root *root = bi->root;
1255 struct btrfs_root *sub_root = root;
1256 struct btrfs_key location;
1259 if (dentry->d_name.len > BTRFS_NAME_LEN)
1260 return ERR_PTR(-ENAMETOOLONG);
1262 mutex_lock(&root->fs_info->fs_mutex);
1263 ret = btrfs_inode_by_name(dir, dentry, &location);
1264 mutex_unlock(&root->fs_info->fs_mutex);
1267 return ERR_PTR(ret);
1270 if (location.objectid) {
1271 ret = fixup_tree_root_location(root, &location, &sub_root,
1274 return ERR_PTR(ret);
1276 return ERR_PTR(-ENOENT);
1277 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1280 return ERR_PTR(-EACCES);
1281 if (inode->i_state & I_NEW) {
1282 /* the inode and parent dir are two different roots */
1283 if (sub_root != root) {
1285 sub_root->inode = inode;
1287 BTRFS_I(inode)->root = sub_root;
1288 memcpy(&BTRFS_I(inode)->location, &location,
1290 btrfs_read_locked_inode(inode);
1291 unlock_new_inode(inode);
1294 return d_splice_alias(inode, dentry);
1297 static unsigned char btrfs_filetype_table[] = {
1298 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1301 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1303 struct inode *inode = filp->f_dentry->d_inode;
1304 struct btrfs_root *root = BTRFS_I(inode)->root;
1305 struct btrfs_item *item;
1306 struct btrfs_dir_item *di;
1307 struct btrfs_key key;
1308 struct btrfs_key found_key;
1309 struct btrfs_path *path;
1312 struct extent_buffer *leaf;
1315 unsigned char d_type;
1320 int key_type = BTRFS_DIR_INDEX_KEY;
1325 /* FIXME, use a real flag for deciding about the key type */
1326 if (root->fs_info->tree_root == root)
1327 key_type = BTRFS_DIR_ITEM_KEY;
1329 /* special case for "." */
1330 if (filp->f_pos == 0) {
1331 over = filldir(dirent, ".", 1,
1339 mutex_lock(&root->fs_info->fs_mutex);
1340 key.objectid = inode->i_ino;
1341 path = btrfs_alloc_path();
1344 /* special case for .., just use the back ref */
1345 if (filp->f_pos == 1) {
1346 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1348 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1350 leaf = path->nodes[0];
1351 slot = path->slots[0];
1352 nritems = btrfs_header_nritems(leaf);
1353 if (slot >= nritems) {
1354 btrfs_release_path(root, path);
1355 goto read_dir_items;
1357 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1358 btrfs_release_path(root, path);
1359 if (found_key.objectid != key.objectid ||
1360 found_key.type != BTRFS_INODE_REF_KEY)
1361 goto read_dir_items;
1362 over = filldir(dirent, "..", 2,
1363 2, found_key.offset, DT_DIR);
1370 btrfs_set_key_type(&key, key_type);
1371 key.offset = filp->f_pos;
1373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1378 leaf = path->nodes[0];
1379 nritems = btrfs_header_nritems(leaf);
1380 slot = path->slots[0];
1381 if (advance || slot >= nritems) {
1382 if (slot >= nritems -1) {
1383 ret = btrfs_next_leaf(root, path);
1386 leaf = path->nodes[0];
1387 nritems = btrfs_header_nritems(leaf);
1388 slot = path->slots[0];
1395 item = btrfs_item_nr(leaf, slot);
1396 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1398 if (found_key.objectid != key.objectid)
1400 if (btrfs_key_type(&found_key) != key_type)
1402 if (found_key.offset < filp->f_pos)
1405 filp->f_pos = found_key.offset;
1407 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1409 di_total = btrfs_item_size(leaf, item);
1410 while(di_cur < di_total) {
1411 struct btrfs_key location;
1413 name_len = btrfs_dir_name_len(leaf, di);
1414 if (name_len < 32) {
1415 name_ptr = tmp_name;
1417 name_ptr = kmalloc(name_len, GFP_NOFS);
1420 read_extent_buffer(leaf, name_ptr,
1421 (unsigned long)(di + 1), name_len);
1423 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1424 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1425 over = filldir(dirent, name_ptr, name_len,
1430 if (name_ptr != tmp_name)
1435 di_len = btrfs_dir_name_len(leaf, di) +
1436 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1438 di = (struct btrfs_dir_item *)((char *)di + di_len);
1441 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1445 btrfs_release_path(root, path);
1446 btrfs_free_path(path);
1447 mutex_unlock(&root->fs_info->fs_mutex);
1451 int btrfs_write_inode(struct inode *inode, int wait)
1453 struct btrfs_root *root = BTRFS_I(inode)->root;
1454 struct btrfs_trans_handle *trans;
1458 mutex_lock(&root->fs_info->fs_mutex);
1459 trans = btrfs_start_transaction(root, 1);
1460 btrfs_set_trans_block_group(trans, inode);
1461 ret = btrfs_commit_transaction(trans, root);
1462 mutex_unlock(&root->fs_info->fs_mutex);
1468 * This is somewhat expensive, updating the tree every time the
1469 * inode changes. But, it is most likely to find the inode in cache.
1470 * FIXME, needs more benchmarking...there are no reasons other than performance
1471 * to keep or drop this code.
1473 void btrfs_dirty_inode(struct inode *inode)
1475 struct btrfs_root *root = BTRFS_I(inode)->root;
1476 struct btrfs_trans_handle *trans;
1478 mutex_lock(&root->fs_info->fs_mutex);
1479 trans = btrfs_start_transaction(root, 1);
1480 btrfs_set_trans_block_group(trans, inode);
1481 btrfs_update_inode(trans, root, inode);
1482 btrfs_end_transaction(trans, root);
1483 mutex_unlock(&root->fs_info->fs_mutex);
1486 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1487 struct btrfs_root *root,
1489 struct btrfs_block_group_cache *group,
1492 struct inode *inode;
1493 struct btrfs_inode_item *inode_item;
1494 struct btrfs_key *location;
1495 struct btrfs_path *path;
1499 path = btrfs_alloc_path();
1502 inode = new_inode(root->fs_info->sb);
1504 return ERR_PTR(-ENOMEM);
1506 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1507 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1508 inode->i_mapping, GFP_NOFS);
1509 BTRFS_I(inode)->root = root;
1515 group = btrfs_find_block_group(root, group, 0, 0, owner);
1516 BTRFS_I(inode)->block_group = group;
1517 BTRFS_I(inode)->flags = 0;
1518 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1522 inode->i_uid = current->fsuid;
1523 inode->i_gid = current->fsgid;
1524 inode->i_mode = mode;
1525 inode->i_ino = objectid;
1526 inode->i_blocks = 0;
1527 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1528 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1529 struct btrfs_inode_item);
1530 fill_inode_item(path->nodes[0], inode_item, inode);
1531 btrfs_mark_buffer_dirty(path->nodes[0]);
1532 btrfs_free_path(path);
1534 location = &BTRFS_I(inode)->location;
1535 location->objectid = objectid;
1536 location->offset = 0;
1537 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1539 insert_inode_hash(inode);
1542 btrfs_free_path(path);
1543 return ERR_PTR(ret);
1546 static inline u8 btrfs_inode_type(struct inode *inode)
1548 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1551 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1552 struct dentry *dentry, struct inode *inode)
1555 struct btrfs_key key;
1556 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1557 struct inode *parent_inode;
1559 key.objectid = inode->i_ino;
1560 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1563 ret = btrfs_insert_dir_item(trans, root,
1564 dentry->d_name.name, dentry->d_name.len,
1565 dentry->d_parent->d_inode->i_ino,
1566 &key, btrfs_inode_type(inode));
1568 ret = btrfs_insert_inode_ref(trans, root,
1569 dentry->d_name.name,
1572 dentry->d_parent->d_inode->i_ino);
1573 parent_inode = dentry->d_parent->d_inode;
1574 parent_inode->i_size += dentry->d_name.len * 2;
1575 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1576 ret = btrfs_update_inode(trans, root,
1577 dentry->d_parent->d_inode);
1582 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1583 struct dentry *dentry, struct inode *inode)
1585 int err = btrfs_add_link(trans, dentry, inode);
1587 d_instantiate(dentry, inode);
1595 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1596 int mode, dev_t rdev)
1598 struct btrfs_trans_handle *trans;
1599 struct btrfs_root *root = BTRFS_I(dir)->root;
1600 struct inode *inode = NULL;
1604 unsigned long nr = 0;
1606 if (!new_valid_dev(rdev))
1609 mutex_lock(&root->fs_info->fs_mutex);
1610 err = btrfs_check_free_space(root, 1, 0);
1614 trans = btrfs_start_transaction(root, 1);
1615 btrfs_set_trans_block_group(trans, dir);
1617 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1623 inode = btrfs_new_inode(trans, root, objectid,
1624 BTRFS_I(dir)->block_group, mode);
1625 err = PTR_ERR(inode);
1629 btrfs_set_trans_block_group(trans, inode);
1630 err = btrfs_add_nondir(trans, dentry, inode);
1634 inode->i_op = &btrfs_special_inode_operations;
1635 init_special_inode(inode, inode->i_mode, rdev);
1636 btrfs_update_inode(trans, root, inode);
1638 dir->i_sb->s_dirt = 1;
1639 btrfs_update_inode_block_group(trans, inode);
1640 btrfs_update_inode_block_group(trans, dir);
1642 nr = trans->blocks_used;
1643 btrfs_end_transaction(trans, root);
1645 mutex_unlock(&root->fs_info->fs_mutex);
1648 inode_dec_link_count(inode);
1651 btrfs_btree_balance_dirty(root, nr);
1652 btrfs_throttle(root);
1656 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1657 int mode, struct nameidata *nd)
1659 struct btrfs_trans_handle *trans;
1660 struct btrfs_root *root = BTRFS_I(dir)->root;
1661 struct inode *inode = NULL;
1664 unsigned long nr = 0;
1667 mutex_lock(&root->fs_info->fs_mutex);
1668 err = btrfs_check_free_space(root, 1, 0);
1671 trans = btrfs_start_transaction(root, 1);
1672 btrfs_set_trans_block_group(trans, dir);
1674 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1680 inode = btrfs_new_inode(trans, root, objectid,
1681 BTRFS_I(dir)->block_group, mode);
1682 err = PTR_ERR(inode);
1686 btrfs_set_trans_block_group(trans, inode);
1687 err = btrfs_add_nondir(trans, dentry, inode);
1691 inode->i_mapping->a_ops = &btrfs_aops;
1692 inode->i_fop = &btrfs_file_operations;
1693 inode->i_op = &btrfs_file_inode_operations;
1694 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1695 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1696 inode->i_mapping, GFP_NOFS);
1697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1699 dir->i_sb->s_dirt = 1;
1700 btrfs_update_inode_block_group(trans, inode);
1701 btrfs_update_inode_block_group(trans, dir);
1703 nr = trans->blocks_used;
1704 btrfs_end_transaction(trans, root);
1706 mutex_unlock(&root->fs_info->fs_mutex);
1709 inode_dec_link_count(inode);
1712 btrfs_btree_balance_dirty(root, nr);
1713 btrfs_throttle(root);
1717 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1718 struct dentry *dentry)
1720 struct btrfs_trans_handle *trans;
1721 struct btrfs_root *root = BTRFS_I(dir)->root;
1722 struct inode *inode = old_dentry->d_inode;
1723 unsigned long nr = 0;
1727 if (inode->i_nlink == 0)
1730 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1735 mutex_lock(&root->fs_info->fs_mutex);
1736 err = btrfs_check_free_space(root, 1, 0);
1739 trans = btrfs_start_transaction(root, 1);
1741 btrfs_set_trans_block_group(trans, dir);
1742 atomic_inc(&inode->i_count);
1743 err = btrfs_add_nondir(trans, dentry, inode);
1748 dir->i_sb->s_dirt = 1;
1749 btrfs_update_inode_block_group(trans, dir);
1750 err = btrfs_update_inode(trans, root, inode);
1755 nr = trans->blocks_used;
1756 btrfs_end_transaction(trans, root);
1758 mutex_unlock(&root->fs_info->fs_mutex);
1761 inode_dec_link_count(inode);
1764 btrfs_btree_balance_dirty(root, nr);
1765 btrfs_throttle(root);
1769 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1771 struct inode *inode;
1772 struct btrfs_trans_handle *trans;
1773 struct btrfs_root *root = BTRFS_I(dir)->root;
1775 int drop_on_err = 0;
1777 unsigned long nr = 1;
1779 mutex_lock(&root->fs_info->fs_mutex);
1780 err = btrfs_check_free_space(root, 1, 0);
1784 trans = btrfs_start_transaction(root, 1);
1785 btrfs_set_trans_block_group(trans, dir);
1787 if (IS_ERR(trans)) {
1788 err = PTR_ERR(trans);
1792 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1798 inode = btrfs_new_inode(trans, root, objectid,
1799 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1800 if (IS_ERR(inode)) {
1801 err = PTR_ERR(inode);
1806 inode->i_op = &btrfs_dir_inode_operations;
1807 inode->i_fop = &btrfs_dir_file_operations;
1808 btrfs_set_trans_block_group(trans, inode);
1811 err = btrfs_update_inode(trans, root, inode);
1815 err = btrfs_add_link(trans, dentry, inode);
1819 d_instantiate(dentry, inode);
1821 dir->i_sb->s_dirt = 1;
1822 btrfs_update_inode_block_group(trans, inode);
1823 btrfs_update_inode_block_group(trans, dir);
1826 nr = trans->blocks_used;
1827 btrfs_end_transaction(trans, root);
1830 mutex_unlock(&root->fs_info->fs_mutex);
1833 btrfs_btree_balance_dirty(root, nr);
1834 btrfs_throttle(root);
1838 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1839 size_t pg_offset, u64 start, u64 len,
1845 u64 extent_start = 0;
1847 u64 objectid = inode->i_ino;
1849 struct btrfs_path *path;
1850 struct btrfs_root *root = BTRFS_I(inode)->root;
1851 struct btrfs_file_extent_item *item;
1852 struct extent_buffer *leaf;
1853 struct btrfs_key found_key;
1854 struct extent_map *em = NULL;
1855 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1857 struct btrfs_trans_handle *trans = NULL;
1859 path = btrfs_alloc_path();
1861 mutex_lock(&root->fs_info->fs_mutex);
1864 spin_lock(&em_tree->lock);
1865 em = lookup_extent_mapping(em_tree, start, len);
1866 spin_unlock(&em_tree->lock);
1869 if (em->start > start) {
1870 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1871 start, len, em->start, em->len);
1874 if (em->block_start == EXTENT_MAP_INLINE && page)
1875 free_extent_map(em);
1879 em = alloc_extent_map(GFP_NOFS);
1885 em->start = EXTENT_MAP_HOLE;
1887 em->bdev = inode->i_sb->s_bdev;
1888 ret = btrfs_lookup_file_extent(trans, root, path,
1889 objectid, start, trans != NULL);
1896 if (path->slots[0] == 0)
1901 leaf = path->nodes[0];
1902 item = btrfs_item_ptr(leaf, path->slots[0],
1903 struct btrfs_file_extent_item);
1904 /* are we inside the extent that was found? */
1905 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1906 found_type = btrfs_key_type(&found_key);
1907 if (found_key.objectid != objectid ||
1908 found_type != BTRFS_EXTENT_DATA_KEY) {
1912 found_type = btrfs_file_extent_type(leaf, item);
1913 extent_start = found_key.offset;
1914 if (found_type == BTRFS_FILE_EXTENT_REG) {
1915 extent_end = extent_start +
1916 btrfs_file_extent_num_bytes(leaf, item);
1918 if (start < extent_start || start >= extent_end) {
1920 if (start < extent_start) {
1921 if (start + len <= extent_start)
1923 em->len = extent_end - extent_start;
1929 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1931 em->start = extent_start;
1932 em->len = extent_end - extent_start;
1933 em->block_start = EXTENT_MAP_HOLE;
1936 bytenr += btrfs_file_extent_offset(leaf, item);
1937 em->block_start = bytenr;
1938 em->start = extent_start;
1939 em->len = extent_end - extent_start;
1941 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1946 size_t extent_offset;
1949 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1951 extent_end = (extent_start + size + root->sectorsize - 1) &
1952 ~((u64)root->sectorsize - 1);
1953 if (start < extent_start || start >= extent_end) {
1955 if (start < extent_start) {
1956 if (start + len <= extent_start)
1958 em->len = extent_end - extent_start;
1964 em->block_start = EXTENT_MAP_INLINE;
1967 em->start = extent_start;
1972 page_start = page_offset(page) + pg_offset;
1973 extent_offset = page_start - extent_start;
1974 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
1975 size - extent_offset);
1976 em->start = extent_start + extent_offset;
1977 em->len = (copy_size + root->sectorsize - 1) &
1978 ~((u64)root->sectorsize - 1);
1980 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1981 if (create == 0 && !PageUptodate(page)) {
1982 read_extent_buffer(leaf, map + pg_offset, ptr,
1984 flush_dcache_page(page);
1985 } else if (create && PageUptodate(page)) {
1988 free_extent_map(em);
1990 btrfs_release_path(root, path);
1991 trans = btrfs_start_transaction(root, 1);
1994 write_extent_buffer(leaf, map + pg_offset, ptr,
1996 btrfs_mark_buffer_dirty(leaf);
1999 set_extent_uptodate(io_tree, em->start,
2000 extent_map_end(em) - 1, GFP_NOFS);
2003 printk("unkknown found_type %d\n", found_type);
2010 em->block_start = EXTENT_MAP_HOLE;
2012 btrfs_release_path(root, path);
2013 if (em->start > start || extent_map_end(em) <= start) {
2014 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2020 spin_lock(&em_tree->lock);
2021 ret = add_extent_mapping(em_tree, em);
2022 if (ret == -EEXIST) {
2023 free_extent_map(em);
2024 em = lookup_extent_mapping(em_tree, start, len);
2027 printk("failing to insert %Lu %Lu\n", start, len);
2030 spin_unlock(&em_tree->lock);
2032 btrfs_free_path(path);
2034 ret = btrfs_end_transaction(trans, root);
2038 mutex_unlock(&root->fs_info->fs_mutex);
2040 free_extent_map(em);
2042 return ERR_PTR(err);
2047 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2049 return extent_bmap(mapping, iblock, btrfs_get_extent);
2052 int btrfs_readpage(struct file *file, struct page *page)
2054 struct extent_io_tree *tree;
2055 tree = &BTRFS_I(page->mapping->host)->io_tree;
2056 return extent_read_full_page(tree, page, btrfs_get_extent);
2059 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2061 struct extent_io_tree *tree;
2064 if (current->flags & PF_MEMALLOC) {
2065 redirty_page_for_writepage(wbc, page);
2069 tree = &BTRFS_I(page->mapping->host)->io_tree;
2070 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2073 static int btrfs_writepages(struct address_space *mapping,
2074 struct writeback_control *wbc)
2076 struct extent_io_tree *tree;
2077 tree = &BTRFS_I(mapping->host)->io_tree;
2078 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2082 btrfs_readpages(struct file *file, struct address_space *mapping,
2083 struct list_head *pages, unsigned nr_pages)
2085 struct extent_io_tree *tree;
2086 tree = &BTRFS_I(mapping->host)->io_tree;
2087 return extent_readpages(tree, mapping, pages, nr_pages,
2091 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2093 struct extent_io_tree *tree;
2094 struct extent_map_tree *map;
2097 tree = &BTRFS_I(page->mapping->host)->io_tree;
2098 map = &BTRFS_I(page->mapping->host)->extent_tree;
2099 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2101 ClearPagePrivate(page);
2102 set_page_private(page, 0);
2103 page_cache_release(page);
2108 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2110 struct extent_io_tree *tree;
2112 tree = &BTRFS_I(page->mapping->host)->io_tree;
2113 extent_invalidatepage(tree, page, offset);
2114 btrfs_releasepage(page, GFP_NOFS);
2118 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2119 * called from a page fault handler when a page is first dirtied. Hence we must
2120 * be careful to check for EOF conditions here. We set the page up correctly
2121 * for a written page which means we get ENOSPC checking when writing into
2122 * holes and correct delalloc and unwritten extent mapping on filesystems that
2123 * support these features.
2125 * We are not allowed to take the i_mutex here so we have to play games to
2126 * protect against truncate races as the page could now be beyond EOF. Because
2127 * vmtruncate() writes the inode size before removing pages, once we have the
2128 * page lock we can determine safely if the page is beyond EOF. If it is not
2129 * beyond EOF, then the page is guaranteed safe against truncation until we
2132 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2134 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2135 struct btrfs_root *root = BTRFS_I(inode)->root;
2141 mutex_lock(&root->fs_info->fs_mutex);
2142 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2143 mutex_unlock(&root->fs_info->fs_mutex);
2150 wait_on_page_writeback(page);
2151 size = i_size_read(inode);
2152 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2154 if ((page->mapping != inode->i_mapping) ||
2155 (page_start > size)) {
2156 /* page got truncated out from underneath us */
2160 /* page is wholly or partially inside EOF */
2161 if (page_start + PAGE_CACHE_SIZE > size)
2162 end = size & ~PAGE_CACHE_MASK;
2164 end = PAGE_CACHE_SIZE;
2166 ret = btrfs_cow_one_page(inode, page, end);
2174 static void btrfs_truncate(struct inode *inode)
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
2178 struct btrfs_trans_handle *trans;
2181 if (!S_ISREG(inode->i_mode))
2183 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2186 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2188 mutex_lock(&root->fs_info->fs_mutex);
2189 trans = btrfs_start_transaction(root, 1);
2190 btrfs_set_trans_block_group(trans, inode);
2192 /* FIXME, add redo link to tree so we don't leak on crash */
2193 ret = btrfs_truncate_in_trans(trans, root, inode);
2194 btrfs_update_inode(trans, root, inode);
2195 nr = trans->blocks_used;
2197 ret = btrfs_end_transaction(trans, root);
2199 mutex_unlock(&root->fs_info->fs_mutex);
2200 btrfs_btree_balance_dirty(root, nr);
2201 btrfs_throttle(root);
2204 static int noinline create_subvol(struct btrfs_root *root, char *name,
2207 struct btrfs_trans_handle *trans;
2208 struct btrfs_key key;
2209 struct btrfs_root_item root_item;
2210 struct btrfs_inode_item *inode_item;
2211 struct extent_buffer *leaf;
2212 struct btrfs_root *new_root = root;
2213 struct inode *inode;
2218 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2219 unsigned long nr = 1;
2221 mutex_lock(&root->fs_info->fs_mutex);
2222 ret = btrfs_check_free_space(root, 1, 0);
2226 trans = btrfs_start_transaction(root, 1);
2229 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2234 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2235 objectid, trans->transid, 0, 0,
2238 return PTR_ERR(leaf);
2240 btrfs_set_header_nritems(leaf, 0);
2241 btrfs_set_header_level(leaf, 0);
2242 btrfs_set_header_bytenr(leaf, leaf->start);
2243 btrfs_set_header_generation(leaf, trans->transid);
2244 btrfs_set_header_owner(leaf, objectid);
2246 write_extent_buffer(leaf, root->fs_info->fsid,
2247 (unsigned long)btrfs_header_fsid(leaf),
2249 btrfs_mark_buffer_dirty(leaf);
2251 inode_item = &root_item.inode;
2252 memset(inode_item, 0, sizeof(*inode_item));
2253 inode_item->generation = cpu_to_le64(1);
2254 inode_item->size = cpu_to_le64(3);
2255 inode_item->nlink = cpu_to_le32(1);
2256 inode_item->nblocks = cpu_to_le64(1);
2257 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2259 btrfs_set_root_bytenr(&root_item, leaf->start);
2260 btrfs_set_root_level(&root_item, 0);
2261 btrfs_set_root_refs(&root_item, 1);
2262 btrfs_set_root_used(&root_item, 0);
2264 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2265 root_item.drop_level = 0;
2267 free_extent_buffer(leaf);
2270 btrfs_set_root_dirid(&root_item, new_dirid);
2272 key.objectid = objectid;
2274 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2275 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2281 * insert the directory item
2283 key.offset = (u64)-1;
2284 dir = root->fs_info->sb->s_root->d_inode;
2285 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2286 name, namelen, dir->i_ino, &key,
2291 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2292 name, namelen, objectid,
2293 root->fs_info->sb->s_root->d_inode->i_ino);
2297 ret = btrfs_commit_transaction(trans, root);
2301 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2304 trans = btrfs_start_transaction(new_root, 1);
2307 inode = btrfs_new_inode(trans, new_root, new_dirid,
2308 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2311 inode->i_op = &btrfs_dir_inode_operations;
2312 inode->i_fop = &btrfs_dir_file_operations;
2313 new_root->inode = inode;
2315 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2319 ret = btrfs_update_inode(trans, new_root, inode);
2323 nr = trans->blocks_used;
2324 err = btrfs_commit_transaction(trans, new_root);
2328 mutex_unlock(&root->fs_info->fs_mutex);
2329 btrfs_btree_balance_dirty(root, nr);
2330 btrfs_throttle(root);
2334 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2336 struct btrfs_pending_snapshot *pending_snapshot;
2337 struct btrfs_trans_handle *trans;
2340 unsigned long nr = 0;
2342 if (!root->ref_cows)
2345 mutex_lock(&root->fs_info->fs_mutex);
2346 ret = btrfs_check_free_space(root, 1, 0);
2350 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2351 if (!pending_snapshot) {
2355 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2356 if (!pending_snapshot->name) {
2358 kfree(pending_snapshot);
2361 memcpy(pending_snapshot->name, name, namelen);
2362 pending_snapshot->name[namelen] = '\0';
2363 trans = btrfs_start_transaction(root, 1);
2365 pending_snapshot->root = root;
2366 list_add(&pending_snapshot->list,
2367 &trans->transaction->pending_snapshots);
2368 ret = btrfs_update_inode(trans, root, root->inode);
2369 err = btrfs_commit_transaction(trans, root);
2372 mutex_unlock(&root->fs_info->fs_mutex);
2373 btrfs_btree_balance_dirty(root, nr);
2374 btrfs_throttle(root);
2378 unsigned long btrfs_force_ra(struct address_space *mapping,
2379 struct file_ra_state *ra, struct file *file,
2380 pgoff_t offset, pgoff_t last_index)
2384 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2385 req_size = last_index - offset + 1;
2386 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2389 req_size = min(last_index - offset + 1, (pgoff_t)128);
2390 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2391 return offset + req_size;
2395 int btrfs_defrag_file(struct file *file) {
2396 struct inode *inode = fdentry(file)->d_inode;
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2398 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2400 unsigned long last_index;
2401 unsigned long ra_index = 0;
2405 u64 existing_delalloc;
2409 mutex_lock(&root->fs_info->fs_mutex);
2410 ret = btrfs_check_free_space(root, inode->i_size, 0);
2411 mutex_unlock(&root->fs_info->fs_mutex);
2415 mutex_lock(&inode->i_mutex);
2416 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2417 for (i = 0; i <= last_index; i++) {
2418 if (i == ra_index) {
2419 ra_index = btrfs_force_ra(inode->i_mapping,
2421 file, ra_index, last_index);
2423 page = grab_cache_page(inode->i_mapping, i);
2426 if (!PageUptodate(page)) {
2427 btrfs_readpage(NULL, page);
2429 if (!PageUptodate(page)) {
2431 page_cache_release(page);
2435 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2436 page_end = page_start + PAGE_CACHE_SIZE - 1;
2438 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2439 delalloc_start = page_start;
2441 count_range_bits(&BTRFS_I(inode)->io_tree,
2442 &delalloc_start, page_end,
2443 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2444 set_extent_delalloc(io_tree, page_start,
2445 page_end, GFP_NOFS);
2447 spin_lock(&root->fs_info->delalloc_lock);
2448 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2450 spin_unlock(&root->fs_info->delalloc_lock);
2452 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2453 set_page_dirty(page);
2455 page_cache_release(page);
2456 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2460 mutex_unlock(&inode->i_mutex);
2464 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2468 struct btrfs_ioctl_vol_args *vol_args;
2469 struct btrfs_trans_handle *trans;
2475 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2480 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2484 namelen = strlen(vol_args->name);
2485 if (namelen > BTRFS_VOL_NAME_MAX) {
2490 sizestr = vol_args->name;
2491 if (!strcmp(sizestr, "max"))
2492 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2494 if (sizestr[0] == '-') {
2497 } else if (sizestr[0] == '+') {
2501 new_size = btrfs_parse_size(sizestr);
2502 if (new_size == 0) {
2508 mutex_lock(&root->fs_info->fs_mutex);
2509 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2512 if (new_size > old_size) {
2516 new_size = old_size - new_size;
2517 } else if (mod > 0) {
2518 new_size = old_size + new_size;
2521 if (new_size < 256 * 1024 * 1024) {
2525 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2530 do_div(new_size, root->sectorsize);
2531 new_size *= root->sectorsize;
2533 printk("new size is %Lu\n", new_size);
2534 if (new_size > old_size) {
2535 trans = btrfs_start_transaction(root, 1);
2536 ret = btrfs_grow_extent_tree(trans, root, new_size);
2537 btrfs_commit_transaction(trans, root);
2539 ret = btrfs_shrink_extent_tree(root, new_size);
2543 mutex_unlock(&root->fs_info->fs_mutex);
2549 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2552 struct btrfs_ioctl_vol_args *vol_args;
2553 struct btrfs_dir_item *di;
2554 struct btrfs_path *path;
2559 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2564 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2569 namelen = strlen(vol_args->name);
2570 if (namelen > BTRFS_VOL_NAME_MAX) {
2574 if (strchr(vol_args->name, '/')) {
2579 path = btrfs_alloc_path();
2585 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2586 mutex_lock(&root->fs_info->fs_mutex);
2587 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2589 vol_args->name, namelen, 0);
2590 mutex_unlock(&root->fs_info->fs_mutex);
2591 btrfs_free_path(path);
2593 if (di && !IS_ERR(di)) {
2603 if (root == root->fs_info->tree_root)
2604 ret = create_subvol(root, vol_args->name, namelen);
2606 ret = create_snapshot(root, vol_args->name, namelen);
2612 static int btrfs_ioctl_defrag(struct file *file)
2614 struct inode *inode = fdentry(file)->d_inode;
2615 struct btrfs_root *root = BTRFS_I(inode)->root;
2617 switch (inode->i_mode & S_IFMT) {
2619 mutex_lock(&root->fs_info->fs_mutex);
2620 btrfs_defrag_root(root, 0);
2621 btrfs_defrag_root(root->fs_info->extent_root, 0);
2622 mutex_unlock(&root->fs_info->fs_mutex);
2625 btrfs_defrag_file(file);
2632 long btrfs_ioctl(struct file *file, unsigned int
2633 cmd, unsigned long arg)
2635 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2638 case BTRFS_IOC_SNAP_CREATE:
2639 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2640 case BTRFS_IOC_DEFRAG:
2641 return btrfs_ioctl_defrag(file);
2642 case BTRFS_IOC_RESIZE:
2643 return btrfs_ioctl_resize(root, (void __user *)arg);
2650 * Called inside transaction, so use GFP_NOFS
2652 struct inode *btrfs_alloc_inode(struct super_block *sb)
2654 struct btrfs_inode *ei;
2656 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2660 ei->ordered_trans = 0;
2661 return &ei->vfs_inode;
2664 void btrfs_destroy_inode(struct inode *inode)
2666 WARN_ON(!list_empty(&inode->i_dentry));
2667 WARN_ON(inode->i_data.nrpages);
2669 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2670 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2673 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2674 static void init_once(struct kmem_cache * cachep, void *foo)
2676 static void init_once(void * foo, struct kmem_cache * cachep,
2677 unsigned long flags)
2680 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2682 inode_init_once(&ei->vfs_inode);
2685 void btrfs_destroy_cachep(void)
2687 if (btrfs_inode_cachep)
2688 kmem_cache_destroy(btrfs_inode_cachep);
2689 if (btrfs_trans_handle_cachep)
2690 kmem_cache_destroy(btrfs_trans_handle_cachep);
2691 if (btrfs_transaction_cachep)
2692 kmem_cache_destroy(btrfs_transaction_cachep);
2693 if (btrfs_bit_radix_cachep)
2694 kmem_cache_destroy(btrfs_bit_radix_cachep);
2695 if (btrfs_path_cachep)
2696 kmem_cache_destroy(btrfs_path_cachep);
2699 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2700 unsigned long extra_flags,
2701 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2702 void (*ctor)(struct kmem_cache *, void *)
2704 void (*ctor)(void *, struct kmem_cache *,
2709 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2710 SLAB_MEM_SPREAD | extra_flags), ctor
2711 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2717 int btrfs_init_cachep(void)
2719 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2720 sizeof(struct btrfs_inode),
2722 if (!btrfs_inode_cachep)
2724 btrfs_trans_handle_cachep =
2725 btrfs_cache_create("btrfs_trans_handle_cache",
2726 sizeof(struct btrfs_trans_handle),
2728 if (!btrfs_trans_handle_cachep)
2730 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2731 sizeof(struct btrfs_transaction),
2733 if (!btrfs_transaction_cachep)
2735 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2736 sizeof(struct btrfs_path),
2738 if (!btrfs_path_cachep)
2740 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2741 SLAB_DESTROY_BY_RCU, NULL);
2742 if (!btrfs_bit_radix_cachep)
2746 btrfs_destroy_cachep();
2750 static int btrfs_getattr(struct vfsmount *mnt,
2751 struct dentry *dentry, struct kstat *stat)
2753 struct inode *inode = dentry->d_inode;
2754 generic_fillattr(inode, stat);
2755 stat->blksize = PAGE_CACHE_SIZE;
2759 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2760 struct inode * new_dir,struct dentry *new_dentry)
2762 struct btrfs_trans_handle *trans;
2763 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2764 struct inode *new_inode = new_dentry->d_inode;
2765 struct inode *old_inode = old_dentry->d_inode;
2766 struct timespec ctime = CURRENT_TIME;
2767 struct btrfs_path *path;
2770 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2771 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2775 mutex_lock(&root->fs_info->fs_mutex);
2776 ret = btrfs_check_free_space(root, 1, 0);
2780 trans = btrfs_start_transaction(root, 1);
2782 btrfs_set_trans_block_group(trans, new_dir);
2783 path = btrfs_alloc_path();
2789 old_dentry->d_inode->i_nlink++;
2790 old_dir->i_ctime = old_dir->i_mtime = ctime;
2791 new_dir->i_ctime = new_dir->i_mtime = ctime;
2792 old_inode->i_ctime = ctime;
2794 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2799 new_inode->i_ctime = CURRENT_TIME;
2800 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2804 ret = btrfs_add_link(trans, new_dentry, old_inode);
2809 btrfs_free_path(path);
2810 btrfs_end_transaction(trans, root);
2812 mutex_unlock(&root->fs_info->fs_mutex);
2816 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2817 const char *symname)
2819 struct btrfs_trans_handle *trans;
2820 struct btrfs_root *root = BTRFS_I(dir)->root;
2821 struct btrfs_path *path;
2822 struct btrfs_key key;
2823 struct inode *inode = NULL;
2830 struct btrfs_file_extent_item *ei;
2831 struct extent_buffer *leaf;
2832 unsigned long nr = 0;
2834 name_len = strlen(symname) + 1;
2835 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2836 return -ENAMETOOLONG;
2838 mutex_lock(&root->fs_info->fs_mutex);
2839 err = btrfs_check_free_space(root, 1, 0);
2843 trans = btrfs_start_transaction(root, 1);
2844 btrfs_set_trans_block_group(trans, dir);
2846 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2852 inode = btrfs_new_inode(trans, root, objectid,
2853 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2854 err = PTR_ERR(inode);
2858 btrfs_set_trans_block_group(trans, inode);
2859 err = btrfs_add_nondir(trans, dentry, inode);
2863 inode->i_mapping->a_ops = &btrfs_aops;
2864 inode->i_fop = &btrfs_file_operations;
2865 inode->i_op = &btrfs_file_inode_operations;
2866 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2867 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2868 inode->i_mapping, GFP_NOFS);
2869 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2871 dir->i_sb->s_dirt = 1;
2872 btrfs_update_inode_block_group(trans, inode);
2873 btrfs_update_inode_block_group(trans, dir);
2877 path = btrfs_alloc_path();
2879 key.objectid = inode->i_ino;
2881 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2882 datasize = btrfs_file_extent_calc_inline_size(name_len);
2883 err = btrfs_insert_empty_item(trans, root, path, &key,
2889 leaf = path->nodes[0];
2890 ei = btrfs_item_ptr(leaf, path->slots[0],
2891 struct btrfs_file_extent_item);
2892 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2893 btrfs_set_file_extent_type(leaf, ei,
2894 BTRFS_FILE_EXTENT_INLINE);
2895 ptr = btrfs_file_extent_inline_start(ei);
2896 write_extent_buffer(leaf, symname, ptr, name_len);
2897 btrfs_mark_buffer_dirty(leaf);
2898 btrfs_free_path(path);
2900 inode->i_op = &btrfs_symlink_inode_operations;
2901 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2902 inode->i_size = name_len - 1;
2903 err = btrfs_update_inode(trans, root, inode);
2908 nr = trans->blocks_used;
2909 btrfs_end_transaction(trans, root);
2911 mutex_unlock(&root->fs_info->fs_mutex);
2913 inode_dec_link_count(inode);
2916 btrfs_btree_balance_dirty(root, nr);
2917 btrfs_throttle(root);
2920 static int btrfs_permission(struct inode *inode, int mask,
2921 struct nameidata *nd)
2923 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2925 return generic_permission(inode, mask, NULL);
2928 static struct inode_operations btrfs_dir_inode_operations = {
2929 .lookup = btrfs_lookup,
2930 .create = btrfs_create,
2931 .unlink = btrfs_unlink,
2933 .mkdir = btrfs_mkdir,
2934 .rmdir = btrfs_rmdir,
2935 .rename = btrfs_rename,
2936 .symlink = btrfs_symlink,
2937 .setattr = btrfs_setattr,
2938 .mknod = btrfs_mknod,
2939 .setxattr = generic_setxattr,
2940 .getxattr = generic_getxattr,
2941 .listxattr = btrfs_listxattr,
2942 .removexattr = generic_removexattr,
2943 .permission = btrfs_permission,
2945 static struct inode_operations btrfs_dir_ro_inode_operations = {
2946 .lookup = btrfs_lookup,
2947 .permission = btrfs_permission,
2949 static struct file_operations btrfs_dir_file_operations = {
2950 .llseek = generic_file_llseek,
2951 .read = generic_read_dir,
2952 .readdir = btrfs_readdir,
2953 .unlocked_ioctl = btrfs_ioctl,
2954 #ifdef CONFIG_COMPAT
2955 .compat_ioctl = btrfs_ioctl,
2959 static struct extent_io_ops btrfs_extent_io_ops = {
2960 .fill_delalloc = run_delalloc_range,
2961 .writepage_io_hook = btrfs_writepage_io_hook,
2962 .readpage_io_hook = btrfs_readpage_io_hook,
2963 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2966 static struct address_space_operations btrfs_aops = {
2967 .readpage = btrfs_readpage,
2968 .writepage = btrfs_writepage,
2969 .writepages = btrfs_writepages,
2970 .readpages = btrfs_readpages,
2971 .sync_page = block_sync_page,
2973 .invalidatepage = btrfs_invalidatepage,
2974 .releasepage = btrfs_releasepage,
2975 .set_page_dirty = __set_page_dirty_nobuffers,
2978 static struct address_space_operations btrfs_symlink_aops = {
2979 .readpage = btrfs_readpage,
2980 .writepage = btrfs_writepage,
2981 .invalidatepage = btrfs_invalidatepage,
2982 .releasepage = btrfs_releasepage,
2985 static struct inode_operations btrfs_file_inode_operations = {
2986 .truncate = btrfs_truncate,
2987 .getattr = btrfs_getattr,
2988 .setattr = btrfs_setattr,
2989 .setxattr = generic_setxattr,
2990 .getxattr = generic_getxattr,
2991 .listxattr = btrfs_listxattr,
2992 .removexattr = generic_removexattr,
2993 .permission = btrfs_permission,
2995 static struct inode_operations btrfs_special_inode_operations = {
2996 .getattr = btrfs_getattr,
2997 .setattr = btrfs_setattr,
2998 .permission = btrfs_permission,
3000 static struct inode_operations btrfs_symlink_inode_operations = {
3001 .readlink = generic_readlink,
3002 .follow_link = page_follow_link_light,
3003 .put_link = page_put_link,
3004 .permission = btrfs_permission,