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/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
45 struct btrfs_iget_args {
47 struct btrfs_root *root;
50 static struct inode_operations btrfs_dir_inode_operations;
51 static struct inode_operations btrfs_symlink_inode_operations;
52 static struct inode_operations btrfs_dir_ro_inode_operations;
53 static struct inode_operations btrfs_special_inode_operations;
54 static struct inode_operations btrfs_file_inode_operations;
55 static struct address_space_operations btrfs_aops;
56 static struct address_space_operations btrfs_symlink_aops;
57 static struct file_operations btrfs_dir_file_operations;
58 static struct extent_io_ops btrfs_extent_io_ops;
60 static struct kmem_cache *btrfs_inode_cachep;
61 struct kmem_cache *btrfs_trans_handle_cachep;
62 struct kmem_cache *btrfs_transaction_cachep;
63 struct kmem_cache *btrfs_bit_radix_cachep;
64 struct kmem_cache *btrfs_path_cachep;
67 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
77 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
95 spin_unlock(&root->fs_info->delalloc_lock);
99 static int cow_file_range(struct inode *inode, u64 start, u64 end)
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
106 u64 blocksize = root->sectorsize;
107 u64 orig_start = start;
109 struct btrfs_key ins;
112 trans = btrfs_start_transaction(root, 1);
114 btrfs_set_trans_block_group(trans, inode);
116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
117 num_bytes = max(blocksize, num_bytes);
118 ret = btrfs_drop_extents(trans, root, inode,
119 start, start + num_bytes, start, &alloc_hint);
120 orig_num_bytes = num_bytes;
122 if (alloc_hint == EXTENT_MAP_INLINE)
125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
139 inode->i_blocks += ins.offset >> 9;
140 btrfs_check_file(root, inode);
141 num_bytes -= cur_alloc_size;
142 alloc_hint = ins.objectid + ins.offset;
143 start += cur_alloc_size;
145 btrfs_drop_extent_cache(inode, orig_start,
146 orig_start + orig_num_bytes - 1);
147 btrfs_add_ordered_inode(inode);
148 btrfs_update_inode(trans, root, inode);
150 btrfs_end_transaction(trans, root);
154 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
162 struct btrfs_root *root = BTRFS_I(inode)->root;
163 struct extent_buffer *leaf;
165 struct btrfs_path *path;
166 struct btrfs_file_extent_item *item;
169 struct btrfs_key found_key;
171 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
172 path = btrfs_alloc_path();
175 ret = btrfs_lookup_file_extent(NULL, root, path,
176 inode->i_ino, start, 0);
178 btrfs_free_path(path);
184 if (path->slots[0] == 0)
189 leaf = path->nodes[0];
190 item = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_file_extent_item);
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
195 found_type = btrfs_key_type(&found_key);
196 if (found_key.objectid != inode->i_ino ||
197 found_type != BTRFS_EXTENT_DATA_KEY) {
201 found_type = btrfs_file_extent_type(leaf, item);
202 extent_start = found_key.offset;
203 if (found_type == BTRFS_FILE_EXTENT_REG) {
204 u64 extent_num_bytes;
206 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
207 extent_end = extent_start + extent_num_bytes;
210 if (loops && start != extent_start)
213 if (start < extent_start || start >= extent_end)
216 cow_end = min(end, extent_end - 1);
217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
225 if (bytenr + extent_num_bytes > total_fs_bytes)
228 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
238 btrfs_free_path(path);
241 btrfs_release_path(root, path);
246 cow_file_range(inode, start, cow_end);
251 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
253 struct btrfs_root *root = BTRFS_I(inode)->root;
255 mutex_lock(&root->fs_info->fs_mutex);
256 if (btrfs_test_opt(root, NODATACOW) ||
257 btrfs_test_flag(inode, NODATACOW))
258 ret = run_delalloc_nocow(inode, start, end);
260 ret = cow_file_range(inode, start, end);
262 mutex_unlock(&root->fs_info->fs_mutex);
266 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
267 unsigned long old, unsigned long bits)
269 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 spin_lock(&root->fs_info->delalloc_lock);
272 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
273 root->fs_info->delalloc_bytes += end - start + 1;
274 spin_unlock(&root->fs_info->delalloc_lock);
279 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
280 unsigned long old, unsigned long bits)
282 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 spin_lock(&root->fs_info->delalloc_lock);
285 if (end - start + 1 > root->fs_info->delalloc_bytes) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end - start + 1, root->fs_info->delalloc_bytes);
288 root->fs_info->delalloc_bytes = 0;
289 BTRFS_I(inode)->delalloc_bytes = 0;
291 root->fs_info->delalloc_bytes -= end - start + 1;
292 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
294 spin_unlock(&root->fs_info->delalloc_lock);
299 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
300 size_t size, struct bio *bio)
302 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
303 struct btrfs_mapping_tree *map_tree;
304 struct btrfs_device *dev;
305 u64 logical = bio->bi_sector << 9;
309 struct bio_vec *bvec;
313 bio_for_each_segment(bvec, bio, i) {
314 length += bvec->bv_len;
316 map_tree = &root->fs_info->mapping_tree;
318 ret = btrfs_map_block(map_tree, logical, &physical, &map_length, &dev);
319 if (map_length < length + size) {
325 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct btrfs_trans_handle *trans;
335 if (btrfs_test_opt(root, NODATASUM) ||
336 btrfs_test_flag(inode, NODATASUM)) {
340 mutex_lock(&root->fs_info->fs_mutex);
341 trans = btrfs_start_transaction(root, 1);
342 btrfs_set_trans_block_group(trans, inode);
343 btrfs_csum_file_blocks(trans, root, inode, bio);
344 ret = btrfs_end_transaction(trans, root);
346 mutex_unlock(&root->fs_info->fs_mutex);
348 return btrfs_map_bio(root, rw, bio);
351 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
354 struct inode *inode = page->mapping->host;
355 struct btrfs_root *root = BTRFS_I(inode)->root;
356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
357 struct btrfs_csum_item *item;
358 struct btrfs_path *path = NULL;
360 if (btrfs_test_opt(root, NODATASUM) ||
361 btrfs_test_flag(inode, NODATASUM))
363 mutex_lock(&root->fs_info->fs_mutex);
364 path = btrfs_alloc_path();
365 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
368 /* a csum that isn't present is a preallocated region. */
369 if (ret == -ENOENT || ret == -EFBIG)
372 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
375 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
377 set_state_private(io_tree, start, csum);
380 btrfs_free_path(path);
381 mutex_unlock(&root->fs_info->fs_mutex);
385 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
386 struct extent_state *state)
388 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
389 struct inode *inode = page->mapping->host;
390 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
392 u64 private = ~(u32)0;
394 struct btrfs_root *root = BTRFS_I(inode)->root;
398 if (btrfs_test_opt(root, NODATASUM) ||
399 btrfs_test_flag(inode, NODATASUM))
401 if (state && state->start == start) {
402 private = state->private;
405 ret = get_state_private(io_tree, start, &private);
407 local_irq_save(flags);
408 kaddr = kmap_atomic(page, KM_IRQ0);
412 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
413 btrfs_csum_final(csum, (char *)&csum);
414 if (csum != private) {
417 kunmap_atomic(kaddr, KM_IRQ0);
418 local_irq_restore(flags);
422 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
423 page->mapping->host->i_ino, (unsigned long long)start, csum,
425 memset(kaddr + offset, 1, end - start + 1);
426 flush_dcache_page(page);
427 kunmap_atomic(kaddr, KM_IRQ0);
428 local_irq_restore(flags);
432 void btrfs_read_locked_inode(struct inode *inode)
434 struct btrfs_path *path;
435 struct extent_buffer *leaf;
436 struct btrfs_inode_item *inode_item;
437 struct btrfs_timespec *tspec;
438 struct btrfs_root *root = BTRFS_I(inode)->root;
439 struct btrfs_key location;
440 u64 alloc_group_block;
444 path = btrfs_alloc_path();
446 mutex_lock(&root->fs_info->fs_mutex);
447 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
449 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
453 leaf = path->nodes[0];
454 inode_item = btrfs_item_ptr(leaf, path->slots[0],
455 struct btrfs_inode_item);
457 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
458 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
459 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
460 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
461 inode->i_size = btrfs_inode_size(leaf, inode_item);
463 tspec = btrfs_inode_atime(inode_item);
464 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
465 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
467 tspec = btrfs_inode_mtime(inode_item);
468 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
469 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
471 tspec = btrfs_inode_ctime(inode_item);
472 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
473 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
475 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
476 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
478 rdev = btrfs_inode_rdev(leaf, inode_item);
480 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
481 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
483 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
484 if (!BTRFS_I(inode)->block_group) {
485 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
487 BTRFS_BLOCK_GROUP_METADATA, 0);
489 btrfs_free_path(path);
492 mutex_unlock(&root->fs_info->fs_mutex);
494 switch (inode->i_mode & S_IFMT) {
496 inode->i_mapping->a_ops = &btrfs_aops;
497 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
498 inode->i_fop = &btrfs_file_operations;
499 inode->i_op = &btrfs_file_inode_operations;
502 inode->i_fop = &btrfs_dir_file_operations;
503 if (root == root->fs_info->tree_root)
504 inode->i_op = &btrfs_dir_ro_inode_operations;
506 inode->i_op = &btrfs_dir_inode_operations;
509 inode->i_op = &btrfs_symlink_inode_operations;
510 inode->i_mapping->a_ops = &btrfs_symlink_aops;
513 init_special_inode(inode, inode->i_mode, rdev);
519 btrfs_release_path(root, path);
520 btrfs_free_path(path);
521 mutex_unlock(&root->fs_info->fs_mutex);
522 make_bad_inode(inode);
525 static void fill_inode_item(struct extent_buffer *leaf,
526 struct btrfs_inode_item *item,
529 btrfs_set_inode_uid(leaf, item, inode->i_uid);
530 btrfs_set_inode_gid(leaf, item, inode->i_gid);
531 btrfs_set_inode_size(leaf, item, inode->i_size);
532 btrfs_set_inode_mode(leaf, item, inode->i_mode);
533 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
535 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
536 inode->i_atime.tv_sec);
537 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
538 inode->i_atime.tv_nsec);
540 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
541 inode->i_mtime.tv_sec);
542 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
543 inode->i_mtime.tv_nsec);
545 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
546 inode->i_ctime.tv_sec);
547 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
548 inode->i_ctime.tv_nsec);
550 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
551 btrfs_set_inode_generation(leaf, item, inode->i_generation);
552 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
553 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
554 btrfs_set_inode_block_group(leaf, item,
555 BTRFS_I(inode)->block_group->key.objectid);
558 int btrfs_update_inode(struct btrfs_trans_handle *trans,
559 struct btrfs_root *root,
562 struct btrfs_inode_item *inode_item;
563 struct btrfs_path *path;
564 struct extent_buffer *leaf;
567 path = btrfs_alloc_path();
569 ret = btrfs_lookup_inode(trans, root, path,
570 &BTRFS_I(inode)->location, 1);
577 leaf = path->nodes[0];
578 inode_item = btrfs_item_ptr(leaf, path->slots[0],
579 struct btrfs_inode_item);
581 fill_inode_item(leaf, inode_item, inode);
582 btrfs_mark_buffer_dirty(leaf);
583 btrfs_set_inode_last_trans(trans, inode);
586 btrfs_release_path(root, path);
587 btrfs_free_path(path);
592 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
593 struct btrfs_root *root,
595 struct dentry *dentry)
597 struct btrfs_path *path;
598 const char *name = dentry->d_name.name;
599 int name_len = dentry->d_name.len;
601 struct extent_buffer *leaf;
602 struct btrfs_dir_item *di;
603 struct btrfs_key key;
605 path = btrfs_alloc_path();
611 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
621 leaf = path->nodes[0];
622 btrfs_dir_item_key_to_cpu(leaf, di, &key);
623 ret = btrfs_delete_one_dir_name(trans, root, path, di);
626 btrfs_release_path(root, path);
628 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
629 key.objectid, name, name_len, -1);
638 ret = btrfs_delete_one_dir_name(trans, root, path, di);
640 dentry->d_inode->i_ctime = dir->i_ctime;
641 ret = btrfs_del_inode_ref(trans, root, name, name_len,
642 dentry->d_inode->i_ino,
643 dentry->d_parent->d_inode->i_ino);
645 printk("failed to delete reference to %.*s, "
646 "inode %lu parent %lu\n", name_len, name,
647 dentry->d_inode->i_ino,
648 dentry->d_parent->d_inode->i_ino);
651 btrfs_free_path(path);
653 dir->i_size -= name_len * 2;
654 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
655 btrfs_update_inode(trans, root, dir);
656 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
657 dentry->d_inode->i_nlink--;
659 drop_nlink(dentry->d_inode);
661 ret = btrfs_update_inode(trans, root, dentry->d_inode);
662 dir->i_sb->s_dirt = 1;
667 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
669 struct btrfs_root *root;
670 struct btrfs_trans_handle *trans;
671 struct inode *inode = dentry->d_inode;
673 unsigned long nr = 0;
675 root = BTRFS_I(dir)->root;
676 mutex_lock(&root->fs_info->fs_mutex);
678 ret = btrfs_check_free_space(root, 1, 1);
682 trans = btrfs_start_transaction(root, 1);
684 btrfs_set_trans_block_group(trans, dir);
685 ret = btrfs_unlink_trans(trans, root, dir, dentry);
686 nr = trans->blocks_used;
688 if (inode->i_nlink == 0) {
690 /* if the inode isn't linked anywhere,
691 * we don't need to worry about
694 found = btrfs_del_ordered_inode(inode);
696 atomic_dec(&inode->i_count);
700 btrfs_end_transaction(trans, root);
702 mutex_unlock(&root->fs_info->fs_mutex);
703 btrfs_btree_balance_dirty(root, nr);
704 btrfs_throttle(root);
708 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
710 struct inode *inode = dentry->d_inode;
713 struct btrfs_root *root = BTRFS_I(dir)->root;
714 struct btrfs_trans_handle *trans;
715 unsigned long nr = 0;
717 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
720 mutex_lock(&root->fs_info->fs_mutex);
721 ret = btrfs_check_free_space(root, 1, 1);
725 trans = btrfs_start_transaction(root, 1);
726 btrfs_set_trans_block_group(trans, dir);
728 /* now the directory is empty */
729 err = btrfs_unlink_trans(trans, root, dir, dentry);
734 nr = trans->blocks_used;
735 ret = btrfs_end_transaction(trans, root);
737 mutex_unlock(&root->fs_info->fs_mutex);
738 btrfs_btree_balance_dirty(root, nr);
739 btrfs_throttle(root);
747 * this can truncate away extent items, csum items and directory items.
748 * It starts at a high offset and removes keys until it can't find
749 * any higher than i_size.
751 * csum items that cross the new i_size are truncated to the new size
754 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
760 struct btrfs_path *path;
761 struct btrfs_key key;
762 struct btrfs_key found_key;
764 struct extent_buffer *leaf;
765 struct btrfs_file_extent_item *fi;
766 u64 extent_start = 0;
767 u64 extent_num_bytes = 0;
773 int pending_del_nr = 0;
774 int pending_del_slot = 0;
775 int extent_type = -1;
777 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
778 path = btrfs_alloc_path();
782 /* FIXME, add redo link to tree so we don't leak on crash */
783 key.objectid = inode->i_ino;
784 key.offset = (u64)-1;
787 btrfs_init_path(path);
789 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
794 BUG_ON(path->slots[0] == 0);
800 leaf = path->nodes[0];
801 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
802 found_type = btrfs_key_type(&found_key);
804 if (found_key.objectid != inode->i_ino)
807 if (found_type < min_type)
810 item_end = found_key.offset;
811 if (found_type == BTRFS_EXTENT_DATA_KEY) {
812 fi = btrfs_item_ptr(leaf, path->slots[0],
813 struct btrfs_file_extent_item);
814 extent_type = btrfs_file_extent_type(leaf, fi);
815 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
817 btrfs_file_extent_num_bytes(leaf, fi);
818 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
819 struct btrfs_item *item = btrfs_item_nr(leaf,
821 item_end += btrfs_file_extent_inline_len(leaf,
826 if (found_type == BTRFS_CSUM_ITEM_KEY) {
827 ret = btrfs_csum_truncate(trans, root, path,
831 if (item_end < inode->i_size) {
832 if (found_type == BTRFS_DIR_ITEM_KEY) {
833 found_type = BTRFS_INODE_ITEM_KEY;
834 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
835 found_type = BTRFS_CSUM_ITEM_KEY;
836 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
837 found_type = BTRFS_XATTR_ITEM_KEY;
838 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
839 found_type = BTRFS_INODE_REF_KEY;
840 } else if (found_type) {
845 btrfs_set_key_type(&key, found_type);
848 if (found_key.offset >= inode->i_size)
854 /* FIXME, shrink the extent if the ref count is only 1 */
855 if (found_type != BTRFS_EXTENT_DATA_KEY)
858 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
860 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
863 btrfs_file_extent_num_bytes(leaf, fi);
864 extent_num_bytes = inode->i_size -
865 found_key.offset + root->sectorsize - 1;
866 extent_num_bytes = extent_num_bytes &
867 ~((u64)root->sectorsize - 1);
868 btrfs_set_file_extent_num_bytes(leaf, fi,
870 num_dec = (orig_num_bytes -
872 if (extent_start != 0)
873 dec_i_blocks(inode, num_dec);
874 btrfs_mark_buffer_dirty(leaf);
877 btrfs_file_extent_disk_num_bytes(leaf,
879 /* FIXME blocksize != 4096 */
880 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
881 if (extent_start != 0) {
883 dec_i_blocks(inode, num_dec);
885 root_gen = btrfs_header_generation(leaf);
886 root_owner = btrfs_header_owner(leaf);
888 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
890 u32 newsize = inode->i_size - found_key.offset;
891 dec_i_blocks(inode, item_end + 1 -
892 found_key.offset - newsize);
894 btrfs_file_extent_calc_inline_size(newsize);
895 ret = btrfs_truncate_item(trans, root, path,
899 dec_i_blocks(inode, item_end + 1 -
905 if (!pending_del_nr) {
906 /* no pending yet, add ourselves */
907 pending_del_slot = path->slots[0];
909 } else if (pending_del_nr &&
910 path->slots[0] + 1 == pending_del_slot) {
911 /* hop on the pending chunk */
913 pending_del_slot = path->slots[0];
915 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
921 ret = btrfs_free_extent(trans, root, extent_start,
924 root_gen, inode->i_ino,
925 found_key.offset, 0);
929 if (path->slots[0] == 0) {
932 btrfs_release_path(root, path);
937 if (pending_del_nr &&
938 path->slots[0] + 1 != pending_del_slot) {
939 struct btrfs_key debug;
941 btrfs_item_key_to_cpu(path->nodes[0], &debug,
943 ret = btrfs_del_items(trans, root, path,
948 btrfs_release_path(root, path);
954 if (pending_del_nr) {
955 ret = btrfs_del_items(trans, root, path, pending_del_slot,
958 btrfs_release_path(root, path);
959 btrfs_free_path(path);
960 inode->i_sb->s_dirt = 1;
964 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
968 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
969 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
970 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
973 WARN_ON(!PageLocked(page));
974 set_page_extent_mapped(page);
976 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
977 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
980 if (zero_start != PAGE_CACHE_SIZE) {
982 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
983 flush_dcache_page(page);
986 set_page_dirty(page);
987 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
993 * taken from block_truncate_page, but does cow as it zeros out
994 * any bytes left in the last page in the file.
996 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
998 struct inode *inode = mapping->host;
999 struct btrfs_root *root = BTRFS_I(inode)->root;
1000 u32 blocksize = root->sectorsize;
1001 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1002 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1007 if ((offset & (blocksize - 1)) == 0)
1011 page = grab_cache_page(mapping, index);
1014 if (!PageUptodate(page)) {
1015 ret = btrfs_readpage(NULL, page);
1017 if (!PageUptodate(page)) {
1022 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1024 ret = btrfs_cow_one_page(inode, page, offset);
1027 page_cache_release(page);
1032 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1034 struct inode *inode = dentry->d_inode;
1037 err = inode_change_ok(inode, attr);
1041 if (S_ISREG(inode->i_mode) &&
1042 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1043 struct btrfs_trans_handle *trans;
1044 struct btrfs_root *root = BTRFS_I(inode)->root;
1045 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1047 u64 mask = root->sectorsize - 1;
1048 u64 hole_start = (inode->i_size + mask) & ~mask;
1049 u64 block_end = (attr->ia_size + mask) & ~mask;
1053 if (attr->ia_size <= hole_start)
1056 mutex_lock(&root->fs_info->fs_mutex);
1057 err = btrfs_check_free_space(root, 1, 0);
1058 mutex_unlock(&root->fs_info->fs_mutex);
1062 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1064 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1065 hole_size = block_end - hole_start;
1067 mutex_lock(&root->fs_info->fs_mutex);
1068 trans = btrfs_start_transaction(root, 1);
1069 btrfs_set_trans_block_group(trans, inode);
1070 err = btrfs_drop_extents(trans, root, inode,
1071 hole_start, block_end, hole_start,
1074 if (alloc_hint != EXTENT_MAP_INLINE) {
1075 err = btrfs_insert_file_extent(trans, root,
1079 btrfs_drop_extent_cache(inode, hole_start,
1081 btrfs_check_file(root, inode);
1083 btrfs_end_transaction(trans, root);
1084 mutex_unlock(&root->fs_info->fs_mutex);
1085 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1090 err = inode_setattr(inode, attr);
1095 void btrfs_put_inode(struct inode *inode)
1099 if (!BTRFS_I(inode)->ordered_trans) {
1103 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1104 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1107 ret = btrfs_del_ordered_inode(inode);
1109 atomic_dec(&inode->i_count);
1113 void btrfs_delete_inode(struct inode *inode)
1115 struct btrfs_trans_handle *trans;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
1120 truncate_inode_pages(&inode->i_data, 0);
1121 if (is_bad_inode(inode)) {
1126 mutex_lock(&root->fs_info->fs_mutex);
1127 trans = btrfs_start_transaction(root, 1);
1129 btrfs_set_trans_block_group(trans, inode);
1130 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1132 goto no_delete_lock;
1134 nr = trans->blocks_used;
1137 btrfs_end_transaction(trans, root);
1138 mutex_unlock(&root->fs_info->fs_mutex);
1139 btrfs_btree_balance_dirty(root, nr);
1140 btrfs_throttle(root);
1144 nr = trans->blocks_used;
1145 btrfs_end_transaction(trans, root);
1146 mutex_unlock(&root->fs_info->fs_mutex);
1147 btrfs_btree_balance_dirty(root, nr);
1148 btrfs_throttle(root);
1154 * this returns the key found in the dir entry in the location pointer.
1155 * If no dir entries were found, location->objectid is 0.
1157 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1158 struct btrfs_key *location)
1160 const char *name = dentry->d_name.name;
1161 int namelen = dentry->d_name.len;
1162 struct btrfs_dir_item *di;
1163 struct btrfs_path *path;
1164 struct btrfs_root *root = BTRFS_I(dir)->root;
1167 if (namelen == 1 && strcmp(name, ".") == 0) {
1168 location->objectid = dir->i_ino;
1169 location->type = BTRFS_INODE_ITEM_KEY;
1170 location->offset = 0;
1173 path = btrfs_alloc_path();
1176 if (namelen == 2 && strcmp(name, "..") == 0) {
1177 struct btrfs_key key;
1178 struct extent_buffer *leaf;
1182 key.objectid = dir->i_ino;
1183 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1185 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1189 leaf = path->nodes[0];
1190 slot = path->slots[0];
1191 nritems = btrfs_header_nritems(leaf);
1192 if (slot >= nritems)
1195 btrfs_item_key_to_cpu(leaf, &key, slot);
1196 if (key.objectid != dir->i_ino ||
1197 key.type != BTRFS_INODE_REF_KEY) {
1200 location->objectid = key.offset;
1201 location->type = BTRFS_INODE_ITEM_KEY;
1202 location->offset = 0;
1206 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1210 if (!di || IS_ERR(di)) {
1213 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1215 btrfs_free_path(path);
1218 location->objectid = 0;
1223 * when we hit a tree root in a directory, the btrfs part of the inode
1224 * needs to be changed to reflect the root directory of the tree root. This
1225 * is kind of like crossing a mount point.
1227 static int fixup_tree_root_location(struct btrfs_root *root,
1228 struct btrfs_key *location,
1229 struct btrfs_root **sub_root,
1230 struct dentry *dentry)
1232 struct btrfs_path *path;
1233 struct btrfs_root_item *ri;
1235 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1237 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1240 path = btrfs_alloc_path();
1242 mutex_lock(&root->fs_info->fs_mutex);
1244 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1245 dentry->d_name.name,
1246 dentry->d_name.len);
1247 if (IS_ERR(*sub_root))
1248 return PTR_ERR(*sub_root);
1250 ri = &(*sub_root)->root_item;
1251 location->objectid = btrfs_root_dirid(ri);
1252 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1253 location->offset = 0;
1255 btrfs_free_path(path);
1256 mutex_unlock(&root->fs_info->fs_mutex);
1260 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1262 struct btrfs_iget_args *args = p;
1263 inode->i_ino = args->ino;
1264 BTRFS_I(inode)->root = args->root;
1265 BTRFS_I(inode)->delalloc_bytes = 0;
1266 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1267 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1268 inode->i_mapping, GFP_NOFS);
1272 static int btrfs_find_actor(struct inode *inode, void *opaque)
1274 struct btrfs_iget_args *args = opaque;
1275 return (args->ino == inode->i_ino &&
1276 args->root == BTRFS_I(inode)->root);
1279 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1282 struct btrfs_iget_args args;
1283 args.ino = objectid;
1284 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1289 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1292 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1293 struct btrfs_root *root)
1295 struct inode *inode;
1296 struct btrfs_iget_args args;
1297 args.ino = objectid;
1300 inode = iget5_locked(s, objectid, btrfs_find_actor,
1301 btrfs_init_locked_inode,
1306 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1307 struct nameidata *nd)
1309 struct inode * inode;
1310 struct btrfs_inode *bi = BTRFS_I(dir);
1311 struct btrfs_root *root = bi->root;
1312 struct btrfs_root *sub_root = root;
1313 struct btrfs_key location;
1316 if (dentry->d_name.len > BTRFS_NAME_LEN)
1317 return ERR_PTR(-ENAMETOOLONG);
1319 mutex_lock(&root->fs_info->fs_mutex);
1320 ret = btrfs_inode_by_name(dir, dentry, &location);
1321 mutex_unlock(&root->fs_info->fs_mutex);
1324 return ERR_PTR(ret);
1327 if (location.objectid) {
1328 ret = fixup_tree_root_location(root, &location, &sub_root,
1331 return ERR_PTR(ret);
1333 return ERR_PTR(-ENOENT);
1334 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1337 return ERR_PTR(-EACCES);
1338 if (inode->i_state & I_NEW) {
1339 /* the inode and parent dir are two different roots */
1340 if (sub_root != root) {
1342 sub_root->inode = inode;
1344 BTRFS_I(inode)->root = sub_root;
1345 memcpy(&BTRFS_I(inode)->location, &location,
1347 btrfs_read_locked_inode(inode);
1348 unlock_new_inode(inode);
1351 return d_splice_alias(inode, dentry);
1354 static unsigned char btrfs_filetype_table[] = {
1355 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1358 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1360 struct inode *inode = filp->f_dentry->d_inode;
1361 struct btrfs_root *root = BTRFS_I(inode)->root;
1362 struct btrfs_item *item;
1363 struct btrfs_dir_item *di;
1364 struct btrfs_key key;
1365 struct btrfs_key found_key;
1366 struct btrfs_path *path;
1369 struct extent_buffer *leaf;
1372 unsigned char d_type;
1377 int key_type = BTRFS_DIR_INDEX_KEY;
1382 /* FIXME, use a real flag for deciding about the key type */
1383 if (root->fs_info->tree_root == root)
1384 key_type = BTRFS_DIR_ITEM_KEY;
1386 /* special case for "." */
1387 if (filp->f_pos == 0) {
1388 over = filldir(dirent, ".", 1,
1396 mutex_lock(&root->fs_info->fs_mutex);
1397 key.objectid = inode->i_ino;
1398 path = btrfs_alloc_path();
1401 /* special case for .., just use the back ref */
1402 if (filp->f_pos == 1) {
1403 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1405 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1407 leaf = path->nodes[0];
1408 slot = path->slots[0];
1409 nritems = btrfs_header_nritems(leaf);
1410 if (slot >= nritems) {
1411 btrfs_release_path(root, path);
1412 goto read_dir_items;
1414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1415 btrfs_release_path(root, path);
1416 if (found_key.objectid != key.objectid ||
1417 found_key.type != BTRFS_INODE_REF_KEY)
1418 goto read_dir_items;
1419 over = filldir(dirent, "..", 2,
1420 2, found_key.offset, DT_DIR);
1427 btrfs_set_key_type(&key, key_type);
1428 key.offset = filp->f_pos;
1430 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1435 leaf = path->nodes[0];
1436 nritems = btrfs_header_nritems(leaf);
1437 slot = path->slots[0];
1438 if (advance || slot >= nritems) {
1439 if (slot >= nritems -1) {
1440 ret = btrfs_next_leaf(root, path);
1443 leaf = path->nodes[0];
1444 nritems = btrfs_header_nritems(leaf);
1445 slot = path->slots[0];
1452 item = btrfs_item_nr(leaf, slot);
1453 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1455 if (found_key.objectid != key.objectid)
1457 if (btrfs_key_type(&found_key) != key_type)
1459 if (found_key.offset < filp->f_pos)
1462 filp->f_pos = found_key.offset;
1464 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1466 di_total = btrfs_item_size(leaf, item);
1467 while(di_cur < di_total) {
1468 struct btrfs_key location;
1470 name_len = btrfs_dir_name_len(leaf, di);
1471 if (name_len < 32) {
1472 name_ptr = tmp_name;
1474 name_ptr = kmalloc(name_len, GFP_NOFS);
1477 read_extent_buffer(leaf, name_ptr,
1478 (unsigned long)(di + 1), name_len);
1480 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1481 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1482 over = filldir(dirent, name_ptr, name_len,
1487 if (name_ptr != tmp_name)
1492 di_len = btrfs_dir_name_len(leaf, di) +
1493 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1495 di = (struct btrfs_dir_item *)((char *)di + di_len);
1498 if (key_type == BTRFS_DIR_INDEX_KEY)
1499 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1505 btrfs_release_path(root, path);
1506 btrfs_free_path(path);
1507 mutex_unlock(&root->fs_info->fs_mutex);
1511 int btrfs_write_inode(struct inode *inode, int wait)
1513 struct btrfs_root *root = BTRFS_I(inode)->root;
1514 struct btrfs_trans_handle *trans;
1518 mutex_lock(&root->fs_info->fs_mutex);
1519 trans = btrfs_start_transaction(root, 1);
1520 btrfs_set_trans_block_group(trans, inode);
1521 ret = btrfs_commit_transaction(trans, root);
1522 mutex_unlock(&root->fs_info->fs_mutex);
1528 * This is somewhat expensive, updating the tree every time the
1529 * inode changes. But, it is most likely to find the inode in cache.
1530 * FIXME, needs more benchmarking...there are no reasons other than performance
1531 * to keep or drop this code.
1533 void btrfs_dirty_inode(struct inode *inode)
1535 struct btrfs_root *root = BTRFS_I(inode)->root;
1536 struct btrfs_trans_handle *trans;
1538 mutex_lock(&root->fs_info->fs_mutex);
1539 trans = btrfs_start_transaction(root, 1);
1540 btrfs_set_trans_block_group(trans, inode);
1541 btrfs_update_inode(trans, root, inode);
1542 btrfs_end_transaction(trans, root);
1543 mutex_unlock(&root->fs_info->fs_mutex);
1546 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1547 struct btrfs_root *root,
1548 const char *name, int name_len,
1551 struct btrfs_block_group_cache *group,
1554 struct inode *inode;
1555 struct btrfs_inode_item *inode_item;
1556 struct btrfs_block_group_cache *new_inode_group;
1557 struct btrfs_key *location;
1558 struct btrfs_path *path;
1559 struct btrfs_inode_ref *ref;
1560 struct btrfs_key key[2];
1566 path = btrfs_alloc_path();
1569 inode = new_inode(root->fs_info->sb);
1571 return ERR_PTR(-ENOMEM);
1573 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1574 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1575 inode->i_mapping, GFP_NOFS);
1576 BTRFS_I(inode)->delalloc_bytes = 0;
1577 BTRFS_I(inode)->root = root;
1583 new_inode_group = btrfs_find_block_group(root, group, 0,
1584 BTRFS_BLOCK_GROUP_METADATA, owner);
1585 if (!new_inode_group) {
1586 printk("find_block group failed\n");
1587 new_inode_group = group;
1589 BTRFS_I(inode)->block_group = new_inode_group;
1590 BTRFS_I(inode)->flags = 0;
1592 key[0].objectid = objectid;
1593 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1596 key[1].objectid = objectid;
1597 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1598 key[1].offset = ref_objectid;
1600 sizes[0] = sizeof(struct btrfs_inode_item);
1601 sizes[1] = name_len + sizeof(*ref);
1603 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1607 if (objectid > root->highest_inode)
1608 root->highest_inode = objectid;
1610 inode->i_uid = current->fsuid;
1611 inode->i_gid = current->fsgid;
1612 inode->i_mode = mode;
1613 inode->i_ino = objectid;
1614 inode->i_blocks = 0;
1615 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1616 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1617 struct btrfs_inode_item);
1618 fill_inode_item(path->nodes[0], inode_item, inode);
1620 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1621 struct btrfs_inode_ref);
1622 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1623 ptr = (unsigned long)(ref + 1);
1624 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1626 btrfs_mark_buffer_dirty(path->nodes[0]);
1627 btrfs_free_path(path);
1629 location = &BTRFS_I(inode)->location;
1630 location->objectid = objectid;
1631 location->offset = 0;
1632 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1634 insert_inode_hash(inode);
1637 btrfs_free_path(path);
1638 return ERR_PTR(ret);
1641 static inline u8 btrfs_inode_type(struct inode *inode)
1643 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1646 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1647 struct dentry *dentry, struct inode *inode,
1651 struct btrfs_key key;
1652 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1653 struct inode *parent_inode;
1655 key.objectid = inode->i_ino;
1656 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1659 ret = btrfs_insert_dir_item(trans, root,
1660 dentry->d_name.name, dentry->d_name.len,
1661 dentry->d_parent->d_inode->i_ino,
1662 &key, btrfs_inode_type(inode));
1665 ret = btrfs_insert_inode_ref(trans, root,
1666 dentry->d_name.name,
1669 dentry->d_parent->d_inode->i_ino);
1671 parent_inode = dentry->d_parent->d_inode;
1672 parent_inode->i_size += dentry->d_name.len * 2;
1673 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1674 ret = btrfs_update_inode(trans, root,
1675 dentry->d_parent->d_inode);
1680 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1681 struct dentry *dentry, struct inode *inode,
1684 int err = btrfs_add_link(trans, dentry, inode, backref);
1686 d_instantiate(dentry, inode);
1694 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1695 int mode, dev_t rdev)
1697 struct btrfs_trans_handle *trans;
1698 struct btrfs_root *root = BTRFS_I(dir)->root;
1699 struct inode *inode = NULL;
1703 unsigned long nr = 0;
1705 if (!new_valid_dev(rdev))
1708 mutex_lock(&root->fs_info->fs_mutex);
1709 err = btrfs_check_free_space(root, 1, 0);
1713 trans = btrfs_start_transaction(root, 1);
1714 btrfs_set_trans_block_group(trans, dir);
1716 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1722 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1724 dentry->d_parent->d_inode->i_ino, objectid,
1725 BTRFS_I(dir)->block_group, mode);
1726 err = PTR_ERR(inode);
1730 btrfs_set_trans_block_group(trans, inode);
1731 err = btrfs_add_nondir(trans, dentry, inode, 0);
1735 inode->i_op = &btrfs_special_inode_operations;
1736 init_special_inode(inode, inode->i_mode, rdev);
1737 btrfs_update_inode(trans, root, inode);
1739 dir->i_sb->s_dirt = 1;
1740 btrfs_update_inode_block_group(trans, inode);
1741 btrfs_update_inode_block_group(trans, dir);
1743 nr = trans->blocks_used;
1744 btrfs_end_transaction(trans, root);
1746 mutex_unlock(&root->fs_info->fs_mutex);
1749 inode_dec_link_count(inode);
1752 btrfs_btree_balance_dirty(root, nr);
1753 btrfs_throttle(root);
1757 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1758 int mode, struct nameidata *nd)
1760 struct btrfs_trans_handle *trans;
1761 struct btrfs_root *root = BTRFS_I(dir)->root;
1762 struct inode *inode = NULL;
1765 unsigned long nr = 0;
1768 mutex_lock(&root->fs_info->fs_mutex);
1769 err = btrfs_check_free_space(root, 1, 0);
1772 trans = btrfs_start_transaction(root, 1);
1773 btrfs_set_trans_block_group(trans, dir);
1775 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1781 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1783 dentry->d_parent->d_inode->i_ino,
1784 objectid, BTRFS_I(dir)->block_group, mode);
1785 err = PTR_ERR(inode);
1789 btrfs_set_trans_block_group(trans, inode);
1790 err = btrfs_add_nondir(trans, dentry, inode, 0);
1794 inode->i_mapping->a_ops = &btrfs_aops;
1795 inode->i_fop = &btrfs_file_operations;
1796 inode->i_op = &btrfs_file_inode_operations;
1797 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1798 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1799 inode->i_mapping, GFP_NOFS);
1800 BTRFS_I(inode)->delalloc_bytes = 0;
1801 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1803 dir->i_sb->s_dirt = 1;
1804 btrfs_update_inode_block_group(trans, inode);
1805 btrfs_update_inode_block_group(trans, dir);
1807 nr = trans->blocks_used;
1808 btrfs_end_transaction(trans, root);
1810 mutex_unlock(&root->fs_info->fs_mutex);
1813 inode_dec_link_count(inode);
1816 btrfs_btree_balance_dirty(root, nr);
1817 btrfs_throttle(root);
1821 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1822 struct dentry *dentry)
1824 struct btrfs_trans_handle *trans;
1825 struct btrfs_root *root = BTRFS_I(dir)->root;
1826 struct inode *inode = old_dentry->d_inode;
1827 unsigned long nr = 0;
1831 if (inode->i_nlink == 0)
1834 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1839 mutex_lock(&root->fs_info->fs_mutex);
1840 err = btrfs_check_free_space(root, 1, 0);
1843 trans = btrfs_start_transaction(root, 1);
1845 btrfs_set_trans_block_group(trans, dir);
1846 atomic_inc(&inode->i_count);
1847 err = btrfs_add_nondir(trans, dentry, inode, 1);
1852 dir->i_sb->s_dirt = 1;
1853 btrfs_update_inode_block_group(trans, dir);
1854 err = btrfs_update_inode(trans, root, inode);
1859 nr = trans->blocks_used;
1860 btrfs_end_transaction(trans, root);
1862 mutex_unlock(&root->fs_info->fs_mutex);
1865 inode_dec_link_count(inode);
1868 btrfs_btree_balance_dirty(root, nr);
1869 btrfs_throttle(root);
1873 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1875 struct inode *inode;
1876 struct btrfs_trans_handle *trans;
1877 struct btrfs_root *root = BTRFS_I(dir)->root;
1879 int drop_on_err = 0;
1881 unsigned long nr = 1;
1883 mutex_lock(&root->fs_info->fs_mutex);
1884 err = btrfs_check_free_space(root, 1, 0);
1888 trans = btrfs_start_transaction(root, 1);
1889 btrfs_set_trans_block_group(trans, dir);
1891 if (IS_ERR(trans)) {
1892 err = PTR_ERR(trans);
1896 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1902 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1904 dentry->d_parent->d_inode->i_ino, objectid,
1905 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1906 if (IS_ERR(inode)) {
1907 err = PTR_ERR(inode);
1912 inode->i_op = &btrfs_dir_inode_operations;
1913 inode->i_fop = &btrfs_dir_file_operations;
1914 btrfs_set_trans_block_group(trans, inode);
1917 err = btrfs_update_inode(trans, root, inode);
1921 err = btrfs_add_link(trans, dentry, inode, 0);
1925 d_instantiate(dentry, inode);
1927 dir->i_sb->s_dirt = 1;
1928 btrfs_update_inode_block_group(trans, inode);
1929 btrfs_update_inode_block_group(trans, dir);
1932 nr = trans->blocks_used;
1933 btrfs_end_transaction(trans, root);
1936 mutex_unlock(&root->fs_info->fs_mutex);
1939 btrfs_btree_balance_dirty(root, nr);
1940 btrfs_throttle(root);
1944 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1945 size_t pg_offset, u64 start, u64 len,
1951 u64 extent_start = 0;
1953 u64 objectid = inode->i_ino;
1955 struct btrfs_path *path;
1956 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 struct btrfs_file_extent_item *item;
1958 struct extent_buffer *leaf;
1959 struct btrfs_key found_key;
1960 struct extent_map *em = NULL;
1961 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1963 struct btrfs_trans_handle *trans = NULL;
1965 path = btrfs_alloc_path();
1967 mutex_lock(&root->fs_info->fs_mutex);
1970 spin_lock(&em_tree->lock);
1971 em = lookup_extent_mapping(em_tree, start, len);
1972 spin_unlock(&em_tree->lock);
1975 if (em->start > start) {
1976 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1977 start, len, em->start, em->len);
1980 if (em->block_start == EXTENT_MAP_INLINE && page)
1981 free_extent_map(em);
1985 em = alloc_extent_map(GFP_NOFS);
1991 em->start = EXTENT_MAP_HOLE;
1993 em->bdev = inode->i_sb->s_bdev;
1994 ret = btrfs_lookup_file_extent(trans, root, path,
1995 objectid, start, trans != NULL);
2002 if (path->slots[0] == 0)
2007 leaf = path->nodes[0];
2008 item = btrfs_item_ptr(leaf, path->slots[0],
2009 struct btrfs_file_extent_item);
2010 /* are we inside the extent that was found? */
2011 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2012 found_type = btrfs_key_type(&found_key);
2013 if (found_key.objectid != objectid ||
2014 found_type != BTRFS_EXTENT_DATA_KEY) {
2018 found_type = btrfs_file_extent_type(leaf, item);
2019 extent_start = found_key.offset;
2020 if (found_type == BTRFS_FILE_EXTENT_REG) {
2021 extent_end = extent_start +
2022 btrfs_file_extent_num_bytes(leaf, item);
2024 if (start < extent_start || start >= extent_end) {
2026 if (start < extent_start) {
2027 if (start + len <= extent_start)
2029 em->len = extent_end - extent_start;
2035 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2037 em->start = extent_start;
2038 em->len = extent_end - extent_start;
2039 em->block_start = EXTENT_MAP_HOLE;
2042 bytenr += btrfs_file_extent_offset(leaf, item);
2043 em->block_start = bytenr;
2044 em->start = extent_start;
2045 em->len = extent_end - extent_start;
2047 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2052 size_t extent_offset;
2055 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2057 extent_end = (extent_start + size + root->sectorsize - 1) &
2058 ~((u64)root->sectorsize - 1);
2059 if (start < extent_start || start >= extent_end) {
2061 if (start < extent_start) {
2062 if (start + len <= extent_start)
2064 em->len = extent_end - extent_start;
2070 em->block_start = EXTENT_MAP_INLINE;
2073 em->start = extent_start;
2078 page_start = page_offset(page) + pg_offset;
2079 extent_offset = page_start - extent_start;
2080 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2081 size - extent_offset);
2082 em->start = extent_start + extent_offset;
2083 em->len = (copy_size + root->sectorsize - 1) &
2084 ~((u64)root->sectorsize - 1);
2086 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2087 if (create == 0 && !PageUptodate(page)) {
2088 read_extent_buffer(leaf, map + pg_offset, ptr,
2090 flush_dcache_page(page);
2091 } else if (create && PageUptodate(page)) {
2094 free_extent_map(em);
2096 btrfs_release_path(root, path);
2097 trans = btrfs_start_transaction(root, 1);
2100 write_extent_buffer(leaf, map + pg_offset, ptr,
2102 btrfs_mark_buffer_dirty(leaf);
2105 set_extent_uptodate(io_tree, em->start,
2106 extent_map_end(em) - 1, GFP_NOFS);
2109 printk("unkknown found_type %d\n", found_type);
2116 em->block_start = EXTENT_MAP_HOLE;
2118 btrfs_release_path(root, path);
2119 if (em->start > start || extent_map_end(em) <= start) {
2120 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2126 spin_lock(&em_tree->lock);
2127 ret = add_extent_mapping(em_tree, em);
2128 if (ret == -EEXIST) {
2129 free_extent_map(em);
2130 em = lookup_extent_mapping(em_tree, start, len);
2133 printk("failing to insert %Lu %Lu\n", start, len);
2136 spin_unlock(&em_tree->lock);
2138 btrfs_free_path(path);
2140 ret = btrfs_end_transaction(trans, root);
2144 mutex_unlock(&root->fs_info->fs_mutex);
2146 free_extent_map(em);
2148 return ERR_PTR(err);
2153 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2155 return extent_bmap(mapping, iblock, btrfs_get_extent);
2158 int btrfs_readpage(struct file *file, struct page *page)
2160 struct extent_io_tree *tree;
2161 tree = &BTRFS_I(page->mapping->host)->io_tree;
2162 return extent_read_full_page(tree, page, btrfs_get_extent);
2165 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2167 struct extent_io_tree *tree;
2170 if (current->flags & PF_MEMALLOC) {
2171 redirty_page_for_writepage(wbc, page);
2175 tree = &BTRFS_I(page->mapping->host)->io_tree;
2176 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2179 static int btrfs_writepages(struct address_space *mapping,
2180 struct writeback_control *wbc)
2182 struct extent_io_tree *tree;
2183 tree = &BTRFS_I(mapping->host)->io_tree;
2184 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2188 btrfs_readpages(struct file *file, struct address_space *mapping,
2189 struct list_head *pages, unsigned nr_pages)
2191 struct extent_io_tree *tree;
2192 tree = &BTRFS_I(mapping->host)->io_tree;
2193 return extent_readpages(tree, mapping, pages, nr_pages,
2197 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2199 struct extent_io_tree *tree;
2200 struct extent_map_tree *map;
2203 tree = &BTRFS_I(page->mapping->host)->io_tree;
2204 map = &BTRFS_I(page->mapping->host)->extent_tree;
2205 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2207 ClearPagePrivate(page);
2208 set_page_private(page, 0);
2209 page_cache_release(page);
2214 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2216 struct extent_io_tree *tree;
2218 tree = &BTRFS_I(page->mapping->host)->io_tree;
2219 extent_invalidatepage(tree, page, offset);
2220 btrfs_releasepage(page, GFP_NOFS);
2224 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2225 * called from a page fault handler when a page is first dirtied. Hence we must
2226 * be careful to check for EOF conditions here. We set the page up correctly
2227 * for a written page which means we get ENOSPC checking when writing into
2228 * holes and correct delalloc and unwritten extent mapping on filesystems that
2229 * support these features.
2231 * We are not allowed to take the i_mutex here so we have to play games to
2232 * protect against truncate races as the page could now be beyond EOF. Because
2233 * vmtruncate() writes the inode size before removing pages, once we have the
2234 * page lock we can determine safely if the page is beyond EOF. If it is not
2235 * beyond EOF, then the page is guaranteed safe against truncation until we
2238 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2240 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2241 struct btrfs_root *root = BTRFS_I(inode)->root;
2247 mutex_lock(&root->fs_info->fs_mutex);
2248 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2249 mutex_unlock(&root->fs_info->fs_mutex);
2256 wait_on_page_writeback(page);
2257 size = i_size_read(inode);
2258 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2260 if ((page->mapping != inode->i_mapping) ||
2261 (page_start > size)) {
2262 /* page got truncated out from underneath us */
2266 /* page is wholly or partially inside EOF */
2267 if (page_start + PAGE_CACHE_SIZE > size)
2268 end = size & ~PAGE_CACHE_MASK;
2270 end = PAGE_CACHE_SIZE;
2272 ret = btrfs_cow_one_page(inode, page, end);
2280 static void btrfs_truncate(struct inode *inode)
2282 struct btrfs_root *root = BTRFS_I(inode)->root;
2284 struct btrfs_trans_handle *trans;
2287 if (!S_ISREG(inode->i_mode))
2289 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2292 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2294 mutex_lock(&root->fs_info->fs_mutex);
2295 trans = btrfs_start_transaction(root, 1);
2296 btrfs_set_trans_block_group(trans, inode);
2298 /* FIXME, add redo link to tree so we don't leak on crash */
2299 ret = btrfs_truncate_in_trans(trans, root, inode,
2300 BTRFS_EXTENT_DATA_KEY);
2301 btrfs_update_inode(trans, root, inode);
2302 nr = trans->blocks_used;
2304 ret = btrfs_end_transaction(trans, root);
2306 mutex_unlock(&root->fs_info->fs_mutex);
2307 btrfs_btree_balance_dirty(root, nr);
2308 btrfs_throttle(root);
2311 static int noinline create_subvol(struct btrfs_root *root, char *name,
2314 struct btrfs_trans_handle *trans;
2315 struct btrfs_key key;
2316 struct btrfs_root_item root_item;
2317 struct btrfs_inode_item *inode_item;
2318 struct extent_buffer *leaf;
2319 struct btrfs_root *new_root = root;
2320 struct inode *inode;
2325 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2326 unsigned long nr = 1;
2328 mutex_lock(&root->fs_info->fs_mutex);
2329 ret = btrfs_check_free_space(root, 1, 0);
2333 trans = btrfs_start_transaction(root, 1);
2336 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2341 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2342 objectid, trans->transid, 0, 0,
2345 return PTR_ERR(leaf);
2347 btrfs_set_header_nritems(leaf, 0);
2348 btrfs_set_header_level(leaf, 0);
2349 btrfs_set_header_bytenr(leaf, leaf->start);
2350 btrfs_set_header_generation(leaf, trans->transid);
2351 btrfs_set_header_owner(leaf, objectid);
2353 write_extent_buffer(leaf, root->fs_info->fsid,
2354 (unsigned long)btrfs_header_fsid(leaf),
2356 btrfs_mark_buffer_dirty(leaf);
2358 inode_item = &root_item.inode;
2359 memset(inode_item, 0, sizeof(*inode_item));
2360 inode_item->generation = cpu_to_le64(1);
2361 inode_item->size = cpu_to_le64(3);
2362 inode_item->nlink = cpu_to_le32(1);
2363 inode_item->nblocks = cpu_to_le64(1);
2364 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2366 btrfs_set_root_bytenr(&root_item, leaf->start);
2367 btrfs_set_root_level(&root_item, 0);
2368 btrfs_set_root_refs(&root_item, 1);
2369 btrfs_set_root_used(&root_item, 0);
2371 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2372 root_item.drop_level = 0;
2374 free_extent_buffer(leaf);
2377 btrfs_set_root_dirid(&root_item, new_dirid);
2379 key.objectid = objectid;
2381 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2382 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2388 * insert the directory item
2390 key.offset = (u64)-1;
2391 dir = root->fs_info->sb->s_root->d_inode;
2392 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2393 name, namelen, dir->i_ino, &key,
2398 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2399 name, namelen, objectid,
2400 root->fs_info->sb->s_root->d_inode->i_ino);
2404 ret = btrfs_commit_transaction(trans, root);
2408 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2411 trans = btrfs_start_transaction(new_root, 1);
2414 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2416 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2419 inode->i_op = &btrfs_dir_inode_operations;
2420 inode->i_fop = &btrfs_dir_file_operations;
2421 new_root->inode = inode;
2423 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2427 ret = btrfs_update_inode(trans, new_root, inode);
2431 nr = trans->blocks_used;
2432 err = btrfs_commit_transaction(trans, new_root);
2436 mutex_unlock(&root->fs_info->fs_mutex);
2437 btrfs_btree_balance_dirty(root, nr);
2438 btrfs_throttle(root);
2442 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2444 struct btrfs_pending_snapshot *pending_snapshot;
2445 struct btrfs_trans_handle *trans;
2448 unsigned long nr = 0;
2450 if (!root->ref_cows)
2453 mutex_lock(&root->fs_info->fs_mutex);
2454 ret = btrfs_check_free_space(root, 1, 0);
2458 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2459 if (!pending_snapshot) {
2463 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2464 if (!pending_snapshot->name) {
2466 kfree(pending_snapshot);
2469 memcpy(pending_snapshot->name, name, namelen);
2470 pending_snapshot->name[namelen] = '\0';
2471 trans = btrfs_start_transaction(root, 1);
2473 pending_snapshot->root = root;
2474 list_add(&pending_snapshot->list,
2475 &trans->transaction->pending_snapshots);
2476 ret = btrfs_update_inode(trans, root, root->inode);
2477 err = btrfs_commit_transaction(trans, root);
2480 mutex_unlock(&root->fs_info->fs_mutex);
2481 btrfs_btree_balance_dirty(root, nr);
2482 btrfs_throttle(root);
2486 unsigned long btrfs_force_ra(struct address_space *mapping,
2487 struct file_ra_state *ra, struct file *file,
2488 pgoff_t offset, pgoff_t last_index)
2492 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2493 req_size = last_index - offset + 1;
2494 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2497 req_size = min(last_index - offset + 1, (pgoff_t)128);
2498 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2499 return offset + req_size;
2503 int btrfs_defrag_file(struct file *file) {
2504 struct inode *inode = fdentry(file)->d_inode;
2505 struct btrfs_root *root = BTRFS_I(inode)->root;
2506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2508 unsigned long last_index;
2509 unsigned long ra_index = 0;
2515 mutex_lock(&root->fs_info->fs_mutex);
2516 ret = btrfs_check_free_space(root, inode->i_size, 0);
2517 mutex_unlock(&root->fs_info->fs_mutex);
2521 mutex_lock(&inode->i_mutex);
2522 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2523 for (i = 0; i <= last_index; i++) {
2524 if (i == ra_index) {
2525 ra_index = btrfs_force_ra(inode->i_mapping,
2527 file, ra_index, last_index);
2529 page = grab_cache_page(inode->i_mapping, i);
2532 if (!PageUptodate(page)) {
2533 btrfs_readpage(NULL, page);
2535 if (!PageUptodate(page)) {
2537 page_cache_release(page);
2541 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2542 page_end = page_start + PAGE_CACHE_SIZE - 1;
2544 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2545 set_extent_delalloc(io_tree, page_start,
2546 page_end, GFP_NOFS);
2548 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2549 set_page_dirty(page);
2551 page_cache_release(page);
2552 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2556 mutex_unlock(&inode->i_mutex);
2560 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2564 struct btrfs_ioctl_vol_args *vol_args;
2565 struct btrfs_trans_handle *trans;
2571 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2576 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2580 namelen = strlen(vol_args->name);
2581 if (namelen > BTRFS_VOL_NAME_MAX) {
2586 sizestr = vol_args->name;
2587 if (!strcmp(sizestr, "max"))
2588 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2590 if (sizestr[0] == '-') {
2593 } else if (sizestr[0] == '+') {
2597 new_size = btrfs_parse_size(sizestr);
2598 if (new_size == 0) {
2604 mutex_lock(&root->fs_info->fs_mutex);
2605 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2608 if (new_size > old_size) {
2612 new_size = old_size - new_size;
2613 } else if (mod > 0) {
2614 new_size = old_size + new_size;
2617 if (new_size < 256 * 1024 * 1024) {
2621 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2626 do_div(new_size, root->sectorsize);
2627 new_size *= root->sectorsize;
2629 printk("new size is %Lu\n", new_size);
2630 if (new_size > old_size) {
2631 trans = btrfs_start_transaction(root, 1);
2632 ret = btrfs_grow_extent_tree(trans, root, new_size);
2633 btrfs_commit_transaction(trans, root);
2635 ret = btrfs_shrink_extent_tree(root, new_size);
2639 mutex_unlock(&root->fs_info->fs_mutex);
2645 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2648 struct btrfs_ioctl_vol_args *vol_args;
2649 struct btrfs_dir_item *di;
2650 struct btrfs_path *path;
2655 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2660 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2665 namelen = strlen(vol_args->name);
2666 if (namelen > BTRFS_VOL_NAME_MAX) {
2670 if (strchr(vol_args->name, '/')) {
2675 path = btrfs_alloc_path();
2681 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2682 mutex_lock(&root->fs_info->fs_mutex);
2683 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2685 vol_args->name, namelen, 0);
2686 mutex_unlock(&root->fs_info->fs_mutex);
2687 btrfs_free_path(path);
2689 if (di && !IS_ERR(di)) {
2699 if (root == root->fs_info->tree_root)
2700 ret = create_subvol(root, vol_args->name, namelen);
2702 ret = create_snapshot(root, vol_args->name, namelen);
2708 static int btrfs_ioctl_defrag(struct file *file)
2710 struct inode *inode = fdentry(file)->d_inode;
2711 struct btrfs_root *root = BTRFS_I(inode)->root;
2713 switch (inode->i_mode & S_IFMT) {
2715 mutex_lock(&root->fs_info->fs_mutex);
2716 btrfs_defrag_root(root, 0);
2717 btrfs_defrag_root(root->fs_info->extent_root, 0);
2718 mutex_unlock(&root->fs_info->fs_mutex);
2721 btrfs_defrag_file(file);
2728 long btrfs_ioctl(struct file *file, unsigned int
2729 cmd, unsigned long arg)
2731 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2734 case BTRFS_IOC_SNAP_CREATE:
2735 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2736 case BTRFS_IOC_DEFRAG:
2737 return btrfs_ioctl_defrag(file);
2738 case BTRFS_IOC_RESIZE:
2739 return btrfs_ioctl_resize(root, (void __user *)arg);
2746 * Called inside transaction, so use GFP_NOFS
2748 struct inode *btrfs_alloc_inode(struct super_block *sb)
2750 struct btrfs_inode *ei;
2752 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2756 ei->ordered_trans = 0;
2757 return &ei->vfs_inode;
2760 void btrfs_destroy_inode(struct inode *inode)
2762 WARN_ON(!list_empty(&inode->i_dentry));
2763 WARN_ON(inode->i_data.nrpages);
2765 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2766 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2769 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2770 static void init_once(struct kmem_cache * cachep, void *foo)
2772 static void init_once(void * foo, struct kmem_cache * cachep,
2773 unsigned long flags)
2776 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2778 inode_init_once(&ei->vfs_inode);
2781 void btrfs_destroy_cachep(void)
2783 if (btrfs_inode_cachep)
2784 kmem_cache_destroy(btrfs_inode_cachep);
2785 if (btrfs_trans_handle_cachep)
2786 kmem_cache_destroy(btrfs_trans_handle_cachep);
2787 if (btrfs_transaction_cachep)
2788 kmem_cache_destroy(btrfs_transaction_cachep);
2789 if (btrfs_bit_radix_cachep)
2790 kmem_cache_destroy(btrfs_bit_radix_cachep);
2791 if (btrfs_path_cachep)
2792 kmem_cache_destroy(btrfs_path_cachep);
2795 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2796 unsigned long extra_flags,
2797 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2798 void (*ctor)(struct kmem_cache *, void *)
2800 void (*ctor)(void *, struct kmem_cache *,
2805 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2806 SLAB_MEM_SPREAD | extra_flags), ctor
2807 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2813 int btrfs_init_cachep(void)
2815 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2816 sizeof(struct btrfs_inode),
2818 if (!btrfs_inode_cachep)
2820 btrfs_trans_handle_cachep =
2821 btrfs_cache_create("btrfs_trans_handle_cache",
2822 sizeof(struct btrfs_trans_handle),
2824 if (!btrfs_trans_handle_cachep)
2826 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2827 sizeof(struct btrfs_transaction),
2829 if (!btrfs_transaction_cachep)
2831 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2832 sizeof(struct btrfs_path),
2834 if (!btrfs_path_cachep)
2836 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2837 SLAB_DESTROY_BY_RCU, NULL);
2838 if (!btrfs_bit_radix_cachep)
2842 btrfs_destroy_cachep();
2846 static int btrfs_getattr(struct vfsmount *mnt,
2847 struct dentry *dentry, struct kstat *stat)
2849 struct inode *inode = dentry->d_inode;
2850 generic_fillattr(inode, stat);
2851 stat->blksize = PAGE_CACHE_SIZE;
2852 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2856 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2857 struct inode * new_dir,struct dentry *new_dentry)
2859 struct btrfs_trans_handle *trans;
2860 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2861 struct inode *new_inode = new_dentry->d_inode;
2862 struct inode *old_inode = old_dentry->d_inode;
2863 struct timespec ctime = CURRENT_TIME;
2864 struct btrfs_path *path;
2867 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2868 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2872 mutex_lock(&root->fs_info->fs_mutex);
2873 ret = btrfs_check_free_space(root, 1, 0);
2877 trans = btrfs_start_transaction(root, 1);
2879 btrfs_set_trans_block_group(trans, new_dir);
2880 path = btrfs_alloc_path();
2886 old_dentry->d_inode->i_nlink++;
2887 old_dir->i_ctime = old_dir->i_mtime = ctime;
2888 new_dir->i_ctime = new_dir->i_mtime = ctime;
2889 old_inode->i_ctime = ctime;
2891 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2896 new_inode->i_ctime = CURRENT_TIME;
2897 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2901 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2906 btrfs_free_path(path);
2907 btrfs_end_transaction(trans, root);
2909 mutex_unlock(&root->fs_info->fs_mutex);
2913 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2914 const char *symname)
2916 struct btrfs_trans_handle *trans;
2917 struct btrfs_root *root = BTRFS_I(dir)->root;
2918 struct btrfs_path *path;
2919 struct btrfs_key key;
2920 struct inode *inode = NULL;
2927 struct btrfs_file_extent_item *ei;
2928 struct extent_buffer *leaf;
2929 unsigned long nr = 0;
2931 name_len = strlen(symname) + 1;
2932 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2933 return -ENAMETOOLONG;
2935 mutex_lock(&root->fs_info->fs_mutex);
2936 err = btrfs_check_free_space(root, 1, 0);
2940 trans = btrfs_start_transaction(root, 1);
2941 btrfs_set_trans_block_group(trans, dir);
2943 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2949 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2951 dentry->d_parent->d_inode->i_ino, objectid,
2952 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2953 err = PTR_ERR(inode);
2957 btrfs_set_trans_block_group(trans, inode);
2958 err = btrfs_add_nondir(trans, dentry, inode, 0);
2962 inode->i_mapping->a_ops = &btrfs_aops;
2963 inode->i_fop = &btrfs_file_operations;
2964 inode->i_op = &btrfs_file_inode_operations;
2965 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2966 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2967 inode->i_mapping, GFP_NOFS);
2968 BTRFS_I(inode)->delalloc_bytes = 0;
2969 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2971 dir->i_sb->s_dirt = 1;
2972 btrfs_update_inode_block_group(trans, inode);
2973 btrfs_update_inode_block_group(trans, dir);
2977 path = btrfs_alloc_path();
2979 key.objectid = inode->i_ino;
2981 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2982 datasize = btrfs_file_extent_calc_inline_size(name_len);
2983 err = btrfs_insert_empty_item(trans, root, path, &key,
2989 leaf = path->nodes[0];
2990 ei = btrfs_item_ptr(leaf, path->slots[0],
2991 struct btrfs_file_extent_item);
2992 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2993 btrfs_set_file_extent_type(leaf, ei,
2994 BTRFS_FILE_EXTENT_INLINE);
2995 ptr = btrfs_file_extent_inline_start(ei);
2996 write_extent_buffer(leaf, symname, ptr, name_len);
2997 btrfs_mark_buffer_dirty(leaf);
2998 btrfs_free_path(path);
3000 inode->i_op = &btrfs_symlink_inode_operations;
3001 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3002 inode->i_size = name_len - 1;
3003 err = btrfs_update_inode(trans, root, inode);
3008 nr = trans->blocks_used;
3009 btrfs_end_transaction(trans, root);
3011 mutex_unlock(&root->fs_info->fs_mutex);
3013 inode_dec_link_count(inode);
3016 btrfs_btree_balance_dirty(root, nr);
3017 btrfs_throttle(root);
3020 static int btrfs_permission(struct inode *inode, int mask,
3021 struct nameidata *nd)
3023 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3025 return generic_permission(inode, mask, NULL);
3028 static struct inode_operations btrfs_dir_inode_operations = {
3029 .lookup = btrfs_lookup,
3030 .create = btrfs_create,
3031 .unlink = btrfs_unlink,
3033 .mkdir = btrfs_mkdir,
3034 .rmdir = btrfs_rmdir,
3035 .rename = btrfs_rename,
3036 .symlink = btrfs_symlink,
3037 .setattr = btrfs_setattr,
3038 .mknod = btrfs_mknod,
3039 .setxattr = generic_setxattr,
3040 .getxattr = generic_getxattr,
3041 .listxattr = btrfs_listxattr,
3042 .removexattr = generic_removexattr,
3043 .permission = btrfs_permission,
3045 static struct inode_operations btrfs_dir_ro_inode_operations = {
3046 .lookup = btrfs_lookup,
3047 .permission = btrfs_permission,
3049 static struct file_operations btrfs_dir_file_operations = {
3050 .llseek = generic_file_llseek,
3051 .read = generic_read_dir,
3052 .readdir = btrfs_readdir,
3053 .unlocked_ioctl = btrfs_ioctl,
3054 #ifdef CONFIG_COMPAT
3055 .compat_ioctl = btrfs_ioctl,
3059 static struct extent_io_ops btrfs_extent_io_ops = {
3060 .fill_delalloc = run_delalloc_range,
3061 .submit_bio_hook = btrfs_submit_bio_hook,
3062 .merge_bio_hook = btrfs_merge_bio_hook,
3063 .readpage_io_hook = btrfs_readpage_io_hook,
3064 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3065 .set_bit_hook = btrfs_set_bit_hook,
3066 .clear_bit_hook = btrfs_clear_bit_hook,
3069 static struct address_space_operations btrfs_aops = {
3070 .readpage = btrfs_readpage,
3071 .writepage = btrfs_writepage,
3072 .writepages = btrfs_writepages,
3073 .readpages = btrfs_readpages,
3074 .sync_page = block_sync_page,
3076 .invalidatepage = btrfs_invalidatepage,
3077 .releasepage = btrfs_releasepage,
3078 .set_page_dirty = __set_page_dirty_nobuffers,
3081 static struct address_space_operations btrfs_symlink_aops = {
3082 .readpage = btrfs_readpage,
3083 .writepage = btrfs_writepage,
3084 .invalidatepage = btrfs_invalidatepage,
3085 .releasepage = btrfs_releasepage,
3088 static struct inode_operations btrfs_file_inode_operations = {
3089 .truncate = btrfs_truncate,
3090 .getattr = btrfs_getattr,
3091 .setattr = btrfs_setattr,
3092 .setxattr = generic_setxattr,
3093 .getxattr = generic_getxattr,
3094 .listxattr = btrfs_listxattr,
3095 .removexattr = generic_removexattr,
3096 .permission = btrfs_permission,
3098 static struct inode_operations btrfs_special_inode_operations = {
3099 .getattr = btrfs_getattr,
3100 .setattr = btrfs_setattr,
3101 .permission = btrfs_permission,
3103 static struct inode_operations btrfs_symlink_inode_operations = {
3104 .readlink = generic_readlink,
3105 .follow_link = page_follow_link_light,
3106 .put_link = page_put_link,
3107 .permission = btrfs_permission,