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);
90 spin_lock(&root->fs_info->delalloc_lock);
91 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
93 spin_unlock(&root->fs_info->delalloc_lock);
97 static int cow_file_range(struct inode *inode, u64 start, u64 end)
99 struct btrfs_root *root = BTRFS_I(inode)->root;
100 struct btrfs_trans_handle *trans;
104 u64 blocksize = root->sectorsize;
105 u64 orig_start = start;
107 struct btrfs_key ins;
110 trans = btrfs_start_transaction(root, 1);
112 btrfs_set_trans_block_group(trans, inode);
114 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
115 num_bytes = max(blocksize, num_bytes);
116 ret = btrfs_drop_extents(trans, root, inode,
117 start, start + num_bytes, start, &alloc_hint);
118 orig_num_bytes = num_bytes;
120 if (alloc_hint == EXTENT_MAP_INLINE)
123 while(num_bytes > 0) {
124 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
125 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
126 root->root_key.objectid,
128 inode->i_ino, start, 0,
129 alloc_hint, (u64)-1, &ins, 1);
134 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
135 start, ins.objectid, ins.offset,
137 inode->i_blocks += ins.offset >> 9;
138 btrfs_check_file(root, inode);
139 num_bytes -= cur_alloc_size;
140 alloc_hint = ins.objectid + ins.offset;
141 start += cur_alloc_size;
143 btrfs_drop_extent_cache(inode, orig_start,
144 orig_start + orig_num_bytes - 1);
145 btrfs_add_ordered_inode(inode);
146 btrfs_update_inode(trans, root, 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;
253 mutex_lock(&root->fs_info->fs_mutex);
254 if (btrfs_test_opt(root, NODATACOW) ||
255 btrfs_test_flag(inode, NODATACOW))
256 ret = run_delalloc_nocow(inode, start, end);
258 ret = cow_file_range(inode, start, end);
260 mutex_unlock(&root->fs_info->fs_mutex);
264 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
265 unsigned long old, unsigned long bits)
267 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
268 struct btrfs_root *root = BTRFS_I(inode)->root;
269 spin_lock(&root->fs_info->delalloc_lock);
270 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
271 root->fs_info->delalloc_bytes += end - start + 1;
272 spin_unlock(&root->fs_info->delalloc_lock);
277 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
278 unsigned long old, unsigned long bits)
280 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 spin_lock(&root->fs_info->delalloc_lock);
283 if (end - start + 1 > root->fs_info->delalloc_bytes) {
284 printk("warning: delalloc account %Lu %Lu\n",
285 end - start + 1, root->fs_info->delalloc_bytes);
286 root->fs_info->delalloc_bytes = 0;
287 BTRFS_I(inode)->delalloc_bytes = 0;
289 root->fs_info->delalloc_bytes -= end - start + 1;
290 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
292 spin_unlock(&root->fs_info->delalloc_lock);
297 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
299 struct inode *inode = page->mapping->host;
300 struct btrfs_root *root = BTRFS_I(inode)->root;
301 struct btrfs_trans_handle *trans;
304 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
305 size_t offset = start - page_start;
306 if (btrfs_test_opt(root, NODATASUM) ||
307 btrfs_test_flag(inode, NODATASUM))
309 mutex_lock(&root->fs_info->fs_mutex);
310 trans = btrfs_start_transaction(root, 1);
311 btrfs_set_trans_block_group(trans, inode);
313 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
314 start, kaddr + offset, end - start + 1);
316 ret = btrfs_end_transaction(trans, root);
318 mutex_unlock(&root->fs_info->fs_mutex);
322 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
325 struct inode *inode = page->mapping->host;
326 struct btrfs_root *root = BTRFS_I(inode)->root;
327 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
328 struct btrfs_csum_item *item;
329 struct btrfs_path *path = NULL;
331 if (btrfs_test_opt(root, NODATASUM) ||
332 btrfs_test_flag(inode, NODATASUM))
334 mutex_lock(&root->fs_info->fs_mutex);
335 path = btrfs_alloc_path();
336 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
339 /* a csum that isn't present is a preallocated region. */
340 if (ret == -ENOENT || ret == -EFBIG)
343 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
346 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
348 set_state_private(io_tree, start, csum);
351 btrfs_free_path(path);
352 mutex_unlock(&root->fs_info->fs_mutex);
356 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
357 struct extent_state *state)
359 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
360 struct inode *inode = page->mapping->host;
361 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
363 u64 private = ~(u32)0;
365 struct btrfs_root *root = BTRFS_I(inode)->root;
369 if (btrfs_test_opt(root, NODATASUM) ||
370 btrfs_test_flag(inode, NODATASUM))
372 if (state && state->start == start) {
373 private = state->private;
376 ret = get_state_private(io_tree, start, &private);
378 local_irq_save(flags);
379 kaddr = kmap_atomic(page, KM_IRQ0);
383 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
384 btrfs_csum_final(csum, (char *)&csum);
385 if (csum != private) {
388 kunmap_atomic(kaddr, KM_IRQ0);
389 local_irq_restore(flags);
393 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
394 page->mapping->host->i_ino, (unsigned long long)start, csum,
396 memset(kaddr + offset, 1, end - start + 1);
397 flush_dcache_page(page);
398 kunmap_atomic(kaddr, KM_IRQ0);
399 local_irq_restore(flags);
403 void btrfs_read_locked_inode(struct inode *inode)
405 struct btrfs_path *path;
406 struct extent_buffer *leaf;
407 struct btrfs_inode_item *inode_item;
408 struct btrfs_inode_timespec *tspec;
409 struct btrfs_root *root = BTRFS_I(inode)->root;
410 struct btrfs_key location;
411 u64 alloc_group_block;
415 path = btrfs_alloc_path();
417 mutex_lock(&root->fs_info->fs_mutex);
418 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
420 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
424 leaf = path->nodes[0];
425 inode_item = btrfs_item_ptr(leaf, path->slots[0],
426 struct btrfs_inode_item);
428 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
429 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
430 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
431 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
432 inode->i_size = btrfs_inode_size(leaf, inode_item);
434 tspec = btrfs_inode_atime(inode_item);
435 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
436 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
438 tspec = btrfs_inode_mtime(inode_item);
439 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
440 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
442 tspec = btrfs_inode_ctime(inode_item);
443 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
444 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
446 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
447 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
449 rdev = btrfs_inode_rdev(leaf, inode_item);
451 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
452 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
454 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
455 if (!BTRFS_I(inode)->block_group) {
456 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
459 btrfs_free_path(path);
462 mutex_unlock(&root->fs_info->fs_mutex);
464 switch (inode->i_mode & S_IFMT) {
466 inode->i_mapping->a_ops = &btrfs_aops;
467 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
468 inode->i_fop = &btrfs_file_operations;
469 inode->i_op = &btrfs_file_inode_operations;
472 inode->i_fop = &btrfs_dir_file_operations;
473 if (root == root->fs_info->tree_root)
474 inode->i_op = &btrfs_dir_ro_inode_operations;
476 inode->i_op = &btrfs_dir_inode_operations;
479 inode->i_op = &btrfs_symlink_inode_operations;
480 inode->i_mapping->a_ops = &btrfs_symlink_aops;
483 init_special_inode(inode, inode->i_mode, rdev);
489 btrfs_release_path(root, path);
490 btrfs_free_path(path);
491 mutex_unlock(&root->fs_info->fs_mutex);
492 make_bad_inode(inode);
495 static void fill_inode_item(struct extent_buffer *leaf,
496 struct btrfs_inode_item *item,
499 btrfs_set_inode_uid(leaf, item, inode->i_uid);
500 btrfs_set_inode_gid(leaf, item, inode->i_gid);
501 btrfs_set_inode_size(leaf, item, inode->i_size);
502 btrfs_set_inode_mode(leaf, item, inode->i_mode);
503 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
505 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
506 inode->i_atime.tv_sec);
507 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
508 inode->i_atime.tv_nsec);
510 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
511 inode->i_mtime.tv_sec);
512 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
513 inode->i_mtime.tv_nsec);
515 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
516 inode->i_ctime.tv_sec);
517 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
518 inode->i_ctime.tv_nsec);
520 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
521 btrfs_set_inode_generation(leaf, item, inode->i_generation);
522 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
523 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
524 btrfs_set_inode_block_group(leaf, item,
525 BTRFS_I(inode)->block_group->key.objectid);
528 int btrfs_update_inode(struct btrfs_trans_handle *trans,
529 struct btrfs_root *root,
532 struct btrfs_inode_item *inode_item;
533 struct btrfs_path *path;
534 struct extent_buffer *leaf;
537 path = btrfs_alloc_path();
539 ret = btrfs_lookup_inode(trans, root, path,
540 &BTRFS_I(inode)->location, 1);
547 leaf = path->nodes[0];
548 inode_item = btrfs_item_ptr(leaf, path->slots[0],
549 struct btrfs_inode_item);
551 fill_inode_item(leaf, inode_item, inode);
552 btrfs_mark_buffer_dirty(leaf);
553 btrfs_set_inode_last_trans(trans, inode);
556 btrfs_release_path(root, path);
557 btrfs_free_path(path);
562 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
563 struct btrfs_root *root,
565 struct dentry *dentry)
567 struct btrfs_path *path;
568 const char *name = dentry->d_name.name;
569 int name_len = dentry->d_name.len;
571 struct extent_buffer *leaf;
572 struct btrfs_dir_item *di;
573 struct btrfs_key key;
575 path = btrfs_alloc_path();
581 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
591 leaf = path->nodes[0];
592 btrfs_dir_item_key_to_cpu(leaf, di, &key);
593 ret = btrfs_delete_one_dir_name(trans, root, path, di);
596 btrfs_release_path(root, path);
598 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
599 key.objectid, name, name_len, -1);
608 ret = btrfs_delete_one_dir_name(trans, root, path, di);
610 dentry->d_inode->i_ctime = dir->i_ctime;
611 ret = btrfs_del_inode_ref(trans, root, name, name_len,
612 dentry->d_inode->i_ino,
613 dentry->d_parent->d_inode->i_ino);
615 printk("failed to delete reference to %.*s, "
616 "inode %lu parent %lu\n", name_len, name,
617 dentry->d_inode->i_ino,
618 dentry->d_parent->d_inode->i_ino);
621 btrfs_free_path(path);
623 dir->i_size -= name_len * 2;
624 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
625 btrfs_update_inode(trans, root, dir);
626 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
627 dentry->d_inode->i_nlink--;
629 drop_nlink(dentry->d_inode);
631 ret = btrfs_update_inode(trans, root, dentry->d_inode);
632 dir->i_sb->s_dirt = 1;
637 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
639 struct btrfs_root *root;
640 struct btrfs_trans_handle *trans;
641 struct inode *inode = dentry->d_inode;
643 unsigned long nr = 0;
645 root = BTRFS_I(dir)->root;
646 mutex_lock(&root->fs_info->fs_mutex);
648 ret = btrfs_check_free_space(root, 1, 1);
652 trans = btrfs_start_transaction(root, 1);
654 btrfs_set_trans_block_group(trans, dir);
655 ret = btrfs_unlink_trans(trans, root, dir, dentry);
656 nr = trans->blocks_used;
658 if (inode->i_nlink == 0) {
660 /* if the inode isn't linked anywhere,
661 * we don't need to worry about
664 found = btrfs_del_ordered_inode(inode);
666 atomic_dec(&inode->i_count);
670 btrfs_end_transaction(trans, root);
672 mutex_unlock(&root->fs_info->fs_mutex);
673 btrfs_btree_balance_dirty(root, nr);
674 btrfs_throttle(root);
678 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
680 struct inode *inode = dentry->d_inode;
683 struct btrfs_root *root = BTRFS_I(dir)->root;
684 struct btrfs_trans_handle *trans;
685 unsigned long nr = 0;
687 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
690 mutex_lock(&root->fs_info->fs_mutex);
691 ret = btrfs_check_free_space(root, 1, 1);
695 trans = btrfs_start_transaction(root, 1);
696 btrfs_set_trans_block_group(trans, dir);
698 /* now the directory is empty */
699 err = btrfs_unlink_trans(trans, root, dir, dentry);
704 nr = trans->blocks_used;
705 ret = btrfs_end_transaction(trans, root);
707 mutex_unlock(&root->fs_info->fs_mutex);
708 btrfs_btree_balance_dirty(root, nr);
709 btrfs_throttle(root);
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,
730 struct btrfs_path *path;
731 struct btrfs_key key;
732 struct btrfs_key found_key;
734 struct extent_buffer *leaf;
735 struct btrfs_file_extent_item *fi;
736 u64 extent_start = 0;
737 u64 extent_num_bytes = 0;
743 int pending_del_nr = 0;
744 int pending_del_slot = 0;
745 int extent_type = -1;
747 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
748 path = btrfs_alloc_path();
752 /* FIXME, add redo link to tree so we don't leak on crash */
753 key.objectid = inode->i_ino;
754 key.offset = (u64)-1;
757 btrfs_init_path(path);
759 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
764 BUG_ON(path->slots[0] == 0);
770 leaf = path->nodes[0];
771 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
772 found_type = btrfs_key_type(&found_key);
774 if (found_key.objectid != inode->i_ino)
777 if (found_type < min_type)
780 item_end = found_key.offset;
781 if (found_type == BTRFS_EXTENT_DATA_KEY) {
782 fi = btrfs_item_ptr(leaf, path->slots[0],
783 struct btrfs_file_extent_item);
784 extent_type = btrfs_file_extent_type(leaf, fi);
785 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
787 btrfs_file_extent_num_bytes(leaf, fi);
788 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
789 struct btrfs_item *item = btrfs_item_nr(leaf,
791 item_end += btrfs_file_extent_inline_len(leaf,
796 if (found_type == BTRFS_CSUM_ITEM_KEY) {
797 ret = btrfs_csum_truncate(trans, root, path,
801 if (item_end < inode->i_size) {
802 if (found_type == BTRFS_DIR_ITEM_KEY) {
803 found_type = BTRFS_INODE_ITEM_KEY;
804 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
805 found_type = BTRFS_CSUM_ITEM_KEY;
806 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
807 found_type = BTRFS_XATTR_ITEM_KEY;
808 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
809 found_type = BTRFS_INODE_REF_KEY;
810 } else if (found_type) {
815 btrfs_set_key_type(&key, found_type);
818 if (found_key.offset >= inode->i_size)
824 /* FIXME, shrink the extent if the ref count is only 1 */
825 if (found_type != BTRFS_EXTENT_DATA_KEY)
828 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
830 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
833 btrfs_file_extent_num_bytes(leaf, fi);
834 extent_num_bytes = inode->i_size -
835 found_key.offset + root->sectorsize - 1;
836 extent_num_bytes = extent_num_bytes &
837 ~((u64)root->sectorsize - 1);
838 btrfs_set_file_extent_num_bytes(leaf, fi,
840 num_dec = (orig_num_bytes -
842 if (extent_start != 0)
843 dec_i_blocks(inode, num_dec);
844 btrfs_mark_buffer_dirty(leaf);
847 btrfs_file_extent_disk_num_bytes(leaf,
849 /* FIXME blocksize != 4096 */
850 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
851 if (extent_start != 0) {
853 dec_i_blocks(inode, num_dec);
855 root_gen = btrfs_header_generation(leaf);
856 root_owner = btrfs_header_owner(leaf);
858 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
860 u32 newsize = inode->i_size - found_key.offset;
861 dec_i_blocks(inode, item_end + 1 -
862 found_key.offset - newsize);
864 btrfs_file_extent_calc_inline_size(newsize);
865 ret = btrfs_truncate_item(trans, root, path,
869 dec_i_blocks(inode, item_end + 1 -
875 if (!pending_del_nr) {
876 /* no pending yet, add ourselves */
877 pending_del_slot = path->slots[0];
879 } else if (pending_del_nr &&
880 path->slots[0] + 1 == pending_del_slot) {
881 /* hop on the pending chunk */
883 pending_del_slot = path->slots[0];
885 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
891 ret = btrfs_free_extent(trans, root, extent_start,
894 root_gen, inode->i_ino,
895 found_key.offset, 0);
899 if (path->slots[0] == 0) {
902 btrfs_release_path(root, path);
907 if (pending_del_nr &&
908 path->slots[0] + 1 != pending_del_slot) {
909 struct btrfs_key debug;
911 btrfs_item_key_to_cpu(path->nodes[0], &debug,
913 ret = btrfs_del_items(trans, root, path,
918 btrfs_release_path(root, path);
924 if (pending_del_nr) {
925 ret = btrfs_del_items(trans, root, path, pending_del_slot,
928 btrfs_release_path(root, path);
929 btrfs_free_path(path);
930 inode->i_sb->s_dirt = 1;
934 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
938 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
939 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
940 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
943 WARN_ON(!PageLocked(page));
944 set_page_extent_mapped(page);
946 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
947 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
950 if (zero_start != PAGE_CACHE_SIZE) {
952 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
953 flush_dcache_page(page);
956 set_page_dirty(page);
957 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
963 * taken from block_truncate_page, but does cow as it zeros out
964 * any bytes left in the last page in the file.
966 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
968 struct inode *inode = mapping->host;
969 struct btrfs_root *root = BTRFS_I(inode)->root;
970 u32 blocksize = root->sectorsize;
971 pgoff_t index = from >> PAGE_CACHE_SHIFT;
972 unsigned offset = from & (PAGE_CACHE_SIZE-1);
977 if ((offset & (blocksize - 1)) == 0)
981 page = grab_cache_page(mapping, index);
984 if (!PageUptodate(page)) {
985 ret = btrfs_readpage(NULL, page);
987 if (!PageUptodate(page)) {
992 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
994 ret = btrfs_cow_one_page(inode, page, offset);
997 page_cache_release(page);
1002 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1004 struct inode *inode = dentry->d_inode;
1007 err = inode_change_ok(inode, attr);
1011 if (S_ISREG(inode->i_mode) &&
1012 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1013 struct btrfs_trans_handle *trans;
1014 struct btrfs_root *root = BTRFS_I(inode)->root;
1015 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1017 u64 mask = root->sectorsize - 1;
1018 u64 hole_start = (inode->i_size + mask) & ~mask;
1019 u64 block_end = (attr->ia_size + mask) & ~mask;
1023 if (attr->ia_size <= hole_start)
1026 mutex_lock(&root->fs_info->fs_mutex);
1027 err = btrfs_check_free_space(root, 1, 0);
1028 mutex_unlock(&root->fs_info->fs_mutex);
1032 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1034 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1035 hole_size = block_end - hole_start;
1037 mutex_lock(&root->fs_info->fs_mutex);
1038 trans = btrfs_start_transaction(root, 1);
1039 btrfs_set_trans_block_group(trans, inode);
1040 err = btrfs_drop_extents(trans, root, inode,
1041 hole_start, block_end, hole_start,
1044 if (alloc_hint != EXTENT_MAP_INLINE) {
1045 err = btrfs_insert_file_extent(trans, root,
1049 btrfs_drop_extent_cache(inode, hole_start,
1051 btrfs_check_file(root, inode);
1053 btrfs_end_transaction(trans, root);
1054 mutex_unlock(&root->fs_info->fs_mutex);
1055 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1060 err = inode_setattr(inode, attr);
1065 void btrfs_put_inode(struct inode *inode)
1069 if (!BTRFS_I(inode)->ordered_trans) {
1073 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1074 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1077 ret = btrfs_del_ordered_inode(inode);
1079 atomic_dec(&inode->i_count);
1083 void btrfs_delete_inode(struct inode *inode)
1085 struct btrfs_trans_handle *trans;
1086 struct btrfs_root *root = BTRFS_I(inode)->root;
1090 truncate_inode_pages(&inode->i_data, 0);
1091 if (is_bad_inode(inode)) {
1096 mutex_lock(&root->fs_info->fs_mutex);
1097 trans = btrfs_start_transaction(root, 1);
1099 btrfs_set_trans_block_group(trans, inode);
1100 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1102 goto no_delete_lock;
1104 nr = trans->blocks_used;
1107 btrfs_end_transaction(trans, root);
1108 mutex_unlock(&root->fs_info->fs_mutex);
1109 btrfs_btree_balance_dirty(root, nr);
1110 btrfs_throttle(root);
1114 nr = trans->blocks_used;
1115 btrfs_end_transaction(trans, root);
1116 mutex_unlock(&root->fs_info->fs_mutex);
1117 btrfs_btree_balance_dirty(root, nr);
1118 btrfs_throttle(root);
1124 * this returns the key found in the dir entry in the location pointer.
1125 * If no dir entries were found, location->objectid is 0.
1127 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1128 struct btrfs_key *location)
1130 const char *name = dentry->d_name.name;
1131 int namelen = dentry->d_name.len;
1132 struct btrfs_dir_item *di;
1133 struct btrfs_path *path;
1134 struct btrfs_root *root = BTRFS_I(dir)->root;
1137 if (namelen == 1 && strcmp(name, ".") == 0) {
1138 location->objectid = dir->i_ino;
1139 location->type = BTRFS_INODE_ITEM_KEY;
1140 location->offset = 0;
1143 path = btrfs_alloc_path();
1146 if (namelen == 2 && strcmp(name, "..") == 0) {
1147 struct btrfs_key key;
1148 struct extent_buffer *leaf;
1152 key.objectid = dir->i_ino;
1153 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1155 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1159 leaf = path->nodes[0];
1160 slot = path->slots[0];
1161 nritems = btrfs_header_nritems(leaf);
1162 if (slot >= nritems)
1165 btrfs_item_key_to_cpu(leaf, &key, slot);
1166 if (key.objectid != dir->i_ino ||
1167 key.type != BTRFS_INODE_REF_KEY) {
1170 location->objectid = key.offset;
1171 location->type = BTRFS_INODE_ITEM_KEY;
1172 location->offset = 0;
1176 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1180 if (!di || IS_ERR(di)) {
1183 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1185 btrfs_free_path(path);
1188 location->objectid = 0;
1193 * when we hit a tree root in a directory, the btrfs part of the inode
1194 * needs to be changed to reflect the root directory of the tree root. This
1195 * is kind of like crossing a mount point.
1197 static int fixup_tree_root_location(struct btrfs_root *root,
1198 struct btrfs_key *location,
1199 struct btrfs_root **sub_root,
1200 struct dentry *dentry)
1202 struct btrfs_path *path;
1203 struct btrfs_root_item *ri;
1205 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1207 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1210 path = btrfs_alloc_path();
1212 mutex_lock(&root->fs_info->fs_mutex);
1214 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1215 dentry->d_name.name,
1216 dentry->d_name.len);
1217 if (IS_ERR(*sub_root))
1218 return PTR_ERR(*sub_root);
1220 ri = &(*sub_root)->root_item;
1221 location->objectid = btrfs_root_dirid(ri);
1222 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1223 location->offset = 0;
1225 btrfs_free_path(path);
1226 mutex_unlock(&root->fs_info->fs_mutex);
1230 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1232 struct btrfs_iget_args *args = p;
1233 inode->i_ino = args->ino;
1234 BTRFS_I(inode)->root = args->root;
1235 BTRFS_I(inode)->delalloc_bytes = 0;
1236 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1237 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1238 inode->i_mapping, GFP_NOFS);
1242 static int btrfs_find_actor(struct inode *inode, void *opaque)
1244 struct btrfs_iget_args *args = opaque;
1245 return (args->ino == inode->i_ino &&
1246 args->root == BTRFS_I(inode)->root);
1249 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1252 struct btrfs_iget_args args;
1253 args.ino = objectid;
1254 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1259 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1262 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1263 struct btrfs_root *root)
1265 struct inode *inode;
1266 struct btrfs_iget_args args;
1267 args.ino = objectid;
1270 inode = iget5_locked(s, objectid, btrfs_find_actor,
1271 btrfs_init_locked_inode,
1276 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1277 struct nameidata *nd)
1279 struct inode * inode;
1280 struct btrfs_inode *bi = BTRFS_I(dir);
1281 struct btrfs_root *root = bi->root;
1282 struct btrfs_root *sub_root = root;
1283 struct btrfs_key location;
1286 if (dentry->d_name.len > BTRFS_NAME_LEN)
1287 return ERR_PTR(-ENAMETOOLONG);
1289 mutex_lock(&root->fs_info->fs_mutex);
1290 ret = btrfs_inode_by_name(dir, dentry, &location);
1291 mutex_unlock(&root->fs_info->fs_mutex);
1294 return ERR_PTR(ret);
1297 if (location.objectid) {
1298 ret = fixup_tree_root_location(root, &location, &sub_root,
1301 return ERR_PTR(ret);
1303 return ERR_PTR(-ENOENT);
1304 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1307 return ERR_PTR(-EACCES);
1308 if (inode->i_state & I_NEW) {
1309 /* the inode and parent dir are two different roots */
1310 if (sub_root != root) {
1312 sub_root->inode = inode;
1314 BTRFS_I(inode)->root = sub_root;
1315 memcpy(&BTRFS_I(inode)->location, &location,
1317 btrfs_read_locked_inode(inode);
1318 unlock_new_inode(inode);
1321 return d_splice_alias(inode, dentry);
1324 static unsigned char btrfs_filetype_table[] = {
1325 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1328 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1330 struct inode *inode = filp->f_dentry->d_inode;
1331 struct btrfs_root *root = BTRFS_I(inode)->root;
1332 struct btrfs_item *item;
1333 struct btrfs_dir_item *di;
1334 struct btrfs_key key;
1335 struct btrfs_key found_key;
1336 struct btrfs_path *path;
1339 struct extent_buffer *leaf;
1342 unsigned char d_type;
1347 int key_type = BTRFS_DIR_INDEX_KEY;
1352 /* FIXME, use a real flag for deciding about the key type */
1353 if (root->fs_info->tree_root == root)
1354 key_type = BTRFS_DIR_ITEM_KEY;
1356 /* special case for "." */
1357 if (filp->f_pos == 0) {
1358 over = filldir(dirent, ".", 1,
1366 mutex_lock(&root->fs_info->fs_mutex);
1367 key.objectid = inode->i_ino;
1368 path = btrfs_alloc_path();
1371 /* special case for .., just use the back ref */
1372 if (filp->f_pos == 1) {
1373 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1375 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1377 leaf = path->nodes[0];
1378 slot = path->slots[0];
1379 nritems = btrfs_header_nritems(leaf);
1380 if (slot >= nritems) {
1381 btrfs_release_path(root, path);
1382 goto read_dir_items;
1384 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1385 btrfs_release_path(root, path);
1386 if (found_key.objectid != key.objectid ||
1387 found_key.type != BTRFS_INODE_REF_KEY)
1388 goto read_dir_items;
1389 over = filldir(dirent, "..", 2,
1390 2, found_key.offset, DT_DIR);
1397 btrfs_set_key_type(&key, key_type);
1398 key.offset = filp->f_pos;
1400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1405 leaf = path->nodes[0];
1406 nritems = btrfs_header_nritems(leaf);
1407 slot = path->slots[0];
1408 if (advance || slot >= nritems) {
1409 if (slot >= nritems -1) {
1410 ret = btrfs_next_leaf(root, path);
1413 leaf = path->nodes[0];
1414 nritems = btrfs_header_nritems(leaf);
1415 slot = path->slots[0];
1422 item = btrfs_item_nr(leaf, slot);
1423 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1425 if (found_key.objectid != key.objectid)
1427 if (btrfs_key_type(&found_key) != key_type)
1429 if (found_key.offset < filp->f_pos)
1432 filp->f_pos = found_key.offset;
1434 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1436 di_total = btrfs_item_size(leaf, item);
1437 while(di_cur < di_total) {
1438 struct btrfs_key location;
1440 name_len = btrfs_dir_name_len(leaf, di);
1441 if (name_len < 32) {
1442 name_ptr = tmp_name;
1444 name_ptr = kmalloc(name_len, GFP_NOFS);
1447 read_extent_buffer(leaf, name_ptr,
1448 (unsigned long)(di + 1), name_len);
1450 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1451 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1452 over = filldir(dirent, name_ptr, name_len,
1457 if (name_ptr != tmp_name)
1462 di_len = btrfs_dir_name_len(leaf, di) +
1463 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1465 di = (struct btrfs_dir_item *)((char *)di + di_len);
1468 if (key_type == BTRFS_DIR_INDEX_KEY)
1469 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1475 btrfs_release_path(root, path);
1476 btrfs_free_path(path);
1477 mutex_unlock(&root->fs_info->fs_mutex);
1481 int btrfs_write_inode(struct inode *inode, int wait)
1483 struct btrfs_root *root = BTRFS_I(inode)->root;
1484 struct btrfs_trans_handle *trans;
1488 mutex_lock(&root->fs_info->fs_mutex);
1489 trans = btrfs_start_transaction(root, 1);
1490 btrfs_set_trans_block_group(trans, inode);
1491 ret = btrfs_commit_transaction(trans, root);
1492 mutex_unlock(&root->fs_info->fs_mutex);
1498 * This is somewhat expensive, updating the tree every time the
1499 * inode changes. But, it is most likely to find the inode in cache.
1500 * FIXME, needs more benchmarking...there are no reasons other than performance
1501 * to keep or drop this code.
1503 void btrfs_dirty_inode(struct inode *inode)
1505 struct btrfs_root *root = BTRFS_I(inode)->root;
1506 struct btrfs_trans_handle *trans;
1508 mutex_lock(&root->fs_info->fs_mutex);
1509 trans = btrfs_start_transaction(root, 1);
1510 btrfs_set_trans_block_group(trans, inode);
1511 btrfs_update_inode(trans, root, inode);
1512 btrfs_end_transaction(trans, root);
1513 mutex_unlock(&root->fs_info->fs_mutex);
1516 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1517 struct btrfs_root *root,
1518 const char *name, int name_len,
1521 struct btrfs_block_group_cache *group,
1524 struct inode *inode;
1525 struct btrfs_inode_item *inode_item;
1526 struct btrfs_key *location;
1527 struct btrfs_path *path;
1528 struct btrfs_inode_ref *ref;
1529 struct btrfs_key key[2];
1535 path = btrfs_alloc_path();
1538 inode = new_inode(root->fs_info->sb);
1540 return ERR_PTR(-ENOMEM);
1542 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1543 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1544 inode->i_mapping, GFP_NOFS);
1545 BTRFS_I(inode)->delalloc_bytes = 0;
1546 BTRFS_I(inode)->root = root;
1552 group = btrfs_find_block_group(root, group, 0, 0, owner);
1553 BTRFS_I(inode)->block_group = group;
1554 BTRFS_I(inode)->flags = 0;
1556 key[0].objectid = objectid;
1557 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1560 key[1].objectid = objectid;
1561 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1562 key[1].offset = ref_objectid;
1564 sizes[0] = sizeof(struct btrfs_inode_item);
1565 sizes[1] = name_len + sizeof(*ref);
1567 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1571 if (objectid > root->highest_inode)
1572 root->highest_inode = objectid;
1574 inode->i_uid = current->fsuid;
1575 inode->i_gid = current->fsgid;
1576 inode->i_mode = mode;
1577 inode->i_ino = objectid;
1578 inode->i_blocks = 0;
1579 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1580 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1581 struct btrfs_inode_item);
1582 fill_inode_item(path->nodes[0], inode_item, inode);
1584 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1585 struct btrfs_inode_ref);
1586 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1587 ptr = (unsigned long)(ref + 1);
1588 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1590 btrfs_mark_buffer_dirty(path->nodes[0]);
1591 btrfs_free_path(path);
1593 location = &BTRFS_I(inode)->location;
1594 location->objectid = objectid;
1595 location->offset = 0;
1596 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1598 insert_inode_hash(inode);
1601 btrfs_free_path(path);
1602 return ERR_PTR(ret);
1605 static inline u8 btrfs_inode_type(struct inode *inode)
1607 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1610 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1611 struct dentry *dentry, struct inode *inode,
1615 struct btrfs_key key;
1616 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1617 struct inode *parent_inode;
1619 key.objectid = inode->i_ino;
1620 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1623 ret = btrfs_insert_dir_item(trans, root,
1624 dentry->d_name.name, dentry->d_name.len,
1625 dentry->d_parent->d_inode->i_ino,
1626 &key, btrfs_inode_type(inode));
1629 ret = btrfs_insert_inode_ref(trans, root,
1630 dentry->d_name.name,
1633 dentry->d_parent->d_inode->i_ino);
1635 parent_inode = dentry->d_parent->d_inode;
1636 parent_inode->i_size += dentry->d_name.len * 2;
1637 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1638 ret = btrfs_update_inode(trans, root,
1639 dentry->d_parent->d_inode);
1644 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1645 struct dentry *dentry, struct inode *inode,
1648 int err = btrfs_add_link(trans, dentry, inode, backref);
1650 d_instantiate(dentry, inode);
1658 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1659 int mode, dev_t rdev)
1661 struct btrfs_trans_handle *trans;
1662 struct btrfs_root *root = BTRFS_I(dir)->root;
1663 struct inode *inode = NULL;
1667 unsigned long nr = 0;
1669 if (!new_valid_dev(rdev))
1672 mutex_lock(&root->fs_info->fs_mutex);
1673 err = btrfs_check_free_space(root, 1, 0);
1677 trans = btrfs_start_transaction(root, 1);
1678 btrfs_set_trans_block_group(trans, dir);
1680 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1686 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1688 dentry->d_parent->d_inode->i_ino, objectid,
1689 BTRFS_I(dir)->block_group, mode);
1690 err = PTR_ERR(inode);
1694 btrfs_set_trans_block_group(trans, inode);
1695 err = btrfs_add_nondir(trans, dentry, inode, 0);
1699 inode->i_op = &btrfs_special_inode_operations;
1700 init_special_inode(inode, inode->i_mode, rdev);
1701 btrfs_update_inode(trans, root, inode);
1703 dir->i_sb->s_dirt = 1;
1704 btrfs_update_inode_block_group(trans, inode);
1705 btrfs_update_inode_block_group(trans, dir);
1707 nr = trans->blocks_used;
1708 btrfs_end_transaction(trans, root);
1710 mutex_unlock(&root->fs_info->fs_mutex);
1713 inode_dec_link_count(inode);
1716 btrfs_btree_balance_dirty(root, nr);
1717 btrfs_throttle(root);
1721 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1722 int mode, struct nameidata *nd)
1724 struct btrfs_trans_handle *trans;
1725 struct btrfs_root *root = BTRFS_I(dir)->root;
1726 struct inode *inode = NULL;
1729 unsigned long nr = 0;
1732 mutex_lock(&root->fs_info->fs_mutex);
1733 err = btrfs_check_free_space(root, 1, 0);
1736 trans = btrfs_start_transaction(root, 1);
1737 btrfs_set_trans_block_group(trans, dir);
1739 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1745 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1747 dentry->d_parent->d_inode->i_ino,
1748 objectid, BTRFS_I(dir)->block_group, mode);
1749 err = PTR_ERR(inode);
1753 btrfs_set_trans_block_group(trans, inode);
1754 err = btrfs_add_nondir(trans, dentry, inode, 0);
1758 inode->i_mapping->a_ops = &btrfs_aops;
1759 inode->i_fop = &btrfs_file_operations;
1760 inode->i_op = &btrfs_file_inode_operations;
1761 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1762 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1763 inode->i_mapping, GFP_NOFS);
1764 BTRFS_I(inode)->delalloc_bytes = 0;
1765 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1767 dir->i_sb->s_dirt = 1;
1768 btrfs_update_inode_block_group(trans, inode);
1769 btrfs_update_inode_block_group(trans, dir);
1771 nr = trans->blocks_used;
1772 btrfs_end_transaction(trans, root);
1774 mutex_unlock(&root->fs_info->fs_mutex);
1777 inode_dec_link_count(inode);
1780 btrfs_btree_balance_dirty(root, nr);
1781 btrfs_throttle(root);
1785 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1786 struct dentry *dentry)
1788 struct btrfs_trans_handle *trans;
1789 struct btrfs_root *root = BTRFS_I(dir)->root;
1790 struct inode *inode = old_dentry->d_inode;
1791 unsigned long nr = 0;
1795 if (inode->i_nlink == 0)
1798 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1803 mutex_lock(&root->fs_info->fs_mutex);
1804 err = btrfs_check_free_space(root, 1, 0);
1807 trans = btrfs_start_transaction(root, 1);
1809 btrfs_set_trans_block_group(trans, dir);
1810 atomic_inc(&inode->i_count);
1811 err = btrfs_add_nondir(trans, dentry, inode, 1);
1816 dir->i_sb->s_dirt = 1;
1817 btrfs_update_inode_block_group(trans, dir);
1818 err = btrfs_update_inode(trans, root, inode);
1823 nr = trans->blocks_used;
1824 btrfs_end_transaction(trans, root);
1826 mutex_unlock(&root->fs_info->fs_mutex);
1829 inode_dec_link_count(inode);
1832 btrfs_btree_balance_dirty(root, nr);
1833 btrfs_throttle(root);
1837 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1839 struct inode *inode;
1840 struct btrfs_trans_handle *trans;
1841 struct btrfs_root *root = BTRFS_I(dir)->root;
1843 int drop_on_err = 0;
1845 unsigned long nr = 1;
1847 mutex_lock(&root->fs_info->fs_mutex);
1848 err = btrfs_check_free_space(root, 1, 0);
1852 trans = btrfs_start_transaction(root, 1);
1853 btrfs_set_trans_block_group(trans, dir);
1855 if (IS_ERR(trans)) {
1856 err = PTR_ERR(trans);
1860 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1866 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1868 dentry->d_parent->d_inode->i_ino, objectid,
1869 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1870 if (IS_ERR(inode)) {
1871 err = PTR_ERR(inode);
1876 inode->i_op = &btrfs_dir_inode_operations;
1877 inode->i_fop = &btrfs_dir_file_operations;
1878 btrfs_set_trans_block_group(trans, inode);
1881 err = btrfs_update_inode(trans, root, inode);
1885 err = btrfs_add_link(trans, dentry, inode, 0);
1889 d_instantiate(dentry, inode);
1891 dir->i_sb->s_dirt = 1;
1892 btrfs_update_inode_block_group(trans, inode);
1893 btrfs_update_inode_block_group(trans, dir);
1896 nr = trans->blocks_used;
1897 btrfs_end_transaction(trans, root);
1900 mutex_unlock(&root->fs_info->fs_mutex);
1903 btrfs_btree_balance_dirty(root, nr);
1904 btrfs_throttle(root);
1908 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1909 size_t pg_offset, u64 start, u64 len,
1915 u64 extent_start = 0;
1917 u64 objectid = inode->i_ino;
1919 struct btrfs_path *path;
1920 struct btrfs_root *root = BTRFS_I(inode)->root;
1921 struct btrfs_file_extent_item *item;
1922 struct extent_buffer *leaf;
1923 struct btrfs_key found_key;
1924 struct extent_map *em = NULL;
1925 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1926 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1927 struct btrfs_trans_handle *trans = NULL;
1929 path = btrfs_alloc_path();
1931 mutex_lock(&root->fs_info->fs_mutex);
1934 spin_lock(&em_tree->lock);
1935 em = lookup_extent_mapping(em_tree, start, len);
1936 spin_unlock(&em_tree->lock);
1939 if (em->start > start) {
1940 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1941 start, len, em->start, em->len);
1944 if (em->block_start == EXTENT_MAP_INLINE && page)
1945 free_extent_map(em);
1949 em = alloc_extent_map(GFP_NOFS);
1955 em->start = EXTENT_MAP_HOLE;
1957 em->bdev = inode->i_sb->s_bdev;
1958 ret = btrfs_lookup_file_extent(trans, root, path,
1959 objectid, start, trans != NULL);
1966 if (path->slots[0] == 0)
1971 leaf = path->nodes[0];
1972 item = btrfs_item_ptr(leaf, path->slots[0],
1973 struct btrfs_file_extent_item);
1974 /* are we inside the extent that was found? */
1975 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1976 found_type = btrfs_key_type(&found_key);
1977 if (found_key.objectid != objectid ||
1978 found_type != BTRFS_EXTENT_DATA_KEY) {
1982 found_type = btrfs_file_extent_type(leaf, item);
1983 extent_start = found_key.offset;
1984 if (found_type == BTRFS_FILE_EXTENT_REG) {
1985 extent_end = extent_start +
1986 btrfs_file_extent_num_bytes(leaf, item);
1988 if (start < extent_start || start >= extent_end) {
1990 if (start < extent_start) {
1991 if (start + len <= extent_start)
1993 em->len = extent_end - extent_start;
1999 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2001 em->start = extent_start;
2002 em->len = extent_end - extent_start;
2003 em->block_start = EXTENT_MAP_HOLE;
2006 bytenr += btrfs_file_extent_offset(leaf, item);
2007 em->block_start = bytenr;
2008 em->start = extent_start;
2009 em->len = extent_end - extent_start;
2011 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2016 size_t extent_offset;
2019 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2021 extent_end = (extent_start + size + root->sectorsize - 1) &
2022 ~((u64)root->sectorsize - 1);
2023 if (start < extent_start || start >= extent_end) {
2025 if (start < extent_start) {
2026 if (start + len <= extent_start)
2028 em->len = extent_end - extent_start;
2034 em->block_start = EXTENT_MAP_INLINE;
2037 em->start = extent_start;
2042 page_start = page_offset(page) + pg_offset;
2043 extent_offset = page_start - extent_start;
2044 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2045 size - extent_offset);
2046 em->start = extent_start + extent_offset;
2047 em->len = (copy_size + root->sectorsize - 1) &
2048 ~((u64)root->sectorsize - 1);
2050 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2051 if (create == 0 && !PageUptodate(page)) {
2052 read_extent_buffer(leaf, map + pg_offset, ptr,
2054 flush_dcache_page(page);
2055 } else if (create && PageUptodate(page)) {
2058 free_extent_map(em);
2060 btrfs_release_path(root, path);
2061 trans = btrfs_start_transaction(root, 1);
2064 write_extent_buffer(leaf, map + pg_offset, ptr,
2066 btrfs_mark_buffer_dirty(leaf);
2069 set_extent_uptodate(io_tree, em->start,
2070 extent_map_end(em) - 1, GFP_NOFS);
2073 printk("unkknown found_type %d\n", found_type);
2080 em->block_start = EXTENT_MAP_HOLE;
2082 btrfs_release_path(root, path);
2083 if (em->start > start || extent_map_end(em) <= start) {
2084 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2090 spin_lock(&em_tree->lock);
2091 ret = add_extent_mapping(em_tree, em);
2092 if (ret == -EEXIST) {
2093 free_extent_map(em);
2094 em = lookup_extent_mapping(em_tree, start, len);
2097 printk("failing to insert %Lu %Lu\n", start, len);
2100 spin_unlock(&em_tree->lock);
2102 btrfs_free_path(path);
2104 ret = btrfs_end_transaction(trans, root);
2108 mutex_unlock(&root->fs_info->fs_mutex);
2110 free_extent_map(em);
2112 return ERR_PTR(err);
2117 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2119 return extent_bmap(mapping, iblock, btrfs_get_extent);
2122 int btrfs_readpage(struct file *file, struct page *page)
2124 struct extent_io_tree *tree;
2125 tree = &BTRFS_I(page->mapping->host)->io_tree;
2126 return extent_read_full_page(tree, page, btrfs_get_extent);
2129 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2131 struct extent_io_tree *tree;
2134 if (current->flags & PF_MEMALLOC) {
2135 redirty_page_for_writepage(wbc, page);
2139 tree = &BTRFS_I(page->mapping->host)->io_tree;
2140 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2143 static int btrfs_writepages(struct address_space *mapping,
2144 struct writeback_control *wbc)
2146 struct extent_io_tree *tree;
2147 tree = &BTRFS_I(mapping->host)->io_tree;
2148 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2152 btrfs_readpages(struct file *file, struct address_space *mapping,
2153 struct list_head *pages, unsigned nr_pages)
2155 struct extent_io_tree *tree;
2156 tree = &BTRFS_I(mapping->host)->io_tree;
2157 return extent_readpages(tree, mapping, pages, nr_pages,
2161 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2163 struct extent_io_tree *tree;
2164 struct extent_map_tree *map;
2167 tree = &BTRFS_I(page->mapping->host)->io_tree;
2168 map = &BTRFS_I(page->mapping->host)->extent_tree;
2169 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2171 ClearPagePrivate(page);
2172 set_page_private(page, 0);
2173 page_cache_release(page);
2178 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2180 struct extent_io_tree *tree;
2182 tree = &BTRFS_I(page->mapping->host)->io_tree;
2183 extent_invalidatepage(tree, page, offset);
2184 btrfs_releasepage(page, GFP_NOFS);
2188 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2189 * called from a page fault handler when a page is first dirtied. Hence we must
2190 * be careful to check for EOF conditions here. We set the page up correctly
2191 * for a written page which means we get ENOSPC checking when writing into
2192 * holes and correct delalloc and unwritten extent mapping on filesystems that
2193 * support these features.
2195 * We are not allowed to take the i_mutex here so we have to play games to
2196 * protect against truncate races as the page could now be beyond EOF. Because
2197 * vmtruncate() writes the inode size before removing pages, once we have the
2198 * page lock we can determine safely if the page is beyond EOF. If it is not
2199 * beyond EOF, then the page is guaranteed safe against truncation until we
2202 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2204 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2205 struct btrfs_root *root = BTRFS_I(inode)->root;
2211 mutex_lock(&root->fs_info->fs_mutex);
2212 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2213 mutex_unlock(&root->fs_info->fs_mutex);
2220 wait_on_page_writeback(page);
2221 size = i_size_read(inode);
2222 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2224 if ((page->mapping != inode->i_mapping) ||
2225 (page_start > size)) {
2226 /* page got truncated out from underneath us */
2230 /* page is wholly or partially inside EOF */
2231 if (page_start + PAGE_CACHE_SIZE > size)
2232 end = size & ~PAGE_CACHE_MASK;
2234 end = PAGE_CACHE_SIZE;
2236 ret = btrfs_cow_one_page(inode, page, end);
2244 static void btrfs_truncate(struct inode *inode)
2246 struct btrfs_root *root = BTRFS_I(inode)->root;
2248 struct btrfs_trans_handle *trans;
2251 if (!S_ISREG(inode->i_mode))
2253 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2256 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2258 mutex_lock(&root->fs_info->fs_mutex);
2259 trans = btrfs_start_transaction(root, 1);
2260 btrfs_set_trans_block_group(trans, inode);
2262 /* FIXME, add redo link to tree so we don't leak on crash */
2263 ret = btrfs_truncate_in_trans(trans, root, inode,
2264 BTRFS_EXTENT_DATA_KEY);
2265 btrfs_update_inode(trans, root, inode);
2266 nr = trans->blocks_used;
2268 ret = btrfs_end_transaction(trans, root);
2270 mutex_unlock(&root->fs_info->fs_mutex);
2271 btrfs_btree_balance_dirty(root, nr);
2272 btrfs_throttle(root);
2275 static int noinline create_subvol(struct btrfs_root *root, char *name,
2278 struct btrfs_trans_handle *trans;
2279 struct btrfs_key key;
2280 struct btrfs_root_item root_item;
2281 struct btrfs_inode_item *inode_item;
2282 struct extent_buffer *leaf;
2283 struct btrfs_root *new_root = root;
2284 struct inode *inode;
2289 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2290 unsigned long nr = 1;
2292 mutex_lock(&root->fs_info->fs_mutex);
2293 ret = btrfs_check_free_space(root, 1, 0);
2297 trans = btrfs_start_transaction(root, 1);
2300 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2305 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2306 objectid, trans->transid, 0, 0,
2309 return PTR_ERR(leaf);
2311 btrfs_set_header_nritems(leaf, 0);
2312 btrfs_set_header_level(leaf, 0);
2313 btrfs_set_header_bytenr(leaf, leaf->start);
2314 btrfs_set_header_generation(leaf, trans->transid);
2315 btrfs_set_header_owner(leaf, objectid);
2317 write_extent_buffer(leaf, root->fs_info->fsid,
2318 (unsigned long)btrfs_header_fsid(leaf),
2320 btrfs_mark_buffer_dirty(leaf);
2322 inode_item = &root_item.inode;
2323 memset(inode_item, 0, sizeof(*inode_item));
2324 inode_item->generation = cpu_to_le64(1);
2325 inode_item->size = cpu_to_le64(3);
2326 inode_item->nlink = cpu_to_le32(1);
2327 inode_item->nblocks = cpu_to_le64(1);
2328 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2330 btrfs_set_root_bytenr(&root_item, leaf->start);
2331 btrfs_set_root_level(&root_item, 0);
2332 btrfs_set_root_refs(&root_item, 1);
2333 btrfs_set_root_used(&root_item, 0);
2335 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2336 root_item.drop_level = 0;
2338 free_extent_buffer(leaf);
2341 btrfs_set_root_dirid(&root_item, new_dirid);
2343 key.objectid = objectid;
2345 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2346 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2352 * insert the directory item
2354 key.offset = (u64)-1;
2355 dir = root->fs_info->sb->s_root->d_inode;
2356 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2357 name, namelen, dir->i_ino, &key,
2362 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2363 name, namelen, objectid,
2364 root->fs_info->sb->s_root->d_inode->i_ino);
2368 ret = btrfs_commit_transaction(trans, root);
2372 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2375 trans = btrfs_start_transaction(new_root, 1);
2378 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2380 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2383 inode->i_op = &btrfs_dir_inode_operations;
2384 inode->i_fop = &btrfs_dir_file_operations;
2385 new_root->inode = inode;
2387 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2391 ret = btrfs_update_inode(trans, new_root, inode);
2395 nr = trans->blocks_used;
2396 err = btrfs_commit_transaction(trans, new_root);
2400 mutex_unlock(&root->fs_info->fs_mutex);
2401 btrfs_btree_balance_dirty(root, nr);
2402 btrfs_throttle(root);
2406 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2408 struct btrfs_pending_snapshot *pending_snapshot;
2409 struct btrfs_trans_handle *trans;
2412 unsigned long nr = 0;
2414 if (!root->ref_cows)
2417 mutex_lock(&root->fs_info->fs_mutex);
2418 ret = btrfs_check_free_space(root, 1, 0);
2422 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2423 if (!pending_snapshot) {
2427 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2428 if (!pending_snapshot->name) {
2430 kfree(pending_snapshot);
2433 memcpy(pending_snapshot->name, name, namelen);
2434 pending_snapshot->name[namelen] = '\0';
2435 trans = btrfs_start_transaction(root, 1);
2437 pending_snapshot->root = root;
2438 list_add(&pending_snapshot->list,
2439 &trans->transaction->pending_snapshots);
2440 ret = btrfs_update_inode(trans, root, root->inode);
2441 err = btrfs_commit_transaction(trans, root);
2444 mutex_unlock(&root->fs_info->fs_mutex);
2445 btrfs_btree_balance_dirty(root, nr);
2446 btrfs_throttle(root);
2450 unsigned long btrfs_force_ra(struct address_space *mapping,
2451 struct file_ra_state *ra, struct file *file,
2452 pgoff_t offset, pgoff_t last_index)
2456 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2457 req_size = last_index - offset + 1;
2458 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2461 req_size = min(last_index - offset + 1, (pgoff_t)128);
2462 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2463 return offset + req_size;
2467 int btrfs_defrag_file(struct file *file) {
2468 struct inode *inode = fdentry(file)->d_inode;
2469 struct btrfs_root *root = BTRFS_I(inode)->root;
2470 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2472 unsigned long last_index;
2473 unsigned long ra_index = 0;
2479 mutex_lock(&root->fs_info->fs_mutex);
2480 ret = btrfs_check_free_space(root, inode->i_size, 0);
2481 mutex_unlock(&root->fs_info->fs_mutex);
2485 mutex_lock(&inode->i_mutex);
2486 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2487 for (i = 0; i <= last_index; i++) {
2488 if (i == ra_index) {
2489 ra_index = btrfs_force_ra(inode->i_mapping,
2491 file, ra_index, last_index);
2493 page = grab_cache_page(inode->i_mapping, i);
2496 if (!PageUptodate(page)) {
2497 btrfs_readpage(NULL, page);
2499 if (!PageUptodate(page)) {
2501 page_cache_release(page);
2505 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2506 page_end = page_start + PAGE_CACHE_SIZE - 1;
2508 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2509 set_extent_delalloc(io_tree, page_start,
2510 page_end, GFP_NOFS);
2512 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2513 set_page_dirty(page);
2515 page_cache_release(page);
2516 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2520 mutex_unlock(&inode->i_mutex);
2524 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2528 struct btrfs_ioctl_vol_args *vol_args;
2529 struct btrfs_trans_handle *trans;
2535 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2540 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2544 namelen = strlen(vol_args->name);
2545 if (namelen > BTRFS_VOL_NAME_MAX) {
2550 sizestr = vol_args->name;
2551 if (!strcmp(sizestr, "max"))
2552 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2554 if (sizestr[0] == '-') {
2557 } else if (sizestr[0] == '+') {
2561 new_size = btrfs_parse_size(sizestr);
2562 if (new_size == 0) {
2568 mutex_lock(&root->fs_info->fs_mutex);
2569 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2572 if (new_size > old_size) {
2576 new_size = old_size - new_size;
2577 } else if (mod > 0) {
2578 new_size = old_size + new_size;
2581 if (new_size < 256 * 1024 * 1024) {
2585 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2590 do_div(new_size, root->sectorsize);
2591 new_size *= root->sectorsize;
2593 printk("new size is %Lu\n", new_size);
2594 if (new_size > old_size) {
2595 trans = btrfs_start_transaction(root, 1);
2596 ret = btrfs_grow_extent_tree(trans, root, new_size);
2597 btrfs_commit_transaction(trans, root);
2599 ret = btrfs_shrink_extent_tree(root, new_size);
2603 mutex_unlock(&root->fs_info->fs_mutex);
2609 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2612 struct btrfs_ioctl_vol_args *vol_args;
2613 struct btrfs_dir_item *di;
2614 struct btrfs_path *path;
2619 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2624 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2629 namelen = strlen(vol_args->name);
2630 if (namelen > BTRFS_VOL_NAME_MAX) {
2634 if (strchr(vol_args->name, '/')) {
2639 path = btrfs_alloc_path();
2645 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2646 mutex_lock(&root->fs_info->fs_mutex);
2647 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2649 vol_args->name, namelen, 0);
2650 mutex_unlock(&root->fs_info->fs_mutex);
2651 btrfs_free_path(path);
2653 if (di && !IS_ERR(di)) {
2663 if (root == root->fs_info->tree_root)
2664 ret = create_subvol(root, vol_args->name, namelen);
2666 ret = create_snapshot(root, vol_args->name, namelen);
2672 static int btrfs_ioctl_defrag(struct file *file)
2674 struct inode *inode = fdentry(file)->d_inode;
2675 struct btrfs_root *root = BTRFS_I(inode)->root;
2677 switch (inode->i_mode & S_IFMT) {
2679 mutex_lock(&root->fs_info->fs_mutex);
2680 btrfs_defrag_root(root, 0);
2681 btrfs_defrag_root(root->fs_info->extent_root, 0);
2682 mutex_unlock(&root->fs_info->fs_mutex);
2685 btrfs_defrag_file(file);
2692 long btrfs_ioctl(struct file *file, unsigned int
2693 cmd, unsigned long arg)
2695 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2698 case BTRFS_IOC_SNAP_CREATE:
2699 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2700 case BTRFS_IOC_DEFRAG:
2701 return btrfs_ioctl_defrag(file);
2702 case BTRFS_IOC_RESIZE:
2703 return btrfs_ioctl_resize(root, (void __user *)arg);
2710 * Called inside transaction, so use GFP_NOFS
2712 struct inode *btrfs_alloc_inode(struct super_block *sb)
2714 struct btrfs_inode *ei;
2716 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2720 ei->ordered_trans = 0;
2721 return &ei->vfs_inode;
2724 void btrfs_destroy_inode(struct inode *inode)
2726 WARN_ON(!list_empty(&inode->i_dentry));
2727 WARN_ON(inode->i_data.nrpages);
2729 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2730 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2733 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2734 static void init_once(struct kmem_cache * cachep, void *foo)
2736 static void init_once(void * foo, struct kmem_cache * cachep,
2737 unsigned long flags)
2740 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2742 inode_init_once(&ei->vfs_inode);
2745 void btrfs_destroy_cachep(void)
2747 if (btrfs_inode_cachep)
2748 kmem_cache_destroy(btrfs_inode_cachep);
2749 if (btrfs_trans_handle_cachep)
2750 kmem_cache_destroy(btrfs_trans_handle_cachep);
2751 if (btrfs_transaction_cachep)
2752 kmem_cache_destroy(btrfs_transaction_cachep);
2753 if (btrfs_bit_radix_cachep)
2754 kmem_cache_destroy(btrfs_bit_radix_cachep);
2755 if (btrfs_path_cachep)
2756 kmem_cache_destroy(btrfs_path_cachep);
2759 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2760 unsigned long extra_flags,
2761 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2762 void (*ctor)(struct kmem_cache *, void *)
2764 void (*ctor)(void *, struct kmem_cache *,
2769 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2770 SLAB_MEM_SPREAD | extra_flags), ctor
2771 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2777 int btrfs_init_cachep(void)
2779 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2780 sizeof(struct btrfs_inode),
2782 if (!btrfs_inode_cachep)
2784 btrfs_trans_handle_cachep =
2785 btrfs_cache_create("btrfs_trans_handle_cache",
2786 sizeof(struct btrfs_trans_handle),
2788 if (!btrfs_trans_handle_cachep)
2790 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2791 sizeof(struct btrfs_transaction),
2793 if (!btrfs_transaction_cachep)
2795 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2796 sizeof(struct btrfs_path),
2798 if (!btrfs_path_cachep)
2800 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2801 SLAB_DESTROY_BY_RCU, NULL);
2802 if (!btrfs_bit_radix_cachep)
2806 btrfs_destroy_cachep();
2810 static int btrfs_getattr(struct vfsmount *mnt,
2811 struct dentry *dentry, struct kstat *stat)
2813 struct inode *inode = dentry->d_inode;
2814 generic_fillattr(inode, stat);
2815 stat->blksize = PAGE_CACHE_SIZE;
2816 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2820 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2821 struct inode * new_dir,struct dentry *new_dentry)
2823 struct btrfs_trans_handle *trans;
2824 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2825 struct inode *new_inode = new_dentry->d_inode;
2826 struct inode *old_inode = old_dentry->d_inode;
2827 struct timespec ctime = CURRENT_TIME;
2828 struct btrfs_path *path;
2831 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2832 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2836 mutex_lock(&root->fs_info->fs_mutex);
2837 ret = btrfs_check_free_space(root, 1, 0);
2841 trans = btrfs_start_transaction(root, 1);
2843 btrfs_set_trans_block_group(trans, new_dir);
2844 path = btrfs_alloc_path();
2850 old_dentry->d_inode->i_nlink++;
2851 old_dir->i_ctime = old_dir->i_mtime = ctime;
2852 new_dir->i_ctime = new_dir->i_mtime = ctime;
2853 old_inode->i_ctime = ctime;
2855 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2860 new_inode->i_ctime = CURRENT_TIME;
2861 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2865 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2870 btrfs_free_path(path);
2871 btrfs_end_transaction(trans, root);
2873 mutex_unlock(&root->fs_info->fs_mutex);
2877 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2878 const char *symname)
2880 struct btrfs_trans_handle *trans;
2881 struct btrfs_root *root = BTRFS_I(dir)->root;
2882 struct btrfs_path *path;
2883 struct btrfs_key key;
2884 struct inode *inode = NULL;
2891 struct btrfs_file_extent_item *ei;
2892 struct extent_buffer *leaf;
2893 unsigned long nr = 0;
2895 name_len = strlen(symname) + 1;
2896 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2897 return -ENAMETOOLONG;
2899 mutex_lock(&root->fs_info->fs_mutex);
2900 err = btrfs_check_free_space(root, 1, 0);
2904 trans = btrfs_start_transaction(root, 1);
2905 btrfs_set_trans_block_group(trans, dir);
2907 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2913 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2915 dentry->d_parent->d_inode->i_ino, objectid,
2916 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2917 err = PTR_ERR(inode);
2921 btrfs_set_trans_block_group(trans, inode);
2922 err = btrfs_add_nondir(trans, dentry, inode, 0);
2926 inode->i_mapping->a_ops = &btrfs_aops;
2927 inode->i_fop = &btrfs_file_operations;
2928 inode->i_op = &btrfs_file_inode_operations;
2929 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2930 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2931 inode->i_mapping, GFP_NOFS);
2932 BTRFS_I(inode)->delalloc_bytes = 0;
2933 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2935 dir->i_sb->s_dirt = 1;
2936 btrfs_update_inode_block_group(trans, inode);
2937 btrfs_update_inode_block_group(trans, dir);
2941 path = btrfs_alloc_path();
2943 key.objectid = inode->i_ino;
2945 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2946 datasize = btrfs_file_extent_calc_inline_size(name_len);
2947 err = btrfs_insert_empty_item(trans, root, path, &key,
2953 leaf = path->nodes[0];
2954 ei = btrfs_item_ptr(leaf, path->slots[0],
2955 struct btrfs_file_extent_item);
2956 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2957 btrfs_set_file_extent_type(leaf, ei,
2958 BTRFS_FILE_EXTENT_INLINE);
2959 ptr = btrfs_file_extent_inline_start(ei);
2960 write_extent_buffer(leaf, symname, ptr, name_len);
2961 btrfs_mark_buffer_dirty(leaf);
2962 btrfs_free_path(path);
2964 inode->i_op = &btrfs_symlink_inode_operations;
2965 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2966 inode->i_size = name_len - 1;
2967 err = btrfs_update_inode(trans, root, inode);
2972 nr = trans->blocks_used;
2973 btrfs_end_transaction(trans, root);
2975 mutex_unlock(&root->fs_info->fs_mutex);
2977 inode_dec_link_count(inode);
2980 btrfs_btree_balance_dirty(root, nr);
2981 btrfs_throttle(root);
2984 static int btrfs_permission(struct inode *inode, int mask,
2985 struct nameidata *nd)
2987 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2989 return generic_permission(inode, mask, NULL);
2992 static struct inode_operations btrfs_dir_inode_operations = {
2993 .lookup = btrfs_lookup,
2994 .create = btrfs_create,
2995 .unlink = btrfs_unlink,
2997 .mkdir = btrfs_mkdir,
2998 .rmdir = btrfs_rmdir,
2999 .rename = btrfs_rename,
3000 .symlink = btrfs_symlink,
3001 .setattr = btrfs_setattr,
3002 .mknod = btrfs_mknod,
3003 .setxattr = generic_setxattr,
3004 .getxattr = generic_getxattr,
3005 .listxattr = btrfs_listxattr,
3006 .removexattr = generic_removexattr,
3007 .permission = btrfs_permission,
3009 static struct inode_operations btrfs_dir_ro_inode_operations = {
3010 .lookup = btrfs_lookup,
3011 .permission = btrfs_permission,
3013 static struct file_operations btrfs_dir_file_operations = {
3014 .llseek = generic_file_llseek,
3015 .read = generic_read_dir,
3016 .readdir = btrfs_readdir,
3017 .unlocked_ioctl = btrfs_ioctl,
3018 #ifdef CONFIG_COMPAT
3019 .compat_ioctl = btrfs_ioctl,
3023 static struct extent_io_ops btrfs_extent_io_ops = {
3024 .fill_delalloc = run_delalloc_range,
3025 .writepage_io_hook = btrfs_writepage_io_hook,
3026 .readpage_io_hook = btrfs_readpage_io_hook,
3027 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3028 .set_bit_hook = btrfs_set_bit_hook,
3029 .clear_bit_hook = btrfs_clear_bit_hook,
3032 static struct address_space_operations btrfs_aops = {
3033 .readpage = btrfs_readpage,
3034 .writepage = btrfs_writepage,
3035 .writepages = btrfs_writepages,
3036 .readpages = btrfs_readpages,
3037 .sync_page = block_sync_page,
3039 .invalidatepage = btrfs_invalidatepage,
3040 .releasepage = btrfs_releasepage,
3041 .set_page_dirty = __set_page_dirty_nobuffers,
3044 static struct address_space_operations btrfs_symlink_aops = {
3045 .readpage = btrfs_readpage,
3046 .writepage = btrfs_writepage,
3047 .invalidatepage = btrfs_invalidatepage,
3048 .releasepage = btrfs_releasepage,
3051 static struct inode_operations btrfs_file_inode_operations = {
3052 .truncate = btrfs_truncate,
3053 .getattr = btrfs_getattr,
3054 .setattr = btrfs_setattr,
3055 .setxattr = generic_setxattr,
3056 .getxattr = generic_getxattr,
3057 .listxattr = btrfs_listxattr,
3058 .removexattr = generic_removexattr,
3059 .permission = btrfs_permission,
3061 static struct inode_operations btrfs_special_inode_operations = {
3062 .getattr = btrfs_getattr,
3063 .setattr = btrfs_setattr,
3064 .permission = btrfs_permission,
3066 static struct inode_operations btrfs_symlink_inode_operations = {
3067 .readlink = generic_readlink,
3068 .follow_link = page_follow_link_light,
3069 .put_link = page_put_link,
3070 .permission = btrfs_permission,