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>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
46 #include "ordered-data.h"
48 struct btrfs_iget_args {
50 struct btrfs_root *root;
53 static struct inode_operations btrfs_dir_inode_operations;
54 static struct inode_operations btrfs_symlink_inode_operations;
55 static struct inode_operations btrfs_dir_ro_inode_operations;
56 static struct inode_operations btrfs_special_inode_operations;
57 static struct inode_operations btrfs_file_inode_operations;
58 static struct address_space_operations btrfs_aops;
59 static struct address_space_operations btrfs_symlink_aops;
60 static struct file_operations btrfs_dir_file_operations;
61 static struct extent_io_ops btrfs_extent_io_ops;
63 static struct kmem_cache *btrfs_inode_cachep;
64 struct kmem_cache *btrfs_trans_handle_cachep;
65 struct kmem_cache *btrfs_transaction_cachep;
66 struct kmem_cache *btrfs_bit_radix_cachep;
67 struct kmem_cache *btrfs_path_cachep;
70 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
80 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
89 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
99 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
105 static int cow_file_range(struct inode *inode, u64 start, u64 end)
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
112 u64 blocksize = root->sectorsize;
114 struct btrfs_key ins;
115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
119 trans = btrfs_join_transaction(root, 1);
121 btrfs_set_trans_block_group(trans, inode);
123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
124 num_bytes = max(blocksize, num_bytes);
125 orig_num_bytes = num_bytes;
127 if (alloc_hint == EXTENT_MAP_INLINE)
130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
131 mutex_lock(&BTRFS_I(inode)->extent_mutex);
132 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
133 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
135 while(num_bytes > 0) {
136 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
137 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
138 root->sectorsize, 0, 0,
144 em = alloc_extent_map(GFP_NOFS);
146 em->len = ins.offset;
147 em->block_start = ins.objectid;
148 em->bdev = root->fs_info->fs_devices->latest_bdev;
149 mutex_lock(&BTRFS_I(inode)->extent_mutex);
150 set_bit(EXTENT_FLAG_PINNED, &em->flags);
152 spin_lock(&em_tree->lock);
153 ret = add_extent_mapping(em_tree, em);
154 spin_unlock(&em_tree->lock);
155 if (ret != -EEXIST) {
159 btrfs_drop_extent_cache(inode, start,
160 start + ins.offset - 1);
162 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
164 cur_alloc_size = ins.offset;
165 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
168 if (num_bytes < cur_alloc_size) {
169 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 num_bytes -= cur_alloc_size;
174 alloc_hint = ins.objectid + ins.offset;
175 start += cur_alloc_size;
178 btrfs_end_transaction(trans, root);
182 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
190 struct btrfs_root *root = BTRFS_I(inode)->root;
191 struct btrfs_block_group_cache *block_group;
192 struct extent_buffer *leaf;
194 struct btrfs_path *path;
195 struct btrfs_file_extent_item *item;
198 struct btrfs_key found_key;
200 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
201 path = btrfs_alloc_path();
204 ret = btrfs_lookup_file_extent(NULL, root, path,
205 inode->i_ino, start, 0);
207 btrfs_free_path(path);
213 if (path->slots[0] == 0)
218 leaf = path->nodes[0];
219 item = btrfs_item_ptr(leaf, path->slots[0],
220 struct btrfs_file_extent_item);
222 /* are we inside the extent that was found? */
223 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
224 found_type = btrfs_key_type(&found_key);
225 if (found_key.objectid != inode->i_ino ||
226 found_type != BTRFS_EXTENT_DATA_KEY)
229 found_type = btrfs_file_extent_type(leaf, item);
230 extent_start = found_key.offset;
231 if (found_type == BTRFS_FILE_EXTENT_REG) {
232 u64 extent_num_bytes;
234 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
235 extent_end = extent_start + extent_num_bytes;
238 if (loops && start != extent_start)
241 if (start < extent_start || start >= extent_end)
244 cow_end = min(end, extent_end - 1);
245 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
258 block_group = btrfs_lookup_block_group(root->fs_info,
260 if (!block_group || block_group->ro)
269 btrfs_free_path(path);
272 btrfs_release_path(root, path);
277 cow_file_range(inode, start, end);
282 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284 struct btrfs_root *root = BTRFS_I(inode)->root;
287 if (btrfs_test_opt(root, NODATACOW) ||
288 btrfs_test_flag(inode, NODATACOW))
289 ret = run_delalloc_nocow(inode, start, end);
291 ret = cow_file_range(inode, start, end);
296 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
297 unsigned long old, unsigned long bits)
300 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
301 struct btrfs_root *root = BTRFS_I(inode)->root;
302 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
303 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
304 root->fs_info->delalloc_bytes += end - start + 1;
305 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
310 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
311 unsigned long old, unsigned long bits)
313 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
314 struct btrfs_root *root = BTRFS_I(inode)->root;
317 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
318 if (end - start + 1 > root->fs_info->delalloc_bytes) {
319 printk("warning: delalloc account %Lu %Lu\n",
320 end - start + 1, root->fs_info->delalloc_bytes);
321 root->fs_info->delalloc_bytes = 0;
322 BTRFS_I(inode)->delalloc_bytes = 0;
324 root->fs_info->delalloc_bytes -= end - start + 1;
325 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
327 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
332 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
333 size_t size, struct bio *bio)
335 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
336 struct btrfs_mapping_tree *map_tree;
337 u64 logical = bio->bi_sector << 9;
342 length = bio->bi_size;
343 map_tree = &root->fs_info->mapping_tree;
345 ret = btrfs_map_block(map_tree, READ, logical,
346 &map_length, NULL, 0);
348 if (map_length < length + size) {
354 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
357 struct btrfs_root *root = BTRFS_I(inode)->root;
360 ret = btrfs_csum_one_bio(root, inode, bio);
363 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
366 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
369 struct btrfs_root *root = BTRFS_I(inode)->root;
372 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
375 if (!(rw & (1 << BIO_RW))) {
379 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
380 inode, rw, bio, mirror_num,
381 __btrfs_submit_bio_hook);
383 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
386 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
387 struct inode *inode, u64 file_offset,
388 struct list_head *list)
390 struct list_head *cur;
391 struct btrfs_ordered_sum *sum;
393 btrfs_set_trans_block_group(trans, inode);
394 list_for_each(cur, list) {
395 sum = list_entry(cur, struct btrfs_ordered_sum, list);
396 mutex_lock(&BTRFS_I(inode)->csum_mutex);
397 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
399 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
404 struct btrfs_writepage_fixup {
406 struct btrfs_work work;
409 /* see btrfs_writepage_start_hook for details on why this is required */
410 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
412 struct btrfs_writepage_fixup *fixup;
413 struct btrfs_ordered_extent *ordered;
419 fixup = container_of(work, struct btrfs_writepage_fixup, work);
423 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
424 ClearPageChecked(page);
428 inode = page->mapping->host;
429 page_start = page_offset(page);
430 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
432 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
434 /* already ordered? We're done */
435 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
436 EXTENT_ORDERED, 0)) {
440 ordered = btrfs_lookup_ordered_extent(inode, page_start);
442 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
445 btrfs_start_ordered_extent(inode, ordered, 1);
449 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
451 ClearPageChecked(page);
453 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
456 page_cache_release(page);
460 * There are a few paths in the higher layers of the kernel that directly
461 * set the page dirty bit without asking the filesystem if it is a
462 * good idea. This causes problems because we want to make sure COW
463 * properly happens and the data=ordered rules are followed.
465 * In our case any range that doesn't have the EXTENT_ORDERED bit set
466 * hasn't been properly setup for IO. We kick off an async process
467 * to fix it up. The async helper will wait for ordered extents, set
468 * the delalloc bit and make it safe to write the page.
470 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
472 struct inode *inode = page->mapping->host;
473 struct btrfs_writepage_fixup *fixup;
474 struct btrfs_root *root = BTRFS_I(inode)->root;
477 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
482 if (PageChecked(page))
485 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
489 SetPageChecked(page);
490 page_cache_get(page);
491 fixup->work.func = btrfs_writepage_fixup_worker;
493 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
497 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
499 struct btrfs_root *root = BTRFS_I(inode)->root;
500 struct btrfs_trans_handle *trans;
501 struct btrfs_ordered_extent *ordered_extent;
502 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
503 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
504 struct extent_map *em;
505 struct extent_map *em_orig;
509 struct list_head list;
510 struct btrfs_key ins;
514 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
518 trans = btrfs_join_transaction(root, 1);
520 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
521 BUG_ON(!ordered_extent);
523 lock_extent(io_tree, ordered_extent->file_offset,
524 ordered_extent->file_offset + ordered_extent->len - 1,
527 INIT_LIST_HEAD(&list);
529 ins.objectid = ordered_extent->start;
530 ins.offset = ordered_extent->len;
531 ins.type = BTRFS_EXTENT_ITEM_KEY;
533 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
534 trans->transid, inode->i_ino,
535 ordered_extent->file_offset, &ins);
538 mutex_lock(&BTRFS_I(inode)->extent_mutex);
540 spin_lock(&em_tree->lock);
541 clear_start = ordered_extent->file_offset;
542 clear_end = ordered_extent->file_offset + ordered_extent->len;
543 em = lookup_extent_mapping(em_tree, clear_start,
544 ordered_extent->len);
546 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
547 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
548 rb = rb_next(&em->rb_node);
551 em = rb_entry(rb, struct extent_map, rb_node);
553 free_extent_map(em_orig);
554 spin_unlock(&em_tree->lock);
556 ret = btrfs_drop_extents(trans, root, inode,
557 ordered_extent->file_offset,
558 ordered_extent->file_offset +
560 ordered_extent->file_offset, &alloc_hint);
562 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
563 ordered_extent->file_offset,
564 ordered_extent->start,
566 ordered_extent->len, 0);
569 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
570 ordered_extent->file_offset +
571 ordered_extent->len - 1);
572 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
574 inode->i_blocks += ordered_extent->len >> 9;
575 unlock_extent(io_tree, ordered_extent->file_offset,
576 ordered_extent->file_offset + ordered_extent->len - 1,
578 add_pending_csums(trans, inode, ordered_extent->file_offset,
579 &ordered_extent->list);
581 btrfs_ordered_update_i_size(inode, ordered_extent);
582 btrfs_remove_ordered_extent(inode, ordered_extent);
585 btrfs_put_ordered_extent(ordered_extent);
586 /* once for the tree */
587 btrfs_put_ordered_extent(ordered_extent);
589 btrfs_update_inode(trans, root, inode);
590 btrfs_end_transaction(trans, root);
594 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
595 struct extent_state *state, int uptodate)
597 return btrfs_finish_ordered_io(page->mapping->host, start, end);
600 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
603 struct inode *inode = page->mapping->host;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
606 struct btrfs_csum_item *item;
607 struct btrfs_path *path = NULL;
610 if (btrfs_test_opt(root, NODATASUM) ||
611 btrfs_test_flag(inode, NODATASUM))
615 * It is possible there is an ordered extent that has
616 * not yet finished for this range in the file. If so,
617 * that extent will have a csum cached, and it will insert
618 * the sum after all the blocks in the extent are fully
619 * on disk. So, look for an ordered extent and use the
620 * sum if found. We have to do this before looking in the
621 * btree because csum items are pre-inserted based on
622 * the file size. btrfs_lookup_csum might find an item
623 * that still hasn't been fully filled.
625 ret = btrfs_find_ordered_sum(inode, start, &csum);
630 path = btrfs_alloc_path();
631 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
634 /* a csum that isn't present is a preallocated region. */
635 if (ret == -ENOENT || ret == -EFBIG)
638 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
642 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
645 set_state_private(io_tree, start, csum);
648 btrfs_free_path(path);
652 struct io_failure_record {
660 int btrfs_io_failed_hook(struct bio *failed_bio,
661 struct page *page, u64 start, u64 end,
662 struct extent_state *state)
664 struct io_failure_record *failrec = NULL;
666 struct extent_map *em;
667 struct inode *inode = page->mapping->host;
668 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
669 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
676 ret = get_state_private(failure_tree, start, &private);
678 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
681 failrec->start = start;
682 failrec->len = end - start + 1;
683 failrec->last_mirror = 0;
685 spin_lock(&em_tree->lock);
686 em = lookup_extent_mapping(em_tree, start, failrec->len);
687 if (em->start > start || em->start + em->len < start) {
691 spin_unlock(&em_tree->lock);
693 if (!em || IS_ERR(em)) {
697 logical = start - em->start;
698 logical = em->block_start + logical;
699 failrec->logical = logical;
701 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
702 EXTENT_DIRTY, GFP_NOFS);
703 set_state_private(failure_tree, start,
704 (u64)(unsigned long)failrec);
706 failrec = (struct io_failure_record *)(unsigned long)private;
708 num_copies = btrfs_num_copies(
709 &BTRFS_I(inode)->root->fs_info->mapping_tree,
710 failrec->logical, failrec->len);
711 failrec->last_mirror++;
713 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
714 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
717 if (state && state->start != failrec->start)
719 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
721 if (!state || failrec->last_mirror > num_copies) {
722 set_state_private(failure_tree, failrec->start, 0);
723 clear_extent_bits(failure_tree, failrec->start,
724 failrec->start + failrec->len - 1,
725 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
729 bio = bio_alloc(GFP_NOFS, 1);
730 bio->bi_private = state;
731 bio->bi_end_io = failed_bio->bi_end_io;
732 bio->bi_sector = failrec->logical >> 9;
733 bio->bi_bdev = failed_bio->bi_bdev;
735 bio_add_page(bio, page, failrec->len, start - page_offset(page));
736 if (failed_bio->bi_rw & (1 << BIO_RW))
741 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
742 failrec->last_mirror);
746 int btrfs_clean_io_failures(struct inode *inode, u64 start)
750 struct io_failure_record *failure;
754 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
755 (u64)-1, 1, EXTENT_DIRTY)) {
756 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
757 start, &private_failure);
759 failure = (struct io_failure_record *)(unsigned long)
761 set_state_private(&BTRFS_I(inode)->io_failure_tree,
763 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
765 failure->start + failure->len - 1,
766 EXTENT_DIRTY | EXTENT_LOCKED,
774 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
775 struct extent_state *state)
777 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
778 struct inode *inode = page->mapping->host;
779 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
781 u64 private = ~(u32)0;
783 struct btrfs_root *root = BTRFS_I(inode)->root;
787 if (btrfs_test_opt(root, NODATASUM) ||
788 btrfs_test_flag(inode, NODATASUM))
790 if (state && state->start == start) {
791 private = state->private;
794 ret = get_state_private(io_tree, start, &private);
796 local_irq_save(flags);
797 kaddr = kmap_atomic(page, KM_IRQ0);
801 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
802 btrfs_csum_final(csum, (char *)&csum);
803 if (csum != private) {
806 kunmap_atomic(kaddr, KM_IRQ0);
807 local_irq_restore(flags);
809 /* if the io failure tree for this inode is non-empty,
810 * check to see if we've recovered from a failed IO
812 btrfs_clean_io_failures(inode, start);
816 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
817 page->mapping->host->i_ino, (unsigned long long)start, csum,
819 memset(kaddr + offset, 1, end - start + 1);
820 flush_dcache_page(page);
821 kunmap_atomic(kaddr, KM_IRQ0);
822 local_irq_restore(flags);
828 void btrfs_read_locked_inode(struct inode *inode)
830 struct btrfs_path *path;
831 struct extent_buffer *leaf;
832 struct btrfs_inode_item *inode_item;
833 struct btrfs_timespec *tspec;
834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 struct btrfs_key location;
836 u64 alloc_group_block;
840 path = btrfs_alloc_path();
842 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
844 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
848 leaf = path->nodes[0];
849 inode_item = btrfs_item_ptr(leaf, path->slots[0],
850 struct btrfs_inode_item);
852 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
853 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
854 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
855 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
856 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
858 tspec = btrfs_inode_atime(inode_item);
859 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
860 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
862 tspec = btrfs_inode_mtime(inode_item);
863 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
864 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
866 tspec = btrfs_inode_ctime(inode_item);
867 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
868 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
870 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
871 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
873 rdev = btrfs_inode_rdev(leaf, inode_item);
875 BTRFS_I(inode)->index_cnt = (u64)-1;
877 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
878 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
880 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
881 if (!BTRFS_I(inode)->block_group) {
882 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
884 BTRFS_BLOCK_GROUP_METADATA, 0);
886 btrfs_free_path(path);
889 switch (inode->i_mode & S_IFMT) {
891 inode->i_mapping->a_ops = &btrfs_aops;
892 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
893 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
894 inode->i_fop = &btrfs_file_operations;
895 inode->i_op = &btrfs_file_inode_operations;
898 inode->i_fop = &btrfs_dir_file_operations;
899 if (root == root->fs_info->tree_root)
900 inode->i_op = &btrfs_dir_ro_inode_operations;
902 inode->i_op = &btrfs_dir_inode_operations;
905 inode->i_op = &btrfs_symlink_inode_operations;
906 inode->i_mapping->a_ops = &btrfs_symlink_aops;
907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
910 init_special_inode(inode, inode->i_mode, rdev);
916 btrfs_free_path(path);
917 make_bad_inode(inode);
920 static void fill_inode_item(struct extent_buffer *leaf,
921 struct btrfs_inode_item *item,
924 btrfs_set_inode_uid(leaf, item, inode->i_uid);
925 btrfs_set_inode_gid(leaf, item, inode->i_gid);
926 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
927 btrfs_set_inode_mode(leaf, item, inode->i_mode);
928 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
930 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
931 inode->i_atime.tv_sec);
932 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
933 inode->i_atime.tv_nsec);
935 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
936 inode->i_mtime.tv_sec);
937 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
938 inode->i_mtime.tv_nsec);
940 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
941 inode->i_ctime.tv_sec);
942 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
943 inode->i_ctime.tv_nsec);
945 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
946 btrfs_set_inode_generation(leaf, item, inode->i_generation);
947 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
948 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
949 btrfs_set_inode_block_group(leaf, item,
950 BTRFS_I(inode)->block_group->key.objectid);
953 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root,
957 struct btrfs_inode_item *inode_item;
958 struct btrfs_path *path;
959 struct extent_buffer *leaf;
962 path = btrfs_alloc_path();
964 ret = btrfs_lookup_inode(trans, root, path,
965 &BTRFS_I(inode)->location, 1);
972 leaf = path->nodes[0];
973 inode_item = btrfs_item_ptr(leaf, path->slots[0],
974 struct btrfs_inode_item);
976 fill_inode_item(leaf, inode_item, inode);
977 btrfs_mark_buffer_dirty(leaf);
978 btrfs_set_inode_last_trans(trans, inode);
981 btrfs_free_path(path);
986 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
989 struct dentry *dentry)
991 struct btrfs_path *path;
992 const char *name = dentry->d_name.name;
993 int name_len = dentry->d_name.len;
995 struct extent_buffer *leaf;
996 struct btrfs_dir_item *di;
997 struct btrfs_key key;
1000 path = btrfs_alloc_path();
1006 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1007 name, name_len, -1);
1016 leaf = path->nodes[0];
1017 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1018 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1021 btrfs_release_path(root, path);
1023 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1024 dentry->d_inode->i_ino,
1025 dentry->d_parent->d_inode->i_ino, &index);
1027 printk("failed to delete reference to %.*s, "
1028 "inode %lu parent %lu\n", name_len, name,
1029 dentry->d_inode->i_ino,
1030 dentry->d_parent->d_inode->i_ino);
1034 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1035 index, name, name_len, -1);
1044 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1045 btrfs_release_path(root, path);
1047 dentry->d_inode->i_ctime = dir->i_ctime;
1049 btrfs_free_path(path);
1051 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1052 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1053 btrfs_update_inode(trans, root, dir);
1054 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1055 dentry->d_inode->i_nlink--;
1057 drop_nlink(dentry->d_inode);
1059 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1060 dir->i_sb->s_dirt = 1;
1065 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1067 struct btrfs_root *root;
1068 struct btrfs_trans_handle *trans;
1070 unsigned long nr = 0;
1072 root = BTRFS_I(dir)->root;
1074 ret = btrfs_check_free_space(root, 1, 1);
1078 trans = btrfs_start_transaction(root, 1);
1080 btrfs_set_trans_block_group(trans, dir);
1081 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1082 nr = trans->blocks_used;
1084 btrfs_end_transaction_throttle(trans, root);
1086 btrfs_btree_balance_dirty(root, nr);
1090 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1092 struct inode *inode = dentry->d_inode;
1095 struct btrfs_root *root = BTRFS_I(dir)->root;
1096 struct btrfs_trans_handle *trans;
1097 unsigned long nr = 0;
1099 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1103 ret = btrfs_check_free_space(root, 1, 1);
1107 trans = btrfs_start_transaction(root, 1);
1108 btrfs_set_trans_block_group(trans, dir);
1110 /* now the directory is empty */
1111 err = btrfs_unlink_trans(trans, root, dir, dentry);
1113 btrfs_i_size_write(inode, 0);
1116 nr = trans->blocks_used;
1117 ret = btrfs_end_transaction_throttle(trans, root);
1119 btrfs_btree_balance_dirty(root, nr);
1127 * this can truncate away extent items, csum items and directory items.
1128 * It starts at a high offset and removes keys until it can't find
1129 * any higher than i_size.
1131 * csum items that cross the new i_size are truncated to the new size
1134 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1135 struct btrfs_root *root,
1136 struct inode *inode,
1140 struct btrfs_path *path;
1141 struct btrfs_key key;
1142 struct btrfs_key found_key;
1144 struct extent_buffer *leaf;
1145 struct btrfs_file_extent_item *fi;
1146 u64 extent_start = 0;
1147 u64 extent_num_bytes = 0;
1153 int pending_del_nr = 0;
1154 int pending_del_slot = 0;
1155 int extent_type = -1;
1156 u64 mask = root->sectorsize - 1;
1158 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1159 path = btrfs_alloc_path();
1163 /* FIXME, add redo link to tree so we don't leak on crash */
1164 key.objectid = inode->i_ino;
1165 key.offset = (u64)-1;
1168 btrfs_init_path(path);
1170 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1175 BUG_ON(path->slots[0] == 0);
1181 leaf = path->nodes[0];
1182 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1183 found_type = btrfs_key_type(&found_key);
1185 if (found_key.objectid != inode->i_ino)
1188 if (found_type < min_type)
1191 item_end = found_key.offset;
1192 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1193 fi = btrfs_item_ptr(leaf, path->slots[0],
1194 struct btrfs_file_extent_item);
1195 extent_type = btrfs_file_extent_type(leaf, fi);
1196 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1198 btrfs_file_extent_num_bytes(leaf, fi);
1199 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1200 struct btrfs_item *item = btrfs_item_nr(leaf,
1202 item_end += btrfs_file_extent_inline_len(leaf,
1207 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1208 ret = btrfs_csum_truncate(trans, root, path,
1212 if (item_end < inode->i_size) {
1213 if (found_type == BTRFS_DIR_ITEM_KEY) {
1214 found_type = BTRFS_INODE_ITEM_KEY;
1215 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1216 found_type = BTRFS_CSUM_ITEM_KEY;
1217 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1218 found_type = BTRFS_XATTR_ITEM_KEY;
1219 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1220 found_type = BTRFS_INODE_REF_KEY;
1221 } else if (found_type) {
1226 btrfs_set_key_type(&key, found_type);
1229 if (found_key.offset >= inode->i_size)
1235 /* FIXME, shrink the extent if the ref count is only 1 */
1236 if (found_type != BTRFS_EXTENT_DATA_KEY)
1239 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1241 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1243 u64 orig_num_bytes =
1244 btrfs_file_extent_num_bytes(leaf, fi);
1245 extent_num_bytes = inode->i_size -
1246 found_key.offset + root->sectorsize - 1;
1247 extent_num_bytes = extent_num_bytes &
1248 ~((u64)root->sectorsize - 1);
1249 btrfs_set_file_extent_num_bytes(leaf, fi,
1251 num_dec = (orig_num_bytes -
1253 if (extent_start != 0)
1254 dec_i_blocks(inode, num_dec);
1255 btrfs_mark_buffer_dirty(leaf);
1258 btrfs_file_extent_disk_num_bytes(leaf,
1260 /* FIXME blocksize != 4096 */
1261 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1262 if (extent_start != 0) {
1264 dec_i_blocks(inode, num_dec);
1266 root_gen = btrfs_header_generation(leaf);
1267 root_owner = btrfs_header_owner(leaf);
1269 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1271 u32 newsize = inode->i_size - found_key.offset;
1272 dec_i_blocks(inode, item_end + 1 -
1273 found_key.offset - newsize);
1275 btrfs_file_extent_calc_inline_size(newsize);
1276 ret = btrfs_truncate_item(trans, root, path,
1280 dec_i_blocks(inode, item_end + 1 -
1286 if (!pending_del_nr) {
1287 /* no pending yet, add ourselves */
1288 pending_del_slot = path->slots[0];
1290 } else if (pending_del_nr &&
1291 path->slots[0] + 1 == pending_del_slot) {
1292 /* hop on the pending chunk */
1294 pending_del_slot = path->slots[0];
1296 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1302 ret = btrfs_free_extent(trans, root, extent_start,
1305 root_gen, inode->i_ino,
1306 found_key.offset, 0);
1310 if (path->slots[0] == 0) {
1313 btrfs_release_path(root, path);
1318 if (pending_del_nr &&
1319 path->slots[0] + 1 != pending_del_slot) {
1320 struct btrfs_key debug;
1322 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1324 ret = btrfs_del_items(trans, root, path,
1329 btrfs_release_path(root, path);
1335 if (pending_del_nr) {
1336 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1339 btrfs_free_path(path);
1340 inode->i_sb->s_dirt = 1;
1345 * taken from block_truncate_page, but does cow as it zeros out
1346 * any bytes left in the last page in the file.
1348 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1350 struct inode *inode = mapping->host;
1351 struct btrfs_root *root = BTRFS_I(inode)->root;
1352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1353 struct btrfs_ordered_extent *ordered;
1355 u32 blocksize = root->sectorsize;
1356 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1357 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1363 if ((offset & (blocksize - 1)) == 0)
1368 page = grab_cache_page(mapping, index);
1372 page_start = page_offset(page);
1373 page_end = page_start + PAGE_CACHE_SIZE - 1;
1375 if (!PageUptodate(page)) {
1376 ret = btrfs_readpage(NULL, page);
1378 if (page->mapping != mapping) {
1380 page_cache_release(page);
1383 if (!PageUptodate(page)) {
1388 wait_on_page_writeback(page);
1390 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1391 set_page_extent_mapped(page);
1393 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1395 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1397 page_cache_release(page);
1398 btrfs_start_ordered_extent(inode, ordered, 1);
1399 btrfs_put_ordered_extent(ordered);
1403 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1404 page_end, GFP_NOFS);
1406 if (offset != PAGE_CACHE_SIZE) {
1408 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1409 flush_dcache_page(page);
1412 ClearPageChecked(page);
1413 set_page_dirty(page);
1414 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1418 page_cache_release(page);
1423 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1425 struct inode *inode = dentry->d_inode;
1428 err = inode_change_ok(inode, attr);
1432 if (S_ISREG(inode->i_mode) &&
1433 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
1436 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1438 u64 mask = root->sectorsize - 1;
1439 u64 hole_start = (inode->i_size + mask) & ~mask;
1440 u64 block_end = (attr->ia_size + mask) & ~mask;
1444 if (attr->ia_size <= hole_start)
1447 err = btrfs_check_free_space(root, 1, 0);
1451 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1453 hole_size = block_end - hole_start;
1454 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1455 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1457 trans = btrfs_start_transaction(root, 1);
1458 btrfs_set_trans_block_group(trans, inode);
1459 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1460 err = btrfs_drop_extents(trans, root, inode,
1461 hole_start, block_end, hole_start,
1464 if (alloc_hint != EXTENT_MAP_INLINE) {
1465 err = btrfs_insert_file_extent(trans, root,
1469 btrfs_drop_extent_cache(inode, hole_start,
1471 btrfs_check_file(root, inode);
1473 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1474 btrfs_end_transaction(trans, root);
1475 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1480 err = inode_setattr(inode, attr);
1485 void btrfs_delete_inode(struct inode *inode)
1487 struct btrfs_trans_handle *trans;
1488 struct btrfs_root *root = BTRFS_I(inode)->root;
1492 truncate_inode_pages(&inode->i_data, 0);
1493 if (is_bad_inode(inode)) {
1496 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1498 btrfs_i_size_write(inode, 0);
1499 trans = btrfs_start_transaction(root, 1);
1501 btrfs_set_trans_block_group(trans, inode);
1502 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1504 goto no_delete_lock;
1506 nr = trans->blocks_used;
1509 btrfs_end_transaction(trans, root);
1510 btrfs_btree_balance_dirty(root, nr);
1514 nr = trans->blocks_used;
1515 btrfs_end_transaction(trans, root);
1516 btrfs_btree_balance_dirty(root, nr);
1522 * this returns the key found in the dir entry in the location pointer.
1523 * If no dir entries were found, location->objectid is 0.
1525 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1526 struct btrfs_key *location)
1528 const char *name = dentry->d_name.name;
1529 int namelen = dentry->d_name.len;
1530 struct btrfs_dir_item *di;
1531 struct btrfs_path *path;
1532 struct btrfs_root *root = BTRFS_I(dir)->root;
1535 if (namelen == 1 && strcmp(name, ".") == 0) {
1536 location->objectid = dir->i_ino;
1537 location->type = BTRFS_INODE_ITEM_KEY;
1538 location->offset = 0;
1541 path = btrfs_alloc_path();
1544 if (namelen == 2 && strcmp(name, "..") == 0) {
1545 struct btrfs_key key;
1546 struct extent_buffer *leaf;
1550 key.objectid = dir->i_ino;
1551 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1553 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1557 leaf = path->nodes[0];
1558 slot = path->slots[0];
1559 nritems = btrfs_header_nritems(leaf);
1560 if (slot >= nritems)
1563 btrfs_item_key_to_cpu(leaf, &key, slot);
1564 if (key.objectid != dir->i_ino ||
1565 key.type != BTRFS_INODE_REF_KEY) {
1568 location->objectid = key.offset;
1569 location->type = BTRFS_INODE_ITEM_KEY;
1570 location->offset = 0;
1574 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1578 if (!di || IS_ERR(di)) {
1581 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1583 btrfs_free_path(path);
1586 location->objectid = 0;
1591 * when we hit a tree root in a directory, the btrfs part of the inode
1592 * needs to be changed to reflect the root directory of the tree root. This
1593 * is kind of like crossing a mount point.
1595 static int fixup_tree_root_location(struct btrfs_root *root,
1596 struct btrfs_key *location,
1597 struct btrfs_root **sub_root,
1598 struct dentry *dentry)
1600 struct btrfs_path *path;
1601 struct btrfs_root_item *ri;
1603 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1605 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1608 path = btrfs_alloc_path();
1611 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1612 dentry->d_name.name,
1613 dentry->d_name.len);
1614 if (IS_ERR(*sub_root))
1615 return PTR_ERR(*sub_root);
1617 ri = &(*sub_root)->root_item;
1618 location->objectid = btrfs_root_dirid(ri);
1619 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1620 location->offset = 0;
1622 btrfs_free_path(path);
1626 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1628 struct btrfs_iget_args *args = p;
1629 inode->i_ino = args->ino;
1630 BTRFS_I(inode)->root = args->root;
1631 BTRFS_I(inode)->delalloc_bytes = 0;
1632 BTRFS_I(inode)->disk_i_size = 0;
1633 BTRFS_I(inode)->index_cnt = (u64)-1;
1634 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1635 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1636 inode->i_mapping, GFP_NOFS);
1637 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1638 inode->i_mapping, GFP_NOFS);
1639 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1640 mutex_init(&BTRFS_I(inode)->csum_mutex);
1641 mutex_init(&BTRFS_I(inode)->extent_mutex);
1645 static int btrfs_find_actor(struct inode *inode, void *opaque)
1647 struct btrfs_iget_args *args = opaque;
1648 return (args->ino == inode->i_ino &&
1649 args->root == BTRFS_I(inode)->root);
1652 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1655 struct btrfs_iget_args args;
1656 args.ino = objectid;
1657 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1662 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1665 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1666 struct btrfs_root *root)
1668 struct inode *inode;
1669 struct btrfs_iget_args args;
1670 args.ino = objectid;
1673 inode = iget5_locked(s, objectid, btrfs_find_actor,
1674 btrfs_init_locked_inode,
1679 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1680 struct nameidata *nd)
1682 struct inode * inode;
1683 struct btrfs_inode *bi = BTRFS_I(dir);
1684 struct btrfs_root *root = bi->root;
1685 struct btrfs_root *sub_root = root;
1686 struct btrfs_key location;
1689 if (dentry->d_name.len > BTRFS_NAME_LEN)
1690 return ERR_PTR(-ENAMETOOLONG);
1692 ret = btrfs_inode_by_name(dir, dentry, &location);
1695 return ERR_PTR(ret);
1698 if (location.objectid) {
1699 ret = fixup_tree_root_location(root, &location, &sub_root,
1702 return ERR_PTR(ret);
1704 return ERR_PTR(-ENOENT);
1705 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1708 return ERR_PTR(-EACCES);
1709 if (inode->i_state & I_NEW) {
1710 /* the inode and parent dir are two different roots */
1711 if (sub_root != root) {
1713 sub_root->inode = inode;
1715 BTRFS_I(inode)->root = sub_root;
1716 memcpy(&BTRFS_I(inode)->location, &location,
1718 btrfs_read_locked_inode(inode);
1719 unlock_new_inode(inode);
1722 return d_splice_alias(inode, dentry);
1725 static unsigned char btrfs_filetype_table[] = {
1726 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1729 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1731 struct inode *inode = filp->f_dentry->d_inode;
1732 struct btrfs_root *root = BTRFS_I(inode)->root;
1733 struct btrfs_item *item;
1734 struct btrfs_dir_item *di;
1735 struct btrfs_key key;
1736 struct btrfs_key found_key;
1737 struct btrfs_path *path;
1740 struct extent_buffer *leaf;
1743 unsigned char d_type;
1748 int key_type = BTRFS_DIR_INDEX_KEY;
1753 /* FIXME, use a real flag for deciding about the key type */
1754 if (root->fs_info->tree_root == root)
1755 key_type = BTRFS_DIR_ITEM_KEY;
1757 /* special case for "." */
1758 if (filp->f_pos == 0) {
1759 over = filldir(dirent, ".", 1,
1767 key.objectid = inode->i_ino;
1768 path = btrfs_alloc_path();
1771 /* special case for .., just use the back ref */
1772 if (filp->f_pos == 1) {
1773 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1777 leaf = path->nodes[0];
1778 slot = path->slots[0];
1779 nritems = btrfs_header_nritems(leaf);
1780 if (slot >= nritems) {
1781 btrfs_release_path(root, path);
1782 goto read_dir_items;
1784 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1785 btrfs_release_path(root, path);
1786 if (found_key.objectid != key.objectid ||
1787 found_key.type != BTRFS_INODE_REF_KEY)
1788 goto read_dir_items;
1789 over = filldir(dirent, "..", 2,
1790 2, found_key.offset, DT_DIR);
1797 btrfs_set_key_type(&key, key_type);
1798 key.offset = filp->f_pos;
1800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1805 leaf = path->nodes[0];
1806 nritems = btrfs_header_nritems(leaf);
1807 slot = path->slots[0];
1808 if (advance || slot >= nritems) {
1809 if (slot >= nritems -1) {
1810 ret = btrfs_next_leaf(root, path);
1813 leaf = path->nodes[0];
1814 nritems = btrfs_header_nritems(leaf);
1815 slot = path->slots[0];
1822 item = btrfs_item_nr(leaf, slot);
1823 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1825 if (found_key.objectid != key.objectid)
1827 if (btrfs_key_type(&found_key) != key_type)
1829 if (found_key.offset < filp->f_pos)
1832 filp->f_pos = found_key.offset;
1834 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1836 di_total = btrfs_item_size(leaf, item);
1837 while(di_cur < di_total) {
1838 struct btrfs_key location;
1840 name_len = btrfs_dir_name_len(leaf, di);
1841 if (name_len < 32) {
1842 name_ptr = tmp_name;
1844 name_ptr = kmalloc(name_len, GFP_NOFS);
1847 read_extent_buffer(leaf, name_ptr,
1848 (unsigned long)(di + 1), name_len);
1850 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1851 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1852 over = filldir(dirent, name_ptr, name_len,
1857 if (name_ptr != tmp_name)
1862 di_len = btrfs_dir_name_len(leaf, di) +
1863 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1865 di = (struct btrfs_dir_item *)((char *)di + di_len);
1868 if (key_type == BTRFS_DIR_INDEX_KEY)
1869 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1875 btrfs_free_path(path);
1879 int btrfs_write_inode(struct inode *inode, int wait)
1881 struct btrfs_root *root = BTRFS_I(inode)->root;
1882 struct btrfs_trans_handle *trans;
1886 trans = btrfs_join_transaction(root, 1);
1887 btrfs_set_trans_block_group(trans, inode);
1888 ret = btrfs_commit_transaction(trans, root);
1894 * This is somewhat expensive, updating the tree every time the
1895 * inode changes. But, it is most likely to find the inode in cache.
1896 * FIXME, needs more benchmarking...there are no reasons other than performance
1897 * to keep or drop this code.
1899 void btrfs_dirty_inode(struct inode *inode)
1901 struct btrfs_root *root = BTRFS_I(inode)->root;
1902 struct btrfs_trans_handle *trans;
1904 trans = btrfs_join_transaction(root, 1);
1905 btrfs_set_trans_block_group(trans, inode);
1906 btrfs_update_inode(trans, root, inode);
1907 btrfs_end_transaction(trans, root);
1910 static int btrfs_set_inode_index_count(struct inode *inode)
1912 struct btrfs_root *root = BTRFS_I(inode)->root;
1913 struct btrfs_key key, found_key;
1914 struct btrfs_path *path;
1915 struct extent_buffer *leaf;
1918 key.objectid = inode->i_ino;
1919 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
1920 key.offset = (u64)-1;
1922 path = btrfs_alloc_path();
1926 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1929 /* FIXME: we should be able to handle this */
1935 * MAGIC NUMBER EXPLANATION:
1936 * since we search a directory based on f_pos we have to start at 2
1937 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
1938 * else has to start at 2
1940 if (path->slots[0] == 0) {
1941 BTRFS_I(inode)->index_cnt = 2;
1947 leaf = path->nodes[0];
1948 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1950 if (found_key.objectid != inode->i_ino ||
1951 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
1952 BTRFS_I(inode)->index_cnt = 2;
1956 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
1958 btrfs_free_path(path);
1962 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
1966 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
1967 ret = btrfs_set_inode_index_count(dir);
1972 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
1973 BTRFS_I(dir)->index_cnt++;
1978 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
1981 const char *name, int name_len,
1984 struct btrfs_block_group_cache *group,
1987 struct inode *inode;
1988 struct btrfs_inode_item *inode_item;
1989 struct btrfs_block_group_cache *new_inode_group;
1990 struct btrfs_key *location;
1991 struct btrfs_path *path;
1992 struct btrfs_inode_ref *ref;
1993 struct btrfs_key key[2];
1999 path = btrfs_alloc_path();
2002 inode = new_inode(root->fs_info->sb);
2004 return ERR_PTR(-ENOMEM);
2007 ret = btrfs_set_inode_index(dir, inode);
2009 return ERR_PTR(ret);
2011 BTRFS_I(inode)->index = 0;
2014 * index_cnt is ignored for everything but a dir,
2015 * btrfs_get_inode_index_count has an explanation for the magic
2018 BTRFS_I(inode)->index_cnt = 2;
2020 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2021 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2022 inode->i_mapping, GFP_NOFS);
2023 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2024 inode->i_mapping, GFP_NOFS);
2025 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2026 mutex_init(&BTRFS_I(inode)->csum_mutex);
2027 mutex_init(&BTRFS_I(inode)->extent_mutex);
2028 BTRFS_I(inode)->delalloc_bytes = 0;
2029 BTRFS_I(inode)->disk_i_size = 0;
2030 BTRFS_I(inode)->root = root;
2036 new_inode_group = btrfs_find_block_group(root, group, 0,
2037 BTRFS_BLOCK_GROUP_METADATA, owner);
2038 if (!new_inode_group) {
2039 printk("find_block group failed\n");
2040 new_inode_group = group;
2042 BTRFS_I(inode)->block_group = new_inode_group;
2043 BTRFS_I(inode)->flags = 0;
2045 key[0].objectid = objectid;
2046 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2049 key[1].objectid = objectid;
2050 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2051 key[1].offset = ref_objectid;
2053 sizes[0] = sizeof(struct btrfs_inode_item);
2054 sizes[1] = name_len + sizeof(*ref);
2056 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2060 if (objectid > root->highest_inode)
2061 root->highest_inode = objectid;
2063 inode->i_uid = current->fsuid;
2064 inode->i_gid = current->fsgid;
2065 inode->i_mode = mode;
2066 inode->i_ino = objectid;
2067 inode->i_blocks = 0;
2068 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2069 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2070 struct btrfs_inode_item);
2071 fill_inode_item(path->nodes[0], inode_item, inode);
2073 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2074 struct btrfs_inode_ref);
2075 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2076 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
2077 ptr = (unsigned long)(ref + 1);
2078 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2080 btrfs_mark_buffer_dirty(path->nodes[0]);
2081 btrfs_free_path(path);
2083 location = &BTRFS_I(inode)->location;
2084 location->objectid = objectid;
2085 location->offset = 0;
2086 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2088 insert_inode_hash(inode);
2092 BTRFS_I(dir)->index_cnt--;
2093 btrfs_free_path(path);
2094 return ERR_PTR(ret);
2097 static inline u8 btrfs_inode_type(struct inode *inode)
2099 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2102 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2103 struct dentry *dentry, struct inode *inode,
2107 struct btrfs_key key;
2108 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2109 struct inode *parent_inode = dentry->d_parent->d_inode;
2111 key.objectid = inode->i_ino;
2112 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2115 ret = btrfs_insert_dir_item(trans, root,
2116 dentry->d_name.name, dentry->d_name.len,
2117 dentry->d_parent->d_inode->i_ino,
2118 &key, btrfs_inode_type(inode),
2119 BTRFS_I(inode)->index);
2122 ret = btrfs_insert_inode_ref(trans, root,
2123 dentry->d_name.name,
2126 parent_inode->i_ino,
2127 BTRFS_I(inode)->index);
2129 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2130 dentry->d_name.len * 2);
2131 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2132 ret = btrfs_update_inode(trans, root,
2133 dentry->d_parent->d_inode);
2138 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2139 struct dentry *dentry, struct inode *inode,
2142 int err = btrfs_add_link(trans, dentry, inode, backref);
2144 d_instantiate(dentry, inode);
2152 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2153 int mode, dev_t rdev)
2155 struct btrfs_trans_handle *trans;
2156 struct btrfs_root *root = BTRFS_I(dir)->root;
2157 struct inode *inode = NULL;
2161 unsigned long nr = 0;
2163 if (!new_valid_dev(rdev))
2166 err = btrfs_check_free_space(root, 1, 0);
2170 trans = btrfs_start_transaction(root, 1);
2171 btrfs_set_trans_block_group(trans, dir);
2173 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2179 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2181 dentry->d_parent->d_inode->i_ino, objectid,
2182 BTRFS_I(dir)->block_group, mode);
2183 err = PTR_ERR(inode);
2187 btrfs_set_trans_block_group(trans, inode);
2188 err = btrfs_add_nondir(trans, dentry, inode, 0);
2192 inode->i_op = &btrfs_special_inode_operations;
2193 init_special_inode(inode, inode->i_mode, rdev);
2194 btrfs_update_inode(trans, root, inode);
2196 dir->i_sb->s_dirt = 1;
2197 btrfs_update_inode_block_group(trans, inode);
2198 btrfs_update_inode_block_group(trans, dir);
2200 nr = trans->blocks_used;
2201 btrfs_end_transaction_throttle(trans, root);
2204 inode_dec_link_count(inode);
2207 btrfs_btree_balance_dirty(root, nr);
2211 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2212 int mode, struct nameidata *nd)
2214 struct btrfs_trans_handle *trans;
2215 struct btrfs_root *root = BTRFS_I(dir)->root;
2216 struct inode *inode = NULL;
2219 unsigned long nr = 0;
2222 err = btrfs_check_free_space(root, 1, 0);
2225 trans = btrfs_start_transaction(root, 1);
2226 btrfs_set_trans_block_group(trans, dir);
2228 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2234 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2236 dentry->d_parent->d_inode->i_ino,
2237 objectid, BTRFS_I(dir)->block_group, mode);
2238 err = PTR_ERR(inode);
2242 btrfs_set_trans_block_group(trans, inode);
2243 err = btrfs_add_nondir(trans, dentry, inode, 0);
2247 inode->i_mapping->a_ops = &btrfs_aops;
2248 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2249 inode->i_fop = &btrfs_file_operations;
2250 inode->i_op = &btrfs_file_inode_operations;
2251 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2252 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2253 inode->i_mapping, GFP_NOFS);
2254 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2255 inode->i_mapping, GFP_NOFS);
2256 mutex_init(&BTRFS_I(inode)->csum_mutex);
2257 mutex_init(&BTRFS_I(inode)->extent_mutex);
2258 BTRFS_I(inode)->delalloc_bytes = 0;
2259 BTRFS_I(inode)->disk_i_size = 0;
2260 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2261 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2263 dir->i_sb->s_dirt = 1;
2264 btrfs_update_inode_block_group(trans, inode);
2265 btrfs_update_inode_block_group(trans, dir);
2267 nr = trans->blocks_used;
2268 btrfs_end_transaction_throttle(trans, root);
2271 inode_dec_link_count(inode);
2274 btrfs_btree_balance_dirty(root, nr);
2278 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2279 struct dentry *dentry)
2281 struct btrfs_trans_handle *trans;
2282 struct btrfs_root *root = BTRFS_I(dir)->root;
2283 struct inode *inode = old_dentry->d_inode;
2284 unsigned long nr = 0;
2288 if (inode->i_nlink == 0)
2291 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2296 err = btrfs_check_free_space(root, 1, 0);
2299 err = btrfs_set_inode_index(dir, inode);
2303 trans = btrfs_start_transaction(root, 1);
2305 btrfs_set_trans_block_group(trans, dir);
2306 atomic_inc(&inode->i_count);
2308 err = btrfs_add_nondir(trans, dentry, inode, 1);
2313 dir->i_sb->s_dirt = 1;
2314 btrfs_update_inode_block_group(trans, dir);
2315 err = btrfs_update_inode(trans, root, inode);
2320 nr = trans->blocks_used;
2321 btrfs_end_transaction_throttle(trans, root);
2324 inode_dec_link_count(inode);
2327 btrfs_btree_balance_dirty(root, nr);
2331 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2333 struct inode *inode = NULL;
2334 struct btrfs_trans_handle *trans;
2335 struct btrfs_root *root = BTRFS_I(dir)->root;
2337 int drop_on_err = 0;
2339 unsigned long nr = 1;
2341 err = btrfs_check_free_space(root, 1, 0);
2345 trans = btrfs_start_transaction(root, 1);
2346 btrfs_set_trans_block_group(trans, dir);
2348 if (IS_ERR(trans)) {
2349 err = PTR_ERR(trans);
2353 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2359 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2361 dentry->d_parent->d_inode->i_ino, objectid,
2362 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2363 if (IS_ERR(inode)) {
2364 err = PTR_ERR(inode);
2369 inode->i_op = &btrfs_dir_inode_operations;
2370 inode->i_fop = &btrfs_dir_file_operations;
2371 btrfs_set_trans_block_group(trans, inode);
2373 btrfs_i_size_write(inode, 0);
2374 err = btrfs_update_inode(trans, root, inode);
2378 err = btrfs_add_link(trans, dentry, inode, 0);
2382 d_instantiate(dentry, inode);
2384 dir->i_sb->s_dirt = 1;
2385 btrfs_update_inode_block_group(trans, inode);
2386 btrfs_update_inode_block_group(trans, dir);
2389 nr = trans->blocks_used;
2390 btrfs_end_transaction_throttle(trans, root);
2395 btrfs_btree_balance_dirty(root, nr);
2399 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2400 struct extent_map *existing,
2401 struct extent_map *em,
2402 u64 map_start, u64 map_len)
2406 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2407 start_diff = map_start - em->start;
2408 em->start = map_start;
2410 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2411 em->block_start += start_diff;
2412 return add_extent_mapping(em_tree, em);
2415 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2416 size_t pg_offset, u64 start, u64 len,
2422 u64 extent_start = 0;
2424 u64 objectid = inode->i_ino;
2426 struct btrfs_path *path = NULL;
2427 struct btrfs_root *root = BTRFS_I(inode)->root;
2428 struct btrfs_file_extent_item *item;
2429 struct extent_buffer *leaf;
2430 struct btrfs_key found_key;
2431 struct extent_map *em = NULL;
2432 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2433 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2434 struct btrfs_trans_handle *trans = NULL;
2437 spin_lock(&em_tree->lock);
2438 em = lookup_extent_mapping(em_tree, start, len);
2440 em->bdev = root->fs_info->fs_devices->latest_bdev;
2441 spin_unlock(&em_tree->lock);
2444 if (em->start > start || em->start + em->len <= start)
2445 free_extent_map(em);
2446 else if (em->block_start == EXTENT_MAP_INLINE && page)
2447 free_extent_map(em);
2451 em = alloc_extent_map(GFP_NOFS);
2456 em->bdev = root->fs_info->fs_devices->latest_bdev;
2457 em->start = EXTENT_MAP_HOLE;
2461 path = btrfs_alloc_path();
2465 ret = btrfs_lookup_file_extent(trans, root, path,
2466 objectid, start, trans != NULL);
2473 if (path->slots[0] == 0)
2478 leaf = path->nodes[0];
2479 item = btrfs_item_ptr(leaf, path->slots[0],
2480 struct btrfs_file_extent_item);
2481 /* are we inside the extent that was found? */
2482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2483 found_type = btrfs_key_type(&found_key);
2484 if (found_key.objectid != objectid ||
2485 found_type != BTRFS_EXTENT_DATA_KEY) {
2489 found_type = btrfs_file_extent_type(leaf, item);
2490 extent_start = found_key.offset;
2491 if (found_type == BTRFS_FILE_EXTENT_REG) {
2492 extent_end = extent_start +
2493 btrfs_file_extent_num_bytes(leaf, item);
2495 if (start < extent_start || start >= extent_end) {
2497 if (start < extent_start) {
2498 if (start + len <= extent_start)
2500 em->len = extent_end - extent_start;
2506 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2508 em->start = extent_start;
2509 em->len = extent_end - extent_start;
2510 em->block_start = EXTENT_MAP_HOLE;
2513 bytenr += btrfs_file_extent_offset(leaf, item);
2514 em->block_start = bytenr;
2515 em->start = extent_start;
2516 em->len = extent_end - extent_start;
2518 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2523 size_t extent_offset;
2526 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2528 extent_end = (extent_start + size + root->sectorsize - 1) &
2529 ~((u64)root->sectorsize - 1);
2530 if (start < extent_start || start >= extent_end) {
2532 if (start < extent_start) {
2533 if (start + len <= extent_start)
2535 em->len = extent_end - extent_start;
2541 em->block_start = EXTENT_MAP_INLINE;
2544 em->start = extent_start;
2549 page_start = page_offset(page) + pg_offset;
2550 extent_offset = page_start - extent_start;
2551 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2552 size - extent_offset);
2553 em->start = extent_start + extent_offset;
2554 em->len = (copy_size + root->sectorsize - 1) &
2555 ~((u64)root->sectorsize - 1);
2557 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2558 if (create == 0 && !PageUptodate(page)) {
2559 read_extent_buffer(leaf, map + pg_offset, ptr,
2561 flush_dcache_page(page);
2562 } else if (create && PageUptodate(page)) {
2565 free_extent_map(em);
2567 btrfs_release_path(root, path);
2568 trans = btrfs_join_transaction(root, 1);
2571 write_extent_buffer(leaf, map + pg_offset, ptr,
2573 btrfs_mark_buffer_dirty(leaf);
2576 set_extent_uptodate(io_tree, em->start,
2577 extent_map_end(em) - 1, GFP_NOFS);
2580 printk("unkknown found_type %d\n", found_type);
2587 em->block_start = EXTENT_MAP_HOLE;
2589 btrfs_release_path(root, path);
2590 if (em->start > start || extent_map_end(em) <= start) {
2591 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2597 spin_lock(&em_tree->lock);
2598 ret = add_extent_mapping(em_tree, em);
2599 /* it is possible that someone inserted the extent into the tree
2600 * while we had the lock dropped. It is also possible that
2601 * an overlapping map exists in the tree
2603 if (ret == -EEXIST) {
2604 struct extent_map *existing;
2608 existing = lookup_extent_mapping(em_tree, start, len);
2609 if (existing && (existing->start > start ||
2610 existing->start + existing->len <= start)) {
2611 free_extent_map(existing);
2615 existing = lookup_extent_mapping(em_tree, em->start,
2618 err = merge_extent_mapping(em_tree, existing,
2621 free_extent_map(existing);
2623 free_extent_map(em);
2628 printk("failing to insert %Lu %Lu\n",
2630 free_extent_map(em);
2634 free_extent_map(em);
2639 spin_unlock(&em_tree->lock);
2642 btrfs_free_path(path);
2644 ret = btrfs_end_transaction(trans, root);
2650 free_extent_map(em);
2652 return ERR_PTR(err);
2657 #if 0 /* waiting for O_DIRECT reads */
2658 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2659 struct buffer_head *bh_result, int create)
2661 struct extent_map *em;
2662 u64 start = (u64)iblock << inode->i_blkbits;
2663 struct btrfs_multi_bio *multi = NULL;
2664 struct btrfs_root *root = BTRFS_I(inode)->root;
2670 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2672 if (!em || IS_ERR(em))
2675 if (em->start > start || em->start + em->len <= start) {
2679 if (em->block_start == EXTENT_MAP_INLINE) {
2684 len = em->start + em->len - start;
2685 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2687 if (em->block_start == EXTENT_MAP_HOLE ||
2688 em->block_start == EXTENT_MAP_DELALLOC) {
2689 bh_result->b_size = len;
2693 logical = start - em->start;
2694 logical = em->block_start + logical;
2697 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2698 logical, &map_length, &multi, 0);
2700 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2701 bh_result->b_size = min(map_length, len);
2703 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2704 set_buffer_mapped(bh_result);
2707 free_extent_map(em);
2712 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2713 const struct iovec *iov, loff_t offset,
2714 unsigned long nr_segs)
2718 struct file *file = iocb->ki_filp;
2719 struct inode *inode = file->f_mapping->host;
2724 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2725 offset, nr_segs, btrfs_get_block, NULL);
2729 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2731 return extent_bmap(mapping, iblock, btrfs_get_extent);
2734 int btrfs_readpage(struct file *file, struct page *page)
2736 struct extent_io_tree *tree;
2737 tree = &BTRFS_I(page->mapping->host)->io_tree;
2738 return extent_read_full_page(tree, page, btrfs_get_extent);
2741 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2743 struct extent_io_tree *tree;
2746 if (current->flags & PF_MEMALLOC) {
2747 redirty_page_for_writepage(wbc, page);
2751 tree = &BTRFS_I(page->mapping->host)->io_tree;
2752 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2755 int btrfs_writepages(struct address_space *mapping,
2756 struct writeback_control *wbc)
2758 struct extent_io_tree *tree;
2759 tree = &BTRFS_I(mapping->host)->io_tree;
2760 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2764 btrfs_readpages(struct file *file, struct address_space *mapping,
2765 struct list_head *pages, unsigned nr_pages)
2767 struct extent_io_tree *tree;
2768 tree = &BTRFS_I(mapping->host)->io_tree;
2769 return extent_readpages(tree, mapping, pages, nr_pages,
2772 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2774 struct extent_io_tree *tree;
2775 struct extent_map_tree *map;
2778 tree = &BTRFS_I(page->mapping->host)->io_tree;
2779 map = &BTRFS_I(page->mapping->host)->extent_tree;
2780 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2782 ClearPagePrivate(page);
2783 set_page_private(page, 0);
2784 page_cache_release(page);
2789 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2791 return __btrfs_releasepage(page, gfp_flags);
2794 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2796 struct extent_io_tree *tree;
2797 struct btrfs_ordered_extent *ordered;
2798 u64 page_start = page_offset(page);
2799 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2801 wait_on_page_writeback(page);
2802 tree = &BTRFS_I(page->mapping->host)->io_tree;
2804 btrfs_releasepage(page, GFP_NOFS);
2808 lock_extent(tree, page_start, page_end, GFP_NOFS);
2809 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2813 * IO on this page will never be started, so we need
2814 * to account for any ordered extents now
2816 clear_extent_bit(tree, page_start, page_end,
2817 EXTENT_DIRTY | EXTENT_DELALLOC |
2818 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2819 btrfs_finish_ordered_io(page->mapping->host,
2820 page_start, page_end);
2821 btrfs_put_ordered_extent(ordered);
2822 lock_extent(tree, page_start, page_end, GFP_NOFS);
2824 clear_extent_bit(tree, page_start, page_end,
2825 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2828 __btrfs_releasepage(page, GFP_NOFS);
2830 ClearPageChecked(page);
2831 if (PagePrivate(page)) {
2832 ClearPagePrivate(page);
2833 set_page_private(page, 0);
2834 page_cache_release(page);
2839 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2840 * called from a page fault handler when a page is first dirtied. Hence we must
2841 * be careful to check for EOF conditions here. We set the page up correctly
2842 * for a written page which means we get ENOSPC checking when writing into
2843 * holes and correct delalloc and unwritten extent mapping on filesystems that
2844 * support these features.
2846 * We are not allowed to take the i_mutex here so we have to play games to
2847 * protect against truncate races as the page could now be beyond EOF. Because
2848 * vmtruncate() writes the inode size before removing pages, once we have the
2849 * page lock we can determine safely if the page is beyond EOF. If it is not
2850 * beyond EOF, then the page is guaranteed safe against truncation until we
2853 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2855 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2856 struct btrfs_root *root = BTRFS_I(inode)->root;
2857 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2858 struct btrfs_ordered_extent *ordered;
2860 unsigned long zero_start;
2866 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2873 size = i_size_read(inode);
2874 page_start = page_offset(page);
2875 page_end = page_start + PAGE_CACHE_SIZE - 1;
2877 if ((page->mapping != inode->i_mapping) ||
2878 (page_start >= size)) {
2879 /* page got truncated out from underneath us */
2882 wait_on_page_writeback(page);
2884 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2885 set_page_extent_mapped(page);
2888 * we can't set the delalloc bits if there are pending ordered
2889 * extents. Drop our locks and wait for them to finish
2891 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2893 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2895 btrfs_start_ordered_extent(inode, ordered, 1);
2896 btrfs_put_ordered_extent(ordered);
2900 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2901 page_end, GFP_NOFS);
2904 /* page is wholly or partially inside EOF */
2905 if (page_start + PAGE_CACHE_SIZE > size)
2906 zero_start = size & ~PAGE_CACHE_MASK;
2908 zero_start = PAGE_CACHE_SIZE;
2910 if (zero_start != PAGE_CACHE_SIZE) {
2912 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2913 flush_dcache_page(page);
2916 ClearPageChecked(page);
2917 set_page_dirty(page);
2918 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2926 static void btrfs_truncate(struct inode *inode)
2928 struct btrfs_root *root = BTRFS_I(inode)->root;
2930 struct btrfs_trans_handle *trans;
2932 u64 mask = root->sectorsize - 1;
2934 if (!S_ISREG(inode->i_mode))
2936 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2939 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2940 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2942 trans = btrfs_start_transaction(root, 1);
2943 btrfs_set_trans_block_group(trans, inode);
2944 btrfs_i_size_write(inode, inode->i_size);
2946 /* FIXME, add redo link to tree so we don't leak on crash */
2947 ret = btrfs_truncate_in_trans(trans, root, inode,
2948 BTRFS_EXTENT_DATA_KEY);
2949 btrfs_update_inode(trans, root, inode);
2950 nr = trans->blocks_used;
2952 ret = btrfs_end_transaction_throttle(trans, root);
2954 btrfs_btree_balance_dirty(root, nr);
2958 * Invalidate a single dcache entry at the root of the filesystem.
2959 * Needed after creation of snapshot or subvolume.
2961 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2964 struct dentry *alias, *entry;
2967 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2971 /* change me if btrfs ever gets a d_hash operation */
2972 qstr.hash = full_name_hash(qstr.name, qstr.len);
2973 entry = d_lookup(alias, &qstr);
2976 d_invalidate(entry);
2982 int btrfs_create_subvol_root(struct btrfs_root *new_root,
2983 struct btrfs_trans_handle *trans, u64 new_dirid,
2984 struct btrfs_block_group_cache *block_group)
2986 struct inode *inode;
2988 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
2989 new_dirid, block_group, S_IFDIR | 0700);
2991 return PTR_ERR(inode);
2992 inode->i_op = &btrfs_dir_inode_operations;
2993 inode->i_fop = &btrfs_dir_file_operations;
2994 new_root->inode = inode;
2997 btrfs_i_size_write(inode, 0);
2999 return btrfs_update_inode(trans, new_root, inode);
3002 unsigned long btrfs_force_ra(struct address_space *mapping,
3003 struct file_ra_state *ra, struct file *file,
3004 pgoff_t offset, pgoff_t last_index)
3006 pgoff_t req_size = last_index - offset + 1;
3008 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3009 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3012 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3013 return offset + req_size;
3017 struct inode *btrfs_alloc_inode(struct super_block *sb)
3019 struct btrfs_inode *ei;
3021 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3025 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3026 return &ei->vfs_inode;
3029 void btrfs_destroy_inode(struct inode *inode)
3031 struct btrfs_ordered_extent *ordered;
3032 WARN_ON(!list_empty(&inode->i_dentry));
3033 WARN_ON(inode->i_data.nrpages);
3036 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3040 printk("found ordered extent %Lu %Lu\n",
3041 ordered->file_offset, ordered->len);
3042 btrfs_remove_ordered_extent(inode, ordered);
3043 btrfs_put_ordered_extent(ordered);
3044 btrfs_put_ordered_extent(ordered);
3047 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3048 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3051 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3052 static void init_once(struct kmem_cache * cachep, void *foo)
3054 static void init_once(void * foo, struct kmem_cache * cachep,
3055 unsigned long flags)
3058 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3060 inode_init_once(&ei->vfs_inode);
3063 void btrfs_destroy_cachep(void)
3065 if (btrfs_inode_cachep)
3066 kmem_cache_destroy(btrfs_inode_cachep);
3067 if (btrfs_trans_handle_cachep)
3068 kmem_cache_destroy(btrfs_trans_handle_cachep);
3069 if (btrfs_transaction_cachep)
3070 kmem_cache_destroy(btrfs_transaction_cachep);
3071 if (btrfs_bit_radix_cachep)
3072 kmem_cache_destroy(btrfs_bit_radix_cachep);
3073 if (btrfs_path_cachep)
3074 kmem_cache_destroy(btrfs_path_cachep);
3077 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3078 unsigned long extra_flags,
3079 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3080 void (*ctor)(struct kmem_cache *, void *)
3082 void (*ctor)(void *, struct kmem_cache *,
3087 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3088 SLAB_MEM_SPREAD | extra_flags), ctor
3089 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3095 int btrfs_init_cachep(void)
3097 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3098 sizeof(struct btrfs_inode),
3100 if (!btrfs_inode_cachep)
3102 btrfs_trans_handle_cachep =
3103 btrfs_cache_create("btrfs_trans_handle_cache",
3104 sizeof(struct btrfs_trans_handle),
3106 if (!btrfs_trans_handle_cachep)
3108 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3109 sizeof(struct btrfs_transaction),
3111 if (!btrfs_transaction_cachep)
3113 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3114 sizeof(struct btrfs_path),
3116 if (!btrfs_path_cachep)
3118 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3119 SLAB_DESTROY_BY_RCU, NULL);
3120 if (!btrfs_bit_radix_cachep)
3124 btrfs_destroy_cachep();
3128 static int btrfs_getattr(struct vfsmount *mnt,
3129 struct dentry *dentry, struct kstat *stat)
3131 struct inode *inode = dentry->d_inode;
3132 generic_fillattr(inode, stat);
3133 stat->blksize = PAGE_CACHE_SIZE;
3134 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3138 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3139 struct inode * new_dir,struct dentry *new_dentry)
3141 struct btrfs_trans_handle *trans;
3142 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3143 struct inode *new_inode = new_dentry->d_inode;
3144 struct inode *old_inode = old_dentry->d_inode;
3145 struct timespec ctime = CURRENT_TIME;
3148 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3149 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3153 ret = btrfs_check_free_space(root, 1, 0);
3157 trans = btrfs_start_transaction(root, 1);
3159 btrfs_set_trans_block_group(trans, new_dir);
3161 old_dentry->d_inode->i_nlink++;
3162 old_dir->i_ctime = old_dir->i_mtime = ctime;
3163 new_dir->i_ctime = new_dir->i_mtime = ctime;
3164 old_inode->i_ctime = ctime;
3166 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3171 new_inode->i_ctime = CURRENT_TIME;
3172 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3176 ret = btrfs_set_inode_index(new_dir, old_inode);
3180 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3185 btrfs_end_transaction(trans, root);
3190 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3191 const char *symname)
3193 struct btrfs_trans_handle *trans;
3194 struct btrfs_root *root = BTRFS_I(dir)->root;
3195 struct btrfs_path *path;
3196 struct btrfs_key key;
3197 struct inode *inode = NULL;
3204 struct btrfs_file_extent_item *ei;
3205 struct extent_buffer *leaf;
3206 unsigned long nr = 0;
3208 name_len = strlen(symname) + 1;
3209 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3210 return -ENAMETOOLONG;
3212 err = btrfs_check_free_space(root, 1, 0);
3216 trans = btrfs_start_transaction(root, 1);
3217 btrfs_set_trans_block_group(trans, dir);
3219 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3225 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3227 dentry->d_parent->d_inode->i_ino, objectid,
3228 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3229 err = PTR_ERR(inode);
3233 btrfs_set_trans_block_group(trans, inode);
3234 err = btrfs_add_nondir(trans, dentry, inode, 0);
3238 inode->i_mapping->a_ops = &btrfs_aops;
3239 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3240 inode->i_fop = &btrfs_file_operations;
3241 inode->i_op = &btrfs_file_inode_operations;
3242 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3243 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3244 inode->i_mapping, GFP_NOFS);
3245 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3246 inode->i_mapping, GFP_NOFS);
3247 mutex_init(&BTRFS_I(inode)->csum_mutex);
3248 mutex_init(&BTRFS_I(inode)->extent_mutex);
3249 BTRFS_I(inode)->delalloc_bytes = 0;
3250 BTRFS_I(inode)->disk_i_size = 0;
3251 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3252 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3254 dir->i_sb->s_dirt = 1;
3255 btrfs_update_inode_block_group(trans, inode);
3256 btrfs_update_inode_block_group(trans, dir);
3260 path = btrfs_alloc_path();
3262 key.objectid = inode->i_ino;
3264 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3265 datasize = btrfs_file_extent_calc_inline_size(name_len);
3266 err = btrfs_insert_empty_item(trans, root, path, &key,
3272 leaf = path->nodes[0];
3273 ei = btrfs_item_ptr(leaf, path->slots[0],
3274 struct btrfs_file_extent_item);
3275 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3276 btrfs_set_file_extent_type(leaf, ei,
3277 BTRFS_FILE_EXTENT_INLINE);
3278 ptr = btrfs_file_extent_inline_start(ei);
3279 write_extent_buffer(leaf, symname, ptr, name_len);
3280 btrfs_mark_buffer_dirty(leaf);
3281 btrfs_free_path(path);
3283 inode->i_op = &btrfs_symlink_inode_operations;
3284 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3285 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3286 btrfs_i_size_write(inode, name_len - 1);
3287 err = btrfs_update_inode(trans, root, inode);
3292 nr = trans->blocks_used;
3293 btrfs_end_transaction_throttle(trans, root);
3296 inode_dec_link_count(inode);
3299 btrfs_btree_balance_dirty(root, nr);
3303 static int btrfs_set_page_dirty(struct page *page)
3305 return __set_page_dirty_nobuffers(page);
3308 static int btrfs_permission(struct inode *inode, int mask,
3309 struct nameidata *nd)
3311 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3313 return generic_permission(inode, mask, NULL);
3316 static struct inode_operations btrfs_dir_inode_operations = {
3317 .lookup = btrfs_lookup,
3318 .create = btrfs_create,
3319 .unlink = btrfs_unlink,
3321 .mkdir = btrfs_mkdir,
3322 .rmdir = btrfs_rmdir,
3323 .rename = btrfs_rename,
3324 .symlink = btrfs_symlink,
3325 .setattr = btrfs_setattr,
3326 .mknod = btrfs_mknod,
3327 .setxattr = generic_setxattr,
3328 .getxattr = generic_getxattr,
3329 .listxattr = btrfs_listxattr,
3330 .removexattr = generic_removexattr,
3331 .permission = btrfs_permission,
3333 static struct inode_operations btrfs_dir_ro_inode_operations = {
3334 .lookup = btrfs_lookup,
3335 .permission = btrfs_permission,
3337 static struct file_operations btrfs_dir_file_operations = {
3338 .llseek = generic_file_llseek,
3339 .read = generic_read_dir,
3340 .readdir = btrfs_readdir,
3341 .unlocked_ioctl = btrfs_ioctl,
3342 #ifdef CONFIG_COMPAT
3343 .compat_ioctl = btrfs_ioctl,
3345 .release = btrfs_release_file,
3348 static struct extent_io_ops btrfs_extent_io_ops = {
3349 .fill_delalloc = run_delalloc_range,
3350 .submit_bio_hook = btrfs_submit_bio_hook,
3351 .merge_bio_hook = btrfs_merge_bio_hook,
3352 .readpage_io_hook = btrfs_readpage_io_hook,
3353 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3354 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3355 .writepage_start_hook = btrfs_writepage_start_hook,
3356 .readpage_io_failed_hook = btrfs_io_failed_hook,
3357 .set_bit_hook = btrfs_set_bit_hook,
3358 .clear_bit_hook = btrfs_clear_bit_hook,
3361 static struct address_space_operations btrfs_aops = {
3362 .readpage = btrfs_readpage,
3363 .writepage = btrfs_writepage,
3364 .writepages = btrfs_writepages,
3365 .readpages = btrfs_readpages,
3366 .sync_page = block_sync_page,
3368 .direct_IO = btrfs_direct_IO,
3369 .invalidatepage = btrfs_invalidatepage,
3370 .releasepage = btrfs_releasepage,
3371 .set_page_dirty = btrfs_set_page_dirty,
3374 static struct address_space_operations btrfs_symlink_aops = {
3375 .readpage = btrfs_readpage,
3376 .writepage = btrfs_writepage,
3377 .invalidatepage = btrfs_invalidatepage,
3378 .releasepage = btrfs_releasepage,
3381 static struct inode_operations btrfs_file_inode_operations = {
3382 .truncate = btrfs_truncate,
3383 .getattr = btrfs_getattr,
3384 .setattr = btrfs_setattr,
3385 .setxattr = generic_setxattr,
3386 .getxattr = generic_getxattr,
3387 .listxattr = btrfs_listxattr,
3388 .removexattr = generic_removexattr,
3389 .permission = btrfs_permission,
3391 static struct inode_operations btrfs_special_inode_operations = {
3392 .getattr = btrfs_getattr,
3393 .setattr = btrfs_setattr,
3394 .permission = btrfs_permission,
3396 static struct inode_operations btrfs_symlink_inode_operations = {
3397 .readlink = generic_readlink,
3398 .follow_link = page_follow_link_light,
3399 .put_link = page_put_link,
3400 .permission = btrfs_permission,