2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/version.h>
35 #include <linux/xattr.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "print-tree.h"
43 struct btrfs_iget_args {
45 struct btrfs_root *root;
48 static struct inode_operations btrfs_dir_inode_operations;
49 static struct inode_operations btrfs_symlink_inode_operations;
50 static struct inode_operations btrfs_dir_ro_inode_operations;
51 static struct inode_operations btrfs_special_inode_operations;
52 static struct inode_operations btrfs_file_inode_operations;
53 static struct address_space_operations btrfs_aops;
54 static struct address_space_operations btrfs_symlink_aops;
55 static struct file_operations btrfs_dir_file_operations;
56 static struct extent_io_ops btrfs_extent_io_ops;
58 static struct kmem_cache *btrfs_inode_cachep;
59 struct kmem_cache *btrfs_trans_handle_cachep;
60 struct kmem_cache *btrfs_transaction_cachep;
61 struct kmem_cache *btrfs_bit_radix_cachep;
62 struct kmem_cache *btrfs_path_cachep;
65 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
95 spin_unlock(&root->fs_info->delalloc_lock);
99 static int cow_file_range(struct inode *inode, u64 start, u64 end)
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
106 u64 blocksize = root->sectorsize;
107 u64 orig_start = start;
109 struct btrfs_key ins;
112 trans = btrfs_start_transaction(root, 1);
114 btrfs_set_trans_block_group(trans, inode);
116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
117 num_bytes = max(blocksize, num_bytes);
118 ret = btrfs_drop_extents(trans, root, inode,
119 start, start + num_bytes, start, &alloc_hint);
120 orig_num_bytes = num_bytes;
122 if (alloc_hint == EXTENT_MAP_INLINE)
125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
139 btrfs_check_file(root, inode);
140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
146 btrfs_add_ordered_inode(inode);
148 btrfs_end_transaction(trans, root);
152 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
160 struct btrfs_root *root = BTRFS_I(inode)->root;
161 struct extent_buffer *leaf;
163 struct btrfs_path *path;
164 struct btrfs_file_extent_item *item;
167 struct btrfs_key found_key;
169 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
170 path = btrfs_alloc_path();
173 ret = btrfs_lookup_file_extent(NULL, root, path,
174 inode->i_ino, start, 0);
176 btrfs_free_path(path);
182 if (path->slots[0] == 0)
187 leaf = path->nodes[0];
188 item = btrfs_item_ptr(leaf, path->slots[0],
189 struct btrfs_file_extent_item);
191 /* are we inside the extent that was found? */
192 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
193 found_type = btrfs_key_type(&found_key);
194 if (found_key.objectid != inode->i_ino ||
195 found_type != BTRFS_EXTENT_DATA_KEY) {
199 found_type = btrfs_file_extent_type(leaf, item);
200 extent_start = found_key.offset;
201 if (found_type == BTRFS_FILE_EXTENT_REG) {
202 u64 extent_num_bytes;
204 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
205 extent_end = extent_start + extent_num_bytes;
208 if (loops && start != extent_start)
211 if (start < extent_start || start >= extent_end)
214 cow_end = min(end, extent_end - 1);
215 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
220 * we may be called by the resizer, make sure we're inside
221 * the limits of the FS
223 if (bytenr + extent_num_bytes > total_fs_bytes)
226 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
236 btrfs_free_path(path);
239 btrfs_release_path(root, path);
244 cow_file_range(inode, start, cow_end);
249 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
251 struct btrfs_root *root = BTRFS_I(inode)->root;
254 mutex_lock(&root->fs_info->fs_mutex);
255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
257 ret = run_delalloc_nocow(inode, start, end);
259 ret = cow_file_range(inode, start, end);
261 spin_lock(&root->fs_info->delalloc_lock);
262 num_bytes = end + 1 - start;
263 if (root->fs_info->delalloc_bytes < num_bytes) {
264 printk("delalloc accounting error total %llu sub %llu\n",
265 root->fs_info->delalloc_bytes, num_bytes);
267 root->fs_info->delalloc_bytes -= num_bytes;
269 spin_unlock(&root->fs_info->delalloc_lock);
271 mutex_unlock(&root->fs_info->fs_mutex);
275 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
277 struct inode *inode = page->mapping->host;
278 struct btrfs_root *root = BTRFS_I(inode)->root;
279 struct btrfs_trans_handle *trans;
282 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
283 size_t offset = start - page_start;
284 if (btrfs_test_opt(root, NODATASUM) ||
285 btrfs_test_flag(inode, NODATASUM))
287 mutex_lock(&root->fs_info->fs_mutex);
288 trans = btrfs_start_transaction(root, 1);
289 btrfs_set_trans_block_group(trans, inode);
291 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
292 start, kaddr + offset, end - start + 1);
294 ret = btrfs_end_transaction(trans, root);
296 mutex_unlock(&root->fs_info->fs_mutex);
300 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
303 struct inode *inode = page->mapping->host;
304 struct btrfs_root *root = BTRFS_I(inode)->root;
305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
306 struct btrfs_csum_item *item;
307 struct btrfs_path *path = NULL;
309 if (btrfs_test_opt(root, NODATASUM) ||
310 btrfs_test_flag(inode, NODATASUM))
312 mutex_lock(&root->fs_info->fs_mutex);
313 path = btrfs_alloc_path();
314 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
317 /* a csum that isn't present is a preallocated region. */
318 if (ret == -ENOENT || ret == -EFBIG)
323 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
325 set_state_private(io_tree, start, csum);
328 btrfs_free_path(path);
329 mutex_unlock(&root->fs_info->fs_mutex);
333 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
335 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
336 struct inode *inode = page->mapping->host;
337 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
341 struct btrfs_root *root = BTRFS_I(inode)->root;
345 if (btrfs_test_opt(root, NODATASUM) ||
346 btrfs_test_flag(inode, NODATASUM))
349 ret = get_state_private(io_tree, start, &private);
350 local_irq_save(flags);
351 kaddr = kmap_atomic(page, KM_IRQ0);
355 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
356 btrfs_csum_final(csum, (char *)&csum);
357 if (csum != private) {
360 kunmap_atomic(kaddr, KM_IRQ0);
361 local_irq_restore(flags);
365 printk("btrfs csum failed ino %lu off %llu\n",
366 page->mapping->host->i_ino, (unsigned long long)start);
367 memset(kaddr + offset, 1, end - start + 1);
368 flush_dcache_page(page);
369 kunmap_atomic(kaddr, KM_IRQ0);
370 local_irq_restore(flags);
374 void btrfs_read_locked_inode(struct inode *inode)
376 struct btrfs_path *path;
377 struct extent_buffer *leaf;
378 struct btrfs_inode_item *inode_item;
379 struct btrfs_inode_timespec *tspec;
380 struct btrfs_root *root = BTRFS_I(inode)->root;
381 struct btrfs_key location;
382 u64 alloc_group_block;
386 path = btrfs_alloc_path();
388 mutex_lock(&root->fs_info->fs_mutex);
389 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
391 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
395 leaf = path->nodes[0];
396 inode_item = btrfs_item_ptr(leaf, path->slots[0],
397 struct btrfs_inode_item);
399 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
400 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
401 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
402 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
403 inode->i_size = btrfs_inode_size(leaf, inode_item);
405 tspec = btrfs_inode_atime(inode_item);
406 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
407 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
409 tspec = btrfs_inode_mtime(inode_item);
410 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
411 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
413 tspec = btrfs_inode_ctime(inode_item);
414 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
415 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
417 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
418 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
420 rdev = btrfs_inode_rdev(leaf, inode_item);
422 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
423 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
425 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
426 if (!BTRFS_I(inode)->block_group) {
427 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
430 btrfs_free_path(path);
433 mutex_unlock(&root->fs_info->fs_mutex);
435 switch (inode->i_mode & S_IFMT) {
437 inode->i_mapping->a_ops = &btrfs_aops;
438 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
439 inode->i_fop = &btrfs_file_operations;
440 inode->i_op = &btrfs_file_inode_operations;
443 inode->i_fop = &btrfs_dir_file_operations;
444 if (root == root->fs_info->tree_root)
445 inode->i_op = &btrfs_dir_ro_inode_operations;
447 inode->i_op = &btrfs_dir_inode_operations;
450 inode->i_op = &btrfs_symlink_inode_operations;
451 inode->i_mapping->a_ops = &btrfs_symlink_aops;
454 init_special_inode(inode, inode->i_mode, rdev);
460 btrfs_release_path(root, path);
461 btrfs_free_path(path);
462 mutex_unlock(&root->fs_info->fs_mutex);
463 make_bad_inode(inode);
466 static void fill_inode_item(struct extent_buffer *leaf,
467 struct btrfs_inode_item *item,
470 btrfs_set_inode_uid(leaf, item, inode->i_uid);
471 btrfs_set_inode_gid(leaf, item, inode->i_gid);
472 btrfs_set_inode_size(leaf, item, inode->i_size);
473 btrfs_set_inode_mode(leaf, item, inode->i_mode);
474 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
476 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
477 inode->i_atime.tv_sec);
478 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
479 inode->i_atime.tv_nsec);
481 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
482 inode->i_mtime.tv_sec);
483 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
484 inode->i_mtime.tv_nsec);
486 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
487 inode->i_ctime.tv_sec);
488 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
489 inode->i_ctime.tv_nsec);
491 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
492 btrfs_set_inode_generation(leaf, item, inode->i_generation);
493 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
494 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
495 btrfs_set_inode_block_group(leaf, item,
496 BTRFS_I(inode)->block_group->key.objectid);
499 int btrfs_update_inode(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root,
503 struct btrfs_inode_item *inode_item;
504 struct btrfs_path *path;
505 struct extent_buffer *leaf;
508 path = btrfs_alloc_path();
510 ret = btrfs_lookup_inode(trans, root, path,
511 &BTRFS_I(inode)->location, 1);
518 leaf = path->nodes[0];
519 inode_item = btrfs_item_ptr(leaf, path->slots[0],
520 struct btrfs_inode_item);
522 fill_inode_item(leaf, inode_item, inode);
523 btrfs_mark_buffer_dirty(leaf);
524 btrfs_set_inode_last_trans(trans, inode);
527 btrfs_release_path(root, path);
528 btrfs_free_path(path);
533 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
534 struct btrfs_root *root,
536 struct dentry *dentry)
538 struct btrfs_path *path;
539 const char *name = dentry->d_name.name;
540 int name_len = dentry->d_name.len;
542 struct extent_buffer *leaf;
543 struct btrfs_dir_item *di;
544 struct btrfs_key key;
546 path = btrfs_alloc_path();
552 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
562 leaf = path->nodes[0];
563 btrfs_dir_item_key_to_cpu(leaf, di, &key);
564 ret = btrfs_delete_one_dir_name(trans, root, path, di);
567 btrfs_release_path(root, path);
569 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
570 key.objectid, name, name_len, -1);
579 ret = btrfs_delete_one_dir_name(trans, root, path, di);
581 dentry->d_inode->i_ctime = dir->i_ctime;
582 ret = btrfs_del_inode_ref(trans, root, name, name_len,
583 dentry->d_inode->i_ino,
584 dentry->d_parent->d_inode->i_ino);
586 printk("failed to delete reference to %.*s, "
587 "inode %lu parent %lu\n", name_len, name,
588 dentry->d_inode->i_ino,
589 dentry->d_parent->d_inode->i_ino);
592 btrfs_free_path(path);
594 dir->i_size -= name_len * 2;
595 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
596 btrfs_update_inode(trans, root, dir);
597 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
598 dentry->d_inode->i_nlink--;
600 drop_nlink(dentry->d_inode);
602 ret = btrfs_update_inode(trans, root, dentry->d_inode);
603 dir->i_sb->s_dirt = 1;
608 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
610 struct btrfs_root *root;
611 struct btrfs_trans_handle *trans;
612 struct inode *inode = dentry->d_inode;
614 unsigned long nr = 0;
616 root = BTRFS_I(dir)->root;
617 mutex_lock(&root->fs_info->fs_mutex);
619 ret = btrfs_check_free_space(root, 1, 1);
623 trans = btrfs_start_transaction(root, 1);
625 btrfs_set_trans_block_group(trans, dir);
626 ret = btrfs_unlink_trans(trans, root, dir, dentry);
627 nr = trans->blocks_used;
629 if (inode->i_nlink == 0) {
631 /* if the inode isn't linked anywhere,
632 * we don't need to worry about
635 found = btrfs_del_ordered_inode(inode);
637 atomic_dec(&inode->i_count);
641 btrfs_end_transaction(trans, root);
643 mutex_unlock(&root->fs_info->fs_mutex);
644 btrfs_btree_balance_dirty(root, nr);
645 btrfs_throttle(root);
649 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
651 struct inode *inode = dentry->d_inode;
654 struct btrfs_root *root = BTRFS_I(dir)->root;
655 struct btrfs_trans_handle *trans;
656 unsigned long nr = 0;
658 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
661 mutex_lock(&root->fs_info->fs_mutex);
662 ret = btrfs_check_free_space(root, 1, 1);
666 trans = btrfs_start_transaction(root, 1);
667 btrfs_set_trans_block_group(trans, dir);
669 /* now the directory is empty */
670 err = btrfs_unlink_trans(trans, root, dir, dentry);
675 nr = trans->blocks_used;
676 ret = btrfs_end_transaction(trans, root);
678 mutex_unlock(&root->fs_info->fs_mutex);
679 btrfs_btree_balance_dirty(root, nr);
680 btrfs_throttle(root);
687 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root,
691 struct btrfs_path *path;
696 path = btrfs_alloc_path();
698 ret = btrfs_lookup_inode(trans, root, path,
699 &BTRFS_I(inode)->location, -1);
703 ret = btrfs_del_item(trans, root, path);
704 btrfs_free_path(path);
709 * this can truncate away extent items, csum items and directory items.
710 * It starts at a high offset and removes keys until it can't find
711 * any higher than i_size.
713 * csum items that cross the new i_size are truncated to the new size
716 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
717 struct btrfs_root *root,
721 struct btrfs_path *path;
722 struct btrfs_key key;
723 struct btrfs_key found_key;
725 struct extent_buffer *leaf;
726 struct btrfs_file_extent_item *fi;
727 u64 extent_start = 0;
728 u64 extent_num_bytes = 0;
734 int extent_type = -1;
736 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
737 path = btrfs_alloc_path();
741 /* FIXME, add redo link to tree so we don't leak on crash */
742 key.objectid = inode->i_ino;
743 key.offset = (u64)-1;
747 btrfs_init_path(path);
749 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
754 BUG_ON(path->slots[0] == 0);
757 leaf = path->nodes[0];
758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
759 found_type = btrfs_key_type(&found_key);
761 if (found_key.objectid != inode->i_ino)
764 if (found_type != BTRFS_CSUM_ITEM_KEY &&
765 found_type != BTRFS_DIR_ITEM_KEY &&
766 found_type != BTRFS_DIR_INDEX_KEY &&
767 found_type != BTRFS_EXTENT_DATA_KEY)
770 item_end = found_key.offset;
771 if (found_type == BTRFS_EXTENT_DATA_KEY) {
772 fi = btrfs_item_ptr(leaf, path->slots[0],
773 struct btrfs_file_extent_item);
774 extent_type = btrfs_file_extent_type(leaf, fi);
775 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
777 btrfs_file_extent_num_bytes(leaf, fi);
778 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
779 struct btrfs_item *item = btrfs_item_nr(leaf,
781 item_end += btrfs_file_extent_inline_len(leaf,
786 if (found_type == BTRFS_CSUM_ITEM_KEY) {
787 ret = btrfs_csum_truncate(trans, root, path,
791 if (item_end < inode->i_size) {
792 if (found_type == BTRFS_DIR_ITEM_KEY) {
793 found_type = BTRFS_INODE_ITEM_KEY;
794 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
795 found_type = BTRFS_CSUM_ITEM_KEY;
796 } else if (found_type) {
801 btrfs_set_key_type(&key, found_type);
802 btrfs_release_path(root, path);
805 if (found_key.offset >= inode->i_size)
811 /* FIXME, shrink the extent if the ref count is only 1 */
812 if (found_type != BTRFS_EXTENT_DATA_KEY)
815 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
817 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
820 btrfs_file_extent_num_bytes(leaf, fi);
821 extent_num_bytes = inode->i_size -
822 found_key.offset + root->sectorsize - 1;
823 btrfs_set_file_extent_num_bytes(leaf, fi,
825 num_dec = (orig_num_bytes -
826 extent_num_bytes) >> 9;
827 if (extent_start != 0) {
828 inode->i_blocks -= num_dec;
830 btrfs_mark_buffer_dirty(leaf);
833 btrfs_file_extent_disk_num_bytes(leaf,
835 /* FIXME blocksize != 4096 */
836 num_dec = btrfs_file_extent_num_bytes(leaf,
838 if (extent_start != 0) {
840 inode->i_blocks -= num_dec;
842 root_gen = btrfs_header_generation(leaf);
843 root_owner = btrfs_header_owner(leaf);
845 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
847 u32 newsize = inode->i_size - found_key.offset;
848 newsize = btrfs_file_extent_calc_inline_size(newsize);
849 ret = btrfs_truncate_item(trans, root, path,
855 ret = btrfs_del_item(trans, root, path);
861 btrfs_release_path(root, path);
863 ret = btrfs_free_extent(trans, root, extent_start,
866 root_gen, inode->i_ino,
867 found_key.offset, 0);
873 btrfs_release_path(root, path);
874 btrfs_free_path(path);
875 inode->i_sb->s_dirt = 1;
879 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
883 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
884 struct btrfs_root *root = BTRFS_I(inode)->root;
885 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
886 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
887 u64 existing_delalloc;
891 WARN_ON(!PageLocked(page));
892 set_page_extent_mapped(page);
894 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
895 delalloc_start = page_start;
896 existing_delalloc = count_range_bits(&BTRFS_I(inode)->io_tree,
897 &delalloc_start, page_end,
898 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
899 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
902 spin_lock(&root->fs_info->delalloc_lock);
903 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
904 spin_unlock(&root->fs_info->delalloc_lock);
906 if (zero_start != PAGE_CACHE_SIZE) {
908 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
909 flush_dcache_page(page);
912 set_page_dirty(page);
913 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
919 * taken from block_truncate_page, but does cow as it zeros out
920 * any bytes left in the last page in the file.
922 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
924 struct inode *inode = mapping->host;
925 struct btrfs_root *root = BTRFS_I(inode)->root;
926 u32 blocksize = root->sectorsize;
927 pgoff_t index = from >> PAGE_CACHE_SHIFT;
928 unsigned offset = from & (PAGE_CACHE_SIZE-1);
933 if ((offset & (blocksize - 1)) == 0)
937 page = grab_cache_page(mapping, index);
940 if (!PageUptodate(page)) {
941 ret = btrfs_readpage(NULL, page);
943 if (!PageUptodate(page)) {
948 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
950 ret = btrfs_cow_one_page(inode, page, offset);
953 page_cache_release(page);
958 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
960 struct inode *inode = dentry->d_inode;
963 err = inode_change_ok(inode, attr);
967 if (S_ISREG(inode->i_mode) &&
968 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
969 struct btrfs_trans_handle *trans;
970 struct btrfs_root *root = BTRFS_I(inode)->root;
971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
973 u64 mask = root->sectorsize - 1;
974 u64 pos = (inode->i_size + mask) & ~mask;
975 u64 block_end = attr->ia_size | mask;
980 if (attr->ia_size <= pos)
983 if (pos != inode->i_size)
984 hole_start = pos + root->sectorsize;
988 mutex_lock(&root->fs_info->fs_mutex);
989 err = btrfs_check_free_space(root, 1, 0);
990 mutex_unlock(&root->fs_info->fs_mutex);
994 btrfs_truncate_page(inode->i_mapping, inode->i_size);
996 lock_extent(io_tree, pos, block_end, GFP_NOFS);
997 hole_size = block_end - hole_start;
999 mutex_lock(&root->fs_info->fs_mutex);
1000 trans = btrfs_start_transaction(root, 1);
1001 btrfs_set_trans_block_group(trans, inode);
1002 err = btrfs_drop_extents(trans, root, inode,
1003 pos, block_end, pos,
1006 if (alloc_hint != EXTENT_MAP_INLINE) {
1007 err = btrfs_insert_file_extent(trans, root,
1011 btrfs_drop_extent_cache(inode, hole_start,
1013 btrfs_check_file(root, inode);
1015 btrfs_end_transaction(trans, root);
1016 mutex_unlock(&root->fs_info->fs_mutex);
1017 unlock_extent(io_tree, pos, block_end, GFP_NOFS);
1022 err = inode_setattr(inode, attr);
1027 void btrfs_put_inode(struct inode *inode)
1031 if (!BTRFS_I(inode)->ordered_trans) {
1035 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1036 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1039 ret = btrfs_del_ordered_inode(inode);
1041 atomic_dec(&inode->i_count);
1045 void btrfs_delete_inode(struct inode *inode)
1047 struct btrfs_trans_handle *trans;
1048 struct btrfs_root *root = BTRFS_I(inode)->root;
1052 truncate_inode_pages(&inode->i_data, 0);
1053 if (is_bad_inode(inode)) {
1058 mutex_lock(&root->fs_info->fs_mutex);
1059 trans = btrfs_start_transaction(root, 1);
1061 btrfs_set_trans_block_group(trans, inode);
1062 ret = btrfs_truncate_in_trans(trans, root, inode);
1064 goto no_delete_lock;
1065 ret = btrfs_delete_xattrs(trans, root, inode);
1067 goto no_delete_lock;
1068 ret = btrfs_free_inode(trans, root, inode);
1070 goto no_delete_lock;
1071 nr = trans->blocks_used;
1073 btrfs_end_transaction(trans, root);
1074 mutex_unlock(&root->fs_info->fs_mutex);
1075 btrfs_btree_balance_dirty(root, nr);
1076 btrfs_throttle(root);
1080 nr = trans->blocks_used;
1081 btrfs_end_transaction(trans, root);
1082 mutex_unlock(&root->fs_info->fs_mutex);
1083 btrfs_btree_balance_dirty(root, nr);
1084 btrfs_throttle(root);
1090 * this returns the key found in the dir entry in the location pointer.
1091 * If no dir entries were found, location->objectid is 0.
1093 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1094 struct btrfs_key *location)
1096 const char *name = dentry->d_name.name;
1097 int namelen = dentry->d_name.len;
1098 struct btrfs_dir_item *di;
1099 struct btrfs_path *path;
1100 struct btrfs_root *root = BTRFS_I(dir)->root;
1103 if (namelen == 1 && strcmp(name, ".") == 0) {
1104 location->objectid = dir->i_ino;
1105 location->type = BTRFS_INODE_ITEM_KEY;
1106 location->offset = 0;
1109 path = btrfs_alloc_path();
1112 if (namelen == 2 && strcmp(name, "..") == 0) {
1113 struct btrfs_key key;
1114 struct extent_buffer *leaf;
1118 key.objectid = dir->i_ino;
1119 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1125 leaf = path->nodes[0];
1126 slot = path->slots[0];
1127 nritems = btrfs_header_nritems(leaf);
1128 if (slot >= nritems)
1131 btrfs_item_key_to_cpu(leaf, &key, slot);
1132 if (key.objectid != dir->i_ino ||
1133 key.type != BTRFS_INODE_REF_KEY) {
1136 location->objectid = key.offset;
1137 location->type = BTRFS_INODE_ITEM_KEY;
1138 location->offset = 0;
1142 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1146 if (!di || IS_ERR(di)) {
1149 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1151 btrfs_free_path(path);
1154 location->objectid = 0;
1159 * when we hit a tree root in a directory, the btrfs part of the inode
1160 * needs to be changed to reflect the root directory of the tree root. This
1161 * is kind of like crossing a mount point.
1163 static int fixup_tree_root_location(struct btrfs_root *root,
1164 struct btrfs_key *location,
1165 struct btrfs_root **sub_root,
1166 struct dentry *dentry)
1168 struct btrfs_path *path;
1169 struct btrfs_root_item *ri;
1171 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1173 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1176 path = btrfs_alloc_path();
1178 mutex_lock(&root->fs_info->fs_mutex);
1180 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1181 dentry->d_name.name,
1182 dentry->d_name.len);
1183 if (IS_ERR(*sub_root))
1184 return PTR_ERR(*sub_root);
1186 ri = &(*sub_root)->root_item;
1187 location->objectid = btrfs_root_dirid(ri);
1188 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1189 location->offset = 0;
1191 btrfs_free_path(path);
1192 mutex_unlock(&root->fs_info->fs_mutex);
1196 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1198 struct btrfs_iget_args *args = p;
1199 inode->i_ino = args->ino;
1200 BTRFS_I(inode)->root = args->root;
1201 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1202 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1203 inode->i_mapping, GFP_NOFS);
1207 static int btrfs_find_actor(struct inode *inode, void *opaque)
1209 struct btrfs_iget_args *args = opaque;
1210 return (args->ino == inode->i_ino &&
1211 args->root == BTRFS_I(inode)->root);
1214 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1217 struct btrfs_iget_args args;
1218 args.ino = objectid;
1219 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1224 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1227 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1228 struct btrfs_root *root)
1230 struct inode *inode;
1231 struct btrfs_iget_args args;
1232 args.ino = objectid;
1235 inode = iget5_locked(s, objectid, btrfs_find_actor,
1236 btrfs_init_locked_inode,
1241 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1242 struct nameidata *nd)
1244 struct inode * inode;
1245 struct btrfs_inode *bi = BTRFS_I(dir);
1246 struct btrfs_root *root = bi->root;
1247 struct btrfs_root *sub_root = root;
1248 struct btrfs_key location;
1251 if (dentry->d_name.len > BTRFS_NAME_LEN)
1252 return ERR_PTR(-ENAMETOOLONG);
1254 mutex_lock(&root->fs_info->fs_mutex);
1255 ret = btrfs_inode_by_name(dir, dentry, &location);
1256 mutex_unlock(&root->fs_info->fs_mutex);
1259 return ERR_PTR(ret);
1262 if (location.objectid) {
1263 ret = fixup_tree_root_location(root, &location, &sub_root,
1266 return ERR_PTR(ret);
1268 return ERR_PTR(-ENOENT);
1269 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1272 return ERR_PTR(-EACCES);
1273 if (inode->i_state & I_NEW) {
1274 /* the inode and parent dir are two different roots */
1275 if (sub_root != root) {
1277 sub_root->inode = inode;
1279 BTRFS_I(inode)->root = sub_root;
1280 memcpy(&BTRFS_I(inode)->location, &location,
1282 btrfs_read_locked_inode(inode);
1283 unlock_new_inode(inode);
1286 return d_splice_alias(inode, dentry);
1289 static unsigned char btrfs_filetype_table[] = {
1290 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1293 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1295 struct inode *inode = filp->f_dentry->d_inode;
1296 struct btrfs_root *root = BTRFS_I(inode)->root;
1297 struct btrfs_item *item;
1298 struct btrfs_dir_item *di;
1299 struct btrfs_key key;
1300 struct btrfs_key found_key;
1301 struct btrfs_path *path;
1304 struct extent_buffer *leaf;
1307 unsigned char d_type;
1312 int key_type = BTRFS_DIR_INDEX_KEY;
1317 /* FIXME, use a real flag for deciding about the key type */
1318 if (root->fs_info->tree_root == root)
1319 key_type = BTRFS_DIR_ITEM_KEY;
1321 /* special case for "." */
1322 if (filp->f_pos == 0) {
1323 over = filldir(dirent, ".", 1,
1331 mutex_lock(&root->fs_info->fs_mutex);
1332 key.objectid = inode->i_ino;
1333 path = btrfs_alloc_path();
1336 /* special case for .., just use the back ref */
1337 if (filp->f_pos == 1) {
1338 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1340 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1342 leaf = path->nodes[0];
1343 slot = path->slots[0];
1344 nritems = btrfs_header_nritems(leaf);
1345 if (slot >= nritems) {
1346 btrfs_release_path(root, path);
1347 goto read_dir_items;
1349 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1350 btrfs_release_path(root, path);
1351 if (found_key.objectid != key.objectid ||
1352 found_key.type != BTRFS_INODE_REF_KEY)
1353 goto read_dir_items;
1354 over = filldir(dirent, "..", 2,
1355 2, found_key.offset, DT_DIR);
1362 btrfs_set_key_type(&key, key_type);
1363 key.offset = filp->f_pos;
1365 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1370 leaf = path->nodes[0];
1371 nritems = btrfs_header_nritems(leaf);
1372 slot = path->slots[0];
1373 if (advance || slot >= nritems) {
1374 if (slot >= nritems -1) {
1375 ret = btrfs_next_leaf(root, path);
1378 leaf = path->nodes[0];
1379 nritems = btrfs_header_nritems(leaf);
1380 slot = path->slots[0];
1387 item = btrfs_item_nr(leaf, slot);
1388 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1390 if (found_key.objectid != key.objectid)
1392 if (btrfs_key_type(&found_key) != key_type)
1394 if (found_key.offset < filp->f_pos)
1397 filp->f_pos = found_key.offset;
1399 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1401 di_total = btrfs_item_size(leaf, item);
1402 while(di_cur < di_total) {
1403 struct btrfs_key location;
1405 name_len = btrfs_dir_name_len(leaf, di);
1406 if (name_len < 32) {
1407 name_ptr = tmp_name;
1409 name_ptr = kmalloc(name_len, GFP_NOFS);
1412 read_extent_buffer(leaf, name_ptr,
1413 (unsigned long)(di + 1), name_len);
1415 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1416 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1417 over = filldir(dirent, name_ptr, name_len,
1422 if (name_ptr != tmp_name)
1427 di_len = btrfs_dir_name_len(leaf, di) +
1428 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1430 di = (struct btrfs_dir_item *)((char *)di + di_len);
1433 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1437 btrfs_release_path(root, path);
1438 btrfs_free_path(path);
1439 mutex_unlock(&root->fs_info->fs_mutex);
1443 int btrfs_write_inode(struct inode *inode, int wait)
1445 struct btrfs_root *root = BTRFS_I(inode)->root;
1446 struct btrfs_trans_handle *trans;
1450 mutex_lock(&root->fs_info->fs_mutex);
1451 trans = btrfs_start_transaction(root, 1);
1452 btrfs_set_trans_block_group(trans, inode);
1453 ret = btrfs_commit_transaction(trans, root);
1454 mutex_unlock(&root->fs_info->fs_mutex);
1460 * This is somewhat expensive, updating the tree every time the
1461 * inode changes. But, it is most likely to find the inode in cache.
1462 * FIXME, needs more benchmarking...there are no reasons other than performance
1463 * to keep or drop this code.
1465 void btrfs_dirty_inode(struct inode *inode)
1467 struct btrfs_root *root = BTRFS_I(inode)->root;
1468 struct btrfs_trans_handle *trans;
1470 mutex_lock(&root->fs_info->fs_mutex);
1471 trans = btrfs_start_transaction(root, 1);
1472 btrfs_set_trans_block_group(trans, inode);
1473 btrfs_update_inode(trans, root, inode);
1474 btrfs_end_transaction(trans, root);
1475 mutex_unlock(&root->fs_info->fs_mutex);
1478 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root,
1481 struct btrfs_block_group_cache *group,
1484 struct inode *inode;
1485 struct btrfs_inode_item *inode_item;
1486 struct btrfs_key *location;
1487 struct btrfs_path *path;
1491 path = btrfs_alloc_path();
1494 inode = new_inode(root->fs_info->sb);
1496 return ERR_PTR(-ENOMEM);
1498 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1499 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1500 inode->i_mapping, GFP_NOFS);
1501 BTRFS_I(inode)->root = root;
1507 group = btrfs_find_block_group(root, group, 0, 0, owner);
1508 BTRFS_I(inode)->block_group = group;
1509 BTRFS_I(inode)->flags = 0;
1510 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1514 inode->i_uid = current->fsuid;
1515 inode->i_gid = current->fsgid;
1516 inode->i_mode = mode;
1517 inode->i_ino = objectid;
1518 inode->i_blocks = 0;
1519 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1520 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1521 struct btrfs_inode_item);
1522 fill_inode_item(path->nodes[0], inode_item, inode);
1523 btrfs_mark_buffer_dirty(path->nodes[0]);
1524 btrfs_free_path(path);
1526 location = &BTRFS_I(inode)->location;
1527 location->objectid = objectid;
1528 location->offset = 0;
1529 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1531 insert_inode_hash(inode);
1534 btrfs_free_path(path);
1535 return ERR_PTR(ret);
1538 static inline u8 btrfs_inode_type(struct inode *inode)
1540 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1543 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1544 struct dentry *dentry, struct inode *inode)
1547 struct btrfs_key key;
1548 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1549 struct inode *parent_inode;
1551 key.objectid = inode->i_ino;
1552 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1555 ret = btrfs_insert_dir_item(trans, root,
1556 dentry->d_name.name, dentry->d_name.len,
1557 dentry->d_parent->d_inode->i_ino,
1558 &key, btrfs_inode_type(inode));
1560 ret = btrfs_insert_inode_ref(trans, root,
1561 dentry->d_name.name,
1564 dentry->d_parent->d_inode->i_ino);
1565 parent_inode = dentry->d_parent->d_inode;
1566 parent_inode->i_size += dentry->d_name.len * 2;
1567 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1568 ret = btrfs_update_inode(trans, root,
1569 dentry->d_parent->d_inode);
1574 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1575 struct dentry *dentry, struct inode *inode)
1577 int err = btrfs_add_link(trans, dentry, inode);
1579 d_instantiate(dentry, inode);
1587 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1588 int mode, dev_t rdev)
1590 struct btrfs_trans_handle *trans;
1591 struct btrfs_root *root = BTRFS_I(dir)->root;
1592 struct inode *inode = NULL;
1596 unsigned long nr = 0;
1598 if (!new_valid_dev(rdev))
1601 mutex_lock(&root->fs_info->fs_mutex);
1602 err = btrfs_check_free_space(root, 1, 0);
1606 trans = btrfs_start_transaction(root, 1);
1607 btrfs_set_trans_block_group(trans, dir);
1609 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1615 inode = btrfs_new_inode(trans, root, objectid,
1616 BTRFS_I(dir)->block_group, mode);
1617 err = PTR_ERR(inode);
1621 btrfs_set_trans_block_group(trans, inode);
1622 err = btrfs_add_nondir(trans, dentry, inode);
1626 inode->i_op = &btrfs_special_inode_operations;
1627 init_special_inode(inode, inode->i_mode, rdev);
1628 btrfs_update_inode(trans, root, inode);
1630 dir->i_sb->s_dirt = 1;
1631 btrfs_update_inode_block_group(trans, inode);
1632 btrfs_update_inode_block_group(trans, dir);
1634 nr = trans->blocks_used;
1635 btrfs_end_transaction(trans, root);
1637 mutex_unlock(&root->fs_info->fs_mutex);
1640 inode_dec_link_count(inode);
1643 btrfs_btree_balance_dirty(root, nr);
1644 btrfs_throttle(root);
1648 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1649 int mode, struct nameidata *nd)
1651 struct btrfs_trans_handle *trans;
1652 struct btrfs_root *root = BTRFS_I(dir)->root;
1653 struct inode *inode = NULL;
1656 unsigned long nr = 0;
1659 mutex_lock(&root->fs_info->fs_mutex);
1660 err = btrfs_check_free_space(root, 1, 0);
1663 trans = btrfs_start_transaction(root, 1);
1664 btrfs_set_trans_block_group(trans, dir);
1666 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1672 inode = btrfs_new_inode(trans, root, objectid,
1673 BTRFS_I(dir)->block_group, mode);
1674 err = PTR_ERR(inode);
1678 btrfs_set_trans_block_group(trans, inode);
1679 err = btrfs_add_nondir(trans, dentry, inode);
1683 inode->i_mapping->a_ops = &btrfs_aops;
1684 inode->i_fop = &btrfs_file_operations;
1685 inode->i_op = &btrfs_file_inode_operations;
1686 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1687 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1688 inode->i_mapping, GFP_NOFS);
1689 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1691 dir->i_sb->s_dirt = 1;
1692 btrfs_update_inode_block_group(trans, inode);
1693 btrfs_update_inode_block_group(trans, dir);
1695 nr = trans->blocks_used;
1696 btrfs_end_transaction(trans, root);
1698 mutex_unlock(&root->fs_info->fs_mutex);
1701 inode_dec_link_count(inode);
1704 btrfs_btree_balance_dirty(root, nr);
1705 btrfs_throttle(root);
1709 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1710 struct dentry *dentry)
1712 struct btrfs_trans_handle *trans;
1713 struct btrfs_root *root = BTRFS_I(dir)->root;
1714 struct inode *inode = old_dentry->d_inode;
1715 unsigned long nr = 0;
1719 if (inode->i_nlink == 0)
1722 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1727 mutex_lock(&root->fs_info->fs_mutex);
1728 err = btrfs_check_free_space(root, 1, 0);
1731 trans = btrfs_start_transaction(root, 1);
1733 btrfs_set_trans_block_group(trans, dir);
1734 atomic_inc(&inode->i_count);
1735 err = btrfs_add_nondir(trans, dentry, inode);
1740 dir->i_sb->s_dirt = 1;
1741 btrfs_update_inode_block_group(trans, dir);
1742 err = btrfs_update_inode(trans, root, inode);
1747 nr = trans->blocks_used;
1748 btrfs_end_transaction(trans, root);
1750 mutex_unlock(&root->fs_info->fs_mutex);
1753 inode_dec_link_count(inode);
1756 btrfs_btree_balance_dirty(root, nr);
1757 btrfs_throttle(root);
1761 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1763 struct inode *inode;
1764 struct btrfs_trans_handle *trans;
1765 struct btrfs_root *root = BTRFS_I(dir)->root;
1767 int drop_on_err = 0;
1769 unsigned long nr = 1;
1771 mutex_lock(&root->fs_info->fs_mutex);
1772 err = btrfs_check_free_space(root, 1, 0);
1776 trans = btrfs_start_transaction(root, 1);
1777 btrfs_set_trans_block_group(trans, dir);
1779 if (IS_ERR(trans)) {
1780 err = PTR_ERR(trans);
1784 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1790 inode = btrfs_new_inode(trans, root, objectid,
1791 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1792 if (IS_ERR(inode)) {
1793 err = PTR_ERR(inode);
1798 inode->i_op = &btrfs_dir_inode_operations;
1799 inode->i_fop = &btrfs_dir_file_operations;
1800 btrfs_set_trans_block_group(trans, inode);
1803 err = btrfs_update_inode(trans, root, inode);
1807 err = btrfs_add_link(trans, dentry, inode);
1811 d_instantiate(dentry, inode);
1813 dir->i_sb->s_dirt = 1;
1814 btrfs_update_inode_block_group(trans, inode);
1815 btrfs_update_inode_block_group(trans, dir);
1818 nr = trans->blocks_used;
1819 btrfs_end_transaction(trans, root);
1822 mutex_unlock(&root->fs_info->fs_mutex);
1825 btrfs_btree_balance_dirty(root, nr);
1826 btrfs_throttle(root);
1830 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1831 size_t page_offset, u64 start, u64 len,
1837 u64 extent_start = 0;
1839 u64 objectid = inode->i_ino;
1841 struct btrfs_path *path;
1842 struct btrfs_root *root = BTRFS_I(inode)->root;
1843 struct btrfs_file_extent_item *item;
1844 struct extent_buffer *leaf;
1845 struct btrfs_key found_key;
1846 struct extent_map *em = NULL;
1847 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1848 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1849 struct btrfs_trans_handle *trans = NULL;
1851 path = btrfs_alloc_path();
1853 mutex_lock(&root->fs_info->fs_mutex);
1856 spin_lock(&em_tree->lock);
1857 em = lookup_extent_mapping(em_tree, start, len);
1858 spin_unlock(&em_tree->lock);
1861 if (em->start > start) {
1862 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1863 start, len, em->start, em->len);
1868 em = alloc_extent_map(GFP_NOFS);
1874 em->start = EXTENT_MAP_HOLE;
1876 em->bdev = inode->i_sb->s_bdev;
1877 ret = btrfs_lookup_file_extent(trans, root, path,
1878 objectid, start, trans != NULL);
1885 if (path->slots[0] == 0)
1890 leaf = path->nodes[0];
1891 item = btrfs_item_ptr(leaf, path->slots[0],
1892 struct btrfs_file_extent_item);
1893 /* are we inside the extent that was found? */
1894 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1895 found_type = btrfs_key_type(&found_key);
1896 if (found_key.objectid != objectid ||
1897 found_type != BTRFS_EXTENT_DATA_KEY) {
1901 found_type = btrfs_file_extent_type(leaf, item);
1902 extent_start = found_key.offset;
1903 if (found_type == BTRFS_FILE_EXTENT_REG) {
1904 extent_end = extent_start +
1905 btrfs_file_extent_num_bytes(leaf, item);
1907 if (start < extent_start || start >= extent_end) {
1909 if (start < extent_start) {
1910 if (start + len <= extent_start)
1912 em->len = extent_end - extent_start;
1918 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1920 em->start = extent_start;
1921 em->len = extent_end - extent_start;
1922 em->block_start = EXTENT_MAP_HOLE;
1925 bytenr += btrfs_file_extent_offset(leaf, item);
1926 em->block_start = bytenr;
1927 em->start = extent_start;
1928 em->len = extent_end - extent_start;
1930 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1934 size_t extent_offset;
1937 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1939 extent_end = (extent_start + size + root->sectorsize - 1) &
1940 ~((u64)root->sectorsize - 1);
1941 if (start < extent_start || start >= extent_end) {
1943 if (start < extent_start) {
1944 if (start + len <= extent_start)
1946 em->len = extent_end - extent_start;
1952 em->block_start = EXTENT_MAP_INLINE;
1955 em->start = extent_start;
1960 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1961 extent_start + page_offset;
1962 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1963 size - extent_offset);
1964 em->start = extent_start + extent_offset;
1965 em->len = copy_size;
1967 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1968 if (create == 0 && !PageUptodate(page)) {
1969 read_extent_buffer(leaf, map + page_offset, ptr,
1971 flush_dcache_page(page);
1972 } else if (create && PageUptodate(page)) {
1975 free_extent_map(em);
1977 btrfs_release_path(root, path);
1978 trans = btrfs_start_transaction(root, 1);
1981 write_extent_buffer(leaf, map + page_offset, ptr,
1983 btrfs_mark_buffer_dirty(leaf);
1986 set_extent_uptodate(io_tree, em->start,
1987 extent_map_end(em) - 1, GFP_NOFS);
1990 printk("unkknown found_type %d\n", found_type);
1997 em->block_start = EXTENT_MAP_HOLE;
1999 btrfs_release_path(root, path);
2000 if (em->start > start || extent_map_end(em) <= start) {
2001 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2007 spin_lock(&em_tree->lock);
2008 ret = add_extent_mapping(em_tree, em);
2009 if (ret == -EEXIST) {
2010 free_extent_map(em);
2011 em = lookup_extent_mapping(em_tree, start, len);
2014 printk("failing to insert %Lu %Lu\n", start, len);
2017 spin_unlock(&em_tree->lock);
2019 btrfs_free_path(path);
2021 ret = btrfs_end_transaction(trans, root);
2025 mutex_unlock(&root->fs_info->fs_mutex);
2027 free_extent_map(em);
2029 return ERR_PTR(err);
2034 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2036 return extent_bmap(mapping, iblock, btrfs_get_extent);
2039 int btrfs_readpage(struct file *file, struct page *page)
2041 struct extent_io_tree *tree;
2042 tree = &BTRFS_I(page->mapping->host)->io_tree;
2043 return extent_read_full_page(tree, page, btrfs_get_extent);
2046 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2048 struct extent_io_tree *tree;
2051 if (current->flags & PF_MEMALLOC) {
2052 redirty_page_for_writepage(wbc, page);
2056 tree = &BTRFS_I(page->mapping->host)->io_tree;
2057 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2060 static int btrfs_writepages(struct address_space *mapping,
2061 struct writeback_control *wbc)
2063 struct extent_io_tree *tree;
2064 tree = &BTRFS_I(mapping->host)->io_tree;
2065 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2069 btrfs_readpages(struct file *file, struct address_space *mapping,
2070 struct list_head *pages, unsigned nr_pages)
2072 struct extent_io_tree *tree;
2073 tree = &BTRFS_I(mapping->host)->io_tree;
2074 return extent_readpages(tree, mapping, pages, nr_pages,
2078 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
2080 struct extent_io_tree *tree;
2081 struct extent_map_tree *map;
2084 tree = &BTRFS_I(page->mapping->host)->io_tree;
2085 map = &BTRFS_I(page->mapping->host)->extent_tree;
2086 ret = try_release_extent_mapping(map, tree, page);
2088 ClearPagePrivate(page);
2089 set_page_private(page, 0);
2090 page_cache_release(page);
2095 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2097 struct extent_io_tree *tree;
2099 tree = &BTRFS_I(page->mapping->host)->io_tree;
2100 extent_invalidatepage(tree, page, offset);
2101 btrfs_releasepage(page, GFP_NOFS);
2105 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2106 * called from a page fault handler when a page is first dirtied. Hence we must
2107 * be careful to check for EOF conditions here. We set the page up correctly
2108 * for a written page which means we get ENOSPC checking when writing into
2109 * holes and correct delalloc and unwritten extent mapping on filesystems that
2110 * support these features.
2112 * We are not allowed to take the i_mutex here so we have to play games to
2113 * protect against truncate races as the page could now be beyond EOF. Because
2114 * vmtruncate() writes the inode size before removing pages, once we have the
2115 * page lock we can determine safely if the page is beyond EOF. If it is not
2116 * beyond EOF, then the page is guaranteed safe against truncation until we
2119 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2121 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2122 struct btrfs_root *root = BTRFS_I(inode)->root;
2128 mutex_lock(&root->fs_info->fs_mutex);
2129 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2130 mutex_unlock(&root->fs_info->fs_mutex);
2137 wait_on_page_writeback(page);
2138 size = i_size_read(inode);
2139 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2141 if ((page->mapping != inode->i_mapping) ||
2142 (page_start > size)) {
2143 /* page got truncated out from underneath us */
2147 /* page is wholly or partially inside EOF */
2148 if (page_start + PAGE_CACHE_SIZE > size)
2149 end = size & ~PAGE_CACHE_MASK;
2151 end = PAGE_CACHE_SIZE;
2153 ret = btrfs_cow_one_page(inode, page, end);
2161 static void btrfs_truncate(struct inode *inode)
2163 struct btrfs_root *root = BTRFS_I(inode)->root;
2165 struct btrfs_trans_handle *trans;
2168 if (!S_ISREG(inode->i_mode))
2170 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2173 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2175 mutex_lock(&root->fs_info->fs_mutex);
2176 trans = btrfs_start_transaction(root, 1);
2177 btrfs_set_trans_block_group(trans, inode);
2179 /* FIXME, add redo link to tree so we don't leak on crash */
2180 ret = btrfs_truncate_in_trans(trans, root, inode);
2181 btrfs_update_inode(trans, root, inode);
2182 nr = trans->blocks_used;
2184 ret = btrfs_end_transaction(trans, root);
2186 mutex_unlock(&root->fs_info->fs_mutex);
2187 btrfs_btree_balance_dirty(root, nr);
2188 btrfs_throttle(root);
2191 static int noinline create_subvol(struct btrfs_root *root, char *name,
2194 struct btrfs_trans_handle *trans;
2195 struct btrfs_key key;
2196 struct btrfs_root_item root_item;
2197 struct btrfs_inode_item *inode_item;
2198 struct extent_buffer *leaf;
2199 struct btrfs_root *new_root = root;
2200 struct inode *inode;
2205 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2206 unsigned long nr = 1;
2208 mutex_lock(&root->fs_info->fs_mutex);
2209 ret = btrfs_check_free_space(root, 1, 0);
2213 trans = btrfs_start_transaction(root, 1);
2216 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2221 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2222 objectid, trans->transid, 0, 0,
2225 return PTR_ERR(leaf);
2227 btrfs_set_header_nritems(leaf, 0);
2228 btrfs_set_header_level(leaf, 0);
2229 btrfs_set_header_bytenr(leaf, leaf->start);
2230 btrfs_set_header_generation(leaf, trans->transid);
2231 btrfs_set_header_owner(leaf, objectid);
2233 write_extent_buffer(leaf, root->fs_info->fsid,
2234 (unsigned long)btrfs_header_fsid(leaf),
2236 btrfs_mark_buffer_dirty(leaf);
2238 inode_item = &root_item.inode;
2239 memset(inode_item, 0, sizeof(*inode_item));
2240 inode_item->generation = cpu_to_le64(1);
2241 inode_item->size = cpu_to_le64(3);
2242 inode_item->nlink = cpu_to_le32(1);
2243 inode_item->nblocks = cpu_to_le64(1);
2244 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2246 btrfs_set_root_bytenr(&root_item, leaf->start);
2247 btrfs_set_root_level(&root_item, 0);
2248 btrfs_set_root_refs(&root_item, 1);
2249 btrfs_set_root_used(&root_item, 0);
2251 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2252 root_item.drop_level = 0;
2254 free_extent_buffer(leaf);
2257 btrfs_set_root_dirid(&root_item, new_dirid);
2259 key.objectid = objectid;
2261 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2262 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2268 * insert the directory item
2270 key.offset = (u64)-1;
2271 dir = root->fs_info->sb->s_root->d_inode;
2272 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2273 name, namelen, dir->i_ino, &key,
2278 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2279 name, namelen, objectid,
2280 root->fs_info->sb->s_root->d_inode->i_ino);
2284 ret = btrfs_commit_transaction(trans, root);
2288 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2291 trans = btrfs_start_transaction(new_root, 1);
2294 inode = btrfs_new_inode(trans, new_root, new_dirid,
2295 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2298 inode->i_op = &btrfs_dir_inode_operations;
2299 inode->i_fop = &btrfs_dir_file_operations;
2300 new_root->inode = inode;
2302 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2306 ret = btrfs_update_inode(trans, new_root, inode);
2310 nr = trans->blocks_used;
2311 err = btrfs_commit_transaction(trans, new_root);
2315 mutex_unlock(&root->fs_info->fs_mutex);
2316 btrfs_btree_balance_dirty(root, nr);
2317 btrfs_throttle(root);
2321 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2323 struct btrfs_pending_snapshot *pending_snapshot;
2324 struct btrfs_trans_handle *trans;
2327 unsigned long nr = 0;
2329 if (!root->ref_cows)
2332 mutex_lock(&root->fs_info->fs_mutex);
2333 ret = btrfs_check_free_space(root, 1, 0);
2337 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2338 if (!pending_snapshot) {
2342 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2343 if (!pending_snapshot->name) {
2345 kfree(pending_snapshot);
2348 memcpy(pending_snapshot->name, name, namelen);
2349 pending_snapshot->name[namelen] = '\0';
2350 trans = btrfs_start_transaction(root, 1);
2352 pending_snapshot->root = root;
2353 list_add(&pending_snapshot->list,
2354 &trans->transaction->pending_snapshots);
2355 ret = btrfs_update_inode(trans, root, root->inode);
2356 err = btrfs_commit_transaction(trans, root);
2359 mutex_unlock(&root->fs_info->fs_mutex);
2360 btrfs_btree_balance_dirty(root, nr);
2361 btrfs_throttle(root);
2365 unsigned long btrfs_force_ra(struct address_space *mapping,
2366 struct file_ra_state *ra, struct file *file,
2367 pgoff_t offset, pgoff_t last_index)
2371 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2372 req_size = last_index - offset + 1;
2373 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2376 req_size = min(last_index - offset + 1, (pgoff_t)128);
2377 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2378 return offset + req_size;
2382 int btrfs_defrag_file(struct file *file) {
2383 struct inode *inode = fdentry(file)->d_inode;
2384 struct btrfs_root *root = BTRFS_I(inode)->root;
2385 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2387 unsigned long last_index;
2388 unsigned long ra_index = 0;
2392 u64 existing_delalloc;
2396 mutex_lock(&root->fs_info->fs_mutex);
2397 ret = btrfs_check_free_space(root, inode->i_size, 0);
2398 mutex_unlock(&root->fs_info->fs_mutex);
2402 mutex_lock(&inode->i_mutex);
2403 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2404 for (i = 0; i <= last_index; i++) {
2405 if (i == ra_index) {
2406 ra_index = btrfs_force_ra(inode->i_mapping,
2408 file, ra_index, last_index);
2410 page = grab_cache_page(inode->i_mapping, i);
2413 if (!PageUptodate(page)) {
2414 btrfs_readpage(NULL, page);
2416 if (!PageUptodate(page)) {
2418 page_cache_release(page);
2422 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2423 page_end = page_start + PAGE_CACHE_SIZE - 1;
2425 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2426 delalloc_start = page_start;
2428 count_range_bits(&BTRFS_I(inode)->io_tree,
2429 &delalloc_start, page_end,
2430 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2431 set_extent_delalloc(io_tree, page_start,
2432 page_end, GFP_NOFS);
2434 spin_lock(&root->fs_info->delalloc_lock);
2435 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2437 spin_unlock(&root->fs_info->delalloc_lock);
2439 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2440 set_page_dirty(page);
2442 page_cache_release(page);
2443 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2447 mutex_unlock(&inode->i_mutex);
2451 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2455 struct btrfs_ioctl_vol_args *vol_args;
2456 struct btrfs_trans_handle *trans;
2462 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2467 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2471 namelen = strlen(vol_args->name);
2472 if (namelen > BTRFS_VOL_NAME_MAX) {
2477 sizestr = vol_args->name;
2478 if (!strcmp(sizestr, "max"))
2479 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2481 if (sizestr[0] == '-') {
2484 } else if (sizestr[0] == '+') {
2488 new_size = btrfs_parse_size(sizestr);
2489 if (new_size == 0) {
2495 mutex_lock(&root->fs_info->fs_mutex);
2496 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2499 if (new_size > old_size) {
2503 new_size = old_size - new_size;
2504 } else if (mod > 0) {
2505 new_size = old_size + new_size;
2508 if (new_size < 256 * 1024 * 1024) {
2512 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2517 do_div(new_size, root->sectorsize);
2518 new_size *= root->sectorsize;
2520 printk("new size is %Lu\n", new_size);
2521 if (new_size > old_size) {
2522 trans = btrfs_start_transaction(root, 1);
2523 ret = btrfs_grow_extent_tree(trans, root, new_size);
2524 btrfs_commit_transaction(trans, root);
2526 ret = btrfs_shrink_extent_tree(root, new_size);
2530 mutex_unlock(&root->fs_info->fs_mutex);
2536 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2539 struct btrfs_ioctl_vol_args *vol_args;
2540 struct btrfs_dir_item *di;
2541 struct btrfs_path *path;
2546 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2551 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2556 namelen = strlen(vol_args->name);
2557 if (namelen > BTRFS_VOL_NAME_MAX) {
2561 if (strchr(vol_args->name, '/')) {
2566 path = btrfs_alloc_path();
2572 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2573 mutex_lock(&root->fs_info->fs_mutex);
2574 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2576 vol_args->name, namelen, 0);
2577 mutex_unlock(&root->fs_info->fs_mutex);
2578 btrfs_free_path(path);
2580 if (di && !IS_ERR(di)) {
2590 if (root == root->fs_info->tree_root)
2591 ret = create_subvol(root, vol_args->name, namelen);
2593 ret = create_snapshot(root, vol_args->name, namelen);
2599 static int btrfs_ioctl_defrag(struct file *file)
2601 struct inode *inode = fdentry(file)->d_inode;
2602 struct btrfs_root *root = BTRFS_I(inode)->root;
2604 switch (inode->i_mode & S_IFMT) {
2606 mutex_lock(&root->fs_info->fs_mutex);
2607 btrfs_defrag_root(root, 0);
2608 btrfs_defrag_root(root->fs_info->extent_root, 0);
2609 mutex_unlock(&root->fs_info->fs_mutex);
2612 btrfs_defrag_file(file);
2619 long btrfs_ioctl(struct file *file, unsigned int
2620 cmd, unsigned long arg)
2622 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2625 case BTRFS_IOC_SNAP_CREATE:
2626 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2627 case BTRFS_IOC_DEFRAG:
2628 return btrfs_ioctl_defrag(file);
2629 case BTRFS_IOC_RESIZE:
2630 return btrfs_ioctl_resize(root, (void __user *)arg);
2637 * Called inside transaction, so use GFP_NOFS
2639 struct inode *btrfs_alloc_inode(struct super_block *sb)
2641 struct btrfs_inode *ei;
2643 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2647 ei->ordered_trans = 0;
2648 return &ei->vfs_inode;
2651 void btrfs_destroy_inode(struct inode *inode)
2653 WARN_ON(!list_empty(&inode->i_dentry));
2654 WARN_ON(inode->i_data.nrpages);
2656 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2657 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2660 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2661 static void init_once(struct kmem_cache * cachep, void *foo)
2663 static void init_once(void * foo, struct kmem_cache * cachep,
2664 unsigned long flags)
2667 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2669 inode_init_once(&ei->vfs_inode);
2672 void btrfs_destroy_cachep(void)
2674 if (btrfs_inode_cachep)
2675 kmem_cache_destroy(btrfs_inode_cachep);
2676 if (btrfs_trans_handle_cachep)
2677 kmem_cache_destroy(btrfs_trans_handle_cachep);
2678 if (btrfs_transaction_cachep)
2679 kmem_cache_destroy(btrfs_transaction_cachep);
2680 if (btrfs_bit_radix_cachep)
2681 kmem_cache_destroy(btrfs_bit_radix_cachep);
2682 if (btrfs_path_cachep)
2683 kmem_cache_destroy(btrfs_path_cachep);
2686 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2687 unsigned long extra_flags,
2688 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2689 void (*ctor)(struct kmem_cache *, void *)
2691 void (*ctor)(void *, struct kmem_cache *,
2696 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2697 SLAB_MEM_SPREAD | extra_flags), ctor
2698 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2704 int btrfs_init_cachep(void)
2706 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2707 sizeof(struct btrfs_inode),
2709 if (!btrfs_inode_cachep)
2711 btrfs_trans_handle_cachep =
2712 btrfs_cache_create("btrfs_trans_handle_cache",
2713 sizeof(struct btrfs_trans_handle),
2715 if (!btrfs_trans_handle_cachep)
2717 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2718 sizeof(struct btrfs_transaction),
2720 if (!btrfs_transaction_cachep)
2722 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2723 sizeof(struct btrfs_path),
2725 if (!btrfs_path_cachep)
2727 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2728 SLAB_DESTROY_BY_RCU, NULL);
2729 if (!btrfs_bit_radix_cachep)
2733 btrfs_destroy_cachep();
2737 static int btrfs_getattr(struct vfsmount *mnt,
2738 struct dentry *dentry, struct kstat *stat)
2740 struct inode *inode = dentry->d_inode;
2741 generic_fillattr(inode, stat);
2742 stat->blksize = PAGE_CACHE_SIZE;
2746 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2747 struct inode * new_dir,struct dentry *new_dentry)
2749 struct btrfs_trans_handle *trans;
2750 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2751 struct inode *new_inode = new_dentry->d_inode;
2752 struct inode *old_inode = old_dentry->d_inode;
2753 struct timespec ctime = CURRENT_TIME;
2754 struct btrfs_path *path;
2757 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2758 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2762 mutex_lock(&root->fs_info->fs_mutex);
2763 ret = btrfs_check_free_space(root, 1, 0);
2767 trans = btrfs_start_transaction(root, 1);
2769 btrfs_set_trans_block_group(trans, new_dir);
2770 path = btrfs_alloc_path();
2776 old_dentry->d_inode->i_nlink++;
2777 old_dir->i_ctime = old_dir->i_mtime = ctime;
2778 new_dir->i_ctime = new_dir->i_mtime = ctime;
2779 old_inode->i_ctime = ctime;
2781 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2786 new_inode->i_ctime = CURRENT_TIME;
2787 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2791 ret = btrfs_add_link(trans, new_dentry, old_inode);
2796 btrfs_free_path(path);
2797 btrfs_end_transaction(trans, root);
2799 mutex_unlock(&root->fs_info->fs_mutex);
2803 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2804 const char *symname)
2806 struct btrfs_trans_handle *trans;
2807 struct btrfs_root *root = BTRFS_I(dir)->root;
2808 struct btrfs_path *path;
2809 struct btrfs_key key;
2810 struct inode *inode = NULL;
2817 struct btrfs_file_extent_item *ei;
2818 struct extent_buffer *leaf;
2819 unsigned long nr = 0;
2821 name_len = strlen(symname) + 1;
2822 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2823 return -ENAMETOOLONG;
2825 mutex_lock(&root->fs_info->fs_mutex);
2826 err = btrfs_check_free_space(root, 1, 0);
2830 trans = btrfs_start_transaction(root, 1);
2831 btrfs_set_trans_block_group(trans, dir);
2833 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2839 inode = btrfs_new_inode(trans, root, objectid,
2840 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2841 err = PTR_ERR(inode);
2845 btrfs_set_trans_block_group(trans, inode);
2846 err = btrfs_add_nondir(trans, dentry, inode);
2850 inode->i_mapping->a_ops = &btrfs_aops;
2851 inode->i_fop = &btrfs_file_operations;
2852 inode->i_op = &btrfs_file_inode_operations;
2853 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2854 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2855 inode->i_mapping, GFP_NOFS);
2856 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2858 dir->i_sb->s_dirt = 1;
2859 btrfs_update_inode_block_group(trans, inode);
2860 btrfs_update_inode_block_group(trans, dir);
2864 path = btrfs_alloc_path();
2866 key.objectid = inode->i_ino;
2868 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2869 datasize = btrfs_file_extent_calc_inline_size(name_len);
2870 err = btrfs_insert_empty_item(trans, root, path, &key,
2876 leaf = path->nodes[0];
2877 ei = btrfs_item_ptr(leaf, path->slots[0],
2878 struct btrfs_file_extent_item);
2879 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2880 btrfs_set_file_extent_type(leaf, ei,
2881 BTRFS_FILE_EXTENT_INLINE);
2882 ptr = btrfs_file_extent_inline_start(ei);
2883 write_extent_buffer(leaf, symname, ptr, name_len);
2884 btrfs_mark_buffer_dirty(leaf);
2885 btrfs_free_path(path);
2887 inode->i_op = &btrfs_symlink_inode_operations;
2888 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2889 inode->i_size = name_len - 1;
2890 err = btrfs_update_inode(trans, root, inode);
2895 nr = trans->blocks_used;
2896 btrfs_end_transaction(trans, root);
2898 mutex_unlock(&root->fs_info->fs_mutex);
2900 inode_dec_link_count(inode);
2903 btrfs_btree_balance_dirty(root, nr);
2904 btrfs_throttle(root);
2907 static int btrfs_permission(struct inode *inode, int mask,
2908 struct nameidata *nd)
2910 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2912 return generic_permission(inode, mask, NULL);
2915 static struct inode_operations btrfs_dir_inode_operations = {
2916 .lookup = btrfs_lookup,
2917 .create = btrfs_create,
2918 .unlink = btrfs_unlink,
2920 .mkdir = btrfs_mkdir,
2921 .rmdir = btrfs_rmdir,
2922 .rename = btrfs_rename,
2923 .symlink = btrfs_symlink,
2924 .setattr = btrfs_setattr,
2925 .mknod = btrfs_mknod,
2926 .setxattr = generic_setxattr,
2927 .getxattr = generic_getxattr,
2928 .listxattr = btrfs_listxattr,
2929 .removexattr = generic_removexattr,
2930 .permission = btrfs_permission,
2932 static struct inode_operations btrfs_dir_ro_inode_operations = {
2933 .lookup = btrfs_lookup,
2934 .permission = btrfs_permission,
2936 static struct file_operations btrfs_dir_file_operations = {
2937 .llseek = generic_file_llseek,
2938 .read = generic_read_dir,
2939 .readdir = btrfs_readdir,
2940 .unlocked_ioctl = btrfs_ioctl,
2941 #ifdef CONFIG_COMPAT
2942 .compat_ioctl = btrfs_ioctl,
2946 static struct extent_io_ops btrfs_extent_io_ops = {
2947 .fill_delalloc = run_delalloc_range,
2948 .writepage_io_hook = btrfs_writepage_io_hook,
2949 .readpage_io_hook = btrfs_readpage_io_hook,
2950 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2953 static struct address_space_operations btrfs_aops = {
2954 .readpage = btrfs_readpage,
2955 .writepage = btrfs_writepage,
2956 .writepages = btrfs_writepages,
2957 .readpages = btrfs_readpages,
2958 .sync_page = block_sync_page,
2960 .invalidatepage = btrfs_invalidatepage,
2961 .releasepage = btrfs_releasepage,
2962 .set_page_dirty = __set_page_dirty_nobuffers,
2965 static struct address_space_operations btrfs_symlink_aops = {
2966 .readpage = btrfs_readpage,
2967 .writepage = btrfs_writepage,
2968 .invalidatepage = btrfs_invalidatepage,
2969 .releasepage = btrfs_releasepage,
2972 static struct inode_operations btrfs_file_inode_operations = {
2973 .truncate = btrfs_truncate,
2974 .getattr = btrfs_getattr,
2975 .setattr = btrfs_setattr,
2976 .setxattr = generic_setxattr,
2977 .getxattr = generic_getxattr,
2978 .listxattr = btrfs_listxattr,
2979 .removexattr = generic_removexattr,
2980 .permission = btrfs_permission,
2982 static struct inode_operations btrfs_special_inode_operations = {
2983 .getattr = btrfs_getattr,
2984 .setattr = btrfs_setattr,
2985 .permission = btrfs_permission,
2987 static struct inode_operations btrfs_symlink_inode_operations = {
2988 .readlink = generic_readlink,
2989 .follow_link = page_follow_link_light,
2990 .put_link = page_put_link,
2991 .permission = btrfs_permission,