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)
336 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
337 struct inode *inode = page->mapping->host;
338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
340 u64 private = ~(u32)0;
342 struct btrfs_root *root = BTRFS_I(inode)->root;
346 if (btrfs_test_opt(root, NODATASUM) ||
347 btrfs_test_flag(inode, NODATASUM))
350 ret = get_state_private(io_tree, start, &private);
351 local_irq_save(flags);
352 kaddr = kmap_atomic(page, KM_IRQ0);
356 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
357 btrfs_csum_final(csum, (char *)&csum);
358 if (csum != private) {
361 kunmap_atomic(kaddr, KM_IRQ0);
362 local_irq_restore(flags);
366 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
367 page->mapping->host->i_ino, (unsigned long long)start, csum,
369 memset(kaddr + offset, 1, end - start + 1);
370 flush_dcache_page(page);
371 kunmap_atomic(kaddr, KM_IRQ0);
372 local_irq_restore(flags);
376 void btrfs_read_locked_inode(struct inode *inode)
378 struct btrfs_path *path;
379 struct extent_buffer *leaf;
380 struct btrfs_inode_item *inode_item;
381 struct btrfs_inode_timespec *tspec;
382 struct btrfs_root *root = BTRFS_I(inode)->root;
383 struct btrfs_key location;
384 u64 alloc_group_block;
388 path = btrfs_alloc_path();
390 mutex_lock(&root->fs_info->fs_mutex);
391 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
393 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
397 leaf = path->nodes[0];
398 inode_item = btrfs_item_ptr(leaf, path->slots[0],
399 struct btrfs_inode_item);
401 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
402 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
403 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
404 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
405 inode->i_size = btrfs_inode_size(leaf, inode_item);
407 tspec = btrfs_inode_atime(inode_item);
408 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
409 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
411 tspec = btrfs_inode_mtime(inode_item);
412 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
413 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
415 tspec = btrfs_inode_ctime(inode_item);
416 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
417 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
419 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
420 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
422 rdev = btrfs_inode_rdev(leaf, inode_item);
424 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
425 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
427 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
428 if (!BTRFS_I(inode)->block_group) {
429 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
432 btrfs_free_path(path);
435 mutex_unlock(&root->fs_info->fs_mutex);
437 switch (inode->i_mode & S_IFMT) {
439 inode->i_mapping->a_ops = &btrfs_aops;
440 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
441 inode->i_fop = &btrfs_file_operations;
442 inode->i_op = &btrfs_file_inode_operations;
445 inode->i_fop = &btrfs_dir_file_operations;
446 if (root == root->fs_info->tree_root)
447 inode->i_op = &btrfs_dir_ro_inode_operations;
449 inode->i_op = &btrfs_dir_inode_operations;
452 inode->i_op = &btrfs_symlink_inode_operations;
453 inode->i_mapping->a_ops = &btrfs_symlink_aops;
456 init_special_inode(inode, inode->i_mode, rdev);
462 btrfs_release_path(root, path);
463 btrfs_free_path(path);
464 mutex_unlock(&root->fs_info->fs_mutex);
465 make_bad_inode(inode);
468 static void fill_inode_item(struct extent_buffer *leaf,
469 struct btrfs_inode_item *item,
472 btrfs_set_inode_uid(leaf, item, inode->i_uid);
473 btrfs_set_inode_gid(leaf, item, inode->i_gid);
474 btrfs_set_inode_size(leaf, item, inode->i_size);
475 btrfs_set_inode_mode(leaf, item, inode->i_mode);
476 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
478 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
479 inode->i_atime.tv_sec);
480 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
481 inode->i_atime.tv_nsec);
483 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
484 inode->i_mtime.tv_sec);
485 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
486 inode->i_mtime.tv_nsec);
488 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
489 inode->i_ctime.tv_sec);
490 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
491 inode->i_ctime.tv_nsec);
493 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
494 btrfs_set_inode_generation(leaf, item, inode->i_generation);
495 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
496 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
497 btrfs_set_inode_block_group(leaf, item,
498 BTRFS_I(inode)->block_group->key.objectid);
501 int btrfs_update_inode(struct btrfs_trans_handle *trans,
502 struct btrfs_root *root,
505 struct btrfs_inode_item *inode_item;
506 struct btrfs_path *path;
507 struct extent_buffer *leaf;
510 path = btrfs_alloc_path();
512 ret = btrfs_lookup_inode(trans, root, path,
513 &BTRFS_I(inode)->location, 1);
520 leaf = path->nodes[0];
521 inode_item = btrfs_item_ptr(leaf, path->slots[0],
522 struct btrfs_inode_item);
524 fill_inode_item(leaf, inode_item, inode);
525 btrfs_mark_buffer_dirty(leaf);
526 btrfs_set_inode_last_trans(trans, inode);
529 btrfs_release_path(root, path);
530 btrfs_free_path(path);
535 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
536 struct btrfs_root *root,
538 struct dentry *dentry)
540 struct btrfs_path *path;
541 const char *name = dentry->d_name.name;
542 int name_len = dentry->d_name.len;
544 struct extent_buffer *leaf;
545 struct btrfs_dir_item *di;
546 struct btrfs_key key;
548 path = btrfs_alloc_path();
554 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
564 leaf = path->nodes[0];
565 btrfs_dir_item_key_to_cpu(leaf, di, &key);
566 ret = btrfs_delete_one_dir_name(trans, root, path, di);
569 btrfs_release_path(root, path);
571 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
572 key.objectid, name, name_len, -1);
581 ret = btrfs_delete_one_dir_name(trans, root, path, di);
583 dentry->d_inode->i_ctime = dir->i_ctime;
584 ret = btrfs_del_inode_ref(trans, root, name, name_len,
585 dentry->d_inode->i_ino,
586 dentry->d_parent->d_inode->i_ino);
588 printk("failed to delete reference to %.*s, "
589 "inode %lu parent %lu\n", name_len, name,
590 dentry->d_inode->i_ino,
591 dentry->d_parent->d_inode->i_ino);
594 btrfs_free_path(path);
596 dir->i_size -= name_len * 2;
597 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
598 btrfs_update_inode(trans, root, dir);
599 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
600 dentry->d_inode->i_nlink--;
602 drop_nlink(dentry->d_inode);
604 ret = btrfs_update_inode(trans, root, dentry->d_inode);
605 dir->i_sb->s_dirt = 1;
610 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
612 struct btrfs_root *root;
613 struct btrfs_trans_handle *trans;
614 struct inode *inode = dentry->d_inode;
616 unsigned long nr = 0;
618 root = BTRFS_I(dir)->root;
619 mutex_lock(&root->fs_info->fs_mutex);
621 ret = btrfs_check_free_space(root, 1, 1);
625 trans = btrfs_start_transaction(root, 1);
627 btrfs_set_trans_block_group(trans, dir);
628 ret = btrfs_unlink_trans(trans, root, dir, dentry);
629 nr = trans->blocks_used;
631 if (inode->i_nlink == 0) {
633 /* if the inode isn't linked anywhere,
634 * we don't need to worry about
637 found = btrfs_del_ordered_inode(inode);
639 atomic_dec(&inode->i_count);
643 btrfs_end_transaction(trans, root);
645 mutex_unlock(&root->fs_info->fs_mutex);
646 btrfs_btree_balance_dirty(root, nr);
647 btrfs_throttle(root);
651 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
653 struct inode *inode = dentry->d_inode;
656 struct btrfs_root *root = BTRFS_I(dir)->root;
657 struct btrfs_trans_handle *trans;
658 unsigned long nr = 0;
660 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
663 mutex_lock(&root->fs_info->fs_mutex);
664 ret = btrfs_check_free_space(root, 1, 1);
668 trans = btrfs_start_transaction(root, 1);
669 btrfs_set_trans_block_group(trans, dir);
671 /* now the directory is empty */
672 err = btrfs_unlink_trans(trans, root, dir, dentry);
677 nr = trans->blocks_used;
678 ret = btrfs_end_transaction(trans, root);
680 mutex_unlock(&root->fs_info->fs_mutex);
681 btrfs_btree_balance_dirty(root, nr);
682 btrfs_throttle(root);
689 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
690 struct btrfs_root *root,
693 struct btrfs_path *path;
698 path = btrfs_alloc_path();
700 ret = btrfs_lookup_inode(trans, root, path,
701 &BTRFS_I(inode)->location, -1);
705 ret = btrfs_del_item(trans, root, path);
706 btrfs_free_path(path);
711 * this can truncate away extent items, csum items and directory items.
712 * It starts at a high offset and removes keys until it can't find
713 * any higher than i_size.
715 * csum items that cross the new i_size are truncated to the new size
718 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
723 struct btrfs_path *path;
724 struct btrfs_key key;
725 struct btrfs_key found_key;
727 struct extent_buffer *leaf;
728 struct btrfs_file_extent_item *fi;
729 u64 extent_start = 0;
730 u64 extent_num_bytes = 0;
736 int extent_type = -1;
738 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
739 path = btrfs_alloc_path();
743 /* FIXME, add redo link to tree so we don't leak on crash */
744 key.objectid = inode->i_ino;
745 key.offset = (u64)-1;
749 btrfs_init_path(path);
751 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
756 BUG_ON(path->slots[0] == 0);
759 leaf = path->nodes[0];
760 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
761 found_type = btrfs_key_type(&found_key);
763 if (found_key.objectid != inode->i_ino)
766 if (found_type != BTRFS_CSUM_ITEM_KEY &&
767 found_type != BTRFS_DIR_ITEM_KEY &&
768 found_type != BTRFS_DIR_INDEX_KEY &&
769 found_type != BTRFS_EXTENT_DATA_KEY)
772 item_end = found_key.offset;
773 if (found_type == BTRFS_EXTENT_DATA_KEY) {
774 fi = btrfs_item_ptr(leaf, path->slots[0],
775 struct btrfs_file_extent_item);
776 extent_type = btrfs_file_extent_type(leaf, fi);
777 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
779 btrfs_file_extent_num_bytes(leaf, fi);
780 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
781 struct btrfs_item *item = btrfs_item_nr(leaf,
783 item_end += btrfs_file_extent_inline_len(leaf,
788 if (found_type == BTRFS_CSUM_ITEM_KEY) {
789 ret = btrfs_csum_truncate(trans, root, path,
793 if (item_end < inode->i_size) {
794 if (found_type == BTRFS_DIR_ITEM_KEY) {
795 found_type = BTRFS_INODE_ITEM_KEY;
796 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
797 found_type = BTRFS_CSUM_ITEM_KEY;
798 } else if (found_type) {
803 btrfs_set_key_type(&key, found_type);
804 btrfs_release_path(root, path);
807 if (found_key.offset >= inode->i_size)
813 /* FIXME, shrink the extent if the ref count is only 1 */
814 if (found_type != BTRFS_EXTENT_DATA_KEY)
817 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
819 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
822 btrfs_file_extent_num_bytes(leaf, fi);
823 extent_num_bytes = inode->i_size -
824 found_key.offset + root->sectorsize - 1;
825 btrfs_set_file_extent_num_bytes(leaf, fi,
827 num_dec = (orig_num_bytes -
828 extent_num_bytes) >> 9;
829 if (extent_start != 0) {
830 inode->i_blocks -= num_dec;
832 btrfs_mark_buffer_dirty(leaf);
835 btrfs_file_extent_disk_num_bytes(leaf,
837 /* FIXME blocksize != 4096 */
838 num_dec = btrfs_file_extent_num_bytes(leaf,
840 if (extent_start != 0) {
842 inode->i_blocks -= num_dec;
844 root_gen = btrfs_header_generation(leaf);
845 root_owner = btrfs_header_owner(leaf);
847 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
849 u32 newsize = inode->i_size - found_key.offset;
850 newsize = btrfs_file_extent_calc_inline_size(newsize);
851 ret = btrfs_truncate_item(trans, root, path,
857 ret = btrfs_del_item(trans, root, path);
863 btrfs_release_path(root, path);
865 ret = btrfs_free_extent(trans, root, extent_start,
868 root_gen, inode->i_ino,
869 found_key.offset, 0);
875 btrfs_release_path(root, path);
876 btrfs_free_path(path);
877 inode->i_sb->s_dirt = 1;
881 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
886 struct btrfs_root *root = BTRFS_I(inode)->root;
887 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
888 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
889 u64 existing_delalloc;
893 WARN_ON(!PageLocked(page));
894 set_page_extent_mapped(page);
896 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
897 delalloc_start = page_start;
898 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
899 &delalloc_start, page_end,
900 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
901 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
904 spin_lock(&root->fs_info->delalloc_lock);
905 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
906 spin_unlock(&root->fs_info->delalloc_lock);
908 if (zero_start != PAGE_CACHE_SIZE) {
910 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
911 flush_dcache_page(page);
914 set_page_dirty(page);
915 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
921 * taken from block_truncate_page, but does cow as it zeros out
922 * any bytes left in the last page in the file.
924 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
926 struct inode *inode = mapping->host;
927 struct btrfs_root *root = BTRFS_I(inode)->root;
928 u32 blocksize = root->sectorsize;
929 pgoff_t index = from >> PAGE_CACHE_SHIFT;
930 unsigned offset = from & (PAGE_CACHE_SIZE-1);
935 if ((offset & (blocksize - 1)) == 0)
939 page = grab_cache_page(mapping, index);
942 if (!PageUptodate(page)) {
943 ret = btrfs_readpage(NULL, page);
945 if (!PageUptodate(page)) {
950 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
952 ret = btrfs_cow_one_page(inode, page, offset);
955 page_cache_release(page);
960 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
962 struct inode *inode = dentry->d_inode;
965 err = inode_change_ok(inode, attr);
969 if (S_ISREG(inode->i_mode) &&
970 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
971 struct btrfs_trans_handle *trans;
972 struct btrfs_root *root = BTRFS_I(inode)->root;
973 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
975 u64 mask = root->sectorsize - 1;
976 u64 pos = (inode->i_size + mask) & ~mask;
977 u64 block_end = attr->ia_size | mask;
982 if (attr->ia_size <= pos)
985 if (pos != inode->i_size)
986 hole_start = pos + root->sectorsize;
990 mutex_lock(&root->fs_info->fs_mutex);
991 err = btrfs_check_free_space(root, 1, 0);
992 mutex_unlock(&root->fs_info->fs_mutex);
996 btrfs_truncate_page(inode->i_mapping, inode->i_size);
998 lock_extent(io_tree, pos, block_end, GFP_NOFS);
999 hole_size = block_end - hole_start;
1001 mutex_lock(&root->fs_info->fs_mutex);
1002 trans = btrfs_start_transaction(root, 1);
1003 btrfs_set_trans_block_group(trans, inode);
1004 err = btrfs_drop_extents(trans, root, inode,
1005 pos, block_end, pos,
1008 if (alloc_hint != EXTENT_MAP_INLINE) {
1009 err = btrfs_insert_file_extent(trans, root,
1013 btrfs_drop_extent_cache(inode, hole_start,
1015 btrfs_check_file(root, inode);
1017 btrfs_end_transaction(trans, root);
1018 mutex_unlock(&root->fs_info->fs_mutex);
1019 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
1024 err = inode_setattr(inode, attr);
1029 void btrfs_put_inode(struct inode *inode)
1033 if (!BTRFS_I(inode)->ordered_trans) {
1037 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1038 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1041 ret = btrfs_del_ordered_inode(inode);
1043 atomic_dec(&inode->i_count);
1047 void btrfs_delete_inode(struct inode *inode)
1049 struct btrfs_trans_handle *trans;
1050 struct btrfs_root *root = BTRFS_I(inode)->root;
1054 truncate_inode_pages(&inode->i_data, 0);
1055 if (is_bad_inode(inode)) {
1060 mutex_lock(&root->fs_info->fs_mutex);
1061 trans = btrfs_start_transaction(root, 1);
1063 btrfs_set_trans_block_group(trans, inode);
1064 ret = btrfs_truncate_in_trans(trans, root, inode);
1066 goto no_delete_lock;
1067 ret = btrfs_delete_xattrs(trans, root, inode);
1069 goto no_delete_lock;
1070 ret = btrfs_free_inode(trans, root, inode);
1072 goto no_delete_lock;
1073 nr = trans->blocks_used;
1075 btrfs_end_transaction(trans, root);
1076 mutex_unlock(&root->fs_info->fs_mutex);
1077 btrfs_btree_balance_dirty(root, nr);
1078 btrfs_throttle(root);
1082 nr = trans->blocks_used;
1083 btrfs_end_transaction(trans, root);
1084 mutex_unlock(&root->fs_info->fs_mutex);
1085 btrfs_btree_balance_dirty(root, nr);
1086 btrfs_throttle(root);
1092 * this returns the key found in the dir entry in the location pointer.
1093 * If no dir entries were found, location->objectid is 0.
1095 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1096 struct btrfs_key *location)
1098 const char *name = dentry->d_name.name;
1099 int namelen = dentry->d_name.len;
1100 struct btrfs_dir_item *di;
1101 struct btrfs_path *path;
1102 struct btrfs_root *root = BTRFS_I(dir)->root;
1105 if (namelen == 1 && strcmp(name, ".") == 0) {
1106 location->objectid = dir->i_ino;
1107 location->type = BTRFS_INODE_ITEM_KEY;
1108 location->offset = 0;
1111 path = btrfs_alloc_path();
1114 if (namelen == 2 && strcmp(name, "..") == 0) {
1115 struct btrfs_key key;
1116 struct extent_buffer *leaf;
1120 key.objectid = dir->i_ino;
1121 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1123 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1127 leaf = path->nodes[0];
1128 slot = path->slots[0];
1129 nritems = btrfs_header_nritems(leaf);
1130 if (slot >= nritems)
1133 btrfs_item_key_to_cpu(leaf, &key, slot);
1134 if (key.objectid != dir->i_ino ||
1135 key.type != BTRFS_INODE_REF_KEY) {
1138 location->objectid = key.offset;
1139 location->type = BTRFS_INODE_ITEM_KEY;
1140 location->offset = 0;
1144 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1148 if (!di || IS_ERR(di)) {
1151 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1153 btrfs_free_path(path);
1156 location->objectid = 0;
1161 * when we hit a tree root in a directory, the btrfs part of the inode
1162 * needs to be changed to reflect the root directory of the tree root. This
1163 * is kind of like crossing a mount point.
1165 static int fixup_tree_root_location(struct btrfs_root *root,
1166 struct btrfs_key *location,
1167 struct btrfs_root **sub_root,
1168 struct dentry *dentry)
1170 struct btrfs_path *path;
1171 struct btrfs_root_item *ri;
1173 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1175 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1178 path = btrfs_alloc_path();
1180 mutex_lock(&root->fs_info->fs_mutex);
1182 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1183 dentry->d_name.name,
1184 dentry->d_name.len);
1185 if (IS_ERR(*sub_root))
1186 return PTR_ERR(*sub_root);
1188 ri = &(*sub_root)->root_item;
1189 location->objectid = btrfs_root_dirid(ri);
1190 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1191 location->offset = 0;
1193 btrfs_free_path(path);
1194 mutex_unlock(&root->fs_info->fs_mutex);
1198 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1200 struct btrfs_iget_args *args = p;
1201 inode->i_ino = args->ino;
1202 BTRFS_I(inode)->root = args->root;
1203 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1204 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1205 inode->i_mapping, GFP_NOFS);
1209 static int btrfs_find_actor(struct inode *inode, void *opaque)
1211 struct btrfs_iget_args *args = opaque;
1212 return (args->ino == inode->i_ino &&
1213 args->root == BTRFS_I(inode)->root);
1216 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1219 struct btrfs_iget_args args;
1220 args.ino = objectid;
1221 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1226 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1229 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1230 struct btrfs_root *root)
1232 struct inode *inode;
1233 struct btrfs_iget_args args;
1234 args.ino = objectid;
1237 inode = iget5_locked(s, objectid, btrfs_find_actor,
1238 btrfs_init_locked_inode,
1243 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1244 struct nameidata *nd)
1246 struct inode * inode;
1247 struct btrfs_inode *bi = BTRFS_I(dir);
1248 struct btrfs_root *root = bi->root;
1249 struct btrfs_root *sub_root = root;
1250 struct btrfs_key location;
1253 if (dentry->d_name.len > BTRFS_NAME_LEN)
1254 return ERR_PTR(-ENAMETOOLONG);
1256 mutex_lock(&root->fs_info->fs_mutex);
1257 ret = btrfs_inode_by_name(dir, dentry, &location);
1258 mutex_unlock(&root->fs_info->fs_mutex);
1261 return ERR_PTR(ret);
1264 if (location.objectid) {
1265 ret = fixup_tree_root_location(root, &location, &sub_root,
1268 return ERR_PTR(ret);
1270 return ERR_PTR(-ENOENT);
1271 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1274 return ERR_PTR(-EACCES);
1275 if (inode->i_state & I_NEW) {
1276 /* the inode and parent dir are two different roots */
1277 if (sub_root != root) {
1279 sub_root->inode = inode;
1281 BTRFS_I(inode)->root = sub_root;
1282 memcpy(&BTRFS_I(inode)->location, &location,
1284 btrfs_read_locked_inode(inode);
1285 unlock_new_inode(inode);
1288 return d_splice_alias(inode, dentry);
1291 static unsigned char btrfs_filetype_table[] = {
1292 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1295 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1297 struct inode *inode = filp->f_dentry->d_inode;
1298 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 struct btrfs_item *item;
1300 struct btrfs_dir_item *di;
1301 struct btrfs_key key;
1302 struct btrfs_key found_key;
1303 struct btrfs_path *path;
1306 struct extent_buffer *leaf;
1309 unsigned char d_type;
1314 int key_type = BTRFS_DIR_INDEX_KEY;
1319 /* FIXME, use a real flag for deciding about the key type */
1320 if (root->fs_info->tree_root == root)
1321 key_type = BTRFS_DIR_ITEM_KEY;
1323 /* special case for "." */
1324 if (filp->f_pos == 0) {
1325 over = filldir(dirent, ".", 1,
1333 mutex_lock(&root->fs_info->fs_mutex);
1334 key.objectid = inode->i_ino;
1335 path = btrfs_alloc_path();
1338 /* special case for .., just use the back ref */
1339 if (filp->f_pos == 1) {
1340 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1344 leaf = path->nodes[0];
1345 slot = path->slots[0];
1346 nritems = btrfs_header_nritems(leaf);
1347 if (slot >= nritems) {
1348 btrfs_release_path(root, path);
1349 goto read_dir_items;
1351 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1352 btrfs_release_path(root, path);
1353 if (found_key.objectid != key.objectid ||
1354 found_key.type != BTRFS_INODE_REF_KEY)
1355 goto read_dir_items;
1356 over = filldir(dirent, "..", 2,
1357 2, found_key.offset, DT_DIR);
1364 btrfs_set_key_type(&key, key_type);
1365 key.offset = filp->f_pos;
1367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1372 leaf = path->nodes[0];
1373 nritems = btrfs_header_nritems(leaf);
1374 slot = path->slots[0];
1375 if (advance || slot >= nritems) {
1376 if (slot >= nritems -1) {
1377 ret = btrfs_next_leaf(root, path);
1380 leaf = path->nodes[0];
1381 nritems = btrfs_header_nritems(leaf);
1382 slot = path->slots[0];
1389 item = btrfs_item_nr(leaf, slot);
1390 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1392 if (found_key.objectid != key.objectid)
1394 if (btrfs_key_type(&found_key) != key_type)
1396 if (found_key.offset < filp->f_pos)
1399 filp->f_pos = found_key.offset;
1401 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1403 di_total = btrfs_item_size(leaf, item);
1404 while(di_cur < di_total) {
1405 struct btrfs_key location;
1407 name_len = btrfs_dir_name_len(leaf, di);
1408 if (name_len < 32) {
1409 name_ptr = tmp_name;
1411 name_ptr = kmalloc(name_len, GFP_NOFS);
1414 read_extent_buffer(leaf, name_ptr,
1415 (unsigned long)(di + 1), name_len);
1417 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1418 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1419 over = filldir(dirent, name_ptr, name_len,
1424 if (name_ptr != tmp_name)
1429 di_len = btrfs_dir_name_len(leaf, di) +
1430 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1432 di = (struct btrfs_dir_item *)((char *)di + di_len);
1435 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1439 btrfs_release_path(root, path);
1440 btrfs_free_path(path);
1441 mutex_unlock(&root->fs_info->fs_mutex);
1445 int btrfs_write_inode(struct inode *inode, int wait)
1447 struct btrfs_root *root = BTRFS_I(inode)->root;
1448 struct btrfs_trans_handle *trans;
1452 mutex_lock(&root->fs_info->fs_mutex);
1453 trans = btrfs_start_transaction(root, 1);
1454 btrfs_set_trans_block_group(trans, inode);
1455 ret = btrfs_commit_transaction(trans, root);
1456 mutex_unlock(&root->fs_info->fs_mutex);
1462 * This is somewhat expensive, updating the tree every time the
1463 * inode changes. But, it is most likely to find the inode in cache.
1464 * FIXME, needs more benchmarking...there are no reasons other than performance
1465 * to keep or drop this code.
1467 void btrfs_dirty_inode(struct inode *inode)
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct btrfs_trans_handle *trans;
1472 mutex_lock(&root->fs_info->fs_mutex);
1473 trans = btrfs_start_transaction(root, 1);
1474 btrfs_set_trans_block_group(trans, inode);
1475 btrfs_update_inode(trans, root, inode);
1476 btrfs_end_transaction(trans, root);
1477 mutex_unlock(&root->fs_info->fs_mutex);
1480 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1481 struct btrfs_root *root,
1483 struct btrfs_block_group_cache *group,
1486 struct inode *inode;
1487 struct btrfs_inode_item *inode_item;
1488 struct btrfs_key *location;
1489 struct btrfs_path *path;
1493 path = btrfs_alloc_path();
1496 inode = new_inode(root->fs_info->sb);
1498 return ERR_PTR(-ENOMEM);
1500 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1501 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1502 inode->i_mapping, GFP_NOFS);
1503 BTRFS_I(inode)->root = root;
1509 group = btrfs_find_block_group(root, group, 0, 0, owner);
1510 BTRFS_I(inode)->block_group = group;
1511 BTRFS_I(inode)->flags = 0;
1512 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1516 inode->i_uid = current->fsuid;
1517 inode->i_gid = current->fsgid;
1518 inode->i_mode = mode;
1519 inode->i_ino = objectid;
1520 inode->i_blocks = 0;
1521 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1522 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1523 struct btrfs_inode_item);
1524 fill_inode_item(path->nodes[0], inode_item, inode);
1525 btrfs_mark_buffer_dirty(path->nodes[0]);
1526 btrfs_free_path(path);
1528 location = &BTRFS_I(inode)->location;
1529 location->objectid = objectid;
1530 location->offset = 0;
1531 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1533 insert_inode_hash(inode);
1536 btrfs_free_path(path);
1537 return ERR_PTR(ret);
1540 static inline u8 btrfs_inode_type(struct inode *inode)
1542 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1545 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1546 struct dentry *dentry, struct inode *inode)
1549 struct btrfs_key key;
1550 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1551 struct inode *parent_inode;
1553 key.objectid = inode->i_ino;
1554 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1557 ret = btrfs_insert_dir_item(trans, root,
1558 dentry->d_name.name, dentry->d_name.len,
1559 dentry->d_parent->d_inode->i_ino,
1560 &key, btrfs_inode_type(inode));
1562 ret = btrfs_insert_inode_ref(trans, root,
1563 dentry->d_name.name,
1566 dentry->d_parent->d_inode->i_ino);
1567 parent_inode = dentry->d_parent->d_inode;
1568 parent_inode->i_size += dentry->d_name.len * 2;
1569 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1570 ret = btrfs_update_inode(trans, root,
1571 dentry->d_parent->d_inode);
1576 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1577 struct dentry *dentry, struct inode *inode)
1579 int err = btrfs_add_link(trans, dentry, inode);
1581 d_instantiate(dentry, inode);
1589 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1590 int mode, dev_t rdev)
1592 struct btrfs_trans_handle *trans;
1593 struct btrfs_root *root = BTRFS_I(dir)->root;
1594 struct inode *inode = NULL;
1598 unsigned long nr = 0;
1600 if (!new_valid_dev(rdev))
1603 mutex_lock(&root->fs_info->fs_mutex);
1604 err = btrfs_check_free_space(root, 1, 0);
1608 trans = btrfs_start_transaction(root, 1);
1609 btrfs_set_trans_block_group(trans, dir);
1611 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1617 inode = btrfs_new_inode(trans, root, objectid,
1618 BTRFS_I(dir)->block_group, mode);
1619 err = PTR_ERR(inode);
1623 btrfs_set_trans_block_group(trans, inode);
1624 err = btrfs_add_nondir(trans, dentry, inode);
1628 inode->i_op = &btrfs_special_inode_operations;
1629 init_special_inode(inode, inode->i_mode, rdev);
1630 btrfs_update_inode(trans, root, inode);
1632 dir->i_sb->s_dirt = 1;
1633 btrfs_update_inode_block_group(trans, inode);
1634 btrfs_update_inode_block_group(trans, dir);
1636 nr = trans->blocks_used;
1637 btrfs_end_transaction(trans, root);
1639 mutex_unlock(&root->fs_info->fs_mutex);
1642 inode_dec_link_count(inode);
1645 btrfs_btree_balance_dirty(root, nr);
1646 btrfs_throttle(root);
1650 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1651 int mode, struct nameidata *nd)
1653 struct btrfs_trans_handle *trans;
1654 struct btrfs_root *root = BTRFS_I(dir)->root;
1655 struct inode *inode = NULL;
1658 unsigned long nr = 0;
1661 mutex_lock(&root->fs_info->fs_mutex);
1662 err = btrfs_check_free_space(root, 1, 0);
1665 trans = btrfs_start_transaction(root, 1);
1666 btrfs_set_trans_block_group(trans, dir);
1668 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1674 inode = btrfs_new_inode(trans, root, objectid,
1675 BTRFS_I(dir)->block_group, mode);
1676 err = PTR_ERR(inode);
1680 btrfs_set_trans_block_group(trans, inode);
1681 err = btrfs_add_nondir(trans, dentry, inode);
1685 inode->i_mapping->a_ops = &btrfs_aops;
1686 inode->i_fop = &btrfs_file_operations;
1687 inode->i_op = &btrfs_file_inode_operations;
1688 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1689 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1690 inode->i_mapping, GFP_NOFS);
1691 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1693 dir->i_sb->s_dirt = 1;
1694 btrfs_update_inode_block_group(trans, inode);
1695 btrfs_update_inode_block_group(trans, dir);
1697 nr = trans->blocks_used;
1698 btrfs_end_transaction(trans, root);
1700 mutex_unlock(&root->fs_info->fs_mutex);
1703 inode_dec_link_count(inode);
1706 btrfs_btree_balance_dirty(root, nr);
1707 btrfs_throttle(root);
1711 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1712 struct dentry *dentry)
1714 struct btrfs_trans_handle *trans;
1715 struct btrfs_root *root = BTRFS_I(dir)->root;
1716 struct inode *inode = old_dentry->d_inode;
1717 unsigned long nr = 0;
1721 if (inode->i_nlink == 0)
1724 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1729 mutex_lock(&root->fs_info->fs_mutex);
1730 err = btrfs_check_free_space(root, 1, 0);
1733 trans = btrfs_start_transaction(root, 1);
1735 btrfs_set_trans_block_group(trans, dir);
1736 atomic_inc(&inode->i_count);
1737 err = btrfs_add_nondir(trans, dentry, inode);
1742 dir->i_sb->s_dirt = 1;
1743 btrfs_update_inode_block_group(trans, dir);
1744 err = btrfs_update_inode(trans, root, inode);
1749 nr = trans->blocks_used;
1750 btrfs_end_transaction(trans, root);
1752 mutex_unlock(&root->fs_info->fs_mutex);
1755 inode_dec_link_count(inode);
1758 btrfs_btree_balance_dirty(root, nr);
1759 btrfs_throttle(root);
1763 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1765 struct inode *inode;
1766 struct btrfs_trans_handle *trans;
1767 struct btrfs_root *root = BTRFS_I(dir)->root;
1769 int drop_on_err = 0;
1771 unsigned long nr = 1;
1773 mutex_lock(&root->fs_info->fs_mutex);
1774 err = btrfs_check_free_space(root, 1, 0);
1778 trans = btrfs_start_transaction(root, 1);
1779 btrfs_set_trans_block_group(trans, dir);
1781 if (IS_ERR(trans)) {
1782 err = PTR_ERR(trans);
1786 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1792 inode = btrfs_new_inode(trans, root, objectid,
1793 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1794 if (IS_ERR(inode)) {
1795 err = PTR_ERR(inode);
1800 inode->i_op = &btrfs_dir_inode_operations;
1801 inode->i_fop = &btrfs_dir_file_operations;
1802 btrfs_set_trans_block_group(trans, inode);
1805 err = btrfs_update_inode(trans, root, inode);
1809 err = btrfs_add_link(trans, dentry, inode);
1813 d_instantiate(dentry, inode);
1815 dir->i_sb->s_dirt = 1;
1816 btrfs_update_inode_block_group(trans, inode);
1817 btrfs_update_inode_block_group(trans, dir);
1820 nr = trans->blocks_used;
1821 btrfs_end_transaction(trans, root);
1824 mutex_unlock(&root->fs_info->fs_mutex);
1827 btrfs_btree_balance_dirty(root, nr);
1828 btrfs_throttle(root);
1832 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1833 size_t page_offset, u64 start, u64 len,
1839 u64 extent_start = 0;
1841 u64 objectid = inode->i_ino;
1843 struct btrfs_path *path;
1844 struct btrfs_root *root = BTRFS_I(inode)->root;
1845 struct btrfs_file_extent_item *item;
1846 struct extent_buffer *leaf;
1847 struct btrfs_key found_key;
1848 struct extent_map *em = NULL;
1849 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1850 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1851 struct btrfs_trans_handle *trans = NULL;
1853 path = btrfs_alloc_path();
1855 mutex_lock(&root->fs_info->fs_mutex);
1858 spin_lock(&em_tree->lock);
1859 em = lookup_extent_mapping(em_tree, start, len);
1860 spin_unlock(&em_tree->lock);
1863 if (em->start > start) {
1864 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1865 start, len, em->start, em->len);
1870 em = alloc_extent_map(GFP_NOFS);
1876 em->start = EXTENT_MAP_HOLE;
1878 em->bdev = inode->i_sb->s_bdev;
1879 ret = btrfs_lookup_file_extent(trans, root, path,
1880 objectid, start, trans != NULL);
1887 if (path->slots[0] == 0)
1892 leaf = path->nodes[0];
1893 item = btrfs_item_ptr(leaf, path->slots[0],
1894 struct btrfs_file_extent_item);
1895 /* are we inside the extent that was found? */
1896 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1897 found_type = btrfs_key_type(&found_key);
1898 if (found_key.objectid != objectid ||
1899 found_type != BTRFS_EXTENT_DATA_KEY) {
1903 found_type = btrfs_file_extent_type(leaf, item);
1904 extent_start = found_key.offset;
1905 if (found_type == BTRFS_FILE_EXTENT_REG) {
1906 extent_end = extent_start +
1907 btrfs_file_extent_num_bytes(leaf, item);
1909 if (start < extent_start || start >= extent_end) {
1911 if (start < extent_start) {
1912 if (start + len <= extent_start)
1914 em->len = extent_end - extent_start;
1920 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1922 em->start = extent_start;
1923 em->len = extent_end - extent_start;
1924 em->block_start = EXTENT_MAP_HOLE;
1927 bytenr += btrfs_file_extent_offset(leaf, item);
1928 em->block_start = bytenr;
1929 em->start = extent_start;
1930 em->len = extent_end - extent_start;
1932 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1936 size_t extent_offset;
1939 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1941 extent_end = (extent_start + size + root->sectorsize - 1) &
1942 ~((u64)root->sectorsize - 1);
1943 if (start < extent_start || start >= extent_end) {
1945 if (start < extent_start) {
1946 if (start + len <= extent_start)
1948 em->len = extent_end - extent_start;
1954 em->block_start = EXTENT_MAP_INLINE;
1957 em->start = extent_start;
1962 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1963 extent_start + page_offset;
1964 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1965 size - extent_offset);
1966 em->start = extent_start + extent_offset;
1967 em->len = copy_size;
1969 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1970 if (create == 0 && !PageUptodate(page)) {
1971 read_extent_buffer(leaf, map + page_offset, ptr,
1973 flush_dcache_page(page);
1974 } else if (create && PageUptodate(page)) {
1977 free_extent_map(em);
1979 btrfs_release_path(root, path);
1980 trans = btrfs_start_transaction(root, 1);
1983 write_extent_buffer(leaf, map + page_offset, ptr,
1985 btrfs_mark_buffer_dirty(leaf);
1988 set_extent_uptodate(io_tree, em->start,
1989 extent_map_end(em) - 1, GFP_NOFS);
1992 printk("unkknown found_type %d\n", found_type);
1999 em->block_start = EXTENT_MAP_HOLE;
2001 btrfs_release_path(root, path);
2002 if (em->start > start || extent_map_end(em) <= start) {
2003 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2009 spin_lock(&em_tree->lock);
2010 ret = add_extent_mapping(em_tree, em);
2011 if (ret == -EEXIST) {
2012 free_extent_map(em);
2013 em = lookup_extent_mapping(em_tree, start, len);
2016 printk("failing to insert %Lu %Lu\n", start, len);
2019 spin_unlock(&em_tree->lock);
2021 btrfs_free_path(path);
2023 ret = btrfs_end_transaction(trans, root);
2027 mutex_unlock(&root->fs_info->fs_mutex);
2029 free_extent_map(em);
2031 return ERR_PTR(err);
2036 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2038 return extent_bmap(mapping, iblock, btrfs_get_extent);
2041 int btrfs_readpage(struct file *file, struct page *page)
2043 struct extent_io_tree *tree;
2044 tree = &BTRFS_I(page->mapping->host)->io_tree;
2045 return extent_read_full_page(tree, page, btrfs_get_extent);
2048 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2050 struct extent_io_tree *tree;
2053 if (current->flags & PF_MEMALLOC) {
2054 redirty_page_for_writepage(wbc, page);
2058 tree = &BTRFS_I(page->mapping->host)->io_tree;
2059 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2062 static int btrfs_writepages(struct address_space *mapping,
2063 struct writeback_control *wbc)
2065 struct extent_io_tree *tree;
2066 tree = &BTRFS_I(mapping->host)->io_tree;
2067 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2071 btrfs_readpages(struct file *file, struct address_space *mapping,
2072 struct list_head *pages, unsigned nr_pages)
2074 struct extent_io_tree *tree;
2075 tree = &BTRFS_I(mapping->host)->io_tree;
2076 return extent_readpages(tree, mapping, pages, nr_pages,
2080 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2082 struct extent_io_tree *tree;
2083 struct extent_map_tree *map;
2086 tree = &BTRFS_I(page->mapping->host)->io_tree;
2087 map = &BTRFS_I(page->mapping->host)->extent_tree;
2088 ret = try_release_extent_mapping(map, tree, page);
2090 ClearPagePrivate(page);
2091 set_page_private(page, 0);
2092 page_cache_release(page);
2097 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2099 struct extent_io_tree *tree;
2101 tree = &BTRFS_I(page->mapping->host)->io_tree;
2102 extent_invalidatepage(tree, page, offset);
2103 btrfs_releasepage(page, GFP_NOFS);
2107 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2108 * called from a page fault handler when a page is first dirtied. Hence we must
2109 * be careful to check for EOF conditions here. We set the page up correctly
2110 * for a written page which means we get ENOSPC checking when writing into
2111 * holes and correct delalloc and unwritten extent mapping on filesystems that
2112 * support these features.
2114 * We are not allowed to take the i_mutex here so we have to play games to
2115 * protect against truncate races as the page could now be beyond EOF. Because
2116 * vmtruncate() writes the inode size before removing pages, once we have the
2117 * page lock we can determine safely if the page is beyond EOF. If it is not
2118 * beyond EOF, then the page is guaranteed safe against truncation until we
2121 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2123 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2124 struct btrfs_root *root = BTRFS_I(inode)->root;
2130 mutex_lock(&root->fs_info->fs_mutex);
2131 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2132 mutex_unlock(&root->fs_info->fs_mutex);
2139 wait_on_page_writeback(page);
2140 size = i_size_read(inode);
2141 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2143 if ((page->mapping != inode->i_mapping) ||
2144 (page_start > size)) {
2145 /* page got truncated out from underneath us */
2149 /* page is wholly or partially inside EOF */
2150 if (page_start + PAGE_CACHE_SIZE > size)
2151 end = size & ~PAGE_CACHE_MASK;
2153 end = PAGE_CACHE_SIZE;
2155 ret = btrfs_cow_one_page(inode, page, end);
2163 static void btrfs_truncate(struct inode *inode)
2165 struct btrfs_root *root = BTRFS_I(inode)->root;
2167 struct btrfs_trans_handle *trans;
2170 if (!S_ISREG(inode->i_mode))
2172 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2175 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2177 mutex_lock(&root->fs_info->fs_mutex);
2178 trans = btrfs_start_transaction(root, 1);
2179 btrfs_set_trans_block_group(trans, inode);
2181 /* FIXME, add redo link to tree so we don't leak on crash */
2182 ret = btrfs_truncate_in_trans(trans, root, inode);
2183 btrfs_update_inode(trans, root, inode);
2184 nr = trans->blocks_used;
2186 ret = btrfs_end_transaction(trans, root);
2188 mutex_unlock(&root->fs_info->fs_mutex);
2189 btrfs_btree_balance_dirty(root, nr);
2190 btrfs_throttle(root);
2193 static int noinline create_subvol(struct btrfs_root *root, char *name,
2196 struct btrfs_trans_handle *trans;
2197 struct btrfs_key key;
2198 struct btrfs_root_item root_item;
2199 struct btrfs_inode_item *inode_item;
2200 struct extent_buffer *leaf;
2201 struct btrfs_root *new_root = root;
2202 struct inode *inode;
2207 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2208 unsigned long nr = 1;
2210 mutex_lock(&root->fs_info->fs_mutex);
2211 ret = btrfs_check_free_space(root, 1, 0);
2215 trans = btrfs_start_transaction(root, 1);
2218 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2223 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2224 objectid, trans->transid, 0, 0,
2227 return PTR_ERR(leaf);
2229 btrfs_set_header_nritems(leaf, 0);
2230 btrfs_set_header_level(leaf, 0);
2231 btrfs_set_header_bytenr(leaf, leaf->start);
2232 btrfs_set_header_generation(leaf, trans->transid);
2233 btrfs_set_header_owner(leaf, objectid);
2235 write_extent_buffer(leaf, root->fs_info->fsid,
2236 (unsigned long)btrfs_header_fsid(leaf),
2238 btrfs_mark_buffer_dirty(leaf);
2240 inode_item = &root_item.inode;
2241 memset(inode_item, 0, sizeof(*inode_item));
2242 inode_item->generation = cpu_to_le64(1);
2243 inode_item->size = cpu_to_le64(3);
2244 inode_item->nlink = cpu_to_le32(1);
2245 inode_item->nblocks = cpu_to_le64(1);
2246 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2248 btrfs_set_root_bytenr(&root_item, leaf->start);
2249 btrfs_set_root_level(&root_item, 0);
2250 btrfs_set_root_refs(&root_item, 1);
2251 btrfs_set_root_used(&root_item, 0);
2253 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2254 root_item.drop_level = 0;
2256 free_extent_buffer(leaf);
2259 btrfs_set_root_dirid(&root_item, new_dirid);
2261 key.objectid = objectid;
2263 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2264 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2270 * insert the directory item
2272 key.offset = (u64)-1;
2273 dir = root->fs_info->sb->s_root->d_inode;
2274 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2275 name, namelen, dir->i_ino, &key,
2280 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2281 name, namelen, objectid,
2282 root->fs_info->sb->s_root->d_inode->i_ino);
2286 ret = btrfs_commit_transaction(trans, root);
2290 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2293 trans = btrfs_start_transaction(new_root, 1);
2296 inode = btrfs_new_inode(trans, new_root, new_dirid,
2297 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2300 inode->i_op = &btrfs_dir_inode_operations;
2301 inode->i_fop = &btrfs_dir_file_operations;
2302 new_root->inode = inode;
2304 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2308 ret = btrfs_update_inode(trans, new_root, inode);
2312 nr = trans->blocks_used;
2313 err = btrfs_commit_transaction(trans, new_root);
2317 mutex_unlock(&root->fs_info->fs_mutex);
2318 btrfs_btree_balance_dirty(root, nr);
2319 btrfs_throttle(root);
2323 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2325 struct btrfs_pending_snapshot *pending_snapshot;
2326 struct btrfs_trans_handle *trans;
2329 unsigned long nr = 0;
2331 if (!root->ref_cows)
2334 mutex_lock(&root->fs_info->fs_mutex);
2335 ret = btrfs_check_free_space(root, 1, 0);
2339 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2340 if (!pending_snapshot) {
2344 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2345 if (!pending_snapshot->name) {
2347 kfree(pending_snapshot);
2350 memcpy(pending_snapshot->name, name, namelen);
2351 pending_snapshot->name[namelen] = '\0';
2352 trans = btrfs_start_transaction(root, 1);
2354 pending_snapshot->root = root;
2355 list_add(&pending_snapshot->list,
2356 &trans->transaction->pending_snapshots);
2357 ret = btrfs_update_inode(trans, root, root->inode);
2358 err = btrfs_commit_transaction(trans, root);
2361 mutex_unlock(&root->fs_info->fs_mutex);
2362 btrfs_btree_balance_dirty(root, nr);
2363 btrfs_throttle(root);
2367 unsigned long btrfs_force_ra(struct address_space *mapping,
2368 struct file_ra_state *ra, struct file *file,
2369 pgoff_t offset, pgoff_t last_index)
2373 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2374 req_size = last_index - offset + 1;
2375 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2378 req_size = min(last_index - offset + 1, (pgoff_t)128);
2379 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2380 return offset + req_size;
2384 int btrfs_defrag_file(struct file *file) {
2385 struct inode *inode = fdentry(file)->d_inode;
2386 struct btrfs_root *root = BTRFS_I(inode)->root;
2387 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2389 unsigned long last_index;
2390 unsigned long ra_index = 0;
2394 u64 existing_delalloc;
2398 mutex_lock(&root->fs_info->fs_mutex);
2399 ret = btrfs_check_free_space(root, inode->i_size, 0);
2400 mutex_unlock(&root->fs_info->fs_mutex);
2404 mutex_lock(&inode->i_mutex);
2405 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2406 for (i = 0; i <= last_index; i++) {
2407 if (i == ra_index) {
2408 ra_index = btrfs_force_ra(inode->i_mapping,
2410 file, ra_index, last_index);
2412 page = grab_cache_page(inode->i_mapping, i);
2415 if (!PageUptodate(page)) {
2416 btrfs_readpage(NULL, page);
2418 if (!PageUptodate(page)) {
2420 page_cache_release(page);
2424 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2425 page_end = page_start + PAGE_CACHE_SIZE - 1;
2427 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2428 delalloc_start = page_start;
2430 count_range_bits(&BTRFS_I(inode)->io_tree,
2431 &delalloc_start, page_end,
2432 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2433 set_extent_delalloc(io_tree, page_start,
2434 page_end, GFP_NOFS);
2436 spin_lock(&root->fs_info->delalloc_lock);
2437 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2439 spin_unlock(&root->fs_info->delalloc_lock);
2441 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2442 set_page_dirty(page);
2444 page_cache_release(page);
2445 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2449 mutex_unlock(&inode->i_mutex);
2453 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2457 struct btrfs_ioctl_vol_args *vol_args;
2458 struct btrfs_trans_handle *trans;
2464 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2469 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2473 namelen = strlen(vol_args->name);
2474 if (namelen > BTRFS_VOL_NAME_MAX) {
2479 sizestr = vol_args->name;
2480 if (!strcmp(sizestr, "max"))
2481 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2483 if (sizestr[0] == '-') {
2486 } else if (sizestr[0] == '+') {
2490 new_size = btrfs_parse_size(sizestr);
2491 if (new_size == 0) {
2497 mutex_lock(&root->fs_info->fs_mutex);
2498 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2501 if (new_size > old_size) {
2505 new_size = old_size - new_size;
2506 } else if (mod > 0) {
2507 new_size = old_size + new_size;
2510 if (new_size < 256 * 1024 * 1024) {
2514 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2519 do_div(new_size, root->sectorsize);
2520 new_size *= root->sectorsize;
2522 printk("new size is %Lu\n", new_size);
2523 if (new_size > old_size) {
2524 trans = btrfs_start_transaction(root, 1);
2525 ret = btrfs_grow_extent_tree(trans, root, new_size);
2526 btrfs_commit_transaction(trans, root);
2528 ret = btrfs_shrink_extent_tree(root, new_size);
2532 mutex_unlock(&root->fs_info->fs_mutex);
2538 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2541 struct btrfs_ioctl_vol_args *vol_args;
2542 struct btrfs_dir_item *di;
2543 struct btrfs_path *path;
2548 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2553 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2558 namelen = strlen(vol_args->name);
2559 if (namelen > BTRFS_VOL_NAME_MAX) {
2563 if (strchr(vol_args->name, '/')) {
2568 path = btrfs_alloc_path();
2574 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2575 mutex_lock(&root->fs_info->fs_mutex);
2576 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2578 vol_args->name, namelen, 0);
2579 mutex_unlock(&root->fs_info->fs_mutex);
2580 btrfs_free_path(path);
2582 if (di && !IS_ERR(di)) {
2592 if (root == root->fs_info->tree_root)
2593 ret = create_subvol(root, vol_args->name, namelen);
2595 ret = create_snapshot(root, vol_args->name, namelen);
2601 static int btrfs_ioctl_defrag(struct file *file)
2603 struct inode *inode = fdentry(file)->d_inode;
2604 struct btrfs_root *root = BTRFS_I(inode)->root;
2606 switch (inode->i_mode & S_IFMT) {
2608 mutex_lock(&root->fs_info->fs_mutex);
2609 btrfs_defrag_root(root, 0);
2610 btrfs_defrag_root(root->fs_info->extent_root, 0);
2611 mutex_unlock(&root->fs_info->fs_mutex);
2614 btrfs_defrag_file(file);
2621 long btrfs_ioctl(struct file *file, unsigned int
2622 cmd, unsigned long arg)
2624 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2627 case BTRFS_IOC_SNAP_CREATE:
2628 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2629 case BTRFS_IOC_DEFRAG:
2630 return btrfs_ioctl_defrag(file);
2631 case BTRFS_IOC_RESIZE:
2632 return btrfs_ioctl_resize(root, (void __user *)arg);
2639 * Called inside transaction, so use GFP_NOFS
2641 struct inode *btrfs_alloc_inode(struct super_block *sb)
2643 struct btrfs_inode *ei;
2645 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2649 ei->ordered_trans = 0;
2650 return &ei->vfs_inode;
2653 void btrfs_destroy_inode(struct inode *inode)
2655 WARN_ON(!list_empty(&inode->i_dentry));
2656 WARN_ON(inode->i_data.nrpages);
2658 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2659 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2662 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2663 static void init_once(struct kmem_cache * cachep, void *foo)
2665 static void init_once(void * foo, struct kmem_cache * cachep,
2666 unsigned long flags)
2669 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2671 inode_init_once(&ei->vfs_inode);
2674 void btrfs_destroy_cachep(void)
2676 if (btrfs_inode_cachep)
2677 kmem_cache_destroy(btrfs_inode_cachep);
2678 if (btrfs_trans_handle_cachep)
2679 kmem_cache_destroy(btrfs_trans_handle_cachep);
2680 if (btrfs_transaction_cachep)
2681 kmem_cache_destroy(btrfs_transaction_cachep);
2682 if (btrfs_bit_radix_cachep)
2683 kmem_cache_destroy(btrfs_bit_radix_cachep);
2684 if (btrfs_path_cachep)
2685 kmem_cache_destroy(btrfs_path_cachep);
2688 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2689 unsigned long extra_flags,
2690 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2691 void (*ctor)(struct kmem_cache *, void *)
2693 void (*ctor)(void *, struct kmem_cache *,
2698 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2699 SLAB_MEM_SPREAD | extra_flags), ctor
2700 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2706 int btrfs_init_cachep(void)
2708 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2709 sizeof(struct btrfs_inode),
2711 if (!btrfs_inode_cachep)
2713 btrfs_trans_handle_cachep =
2714 btrfs_cache_create("btrfs_trans_handle_cache",
2715 sizeof(struct btrfs_trans_handle),
2717 if (!btrfs_trans_handle_cachep)
2719 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2720 sizeof(struct btrfs_transaction),
2722 if (!btrfs_transaction_cachep)
2724 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2725 sizeof(struct btrfs_path),
2727 if (!btrfs_path_cachep)
2729 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2730 SLAB_DESTROY_BY_RCU, NULL);
2731 if (!btrfs_bit_radix_cachep)
2735 btrfs_destroy_cachep();
2739 static int btrfs_getattr(struct vfsmount *mnt,
2740 struct dentry *dentry, struct kstat *stat)
2742 struct inode *inode = dentry->d_inode;
2743 generic_fillattr(inode, stat);
2744 stat->blksize = PAGE_CACHE_SIZE;
2748 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2749 struct inode * new_dir,struct dentry *new_dentry)
2751 struct btrfs_trans_handle *trans;
2752 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2753 struct inode *new_inode = new_dentry->d_inode;
2754 struct inode *old_inode = old_dentry->d_inode;
2755 struct timespec ctime = CURRENT_TIME;
2756 struct btrfs_path *path;
2759 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2760 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2764 mutex_lock(&root->fs_info->fs_mutex);
2765 ret = btrfs_check_free_space(root, 1, 0);
2769 trans = btrfs_start_transaction(root, 1);
2771 btrfs_set_trans_block_group(trans, new_dir);
2772 path = btrfs_alloc_path();
2778 old_dentry->d_inode->i_nlink++;
2779 old_dir->i_ctime = old_dir->i_mtime = ctime;
2780 new_dir->i_ctime = new_dir->i_mtime = ctime;
2781 old_inode->i_ctime = ctime;
2783 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2788 new_inode->i_ctime = CURRENT_TIME;
2789 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2793 ret = btrfs_add_link(trans, new_dentry, old_inode);
2798 btrfs_free_path(path);
2799 btrfs_end_transaction(trans, root);
2801 mutex_unlock(&root->fs_info->fs_mutex);
2805 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2806 const char *symname)
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2810 struct btrfs_path *path;
2811 struct btrfs_key key;
2812 struct inode *inode = NULL;
2819 struct btrfs_file_extent_item *ei;
2820 struct extent_buffer *leaf;
2821 unsigned long nr = 0;
2823 name_len = strlen(symname) + 1;
2824 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2825 return -ENAMETOOLONG;
2827 mutex_lock(&root->fs_info->fs_mutex);
2828 err = btrfs_check_free_space(root, 1, 0);
2832 trans = btrfs_start_transaction(root, 1);
2833 btrfs_set_trans_block_group(trans, dir);
2835 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2841 inode = btrfs_new_inode(trans, root, objectid,
2842 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2843 err = PTR_ERR(inode);
2847 btrfs_set_trans_block_group(trans, inode);
2848 err = btrfs_add_nondir(trans, dentry, inode);
2852 inode->i_mapping->a_ops = &btrfs_aops;
2853 inode->i_fop = &btrfs_file_operations;
2854 inode->i_op = &btrfs_file_inode_operations;
2855 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2856 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2857 inode->i_mapping, GFP_NOFS);
2858 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2860 dir->i_sb->s_dirt = 1;
2861 btrfs_update_inode_block_group(trans, inode);
2862 btrfs_update_inode_block_group(trans, dir);
2866 path = btrfs_alloc_path();
2868 key.objectid = inode->i_ino;
2870 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2871 datasize = btrfs_file_extent_calc_inline_size(name_len);
2872 err = btrfs_insert_empty_item(trans, root, path, &key,
2878 leaf = path->nodes[0];
2879 ei = btrfs_item_ptr(leaf, path->slots[0],
2880 struct btrfs_file_extent_item);
2881 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2882 btrfs_set_file_extent_type(leaf, ei,
2883 BTRFS_FILE_EXTENT_INLINE);
2884 ptr = btrfs_file_extent_inline_start(ei);
2885 write_extent_buffer(leaf, symname, ptr, name_len);
2886 btrfs_mark_buffer_dirty(leaf);
2887 btrfs_free_path(path);
2889 inode->i_op = &btrfs_symlink_inode_operations;
2890 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2891 inode->i_size = name_len - 1;
2892 err = btrfs_update_inode(trans, root, inode);
2897 nr = trans->blocks_used;
2898 btrfs_end_transaction(trans, root);
2900 mutex_unlock(&root->fs_info->fs_mutex);
2902 inode_dec_link_count(inode);
2905 btrfs_btree_balance_dirty(root, nr);
2906 btrfs_throttle(root);
2909 static int btrfs_permission(struct inode *inode, int mask,
2910 struct nameidata *nd)
2912 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2914 return generic_permission(inode, mask, NULL);
2917 static struct inode_operations btrfs_dir_inode_operations = {
2918 .lookup = btrfs_lookup,
2919 .create = btrfs_create,
2920 .unlink = btrfs_unlink,
2922 .mkdir = btrfs_mkdir,
2923 .rmdir = btrfs_rmdir,
2924 .rename = btrfs_rename,
2925 .symlink = btrfs_symlink,
2926 .setattr = btrfs_setattr,
2927 .mknod = btrfs_mknod,
2928 .setxattr = generic_setxattr,
2929 .getxattr = generic_getxattr,
2930 .listxattr = btrfs_listxattr,
2931 .removexattr = generic_removexattr,
2932 .permission = btrfs_permission,
2934 static struct inode_operations btrfs_dir_ro_inode_operations = {
2935 .lookup = btrfs_lookup,
2936 .permission = btrfs_permission,
2938 static struct file_operations btrfs_dir_file_operations = {
2939 .llseek = generic_file_llseek,
2940 .read = generic_read_dir,
2941 .readdir = btrfs_readdir,
2942 .unlocked_ioctl = btrfs_ioctl,
2943 #ifdef CONFIG_COMPAT
2944 .compat_ioctl = btrfs_ioctl,
2948 static struct extent_io_ops btrfs_extent_io_ops = {
2949 .fill_delalloc = run_delalloc_range,
2950 .writepage_io_hook = btrfs_writepage_io_hook,
2951 .readpage_io_hook = btrfs_readpage_io_hook,
2952 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2955 static struct address_space_operations btrfs_aops = {
2956 .readpage = btrfs_readpage,
2957 .writepage = btrfs_writepage,
2958 .writepages = btrfs_writepages,
2959 .readpages = btrfs_readpages,
2960 .sync_page = block_sync_page,
2962 .invalidatepage = btrfs_invalidatepage,
2963 .releasepage = btrfs_releasepage,
2964 .set_page_dirty = __set_page_dirty_nobuffers,
2967 static struct address_space_operations btrfs_symlink_aops = {
2968 .readpage = btrfs_readpage,
2969 .writepage = btrfs_writepage,
2970 .invalidatepage = btrfs_invalidatepage,
2971 .releasepage = btrfs_releasepage,
2974 static struct inode_operations btrfs_file_inode_operations = {
2975 .truncate = btrfs_truncate,
2976 .getattr = btrfs_getattr,
2977 .setattr = btrfs_setattr,
2978 .setxattr = generic_setxattr,
2979 .getxattr = generic_getxattr,
2980 .listxattr = btrfs_listxattr,
2981 .removexattr = generic_removexattr,
2982 .permission = btrfs_permission,
2984 static struct inode_operations btrfs_special_inode_operations = {
2985 .getattr = btrfs_getattr,
2986 .setattr = btrfs_setattr,
2987 .permission = btrfs_permission,
2989 static struct inode_operations btrfs_symlink_inode_operations = {
2990 .readlink = generic_readlink,
2991 .follow_link = page_follow_link_light,
2992 .put_link = page_put_link,
2993 .permission = btrfs_permission,