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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
23 #include <linux/pagemap.h>
24 #include <linux/highmem.h>
25 #include <linux/time.h>
26 #include <linux/init.h>
27 #include <linux/string.h>
28 #include <linux/smp_lock.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/version.h>
37 #include <linux/xattr.h>
40 #include "transaction.h"
41 #include "btrfs_inode.h"
43 #include "print-tree.h"
46 struct btrfs_iget_args {
48 struct btrfs_root *root;
51 static struct inode_operations btrfs_dir_inode_operations;
52 static struct inode_operations btrfs_symlink_inode_operations;
53 static struct inode_operations btrfs_dir_ro_inode_operations;
54 static struct inode_operations btrfs_special_inode_operations;
55 static struct inode_operations btrfs_file_inode_operations;
56 static struct address_space_operations btrfs_aops;
57 static struct address_space_operations btrfs_symlink_aops;
58 static struct file_operations btrfs_dir_file_operations;
59 static struct extent_io_ops btrfs_extent_io_ops;
61 static struct kmem_cache *btrfs_inode_cachep;
62 struct kmem_cache *btrfs_trans_handle_cachep;
63 struct kmem_cache *btrfs_transaction_cachep;
64 struct kmem_cache *btrfs_bit_radix_cachep;
65 struct kmem_cache *btrfs_path_cachep;
68 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
69 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
70 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
71 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
72 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
73 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
74 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
75 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
78 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
81 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
82 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
94 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
95 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
97 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
101 static int cow_file_range(struct inode *inode, u64 start, u64 end)
103 struct btrfs_root *root = BTRFS_I(inode)->root;
104 struct btrfs_trans_handle *trans;
108 u64 blocksize = root->sectorsize;
109 u64 orig_start = start;
111 struct btrfs_key ins;
114 trans = btrfs_start_transaction(root, 1);
116 btrfs_set_trans_block_group(trans, inode);
118 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
119 num_bytes = max(blocksize, num_bytes);
120 ret = btrfs_drop_extents(trans, root, inode,
121 start, start + num_bytes, start, &alloc_hint);
122 orig_num_bytes = num_bytes;
124 if (alloc_hint == EXTENT_MAP_INLINE)
127 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
129 while(num_bytes > 0) {
130 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
131 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
133 root->root_key.objectid,
135 inode->i_ino, start, 0,
136 alloc_hint, (u64)-1, &ins, 1);
141 cur_alloc_size = ins.offset;
142 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
143 start, ins.objectid, ins.offset,
145 inode->i_blocks += ins.offset >> 9;
146 btrfs_check_file(root, inode);
147 if (num_bytes < cur_alloc_size) {
148 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
152 num_bytes -= cur_alloc_size;
153 alloc_hint = ins.objectid + ins.offset;
154 start += cur_alloc_size;
156 btrfs_drop_extent_cache(inode, orig_start,
157 orig_start + orig_num_bytes - 1);
158 btrfs_add_ordered_inode(inode);
159 btrfs_update_inode(trans, root, inode);
161 btrfs_end_transaction(trans, root);
165 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
173 struct btrfs_root *root = BTRFS_I(inode)->root;
174 struct extent_buffer *leaf;
176 struct btrfs_path *path;
177 struct btrfs_file_extent_item *item;
180 struct btrfs_key found_key;
182 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
183 path = btrfs_alloc_path();
186 ret = btrfs_lookup_file_extent(NULL, root, path,
187 inode->i_ino, start, 0);
189 btrfs_free_path(path);
195 if (path->slots[0] == 0)
200 leaf = path->nodes[0];
201 item = btrfs_item_ptr(leaf, path->slots[0],
202 struct btrfs_file_extent_item);
204 /* are we inside the extent that was found? */
205 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
206 found_type = btrfs_key_type(&found_key);
207 if (found_key.objectid != inode->i_ino ||
208 found_type != BTRFS_EXTENT_DATA_KEY) {
212 found_type = btrfs_file_extent_type(leaf, item);
213 extent_start = found_key.offset;
214 if (found_type == BTRFS_FILE_EXTENT_REG) {
215 u64 extent_num_bytes;
217 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
218 extent_end = extent_start + extent_num_bytes;
221 if (loops && start != extent_start)
224 if (start < extent_start || start >= extent_end)
227 cow_end = min(end, extent_end - 1);
228 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
233 * we may be called by the resizer, make sure we're inside
234 * the limits of the FS
236 if (bytenr + extent_num_bytes > total_fs_bytes)
239 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
249 btrfs_free_path(path);
252 btrfs_release_path(root, path);
257 cow_file_range(inode, start, cow_end);
262 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
264 struct btrfs_root *root = BTRFS_I(inode)->root;
266 mutex_lock(&root->fs_info->fs_mutex);
267 if (btrfs_test_opt(root, NODATACOW) ||
268 btrfs_test_flag(inode, NODATACOW))
269 ret = run_delalloc_nocow(inode, start, end);
271 ret = cow_file_range(inode, start, end);
273 mutex_unlock(&root->fs_info->fs_mutex);
277 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
278 unsigned long old, unsigned long bits)
281 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
282 struct btrfs_root *root = BTRFS_I(inode)->root;
283 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
284 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
285 root->fs_info->delalloc_bytes += end - start + 1;
286 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
291 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
292 unsigned long old, unsigned long bits)
294 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
295 struct btrfs_root *root = BTRFS_I(inode)->root;
298 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
299 if (end - start + 1 > root->fs_info->delalloc_bytes) {
300 printk("warning: delalloc account %Lu %Lu\n",
301 end - start + 1, root->fs_info->delalloc_bytes);
302 root->fs_info->delalloc_bytes = 0;
303 BTRFS_I(inode)->delalloc_bytes = 0;
305 root->fs_info->delalloc_bytes -= end - start + 1;
306 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
308 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
313 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
314 size_t size, struct bio *bio)
316 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
317 struct btrfs_mapping_tree *map_tree;
318 u64 logical = bio->bi_sector << 9;
323 length = bio->bi_size;
324 map_tree = &root->fs_info->mapping_tree;
326 ret = btrfs_map_block(map_tree, READ, logical,
327 &map_length, NULL, 0);
329 if (map_length < length + size) {
335 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 struct btrfs_trans_handle *trans;
343 ret = btrfs_csum_one_bio(root, bio, &sums);
346 mutex_lock(&root->fs_info->fs_mutex);
347 trans = btrfs_start_transaction(root, 1);
349 btrfs_set_trans_block_group(trans, inode);
350 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
352 ret = btrfs_end_transaction(trans, root);
354 mutex_unlock(&root->fs_info->fs_mutex);
358 return btrfs_map_bio(root, rw, bio, mirror_num);
361 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
364 struct btrfs_root *root = BTRFS_I(inode)->root;
367 if (!(rw & (1 << BIO_RW))) {
368 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
373 if (btrfs_test_opt(root, NODATASUM) ||
374 btrfs_test_flag(inode, NODATASUM)) {
378 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
379 inode, rw, bio, mirror_num,
380 __btrfs_submit_bio_hook);
382 return btrfs_map_bio(root, rw, bio, mirror_num);
385 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
388 struct inode *inode = page->mapping->host;
389 struct btrfs_root *root = BTRFS_I(inode)->root;
390 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
391 struct btrfs_csum_item *item;
392 struct btrfs_path *path = NULL;
395 if (btrfs_test_opt(root, NODATASUM) ||
396 btrfs_test_flag(inode, NODATASUM))
399 mutex_lock(&root->fs_info->fs_mutex);
400 path = btrfs_alloc_path();
401 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
404 /* a csum that isn't present is a preallocated region. */
405 if (ret == -ENOENT || ret == -EFBIG)
408 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
411 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
413 set_state_private(io_tree, start, csum);
416 btrfs_free_path(path);
417 mutex_unlock(&root->fs_info->fs_mutex);
421 struct io_failure_record {
429 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
430 struct page *page, u64 start, u64 end,
431 struct extent_state *state)
433 struct io_failure_record *failrec = NULL;
435 struct extent_map *em;
436 struct inode *inode = page->mapping->host;
437 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
438 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
444 ret = get_state_private(failure_tree, start, &private);
446 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
449 failrec->start = start;
450 failrec->len = end - start + 1;
451 failrec->last_mirror = 0;
453 spin_lock(&em_tree->lock);
454 em = lookup_extent_mapping(em_tree, start, failrec->len);
455 if (em->start > start || em->start + em->len < start) {
459 spin_unlock(&em_tree->lock);
461 if (!em || IS_ERR(em)) {
465 logical = start - em->start;
466 logical = em->block_start + logical;
467 failrec->logical = logical;
469 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
470 EXTENT_DIRTY, GFP_NOFS);
471 set_state_private(failure_tree, start,
472 (u64)(unsigned long)failrec);
474 failrec = (struct io_failure_record *)(unsigned long)private;
476 num_copies = btrfs_num_copies(
477 &BTRFS_I(inode)->root->fs_info->mapping_tree,
478 failrec->logical, failrec->len);
479 failrec->last_mirror++;
481 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
482 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
485 if (state && state->start != failrec->start)
487 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
489 if (!state || failrec->last_mirror > num_copies) {
490 set_state_private(failure_tree, failrec->start, 0);
491 clear_extent_bits(failure_tree, failrec->start,
492 failrec->start + failrec->len - 1,
493 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
497 bio = bio_alloc(GFP_NOFS, 1);
498 bio->bi_private = state;
499 bio->bi_end_io = failed_bio->bi_end_io;
500 bio->bi_sector = failrec->logical >> 9;
501 bio->bi_bdev = failed_bio->bi_bdev;
503 bio_add_page(bio, page, failrec->len, start - page_offset(page));
504 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
508 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
509 struct extent_state *state)
511 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
512 struct inode *inode = page->mapping->host;
513 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
515 u64 private = ~(u32)0;
517 struct btrfs_root *root = BTRFS_I(inode)->root;
521 if (btrfs_test_opt(root, NODATASUM) ||
522 btrfs_test_flag(inode, NODATASUM))
524 if (state && state->start == start) {
525 private = state->private;
528 ret = get_state_private(io_tree, start, &private);
530 local_irq_save(flags);
531 kaddr = kmap_atomic(page, KM_IRQ0);
535 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
536 btrfs_csum_final(csum, (char *)&csum);
537 if (csum != private) {
540 kunmap_atomic(kaddr, KM_IRQ0);
541 local_irq_restore(flags);
543 /* if the io failure tree for this inode is non-empty,
544 * check to see if we've recovered from a failed IO
547 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
548 (u64)-1, 1, EXTENT_DIRTY)) {
550 struct io_failure_record *failure;
551 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
552 start, &private_failure);
554 failure = (struct io_failure_record *)(unsigned long)
556 set_state_private(&BTRFS_I(inode)->io_failure_tree,
558 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
560 failure->start + failure->len - 1,
561 EXTENT_DIRTY | EXTENT_LOCKED,
569 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
570 page->mapping->host->i_ino, (unsigned long long)start, csum,
572 memset(kaddr + offset, 1, end - start + 1);
573 flush_dcache_page(page);
574 kunmap_atomic(kaddr, KM_IRQ0);
575 local_irq_restore(flags);
581 void btrfs_read_locked_inode(struct inode *inode)
583 struct btrfs_path *path;
584 struct extent_buffer *leaf;
585 struct btrfs_inode_item *inode_item;
586 struct btrfs_timespec *tspec;
587 struct btrfs_root *root = BTRFS_I(inode)->root;
588 struct btrfs_key location;
589 u64 alloc_group_block;
593 path = btrfs_alloc_path();
595 mutex_lock(&root->fs_info->fs_mutex);
596 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
598 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
602 leaf = path->nodes[0];
603 inode_item = btrfs_item_ptr(leaf, path->slots[0],
604 struct btrfs_inode_item);
606 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
607 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
608 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
609 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
610 inode->i_size = btrfs_inode_size(leaf, inode_item);
612 tspec = btrfs_inode_atime(inode_item);
613 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
614 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
616 tspec = btrfs_inode_mtime(inode_item);
617 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
618 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
620 tspec = btrfs_inode_ctime(inode_item);
621 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
622 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
624 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
625 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
627 rdev = btrfs_inode_rdev(leaf, inode_item);
629 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
630 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
632 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
633 if (!BTRFS_I(inode)->block_group) {
634 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
636 BTRFS_BLOCK_GROUP_METADATA, 0);
638 btrfs_free_path(path);
641 mutex_unlock(&root->fs_info->fs_mutex);
643 switch (inode->i_mode & S_IFMT) {
645 inode->i_mapping->a_ops = &btrfs_aops;
646 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
647 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
648 inode->i_fop = &btrfs_file_operations;
649 inode->i_op = &btrfs_file_inode_operations;
652 inode->i_fop = &btrfs_dir_file_operations;
653 if (root == root->fs_info->tree_root)
654 inode->i_op = &btrfs_dir_ro_inode_operations;
656 inode->i_op = &btrfs_dir_inode_operations;
659 inode->i_op = &btrfs_symlink_inode_operations;
660 inode->i_mapping->a_ops = &btrfs_symlink_aops;
661 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
664 init_special_inode(inode, inode->i_mode, rdev);
670 btrfs_release_path(root, path);
671 btrfs_free_path(path);
672 mutex_unlock(&root->fs_info->fs_mutex);
673 make_bad_inode(inode);
676 static void fill_inode_item(struct extent_buffer *leaf,
677 struct btrfs_inode_item *item,
680 btrfs_set_inode_uid(leaf, item, inode->i_uid);
681 btrfs_set_inode_gid(leaf, item, inode->i_gid);
682 btrfs_set_inode_size(leaf, item, inode->i_size);
683 btrfs_set_inode_mode(leaf, item, inode->i_mode);
684 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
686 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
687 inode->i_atime.tv_sec);
688 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
689 inode->i_atime.tv_nsec);
691 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
692 inode->i_mtime.tv_sec);
693 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
694 inode->i_mtime.tv_nsec);
696 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
697 inode->i_ctime.tv_sec);
698 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
699 inode->i_ctime.tv_nsec);
701 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
702 btrfs_set_inode_generation(leaf, item, inode->i_generation);
703 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
704 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
705 btrfs_set_inode_block_group(leaf, item,
706 BTRFS_I(inode)->block_group->key.objectid);
709 int btrfs_update_inode(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root,
713 struct btrfs_inode_item *inode_item;
714 struct btrfs_path *path;
715 struct extent_buffer *leaf;
718 path = btrfs_alloc_path();
720 ret = btrfs_lookup_inode(trans, root, path,
721 &BTRFS_I(inode)->location, 1);
728 leaf = path->nodes[0];
729 inode_item = btrfs_item_ptr(leaf, path->slots[0],
730 struct btrfs_inode_item);
732 fill_inode_item(leaf, inode_item, inode);
733 btrfs_mark_buffer_dirty(leaf);
734 btrfs_set_inode_last_trans(trans, inode);
737 btrfs_release_path(root, path);
738 btrfs_free_path(path);
743 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
744 struct btrfs_root *root,
746 struct dentry *dentry)
748 struct btrfs_path *path;
749 const char *name = dentry->d_name.name;
750 int name_len = dentry->d_name.len;
752 struct extent_buffer *leaf;
753 struct btrfs_dir_item *di;
754 struct btrfs_key key;
756 path = btrfs_alloc_path();
762 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
772 leaf = path->nodes[0];
773 btrfs_dir_item_key_to_cpu(leaf, di, &key);
774 ret = btrfs_delete_one_dir_name(trans, root, path, di);
777 btrfs_release_path(root, path);
779 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
780 key.objectid, name, name_len, -1);
789 ret = btrfs_delete_one_dir_name(trans, root, path, di);
791 dentry->d_inode->i_ctime = dir->i_ctime;
792 ret = btrfs_del_inode_ref(trans, root, name, name_len,
793 dentry->d_inode->i_ino,
794 dentry->d_parent->d_inode->i_ino);
796 printk("failed to delete reference to %.*s, "
797 "inode %lu parent %lu\n", name_len, name,
798 dentry->d_inode->i_ino,
799 dentry->d_parent->d_inode->i_ino);
802 btrfs_free_path(path);
804 dir->i_size -= name_len * 2;
805 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
806 btrfs_update_inode(trans, root, dir);
807 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
808 dentry->d_inode->i_nlink--;
810 drop_nlink(dentry->d_inode);
812 ret = btrfs_update_inode(trans, root, dentry->d_inode);
813 dir->i_sb->s_dirt = 1;
818 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
820 struct btrfs_root *root;
821 struct btrfs_trans_handle *trans;
822 struct inode *inode = dentry->d_inode;
824 unsigned long nr = 0;
826 root = BTRFS_I(dir)->root;
827 mutex_lock(&root->fs_info->fs_mutex);
829 ret = btrfs_check_free_space(root, 1, 1);
833 trans = btrfs_start_transaction(root, 1);
835 btrfs_set_trans_block_group(trans, dir);
836 ret = btrfs_unlink_trans(trans, root, dir, dentry);
837 nr = trans->blocks_used;
839 if (inode->i_nlink == 0) {
841 /* if the inode isn't linked anywhere,
842 * we don't need to worry about
845 found = btrfs_del_ordered_inode(inode);
847 atomic_dec(&inode->i_count);
851 btrfs_end_transaction(trans, root);
853 mutex_unlock(&root->fs_info->fs_mutex);
854 btrfs_btree_balance_dirty(root, nr);
855 btrfs_throttle(root);
859 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
861 struct inode *inode = dentry->d_inode;
864 struct btrfs_root *root = BTRFS_I(dir)->root;
865 struct btrfs_trans_handle *trans;
866 unsigned long nr = 0;
868 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
871 mutex_lock(&root->fs_info->fs_mutex);
872 ret = btrfs_check_free_space(root, 1, 1);
876 trans = btrfs_start_transaction(root, 1);
877 btrfs_set_trans_block_group(trans, dir);
879 /* now the directory is empty */
880 err = btrfs_unlink_trans(trans, root, dir, dentry);
885 nr = trans->blocks_used;
886 ret = btrfs_end_transaction(trans, root);
888 mutex_unlock(&root->fs_info->fs_mutex);
889 btrfs_btree_balance_dirty(root, nr);
890 btrfs_throttle(root);
898 * this can truncate away extent items, csum items and directory items.
899 * It starts at a high offset and removes keys until it can't find
900 * any higher than i_size.
902 * csum items that cross the new i_size are truncated to the new size
905 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
911 struct btrfs_path *path;
912 struct btrfs_key key;
913 struct btrfs_key found_key;
915 struct extent_buffer *leaf;
916 struct btrfs_file_extent_item *fi;
917 u64 extent_start = 0;
918 u64 extent_num_bytes = 0;
924 int pending_del_nr = 0;
925 int pending_del_slot = 0;
926 int extent_type = -1;
927 u64 mask = root->sectorsize - 1;
929 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
930 path = btrfs_alloc_path();
934 /* FIXME, add redo link to tree so we don't leak on crash */
935 key.objectid = inode->i_ino;
936 key.offset = (u64)-1;
939 btrfs_init_path(path);
941 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
946 BUG_ON(path->slots[0] == 0);
952 leaf = path->nodes[0];
953 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
954 found_type = btrfs_key_type(&found_key);
956 if (found_key.objectid != inode->i_ino)
959 if (found_type < min_type)
962 item_end = found_key.offset;
963 if (found_type == BTRFS_EXTENT_DATA_KEY) {
964 fi = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_file_extent_item);
966 extent_type = btrfs_file_extent_type(leaf, fi);
967 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
969 btrfs_file_extent_num_bytes(leaf, fi);
970 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
971 struct btrfs_item *item = btrfs_item_nr(leaf,
973 item_end += btrfs_file_extent_inline_len(leaf,
978 if (found_type == BTRFS_CSUM_ITEM_KEY) {
979 ret = btrfs_csum_truncate(trans, root, path,
983 if (item_end < inode->i_size) {
984 if (found_type == BTRFS_DIR_ITEM_KEY) {
985 found_type = BTRFS_INODE_ITEM_KEY;
986 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
987 found_type = BTRFS_CSUM_ITEM_KEY;
988 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
989 found_type = BTRFS_XATTR_ITEM_KEY;
990 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
991 found_type = BTRFS_INODE_REF_KEY;
992 } else if (found_type) {
997 btrfs_set_key_type(&key, found_type);
1000 if (found_key.offset >= inode->i_size)
1006 /* FIXME, shrink the extent if the ref count is only 1 */
1007 if (found_type != BTRFS_EXTENT_DATA_KEY)
1010 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1012 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1014 u64 orig_num_bytes =
1015 btrfs_file_extent_num_bytes(leaf, fi);
1016 extent_num_bytes = inode->i_size -
1017 found_key.offset + root->sectorsize - 1;
1018 extent_num_bytes = extent_num_bytes &
1019 ~((u64)root->sectorsize - 1);
1020 btrfs_set_file_extent_num_bytes(leaf, fi,
1022 num_dec = (orig_num_bytes -
1024 if (extent_start != 0)
1025 dec_i_blocks(inode, num_dec);
1026 btrfs_mark_buffer_dirty(leaf);
1029 btrfs_file_extent_disk_num_bytes(leaf,
1031 /* FIXME blocksize != 4096 */
1032 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1033 if (extent_start != 0) {
1035 dec_i_blocks(inode, num_dec);
1037 root_gen = btrfs_header_generation(leaf);
1038 root_owner = btrfs_header_owner(leaf);
1040 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1042 u32 newsize = inode->i_size - found_key.offset;
1043 dec_i_blocks(inode, item_end + 1 -
1044 found_key.offset - newsize);
1046 btrfs_file_extent_calc_inline_size(newsize);
1047 ret = btrfs_truncate_item(trans, root, path,
1051 dec_i_blocks(inode, item_end + 1 -
1057 if (!pending_del_nr) {
1058 /* no pending yet, add ourselves */
1059 pending_del_slot = path->slots[0];
1061 } else if (pending_del_nr &&
1062 path->slots[0] + 1 == pending_del_slot) {
1063 /* hop on the pending chunk */
1065 pending_del_slot = path->slots[0];
1067 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1073 ret = btrfs_free_extent(trans, root, extent_start,
1076 root_gen, inode->i_ino,
1077 found_key.offset, 0);
1081 if (path->slots[0] == 0) {
1084 btrfs_release_path(root, path);
1089 if (pending_del_nr &&
1090 path->slots[0] + 1 != pending_del_slot) {
1091 struct btrfs_key debug;
1093 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1095 ret = btrfs_del_items(trans, root, path,
1100 btrfs_release_path(root, path);
1106 if (pending_del_nr) {
1107 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1110 btrfs_release_path(root, path);
1111 btrfs_free_path(path);
1112 inode->i_sb->s_dirt = 1;
1116 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1120 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1121 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1122 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1125 WARN_ON(!PageLocked(page));
1126 set_page_extent_mapped(page);
1128 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1129 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1130 page_end, GFP_NOFS);
1132 if (zero_start != PAGE_CACHE_SIZE) {
1134 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1135 flush_dcache_page(page);
1138 set_page_dirty(page);
1139 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1145 * taken from block_truncate_page, but does cow as it zeros out
1146 * any bytes left in the last page in the file.
1148 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1150 struct inode *inode = mapping->host;
1151 struct btrfs_root *root = BTRFS_I(inode)->root;
1152 u32 blocksize = root->sectorsize;
1153 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1154 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1159 if ((offset & (blocksize - 1)) == 0)
1163 page = grab_cache_page(mapping, index);
1166 if (!PageUptodate(page)) {
1167 ret = btrfs_readpage(NULL, page);
1169 if (!PageUptodate(page)) {
1174 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1176 ret = btrfs_cow_one_page(inode, page, offset);
1179 page_cache_release(page);
1184 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1186 struct inode *inode = dentry->d_inode;
1189 err = inode_change_ok(inode, attr);
1193 if (S_ISREG(inode->i_mode) &&
1194 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1195 struct btrfs_trans_handle *trans;
1196 struct btrfs_root *root = BTRFS_I(inode)->root;
1197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1199 u64 mask = root->sectorsize - 1;
1200 u64 hole_start = (inode->i_size + mask) & ~mask;
1201 u64 block_end = (attr->ia_size + mask) & ~mask;
1205 if (attr->ia_size <= hole_start)
1208 mutex_lock(&root->fs_info->fs_mutex);
1209 err = btrfs_check_free_space(root, 1, 0);
1210 mutex_unlock(&root->fs_info->fs_mutex);
1214 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1216 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1217 hole_size = block_end - hole_start;
1219 mutex_lock(&root->fs_info->fs_mutex);
1220 trans = btrfs_start_transaction(root, 1);
1221 btrfs_set_trans_block_group(trans, inode);
1222 err = btrfs_drop_extents(trans, root, inode,
1223 hole_start, block_end, hole_start,
1226 if (alloc_hint != EXTENT_MAP_INLINE) {
1227 err = btrfs_insert_file_extent(trans, root,
1231 btrfs_drop_extent_cache(inode, hole_start,
1233 btrfs_check_file(root, inode);
1235 btrfs_end_transaction(trans, root);
1236 mutex_unlock(&root->fs_info->fs_mutex);
1237 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1242 err = inode_setattr(inode, attr);
1247 void btrfs_put_inode(struct inode *inode)
1251 if (!BTRFS_I(inode)->ordered_trans) {
1255 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1256 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1259 ret = btrfs_del_ordered_inode(inode);
1261 atomic_dec(&inode->i_count);
1265 void btrfs_delete_inode(struct inode *inode)
1267 struct btrfs_trans_handle *trans;
1268 struct btrfs_root *root = BTRFS_I(inode)->root;
1272 truncate_inode_pages(&inode->i_data, 0);
1273 if (is_bad_inode(inode)) {
1278 mutex_lock(&root->fs_info->fs_mutex);
1279 trans = btrfs_start_transaction(root, 1);
1281 btrfs_set_trans_block_group(trans, inode);
1282 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1284 goto no_delete_lock;
1286 nr = trans->blocks_used;
1289 btrfs_end_transaction(trans, root);
1290 mutex_unlock(&root->fs_info->fs_mutex);
1291 btrfs_btree_balance_dirty(root, nr);
1292 btrfs_throttle(root);
1296 nr = trans->blocks_used;
1297 btrfs_end_transaction(trans, root);
1298 mutex_unlock(&root->fs_info->fs_mutex);
1299 btrfs_btree_balance_dirty(root, nr);
1300 btrfs_throttle(root);
1306 * this returns the key found in the dir entry in the location pointer.
1307 * If no dir entries were found, location->objectid is 0.
1309 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1310 struct btrfs_key *location)
1312 const char *name = dentry->d_name.name;
1313 int namelen = dentry->d_name.len;
1314 struct btrfs_dir_item *di;
1315 struct btrfs_path *path;
1316 struct btrfs_root *root = BTRFS_I(dir)->root;
1319 if (namelen == 1 && strcmp(name, ".") == 0) {
1320 location->objectid = dir->i_ino;
1321 location->type = BTRFS_INODE_ITEM_KEY;
1322 location->offset = 0;
1325 path = btrfs_alloc_path();
1328 if (namelen == 2 && strcmp(name, "..") == 0) {
1329 struct btrfs_key key;
1330 struct extent_buffer *leaf;
1334 key.objectid = dir->i_ino;
1335 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1341 leaf = path->nodes[0];
1342 slot = path->slots[0];
1343 nritems = btrfs_header_nritems(leaf);
1344 if (slot >= nritems)
1347 btrfs_item_key_to_cpu(leaf, &key, slot);
1348 if (key.objectid != dir->i_ino ||
1349 key.type != BTRFS_INODE_REF_KEY) {
1352 location->objectid = key.offset;
1353 location->type = BTRFS_INODE_ITEM_KEY;
1354 location->offset = 0;
1358 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1362 if (!di || IS_ERR(di)) {
1365 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1367 btrfs_free_path(path);
1370 location->objectid = 0;
1375 * when we hit a tree root in a directory, the btrfs part of the inode
1376 * needs to be changed to reflect the root directory of the tree root. This
1377 * is kind of like crossing a mount point.
1379 static int fixup_tree_root_location(struct btrfs_root *root,
1380 struct btrfs_key *location,
1381 struct btrfs_root **sub_root,
1382 struct dentry *dentry)
1384 struct btrfs_path *path;
1385 struct btrfs_root_item *ri;
1387 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1389 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1392 path = btrfs_alloc_path();
1394 mutex_lock(&root->fs_info->fs_mutex);
1396 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1397 dentry->d_name.name,
1398 dentry->d_name.len);
1399 if (IS_ERR(*sub_root))
1400 return PTR_ERR(*sub_root);
1402 ri = &(*sub_root)->root_item;
1403 location->objectid = btrfs_root_dirid(ri);
1404 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1405 location->offset = 0;
1407 btrfs_free_path(path);
1408 mutex_unlock(&root->fs_info->fs_mutex);
1412 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1414 struct btrfs_iget_args *args = p;
1415 inode->i_ino = args->ino;
1416 BTRFS_I(inode)->root = args->root;
1417 BTRFS_I(inode)->delalloc_bytes = 0;
1418 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1419 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1420 inode->i_mapping, GFP_NOFS);
1421 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1422 inode->i_mapping, GFP_NOFS);
1423 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1427 static int btrfs_find_actor(struct inode *inode, void *opaque)
1429 struct btrfs_iget_args *args = opaque;
1430 return (args->ino == inode->i_ino &&
1431 args->root == BTRFS_I(inode)->root);
1434 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1437 struct btrfs_iget_args args;
1438 args.ino = objectid;
1439 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1444 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1447 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1448 struct btrfs_root *root)
1450 struct inode *inode;
1451 struct btrfs_iget_args args;
1452 args.ino = objectid;
1455 inode = iget5_locked(s, objectid, btrfs_find_actor,
1456 btrfs_init_locked_inode,
1461 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1462 struct nameidata *nd)
1464 struct inode * inode;
1465 struct btrfs_inode *bi = BTRFS_I(dir);
1466 struct btrfs_root *root = bi->root;
1467 struct btrfs_root *sub_root = root;
1468 struct btrfs_key location;
1471 if (dentry->d_name.len > BTRFS_NAME_LEN)
1472 return ERR_PTR(-ENAMETOOLONG);
1474 mutex_lock(&root->fs_info->fs_mutex);
1475 ret = btrfs_inode_by_name(dir, dentry, &location);
1476 mutex_unlock(&root->fs_info->fs_mutex);
1479 return ERR_PTR(ret);
1482 if (location.objectid) {
1483 ret = fixup_tree_root_location(root, &location, &sub_root,
1486 return ERR_PTR(ret);
1488 return ERR_PTR(-ENOENT);
1489 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1492 return ERR_PTR(-EACCES);
1493 if (inode->i_state & I_NEW) {
1494 /* the inode and parent dir are two different roots */
1495 if (sub_root != root) {
1497 sub_root->inode = inode;
1499 BTRFS_I(inode)->root = sub_root;
1500 memcpy(&BTRFS_I(inode)->location, &location,
1502 btrfs_read_locked_inode(inode);
1503 unlock_new_inode(inode);
1506 return d_splice_alias(inode, dentry);
1509 static unsigned char btrfs_filetype_table[] = {
1510 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1513 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1515 struct inode *inode = filp->f_dentry->d_inode;
1516 struct btrfs_root *root = BTRFS_I(inode)->root;
1517 struct btrfs_item *item;
1518 struct btrfs_dir_item *di;
1519 struct btrfs_key key;
1520 struct btrfs_key found_key;
1521 struct btrfs_path *path;
1524 struct extent_buffer *leaf;
1527 unsigned char d_type;
1532 int key_type = BTRFS_DIR_INDEX_KEY;
1537 /* FIXME, use a real flag for deciding about the key type */
1538 if (root->fs_info->tree_root == root)
1539 key_type = BTRFS_DIR_ITEM_KEY;
1541 /* special case for "." */
1542 if (filp->f_pos == 0) {
1543 over = filldir(dirent, ".", 1,
1551 mutex_lock(&root->fs_info->fs_mutex);
1552 key.objectid = inode->i_ino;
1553 path = btrfs_alloc_path();
1556 /* special case for .., just use the back ref */
1557 if (filp->f_pos == 1) {
1558 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1560 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1562 leaf = path->nodes[0];
1563 slot = path->slots[0];
1564 nritems = btrfs_header_nritems(leaf);
1565 if (slot >= nritems) {
1566 btrfs_release_path(root, path);
1567 goto read_dir_items;
1569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1570 btrfs_release_path(root, path);
1571 if (found_key.objectid != key.objectid ||
1572 found_key.type != BTRFS_INODE_REF_KEY)
1573 goto read_dir_items;
1574 over = filldir(dirent, "..", 2,
1575 2, found_key.offset, DT_DIR);
1582 btrfs_set_key_type(&key, key_type);
1583 key.offset = filp->f_pos;
1585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1590 leaf = path->nodes[0];
1591 nritems = btrfs_header_nritems(leaf);
1592 slot = path->slots[0];
1593 if (advance || slot >= nritems) {
1594 if (slot >= nritems -1) {
1595 ret = btrfs_next_leaf(root, path);
1598 leaf = path->nodes[0];
1599 nritems = btrfs_header_nritems(leaf);
1600 slot = path->slots[0];
1607 item = btrfs_item_nr(leaf, slot);
1608 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1610 if (found_key.objectid != key.objectid)
1612 if (btrfs_key_type(&found_key) != key_type)
1614 if (found_key.offset < filp->f_pos)
1617 filp->f_pos = found_key.offset;
1619 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1621 di_total = btrfs_item_size(leaf, item);
1622 while(di_cur < di_total) {
1623 struct btrfs_key location;
1625 name_len = btrfs_dir_name_len(leaf, di);
1626 if (name_len < 32) {
1627 name_ptr = tmp_name;
1629 name_ptr = kmalloc(name_len, GFP_NOFS);
1632 read_extent_buffer(leaf, name_ptr,
1633 (unsigned long)(di + 1), name_len);
1635 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1636 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1637 over = filldir(dirent, name_ptr, name_len,
1642 if (name_ptr != tmp_name)
1647 di_len = btrfs_dir_name_len(leaf, di) +
1648 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1650 di = (struct btrfs_dir_item *)((char *)di + di_len);
1653 if (key_type == BTRFS_DIR_INDEX_KEY)
1654 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1660 btrfs_release_path(root, path);
1661 btrfs_free_path(path);
1662 mutex_unlock(&root->fs_info->fs_mutex);
1666 int btrfs_write_inode(struct inode *inode, int wait)
1668 struct btrfs_root *root = BTRFS_I(inode)->root;
1669 struct btrfs_trans_handle *trans;
1673 mutex_lock(&root->fs_info->fs_mutex);
1674 trans = btrfs_start_transaction(root, 1);
1675 btrfs_set_trans_block_group(trans, inode);
1676 ret = btrfs_commit_transaction(trans, root);
1677 mutex_unlock(&root->fs_info->fs_mutex);
1683 * This is somewhat expensive, updating the tree every time the
1684 * inode changes. But, it is most likely to find the inode in cache.
1685 * FIXME, needs more benchmarking...there are no reasons other than performance
1686 * to keep or drop this code.
1688 void btrfs_dirty_inode(struct inode *inode)
1690 struct btrfs_root *root = BTRFS_I(inode)->root;
1691 struct btrfs_trans_handle *trans;
1693 mutex_lock(&root->fs_info->fs_mutex);
1694 trans = btrfs_start_transaction(root, 1);
1695 btrfs_set_trans_block_group(trans, inode);
1696 btrfs_update_inode(trans, root, inode);
1697 btrfs_end_transaction(trans, root);
1698 mutex_unlock(&root->fs_info->fs_mutex);
1701 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1702 struct btrfs_root *root,
1703 const char *name, int name_len,
1706 struct btrfs_block_group_cache *group,
1709 struct inode *inode;
1710 struct btrfs_inode_item *inode_item;
1711 struct btrfs_block_group_cache *new_inode_group;
1712 struct btrfs_key *location;
1713 struct btrfs_path *path;
1714 struct btrfs_inode_ref *ref;
1715 struct btrfs_key key[2];
1721 path = btrfs_alloc_path();
1724 inode = new_inode(root->fs_info->sb);
1726 return ERR_PTR(-ENOMEM);
1728 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1729 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1730 inode->i_mapping, GFP_NOFS);
1731 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1732 inode->i_mapping, GFP_NOFS);
1733 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1734 BTRFS_I(inode)->delalloc_bytes = 0;
1735 BTRFS_I(inode)->root = root;
1741 new_inode_group = btrfs_find_block_group(root, group, 0,
1742 BTRFS_BLOCK_GROUP_METADATA, owner);
1743 if (!new_inode_group) {
1744 printk("find_block group failed\n");
1745 new_inode_group = group;
1747 BTRFS_I(inode)->block_group = new_inode_group;
1748 BTRFS_I(inode)->flags = 0;
1750 key[0].objectid = objectid;
1751 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1754 key[1].objectid = objectid;
1755 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1756 key[1].offset = ref_objectid;
1758 sizes[0] = sizeof(struct btrfs_inode_item);
1759 sizes[1] = name_len + sizeof(*ref);
1761 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1765 if (objectid > root->highest_inode)
1766 root->highest_inode = objectid;
1768 inode->i_uid = current->fsuid;
1769 inode->i_gid = current->fsgid;
1770 inode->i_mode = mode;
1771 inode->i_ino = objectid;
1772 inode->i_blocks = 0;
1773 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1774 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_item);
1776 fill_inode_item(path->nodes[0], inode_item, inode);
1778 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1779 struct btrfs_inode_ref);
1780 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1781 ptr = (unsigned long)(ref + 1);
1782 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1784 btrfs_mark_buffer_dirty(path->nodes[0]);
1785 btrfs_free_path(path);
1787 location = &BTRFS_I(inode)->location;
1788 location->objectid = objectid;
1789 location->offset = 0;
1790 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1792 insert_inode_hash(inode);
1795 btrfs_free_path(path);
1796 return ERR_PTR(ret);
1799 static inline u8 btrfs_inode_type(struct inode *inode)
1801 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1804 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1805 struct dentry *dentry, struct inode *inode,
1809 struct btrfs_key key;
1810 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1811 struct inode *parent_inode;
1813 key.objectid = inode->i_ino;
1814 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1817 ret = btrfs_insert_dir_item(trans, root,
1818 dentry->d_name.name, dentry->d_name.len,
1819 dentry->d_parent->d_inode->i_ino,
1820 &key, btrfs_inode_type(inode));
1823 ret = btrfs_insert_inode_ref(trans, root,
1824 dentry->d_name.name,
1827 dentry->d_parent->d_inode->i_ino);
1829 parent_inode = dentry->d_parent->d_inode;
1830 parent_inode->i_size += dentry->d_name.len * 2;
1831 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1832 ret = btrfs_update_inode(trans, root,
1833 dentry->d_parent->d_inode);
1838 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1839 struct dentry *dentry, struct inode *inode,
1842 int err = btrfs_add_link(trans, dentry, inode, backref);
1844 d_instantiate(dentry, inode);
1852 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1853 int mode, dev_t rdev)
1855 struct btrfs_trans_handle *trans;
1856 struct btrfs_root *root = BTRFS_I(dir)->root;
1857 struct inode *inode = NULL;
1861 unsigned long nr = 0;
1863 if (!new_valid_dev(rdev))
1866 mutex_lock(&root->fs_info->fs_mutex);
1867 err = btrfs_check_free_space(root, 1, 0);
1871 trans = btrfs_start_transaction(root, 1);
1872 btrfs_set_trans_block_group(trans, dir);
1874 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1880 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1882 dentry->d_parent->d_inode->i_ino, objectid,
1883 BTRFS_I(dir)->block_group, mode);
1884 err = PTR_ERR(inode);
1888 btrfs_set_trans_block_group(trans, inode);
1889 err = btrfs_add_nondir(trans, dentry, inode, 0);
1893 inode->i_op = &btrfs_special_inode_operations;
1894 init_special_inode(inode, inode->i_mode, rdev);
1895 btrfs_update_inode(trans, root, inode);
1897 dir->i_sb->s_dirt = 1;
1898 btrfs_update_inode_block_group(trans, inode);
1899 btrfs_update_inode_block_group(trans, dir);
1901 nr = trans->blocks_used;
1902 btrfs_end_transaction(trans, root);
1904 mutex_unlock(&root->fs_info->fs_mutex);
1907 inode_dec_link_count(inode);
1910 btrfs_btree_balance_dirty(root, nr);
1911 btrfs_throttle(root);
1915 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1916 int mode, struct nameidata *nd)
1918 struct btrfs_trans_handle *trans;
1919 struct btrfs_root *root = BTRFS_I(dir)->root;
1920 struct inode *inode = NULL;
1923 unsigned long nr = 0;
1926 mutex_lock(&root->fs_info->fs_mutex);
1927 err = btrfs_check_free_space(root, 1, 0);
1930 trans = btrfs_start_transaction(root, 1);
1931 btrfs_set_trans_block_group(trans, dir);
1933 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1939 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1941 dentry->d_parent->d_inode->i_ino,
1942 objectid, BTRFS_I(dir)->block_group, mode);
1943 err = PTR_ERR(inode);
1947 btrfs_set_trans_block_group(trans, inode);
1948 err = btrfs_add_nondir(trans, dentry, inode, 0);
1952 inode->i_mapping->a_ops = &btrfs_aops;
1953 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1954 inode->i_fop = &btrfs_file_operations;
1955 inode->i_op = &btrfs_file_inode_operations;
1956 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1957 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1958 inode->i_mapping, GFP_NOFS);
1959 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1960 inode->i_mapping, GFP_NOFS);
1961 BTRFS_I(inode)->delalloc_bytes = 0;
1962 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1963 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1965 dir->i_sb->s_dirt = 1;
1966 btrfs_update_inode_block_group(trans, inode);
1967 btrfs_update_inode_block_group(trans, dir);
1969 nr = trans->blocks_used;
1970 btrfs_end_transaction(trans, root);
1972 mutex_unlock(&root->fs_info->fs_mutex);
1975 inode_dec_link_count(inode);
1978 btrfs_btree_balance_dirty(root, nr);
1979 btrfs_throttle(root);
1983 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1984 struct dentry *dentry)
1986 struct btrfs_trans_handle *trans;
1987 struct btrfs_root *root = BTRFS_I(dir)->root;
1988 struct inode *inode = old_dentry->d_inode;
1989 unsigned long nr = 0;
1993 if (inode->i_nlink == 0)
1996 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2001 mutex_lock(&root->fs_info->fs_mutex);
2002 err = btrfs_check_free_space(root, 1, 0);
2005 trans = btrfs_start_transaction(root, 1);
2007 btrfs_set_trans_block_group(trans, dir);
2008 atomic_inc(&inode->i_count);
2009 err = btrfs_add_nondir(trans, dentry, inode, 1);
2014 dir->i_sb->s_dirt = 1;
2015 btrfs_update_inode_block_group(trans, dir);
2016 err = btrfs_update_inode(trans, root, inode);
2021 nr = trans->blocks_used;
2022 btrfs_end_transaction(trans, root);
2024 mutex_unlock(&root->fs_info->fs_mutex);
2027 inode_dec_link_count(inode);
2030 btrfs_btree_balance_dirty(root, nr);
2031 btrfs_throttle(root);
2035 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2037 struct inode *inode;
2038 struct btrfs_trans_handle *trans;
2039 struct btrfs_root *root = BTRFS_I(dir)->root;
2041 int drop_on_err = 0;
2043 unsigned long nr = 1;
2045 mutex_lock(&root->fs_info->fs_mutex);
2046 err = btrfs_check_free_space(root, 1, 0);
2050 trans = btrfs_start_transaction(root, 1);
2051 btrfs_set_trans_block_group(trans, dir);
2053 if (IS_ERR(trans)) {
2054 err = PTR_ERR(trans);
2058 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2064 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2066 dentry->d_parent->d_inode->i_ino, objectid,
2067 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2068 if (IS_ERR(inode)) {
2069 err = PTR_ERR(inode);
2074 inode->i_op = &btrfs_dir_inode_operations;
2075 inode->i_fop = &btrfs_dir_file_operations;
2076 btrfs_set_trans_block_group(trans, inode);
2079 err = btrfs_update_inode(trans, root, inode);
2083 err = btrfs_add_link(trans, dentry, inode, 0);
2087 d_instantiate(dentry, inode);
2089 dir->i_sb->s_dirt = 1;
2090 btrfs_update_inode_block_group(trans, inode);
2091 btrfs_update_inode_block_group(trans, dir);
2094 nr = trans->blocks_used;
2095 btrfs_end_transaction(trans, root);
2098 mutex_unlock(&root->fs_info->fs_mutex);
2101 btrfs_btree_balance_dirty(root, nr);
2102 btrfs_throttle(root);
2106 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2107 struct extent_map *existing,
2108 struct extent_map *em)
2113 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2115 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2118 if (!real_blocks && em->block_start != existing->block_start)
2121 new_end = max(existing->start + existing->len, em->start + em->len);
2123 if (existing->start >= em->start) {
2124 if (em->start + em->len < existing->start)
2127 start_diff = existing->start - em->start;
2128 if (real_blocks && em->block_start + start_diff !=
2129 existing->block_start)
2132 em->len = new_end - em->start;
2134 remove_extent_mapping(em_tree, existing);
2135 /* free for the tree */
2136 free_extent_map(existing);
2137 ret = add_extent_mapping(em_tree, em);
2139 } else if (em->start > existing->start) {
2141 if (existing->start + existing->len < em->start)
2144 start_diff = em->start - existing->start;
2145 if (real_blocks && existing->block_start + start_diff !=
2149 remove_extent_mapping(em_tree, existing);
2150 em->block_start = existing->block_start;
2151 em->start = existing->start;
2152 em->len = new_end - existing->start;
2153 free_extent_map(existing);
2155 ret = add_extent_mapping(em_tree, em);
2162 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2163 existing->start, existing->len, existing->block_start,
2164 em->start, em->len, em->block_start);
2168 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2169 size_t pg_offset, u64 start, u64 len,
2175 u64 extent_start = 0;
2177 u64 objectid = inode->i_ino;
2179 struct btrfs_path *path;
2180 struct btrfs_root *root = BTRFS_I(inode)->root;
2181 struct btrfs_file_extent_item *item;
2182 struct extent_buffer *leaf;
2183 struct btrfs_key found_key;
2184 struct extent_map *em = NULL;
2185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2186 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2187 struct btrfs_trans_handle *trans = NULL;
2189 path = btrfs_alloc_path();
2191 mutex_lock(&root->fs_info->fs_mutex);
2194 spin_lock(&em_tree->lock);
2195 em = lookup_extent_mapping(em_tree, start, len);
2196 spin_unlock(&em_tree->lock);
2199 if (em->start > start || em->start + em->len <= start)
2200 free_extent_map(em);
2201 else if (em->block_start == EXTENT_MAP_INLINE && page)
2202 free_extent_map(em);
2206 em = alloc_extent_map(GFP_NOFS);
2212 em->start = EXTENT_MAP_HOLE;
2214 em->bdev = inode->i_sb->s_bdev;
2215 ret = btrfs_lookup_file_extent(trans, root, path,
2216 objectid, start, trans != NULL);
2223 if (path->slots[0] == 0)
2228 leaf = path->nodes[0];
2229 item = btrfs_item_ptr(leaf, path->slots[0],
2230 struct btrfs_file_extent_item);
2231 /* are we inside the extent that was found? */
2232 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2233 found_type = btrfs_key_type(&found_key);
2234 if (found_key.objectid != objectid ||
2235 found_type != BTRFS_EXTENT_DATA_KEY) {
2239 found_type = btrfs_file_extent_type(leaf, item);
2240 extent_start = found_key.offset;
2241 if (found_type == BTRFS_FILE_EXTENT_REG) {
2242 extent_end = extent_start +
2243 btrfs_file_extent_num_bytes(leaf, item);
2245 if (start < extent_start || start >= extent_end) {
2247 if (start < extent_start) {
2248 if (start + len <= extent_start)
2250 em->len = extent_end - extent_start;
2256 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2258 em->start = extent_start;
2259 em->len = extent_end - extent_start;
2260 em->block_start = EXTENT_MAP_HOLE;
2263 bytenr += btrfs_file_extent_offset(leaf, item);
2264 em->block_start = bytenr;
2265 em->start = extent_start;
2266 em->len = extent_end - extent_start;
2268 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2273 size_t extent_offset;
2276 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2278 extent_end = (extent_start + size + root->sectorsize - 1) &
2279 ~((u64)root->sectorsize - 1);
2280 if (start < extent_start || start >= extent_end) {
2282 if (start < extent_start) {
2283 if (start + len <= extent_start)
2285 em->len = extent_end - extent_start;
2291 em->block_start = EXTENT_MAP_INLINE;
2294 em->start = extent_start;
2299 page_start = page_offset(page) + pg_offset;
2300 extent_offset = page_start - extent_start;
2301 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2302 size - extent_offset);
2303 em->start = extent_start + extent_offset;
2304 em->len = (copy_size + root->sectorsize - 1) &
2305 ~((u64)root->sectorsize - 1);
2307 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2308 if (create == 0 && !PageUptodate(page)) {
2309 read_extent_buffer(leaf, map + pg_offset, ptr,
2311 flush_dcache_page(page);
2312 } else if (create && PageUptodate(page)) {
2315 free_extent_map(em);
2317 btrfs_release_path(root, path);
2318 trans = btrfs_start_transaction(root, 1);
2321 write_extent_buffer(leaf, map + pg_offset, ptr,
2323 btrfs_mark_buffer_dirty(leaf);
2326 set_extent_uptodate(io_tree, em->start,
2327 extent_map_end(em) - 1, GFP_NOFS);
2330 printk("unkknown found_type %d\n", found_type);
2337 em->block_start = EXTENT_MAP_HOLE;
2339 btrfs_release_path(root, path);
2340 if (em->start > start || extent_map_end(em) <= start) {
2341 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2347 spin_lock(&em_tree->lock);
2348 ret = add_extent_mapping(em_tree, em);
2349 /* it is possible that someone inserted the extent into the tree
2350 * while we had the lock dropped. It is also possible that
2351 * an overlapping map exists in the tree
2353 if (ret == -EEXIST) {
2354 struct extent_map *existing;
2355 existing = lookup_extent_mapping(em_tree, start, len);
2356 if (existing && (existing->start > start ||
2357 existing->start + existing->len <= start)) {
2358 free_extent_map(existing);
2362 existing = lookup_extent_mapping(em_tree, em->start,
2365 err = merge_extent_mapping(em_tree, existing,
2367 free_extent_map(existing);
2369 free_extent_map(em);
2374 printk("failing to insert %Lu %Lu\n",
2376 free_extent_map(em);
2380 free_extent_map(em);
2384 spin_unlock(&em_tree->lock);
2386 btrfs_free_path(path);
2388 ret = btrfs_end_transaction(trans, root);
2392 mutex_unlock(&root->fs_info->fs_mutex);
2394 free_extent_map(em);
2396 return ERR_PTR(err);
2401 #if 0 /* waiting for O_DIRECT reads */
2402 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2403 struct buffer_head *bh_result, int create)
2405 struct extent_map *em;
2406 u64 start = (u64)iblock << inode->i_blkbits;
2407 struct btrfs_multi_bio *multi = NULL;
2408 struct btrfs_root *root = BTRFS_I(inode)->root;
2414 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2416 if (!em || IS_ERR(em))
2419 if (em->start > start || em->start + em->len <= start) {
2423 if (em->block_start == EXTENT_MAP_INLINE) {
2428 len = em->start + em->len - start;
2429 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2431 if (em->block_start == EXTENT_MAP_HOLE ||
2432 em->block_start == EXTENT_MAP_DELALLOC) {
2433 bh_result->b_size = len;
2437 logical = start - em->start;
2438 logical = em->block_start + logical;
2441 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2442 logical, &map_length, &multi, 0);
2444 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2445 bh_result->b_size = min(map_length, len);
2447 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2448 set_buffer_mapped(bh_result);
2451 free_extent_map(em);
2456 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2457 const struct iovec *iov, loff_t offset,
2458 unsigned long nr_segs)
2462 struct file *file = iocb->ki_filp;
2463 struct inode *inode = file->f_mapping->host;
2468 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2469 offset, nr_segs, btrfs_get_block, NULL);
2473 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2475 return extent_bmap(mapping, iblock, btrfs_get_extent);
2478 int btrfs_readpage(struct file *file, struct page *page)
2480 struct extent_io_tree *tree;
2481 tree = &BTRFS_I(page->mapping->host)->io_tree;
2482 return extent_read_full_page(tree, page, btrfs_get_extent);
2485 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2487 struct extent_io_tree *tree;
2490 if (current->flags & PF_MEMALLOC) {
2491 redirty_page_for_writepage(wbc, page);
2495 tree = &BTRFS_I(page->mapping->host)->io_tree;
2496 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2499 static int btrfs_writepages(struct address_space *mapping,
2500 struct writeback_control *wbc)
2502 struct extent_io_tree *tree;
2503 tree = &BTRFS_I(mapping->host)->io_tree;
2504 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2508 btrfs_readpages(struct file *file, struct address_space *mapping,
2509 struct list_head *pages, unsigned nr_pages)
2511 struct extent_io_tree *tree;
2512 tree = &BTRFS_I(mapping->host)->io_tree;
2513 return extent_readpages(tree, mapping, pages, nr_pages,
2517 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2519 struct extent_io_tree *tree;
2520 struct extent_map_tree *map;
2523 tree = &BTRFS_I(page->mapping->host)->io_tree;
2524 map = &BTRFS_I(page->mapping->host)->extent_tree;
2525 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2527 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2528 ClearPagePrivate(page);
2529 set_page_private(page, 0);
2530 page_cache_release(page);
2535 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2537 struct extent_io_tree *tree;
2539 tree = &BTRFS_I(page->mapping->host)->io_tree;
2540 extent_invalidatepage(tree, page, offset);
2541 btrfs_releasepage(page, GFP_NOFS);
2542 if (PagePrivate(page)) {
2543 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2544 ClearPagePrivate(page);
2545 set_page_private(page, 0);
2546 page_cache_release(page);
2551 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2552 * called from a page fault handler when a page is first dirtied. Hence we must
2553 * be careful to check for EOF conditions here. We set the page up correctly
2554 * for a written page which means we get ENOSPC checking when writing into
2555 * holes and correct delalloc and unwritten extent mapping on filesystems that
2556 * support these features.
2558 * We are not allowed to take the i_mutex here so we have to play games to
2559 * protect against truncate races as the page could now be beyond EOF. Because
2560 * vmtruncate() writes the inode size before removing pages, once we have the
2561 * page lock we can determine safely if the page is beyond EOF. If it is not
2562 * beyond EOF, then the page is guaranteed safe against truncation until we
2565 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2567 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2568 struct btrfs_root *root = BTRFS_I(inode)->root;
2574 mutex_lock(&root->fs_info->fs_mutex);
2575 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2576 mutex_unlock(&root->fs_info->fs_mutex);
2583 wait_on_page_writeback(page);
2584 size = i_size_read(inode);
2585 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2587 if ((page->mapping != inode->i_mapping) ||
2588 (page_start > size)) {
2589 /* page got truncated out from underneath us */
2593 /* page is wholly or partially inside EOF */
2594 if (page_start + PAGE_CACHE_SIZE > size)
2595 end = size & ~PAGE_CACHE_MASK;
2597 end = PAGE_CACHE_SIZE;
2599 ret = btrfs_cow_one_page(inode, page, end);
2607 static void btrfs_truncate(struct inode *inode)
2609 struct btrfs_root *root = BTRFS_I(inode)->root;
2611 struct btrfs_trans_handle *trans;
2614 if (!S_ISREG(inode->i_mode))
2616 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2619 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2621 mutex_lock(&root->fs_info->fs_mutex);
2622 trans = btrfs_start_transaction(root, 1);
2623 btrfs_set_trans_block_group(trans, inode);
2625 /* FIXME, add redo link to tree so we don't leak on crash */
2626 ret = btrfs_truncate_in_trans(trans, root, inode,
2627 BTRFS_EXTENT_DATA_KEY);
2628 btrfs_update_inode(trans, root, inode);
2629 nr = trans->blocks_used;
2631 ret = btrfs_end_transaction(trans, root);
2633 mutex_unlock(&root->fs_info->fs_mutex);
2634 btrfs_btree_balance_dirty(root, nr);
2635 btrfs_throttle(root);
2638 static int noinline create_subvol(struct btrfs_root *root, char *name,
2641 struct btrfs_trans_handle *trans;
2642 struct btrfs_key key;
2643 struct btrfs_root_item root_item;
2644 struct btrfs_inode_item *inode_item;
2645 struct extent_buffer *leaf;
2646 struct btrfs_root *new_root = root;
2647 struct inode *inode;
2652 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2653 unsigned long nr = 1;
2655 mutex_lock(&root->fs_info->fs_mutex);
2656 ret = btrfs_check_free_space(root, 1, 0);
2660 trans = btrfs_start_transaction(root, 1);
2663 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2668 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2669 objectid, trans->transid, 0, 0,
2672 return PTR_ERR(leaf);
2674 btrfs_set_header_nritems(leaf, 0);
2675 btrfs_set_header_level(leaf, 0);
2676 btrfs_set_header_bytenr(leaf, leaf->start);
2677 btrfs_set_header_generation(leaf, trans->transid);
2678 btrfs_set_header_owner(leaf, objectid);
2680 write_extent_buffer(leaf, root->fs_info->fsid,
2681 (unsigned long)btrfs_header_fsid(leaf),
2683 btrfs_mark_buffer_dirty(leaf);
2685 inode_item = &root_item.inode;
2686 memset(inode_item, 0, sizeof(*inode_item));
2687 inode_item->generation = cpu_to_le64(1);
2688 inode_item->size = cpu_to_le64(3);
2689 inode_item->nlink = cpu_to_le32(1);
2690 inode_item->nblocks = cpu_to_le64(1);
2691 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2693 btrfs_set_root_bytenr(&root_item, leaf->start);
2694 btrfs_set_root_level(&root_item, 0);
2695 btrfs_set_root_refs(&root_item, 1);
2696 btrfs_set_root_used(&root_item, 0);
2698 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2699 root_item.drop_level = 0;
2701 free_extent_buffer(leaf);
2704 btrfs_set_root_dirid(&root_item, new_dirid);
2706 key.objectid = objectid;
2708 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2709 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2715 * insert the directory item
2717 key.offset = (u64)-1;
2718 dir = root->fs_info->sb->s_root->d_inode;
2719 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2720 name, namelen, dir->i_ino, &key,
2725 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2726 name, namelen, objectid,
2727 root->fs_info->sb->s_root->d_inode->i_ino);
2731 ret = btrfs_commit_transaction(trans, root);
2735 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2738 trans = btrfs_start_transaction(new_root, 1);
2741 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2743 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2746 inode->i_op = &btrfs_dir_inode_operations;
2747 inode->i_fop = &btrfs_dir_file_operations;
2748 new_root->inode = inode;
2750 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2754 ret = btrfs_update_inode(trans, new_root, inode);
2758 nr = trans->blocks_used;
2759 err = btrfs_commit_transaction(trans, new_root);
2763 mutex_unlock(&root->fs_info->fs_mutex);
2764 btrfs_btree_balance_dirty(root, nr);
2765 btrfs_throttle(root);
2769 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2771 struct btrfs_pending_snapshot *pending_snapshot;
2772 struct btrfs_trans_handle *trans;
2775 unsigned long nr = 0;
2777 if (!root->ref_cows)
2780 mutex_lock(&root->fs_info->fs_mutex);
2781 ret = btrfs_check_free_space(root, 1, 0);
2785 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2786 if (!pending_snapshot) {
2790 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2791 if (!pending_snapshot->name) {
2793 kfree(pending_snapshot);
2796 memcpy(pending_snapshot->name, name, namelen);
2797 pending_snapshot->name[namelen] = '\0';
2798 trans = btrfs_start_transaction(root, 1);
2800 pending_snapshot->root = root;
2801 list_add(&pending_snapshot->list,
2802 &trans->transaction->pending_snapshots);
2803 ret = btrfs_update_inode(trans, root, root->inode);
2804 err = btrfs_commit_transaction(trans, root);
2807 mutex_unlock(&root->fs_info->fs_mutex);
2808 btrfs_btree_balance_dirty(root, nr);
2809 btrfs_throttle(root);
2813 unsigned long btrfs_force_ra(struct address_space *mapping,
2814 struct file_ra_state *ra, struct file *file,
2815 pgoff_t offset, pgoff_t last_index)
2817 pgoff_t req_size = last_index - offset + 1;
2819 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2820 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2823 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2824 return offset + req_size;
2828 int btrfs_defrag_file(struct file *file) {
2829 struct inode *inode = fdentry(file)->d_inode;
2830 struct btrfs_root *root = BTRFS_I(inode)->root;
2831 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2833 unsigned long last_index;
2834 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2835 unsigned long total_read = 0;
2841 mutex_lock(&root->fs_info->fs_mutex);
2842 ret = btrfs_check_free_space(root, inode->i_size, 0);
2843 mutex_unlock(&root->fs_info->fs_mutex);
2847 mutex_lock(&inode->i_mutex);
2848 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2849 for (i = 0; i <= last_index; i++) {
2850 if (total_read % ra_pages == 0) {
2851 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2852 min(last_index, i + ra_pages - 1));
2855 page = grab_cache_page(inode->i_mapping, i);
2858 if (!PageUptodate(page)) {
2859 btrfs_readpage(NULL, page);
2861 if (!PageUptodate(page)) {
2863 page_cache_release(page);
2868 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2869 ClearPageDirty(page);
2871 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2873 wait_on_page_writeback(page);
2874 set_page_extent_mapped(page);
2876 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2877 page_end = page_start + PAGE_CACHE_SIZE - 1;
2879 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2880 set_extent_delalloc(io_tree, page_start,
2881 page_end, GFP_NOFS);
2883 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2884 set_page_dirty(page);
2886 page_cache_release(page);
2887 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2891 mutex_unlock(&inode->i_mutex);
2895 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2900 struct btrfs_ioctl_vol_args *vol_args;
2901 struct btrfs_trans_handle *trans;
2902 struct btrfs_device *device = NULL;
2904 char *devstr = NULL;
2909 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2914 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2918 namelen = strlen(vol_args->name);
2919 if (namelen > BTRFS_VOL_NAME_MAX) {
2924 mutex_lock(&root->fs_info->fs_mutex);
2925 sizestr = vol_args->name;
2926 devstr = strchr(sizestr, ':');
2929 sizestr = devstr + 1;
2931 devstr = vol_args->name;
2932 devid = simple_strtoull(devstr, &end, 10);
2933 printk("resizing devid %Lu\n", devid);
2935 device = btrfs_find_device(root, devid, NULL);
2937 printk("resizer unable to find device %Lu\n", devid);
2941 if (!strcmp(sizestr, "max"))
2942 new_size = device->bdev->bd_inode->i_size;
2944 if (sizestr[0] == '-') {
2947 } else if (sizestr[0] == '+') {
2951 new_size = btrfs_parse_size(sizestr);
2952 if (new_size == 0) {
2958 old_size = device->total_bytes;
2961 if (new_size > old_size) {
2965 new_size = old_size - new_size;
2966 } else if (mod > 0) {
2967 new_size = old_size + new_size;
2970 if (new_size < 256 * 1024 * 1024) {
2974 if (new_size > device->bdev->bd_inode->i_size) {
2979 do_div(new_size, root->sectorsize);
2980 new_size *= root->sectorsize;
2982 printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
2984 if (new_size > old_size) {
2985 trans = btrfs_start_transaction(root, 1);
2986 ret = btrfs_grow_device(trans, device, new_size);
2987 btrfs_commit_transaction(trans, root);
2989 ret = btrfs_shrink_device(device, new_size);
2993 mutex_unlock(&root->fs_info->fs_mutex);
2999 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3002 struct btrfs_ioctl_vol_args *vol_args;
3003 struct btrfs_dir_item *di;
3004 struct btrfs_path *path;
3009 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3014 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3019 namelen = strlen(vol_args->name);
3020 if (namelen > BTRFS_VOL_NAME_MAX) {
3024 if (strchr(vol_args->name, '/')) {
3029 path = btrfs_alloc_path();
3035 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3036 mutex_lock(&root->fs_info->fs_mutex);
3037 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3039 vol_args->name, namelen, 0);
3040 mutex_unlock(&root->fs_info->fs_mutex);
3041 btrfs_free_path(path);
3043 if (di && !IS_ERR(di)) {
3053 if (root == root->fs_info->tree_root)
3054 ret = create_subvol(root, vol_args->name, namelen);
3056 ret = create_snapshot(root, vol_args->name, namelen);
3062 static int btrfs_ioctl_defrag(struct file *file)
3064 struct inode *inode = fdentry(file)->d_inode;
3065 struct btrfs_root *root = BTRFS_I(inode)->root;
3067 switch (inode->i_mode & S_IFMT) {
3069 mutex_lock(&root->fs_info->fs_mutex);
3070 btrfs_defrag_root(root, 0);
3071 btrfs_defrag_root(root->fs_info->extent_root, 0);
3072 mutex_unlock(&root->fs_info->fs_mutex);
3075 btrfs_defrag_file(file);
3082 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3084 struct btrfs_ioctl_vol_args *vol_args;
3087 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3092 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3096 ret = btrfs_init_new_device(root, vol_args->name);
3103 long btrfs_ioctl(struct file *file, unsigned int
3104 cmd, unsigned long arg)
3106 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3109 case BTRFS_IOC_SNAP_CREATE:
3110 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3111 case BTRFS_IOC_DEFRAG:
3112 return btrfs_ioctl_defrag(file);
3113 case BTRFS_IOC_RESIZE:
3114 return btrfs_ioctl_resize(root, (void __user *)arg);
3115 case BTRFS_IOC_ADD_DEV:
3116 return btrfs_ioctl_add_dev(root, (void __user *)arg);
3117 case BTRFS_IOC_BALANCE:
3118 return btrfs_balance(root->fs_info->dev_root);
3125 * Called inside transaction, so use GFP_NOFS
3127 struct inode *btrfs_alloc_inode(struct super_block *sb)
3129 struct btrfs_inode *ei;
3131 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3135 ei->ordered_trans = 0;
3136 return &ei->vfs_inode;
3139 void btrfs_destroy_inode(struct inode *inode)
3141 WARN_ON(!list_empty(&inode->i_dentry));
3142 WARN_ON(inode->i_data.nrpages);
3144 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3145 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3148 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3149 static void init_once(struct kmem_cache * cachep, void *foo)
3151 static void init_once(void * foo, struct kmem_cache * cachep,
3152 unsigned long flags)
3155 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3157 inode_init_once(&ei->vfs_inode);
3160 void btrfs_destroy_cachep(void)
3162 if (btrfs_inode_cachep)
3163 kmem_cache_destroy(btrfs_inode_cachep);
3164 if (btrfs_trans_handle_cachep)
3165 kmem_cache_destroy(btrfs_trans_handle_cachep);
3166 if (btrfs_transaction_cachep)
3167 kmem_cache_destroy(btrfs_transaction_cachep);
3168 if (btrfs_bit_radix_cachep)
3169 kmem_cache_destroy(btrfs_bit_radix_cachep);
3170 if (btrfs_path_cachep)
3171 kmem_cache_destroy(btrfs_path_cachep);
3174 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3175 unsigned long extra_flags,
3176 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3177 void (*ctor)(struct kmem_cache *, void *)
3179 void (*ctor)(void *, struct kmem_cache *,
3184 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3185 SLAB_MEM_SPREAD | extra_flags), ctor
3186 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3192 int btrfs_init_cachep(void)
3194 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3195 sizeof(struct btrfs_inode),
3197 if (!btrfs_inode_cachep)
3199 btrfs_trans_handle_cachep =
3200 btrfs_cache_create("btrfs_trans_handle_cache",
3201 sizeof(struct btrfs_trans_handle),
3203 if (!btrfs_trans_handle_cachep)
3205 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3206 sizeof(struct btrfs_transaction),
3208 if (!btrfs_transaction_cachep)
3210 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3211 sizeof(struct btrfs_path),
3213 if (!btrfs_path_cachep)
3215 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3216 SLAB_DESTROY_BY_RCU, NULL);
3217 if (!btrfs_bit_radix_cachep)
3221 btrfs_destroy_cachep();
3225 static int btrfs_getattr(struct vfsmount *mnt,
3226 struct dentry *dentry, struct kstat *stat)
3228 struct inode *inode = dentry->d_inode;
3229 generic_fillattr(inode, stat);
3230 stat->blksize = PAGE_CACHE_SIZE;
3231 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3235 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3236 struct inode * new_dir,struct dentry *new_dentry)
3238 struct btrfs_trans_handle *trans;
3239 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3240 struct inode *new_inode = new_dentry->d_inode;
3241 struct inode *old_inode = old_dentry->d_inode;
3242 struct timespec ctime = CURRENT_TIME;
3243 struct btrfs_path *path;
3246 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3247 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3251 mutex_lock(&root->fs_info->fs_mutex);
3252 ret = btrfs_check_free_space(root, 1, 0);
3256 trans = btrfs_start_transaction(root, 1);
3258 btrfs_set_trans_block_group(trans, new_dir);
3259 path = btrfs_alloc_path();
3265 old_dentry->d_inode->i_nlink++;
3266 old_dir->i_ctime = old_dir->i_mtime = ctime;
3267 new_dir->i_ctime = new_dir->i_mtime = ctime;
3268 old_inode->i_ctime = ctime;
3270 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3275 new_inode->i_ctime = CURRENT_TIME;
3276 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3280 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3285 btrfs_free_path(path);
3286 btrfs_end_transaction(trans, root);
3288 mutex_unlock(&root->fs_info->fs_mutex);
3292 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3293 const char *symname)
3295 struct btrfs_trans_handle *trans;
3296 struct btrfs_root *root = BTRFS_I(dir)->root;
3297 struct btrfs_path *path;
3298 struct btrfs_key key;
3299 struct inode *inode = NULL;
3306 struct btrfs_file_extent_item *ei;
3307 struct extent_buffer *leaf;
3308 unsigned long nr = 0;
3310 name_len = strlen(symname) + 1;
3311 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3312 return -ENAMETOOLONG;
3314 mutex_lock(&root->fs_info->fs_mutex);
3315 err = btrfs_check_free_space(root, 1, 0);
3319 trans = btrfs_start_transaction(root, 1);
3320 btrfs_set_trans_block_group(trans, dir);
3322 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3328 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3330 dentry->d_parent->d_inode->i_ino, objectid,
3331 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3332 err = PTR_ERR(inode);
3336 btrfs_set_trans_block_group(trans, inode);
3337 err = btrfs_add_nondir(trans, dentry, inode, 0);
3341 inode->i_mapping->a_ops = &btrfs_aops;
3342 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3343 inode->i_fop = &btrfs_file_operations;
3344 inode->i_op = &btrfs_file_inode_operations;
3345 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3346 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3347 inode->i_mapping, GFP_NOFS);
3348 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3349 inode->i_mapping, GFP_NOFS);
3350 BTRFS_I(inode)->delalloc_bytes = 0;
3351 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
3352 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3354 dir->i_sb->s_dirt = 1;
3355 btrfs_update_inode_block_group(trans, inode);
3356 btrfs_update_inode_block_group(trans, dir);
3360 path = btrfs_alloc_path();
3362 key.objectid = inode->i_ino;
3364 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3365 datasize = btrfs_file_extent_calc_inline_size(name_len);
3366 err = btrfs_insert_empty_item(trans, root, path, &key,
3372 leaf = path->nodes[0];
3373 ei = btrfs_item_ptr(leaf, path->slots[0],
3374 struct btrfs_file_extent_item);
3375 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3376 btrfs_set_file_extent_type(leaf, ei,
3377 BTRFS_FILE_EXTENT_INLINE);
3378 ptr = btrfs_file_extent_inline_start(ei);
3379 write_extent_buffer(leaf, symname, ptr, name_len);
3380 btrfs_mark_buffer_dirty(leaf);
3381 btrfs_free_path(path);
3383 inode->i_op = &btrfs_symlink_inode_operations;
3384 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3385 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3386 inode->i_size = name_len - 1;
3387 err = btrfs_update_inode(trans, root, inode);
3392 nr = trans->blocks_used;
3393 btrfs_end_transaction(trans, root);
3395 mutex_unlock(&root->fs_info->fs_mutex);
3397 inode_dec_link_count(inode);
3400 btrfs_btree_balance_dirty(root, nr);
3401 btrfs_throttle(root);
3405 static int btrfs_permission(struct inode *inode, int mask,
3406 struct nameidata *nd)
3408 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3410 return generic_permission(inode, mask, NULL);
3413 static struct inode_operations btrfs_dir_inode_operations = {
3414 .lookup = btrfs_lookup,
3415 .create = btrfs_create,
3416 .unlink = btrfs_unlink,
3418 .mkdir = btrfs_mkdir,
3419 .rmdir = btrfs_rmdir,
3420 .rename = btrfs_rename,
3421 .symlink = btrfs_symlink,
3422 .setattr = btrfs_setattr,
3423 .mknod = btrfs_mknod,
3424 .setxattr = generic_setxattr,
3425 .getxattr = generic_getxattr,
3426 .listxattr = btrfs_listxattr,
3427 .removexattr = generic_removexattr,
3428 .permission = btrfs_permission,
3430 static struct inode_operations btrfs_dir_ro_inode_operations = {
3431 .lookup = btrfs_lookup,
3432 .permission = btrfs_permission,
3434 static struct file_operations btrfs_dir_file_operations = {
3435 .llseek = generic_file_llseek,
3436 .read = generic_read_dir,
3437 .readdir = btrfs_readdir,
3438 .unlocked_ioctl = btrfs_ioctl,
3439 #ifdef CONFIG_COMPAT
3440 .compat_ioctl = btrfs_ioctl,
3444 static struct extent_io_ops btrfs_extent_io_ops = {
3445 .fill_delalloc = run_delalloc_range,
3446 .submit_bio_hook = btrfs_submit_bio_hook,
3447 .merge_bio_hook = btrfs_merge_bio_hook,
3448 .readpage_io_hook = btrfs_readpage_io_hook,
3449 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3450 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3451 .set_bit_hook = btrfs_set_bit_hook,
3452 .clear_bit_hook = btrfs_clear_bit_hook,
3455 static struct address_space_operations btrfs_aops = {
3456 .readpage = btrfs_readpage,
3457 .writepage = btrfs_writepage,
3458 .writepages = btrfs_writepages,
3459 .readpages = btrfs_readpages,
3460 .sync_page = block_sync_page,
3462 .direct_IO = btrfs_direct_IO,
3463 .invalidatepage = btrfs_invalidatepage,
3464 .releasepage = btrfs_releasepage,
3465 .set_page_dirty = __set_page_dirty_nobuffers,
3468 static struct address_space_operations btrfs_symlink_aops = {
3469 .readpage = btrfs_readpage,
3470 .writepage = btrfs_writepage,
3471 .invalidatepage = btrfs_invalidatepage,
3472 .releasepage = btrfs_releasepage,
3475 static struct inode_operations btrfs_file_inode_operations = {
3476 .truncate = btrfs_truncate,
3477 .getattr = btrfs_getattr,
3478 .setattr = btrfs_setattr,
3479 .setxattr = generic_setxattr,
3480 .getxattr = generic_getxattr,
3481 .listxattr = btrfs_listxattr,
3482 .removexattr = generic_removexattr,
3483 .permission = btrfs_permission,
3485 static struct inode_operations btrfs_special_inode_operations = {
3486 .getattr = btrfs_getattr,
3487 .setattr = btrfs_setattr,
3488 .permission = btrfs_permission,
3490 static struct inode_operations btrfs_symlink_inode_operations = {
3491 .readlink = generic_readlink,
3492 .follow_link = page_follow_link_light,
3493 .put_link = page_put_link,
3494 .permission = btrfs_permission,