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;
81 u64 blocksize = root->sectorsize;
85 trans = btrfs_start_transaction(root, 1);
87 btrfs_set_trans_block_group(trans, inode);
89 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
90 num_bytes = max(blocksize, num_bytes);
91 ret = btrfs_drop_extents(trans, root, inode,
92 start, start + num_bytes, start, &alloc_hint);
94 if (alloc_hint == EXTENT_MAP_INLINE)
97 ret = btrfs_alloc_extent(trans, root, num_bytes,
98 root->root_key.objectid, trans->transid,
99 inode->i_ino, start, 0,
100 alloc_hint, (u64)-1, &ins, 1);
105 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
106 start, ins.objectid, ins.offset,
109 btrfs_end_transaction(trans, root);
113 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
119 struct btrfs_root *root = BTRFS_I(inode)->root;
120 struct extent_buffer *leaf;
122 struct btrfs_path *path;
123 struct btrfs_file_extent_item *item;
126 struct btrfs_key found_key;
128 path = btrfs_alloc_path();
131 ret = btrfs_lookup_file_extent(NULL, root, path,
132 inode->i_ino, start, 0);
134 btrfs_free_path(path);
140 if (path->slots[0] == 0)
145 leaf = path->nodes[0];
146 item = btrfs_item_ptr(leaf, path->slots[0],
147 struct btrfs_file_extent_item);
149 /* are we inside the extent that was found? */
150 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
151 found_type = btrfs_key_type(&found_key);
152 if (found_key.objectid != inode->i_ino ||
153 found_type != BTRFS_EXTENT_DATA_KEY) {
157 found_type = btrfs_file_extent_type(leaf, item);
158 extent_start = found_key.offset;
159 if (found_type == BTRFS_FILE_EXTENT_REG) {
160 extent_end = extent_start +
161 btrfs_file_extent_num_bytes(leaf, item);
164 if (start < extent_start || start >= extent_end)
167 cow_end = min(end, extent_end - 1);
168 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
172 bytenr += btrfs_file_extent_offset(leaf, item);
173 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
178 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
183 btrfs_free_path(path);
186 btrfs_release_path(root, path);
190 cow_file_range(inode, start, cow_end);
195 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
197 struct btrfs_root *root = BTRFS_I(inode)->root;
200 mutex_lock(&root->fs_info->fs_mutex);
201 if (btrfs_test_opt(root, NODATACOW))
202 ret = run_delalloc_nocow(inode, start, end);
204 ret = cow_file_range(inode, start, end);
205 mutex_unlock(&root->fs_info->fs_mutex);
209 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
211 struct inode *inode = page->mapping->host;
212 struct btrfs_root *root = BTRFS_I(inode)->root;
213 struct btrfs_trans_handle *trans;
216 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
217 size_t offset = start - page_start;
219 if (btrfs_test_opt(root, NODATASUM))
222 mutex_lock(&root->fs_info->fs_mutex);
223 trans = btrfs_start_transaction(root, 1);
224 btrfs_set_trans_block_group(trans, inode);
226 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
227 start, kaddr + offset, end - start + 1);
229 ret = btrfs_end_transaction(trans, root);
231 mutex_unlock(&root->fs_info->fs_mutex);
235 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
238 struct inode *inode = page->mapping->host;
239 struct btrfs_root *root = BTRFS_I(inode)->root;
240 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
241 struct btrfs_csum_item *item;
242 struct btrfs_path *path = NULL;
245 if (btrfs_test_opt(root, NODATASUM))
248 mutex_lock(&root->fs_info->fs_mutex);
249 path = btrfs_alloc_path();
250 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
253 /* a csum that isn't present is a preallocated region. */
254 if (ret == -ENOENT || ret == -EFBIG)
259 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
261 set_state_private(em_tree, start, csum);
264 btrfs_free_path(path);
265 mutex_unlock(&root->fs_info->fs_mutex);
269 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
271 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
272 struct inode *inode = page->mapping->host;
273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
277 struct btrfs_root *root = BTRFS_I(inode)->root;
281 if (btrfs_test_opt(root, NODATASUM))
284 ret = get_state_private(em_tree, start, &private);
285 local_irq_save(flags);
286 kaddr = kmap_atomic(page, KM_IRQ0);
290 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
291 btrfs_csum_final(csum, (char *)&csum);
292 if (csum != private) {
295 kunmap_atomic(kaddr, KM_IRQ0);
296 local_irq_restore(flags);
300 printk("btrfs csum failed ino %lu off %llu\n",
301 page->mapping->host->i_ino, (unsigned long long)start);
302 memset(kaddr + offset, 1, end - start + 1);
303 flush_dcache_page(page);
304 kunmap_atomic(kaddr, KM_IRQ0);
305 local_irq_restore(flags);
309 void btrfs_read_locked_inode(struct inode *inode)
311 struct btrfs_path *path;
312 struct extent_buffer *leaf;
313 struct btrfs_inode_item *inode_item;
314 struct btrfs_inode_timespec *tspec;
315 struct btrfs_root *root = BTRFS_I(inode)->root;
316 struct btrfs_key location;
317 u64 alloc_group_block;
321 path = btrfs_alloc_path();
323 mutex_lock(&root->fs_info->fs_mutex);
325 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
326 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
330 leaf = path->nodes[0];
331 inode_item = btrfs_item_ptr(leaf, path->slots[0],
332 struct btrfs_inode_item);
334 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
335 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
336 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
337 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
338 inode->i_size = btrfs_inode_size(leaf, inode_item);
340 tspec = btrfs_inode_atime(inode_item);
341 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
342 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
344 tspec = btrfs_inode_mtime(inode_item);
345 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
346 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
348 tspec = btrfs_inode_ctime(inode_item);
349 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
350 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
352 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
353 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
355 rdev = btrfs_inode_rdev(leaf, inode_item);
357 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
358 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
361 btrfs_free_path(path);
364 mutex_unlock(&root->fs_info->fs_mutex);
366 switch (inode->i_mode & S_IFMT) {
368 inode->i_mapping->a_ops = &btrfs_aops;
369 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
370 inode->i_fop = &btrfs_file_operations;
371 inode->i_op = &btrfs_file_inode_operations;
374 inode->i_fop = &btrfs_dir_file_operations;
375 if (root == root->fs_info->tree_root)
376 inode->i_op = &btrfs_dir_ro_inode_operations;
378 inode->i_op = &btrfs_dir_inode_operations;
381 inode->i_op = &btrfs_symlink_inode_operations;
382 inode->i_mapping->a_ops = &btrfs_symlink_aops;
385 init_special_inode(inode, inode->i_mode, rdev);
391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
393 mutex_unlock(&root->fs_info->fs_mutex);
394 make_bad_inode(inode);
397 static void fill_inode_item(struct extent_buffer *leaf,
398 struct btrfs_inode_item *item,
401 btrfs_set_inode_uid(leaf, item, inode->i_uid);
402 btrfs_set_inode_gid(leaf, item, inode->i_gid);
403 btrfs_set_inode_size(leaf, item, inode->i_size);
404 btrfs_set_inode_mode(leaf, item, inode->i_mode);
405 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
407 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
408 inode->i_atime.tv_sec);
409 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
410 inode->i_atime.tv_nsec);
412 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
413 inode->i_mtime.tv_sec);
414 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
415 inode->i_mtime.tv_nsec);
417 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
418 inode->i_ctime.tv_sec);
419 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
420 inode->i_ctime.tv_nsec);
422 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
423 btrfs_set_inode_generation(leaf, item, inode->i_generation);
424 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
425 btrfs_set_inode_block_group(leaf, item,
426 BTRFS_I(inode)->block_group->key.objectid);
429 int btrfs_update_inode(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root,
433 struct btrfs_inode_item *inode_item;
434 struct btrfs_path *path;
435 struct extent_buffer *leaf;
438 path = btrfs_alloc_path();
440 ret = btrfs_lookup_inode(trans, root, path,
441 &BTRFS_I(inode)->location, 1);
448 leaf = path->nodes[0];
449 inode_item = btrfs_item_ptr(leaf, path->slots[0],
450 struct btrfs_inode_item);
452 fill_inode_item(leaf, inode_item, inode);
453 btrfs_mark_buffer_dirty(leaf);
454 btrfs_set_inode_last_trans(trans, inode);
457 btrfs_release_path(root, path);
458 btrfs_free_path(path);
463 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
464 struct btrfs_root *root,
466 struct dentry *dentry)
468 struct btrfs_path *path;
469 const char *name = dentry->d_name.name;
470 int name_len = dentry->d_name.len;
472 struct extent_buffer *leaf;
473 struct btrfs_dir_item *di;
474 struct btrfs_key key;
476 path = btrfs_alloc_path();
482 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
492 leaf = path->nodes[0];
493 btrfs_dir_item_key_to_cpu(leaf, di, &key);
494 ret = btrfs_delete_one_dir_name(trans, root, path, di);
497 btrfs_release_path(root, path);
499 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
500 key.objectid, name, name_len, -1);
509 ret = btrfs_delete_one_dir_name(trans, root, path, di);
511 dentry->d_inode->i_ctime = dir->i_ctime;
512 ret = btrfs_del_inode_ref(trans, root, name, name_len,
513 dentry->d_inode->i_ino,
514 dentry->d_parent->d_inode->i_ino);
516 printk("failed to delete reference to %.*s, "
517 "inode %lu parent %lu\n", name_len, name,
518 dentry->d_inode->i_ino,
519 dentry->d_parent->d_inode->i_ino);
522 btrfs_free_path(path);
524 dir->i_size -= name_len * 2;
525 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
526 btrfs_update_inode(trans, root, dir);
527 drop_nlink(dentry->d_inode);
528 ret = btrfs_update_inode(trans, root, dentry->d_inode);
529 dir->i_sb->s_dirt = 1;
534 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
536 struct btrfs_root *root;
537 struct btrfs_trans_handle *trans;
541 root = BTRFS_I(dir)->root;
542 mutex_lock(&root->fs_info->fs_mutex);
543 trans = btrfs_start_transaction(root, 1);
545 btrfs_set_trans_block_group(trans, dir);
546 ret = btrfs_unlink_trans(trans, root, dir, dentry);
547 nr = trans->blocks_used;
549 btrfs_end_transaction(trans, root);
550 mutex_unlock(&root->fs_info->fs_mutex);
551 btrfs_btree_balance_dirty(root, nr);
556 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
558 struct inode *inode = dentry->d_inode;
561 struct btrfs_root *root = BTRFS_I(dir)->root;
562 struct btrfs_trans_handle *trans;
565 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
568 mutex_lock(&root->fs_info->fs_mutex);
569 trans = btrfs_start_transaction(root, 1);
570 btrfs_set_trans_block_group(trans, dir);
572 /* now the directory is empty */
573 err = btrfs_unlink_trans(trans, root, dir, dentry);
578 nr = trans->blocks_used;
579 ret = btrfs_end_transaction(trans, root);
580 mutex_unlock(&root->fs_info->fs_mutex);
581 btrfs_btree_balance_dirty(root, nr);
588 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
589 struct btrfs_root *root,
592 struct btrfs_path *path;
597 path = btrfs_alloc_path();
599 ret = btrfs_lookup_inode(trans, root, path,
600 &BTRFS_I(inode)->location, -1);
604 ret = btrfs_del_item(trans, root, path);
605 btrfs_free_path(path);
610 * this can truncate away extent items, csum items and directory items.
611 * It starts at a high offset and removes keys until it can't find
612 * any higher than i_size.
614 * csum items that cross the new i_size are truncated to the new size
617 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
618 struct btrfs_root *root,
622 struct btrfs_path *path;
623 struct btrfs_key key;
624 struct btrfs_key found_key;
626 struct extent_buffer *leaf;
627 struct btrfs_file_extent_item *fi;
628 u64 extent_start = 0;
629 u64 extent_num_bytes = 0;
635 int extent_type = -1;
637 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
638 path = btrfs_alloc_path();
642 /* FIXME, add redo link to tree so we don't leak on crash */
643 key.objectid = inode->i_ino;
644 key.offset = (u64)-1;
648 btrfs_init_path(path);
650 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
655 BUG_ON(path->slots[0] == 0);
658 leaf = path->nodes[0];
659 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
660 found_type = btrfs_key_type(&found_key);
662 if (found_key.objectid != inode->i_ino)
665 if (found_type != BTRFS_CSUM_ITEM_KEY &&
666 found_type != BTRFS_DIR_ITEM_KEY &&
667 found_type != BTRFS_DIR_INDEX_KEY &&
668 found_type != BTRFS_EXTENT_DATA_KEY)
671 item_end = found_key.offset;
672 if (found_type == BTRFS_EXTENT_DATA_KEY) {
673 fi = btrfs_item_ptr(leaf, path->slots[0],
674 struct btrfs_file_extent_item);
675 extent_type = btrfs_file_extent_type(leaf, fi);
676 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
678 btrfs_file_extent_num_bytes(leaf, fi);
679 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
680 struct btrfs_item *item = btrfs_item_nr(leaf,
682 item_end += btrfs_file_extent_inline_len(leaf,
687 if (found_type == BTRFS_CSUM_ITEM_KEY) {
688 ret = btrfs_csum_truncate(trans, root, path,
692 if (item_end < inode->i_size) {
693 if (found_type == BTRFS_DIR_ITEM_KEY) {
694 found_type = BTRFS_INODE_ITEM_KEY;
695 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
696 found_type = BTRFS_CSUM_ITEM_KEY;
697 } else if (found_type) {
702 btrfs_set_key_type(&key, found_type);
703 btrfs_release_path(root, path);
706 if (found_key.offset >= inode->i_size)
712 /* FIXME, shrink the extent if the ref count is only 1 */
713 if (found_type != BTRFS_EXTENT_DATA_KEY)
716 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
718 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
721 btrfs_file_extent_num_bytes(leaf, fi);
722 extent_num_bytes = inode->i_size -
723 found_key.offset + root->sectorsize - 1;
724 btrfs_set_file_extent_num_bytes(leaf, fi,
726 num_dec = (orig_num_bytes -
727 extent_num_bytes) >> 9;
728 if (extent_start != 0) {
729 inode->i_blocks -= num_dec;
731 btrfs_mark_buffer_dirty(leaf);
734 btrfs_file_extent_disk_num_bytes(leaf,
736 /* FIXME blocksize != 4096 */
737 num_dec = btrfs_file_extent_num_bytes(leaf,
739 if (extent_start != 0) {
741 inode->i_blocks -= num_dec;
743 root_gen = btrfs_header_generation(leaf);
744 root_owner = btrfs_header_owner(leaf);
746 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
748 u32 newsize = inode->i_size - found_key.offset;
749 newsize = btrfs_file_extent_calc_inline_size(newsize);
750 ret = btrfs_truncate_item(trans, root, path,
756 ret = btrfs_del_item(trans, root, path);
762 btrfs_release_path(root, path);
764 ret = btrfs_free_extent(trans, root, extent_start,
767 root_gen, inode->i_ino,
768 found_key.offset, 0);
774 btrfs_release_path(root, path);
775 btrfs_free_path(path);
776 inode->i_sb->s_dirt = 1;
780 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
785 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
786 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
787 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
789 set_page_extent_mapped(page);
791 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
792 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
794 if (zero_start != PAGE_CACHE_SIZE) {
796 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
797 flush_dcache_page(page);
800 set_page_dirty(page);
801 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
807 * taken from block_truncate_page, but does cow as it zeros out
808 * any bytes left in the last page in the file.
810 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
812 struct inode *inode = mapping->host;
813 struct btrfs_root *root = BTRFS_I(inode)->root;
814 u32 blocksize = root->sectorsize;
815 pgoff_t index = from >> PAGE_CACHE_SHIFT;
816 unsigned offset = from & (PAGE_CACHE_SIZE-1);
821 if ((offset & (blocksize - 1)) == 0)
824 down_read(&root->snap_sem);
826 page = grab_cache_page(mapping, index);
829 if (!PageUptodate(page)) {
830 ret = btrfs_readpage(NULL, page);
832 if (!PageUptodate(page)) {
837 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
839 ret = btrfs_cow_one_page(inode, page, offset);
842 page_cache_release(page);
843 up_read(&BTRFS_I(inode)->root->snap_sem);
848 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
850 struct inode *inode = dentry->d_inode;
853 err = inode_change_ok(inode, attr);
857 if (S_ISREG(inode->i_mode) &&
858 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
859 struct btrfs_trans_handle *trans;
860 struct btrfs_root *root = BTRFS_I(inode)->root;
861 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
863 u64 mask = root->sectorsize - 1;
864 u64 pos = (inode->i_size + mask) & ~mask;
865 u64 block_end = attr->ia_size | mask;
869 if (attr->ia_size <= pos)
872 btrfs_truncate_page(inode->i_mapping, inode->i_size);
874 lock_extent(em_tree, pos, block_end, GFP_NOFS);
875 hole_size = (attr->ia_size - pos + mask) & ~mask;
877 mutex_lock(&root->fs_info->fs_mutex);
878 trans = btrfs_start_transaction(root, 1);
879 btrfs_set_trans_block_group(trans, inode);
880 err = btrfs_drop_extents(trans, root, inode,
881 pos, pos + hole_size, pos,
884 if (alloc_hint != EXTENT_MAP_INLINE) {
885 err = btrfs_insert_file_extent(trans, root,
887 pos, 0, 0, hole_size);
889 btrfs_end_transaction(trans, root);
890 mutex_unlock(&root->fs_info->fs_mutex);
891 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
896 err = inode_setattr(inode, attr);
900 void btrfs_delete_inode(struct inode *inode)
902 struct btrfs_trans_handle *trans;
903 struct btrfs_root *root = BTRFS_I(inode)->root;
907 truncate_inode_pages(&inode->i_data, 0);
908 if (is_bad_inode(inode)) {
913 mutex_lock(&root->fs_info->fs_mutex);
914 trans = btrfs_start_transaction(root, 1);
916 btrfs_set_trans_block_group(trans, inode);
917 ret = btrfs_truncate_in_trans(trans, root, inode);
920 ret = btrfs_delete_xattrs(trans, root, inode);
923 ret = btrfs_free_inode(trans, root, inode);
926 nr = trans->blocks_used;
928 btrfs_end_transaction(trans, root);
929 mutex_unlock(&root->fs_info->fs_mutex);
930 btrfs_btree_balance_dirty(root, nr);
934 nr = trans->blocks_used;
935 btrfs_end_transaction(trans, root);
936 mutex_unlock(&root->fs_info->fs_mutex);
937 btrfs_btree_balance_dirty(root, nr);
943 * this returns the key found in the dir entry in the location pointer.
944 * If no dir entries were found, location->objectid is 0.
946 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
947 struct btrfs_key *location)
949 const char *name = dentry->d_name.name;
950 int namelen = dentry->d_name.len;
951 struct btrfs_dir_item *di;
952 struct btrfs_path *path;
953 struct btrfs_root *root = BTRFS_I(dir)->root;
956 if (namelen == 1 && strcmp(name, ".") == 0) {
957 location->objectid = dir->i_ino;
958 location->type = BTRFS_INODE_ITEM_KEY;
959 location->offset = 0;
962 path = btrfs_alloc_path();
965 if (namelen == 2 && strcmp(name, "..") == 0) {
966 struct btrfs_key key;
967 struct extent_buffer *leaf;
971 key.objectid = dir->i_ino;
972 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
978 leaf = path->nodes[0];
979 slot = path->slots[0];
980 nritems = btrfs_header_nritems(leaf);
984 btrfs_item_key_to_cpu(leaf, &key, slot);
985 if (key.objectid != dir->i_ino ||
986 key.type != BTRFS_INODE_REF_KEY) {
989 location->objectid = key.offset;
990 location->type = BTRFS_INODE_ITEM_KEY;
991 location->offset = 0;
995 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
999 if (!di || IS_ERR(di)) {
1002 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1004 btrfs_free_path(path);
1007 location->objectid = 0;
1012 * when we hit a tree root in a directory, the btrfs part of the inode
1013 * needs to be changed to reflect the root directory of the tree root. This
1014 * is kind of like crossing a mount point.
1016 static int fixup_tree_root_location(struct btrfs_root *root,
1017 struct btrfs_key *location,
1018 struct btrfs_root **sub_root,
1019 struct dentry *dentry)
1021 struct btrfs_path *path;
1022 struct btrfs_root_item *ri;
1024 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1026 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1029 path = btrfs_alloc_path();
1031 mutex_lock(&root->fs_info->fs_mutex);
1033 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1034 dentry->d_name.name,
1035 dentry->d_name.len);
1036 if (IS_ERR(*sub_root))
1037 return PTR_ERR(*sub_root);
1039 ri = &(*sub_root)->root_item;
1040 location->objectid = btrfs_root_dirid(ri);
1041 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1042 location->offset = 0;
1044 btrfs_free_path(path);
1045 mutex_unlock(&root->fs_info->fs_mutex);
1049 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1051 struct btrfs_iget_args *args = p;
1052 inode->i_ino = args->ino;
1053 BTRFS_I(inode)->root = args->root;
1054 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1055 inode->i_mapping, GFP_NOFS);
1059 static int btrfs_find_actor(struct inode *inode, void *opaque)
1061 struct btrfs_iget_args *args = opaque;
1062 return (args->ino == inode->i_ino &&
1063 args->root == BTRFS_I(inode)->root);
1066 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1067 struct btrfs_root *root)
1069 struct inode *inode;
1070 struct btrfs_iget_args args;
1071 args.ino = objectid;
1074 inode = iget5_locked(s, objectid, btrfs_find_actor,
1075 btrfs_init_locked_inode,
1080 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1081 struct nameidata *nd)
1083 struct inode * inode;
1084 struct btrfs_inode *bi = BTRFS_I(dir);
1085 struct btrfs_root *root = bi->root;
1086 struct btrfs_root *sub_root = root;
1087 struct btrfs_key location;
1090 if (dentry->d_name.len > BTRFS_NAME_LEN)
1091 return ERR_PTR(-ENAMETOOLONG);
1093 mutex_lock(&root->fs_info->fs_mutex);
1094 ret = btrfs_inode_by_name(dir, dentry, &location);
1095 mutex_unlock(&root->fs_info->fs_mutex);
1098 return ERR_PTR(ret);
1101 if (location.objectid) {
1102 ret = fixup_tree_root_location(root, &location, &sub_root,
1105 return ERR_PTR(ret);
1107 return ERR_PTR(-ENOENT);
1108 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1111 return ERR_PTR(-EACCES);
1112 if (inode->i_state & I_NEW) {
1113 /* the inode and parent dir are two different roots */
1114 if (sub_root != root) {
1116 sub_root->inode = inode;
1118 BTRFS_I(inode)->root = sub_root;
1119 memcpy(&BTRFS_I(inode)->location, &location,
1121 btrfs_read_locked_inode(inode);
1122 unlock_new_inode(inode);
1125 return d_splice_alias(inode, dentry);
1128 static unsigned char btrfs_filetype_table[] = {
1129 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1132 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1134 struct inode *inode = filp->f_path.dentry->d_inode;
1135 struct btrfs_root *root = BTRFS_I(inode)->root;
1136 struct btrfs_item *item;
1137 struct btrfs_dir_item *di;
1138 struct btrfs_key key;
1139 struct btrfs_key found_key;
1140 struct btrfs_path *path;
1143 struct extent_buffer *leaf;
1146 unsigned char d_type;
1151 int key_type = BTRFS_DIR_INDEX_KEY;
1156 /* FIXME, use a real flag for deciding about the key type */
1157 if (root->fs_info->tree_root == root)
1158 key_type = BTRFS_DIR_ITEM_KEY;
1160 /* special case for "." */
1161 if (filp->f_pos == 0) {
1162 over = filldir(dirent, ".", 1,
1170 mutex_lock(&root->fs_info->fs_mutex);
1171 key.objectid = inode->i_ino;
1172 path = btrfs_alloc_path();
1175 /* special case for .., just use the back ref */
1176 if (filp->f_pos == 1) {
1177 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1179 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1181 leaf = path->nodes[0];
1182 slot = path->slots[0];
1183 nritems = btrfs_header_nritems(leaf);
1184 if (slot >= nritems) {
1185 btrfs_release_path(root, path);
1186 goto read_dir_items;
1188 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1189 btrfs_release_path(root, path);
1190 if (found_key.objectid != key.objectid ||
1191 found_key.type != BTRFS_INODE_REF_KEY)
1192 goto read_dir_items;
1193 over = filldir(dirent, "..", 2,
1194 2, found_key.offset, DT_DIR);
1201 btrfs_set_key_type(&key, key_type);
1202 key.offset = filp->f_pos;
1204 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1209 leaf = path->nodes[0];
1210 nritems = btrfs_header_nritems(leaf);
1211 slot = path->slots[0];
1212 if (advance || slot >= nritems) {
1213 if (slot >= nritems -1) {
1214 ret = btrfs_next_leaf(root, path);
1217 leaf = path->nodes[0];
1218 nritems = btrfs_header_nritems(leaf);
1219 slot = path->slots[0];
1226 item = btrfs_item_nr(leaf, slot);
1227 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1229 if (found_key.objectid != key.objectid)
1231 if (btrfs_key_type(&found_key) != key_type)
1233 if (found_key.offset < filp->f_pos)
1236 filp->f_pos = found_key.offset;
1238 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1240 di_total = btrfs_item_size(leaf, item);
1241 while(di_cur < di_total) {
1242 struct btrfs_key location;
1244 name_len = btrfs_dir_name_len(leaf, di);
1245 if (name_len < 32) {
1246 name_ptr = tmp_name;
1248 name_ptr = kmalloc(name_len, GFP_NOFS);
1251 read_extent_buffer(leaf, name_ptr,
1252 (unsigned long)(di + 1), name_len);
1254 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1255 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1257 over = filldir(dirent, name_ptr, name_len,
1262 if (name_ptr != tmp_name)
1267 di_len = btrfs_dir_name_len(leaf, di) +
1268 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1270 di = (struct btrfs_dir_item *)((char *)di + di_len);
1277 btrfs_release_path(root, path);
1278 btrfs_free_path(path);
1279 mutex_unlock(&root->fs_info->fs_mutex);
1283 int btrfs_write_inode(struct inode *inode, int wait)
1285 struct btrfs_root *root = BTRFS_I(inode)->root;
1286 struct btrfs_trans_handle *trans;
1290 mutex_lock(&root->fs_info->fs_mutex);
1291 trans = btrfs_start_transaction(root, 1);
1292 btrfs_set_trans_block_group(trans, inode);
1293 ret = btrfs_commit_transaction(trans, root);
1294 mutex_unlock(&root->fs_info->fs_mutex);
1300 * This is somewhat expensive, updating the tree every time the
1301 * inode changes. But, it is most likely to find the inode in cache.
1302 * FIXME, needs more benchmarking...there are no reasons other than performance
1303 * to keep or drop this code.
1305 void btrfs_dirty_inode(struct inode *inode)
1307 struct btrfs_root *root = BTRFS_I(inode)->root;
1308 struct btrfs_trans_handle *trans;
1310 mutex_lock(&root->fs_info->fs_mutex);
1311 trans = btrfs_start_transaction(root, 1);
1312 btrfs_set_trans_block_group(trans, inode);
1313 btrfs_update_inode(trans, root, inode);
1314 btrfs_end_transaction(trans, root);
1315 mutex_unlock(&root->fs_info->fs_mutex);
1318 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1319 struct btrfs_root *root,
1321 struct btrfs_block_group_cache *group,
1324 struct inode *inode;
1325 struct btrfs_inode_item *inode_item;
1326 struct btrfs_key *location;
1327 struct btrfs_path *path;
1331 path = btrfs_alloc_path();
1334 inode = new_inode(root->fs_info->sb);
1336 return ERR_PTR(-ENOMEM);
1338 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1339 inode->i_mapping, GFP_NOFS);
1340 BTRFS_I(inode)->root = root;
1346 group = btrfs_find_block_group(root, group, 0, 0, owner);
1347 BTRFS_I(inode)->block_group = group;
1349 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1353 inode->i_uid = current->fsuid;
1354 inode->i_gid = current->fsgid;
1355 inode->i_mode = mode;
1356 inode->i_ino = objectid;
1357 inode->i_blocks = 0;
1358 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1359 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1360 struct btrfs_inode_item);
1361 fill_inode_item(path->nodes[0], inode_item, inode);
1362 btrfs_mark_buffer_dirty(path->nodes[0]);
1363 btrfs_free_path(path);
1365 location = &BTRFS_I(inode)->location;
1366 location->objectid = objectid;
1367 location->offset = 0;
1368 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1370 insert_inode_hash(inode);
1373 btrfs_free_path(path);
1374 return ERR_PTR(ret);
1377 static inline u8 btrfs_inode_type(struct inode *inode)
1379 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1382 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1383 struct dentry *dentry, struct inode *inode)
1386 struct btrfs_key key;
1387 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1388 struct inode *parent_inode;
1390 key.objectid = inode->i_ino;
1391 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1394 ret = btrfs_insert_dir_item(trans, root,
1395 dentry->d_name.name, dentry->d_name.len,
1396 dentry->d_parent->d_inode->i_ino,
1397 &key, btrfs_inode_type(inode));
1399 ret = btrfs_insert_inode_ref(trans, root,
1400 dentry->d_name.name,
1403 dentry->d_parent->d_inode->i_ino);
1404 parent_inode = dentry->d_parent->d_inode;
1405 parent_inode->i_size += dentry->d_name.len * 2;
1406 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1407 ret = btrfs_update_inode(trans, root,
1408 dentry->d_parent->d_inode);
1413 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1414 struct dentry *dentry, struct inode *inode)
1416 int err = btrfs_add_link(trans, dentry, inode);
1418 d_instantiate(dentry, inode);
1426 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1427 int mode, dev_t rdev)
1429 struct btrfs_trans_handle *trans;
1430 struct btrfs_root *root = BTRFS_I(dir)->root;
1431 struct inode *inode;
1437 if (!new_valid_dev(rdev))
1440 mutex_lock(&root->fs_info->fs_mutex);
1441 trans = btrfs_start_transaction(root, 1);
1442 btrfs_set_trans_block_group(trans, dir);
1444 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1450 inode = btrfs_new_inode(trans, root, objectid,
1451 BTRFS_I(dir)->block_group, mode);
1452 err = PTR_ERR(inode);
1456 btrfs_set_trans_block_group(trans, inode);
1457 err = btrfs_add_nondir(trans, dentry, inode);
1461 inode->i_op = &btrfs_special_inode_operations;
1462 init_special_inode(inode, inode->i_mode, rdev);
1463 btrfs_update_inode(trans, root, inode);
1465 dir->i_sb->s_dirt = 1;
1466 btrfs_update_inode_block_group(trans, inode);
1467 btrfs_update_inode_block_group(trans, dir);
1469 nr = trans->blocks_used;
1470 btrfs_end_transaction(trans, root);
1471 mutex_unlock(&root->fs_info->fs_mutex);
1474 inode_dec_link_count(inode);
1477 btrfs_btree_balance_dirty(root, nr);
1481 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1482 int mode, struct nameidata *nd)
1484 struct btrfs_trans_handle *trans;
1485 struct btrfs_root *root = BTRFS_I(dir)->root;
1486 struct inode *inode;
1492 mutex_lock(&root->fs_info->fs_mutex);
1493 trans = btrfs_start_transaction(root, 1);
1494 btrfs_set_trans_block_group(trans, dir);
1496 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1502 inode = btrfs_new_inode(trans, root, objectid,
1503 BTRFS_I(dir)->block_group, mode);
1504 err = PTR_ERR(inode);
1508 btrfs_set_trans_block_group(trans, inode);
1509 err = btrfs_add_nondir(trans, dentry, inode);
1513 inode->i_mapping->a_ops = &btrfs_aops;
1514 inode->i_fop = &btrfs_file_operations;
1515 inode->i_op = &btrfs_file_inode_operations;
1516 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1517 inode->i_mapping, GFP_NOFS);
1518 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
1520 dir->i_sb->s_dirt = 1;
1521 btrfs_update_inode_block_group(trans, inode);
1522 btrfs_update_inode_block_group(trans, dir);
1524 nr = trans->blocks_used;
1525 btrfs_end_transaction(trans, root);
1526 mutex_unlock(&root->fs_info->fs_mutex);
1529 inode_dec_link_count(inode);
1532 btrfs_btree_balance_dirty(root, nr);
1536 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1537 struct dentry *dentry)
1539 struct btrfs_trans_handle *trans;
1540 struct btrfs_root *root = BTRFS_I(dir)->root;
1541 struct inode *inode = old_dentry->d_inode;
1546 if (inode->i_nlink == 0)
1550 mutex_lock(&root->fs_info->fs_mutex);
1551 trans = btrfs_start_transaction(root, 1);
1553 btrfs_set_trans_block_group(trans, dir);
1554 atomic_inc(&inode->i_count);
1555 err = btrfs_add_nondir(trans, dentry, inode);
1560 dir->i_sb->s_dirt = 1;
1561 btrfs_update_inode_block_group(trans, dir);
1562 err = btrfs_update_inode(trans, root, inode);
1567 nr = trans->blocks_used;
1568 btrfs_end_transaction(trans, root);
1569 mutex_unlock(&root->fs_info->fs_mutex);
1572 inode_dec_link_count(inode);
1575 btrfs_btree_balance_dirty(root, nr);
1579 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1581 struct inode *inode;
1582 struct btrfs_trans_handle *trans;
1583 struct btrfs_root *root = BTRFS_I(dir)->root;
1585 int drop_on_err = 0;
1587 unsigned long nr = 1;
1589 mutex_lock(&root->fs_info->fs_mutex);
1590 trans = btrfs_start_transaction(root, 1);
1591 btrfs_set_trans_block_group(trans, dir);
1593 if (IS_ERR(trans)) {
1594 err = PTR_ERR(trans);
1598 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1604 inode = btrfs_new_inode(trans, root, objectid,
1605 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1606 if (IS_ERR(inode)) {
1607 err = PTR_ERR(inode);
1612 inode->i_op = &btrfs_dir_inode_operations;
1613 inode->i_fop = &btrfs_dir_file_operations;
1614 btrfs_set_trans_block_group(trans, inode);
1617 err = btrfs_update_inode(trans, root, inode);
1621 err = btrfs_add_link(trans, dentry, inode);
1625 d_instantiate(dentry, inode);
1627 dir->i_sb->s_dirt = 1;
1628 btrfs_update_inode_block_group(trans, inode);
1629 btrfs_update_inode_block_group(trans, dir);
1632 nr = trans->blocks_used;
1633 btrfs_end_transaction(trans, root);
1636 mutex_unlock(&root->fs_info->fs_mutex);
1639 btrfs_btree_balance_dirty(root, nr);
1643 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1644 size_t page_offset, u64 start, u64 end,
1650 u64 extent_start = 0;
1652 u64 objectid = inode->i_ino;
1654 int failed_insert = 0;
1655 struct btrfs_path *path;
1656 struct btrfs_root *root = BTRFS_I(inode)->root;
1657 struct btrfs_file_extent_item *item;
1658 struct extent_buffer *leaf;
1659 struct btrfs_key found_key;
1660 struct extent_map *em = NULL;
1661 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1662 struct btrfs_trans_handle *trans = NULL;
1664 path = btrfs_alloc_path();
1666 mutex_lock(&root->fs_info->fs_mutex);
1669 em = lookup_extent_mapping(em_tree, start, end);
1674 em = alloc_extent_map(GFP_NOFS);
1679 em->start = EXTENT_MAP_HOLE;
1680 em->end = EXTENT_MAP_HOLE;
1682 em->bdev = inode->i_sb->s_bdev;
1683 ret = btrfs_lookup_file_extent(trans, root, path,
1684 objectid, start, trans != NULL);
1691 if (path->slots[0] == 0)
1696 leaf = path->nodes[0];
1697 item = btrfs_item_ptr(leaf, path->slots[0],
1698 struct btrfs_file_extent_item);
1699 /* are we inside the extent that was found? */
1700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1701 found_type = btrfs_key_type(&found_key);
1702 if (found_key.objectid != objectid ||
1703 found_type != BTRFS_EXTENT_DATA_KEY) {
1707 found_type = btrfs_file_extent_type(leaf, item);
1708 extent_start = found_key.offset;
1709 if (found_type == BTRFS_FILE_EXTENT_REG) {
1710 extent_end = extent_start +
1711 btrfs_file_extent_num_bytes(leaf, item);
1713 if (start < extent_start || start >= extent_end) {
1715 if (start < extent_start) {
1716 if (end < extent_start)
1718 em->end = extent_end - 1;
1724 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1726 em->start = extent_start;
1727 em->end = extent_end - 1;
1728 em->block_start = EXTENT_MAP_HOLE;
1729 em->block_end = EXTENT_MAP_HOLE;
1732 bytenr += btrfs_file_extent_offset(leaf, item);
1733 em->block_start = bytenr;
1734 em->block_end = em->block_start +
1735 btrfs_file_extent_num_bytes(leaf, item) - 1;
1736 em->start = extent_start;
1737 em->end = extent_end - 1;
1739 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1743 size_t extent_offset;
1746 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1748 extent_end = (extent_start + size - 1) |
1749 ((u64)root->sectorsize - 1);
1750 if (start < extent_start || start >= extent_end) {
1752 if (start < extent_start) {
1753 if (end < extent_start)
1755 em->end = extent_end;
1761 em->block_start = EXTENT_MAP_INLINE;
1762 em->block_end = EXTENT_MAP_INLINE;
1765 em->start = extent_start;
1766 em->end = extent_start + size - 1;
1770 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
1771 extent_start + page_offset;
1772 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1773 size - extent_offset);
1774 em->start = extent_start + extent_offset;
1775 em->end = (em->start + copy_size -1) |
1776 ((u64)root->sectorsize -1);
1778 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1779 if (create == 0 && !PageUptodate(page)) {
1780 read_extent_buffer(leaf, map + page_offset, ptr,
1782 flush_dcache_page(page);
1783 } else if (create && PageUptodate(page)) {
1786 free_extent_map(em);
1788 btrfs_release_path(root, path);
1789 trans = btrfs_start_transaction(root, 1);
1792 write_extent_buffer(leaf, map + page_offset, ptr,
1794 btrfs_mark_buffer_dirty(leaf);
1797 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
1800 printk("unkknown found_type %d\n", found_type);
1807 em->block_start = EXTENT_MAP_HOLE;
1808 em->block_end = EXTENT_MAP_HOLE;
1810 btrfs_release_path(root, path);
1811 if (em->start > start || em->end < start) {
1812 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
1816 ret = add_extent_mapping(em_tree, em);
1817 if (ret == -EEXIST) {
1818 free_extent_map(em);
1821 if (failed_insert > 5) {
1822 printk("failing to insert %Lu %Lu\n", start, end);
1830 btrfs_free_path(path);
1832 ret = btrfs_end_transaction(trans, root);
1836 mutex_unlock(&root->fs_info->fs_mutex);
1838 free_extent_map(em);
1840 return ERR_PTR(err);
1845 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1847 return extent_bmap(mapping, iblock, btrfs_get_extent);
1850 static int btrfs_prepare_write(struct file *file, struct page *page,
1851 unsigned from, unsigned to)
1853 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1854 page->mapping->host, page, from, to,
1858 int btrfs_readpage(struct file *file, struct page *page)
1860 struct extent_map_tree *tree;
1861 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1862 return extent_read_full_page(tree, page, btrfs_get_extent);
1864 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1866 struct extent_map_tree *tree;
1869 if (current->flags & PF_MEMALLOC) {
1870 redirty_page_for_writepage(wbc, page);
1874 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1875 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1878 static int btrfs_writepages(struct address_space *mapping,
1879 struct writeback_control *wbc)
1881 struct extent_map_tree *tree;
1882 tree = &BTRFS_I(mapping->host)->extent_tree;
1883 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1887 btrfs_readpages(struct file *file, struct address_space *mapping,
1888 struct list_head *pages, unsigned nr_pages)
1890 struct extent_map_tree *tree;
1891 tree = &BTRFS_I(mapping->host)->extent_tree;
1892 return extent_readpages(tree, mapping, pages, nr_pages,
1896 static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1898 struct extent_map_tree *tree;
1901 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1902 ret = try_release_extent_mapping(tree, page);
1904 ClearPagePrivate(page);
1905 set_page_private(page, 0);
1906 page_cache_release(page);
1911 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1913 struct extent_map_tree *tree;
1915 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1916 extent_invalidatepage(tree, page, offset);
1917 btrfs_releasepage(page, GFP_NOFS);
1921 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1922 * called from a page fault handler when a page is first dirtied. Hence we must
1923 * be careful to check for EOF conditions here. We set the page up correctly
1924 * for a written page which means we get ENOSPC checking when writing into
1925 * holes and correct delalloc and unwritten extent mapping on filesystems that
1926 * support these features.
1928 * We are not allowed to take the i_mutex here so we have to play games to
1929 * protect against truncate races as the page could now be beyond EOF. Because
1930 * vmtruncate() writes the inode size before removing pages, once we have the
1931 * page lock we can determine safely if the page is beyond EOF. If it is not
1932 * beyond EOF, then the page is guaranteed safe against truncation until we
1935 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1937 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1943 down_read(&BTRFS_I(inode)->root->snap_sem);
1945 wait_on_page_writeback(page);
1946 size = i_size_read(inode);
1947 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1949 if ((page->mapping != inode->i_mapping) ||
1950 (page_start > size)) {
1951 /* page got truncated out from underneath us */
1955 /* page is wholly or partially inside EOF */
1956 if (page_start + PAGE_CACHE_SIZE > size)
1957 end = size & ~PAGE_CACHE_MASK;
1959 end = PAGE_CACHE_SIZE;
1961 ret = btrfs_cow_one_page(inode, page, end);
1964 up_read(&BTRFS_I(inode)->root->snap_sem);
1969 static void btrfs_truncate(struct inode *inode)
1971 struct btrfs_root *root = BTRFS_I(inode)->root;
1973 struct btrfs_trans_handle *trans;
1976 if (!S_ISREG(inode->i_mode))
1978 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1981 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1983 mutex_lock(&root->fs_info->fs_mutex);
1984 trans = btrfs_start_transaction(root, 1);
1985 btrfs_set_trans_block_group(trans, inode);
1987 /* FIXME, add redo link to tree so we don't leak on crash */
1988 ret = btrfs_truncate_in_trans(trans, root, inode);
1989 btrfs_update_inode(trans, root, inode);
1990 nr = trans->blocks_used;
1992 ret = btrfs_end_transaction(trans, root);
1994 mutex_unlock(&root->fs_info->fs_mutex);
1995 btrfs_btree_balance_dirty(root, nr);
1998 int btrfs_commit_write(struct file *file, struct page *page,
1999 unsigned from, unsigned to)
2001 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2002 struct inode *inode = page->mapping->host;
2004 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2006 if (pos > inode->i_size) {
2007 i_size_write(inode, pos);
2008 mark_inode_dirty(inode);
2013 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2015 struct btrfs_trans_handle *trans;
2016 struct btrfs_key key;
2017 struct btrfs_root_item root_item;
2018 struct btrfs_inode_item *inode_item;
2019 struct extent_buffer *leaf;
2020 struct btrfs_root *new_root;
2021 struct inode *inode;
2026 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2027 unsigned long nr = 1;
2029 mutex_lock(&root->fs_info->fs_mutex);
2030 trans = btrfs_start_transaction(root, 1);
2033 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2038 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2039 objectid, trans->transid, 0, 0,
2042 return PTR_ERR(leaf);
2044 btrfs_set_header_nritems(leaf, 0);
2045 btrfs_set_header_level(leaf, 0);
2046 btrfs_set_header_bytenr(leaf, leaf->start);
2047 btrfs_set_header_generation(leaf, trans->transid);
2048 btrfs_set_header_owner(leaf, objectid);
2050 write_extent_buffer(leaf, root->fs_info->fsid,
2051 (unsigned long)btrfs_header_fsid(leaf),
2053 btrfs_mark_buffer_dirty(leaf);
2055 inode_item = &root_item.inode;
2056 memset(inode_item, 0, sizeof(*inode_item));
2057 inode_item->generation = cpu_to_le64(1);
2058 inode_item->size = cpu_to_le64(3);
2059 inode_item->nlink = cpu_to_le32(1);
2060 inode_item->nblocks = cpu_to_le64(1);
2061 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2063 btrfs_set_root_bytenr(&root_item, leaf->start);
2064 btrfs_set_root_level(&root_item, 0);
2065 btrfs_set_root_refs(&root_item, 1);
2066 btrfs_set_root_used(&root_item, 0);
2068 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2069 root_item.drop_level = 0;
2071 free_extent_buffer(leaf);
2074 btrfs_set_root_dirid(&root_item, new_dirid);
2076 key.objectid = objectid;
2078 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2079 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2085 * insert the directory item
2087 key.offset = (u64)-1;
2088 dir = root->fs_info->sb->s_root->d_inode;
2089 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2090 name, namelen, dir->i_ino, &key,
2095 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2096 name, namelen, objectid,
2097 root->fs_info->sb->s_root->d_inode->i_ino);
2101 ret = btrfs_commit_transaction(trans, root);
2105 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2108 trans = btrfs_start_transaction(new_root, 1);
2111 inode = btrfs_new_inode(trans, new_root, new_dirid,
2112 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2115 inode->i_op = &btrfs_dir_inode_operations;
2116 inode->i_fop = &btrfs_dir_file_operations;
2117 new_root->inode = inode;
2119 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2123 ret = btrfs_update_inode(trans, new_root, inode);
2127 nr = trans->blocks_used;
2128 err = btrfs_commit_transaction(trans, root);
2132 mutex_unlock(&root->fs_info->fs_mutex);
2133 btrfs_btree_balance_dirty(root, nr);
2137 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2139 struct btrfs_trans_handle *trans;
2140 struct btrfs_key key;
2141 struct btrfs_root_item new_root_item;
2142 struct extent_buffer *tmp;
2148 if (!root->ref_cows)
2151 down_write(&root->snap_sem);
2152 freeze_bdev(root->fs_info->sb->s_bdev);
2153 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2155 mutex_lock(&root->fs_info->fs_mutex);
2156 trans = btrfs_start_transaction(root, 1);
2159 ret = btrfs_update_inode(trans, root, root->inode);
2163 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2168 memcpy(&new_root_item, &root->root_item,
2169 sizeof(new_root_item));
2171 key.objectid = objectid;
2173 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2175 extent_buffer_get(root->node);
2176 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
2177 free_extent_buffer(tmp);
2179 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2181 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2182 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
2183 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2185 free_extent_buffer(tmp);
2190 * insert the directory item
2192 key.offset = (u64)-1;
2193 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2195 root->fs_info->sb->s_root->d_inode->i_ino,
2196 &key, BTRFS_FT_DIR);
2201 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2202 name, namelen, objectid,
2203 root->fs_info->sb->s_root->d_inode->i_ino);
2208 nr = trans->blocks_used;
2209 err = btrfs_commit_transaction(trans, root);
2214 mutex_unlock(&root->fs_info->fs_mutex);
2215 up_write(&root->snap_sem);
2216 btrfs_btree_balance_dirty(root, nr);
2220 static unsigned long force_ra(struct address_space *mapping,
2221 struct file_ra_state *ra, struct file *file,
2222 pgoff_t offset, pgoff_t last_index)
2226 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2227 req_size = last_index - offset + 1;
2228 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2231 req_size = min(last_index - offset + 1, (pgoff_t)128);
2232 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2233 return offset + req_size;
2237 int btrfs_defrag_file(struct file *file) {
2238 struct inode *inode = file->f_path.dentry->d_inode;
2239 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2241 unsigned long last_index;
2242 unsigned long ra_index = 0;
2247 mutex_lock(&inode->i_mutex);
2248 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2249 for (i = 0; i <= last_index; i++) {
2250 if (i == ra_index) {
2251 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2252 file, ra_index, last_index);
2254 page = grab_cache_page(inode->i_mapping, i);
2257 if (!PageUptodate(page)) {
2258 btrfs_readpage(NULL, page);
2260 if (!PageUptodate(page)) {
2262 page_cache_release(page);
2266 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2267 page_end = page_start + PAGE_CACHE_SIZE - 1;
2269 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2270 set_extent_delalloc(em_tree, page_start,
2271 page_end, GFP_NOFS);
2272 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2273 set_page_dirty(page);
2275 page_cache_release(page);
2276 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2280 mutex_unlock(&inode->i_mutex);
2284 static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2286 struct btrfs_ioctl_vol_args vol_args;
2287 struct btrfs_dir_item *di;
2288 struct btrfs_path *path;
2292 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2295 namelen = strlen(vol_args.name);
2296 if (namelen > BTRFS_VOL_NAME_MAX)
2298 if (strchr(vol_args.name, '/'))
2301 path = btrfs_alloc_path();
2305 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2306 mutex_lock(&root->fs_info->fs_mutex);
2307 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2309 vol_args.name, namelen, 0);
2310 mutex_unlock(&root->fs_info->fs_mutex);
2311 btrfs_free_path(path);
2312 if (di && !IS_ERR(di))
2317 if (root == root->fs_info->tree_root)
2318 return create_subvol(root, vol_args.name, namelen);
2319 return create_snapshot(root, vol_args.name, namelen);
2322 static int btrfs_ioctl_defrag(struct file *file)
2324 struct inode *inode = file->f_path.dentry->d_inode;
2325 struct btrfs_root *root = BTRFS_I(inode)->root;
2327 switch (inode->i_mode & S_IFMT) {
2329 mutex_lock(&root->fs_info->fs_mutex);
2330 btrfs_defrag_root(root, 0);
2331 btrfs_defrag_root(root->fs_info->extent_root, 0);
2332 mutex_unlock(&root->fs_info->fs_mutex);
2335 btrfs_defrag_file(file);
2342 long btrfs_ioctl(struct file *file, unsigned int
2343 cmd, unsigned long arg)
2345 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
2348 case BTRFS_IOC_SNAP_CREATE:
2349 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2350 case BTRFS_IOC_DEFRAG:
2351 return btrfs_ioctl_defrag(file);
2358 * Called inside transaction, so use GFP_NOFS
2360 struct inode *btrfs_alloc_inode(struct super_block *sb)
2362 struct btrfs_inode *ei;
2364 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2368 return &ei->vfs_inode;
2371 void btrfs_destroy_inode(struct inode *inode)
2373 WARN_ON(!list_empty(&inode->i_dentry));
2374 WARN_ON(inode->i_data.nrpages);
2376 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2379 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2380 static void init_once(struct kmem_cache * cachep, void *foo)
2382 static void init_once(void * foo, struct kmem_cache * cachep,
2383 unsigned long flags)
2386 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2388 inode_init_once(&ei->vfs_inode);
2391 void btrfs_destroy_cachep(void)
2393 if (btrfs_inode_cachep)
2394 kmem_cache_destroy(btrfs_inode_cachep);
2395 if (btrfs_trans_handle_cachep)
2396 kmem_cache_destroy(btrfs_trans_handle_cachep);
2397 if (btrfs_transaction_cachep)
2398 kmem_cache_destroy(btrfs_transaction_cachep);
2399 if (btrfs_bit_radix_cachep)
2400 kmem_cache_destroy(btrfs_bit_radix_cachep);
2401 if (btrfs_path_cachep)
2402 kmem_cache_destroy(btrfs_path_cachep);
2405 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2406 unsigned long extra_flags,
2407 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2408 void (*ctor)(struct kmem_cache *, void *)
2410 void (*ctor)(void *, struct kmem_cache *,
2415 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2416 SLAB_MEM_SPREAD | extra_flags), ctor
2417 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2423 int btrfs_init_cachep(void)
2425 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2426 sizeof(struct btrfs_inode),
2428 if (!btrfs_inode_cachep)
2430 btrfs_trans_handle_cachep =
2431 btrfs_cache_create("btrfs_trans_handle_cache",
2432 sizeof(struct btrfs_trans_handle),
2434 if (!btrfs_trans_handle_cachep)
2436 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2437 sizeof(struct btrfs_transaction),
2439 if (!btrfs_transaction_cachep)
2441 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2442 sizeof(struct btrfs_path),
2444 if (!btrfs_path_cachep)
2446 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2447 SLAB_DESTROY_BY_RCU, NULL);
2448 if (!btrfs_bit_radix_cachep)
2452 btrfs_destroy_cachep();
2456 static int btrfs_getattr(struct vfsmount *mnt,
2457 struct dentry *dentry, struct kstat *stat)
2459 struct inode *inode = dentry->d_inode;
2460 generic_fillattr(inode, stat);
2461 stat->blksize = 256 * 1024;
2465 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2466 struct inode * new_dir,struct dentry *new_dentry)
2468 struct btrfs_trans_handle *trans;
2469 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2470 struct inode *new_inode = new_dentry->d_inode;
2471 struct inode *old_inode = old_dentry->d_inode;
2472 struct timespec ctime = CURRENT_TIME;
2473 struct btrfs_path *path;
2476 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2477 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2481 mutex_lock(&root->fs_info->fs_mutex);
2482 trans = btrfs_start_transaction(root, 1);
2484 btrfs_set_trans_block_group(trans, new_dir);
2485 path = btrfs_alloc_path();
2491 old_dentry->d_inode->i_nlink++;
2492 old_dir->i_ctime = old_dir->i_mtime = ctime;
2493 new_dir->i_ctime = new_dir->i_mtime = ctime;
2494 old_inode->i_ctime = ctime;
2496 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2501 new_inode->i_ctime = CURRENT_TIME;
2502 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2506 ret = btrfs_add_link(trans, new_dentry, old_inode);
2511 btrfs_free_path(path);
2512 btrfs_end_transaction(trans, root);
2513 mutex_unlock(&root->fs_info->fs_mutex);
2517 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2518 const char *symname)
2520 struct btrfs_trans_handle *trans;
2521 struct btrfs_root *root = BTRFS_I(dir)->root;
2522 struct btrfs_path *path;
2523 struct btrfs_key key;
2524 struct inode *inode;
2531 struct btrfs_file_extent_item *ei;
2532 struct extent_buffer *leaf;
2535 name_len = strlen(symname) + 1;
2536 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2537 return -ENAMETOOLONG;
2538 mutex_lock(&root->fs_info->fs_mutex);
2539 trans = btrfs_start_transaction(root, 1);
2540 btrfs_set_trans_block_group(trans, dir);
2542 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2548 inode = btrfs_new_inode(trans, root, objectid,
2549 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2550 err = PTR_ERR(inode);
2554 btrfs_set_trans_block_group(trans, inode);
2555 err = btrfs_add_nondir(trans, dentry, inode);
2559 inode->i_mapping->a_ops = &btrfs_aops;
2560 inode->i_fop = &btrfs_file_operations;
2561 inode->i_op = &btrfs_file_inode_operations;
2562 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2563 inode->i_mapping, GFP_NOFS);
2564 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
2566 dir->i_sb->s_dirt = 1;
2567 btrfs_update_inode_block_group(trans, inode);
2568 btrfs_update_inode_block_group(trans, dir);
2572 path = btrfs_alloc_path();
2574 key.objectid = inode->i_ino;
2576 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2577 datasize = btrfs_file_extent_calc_inline_size(name_len);
2578 err = btrfs_insert_empty_item(trans, root, path, &key,
2584 leaf = path->nodes[0];
2585 ei = btrfs_item_ptr(leaf, path->slots[0],
2586 struct btrfs_file_extent_item);
2587 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2588 btrfs_set_file_extent_type(leaf, ei,
2589 BTRFS_FILE_EXTENT_INLINE);
2590 ptr = btrfs_file_extent_inline_start(ei);
2591 write_extent_buffer(leaf, symname, ptr, name_len);
2592 btrfs_mark_buffer_dirty(leaf);
2593 btrfs_free_path(path);
2595 inode->i_op = &btrfs_symlink_inode_operations;
2596 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2597 inode->i_size = name_len - 1;
2598 err = btrfs_update_inode(trans, root, inode);
2603 nr = trans->blocks_used;
2604 btrfs_end_transaction(trans, root);
2605 mutex_unlock(&root->fs_info->fs_mutex);
2607 inode_dec_link_count(inode);
2610 btrfs_btree_balance_dirty(root, nr);
2614 static struct inode_operations btrfs_dir_inode_operations = {
2615 .lookup = btrfs_lookup,
2616 .create = btrfs_create,
2617 .unlink = btrfs_unlink,
2619 .mkdir = btrfs_mkdir,
2620 .rmdir = btrfs_rmdir,
2621 .rename = btrfs_rename,
2622 .symlink = btrfs_symlink,
2623 .setattr = btrfs_setattr,
2624 .mknod = btrfs_mknod,
2625 .setxattr = generic_setxattr,
2626 .getxattr = generic_getxattr,
2627 .listxattr = btrfs_listxattr,
2628 .removexattr = generic_removexattr,
2631 static struct inode_operations btrfs_dir_ro_inode_operations = {
2632 .lookup = btrfs_lookup,
2635 static struct file_operations btrfs_dir_file_operations = {
2636 .llseek = generic_file_llseek,
2637 .read = generic_read_dir,
2638 .readdir = btrfs_readdir,
2639 .unlocked_ioctl = btrfs_ioctl,
2640 #ifdef CONFIG_COMPAT
2641 .compat_ioctl = btrfs_ioctl,
2645 static struct extent_map_ops btrfs_extent_map_ops = {
2646 .fill_delalloc = run_delalloc_range,
2647 .writepage_io_hook = btrfs_writepage_io_hook,
2648 .readpage_io_hook = btrfs_readpage_io_hook,
2649 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2652 static struct address_space_operations btrfs_aops = {
2653 .readpage = btrfs_readpage,
2654 .writepage = btrfs_writepage,
2655 .writepages = btrfs_writepages,
2656 .readpages = btrfs_readpages,
2657 .sync_page = block_sync_page,
2658 .prepare_write = btrfs_prepare_write,
2659 .commit_write = btrfs_commit_write,
2661 .invalidatepage = btrfs_invalidatepage,
2662 .releasepage = btrfs_releasepage,
2663 .set_page_dirty = __set_page_dirty_nobuffers,
2666 static struct address_space_operations btrfs_symlink_aops = {
2667 .readpage = btrfs_readpage,
2668 .writepage = btrfs_writepage,
2669 .invalidatepage = btrfs_invalidatepage,
2670 .releasepage = btrfs_releasepage,
2673 static struct inode_operations btrfs_file_inode_operations = {
2674 .truncate = btrfs_truncate,
2675 .getattr = btrfs_getattr,
2676 .setattr = btrfs_setattr,
2677 .setxattr = generic_setxattr,
2678 .getxattr = generic_getxattr,
2679 .listxattr = btrfs_listxattr,
2680 .removexattr = generic_removexattr,
2683 static struct inode_operations btrfs_special_inode_operations = {
2684 .getattr = btrfs_getattr,
2685 .setattr = btrfs_setattr,
2688 static struct inode_operations btrfs_symlink_inode_operations = {
2689 .readlink = generic_readlink,
2690 .follow_link = page_follow_link_light,
2691 .put_link = page_put_link,