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 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
536 dentry->d_inode->i_nlink--;
538 drop_nlink(dentry->d_inode);
540 ret = btrfs_update_inode(trans, root, dentry->d_inode);
541 dir->i_sb->s_dirt = 1;
546 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
548 struct btrfs_root *root;
549 struct btrfs_trans_handle *trans;
553 root = BTRFS_I(dir)->root;
554 mutex_lock(&root->fs_info->fs_mutex);
555 trans = btrfs_start_transaction(root, 1);
557 btrfs_set_trans_block_group(trans, dir);
558 ret = btrfs_unlink_trans(trans, root, dir, dentry);
559 nr = trans->blocks_used;
561 btrfs_end_transaction(trans, root);
562 mutex_unlock(&root->fs_info->fs_mutex);
563 btrfs_btree_balance_dirty(root, nr);
568 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
570 struct inode *inode = dentry->d_inode;
573 struct btrfs_root *root = BTRFS_I(dir)->root;
574 struct btrfs_trans_handle *trans;
577 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
580 mutex_lock(&root->fs_info->fs_mutex);
581 trans = btrfs_start_transaction(root, 1);
582 btrfs_set_trans_block_group(trans, dir);
584 /* now the directory is empty */
585 err = btrfs_unlink_trans(trans, root, dir, dentry);
590 nr = trans->blocks_used;
591 ret = btrfs_end_transaction(trans, root);
592 mutex_unlock(&root->fs_info->fs_mutex);
593 btrfs_btree_balance_dirty(root, nr);
600 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
601 struct btrfs_root *root,
604 struct btrfs_path *path;
609 path = btrfs_alloc_path();
611 ret = btrfs_lookup_inode(trans, root, path,
612 &BTRFS_I(inode)->location, -1);
616 ret = btrfs_del_item(trans, root, path);
617 btrfs_free_path(path);
622 * this can truncate away extent items, csum items and directory items.
623 * It starts at a high offset and removes keys until it can't find
624 * any higher than i_size.
626 * csum items that cross the new i_size are truncated to the new size
629 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
630 struct btrfs_root *root,
634 struct btrfs_path *path;
635 struct btrfs_key key;
636 struct btrfs_key found_key;
638 struct extent_buffer *leaf;
639 struct btrfs_file_extent_item *fi;
640 u64 extent_start = 0;
641 u64 extent_num_bytes = 0;
647 int extent_type = -1;
649 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
650 path = btrfs_alloc_path();
654 /* FIXME, add redo link to tree so we don't leak on crash */
655 key.objectid = inode->i_ino;
656 key.offset = (u64)-1;
660 btrfs_init_path(path);
662 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
667 BUG_ON(path->slots[0] == 0);
670 leaf = path->nodes[0];
671 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
672 found_type = btrfs_key_type(&found_key);
674 if (found_key.objectid != inode->i_ino)
677 if (found_type != BTRFS_CSUM_ITEM_KEY &&
678 found_type != BTRFS_DIR_ITEM_KEY &&
679 found_type != BTRFS_DIR_INDEX_KEY &&
680 found_type != BTRFS_EXTENT_DATA_KEY)
683 item_end = found_key.offset;
684 if (found_type == BTRFS_EXTENT_DATA_KEY) {
685 fi = btrfs_item_ptr(leaf, path->slots[0],
686 struct btrfs_file_extent_item);
687 extent_type = btrfs_file_extent_type(leaf, fi);
688 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
690 btrfs_file_extent_num_bytes(leaf, fi);
691 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
692 struct btrfs_item *item = btrfs_item_nr(leaf,
694 item_end += btrfs_file_extent_inline_len(leaf,
699 if (found_type == BTRFS_CSUM_ITEM_KEY) {
700 ret = btrfs_csum_truncate(trans, root, path,
704 if (item_end < inode->i_size) {
705 if (found_type == BTRFS_DIR_ITEM_KEY) {
706 found_type = BTRFS_INODE_ITEM_KEY;
707 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
708 found_type = BTRFS_CSUM_ITEM_KEY;
709 } else if (found_type) {
714 btrfs_set_key_type(&key, found_type);
715 btrfs_release_path(root, path);
718 if (found_key.offset >= inode->i_size)
724 /* FIXME, shrink the extent if the ref count is only 1 */
725 if (found_type != BTRFS_EXTENT_DATA_KEY)
728 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
730 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
733 btrfs_file_extent_num_bytes(leaf, fi);
734 extent_num_bytes = inode->i_size -
735 found_key.offset + root->sectorsize - 1;
736 btrfs_set_file_extent_num_bytes(leaf, fi,
738 num_dec = (orig_num_bytes -
739 extent_num_bytes) >> 9;
740 if (extent_start != 0) {
741 inode->i_blocks -= num_dec;
743 btrfs_mark_buffer_dirty(leaf);
746 btrfs_file_extent_disk_num_bytes(leaf,
748 /* FIXME blocksize != 4096 */
749 num_dec = btrfs_file_extent_num_bytes(leaf,
751 if (extent_start != 0) {
753 inode->i_blocks -= num_dec;
755 root_gen = btrfs_header_generation(leaf);
756 root_owner = btrfs_header_owner(leaf);
758 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
760 u32 newsize = inode->i_size - found_key.offset;
761 newsize = btrfs_file_extent_calc_inline_size(newsize);
762 ret = btrfs_truncate_item(trans, root, path,
768 ret = btrfs_del_item(trans, root, path);
774 btrfs_release_path(root, path);
776 ret = btrfs_free_extent(trans, root, extent_start,
779 root_gen, inode->i_ino,
780 found_key.offset, 0);
786 btrfs_release_path(root, path);
787 btrfs_free_path(path);
788 inode->i_sb->s_dirt = 1;
792 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
797 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
798 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
799 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
801 WARN_ON(!PageLocked(page));
802 set_page_extent_mapped(page);
804 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
805 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
807 if (zero_start != PAGE_CACHE_SIZE) {
809 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
810 flush_dcache_page(page);
813 set_page_dirty(page);
814 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
820 * taken from block_truncate_page, but does cow as it zeros out
821 * any bytes left in the last page in the file.
823 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
825 struct inode *inode = mapping->host;
826 struct btrfs_root *root = BTRFS_I(inode)->root;
827 u32 blocksize = root->sectorsize;
828 pgoff_t index = from >> PAGE_CACHE_SHIFT;
829 unsigned offset = from & (PAGE_CACHE_SIZE-1);
834 if ((offset & (blocksize - 1)) == 0)
837 down_read(&root->snap_sem);
839 page = grab_cache_page(mapping, index);
842 if (!PageUptodate(page)) {
843 ret = btrfs_readpage(NULL, page);
845 if (!PageUptodate(page)) {
850 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
852 ret = btrfs_cow_one_page(inode, page, offset);
855 page_cache_release(page);
856 up_read(&BTRFS_I(inode)->root->snap_sem);
861 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
863 struct inode *inode = dentry->d_inode;
866 err = inode_change_ok(inode, attr);
870 if (S_ISREG(inode->i_mode) &&
871 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
872 struct btrfs_trans_handle *trans;
873 struct btrfs_root *root = BTRFS_I(inode)->root;
874 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
876 u64 mask = root->sectorsize - 1;
877 u64 pos = (inode->i_size + mask) & ~mask;
878 u64 block_end = attr->ia_size | mask;
882 if (attr->ia_size <= pos)
885 btrfs_truncate_page(inode->i_mapping, inode->i_size);
887 lock_extent(em_tree, pos, block_end, GFP_NOFS);
888 hole_size = (attr->ia_size - pos + mask) & ~mask;
890 mutex_lock(&root->fs_info->fs_mutex);
891 trans = btrfs_start_transaction(root, 1);
892 btrfs_set_trans_block_group(trans, inode);
893 err = btrfs_drop_extents(trans, root, inode,
894 pos, pos + hole_size, pos,
897 if (alloc_hint != EXTENT_MAP_INLINE) {
898 err = btrfs_insert_file_extent(trans, root,
900 pos, 0, 0, hole_size);
902 btrfs_end_transaction(trans, root);
903 mutex_unlock(&root->fs_info->fs_mutex);
904 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
909 err = inode_setattr(inode, attr);
913 void btrfs_delete_inode(struct inode *inode)
915 struct btrfs_trans_handle *trans;
916 struct btrfs_root *root = BTRFS_I(inode)->root;
920 truncate_inode_pages(&inode->i_data, 0);
921 if (is_bad_inode(inode)) {
926 mutex_lock(&root->fs_info->fs_mutex);
927 trans = btrfs_start_transaction(root, 1);
929 btrfs_set_trans_block_group(trans, inode);
930 ret = btrfs_truncate_in_trans(trans, root, inode);
933 ret = btrfs_delete_xattrs(trans, root, inode);
936 ret = btrfs_free_inode(trans, root, inode);
939 nr = trans->blocks_used;
941 btrfs_end_transaction(trans, root);
942 mutex_unlock(&root->fs_info->fs_mutex);
943 btrfs_btree_balance_dirty(root, nr);
947 nr = trans->blocks_used;
948 btrfs_end_transaction(trans, root);
949 mutex_unlock(&root->fs_info->fs_mutex);
950 btrfs_btree_balance_dirty(root, nr);
956 * this returns the key found in the dir entry in the location pointer.
957 * If no dir entries were found, location->objectid is 0.
959 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
960 struct btrfs_key *location)
962 const char *name = dentry->d_name.name;
963 int namelen = dentry->d_name.len;
964 struct btrfs_dir_item *di;
965 struct btrfs_path *path;
966 struct btrfs_root *root = BTRFS_I(dir)->root;
969 if (namelen == 1 && strcmp(name, ".") == 0) {
970 location->objectid = dir->i_ino;
971 location->type = BTRFS_INODE_ITEM_KEY;
972 location->offset = 0;
975 path = btrfs_alloc_path();
978 if (namelen == 2 && strcmp(name, "..") == 0) {
979 struct btrfs_key key;
980 struct extent_buffer *leaf;
984 key.objectid = dir->i_ino;
985 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
987 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
991 leaf = path->nodes[0];
992 slot = path->slots[0];
993 nritems = btrfs_header_nritems(leaf);
997 btrfs_item_key_to_cpu(leaf, &key, slot);
998 if (key.objectid != dir->i_ino ||
999 key.type != BTRFS_INODE_REF_KEY) {
1002 location->objectid = key.offset;
1003 location->type = BTRFS_INODE_ITEM_KEY;
1004 location->offset = 0;
1008 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1012 if (!di || IS_ERR(di)) {
1015 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1017 btrfs_free_path(path);
1020 location->objectid = 0;
1025 * when we hit a tree root in a directory, the btrfs part of the inode
1026 * needs to be changed to reflect the root directory of the tree root. This
1027 * is kind of like crossing a mount point.
1029 static int fixup_tree_root_location(struct btrfs_root *root,
1030 struct btrfs_key *location,
1031 struct btrfs_root **sub_root,
1032 struct dentry *dentry)
1034 struct btrfs_path *path;
1035 struct btrfs_root_item *ri;
1037 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1039 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1042 path = btrfs_alloc_path();
1044 mutex_lock(&root->fs_info->fs_mutex);
1046 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1047 dentry->d_name.name,
1048 dentry->d_name.len);
1049 if (IS_ERR(*sub_root))
1050 return PTR_ERR(*sub_root);
1052 ri = &(*sub_root)->root_item;
1053 location->objectid = btrfs_root_dirid(ri);
1054 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1055 location->offset = 0;
1057 btrfs_free_path(path);
1058 mutex_unlock(&root->fs_info->fs_mutex);
1062 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1064 struct btrfs_iget_args *args = p;
1065 inode->i_ino = args->ino;
1066 BTRFS_I(inode)->root = args->root;
1067 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1068 inode->i_mapping, GFP_NOFS);
1072 static int btrfs_find_actor(struct inode *inode, void *opaque)
1074 struct btrfs_iget_args *args = opaque;
1075 return (args->ino == inode->i_ino &&
1076 args->root == BTRFS_I(inode)->root);
1079 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1080 struct btrfs_root *root)
1082 struct inode *inode;
1083 struct btrfs_iget_args args;
1084 args.ino = objectid;
1087 inode = iget5_locked(s, objectid, btrfs_find_actor,
1088 btrfs_init_locked_inode,
1093 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1094 struct nameidata *nd)
1096 struct inode * inode;
1097 struct btrfs_inode *bi = BTRFS_I(dir);
1098 struct btrfs_root *root = bi->root;
1099 struct btrfs_root *sub_root = root;
1100 struct btrfs_key location;
1103 if (dentry->d_name.len > BTRFS_NAME_LEN)
1104 return ERR_PTR(-ENAMETOOLONG);
1106 mutex_lock(&root->fs_info->fs_mutex);
1107 ret = btrfs_inode_by_name(dir, dentry, &location);
1108 mutex_unlock(&root->fs_info->fs_mutex);
1111 return ERR_PTR(ret);
1114 if (location.objectid) {
1115 ret = fixup_tree_root_location(root, &location, &sub_root,
1118 return ERR_PTR(ret);
1120 return ERR_PTR(-ENOENT);
1121 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1124 return ERR_PTR(-EACCES);
1125 if (inode->i_state & I_NEW) {
1126 /* the inode and parent dir are two different roots */
1127 if (sub_root != root) {
1129 sub_root->inode = inode;
1131 BTRFS_I(inode)->root = sub_root;
1132 memcpy(&BTRFS_I(inode)->location, &location,
1134 btrfs_read_locked_inode(inode);
1135 unlock_new_inode(inode);
1138 return d_splice_alias(inode, dentry);
1141 static unsigned char btrfs_filetype_table[] = {
1142 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1145 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1147 struct inode *inode = filp->f_dentry->d_inode;
1148 struct btrfs_root *root = BTRFS_I(inode)->root;
1149 struct btrfs_item *item;
1150 struct btrfs_dir_item *di;
1151 struct btrfs_key key;
1152 struct btrfs_key found_key;
1153 struct btrfs_path *path;
1156 struct extent_buffer *leaf;
1159 unsigned char d_type;
1164 int key_type = BTRFS_DIR_INDEX_KEY;
1169 /* FIXME, use a real flag for deciding about the key type */
1170 if (root->fs_info->tree_root == root)
1171 key_type = BTRFS_DIR_ITEM_KEY;
1173 /* special case for "." */
1174 if (filp->f_pos == 0) {
1175 over = filldir(dirent, ".", 1,
1183 mutex_lock(&root->fs_info->fs_mutex);
1184 key.objectid = inode->i_ino;
1185 path = btrfs_alloc_path();
1188 /* special case for .., just use the back ref */
1189 if (filp->f_pos == 1) {
1190 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1194 leaf = path->nodes[0];
1195 slot = path->slots[0];
1196 nritems = btrfs_header_nritems(leaf);
1197 if (slot >= nritems) {
1198 btrfs_release_path(root, path);
1199 goto read_dir_items;
1201 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1202 btrfs_release_path(root, path);
1203 if (found_key.objectid != key.objectid ||
1204 found_key.type != BTRFS_INODE_REF_KEY)
1205 goto read_dir_items;
1206 over = filldir(dirent, "..", 2,
1207 2, found_key.offset, DT_DIR);
1214 btrfs_set_key_type(&key, key_type);
1215 key.offset = filp->f_pos;
1217 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1222 leaf = path->nodes[0];
1223 nritems = btrfs_header_nritems(leaf);
1224 slot = path->slots[0];
1225 if (advance || slot >= nritems) {
1226 if (slot >= nritems -1) {
1227 ret = btrfs_next_leaf(root, path);
1230 leaf = path->nodes[0];
1231 nritems = btrfs_header_nritems(leaf);
1232 slot = path->slots[0];
1239 item = btrfs_item_nr(leaf, slot);
1240 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1242 if (found_key.objectid != key.objectid)
1244 if (btrfs_key_type(&found_key) != key_type)
1246 if (found_key.offset < filp->f_pos)
1249 filp->f_pos = found_key.offset;
1251 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1253 di_total = btrfs_item_size(leaf, item);
1254 while(di_cur < di_total) {
1255 struct btrfs_key location;
1257 name_len = btrfs_dir_name_len(leaf, di);
1258 if (name_len < 32) {
1259 name_ptr = tmp_name;
1261 name_ptr = kmalloc(name_len, GFP_NOFS);
1264 read_extent_buffer(leaf, name_ptr,
1265 (unsigned long)(di + 1), name_len);
1267 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1268 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1270 over = filldir(dirent, name_ptr, name_len,
1275 if (name_ptr != tmp_name)
1280 di_len = btrfs_dir_name_len(leaf, di) +
1281 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1283 di = (struct btrfs_dir_item *)((char *)di + di_len);
1290 btrfs_release_path(root, path);
1291 btrfs_free_path(path);
1292 mutex_unlock(&root->fs_info->fs_mutex);
1296 int btrfs_write_inode(struct inode *inode, int wait)
1298 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 struct btrfs_trans_handle *trans;
1303 mutex_lock(&root->fs_info->fs_mutex);
1304 trans = btrfs_start_transaction(root, 1);
1305 btrfs_set_trans_block_group(trans, inode);
1306 ret = btrfs_commit_transaction(trans, root);
1307 mutex_unlock(&root->fs_info->fs_mutex);
1313 * This is somewhat expensive, updating the tree every time the
1314 * inode changes. But, it is most likely to find the inode in cache.
1315 * FIXME, needs more benchmarking...there are no reasons other than performance
1316 * to keep or drop this code.
1318 void btrfs_dirty_inode(struct inode *inode)
1320 struct btrfs_root *root = BTRFS_I(inode)->root;
1321 struct btrfs_trans_handle *trans;
1323 mutex_lock(&root->fs_info->fs_mutex);
1324 trans = btrfs_start_transaction(root, 1);
1325 btrfs_set_trans_block_group(trans, inode);
1326 btrfs_update_inode(trans, root, inode);
1327 btrfs_end_transaction(trans, root);
1328 mutex_unlock(&root->fs_info->fs_mutex);
1331 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root,
1334 struct btrfs_block_group_cache *group,
1337 struct inode *inode;
1338 struct btrfs_inode_item *inode_item;
1339 struct btrfs_key *location;
1340 struct btrfs_path *path;
1344 path = btrfs_alloc_path();
1347 inode = new_inode(root->fs_info->sb);
1349 return ERR_PTR(-ENOMEM);
1351 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1352 inode->i_mapping, GFP_NOFS);
1353 BTRFS_I(inode)->root = root;
1359 group = btrfs_find_block_group(root, group, 0, 0, owner);
1360 BTRFS_I(inode)->block_group = group;
1362 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1366 inode->i_uid = current->fsuid;
1367 inode->i_gid = current->fsgid;
1368 inode->i_mode = mode;
1369 inode->i_ino = objectid;
1370 inode->i_blocks = 0;
1371 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1372 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1373 struct btrfs_inode_item);
1374 fill_inode_item(path->nodes[0], inode_item, inode);
1375 btrfs_mark_buffer_dirty(path->nodes[0]);
1376 btrfs_free_path(path);
1378 location = &BTRFS_I(inode)->location;
1379 location->objectid = objectid;
1380 location->offset = 0;
1381 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1383 insert_inode_hash(inode);
1386 btrfs_free_path(path);
1387 return ERR_PTR(ret);
1390 static inline u8 btrfs_inode_type(struct inode *inode)
1392 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1395 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1396 struct dentry *dentry, struct inode *inode)
1399 struct btrfs_key key;
1400 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1401 struct inode *parent_inode;
1403 key.objectid = inode->i_ino;
1404 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1407 ret = btrfs_insert_dir_item(trans, root,
1408 dentry->d_name.name, dentry->d_name.len,
1409 dentry->d_parent->d_inode->i_ino,
1410 &key, btrfs_inode_type(inode));
1412 ret = btrfs_insert_inode_ref(trans, root,
1413 dentry->d_name.name,
1416 dentry->d_parent->d_inode->i_ino);
1417 parent_inode = dentry->d_parent->d_inode;
1418 parent_inode->i_size += dentry->d_name.len * 2;
1419 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1420 ret = btrfs_update_inode(trans, root,
1421 dentry->d_parent->d_inode);
1426 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1427 struct dentry *dentry, struct inode *inode)
1429 int err = btrfs_add_link(trans, dentry, inode);
1431 d_instantiate(dentry, inode);
1439 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1440 int mode, dev_t rdev)
1442 struct btrfs_trans_handle *trans;
1443 struct btrfs_root *root = BTRFS_I(dir)->root;
1444 struct inode *inode;
1450 if (!new_valid_dev(rdev))
1453 mutex_lock(&root->fs_info->fs_mutex);
1454 trans = btrfs_start_transaction(root, 1);
1455 btrfs_set_trans_block_group(trans, dir);
1457 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1463 inode = btrfs_new_inode(trans, root, objectid,
1464 BTRFS_I(dir)->block_group, mode);
1465 err = PTR_ERR(inode);
1469 btrfs_set_trans_block_group(trans, inode);
1470 err = btrfs_add_nondir(trans, dentry, inode);
1474 inode->i_op = &btrfs_special_inode_operations;
1475 init_special_inode(inode, inode->i_mode, rdev);
1476 btrfs_update_inode(trans, root, inode);
1478 dir->i_sb->s_dirt = 1;
1479 btrfs_update_inode_block_group(trans, inode);
1480 btrfs_update_inode_block_group(trans, dir);
1482 nr = trans->blocks_used;
1483 btrfs_end_transaction(trans, root);
1484 mutex_unlock(&root->fs_info->fs_mutex);
1487 inode_dec_link_count(inode);
1490 btrfs_btree_balance_dirty(root, nr);
1494 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1495 int mode, struct nameidata *nd)
1497 struct btrfs_trans_handle *trans;
1498 struct btrfs_root *root = BTRFS_I(dir)->root;
1499 struct inode *inode;
1505 mutex_lock(&root->fs_info->fs_mutex);
1506 trans = btrfs_start_transaction(root, 1);
1507 btrfs_set_trans_block_group(trans, dir);
1509 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1515 inode = btrfs_new_inode(trans, root, objectid,
1516 BTRFS_I(dir)->block_group, mode);
1517 err = PTR_ERR(inode);
1521 btrfs_set_trans_block_group(trans, inode);
1522 err = btrfs_add_nondir(trans, dentry, inode);
1526 inode->i_mapping->a_ops = &btrfs_aops;
1527 inode->i_fop = &btrfs_file_operations;
1528 inode->i_op = &btrfs_file_inode_operations;
1529 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1530 inode->i_mapping, GFP_NOFS);
1531 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1533 dir->i_sb->s_dirt = 1;
1534 btrfs_update_inode_block_group(trans, inode);
1535 btrfs_update_inode_block_group(trans, dir);
1537 nr = trans->blocks_used;
1538 btrfs_end_transaction(trans, root);
1539 mutex_unlock(&root->fs_info->fs_mutex);
1542 inode_dec_link_count(inode);
1545 btrfs_btree_balance_dirty(root, nr);
1549 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1550 struct dentry *dentry)
1552 struct btrfs_trans_handle *trans;
1553 struct btrfs_root *root = BTRFS_I(dir)->root;
1554 struct inode *inode = old_dentry->d_inode;
1559 if (inode->i_nlink == 0)
1562 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1567 mutex_lock(&root->fs_info->fs_mutex);
1568 trans = btrfs_start_transaction(root, 1);
1570 btrfs_set_trans_block_group(trans, dir);
1571 atomic_inc(&inode->i_count);
1572 err = btrfs_add_nondir(trans, dentry, inode);
1577 dir->i_sb->s_dirt = 1;
1578 btrfs_update_inode_block_group(trans, dir);
1579 err = btrfs_update_inode(trans, root, inode);
1584 nr = trans->blocks_used;
1585 btrfs_end_transaction(trans, root);
1586 mutex_unlock(&root->fs_info->fs_mutex);
1589 inode_dec_link_count(inode);
1592 btrfs_btree_balance_dirty(root, nr);
1596 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1598 struct inode *inode;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_root *root = BTRFS_I(dir)->root;
1602 int drop_on_err = 0;
1604 unsigned long nr = 1;
1606 mutex_lock(&root->fs_info->fs_mutex);
1607 trans = btrfs_start_transaction(root, 1);
1608 btrfs_set_trans_block_group(trans, dir);
1610 if (IS_ERR(trans)) {
1611 err = PTR_ERR(trans);
1615 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1621 inode = btrfs_new_inode(trans, root, objectid,
1622 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1623 if (IS_ERR(inode)) {
1624 err = PTR_ERR(inode);
1629 inode->i_op = &btrfs_dir_inode_operations;
1630 inode->i_fop = &btrfs_dir_file_operations;
1631 btrfs_set_trans_block_group(trans, inode);
1634 err = btrfs_update_inode(trans, root, inode);
1638 err = btrfs_add_link(trans, dentry, inode);
1642 d_instantiate(dentry, inode);
1644 dir->i_sb->s_dirt = 1;
1645 btrfs_update_inode_block_group(trans, inode);
1646 btrfs_update_inode_block_group(trans, dir);
1649 nr = trans->blocks_used;
1650 btrfs_end_transaction(trans, root);
1653 mutex_unlock(&root->fs_info->fs_mutex);
1656 btrfs_btree_balance_dirty(root, nr);
1660 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1661 size_t page_offset, u64 start, u64 end,
1667 u64 extent_start = 0;
1669 u64 objectid = inode->i_ino;
1671 int failed_insert = 0;
1672 struct btrfs_path *path;
1673 struct btrfs_root *root = BTRFS_I(inode)->root;
1674 struct btrfs_file_extent_item *item;
1675 struct extent_buffer *leaf;
1676 struct btrfs_key found_key;
1677 struct extent_map *em = NULL;
1678 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1679 struct btrfs_trans_handle *trans = NULL;
1681 path = btrfs_alloc_path();
1683 mutex_lock(&root->fs_info->fs_mutex);
1686 em = lookup_extent_mapping(em_tree, start, end);
1691 em = alloc_extent_map(GFP_NOFS);
1696 em->start = EXTENT_MAP_HOLE;
1697 em->end = EXTENT_MAP_HOLE;
1699 em->bdev = inode->i_sb->s_bdev;
1700 ret = btrfs_lookup_file_extent(trans, root, path,
1701 objectid, start, trans != NULL);
1708 if (path->slots[0] == 0)
1713 leaf = path->nodes[0];
1714 item = btrfs_item_ptr(leaf, path->slots[0],
1715 struct btrfs_file_extent_item);
1716 /* are we inside the extent that was found? */
1717 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1718 found_type = btrfs_key_type(&found_key);
1719 if (found_key.objectid != objectid ||
1720 found_type != BTRFS_EXTENT_DATA_KEY) {
1724 found_type = btrfs_file_extent_type(leaf, item);
1725 extent_start = found_key.offset;
1726 if (found_type == BTRFS_FILE_EXTENT_REG) {
1727 extent_end = extent_start +
1728 btrfs_file_extent_num_bytes(leaf, item);
1730 if (start < extent_start || start >= extent_end) {
1732 if (start < extent_start) {
1733 if (end < extent_start)
1735 em->end = extent_end - 1;
1741 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1743 em->start = extent_start;
1744 em->end = extent_end - 1;
1745 em->block_start = EXTENT_MAP_HOLE;
1746 em->block_end = EXTENT_MAP_HOLE;
1749 bytenr += btrfs_file_extent_offset(leaf, item);
1750 em->block_start = bytenr;
1751 em->block_end = em->block_start +
1752 btrfs_file_extent_num_bytes(leaf, item) - 1;
1753 em->start = extent_start;
1754 em->end = extent_end - 1;
1756 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1760 size_t extent_offset;
1763 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1765 extent_end = (extent_start + size - 1) |
1766 ((u64)root->sectorsize - 1);
1767 if (start < extent_start || start >= extent_end) {
1769 if (start < extent_start) {
1770 if (end < extent_start)
1772 em->end = extent_end;
1778 em->block_start = EXTENT_MAP_INLINE;
1779 em->block_end = EXTENT_MAP_INLINE;
1782 em->start = extent_start;
1783 em->end = extent_start + size - 1;
1787 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1788 extent_start + page_offset;
1789 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1790 size - extent_offset);
1791 em->start = extent_start + extent_offset;
1792 em->end = (em->start + copy_size -1) |
1793 ((u64)root->sectorsize -1);
1795 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1796 if (create == 0 && !PageUptodate(page)) {
1797 read_extent_buffer(leaf, map + page_offset, ptr,
1799 flush_dcache_page(page);
1800 } else if (create && PageUptodate(page)) {
1803 free_extent_map(em);
1805 btrfs_release_path(root, path);
1806 trans = btrfs_start_transaction(root, 1);
1809 write_extent_buffer(leaf, map + page_offset, ptr,
1811 btrfs_mark_buffer_dirty(leaf);
1814 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1817 printk("unkknown found_type %d\n", found_type);
1824 em->block_start = EXTENT_MAP_HOLE;
1825 em->block_end = EXTENT_MAP_HOLE;
1827 btrfs_release_path(root, path);
1828 if (em->start > start || em->end < start) {
1829 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1833 ret = add_extent_mapping(em_tree, em);
1834 if (ret == -EEXIST) {
1835 free_extent_map(em);
1837 if (0 && failed_insert == 1) {
1838 btrfs_drop_extent_cache(inode, start, end);
1841 if (failed_insert > 5) {
1842 printk("failing to insert %Lu %Lu\n", start, end);
1850 btrfs_free_path(path);
1852 ret = btrfs_end_transaction(trans, root);
1856 mutex_unlock(&root->fs_info->fs_mutex);
1858 free_extent_map(em);
1860 return ERR_PTR(err);
1865 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1867 return extent_bmap(mapping, iblock, btrfs_get_extent);
1870 static int btrfs_prepare_write(struct file *file, struct page *page,
1871 unsigned from, unsigned to)
1873 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1874 page->mapping->host, page, from, to,
1878 int btrfs_readpage(struct file *file, struct page *page)
1880 struct extent_map_tree *tree;
1881 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1882 return extent_read_full_page(tree, page, btrfs_get_extent);
1884 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1886 struct extent_map_tree *tree;
1889 if (current->flags & PF_MEMALLOC) {
1890 redirty_page_for_writepage(wbc, page);
1894 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1895 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1898 static int btrfs_writepages(struct address_space *mapping,
1899 struct writeback_control *wbc)
1901 struct extent_map_tree *tree;
1902 tree = &BTRFS_I(mapping->host)->extent_tree;
1903 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1907 btrfs_readpages(struct file *file, struct address_space *mapping,
1908 struct list_head *pages, unsigned nr_pages)
1910 struct extent_map_tree *tree;
1911 tree = &BTRFS_I(mapping->host)->extent_tree;
1912 return extent_readpages(tree, mapping, pages, nr_pages,
1916 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1918 struct extent_map_tree *tree;
1921 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1922 ret = try_release_extent_mapping(tree, page);
1924 ClearPagePrivate(page);
1925 set_page_private(page, 0);
1926 page_cache_release(page);
1931 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1933 struct extent_map_tree *tree;
1935 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1936 extent_invalidatepage(tree, page, offset);
1937 btrfs_releasepage(page, GFP_NOFS);
1941 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1942 * called from a page fault handler when a page is first dirtied. Hence we must
1943 * be careful to check for EOF conditions here. We set the page up correctly
1944 * for a written page which means we get ENOSPC checking when writing into
1945 * holes and correct delalloc and unwritten extent mapping on filesystems that
1946 * support these features.
1948 * We are not allowed to take the i_mutex here so we have to play games to
1949 * protect against truncate races as the page could now be beyond EOF. Because
1950 * vmtruncate() writes the inode size before removing pages, once we have the
1951 * page lock we can determine safely if the page is beyond EOF. If it is not
1952 * beyond EOF, then the page is guaranteed safe against truncation until we
1955 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1957 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1963 down_read(&BTRFS_I(inode)->root->snap_sem);
1965 wait_on_page_writeback(page);
1966 size = i_size_read(inode);
1967 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1969 if ((page->mapping != inode->i_mapping) ||
1970 (page_start > size)) {
1971 /* page got truncated out from underneath us */
1975 /* page is wholly or partially inside EOF */
1976 if (page_start + PAGE_CACHE_SIZE > size)
1977 end = size & ~PAGE_CACHE_MASK;
1979 end = PAGE_CACHE_SIZE;
1981 ret = btrfs_cow_one_page(inode, page, end);
1984 up_read(&BTRFS_I(inode)->root->snap_sem);
1989 static void btrfs_truncate(struct inode *inode)
1991 struct btrfs_root *root = BTRFS_I(inode)->root;
1993 struct btrfs_trans_handle *trans;
1996 if (!S_ISREG(inode->i_mode))
1998 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2001 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2003 mutex_lock(&root->fs_info->fs_mutex);
2004 trans = btrfs_start_transaction(root, 1);
2005 btrfs_set_trans_block_group(trans, inode);
2007 /* FIXME, add redo link to tree so we don't leak on crash */
2008 ret = btrfs_truncate_in_trans(trans, root, inode);
2009 btrfs_update_inode(trans, root, inode);
2010 nr = trans->blocks_used;
2012 ret = btrfs_end_transaction(trans, root);
2014 mutex_unlock(&root->fs_info->fs_mutex);
2015 btrfs_btree_balance_dirty(root, nr);
2018 int btrfs_commit_write(struct file *file, struct page *page,
2019 unsigned from, unsigned to)
2021 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2022 struct inode *inode = page->mapping->host;
2024 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2026 if (pos > inode->i_size) {
2027 i_size_write(inode, pos);
2028 mark_inode_dirty(inode);
2033 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2035 struct btrfs_trans_handle *trans;
2036 struct btrfs_key key;
2037 struct btrfs_root_item root_item;
2038 struct btrfs_inode_item *inode_item;
2039 struct extent_buffer *leaf;
2040 struct btrfs_root *new_root;
2041 struct inode *inode;
2046 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2047 unsigned long nr = 1;
2049 mutex_lock(&root->fs_info->fs_mutex);
2050 trans = btrfs_start_transaction(root, 1);
2053 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2058 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2059 objectid, trans->transid, 0, 0,
2062 return PTR_ERR(leaf);
2064 btrfs_set_header_nritems(leaf, 0);
2065 btrfs_set_header_level(leaf, 0);
2066 btrfs_set_header_bytenr(leaf, leaf->start);
2067 btrfs_set_header_generation(leaf, trans->transid);
2068 btrfs_set_header_owner(leaf, objectid);
2070 write_extent_buffer(leaf, root->fs_info->fsid,
2071 (unsigned long)btrfs_header_fsid(leaf),
2073 btrfs_mark_buffer_dirty(leaf);
2075 inode_item = &root_item.inode;
2076 memset(inode_item, 0, sizeof(*inode_item));
2077 inode_item->generation = cpu_to_le64(1);
2078 inode_item->size = cpu_to_le64(3);
2079 inode_item->nlink = cpu_to_le32(1);
2080 inode_item->nblocks = cpu_to_le64(1);
2081 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2083 btrfs_set_root_bytenr(&root_item, leaf->start);
2084 btrfs_set_root_level(&root_item, 0);
2085 btrfs_set_root_refs(&root_item, 1);
2086 btrfs_set_root_used(&root_item, 0);
2088 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2089 root_item.drop_level = 0;
2091 free_extent_buffer(leaf);
2094 btrfs_set_root_dirid(&root_item, new_dirid);
2096 key.objectid = objectid;
2098 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2099 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2105 * insert the directory item
2107 key.offset = (u64)-1;
2108 dir = root->fs_info->sb->s_root->d_inode;
2109 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2110 name, namelen, dir->i_ino, &key,
2115 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2116 name, namelen, objectid,
2117 root->fs_info->sb->s_root->d_inode->i_ino);
2121 ret = btrfs_commit_transaction(trans, root);
2125 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2128 trans = btrfs_start_transaction(new_root, 1);
2131 inode = btrfs_new_inode(trans, new_root, new_dirid,
2132 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2135 inode->i_op = &btrfs_dir_inode_operations;
2136 inode->i_fop = &btrfs_dir_file_operations;
2137 new_root->inode = inode;
2139 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2143 ret = btrfs_update_inode(trans, new_root, inode);
2147 nr = trans->blocks_used;
2148 err = btrfs_commit_transaction(trans, root);
2152 mutex_unlock(&root->fs_info->fs_mutex);
2153 btrfs_btree_balance_dirty(root, nr);
2157 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2159 struct btrfs_trans_handle *trans;
2160 struct btrfs_key key;
2161 struct btrfs_root_item new_root_item;
2162 struct extent_buffer *tmp;
2168 if (!root->ref_cows)
2171 down_write(&root->snap_sem);
2172 freeze_bdev(root->fs_info->sb->s_bdev);
2173 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2175 mutex_lock(&root->fs_info->fs_mutex);
2176 trans = btrfs_start_transaction(root, 1);
2179 ret = btrfs_update_inode(trans, root, root->inode);
2183 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2188 memcpy(&new_root_item, &root->root_item,
2189 sizeof(new_root_item));
2191 key.objectid = objectid;
2193 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2195 extent_buffer_get(root->node);
2196 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
2197 free_extent_buffer(tmp);
2199 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2201 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2202 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
2203 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2205 free_extent_buffer(tmp);
2210 * insert the directory item
2212 key.offset = (u64)-1;
2213 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2215 root->fs_info->sb->s_root->d_inode->i_ino,
2216 &key, BTRFS_FT_DIR);
2221 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2222 name, namelen, objectid,
2223 root->fs_info->sb->s_root->d_inode->i_ino);
2228 nr = trans->blocks_used;
2229 err = btrfs_commit_transaction(trans, root);
2234 mutex_unlock(&root->fs_info->fs_mutex);
2235 up_write(&root->snap_sem);
2236 btrfs_btree_balance_dirty(root, nr);
2240 static unsigned long force_ra(struct address_space *mapping,
2241 struct file_ra_state *ra, struct file *file,
2242 pgoff_t offset, pgoff_t last_index)
2246 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2247 req_size = last_index - offset + 1;
2248 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2251 req_size = min(last_index - offset + 1, (pgoff_t)128);
2252 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2253 return offset + req_size;
2257 int btrfs_defrag_file(struct file *file) {
2258 struct inode *inode = fdentry(file)->d_inode;
2259 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2261 unsigned long last_index;
2262 unsigned long ra_index = 0;
2267 mutex_lock(&inode->i_mutex);
2268 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2269 for (i = 0; i <= last_index; i++) {
2270 if (i == ra_index) {
2271 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2272 file, ra_index, last_index);
2274 page = grab_cache_page(inode->i_mapping, i);
2277 if (!PageUptodate(page)) {
2278 btrfs_readpage(NULL, page);
2280 if (!PageUptodate(page)) {
2282 page_cache_release(page);
2286 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2287 page_end = page_start + PAGE_CACHE_SIZE - 1;
2289 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2290 set_extent_delalloc(em_tree, page_start,
2291 page_end, GFP_NOFS);
2292 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2293 set_page_dirty(page);
2295 page_cache_release(page);
2296 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2300 mutex_unlock(&inode->i_mutex);
2304 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2306 struct btrfs_ioctl_vol_args *vol_args;
2307 struct btrfs_dir_item *di;
2308 struct btrfs_path *path;
2313 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2318 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2323 namelen = strlen(vol_args->name);
2324 if (namelen > BTRFS_VOL_NAME_MAX) {
2328 if (strchr(vol_args->name, '/')) {
2333 path = btrfs_alloc_path();
2339 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2340 mutex_lock(&root->fs_info->fs_mutex);
2341 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2343 vol_args->name, namelen, 0);
2344 mutex_unlock(&root->fs_info->fs_mutex);
2345 btrfs_free_path(path);
2347 if (di && !IS_ERR(di)) {
2357 if (root == root->fs_info->tree_root)
2358 ret = create_subvol(root, vol_args->name, namelen);
2360 ret = create_snapshot(root, vol_args->name, namelen);
2366 static int btrfs_ioctl_defrag(struct file *file)
2368 struct inode *inode = fdentry(file)->d_inode;
2369 struct btrfs_root *root = BTRFS_I(inode)->root;
2371 switch (inode->i_mode & S_IFMT) {
2373 mutex_lock(&root->fs_info->fs_mutex);
2374 btrfs_defrag_root(root, 0);
2375 btrfs_defrag_root(root->fs_info->extent_root, 0);
2376 mutex_unlock(&root->fs_info->fs_mutex);
2379 btrfs_defrag_file(file);
2386 long btrfs_ioctl(struct file *file, unsigned int
2387 cmd, unsigned long arg)
2389 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2392 case BTRFS_IOC_SNAP_CREATE:
2393 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2394 case BTRFS_IOC_DEFRAG:
2395 return btrfs_ioctl_defrag(file);
2402 * Called inside transaction, so use GFP_NOFS
2404 struct inode *btrfs_alloc_inode(struct super_block *sb)
2406 struct btrfs_inode *ei;
2408 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2412 return &ei->vfs_inode;
2415 void btrfs_destroy_inode(struct inode *inode)
2417 WARN_ON(!list_empty(&inode->i_dentry));
2418 WARN_ON(inode->i_data.nrpages);
2420 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2423 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2424 static void init_once(struct kmem_cache * cachep, void *foo)
2426 static void init_once(void * foo, struct kmem_cache * cachep,
2427 unsigned long flags)
2430 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2432 inode_init_once(&ei->vfs_inode);
2435 void btrfs_destroy_cachep(void)
2437 if (btrfs_inode_cachep)
2438 kmem_cache_destroy(btrfs_inode_cachep);
2439 if (btrfs_trans_handle_cachep)
2440 kmem_cache_destroy(btrfs_trans_handle_cachep);
2441 if (btrfs_transaction_cachep)
2442 kmem_cache_destroy(btrfs_transaction_cachep);
2443 if (btrfs_bit_radix_cachep)
2444 kmem_cache_destroy(btrfs_bit_radix_cachep);
2445 if (btrfs_path_cachep)
2446 kmem_cache_destroy(btrfs_path_cachep);
2449 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2450 unsigned long extra_flags,
2451 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2452 void (*ctor)(struct kmem_cache *, void *)
2454 void (*ctor)(void *, struct kmem_cache *,
2459 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2460 SLAB_MEM_SPREAD | extra_flags), ctor
2461 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2467 int btrfs_init_cachep(void)
2469 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2470 sizeof(struct btrfs_inode),
2472 if (!btrfs_inode_cachep)
2474 btrfs_trans_handle_cachep =
2475 btrfs_cache_create("btrfs_trans_handle_cache",
2476 sizeof(struct btrfs_trans_handle),
2478 if (!btrfs_trans_handle_cachep)
2480 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2481 sizeof(struct btrfs_transaction),
2483 if (!btrfs_transaction_cachep)
2485 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2486 sizeof(struct btrfs_path),
2488 if (!btrfs_path_cachep)
2490 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2491 SLAB_DESTROY_BY_RCU, NULL);
2492 if (!btrfs_bit_radix_cachep)
2496 btrfs_destroy_cachep();
2500 static int btrfs_getattr(struct vfsmount *mnt,
2501 struct dentry *dentry, struct kstat *stat)
2503 struct inode *inode = dentry->d_inode;
2504 generic_fillattr(inode, stat);
2505 stat->blksize = 256 * 1024;
2509 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2510 struct inode * new_dir,struct dentry *new_dentry)
2512 struct btrfs_trans_handle *trans;
2513 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2514 struct inode *new_inode = new_dentry->d_inode;
2515 struct inode *old_inode = old_dentry->d_inode;
2516 struct timespec ctime = CURRENT_TIME;
2517 struct btrfs_path *path;
2520 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2521 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2525 mutex_lock(&root->fs_info->fs_mutex);
2526 trans = btrfs_start_transaction(root, 1);
2528 btrfs_set_trans_block_group(trans, new_dir);
2529 path = btrfs_alloc_path();
2535 old_dentry->d_inode->i_nlink++;
2536 old_dir->i_ctime = old_dir->i_mtime = ctime;
2537 new_dir->i_ctime = new_dir->i_mtime = ctime;
2538 old_inode->i_ctime = ctime;
2540 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2545 new_inode->i_ctime = CURRENT_TIME;
2546 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2550 ret = btrfs_add_link(trans, new_dentry, old_inode);
2555 btrfs_free_path(path);
2556 btrfs_end_transaction(trans, root);
2557 mutex_unlock(&root->fs_info->fs_mutex);
2561 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2562 const char *symname)
2564 struct btrfs_trans_handle *trans;
2565 struct btrfs_root *root = BTRFS_I(dir)->root;
2566 struct btrfs_path *path;
2567 struct btrfs_key key;
2568 struct inode *inode;
2575 struct btrfs_file_extent_item *ei;
2576 struct extent_buffer *leaf;
2579 name_len = strlen(symname) + 1;
2580 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2581 return -ENAMETOOLONG;
2582 mutex_lock(&root->fs_info->fs_mutex);
2583 trans = btrfs_start_transaction(root, 1);
2584 btrfs_set_trans_block_group(trans, dir);
2586 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2592 inode = btrfs_new_inode(trans, root, objectid,
2593 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2594 err = PTR_ERR(inode);
2598 btrfs_set_trans_block_group(trans, inode);
2599 err = btrfs_add_nondir(trans, dentry, inode);
2603 inode->i_mapping->a_ops = &btrfs_aops;
2604 inode->i_fop = &btrfs_file_operations;
2605 inode->i_op = &btrfs_file_inode_operations;
2606 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2607 inode->i_mapping, GFP_NOFS);
2608 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2610 dir->i_sb->s_dirt = 1;
2611 btrfs_update_inode_block_group(trans, inode);
2612 btrfs_update_inode_block_group(trans, dir);
2616 path = btrfs_alloc_path();
2618 key.objectid = inode->i_ino;
2620 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2621 datasize = btrfs_file_extent_calc_inline_size(name_len);
2622 err = btrfs_insert_empty_item(trans, root, path, &key,
2628 leaf = path->nodes[0];
2629 ei = btrfs_item_ptr(leaf, path->slots[0],
2630 struct btrfs_file_extent_item);
2631 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2632 btrfs_set_file_extent_type(leaf, ei,
2633 BTRFS_FILE_EXTENT_INLINE);
2634 ptr = btrfs_file_extent_inline_start(ei);
2635 write_extent_buffer(leaf, symname, ptr, name_len);
2636 btrfs_mark_buffer_dirty(leaf);
2637 btrfs_free_path(path);
2639 inode->i_op = &btrfs_symlink_inode_operations;
2640 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2641 inode->i_size = name_len - 1;
2642 err = btrfs_update_inode(trans, root, inode);
2647 nr = trans->blocks_used;
2648 btrfs_end_transaction(trans, root);
2649 mutex_unlock(&root->fs_info->fs_mutex);
2651 inode_dec_link_count(inode);
2654 btrfs_btree_balance_dirty(root, nr);
2658 static struct inode_operations btrfs_dir_inode_operations = {
2659 .lookup = btrfs_lookup,
2660 .create = btrfs_create,
2661 .unlink = btrfs_unlink,
2663 .mkdir = btrfs_mkdir,
2664 .rmdir = btrfs_rmdir,
2665 .rename = btrfs_rename,
2666 .symlink = btrfs_symlink,
2667 .setattr = btrfs_setattr,
2668 .mknod = btrfs_mknod,
2669 .setxattr = generic_setxattr,
2670 .getxattr = generic_getxattr,
2671 .listxattr = btrfs_listxattr,
2672 .removexattr = generic_removexattr,
2675 static struct inode_operations btrfs_dir_ro_inode_operations = {
2676 .lookup = btrfs_lookup,
2679 static struct file_operations btrfs_dir_file_operations = {
2680 .llseek = generic_file_llseek,
2681 .read = generic_read_dir,
2682 .readdir = btrfs_readdir,
2683 .unlocked_ioctl = btrfs_ioctl,
2684 #ifdef CONFIG_COMPAT
2685 .compat_ioctl = btrfs_ioctl,
2689 static struct extent_map_ops btrfs_extent_map_ops = {
2690 .fill_delalloc = run_delalloc_range,
2691 .writepage_io_hook = btrfs_writepage_io_hook,
2692 .readpage_io_hook = btrfs_readpage_io_hook,
2693 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2696 static struct address_space_operations btrfs_aops = {
2697 .readpage = btrfs_readpage,
2698 .writepage = btrfs_writepage,
2699 .writepages = btrfs_writepages,
2700 .readpages = btrfs_readpages,
2701 .sync_page = block_sync_page,
2702 .prepare_write = btrfs_prepare_write,
2703 .commit_write = btrfs_commit_write,
2705 .invalidatepage = btrfs_invalidatepage,
2706 .releasepage = btrfs_releasepage,
2707 .set_page_dirty = __set_page_dirty_nobuffers,
2710 static struct address_space_operations btrfs_symlink_aops = {
2711 .readpage = btrfs_readpage,
2712 .writepage = btrfs_writepage,
2713 .invalidatepage = btrfs_invalidatepage,
2714 .releasepage = btrfs_releasepage,
2717 static struct inode_operations btrfs_file_inode_operations = {
2718 .truncate = btrfs_truncate,
2719 .getattr = btrfs_getattr,
2720 .setattr = btrfs_setattr,
2721 .setxattr = generic_setxattr,
2722 .getxattr = generic_getxattr,
2723 .listxattr = btrfs_listxattr,
2724 .removexattr = generic_removexattr,
2727 static struct inode_operations btrfs_special_inode_operations = {
2728 .getattr = btrfs_getattr,
2729 .setattr = btrfs_setattr,
2732 static struct inode_operations btrfs_symlink_inode_operations = {
2733 .readlink = generic_readlink,
2734 .follow_link = page_follow_link_light,
2735 .put_link = page_put_link,