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))
614 path = btrfs_alloc_path();
615 mutex_lock(&BTRFS_I(inode)->csum_mutex);
616 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
619 * It is possible there is an ordered extent that has
620 * not yet finished for this range in the file. If so,
621 * that extent will have a csum cached, and it will insert
622 * the sum after all the blocks in the extent are fully
623 * on disk. So, look for an ordered extent and use the
626 ret = btrfs_find_ordered_sum(inode, start, &csum);
631 /* a csum that isn't present is a preallocated region. */
632 if (ret == -ENOENT || ret == -EFBIG)
635 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
639 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
642 set_state_private(io_tree, start, csum);
644 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
646 btrfs_free_path(path);
650 struct io_failure_record {
658 int btrfs_io_failed_hook(struct bio *failed_bio,
659 struct page *page, u64 start, u64 end,
660 struct extent_state *state)
662 struct io_failure_record *failrec = NULL;
664 struct extent_map *em;
665 struct inode *inode = page->mapping->host;
666 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
667 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
674 ret = get_state_private(failure_tree, start, &private);
676 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
679 failrec->start = start;
680 failrec->len = end - start + 1;
681 failrec->last_mirror = 0;
683 spin_lock(&em_tree->lock);
684 em = lookup_extent_mapping(em_tree, start, failrec->len);
685 if (em->start > start || em->start + em->len < start) {
689 spin_unlock(&em_tree->lock);
691 if (!em || IS_ERR(em)) {
695 logical = start - em->start;
696 logical = em->block_start + logical;
697 failrec->logical = logical;
699 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
700 EXTENT_DIRTY, GFP_NOFS);
701 set_state_private(failure_tree, start,
702 (u64)(unsigned long)failrec);
704 failrec = (struct io_failure_record *)(unsigned long)private;
706 num_copies = btrfs_num_copies(
707 &BTRFS_I(inode)->root->fs_info->mapping_tree,
708 failrec->logical, failrec->len);
709 failrec->last_mirror++;
711 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
712 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
715 if (state && state->start != failrec->start)
717 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
719 if (!state || failrec->last_mirror > num_copies) {
720 set_state_private(failure_tree, failrec->start, 0);
721 clear_extent_bits(failure_tree, failrec->start,
722 failrec->start + failrec->len - 1,
723 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
727 bio = bio_alloc(GFP_NOFS, 1);
728 bio->bi_private = state;
729 bio->bi_end_io = failed_bio->bi_end_io;
730 bio->bi_sector = failrec->logical >> 9;
731 bio->bi_bdev = failed_bio->bi_bdev;
733 bio_add_page(bio, page, failrec->len, start - page_offset(page));
734 if (failed_bio->bi_rw & (1 << BIO_RW))
739 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
740 failrec->last_mirror);
744 int btrfs_clean_io_failures(struct inode *inode, u64 start)
748 struct io_failure_record *failure;
752 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
753 (u64)-1, 1, EXTENT_DIRTY)) {
754 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
755 start, &private_failure);
757 failure = (struct io_failure_record *)(unsigned long)
759 set_state_private(&BTRFS_I(inode)->io_failure_tree,
761 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
763 failure->start + failure->len - 1,
764 EXTENT_DIRTY | EXTENT_LOCKED,
772 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
773 struct extent_state *state)
775 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
776 struct inode *inode = page->mapping->host;
777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
779 u64 private = ~(u32)0;
781 struct btrfs_root *root = BTRFS_I(inode)->root;
785 if (btrfs_test_opt(root, NODATASUM) ||
786 btrfs_test_flag(inode, NODATASUM))
788 if (state && state->start == start) {
789 private = state->private;
792 ret = get_state_private(io_tree, start, &private);
794 local_irq_save(flags);
795 kaddr = kmap_atomic(page, KM_IRQ0);
799 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
800 btrfs_csum_final(csum, (char *)&csum);
801 if (csum != private) {
804 kunmap_atomic(kaddr, KM_IRQ0);
805 local_irq_restore(flags);
807 /* if the io failure tree for this inode is non-empty,
808 * check to see if we've recovered from a failed IO
810 btrfs_clean_io_failures(inode, start);
814 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
815 page->mapping->host->i_ino, (unsigned long long)start, csum,
817 memset(kaddr + offset, 1, end - start + 1);
818 flush_dcache_page(page);
819 kunmap_atomic(kaddr, KM_IRQ0);
820 local_irq_restore(flags);
826 void btrfs_read_locked_inode(struct inode *inode)
828 struct btrfs_path *path;
829 struct extent_buffer *leaf;
830 struct btrfs_inode_item *inode_item;
831 struct btrfs_timespec *tspec;
832 struct btrfs_root *root = BTRFS_I(inode)->root;
833 struct btrfs_key location;
834 u64 alloc_group_block;
838 path = btrfs_alloc_path();
840 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
842 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
846 leaf = path->nodes[0];
847 inode_item = btrfs_item_ptr(leaf, path->slots[0],
848 struct btrfs_inode_item);
850 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
851 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
852 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
853 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
854 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
856 tspec = btrfs_inode_atime(inode_item);
857 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
858 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
860 tspec = btrfs_inode_mtime(inode_item);
861 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
862 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
864 tspec = btrfs_inode_ctime(inode_item);
865 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
866 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
868 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
869 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
871 rdev = btrfs_inode_rdev(leaf, inode_item);
873 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
874 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
876 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
877 if (!BTRFS_I(inode)->block_group) {
878 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
880 BTRFS_BLOCK_GROUP_METADATA, 0);
882 btrfs_free_path(path);
885 switch (inode->i_mode & S_IFMT) {
887 inode->i_mapping->a_ops = &btrfs_aops;
888 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
889 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
890 inode->i_fop = &btrfs_file_operations;
891 inode->i_op = &btrfs_file_inode_operations;
894 inode->i_fop = &btrfs_dir_file_operations;
895 if (root == root->fs_info->tree_root)
896 inode->i_op = &btrfs_dir_ro_inode_operations;
898 inode->i_op = &btrfs_dir_inode_operations;
901 inode->i_op = &btrfs_symlink_inode_operations;
902 inode->i_mapping->a_ops = &btrfs_symlink_aops;
903 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
906 init_special_inode(inode, inode->i_mode, rdev);
912 btrfs_free_path(path);
913 make_bad_inode(inode);
916 static void fill_inode_item(struct extent_buffer *leaf,
917 struct btrfs_inode_item *item,
920 btrfs_set_inode_uid(leaf, item, inode->i_uid);
921 btrfs_set_inode_gid(leaf, item, inode->i_gid);
922 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
923 btrfs_set_inode_mode(leaf, item, inode->i_mode);
924 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
926 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
927 inode->i_atime.tv_sec);
928 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
929 inode->i_atime.tv_nsec);
931 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
932 inode->i_mtime.tv_sec);
933 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
934 inode->i_mtime.tv_nsec);
936 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
937 inode->i_ctime.tv_sec);
938 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
939 inode->i_ctime.tv_nsec);
941 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
942 btrfs_set_inode_generation(leaf, item, inode->i_generation);
943 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
944 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
945 btrfs_set_inode_block_group(leaf, item,
946 BTRFS_I(inode)->block_group->key.objectid);
949 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
950 struct btrfs_root *root,
953 struct btrfs_inode_item *inode_item;
954 struct btrfs_path *path;
955 struct extent_buffer *leaf;
958 path = btrfs_alloc_path();
960 ret = btrfs_lookup_inode(trans, root, path,
961 &BTRFS_I(inode)->location, 1);
968 leaf = path->nodes[0];
969 inode_item = btrfs_item_ptr(leaf, path->slots[0],
970 struct btrfs_inode_item);
972 fill_inode_item(leaf, inode_item, inode);
973 btrfs_mark_buffer_dirty(leaf);
974 btrfs_set_inode_last_trans(trans, inode);
977 btrfs_free_path(path);
982 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
983 struct btrfs_root *root,
985 struct dentry *dentry)
987 struct btrfs_path *path;
988 const char *name = dentry->d_name.name;
989 int name_len = dentry->d_name.len;
991 struct extent_buffer *leaf;
992 struct btrfs_dir_item *di;
993 struct btrfs_key key;
995 path = btrfs_alloc_path();
1001 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1002 name, name_len, -1);
1011 leaf = path->nodes[0];
1012 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1013 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1016 btrfs_release_path(root, path);
1018 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1019 key.objectid, name, name_len, -1);
1028 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1029 btrfs_release_path(root, path);
1031 dentry->d_inode->i_ctime = dir->i_ctime;
1032 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1033 dentry->d_inode->i_ino,
1034 dentry->d_parent->d_inode->i_ino);
1036 printk("failed to delete reference to %.*s, "
1037 "inode %lu parent %lu\n", name_len, name,
1038 dentry->d_inode->i_ino,
1039 dentry->d_parent->d_inode->i_ino);
1042 btrfs_free_path(path);
1044 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1045 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1046 btrfs_update_inode(trans, root, dir);
1047 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1048 dentry->d_inode->i_nlink--;
1050 drop_nlink(dentry->d_inode);
1052 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1053 dir->i_sb->s_dirt = 1;
1058 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1060 struct btrfs_root *root;
1061 struct btrfs_trans_handle *trans;
1063 unsigned long nr = 0;
1065 root = BTRFS_I(dir)->root;
1067 ret = btrfs_check_free_space(root, 1, 1);
1071 trans = btrfs_start_transaction(root, 1);
1073 btrfs_set_trans_block_group(trans, dir);
1074 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1075 nr = trans->blocks_used;
1077 btrfs_end_transaction_throttle(trans, root);
1079 btrfs_btree_balance_dirty(root, nr);
1083 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1085 struct inode *inode = dentry->d_inode;
1088 struct btrfs_root *root = BTRFS_I(dir)->root;
1089 struct btrfs_trans_handle *trans;
1090 unsigned long nr = 0;
1092 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1096 ret = btrfs_check_free_space(root, 1, 1);
1100 trans = btrfs_start_transaction(root, 1);
1101 btrfs_set_trans_block_group(trans, dir);
1103 /* now the directory is empty */
1104 err = btrfs_unlink_trans(trans, root, dir, dentry);
1106 btrfs_i_size_write(inode, 0);
1109 nr = trans->blocks_used;
1110 ret = btrfs_end_transaction_throttle(trans, root);
1112 btrfs_btree_balance_dirty(root, nr);
1120 * this can truncate away extent items, csum items and directory items.
1121 * It starts at a high offset and removes keys until it can't find
1122 * any higher than i_size.
1124 * csum items that cross the new i_size are truncated to the new size
1127 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1128 struct btrfs_root *root,
1129 struct inode *inode,
1133 struct btrfs_path *path;
1134 struct btrfs_key key;
1135 struct btrfs_key found_key;
1137 struct extent_buffer *leaf;
1138 struct btrfs_file_extent_item *fi;
1139 u64 extent_start = 0;
1140 u64 extent_num_bytes = 0;
1146 int pending_del_nr = 0;
1147 int pending_del_slot = 0;
1148 int extent_type = -1;
1149 u64 mask = root->sectorsize - 1;
1151 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1152 path = btrfs_alloc_path();
1156 /* FIXME, add redo link to tree so we don't leak on crash */
1157 key.objectid = inode->i_ino;
1158 key.offset = (u64)-1;
1161 btrfs_init_path(path);
1163 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1168 BUG_ON(path->slots[0] == 0);
1174 leaf = path->nodes[0];
1175 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1176 found_type = btrfs_key_type(&found_key);
1178 if (found_key.objectid != inode->i_ino)
1181 if (found_type < min_type)
1184 item_end = found_key.offset;
1185 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1186 fi = btrfs_item_ptr(leaf, path->slots[0],
1187 struct btrfs_file_extent_item);
1188 extent_type = btrfs_file_extent_type(leaf, fi);
1189 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1191 btrfs_file_extent_num_bytes(leaf, fi);
1192 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1193 struct btrfs_item *item = btrfs_item_nr(leaf,
1195 item_end += btrfs_file_extent_inline_len(leaf,
1200 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1201 ret = btrfs_csum_truncate(trans, root, path,
1205 if (item_end < inode->i_size) {
1206 if (found_type == BTRFS_DIR_ITEM_KEY) {
1207 found_type = BTRFS_INODE_ITEM_KEY;
1208 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1209 found_type = BTRFS_CSUM_ITEM_KEY;
1210 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1211 found_type = BTRFS_XATTR_ITEM_KEY;
1212 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1213 found_type = BTRFS_INODE_REF_KEY;
1214 } else if (found_type) {
1219 btrfs_set_key_type(&key, found_type);
1222 if (found_key.offset >= inode->i_size)
1228 /* FIXME, shrink the extent if the ref count is only 1 */
1229 if (found_type != BTRFS_EXTENT_DATA_KEY)
1232 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1234 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1236 u64 orig_num_bytes =
1237 btrfs_file_extent_num_bytes(leaf, fi);
1238 extent_num_bytes = inode->i_size -
1239 found_key.offset + root->sectorsize - 1;
1240 extent_num_bytes = extent_num_bytes &
1241 ~((u64)root->sectorsize - 1);
1242 btrfs_set_file_extent_num_bytes(leaf, fi,
1244 num_dec = (orig_num_bytes -
1246 if (extent_start != 0)
1247 dec_i_blocks(inode, num_dec);
1248 btrfs_mark_buffer_dirty(leaf);
1251 btrfs_file_extent_disk_num_bytes(leaf,
1253 /* FIXME blocksize != 4096 */
1254 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1255 if (extent_start != 0) {
1257 dec_i_blocks(inode, num_dec);
1259 root_gen = btrfs_header_generation(leaf);
1260 root_owner = btrfs_header_owner(leaf);
1262 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1264 u32 newsize = inode->i_size - found_key.offset;
1265 dec_i_blocks(inode, item_end + 1 -
1266 found_key.offset - newsize);
1268 btrfs_file_extent_calc_inline_size(newsize);
1269 ret = btrfs_truncate_item(trans, root, path,
1273 dec_i_blocks(inode, item_end + 1 -
1279 if (!pending_del_nr) {
1280 /* no pending yet, add ourselves */
1281 pending_del_slot = path->slots[0];
1283 } else if (pending_del_nr &&
1284 path->slots[0] + 1 == pending_del_slot) {
1285 /* hop on the pending chunk */
1287 pending_del_slot = path->slots[0];
1289 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1295 ret = btrfs_free_extent(trans, root, extent_start,
1298 root_gen, inode->i_ino,
1299 found_key.offset, 0);
1303 if (path->slots[0] == 0) {
1306 btrfs_release_path(root, path);
1311 if (pending_del_nr &&
1312 path->slots[0] + 1 != pending_del_slot) {
1313 struct btrfs_key debug;
1315 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1317 ret = btrfs_del_items(trans, root, path,
1322 btrfs_release_path(root, path);
1328 if (pending_del_nr) {
1329 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1332 btrfs_free_path(path);
1333 inode->i_sb->s_dirt = 1;
1338 * taken from block_truncate_page, but does cow as it zeros out
1339 * any bytes left in the last page in the file.
1341 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1343 struct inode *inode = mapping->host;
1344 struct btrfs_root *root = BTRFS_I(inode)->root;
1345 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1346 struct btrfs_ordered_extent *ordered;
1348 u32 blocksize = root->sectorsize;
1349 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1350 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1356 if ((offset & (blocksize - 1)) == 0)
1361 page = grab_cache_page(mapping, index);
1365 page_start = page_offset(page);
1366 page_end = page_start + PAGE_CACHE_SIZE - 1;
1368 if (!PageUptodate(page)) {
1369 ret = btrfs_readpage(NULL, page);
1371 if (page->mapping != mapping) {
1373 page_cache_release(page);
1376 if (!PageUptodate(page)) {
1381 wait_on_page_writeback(page);
1383 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1384 set_page_extent_mapped(page);
1386 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1388 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1390 page_cache_release(page);
1391 btrfs_start_ordered_extent(inode, ordered, 1);
1392 btrfs_put_ordered_extent(ordered);
1396 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1397 page_end, GFP_NOFS);
1399 if (offset != PAGE_CACHE_SIZE) {
1401 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1402 flush_dcache_page(page);
1405 ClearPageChecked(page);
1406 set_page_dirty(page);
1407 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1410 page_cache_release(page);
1415 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1417 struct inode *inode = dentry->d_inode;
1420 err = inode_change_ok(inode, attr);
1424 if (S_ISREG(inode->i_mode) &&
1425 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1426 struct btrfs_trans_handle *trans;
1427 struct btrfs_root *root = BTRFS_I(inode)->root;
1428 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1430 u64 mask = root->sectorsize - 1;
1431 u64 hole_start = (inode->i_size + mask) & ~mask;
1432 u64 block_end = (attr->ia_size + mask) & ~mask;
1436 if (attr->ia_size <= hole_start)
1439 err = btrfs_check_free_space(root, 1, 0);
1443 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1445 hole_size = block_end - hole_start;
1446 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1447 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1449 trans = btrfs_start_transaction(root, 1);
1450 btrfs_set_trans_block_group(trans, inode);
1451 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1452 err = btrfs_drop_extents(trans, root, inode,
1453 hole_start, block_end, hole_start,
1456 if (alloc_hint != EXTENT_MAP_INLINE) {
1457 err = btrfs_insert_file_extent(trans, root,
1461 btrfs_drop_extent_cache(inode, hole_start,
1463 btrfs_check_file(root, inode);
1465 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1466 btrfs_end_transaction(trans, root);
1467 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1472 err = inode_setattr(inode, attr);
1477 void btrfs_delete_inode(struct inode *inode)
1479 struct btrfs_trans_handle *trans;
1480 struct btrfs_root *root = BTRFS_I(inode)->root;
1484 truncate_inode_pages(&inode->i_data, 0);
1485 if (is_bad_inode(inode)) {
1488 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1490 btrfs_i_size_write(inode, 0);
1491 trans = btrfs_start_transaction(root, 1);
1493 btrfs_set_trans_block_group(trans, inode);
1494 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1496 goto no_delete_lock;
1498 nr = trans->blocks_used;
1501 btrfs_end_transaction(trans, root);
1502 btrfs_btree_balance_dirty(root, nr);
1506 nr = trans->blocks_used;
1507 btrfs_end_transaction(trans, root);
1508 btrfs_btree_balance_dirty(root, nr);
1514 * this returns the key found in the dir entry in the location pointer.
1515 * If no dir entries were found, location->objectid is 0.
1517 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1518 struct btrfs_key *location)
1520 const char *name = dentry->d_name.name;
1521 int namelen = dentry->d_name.len;
1522 struct btrfs_dir_item *di;
1523 struct btrfs_path *path;
1524 struct btrfs_root *root = BTRFS_I(dir)->root;
1527 if (namelen == 1 && strcmp(name, ".") == 0) {
1528 location->objectid = dir->i_ino;
1529 location->type = BTRFS_INODE_ITEM_KEY;
1530 location->offset = 0;
1533 path = btrfs_alloc_path();
1536 if (namelen == 2 && strcmp(name, "..") == 0) {
1537 struct btrfs_key key;
1538 struct extent_buffer *leaf;
1542 key.objectid = dir->i_ino;
1543 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1549 leaf = path->nodes[0];
1550 slot = path->slots[0];
1551 nritems = btrfs_header_nritems(leaf);
1552 if (slot >= nritems)
1555 btrfs_item_key_to_cpu(leaf, &key, slot);
1556 if (key.objectid != dir->i_ino ||
1557 key.type != BTRFS_INODE_REF_KEY) {
1560 location->objectid = key.offset;
1561 location->type = BTRFS_INODE_ITEM_KEY;
1562 location->offset = 0;
1566 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1570 if (!di || IS_ERR(di)) {
1573 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1575 btrfs_free_path(path);
1578 location->objectid = 0;
1583 * when we hit a tree root in a directory, the btrfs part of the inode
1584 * needs to be changed to reflect the root directory of the tree root. This
1585 * is kind of like crossing a mount point.
1587 static int fixup_tree_root_location(struct btrfs_root *root,
1588 struct btrfs_key *location,
1589 struct btrfs_root **sub_root,
1590 struct dentry *dentry)
1592 struct btrfs_path *path;
1593 struct btrfs_root_item *ri;
1595 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1597 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1600 path = btrfs_alloc_path();
1603 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1604 dentry->d_name.name,
1605 dentry->d_name.len);
1606 if (IS_ERR(*sub_root))
1607 return PTR_ERR(*sub_root);
1609 ri = &(*sub_root)->root_item;
1610 location->objectid = btrfs_root_dirid(ri);
1611 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1612 location->offset = 0;
1614 btrfs_free_path(path);
1618 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1620 struct btrfs_iget_args *args = p;
1621 inode->i_ino = args->ino;
1622 BTRFS_I(inode)->root = args->root;
1623 BTRFS_I(inode)->delalloc_bytes = 0;
1624 BTRFS_I(inode)->disk_i_size = 0;
1625 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1626 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1627 inode->i_mapping, GFP_NOFS);
1628 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1629 inode->i_mapping, GFP_NOFS);
1630 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1631 mutex_init(&BTRFS_I(inode)->csum_mutex);
1632 mutex_init(&BTRFS_I(inode)->extent_mutex);
1636 static int btrfs_find_actor(struct inode *inode, void *opaque)
1638 struct btrfs_iget_args *args = opaque;
1639 return (args->ino == inode->i_ino &&
1640 args->root == BTRFS_I(inode)->root);
1643 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1646 struct btrfs_iget_args args;
1647 args.ino = objectid;
1648 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1653 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1656 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1657 struct btrfs_root *root)
1659 struct inode *inode;
1660 struct btrfs_iget_args args;
1661 args.ino = objectid;
1664 inode = iget5_locked(s, objectid, btrfs_find_actor,
1665 btrfs_init_locked_inode,
1670 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1671 struct nameidata *nd)
1673 struct inode * inode;
1674 struct btrfs_inode *bi = BTRFS_I(dir);
1675 struct btrfs_root *root = bi->root;
1676 struct btrfs_root *sub_root = root;
1677 struct btrfs_key location;
1680 if (dentry->d_name.len > BTRFS_NAME_LEN)
1681 return ERR_PTR(-ENAMETOOLONG);
1683 ret = btrfs_inode_by_name(dir, dentry, &location);
1686 return ERR_PTR(ret);
1689 if (location.objectid) {
1690 ret = fixup_tree_root_location(root, &location, &sub_root,
1693 return ERR_PTR(ret);
1695 return ERR_PTR(-ENOENT);
1696 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1699 return ERR_PTR(-EACCES);
1700 if (inode->i_state & I_NEW) {
1701 /* the inode and parent dir are two different roots */
1702 if (sub_root != root) {
1704 sub_root->inode = inode;
1706 BTRFS_I(inode)->root = sub_root;
1707 memcpy(&BTRFS_I(inode)->location, &location,
1709 btrfs_read_locked_inode(inode);
1710 unlock_new_inode(inode);
1713 return d_splice_alias(inode, dentry);
1716 static unsigned char btrfs_filetype_table[] = {
1717 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1720 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1722 struct inode *inode = filp->f_dentry->d_inode;
1723 struct btrfs_root *root = BTRFS_I(inode)->root;
1724 struct btrfs_item *item;
1725 struct btrfs_dir_item *di;
1726 struct btrfs_key key;
1727 struct btrfs_key found_key;
1728 struct btrfs_path *path;
1731 struct extent_buffer *leaf;
1734 unsigned char d_type;
1739 int key_type = BTRFS_DIR_INDEX_KEY;
1744 /* FIXME, use a real flag for deciding about the key type */
1745 if (root->fs_info->tree_root == root)
1746 key_type = BTRFS_DIR_ITEM_KEY;
1748 /* special case for "." */
1749 if (filp->f_pos == 0) {
1750 over = filldir(dirent, ".", 1,
1758 key.objectid = inode->i_ino;
1759 path = btrfs_alloc_path();
1762 /* special case for .., just use the back ref */
1763 if (filp->f_pos == 1) {
1764 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1768 leaf = path->nodes[0];
1769 slot = path->slots[0];
1770 nritems = btrfs_header_nritems(leaf);
1771 if (slot >= nritems) {
1772 btrfs_release_path(root, path);
1773 goto read_dir_items;
1775 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1776 btrfs_release_path(root, path);
1777 if (found_key.objectid != key.objectid ||
1778 found_key.type != BTRFS_INODE_REF_KEY)
1779 goto read_dir_items;
1780 over = filldir(dirent, "..", 2,
1781 2, found_key.offset, DT_DIR);
1788 btrfs_set_key_type(&key, key_type);
1789 key.offset = filp->f_pos;
1791 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1796 leaf = path->nodes[0];
1797 nritems = btrfs_header_nritems(leaf);
1798 slot = path->slots[0];
1799 if (advance || slot >= nritems) {
1800 if (slot >= nritems -1) {
1801 ret = btrfs_next_leaf(root, path);
1804 leaf = path->nodes[0];
1805 nritems = btrfs_header_nritems(leaf);
1806 slot = path->slots[0];
1813 item = btrfs_item_nr(leaf, slot);
1814 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1816 if (found_key.objectid != key.objectid)
1818 if (btrfs_key_type(&found_key) != key_type)
1820 if (found_key.offset < filp->f_pos)
1823 filp->f_pos = found_key.offset;
1825 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1827 di_total = btrfs_item_size(leaf, item);
1828 while(di_cur < di_total) {
1829 struct btrfs_key location;
1831 name_len = btrfs_dir_name_len(leaf, di);
1832 if (name_len < 32) {
1833 name_ptr = tmp_name;
1835 name_ptr = kmalloc(name_len, GFP_NOFS);
1838 read_extent_buffer(leaf, name_ptr,
1839 (unsigned long)(di + 1), name_len);
1841 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1842 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1843 over = filldir(dirent, name_ptr, name_len,
1848 if (name_ptr != tmp_name)
1853 di_len = btrfs_dir_name_len(leaf, di) +
1854 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1856 di = (struct btrfs_dir_item *)((char *)di + di_len);
1859 if (key_type == BTRFS_DIR_INDEX_KEY)
1860 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1866 btrfs_free_path(path);
1870 int btrfs_write_inode(struct inode *inode, int wait)
1872 struct btrfs_root *root = BTRFS_I(inode)->root;
1873 struct btrfs_trans_handle *trans;
1877 trans = btrfs_join_transaction(root, 1);
1878 btrfs_set_trans_block_group(trans, inode);
1879 ret = btrfs_commit_transaction(trans, root);
1885 * This is somewhat expensive, updating the tree every time the
1886 * inode changes. But, it is most likely to find the inode in cache.
1887 * FIXME, needs more benchmarking...there are no reasons other than performance
1888 * to keep or drop this code.
1890 void btrfs_dirty_inode(struct inode *inode)
1892 struct btrfs_root *root = BTRFS_I(inode)->root;
1893 struct btrfs_trans_handle *trans;
1895 trans = btrfs_join_transaction(root, 1);
1896 btrfs_set_trans_block_group(trans, inode);
1897 btrfs_update_inode(trans, root, inode);
1898 btrfs_end_transaction(trans, root);
1901 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1902 struct btrfs_root *root,
1903 const char *name, int name_len,
1906 struct btrfs_block_group_cache *group,
1909 struct inode *inode;
1910 struct btrfs_inode_item *inode_item;
1911 struct btrfs_block_group_cache *new_inode_group;
1912 struct btrfs_key *location;
1913 struct btrfs_path *path;
1914 struct btrfs_inode_ref *ref;
1915 struct btrfs_key key[2];
1921 path = btrfs_alloc_path();
1924 inode = new_inode(root->fs_info->sb);
1926 return ERR_PTR(-ENOMEM);
1928 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1929 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1930 inode->i_mapping, GFP_NOFS);
1931 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1932 inode->i_mapping, GFP_NOFS);
1933 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1934 mutex_init(&BTRFS_I(inode)->csum_mutex);
1935 mutex_init(&BTRFS_I(inode)->extent_mutex);
1936 BTRFS_I(inode)->delalloc_bytes = 0;
1937 BTRFS_I(inode)->disk_i_size = 0;
1938 BTRFS_I(inode)->root = root;
1944 new_inode_group = btrfs_find_block_group(root, group, 0,
1945 BTRFS_BLOCK_GROUP_METADATA, owner);
1946 if (!new_inode_group) {
1947 printk("find_block group failed\n");
1948 new_inode_group = group;
1950 BTRFS_I(inode)->block_group = new_inode_group;
1951 BTRFS_I(inode)->flags = 0;
1953 key[0].objectid = objectid;
1954 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1957 key[1].objectid = objectid;
1958 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1959 key[1].offset = ref_objectid;
1961 sizes[0] = sizeof(struct btrfs_inode_item);
1962 sizes[1] = name_len + sizeof(*ref);
1964 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1968 if (objectid > root->highest_inode)
1969 root->highest_inode = objectid;
1971 inode->i_uid = current->fsuid;
1972 inode->i_gid = current->fsgid;
1973 inode->i_mode = mode;
1974 inode->i_ino = objectid;
1975 inode->i_blocks = 0;
1976 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1977 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1978 struct btrfs_inode_item);
1979 fill_inode_item(path->nodes[0], inode_item, inode);
1981 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1982 struct btrfs_inode_ref);
1983 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1984 ptr = (unsigned long)(ref + 1);
1985 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1987 btrfs_mark_buffer_dirty(path->nodes[0]);
1988 btrfs_free_path(path);
1990 location = &BTRFS_I(inode)->location;
1991 location->objectid = objectid;
1992 location->offset = 0;
1993 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1995 insert_inode_hash(inode);
1998 btrfs_free_path(path);
1999 return ERR_PTR(ret);
2002 static inline u8 btrfs_inode_type(struct inode *inode)
2004 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2007 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2008 struct dentry *dentry, struct inode *inode,
2012 struct btrfs_key key;
2013 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2014 struct inode *parent_inode;
2016 key.objectid = inode->i_ino;
2017 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2020 ret = btrfs_insert_dir_item(trans, root,
2021 dentry->d_name.name, dentry->d_name.len,
2022 dentry->d_parent->d_inode->i_ino,
2023 &key, btrfs_inode_type(inode));
2026 ret = btrfs_insert_inode_ref(trans, root,
2027 dentry->d_name.name,
2030 dentry->d_parent->d_inode->i_ino);
2032 parent_inode = dentry->d_parent->d_inode;
2033 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2034 dentry->d_name.len * 2);
2035 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2036 ret = btrfs_update_inode(trans, root,
2037 dentry->d_parent->d_inode);
2042 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2043 struct dentry *dentry, struct inode *inode,
2046 int err = btrfs_add_link(trans, dentry, inode, backref);
2048 d_instantiate(dentry, inode);
2056 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2057 int mode, dev_t rdev)
2059 struct btrfs_trans_handle *trans;
2060 struct btrfs_root *root = BTRFS_I(dir)->root;
2061 struct inode *inode = NULL;
2065 unsigned long nr = 0;
2067 if (!new_valid_dev(rdev))
2070 err = btrfs_check_free_space(root, 1, 0);
2074 trans = btrfs_start_transaction(root, 1);
2075 btrfs_set_trans_block_group(trans, dir);
2077 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2083 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2085 dentry->d_parent->d_inode->i_ino, objectid,
2086 BTRFS_I(dir)->block_group, mode);
2087 err = PTR_ERR(inode);
2091 btrfs_set_trans_block_group(trans, inode);
2092 err = btrfs_add_nondir(trans, dentry, inode, 0);
2096 inode->i_op = &btrfs_special_inode_operations;
2097 init_special_inode(inode, inode->i_mode, rdev);
2098 btrfs_update_inode(trans, root, inode);
2100 dir->i_sb->s_dirt = 1;
2101 btrfs_update_inode_block_group(trans, inode);
2102 btrfs_update_inode_block_group(trans, dir);
2104 nr = trans->blocks_used;
2105 btrfs_end_transaction_throttle(trans, root);
2108 inode_dec_link_count(inode);
2111 btrfs_btree_balance_dirty(root, nr);
2115 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2116 int mode, struct nameidata *nd)
2118 struct btrfs_trans_handle *trans;
2119 struct btrfs_root *root = BTRFS_I(dir)->root;
2120 struct inode *inode = NULL;
2123 unsigned long nr = 0;
2126 err = btrfs_check_free_space(root, 1, 0);
2129 trans = btrfs_start_transaction(root, 1);
2130 btrfs_set_trans_block_group(trans, dir);
2132 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2138 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2140 dentry->d_parent->d_inode->i_ino,
2141 objectid, BTRFS_I(dir)->block_group, mode);
2142 err = PTR_ERR(inode);
2146 btrfs_set_trans_block_group(trans, inode);
2147 err = btrfs_add_nondir(trans, dentry, inode, 0);
2151 inode->i_mapping->a_ops = &btrfs_aops;
2152 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2153 inode->i_fop = &btrfs_file_operations;
2154 inode->i_op = &btrfs_file_inode_operations;
2155 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2156 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2157 inode->i_mapping, GFP_NOFS);
2158 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2159 inode->i_mapping, GFP_NOFS);
2160 mutex_init(&BTRFS_I(inode)->csum_mutex);
2161 mutex_init(&BTRFS_I(inode)->extent_mutex);
2162 BTRFS_I(inode)->delalloc_bytes = 0;
2163 BTRFS_I(inode)->disk_i_size = 0;
2164 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2165 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2167 dir->i_sb->s_dirt = 1;
2168 btrfs_update_inode_block_group(trans, inode);
2169 btrfs_update_inode_block_group(trans, dir);
2171 nr = trans->blocks_used;
2172 btrfs_end_transaction_throttle(trans, root);
2175 inode_dec_link_count(inode);
2178 btrfs_btree_balance_dirty(root, nr);
2182 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2183 struct dentry *dentry)
2185 struct btrfs_trans_handle *trans;
2186 struct btrfs_root *root = BTRFS_I(dir)->root;
2187 struct inode *inode = old_dentry->d_inode;
2188 unsigned long nr = 0;
2192 if (inode->i_nlink == 0)
2195 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2200 err = btrfs_check_free_space(root, 1, 0);
2203 trans = btrfs_start_transaction(root, 1);
2205 btrfs_set_trans_block_group(trans, dir);
2206 atomic_inc(&inode->i_count);
2207 err = btrfs_add_nondir(trans, dentry, inode, 1);
2212 dir->i_sb->s_dirt = 1;
2213 btrfs_update_inode_block_group(trans, dir);
2214 err = btrfs_update_inode(trans, root, inode);
2219 nr = trans->blocks_used;
2220 btrfs_end_transaction_throttle(trans, root);
2223 inode_dec_link_count(inode);
2226 btrfs_btree_balance_dirty(root, nr);
2230 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2232 struct inode *inode = NULL;
2233 struct btrfs_trans_handle *trans;
2234 struct btrfs_root *root = BTRFS_I(dir)->root;
2236 int drop_on_err = 0;
2238 unsigned long nr = 1;
2240 err = btrfs_check_free_space(root, 1, 0);
2244 trans = btrfs_start_transaction(root, 1);
2245 btrfs_set_trans_block_group(trans, dir);
2247 if (IS_ERR(trans)) {
2248 err = PTR_ERR(trans);
2252 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2258 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2260 dentry->d_parent->d_inode->i_ino, objectid,
2261 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2262 if (IS_ERR(inode)) {
2263 err = PTR_ERR(inode);
2268 inode->i_op = &btrfs_dir_inode_operations;
2269 inode->i_fop = &btrfs_dir_file_operations;
2270 btrfs_set_trans_block_group(trans, inode);
2272 btrfs_i_size_write(inode, 0);
2273 err = btrfs_update_inode(trans, root, inode);
2277 err = btrfs_add_link(trans, dentry, inode, 0);
2281 d_instantiate(dentry, inode);
2283 dir->i_sb->s_dirt = 1;
2284 btrfs_update_inode_block_group(trans, inode);
2285 btrfs_update_inode_block_group(trans, dir);
2288 nr = trans->blocks_used;
2289 btrfs_end_transaction_throttle(trans, root);
2294 btrfs_btree_balance_dirty(root, nr);
2298 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2299 struct extent_map *existing,
2300 struct extent_map *em,
2301 u64 map_start, u64 map_len)
2305 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2306 start_diff = map_start - em->start;
2307 em->start = map_start;
2309 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2310 em->block_start += start_diff;
2311 return add_extent_mapping(em_tree, em);
2314 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2315 size_t pg_offset, u64 start, u64 len,
2321 u64 extent_start = 0;
2323 u64 objectid = inode->i_ino;
2325 struct btrfs_path *path = NULL;
2326 struct btrfs_root *root = BTRFS_I(inode)->root;
2327 struct btrfs_file_extent_item *item;
2328 struct extent_buffer *leaf;
2329 struct btrfs_key found_key;
2330 struct extent_map *em = NULL;
2331 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2332 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2333 struct btrfs_trans_handle *trans = NULL;
2336 spin_lock(&em_tree->lock);
2337 em = lookup_extent_mapping(em_tree, start, len);
2339 em->bdev = root->fs_info->fs_devices->latest_bdev;
2340 spin_unlock(&em_tree->lock);
2343 if (em->start > start || em->start + em->len <= start)
2344 free_extent_map(em);
2345 else if (em->block_start == EXTENT_MAP_INLINE && page)
2346 free_extent_map(em);
2350 em = alloc_extent_map(GFP_NOFS);
2355 em->bdev = root->fs_info->fs_devices->latest_bdev;
2356 em->start = EXTENT_MAP_HOLE;
2360 path = btrfs_alloc_path();
2364 ret = btrfs_lookup_file_extent(trans, root, path,
2365 objectid, start, trans != NULL);
2372 if (path->slots[0] == 0)
2377 leaf = path->nodes[0];
2378 item = btrfs_item_ptr(leaf, path->slots[0],
2379 struct btrfs_file_extent_item);
2380 /* are we inside the extent that was found? */
2381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2382 found_type = btrfs_key_type(&found_key);
2383 if (found_key.objectid != objectid ||
2384 found_type != BTRFS_EXTENT_DATA_KEY) {
2388 found_type = btrfs_file_extent_type(leaf, item);
2389 extent_start = found_key.offset;
2390 if (found_type == BTRFS_FILE_EXTENT_REG) {
2391 extent_end = extent_start +
2392 btrfs_file_extent_num_bytes(leaf, item);
2394 if (start < extent_start || start >= extent_end) {
2396 if (start < extent_start) {
2397 if (start + len <= extent_start)
2399 em->len = extent_end - extent_start;
2405 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2407 em->start = extent_start;
2408 em->len = extent_end - extent_start;
2409 em->block_start = EXTENT_MAP_HOLE;
2412 bytenr += btrfs_file_extent_offset(leaf, item);
2413 em->block_start = bytenr;
2414 em->start = extent_start;
2415 em->len = extent_end - extent_start;
2417 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2422 size_t extent_offset;
2425 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2427 extent_end = (extent_start + size + root->sectorsize - 1) &
2428 ~((u64)root->sectorsize - 1);
2429 if (start < extent_start || start >= extent_end) {
2431 if (start < extent_start) {
2432 if (start + len <= extent_start)
2434 em->len = extent_end - extent_start;
2440 em->block_start = EXTENT_MAP_INLINE;
2443 em->start = extent_start;
2448 page_start = page_offset(page) + pg_offset;
2449 extent_offset = page_start - extent_start;
2450 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2451 size - extent_offset);
2452 em->start = extent_start + extent_offset;
2453 em->len = (copy_size + root->sectorsize - 1) &
2454 ~((u64)root->sectorsize - 1);
2456 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2457 if (create == 0 && !PageUptodate(page)) {
2458 read_extent_buffer(leaf, map + pg_offset, ptr,
2460 flush_dcache_page(page);
2461 } else if (create && PageUptodate(page)) {
2464 free_extent_map(em);
2466 btrfs_release_path(root, path);
2467 trans = btrfs_join_transaction(root, 1);
2470 write_extent_buffer(leaf, map + pg_offset, ptr,
2472 btrfs_mark_buffer_dirty(leaf);
2475 set_extent_uptodate(io_tree, em->start,
2476 extent_map_end(em) - 1, GFP_NOFS);
2479 printk("unkknown found_type %d\n", found_type);
2486 em->block_start = EXTENT_MAP_HOLE;
2488 btrfs_release_path(root, path);
2489 if (em->start > start || extent_map_end(em) <= start) {
2490 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2496 spin_lock(&em_tree->lock);
2497 ret = add_extent_mapping(em_tree, em);
2498 /* it is possible that someone inserted the extent into the tree
2499 * while we had the lock dropped. It is also possible that
2500 * an overlapping map exists in the tree
2502 if (ret == -EEXIST) {
2503 struct extent_map *existing;
2507 existing = lookup_extent_mapping(em_tree, start, len);
2508 if (existing && (existing->start > start ||
2509 existing->start + existing->len <= start)) {
2510 free_extent_map(existing);
2514 existing = lookup_extent_mapping(em_tree, em->start,
2517 err = merge_extent_mapping(em_tree, existing,
2520 free_extent_map(existing);
2522 free_extent_map(em);
2527 printk("failing to insert %Lu %Lu\n",
2529 free_extent_map(em);
2533 free_extent_map(em);
2538 spin_unlock(&em_tree->lock);
2541 btrfs_free_path(path);
2543 ret = btrfs_end_transaction(trans, root);
2549 free_extent_map(em);
2551 return ERR_PTR(err);
2556 #if 0 /* waiting for O_DIRECT reads */
2557 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2558 struct buffer_head *bh_result, int create)
2560 struct extent_map *em;
2561 u64 start = (u64)iblock << inode->i_blkbits;
2562 struct btrfs_multi_bio *multi = NULL;
2563 struct btrfs_root *root = BTRFS_I(inode)->root;
2569 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2571 if (!em || IS_ERR(em))
2574 if (em->start > start || em->start + em->len <= start) {
2578 if (em->block_start == EXTENT_MAP_INLINE) {
2583 len = em->start + em->len - start;
2584 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2586 if (em->block_start == EXTENT_MAP_HOLE ||
2587 em->block_start == EXTENT_MAP_DELALLOC) {
2588 bh_result->b_size = len;
2592 logical = start - em->start;
2593 logical = em->block_start + logical;
2596 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2597 logical, &map_length, &multi, 0);
2599 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2600 bh_result->b_size = min(map_length, len);
2602 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2603 set_buffer_mapped(bh_result);
2606 free_extent_map(em);
2611 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2612 const struct iovec *iov, loff_t offset,
2613 unsigned long nr_segs)
2617 struct file *file = iocb->ki_filp;
2618 struct inode *inode = file->f_mapping->host;
2623 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2624 offset, nr_segs, btrfs_get_block, NULL);
2628 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2630 return extent_bmap(mapping, iblock, btrfs_get_extent);
2633 int btrfs_readpage(struct file *file, struct page *page)
2635 struct extent_io_tree *tree;
2636 tree = &BTRFS_I(page->mapping->host)->io_tree;
2637 return extent_read_full_page(tree, page, btrfs_get_extent);
2640 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2642 struct extent_io_tree *tree;
2645 if (current->flags & PF_MEMALLOC) {
2646 redirty_page_for_writepage(wbc, page);
2650 tree = &BTRFS_I(page->mapping->host)->io_tree;
2651 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2654 int btrfs_writepages(struct address_space *mapping,
2655 struct writeback_control *wbc)
2657 struct extent_io_tree *tree;
2658 tree = &BTRFS_I(mapping->host)->io_tree;
2659 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2663 btrfs_readpages(struct file *file, struct address_space *mapping,
2664 struct list_head *pages, unsigned nr_pages)
2666 struct extent_io_tree *tree;
2667 tree = &BTRFS_I(mapping->host)->io_tree;
2668 return extent_readpages(tree, mapping, pages, nr_pages,
2671 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2673 struct extent_io_tree *tree;
2674 struct extent_map_tree *map;
2677 tree = &BTRFS_I(page->mapping->host)->io_tree;
2678 map = &BTRFS_I(page->mapping->host)->extent_tree;
2679 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2681 ClearPagePrivate(page);
2682 set_page_private(page, 0);
2683 page_cache_release(page);
2688 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2690 return __btrfs_releasepage(page, gfp_flags);
2693 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2695 struct extent_io_tree *tree;
2696 struct btrfs_ordered_extent *ordered;
2697 u64 page_start = page_offset(page);
2698 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2700 wait_on_page_writeback(page);
2701 tree = &BTRFS_I(page->mapping->host)->io_tree;
2703 btrfs_releasepage(page, GFP_NOFS);
2707 lock_extent(tree, page_start, page_end, GFP_NOFS);
2708 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2712 * IO on this page will never be started, so we need
2713 * to account for any ordered extents now
2715 clear_extent_bit(tree, page_start, page_end,
2716 EXTENT_DIRTY | EXTENT_DELALLOC |
2717 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2718 btrfs_finish_ordered_io(page->mapping->host,
2719 page_start, page_end);
2720 btrfs_put_ordered_extent(ordered);
2721 lock_extent(tree, page_start, page_end, GFP_NOFS);
2723 clear_extent_bit(tree, page_start, page_end,
2724 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2727 __btrfs_releasepage(page, GFP_NOFS);
2729 ClearPageChecked(page);
2730 if (PagePrivate(page)) {
2731 ClearPagePrivate(page);
2732 set_page_private(page, 0);
2733 page_cache_release(page);
2738 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2739 * called from a page fault handler when a page is first dirtied. Hence we must
2740 * be careful to check for EOF conditions here. We set the page up correctly
2741 * for a written page which means we get ENOSPC checking when writing into
2742 * holes and correct delalloc and unwritten extent mapping on filesystems that
2743 * support these features.
2745 * We are not allowed to take the i_mutex here so we have to play games to
2746 * protect against truncate races as the page could now be beyond EOF. Because
2747 * vmtruncate() writes the inode size before removing pages, once we have the
2748 * page lock we can determine safely if the page is beyond EOF. If it is not
2749 * beyond EOF, then the page is guaranteed safe against truncation until we
2752 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2754 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2755 struct btrfs_root *root = BTRFS_I(inode)->root;
2756 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2757 struct btrfs_ordered_extent *ordered;
2759 unsigned long zero_start;
2765 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2772 size = i_size_read(inode);
2773 page_start = page_offset(page);
2774 page_end = page_start + PAGE_CACHE_SIZE - 1;
2776 if ((page->mapping != inode->i_mapping) ||
2777 (page_start >= size)) {
2778 /* page got truncated out from underneath us */
2781 wait_on_page_writeback(page);
2783 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2784 set_page_extent_mapped(page);
2787 * we can't set the delalloc bits if there are pending ordered
2788 * extents. Drop our locks and wait for them to finish
2790 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2792 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2794 btrfs_start_ordered_extent(inode, ordered, 1);
2795 btrfs_put_ordered_extent(ordered);
2799 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2800 page_end, GFP_NOFS);
2803 /* page is wholly or partially inside EOF */
2804 if (page_start + PAGE_CACHE_SIZE > size)
2805 zero_start = size & ~PAGE_CACHE_MASK;
2807 zero_start = PAGE_CACHE_SIZE;
2809 if (zero_start != PAGE_CACHE_SIZE) {
2811 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2812 flush_dcache_page(page);
2815 ClearPageChecked(page);
2816 set_page_dirty(page);
2817 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2825 static void btrfs_truncate(struct inode *inode)
2827 struct btrfs_root *root = BTRFS_I(inode)->root;
2829 struct btrfs_trans_handle *trans;
2831 u64 mask = root->sectorsize - 1;
2833 if (!S_ISREG(inode->i_mode))
2835 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2838 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2839 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2841 trans = btrfs_start_transaction(root, 1);
2842 btrfs_set_trans_block_group(trans, inode);
2843 btrfs_i_size_write(inode, inode->i_size);
2845 /* FIXME, add redo link to tree so we don't leak on crash */
2846 ret = btrfs_truncate_in_trans(trans, root, inode,
2847 BTRFS_EXTENT_DATA_KEY);
2848 btrfs_update_inode(trans, root, inode);
2849 nr = trans->blocks_used;
2851 ret = btrfs_end_transaction_throttle(trans, root);
2853 btrfs_btree_balance_dirty(root, nr);
2857 * Invalidate a single dcache entry at the root of the filesystem.
2858 * Needed after creation of snapshot or subvolume.
2860 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2863 struct dentry *alias, *entry;
2866 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2870 /* change me if btrfs ever gets a d_hash operation */
2871 qstr.hash = full_name_hash(qstr.name, qstr.len);
2872 entry = d_lookup(alias, &qstr);
2875 d_invalidate(entry);
2881 int btrfs_create_subvol_root(struct btrfs_root *new_root,
2882 struct btrfs_trans_handle *trans, u64 new_dirid,
2883 struct btrfs_block_group_cache *block_group)
2885 struct inode *inode;
2888 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2889 new_dirid, block_group, S_IFDIR | 0700);
2891 return PTR_ERR(inode);
2892 inode->i_op = &btrfs_dir_inode_operations;
2893 inode->i_fop = &btrfs_dir_file_operations;
2894 new_root->inode = inode;
2896 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2899 btrfs_i_size_write(inode, 0);
2901 return btrfs_update_inode(trans, new_root, inode);
2904 unsigned long btrfs_force_ra(struct address_space *mapping,
2905 struct file_ra_state *ra, struct file *file,
2906 pgoff_t offset, pgoff_t last_index)
2908 pgoff_t req_size = last_index - offset + 1;
2910 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2911 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2914 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2915 return offset + req_size;
2919 struct inode *btrfs_alloc_inode(struct super_block *sb)
2921 struct btrfs_inode *ei;
2923 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2927 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
2928 return &ei->vfs_inode;
2931 void btrfs_destroy_inode(struct inode *inode)
2933 struct btrfs_ordered_extent *ordered;
2934 WARN_ON(!list_empty(&inode->i_dentry));
2935 WARN_ON(inode->i_data.nrpages);
2938 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2942 printk("found ordered extent %Lu %Lu\n",
2943 ordered->file_offset, ordered->len);
2944 btrfs_remove_ordered_extent(inode, ordered);
2945 btrfs_put_ordered_extent(ordered);
2946 btrfs_put_ordered_extent(ordered);
2949 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2950 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2953 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2954 static void init_once(struct kmem_cache * cachep, void *foo)
2956 static void init_once(void * foo, struct kmem_cache * cachep,
2957 unsigned long flags)
2960 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2962 inode_init_once(&ei->vfs_inode);
2965 void btrfs_destroy_cachep(void)
2967 if (btrfs_inode_cachep)
2968 kmem_cache_destroy(btrfs_inode_cachep);
2969 if (btrfs_trans_handle_cachep)
2970 kmem_cache_destroy(btrfs_trans_handle_cachep);
2971 if (btrfs_transaction_cachep)
2972 kmem_cache_destroy(btrfs_transaction_cachep);
2973 if (btrfs_bit_radix_cachep)
2974 kmem_cache_destroy(btrfs_bit_radix_cachep);
2975 if (btrfs_path_cachep)
2976 kmem_cache_destroy(btrfs_path_cachep);
2979 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2980 unsigned long extra_flags,
2981 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2982 void (*ctor)(struct kmem_cache *, void *)
2984 void (*ctor)(void *, struct kmem_cache *,
2989 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2990 SLAB_MEM_SPREAD | extra_flags), ctor
2991 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2997 int btrfs_init_cachep(void)
2999 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3000 sizeof(struct btrfs_inode),
3002 if (!btrfs_inode_cachep)
3004 btrfs_trans_handle_cachep =
3005 btrfs_cache_create("btrfs_trans_handle_cache",
3006 sizeof(struct btrfs_trans_handle),
3008 if (!btrfs_trans_handle_cachep)
3010 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3011 sizeof(struct btrfs_transaction),
3013 if (!btrfs_transaction_cachep)
3015 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3016 sizeof(struct btrfs_path),
3018 if (!btrfs_path_cachep)
3020 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3021 SLAB_DESTROY_BY_RCU, NULL);
3022 if (!btrfs_bit_radix_cachep)
3026 btrfs_destroy_cachep();
3030 static int btrfs_getattr(struct vfsmount *mnt,
3031 struct dentry *dentry, struct kstat *stat)
3033 struct inode *inode = dentry->d_inode;
3034 generic_fillattr(inode, stat);
3035 stat->blksize = PAGE_CACHE_SIZE;
3036 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3040 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3041 struct inode * new_dir,struct dentry *new_dentry)
3043 struct btrfs_trans_handle *trans;
3044 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3045 struct inode *new_inode = new_dentry->d_inode;
3046 struct inode *old_inode = old_dentry->d_inode;
3047 struct timespec ctime = CURRENT_TIME;
3050 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3051 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3055 ret = btrfs_check_free_space(root, 1, 0);
3059 trans = btrfs_start_transaction(root, 1);
3061 btrfs_set_trans_block_group(trans, new_dir);
3063 old_dentry->d_inode->i_nlink++;
3064 old_dir->i_ctime = old_dir->i_mtime = ctime;
3065 new_dir->i_ctime = new_dir->i_mtime = ctime;
3066 old_inode->i_ctime = ctime;
3068 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3073 new_inode->i_ctime = CURRENT_TIME;
3074 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3078 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3083 btrfs_end_transaction(trans, root);
3088 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3089 const char *symname)
3091 struct btrfs_trans_handle *trans;
3092 struct btrfs_root *root = BTRFS_I(dir)->root;
3093 struct btrfs_path *path;
3094 struct btrfs_key key;
3095 struct inode *inode = NULL;
3102 struct btrfs_file_extent_item *ei;
3103 struct extent_buffer *leaf;
3104 unsigned long nr = 0;
3106 name_len = strlen(symname) + 1;
3107 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3108 return -ENAMETOOLONG;
3110 err = btrfs_check_free_space(root, 1, 0);
3114 trans = btrfs_start_transaction(root, 1);
3115 btrfs_set_trans_block_group(trans, dir);
3117 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3123 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3125 dentry->d_parent->d_inode->i_ino, objectid,
3126 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3127 err = PTR_ERR(inode);
3131 btrfs_set_trans_block_group(trans, inode);
3132 err = btrfs_add_nondir(trans, dentry, inode, 0);
3136 inode->i_mapping->a_ops = &btrfs_aops;
3137 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3138 inode->i_fop = &btrfs_file_operations;
3139 inode->i_op = &btrfs_file_inode_operations;
3140 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3141 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3142 inode->i_mapping, GFP_NOFS);
3143 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3144 inode->i_mapping, GFP_NOFS);
3145 mutex_init(&BTRFS_I(inode)->csum_mutex);
3146 mutex_init(&BTRFS_I(inode)->extent_mutex);
3147 BTRFS_I(inode)->delalloc_bytes = 0;
3148 BTRFS_I(inode)->disk_i_size = 0;
3149 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3150 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3152 dir->i_sb->s_dirt = 1;
3153 btrfs_update_inode_block_group(trans, inode);
3154 btrfs_update_inode_block_group(trans, dir);
3158 path = btrfs_alloc_path();
3160 key.objectid = inode->i_ino;
3162 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3163 datasize = btrfs_file_extent_calc_inline_size(name_len);
3164 err = btrfs_insert_empty_item(trans, root, path, &key,
3170 leaf = path->nodes[0];
3171 ei = btrfs_item_ptr(leaf, path->slots[0],
3172 struct btrfs_file_extent_item);
3173 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3174 btrfs_set_file_extent_type(leaf, ei,
3175 BTRFS_FILE_EXTENT_INLINE);
3176 ptr = btrfs_file_extent_inline_start(ei);
3177 write_extent_buffer(leaf, symname, ptr, name_len);
3178 btrfs_mark_buffer_dirty(leaf);
3179 btrfs_free_path(path);
3181 inode->i_op = &btrfs_symlink_inode_operations;
3182 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3183 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3184 btrfs_i_size_write(inode, name_len - 1);
3185 err = btrfs_update_inode(trans, root, inode);
3190 nr = trans->blocks_used;
3191 btrfs_end_transaction_throttle(trans, root);
3194 inode_dec_link_count(inode);
3197 btrfs_btree_balance_dirty(root, nr);
3201 static int btrfs_set_page_dirty(struct page *page)
3203 return __set_page_dirty_nobuffers(page);
3206 static int btrfs_permission(struct inode *inode, int mask,
3207 struct nameidata *nd)
3209 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3211 return generic_permission(inode, mask, NULL);
3214 static struct inode_operations btrfs_dir_inode_operations = {
3215 .lookup = btrfs_lookup,
3216 .create = btrfs_create,
3217 .unlink = btrfs_unlink,
3219 .mkdir = btrfs_mkdir,
3220 .rmdir = btrfs_rmdir,
3221 .rename = btrfs_rename,
3222 .symlink = btrfs_symlink,
3223 .setattr = btrfs_setattr,
3224 .mknod = btrfs_mknod,
3225 .setxattr = generic_setxattr,
3226 .getxattr = generic_getxattr,
3227 .listxattr = btrfs_listxattr,
3228 .removexattr = generic_removexattr,
3229 .permission = btrfs_permission,
3231 static struct inode_operations btrfs_dir_ro_inode_operations = {
3232 .lookup = btrfs_lookup,
3233 .permission = btrfs_permission,
3235 static struct file_operations btrfs_dir_file_operations = {
3236 .llseek = generic_file_llseek,
3237 .read = generic_read_dir,
3238 .readdir = btrfs_readdir,
3239 .unlocked_ioctl = btrfs_ioctl,
3240 #ifdef CONFIG_COMPAT
3241 .compat_ioctl = btrfs_ioctl,
3243 .release = btrfs_release_file,
3246 static struct extent_io_ops btrfs_extent_io_ops = {
3247 .fill_delalloc = run_delalloc_range,
3248 .submit_bio_hook = btrfs_submit_bio_hook,
3249 .merge_bio_hook = btrfs_merge_bio_hook,
3250 .readpage_io_hook = btrfs_readpage_io_hook,
3251 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3252 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3253 .writepage_start_hook = btrfs_writepage_start_hook,
3254 .readpage_io_failed_hook = btrfs_io_failed_hook,
3255 .set_bit_hook = btrfs_set_bit_hook,
3256 .clear_bit_hook = btrfs_clear_bit_hook,
3259 static struct address_space_operations btrfs_aops = {
3260 .readpage = btrfs_readpage,
3261 .writepage = btrfs_writepage,
3262 .writepages = btrfs_writepages,
3263 .readpages = btrfs_readpages,
3264 .sync_page = block_sync_page,
3266 .direct_IO = btrfs_direct_IO,
3267 .invalidatepage = btrfs_invalidatepage,
3268 .releasepage = btrfs_releasepage,
3269 .set_page_dirty = btrfs_set_page_dirty,
3272 static struct address_space_operations btrfs_symlink_aops = {
3273 .readpage = btrfs_readpage,
3274 .writepage = btrfs_writepage,
3275 .invalidatepage = btrfs_invalidatepage,
3276 .releasepage = btrfs_releasepage,
3279 static struct inode_operations btrfs_file_inode_operations = {
3280 .truncate = btrfs_truncate,
3281 .getattr = btrfs_getattr,
3282 .setattr = btrfs_setattr,
3283 .setxattr = generic_setxattr,
3284 .getxattr = generic_getxattr,
3285 .listxattr = btrfs_listxattr,
3286 .removexattr = generic_removexattr,
3287 .permission = btrfs_permission,
3289 static struct inode_operations btrfs_special_inode_operations = {
3290 .getattr = btrfs_getattr,
3291 .setattr = btrfs_setattr,
3292 .permission = btrfs_permission,
3294 static struct inode_operations btrfs_symlink_inode_operations = {
3295 .readlink = generic_readlink,
3296 .follow_link = page_follow_link_light,
3297 .put_link = page_put_link,
3298 .permission = btrfs_permission,