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 static int cow_file_range(struct inode *inode, u64 start, u64 end)
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_trans_handle *trans;
82 u64 blocksize = root->sectorsize;
86 trans = btrfs_start_transaction(root, 1);
88 btrfs_set_trans_block_group(trans, inode);
90 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
91 num_bytes = max(blocksize, num_bytes);
92 ret = btrfs_drop_extents(trans, root, inode,
93 start, start + num_bytes, start, &alloc_hint);
95 if (alloc_hint == EXTENT_MAP_INLINE)
98 while(num_bytes > 0) {
99 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
100 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
101 root->root_key.objectid,
103 inode->i_ino, start, 0,
104 alloc_hint, (u64)-1, &ins, 1);
109 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
110 start, ins.objectid, ins.offset,
112 num_bytes -= cur_alloc_size;
113 alloc_hint = ins.objectid + ins.offset;
114 start += cur_alloc_size;
117 btrfs_end_transaction(trans, root);
121 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
127 struct btrfs_root *root = BTRFS_I(inode)->root;
128 struct extent_buffer *leaf;
130 struct btrfs_path *path;
131 struct btrfs_file_extent_item *item;
134 struct btrfs_key found_key;
136 path = btrfs_alloc_path();
139 ret = btrfs_lookup_file_extent(NULL, root, path,
140 inode->i_ino, start, 0);
142 btrfs_free_path(path);
148 if (path->slots[0] == 0)
153 leaf = path->nodes[0];
154 item = btrfs_item_ptr(leaf, path->slots[0],
155 struct btrfs_file_extent_item);
157 /* are we inside the extent that was found? */
158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
159 found_type = btrfs_key_type(&found_key);
160 if (found_key.objectid != inode->i_ino ||
161 found_type != BTRFS_EXTENT_DATA_KEY) {
165 found_type = btrfs_file_extent_type(leaf, item);
166 extent_start = found_key.offset;
167 if (found_type == BTRFS_FILE_EXTENT_REG) {
168 extent_end = extent_start +
169 btrfs_file_extent_num_bytes(leaf, item);
172 if (start < extent_start || start >= extent_end)
175 cow_end = min(end, extent_end - 1);
176 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
180 bytenr += btrfs_file_extent_offset(leaf, item);
181 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
186 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
191 btrfs_free_path(path);
194 btrfs_release_path(root, path);
198 cow_file_range(inode, start, cow_end);
203 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
205 struct btrfs_root *root = BTRFS_I(inode)->root;
208 mutex_lock(&root->fs_info->fs_mutex);
209 if (btrfs_test_opt(root, NODATACOW))
210 ret = run_delalloc_nocow(inode, start, end);
212 ret = cow_file_range(inode, start, end);
213 mutex_unlock(&root->fs_info->fs_mutex);
217 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
219 struct inode *inode = page->mapping->host;
220 struct btrfs_root *root = BTRFS_I(inode)->root;
221 struct btrfs_trans_handle *trans;
224 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
225 size_t offset = start - page_start;
227 if (btrfs_test_opt(root, NODATASUM))
230 mutex_lock(&root->fs_info->fs_mutex);
231 trans = btrfs_start_transaction(root, 1);
232 btrfs_set_trans_block_group(trans, inode);
234 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
235 start, kaddr + offset, end - start + 1);
237 ret = btrfs_end_transaction(trans, root);
239 mutex_unlock(&root->fs_info->fs_mutex);
243 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
246 struct inode *inode = page->mapping->host;
247 struct btrfs_root *root = BTRFS_I(inode)->root;
248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
249 struct btrfs_csum_item *item;
250 struct btrfs_path *path = NULL;
253 if (btrfs_test_opt(root, NODATASUM))
256 mutex_lock(&root->fs_info->fs_mutex);
257 path = btrfs_alloc_path();
258 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
261 /* a csum that isn't present is a preallocated region. */
262 if (ret == -ENOENT || ret == -EFBIG)
267 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
269 set_state_private(em_tree, start, csum);
272 btrfs_free_path(path);
273 mutex_unlock(&root->fs_info->fs_mutex);
277 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
279 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
280 struct inode *inode = page->mapping->host;
281 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
285 struct btrfs_root *root = BTRFS_I(inode)->root;
289 if (btrfs_test_opt(root, NODATASUM))
292 ret = get_state_private(em_tree, start, &private);
293 local_irq_save(flags);
294 kaddr = kmap_atomic(page, KM_IRQ0);
298 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
299 btrfs_csum_final(csum, (char *)&csum);
300 if (csum != private) {
303 kunmap_atomic(kaddr, KM_IRQ0);
304 local_irq_restore(flags);
308 printk("btrfs csum failed ino %lu off %llu\n",
309 page->mapping->host->i_ino, (unsigned long long)start);
310 memset(kaddr + offset, 1, end - start + 1);
311 flush_dcache_page(page);
312 kunmap_atomic(kaddr, KM_IRQ0);
313 local_irq_restore(flags);
317 void btrfs_read_locked_inode(struct inode *inode)
319 struct btrfs_path *path;
320 struct extent_buffer *leaf;
321 struct btrfs_inode_item *inode_item;
322 struct btrfs_inode_timespec *tspec;
323 struct btrfs_root *root = BTRFS_I(inode)->root;
324 struct btrfs_key location;
325 u64 alloc_group_block;
329 path = btrfs_alloc_path();
331 mutex_lock(&root->fs_info->fs_mutex);
333 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
334 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
338 leaf = path->nodes[0];
339 inode_item = btrfs_item_ptr(leaf, path->slots[0],
340 struct btrfs_inode_item);
342 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
343 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
344 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
345 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
346 inode->i_size = btrfs_inode_size(leaf, inode_item);
348 tspec = btrfs_inode_atime(inode_item);
349 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
350 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
352 tspec = btrfs_inode_mtime(inode_item);
353 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
354 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
356 tspec = btrfs_inode_ctime(inode_item);
357 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
358 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
360 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
361 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
363 rdev = btrfs_inode_rdev(leaf, inode_item);
365 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
366 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
369 btrfs_free_path(path);
372 mutex_unlock(&root->fs_info->fs_mutex);
374 switch (inode->i_mode & S_IFMT) {
376 inode->i_mapping->a_ops = &btrfs_aops;
377 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
378 inode->i_fop = &btrfs_file_operations;
379 inode->i_op = &btrfs_file_inode_operations;
382 inode->i_fop = &btrfs_dir_file_operations;
383 if (root == root->fs_info->tree_root)
384 inode->i_op = &btrfs_dir_ro_inode_operations;
386 inode->i_op = &btrfs_dir_inode_operations;
389 inode->i_op = &btrfs_symlink_inode_operations;
390 inode->i_mapping->a_ops = &btrfs_symlink_aops;
393 init_special_inode(inode, inode->i_mode, rdev);
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 mutex_unlock(&root->fs_info->fs_mutex);
402 make_bad_inode(inode);
405 static void fill_inode_item(struct extent_buffer *leaf,
406 struct btrfs_inode_item *item,
409 btrfs_set_inode_uid(leaf, item, inode->i_uid);
410 btrfs_set_inode_gid(leaf, item, inode->i_gid);
411 btrfs_set_inode_size(leaf, item, inode->i_size);
412 btrfs_set_inode_mode(leaf, item, inode->i_mode);
413 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
415 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
416 inode->i_atime.tv_sec);
417 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
418 inode->i_atime.tv_nsec);
420 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
421 inode->i_mtime.tv_sec);
422 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
423 inode->i_mtime.tv_nsec);
425 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
426 inode->i_ctime.tv_sec);
427 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
428 inode->i_ctime.tv_nsec);
430 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
431 btrfs_set_inode_generation(leaf, item, inode->i_generation);
432 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
433 btrfs_set_inode_block_group(leaf, item,
434 BTRFS_I(inode)->block_group->key.objectid);
437 int btrfs_update_inode(struct btrfs_trans_handle *trans,
438 struct btrfs_root *root,
441 struct btrfs_inode_item *inode_item;
442 struct btrfs_path *path;
443 struct extent_buffer *leaf;
446 path = btrfs_alloc_path();
448 ret = btrfs_lookup_inode(trans, root, path,
449 &BTRFS_I(inode)->location, 1);
456 leaf = path->nodes[0];
457 inode_item = btrfs_item_ptr(leaf, path->slots[0],
458 struct btrfs_inode_item);
460 fill_inode_item(leaf, inode_item, inode);
461 btrfs_mark_buffer_dirty(leaf);
462 btrfs_set_inode_last_trans(trans, inode);
465 btrfs_release_path(root, path);
466 btrfs_free_path(path);
471 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
472 struct btrfs_root *root,
474 struct dentry *dentry)
476 struct btrfs_path *path;
477 const char *name = dentry->d_name.name;
478 int name_len = dentry->d_name.len;
480 struct extent_buffer *leaf;
481 struct btrfs_dir_item *di;
482 struct btrfs_key key;
484 path = btrfs_alloc_path();
490 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
500 leaf = path->nodes[0];
501 btrfs_dir_item_key_to_cpu(leaf, di, &key);
502 ret = btrfs_delete_one_dir_name(trans, root, path, di);
505 btrfs_release_path(root, path);
507 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
508 key.objectid, name, name_len, -1);
517 ret = btrfs_delete_one_dir_name(trans, root, path, di);
519 dentry->d_inode->i_ctime = dir->i_ctime;
520 ret = btrfs_del_inode_ref(trans, root, name, name_len,
521 dentry->d_inode->i_ino,
522 dentry->d_parent->d_inode->i_ino);
524 printk("failed to delete reference to %.*s, "
525 "inode %lu parent %lu\n", name_len, name,
526 dentry->d_inode->i_ino,
527 dentry->d_parent->d_inode->i_ino);
530 btrfs_free_path(path);
532 dir->i_size -= name_len * 2;
533 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
534 btrfs_update_inode(trans, root, dir);
535 drop_nlink(dentry->d_inode);
536 ret = btrfs_update_inode(trans, root, dentry->d_inode);
537 dir->i_sb->s_dirt = 1;
542 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
544 struct btrfs_root *root;
545 struct btrfs_trans_handle *trans;
549 root = BTRFS_I(dir)->root;
550 mutex_lock(&root->fs_info->fs_mutex);
551 trans = btrfs_start_transaction(root, 1);
553 btrfs_set_trans_block_group(trans, dir);
554 ret = btrfs_unlink_trans(trans, root, dir, dentry);
555 nr = trans->blocks_used;
557 btrfs_end_transaction(trans, root);
558 mutex_unlock(&root->fs_info->fs_mutex);
559 btrfs_btree_balance_dirty(root, nr);
564 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
566 struct inode *inode = dentry->d_inode;
569 struct btrfs_root *root = BTRFS_I(dir)->root;
570 struct btrfs_trans_handle *trans;
573 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
576 mutex_lock(&root->fs_info->fs_mutex);
577 trans = btrfs_start_transaction(root, 1);
578 btrfs_set_trans_block_group(trans, dir);
580 /* now the directory is empty */
581 err = btrfs_unlink_trans(trans, root, dir, dentry);
586 nr = trans->blocks_used;
587 ret = btrfs_end_transaction(trans, root);
588 mutex_unlock(&root->fs_info->fs_mutex);
589 btrfs_btree_balance_dirty(root, nr);
596 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
600 struct btrfs_path *path;
605 path = btrfs_alloc_path();
607 ret = btrfs_lookup_inode(trans, root, path,
608 &BTRFS_I(inode)->location, -1);
612 ret = btrfs_del_item(trans, root, path);
613 btrfs_free_path(path);
618 * this can truncate away extent items, csum items and directory items.
619 * It starts at a high offset and removes keys until it can't find
620 * any higher than i_size.
622 * csum items that cross the new i_size are truncated to the new size
625 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
630 struct btrfs_path *path;
631 struct btrfs_key key;
632 struct btrfs_key found_key;
634 struct extent_buffer *leaf;
635 struct btrfs_file_extent_item *fi;
636 u64 extent_start = 0;
637 u64 extent_num_bytes = 0;
643 int extent_type = -1;
645 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
646 path = btrfs_alloc_path();
650 /* FIXME, add redo link to tree so we don't leak on crash */
651 key.objectid = inode->i_ino;
652 key.offset = (u64)-1;
656 btrfs_init_path(path);
658 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
663 BUG_ON(path->slots[0] == 0);
666 leaf = path->nodes[0];
667 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
668 found_type = btrfs_key_type(&found_key);
670 if (found_key.objectid != inode->i_ino)
673 if (found_type != BTRFS_CSUM_ITEM_KEY &&
674 found_type != BTRFS_DIR_ITEM_KEY &&
675 found_type != BTRFS_DIR_INDEX_KEY &&
676 found_type != BTRFS_EXTENT_DATA_KEY)
679 item_end = found_key.offset;
680 if (found_type == BTRFS_EXTENT_DATA_KEY) {
681 fi = btrfs_item_ptr(leaf, path->slots[0],
682 struct btrfs_file_extent_item);
683 extent_type = btrfs_file_extent_type(leaf, fi);
684 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
686 btrfs_file_extent_num_bytes(leaf, fi);
687 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
688 struct btrfs_item *item = btrfs_item_nr(leaf,
690 item_end += btrfs_file_extent_inline_len(leaf,
695 if (found_type == BTRFS_CSUM_ITEM_KEY) {
696 ret = btrfs_csum_truncate(trans, root, path,
700 if (item_end < inode->i_size) {
701 if (found_type == BTRFS_DIR_ITEM_KEY) {
702 found_type = BTRFS_INODE_ITEM_KEY;
703 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
704 found_type = BTRFS_CSUM_ITEM_KEY;
705 } else if (found_type) {
710 btrfs_set_key_type(&key, found_type);
711 btrfs_release_path(root, path);
714 if (found_key.offset >= inode->i_size)
720 /* FIXME, shrink the extent if the ref count is only 1 */
721 if (found_type != BTRFS_EXTENT_DATA_KEY)
724 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
726 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
729 btrfs_file_extent_num_bytes(leaf, fi);
730 extent_num_bytes = inode->i_size -
731 found_key.offset + root->sectorsize - 1;
732 btrfs_set_file_extent_num_bytes(leaf, fi,
734 num_dec = (orig_num_bytes -
735 extent_num_bytes) >> 9;
736 if (extent_start != 0) {
737 inode->i_blocks -= num_dec;
739 btrfs_mark_buffer_dirty(leaf);
742 btrfs_file_extent_disk_num_bytes(leaf,
744 /* FIXME blocksize != 4096 */
745 num_dec = btrfs_file_extent_num_bytes(leaf,
747 if (extent_start != 0) {
749 inode->i_blocks -= num_dec;
751 root_gen = btrfs_header_generation(leaf);
752 root_owner = btrfs_header_owner(leaf);
754 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
756 u32 newsize = inode->i_size - found_key.offset;
757 newsize = btrfs_file_extent_calc_inline_size(newsize);
758 ret = btrfs_truncate_item(trans, root, path,
764 ret = btrfs_del_item(trans, root, path);
770 btrfs_release_path(root, path);
772 ret = btrfs_free_extent(trans, root, extent_start,
775 root_gen, inode->i_ino,
776 found_key.offset, 0);
782 btrfs_release_path(root, path);
783 btrfs_free_path(path);
784 inode->i_sb->s_dirt = 1;
788 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
793 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
794 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
795 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
797 set_page_extent_mapped(page);
799 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
800 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
802 if (zero_start != PAGE_CACHE_SIZE) {
804 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
805 flush_dcache_page(page);
808 set_page_dirty(page);
809 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
815 * taken from block_truncate_page, but does cow as it zeros out
816 * any bytes left in the last page in the file.
818 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
820 struct inode *inode = mapping->host;
821 struct btrfs_root *root = BTRFS_I(inode)->root;
822 u32 blocksize = root->sectorsize;
823 pgoff_t index = from >> PAGE_CACHE_SHIFT;
824 unsigned offset = from & (PAGE_CACHE_SIZE-1);
829 if ((offset & (blocksize - 1)) == 0)
832 down_read(&root->snap_sem);
834 page = grab_cache_page(mapping, index);
837 if (!PageUptodate(page)) {
838 ret = btrfs_readpage(NULL, page);
840 if (!PageUptodate(page)) {
845 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
847 ret = btrfs_cow_one_page(inode, page, offset);
850 page_cache_release(page);
851 up_read(&BTRFS_I(inode)->root->snap_sem);
856 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
858 struct inode *inode = dentry->d_inode;
861 err = inode_change_ok(inode, attr);
865 if (S_ISREG(inode->i_mode) &&
866 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
867 struct btrfs_trans_handle *trans;
868 struct btrfs_root *root = BTRFS_I(inode)->root;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
871 u64 mask = root->sectorsize - 1;
872 u64 pos = (inode->i_size + mask) & ~mask;
873 u64 block_end = attr->ia_size | mask;
877 if (attr->ia_size <= pos)
880 btrfs_truncate_page(inode->i_mapping, inode->i_size);
882 lock_extent(em_tree, pos, block_end, GFP_NOFS);
883 hole_size = (attr->ia_size - pos + mask) & ~mask;
885 mutex_lock(&root->fs_info->fs_mutex);
886 trans = btrfs_start_transaction(root, 1);
887 btrfs_set_trans_block_group(trans, inode);
888 err = btrfs_drop_extents(trans, root, inode,
889 pos, pos + hole_size, pos,
892 if (alloc_hint != EXTENT_MAP_INLINE) {
893 err = btrfs_insert_file_extent(trans, root,
895 pos, 0, 0, hole_size);
897 btrfs_end_transaction(trans, root);
898 mutex_unlock(&root->fs_info->fs_mutex);
899 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
904 err = inode_setattr(inode, attr);
908 void btrfs_delete_inode(struct inode *inode)
910 struct btrfs_trans_handle *trans;
911 struct btrfs_root *root = BTRFS_I(inode)->root;
915 truncate_inode_pages(&inode->i_data, 0);
916 if (is_bad_inode(inode)) {
921 mutex_lock(&root->fs_info->fs_mutex);
922 trans = btrfs_start_transaction(root, 1);
924 btrfs_set_trans_block_group(trans, inode);
925 ret = btrfs_truncate_in_trans(trans, root, inode);
928 ret = btrfs_delete_xattrs(trans, root, inode);
931 ret = btrfs_free_inode(trans, root, inode);
934 nr = trans->blocks_used;
936 btrfs_end_transaction(trans, root);
937 mutex_unlock(&root->fs_info->fs_mutex);
938 btrfs_btree_balance_dirty(root, nr);
942 nr = trans->blocks_used;
943 btrfs_end_transaction(trans, root);
944 mutex_unlock(&root->fs_info->fs_mutex);
945 btrfs_btree_balance_dirty(root, nr);
951 * this returns the key found in the dir entry in the location pointer.
952 * If no dir entries were found, location->objectid is 0.
954 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
955 struct btrfs_key *location)
957 const char *name = dentry->d_name.name;
958 int namelen = dentry->d_name.len;
959 struct btrfs_dir_item *di;
960 struct btrfs_path *path;
961 struct btrfs_root *root = BTRFS_I(dir)->root;
964 if (namelen == 1 && strcmp(name, ".") == 0) {
965 location->objectid = dir->i_ino;
966 location->type = BTRFS_INODE_ITEM_KEY;
967 location->offset = 0;
970 path = btrfs_alloc_path();
973 if (namelen == 2 && strcmp(name, "..") == 0) {
974 struct btrfs_key key;
975 struct extent_buffer *leaf;
979 key.objectid = dir->i_ino;
980 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
982 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
986 leaf = path->nodes[0];
987 slot = path->slots[0];
988 nritems = btrfs_header_nritems(leaf);
992 btrfs_item_key_to_cpu(leaf, &key, slot);
993 if (key.objectid != dir->i_ino ||
994 key.type != BTRFS_INODE_REF_KEY) {
997 location->objectid = key.offset;
998 location->type = BTRFS_INODE_ITEM_KEY;
999 location->offset = 0;
1003 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1007 if (!di || IS_ERR(di)) {
1010 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1012 btrfs_free_path(path);
1015 location->objectid = 0;
1020 * when we hit a tree root in a directory, the btrfs part of the inode
1021 * needs to be changed to reflect the root directory of the tree root. This
1022 * is kind of like crossing a mount point.
1024 static int fixup_tree_root_location(struct btrfs_root *root,
1025 struct btrfs_key *location,
1026 struct btrfs_root **sub_root,
1027 struct dentry *dentry)
1029 struct btrfs_path *path;
1030 struct btrfs_root_item *ri;
1032 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1034 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1037 path = btrfs_alloc_path();
1039 mutex_lock(&root->fs_info->fs_mutex);
1041 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1042 dentry->d_name.name,
1043 dentry->d_name.len);
1044 if (IS_ERR(*sub_root))
1045 return PTR_ERR(*sub_root);
1047 ri = &(*sub_root)->root_item;
1048 location->objectid = btrfs_root_dirid(ri);
1049 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1050 location->offset = 0;
1052 btrfs_free_path(path);
1053 mutex_unlock(&root->fs_info->fs_mutex);
1057 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1059 struct btrfs_iget_args *args = p;
1060 inode->i_ino = args->ino;
1061 BTRFS_I(inode)->root = args->root;
1062 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1063 inode->i_mapping, GFP_NOFS);
1067 static int btrfs_find_actor(struct inode *inode, void *opaque)
1069 struct btrfs_iget_args *args = opaque;
1070 return (args->ino == inode->i_ino &&
1071 args->root == BTRFS_I(inode)->root);
1074 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1075 struct btrfs_root *root)
1077 struct inode *inode;
1078 struct btrfs_iget_args args;
1079 args.ino = objectid;
1082 inode = iget5_locked(s, objectid, btrfs_find_actor,
1083 btrfs_init_locked_inode,
1088 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1089 struct nameidata *nd)
1091 struct inode * inode;
1092 struct btrfs_inode *bi = BTRFS_I(dir);
1093 struct btrfs_root *root = bi->root;
1094 struct btrfs_root *sub_root = root;
1095 struct btrfs_key location;
1098 if (dentry->d_name.len > BTRFS_NAME_LEN)
1099 return ERR_PTR(-ENAMETOOLONG);
1101 mutex_lock(&root->fs_info->fs_mutex);
1102 ret = btrfs_inode_by_name(dir, dentry, &location);
1103 mutex_unlock(&root->fs_info->fs_mutex);
1106 return ERR_PTR(ret);
1109 if (location.objectid) {
1110 ret = fixup_tree_root_location(root, &location, &sub_root,
1113 return ERR_PTR(ret);
1115 return ERR_PTR(-ENOENT);
1116 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1119 return ERR_PTR(-EACCES);
1120 if (inode->i_state & I_NEW) {
1121 /* the inode and parent dir are two different roots */
1122 if (sub_root != root) {
1124 sub_root->inode = inode;
1126 BTRFS_I(inode)->root = sub_root;
1127 memcpy(&BTRFS_I(inode)->location, &location,
1129 btrfs_read_locked_inode(inode);
1130 unlock_new_inode(inode);
1133 return d_splice_alias(inode, dentry);
1136 static unsigned char btrfs_filetype_table[] = {
1137 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1140 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1142 struct inode *inode = filp->f_path.dentry->d_inode;
1143 struct btrfs_root *root = BTRFS_I(inode)->root;
1144 struct btrfs_item *item;
1145 struct btrfs_dir_item *di;
1146 struct btrfs_key key;
1147 struct btrfs_key found_key;
1148 struct btrfs_path *path;
1151 struct extent_buffer *leaf;
1154 unsigned char d_type;
1159 int key_type = BTRFS_DIR_INDEX_KEY;
1164 /* FIXME, use a real flag for deciding about the key type */
1165 if (root->fs_info->tree_root == root)
1166 key_type = BTRFS_DIR_ITEM_KEY;
1168 /* special case for "." */
1169 if (filp->f_pos == 0) {
1170 over = filldir(dirent, ".", 1,
1178 mutex_lock(&root->fs_info->fs_mutex);
1179 key.objectid = inode->i_ino;
1180 path = btrfs_alloc_path();
1183 /* special case for .., just use the back ref */
1184 if (filp->f_pos == 1) {
1185 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1189 leaf = path->nodes[0];
1190 slot = path->slots[0];
1191 nritems = btrfs_header_nritems(leaf);
1192 if (slot >= nritems) {
1193 btrfs_release_path(root, path);
1194 goto read_dir_items;
1196 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1197 btrfs_release_path(root, path);
1198 if (found_key.objectid != key.objectid ||
1199 found_key.type != BTRFS_INODE_REF_KEY)
1200 goto read_dir_items;
1201 over = filldir(dirent, "..", 2,
1202 2, found_key.offset, DT_DIR);
1209 btrfs_set_key_type(&key, key_type);
1210 key.offset = filp->f_pos;
1212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1217 leaf = path->nodes[0];
1218 nritems = btrfs_header_nritems(leaf);
1219 slot = path->slots[0];
1220 if (advance || slot >= nritems) {
1221 if (slot >= nritems -1) {
1222 ret = btrfs_next_leaf(root, path);
1225 leaf = path->nodes[0];
1226 nritems = btrfs_header_nritems(leaf);
1227 slot = path->slots[0];
1234 item = btrfs_item_nr(leaf, slot);
1235 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1237 if (found_key.objectid != key.objectid)
1239 if (btrfs_key_type(&found_key) != key_type)
1241 if (found_key.offset < filp->f_pos)
1244 filp->f_pos = found_key.offset;
1246 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1248 di_total = btrfs_item_size(leaf, item);
1249 while(di_cur < di_total) {
1250 struct btrfs_key location;
1252 name_len = btrfs_dir_name_len(leaf, di);
1253 if (name_len < 32) {
1254 name_ptr = tmp_name;
1256 name_ptr = kmalloc(name_len, GFP_NOFS);
1259 read_extent_buffer(leaf, name_ptr,
1260 (unsigned long)(di + 1), name_len);
1262 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1263 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1265 over = filldir(dirent, name_ptr, name_len,
1270 if (name_ptr != tmp_name)
1275 di_len = btrfs_dir_name_len(leaf, di) +
1276 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1278 di = (struct btrfs_dir_item *)((char *)di + di_len);
1285 btrfs_release_path(root, path);
1286 btrfs_free_path(path);
1287 mutex_unlock(&root->fs_info->fs_mutex);
1291 int btrfs_write_inode(struct inode *inode, int wait)
1293 struct btrfs_root *root = BTRFS_I(inode)->root;
1294 struct btrfs_trans_handle *trans;
1298 mutex_lock(&root->fs_info->fs_mutex);
1299 trans = btrfs_start_transaction(root, 1);
1300 btrfs_set_trans_block_group(trans, inode);
1301 ret = btrfs_commit_transaction(trans, root);
1302 mutex_unlock(&root->fs_info->fs_mutex);
1308 * This is somewhat expensive, updating the tree every time the
1309 * inode changes. But, it is most likely to find the inode in cache.
1310 * FIXME, needs more benchmarking...there are no reasons other than performance
1311 * to keep or drop this code.
1313 void btrfs_dirty_inode(struct inode *inode)
1315 struct btrfs_root *root = BTRFS_I(inode)->root;
1316 struct btrfs_trans_handle *trans;
1318 mutex_lock(&root->fs_info->fs_mutex);
1319 trans = btrfs_start_transaction(root, 1);
1320 btrfs_set_trans_block_group(trans, inode);
1321 btrfs_update_inode(trans, root, inode);
1322 btrfs_end_transaction(trans, root);
1323 mutex_unlock(&root->fs_info->fs_mutex);
1326 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1329 struct btrfs_block_group_cache *group,
1332 struct inode *inode;
1333 struct btrfs_inode_item *inode_item;
1334 struct btrfs_key *location;
1335 struct btrfs_path *path;
1339 path = btrfs_alloc_path();
1342 inode = new_inode(root->fs_info->sb);
1344 return ERR_PTR(-ENOMEM);
1346 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1347 inode->i_mapping, GFP_NOFS);
1348 BTRFS_I(inode)->root = root;
1354 group = btrfs_find_block_group(root, group, 0, 0, owner);
1355 BTRFS_I(inode)->block_group = group;
1357 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1361 inode->i_uid = current->fsuid;
1362 inode->i_gid = current->fsgid;
1363 inode->i_mode = mode;
1364 inode->i_ino = objectid;
1365 inode->i_blocks = 0;
1366 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1367 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1368 struct btrfs_inode_item);
1369 fill_inode_item(path->nodes[0], inode_item, inode);
1370 btrfs_mark_buffer_dirty(path->nodes[0]);
1371 btrfs_free_path(path);
1373 location = &BTRFS_I(inode)->location;
1374 location->objectid = objectid;
1375 location->offset = 0;
1376 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1378 insert_inode_hash(inode);
1381 btrfs_free_path(path);
1382 return ERR_PTR(ret);
1385 static inline u8 btrfs_inode_type(struct inode *inode)
1387 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1390 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1391 struct dentry *dentry, struct inode *inode)
1394 struct btrfs_key key;
1395 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1396 struct inode *parent_inode;
1398 key.objectid = inode->i_ino;
1399 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1402 ret = btrfs_insert_dir_item(trans, root,
1403 dentry->d_name.name, dentry->d_name.len,
1404 dentry->d_parent->d_inode->i_ino,
1405 &key, btrfs_inode_type(inode));
1407 ret = btrfs_insert_inode_ref(trans, root,
1408 dentry->d_name.name,
1411 dentry->d_parent->d_inode->i_ino);
1412 parent_inode = dentry->d_parent->d_inode;
1413 parent_inode->i_size += dentry->d_name.len * 2;
1414 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1415 ret = btrfs_update_inode(trans, root,
1416 dentry->d_parent->d_inode);
1421 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1422 struct dentry *dentry, struct inode *inode)
1424 int err = btrfs_add_link(trans, dentry, inode);
1426 d_instantiate(dentry, inode);
1434 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1435 int mode, dev_t rdev)
1437 struct btrfs_trans_handle *trans;
1438 struct btrfs_root *root = BTRFS_I(dir)->root;
1439 struct inode *inode;
1445 if (!new_valid_dev(rdev))
1448 mutex_lock(&root->fs_info->fs_mutex);
1449 trans = btrfs_start_transaction(root, 1);
1450 btrfs_set_trans_block_group(trans, dir);
1452 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1458 inode = btrfs_new_inode(trans, root, objectid,
1459 BTRFS_I(dir)->block_group, mode);
1460 err = PTR_ERR(inode);
1464 btrfs_set_trans_block_group(trans, inode);
1465 err = btrfs_add_nondir(trans, dentry, inode);
1469 inode->i_op = &btrfs_special_inode_operations;
1470 init_special_inode(inode, inode->i_mode, rdev);
1471 btrfs_update_inode(trans, root, inode);
1473 dir->i_sb->s_dirt = 1;
1474 btrfs_update_inode_block_group(trans, inode);
1475 btrfs_update_inode_block_group(trans, dir);
1477 nr = trans->blocks_used;
1478 btrfs_end_transaction(trans, root);
1479 mutex_unlock(&root->fs_info->fs_mutex);
1482 inode_dec_link_count(inode);
1485 btrfs_btree_balance_dirty(root, nr);
1489 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1490 int mode, struct nameidata *nd)
1492 struct btrfs_trans_handle *trans;
1493 struct btrfs_root *root = BTRFS_I(dir)->root;
1494 struct inode *inode;
1500 mutex_lock(&root->fs_info->fs_mutex);
1501 trans = btrfs_start_transaction(root, 1);
1502 btrfs_set_trans_block_group(trans, dir);
1504 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1510 inode = btrfs_new_inode(trans, root, objectid,
1511 BTRFS_I(dir)->block_group, mode);
1512 err = PTR_ERR(inode);
1516 btrfs_set_trans_block_group(trans, inode);
1517 err = btrfs_add_nondir(trans, dentry, inode);
1521 inode->i_mapping->a_ops = &btrfs_aops;
1522 inode->i_fop = &btrfs_file_operations;
1523 inode->i_op = &btrfs_file_inode_operations;
1524 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1525 inode->i_mapping, GFP_NOFS);
1526 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1528 dir->i_sb->s_dirt = 1;
1529 btrfs_update_inode_block_group(trans, inode);
1530 btrfs_update_inode_block_group(trans, dir);
1532 nr = trans->blocks_used;
1533 btrfs_end_transaction(trans, root);
1534 mutex_unlock(&root->fs_info->fs_mutex);
1537 inode_dec_link_count(inode);
1540 btrfs_btree_balance_dirty(root, nr);
1544 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1545 struct dentry *dentry)
1547 struct btrfs_trans_handle *trans;
1548 struct btrfs_root *root = BTRFS_I(dir)->root;
1549 struct inode *inode = old_dentry->d_inode;
1554 if (inode->i_nlink == 0)
1558 mutex_lock(&root->fs_info->fs_mutex);
1559 trans = btrfs_start_transaction(root, 1);
1561 btrfs_set_trans_block_group(trans, dir);
1562 atomic_inc(&inode->i_count);
1563 err = btrfs_add_nondir(trans, dentry, inode);
1568 dir->i_sb->s_dirt = 1;
1569 btrfs_update_inode_block_group(trans, dir);
1570 err = btrfs_update_inode(trans, root, inode);
1575 nr = trans->blocks_used;
1576 btrfs_end_transaction(trans, root);
1577 mutex_unlock(&root->fs_info->fs_mutex);
1580 inode_dec_link_count(inode);
1583 btrfs_btree_balance_dirty(root, nr);
1587 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1589 struct inode *inode;
1590 struct btrfs_trans_handle *trans;
1591 struct btrfs_root *root = BTRFS_I(dir)->root;
1593 int drop_on_err = 0;
1595 unsigned long nr = 1;
1597 mutex_lock(&root->fs_info->fs_mutex);
1598 trans = btrfs_start_transaction(root, 1);
1599 btrfs_set_trans_block_group(trans, dir);
1601 if (IS_ERR(trans)) {
1602 err = PTR_ERR(trans);
1606 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1612 inode = btrfs_new_inode(trans, root, objectid,
1613 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1614 if (IS_ERR(inode)) {
1615 err = PTR_ERR(inode);
1620 inode->i_op = &btrfs_dir_inode_operations;
1621 inode->i_fop = &btrfs_dir_file_operations;
1622 btrfs_set_trans_block_group(trans, inode);
1625 err = btrfs_update_inode(trans, root, inode);
1629 err = btrfs_add_link(trans, dentry, inode);
1633 d_instantiate(dentry, inode);
1635 dir->i_sb->s_dirt = 1;
1636 btrfs_update_inode_block_group(trans, inode);
1637 btrfs_update_inode_block_group(trans, dir);
1640 nr = trans->blocks_used;
1641 btrfs_end_transaction(trans, root);
1644 mutex_unlock(&root->fs_info->fs_mutex);
1647 btrfs_btree_balance_dirty(root, nr);
1651 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1652 size_t page_offset, u64 start, u64 end,
1658 u64 extent_start = 0;
1660 u64 objectid = inode->i_ino;
1662 int failed_insert = 0;
1663 struct btrfs_path *path;
1664 struct btrfs_root *root = BTRFS_I(inode)->root;
1665 struct btrfs_file_extent_item *item;
1666 struct extent_buffer *leaf;
1667 struct btrfs_key found_key;
1668 struct extent_map *em = NULL;
1669 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1670 struct btrfs_trans_handle *trans = NULL;
1672 path = btrfs_alloc_path();
1674 mutex_lock(&root->fs_info->fs_mutex);
1677 em = lookup_extent_mapping(em_tree, start, end);
1682 em = alloc_extent_map(GFP_NOFS);
1687 em->start = EXTENT_MAP_HOLE;
1688 em->end = EXTENT_MAP_HOLE;
1690 em->bdev = inode->i_sb->s_bdev;
1691 ret = btrfs_lookup_file_extent(trans, root, path,
1692 objectid, start, trans != NULL);
1699 if (path->slots[0] == 0)
1704 leaf = path->nodes[0];
1705 item = btrfs_item_ptr(leaf, path->slots[0],
1706 struct btrfs_file_extent_item);
1707 /* are we inside the extent that was found? */
1708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1709 found_type = btrfs_key_type(&found_key);
1710 if (found_key.objectid != objectid ||
1711 found_type != BTRFS_EXTENT_DATA_KEY) {
1715 found_type = btrfs_file_extent_type(leaf, item);
1716 extent_start = found_key.offset;
1717 if (found_type == BTRFS_FILE_EXTENT_REG) {
1718 extent_end = extent_start +
1719 btrfs_file_extent_num_bytes(leaf, item);
1721 if (start < extent_start || start >= extent_end) {
1723 if (start < extent_start) {
1724 if (end < extent_start)
1726 em->end = extent_end - 1;
1732 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1734 em->start = extent_start;
1735 em->end = extent_end - 1;
1736 em->block_start = EXTENT_MAP_HOLE;
1737 em->block_end = EXTENT_MAP_HOLE;
1740 bytenr += btrfs_file_extent_offset(leaf, item);
1741 em->block_start = bytenr;
1742 em->block_end = em->block_start +
1743 btrfs_file_extent_num_bytes(leaf, item) - 1;
1744 em->start = extent_start;
1745 em->end = extent_end - 1;
1747 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1751 size_t extent_offset;
1754 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1756 extent_end = (extent_start + size - 1) |
1757 ((u64)root->sectorsize - 1);
1758 if (start < extent_start || start >= extent_end) {
1760 if (start < extent_start) {
1761 if (end < extent_start)
1763 em->end = extent_end;
1769 em->block_start = EXTENT_MAP_INLINE;
1770 em->block_end = EXTENT_MAP_INLINE;
1773 em->start = extent_start;
1774 em->end = extent_start + size - 1;
1778 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1779 extent_start + page_offset;
1780 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1781 size - extent_offset);
1782 em->start = extent_start + extent_offset;
1783 em->end = (em->start + copy_size -1) |
1784 ((u64)root->sectorsize -1);
1786 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1787 if (create == 0 && !PageUptodate(page)) {
1788 read_extent_buffer(leaf, map + page_offset, ptr,
1790 flush_dcache_page(page);
1791 } else if (create && PageUptodate(page)) {
1794 free_extent_map(em);
1796 btrfs_release_path(root, path);
1797 trans = btrfs_start_transaction(root, 1);
1800 write_extent_buffer(leaf, map + page_offset, ptr,
1802 btrfs_mark_buffer_dirty(leaf);
1805 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1808 printk("unkknown found_type %d\n", found_type);
1815 em->block_start = EXTENT_MAP_HOLE;
1816 em->block_end = EXTENT_MAP_HOLE;
1818 btrfs_release_path(root, path);
1819 if (em->start > start || em->end < start) {
1820 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1824 ret = add_extent_mapping(em_tree, em);
1825 if (ret == -EEXIST) {
1826 free_extent_map(em);
1829 if (failed_insert > 5) {
1830 printk("failing to insert %Lu %Lu\n", start, end);
1838 btrfs_free_path(path);
1840 ret = btrfs_end_transaction(trans, root);
1844 mutex_unlock(&root->fs_info->fs_mutex);
1846 free_extent_map(em);
1848 return ERR_PTR(err);
1853 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1855 return extent_bmap(mapping, iblock, btrfs_get_extent);
1858 static int btrfs_prepare_write(struct file *file, struct page *page,
1859 unsigned from, unsigned to)
1861 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1862 page->mapping->host, page, from, to,
1866 int btrfs_readpage(struct file *file, struct page *page)
1868 struct extent_map_tree *tree;
1869 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1870 return extent_read_full_page(tree, page, btrfs_get_extent);
1872 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1874 struct extent_map_tree *tree;
1877 if (current->flags & PF_MEMALLOC) {
1878 redirty_page_for_writepage(wbc, page);
1882 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1883 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1886 static int btrfs_writepages(struct address_space *mapping,
1887 struct writeback_control *wbc)
1889 struct extent_map_tree *tree;
1890 tree = &BTRFS_I(mapping->host)->extent_tree;
1891 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1895 btrfs_readpages(struct file *file, struct address_space *mapping,
1896 struct list_head *pages, unsigned nr_pages)
1898 struct extent_map_tree *tree;
1899 tree = &BTRFS_I(mapping->host)->extent_tree;
1900 return extent_readpages(tree, mapping, pages, nr_pages,
1904 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1906 struct extent_map_tree *tree;
1909 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1910 ret = try_release_extent_mapping(tree, page);
1912 ClearPagePrivate(page);
1913 set_page_private(page, 0);
1914 page_cache_release(page);
1919 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1921 struct extent_map_tree *tree;
1923 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1924 extent_invalidatepage(tree, page, offset);
1925 btrfs_releasepage(page, GFP_NOFS);
1929 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1930 * called from a page fault handler when a page is first dirtied. Hence we must
1931 * be careful to check for EOF conditions here. We set the page up correctly
1932 * for a written page which means we get ENOSPC checking when writing into
1933 * holes and correct delalloc and unwritten extent mapping on filesystems that
1934 * support these features.
1936 * We are not allowed to take the i_mutex here so we have to play games to
1937 * protect against truncate races as the page could now be beyond EOF. Because
1938 * vmtruncate() writes the inode size before removing pages, once we have the
1939 * page lock we can determine safely if the page is beyond EOF. If it is not
1940 * beyond EOF, then the page is guaranteed safe against truncation until we
1943 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1945 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1951 down_read(&BTRFS_I(inode)->root->snap_sem);
1953 wait_on_page_writeback(page);
1954 size = i_size_read(inode);
1955 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1957 if ((page->mapping != inode->i_mapping) ||
1958 (page_start > size)) {
1959 /* page got truncated out from underneath us */
1963 /* page is wholly or partially inside EOF */
1964 if (page_start + PAGE_CACHE_SIZE > size)
1965 end = size & ~PAGE_CACHE_MASK;
1967 end = PAGE_CACHE_SIZE;
1969 ret = btrfs_cow_one_page(inode, page, end);
1972 up_read(&BTRFS_I(inode)->root->snap_sem);
1977 static void btrfs_truncate(struct inode *inode)
1979 struct btrfs_root *root = BTRFS_I(inode)->root;
1981 struct btrfs_trans_handle *trans;
1984 if (!S_ISREG(inode->i_mode))
1986 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1989 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1991 mutex_lock(&root->fs_info->fs_mutex);
1992 trans = btrfs_start_transaction(root, 1);
1993 btrfs_set_trans_block_group(trans, inode);
1995 /* FIXME, add redo link to tree so we don't leak on crash */
1996 ret = btrfs_truncate_in_trans(trans, root, inode);
1997 btrfs_update_inode(trans, root, inode);
1998 nr = trans->blocks_used;
2000 ret = btrfs_end_transaction(trans, root);
2002 mutex_unlock(&root->fs_info->fs_mutex);
2003 btrfs_btree_balance_dirty(root, nr);
2006 int btrfs_commit_write(struct file *file, struct page *page,
2007 unsigned from, unsigned to)
2009 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2010 struct inode *inode = page->mapping->host;
2012 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2014 if (pos > inode->i_size) {
2015 i_size_write(inode, pos);
2016 mark_inode_dirty(inode);
2021 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2023 struct btrfs_trans_handle *trans;
2024 struct btrfs_key key;
2025 struct btrfs_root_item root_item;
2026 struct btrfs_inode_item *inode_item;
2027 struct extent_buffer *leaf;
2028 struct btrfs_root *new_root;
2029 struct inode *inode;
2034 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2035 unsigned long nr = 1;
2037 mutex_lock(&root->fs_info->fs_mutex);
2038 trans = btrfs_start_transaction(root, 1);
2041 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2046 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2047 objectid, trans->transid, 0, 0,
2050 return PTR_ERR(leaf);
2052 btrfs_set_header_nritems(leaf, 0);
2053 btrfs_set_header_level(leaf, 0);
2054 btrfs_set_header_bytenr(leaf, leaf->start);
2055 btrfs_set_header_generation(leaf, trans->transid);
2056 btrfs_set_header_owner(leaf, objectid);
2058 write_extent_buffer(leaf, root->fs_info->fsid,
2059 (unsigned long)btrfs_header_fsid(leaf),
2061 btrfs_mark_buffer_dirty(leaf);
2063 inode_item = &root_item.inode;
2064 memset(inode_item, 0, sizeof(*inode_item));
2065 inode_item->generation = cpu_to_le64(1);
2066 inode_item->size = cpu_to_le64(3);
2067 inode_item->nlink = cpu_to_le32(1);
2068 inode_item->nblocks = cpu_to_le64(1);
2069 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2071 btrfs_set_root_bytenr(&root_item, leaf->start);
2072 btrfs_set_root_level(&root_item, 0);
2073 btrfs_set_root_refs(&root_item, 1);
2074 btrfs_set_root_used(&root_item, 0);
2076 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2077 root_item.drop_level = 0;
2079 free_extent_buffer(leaf);
2082 btrfs_set_root_dirid(&root_item, new_dirid);
2084 key.objectid = objectid;
2086 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2087 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2093 * insert the directory item
2095 key.offset = (u64)-1;
2096 dir = root->fs_info->sb->s_root->d_inode;
2097 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2098 name, namelen, dir->i_ino, &key,
2103 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2104 name, namelen, objectid,
2105 root->fs_info->sb->s_root->d_inode->i_ino);
2109 ret = btrfs_commit_transaction(trans, root);
2113 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2116 trans = btrfs_start_transaction(new_root, 1);
2119 inode = btrfs_new_inode(trans, new_root, new_dirid,
2120 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2123 inode->i_op = &btrfs_dir_inode_operations;
2124 inode->i_fop = &btrfs_dir_file_operations;
2125 new_root->inode = inode;
2127 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2131 ret = btrfs_update_inode(trans, new_root, inode);
2135 nr = trans->blocks_used;
2136 err = btrfs_commit_transaction(trans, root);
2140 mutex_unlock(&root->fs_info->fs_mutex);
2141 btrfs_btree_balance_dirty(root, nr);
2145 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2147 struct btrfs_trans_handle *trans;
2148 struct btrfs_key key;
2149 struct btrfs_root_item new_root_item;
2150 struct extent_buffer *tmp;
2156 if (!root->ref_cows)
2159 down_write(&root->snap_sem);
2160 freeze_bdev(root->fs_info->sb->s_bdev);
2161 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2163 mutex_lock(&root->fs_info->fs_mutex);
2164 trans = btrfs_start_transaction(root, 1);
2167 ret = btrfs_update_inode(trans, root, root->inode);
2171 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2176 memcpy(&new_root_item, &root->root_item,
2177 sizeof(new_root_item));
2179 key.objectid = objectid;
2181 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2183 extent_buffer_get(root->node);
2184 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
2185 free_extent_buffer(tmp);
2187 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2189 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2190 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
2191 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2193 free_extent_buffer(tmp);
2198 * insert the directory item
2200 key.offset = (u64)-1;
2201 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2203 root->fs_info->sb->s_root->d_inode->i_ino,
2204 &key, BTRFS_FT_DIR);
2209 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2210 name, namelen, objectid,
2211 root->fs_info->sb->s_root->d_inode->i_ino);
2216 nr = trans->blocks_used;
2217 err = btrfs_commit_transaction(trans, root);
2222 mutex_unlock(&root->fs_info->fs_mutex);
2223 up_write(&root->snap_sem);
2224 btrfs_btree_balance_dirty(root, nr);
2228 static unsigned long force_ra(struct address_space *mapping,
2229 struct file_ra_state *ra, struct file *file,
2230 pgoff_t offset, pgoff_t last_index)
2234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2235 req_size = last_index - offset + 1;
2236 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2239 req_size = min(last_index - offset + 1, (pgoff_t)128);
2240 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2241 return offset + req_size;
2245 int btrfs_defrag_file(struct file *file) {
2246 struct inode *inode = file->f_path.dentry->d_inode;
2247 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2249 unsigned long last_index;
2250 unsigned long ra_index = 0;
2255 mutex_lock(&inode->i_mutex);
2256 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2257 for (i = 0; i <= last_index; i++) {
2258 if (i == ra_index) {
2259 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2260 file, ra_index, last_index);
2262 page = grab_cache_page(inode->i_mapping, i);
2265 if (!PageUptodate(page)) {
2266 btrfs_readpage(NULL, page);
2268 if (!PageUptodate(page)) {
2270 page_cache_release(page);
2274 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2275 page_end = page_start + PAGE_CACHE_SIZE - 1;
2277 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2278 set_extent_delalloc(em_tree, page_start,
2279 page_end, GFP_NOFS);
2280 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2281 set_page_dirty(page);
2283 page_cache_release(page);
2284 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2288 mutex_unlock(&inode->i_mutex);
2292 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2294 struct btrfs_ioctl_vol_args vol_args;
2295 struct btrfs_dir_item *di;
2296 struct btrfs_path *path;
2300 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2303 namelen = strlen(vol_args.name);
2304 if (namelen > BTRFS_VOL_NAME_MAX)
2306 if (strchr(vol_args.name, '/'))
2309 path = btrfs_alloc_path();
2313 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2314 mutex_lock(&root->fs_info->fs_mutex);
2315 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2317 vol_args.name, namelen, 0);
2318 mutex_unlock(&root->fs_info->fs_mutex);
2319 btrfs_free_path(path);
2320 if (di && !IS_ERR(di))
2325 if (root == root->fs_info->tree_root)
2326 return create_subvol(root, vol_args.name, namelen);
2327 return create_snapshot(root, vol_args.name, namelen);
2330 static int btrfs_ioctl_defrag(struct file *file)
2332 struct inode *inode = file->f_path.dentry->d_inode;
2333 struct btrfs_root *root = BTRFS_I(inode)->root;
2335 switch (inode->i_mode & S_IFMT) {
2337 mutex_lock(&root->fs_info->fs_mutex);
2338 btrfs_defrag_root(root, 0);
2339 btrfs_defrag_root(root->fs_info->extent_root, 0);
2340 mutex_unlock(&root->fs_info->fs_mutex);
2343 btrfs_defrag_file(file);
2350 long btrfs_ioctl(struct file *file, unsigned int
2351 cmd, unsigned long arg)
2353 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
2356 case BTRFS_IOC_SNAP_CREATE:
2357 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2358 case BTRFS_IOC_DEFRAG:
2359 return btrfs_ioctl_defrag(file);
2366 * Called inside transaction, so use GFP_NOFS
2368 struct inode *btrfs_alloc_inode(struct super_block *sb)
2370 struct btrfs_inode *ei;
2372 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2376 return &ei->vfs_inode;
2379 void btrfs_destroy_inode(struct inode *inode)
2381 WARN_ON(!list_empty(&inode->i_dentry));
2382 WARN_ON(inode->i_data.nrpages);
2384 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2387 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2388 static void init_once(struct kmem_cache * cachep, void *foo)
2390 static void init_once(void * foo, struct kmem_cache * cachep,
2391 unsigned long flags)
2394 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2396 inode_init_once(&ei->vfs_inode);
2399 void btrfs_destroy_cachep(void)
2401 if (btrfs_inode_cachep)
2402 kmem_cache_destroy(btrfs_inode_cachep);
2403 if (btrfs_trans_handle_cachep)
2404 kmem_cache_destroy(btrfs_trans_handle_cachep);
2405 if (btrfs_transaction_cachep)
2406 kmem_cache_destroy(btrfs_transaction_cachep);
2407 if (btrfs_bit_radix_cachep)
2408 kmem_cache_destroy(btrfs_bit_radix_cachep);
2409 if (btrfs_path_cachep)
2410 kmem_cache_destroy(btrfs_path_cachep);
2413 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2414 unsigned long extra_flags,
2415 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2416 void (*ctor)(struct kmem_cache *, void *)
2418 void (*ctor)(void *, struct kmem_cache *,
2423 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2424 SLAB_MEM_SPREAD | extra_flags), ctor
2425 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2431 int btrfs_init_cachep(void)
2433 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2434 sizeof(struct btrfs_inode),
2436 if (!btrfs_inode_cachep)
2438 btrfs_trans_handle_cachep =
2439 btrfs_cache_create("btrfs_trans_handle_cache",
2440 sizeof(struct btrfs_trans_handle),
2442 if (!btrfs_trans_handle_cachep)
2444 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2445 sizeof(struct btrfs_transaction),
2447 if (!btrfs_transaction_cachep)
2449 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2450 sizeof(struct btrfs_path),
2452 if (!btrfs_path_cachep)
2454 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2455 SLAB_DESTROY_BY_RCU, NULL);
2456 if (!btrfs_bit_radix_cachep)
2460 btrfs_destroy_cachep();
2464 static int btrfs_getattr(struct vfsmount *mnt,
2465 struct dentry *dentry, struct kstat *stat)
2467 struct inode *inode = dentry->d_inode;
2468 generic_fillattr(inode, stat);
2469 stat->blksize = 256 * 1024;
2473 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2474 struct inode * new_dir,struct dentry *new_dentry)
2476 struct btrfs_trans_handle *trans;
2477 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2478 struct inode *new_inode = new_dentry->d_inode;
2479 struct inode *old_inode = old_dentry->d_inode;
2480 struct timespec ctime = CURRENT_TIME;
2481 struct btrfs_path *path;
2484 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2485 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2489 mutex_lock(&root->fs_info->fs_mutex);
2490 trans = btrfs_start_transaction(root, 1);
2492 btrfs_set_trans_block_group(trans, new_dir);
2493 path = btrfs_alloc_path();
2499 old_dentry->d_inode->i_nlink++;
2500 old_dir->i_ctime = old_dir->i_mtime = ctime;
2501 new_dir->i_ctime = new_dir->i_mtime = ctime;
2502 old_inode->i_ctime = ctime;
2504 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2509 new_inode->i_ctime = CURRENT_TIME;
2510 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2514 ret = btrfs_add_link(trans, new_dentry, old_inode);
2519 btrfs_free_path(path);
2520 btrfs_end_transaction(trans, root);
2521 mutex_unlock(&root->fs_info->fs_mutex);
2525 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2526 const char *symname)
2528 struct btrfs_trans_handle *trans;
2529 struct btrfs_root *root = BTRFS_I(dir)->root;
2530 struct btrfs_path *path;
2531 struct btrfs_key key;
2532 struct inode *inode;
2539 struct btrfs_file_extent_item *ei;
2540 struct extent_buffer *leaf;
2543 name_len = strlen(symname) + 1;
2544 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2545 return -ENAMETOOLONG;
2546 mutex_lock(&root->fs_info->fs_mutex);
2547 trans = btrfs_start_transaction(root, 1);
2548 btrfs_set_trans_block_group(trans, dir);
2550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2556 inode = btrfs_new_inode(trans, root, objectid,
2557 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2558 err = PTR_ERR(inode);
2562 btrfs_set_trans_block_group(trans, inode);
2563 err = btrfs_add_nondir(trans, dentry, inode);
2567 inode->i_mapping->a_ops = &btrfs_aops;
2568 inode->i_fop = &btrfs_file_operations;
2569 inode->i_op = &btrfs_file_inode_operations;
2570 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2571 inode->i_mapping, GFP_NOFS);
2572 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2574 dir->i_sb->s_dirt = 1;
2575 btrfs_update_inode_block_group(trans, inode);
2576 btrfs_update_inode_block_group(trans, dir);
2580 path = btrfs_alloc_path();
2582 key.objectid = inode->i_ino;
2584 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2585 datasize = btrfs_file_extent_calc_inline_size(name_len);
2586 err = btrfs_insert_empty_item(trans, root, path, &key,
2592 leaf = path->nodes[0];
2593 ei = btrfs_item_ptr(leaf, path->slots[0],
2594 struct btrfs_file_extent_item);
2595 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2596 btrfs_set_file_extent_type(leaf, ei,
2597 BTRFS_FILE_EXTENT_INLINE);
2598 ptr = btrfs_file_extent_inline_start(ei);
2599 write_extent_buffer(leaf, symname, ptr, name_len);
2600 btrfs_mark_buffer_dirty(leaf);
2601 btrfs_free_path(path);
2603 inode->i_op = &btrfs_symlink_inode_operations;
2604 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2605 inode->i_size = name_len - 1;
2606 err = btrfs_update_inode(trans, root, inode);
2611 nr = trans->blocks_used;
2612 btrfs_end_transaction(trans, root);
2613 mutex_unlock(&root->fs_info->fs_mutex);
2615 inode_dec_link_count(inode);
2618 btrfs_btree_balance_dirty(root, nr);
2622 static struct inode_operations btrfs_dir_inode_operations = {
2623 .lookup = btrfs_lookup,
2624 .create = btrfs_create,
2625 .unlink = btrfs_unlink,
2627 .mkdir = btrfs_mkdir,
2628 .rmdir = btrfs_rmdir,
2629 .rename = btrfs_rename,
2630 .symlink = btrfs_symlink,
2631 .setattr = btrfs_setattr,
2632 .mknod = btrfs_mknod,
2633 .setxattr = generic_setxattr,
2634 .getxattr = generic_getxattr,
2635 .listxattr = btrfs_listxattr,
2636 .removexattr = generic_removexattr,
2639 static struct inode_operations btrfs_dir_ro_inode_operations = {
2640 .lookup = btrfs_lookup,
2643 static struct file_operations btrfs_dir_file_operations = {
2644 .llseek = generic_file_llseek,
2645 .read = generic_read_dir,
2646 .readdir = btrfs_readdir,
2647 .unlocked_ioctl = btrfs_ioctl,
2648 #ifdef CONFIG_COMPAT
2649 .compat_ioctl = btrfs_ioctl,
2653 static struct extent_map_ops btrfs_extent_map_ops = {
2654 .fill_delalloc = run_delalloc_range,
2655 .writepage_io_hook = btrfs_writepage_io_hook,
2656 .readpage_io_hook = btrfs_readpage_io_hook,
2657 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2660 static struct address_space_operations btrfs_aops = {
2661 .readpage = btrfs_readpage,
2662 .writepage = btrfs_writepage,
2663 .writepages = btrfs_writepages,
2664 .readpages = btrfs_readpages,
2665 .sync_page = block_sync_page,
2666 .prepare_write = btrfs_prepare_write,
2667 .commit_write = btrfs_commit_write,
2669 .invalidatepage = btrfs_invalidatepage,
2670 .releasepage = btrfs_releasepage,
2671 .set_page_dirty = __set_page_dirty_nobuffers,
2674 static struct address_space_operations btrfs_symlink_aops = {
2675 .readpage = btrfs_readpage,
2676 .writepage = btrfs_writepage,
2677 .invalidatepage = btrfs_invalidatepage,
2678 .releasepage = btrfs_releasepage,
2681 static struct inode_operations btrfs_file_inode_operations = {
2682 .truncate = btrfs_truncate,
2683 .getattr = btrfs_getattr,
2684 .setattr = btrfs_setattr,
2685 .setxattr = generic_setxattr,
2686 .getxattr = generic_getxattr,
2687 .listxattr = btrfs_listxattr,
2688 .removexattr = generic_removexattr,
2691 static struct inode_operations btrfs_special_inode_operations = {
2692 .getattr = btrfs_getattr,
2693 .setattr = btrfs_setattr,
2696 static struct inode_operations btrfs_symlink_inode_operations = {
2697 .readlink = generic_readlink,
2698 .follow_link = page_follow_link_light,
2699 .put_link = page_put_link,