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_map_ops btrfs_extent_map_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 struct btrfs_key ins;
108 trans = btrfs_start_transaction(root, 1);
110 btrfs_set_trans_block_group(trans, inode);
112 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
113 num_bytes = max(blocksize, num_bytes);
114 ret = btrfs_drop_extents(trans, root, inode,
115 start, start + num_bytes, start, &alloc_hint);
117 if (alloc_hint == EXTENT_MAP_INLINE)
120 while(num_bytes > 0) {
121 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
122 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
123 root->root_key.objectid,
125 inode->i_ino, start, 0,
126 alloc_hint, (u64)-1, &ins, 1);
131 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
132 start, ins.objectid, ins.offset,
134 num_bytes -= cur_alloc_size;
135 alloc_hint = ins.objectid + ins.offset;
136 start += cur_alloc_size;
139 btrfs_end_transaction(trans, root);
143 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
150 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct extent_buffer *leaf;
153 struct btrfs_path *path;
154 struct btrfs_file_extent_item *item;
157 struct btrfs_key found_key;
159 path = btrfs_alloc_path();
162 ret = btrfs_lookup_file_extent(NULL, root, path,
163 inode->i_ino, start, 0);
165 btrfs_free_path(path);
171 if (path->slots[0] == 0)
176 leaf = path->nodes[0];
177 item = btrfs_item_ptr(leaf, path->slots[0],
178 struct btrfs_file_extent_item);
180 /* are we inside the extent that was found? */
181 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
182 found_type = btrfs_key_type(&found_key);
183 if (found_key.objectid != inode->i_ino ||
184 found_type != BTRFS_EXTENT_DATA_KEY) {
188 found_type = btrfs_file_extent_type(leaf, item);
189 extent_start = found_key.offset;
190 if (found_type == BTRFS_FILE_EXTENT_REG) {
191 extent_end = extent_start +
192 btrfs_file_extent_num_bytes(leaf, item);
195 if (loops && start != extent_start)
198 if (start < extent_start || start >= extent_end)
201 cow_end = min(end, extent_end - 1);
202 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
206 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
211 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
216 btrfs_free_path(path);
219 btrfs_release_path(root, path);
224 cow_file_range(inode, start, cow_end);
229 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
231 struct btrfs_root *root = BTRFS_I(inode)->root;
235 mutex_lock(&root->fs_info->fs_mutex);
236 if (btrfs_test_opt(root, NODATACOW))
237 ret = run_delalloc_nocow(inode, start, end);
239 ret = cow_file_range(inode, start, end);
241 spin_lock(&root->fs_info->delalloc_lock);
242 num_bytes = end + 1 - start;
243 if (root->fs_info->delalloc_bytes < num_bytes) {
244 printk("delalloc accounting error total %llu sub %llu\n",
245 root->fs_info->delalloc_bytes, num_bytes);
247 root->fs_info->delalloc_bytes -= num_bytes;
249 spin_unlock(&root->fs_info->delalloc_lock);
251 mutex_unlock(&root->fs_info->fs_mutex);
255 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
257 struct inode *inode = page->mapping->host;
258 struct btrfs_root *root = BTRFS_I(inode)->root;
259 struct btrfs_trans_handle *trans;
262 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
263 size_t offset = start - page_start;
265 if (btrfs_test_opt(root, NODATASUM))
268 mutex_lock(&root->fs_info->fs_mutex);
269 trans = btrfs_start_transaction(root, 1);
270 btrfs_set_trans_block_group(trans, inode);
272 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
273 start, kaddr + offset, end - start + 1);
275 ret = btrfs_end_transaction(trans, root);
277 mutex_unlock(&root->fs_info->fs_mutex);
281 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
284 struct inode *inode = page->mapping->host;
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
287 struct btrfs_csum_item *item;
288 struct btrfs_path *path = NULL;
291 if (btrfs_test_opt(root, NODATASUM))
294 mutex_lock(&root->fs_info->fs_mutex);
295 path = btrfs_alloc_path();
296 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
299 /* a csum that isn't present is a preallocated region. */
300 if (ret == -ENOENT || ret == -EFBIG)
305 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
307 set_state_private(em_tree, start, csum);
310 btrfs_free_path(path);
311 mutex_unlock(&root->fs_info->fs_mutex);
315 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
317 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
318 struct inode *inode = page->mapping->host;
319 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
323 struct btrfs_root *root = BTRFS_I(inode)->root;
327 if (btrfs_test_opt(root, NODATASUM))
330 ret = get_state_private(em_tree, start, &private);
331 local_irq_save(flags);
332 kaddr = kmap_atomic(page, KM_IRQ0);
336 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
337 btrfs_csum_final(csum, (char *)&csum);
338 if (csum != private) {
341 kunmap_atomic(kaddr, KM_IRQ0);
342 local_irq_restore(flags);
346 printk("btrfs csum failed ino %lu off %llu\n",
347 page->mapping->host->i_ino, (unsigned long long)start);
348 memset(kaddr + offset, 1, end - start + 1);
349 flush_dcache_page(page);
350 kunmap_atomic(kaddr, KM_IRQ0);
351 local_irq_restore(flags);
355 void btrfs_read_locked_inode(struct inode *inode)
357 struct btrfs_path *path;
358 struct extent_buffer *leaf;
359 struct btrfs_inode_item *inode_item;
360 struct btrfs_inode_timespec *tspec;
361 struct btrfs_root *root = BTRFS_I(inode)->root;
362 struct btrfs_key location;
363 u64 alloc_group_block;
367 path = btrfs_alloc_path();
369 mutex_lock(&root->fs_info->fs_mutex);
371 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
372 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
376 leaf = path->nodes[0];
377 inode_item = btrfs_item_ptr(leaf, path->slots[0],
378 struct btrfs_inode_item);
380 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
381 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
382 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
383 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
384 inode->i_size = btrfs_inode_size(leaf, inode_item);
386 tspec = btrfs_inode_atime(inode_item);
387 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
388 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
390 tspec = btrfs_inode_mtime(inode_item);
391 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
392 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
394 tspec = btrfs_inode_ctime(inode_item);
395 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
396 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
398 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
399 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
401 rdev = btrfs_inode_rdev(leaf, inode_item);
403 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
404 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
407 if (!BTRFS_I(inode)->block_group) {
408 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
411 btrfs_free_path(path);
414 mutex_unlock(&root->fs_info->fs_mutex);
416 switch (inode->i_mode & S_IFMT) {
418 inode->i_mapping->a_ops = &btrfs_aops;
419 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
420 inode->i_fop = &btrfs_file_operations;
421 inode->i_op = &btrfs_file_inode_operations;
424 inode->i_fop = &btrfs_dir_file_operations;
425 if (root == root->fs_info->tree_root)
426 inode->i_op = &btrfs_dir_ro_inode_operations;
428 inode->i_op = &btrfs_dir_inode_operations;
431 inode->i_op = &btrfs_symlink_inode_operations;
432 inode->i_mapping->a_ops = &btrfs_symlink_aops;
435 init_special_inode(inode, inode->i_mode, rdev);
441 btrfs_release_path(root, path);
442 btrfs_free_path(path);
443 mutex_unlock(&root->fs_info->fs_mutex);
444 make_bad_inode(inode);
447 static void fill_inode_item(struct extent_buffer *leaf,
448 struct btrfs_inode_item *item,
451 btrfs_set_inode_uid(leaf, item, inode->i_uid);
452 btrfs_set_inode_gid(leaf, item, inode->i_gid);
453 btrfs_set_inode_size(leaf, item, inode->i_size);
454 btrfs_set_inode_mode(leaf, item, inode->i_mode);
455 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
457 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
458 inode->i_atime.tv_sec);
459 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
460 inode->i_atime.tv_nsec);
462 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
463 inode->i_mtime.tv_sec);
464 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
465 inode->i_mtime.tv_nsec);
467 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
468 inode->i_ctime.tv_sec);
469 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
470 inode->i_ctime.tv_nsec);
472 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
473 btrfs_set_inode_generation(leaf, item, inode->i_generation);
474 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
475 btrfs_set_inode_block_group(leaf, item,
476 BTRFS_I(inode)->block_group->key.objectid);
479 int btrfs_update_inode(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
483 struct btrfs_inode_item *inode_item;
484 struct btrfs_path *path;
485 struct extent_buffer *leaf;
488 path = btrfs_alloc_path();
490 ret = btrfs_lookup_inode(trans, root, path,
491 &BTRFS_I(inode)->location, 1);
498 leaf = path->nodes[0];
499 inode_item = btrfs_item_ptr(leaf, path->slots[0],
500 struct btrfs_inode_item);
502 fill_inode_item(leaf, inode_item, inode);
503 btrfs_mark_buffer_dirty(leaf);
504 btrfs_set_inode_last_trans(trans, inode);
507 btrfs_release_path(root, path);
508 btrfs_free_path(path);
513 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root,
516 struct dentry *dentry)
518 struct btrfs_path *path;
519 const char *name = dentry->d_name.name;
520 int name_len = dentry->d_name.len;
522 struct extent_buffer *leaf;
523 struct btrfs_dir_item *di;
524 struct btrfs_key key;
526 path = btrfs_alloc_path();
532 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
542 leaf = path->nodes[0];
543 btrfs_dir_item_key_to_cpu(leaf, di, &key);
544 ret = btrfs_delete_one_dir_name(trans, root, path, di);
547 btrfs_release_path(root, path);
549 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
550 key.objectid, name, name_len, -1);
559 ret = btrfs_delete_one_dir_name(trans, root, path, di);
561 dentry->d_inode->i_ctime = dir->i_ctime;
562 ret = btrfs_del_inode_ref(trans, root, name, name_len,
563 dentry->d_inode->i_ino,
564 dentry->d_parent->d_inode->i_ino);
566 printk("failed to delete reference to %.*s, "
567 "inode %lu parent %lu\n", name_len, name,
568 dentry->d_inode->i_ino,
569 dentry->d_parent->d_inode->i_ino);
572 btrfs_free_path(path);
574 dir->i_size -= name_len * 2;
575 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
576 btrfs_update_inode(trans, root, dir);
577 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
578 dentry->d_inode->i_nlink--;
580 drop_nlink(dentry->d_inode);
582 ret = btrfs_update_inode(trans, root, dentry->d_inode);
583 dir->i_sb->s_dirt = 1;
588 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
590 struct btrfs_root *root;
591 struct btrfs_trans_handle *trans;
593 unsigned long nr = 0;
595 root = BTRFS_I(dir)->root;
596 mutex_lock(&root->fs_info->fs_mutex);
598 ret = btrfs_check_free_space(root, 1, 1);
602 trans = btrfs_start_transaction(root, 1);
604 btrfs_set_trans_block_group(trans, dir);
605 ret = btrfs_unlink_trans(trans, root, dir, dentry);
606 nr = trans->blocks_used;
608 btrfs_end_transaction(trans, root);
610 mutex_unlock(&root->fs_info->fs_mutex);
611 btrfs_btree_balance_dirty(root, nr);
615 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
617 struct inode *inode = dentry->d_inode;
620 struct btrfs_root *root = BTRFS_I(dir)->root;
621 struct btrfs_trans_handle *trans;
622 unsigned long nr = 0;
624 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
627 mutex_lock(&root->fs_info->fs_mutex);
628 ret = btrfs_check_free_space(root, 1, 1);
632 trans = btrfs_start_transaction(root, 1);
633 btrfs_set_trans_block_group(trans, dir);
635 /* now the directory is empty */
636 err = btrfs_unlink_trans(trans, root, dir, dentry);
641 nr = trans->blocks_used;
642 ret = btrfs_end_transaction(trans, root);
644 mutex_unlock(&root->fs_info->fs_mutex);
645 btrfs_btree_balance_dirty(root, nr);
652 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
653 struct btrfs_root *root,
656 struct btrfs_path *path;
661 path = btrfs_alloc_path();
663 ret = btrfs_lookup_inode(trans, root, path,
664 &BTRFS_I(inode)->location, -1);
668 ret = btrfs_del_item(trans, root, path);
669 btrfs_free_path(path);
674 * this can truncate away extent items, csum items and directory items.
675 * It starts at a high offset and removes keys until it can't find
676 * any higher than i_size.
678 * csum items that cross the new i_size are truncated to the new size
681 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
682 struct btrfs_root *root,
686 struct btrfs_path *path;
687 struct btrfs_key key;
688 struct btrfs_key found_key;
690 struct extent_buffer *leaf;
691 struct btrfs_file_extent_item *fi;
692 u64 extent_start = 0;
693 u64 extent_num_bytes = 0;
699 int extent_type = -1;
701 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
702 path = btrfs_alloc_path();
706 /* FIXME, add redo link to tree so we don't leak on crash */
707 key.objectid = inode->i_ino;
708 key.offset = (u64)-1;
712 btrfs_init_path(path);
714 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
719 BUG_ON(path->slots[0] == 0);
722 leaf = path->nodes[0];
723 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
724 found_type = btrfs_key_type(&found_key);
726 if (found_key.objectid != inode->i_ino)
729 if (found_type != BTRFS_CSUM_ITEM_KEY &&
730 found_type != BTRFS_DIR_ITEM_KEY &&
731 found_type != BTRFS_DIR_INDEX_KEY &&
732 found_type != BTRFS_EXTENT_DATA_KEY)
735 item_end = found_key.offset;
736 if (found_type == BTRFS_EXTENT_DATA_KEY) {
737 fi = btrfs_item_ptr(leaf, path->slots[0],
738 struct btrfs_file_extent_item);
739 extent_type = btrfs_file_extent_type(leaf, fi);
740 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
742 btrfs_file_extent_num_bytes(leaf, fi);
743 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
744 struct btrfs_item *item = btrfs_item_nr(leaf,
746 item_end += btrfs_file_extent_inline_len(leaf,
751 if (found_type == BTRFS_CSUM_ITEM_KEY) {
752 ret = btrfs_csum_truncate(trans, root, path,
756 if (item_end < inode->i_size) {
757 if (found_type == BTRFS_DIR_ITEM_KEY) {
758 found_type = BTRFS_INODE_ITEM_KEY;
759 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
760 found_type = BTRFS_CSUM_ITEM_KEY;
761 } else if (found_type) {
766 btrfs_set_key_type(&key, found_type);
767 btrfs_release_path(root, path);
770 if (found_key.offset >= inode->i_size)
776 /* FIXME, shrink the extent if the ref count is only 1 */
777 if (found_type != BTRFS_EXTENT_DATA_KEY)
780 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
782 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
785 btrfs_file_extent_num_bytes(leaf, fi);
786 extent_num_bytes = inode->i_size -
787 found_key.offset + root->sectorsize - 1;
788 btrfs_set_file_extent_num_bytes(leaf, fi,
790 num_dec = (orig_num_bytes -
791 extent_num_bytes) >> 9;
792 if (extent_start != 0) {
793 inode->i_blocks -= num_dec;
795 btrfs_mark_buffer_dirty(leaf);
798 btrfs_file_extent_disk_num_bytes(leaf,
800 /* FIXME blocksize != 4096 */
801 num_dec = btrfs_file_extent_num_bytes(leaf,
803 if (extent_start != 0) {
805 inode->i_blocks -= num_dec;
807 root_gen = btrfs_header_generation(leaf);
808 root_owner = btrfs_header_owner(leaf);
810 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
812 u32 newsize = inode->i_size - found_key.offset;
813 newsize = btrfs_file_extent_calc_inline_size(newsize);
814 ret = btrfs_truncate_item(trans, root, path,
820 ret = btrfs_del_item(trans, root, path);
826 btrfs_release_path(root, path);
828 ret = btrfs_free_extent(trans, root, extent_start,
831 root_gen, inode->i_ino,
832 found_key.offset, 0);
838 btrfs_release_path(root, path);
839 btrfs_free_path(path);
840 inode->i_sb->s_dirt = 1;
844 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
848 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
849 struct btrfs_root *root = BTRFS_I(inode)->root;
850 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
851 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
852 u64 existing_delalloc;
856 WARN_ON(!PageLocked(page));
857 set_page_extent_mapped(page);
859 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
860 delalloc_start = page_start;
861 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
862 &delalloc_start, page_end,
863 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
864 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
867 spin_lock(&root->fs_info->delalloc_lock);
868 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
869 spin_unlock(&root->fs_info->delalloc_lock);
871 if (zero_start != PAGE_CACHE_SIZE) {
873 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
874 flush_dcache_page(page);
877 set_page_dirty(page);
878 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
884 * taken from block_truncate_page, but does cow as it zeros out
885 * any bytes left in the last page in the file.
887 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
889 struct inode *inode = mapping->host;
890 struct btrfs_root *root = BTRFS_I(inode)->root;
891 u32 blocksize = root->sectorsize;
892 pgoff_t index = from >> PAGE_CACHE_SHIFT;
893 unsigned offset = from & (PAGE_CACHE_SIZE-1);
898 if ((offset & (blocksize - 1)) == 0)
901 down_read(&root->snap_sem);
903 page = grab_cache_page(mapping, index);
906 if (!PageUptodate(page)) {
907 ret = btrfs_readpage(NULL, page);
909 if (!PageUptodate(page)) {
914 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
916 ret = btrfs_cow_one_page(inode, page, offset);
919 page_cache_release(page);
920 up_read(&BTRFS_I(inode)->root->snap_sem);
925 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
927 struct inode *inode = dentry->d_inode;
930 err = inode_change_ok(inode, attr);
934 if (S_ISREG(inode->i_mode) &&
935 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
936 struct btrfs_trans_handle *trans;
937 struct btrfs_root *root = BTRFS_I(inode)->root;
938 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
940 u64 mask = root->sectorsize - 1;
941 u64 pos = (inode->i_size + mask) & ~mask;
942 u64 block_end = attr->ia_size | mask;
946 if (attr->ia_size <= pos)
949 mutex_lock(&root->fs_info->fs_mutex);
950 err = btrfs_check_free_space(root, 1, 0);
951 mutex_unlock(&root->fs_info->fs_mutex);
955 btrfs_truncate_page(inode->i_mapping, inode->i_size);
957 lock_extent(em_tree, pos, block_end, GFP_NOFS);
958 hole_size = (attr->ia_size - pos + mask) & ~mask;
960 mutex_lock(&root->fs_info->fs_mutex);
961 trans = btrfs_start_transaction(root, 1);
962 btrfs_set_trans_block_group(trans, inode);
963 err = btrfs_drop_extents(trans, root, inode,
964 pos, pos + hole_size, pos,
967 if (alloc_hint != EXTENT_MAP_INLINE) {
968 err = btrfs_insert_file_extent(trans, root,
970 pos, 0, 0, hole_size);
972 btrfs_end_transaction(trans, root);
973 mutex_unlock(&root->fs_info->fs_mutex);
974 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
979 err = inode_setattr(inode, attr);
983 void btrfs_delete_inode(struct inode *inode)
985 struct btrfs_trans_handle *trans;
986 struct btrfs_root *root = BTRFS_I(inode)->root;
990 truncate_inode_pages(&inode->i_data, 0);
991 if (is_bad_inode(inode)) {
996 mutex_lock(&root->fs_info->fs_mutex);
997 trans = btrfs_start_transaction(root, 1);
999 btrfs_set_trans_block_group(trans, inode);
1000 ret = btrfs_truncate_in_trans(trans, root, inode);
1002 goto no_delete_lock;
1003 ret = btrfs_delete_xattrs(trans, root, inode);
1005 goto no_delete_lock;
1006 ret = btrfs_free_inode(trans, root, inode);
1008 goto no_delete_lock;
1009 nr = trans->blocks_used;
1011 btrfs_end_transaction(trans, root);
1012 mutex_unlock(&root->fs_info->fs_mutex);
1013 btrfs_btree_balance_dirty(root, nr);
1017 nr = trans->blocks_used;
1018 btrfs_end_transaction(trans, root);
1019 mutex_unlock(&root->fs_info->fs_mutex);
1020 btrfs_btree_balance_dirty(root, nr);
1026 * this returns the key found in the dir entry in the location pointer.
1027 * If no dir entries were found, location->objectid is 0.
1029 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1030 struct btrfs_key *location)
1032 const char *name = dentry->d_name.name;
1033 int namelen = dentry->d_name.len;
1034 struct btrfs_dir_item *di;
1035 struct btrfs_path *path;
1036 struct btrfs_root *root = BTRFS_I(dir)->root;
1039 if (namelen == 1 && strcmp(name, ".") == 0) {
1040 location->objectid = dir->i_ino;
1041 location->type = BTRFS_INODE_ITEM_KEY;
1042 location->offset = 0;
1045 path = btrfs_alloc_path();
1048 if (namelen == 2 && strcmp(name, "..") == 0) {
1049 struct btrfs_key key;
1050 struct extent_buffer *leaf;
1054 key.objectid = dir->i_ino;
1055 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1057 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1061 leaf = path->nodes[0];
1062 slot = path->slots[0];
1063 nritems = btrfs_header_nritems(leaf);
1064 if (slot >= nritems)
1067 btrfs_item_key_to_cpu(leaf, &key, slot);
1068 if (key.objectid != dir->i_ino ||
1069 key.type != BTRFS_INODE_REF_KEY) {
1072 location->objectid = key.offset;
1073 location->type = BTRFS_INODE_ITEM_KEY;
1074 location->offset = 0;
1078 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1082 if (!di || IS_ERR(di)) {
1085 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1087 btrfs_free_path(path);
1090 location->objectid = 0;
1095 * when we hit a tree root in a directory, the btrfs part of the inode
1096 * needs to be changed to reflect the root directory of the tree root. This
1097 * is kind of like crossing a mount point.
1099 static int fixup_tree_root_location(struct btrfs_root *root,
1100 struct btrfs_key *location,
1101 struct btrfs_root **sub_root,
1102 struct dentry *dentry)
1104 struct btrfs_path *path;
1105 struct btrfs_root_item *ri;
1107 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1109 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1112 path = btrfs_alloc_path();
1114 mutex_lock(&root->fs_info->fs_mutex);
1116 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1117 dentry->d_name.name,
1118 dentry->d_name.len);
1119 if (IS_ERR(*sub_root))
1120 return PTR_ERR(*sub_root);
1122 ri = &(*sub_root)->root_item;
1123 location->objectid = btrfs_root_dirid(ri);
1124 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1125 location->offset = 0;
1127 btrfs_free_path(path);
1128 mutex_unlock(&root->fs_info->fs_mutex);
1132 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1134 struct btrfs_iget_args *args = p;
1135 inode->i_ino = args->ino;
1136 BTRFS_I(inode)->root = args->root;
1137 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1138 inode->i_mapping, GFP_NOFS);
1142 static int btrfs_find_actor(struct inode *inode, void *opaque)
1144 struct btrfs_iget_args *args = opaque;
1145 return (args->ino == inode->i_ino &&
1146 args->root == BTRFS_I(inode)->root);
1149 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1150 struct btrfs_root *root)
1152 struct inode *inode;
1153 struct btrfs_iget_args args;
1154 args.ino = objectid;
1157 inode = iget5_locked(s, objectid, btrfs_find_actor,
1158 btrfs_init_locked_inode,
1163 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1164 struct nameidata *nd)
1166 struct inode * inode;
1167 struct btrfs_inode *bi = BTRFS_I(dir);
1168 struct btrfs_root *root = bi->root;
1169 struct btrfs_root *sub_root = root;
1170 struct btrfs_key location;
1173 if (dentry->d_name.len > BTRFS_NAME_LEN)
1174 return ERR_PTR(-ENAMETOOLONG);
1176 mutex_lock(&root->fs_info->fs_mutex);
1177 ret = btrfs_inode_by_name(dir, dentry, &location);
1178 mutex_unlock(&root->fs_info->fs_mutex);
1181 return ERR_PTR(ret);
1184 if (location.objectid) {
1185 ret = fixup_tree_root_location(root, &location, &sub_root,
1188 return ERR_PTR(ret);
1190 return ERR_PTR(-ENOENT);
1191 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1194 return ERR_PTR(-EACCES);
1195 if (inode->i_state & I_NEW) {
1196 /* the inode and parent dir are two different roots */
1197 if (sub_root != root) {
1199 sub_root->inode = inode;
1201 BTRFS_I(inode)->root = sub_root;
1202 memcpy(&BTRFS_I(inode)->location, &location,
1204 btrfs_read_locked_inode(inode);
1205 unlock_new_inode(inode);
1208 return d_splice_alias(inode, dentry);
1211 static unsigned char btrfs_filetype_table[] = {
1212 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1215 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1217 struct inode *inode = filp->f_dentry->d_inode;
1218 struct btrfs_root *root = BTRFS_I(inode)->root;
1219 struct btrfs_item *item;
1220 struct btrfs_dir_item *di;
1221 struct btrfs_key key;
1222 struct btrfs_key found_key;
1223 struct btrfs_path *path;
1226 struct extent_buffer *leaf;
1229 unsigned char d_type;
1234 int key_type = BTRFS_DIR_INDEX_KEY;
1239 /* FIXME, use a real flag for deciding about the key type */
1240 if (root->fs_info->tree_root == root)
1241 key_type = BTRFS_DIR_ITEM_KEY;
1243 /* special case for "." */
1244 if (filp->f_pos == 0) {
1245 over = filldir(dirent, ".", 1,
1253 mutex_lock(&root->fs_info->fs_mutex);
1254 key.objectid = inode->i_ino;
1255 path = btrfs_alloc_path();
1258 /* special case for .., just use the back ref */
1259 if (filp->f_pos == 1) {
1260 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1264 leaf = path->nodes[0];
1265 slot = path->slots[0];
1266 nritems = btrfs_header_nritems(leaf);
1267 if (slot >= nritems) {
1268 btrfs_release_path(root, path);
1269 goto read_dir_items;
1271 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1272 btrfs_release_path(root, path);
1273 if (found_key.objectid != key.objectid ||
1274 found_key.type != BTRFS_INODE_REF_KEY)
1275 goto read_dir_items;
1276 over = filldir(dirent, "..", 2,
1277 2, found_key.offset, DT_DIR);
1284 btrfs_set_key_type(&key, key_type);
1285 key.offset = filp->f_pos;
1287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1292 leaf = path->nodes[0];
1293 nritems = btrfs_header_nritems(leaf);
1294 slot = path->slots[0];
1295 if (advance || slot >= nritems) {
1296 if (slot >= nritems -1) {
1297 ret = btrfs_next_leaf(root, path);
1300 leaf = path->nodes[0];
1301 nritems = btrfs_header_nritems(leaf);
1302 slot = path->slots[0];
1309 item = btrfs_item_nr(leaf, slot);
1310 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1312 if (found_key.objectid != key.objectid)
1314 if (btrfs_key_type(&found_key) != key_type)
1316 if (found_key.offset < filp->f_pos)
1319 filp->f_pos = found_key.offset;
1321 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1323 di_total = btrfs_item_size(leaf, item);
1324 while(di_cur < di_total) {
1325 struct btrfs_key location;
1327 name_len = btrfs_dir_name_len(leaf, di);
1328 if (name_len < 32) {
1329 name_ptr = tmp_name;
1331 name_ptr = kmalloc(name_len, GFP_NOFS);
1334 read_extent_buffer(leaf, name_ptr,
1335 (unsigned long)(di + 1), name_len);
1337 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1338 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1340 over = filldir(dirent, name_ptr, name_len,
1345 if (name_ptr != tmp_name)
1350 di_len = btrfs_dir_name_len(leaf, di) +
1351 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1353 di = (struct btrfs_dir_item *)((char *)di + di_len);
1360 btrfs_release_path(root, path);
1361 btrfs_free_path(path);
1362 mutex_unlock(&root->fs_info->fs_mutex);
1366 int btrfs_write_inode(struct inode *inode, int wait)
1368 struct btrfs_root *root = BTRFS_I(inode)->root;
1369 struct btrfs_trans_handle *trans;
1373 mutex_lock(&root->fs_info->fs_mutex);
1374 trans = btrfs_start_transaction(root, 1);
1375 btrfs_set_trans_block_group(trans, inode);
1376 ret = btrfs_commit_transaction(trans, root);
1377 mutex_unlock(&root->fs_info->fs_mutex);
1383 * This is somewhat expensive, updating the tree every time the
1384 * inode changes. But, it is most likely to find the inode in cache.
1385 * FIXME, needs more benchmarking...there are no reasons other than performance
1386 * to keep or drop this code.
1388 void btrfs_dirty_inode(struct inode *inode)
1390 struct btrfs_root *root = BTRFS_I(inode)->root;
1391 struct btrfs_trans_handle *trans;
1393 mutex_lock(&root->fs_info->fs_mutex);
1394 trans = btrfs_start_transaction(root, 1);
1395 btrfs_set_trans_block_group(trans, inode);
1396 btrfs_update_inode(trans, root, inode);
1397 btrfs_end_transaction(trans, root);
1398 mutex_unlock(&root->fs_info->fs_mutex);
1401 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1404 struct btrfs_block_group_cache *group,
1407 struct inode *inode;
1408 struct btrfs_inode_item *inode_item;
1409 struct btrfs_key *location;
1410 struct btrfs_path *path;
1414 path = btrfs_alloc_path();
1417 inode = new_inode(root->fs_info->sb);
1419 return ERR_PTR(-ENOMEM);
1421 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1422 inode->i_mapping, GFP_NOFS);
1423 BTRFS_I(inode)->root = root;
1429 group = btrfs_find_block_group(root, group, 0, 0, owner);
1430 BTRFS_I(inode)->block_group = group;
1432 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1436 inode->i_uid = current->fsuid;
1437 inode->i_gid = current->fsgid;
1438 inode->i_mode = mode;
1439 inode->i_ino = objectid;
1440 inode->i_blocks = 0;
1441 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1442 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1443 struct btrfs_inode_item);
1444 fill_inode_item(path->nodes[0], inode_item, inode);
1445 btrfs_mark_buffer_dirty(path->nodes[0]);
1446 btrfs_free_path(path);
1448 location = &BTRFS_I(inode)->location;
1449 location->objectid = objectid;
1450 location->offset = 0;
1451 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1453 insert_inode_hash(inode);
1456 btrfs_free_path(path);
1457 return ERR_PTR(ret);
1460 static inline u8 btrfs_inode_type(struct inode *inode)
1462 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1465 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1466 struct dentry *dentry, struct inode *inode)
1469 struct btrfs_key key;
1470 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1471 struct inode *parent_inode;
1473 key.objectid = inode->i_ino;
1474 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1477 ret = btrfs_insert_dir_item(trans, root,
1478 dentry->d_name.name, dentry->d_name.len,
1479 dentry->d_parent->d_inode->i_ino,
1480 &key, btrfs_inode_type(inode));
1482 ret = btrfs_insert_inode_ref(trans, root,
1483 dentry->d_name.name,
1486 dentry->d_parent->d_inode->i_ino);
1487 parent_inode = dentry->d_parent->d_inode;
1488 parent_inode->i_size += dentry->d_name.len * 2;
1489 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1490 ret = btrfs_update_inode(trans, root,
1491 dentry->d_parent->d_inode);
1496 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1497 struct dentry *dentry, struct inode *inode)
1499 int err = btrfs_add_link(trans, dentry, inode);
1501 d_instantiate(dentry, inode);
1509 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1510 int mode, dev_t rdev)
1512 struct btrfs_trans_handle *trans;
1513 struct btrfs_root *root = BTRFS_I(dir)->root;
1514 struct inode *inode = NULL;
1518 unsigned long nr = 0;
1520 if (!new_valid_dev(rdev))
1523 mutex_lock(&root->fs_info->fs_mutex);
1524 err = btrfs_check_free_space(root, 1, 0);
1528 trans = btrfs_start_transaction(root, 1);
1529 btrfs_set_trans_block_group(trans, dir);
1531 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1537 inode = btrfs_new_inode(trans, root, objectid,
1538 BTRFS_I(dir)->block_group, mode);
1539 err = PTR_ERR(inode);
1543 btrfs_set_trans_block_group(trans, inode);
1544 err = btrfs_add_nondir(trans, dentry, inode);
1548 inode->i_op = &btrfs_special_inode_operations;
1549 init_special_inode(inode, inode->i_mode, rdev);
1550 btrfs_update_inode(trans, root, inode);
1552 dir->i_sb->s_dirt = 1;
1553 btrfs_update_inode_block_group(trans, inode);
1554 btrfs_update_inode_block_group(trans, dir);
1556 nr = trans->blocks_used;
1557 btrfs_end_transaction(trans, root);
1559 mutex_unlock(&root->fs_info->fs_mutex);
1562 inode_dec_link_count(inode);
1565 btrfs_btree_balance_dirty(root, nr);
1569 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1570 int mode, struct nameidata *nd)
1572 struct btrfs_trans_handle *trans;
1573 struct btrfs_root *root = BTRFS_I(dir)->root;
1574 struct inode *inode = NULL;
1577 unsigned long nr = 0;
1580 mutex_lock(&root->fs_info->fs_mutex);
1581 err = btrfs_check_free_space(root, 1, 0);
1584 trans = btrfs_start_transaction(root, 1);
1585 btrfs_set_trans_block_group(trans, dir);
1587 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1593 inode = btrfs_new_inode(trans, root, objectid,
1594 BTRFS_I(dir)->block_group, mode);
1595 err = PTR_ERR(inode);
1599 btrfs_set_trans_block_group(trans, inode);
1600 err = btrfs_add_nondir(trans, dentry, inode);
1604 inode->i_mapping->a_ops = &btrfs_aops;
1605 inode->i_fop = &btrfs_file_operations;
1606 inode->i_op = &btrfs_file_inode_operations;
1607 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1608 inode->i_mapping, GFP_NOFS);
1609 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1611 dir->i_sb->s_dirt = 1;
1612 btrfs_update_inode_block_group(trans, inode);
1613 btrfs_update_inode_block_group(trans, dir);
1615 nr = trans->blocks_used;
1616 btrfs_end_transaction(trans, root);
1618 mutex_unlock(&root->fs_info->fs_mutex);
1621 inode_dec_link_count(inode);
1624 btrfs_btree_balance_dirty(root, nr);
1628 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1629 struct dentry *dentry)
1631 struct btrfs_trans_handle *trans;
1632 struct btrfs_root *root = BTRFS_I(dir)->root;
1633 struct inode *inode = old_dentry->d_inode;
1634 unsigned long nr = 0;
1638 if (inode->i_nlink == 0)
1641 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1646 mutex_lock(&root->fs_info->fs_mutex);
1647 err = btrfs_check_free_space(root, 1, 0);
1650 trans = btrfs_start_transaction(root, 1);
1652 btrfs_set_trans_block_group(trans, dir);
1653 atomic_inc(&inode->i_count);
1654 err = btrfs_add_nondir(trans, dentry, inode);
1659 dir->i_sb->s_dirt = 1;
1660 btrfs_update_inode_block_group(trans, dir);
1661 err = btrfs_update_inode(trans, root, inode);
1666 nr = trans->blocks_used;
1667 btrfs_end_transaction(trans, root);
1669 mutex_unlock(&root->fs_info->fs_mutex);
1672 inode_dec_link_count(inode);
1675 btrfs_btree_balance_dirty(root, nr);
1679 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1681 struct inode *inode;
1682 struct btrfs_trans_handle *trans;
1683 struct btrfs_root *root = BTRFS_I(dir)->root;
1685 int drop_on_err = 0;
1687 unsigned long nr = 1;
1689 mutex_lock(&root->fs_info->fs_mutex);
1690 err = btrfs_check_free_space(root, 1, 0);
1694 trans = btrfs_start_transaction(root, 1);
1695 btrfs_set_trans_block_group(trans, dir);
1697 if (IS_ERR(trans)) {
1698 err = PTR_ERR(trans);
1702 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1708 inode = btrfs_new_inode(trans, root, objectid,
1709 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1710 if (IS_ERR(inode)) {
1711 err = PTR_ERR(inode);
1716 inode->i_op = &btrfs_dir_inode_operations;
1717 inode->i_fop = &btrfs_dir_file_operations;
1718 btrfs_set_trans_block_group(trans, inode);
1721 err = btrfs_update_inode(trans, root, inode);
1725 err = btrfs_add_link(trans, dentry, inode);
1729 d_instantiate(dentry, inode);
1731 dir->i_sb->s_dirt = 1;
1732 btrfs_update_inode_block_group(trans, inode);
1733 btrfs_update_inode_block_group(trans, dir);
1736 nr = trans->blocks_used;
1737 btrfs_end_transaction(trans, root);
1740 mutex_unlock(&root->fs_info->fs_mutex);
1743 btrfs_btree_balance_dirty(root, nr);
1747 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1748 size_t page_offset, u64 start, u64 end,
1754 u64 extent_start = 0;
1756 u64 objectid = inode->i_ino;
1758 int failed_insert = 0;
1759 struct btrfs_path *path;
1760 struct btrfs_root *root = BTRFS_I(inode)->root;
1761 struct btrfs_file_extent_item *item;
1762 struct extent_buffer *leaf;
1763 struct btrfs_key found_key;
1764 struct extent_map *em = NULL;
1765 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1766 struct btrfs_trans_handle *trans = NULL;
1768 path = btrfs_alloc_path();
1770 mutex_lock(&root->fs_info->fs_mutex);
1773 em = lookup_extent_mapping(em_tree, start, end);
1775 if (em->start > start) {
1776 printk("get_extent start %Lu em start %Lu\n",
1783 em = alloc_extent_map(GFP_NOFS);
1788 em->start = EXTENT_MAP_HOLE;
1789 em->end = EXTENT_MAP_HOLE;
1791 em->bdev = inode->i_sb->s_bdev;
1792 ret = btrfs_lookup_file_extent(trans, root, path,
1793 objectid, start, trans != NULL);
1800 if (path->slots[0] == 0)
1805 leaf = path->nodes[0];
1806 item = btrfs_item_ptr(leaf, path->slots[0],
1807 struct btrfs_file_extent_item);
1808 /* are we inside the extent that was found? */
1809 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1810 found_type = btrfs_key_type(&found_key);
1811 if (found_key.objectid != objectid ||
1812 found_type != BTRFS_EXTENT_DATA_KEY) {
1816 found_type = btrfs_file_extent_type(leaf, item);
1817 extent_start = found_key.offset;
1818 if (found_type == BTRFS_FILE_EXTENT_REG) {
1819 extent_end = extent_start +
1820 btrfs_file_extent_num_bytes(leaf, item);
1822 if (start < extent_start || start >= extent_end) {
1824 if (start < extent_start) {
1825 if (end < extent_start)
1827 em->end = extent_end - 1;
1833 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1835 em->start = extent_start;
1836 em->end = extent_end - 1;
1837 em->block_start = EXTENT_MAP_HOLE;
1838 em->block_end = EXTENT_MAP_HOLE;
1841 bytenr += btrfs_file_extent_offset(leaf, item);
1842 em->block_start = bytenr;
1843 em->block_end = em->block_start +
1844 btrfs_file_extent_num_bytes(leaf, item) - 1;
1845 em->start = extent_start;
1846 em->end = extent_end - 1;
1848 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1852 size_t extent_offset;
1855 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1857 extent_end = (extent_start + size - 1) |
1858 ((u64)root->sectorsize - 1);
1859 if (start < extent_start || start >= extent_end) {
1861 if (start < extent_start) {
1862 if (end < extent_start)
1864 em->end = extent_end;
1870 em->block_start = EXTENT_MAP_INLINE;
1871 em->block_end = EXTENT_MAP_INLINE;
1874 em->start = extent_start;
1875 em->end = extent_start + size - 1;
1879 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1880 extent_start + page_offset;
1881 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1882 size - extent_offset);
1883 em->start = extent_start + extent_offset;
1884 em->end = (em->start + copy_size -1) |
1885 ((u64)root->sectorsize -1);
1887 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1888 if (create == 0 && !PageUptodate(page)) {
1889 read_extent_buffer(leaf, map + page_offset, ptr,
1891 flush_dcache_page(page);
1892 } else if (create && PageUptodate(page)) {
1895 free_extent_map(em);
1897 btrfs_release_path(root, path);
1898 trans = btrfs_start_transaction(root, 1);
1901 write_extent_buffer(leaf, map + page_offset, ptr,
1903 btrfs_mark_buffer_dirty(leaf);
1906 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1909 printk("unkknown found_type %d\n", found_type);
1916 em->block_start = EXTENT_MAP_HOLE;
1917 em->block_end = EXTENT_MAP_HOLE;
1919 btrfs_release_path(root, path);
1920 if (em->start > start || em->end < start) {
1921 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1925 ret = add_extent_mapping(em_tree, em);
1926 if (ret == -EEXIST) {
1927 free_extent_map(em);
1929 if (0 && failed_insert == 1) {
1930 btrfs_drop_extent_cache(inode, start, end);
1933 if (failed_insert > 5) {
1934 printk("failing to insert %Lu %Lu\n", start, end);
1942 btrfs_free_path(path);
1944 ret = btrfs_end_transaction(trans, root);
1948 mutex_unlock(&root->fs_info->fs_mutex);
1950 free_extent_map(em);
1952 return ERR_PTR(err);
1957 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1959 return extent_bmap(mapping, iblock, btrfs_get_extent);
1962 int btrfs_readpage(struct file *file, struct page *page)
1964 struct extent_map_tree *tree;
1965 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1966 return extent_read_full_page(tree, page, btrfs_get_extent);
1969 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1971 struct extent_map_tree *tree;
1974 if (current->flags & PF_MEMALLOC) {
1975 redirty_page_for_writepage(wbc, page);
1979 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1980 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1983 static int btrfs_writepages(struct address_space *mapping,
1984 struct writeback_control *wbc)
1986 struct extent_map_tree *tree;
1987 tree = &BTRFS_I(mapping->host)->extent_tree;
1988 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1992 btrfs_readpages(struct file *file, struct address_space *mapping,
1993 struct list_head *pages, unsigned nr_pages)
1995 struct extent_map_tree *tree;
1996 tree = &BTRFS_I(mapping->host)->extent_tree;
1997 return extent_readpages(tree, mapping, pages, nr_pages,
2001 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2003 struct extent_map_tree *tree;
2006 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2007 ret = try_release_extent_mapping(tree, page);
2009 ClearPagePrivate(page);
2010 set_page_private(page, 0);
2011 page_cache_release(page);
2016 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2018 struct extent_map_tree *tree;
2020 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2021 extent_invalidatepage(tree, page, offset);
2022 btrfs_releasepage(page, GFP_NOFS);
2026 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2027 * called from a page fault handler when a page is first dirtied. Hence we must
2028 * be careful to check for EOF conditions here. We set the page up correctly
2029 * for a written page which means we get ENOSPC checking when writing into
2030 * holes and correct delalloc and unwritten extent mapping on filesystems that
2031 * support these features.
2033 * We are not allowed to take the i_mutex here so we have to play games to
2034 * protect against truncate races as the page could now be beyond EOF. Because
2035 * vmtruncate() writes the inode size before removing pages, once we have the
2036 * page lock we can determine safely if the page is beyond EOF. If it is not
2037 * beyond EOF, then the page is guaranteed safe against truncation until we
2040 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2042 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2043 struct btrfs_root *root = BTRFS_I(inode)->root;
2049 mutex_lock(&root->fs_info->fs_mutex);
2050 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2051 mutex_unlock(&root->fs_info->fs_mutex);
2057 down_read(&BTRFS_I(inode)->root->snap_sem);
2059 wait_on_page_writeback(page);
2060 size = i_size_read(inode);
2061 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2063 if ((page->mapping != inode->i_mapping) ||
2064 (page_start > size)) {
2065 /* page got truncated out from underneath us */
2069 /* page is wholly or partially inside EOF */
2070 if (page_start + PAGE_CACHE_SIZE > size)
2071 end = size & ~PAGE_CACHE_MASK;
2073 end = PAGE_CACHE_SIZE;
2075 ret = btrfs_cow_one_page(inode, page, end);
2078 up_read(&BTRFS_I(inode)->root->snap_sem);
2084 static void btrfs_truncate(struct inode *inode)
2086 struct btrfs_root *root = BTRFS_I(inode)->root;
2088 struct btrfs_trans_handle *trans;
2091 if (!S_ISREG(inode->i_mode))
2093 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2096 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2098 mutex_lock(&root->fs_info->fs_mutex);
2099 trans = btrfs_start_transaction(root, 1);
2100 btrfs_set_trans_block_group(trans, inode);
2102 /* FIXME, add redo link to tree so we don't leak on crash */
2103 ret = btrfs_truncate_in_trans(trans, root, inode);
2104 btrfs_update_inode(trans, root, inode);
2105 nr = trans->blocks_used;
2107 ret = btrfs_end_transaction(trans, root);
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 btrfs_btree_balance_dirty(root, nr);
2113 static int noinline create_subvol(struct btrfs_root *root, char *name,
2116 struct btrfs_trans_handle *trans;
2117 struct btrfs_key key;
2118 struct btrfs_root_item root_item;
2119 struct btrfs_inode_item *inode_item;
2120 struct extent_buffer *leaf;
2121 struct btrfs_root *new_root;
2122 struct inode *inode;
2127 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2128 unsigned long nr = 1;
2130 mutex_lock(&root->fs_info->fs_mutex);
2131 ret = btrfs_check_free_space(root, 1, 0);
2135 trans = btrfs_start_transaction(root, 1);
2138 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2143 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2144 objectid, trans->transid, 0, 0,
2147 return PTR_ERR(leaf);
2149 btrfs_set_header_nritems(leaf, 0);
2150 btrfs_set_header_level(leaf, 0);
2151 btrfs_set_header_bytenr(leaf, leaf->start);
2152 btrfs_set_header_generation(leaf, trans->transid);
2153 btrfs_set_header_owner(leaf, objectid);
2155 write_extent_buffer(leaf, root->fs_info->fsid,
2156 (unsigned long)btrfs_header_fsid(leaf),
2158 btrfs_mark_buffer_dirty(leaf);
2160 inode_item = &root_item.inode;
2161 memset(inode_item, 0, sizeof(*inode_item));
2162 inode_item->generation = cpu_to_le64(1);
2163 inode_item->size = cpu_to_le64(3);
2164 inode_item->nlink = cpu_to_le32(1);
2165 inode_item->nblocks = cpu_to_le64(1);
2166 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2168 btrfs_set_root_bytenr(&root_item, leaf->start);
2169 btrfs_set_root_level(&root_item, 0);
2170 btrfs_set_root_refs(&root_item, 1);
2171 btrfs_set_root_used(&root_item, 0);
2173 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2174 root_item.drop_level = 0;
2176 free_extent_buffer(leaf);
2179 btrfs_set_root_dirid(&root_item, new_dirid);
2181 key.objectid = objectid;
2183 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2184 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2190 * insert the directory item
2192 key.offset = (u64)-1;
2193 dir = root->fs_info->sb->s_root->d_inode;
2194 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2195 name, namelen, dir->i_ino, &key,
2200 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2201 name, namelen, objectid,
2202 root->fs_info->sb->s_root->d_inode->i_ino);
2206 ret = btrfs_commit_transaction(trans, root);
2210 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2213 trans = btrfs_start_transaction(new_root, 1);
2216 inode = btrfs_new_inode(trans, new_root, new_dirid,
2217 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2220 inode->i_op = &btrfs_dir_inode_operations;
2221 inode->i_fop = &btrfs_dir_file_operations;
2222 new_root->inode = inode;
2224 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2228 ret = btrfs_update_inode(trans, new_root, inode);
2232 nr = trans->blocks_used;
2233 err = btrfs_commit_transaction(trans, root);
2237 mutex_unlock(&root->fs_info->fs_mutex);
2238 btrfs_btree_balance_dirty(root, nr);
2242 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2244 struct btrfs_trans_handle *trans;
2245 struct btrfs_key key;
2246 struct btrfs_root_item new_root_item;
2247 struct extent_buffer *tmp;
2251 unsigned long nr = 0;
2253 if (!root->ref_cows)
2256 down_write(&root->snap_sem);
2257 freeze_bdev(root->fs_info->sb->s_bdev);
2258 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2260 mutex_lock(&root->fs_info->fs_mutex);
2261 ret = btrfs_check_free_space(root, 1, 0);
2265 trans = btrfs_start_transaction(root, 1);
2268 ret = btrfs_update_inode(trans, root, root->inode);
2272 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2277 memcpy(&new_root_item, &root->root_item,
2278 sizeof(new_root_item));
2280 key.objectid = objectid;
2282 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2284 extent_buffer_get(root->node);
2285 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
2286 free_extent_buffer(tmp);
2288 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2290 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2291 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
2292 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2294 free_extent_buffer(tmp);
2299 * insert the directory item
2301 key.offset = (u64)-1;
2302 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2304 root->fs_info->sb->s_root->d_inode->i_ino,
2305 &key, BTRFS_FT_DIR);
2310 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2311 name, namelen, objectid,
2312 root->fs_info->sb->s_root->d_inode->i_ino);
2317 nr = trans->blocks_used;
2318 err = btrfs_commit_transaction(trans, root);
2323 mutex_unlock(&root->fs_info->fs_mutex);
2324 up_write(&root->snap_sem);
2325 btrfs_btree_balance_dirty(root, nr);
2329 unsigned long btrfs_force_ra(struct address_space *mapping,
2330 struct file_ra_state *ra, struct file *file,
2331 pgoff_t offset, pgoff_t last_index)
2335 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2336 req_size = last_index - offset + 1;
2337 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2340 req_size = min(last_index - offset + 1, (pgoff_t)128);
2341 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2342 return offset + req_size;
2346 int btrfs_defrag_file(struct file *file) {
2347 struct inode *inode = fdentry(file)->d_inode;
2348 struct btrfs_root *root = BTRFS_I(inode)->root;
2349 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2351 unsigned long last_index;
2352 unsigned long ra_index = 0;
2356 u64 existing_delalloc;
2360 mutex_lock(&root->fs_info->fs_mutex);
2361 ret = btrfs_check_free_space(root, inode->i_size, 0);
2362 mutex_unlock(&root->fs_info->fs_mutex);
2366 mutex_lock(&inode->i_mutex);
2367 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2368 for (i = 0; i <= last_index; i++) {
2369 if (i == ra_index) {
2370 ra_index = btrfs_force_ra(inode->i_mapping,
2372 file, ra_index, last_index);
2374 page = grab_cache_page(inode->i_mapping, i);
2377 if (!PageUptodate(page)) {
2378 btrfs_readpage(NULL, page);
2380 if (!PageUptodate(page)) {
2382 page_cache_release(page);
2386 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2387 page_end = page_start + PAGE_CACHE_SIZE - 1;
2389 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2390 delalloc_start = page_start;
2392 count_range_bits(&BTRFS_I(inode)->extent_tree,
2393 &delalloc_start, page_end,
2394 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2395 set_extent_delalloc(em_tree, page_start,
2396 page_end, GFP_NOFS);
2398 spin_lock(&root->fs_info->delalloc_lock);
2399 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2401 spin_unlock(&root->fs_info->delalloc_lock);
2403 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2404 set_page_dirty(page);
2406 page_cache_release(page);
2407 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2411 mutex_unlock(&inode->i_mutex);
2415 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2419 struct btrfs_ioctl_vol_args *vol_args;
2420 struct btrfs_trans_handle *trans;
2426 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2431 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2435 namelen = strlen(vol_args->name);
2436 if (namelen > BTRFS_VOL_NAME_MAX) {
2441 sizestr = vol_args->name;
2442 if (!strcmp(sizestr, "max"))
2443 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2445 if (sizestr[0] == '-') {
2448 } else if (sizestr[0] == '+') {
2452 new_size = btrfs_parse_size(sizestr);
2453 if (new_size == 0) {
2459 mutex_lock(&root->fs_info->fs_mutex);
2460 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2463 if (new_size > old_size) {
2467 new_size = old_size - new_size;
2468 } else if (mod > 0) {
2469 new_size = old_size + new_size;
2472 if (new_size < 256 * 1024 * 1024) {
2476 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2481 do_div(new_size, root->sectorsize);
2482 new_size *= root->sectorsize;
2484 printk("new size is %Lu\n", new_size);
2485 if (new_size > old_size) {
2486 trans = btrfs_start_transaction(root, 1);
2487 ret = btrfs_grow_extent_tree(trans, root, new_size);
2488 btrfs_commit_transaction(trans, root);
2490 ret = btrfs_shrink_extent_tree(root, new_size);
2494 mutex_unlock(&root->fs_info->fs_mutex);
2500 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2503 struct btrfs_ioctl_vol_args *vol_args;
2504 struct btrfs_dir_item *di;
2505 struct btrfs_path *path;
2510 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2515 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2520 namelen = strlen(vol_args->name);
2521 if (namelen > BTRFS_VOL_NAME_MAX) {
2525 if (strchr(vol_args->name, '/')) {
2530 path = btrfs_alloc_path();
2536 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2537 mutex_lock(&root->fs_info->fs_mutex);
2538 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2540 vol_args->name, namelen, 0);
2541 mutex_unlock(&root->fs_info->fs_mutex);
2542 btrfs_free_path(path);
2544 if (di && !IS_ERR(di)) {
2554 if (root == root->fs_info->tree_root)
2555 ret = create_subvol(root, vol_args->name, namelen);
2557 ret = create_snapshot(root, vol_args->name, namelen);
2563 static int btrfs_ioctl_defrag(struct file *file)
2565 struct inode *inode = fdentry(file)->d_inode;
2566 struct btrfs_root *root = BTRFS_I(inode)->root;
2568 switch (inode->i_mode & S_IFMT) {
2570 mutex_lock(&root->fs_info->fs_mutex);
2571 btrfs_defrag_root(root, 0);
2572 btrfs_defrag_root(root->fs_info->extent_root, 0);
2573 mutex_unlock(&root->fs_info->fs_mutex);
2576 btrfs_defrag_file(file);
2583 long btrfs_ioctl(struct file *file, unsigned int
2584 cmd, unsigned long arg)
2586 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2589 case BTRFS_IOC_SNAP_CREATE:
2590 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2591 case BTRFS_IOC_DEFRAG:
2592 return btrfs_ioctl_defrag(file);
2593 case BTRFS_IOC_RESIZE:
2594 return btrfs_ioctl_resize(root, (void __user *)arg);
2601 * Called inside transaction, so use GFP_NOFS
2603 struct inode *btrfs_alloc_inode(struct super_block *sb)
2605 struct btrfs_inode *ei;
2607 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2611 return &ei->vfs_inode;
2614 void btrfs_destroy_inode(struct inode *inode)
2616 WARN_ON(!list_empty(&inode->i_dentry));
2617 WARN_ON(inode->i_data.nrpages);
2619 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2622 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2623 static void init_once(struct kmem_cache * cachep, void *foo)
2625 static void init_once(void * foo, struct kmem_cache * cachep,
2626 unsigned long flags)
2629 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2631 inode_init_once(&ei->vfs_inode);
2634 void btrfs_destroy_cachep(void)
2636 if (btrfs_inode_cachep)
2637 kmem_cache_destroy(btrfs_inode_cachep);
2638 if (btrfs_trans_handle_cachep)
2639 kmem_cache_destroy(btrfs_trans_handle_cachep);
2640 if (btrfs_transaction_cachep)
2641 kmem_cache_destroy(btrfs_transaction_cachep);
2642 if (btrfs_bit_radix_cachep)
2643 kmem_cache_destroy(btrfs_bit_radix_cachep);
2644 if (btrfs_path_cachep)
2645 kmem_cache_destroy(btrfs_path_cachep);
2648 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2649 unsigned long extra_flags,
2650 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2651 void (*ctor)(struct kmem_cache *, void *)
2653 void (*ctor)(void *, struct kmem_cache *,
2658 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2659 SLAB_MEM_SPREAD | extra_flags), ctor
2660 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2666 int btrfs_init_cachep(void)
2668 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2669 sizeof(struct btrfs_inode),
2671 if (!btrfs_inode_cachep)
2673 btrfs_trans_handle_cachep =
2674 btrfs_cache_create("btrfs_trans_handle_cache",
2675 sizeof(struct btrfs_trans_handle),
2677 if (!btrfs_trans_handle_cachep)
2679 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2680 sizeof(struct btrfs_transaction),
2682 if (!btrfs_transaction_cachep)
2684 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2685 sizeof(struct btrfs_path),
2687 if (!btrfs_path_cachep)
2689 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2690 SLAB_DESTROY_BY_RCU, NULL);
2691 if (!btrfs_bit_radix_cachep)
2695 btrfs_destroy_cachep();
2699 static int btrfs_getattr(struct vfsmount *mnt,
2700 struct dentry *dentry, struct kstat *stat)
2702 struct inode *inode = dentry->d_inode;
2703 generic_fillattr(inode, stat);
2704 stat->blksize = 256 * 1024;
2708 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2709 struct inode * new_dir,struct dentry *new_dentry)
2711 struct btrfs_trans_handle *trans;
2712 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2713 struct inode *new_inode = new_dentry->d_inode;
2714 struct inode *old_inode = old_dentry->d_inode;
2715 struct timespec ctime = CURRENT_TIME;
2716 struct btrfs_path *path;
2719 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2720 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2724 mutex_lock(&root->fs_info->fs_mutex);
2725 ret = btrfs_check_free_space(root, 1, 0);
2729 trans = btrfs_start_transaction(root, 1);
2731 btrfs_set_trans_block_group(trans, new_dir);
2732 path = btrfs_alloc_path();
2738 old_dentry->d_inode->i_nlink++;
2739 old_dir->i_ctime = old_dir->i_mtime = ctime;
2740 new_dir->i_ctime = new_dir->i_mtime = ctime;
2741 old_inode->i_ctime = ctime;
2743 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2748 new_inode->i_ctime = CURRENT_TIME;
2749 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2753 ret = btrfs_add_link(trans, new_dentry, old_inode);
2758 btrfs_free_path(path);
2759 btrfs_end_transaction(trans, root);
2761 mutex_unlock(&root->fs_info->fs_mutex);
2765 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2766 const char *symname)
2768 struct btrfs_trans_handle *trans;
2769 struct btrfs_root *root = BTRFS_I(dir)->root;
2770 struct btrfs_path *path;
2771 struct btrfs_key key;
2772 struct inode *inode = NULL;
2779 struct btrfs_file_extent_item *ei;
2780 struct extent_buffer *leaf;
2781 unsigned long nr = 0;
2783 name_len = strlen(symname) + 1;
2784 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2785 return -ENAMETOOLONG;
2787 mutex_lock(&root->fs_info->fs_mutex);
2788 err = btrfs_check_free_space(root, 1, 0);
2792 trans = btrfs_start_transaction(root, 1);
2793 btrfs_set_trans_block_group(trans, dir);
2795 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2801 inode = btrfs_new_inode(trans, root, objectid,
2802 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2803 err = PTR_ERR(inode);
2807 btrfs_set_trans_block_group(trans, inode);
2808 err = btrfs_add_nondir(trans, dentry, inode);
2812 inode->i_mapping->a_ops = &btrfs_aops;
2813 inode->i_fop = &btrfs_file_operations;
2814 inode->i_op = &btrfs_file_inode_operations;
2815 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2816 inode->i_mapping, GFP_NOFS);
2817 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2819 dir->i_sb->s_dirt = 1;
2820 btrfs_update_inode_block_group(trans, inode);
2821 btrfs_update_inode_block_group(trans, dir);
2825 path = btrfs_alloc_path();
2827 key.objectid = inode->i_ino;
2829 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2830 datasize = btrfs_file_extent_calc_inline_size(name_len);
2831 err = btrfs_insert_empty_item(trans, root, path, &key,
2837 leaf = path->nodes[0];
2838 ei = btrfs_item_ptr(leaf, path->slots[0],
2839 struct btrfs_file_extent_item);
2840 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2841 btrfs_set_file_extent_type(leaf, ei,
2842 BTRFS_FILE_EXTENT_INLINE);
2843 ptr = btrfs_file_extent_inline_start(ei);
2844 write_extent_buffer(leaf, symname, ptr, name_len);
2845 btrfs_mark_buffer_dirty(leaf);
2846 btrfs_free_path(path);
2848 inode->i_op = &btrfs_symlink_inode_operations;
2849 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2850 inode->i_size = name_len - 1;
2851 err = btrfs_update_inode(trans, root, inode);
2856 nr = trans->blocks_used;
2857 btrfs_end_transaction(trans, root);
2859 mutex_unlock(&root->fs_info->fs_mutex);
2861 inode_dec_link_count(inode);
2864 btrfs_btree_balance_dirty(root, nr);
2868 static struct inode_operations btrfs_dir_inode_operations = {
2869 .lookup = btrfs_lookup,
2870 .create = btrfs_create,
2871 .unlink = btrfs_unlink,
2873 .mkdir = btrfs_mkdir,
2874 .rmdir = btrfs_rmdir,
2875 .rename = btrfs_rename,
2876 .symlink = btrfs_symlink,
2877 .setattr = btrfs_setattr,
2878 .mknod = btrfs_mknod,
2879 .setxattr = generic_setxattr,
2880 .getxattr = generic_getxattr,
2881 .listxattr = btrfs_listxattr,
2882 .removexattr = generic_removexattr,
2885 static struct inode_operations btrfs_dir_ro_inode_operations = {
2886 .lookup = btrfs_lookup,
2889 static struct file_operations btrfs_dir_file_operations = {
2890 .llseek = generic_file_llseek,
2891 .read = generic_read_dir,
2892 .readdir = btrfs_readdir,
2893 .unlocked_ioctl = btrfs_ioctl,
2894 #ifdef CONFIG_COMPAT
2895 .compat_ioctl = btrfs_ioctl,
2899 static struct extent_map_ops btrfs_extent_map_ops = {
2900 .fill_delalloc = run_delalloc_range,
2901 .writepage_io_hook = btrfs_writepage_io_hook,
2902 .readpage_io_hook = btrfs_readpage_io_hook,
2903 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2906 static struct address_space_operations btrfs_aops = {
2907 .readpage = btrfs_readpage,
2908 .writepage = btrfs_writepage,
2909 .writepages = btrfs_writepages,
2910 .readpages = btrfs_readpages,
2911 .sync_page = block_sync_page,
2913 .invalidatepage = btrfs_invalidatepage,
2914 .releasepage = btrfs_releasepage,
2915 .set_page_dirty = __set_page_dirty_nobuffers,
2918 static struct address_space_operations btrfs_symlink_aops = {
2919 .readpage = btrfs_readpage,
2920 .writepage = btrfs_writepage,
2921 .invalidatepage = btrfs_invalidatepage,
2922 .releasepage = btrfs_releasepage,
2925 static struct inode_operations btrfs_file_inode_operations = {
2926 .truncate = btrfs_truncate,
2927 .getattr = btrfs_getattr,
2928 .setattr = btrfs_setattr,
2929 .setxattr = generic_setxattr,
2930 .getxattr = generic_getxattr,
2931 .listxattr = btrfs_listxattr,
2932 .removexattr = generic_removexattr,
2935 static struct inode_operations btrfs_special_inode_operations = {
2936 .getattr = btrfs_getattr,
2937 .setattr = btrfs_setattr,
2940 static struct inode_operations btrfs_symlink_inode_operations = {
2941 .readlink = generic_readlink,
2942 .follow_link = page_follow_link_light,
2943 .put_link = page_put_link,