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 btrfs_check_file(root, inode);
138 num_bytes -= cur_alloc_size;
139 alloc_hint = ins.objectid + ins.offset;
140 start += cur_alloc_size;
142 btrfs_drop_extent_cache(inode, orig_start,
143 orig_start + orig_num_bytes - 1);
144 btrfs_add_ordered_inode(inode);
146 btrfs_end_transaction(trans, root);
150 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
158 struct btrfs_root *root = BTRFS_I(inode)->root;
159 struct extent_buffer *leaf;
161 struct btrfs_path *path;
162 struct btrfs_file_extent_item *item;
165 struct btrfs_key found_key;
167 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
168 path = btrfs_alloc_path();
171 ret = btrfs_lookup_file_extent(NULL, root, path,
172 inode->i_ino, start, 0);
174 btrfs_free_path(path);
180 if (path->slots[0] == 0)
185 leaf = path->nodes[0];
186 item = btrfs_item_ptr(leaf, path->slots[0],
187 struct btrfs_file_extent_item);
189 /* are we inside the extent that was found? */
190 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
191 found_type = btrfs_key_type(&found_key);
192 if (found_key.objectid != inode->i_ino ||
193 found_type != BTRFS_EXTENT_DATA_KEY) {
197 found_type = btrfs_file_extent_type(leaf, item);
198 extent_start = found_key.offset;
199 if (found_type == BTRFS_FILE_EXTENT_REG) {
200 u64 extent_num_bytes;
202 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
203 extent_end = extent_start + extent_num_bytes;
206 if (loops && start != extent_start)
209 if (start < extent_start || start >= extent_end)
212 cow_end = min(end, extent_end - 1);
213 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
218 * we may be called by the resizer, make sure we're inside
219 * the limits of the FS
221 if (bytenr + extent_num_bytes > total_fs_bytes)
224 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
234 btrfs_free_path(path);
237 btrfs_release_path(root, path);
242 cow_file_range(inode, start, cow_end);
247 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
249 struct btrfs_root *root = BTRFS_I(inode)->root;
251 mutex_lock(&root->fs_info->fs_mutex);
252 if (btrfs_test_opt(root, NODATACOW) ||
253 btrfs_test_flag(inode, NODATACOW))
254 ret = run_delalloc_nocow(inode, start, end);
256 ret = cow_file_range(inode, start, end);
258 mutex_unlock(&root->fs_info->fs_mutex);
262 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
265 if ((bits & EXTENT_DELALLOC)) {
266 struct btrfs_root *root = BTRFS_I(inode)->root;
267 spin_lock(&root->fs_info->delalloc_lock);
268 root->fs_info->delalloc_bytes += end - start + 1;
269 spin_unlock(&root->fs_info->delalloc_lock);
274 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
277 if ((bits & EXTENT_DELALLOC)) {
278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 spin_lock(&root->fs_info->delalloc_lock);
280 root->fs_info->delalloc_bytes -= end - start + 1;
281 spin_unlock(&root->fs_info->delalloc_lock);
286 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
288 struct inode *inode = page->mapping->host;
289 struct btrfs_root *root = BTRFS_I(inode)->root;
290 struct btrfs_trans_handle *trans;
293 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
294 size_t offset = start - page_start;
295 if (btrfs_test_opt(root, NODATASUM) ||
296 btrfs_test_flag(inode, NODATASUM))
298 mutex_lock(&root->fs_info->fs_mutex);
299 trans = btrfs_start_transaction(root, 1);
300 btrfs_set_trans_block_group(trans, inode);
302 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
303 start, kaddr + offset, end - start + 1);
305 ret = btrfs_end_transaction(trans, root);
307 mutex_unlock(&root->fs_info->fs_mutex);
311 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
314 struct inode *inode = page->mapping->host;
315 struct btrfs_root *root = BTRFS_I(inode)->root;
316 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
317 struct btrfs_csum_item *item;
318 struct btrfs_path *path = NULL;
320 if (btrfs_test_opt(root, NODATASUM) ||
321 btrfs_test_flag(inode, NODATASUM))
323 mutex_lock(&root->fs_info->fs_mutex);
324 path = btrfs_alloc_path();
325 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
328 /* a csum that isn't present is a preallocated region. */
329 if (ret == -ENOENT || ret == -EFBIG)
332 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
335 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
337 set_state_private(io_tree, start, csum);
340 btrfs_free_path(path);
341 mutex_unlock(&root->fs_info->fs_mutex);
345 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
346 struct extent_state *state)
348 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
349 struct inode *inode = page->mapping->host;
350 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
352 u64 private = ~(u32)0;
354 struct btrfs_root *root = BTRFS_I(inode)->root;
358 if (btrfs_test_opt(root, NODATASUM) ||
359 btrfs_test_flag(inode, NODATASUM))
362 if (state->start == start) {
363 private = state->private;
366 ret = get_state_private(io_tree, start, &private);
368 local_irq_save(flags);
369 kaddr = kmap_atomic(page, KM_IRQ0);
373 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
374 btrfs_csum_final(csum, (char *)&csum);
375 if (csum != private) {
378 kunmap_atomic(kaddr, KM_IRQ0);
379 local_irq_restore(flags);
383 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
384 page->mapping->host->i_ino, (unsigned long long)start, csum,
386 memset(kaddr + offset, 1, end - start + 1);
387 flush_dcache_page(page);
388 kunmap_atomic(kaddr, KM_IRQ0);
389 local_irq_restore(flags);
393 void btrfs_read_locked_inode(struct inode *inode)
395 struct btrfs_path *path;
396 struct extent_buffer *leaf;
397 struct btrfs_inode_item *inode_item;
398 struct btrfs_inode_timespec *tspec;
399 struct btrfs_root *root = BTRFS_I(inode)->root;
400 struct btrfs_key location;
401 u64 alloc_group_block;
405 path = btrfs_alloc_path();
407 mutex_lock(&root->fs_info->fs_mutex);
408 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
410 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
414 leaf = path->nodes[0];
415 inode_item = btrfs_item_ptr(leaf, path->slots[0],
416 struct btrfs_inode_item);
418 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
419 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
420 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
421 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
422 inode->i_size = btrfs_inode_size(leaf, inode_item);
424 tspec = btrfs_inode_atime(inode_item);
425 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
426 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
428 tspec = btrfs_inode_mtime(inode_item);
429 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
430 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
432 tspec = btrfs_inode_ctime(inode_item);
433 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
434 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
436 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
437 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
439 rdev = btrfs_inode_rdev(leaf, inode_item);
441 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
442 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
444 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
445 if (!BTRFS_I(inode)->block_group) {
446 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
449 btrfs_free_path(path);
452 mutex_unlock(&root->fs_info->fs_mutex);
454 switch (inode->i_mode & S_IFMT) {
456 inode->i_mapping->a_ops = &btrfs_aops;
457 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
458 inode->i_fop = &btrfs_file_operations;
459 inode->i_op = &btrfs_file_inode_operations;
462 inode->i_fop = &btrfs_dir_file_operations;
463 if (root == root->fs_info->tree_root)
464 inode->i_op = &btrfs_dir_ro_inode_operations;
466 inode->i_op = &btrfs_dir_inode_operations;
469 inode->i_op = &btrfs_symlink_inode_operations;
470 inode->i_mapping->a_ops = &btrfs_symlink_aops;
473 init_special_inode(inode, inode->i_mode, rdev);
479 btrfs_release_path(root, path);
480 btrfs_free_path(path);
481 mutex_unlock(&root->fs_info->fs_mutex);
482 make_bad_inode(inode);
485 static void fill_inode_item(struct extent_buffer *leaf,
486 struct btrfs_inode_item *item,
489 btrfs_set_inode_uid(leaf, item, inode->i_uid);
490 btrfs_set_inode_gid(leaf, item, inode->i_gid);
491 btrfs_set_inode_size(leaf, item, inode->i_size);
492 btrfs_set_inode_mode(leaf, item, inode->i_mode);
493 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
495 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
496 inode->i_atime.tv_sec);
497 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
498 inode->i_atime.tv_nsec);
500 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
501 inode->i_mtime.tv_sec);
502 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
503 inode->i_mtime.tv_nsec);
505 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
506 inode->i_ctime.tv_sec);
507 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
508 inode->i_ctime.tv_nsec);
510 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
511 btrfs_set_inode_generation(leaf, item, inode->i_generation);
512 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
513 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
514 btrfs_set_inode_block_group(leaf, item,
515 BTRFS_I(inode)->block_group->key.objectid);
518 int btrfs_update_inode(struct btrfs_trans_handle *trans,
519 struct btrfs_root *root,
522 struct btrfs_inode_item *inode_item;
523 struct btrfs_path *path;
524 struct extent_buffer *leaf;
527 path = btrfs_alloc_path();
529 ret = btrfs_lookup_inode(trans, root, path,
530 &BTRFS_I(inode)->location, 1);
537 leaf = path->nodes[0];
538 inode_item = btrfs_item_ptr(leaf, path->slots[0],
539 struct btrfs_inode_item);
541 fill_inode_item(leaf, inode_item, inode);
542 btrfs_mark_buffer_dirty(leaf);
543 btrfs_set_inode_last_trans(trans, inode);
546 btrfs_release_path(root, path);
547 btrfs_free_path(path);
552 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
555 struct dentry *dentry)
557 struct btrfs_path *path;
558 const char *name = dentry->d_name.name;
559 int name_len = dentry->d_name.len;
561 struct extent_buffer *leaf;
562 struct btrfs_dir_item *di;
563 struct btrfs_key key;
565 path = btrfs_alloc_path();
571 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
581 leaf = path->nodes[0];
582 btrfs_dir_item_key_to_cpu(leaf, di, &key);
583 ret = btrfs_delete_one_dir_name(trans, root, path, di);
586 btrfs_release_path(root, path);
588 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
589 key.objectid, name, name_len, -1);
598 ret = btrfs_delete_one_dir_name(trans, root, path, di);
600 dentry->d_inode->i_ctime = dir->i_ctime;
601 ret = btrfs_del_inode_ref(trans, root, name, name_len,
602 dentry->d_inode->i_ino,
603 dentry->d_parent->d_inode->i_ino);
605 printk("failed to delete reference to %.*s, "
606 "inode %lu parent %lu\n", name_len, name,
607 dentry->d_inode->i_ino,
608 dentry->d_parent->d_inode->i_ino);
611 btrfs_free_path(path);
613 dir->i_size -= name_len * 2;
614 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
615 btrfs_update_inode(trans, root, dir);
616 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
617 dentry->d_inode->i_nlink--;
619 drop_nlink(dentry->d_inode);
621 ret = btrfs_update_inode(trans, root, dentry->d_inode);
622 dir->i_sb->s_dirt = 1;
627 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
629 struct btrfs_root *root;
630 struct btrfs_trans_handle *trans;
631 struct inode *inode = dentry->d_inode;
633 unsigned long nr = 0;
635 root = BTRFS_I(dir)->root;
636 mutex_lock(&root->fs_info->fs_mutex);
638 ret = btrfs_check_free_space(root, 1, 1);
642 trans = btrfs_start_transaction(root, 1);
644 btrfs_set_trans_block_group(trans, dir);
645 ret = btrfs_unlink_trans(trans, root, dir, dentry);
646 nr = trans->blocks_used;
648 if (inode->i_nlink == 0) {
650 /* if the inode isn't linked anywhere,
651 * we don't need to worry about
654 found = btrfs_del_ordered_inode(inode);
656 atomic_dec(&inode->i_count);
660 btrfs_end_transaction(trans, root);
662 mutex_unlock(&root->fs_info->fs_mutex);
663 btrfs_btree_balance_dirty(root, nr);
664 btrfs_throttle(root);
668 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
670 struct inode *inode = dentry->d_inode;
673 struct btrfs_root *root = BTRFS_I(dir)->root;
674 struct btrfs_trans_handle *trans;
675 unsigned long nr = 0;
677 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
680 mutex_lock(&root->fs_info->fs_mutex);
681 ret = btrfs_check_free_space(root, 1, 1);
685 trans = btrfs_start_transaction(root, 1);
686 btrfs_set_trans_block_group(trans, dir);
688 /* now the directory is empty */
689 err = btrfs_unlink_trans(trans, root, dir, dentry);
694 nr = trans->blocks_used;
695 ret = btrfs_end_transaction(trans, root);
697 mutex_unlock(&root->fs_info->fs_mutex);
698 btrfs_btree_balance_dirty(root, nr);
699 btrfs_throttle(root);
707 * this can truncate away extent items, csum items and directory items.
708 * It starts at a high offset and removes keys until it can't find
709 * any higher than i_size.
711 * csum items that cross the new i_size are truncated to the new size
714 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
715 struct btrfs_root *root,
720 struct btrfs_path *path;
721 struct btrfs_key key;
722 struct btrfs_key found_key;
724 struct extent_buffer *leaf;
725 struct btrfs_file_extent_item *fi;
726 u64 extent_start = 0;
727 u64 extent_num_bytes = 0;
733 int pending_del_nr = 0;
734 int pending_del_slot = 0;
735 int extent_type = -1;
737 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
738 path = btrfs_alloc_path();
742 /* FIXME, add redo link to tree so we don't leak on crash */
743 key.objectid = inode->i_ino;
744 key.offset = (u64)-1;
747 btrfs_init_path(path);
749 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
754 BUG_ON(path->slots[0] == 0);
760 leaf = path->nodes[0];
761 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
762 found_type = btrfs_key_type(&found_key);
764 if (found_key.objectid != inode->i_ino)
767 if (found_type < min_type)
770 item_end = found_key.offset;
771 if (found_type == BTRFS_EXTENT_DATA_KEY) {
772 fi = btrfs_item_ptr(leaf, path->slots[0],
773 struct btrfs_file_extent_item);
774 extent_type = btrfs_file_extent_type(leaf, fi);
775 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
777 btrfs_file_extent_num_bytes(leaf, fi);
778 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
779 struct btrfs_item *item = btrfs_item_nr(leaf,
781 item_end += btrfs_file_extent_inline_len(leaf,
786 if (found_type == BTRFS_CSUM_ITEM_KEY) {
787 ret = btrfs_csum_truncate(trans, root, path,
791 if (item_end < inode->i_size) {
792 if (found_type == BTRFS_DIR_ITEM_KEY) {
793 found_type = BTRFS_INODE_ITEM_KEY;
794 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
795 found_type = BTRFS_CSUM_ITEM_KEY;
796 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
797 found_type = BTRFS_XATTR_ITEM_KEY;
798 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
799 found_type = BTRFS_INODE_REF_KEY;
800 } else if (found_type) {
805 btrfs_set_key_type(&key, found_type);
808 if (found_key.offset >= inode->i_size)
814 /* FIXME, shrink the extent if the ref count is only 1 */
815 if (found_type != BTRFS_EXTENT_DATA_KEY)
818 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
820 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
823 btrfs_file_extent_num_bytes(leaf, fi);
824 extent_num_bytes = inode->i_size -
825 found_key.offset + root->sectorsize - 1;
826 extent_num_bytes = extent_num_bytes &
827 ~((u64)root->sectorsize - 1);
828 btrfs_set_file_extent_num_bytes(leaf, fi,
830 num_dec = (orig_num_bytes -
831 extent_num_bytes) >> 9;
832 if (extent_start != 0) {
833 inode->i_blocks -= num_dec;
835 btrfs_mark_buffer_dirty(leaf);
838 btrfs_file_extent_disk_num_bytes(leaf,
840 /* FIXME blocksize != 4096 */
841 num_dec = btrfs_file_extent_num_bytes(leaf,
843 if (extent_start != 0) {
845 inode->i_blocks -= num_dec;
847 root_gen = btrfs_header_generation(leaf);
848 root_owner = btrfs_header_owner(leaf);
850 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
852 u32 newsize = inode->i_size - found_key.offset;
853 newsize = btrfs_file_extent_calc_inline_size(newsize);
854 ret = btrfs_truncate_item(trans, root, path,
860 if (!pending_del_nr) {
861 /* no pending yet, add ourselves */
862 pending_del_slot = path->slots[0];
864 } else if (pending_del_nr &&
865 path->slots[0] + 1 == pending_del_slot) {
866 /* hop on the pending chunk */
868 pending_del_slot = path->slots[0];
870 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
876 ret = btrfs_free_extent(trans, root, extent_start,
879 root_gen, inode->i_ino,
880 found_key.offset, 0);
884 if (path->slots[0] == 0) {
887 btrfs_release_path(root, path);
892 if (pending_del_nr &&
893 path->slots[0] + 1 != pending_del_slot) {
894 struct btrfs_key debug;
896 btrfs_item_key_to_cpu(path->nodes[0], &debug,
898 ret = btrfs_del_items(trans, root, path,
903 btrfs_release_path(root, path);
909 if (pending_del_nr) {
910 ret = btrfs_del_items(trans, root, path, pending_del_slot,
913 btrfs_release_path(root, path);
914 btrfs_free_path(path);
915 inode->i_sb->s_dirt = 1;
919 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
924 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
925 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
928 WARN_ON(!PageLocked(page));
929 set_page_extent_mapped(page);
931 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
932 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
935 if (zero_start != PAGE_CACHE_SIZE) {
937 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
938 flush_dcache_page(page);
941 set_page_dirty(page);
942 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
948 * taken from block_truncate_page, but does cow as it zeros out
949 * any bytes left in the last page in the file.
951 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
953 struct inode *inode = mapping->host;
954 struct btrfs_root *root = BTRFS_I(inode)->root;
955 u32 blocksize = root->sectorsize;
956 pgoff_t index = from >> PAGE_CACHE_SHIFT;
957 unsigned offset = from & (PAGE_CACHE_SIZE-1);
962 if ((offset & (blocksize - 1)) == 0)
966 page = grab_cache_page(mapping, index);
969 if (!PageUptodate(page)) {
970 ret = btrfs_readpage(NULL, page);
972 if (!PageUptodate(page)) {
977 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
979 ret = btrfs_cow_one_page(inode, page, offset);
982 page_cache_release(page);
987 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
989 struct inode *inode = dentry->d_inode;
992 err = inode_change_ok(inode, attr);
996 if (S_ISREG(inode->i_mode) &&
997 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
998 struct btrfs_trans_handle *trans;
999 struct btrfs_root *root = BTRFS_I(inode)->root;
1000 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1002 u64 mask = root->sectorsize - 1;
1003 u64 hole_start = (inode->i_size + mask) & ~mask;
1004 u64 block_end = (attr->ia_size + mask) & ~mask;
1008 if (attr->ia_size <= hole_start)
1011 mutex_lock(&root->fs_info->fs_mutex);
1012 err = btrfs_check_free_space(root, 1, 0);
1013 mutex_unlock(&root->fs_info->fs_mutex);
1017 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1019 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1020 hole_size = block_end - hole_start;
1022 mutex_lock(&root->fs_info->fs_mutex);
1023 trans = btrfs_start_transaction(root, 1);
1024 btrfs_set_trans_block_group(trans, inode);
1025 err = btrfs_drop_extents(trans, root, inode,
1026 hole_start, block_end, hole_start,
1029 if (alloc_hint != EXTENT_MAP_INLINE) {
1030 err = btrfs_insert_file_extent(trans, root,
1034 btrfs_drop_extent_cache(inode, hole_start,
1036 btrfs_check_file(root, inode);
1038 btrfs_end_transaction(trans, root);
1039 mutex_unlock(&root->fs_info->fs_mutex);
1040 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1045 err = inode_setattr(inode, attr);
1050 void btrfs_put_inode(struct inode *inode)
1054 if (!BTRFS_I(inode)->ordered_trans) {
1058 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1059 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1062 ret = btrfs_del_ordered_inode(inode);
1064 atomic_dec(&inode->i_count);
1068 void btrfs_delete_inode(struct inode *inode)
1070 struct btrfs_trans_handle *trans;
1071 struct btrfs_root *root = BTRFS_I(inode)->root;
1075 truncate_inode_pages(&inode->i_data, 0);
1076 if (is_bad_inode(inode)) {
1081 mutex_lock(&root->fs_info->fs_mutex);
1082 trans = btrfs_start_transaction(root, 1);
1084 btrfs_set_trans_block_group(trans, inode);
1085 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1087 goto no_delete_lock;
1089 nr = trans->blocks_used;
1092 btrfs_end_transaction(trans, root);
1093 mutex_unlock(&root->fs_info->fs_mutex);
1094 btrfs_btree_balance_dirty(root, nr);
1095 btrfs_throttle(root);
1099 nr = trans->blocks_used;
1100 btrfs_end_transaction(trans, root);
1101 mutex_unlock(&root->fs_info->fs_mutex);
1102 btrfs_btree_balance_dirty(root, nr);
1103 btrfs_throttle(root);
1109 * this returns the key found in the dir entry in the location pointer.
1110 * If no dir entries were found, location->objectid is 0.
1112 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1113 struct btrfs_key *location)
1115 const char *name = dentry->d_name.name;
1116 int namelen = dentry->d_name.len;
1117 struct btrfs_dir_item *di;
1118 struct btrfs_path *path;
1119 struct btrfs_root *root = BTRFS_I(dir)->root;
1122 if (namelen == 1 && strcmp(name, ".") == 0) {
1123 location->objectid = dir->i_ino;
1124 location->type = BTRFS_INODE_ITEM_KEY;
1125 location->offset = 0;
1128 path = btrfs_alloc_path();
1131 if (namelen == 2 && strcmp(name, "..") == 0) {
1132 struct btrfs_key key;
1133 struct extent_buffer *leaf;
1137 key.objectid = dir->i_ino;
1138 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1144 leaf = path->nodes[0];
1145 slot = path->slots[0];
1146 nritems = btrfs_header_nritems(leaf);
1147 if (slot >= nritems)
1150 btrfs_item_key_to_cpu(leaf, &key, slot);
1151 if (key.objectid != dir->i_ino ||
1152 key.type != BTRFS_INODE_REF_KEY) {
1155 location->objectid = key.offset;
1156 location->type = BTRFS_INODE_ITEM_KEY;
1157 location->offset = 0;
1161 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1165 if (!di || IS_ERR(di)) {
1168 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1170 btrfs_free_path(path);
1173 location->objectid = 0;
1178 * when we hit a tree root in a directory, the btrfs part of the inode
1179 * needs to be changed to reflect the root directory of the tree root. This
1180 * is kind of like crossing a mount point.
1182 static int fixup_tree_root_location(struct btrfs_root *root,
1183 struct btrfs_key *location,
1184 struct btrfs_root **sub_root,
1185 struct dentry *dentry)
1187 struct btrfs_path *path;
1188 struct btrfs_root_item *ri;
1190 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1192 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1195 path = btrfs_alloc_path();
1197 mutex_lock(&root->fs_info->fs_mutex);
1199 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1200 dentry->d_name.name,
1201 dentry->d_name.len);
1202 if (IS_ERR(*sub_root))
1203 return PTR_ERR(*sub_root);
1205 ri = &(*sub_root)->root_item;
1206 location->objectid = btrfs_root_dirid(ri);
1207 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1208 location->offset = 0;
1210 btrfs_free_path(path);
1211 mutex_unlock(&root->fs_info->fs_mutex);
1215 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1217 struct btrfs_iget_args *args = p;
1218 inode->i_ino = args->ino;
1219 BTRFS_I(inode)->root = args->root;
1220 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1221 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1222 inode->i_mapping, GFP_NOFS);
1226 static int btrfs_find_actor(struct inode *inode, void *opaque)
1228 struct btrfs_iget_args *args = opaque;
1229 return (args->ino == inode->i_ino &&
1230 args->root == BTRFS_I(inode)->root);
1233 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1236 struct btrfs_iget_args args;
1237 args.ino = objectid;
1238 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1243 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1246 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1247 struct btrfs_root *root)
1249 struct inode *inode;
1250 struct btrfs_iget_args args;
1251 args.ino = objectid;
1254 inode = iget5_locked(s, objectid, btrfs_find_actor,
1255 btrfs_init_locked_inode,
1260 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1261 struct nameidata *nd)
1263 struct inode * inode;
1264 struct btrfs_inode *bi = BTRFS_I(dir);
1265 struct btrfs_root *root = bi->root;
1266 struct btrfs_root *sub_root = root;
1267 struct btrfs_key location;
1270 if (dentry->d_name.len > BTRFS_NAME_LEN)
1271 return ERR_PTR(-ENAMETOOLONG);
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 ret = btrfs_inode_by_name(dir, dentry, &location);
1275 mutex_unlock(&root->fs_info->fs_mutex);
1278 return ERR_PTR(ret);
1281 if (location.objectid) {
1282 ret = fixup_tree_root_location(root, &location, &sub_root,
1285 return ERR_PTR(ret);
1287 return ERR_PTR(-ENOENT);
1288 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1291 return ERR_PTR(-EACCES);
1292 if (inode->i_state & I_NEW) {
1293 /* the inode and parent dir are two different roots */
1294 if (sub_root != root) {
1296 sub_root->inode = inode;
1298 BTRFS_I(inode)->root = sub_root;
1299 memcpy(&BTRFS_I(inode)->location, &location,
1301 btrfs_read_locked_inode(inode);
1302 unlock_new_inode(inode);
1305 return d_splice_alias(inode, dentry);
1308 static unsigned char btrfs_filetype_table[] = {
1309 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1312 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1314 struct inode *inode = filp->f_dentry->d_inode;
1315 struct btrfs_root *root = BTRFS_I(inode)->root;
1316 struct btrfs_item *item;
1317 struct btrfs_dir_item *di;
1318 struct btrfs_key key;
1319 struct btrfs_key found_key;
1320 struct btrfs_path *path;
1323 struct extent_buffer *leaf;
1326 unsigned char d_type;
1331 int key_type = BTRFS_DIR_INDEX_KEY;
1336 /* FIXME, use a real flag for deciding about the key type */
1337 if (root->fs_info->tree_root == root)
1338 key_type = BTRFS_DIR_ITEM_KEY;
1340 /* special case for "." */
1341 if (filp->f_pos == 0) {
1342 over = filldir(dirent, ".", 1,
1350 mutex_lock(&root->fs_info->fs_mutex);
1351 key.objectid = inode->i_ino;
1352 path = btrfs_alloc_path();
1355 /* special case for .., just use the back ref */
1356 if (filp->f_pos == 1) {
1357 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1359 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1361 leaf = path->nodes[0];
1362 slot = path->slots[0];
1363 nritems = btrfs_header_nritems(leaf);
1364 if (slot >= nritems) {
1365 btrfs_release_path(root, path);
1366 goto read_dir_items;
1368 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1369 btrfs_release_path(root, path);
1370 if (found_key.objectid != key.objectid ||
1371 found_key.type != BTRFS_INODE_REF_KEY)
1372 goto read_dir_items;
1373 over = filldir(dirent, "..", 2,
1374 2, found_key.offset, DT_DIR);
1381 btrfs_set_key_type(&key, key_type);
1382 key.offset = filp->f_pos;
1384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1389 leaf = path->nodes[0];
1390 nritems = btrfs_header_nritems(leaf);
1391 slot = path->slots[0];
1392 if (advance || slot >= nritems) {
1393 if (slot >= nritems -1) {
1394 ret = btrfs_next_leaf(root, path);
1397 leaf = path->nodes[0];
1398 nritems = btrfs_header_nritems(leaf);
1399 slot = path->slots[0];
1406 item = btrfs_item_nr(leaf, slot);
1407 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1409 if (found_key.objectid != key.objectid)
1411 if (btrfs_key_type(&found_key) != key_type)
1413 if (found_key.offset < filp->f_pos)
1416 filp->f_pos = found_key.offset;
1418 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1420 di_total = btrfs_item_size(leaf, item);
1421 while(di_cur < di_total) {
1422 struct btrfs_key location;
1424 name_len = btrfs_dir_name_len(leaf, di);
1425 if (name_len < 32) {
1426 name_ptr = tmp_name;
1428 name_ptr = kmalloc(name_len, GFP_NOFS);
1431 read_extent_buffer(leaf, name_ptr,
1432 (unsigned long)(di + 1), name_len);
1434 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1435 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1436 over = filldir(dirent, name_ptr, name_len,
1441 if (name_ptr != tmp_name)
1446 di_len = btrfs_dir_name_len(leaf, di) +
1447 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1449 di = (struct btrfs_dir_item *)((char *)di + di_len);
1452 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1456 btrfs_release_path(root, path);
1457 btrfs_free_path(path);
1458 mutex_unlock(&root->fs_info->fs_mutex);
1462 int btrfs_write_inode(struct inode *inode, int wait)
1464 struct btrfs_root *root = BTRFS_I(inode)->root;
1465 struct btrfs_trans_handle *trans;
1469 mutex_lock(&root->fs_info->fs_mutex);
1470 trans = btrfs_start_transaction(root, 1);
1471 btrfs_set_trans_block_group(trans, inode);
1472 ret = btrfs_commit_transaction(trans, root);
1473 mutex_unlock(&root->fs_info->fs_mutex);
1479 * This is somewhat expensive, updating the tree every time the
1480 * inode changes. But, it is most likely to find the inode in cache.
1481 * FIXME, needs more benchmarking...there are no reasons other than performance
1482 * to keep or drop this code.
1484 void btrfs_dirty_inode(struct inode *inode)
1486 struct btrfs_root *root = BTRFS_I(inode)->root;
1487 struct btrfs_trans_handle *trans;
1489 mutex_lock(&root->fs_info->fs_mutex);
1490 trans = btrfs_start_transaction(root, 1);
1491 btrfs_set_trans_block_group(trans, inode);
1492 btrfs_update_inode(trans, root, inode);
1493 btrfs_end_transaction(trans, root);
1494 mutex_unlock(&root->fs_info->fs_mutex);
1497 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1498 struct btrfs_root *root,
1499 const char *name, int name_len,
1502 struct btrfs_block_group_cache *group,
1505 struct inode *inode;
1506 struct btrfs_inode_item *inode_item;
1507 struct btrfs_key *location;
1508 struct btrfs_path *path;
1509 struct btrfs_inode_ref *ref;
1510 struct btrfs_key key[2];
1516 path = btrfs_alloc_path();
1519 inode = new_inode(root->fs_info->sb);
1521 return ERR_PTR(-ENOMEM);
1523 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1524 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1525 inode->i_mapping, GFP_NOFS);
1526 BTRFS_I(inode)->root = root;
1532 group = btrfs_find_block_group(root, group, 0, 0, owner);
1533 BTRFS_I(inode)->block_group = group;
1534 BTRFS_I(inode)->flags = 0;
1536 key[0].objectid = objectid;
1537 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1540 key[1].objectid = objectid;
1541 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1542 key[1].offset = ref_objectid;
1544 sizes[0] = sizeof(struct btrfs_inode_item);
1545 sizes[1] = name_len + sizeof(*ref);
1547 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1551 if (objectid > root->highest_inode)
1552 root->highest_inode = objectid;
1554 inode->i_uid = current->fsuid;
1555 inode->i_gid = current->fsgid;
1556 inode->i_mode = mode;
1557 inode->i_ino = objectid;
1558 inode->i_blocks = 0;
1559 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1560 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1561 struct btrfs_inode_item);
1562 fill_inode_item(path->nodes[0], inode_item, inode);
1564 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1565 struct btrfs_inode_ref);
1566 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1567 ptr = (unsigned long)(ref + 1);
1568 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1570 btrfs_mark_buffer_dirty(path->nodes[0]);
1571 btrfs_free_path(path);
1573 location = &BTRFS_I(inode)->location;
1574 location->objectid = objectid;
1575 location->offset = 0;
1576 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1578 insert_inode_hash(inode);
1581 btrfs_free_path(path);
1582 return ERR_PTR(ret);
1585 static inline u8 btrfs_inode_type(struct inode *inode)
1587 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1590 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1591 struct dentry *dentry, struct inode *inode,
1595 struct btrfs_key key;
1596 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1597 struct inode *parent_inode;
1599 key.objectid = inode->i_ino;
1600 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1603 ret = btrfs_insert_dir_item(trans, root,
1604 dentry->d_name.name, dentry->d_name.len,
1605 dentry->d_parent->d_inode->i_ino,
1606 &key, btrfs_inode_type(inode));
1609 ret = btrfs_insert_inode_ref(trans, root,
1610 dentry->d_name.name,
1613 dentry->d_parent->d_inode->i_ino);
1615 parent_inode = dentry->d_parent->d_inode;
1616 parent_inode->i_size += dentry->d_name.len * 2;
1617 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1618 ret = btrfs_update_inode(trans, root,
1619 dentry->d_parent->d_inode);
1624 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1625 struct dentry *dentry, struct inode *inode,
1628 int err = btrfs_add_link(trans, dentry, inode, backref);
1630 d_instantiate(dentry, inode);
1638 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1639 int mode, dev_t rdev)
1641 struct btrfs_trans_handle *trans;
1642 struct btrfs_root *root = BTRFS_I(dir)->root;
1643 struct inode *inode = NULL;
1647 unsigned long nr = 0;
1649 if (!new_valid_dev(rdev))
1652 mutex_lock(&root->fs_info->fs_mutex);
1653 err = btrfs_check_free_space(root, 1, 0);
1657 trans = btrfs_start_transaction(root, 1);
1658 btrfs_set_trans_block_group(trans, dir);
1660 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1666 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1668 dentry->d_parent->d_inode->i_ino, objectid,
1669 BTRFS_I(dir)->block_group, mode);
1670 err = PTR_ERR(inode);
1674 btrfs_set_trans_block_group(trans, inode);
1675 err = btrfs_add_nondir(trans, dentry, inode, 0);
1679 inode->i_op = &btrfs_special_inode_operations;
1680 init_special_inode(inode, inode->i_mode, rdev);
1681 btrfs_update_inode(trans, root, inode);
1683 dir->i_sb->s_dirt = 1;
1684 btrfs_update_inode_block_group(trans, inode);
1685 btrfs_update_inode_block_group(trans, dir);
1687 nr = trans->blocks_used;
1688 btrfs_end_transaction(trans, root);
1690 mutex_unlock(&root->fs_info->fs_mutex);
1693 inode_dec_link_count(inode);
1696 btrfs_btree_balance_dirty(root, nr);
1697 btrfs_throttle(root);
1701 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1702 int mode, struct nameidata *nd)
1704 struct btrfs_trans_handle *trans;
1705 struct btrfs_root *root = BTRFS_I(dir)->root;
1706 struct inode *inode = NULL;
1709 unsigned long nr = 0;
1712 mutex_lock(&root->fs_info->fs_mutex);
1713 err = btrfs_check_free_space(root, 1, 0);
1716 trans = btrfs_start_transaction(root, 1);
1717 btrfs_set_trans_block_group(trans, dir);
1719 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1725 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1727 dentry->d_parent->d_inode->i_ino,
1728 objectid, BTRFS_I(dir)->block_group, mode);
1729 err = PTR_ERR(inode);
1733 btrfs_set_trans_block_group(trans, inode);
1734 err = btrfs_add_nondir(trans, dentry, inode, 0);
1738 inode->i_mapping->a_ops = &btrfs_aops;
1739 inode->i_fop = &btrfs_file_operations;
1740 inode->i_op = &btrfs_file_inode_operations;
1741 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1742 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1743 inode->i_mapping, GFP_NOFS);
1744 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1746 dir->i_sb->s_dirt = 1;
1747 btrfs_update_inode_block_group(trans, inode);
1748 btrfs_update_inode_block_group(trans, dir);
1750 nr = trans->blocks_used;
1751 btrfs_end_transaction(trans, root);
1753 mutex_unlock(&root->fs_info->fs_mutex);
1756 inode_dec_link_count(inode);
1759 btrfs_btree_balance_dirty(root, nr);
1760 btrfs_throttle(root);
1764 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1765 struct dentry *dentry)
1767 struct btrfs_trans_handle *trans;
1768 struct btrfs_root *root = BTRFS_I(dir)->root;
1769 struct inode *inode = old_dentry->d_inode;
1770 unsigned long nr = 0;
1774 if (inode->i_nlink == 0)
1777 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1782 mutex_lock(&root->fs_info->fs_mutex);
1783 err = btrfs_check_free_space(root, 1, 0);
1786 trans = btrfs_start_transaction(root, 1);
1788 btrfs_set_trans_block_group(trans, dir);
1789 atomic_inc(&inode->i_count);
1790 err = btrfs_add_nondir(trans, dentry, inode, 1);
1795 dir->i_sb->s_dirt = 1;
1796 btrfs_update_inode_block_group(trans, dir);
1797 err = btrfs_update_inode(trans, root, inode);
1802 nr = trans->blocks_used;
1803 btrfs_end_transaction(trans, root);
1805 mutex_unlock(&root->fs_info->fs_mutex);
1808 inode_dec_link_count(inode);
1811 btrfs_btree_balance_dirty(root, nr);
1812 btrfs_throttle(root);
1816 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1818 struct inode *inode;
1819 struct btrfs_trans_handle *trans;
1820 struct btrfs_root *root = BTRFS_I(dir)->root;
1822 int drop_on_err = 0;
1824 unsigned long nr = 1;
1826 mutex_lock(&root->fs_info->fs_mutex);
1827 err = btrfs_check_free_space(root, 1, 0);
1831 trans = btrfs_start_transaction(root, 1);
1832 btrfs_set_trans_block_group(trans, dir);
1834 if (IS_ERR(trans)) {
1835 err = PTR_ERR(trans);
1839 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1845 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1847 dentry->d_parent->d_inode->i_ino, objectid,
1848 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1849 if (IS_ERR(inode)) {
1850 err = PTR_ERR(inode);
1855 inode->i_op = &btrfs_dir_inode_operations;
1856 inode->i_fop = &btrfs_dir_file_operations;
1857 btrfs_set_trans_block_group(trans, inode);
1860 err = btrfs_update_inode(trans, root, inode);
1864 err = btrfs_add_link(trans, dentry, inode, 0);
1868 d_instantiate(dentry, inode);
1870 dir->i_sb->s_dirt = 1;
1871 btrfs_update_inode_block_group(trans, inode);
1872 btrfs_update_inode_block_group(trans, dir);
1875 nr = trans->blocks_used;
1876 btrfs_end_transaction(trans, root);
1879 mutex_unlock(&root->fs_info->fs_mutex);
1882 btrfs_btree_balance_dirty(root, nr);
1883 btrfs_throttle(root);
1887 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1888 size_t pg_offset, u64 start, u64 len,
1894 u64 extent_start = 0;
1896 u64 objectid = inode->i_ino;
1898 struct btrfs_path *path;
1899 struct btrfs_root *root = BTRFS_I(inode)->root;
1900 struct btrfs_file_extent_item *item;
1901 struct extent_buffer *leaf;
1902 struct btrfs_key found_key;
1903 struct extent_map *em = NULL;
1904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1906 struct btrfs_trans_handle *trans = NULL;
1908 path = btrfs_alloc_path();
1910 mutex_lock(&root->fs_info->fs_mutex);
1913 spin_lock(&em_tree->lock);
1914 em = lookup_extent_mapping(em_tree, start, len);
1915 spin_unlock(&em_tree->lock);
1918 if (em->start > start) {
1919 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1920 start, len, em->start, em->len);
1923 if (em->block_start == EXTENT_MAP_INLINE && page)
1924 free_extent_map(em);
1928 em = alloc_extent_map(GFP_NOFS);
1934 em->start = EXTENT_MAP_HOLE;
1936 em->bdev = inode->i_sb->s_bdev;
1937 ret = btrfs_lookup_file_extent(trans, root, path,
1938 objectid, start, trans != NULL);
1945 if (path->slots[0] == 0)
1950 leaf = path->nodes[0];
1951 item = btrfs_item_ptr(leaf, path->slots[0],
1952 struct btrfs_file_extent_item);
1953 /* are we inside the extent that was found? */
1954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1955 found_type = btrfs_key_type(&found_key);
1956 if (found_key.objectid != objectid ||
1957 found_type != BTRFS_EXTENT_DATA_KEY) {
1961 found_type = btrfs_file_extent_type(leaf, item);
1962 extent_start = found_key.offset;
1963 if (found_type == BTRFS_FILE_EXTENT_REG) {
1964 extent_end = extent_start +
1965 btrfs_file_extent_num_bytes(leaf, item);
1967 if (start < extent_start || start >= extent_end) {
1969 if (start < extent_start) {
1970 if (start + len <= extent_start)
1972 em->len = extent_end - extent_start;
1978 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1980 em->start = extent_start;
1981 em->len = extent_end - extent_start;
1982 em->block_start = EXTENT_MAP_HOLE;
1985 bytenr += btrfs_file_extent_offset(leaf, item);
1986 em->block_start = bytenr;
1987 em->start = extent_start;
1988 em->len = extent_end - extent_start;
1990 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1995 size_t extent_offset;
1998 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2000 extent_end = (extent_start + size + root->sectorsize - 1) &
2001 ~((u64)root->sectorsize - 1);
2002 if (start < extent_start || start >= extent_end) {
2004 if (start < extent_start) {
2005 if (start + len <= extent_start)
2007 em->len = extent_end - extent_start;
2013 em->block_start = EXTENT_MAP_INLINE;
2016 em->start = extent_start;
2021 page_start = page_offset(page) + pg_offset;
2022 extent_offset = page_start - extent_start;
2023 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2024 size - extent_offset);
2025 em->start = extent_start + extent_offset;
2026 em->len = (copy_size + root->sectorsize - 1) &
2027 ~((u64)root->sectorsize - 1);
2029 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2030 if (create == 0 && !PageUptodate(page)) {
2031 read_extent_buffer(leaf, map + pg_offset, ptr,
2033 flush_dcache_page(page);
2034 } else if (create && PageUptodate(page)) {
2037 free_extent_map(em);
2039 btrfs_release_path(root, path);
2040 trans = btrfs_start_transaction(root, 1);
2043 write_extent_buffer(leaf, map + pg_offset, ptr,
2045 btrfs_mark_buffer_dirty(leaf);
2048 set_extent_uptodate(io_tree, em->start,
2049 extent_map_end(em) - 1, GFP_NOFS);
2052 printk("unkknown found_type %d\n", found_type);
2059 em->block_start = EXTENT_MAP_HOLE;
2061 btrfs_release_path(root, path);
2062 if (em->start > start || extent_map_end(em) <= start) {
2063 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2069 spin_lock(&em_tree->lock);
2070 ret = add_extent_mapping(em_tree, em);
2071 if (ret == -EEXIST) {
2072 free_extent_map(em);
2073 em = lookup_extent_mapping(em_tree, start, len);
2076 printk("failing to insert %Lu %Lu\n", start, len);
2079 spin_unlock(&em_tree->lock);
2081 btrfs_free_path(path);
2083 ret = btrfs_end_transaction(trans, root);
2087 mutex_unlock(&root->fs_info->fs_mutex);
2089 free_extent_map(em);
2091 return ERR_PTR(err);
2096 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2098 return extent_bmap(mapping, iblock, btrfs_get_extent);
2101 int btrfs_readpage(struct file *file, struct page *page)
2103 struct extent_io_tree *tree;
2104 tree = &BTRFS_I(page->mapping->host)->io_tree;
2105 return extent_read_full_page(tree, page, btrfs_get_extent);
2108 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2110 struct extent_io_tree *tree;
2113 if (current->flags & PF_MEMALLOC) {
2114 redirty_page_for_writepage(wbc, page);
2118 tree = &BTRFS_I(page->mapping->host)->io_tree;
2119 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2122 static int btrfs_writepages(struct address_space *mapping,
2123 struct writeback_control *wbc)
2125 struct extent_io_tree *tree;
2126 tree = &BTRFS_I(mapping->host)->io_tree;
2127 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2131 btrfs_readpages(struct file *file, struct address_space *mapping,
2132 struct list_head *pages, unsigned nr_pages)
2134 struct extent_io_tree *tree;
2135 tree = &BTRFS_I(mapping->host)->io_tree;
2136 return extent_readpages(tree, mapping, pages, nr_pages,
2140 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2142 struct extent_io_tree *tree;
2143 struct extent_map_tree *map;
2146 tree = &BTRFS_I(page->mapping->host)->io_tree;
2147 map = &BTRFS_I(page->mapping->host)->extent_tree;
2148 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2150 ClearPagePrivate(page);
2151 set_page_private(page, 0);
2152 page_cache_release(page);
2157 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2159 struct extent_io_tree *tree;
2161 tree = &BTRFS_I(page->mapping->host)->io_tree;
2162 extent_invalidatepage(tree, page, offset);
2163 btrfs_releasepage(page, GFP_NOFS);
2167 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2168 * called from a page fault handler when a page is first dirtied. Hence we must
2169 * be careful to check for EOF conditions here. We set the page up correctly
2170 * for a written page which means we get ENOSPC checking when writing into
2171 * holes and correct delalloc and unwritten extent mapping on filesystems that
2172 * support these features.
2174 * We are not allowed to take the i_mutex here so we have to play games to
2175 * protect against truncate races as the page could now be beyond EOF. Because
2176 * vmtruncate() writes the inode size before removing pages, once we have the
2177 * page lock we can determine safely if the page is beyond EOF. If it is not
2178 * beyond EOF, then the page is guaranteed safe against truncation until we
2181 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2183 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2184 struct btrfs_root *root = BTRFS_I(inode)->root;
2190 mutex_lock(&root->fs_info->fs_mutex);
2191 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2192 mutex_unlock(&root->fs_info->fs_mutex);
2199 wait_on_page_writeback(page);
2200 size = i_size_read(inode);
2201 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2203 if ((page->mapping != inode->i_mapping) ||
2204 (page_start > size)) {
2205 /* page got truncated out from underneath us */
2209 /* page is wholly or partially inside EOF */
2210 if (page_start + PAGE_CACHE_SIZE > size)
2211 end = size & ~PAGE_CACHE_MASK;
2213 end = PAGE_CACHE_SIZE;
2215 ret = btrfs_cow_one_page(inode, page, end);
2223 static void btrfs_truncate(struct inode *inode)
2225 struct btrfs_root *root = BTRFS_I(inode)->root;
2227 struct btrfs_trans_handle *trans;
2230 if (!S_ISREG(inode->i_mode))
2232 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2235 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2237 mutex_lock(&root->fs_info->fs_mutex);
2238 trans = btrfs_start_transaction(root, 1);
2239 btrfs_set_trans_block_group(trans, inode);
2241 /* FIXME, add redo link to tree so we don't leak on crash */
2242 ret = btrfs_truncate_in_trans(trans, root, inode,
2243 BTRFS_EXTENT_DATA_KEY);
2244 btrfs_update_inode(trans, root, inode);
2245 nr = trans->blocks_used;
2247 ret = btrfs_end_transaction(trans, root);
2249 mutex_unlock(&root->fs_info->fs_mutex);
2250 btrfs_btree_balance_dirty(root, nr);
2251 btrfs_throttle(root);
2254 static int noinline create_subvol(struct btrfs_root *root, char *name,
2257 struct btrfs_trans_handle *trans;
2258 struct btrfs_key key;
2259 struct btrfs_root_item root_item;
2260 struct btrfs_inode_item *inode_item;
2261 struct extent_buffer *leaf;
2262 struct btrfs_root *new_root = root;
2263 struct inode *inode;
2268 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2269 unsigned long nr = 1;
2271 mutex_lock(&root->fs_info->fs_mutex);
2272 ret = btrfs_check_free_space(root, 1, 0);
2276 trans = btrfs_start_transaction(root, 1);
2279 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2284 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2285 objectid, trans->transid, 0, 0,
2288 return PTR_ERR(leaf);
2290 btrfs_set_header_nritems(leaf, 0);
2291 btrfs_set_header_level(leaf, 0);
2292 btrfs_set_header_bytenr(leaf, leaf->start);
2293 btrfs_set_header_generation(leaf, trans->transid);
2294 btrfs_set_header_owner(leaf, objectid);
2296 write_extent_buffer(leaf, root->fs_info->fsid,
2297 (unsigned long)btrfs_header_fsid(leaf),
2299 btrfs_mark_buffer_dirty(leaf);
2301 inode_item = &root_item.inode;
2302 memset(inode_item, 0, sizeof(*inode_item));
2303 inode_item->generation = cpu_to_le64(1);
2304 inode_item->size = cpu_to_le64(3);
2305 inode_item->nlink = cpu_to_le32(1);
2306 inode_item->nblocks = cpu_to_le64(1);
2307 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2309 btrfs_set_root_bytenr(&root_item, leaf->start);
2310 btrfs_set_root_level(&root_item, 0);
2311 btrfs_set_root_refs(&root_item, 1);
2312 btrfs_set_root_used(&root_item, 0);
2314 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2315 root_item.drop_level = 0;
2317 free_extent_buffer(leaf);
2320 btrfs_set_root_dirid(&root_item, new_dirid);
2322 key.objectid = objectid;
2324 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2325 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2331 * insert the directory item
2333 key.offset = (u64)-1;
2334 dir = root->fs_info->sb->s_root->d_inode;
2335 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2336 name, namelen, dir->i_ino, &key,
2341 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2342 name, namelen, objectid,
2343 root->fs_info->sb->s_root->d_inode->i_ino);
2347 ret = btrfs_commit_transaction(trans, root);
2351 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2354 trans = btrfs_start_transaction(new_root, 1);
2357 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2359 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2362 inode->i_op = &btrfs_dir_inode_operations;
2363 inode->i_fop = &btrfs_dir_file_operations;
2364 new_root->inode = inode;
2366 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2370 ret = btrfs_update_inode(trans, new_root, inode);
2374 nr = trans->blocks_used;
2375 err = btrfs_commit_transaction(trans, new_root);
2379 mutex_unlock(&root->fs_info->fs_mutex);
2380 btrfs_btree_balance_dirty(root, nr);
2381 btrfs_throttle(root);
2385 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2387 struct btrfs_pending_snapshot *pending_snapshot;
2388 struct btrfs_trans_handle *trans;
2391 unsigned long nr = 0;
2393 if (!root->ref_cows)
2396 mutex_lock(&root->fs_info->fs_mutex);
2397 ret = btrfs_check_free_space(root, 1, 0);
2401 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2402 if (!pending_snapshot) {
2406 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2407 if (!pending_snapshot->name) {
2409 kfree(pending_snapshot);
2412 memcpy(pending_snapshot->name, name, namelen);
2413 pending_snapshot->name[namelen] = '\0';
2414 trans = btrfs_start_transaction(root, 1);
2416 pending_snapshot->root = root;
2417 list_add(&pending_snapshot->list,
2418 &trans->transaction->pending_snapshots);
2419 ret = btrfs_update_inode(trans, root, root->inode);
2420 err = btrfs_commit_transaction(trans, root);
2423 mutex_unlock(&root->fs_info->fs_mutex);
2424 btrfs_btree_balance_dirty(root, nr);
2425 btrfs_throttle(root);
2429 unsigned long btrfs_force_ra(struct address_space *mapping,
2430 struct file_ra_state *ra, struct file *file,
2431 pgoff_t offset, pgoff_t last_index)
2435 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2436 req_size = last_index - offset + 1;
2437 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2440 req_size = min(last_index - offset + 1, (pgoff_t)128);
2441 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2442 return offset + req_size;
2446 int btrfs_defrag_file(struct file *file) {
2447 struct inode *inode = fdentry(file)->d_inode;
2448 struct btrfs_root *root = BTRFS_I(inode)->root;
2449 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2451 unsigned long last_index;
2452 unsigned long ra_index = 0;
2458 mutex_lock(&root->fs_info->fs_mutex);
2459 ret = btrfs_check_free_space(root, inode->i_size, 0);
2460 mutex_unlock(&root->fs_info->fs_mutex);
2464 mutex_lock(&inode->i_mutex);
2465 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2466 for (i = 0; i <= last_index; i++) {
2467 if (i == ra_index) {
2468 ra_index = btrfs_force_ra(inode->i_mapping,
2470 file, ra_index, last_index);
2472 page = grab_cache_page(inode->i_mapping, i);
2475 if (!PageUptodate(page)) {
2476 btrfs_readpage(NULL, page);
2478 if (!PageUptodate(page)) {
2480 page_cache_release(page);
2484 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2485 page_end = page_start + PAGE_CACHE_SIZE - 1;
2487 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2488 set_extent_delalloc(io_tree, page_start,
2489 page_end, GFP_NOFS);
2491 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2492 set_page_dirty(page);
2494 page_cache_release(page);
2495 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2499 mutex_unlock(&inode->i_mutex);
2503 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2507 struct btrfs_ioctl_vol_args *vol_args;
2508 struct btrfs_trans_handle *trans;
2514 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2519 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2523 namelen = strlen(vol_args->name);
2524 if (namelen > BTRFS_VOL_NAME_MAX) {
2529 sizestr = vol_args->name;
2530 if (!strcmp(sizestr, "max"))
2531 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2533 if (sizestr[0] == '-') {
2536 } else if (sizestr[0] == '+') {
2540 new_size = btrfs_parse_size(sizestr);
2541 if (new_size == 0) {
2547 mutex_lock(&root->fs_info->fs_mutex);
2548 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2551 if (new_size > old_size) {
2555 new_size = old_size - new_size;
2556 } else if (mod > 0) {
2557 new_size = old_size + new_size;
2560 if (new_size < 256 * 1024 * 1024) {
2564 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2569 do_div(new_size, root->sectorsize);
2570 new_size *= root->sectorsize;
2572 printk("new size is %Lu\n", new_size);
2573 if (new_size > old_size) {
2574 trans = btrfs_start_transaction(root, 1);
2575 ret = btrfs_grow_extent_tree(trans, root, new_size);
2576 btrfs_commit_transaction(trans, root);
2578 ret = btrfs_shrink_extent_tree(root, new_size);
2582 mutex_unlock(&root->fs_info->fs_mutex);
2588 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2591 struct btrfs_ioctl_vol_args *vol_args;
2592 struct btrfs_dir_item *di;
2593 struct btrfs_path *path;
2598 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2603 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2608 namelen = strlen(vol_args->name);
2609 if (namelen > BTRFS_VOL_NAME_MAX) {
2613 if (strchr(vol_args->name, '/')) {
2618 path = btrfs_alloc_path();
2624 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2625 mutex_lock(&root->fs_info->fs_mutex);
2626 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2628 vol_args->name, namelen, 0);
2629 mutex_unlock(&root->fs_info->fs_mutex);
2630 btrfs_free_path(path);
2632 if (di && !IS_ERR(di)) {
2642 if (root == root->fs_info->tree_root)
2643 ret = create_subvol(root, vol_args->name, namelen);
2645 ret = create_snapshot(root, vol_args->name, namelen);
2651 static int btrfs_ioctl_defrag(struct file *file)
2653 struct inode *inode = fdentry(file)->d_inode;
2654 struct btrfs_root *root = BTRFS_I(inode)->root;
2656 switch (inode->i_mode & S_IFMT) {
2658 mutex_lock(&root->fs_info->fs_mutex);
2659 btrfs_defrag_root(root, 0);
2660 btrfs_defrag_root(root->fs_info->extent_root, 0);
2661 mutex_unlock(&root->fs_info->fs_mutex);
2664 btrfs_defrag_file(file);
2671 long btrfs_ioctl(struct file *file, unsigned int
2672 cmd, unsigned long arg)
2674 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2677 case BTRFS_IOC_SNAP_CREATE:
2678 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2679 case BTRFS_IOC_DEFRAG:
2680 return btrfs_ioctl_defrag(file);
2681 case BTRFS_IOC_RESIZE:
2682 return btrfs_ioctl_resize(root, (void __user *)arg);
2689 * Called inside transaction, so use GFP_NOFS
2691 struct inode *btrfs_alloc_inode(struct super_block *sb)
2693 struct btrfs_inode *ei;
2695 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2699 ei->ordered_trans = 0;
2700 return &ei->vfs_inode;
2703 void btrfs_destroy_inode(struct inode *inode)
2705 WARN_ON(!list_empty(&inode->i_dentry));
2706 WARN_ON(inode->i_data.nrpages);
2708 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2709 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2712 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2713 static void init_once(struct kmem_cache * cachep, void *foo)
2715 static void init_once(void * foo, struct kmem_cache * cachep,
2716 unsigned long flags)
2719 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2721 inode_init_once(&ei->vfs_inode);
2724 void btrfs_destroy_cachep(void)
2726 if (btrfs_inode_cachep)
2727 kmem_cache_destroy(btrfs_inode_cachep);
2728 if (btrfs_trans_handle_cachep)
2729 kmem_cache_destroy(btrfs_trans_handle_cachep);
2730 if (btrfs_transaction_cachep)
2731 kmem_cache_destroy(btrfs_transaction_cachep);
2732 if (btrfs_bit_radix_cachep)
2733 kmem_cache_destroy(btrfs_bit_radix_cachep);
2734 if (btrfs_path_cachep)
2735 kmem_cache_destroy(btrfs_path_cachep);
2738 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2739 unsigned long extra_flags,
2740 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2741 void (*ctor)(struct kmem_cache *, void *)
2743 void (*ctor)(void *, struct kmem_cache *,
2748 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2749 SLAB_MEM_SPREAD | extra_flags), ctor
2750 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2756 int btrfs_init_cachep(void)
2758 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2759 sizeof(struct btrfs_inode),
2761 if (!btrfs_inode_cachep)
2763 btrfs_trans_handle_cachep =
2764 btrfs_cache_create("btrfs_trans_handle_cache",
2765 sizeof(struct btrfs_trans_handle),
2767 if (!btrfs_trans_handle_cachep)
2769 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2770 sizeof(struct btrfs_transaction),
2772 if (!btrfs_transaction_cachep)
2774 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2775 sizeof(struct btrfs_path),
2777 if (!btrfs_path_cachep)
2779 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2780 SLAB_DESTROY_BY_RCU, NULL);
2781 if (!btrfs_bit_radix_cachep)
2785 btrfs_destroy_cachep();
2789 static int btrfs_getattr(struct vfsmount *mnt,
2790 struct dentry *dentry, struct kstat *stat)
2792 struct inode *inode = dentry->d_inode;
2793 generic_fillattr(inode, stat);
2794 stat->blksize = PAGE_CACHE_SIZE;
2798 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2799 struct inode * new_dir,struct dentry *new_dentry)
2801 struct btrfs_trans_handle *trans;
2802 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2803 struct inode *new_inode = new_dentry->d_inode;
2804 struct inode *old_inode = old_dentry->d_inode;
2805 struct timespec ctime = CURRENT_TIME;
2806 struct btrfs_path *path;
2809 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2810 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2814 mutex_lock(&root->fs_info->fs_mutex);
2815 ret = btrfs_check_free_space(root, 1, 0);
2819 trans = btrfs_start_transaction(root, 1);
2821 btrfs_set_trans_block_group(trans, new_dir);
2822 path = btrfs_alloc_path();
2828 old_dentry->d_inode->i_nlink++;
2829 old_dir->i_ctime = old_dir->i_mtime = ctime;
2830 new_dir->i_ctime = new_dir->i_mtime = ctime;
2831 old_inode->i_ctime = ctime;
2833 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2838 new_inode->i_ctime = CURRENT_TIME;
2839 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2843 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2848 btrfs_free_path(path);
2849 btrfs_end_transaction(trans, root);
2851 mutex_unlock(&root->fs_info->fs_mutex);
2855 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2856 const char *symname)
2858 struct btrfs_trans_handle *trans;
2859 struct btrfs_root *root = BTRFS_I(dir)->root;
2860 struct btrfs_path *path;
2861 struct btrfs_key key;
2862 struct inode *inode = NULL;
2869 struct btrfs_file_extent_item *ei;
2870 struct extent_buffer *leaf;
2871 unsigned long nr = 0;
2873 name_len = strlen(symname) + 1;
2874 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2875 return -ENAMETOOLONG;
2877 mutex_lock(&root->fs_info->fs_mutex);
2878 err = btrfs_check_free_space(root, 1, 0);
2882 trans = btrfs_start_transaction(root, 1);
2883 btrfs_set_trans_block_group(trans, dir);
2885 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2891 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2893 dentry->d_parent->d_inode->i_ino, objectid,
2894 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2895 err = PTR_ERR(inode);
2899 btrfs_set_trans_block_group(trans, inode);
2900 err = btrfs_add_nondir(trans, dentry, inode, 0);
2904 inode->i_mapping->a_ops = &btrfs_aops;
2905 inode->i_fop = &btrfs_file_operations;
2906 inode->i_op = &btrfs_file_inode_operations;
2907 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2908 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2909 inode->i_mapping, GFP_NOFS);
2910 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2912 dir->i_sb->s_dirt = 1;
2913 btrfs_update_inode_block_group(trans, inode);
2914 btrfs_update_inode_block_group(trans, dir);
2918 path = btrfs_alloc_path();
2920 key.objectid = inode->i_ino;
2922 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2923 datasize = btrfs_file_extent_calc_inline_size(name_len);
2924 err = btrfs_insert_empty_item(trans, root, path, &key,
2930 leaf = path->nodes[0];
2931 ei = btrfs_item_ptr(leaf, path->slots[0],
2932 struct btrfs_file_extent_item);
2933 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2934 btrfs_set_file_extent_type(leaf, ei,
2935 BTRFS_FILE_EXTENT_INLINE);
2936 ptr = btrfs_file_extent_inline_start(ei);
2937 write_extent_buffer(leaf, symname, ptr, name_len);
2938 btrfs_mark_buffer_dirty(leaf);
2939 btrfs_free_path(path);
2941 inode->i_op = &btrfs_symlink_inode_operations;
2942 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2943 inode->i_size = name_len - 1;
2944 err = btrfs_update_inode(trans, root, inode);
2949 nr = trans->blocks_used;
2950 btrfs_end_transaction(trans, root);
2952 mutex_unlock(&root->fs_info->fs_mutex);
2954 inode_dec_link_count(inode);
2957 btrfs_btree_balance_dirty(root, nr);
2958 btrfs_throttle(root);
2961 static int btrfs_permission(struct inode *inode, int mask,
2962 struct nameidata *nd)
2964 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2966 return generic_permission(inode, mask, NULL);
2969 static struct inode_operations btrfs_dir_inode_operations = {
2970 .lookup = btrfs_lookup,
2971 .create = btrfs_create,
2972 .unlink = btrfs_unlink,
2974 .mkdir = btrfs_mkdir,
2975 .rmdir = btrfs_rmdir,
2976 .rename = btrfs_rename,
2977 .symlink = btrfs_symlink,
2978 .setattr = btrfs_setattr,
2979 .mknod = btrfs_mknod,
2980 .setxattr = generic_setxattr,
2981 .getxattr = generic_getxattr,
2982 .listxattr = btrfs_listxattr,
2983 .removexattr = generic_removexattr,
2984 .permission = btrfs_permission,
2986 static struct inode_operations btrfs_dir_ro_inode_operations = {
2987 .lookup = btrfs_lookup,
2988 .permission = btrfs_permission,
2990 static struct file_operations btrfs_dir_file_operations = {
2991 .llseek = generic_file_llseek,
2992 .read = generic_read_dir,
2993 .readdir = btrfs_readdir,
2994 .unlocked_ioctl = btrfs_ioctl,
2995 #ifdef CONFIG_COMPAT
2996 .compat_ioctl = btrfs_ioctl,
3000 static struct extent_io_ops btrfs_extent_io_ops = {
3001 .fill_delalloc = run_delalloc_range,
3002 .writepage_io_hook = btrfs_writepage_io_hook,
3003 .readpage_io_hook = btrfs_readpage_io_hook,
3004 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3007 static struct address_space_operations btrfs_aops = {
3008 .readpage = btrfs_readpage,
3009 .writepage = btrfs_writepage,
3010 .writepages = btrfs_writepages,
3011 .readpages = btrfs_readpages,
3012 .sync_page = block_sync_page,
3014 .invalidatepage = btrfs_invalidatepage,
3015 .releasepage = btrfs_releasepage,
3016 .set_page_dirty = __set_page_dirty_nobuffers,
3019 static struct address_space_operations btrfs_symlink_aops = {
3020 .readpage = btrfs_readpage,
3021 .writepage = btrfs_writepage,
3022 .invalidatepage = btrfs_invalidatepage,
3023 .releasepage = btrfs_releasepage,
3026 static struct inode_operations btrfs_file_inode_operations = {
3027 .truncate = btrfs_truncate,
3028 .getattr = btrfs_getattr,
3029 .setattr = btrfs_setattr,
3030 .setxattr = generic_setxattr,
3031 .getxattr = generic_getxattr,
3032 .listxattr = btrfs_listxattr,
3033 .removexattr = generic_removexattr,
3034 .permission = btrfs_permission,
3036 static struct inode_operations btrfs_special_inode_operations = {
3037 .getattr = btrfs_getattr,
3038 .setattr = btrfs_setattr,
3039 .permission = btrfs_permission,
3041 static struct inode_operations btrfs_symlink_inode_operations = {
3042 .readlink = generic_readlink,
3043 .follow_link = page_follow_link_light,
3044 .put_link = page_put_link,
3045 .permission = btrfs_permission,