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);
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 struct btrfs_key ins;
110 trans = btrfs_start_transaction(root, 1);
112 btrfs_set_trans_block_group(trans, inode);
114 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
115 num_bytes = max(blocksize, num_bytes);
116 ret = btrfs_drop_extents(trans, root, inode,
117 start, start + num_bytes, start, &alloc_hint);
119 if (alloc_hint == EXTENT_MAP_INLINE)
122 while(num_bytes > 0) {
123 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
124 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
125 root->root_key.objectid,
127 inode->i_ino, start, 0,
128 alloc_hint, (u64)-1, &ins, 1);
133 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
134 start, ins.objectid, ins.offset,
136 num_bytes -= cur_alloc_size;
137 alloc_hint = ins.objectid + ins.offset;
138 start += cur_alloc_size;
140 btrfs_add_ordered_inode(inode);
142 btrfs_end_transaction(trans, root);
146 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
154 struct btrfs_root *root = BTRFS_I(inode)->root;
155 struct extent_buffer *leaf;
157 struct btrfs_path *path;
158 struct btrfs_file_extent_item *item;
161 struct btrfs_key found_key;
163 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
164 path = btrfs_alloc_path();
167 ret = btrfs_lookup_file_extent(NULL, root, path,
168 inode->i_ino, start, 0);
170 btrfs_free_path(path);
176 if (path->slots[0] == 0)
181 leaf = path->nodes[0];
182 item = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_file_extent_item);
185 /* are we inside the extent that was found? */
186 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
187 found_type = btrfs_key_type(&found_key);
188 if (found_key.objectid != inode->i_ino ||
189 found_type != BTRFS_EXTENT_DATA_KEY) {
193 found_type = btrfs_file_extent_type(leaf, item);
194 extent_start = found_key.offset;
195 if (found_type == BTRFS_FILE_EXTENT_REG) {
196 u64 extent_num_bytes;
198 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
199 extent_end = extent_start + extent_num_bytes;
202 if (loops && start != extent_start)
205 if (start < extent_start || start >= extent_end)
208 cow_end = min(end, extent_end - 1);
209 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
214 * we may be called by the resizer, make sure we're inside
215 * the limits of the FS
217 if (bytenr + extent_num_bytes > total_fs_bytes)
220 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
230 btrfs_free_path(path);
233 btrfs_release_path(root, path);
238 cow_file_range(inode, start, cow_end);
243 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
245 struct btrfs_root *root = BTRFS_I(inode)->root;
248 mutex_lock(&root->fs_info->fs_mutex);
249 if (btrfs_test_opt(root, NODATACOW) ||
250 btrfs_test_flag(inode, NODATACOW))
251 ret = run_delalloc_nocow(inode, start, end);
253 ret = cow_file_range(inode, start, end);
255 spin_lock(&root->fs_info->delalloc_lock);
256 num_bytes = end + 1 - start;
257 if (root->fs_info->delalloc_bytes < num_bytes) {
258 printk("delalloc accounting error total %llu sub %llu\n",
259 root->fs_info->delalloc_bytes, num_bytes);
261 root->fs_info->delalloc_bytes -= num_bytes;
263 spin_unlock(&root->fs_info->delalloc_lock);
265 mutex_unlock(&root->fs_info->fs_mutex);
269 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
271 struct inode *inode = page->mapping->host;
272 struct btrfs_root *root = BTRFS_I(inode)->root;
273 struct btrfs_trans_handle *trans;
276 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
277 size_t offset = start - page_start;
278 if (btrfs_test_opt(root, NODATASUM) ||
279 btrfs_test_flag(inode, NODATASUM))
281 mutex_lock(&root->fs_info->fs_mutex);
282 trans = btrfs_start_transaction(root, 1);
283 btrfs_set_trans_block_group(trans, inode);
285 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
286 start, kaddr + offset, end - start + 1);
288 ret = btrfs_end_transaction(trans, root);
290 mutex_unlock(&root->fs_info->fs_mutex);
294 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
297 struct inode *inode = page->mapping->host;
298 struct btrfs_root *root = BTRFS_I(inode)->root;
299 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
300 struct btrfs_csum_item *item;
301 struct btrfs_path *path = NULL;
303 if (btrfs_test_opt(root, NODATASUM) ||
304 btrfs_test_flag(inode, NODATASUM))
306 mutex_lock(&root->fs_info->fs_mutex);
307 path = btrfs_alloc_path();
308 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
311 /* a csum that isn't present is a preallocated region. */
312 if (ret == -ENOENT || ret == -EFBIG)
317 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
319 set_state_private(em_tree, start, csum);
322 btrfs_free_path(path);
323 mutex_unlock(&root->fs_info->fs_mutex);
327 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
329 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
330 struct inode *inode = page->mapping->host;
331 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
335 struct btrfs_root *root = BTRFS_I(inode)->root;
338 if (btrfs_test_opt(root, NODATASUM) ||
339 btrfs_test_flag(inode, NODATASUM))
341 ret = get_state_private(em_tree, start, &private);
342 local_irq_save(flags);
343 kaddr = kmap_atomic(page, KM_IRQ0);
347 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
348 btrfs_csum_final(csum, (char *)&csum);
349 if (csum != private) {
352 kunmap_atomic(kaddr, KM_IRQ0);
353 local_irq_restore(flags);
357 printk("btrfs csum failed ino %lu off %llu\n",
358 page->mapping->host->i_ino, (unsigned long long)start);
359 memset(kaddr + offset, 1, end - start + 1);
360 flush_dcache_page(page);
361 kunmap_atomic(kaddr, KM_IRQ0);
362 local_irq_restore(flags);
366 void btrfs_read_locked_inode(struct inode *inode)
368 struct btrfs_path *path;
369 struct extent_buffer *leaf;
370 struct btrfs_inode_item *inode_item;
371 struct btrfs_inode_timespec *tspec;
372 struct btrfs_root *root = BTRFS_I(inode)->root;
373 struct btrfs_key location;
374 u64 alloc_group_block;
378 path = btrfs_alloc_path();
380 mutex_lock(&root->fs_info->fs_mutex);
381 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
383 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
387 leaf = path->nodes[0];
388 inode_item = btrfs_item_ptr(leaf, path->slots[0],
389 struct btrfs_inode_item);
391 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
392 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
393 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
394 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
395 inode->i_size = btrfs_inode_size(leaf, inode_item);
397 tspec = btrfs_inode_atime(inode_item);
398 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
399 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
401 tspec = btrfs_inode_mtime(inode_item);
402 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
403 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
405 tspec = btrfs_inode_ctime(inode_item);
406 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
407 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
409 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
410 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
412 rdev = btrfs_inode_rdev(leaf, inode_item);
414 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
415 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
417 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
418 if (!BTRFS_I(inode)->block_group) {
419 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
422 btrfs_free_path(path);
425 mutex_unlock(&root->fs_info->fs_mutex);
427 switch (inode->i_mode & S_IFMT) {
429 inode->i_mapping->a_ops = &btrfs_aops;
430 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
431 inode->i_fop = &btrfs_file_operations;
432 inode->i_op = &btrfs_file_inode_operations;
435 inode->i_fop = &btrfs_dir_file_operations;
436 if (root == root->fs_info->tree_root)
437 inode->i_op = &btrfs_dir_ro_inode_operations;
439 inode->i_op = &btrfs_dir_inode_operations;
442 inode->i_op = &btrfs_symlink_inode_operations;
443 inode->i_mapping->a_ops = &btrfs_symlink_aops;
446 init_special_inode(inode, inode->i_mode, rdev);
452 btrfs_release_path(root, path);
453 btrfs_free_path(path);
454 mutex_unlock(&root->fs_info->fs_mutex);
455 make_bad_inode(inode);
458 static void fill_inode_item(struct extent_buffer *leaf,
459 struct btrfs_inode_item *item,
462 btrfs_set_inode_uid(leaf, item, inode->i_uid);
463 btrfs_set_inode_gid(leaf, item, inode->i_gid);
464 btrfs_set_inode_size(leaf, item, inode->i_size);
465 btrfs_set_inode_mode(leaf, item, inode->i_mode);
466 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
468 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
469 inode->i_atime.tv_sec);
470 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
471 inode->i_atime.tv_nsec);
473 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
474 inode->i_mtime.tv_sec);
475 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
476 inode->i_mtime.tv_nsec);
478 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
479 inode->i_ctime.tv_sec);
480 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
481 inode->i_ctime.tv_nsec);
483 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
484 btrfs_set_inode_generation(leaf, item, inode->i_generation);
485 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
486 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
487 btrfs_set_inode_block_group(leaf, item,
488 BTRFS_I(inode)->block_group->key.objectid);
491 int btrfs_update_inode(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root,
495 struct btrfs_inode_item *inode_item;
496 struct btrfs_path *path;
497 struct extent_buffer *leaf;
500 path = btrfs_alloc_path();
502 ret = btrfs_lookup_inode(trans, root, path,
503 &BTRFS_I(inode)->location, 1);
510 leaf = path->nodes[0];
511 inode_item = btrfs_item_ptr(leaf, path->slots[0],
512 struct btrfs_inode_item);
514 fill_inode_item(leaf, inode_item, inode);
515 btrfs_mark_buffer_dirty(leaf);
516 btrfs_set_inode_last_trans(trans, inode);
519 btrfs_release_path(root, path);
520 btrfs_free_path(path);
525 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root,
528 struct dentry *dentry)
530 struct btrfs_path *path;
531 const char *name = dentry->d_name.name;
532 int name_len = dentry->d_name.len;
534 struct extent_buffer *leaf;
535 struct btrfs_dir_item *di;
536 struct btrfs_key key;
538 path = btrfs_alloc_path();
544 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
554 leaf = path->nodes[0];
555 btrfs_dir_item_key_to_cpu(leaf, di, &key);
556 ret = btrfs_delete_one_dir_name(trans, root, path, di);
559 btrfs_release_path(root, path);
561 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
562 key.objectid, name, name_len, -1);
571 ret = btrfs_delete_one_dir_name(trans, root, path, di);
573 dentry->d_inode->i_ctime = dir->i_ctime;
574 ret = btrfs_del_inode_ref(trans, root, name, name_len,
575 dentry->d_inode->i_ino,
576 dentry->d_parent->d_inode->i_ino);
578 printk("failed to delete reference to %.*s, "
579 "inode %lu parent %lu\n", name_len, name,
580 dentry->d_inode->i_ino,
581 dentry->d_parent->d_inode->i_ino);
584 btrfs_free_path(path);
586 dir->i_size -= name_len * 2;
587 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
588 btrfs_update_inode(trans, root, dir);
589 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
590 dentry->d_inode->i_nlink--;
592 drop_nlink(dentry->d_inode);
594 ret = btrfs_update_inode(trans, root, dentry->d_inode);
595 dir->i_sb->s_dirt = 1;
600 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
602 struct btrfs_root *root;
603 struct btrfs_trans_handle *trans;
605 unsigned long nr = 0;
607 root = BTRFS_I(dir)->root;
608 mutex_lock(&root->fs_info->fs_mutex);
610 ret = btrfs_check_free_space(root, 1, 1);
614 trans = btrfs_start_transaction(root, 1);
616 btrfs_set_trans_block_group(trans, dir);
617 ret = btrfs_unlink_trans(trans, root, dir, dentry);
618 nr = trans->blocks_used;
620 btrfs_end_transaction(trans, root);
622 mutex_unlock(&root->fs_info->fs_mutex);
623 btrfs_btree_balance_dirty(root, nr);
624 btrfs_throttle(root);
628 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
630 struct inode *inode = dentry->d_inode;
633 struct btrfs_root *root = BTRFS_I(dir)->root;
634 struct btrfs_trans_handle *trans;
635 unsigned long nr = 0;
637 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
640 mutex_lock(&root->fs_info->fs_mutex);
641 ret = btrfs_check_free_space(root, 1, 1);
645 trans = btrfs_start_transaction(root, 1);
646 btrfs_set_trans_block_group(trans, dir);
648 /* now the directory is empty */
649 err = btrfs_unlink_trans(trans, root, dir, dentry);
654 nr = trans->blocks_used;
655 ret = btrfs_end_transaction(trans, root);
657 mutex_unlock(&root->fs_info->fs_mutex);
658 btrfs_btree_balance_dirty(root, nr);
659 btrfs_throttle(root);
666 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root,
670 struct btrfs_path *path;
675 path = btrfs_alloc_path();
677 ret = btrfs_lookup_inode(trans, root, path,
678 &BTRFS_I(inode)->location, -1);
682 ret = btrfs_del_item(trans, root, path);
683 btrfs_free_path(path);
688 * this can truncate away extent items, csum items and directory items.
689 * It starts at a high offset and removes keys until it can't find
690 * any higher than i_size.
692 * csum items that cross the new i_size are truncated to the new size
695 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
696 struct btrfs_root *root,
700 struct btrfs_path *path;
701 struct btrfs_key key;
702 struct btrfs_key found_key;
704 struct extent_buffer *leaf;
705 struct btrfs_file_extent_item *fi;
706 u64 extent_start = 0;
707 u64 extent_num_bytes = 0;
713 int extent_type = -1;
715 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
716 path = btrfs_alloc_path();
720 /* FIXME, add redo link to tree so we don't leak on crash */
721 key.objectid = inode->i_ino;
722 key.offset = (u64)-1;
726 btrfs_init_path(path);
728 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
733 BUG_ON(path->slots[0] == 0);
736 leaf = path->nodes[0];
737 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
738 found_type = btrfs_key_type(&found_key);
740 if (found_key.objectid != inode->i_ino)
743 if (found_type != BTRFS_CSUM_ITEM_KEY &&
744 found_type != BTRFS_DIR_ITEM_KEY &&
745 found_type != BTRFS_DIR_INDEX_KEY &&
746 found_type != BTRFS_EXTENT_DATA_KEY)
749 item_end = found_key.offset;
750 if (found_type == BTRFS_EXTENT_DATA_KEY) {
751 fi = btrfs_item_ptr(leaf, path->slots[0],
752 struct btrfs_file_extent_item);
753 extent_type = btrfs_file_extent_type(leaf, fi);
754 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
756 btrfs_file_extent_num_bytes(leaf, fi);
757 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
758 struct btrfs_item *item = btrfs_item_nr(leaf,
760 item_end += btrfs_file_extent_inline_len(leaf,
765 if (found_type == BTRFS_CSUM_ITEM_KEY) {
766 ret = btrfs_csum_truncate(trans, root, path,
770 if (item_end < inode->i_size) {
771 if (found_type == BTRFS_DIR_ITEM_KEY) {
772 found_type = BTRFS_INODE_ITEM_KEY;
773 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
774 found_type = BTRFS_CSUM_ITEM_KEY;
775 } else if (found_type) {
780 btrfs_set_key_type(&key, found_type);
781 btrfs_release_path(root, path);
784 if (found_key.offset >= inode->i_size)
790 /* FIXME, shrink the extent if the ref count is only 1 */
791 if (found_type != BTRFS_EXTENT_DATA_KEY)
794 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
796 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
799 btrfs_file_extent_num_bytes(leaf, fi);
800 extent_num_bytes = inode->i_size -
801 found_key.offset + root->sectorsize - 1;
802 btrfs_set_file_extent_num_bytes(leaf, fi,
804 num_dec = (orig_num_bytes -
805 extent_num_bytes) >> 9;
806 if (extent_start != 0) {
807 inode->i_blocks -= num_dec;
809 btrfs_mark_buffer_dirty(leaf);
812 btrfs_file_extent_disk_num_bytes(leaf,
814 /* FIXME blocksize != 4096 */
815 num_dec = btrfs_file_extent_num_bytes(leaf,
817 if (extent_start != 0) {
819 inode->i_blocks -= num_dec;
821 root_gen = btrfs_header_generation(leaf);
822 root_owner = btrfs_header_owner(leaf);
824 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
826 u32 newsize = inode->i_size - found_key.offset;
827 newsize = btrfs_file_extent_calc_inline_size(newsize);
828 ret = btrfs_truncate_item(trans, root, path,
834 ret = btrfs_del_item(trans, root, path);
840 btrfs_release_path(root, path);
842 ret = btrfs_free_extent(trans, root, extent_start,
845 root_gen, inode->i_ino,
846 found_key.offset, 0);
852 btrfs_release_path(root, path);
853 btrfs_free_path(path);
854 inode->i_sb->s_dirt = 1;
858 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
862 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 struct btrfs_root *root = BTRFS_I(inode)->root;
864 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
865 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
866 u64 existing_delalloc;
870 WARN_ON(!PageLocked(page));
871 set_page_extent_mapped(page);
873 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
874 delalloc_start = page_start;
875 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
876 &delalloc_start, page_end,
877 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
878 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
881 spin_lock(&root->fs_info->delalloc_lock);
882 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
883 spin_unlock(&root->fs_info->delalloc_lock);
885 if (zero_start != PAGE_CACHE_SIZE) {
887 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
888 flush_dcache_page(page);
891 set_page_dirty(page);
892 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
898 * taken from block_truncate_page, but does cow as it zeros out
899 * any bytes left in the last page in the file.
901 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
903 struct inode *inode = mapping->host;
904 struct btrfs_root *root = BTRFS_I(inode)->root;
905 u32 blocksize = root->sectorsize;
906 pgoff_t index = from >> PAGE_CACHE_SHIFT;
907 unsigned offset = from & (PAGE_CACHE_SIZE-1);
912 if ((offset & (blocksize - 1)) == 0)
916 page = grab_cache_page(mapping, index);
919 if (!PageUptodate(page)) {
920 ret = btrfs_readpage(NULL, page);
922 if (!PageUptodate(page)) {
927 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
929 ret = btrfs_cow_one_page(inode, page, offset);
932 page_cache_release(page);
937 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
939 struct inode *inode = dentry->d_inode;
942 err = inode_change_ok(inode, attr);
946 if (S_ISREG(inode->i_mode) &&
947 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
948 struct btrfs_trans_handle *trans;
949 struct btrfs_root *root = BTRFS_I(inode)->root;
950 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
952 u64 mask = root->sectorsize - 1;
953 u64 pos = (inode->i_size + mask) & ~mask;
954 u64 block_end = attr->ia_size | mask;
958 if (attr->ia_size <= pos)
961 mutex_lock(&root->fs_info->fs_mutex);
962 err = btrfs_check_free_space(root, 1, 0);
963 mutex_unlock(&root->fs_info->fs_mutex);
967 btrfs_truncate_page(inode->i_mapping, inode->i_size);
969 lock_extent(em_tree, pos, block_end, GFP_NOFS);
970 hole_size = (attr->ia_size - pos + mask) & ~mask;
972 mutex_lock(&root->fs_info->fs_mutex);
973 trans = btrfs_start_transaction(root, 1);
974 btrfs_set_trans_block_group(trans, inode);
975 err = btrfs_drop_extents(trans, root, inode,
976 pos, pos + hole_size, pos,
979 if (alloc_hint != EXTENT_MAP_INLINE) {
980 err = btrfs_insert_file_extent(trans, root,
982 pos, 0, 0, hole_size);
984 btrfs_end_transaction(trans, root);
985 mutex_unlock(&root->fs_info->fs_mutex);
986 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
991 err = inode_setattr(inode, attr);
996 void btrfs_drop_inode(struct inode *inode)
998 if (!BTRFS_I(inode)->ordered_trans || inode->i_nlink) {
999 generic_drop_inode(inode);
1002 /* FIXME, make sure this delete actually ends up in the transaction */
1003 btrfs_del_ordered_inode(inode);
1004 generic_drop_inode(inode);
1007 void btrfs_delete_inode(struct inode *inode)
1009 struct btrfs_trans_handle *trans;
1010 struct btrfs_root *root = BTRFS_I(inode)->root;
1014 truncate_inode_pages(&inode->i_data, 0);
1015 if (is_bad_inode(inode)) {
1020 mutex_lock(&root->fs_info->fs_mutex);
1021 trans = btrfs_start_transaction(root, 1);
1023 btrfs_set_trans_block_group(trans, inode);
1024 ret = btrfs_truncate_in_trans(trans, root, inode);
1026 goto no_delete_lock;
1027 ret = btrfs_delete_xattrs(trans, root, inode);
1029 goto no_delete_lock;
1030 ret = btrfs_free_inode(trans, root, inode);
1032 goto no_delete_lock;
1033 nr = trans->blocks_used;
1035 btrfs_end_transaction(trans, root);
1036 mutex_unlock(&root->fs_info->fs_mutex);
1037 btrfs_btree_balance_dirty(root, nr);
1038 btrfs_throttle(root);
1042 nr = trans->blocks_used;
1043 btrfs_end_transaction(trans, root);
1044 mutex_unlock(&root->fs_info->fs_mutex);
1045 btrfs_btree_balance_dirty(root, nr);
1046 btrfs_throttle(root);
1052 * this returns the key found in the dir entry in the location pointer.
1053 * If no dir entries were found, location->objectid is 0.
1055 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1056 struct btrfs_key *location)
1058 const char *name = dentry->d_name.name;
1059 int namelen = dentry->d_name.len;
1060 struct btrfs_dir_item *di;
1061 struct btrfs_path *path;
1062 struct btrfs_root *root = BTRFS_I(dir)->root;
1065 if (namelen == 1 && strcmp(name, ".") == 0) {
1066 location->objectid = dir->i_ino;
1067 location->type = BTRFS_INODE_ITEM_KEY;
1068 location->offset = 0;
1071 path = btrfs_alloc_path();
1074 if (namelen == 2 && strcmp(name, "..") == 0) {
1075 struct btrfs_key key;
1076 struct extent_buffer *leaf;
1080 key.objectid = dir->i_ino;
1081 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1083 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1087 leaf = path->nodes[0];
1088 slot = path->slots[0];
1089 nritems = btrfs_header_nritems(leaf);
1090 if (slot >= nritems)
1093 btrfs_item_key_to_cpu(leaf, &key, slot);
1094 if (key.objectid != dir->i_ino ||
1095 key.type != BTRFS_INODE_REF_KEY) {
1098 location->objectid = key.offset;
1099 location->type = BTRFS_INODE_ITEM_KEY;
1100 location->offset = 0;
1104 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1108 if (!di || IS_ERR(di)) {
1111 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1113 btrfs_free_path(path);
1116 location->objectid = 0;
1121 * when we hit a tree root in a directory, the btrfs part of the inode
1122 * needs to be changed to reflect the root directory of the tree root. This
1123 * is kind of like crossing a mount point.
1125 static int fixup_tree_root_location(struct btrfs_root *root,
1126 struct btrfs_key *location,
1127 struct btrfs_root **sub_root,
1128 struct dentry *dentry)
1130 struct btrfs_path *path;
1131 struct btrfs_root_item *ri;
1133 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1135 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1138 path = btrfs_alloc_path();
1140 mutex_lock(&root->fs_info->fs_mutex);
1142 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1143 dentry->d_name.name,
1144 dentry->d_name.len);
1145 if (IS_ERR(*sub_root))
1146 return PTR_ERR(*sub_root);
1148 ri = &(*sub_root)->root_item;
1149 location->objectid = btrfs_root_dirid(ri);
1150 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1151 location->offset = 0;
1153 btrfs_free_path(path);
1154 mutex_unlock(&root->fs_info->fs_mutex);
1158 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1160 struct btrfs_iget_args *args = p;
1161 inode->i_ino = args->ino;
1162 BTRFS_I(inode)->root = args->root;
1163 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1164 inode->i_mapping, GFP_NOFS);
1168 static int btrfs_find_actor(struct inode *inode, void *opaque)
1170 struct btrfs_iget_args *args = opaque;
1171 return (args->ino == inode->i_ino &&
1172 args->root == BTRFS_I(inode)->root);
1175 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1178 struct btrfs_iget_args args;
1179 args.ino = objectid;
1180 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1185 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1188 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1189 struct btrfs_root *root)
1191 struct inode *inode;
1192 struct btrfs_iget_args args;
1193 args.ino = objectid;
1196 inode = iget5_locked(s, objectid, btrfs_find_actor,
1197 btrfs_init_locked_inode,
1202 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1203 struct nameidata *nd)
1205 struct inode * inode;
1206 struct btrfs_inode *bi = BTRFS_I(dir);
1207 struct btrfs_root *root = bi->root;
1208 struct btrfs_root *sub_root = root;
1209 struct btrfs_key location;
1212 if (dentry->d_name.len > BTRFS_NAME_LEN)
1213 return ERR_PTR(-ENAMETOOLONG);
1215 mutex_lock(&root->fs_info->fs_mutex);
1216 ret = btrfs_inode_by_name(dir, dentry, &location);
1217 mutex_unlock(&root->fs_info->fs_mutex);
1220 return ERR_PTR(ret);
1223 if (location.objectid) {
1224 ret = fixup_tree_root_location(root, &location, &sub_root,
1227 return ERR_PTR(ret);
1229 return ERR_PTR(-ENOENT);
1230 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1233 return ERR_PTR(-EACCES);
1234 if (inode->i_state & I_NEW) {
1235 /* the inode and parent dir are two different roots */
1236 if (sub_root != root) {
1238 sub_root->inode = inode;
1240 BTRFS_I(inode)->root = sub_root;
1241 memcpy(&BTRFS_I(inode)->location, &location,
1243 btrfs_read_locked_inode(inode);
1244 unlock_new_inode(inode);
1247 return d_splice_alias(inode, dentry);
1250 static unsigned char btrfs_filetype_table[] = {
1251 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1254 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1256 struct inode *inode = filp->f_dentry->d_inode;
1257 struct btrfs_root *root = BTRFS_I(inode)->root;
1258 struct btrfs_item *item;
1259 struct btrfs_dir_item *di;
1260 struct btrfs_key key;
1261 struct btrfs_key found_key;
1262 struct btrfs_path *path;
1265 struct extent_buffer *leaf;
1268 unsigned char d_type;
1273 int key_type = BTRFS_DIR_INDEX_KEY;
1278 /* FIXME, use a real flag for deciding about the key type */
1279 if (root->fs_info->tree_root == root)
1280 key_type = BTRFS_DIR_ITEM_KEY;
1282 /* special case for "." */
1283 if (filp->f_pos == 0) {
1284 over = filldir(dirent, ".", 1,
1292 mutex_lock(&root->fs_info->fs_mutex);
1293 key.objectid = inode->i_ino;
1294 path = btrfs_alloc_path();
1297 /* special case for .., just use the back ref */
1298 if (filp->f_pos == 1) {
1299 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1303 leaf = path->nodes[0];
1304 slot = path->slots[0];
1305 nritems = btrfs_header_nritems(leaf);
1306 if (slot >= nritems) {
1307 btrfs_release_path(root, path);
1308 goto read_dir_items;
1310 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1311 btrfs_release_path(root, path);
1312 if (found_key.objectid != key.objectid ||
1313 found_key.type != BTRFS_INODE_REF_KEY)
1314 goto read_dir_items;
1315 over = filldir(dirent, "..", 2,
1316 2, found_key.offset, DT_DIR);
1323 btrfs_set_key_type(&key, key_type);
1324 key.offset = filp->f_pos;
1326 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1331 leaf = path->nodes[0];
1332 nritems = btrfs_header_nritems(leaf);
1333 slot = path->slots[0];
1334 if (advance || slot >= nritems) {
1335 if (slot >= nritems -1) {
1336 ret = btrfs_next_leaf(root, path);
1339 leaf = path->nodes[0];
1340 nritems = btrfs_header_nritems(leaf);
1341 slot = path->slots[0];
1348 item = btrfs_item_nr(leaf, slot);
1349 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1351 if (found_key.objectid != key.objectid)
1353 if (btrfs_key_type(&found_key) != key_type)
1355 if (found_key.offset < filp->f_pos)
1358 filp->f_pos = found_key.offset;
1360 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1362 di_total = btrfs_item_size(leaf, item);
1363 while(di_cur < di_total) {
1364 struct btrfs_key location;
1366 name_len = btrfs_dir_name_len(leaf, di);
1367 if (name_len < 32) {
1368 name_ptr = tmp_name;
1370 name_ptr = kmalloc(name_len, GFP_NOFS);
1373 read_extent_buffer(leaf, name_ptr,
1374 (unsigned long)(di + 1), name_len);
1376 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1377 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1378 over = filldir(dirent, name_ptr, name_len,
1383 if (name_ptr != tmp_name)
1388 di_len = btrfs_dir_name_len(leaf, di) +
1389 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1391 di = (struct btrfs_dir_item *)((char *)di + di_len);
1398 btrfs_release_path(root, path);
1399 btrfs_free_path(path);
1400 mutex_unlock(&root->fs_info->fs_mutex);
1404 int btrfs_write_inode(struct inode *inode, int wait)
1406 struct btrfs_root *root = BTRFS_I(inode)->root;
1407 struct btrfs_trans_handle *trans;
1411 mutex_lock(&root->fs_info->fs_mutex);
1412 trans = btrfs_start_transaction(root, 1);
1413 btrfs_set_trans_block_group(trans, inode);
1414 ret = btrfs_commit_transaction(trans, root);
1415 mutex_unlock(&root->fs_info->fs_mutex);
1421 * This is somewhat expensive, updating the tree every time the
1422 * inode changes. But, it is most likely to find the inode in cache.
1423 * FIXME, needs more benchmarking...there are no reasons other than performance
1424 * to keep or drop this code.
1426 void btrfs_dirty_inode(struct inode *inode)
1428 struct btrfs_root *root = BTRFS_I(inode)->root;
1429 struct btrfs_trans_handle *trans;
1431 mutex_lock(&root->fs_info->fs_mutex);
1432 trans = btrfs_start_transaction(root, 1);
1433 btrfs_set_trans_block_group(trans, inode);
1434 btrfs_update_inode(trans, root, inode);
1435 btrfs_end_transaction(trans, root);
1436 mutex_unlock(&root->fs_info->fs_mutex);
1439 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1442 struct btrfs_block_group_cache *group,
1445 struct inode *inode;
1446 struct btrfs_inode_item *inode_item;
1447 struct btrfs_key *location;
1448 struct btrfs_path *path;
1452 path = btrfs_alloc_path();
1455 inode = new_inode(root->fs_info->sb);
1457 return ERR_PTR(-ENOMEM);
1459 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1460 inode->i_mapping, GFP_NOFS);
1461 BTRFS_I(inode)->root = root;
1467 group = btrfs_find_block_group(root, group, 0, 0, owner);
1468 BTRFS_I(inode)->block_group = group;
1469 BTRFS_I(inode)->flags = 0;
1470 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1474 inode->i_uid = current->fsuid;
1475 inode->i_gid = current->fsgid;
1476 inode->i_mode = mode;
1477 inode->i_ino = objectid;
1478 inode->i_blocks = 0;
1479 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1480 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1481 struct btrfs_inode_item);
1482 fill_inode_item(path->nodes[0], inode_item, inode);
1483 btrfs_mark_buffer_dirty(path->nodes[0]);
1484 btrfs_free_path(path);
1486 location = &BTRFS_I(inode)->location;
1487 location->objectid = objectid;
1488 location->offset = 0;
1489 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1491 insert_inode_hash(inode);
1494 btrfs_free_path(path);
1495 return ERR_PTR(ret);
1498 static inline u8 btrfs_inode_type(struct inode *inode)
1500 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1503 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1504 struct dentry *dentry, struct inode *inode)
1507 struct btrfs_key key;
1508 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1509 struct inode *parent_inode;
1511 key.objectid = inode->i_ino;
1512 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1515 ret = btrfs_insert_dir_item(trans, root,
1516 dentry->d_name.name, dentry->d_name.len,
1517 dentry->d_parent->d_inode->i_ino,
1518 &key, btrfs_inode_type(inode));
1520 ret = btrfs_insert_inode_ref(trans, root,
1521 dentry->d_name.name,
1524 dentry->d_parent->d_inode->i_ino);
1525 parent_inode = dentry->d_parent->d_inode;
1526 parent_inode->i_size += dentry->d_name.len * 2;
1527 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1528 ret = btrfs_update_inode(trans, root,
1529 dentry->d_parent->d_inode);
1534 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1535 struct dentry *dentry, struct inode *inode)
1537 int err = btrfs_add_link(trans, dentry, inode);
1539 d_instantiate(dentry, inode);
1547 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1548 int mode, dev_t rdev)
1550 struct btrfs_trans_handle *trans;
1551 struct btrfs_root *root = BTRFS_I(dir)->root;
1552 struct inode *inode = NULL;
1556 unsigned long nr = 0;
1558 if (!new_valid_dev(rdev))
1561 mutex_lock(&root->fs_info->fs_mutex);
1562 err = btrfs_check_free_space(root, 1, 0);
1566 trans = btrfs_start_transaction(root, 1);
1567 btrfs_set_trans_block_group(trans, dir);
1569 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1575 inode = btrfs_new_inode(trans, root, objectid,
1576 BTRFS_I(dir)->block_group, mode);
1577 err = PTR_ERR(inode);
1581 btrfs_set_trans_block_group(trans, inode);
1582 err = btrfs_add_nondir(trans, dentry, inode);
1586 inode->i_op = &btrfs_special_inode_operations;
1587 init_special_inode(inode, inode->i_mode, rdev);
1588 btrfs_update_inode(trans, root, inode);
1590 dir->i_sb->s_dirt = 1;
1591 btrfs_update_inode_block_group(trans, inode);
1592 btrfs_update_inode_block_group(trans, dir);
1594 nr = trans->blocks_used;
1595 btrfs_end_transaction(trans, root);
1597 mutex_unlock(&root->fs_info->fs_mutex);
1600 inode_dec_link_count(inode);
1603 btrfs_btree_balance_dirty(root, nr);
1604 btrfs_throttle(root);
1608 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1609 int mode, struct nameidata *nd)
1611 struct btrfs_trans_handle *trans;
1612 struct btrfs_root *root = BTRFS_I(dir)->root;
1613 struct inode *inode = NULL;
1616 unsigned long nr = 0;
1619 mutex_lock(&root->fs_info->fs_mutex);
1620 err = btrfs_check_free_space(root, 1, 0);
1623 trans = btrfs_start_transaction(root, 1);
1624 btrfs_set_trans_block_group(trans, dir);
1626 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1632 inode = btrfs_new_inode(trans, root, objectid,
1633 BTRFS_I(dir)->block_group, mode);
1634 err = PTR_ERR(inode);
1638 btrfs_set_trans_block_group(trans, inode);
1639 err = btrfs_add_nondir(trans, dentry, inode);
1643 inode->i_mapping->a_ops = &btrfs_aops;
1644 inode->i_fop = &btrfs_file_operations;
1645 inode->i_op = &btrfs_file_inode_operations;
1646 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1647 inode->i_mapping, GFP_NOFS);
1648 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1650 dir->i_sb->s_dirt = 1;
1651 btrfs_update_inode_block_group(trans, inode);
1652 btrfs_update_inode_block_group(trans, dir);
1654 nr = trans->blocks_used;
1655 btrfs_end_transaction(trans, root);
1657 mutex_unlock(&root->fs_info->fs_mutex);
1660 inode_dec_link_count(inode);
1663 btrfs_btree_balance_dirty(root, nr);
1664 btrfs_throttle(root);
1668 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1669 struct dentry *dentry)
1671 struct btrfs_trans_handle *trans;
1672 struct btrfs_root *root = BTRFS_I(dir)->root;
1673 struct inode *inode = old_dentry->d_inode;
1674 unsigned long nr = 0;
1678 if (inode->i_nlink == 0)
1681 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1686 mutex_lock(&root->fs_info->fs_mutex);
1687 err = btrfs_check_free_space(root, 1, 0);
1690 trans = btrfs_start_transaction(root, 1);
1692 btrfs_set_trans_block_group(trans, dir);
1693 atomic_inc(&inode->i_count);
1694 err = btrfs_add_nondir(trans, dentry, inode);
1699 dir->i_sb->s_dirt = 1;
1700 btrfs_update_inode_block_group(trans, dir);
1701 err = btrfs_update_inode(trans, root, inode);
1706 nr = trans->blocks_used;
1707 btrfs_end_transaction(trans, root);
1709 mutex_unlock(&root->fs_info->fs_mutex);
1712 inode_dec_link_count(inode);
1715 btrfs_btree_balance_dirty(root, nr);
1716 btrfs_throttle(root);
1720 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1722 struct inode *inode;
1723 struct btrfs_trans_handle *trans;
1724 struct btrfs_root *root = BTRFS_I(dir)->root;
1726 int drop_on_err = 0;
1728 unsigned long nr = 1;
1730 mutex_lock(&root->fs_info->fs_mutex);
1731 err = btrfs_check_free_space(root, 1, 0);
1735 trans = btrfs_start_transaction(root, 1);
1736 btrfs_set_trans_block_group(trans, dir);
1738 if (IS_ERR(trans)) {
1739 err = PTR_ERR(trans);
1743 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1749 inode = btrfs_new_inode(trans, root, objectid,
1750 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1751 if (IS_ERR(inode)) {
1752 err = PTR_ERR(inode);
1757 inode->i_op = &btrfs_dir_inode_operations;
1758 inode->i_fop = &btrfs_dir_file_operations;
1759 btrfs_set_trans_block_group(trans, inode);
1762 err = btrfs_update_inode(trans, root, inode);
1766 err = btrfs_add_link(trans, dentry, inode);
1770 d_instantiate(dentry, inode);
1772 dir->i_sb->s_dirt = 1;
1773 btrfs_update_inode_block_group(trans, inode);
1774 btrfs_update_inode_block_group(trans, dir);
1777 nr = trans->blocks_used;
1778 btrfs_end_transaction(trans, root);
1781 mutex_unlock(&root->fs_info->fs_mutex);
1784 btrfs_btree_balance_dirty(root, nr);
1785 btrfs_throttle(root);
1789 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1790 size_t page_offset, u64 start, u64 end,
1796 u64 extent_start = 0;
1798 u64 objectid = inode->i_ino;
1800 int failed_insert = 0;
1801 struct btrfs_path *path;
1802 struct btrfs_root *root = BTRFS_I(inode)->root;
1803 struct btrfs_file_extent_item *item;
1804 struct extent_buffer *leaf;
1805 struct btrfs_key found_key;
1806 struct extent_map *em = NULL;
1807 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1808 struct btrfs_trans_handle *trans = NULL;
1810 path = btrfs_alloc_path();
1812 mutex_lock(&root->fs_info->fs_mutex);
1815 em = lookup_extent_mapping(em_tree, start, end);
1817 if (em->start > start) {
1818 printk("get_extent start %Lu em start %Lu\n",
1825 em = alloc_extent_map(GFP_NOFS);
1830 em->start = EXTENT_MAP_HOLE;
1831 em->end = EXTENT_MAP_HOLE;
1833 em->bdev = inode->i_sb->s_bdev;
1834 ret = btrfs_lookup_file_extent(trans, root, path,
1835 objectid, start, trans != NULL);
1842 if (path->slots[0] == 0)
1847 leaf = path->nodes[0];
1848 item = btrfs_item_ptr(leaf, path->slots[0],
1849 struct btrfs_file_extent_item);
1850 /* are we inside the extent that was found? */
1851 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1852 found_type = btrfs_key_type(&found_key);
1853 if (found_key.objectid != objectid ||
1854 found_type != BTRFS_EXTENT_DATA_KEY) {
1858 found_type = btrfs_file_extent_type(leaf, item);
1859 extent_start = found_key.offset;
1860 if (found_type == BTRFS_FILE_EXTENT_REG) {
1861 extent_end = extent_start +
1862 btrfs_file_extent_num_bytes(leaf, item);
1864 if (start < extent_start || start >= extent_end) {
1866 if (start < extent_start) {
1867 if (end < extent_start)
1869 em->end = extent_end - 1;
1875 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1877 em->start = extent_start;
1878 em->end = extent_end - 1;
1879 em->block_start = EXTENT_MAP_HOLE;
1880 em->block_end = EXTENT_MAP_HOLE;
1883 bytenr += btrfs_file_extent_offset(leaf, item);
1884 em->block_start = bytenr;
1885 em->block_end = em->block_start +
1886 btrfs_file_extent_num_bytes(leaf, item) - 1;
1887 em->start = extent_start;
1888 em->end = extent_end - 1;
1890 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1894 size_t extent_offset;
1897 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1899 extent_end = (extent_start + size - 1) |
1900 ((u64)root->sectorsize - 1);
1901 if (start < extent_start || start >= extent_end) {
1903 if (start < extent_start) {
1904 if (end < extent_start)
1906 em->end = extent_end;
1912 em->block_start = EXTENT_MAP_INLINE;
1913 em->block_end = EXTENT_MAP_INLINE;
1916 em->start = extent_start;
1917 em->end = extent_start + size - 1;
1921 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1922 extent_start + page_offset;
1923 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1924 size - extent_offset);
1925 em->start = extent_start + extent_offset;
1926 em->end = (em->start + copy_size -1) |
1927 ((u64)root->sectorsize -1);
1929 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1930 if (create == 0 && !PageUptodate(page)) {
1931 read_extent_buffer(leaf, map + page_offset, ptr,
1933 flush_dcache_page(page);
1934 } else if (create && PageUptodate(page)) {
1937 free_extent_map(em);
1939 btrfs_release_path(root, path);
1940 trans = btrfs_start_transaction(root, 1);
1943 write_extent_buffer(leaf, map + page_offset, ptr,
1945 btrfs_mark_buffer_dirty(leaf);
1948 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1951 printk("unkknown found_type %d\n", found_type);
1958 em->block_start = EXTENT_MAP_HOLE;
1959 em->block_end = EXTENT_MAP_HOLE;
1961 btrfs_release_path(root, path);
1962 if (em->start > start || em->end < start) {
1963 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1967 ret = add_extent_mapping(em_tree, em);
1968 if (ret == -EEXIST) {
1969 free_extent_map(em);
1971 if (0 && failed_insert == 1) {
1972 btrfs_drop_extent_cache(inode, start, end);
1975 if (failed_insert > 5) {
1976 printk("failing to insert %Lu %Lu\n", start, end);
1984 btrfs_free_path(path);
1986 ret = btrfs_end_transaction(trans, root);
1990 mutex_unlock(&root->fs_info->fs_mutex);
1992 free_extent_map(em);
1994 return ERR_PTR(err);
1999 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2001 return extent_bmap(mapping, iblock, btrfs_get_extent);
2004 int btrfs_readpage(struct file *file, struct page *page)
2006 struct extent_map_tree *tree;
2007 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2008 return extent_read_full_page(tree, page, btrfs_get_extent);
2011 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2013 struct extent_map_tree *tree;
2016 if (current->flags & PF_MEMALLOC) {
2017 redirty_page_for_writepage(wbc, page);
2021 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2022 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2025 static int btrfs_writepages(struct address_space *mapping,
2026 struct writeback_control *wbc)
2028 struct extent_map_tree *tree;
2029 tree = &BTRFS_I(mapping->host)->extent_tree;
2030 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2034 btrfs_readpages(struct file *file, struct address_space *mapping,
2035 struct list_head *pages, unsigned nr_pages)
2037 struct extent_map_tree *tree;
2038 tree = &BTRFS_I(mapping->host)->extent_tree;
2039 return extent_readpages(tree, mapping, pages, nr_pages,
2043 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2045 struct extent_map_tree *tree;
2048 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2049 ret = try_release_extent_mapping(tree, page);
2051 ClearPagePrivate(page);
2052 set_page_private(page, 0);
2053 page_cache_release(page);
2058 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2060 struct extent_map_tree *tree;
2062 tree = &BTRFS_I(page->mapping->host)->extent_tree;
2063 extent_invalidatepage(tree, page, offset);
2064 btrfs_releasepage(page, GFP_NOFS);
2068 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2069 * called from a page fault handler when a page is first dirtied. Hence we must
2070 * be careful to check for EOF conditions here. We set the page up correctly
2071 * for a written page which means we get ENOSPC checking when writing into
2072 * holes and correct delalloc and unwritten extent mapping on filesystems that
2073 * support these features.
2075 * We are not allowed to take the i_mutex here so we have to play games to
2076 * protect against truncate races as the page could now be beyond EOF. Because
2077 * vmtruncate() writes the inode size before removing pages, once we have the
2078 * page lock we can determine safely if the page is beyond EOF. If it is not
2079 * beyond EOF, then the page is guaranteed safe against truncation until we
2082 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2084 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2085 struct btrfs_root *root = BTRFS_I(inode)->root;
2091 mutex_lock(&root->fs_info->fs_mutex);
2092 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2093 mutex_unlock(&root->fs_info->fs_mutex);
2100 wait_on_page_writeback(page);
2101 size = i_size_read(inode);
2102 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2104 if ((page->mapping != inode->i_mapping) ||
2105 (page_start > size)) {
2106 /* page got truncated out from underneath us */
2110 /* page is wholly or partially inside EOF */
2111 if (page_start + PAGE_CACHE_SIZE > size)
2112 end = size & ~PAGE_CACHE_MASK;
2114 end = PAGE_CACHE_SIZE;
2116 ret = btrfs_cow_one_page(inode, page, end);
2124 static void btrfs_truncate(struct inode *inode)
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2128 struct btrfs_trans_handle *trans;
2131 if (!S_ISREG(inode->i_mode))
2133 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2136 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2138 mutex_lock(&root->fs_info->fs_mutex);
2139 trans = btrfs_start_transaction(root, 1);
2140 btrfs_set_trans_block_group(trans, inode);
2142 /* FIXME, add redo link to tree so we don't leak on crash */
2143 ret = btrfs_truncate_in_trans(trans, root, inode);
2144 btrfs_update_inode(trans, root, inode);
2145 nr = trans->blocks_used;
2147 ret = btrfs_end_transaction(trans, root);
2149 mutex_unlock(&root->fs_info->fs_mutex);
2150 btrfs_btree_balance_dirty(root, nr);
2151 btrfs_throttle(root);
2154 static int noinline create_subvol(struct btrfs_root *root, char *name,
2157 struct btrfs_trans_handle *trans;
2158 struct btrfs_key key;
2159 struct btrfs_root_item root_item;
2160 struct btrfs_inode_item *inode_item;
2161 struct extent_buffer *leaf;
2162 struct btrfs_root *new_root = root;
2163 struct inode *inode;
2168 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2169 unsigned long nr = 1;
2171 mutex_lock(&root->fs_info->fs_mutex);
2172 ret = btrfs_check_free_space(root, 1, 0);
2176 trans = btrfs_start_transaction(root, 1);
2179 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2184 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2185 objectid, trans->transid, 0, 0,
2188 return PTR_ERR(leaf);
2190 btrfs_set_header_nritems(leaf, 0);
2191 btrfs_set_header_level(leaf, 0);
2192 btrfs_set_header_bytenr(leaf, leaf->start);
2193 btrfs_set_header_generation(leaf, trans->transid);
2194 btrfs_set_header_owner(leaf, objectid);
2196 write_extent_buffer(leaf, root->fs_info->fsid,
2197 (unsigned long)btrfs_header_fsid(leaf),
2199 btrfs_mark_buffer_dirty(leaf);
2201 inode_item = &root_item.inode;
2202 memset(inode_item, 0, sizeof(*inode_item));
2203 inode_item->generation = cpu_to_le64(1);
2204 inode_item->size = cpu_to_le64(3);
2205 inode_item->nlink = cpu_to_le32(1);
2206 inode_item->nblocks = cpu_to_le64(1);
2207 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2209 btrfs_set_root_bytenr(&root_item, leaf->start);
2210 btrfs_set_root_level(&root_item, 0);
2211 btrfs_set_root_refs(&root_item, 1);
2212 btrfs_set_root_used(&root_item, 0);
2214 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2215 root_item.drop_level = 0;
2217 free_extent_buffer(leaf);
2220 btrfs_set_root_dirid(&root_item, new_dirid);
2222 key.objectid = objectid;
2224 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2225 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2231 * insert the directory item
2233 key.offset = (u64)-1;
2234 dir = root->fs_info->sb->s_root->d_inode;
2235 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2236 name, namelen, dir->i_ino, &key,
2241 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2242 name, namelen, objectid,
2243 root->fs_info->sb->s_root->d_inode->i_ino);
2247 ret = btrfs_commit_transaction(trans, root);
2251 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2254 trans = btrfs_start_transaction(new_root, 1);
2257 inode = btrfs_new_inode(trans, new_root, new_dirid,
2258 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2261 inode->i_op = &btrfs_dir_inode_operations;
2262 inode->i_fop = &btrfs_dir_file_operations;
2263 new_root->inode = inode;
2265 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2269 ret = btrfs_update_inode(trans, new_root, inode);
2273 nr = trans->blocks_used;
2274 err = btrfs_commit_transaction(trans, new_root);
2278 mutex_unlock(&root->fs_info->fs_mutex);
2279 btrfs_btree_balance_dirty(root, nr);
2280 btrfs_throttle(root);
2284 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2286 struct btrfs_pending_snapshot *pending_snapshot;
2287 struct btrfs_trans_handle *trans;
2290 unsigned long nr = 0;
2292 if (!root->ref_cows)
2295 mutex_lock(&root->fs_info->fs_mutex);
2296 ret = btrfs_check_free_space(root, 1, 0);
2300 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2301 if (!pending_snapshot) {
2305 pending_snapshot->name = kstrndup(name, namelen, GFP_NOFS);
2306 if (!pending_snapshot->name) {
2308 kfree(pending_snapshot);
2311 trans = btrfs_start_transaction(root, 1);
2314 pending_snapshot->root = root;
2315 list_add(&pending_snapshot->list,
2316 &trans->transaction->pending_snapshots);
2317 ret = btrfs_update_inode(trans, root, root->inode);
2318 err = btrfs_commit_transaction(trans, root);
2321 mutex_unlock(&root->fs_info->fs_mutex);
2322 btrfs_btree_balance_dirty(root, nr);
2323 btrfs_throttle(root);
2327 unsigned long btrfs_force_ra(struct address_space *mapping,
2328 struct file_ra_state *ra, struct file *file,
2329 pgoff_t offset, pgoff_t last_index)
2333 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2334 req_size = last_index - offset + 1;
2335 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2338 req_size = min(last_index - offset + 1, (pgoff_t)128);
2339 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2340 return offset + req_size;
2344 int btrfs_defrag_file(struct file *file) {
2345 struct inode *inode = fdentry(file)->d_inode;
2346 struct btrfs_root *root = BTRFS_I(inode)->root;
2347 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2349 unsigned long last_index;
2350 unsigned long ra_index = 0;
2354 u64 existing_delalloc;
2358 mutex_lock(&root->fs_info->fs_mutex);
2359 ret = btrfs_check_free_space(root, inode->i_size, 0);
2360 mutex_unlock(&root->fs_info->fs_mutex);
2364 mutex_lock(&inode->i_mutex);
2365 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2366 for (i = 0; i <= last_index; i++) {
2367 if (i == ra_index) {
2368 ra_index = btrfs_force_ra(inode->i_mapping,
2370 file, ra_index, last_index);
2372 page = grab_cache_page(inode->i_mapping, i);
2375 if (!PageUptodate(page)) {
2376 btrfs_readpage(NULL, page);
2378 if (!PageUptodate(page)) {
2380 page_cache_release(page);
2384 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2385 page_end = page_start + PAGE_CACHE_SIZE - 1;
2387 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2388 delalloc_start = page_start;
2390 count_range_bits(&BTRFS_I(inode)->extent_tree,
2391 &delalloc_start, page_end,
2392 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2393 set_extent_delalloc(em_tree, page_start,
2394 page_end, GFP_NOFS);
2396 spin_lock(&root->fs_info->delalloc_lock);
2397 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2399 spin_unlock(&root->fs_info->delalloc_lock);
2401 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2402 set_page_dirty(page);
2404 page_cache_release(page);
2405 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2409 mutex_unlock(&inode->i_mutex);
2413 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2417 struct btrfs_ioctl_vol_args *vol_args;
2418 struct btrfs_trans_handle *trans;
2424 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2429 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2433 namelen = strlen(vol_args->name);
2434 if (namelen > BTRFS_VOL_NAME_MAX) {
2439 sizestr = vol_args->name;
2440 if (!strcmp(sizestr, "max"))
2441 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2443 if (sizestr[0] == '-') {
2446 } else if (sizestr[0] == '+') {
2450 new_size = btrfs_parse_size(sizestr);
2451 if (new_size == 0) {
2457 mutex_lock(&root->fs_info->fs_mutex);
2458 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2461 if (new_size > old_size) {
2465 new_size = old_size - new_size;
2466 } else if (mod > 0) {
2467 new_size = old_size + new_size;
2470 if (new_size < 256 * 1024 * 1024) {
2474 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2479 do_div(new_size, root->sectorsize);
2480 new_size *= root->sectorsize;
2482 printk("new size is %Lu\n", new_size);
2483 if (new_size > old_size) {
2484 trans = btrfs_start_transaction(root, 1);
2485 ret = btrfs_grow_extent_tree(trans, root, new_size);
2486 btrfs_commit_transaction(trans, root);
2488 ret = btrfs_shrink_extent_tree(root, new_size);
2492 mutex_unlock(&root->fs_info->fs_mutex);
2498 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2501 struct btrfs_ioctl_vol_args *vol_args;
2502 struct btrfs_dir_item *di;
2503 struct btrfs_path *path;
2508 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2513 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2518 namelen = strlen(vol_args->name);
2519 if (namelen > BTRFS_VOL_NAME_MAX) {
2523 if (strchr(vol_args->name, '/')) {
2528 path = btrfs_alloc_path();
2534 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2535 mutex_lock(&root->fs_info->fs_mutex);
2536 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2538 vol_args->name, namelen, 0);
2539 mutex_unlock(&root->fs_info->fs_mutex);
2540 btrfs_free_path(path);
2542 if (di && !IS_ERR(di)) {
2552 if (root == root->fs_info->tree_root)
2553 ret = create_subvol(root, vol_args->name, namelen);
2555 ret = create_snapshot(root, vol_args->name, namelen);
2561 static int btrfs_ioctl_defrag(struct file *file)
2563 struct inode *inode = fdentry(file)->d_inode;
2564 struct btrfs_root *root = BTRFS_I(inode)->root;
2566 switch (inode->i_mode & S_IFMT) {
2568 mutex_lock(&root->fs_info->fs_mutex);
2569 btrfs_defrag_root(root, 0);
2570 btrfs_defrag_root(root->fs_info->extent_root, 0);
2571 mutex_unlock(&root->fs_info->fs_mutex);
2574 btrfs_defrag_file(file);
2581 long btrfs_ioctl(struct file *file, unsigned int
2582 cmd, unsigned long arg)
2584 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2587 case BTRFS_IOC_SNAP_CREATE:
2588 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2589 case BTRFS_IOC_DEFRAG:
2590 return btrfs_ioctl_defrag(file);
2591 case BTRFS_IOC_RESIZE:
2592 return btrfs_ioctl_resize(root, (void __user *)arg);
2599 * Called inside transaction, so use GFP_NOFS
2601 struct inode *btrfs_alloc_inode(struct super_block *sb)
2603 struct btrfs_inode *ei;
2605 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2609 ei->ordered_trans = 0;
2610 return &ei->vfs_inode;
2613 void btrfs_destroy_inode(struct inode *inode)
2615 WARN_ON(!list_empty(&inode->i_dentry));
2616 WARN_ON(inode->i_data.nrpages);
2618 btrfs_drop_extent_cache(inode, 0, (u64)-1);
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 = PAGE_CACHE_SIZE;
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);
2865 btrfs_throttle(root);
2868 static int btrfs_permission(struct inode *inode, int mask,
2869 struct nameidata *nd)
2871 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2873 return generic_permission(inode, mask, NULL);
2876 static struct inode_operations btrfs_dir_inode_operations = {
2877 .lookup = btrfs_lookup,
2878 .create = btrfs_create,
2879 .unlink = btrfs_unlink,
2881 .mkdir = btrfs_mkdir,
2882 .rmdir = btrfs_rmdir,
2883 .rename = btrfs_rename,
2884 .symlink = btrfs_symlink,
2885 .setattr = btrfs_setattr,
2886 .mknod = btrfs_mknod,
2887 .setxattr = generic_setxattr,
2888 .getxattr = generic_getxattr,
2889 .listxattr = btrfs_listxattr,
2890 .removexattr = generic_removexattr,
2891 .permission = btrfs_permission,
2893 static struct inode_operations btrfs_dir_ro_inode_operations = {
2894 .lookup = btrfs_lookup,
2895 .permission = btrfs_permission,
2897 static struct file_operations btrfs_dir_file_operations = {
2898 .llseek = generic_file_llseek,
2899 .read = generic_read_dir,
2900 .readdir = btrfs_readdir,
2901 .unlocked_ioctl = btrfs_ioctl,
2902 #ifdef CONFIG_COMPAT
2903 .compat_ioctl = btrfs_ioctl,
2907 static struct extent_map_ops btrfs_extent_map_ops = {
2908 .fill_delalloc = run_delalloc_range,
2909 .writepage_io_hook = btrfs_writepage_io_hook,
2910 .readpage_io_hook = btrfs_readpage_io_hook,
2911 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2914 static struct address_space_operations btrfs_aops = {
2915 .readpage = btrfs_readpage,
2916 .writepage = btrfs_writepage,
2917 .writepages = btrfs_writepages,
2918 .readpages = btrfs_readpages,
2919 .sync_page = block_sync_page,
2921 .invalidatepage = btrfs_invalidatepage,
2922 .releasepage = btrfs_releasepage,
2923 .set_page_dirty = __set_page_dirty_nobuffers,
2926 static struct address_space_operations btrfs_symlink_aops = {
2927 .readpage = btrfs_readpage,
2928 .writepage = btrfs_writepage,
2929 .invalidatepage = btrfs_invalidatepage,
2930 .releasepage = btrfs_releasepage,
2933 static struct inode_operations btrfs_file_inode_operations = {
2934 .truncate = btrfs_truncate,
2935 .getattr = btrfs_getattr,
2936 .setattr = btrfs_setattr,
2937 .setxattr = generic_setxattr,
2938 .getxattr = generic_getxattr,
2939 .listxattr = btrfs_listxattr,
2940 .removexattr = generic_removexattr,
2941 .permission = btrfs_permission,
2943 static struct inode_operations btrfs_special_inode_operations = {
2944 .getattr = btrfs_getattr,
2945 .setattr = btrfs_setattr,
2946 .permission = btrfs_permission,
2948 static struct inode_operations btrfs_symlink_inode_operations = {
2949 .readlink = generic_readlink,
2950 .follow_link = page_follow_link_light,
2951 .put_link = page_put_link,
2952 .permission = btrfs_permission,