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>
39 #include <linux/posix_acl.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
47 #include "ordered-data.h"
50 struct btrfs_iget_args {
52 struct btrfs_root *root;
55 static struct inode_operations btrfs_dir_inode_operations;
56 static struct inode_operations btrfs_symlink_inode_operations;
57 static struct inode_operations btrfs_dir_ro_inode_operations;
58 static struct inode_operations btrfs_special_inode_operations;
59 static struct inode_operations btrfs_file_inode_operations;
60 static struct address_space_operations btrfs_aops;
61 static struct address_space_operations btrfs_symlink_aops;
62 static struct file_operations btrfs_dir_file_operations;
63 static struct extent_io_ops btrfs_extent_io_ops;
65 static struct kmem_cache *btrfs_inode_cachep;
66 struct kmem_cache *btrfs_trans_handle_cachep;
67 struct kmem_cache *btrfs_transaction_cachep;
68 struct kmem_cache *btrfs_bit_radix_cachep;
69 struct kmem_cache *btrfs_path_cachep;
72 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
73 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
74 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
75 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
76 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
77 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
78 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
79 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82 static void btrfs_truncate(struct inode *inode);
84 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
93 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
94 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
95 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
103 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
105 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
109 static int cow_file_range(struct inode *inode, u64 start, u64 end)
111 struct btrfs_root *root = BTRFS_I(inode)->root;
112 struct btrfs_trans_handle *trans;
116 u64 blocksize = root->sectorsize;
118 struct btrfs_key ins;
119 struct extent_map *em;
120 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
123 trans = btrfs_join_transaction(root, 1);
125 btrfs_set_trans_block_group(trans, inode);
127 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
128 num_bytes = max(blocksize, num_bytes);
129 orig_num_bytes = num_bytes;
131 if (alloc_hint == EXTENT_MAP_INLINE)
134 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
135 mutex_lock(&BTRFS_I(inode)->extent_mutex);
136 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
137 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
139 while(num_bytes > 0) {
140 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
141 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
142 root->sectorsize, 0, 0,
148 em = alloc_extent_map(GFP_NOFS);
150 em->len = ins.offset;
151 em->block_start = ins.objectid;
152 em->bdev = root->fs_info->fs_devices->latest_bdev;
153 mutex_lock(&BTRFS_I(inode)->extent_mutex);
154 set_bit(EXTENT_FLAG_PINNED, &em->flags);
156 spin_lock(&em_tree->lock);
157 ret = add_extent_mapping(em_tree, em);
158 spin_unlock(&em_tree->lock);
159 if (ret != -EEXIST) {
163 btrfs_drop_extent_cache(inode, start,
164 start + ins.offset - 1);
166 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
168 cur_alloc_size = ins.offset;
169 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
172 if (num_bytes < cur_alloc_size) {
173 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
177 num_bytes -= cur_alloc_size;
178 alloc_hint = ins.objectid + ins.offset;
179 start += cur_alloc_size;
182 btrfs_end_transaction(trans, root);
186 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
193 struct btrfs_root *root = BTRFS_I(inode)->root;
194 struct btrfs_block_group_cache *block_group;
195 struct btrfs_trans_handle *trans;
196 struct extent_buffer *leaf;
198 struct btrfs_path *path;
199 struct btrfs_file_extent_item *item;
202 struct btrfs_key found_key;
204 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
205 path = btrfs_alloc_path();
207 trans = btrfs_join_transaction(root, 1);
210 ret = btrfs_lookup_file_extent(NULL, root, path,
211 inode->i_ino, start, 0);
218 if (path->slots[0] == 0)
223 leaf = path->nodes[0];
224 item = btrfs_item_ptr(leaf, path->slots[0],
225 struct btrfs_file_extent_item);
227 /* are we inside the extent that was found? */
228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
229 found_type = btrfs_key_type(&found_key);
230 if (found_key.objectid != inode->i_ino ||
231 found_type != BTRFS_EXTENT_DATA_KEY)
234 found_type = btrfs_file_extent_type(leaf, item);
235 extent_start = found_key.offset;
236 if (found_type == BTRFS_FILE_EXTENT_REG) {
237 u64 extent_num_bytes;
239 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
240 extent_end = extent_start + extent_num_bytes;
243 if (loops && start != extent_start)
246 if (start < extent_start || start >= extent_end)
249 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
253 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
256 * we may be called by the resizer, make sure we're inside
257 * the limits of the FS
259 block_group = btrfs_lookup_block_group(root->fs_info,
261 if (!block_group || block_group->ro)
264 bytenr += btrfs_file_extent_offset(leaf, item);
265 extent_num_bytes = min(end + 1, extent_end) - start;
266 ret = btrfs_add_ordered_extent(inode, start, bytenr,
267 extent_num_bytes, 1);
273 btrfs_release_path(root, path);
281 btrfs_end_transaction(trans, root);
282 btrfs_free_path(path);
283 return cow_file_range(inode, start, end);
287 btrfs_end_transaction(trans, root);
288 btrfs_free_path(path);
292 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
294 struct btrfs_root *root = BTRFS_I(inode)->root;
297 if (btrfs_test_opt(root, NODATACOW) ||
298 btrfs_test_flag(inode, NODATACOW))
299 ret = run_delalloc_nocow(inode, start, end);
301 ret = cow_file_range(inode, start, end);
306 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
307 unsigned long old, unsigned long bits)
310 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
311 struct btrfs_root *root = BTRFS_I(inode)->root;
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
313 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
314 root->fs_info->delalloc_bytes += end - start + 1;
315 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
316 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
317 &root->fs_info->delalloc_inodes);
319 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
324 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
325 unsigned long old, unsigned long bits)
327 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
328 struct btrfs_root *root = BTRFS_I(inode)->root;
331 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
332 if (end - start + 1 > root->fs_info->delalloc_bytes) {
333 printk("warning: delalloc account %Lu %Lu\n",
334 end - start + 1, root->fs_info->delalloc_bytes);
335 root->fs_info->delalloc_bytes = 0;
336 BTRFS_I(inode)->delalloc_bytes = 0;
338 root->fs_info->delalloc_bytes -= end - start + 1;
339 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
341 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
342 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
343 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
345 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
350 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
351 size_t size, struct bio *bio)
353 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
354 struct btrfs_mapping_tree *map_tree;
355 u64 logical = bio->bi_sector << 9;
360 length = bio->bi_size;
361 map_tree = &root->fs_info->mapping_tree;
363 ret = btrfs_map_block(map_tree, READ, logical,
364 &map_length, NULL, 0);
366 if (map_length < length + size) {
372 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
375 struct btrfs_root *root = BTRFS_I(inode)->root;
378 ret = btrfs_csum_one_bio(root, inode, bio);
381 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
384 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
387 struct btrfs_root *root = BTRFS_I(inode)->root;
390 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
393 if (btrfs_test_opt(root, NODATASUM) ||
394 btrfs_test_flag(inode, NODATASUM)) {
398 if (!(rw & (1 << BIO_RW))) {
399 btrfs_lookup_bio_sums(root, inode, bio);
402 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
403 inode, rw, bio, mirror_num,
404 __btrfs_submit_bio_hook);
406 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
409 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
410 struct inode *inode, u64 file_offset,
411 struct list_head *list)
413 struct list_head *cur;
414 struct btrfs_ordered_sum *sum;
416 btrfs_set_trans_block_group(trans, inode);
417 list_for_each(cur, list) {
418 sum = list_entry(cur, struct btrfs_ordered_sum, list);
419 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
425 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
427 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
431 struct btrfs_writepage_fixup {
433 struct btrfs_work work;
436 /* see btrfs_writepage_start_hook for details on why this is required */
437 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
439 struct btrfs_writepage_fixup *fixup;
440 struct btrfs_ordered_extent *ordered;
446 fixup = container_of(work, struct btrfs_writepage_fixup, work);
450 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
451 ClearPageChecked(page);
455 inode = page->mapping->host;
456 page_start = page_offset(page);
457 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
459 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
461 /* already ordered? We're done */
462 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
463 EXTENT_ORDERED, 0)) {
467 ordered = btrfs_lookup_ordered_extent(inode, page_start);
469 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
472 btrfs_start_ordered_extent(inode, ordered, 1);
476 btrfs_set_extent_delalloc(inode, page_start, page_end);
477 ClearPageChecked(page);
479 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
482 page_cache_release(page);
486 * There are a few paths in the higher layers of the kernel that directly
487 * set the page dirty bit without asking the filesystem if it is a
488 * good idea. This causes problems because we want to make sure COW
489 * properly happens and the data=ordered rules are followed.
491 * In our case any range that doesn't have the EXTENT_ORDERED bit set
492 * hasn't been properly setup for IO. We kick off an async process
493 * to fix it up. The async helper will wait for ordered extents, set
494 * the delalloc bit and make it safe to write the page.
496 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
498 struct inode *inode = page->mapping->host;
499 struct btrfs_writepage_fixup *fixup;
500 struct btrfs_root *root = BTRFS_I(inode)->root;
503 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
508 if (PageChecked(page))
511 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
515 SetPageChecked(page);
516 page_cache_get(page);
517 fixup->work.func = btrfs_writepage_fixup_worker;
519 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
523 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
525 struct btrfs_root *root = BTRFS_I(inode)->root;
526 struct btrfs_trans_handle *trans;
527 struct btrfs_ordered_extent *ordered_extent;
528 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
530 struct list_head list;
531 struct btrfs_key ins;
534 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
538 trans = btrfs_join_transaction(root, 1);
540 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
541 BUG_ON(!ordered_extent);
542 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
545 lock_extent(io_tree, ordered_extent->file_offset,
546 ordered_extent->file_offset + ordered_extent->len - 1,
549 INIT_LIST_HEAD(&list);
551 ins.objectid = ordered_extent->start;
552 ins.offset = ordered_extent->len;
553 ins.type = BTRFS_EXTENT_ITEM_KEY;
555 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
556 trans->transid, inode->i_ino,
557 ordered_extent->file_offset, &ins);
560 mutex_lock(&BTRFS_I(inode)->extent_mutex);
562 ret = btrfs_drop_extents(trans, root, inode,
563 ordered_extent->file_offset,
564 ordered_extent->file_offset +
566 ordered_extent->file_offset, &alloc_hint);
568 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
569 ordered_extent->file_offset,
570 ordered_extent->start,
572 ordered_extent->len, 0);
575 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
576 ordered_extent->file_offset +
577 ordered_extent->len - 1);
578 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
580 inode->i_blocks += ordered_extent->len >> 9;
581 unlock_extent(io_tree, ordered_extent->file_offset,
582 ordered_extent->file_offset + ordered_extent->len - 1,
585 add_pending_csums(trans, inode, ordered_extent->file_offset,
586 &ordered_extent->list);
588 btrfs_ordered_update_i_size(inode, ordered_extent);
589 btrfs_remove_ordered_extent(inode, ordered_extent);
592 btrfs_put_ordered_extent(ordered_extent);
593 /* once for the tree */
594 btrfs_put_ordered_extent(ordered_extent);
596 btrfs_update_inode(trans, root, inode);
597 btrfs_end_transaction(trans, root);
601 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
602 struct extent_state *state, int uptodate)
604 return btrfs_finish_ordered_io(page->mapping->host, start, end);
607 struct io_failure_record {
615 int btrfs_io_failed_hook(struct bio *failed_bio,
616 struct page *page, u64 start, u64 end,
617 struct extent_state *state)
619 struct io_failure_record *failrec = NULL;
621 struct extent_map *em;
622 struct inode *inode = page->mapping->host;
623 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
631 ret = get_state_private(failure_tree, start, &private);
633 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
636 failrec->start = start;
637 failrec->len = end - start + 1;
638 failrec->last_mirror = 0;
640 spin_lock(&em_tree->lock);
641 em = lookup_extent_mapping(em_tree, start, failrec->len);
642 if (em->start > start || em->start + em->len < start) {
646 spin_unlock(&em_tree->lock);
648 if (!em || IS_ERR(em)) {
652 logical = start - em->start;
653 logical = em->block_start + logical;
654 failrec->logical = logical;
656 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
657 EXTENT_DIRTY, GFP_NOFS);
658 set_state_private(failure_tree, start,
659 (u64)(unsigned long)failrec);
661 failrec = (struct io_failure_record *)(unsigned long)private;
663 num_copies = btrfs_num_copies(
664 &BTRFS_I(inode)->root->fs_info->mapping_tree,
665 failrec->logical, failrec->len);
666 failrec->last_mirror++;
668 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
669 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
672 if (state && state->start != failrec->start)
674 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
676 if (!state || failrec->last_mirror > num_copies) {
677 set_state_private(failure_tree, failrec->start, 0);
678 clear_extent_bits(failure_tree, failrec->start,
679 failrec->start + failrec->len - 1,
680 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
684 bio = bio_alloc(GFP_NOFS, 1);
685 bio->bi_private = state;
686 bio->bi_end_io = failed_bio->bi_end_io;
687 bio->bi_sector = failrec->logical >> 9;
688 bio->bi_bdev = failed_bio->bi_bdev;
690 bio_add_page(bio, page, failrec->len, start - page_offset(page));
691 if (failed_bio->bi_rw & (1 << BIO_RW))
696 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
697 failrec->last_mirror);
701 int btrfs_clean_io_failures(struct inode *inode, u64 start)
705 struct io_failure_record *failure;
709 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
710 (u64)-1, 1, EXTENT_DIRTY)) {
711 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
712 start, &private_failure);
714 failure = (struct io_failure_record *)(unsigned long)
716 set_state_private(&BTRFS_I(inode)->io_failure_tree,
718 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
720 failure->start + failure->len - 1,
721 EXTENT_DIRTY | EXTENT_LOCKED,
729 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
730 struct extent_state *state)
732 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
733 struct inode *inode = page->mapping->host;
734 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
736 u64 private = ~(u32)0;
738 struct btrfs_root *root = BTRFS_I(inode)->root;
742 if (btrfs_test_opt(root, NODATASUM) ||
743 btrfs_test_flag(inode, NODATASUM))
745 if (state && state->start == start) {
746 private = state->private;
749 ret = get_state_private(io_tree, start, &private);
751 local_irq_save(flags);
752 kaddr = kmap_atomic(page, KM_IRQ0);
756 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
757 btrfs_csum_final(csum, (char *)&csum);
758 if (csum != private) {
761 kunmap_atomic(kaddr, KM_IRQ0);
762 local_irq_restore(flags);
764 /* if the io failure tree for this inode is non-empty,
765 * check to see if we've recovered from a failed IO
767 btrfs_clean_io_failures(inode, start);
771 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
772 page->mapping->host->i_ino, (unsigned long long)start, csum,
774 memset(kaddr + offset, 1, end - start + 1);
775 flush_dcache_page(page);
776 kunmap_atomic(kaddr, KM_IRQ0);
777 local_irq_restore(flags);
784 * This creates an orphan entry for the given inode in case something goes
785 * wrong in the middle of an unlink/truncate.
787 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
789 struct btrfs_root *root = BTRFS_I(inode)->root;
792 spin_lock(&root->list_lock);
794 /* already on the orphan list, we're good */
795 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
796 spin_unlock(&root->list_lock);
800 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
802 spin_unlock(&root->list_lock);
805 * insert an orphan item to track this unlinked/truncated file
807 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
813 * We have done the truncate/delete so we can go ahead and remove the orphan
814 * item for this particular inode.
816 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
818 struct btrfs_root *root = BTRFS_I(inode)->root;
821 spin_lock(&root->list_lock);
823 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
824 spin_unlock(&root->list_lock);
828 list_del_init(&BTRFS_I(inode)->i_orphan);
830 spin_unlock(&root->list_lock);
834 spin_unlock(&root->list_lock);
836 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
842 * this cleans up any orphans that may be left on the list from the last use
845 void btrfs_orphan_cleanup(struct btrfs_root *root)
847 struct btrfs_path *path;
848 struct extent_buffer *leaf;
849 struct btrfs_item *item;
850 struct btrfs_key key, found_key;
851 struct btrfs_trans_handle *trans;
853 int ret = 0, nr_unlink = 0, nr_truncate = 0;
855 /* don't do orphan cleanup if the fs is readonly. */
856 if (root->inode->i_sb->s_flags & MS_RDONLY)
859 path = btrfs_alloc_path();
864 key.objectid = BTRFS_ORPHAN_OBJECTID;
865 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
866 key.offset = (u64)-1;
868 trans = btrfs_start_transaction(root, 1);
869 btrfs_set_trans_block_group(trans, root->inode);
872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
874 printk(KERN_ERR "Error searching slot for orphan: %d"
880 * if ret == 0 means we found what we were searching for, which
881 * is weird, but possible, so only screw with path if we didnt
882 * find the key and see if we have stuff that matches
885 if (path->slots[0] == 0)
890 /* pull out the item */
891 leaf = path->nodes[0];
892 item = btrfs_item_nr(leaf, path->slots[0]);
893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
895 /* make sure the item matches what we want */
896 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
898 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
901 /* release the path since we're done with it */
902 btrfs_release_path(root, path);
905 * this is where we are basically btrfs_lookup, without the
906 * crossing root thing. we store the inode number in the
907 * offset of the orphan item.
909 inode = btrfs_iget_locked(root->inode->i_sb,
910 found_key.offset, root);
914 if (inode->i_state & I_NEW) {
915 BTRFS_I(inode)->root = root;
917 /* have to set the location manually */
918 BTRFS_I(inode)->location.objectid = inode->i_ino;
919 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
920 BTRFS_I(inode)->location.offset = 0;
922 btrfs_read_locked_inode(inode);
923 unlock_new_inode(inode);
927 * add this inode to the orphan list so btrfs_orphan_del does
928 * the proper thing when we hit it
930 spin_lock(&root->list_lock);
931 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
932 spin_unlock(&root->list_lock);
935 * if this is a bad inode, means we actually succeeded in
936 * removing the inode, but not the orphan record, which means
937 * we need to manually delete the orphan since iput will just
940 if (is_bad_inode(inode)) {
941 btrfs_orphan_del(trans, inode);
946 /* if we have links, this was a truncate, lets do that */
947 if (inode->i_nlink) {
949 btrfs_truncate(inode);
954 /* this will do delete_inode and everything for us */
959 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
961 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
963 btrfs_free_path(path);
964 btrfs_end_transaction(trans, root);
967 void btrfs_read_locked_inode(struct inode *inode)
969 struct btrfs_path *path;
970 struct extent_buffer *leaf;
971 struct btrfs_inode_item *inode_item;
972 struct btrfs_timespec *tspec;
973 struct btrfs_root *root = BTRFS_I(inode)->root;
974 struct btrfs_key location;
975 u64 alloc_group_block;
979 path = btrfs_alloc_path();
981 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
983 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
987 leaf = path->nodes[0];
988 inode_item = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_inode_item);
991 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
992 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
993 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
994 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
995 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
997 tspec = btrfs_inode_atime(inode_item);
998 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
999 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1001 tspec = btrfs_inode_mtime(inode_item);
1002 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1003 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1005 tspec = btrfs_inode_ctime(inode_item);
1006 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1007 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1009 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1010 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
1012 rdev = btrfs_inode_rdev(leaf, inode_item);
1014 BTRFS_I(inode)->index_cnt = (u64)-1;
1016 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1017 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1019 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1020 if (!BTRFS_I(inode)->block_group) {
1021 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1023 BTRFS_BLOCK_GROUP_METADATA, 0);
1025 btrfs_free_path(path);
1028 switch (inode->i_mode & S_IFMT) {
1030 inode->i_mapping->a_ops = &btrfs_aops;
1031 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1032 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1033 inode->i_fop = &btrfs_file_operations;
1034 inode->i_op = &btrfs_file_inode_operations;
1037 inode->i_fop = &btrfs_dir_file_operations;
1038 if (root == root->fs_info->tree_root)
1039 inode->i_op = &btrfs_dir_ro_inode_operations;
1041 inode->i_op = &btrfs_dir_inode_operations;
1044 inode->i_op = &btrfs_symlink_inode_operations;
1045 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1046 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1049 init_special_inode(inode, inode->i_mode, rdev);
1055 btrfs_free_path(path);
1056 make_bad_inode(inode);
1059 static void fill_inode_item(struct extent_buffer *leaf,
1060 struct btrfs_inode_item *item,
1061 struct inode *inode)
1063 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1064 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1065 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1066 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1067 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1069 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1070 inode->i_atime.tv_sec);
1071 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1072 inode->i_atime.tv_nsec);
1074 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1075 inode->i_mtime.tv_sec);
1076 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1077 inode->i_mtime.tv_nsec);
1079 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1080 inode->i_ctime.tv_sec);
1081 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1082 inode->i_ctime.tv_nsec);
1084 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1085 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1086 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1087 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1088 btrfs_set_inode_block_group(leaf, item,
1089 BTRFS_I(inode)->block_group->key.objectid);
1092 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1093 struct btrfs_root *root,
1094 struct inode *inode)
1096 struct btrfs_inode_item *inode_item;
1097 struct btrfs_path *path;
1098 struct extent_buffer *leaf;
1101 path = btrfs_alloc_path();
1103 ret = btrfs_lookup_inode(trans, root, path,
1104 &BTRFS_I(inode)->location, 1);
1111 leaf = path->nodes[0];
1112 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1113 struct btrfs_inode_item);
1115 fill_inode_item(leaf, inode_item, inode);
1116 btrfs_mark_buffer_dirty(leaf);
1117 btrfs_set_inode_last_trans(trans, inode);
1120 btrfs_free_path(path);
1125 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1126 struct btrfs_root *root,
1128 struct dentry *dentry)
1130 struct btrfs_path *path;
1131 const char *name = dentry->d_name.name;
1132 int name_len = dentry->d_name.len;
1134 struct extent_buffer *leaf;
1135 struct btrfs_dir_item *di;
1136 struct btrfs_key key;
1139 path = btrfs_alloc_path();
1145 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1146 name, name_len, -1);
1155 leaf = path->nodes[0];
1156 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1157 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1160 btrfs_release_path(root, path);
1162 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1163 dentry->d_inode->i_ino,
1164 dentry->d_parent->d_inode->i_ino, &index);
1166 printk("failed to delete reference to %.*s, "
1167 "inode %lu parent %lu\n", name_len, name,
1168 dentry->d_inode->i_ino,
1169 dentry->d_parent->d_inode->i_ino);
1173 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1174 index, name, name_len, -1);
1183 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1184 btrfs_release_path(root, path);
1186 dentry->d_inode->i_ctime = dir->i_ctime;
1188 btrfs_free_path(path);
1190 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1191 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1192 btrfs_update_inode(trans, root, dir);
1193 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1194 dentry->d_inode->i_nlink--;
1196 drop_nlink(dentry->d_inode);
1198 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1199 dir->i_sb->s_dirt = 1;
1204 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1206 struct btrfs_root *root;
1207 struct btrfs_trans_handle *trans;
1208 struct inode *inode = dentry->d_inode;
1210 unsigned long nr = 0;
1212 root = BTRFS_I(dir)->root;
1214 ret = btrfs_check_free_space(root, 1, 1);
1218 trans = btrfs_start_transaction(root, 1);
1220 btrfs_set_trans_block_group(trans, dir);
1221 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1223 if (inode->i_nlink == 0)
1224 ret = btrfs_orphan_add(trans, inode);
1226 nr = trans->blocks_used;
1228 btrfs_end_transaction_throttle(trans, root);
1230 btrfs_btree_balance_dirty(root, nr);
1234 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1236 struct inode *inode = dentry->d_inode;
1239 struct btrfs_root *root = BTRFS_I(dir)->root;
1240 struct btrfs_trans_handle *trans;
1241 unsigned long nr = 0;
1243 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1247 ret = btrfs_check_free_space(root, 1, 1);
1251 trans = btrfs_start_transaction(root, 1);
1252 btrfs_set_trans_block_group(trans, dir);
1254 err = btrfs_orphan_add(trans, inode);
1258 /* now the directory is empty */
1259 err = btrfs_unlink_trans(trans, root, dir, dentry);
1261 btrfs_i_size_write(inode, 0);
1265 nr = trans->blocks_used;
1266 ret = btrfs_end_transaction_throttle(trans, root);
1268 btrfs_btree_balance_dirty(root, nr);
1276 * this can truncate away extent items, csum items and directory items.
1277 * It starts at a high offset and removes keys until it can't find
1278 * any higher than i_size.
1280 * csum items that cross the new i_size are truncated to the new size
1283 * min_type is the minimum key type to truncate down to. If set to 0, this
1284 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1286 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1287 struct btrfs_root *root,
1288 struct inode *inode,
1292 struct btrfs_path *path;
1293 struct btrfs_key key;
1294 struct btrfs_key found_key;
1296 struct extent_buffer *leaf;
1297 struct btrfs_file_extent_item *fi;
1298 u64 extent_start = 0;
1299 u64 extent_num_bytes = 0;
1305 int pending_del_nr = 0;
1306 int pending_del_slot = 0;
1307 int extent_type = -1;
1308 u64 mask = root->sectorsize - 1;
1310 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1311 path = btrfs_alloc_path();
1315 /* FIXME, add redo link to tree so we don't leak on crash */
1316 key.objectid = inode->i_ino;
1317 key.offset = (u64)-1;
1320 btrfs_init_path(path);
1322 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1327 BUG_ON(path->slots[0] == 0);
1333 leaf = path->nodes[0];
1334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1335 found_type = btrfs_key_type(&found_key);
1337 if (found_key.objectid != inode->i_ino)
1340 if (found_type < min_type)
1343 item_end = found_key.offset;
1344 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1345 fi = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(leaf, fi);
1348 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1350 btrfs_file_extent_num_bytes(leaf, fi);
1351 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1352 struct btrfs_item *item = btrfs_item_nr(leaf,
1354 item_end += btrfs_file_extent_inline_len(leaf,
1359 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1360 ret = btrfs_csum_truncate(trans, root, path,
1364 if (item_end < inode->i_size) {
1365 if (found_type == BTRFS_DIR_ITEM_KEY) {
1366 found_type = BTRFS_INODE_ITEM_KEY;
1367 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1368 found_type = BTRFS_CSUM_ITEM_KEY;
1369 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1370 found_type = BTRFS_XATTR_ITEM_KEY;
1371 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1372 found_type = BTRFS_INODE_REF_KEY;
1373 } else if (found_type) {
1378 btrfs_set_key_type(&key, found_type);
1381 if (found_key.offset >= inode->i_size)
1387 /* FIXME, shrink the extent if the ref count is only 1 */
1388 if (found_type != BTRFS_EXTENT_DATA_KEY)
1391 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1393 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1395 u64 orig_num_bytes =
1396 btrfs_file_extent_num_bytes(leaf, fi);
1397 extent_num_bytes = inode->i_size -
1398 found_key.offset + root->sectorsize - 1;
1399 extent_num_bytes = extent_num_bytes &
1400 ~((u64)root->sectorsize - 1);
1401 btrfs_set_file_extent_num_bytes(leaf, fi,
1403 num_dec = (orig_num_bytes -
1405 if (extent_start != 0)
1406 dec_i_blocks(inode, num_dec);
1407 btrfs_mark_buffer_dirty(leaf);
1410 btrfs_file_extent_disk_num_bytes(leaf,
1412 /* FIXME blocksize != 4096 */
1413 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1414 if (extent_start != 0) {
1416 dec_i_blocks(inode, num_dec);
1418 root_gen = btrfs_header_generation(leaf);
1419 root_owner = btrfs_header_owner(leaf);
1421 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1423 u32 newsize = inode->i_size - found_key.offset;
1424 dec_i_blocks(inode, item_end + 1 -
1425 found_key.offset - newsize);
1427 btrfs_file_extent_calc_inline_size(newsize);
1428 ret = btrfs_truncate_item(trans, root, path,
1432 dec_i_blocks(inode, item_end + 1 -
1438 if (!pending_del_nr) {
1439 /* no pending yet, add ourselves */
1440 pending_del_slot = path->slots[0];
1442 } else if (pending_del_nr &&
1443 path->slots[0] + 1 == pending_del_slot) {
1444 /* hop on the pending chunk */
1446 pending_del_slot = path->slots[0];
1448 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1454 ret = btrfs_free_extent(trans, root, extent_start,
1457 root_gen, inode->i_ino,
1458 found_key.offset, 0);
1462 if (path->slots[0] == 0) {
1465 btrfs_release_path(root, path);
1470 if (pending_del_nr &&
1471 path->slots[0] + 1 != pending_del_slot) {
1472 struct btrfs_key debug;
1474 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1476 ret = btrfs_del_items(trans, root, path,
1481 btrfs_release_path(root, path);
1487 if (pending_del_nr) {
1488 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1491 btrfs_free_path(path);
1492 inode->i_sb->s_dirt = 1;
1497 * taken from block_truncate_page, but does cow as it zeros out
1498 * any bytes left in the last page in the file.
1500 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1502 struct inode *inode = mapping->host;
1503 struct btrfs_root *root = BTRFS_I(inode)->root;
1504 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1505 struct btrfs_ordered_extent *ordered;
1507 u32 blocksize = root->sectorsize;
1508 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1509 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1515 if ((offset & (blocksize - 1)) == 0)
1520 page = grab_cache_page(mapping, index);
1524 page_start = page_offset(page);
1525 page_end = page_start + PAGE_CACHE_SIZE - 1;
1527 if (!PageUptodate(page)) {
1528 ret = btrfs_readpage(NULL, page);
1530 if (page->mapping != mapping) {
1532 page_cache_release(page);
1535 if (!PageUptodate(page)) {
1540 wait_on_page_writeback(page);
1542 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1543 set_page_extent_mapped(page);
1545 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1547 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1549 page_cache_release(page);
1550 btrfs_start_ordered_extent(inode, ordered, 1);
1551 btrfs_put_ordered_extent(ordered);
1555 btrfs_set_extent_delalloc(inode, page_start, page_end);
1557 if (offset != PAGE_CACHE_SIZE) {
1559 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1560 flush_dcache_page(page);
1563 ClearPageChecked(page);
1564 set_page_dirty(page);
1565 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1569 page_cache_release(page);
1574 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1576 struct inode *inode = dentry->d_inode;
1579 err = inode_change_ok(inode, attr);
1583 if (S_ISREG(inode->i_mode) &&
1584 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1585 struct btrfs_trans_handle *trans;
1586 struct btrfs_root *root = BTRFS_I(inode)->root;
1587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1589 u64 mask = root->sectorsize - 1;
1590 u64 hole_start = (inode->i_size + mask) & ~mask;
1591 u64 block_end = (attr->ia_size + mask) & ~mask;
1595 if (attr->ia_size <= hole_start)
1598 err = btrfs_check_free_space(root, 1, 0);
1602 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1604 hole_size = block_end - hole_start;
1606 struct btrfs_ordered_extent *ordered;
1607 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1609 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1610 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1612 unlock_extent(io_tree, hole_start,
1613 block_end - 1, GFP_NOFS);
1614 btrfs_put_ordered_extent(ordered);
1620 trans = btrfs_start_transaction(root, 1);
1621 btrfs_set_trans_block_group(trans, inode);
1622 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1623 err = btrfs_drop_extents(trans, root, inode,
1624 hole_start, block_end, hole_start,
1627 if (alloc_hint != EXTENT_MAP_INLINE) {
1628 err = btrfs_insert_file_extent(trans, root,
1632 btrfs_drop_extent_cache(inode, hole_start,
1634 btrfs_check_file(root, inode);
1636 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1637 btrfs_end_transaction(trans, root);
1638 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1643 err = inode_setattr(inode, attr);
1645 if (!err && ((attr->ia_valid & ATTR_MODE)))
1646 err = btrfs_acl_chmod(inode);
1651 void btrfs_delete_inode(struct inode *inode)
1653 struct btrfs_trans_handle *trans;
1654 struct btrfs_root *root = BTRFS_I(inode)->root;
1658 truncate_inode_pages(&inode->i_data, 0);
1659 if (is_bad_inode(inode)) {
1660 btrfs_orphan_del(NULL, inode);
1663 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1665 btrfs_i_size_write(inode, 0);
1666 trans = btrfs_start_transaction(root, 1);
1668 btrfs_set_trans_block_group(trans, inode);
1669 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1671 btrfs_orphan_del(NULL, inode);
1672 goto no_delete_lock;
1675 btrfs_orphan_del(trans, inode);
1677 nr = trans->blocks_used;
1680 btrfs_end_transaction(trans, root);
1681 btrfs_btree_balance_dirty(root, nr);
1685 nr = trans->blocks_used;
1686 btrfs_end_transaction(trans, root);
1687 btrfs_btree_balance_dirty(root, nr);
1693 * this returns the key found in the dir entry in the location pointer.
1694 * If no dir entries were found, location->objectid is 0.
1696 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1697 struct btrfs_key *location)
1699 const char *name = dentry->d_name.name;
1700 int namelen = dentry->d_name.len;
1701 struct btrfs_dir_item *di;
1702 struct btrfs_path *path;
1703 struct btrfs_root *root = BTRFS_I(dir)->root;
1706 path = btrfs_alloc_path();
1709 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1713 if (!di || IS_ERR(di)) {
1716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1718 btrfs_free_path(path);
1721 location->objectid = 0;
1726 * when we hit a tree root in a directory, the btrfs part of the inode
1727 * needs to be changed to reflect the root directory of the tree root. This
1728 * is kind of like crossing a mount point.
1730 static int fixup_tree_root_location(struct btrfs_root *root,
1731 struct btrfs_key *location,
1732 struct btrfs_root **sub_root,
1733 struct dentry *dentry)
1735 struct btrfs_root_item *ri;
1737 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1739 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1742 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1743 dentry->d_name.name,
1744 dentry->d_name.len);
1745 if (IS_ERR(*sub_root))
1746 return PTR_ERR(*sub_root);
1748 ri = &(*sub_root)->root_item;
1749 location->objectid = btrfs_root_dirid(ri);
1750 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1751 location->offset = 0;
1756 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1758 struct btrfs_iget_args *args = p;
1759 inode->i_ino = args->ino;
1760 BTRFS_I(inode)->root = args->root;
1761 BTRFS_I(inode)->delalloc_bytes = 0;
1762 inode->i_mapping->writeback_index = 0;
1763 BTRFS_I(inode)->disk_i_size = 0;
1764 BTRFS_I(inode)->index_cnt = (u64)-1;
1765 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1766 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1767 inode->i_mapping, GFP_NOFS);
1768 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1769 inode->i_mapping, GFP_NOFS);
1770 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
1771 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1772 mutex_init(&BTRFS_I(inode)->csum_mutex);
1773 mutex_init(&BTRFS_I(inode)->extent_mutex);
1777 static int btrfs_find_actor(struct inode *inode, void *opaque)
1779 struct btrfs_iget_args *args = opaque;
1780 return (args->ino == inode->i_ino &&
1781 args->root == BTRFS_I(inode)->root);
1784 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1787 struct btrfs_iget_args args;
1788 args.ino = objectid;
1789 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1794 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1797 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1798 struct btrfs_root *root)
1800 struct inode *inode;
1801 struct btrfs_iget_args args;
1802 args.ino = objectid;
1805 inode = iget5_locked(s, objectid, btrfs_find_actor,
1806 btrfs_init_locked_inode,
1811 /* Get an inode object given its location and corresponding root.
1812 * Returns in *is_new if the inode was read from disk
1814 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1815 struct btrfs_root *root, int *is_new)
1817 struct inode *inode;
1819 inode = btrfs_iget_locked(s, location->objectid, root);
1821 return ERR_PTR(-EACCES);
1823 if (inode->i_state & I_NEW) {
1824 BTRFS_I(inode)->root = root;
1825 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1826 btrfs_read_locked_inode(inode);
1827 unlock_new_inode(inode);
1838 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1839 struct nameidata *nd)
1841 struct inode * inode;
1842 struct btrfs_inode *bi = BTRFS_I(dir);
1843 struct btrfs_root *root = bi->root;
1844 struct btrfs_root *sub_root = root;
1845 struct btrfs_key location;
1846 int ret, new, do_orphan = 0;
1848 if (dentry->d_name.len > BTRFS_NAME_LEN)
1849 return ERR_PTR(-ENAMETOOLONG);
1851 ret = btrfs_inode_by_name(dir, dentry, &location);
1854 return ERR_PTR(ret);
1857 if (location.objectid) {
1858 ret = fixup_tree_root_location(root, &location, &sub_root,
1861 return ERR_PTR(ret);
1863 return ERR_PTR(-ENOENT);
1864 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
1866 return ERR_CAST(inode);
1868 /* the inode and parent dir are two different roots */
1869 if (new && root != sub_root) {
1871 sub_root->inode = inode;
1876 if (unlikely(do_orphan))
1877 btrfs_orphan_cleanup(sub_root);
1879 return d_splice_alias(inode, dentry);
1882 static unsigned char btrfs_filetype_table[] = {
1883 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1886 static int btrfs_real_readdir(struct file *filp, void *dirent,
1889 struct inode *inode = filp->f_dentry->d_inode;
1890 struct btrfs_root *root = BTRFS_I(inode)->root;
1891 struct btrfs_item *item;
1892 struct btrfs_dir_item *di;
1893 struct btrfs_key key;
1894 struct btrfs_key found_key;
1895 struct btrfs_path *path;
1898 struct extent_buffer *leaf;
1901 unsigned char d_type;
1906 int key_type = BTRFS_DIR_INDEX_KEY;
1911 /* FIXME, use a real flag for deciding about the key type */
1912 if (root->fs_info->tree_root == root)
1913 key_type = BTRFS_DIR_ITEM_KEY;
1915 /* special case for "." */
1916 if (filp->f_pos == 0) {
1917 over = filldir(dirent, ".", 1,
1924 /* special case for .., just use the back ref */
1925 if (filp->f_pos == 1) {
1926 u64 pino = parent_ino(filp->f_path.dentry);
1927 over = filldir(dirent, "..", 2,
1934 path = btrfs_alloc_path();
1937 btrfs_set_key_type(&key, key_type);
1938 key.offset = filp->f_pos;
1939 key.objectid = inode->i_ino;
1941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1947 leaf = path->nodes[0];
1948 nritems = btrfs_header_nritems(leaf);
1949 slot = path->slots[0];
1950 if (advance || slot >= nritems) {
1951 if (slot >= nritems - 1) {
1952 ret = btrfs_next_leaf(root, path);
1955 leaf = path->nodes[0];
1956 nritems = btrfs_header_nritems(leaf);
1957 slot = path->slots[0];
1964 item = btrfs_item_nr(leaf, slot);
1965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1967 if (found_key.objectid != key.objectid)
1969 if (btrfs_key_type(&found_key) != key_type)
1971 if (found_key.offset < filp->f_pos)
1974 filp->f_pos = found_key.offset;
1976 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1978 di_total = btrfs_item_size(leaf, item);
1980 while (di_cur < di_total) {
1981 struct btrfs_key location;
1983 name_len = btrfs_dir_name_len(leaf, di);
1984 if (name_len <= sizeof(tmp_name)) {
1985 name_ptr = tmp_name;
1987 name_ptr = kmalloc(name_len, GFP_NOFS);
1993 read_extent_buffer(leaf, name_ptr,
1994 (unsigned long)(di + 1), name_len);
1996 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1997 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1998 over = filldir(dirent, name_ptr, name_len,
1999 found_key.offset, location.objectid,
2002 if (name_ptr != tmp_name)
2008 di_len = btrfs_dir_name_len(leaf, di) +
2009 btrfs_dir_data_len(leaf, di) + sizeof(*di);
2011 di = (struct btrfs_dir_item *)((char *)di + di_len);
2015 /* Reached end of directory/root. Bump pos past the last item. */
2016 if (key_type == BTRFS_DIR_INDEX_KEY)
2017 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2023 btrfs_free_path(path);
2027 /* Kernels earlier than 2.6.28 still have the NFS deadlock where nfsd
2028 will call the file system's ->lookup() method from within its
2029 filldir callback, which in turn was called from the file system's
2030 ->readdir() method. And will deadlock for many file systems. */
2031 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
2033 struct nfshack_dirent {
2037 unsigned int d_type;
2041 struct nfshack_readdir {
2049 static int btrfs_nfshack_filldir(void *__buf, const char *name, int namlen,
2050 loff_t offset, u64 ino, unsigned int d_type)
2052 struct nfshack_readdir *buf = __buf;
2053 struct nfshack_dirent *de = (void *)(buf->dirent + buf->used);
2054 unsigned int reclen;
2056 reclen = ALIGN(sizeof(struct nfshack_dirent) + namlen, sizeof(u64));
2057 if (buf->used + reclen > PAGE_SIZE) {
2062 de->namlen = namlen;
2063 de->offset = offset;
2065 de->d_type = d_type;
2066 memcpy(de->name, name, namlen);
2067 buf->used += reclen;
2072 static int btrfs_nfshack_readdir(struct file *file, void *dirent,
2075 struct nfshack_readdir buf;
2076 struct nfshack_dirent *de;
2081 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
2085 offset = file->f_pos;
2088 unsigned int reclen;
2092 err = btrfs_real_readdir(file, &buf, btrfs_nfshack_filldir);
2101 de = (struct nfshack_dirent *)buf.dirent;
2103 offset = de->offset;
2105 if (filldir(dirent, de->name, de->namlen, de->offset,
2106 de->ino, de->d_type))
2108 offset = file->f_pos;
2110 reclen = ALIGN(sizeof(*de) + de->namlen,
2113 de = (struct nfshack_dirent *)((char *)de + reclen);
2118 free_page((unsigned long)buf.dirent);
2119 file->f_pos = offset;
2125 int btrfs_write_inode(struct inode *inode, int wait)
2127 struct btrfs_root *root = BTRFS_I(inode)->root;
2128 struct btrfs_trans_handle *trans;
2131 if (root->fs_info->closing > 1)
2135 trans = btrfs_join_transaction(root, 1);
2136 btrfs_set_trans_block_group(trans, inode);
2137 ret = btrfs_commit_transaction(trans, root);
2143 * This is somewhat expensive, updating the tree every time the
2144 * inode changes. But, it is most likely to find the inode in cache.
2145 * FIXME, needs more benchmarking...there are no reasons other than performance
2146 * to keep or drop this code.
2148 void btrfs_dirty_inode(struct inode *inode)
2150 struct btrfs_root *root = BTRFS_I(inode)->root;
2151 struct btrfs_trans_handle *trans;
2153 trans = btrfs_join_transaction(root, 1);
2154 btrfs_set_trans_block_group(trans, inode);
2155 btrfs_update_inode(trans, root, inode);
2156 btrfs_end_transaction(trans, root);
2159 static int btrfs_set_inode_index_count(struct inode *inode)
2161 struct btrfs_root *root = BTRFS_I(inode)->root;
2162 struct btrfs_key key, found_key;
2163 struct btrfs_path *path;
2164 struct extent_buffer *leaf;
2167 key.objectid = inode->i_ino;
2168 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2169 key.offset = (u64)-1;
2171 path = btrfs_alloc_path();
2175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2178 /* FIXME: we should be able to handle this */
2184 * MAGIC NUMBER EXPLANATION:
2185 * since we search a directory based on f_pos we have to start at 2
2186 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2187 * else has to start at 2
2189 if (path->slots[0] == 0) {
2190 BTRFS_I(inode)->index_cnt = 2;
2196 leaf = path->nodes[0];
2197 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2199 if (found_key.objectid != inode->i_ino ||
2200 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2201 BTRFS_I(inode)->index_cnt = 2;
2205 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2207 btrfs_free_path(path);
2211 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2216 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2217 ret = btrfs_set_inode_index_count(dir);
2222 *index = BTRFS_I(dir)->index_cnt;
2223 BTRFS_I(dir)->index_cnt++;
2228 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root,
2231 const char *name, int name_len,
2234 struct btrfs_block_group_cache *group,
2235 int mode, u64 *index)
2237 struct inode *inode;
2238 struct btrfs_inode_item *inode_item;
2239 struct btrfs_block_group_cache *new_inode_group;
2240 struct btrfs_key *location;
2241 struct btrfs_path *path;
2242 struct btrfs_inode_ref *ref;
2243 struct btrfs_key key[2];
2249 path = btrfs_alloc_path();
2252 inode = new_inode(root->fs_info->sb);
2254 return ERR_PTR(-ENOMEM);
2257 ret = btrfs_set_inode_index(dir, inode, index);
2259 return ERR_PTR(ret);
2262 * index_cnt is ignored for everything but a dir,
2263 * btrfs_get_inode_index_count has an explanation for the magic
2266 BTRFS_I(inode)->index_cnt = 2;
2268 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2269 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2270 inode->i_mapping, GFP_NOFS);
2271 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2272 inode->i_mapping, GFP_NOFS);
2273 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2274 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2275 mutex_init(&BTRFS_I(inode)->csum_mutex);
2276 mutex_init(&BTRFS_I(inode)->extent_mutex);
2277 BTRFS_I(inode)->delalloc_bytes = 0;
2278 inode->i_mapping->writeback_index = 0;
2279 BTRFS_I(inode)->disk_i_size = 0;
2280 BTRFS_I(inode)->root = root;
2286 new_inode_group = btrfs_find_block_group(root, group, 0,
2287 BTRFS_BLOCK_GROUP_METADATA, owner);
2288 if (!new_inode_group) {
2289 printk("find_block group failed\n");
2290 new_inode_group = group;
2292 BTRFS_I(inode)->block_group = new_inode_group;
2293 BTRFS_I(inode)->flags = 0;
2295 key[0].objectid = objectid;
2296 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2299 key[1].objectid = objectid;
2300 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2301 key[1].offset = ref_objectid;
2303 sizes[0] = sizeof(struct btrfs_inode_item);
2304 sizes[1] = name_len + sizeof(*ref);
2306 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2310 if (objectid > root->highest_inode)
2311 root->highest_inode = objectid;
2313 inode->i_uid = current->fsuid;
2314 inode->i_gid = current->fsgid;
2315 inode->i_mode = mode;
2316 inode->i_ino = objectid;
2317 inode->i_blocks = 0;
2318 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2319 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2320 struct btrfs_inode_item);
2321 fill_inode_item(path->nodes[0], inode_item, inode);
2323 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2324 struct btrfs_inode_ref);
2325 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2326 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2327 ptr = (unsigned long)(ref + 1);
2328 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2330 btrfs_mark_buffer_dirty(path->nodes[0]);
2331 btrfs_free_path(path);
2333 location = &BTRFS_I(inode)->location;
2334 location->objectid = objectid;
2335 location->offset = 0;
2336 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2338 insert_inode_hash(inode);
2342 BTRFS_I(dir)->index_cnt--;
2343 btrfs_free_path(path);
2344 return ERR_PTR(ret);
2347 static inline u8 btrfs_inode_type(struct inode *inode)
2349 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2352 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2353 struct dentry *dentry, struct inode *inode,
2354 int add_backref, u64 index)
2357 struct btrfs_key key;
2358 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2359 struct inode *parent_inode = dentry->d_parent->d_inode;
2361 key.objectid = inode->i_ino;
2362 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2365 ret = btrfs_insert_dir_item(trans, root,
2366 dentry->d_name.name, dentry->d_name.len,
2367 dentry->d_parent->d_inode->i_ino,
2368 &key, btrfs_inode_type(inode),
2372 ret = btrfs_insert_inode_ref(trans, root,
2373 dentry->d_name.name,
2376 parent_inode->i_ino,
2379 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2380 dentry->d_name.len * 2);
2381 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2382 ret = btrfs_update_inode(trans, root,
2383 dentry->d_parent->d_inode);
2388 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2389 struct dentry *dentry, struct inode *inode,
2390 int backref, u64 index)
2392 int err = btrfs_add_link(trans, dentry, inode, backref, index);
2394 d_instantiate(dentry, inode);
2402 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2403 int mode, dev_t rdev)
2405 struct btrfs_trans_handle *trans;
2406 struct btrfs_root *root = BTRFS_I(dir)->root;
2407 struct inode *inode = NULL;
2411 unsigned long nr = 0;
2414 if (!new_valid_dev(rdev))
2417 err = btrfs_check_free_space(root, 1, 0);
2421 trans = btrfs_start_transaction(root, 1);
2422 btrfs_set_trans_block_group(trans, dir);
2424 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2430 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2432 dentry->d_parent->d_inode->i_ino, objectid,
2433 BTRFS_I(dir)->block_group, mode, &index);
2434 err = PTR_ERR(inode);
2438 err = btrfs_init_acl(inode, dir);
2444 btrfs_set_trans_block_group(trans, inode);
2445 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2449 inode->i_op = &btrfs_special_inode_operations;
2450 init_special_inode(inode, inode->i_mode, rdev);
2451 btrfs_update_inode(trans, root, inode);
2453 dir->i_sb->s_dirt = 1;
2454 btrfs_update_inode_block_group(trans, inode);
2455 btrfs_update_inode_block_group(trans, dir);
2457 nr = trans->blocks_used;
2458 btrfs_end_transaction_throttle(trans, root);
2461 inode_dec_link_count(inode);
2464 btrfs_btree_balance_dirty(root, nr);
2468 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2469 int mode, struct nameidata *nd)
2471 struct btrfs_trans_handle *trans;
2472 struct btrfs_root *root = BTRFS_I(dir)->root;
2473 struct inode *inode = NULL;
2476 unsigned long nr = 0;
2480 err = btrfs_check_free_space(root, 1, 0);
2483 trans = btrfs_start_transaction(root, 1);
2484 btrfs_set_trans_block_group(trans, dir);
2486 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2492 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2494 dentry->d_parent->d_inode->i_ino,
2495 objectid, BTRFS_I(dir)->block_group, mode,
2497 err = PTR_ERR(inode);
2501 err = btrfs_init_acl(inode, dir);
2507 btrfs_set_trans_block_group(trans, inode);
2508 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2512 inode->i_mapping->a_ops = &btrfs_aops;
2513 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2514 inode->i_fop = &btrfs_file_operations;
2515 inode->i_op = &btrfs_file_inode_operations;
2516 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2517 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2518 inode->i_mapping, GFP_NOFS);
2519 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2520 inode->i_mapping, GFP_NOFS);
2521 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2522 mutex_init(&BTRFS_I(inode)->csum_mutex);
2523 mutex_init(&BTRFS_I(inode)->extent_mutex);
2524 BTRFS_I(inode)->delalloc_bytes = 0;
2525 BTRFS_I(inode)->disk_i_size = 0;
2526 inode->i_mapping->writeback_index = 0;
2527 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2528 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2530 dir->i_sb->s_dirt = 1;
2531 btrfs_update_inode_block_group(trans, inode);
2532 btrfs_update_inode_block_group(trans, dir);
2534 nr = trans->blocks_used;
2535 btrfs_end_transaction_throttle(trans, root);
2538 inode_dec_link_count(inode);
2541 btrfs_btree_balance_dirty(root, nr);
2545 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2546 struct dentry *dentry)
2548 struct btrfs_trans_handle *trans;
2549 struct btrfs_root *root = BTRFS_I(dir)->root;
2550 struct inode *inode = old_dentry->d_inode;
2552 unsigned long nr = 0;
2556 if (inode->i_nlink == 0)
2559 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2564 err = btrfs_check_free_space(root, 1, 0);
2567 err = btrfs_set_inode_index(dir, inode, &index);
2571 trans = btrfs_start_transaction(root, 1);
2573 btrfs_set_trans_block_group(trans, dir);
2574 atomic_inc(&inode->i_count);
2576 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
2581 dir->i_sb->s_dirt = 1;
2582 btrfs_update_inode_block_group(trans, dir);
2583 err = btrfs_update_inode(trans, root, inode);
2588 nr = trans->blocks_used;
2589 btrfs_end_transaction_throttle(trans, root);
2592 inode_dec_link_count(inode);
2595 btrfs_btree_balance_dirty(root, nr);
2599 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2601 struct inode *inode = NULL;
2602 struct btrfs_trans_handle *trans;
2603 struct btrfs_root *root = BTRFS_I(dir)->root;
2605 int drop_on_err = 0;
2608 unsigned long nr = 1;
2610 err = btrfs_check_free_space(root, 1, 0);
2614 trans = btrfs_start_transaction(root, 1);
2615 btrfs_set_trans_block_group(trans, dir);
2617 if (IS_ERR(trans)) {
2618 err = PTR_ERR(trans);
2622 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2628 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2630 dentry->d_parent->d_inode->i_ino, objectid,
2631 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2633 if (IS_ERR(inode)) {
2634 err = PTR_ERR(inode);
2640 err = btrfs_init_acl(inode, dir);
2644 inode->i_op = &btrfs_dir_inode_operations;
2645 inode->i_fop = &btrfs_dir_file_operations;
2646 btrfs_set_trans_block_group(trans, inode);
2648 btrfs_i_size_write(inode, 0);
2649 err = btrfs_update_inode(trans, root, inode);
2653 err = btrfs_add_link(trans, dentry, inode, 0, index);
2657 d_instantiate(dentry, inode);
2659 dir->i_sb->s_dirt = 1;
2660 btrfs_update_inode_block_group(trans, inode);
2661 btrfs_update_inode_block_group(trans, dir);
2664 nr = trans->blocks_used;
2665 btrfs_end_transaction_throttle(trans, root);
2670 btrfs_btree_balance_dirty(root, nr);
2674 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2675 struct extent_map *existing,
2676 struct extent_map *em,
2677 u64 map_start, u64 map_len)
2681 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2682 start_diff = map_start - em->start;
2683 em->start = map_start;
2685 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2686 em->block_start += start_diff;
2687 return add_extent_mapping(em_tree, em);
2690 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2691 size_t pg_offset, u64 start, u64 len,
2697 u64 extent_start = 0;
2699 u64 objectid = inode->i_ino;
2701 struct btrfs_path *path = NULL;
2702 struct btrfs_root *root = BTRFS_I(inode)->root;
2703 struct btrfs_file_extent_item *item;
2704 struct extent_buffer *leaf;
2705 struct btrfs_key found_key;
2706 struct extent_map *em = NULL;
2707 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2709 struct btrfs_trans_handle *trans = NULL;
2712 spin_lock(&em_tree->lock);
2713 em = lookup_extent_mapping(em_tree, start, len);
2715 em->bdev = root->fs_info->fs_devices->latest_bdev;
2716 spin_unlock(&em_tree->lock);
2719 if (em->start > start || em->start + em->len <= start)
2720 free_extent_map(em);
2721 else if (em->block_start == EXTENT_MAP_INLINE && page)
2722 free_extent_map(em);
2726 em = alloc_extent_map(GFP_NOFS);
2731 em->bdev = root->fs_info->fs_devices->latest_bdev;
2732 em->start = EXTENT_MAP_HOLE;
2736 path = btrfs_alloc_path();
2740 ret = btrfs_lookup_file_extent(trans, root, path,
2741 objectid, start, trans != NULL);
2748 if (path->slots[0] == 0)
2753 leaf = path->nodes[0];
2754 item = btrfs_item_ptr(leaf, path->slots[0],
2755 struct btrfs_file_extent_item);
2756 /* are we inside the extent that was found? */
2757 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2758 found_type = btrfs_key_type(&found_key);
2759 if (found_key.objectid != objectid ||
2760 found_type != BTRFS_EXTENT_DATA_KEY) {
2764 found_type = btrfs_file_extent_type(leaf, item);
2765 extent_start = found_key.offset;
2766 if (found_type == BTRFS_FILE_EXTENT_REG) {
2767 extent_end = extent_start +
2768 btrfs_file_extent_num_bytes(leaf, item);
2770 if (start < extent_start || start >= extent_end) {
2772 if (start < extent_start) {
2773 if (start + len <= extent_start)
2775 em->len = extent_end - extent_start;
2781 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2783 em->start = extent_start;
2784 em->len = extent_end - extent_start;
2785 em->block_start = EXTENT_MAP_HOLE;
2788 bytenr += btrfs_file_extent_offset(leaf, item);
2789 em->block_start = bytenr;
2790 em->start = extent_start;
2791 em->len = extent_end - extent_start;
2793 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2798 size_t extent_offset;
2801 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2803 extent_end = (extent_start + size + root->sectorsize - 1) &
2804 ~((u64)root->sectorsize - 1);
2805 if (start < extent_start || start >= extent_end) {
2807 if (start < extent_start) {
2808 if (start + len <= extent_start)
2810 em->len = extent_end - extent_start;
2816 em->block_start = EXTENT_MAP_INLINE;
2819 em->start = extent_start;
2824 page_start = page_offset(page) + pg_offset;
2825 extent_offset = page_start - extent_start;
2826 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2827 size - extent_offset);
2828 em->start = extent_start + extent_offset;
2829 em->len = (copy_size + root->sectorsize - 1) &
2830 ~((u64)root->sectorsize - 1);
2832 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2833 if (create == 0 && !PageUptodate(page)) {
2834 read_extent_buffer(leaf, map + pg_offset, ptr,
2836 flush_dcache_page(page);
2837 } else if (create && PageUptodate(page)) {
2840 free_extent_map(em);
2842 btrfs_release_path(root, path);
2843 trans = btrfs_join_transaction(root, 1);
2846 write_extent_buffer(leaf, map + pg_offset, ptr,
2848 btrfs_mark_buffer_dirty(leaf);
2851 set_extent_uptodate(io_tree, em->start,
2852 extent_map_end(em) - 1, GFP_NOFS);
2855 printk("unkknown found_type %d\n", found_type);
2862 em->block_start = EXTENT_MAP_HOLE;
2864 btrfs_release_path(root, path);
2865 if (em->start > start || extent_map_end(em) <= start) {
2866 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2872 spin_lock(&em_tree->lock);
2873 ret = add_extent_mapping(em_tree, em);
2874 /* it is possible that someone inserted the extent into the tree
2875 * while we had the lock dropped. It is also possible that
2876 * an overlapping map exists in the tree
2878 if (ret == -EEXIST) {
2879 struct extent_map *existing;
2883 existing = lookup_extent_mapping(em_tree, start, len);
2884 if (existing && (existing->start > start ||
2885 existing->start + existing->len <= start)) {
2886 free_extent_map(existing);
2890 existing = lookup_extent_mapping(em_tree, em->start,
2893 err = merge_extent_mapping(em_tree, existing,
2896 free_extent_map(existing);
2898 free_extent_map(em);
2903 printk("failing to insert %Lu %Lu\n",
2905 free_extent_map(em);
2909 free_extent_map(em);
2914 spin_unlock(&em_tree->lock);
2917 btrfs_free_path(path);
2919 ret = btrfs_end_transaction(trans, root);
2925 free_extent_map(em);
2927 return ERR_PTR(err);
2932 #if 0 /* waiting for O_DIRECT reads */
2933 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2934 struct buffer_head *bh_result, int create)
2936 struct extent_map *em;
2937 u64 start = (u64)iblock << inode->i_blkbits;
2938 struct btrfs_multi_bio *multi = NULL;
2939 struct btrfs_root *root = BTRFS_I(inode)->root;
2945 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2947 if (!em || IS_ERR(em))
2950 if (em->start > start || em->start + em->len <= start) {
2954 if (em->block_start == EXTENT_MAP_INLINE) {
2959 len = em->start + em->len - start;
2960 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2962 if (em->block_start == EXTENT_MAP_HOLE ||
2963 em->block_start == EXTENT_MAP_DELALLOC) {
2964 bh_result->b_size = len;
2968 logical = start - em->start;
2969 logical = em->block_start + logical;
2972 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2973 logical, &map_length, &multi, 0);
2975 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2976 bh_result->b_size = min(map_length, len);
2978 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2979 set_buffer_mapped(bh_result);
2982 free_extent_map(em);
2987 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2988 const struct iovec *iov, loff_t offset,
2989 unsigned long nr_segs)
2993 struct file *file = iocb->ki_filp;
2994 struct inode *inode = file->f_mapping->host;
2999 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3000 offset, nr_segs, btrfs_get_block, NULL);
3004 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
3006 return extent_bmap(mapping, iblock, btrfs_get_extent);
3009 int btrfs_readpage(struct file *file, struct page *page)
3011 struct extent_io_tree *tree;
3012 tree = &BTRFS_I(page->mapping->host)->io_tree;
3013 return extent_read_full_page(tree, page, btrfs_get_extent);
3016 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3018 struct extent_io_tree *tree;
3021 if (current->flags & PF_MEMALLOC) {
3022 redirty_page_for_writepage(wbc, page);
3026 tree = &BTRFS_I(page->mapping->host)->io_tree;
3027 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3030 int btrfs_writepages(struct address_space *mapping,
3031 struct writeback_control *wbc)
3033 struct extent_io_tree *tree;
3034 tree = &BTRFS_I(mapping->host)->io_tree;
3035 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3039 btrfs_readpages(struct file *file, struct address_space *mapping,
3040 struct list_head *pages, unsigned nr_pages)
3042 struct extent_io_tree *tree;
3043 tree = &BTRFS_I(mapping->host)->io_tree;
3044 return extent_readpages(tree, mapping, pages, nr_pages,
3047 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3049 struct extent_io_tree *tree;
3050 struct extent_map_tree *map;
3053 tree = &BTRFS_I(page->mapping->host)->io_tree;
3054 map = &BTRFS_I(page->mapping->host)->extent_tree;
3055 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3057 ClearPagePrivate(page);
3058 set_page_private(page, 0);
3059 page_cache_release(page);
3064 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3066 return __btrfs_releasepage(page, gfp_flags);
3069 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3071 struct extent_io_tree *tree;
3072 struct btrfs_ordered_extent *ordered;
3073 u64 page_start = page_offset(page);
3074 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3076 wait_on_page_writeback(page);
3077 tree = &BTRFS_I(page->mapping->host)->io_tree;
3079 btrfs_releasepage(page, GFP_NOFS);
3083 lock_extent(tree, page_start, page_end, GFP_NOFS);
3084 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3088 * IO on this page will never be started, so we need
3089 * to account for any ordered extents now
3091 clear_extent_bit(tree, page_start, page_end,
3092 EXTENT_DIRTY | EXTENT_DELALLOC |
3093 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3094 btrfs_finish_ordered_io(page->mapping->host,
3095 page_start, page_end);
3096 btrfs_put_ordered_extent(ordered);
3097 lock_extent(tree, page_start, page_end, GFP_NOFS);
3099 clear_extent_bit(tree, page_start, page_end,
3100 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3103 __btrfs_releasepage(page, GFP_NOFS);
3105 ClearPageChecked(page);
3106 if (PagePrivate(page)) {
3107 ClearPagePrivate(page);
3108 set_page_private(page, 0);
3109 page_cache_release(page);
3114 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3115 * called from a page fault handler when a page is first dirtied. Hence we must
3116 * be careful to check for EOF conditions here. We set the page up correctly
3117 * for a written page which means we get ENOSPC checking when writing into
3118 * holes and correct delalloc and unwritten extent mapping on filesystems that
3119 * support these features.
3121 * We are not allowed to take the i_mutex here so we have to play games to
3122 * protect against truncate races as the page could now be beyond EOF. Because
3123 * vmtruncate() writes the inode size before removing pages, once we have the
3124 * page lock we can determine safely if the page is beyond EOF. If it is not
3125 * beyond EOF, then the page is guaranteed safe against truncation until we
3128 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3130 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3131 struct btrfs_root *root = BTRFS_I(inode)->root;
3132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3133 struct btrfs_ordered_extent *ordered;
3135 unsigned long zero_start;
3141 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3148 size = i_size_read(inode);
3149 page_start = page_offset(page);
3150 page_end = page_start + PAGE_CACHE_SIZE - 1;
3152 if ((page->mapping != inode->i_mapping) ||
3153 (page_start >= size)) {
3154 /* page got truncated out from underneath us */
3157 wait_on_page_writeback(page);
3159 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3160 set_page_extent_mapped(page);
3163 * we can't set the delalloc bits if there are pending ordered
3164 * extents. Drop our locks and wait for them to finish
3166 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3170 btrfs_start_ordered_extent(inode, ordered, 1);
3171 btrfs_put_ordered_extent(ordered);
3175 btrfs_set_extent_delalloc(inode, page_start, page_end);
3178 /* page is wholly or partially inside EOF */
3179 if (page_start + PAGE_CACHE_SIZE > size)
3180 zero_start = size & ~PAGE_CACHE_MASK;
3182 zero_start = PAGE_CACHE_SIZE;
3184 if (zero_start != PAGE_CACHE_SIZE) {
3186 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3187 flush_dcache_page(page);
3190 ClearPageChecked(page);
3191 set_page_dirty(page);
3192 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3200 static void btrfs_truncate(struct inode *inode)
3202 struct btrfs_root *root = BTRFS_I(inode)->root;
3204 struct btrfs_trans_handle *trans;
3206 u64 mask = root->sectorsize - 1;
3208 if (!S_ISREG(inode->i_mode))
3210 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3213 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3214 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3216 trans = btrfs_start_transaction(root, 1);
3217 btrfs_set_trans_block_group(trans, inode);
3218 btrfs_i_size_write(inode, inode->i_size);
3220 ret = btrfs_orphan_add(trans, inode);
3223 /* FIXME, add redo link to tree so we don't leak on crash */
3224 ret = btrfs_truncate_in_trans(trans, root, inode,
3225 BTRFS_EXTENT_DATA_KEY);
3226 btrfs_update_inode(trans, root, inode);
3228 ret = btrfs_orphan_del(trans, inode);
3232 nr = trans->blocks_used;
3233 ret = btrfs_end_transaction_throttle(trans, root);
3235 btrfs_btree_balance_dirty(root, nr);
3239 * Invalidate a single dcache entry at the root of the filesystem.
3240 * Needed after creation of snapshot or subvolume.
3242 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3245 struct dentry *alias, *entry;
3248 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3252 /* change me if btrfs ever gets a d_hash operation */
3253 qstr.hash = full_name_hash(qstr.name, qstr.len);
3254 entry = d_lookup(alias, &qstr);
3257 d_invalidate(entry);
3263 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3264 struct btrfs_trans_handle *trans, u64 new_dirid,
3265 struct btrfs_block_group_cache *block_group)
3267 struct inode *inode;
3270 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3271 new_dirid, block_group, S_IFDIR | 0700, &index);
3273 return PTR_ERR(inode);
3274 inode->i_op = &btrfs_dir_inode_operations;
3275 inode->i_fop = &btrfs_dir_file_operations;
3276 new_root->inode = inode;
3279 btrfs_i_size_write(inode, 0);
3281 return btrfs_update_inode(trans, new_root, inode);
3284 unsigned long btrfs_force_ra(struct address_space *mapping,
3285 struct file_ra_state *ra, struct file *file,
3286 pgoff_t offset, pgoff_t last_index)
3288 pgoff_t req_size = last_index - offset + 1;
3290 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3291 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3294 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3295 return offset + req_size;
3299 struct inode *btrfs_alloc_inode(struct super_block *sb)
3301 struct btrfs_inode *ei;
3303 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3307 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3308 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3309 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3310 INIT_LIST_HEAD(&ei->i_orphan);
3311 return &ei->vfs_inode;
3314 void btrfs_destroy_inode(struct inode *inode)
3316 struct btrfs_ordered_extent *ordered;
3317 WARN_ON(!list_empty(&inode->i_dentry));
3318 WARN_ON(inode->i_data.nrpages);
3320 if (BTRFS_I(inode)->i_acl &&
3321 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3322 posix_acl_release(BTRFS_I(inode)->i_acl);
3323 if (BTRFS_I(inode)->i_default_acl &&
3324 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3325 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3327 spin_lock(&BTRFS_I(inode)->root->list_lock);
3328 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3329 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3330 " list\n", inode->i_ino);
3333 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3336 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3340 printk("found ordered extent %Lu %Lu\n",
3341 ordered->file_offset, ordered->len);
3342 btrfs_remove_ordered_extent(inode, ordered);
3343 btrfs_put_ordered_extent(ordered);
3344 btrfs_put_ordered_extent(ordered);
3347 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3348 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3351 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3352 static void init_once(void *foo)
3353 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3354 static void init_once(struct kmem_cache * cachep, void *foo)
3356 static void init_once(void * foo, struct kmem_cache * cachep,
3357 unsigned long flags)
3360 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3362 inode_init_once(&ei->vfs_inode);
3365 void btrfs_destroy_cachep(void)
3367 if (btrfs_inode_cachep)
3368 kmem_cache_destroy(btrfs_inode_cachep);
3369 if (btrfs_trans_handle_cachep)
3370 kmem_cache_destroy(btrfs_trans_handle_cachep);
3371 if (btrfs_transaction_cachep)
3372 kmem_cache_destroy(btrfs_transaction_cachep);
3373 if (btrfs_bit_radix_cachep)
3374 kmem_cache_destroy(btrfs_bit_radix_cachep);
3375 if (btrfs_path_cachep)
3376 kmem_cache_destroy(btrfs_path_cachep);
3379 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3380 unsigned long extra_flags,
3381 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3382 void (*ctor)(void *)
3383 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3384 void (*ctor)(struct kmem_cache *, void *)
3386 void (*ctor)(void *, struct kmem_cache *,
3391 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3392 SLAB_MEM_SPREAD | extra_flags), ctor
3393 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3399 int btrfs_init_cachep(void)
3401 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3402 sizeof(struct btrfs_inode),
3404 if (!btrfs_inode_cachep)
3406 btrfs_trans_handle_cachep =
3407 btrfs_cache_create("btrfs_trans_handle_cache",
3408 sizeof(struct btrfs_trans_handle),
3410 if (!btrfs_trans_handle_cachep)
3412 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3413 sizeof(struct btrfs_transaction),
3415 if (!btrfs_transaction_cachep)
3417 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3418 sizeof(struct btrfs_path),
3420 if (!btrfs_path_cachep)
3422 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3423 SLAB_DESTROY_BY_RCU, NULL);
3424 if (!btrfs_bit_radix_cachep)
3428 btrfs_destroy_cachep();
3432 static int btrfs_getattr(struct vfsmount *mnt,
3433 struct dentry *dentry, struct kstat *stat)
3435 struct inode *inode = dentry->d_inode;
3436 generic_fillattr(inode, stat);
3437 stat->blksize = PAGE_CACHE_SIZE;
3438 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3442 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3443 struct inode * new_dir,struct dentry *new_dentry)
3445 struct btrfs_trans_handle *trans;
3446 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3447 struct inode *new_inode = new_dentry->d_inode;
3448 struct inode *old_inode = old_dentry->d_inode;
3449 struct timespec ctime = CURRENT_TIME;
3453 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3454 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3458 ret = btrfs_check_free_space(root, 1, 0);
3462 trans = btrfs_start_transaction(root, 1);
3464 btrfs_set_trans_block_group(trans, new_dir);
3466 old_dentry->d_inode->i_nlink++;
3467 old_dir->i_ctime = old_dir->i_mtime = ctime;
3468 new_dir->i_ctime = new_dir->i_mtime = ctime;
3469 old_inode->i_ctime = ctime;
3471 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3476 new_inode->i_ctime = CURRENT_TIME;
3477 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3480 if (new_inode->i_nlink == 0) {
3481 ret = btrfs_orphan_add(trans, new_inode);
3486 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
3490 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
3495 btrfs_end_transaction_throttle(trans, root);
3500 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3502 struct list_head *head = &root->fs_info->delalloc_inodes;
3503 struct btrfs_inode *binode;
3504 unsigned long flags;
3506 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3507 while(!list_empty(head)) {
3508 binode = list_entry(head->next, struct btrfs_inode,
3510 atomic_inc(&binode->vfs_inode.i_count);
3511 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3512 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3513 iput(&binode->vfs_inode);
3514 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3516 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3520 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3521 const char *symname)
3523 struct btrfs_trans_handle *trans;
3524 struct btrfs_root *root = BTRFS_I(dir)->root;
3525 struct btrfs_path *path;
3526 struct btrfs_key key;
3527 struct inode *inode = NULL;
3535 struct btrfs_file_extent_item *ei;
3536 struct extent_buffer *leaf;
3537 unsigned long nr = 0;
3539 name_len = strlen(symname) + 1;
3540 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3541 return -ENAMETOOLONG;
3543 err = btrfs_check_free_space(root, 1, 0);
3547 trans = btrfs_start_transaction(root, 1);
3548 btrfs_set_trans_block_group(trans, dir);
3550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3556 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3558 dentry->d_parent->d_inode->i_ino, objectid,
3559 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3561 err = PTR_ERR(inode);
3565 err = btrfs_init_acl(inode, dir);
3571 btrfs_set_trans_block_group(trans, inode);
3572 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3576 inode->i_mapping->a_ops = &btrfs_aops;
3577 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3578 inode->i_fop = &btrfs_file_operations;
3579 inode->i_op = &btrfs_file_inode_operations;
3580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3582 inode->i_mapping, GFP_NOFS);
3583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3584 inode->i_mapping, GFP_NOFS);
3585 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3586 mutex_init(&BTRFS_I(inode)->csum_mutex);
3587 mutex_init(&BTRFS_I(inode)->extent_mutex);
3588 BTRFS_I(inode)->delalloc_bytes = 0;
3589 BTRFS_I(inode)->disk_i_size = 0;
3590 inode->i_mapping->writeback_index = 0;
3591 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3592 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3594 dir->i_sb->s_dirt = 1;
3595 btrfs_update_inode_block_group(trans, inode);
3596 btrfs_update_inode_block_group(trans, dir);
3600 path = btrfs_alloc_path();
3602 key.objectid = inode->i_ino;
3604 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3605 datasize = btrfs_file_extent_calc_inline_size(name_len);
3606 err = btrfs_insert_empty_item(trans, root, path, &key,
3612 leaf = path->nodes[0];
3613 ei = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_file_extent_item);
3615 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3616 btrfs_set_file_extent_type(leaf, ei,
3617 BTRFS_FILE_EXTENT_INLINE);
3618 ptr = btrfs_file_extent_inline_start(ei);
3619 write_extent_buffer(leaf, symname, ptr, name_len);
3620 btrfs_mark_buffer_dirty(leaf);
3621 btrfs_free_path(path);
3623 inode->i_op = &btrfs_symlink_inode_operations;
3624 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3625 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3626 btrfs_i_size_write(inode, name_len - 1);
3627 err = btrfs_update_inode(trans, root, inode);
3632 nr = trans->blocks_used;
3633 btrfs_end_transaction_throttle(trans, root);
3636 inode_dec_link_count(inode);
3639 btrfs_btree_balance_dirty(root, nr);
3643 static int btrfs_set_page_dirty(struct page *page)
3645 return __set_page_dirty_nobuffers(page);
3648 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3649 static int btrfs_permission(struct inode *inode, int mask)
3651 static int btrfs_permission(struct inode *inode, int mask,
3652 struct nameidata *nd)
3655 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3657 return generic_permission(inode, mask, btrfs_check_acl);
3660 static struct inode_operations btrfs_dir_inode_operations = {
3661 .lookup = btrfs_lookup,
3662 .create = btrfs_create,
3663 .unlink = btrfs_unlink,
3665 .mkdir = btrfs_mkdir,
3666 .rmdir = btrfs_rmdir,
3667 .rename = btrfs_rename,
3668 .symlink = btrfs_symlink,
3669 .setattr = btrfs_setattr,
3670 .mknod = btrfs_mknod,
3671 .setxattr = btrfs_setxattr,
3672 .getxattr = btrfs_getxattr,
3673 .listxattr = btrfs_listxattr,
3674 .removexattr = btrfs_removexattr,
3675 .permission = btrfs_permission,
3677 static struct inode_operations btrfs_dir_ro_inode_operations = {
3678 .lookup = btrfs_lookup,
3679 .permission = btrfs_permission,
3681 static struct file_operations btrfs_dir_file_operations = {
3682 .llseek = generic_file_llseek,
3683 .read = generic_read_dir,
3684 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
3685 .readdir = btrfs_nfshack_readdir,
3686 #else /* NFSd readdir/lookup deadlock is fixed */
3687 .readdir = btrfs_real_readdir,
3689 .unlocked_ioctl = btrfs_ioctl,
3690 #ifdef CONFIG_COMPAT
3691 .compat_ioctl = btrfs_ioctl,
3693 .release = btrfs_release_file,
3696 static struct extent_io_ops btrfs_extent_io_ops = {
3697 .fill_delalloc = run_delalloc_range,
3698 .submit_bio_hook = btrfs_submit_bio_hook,
3699 .merge_bio_hook = btrfs_merge_bio_hook,
3700 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3701 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3702 .writepage_start_hook = btrfs_writepage_start_hook,
3703 .readpage_io_failed_hook = btrfs_io_failed_hook,
3704 .set_bit_hook = btrfs_set_bit_hook,
3705 .clear_bit_hook = btrfs_clear_bit_hook,
3708 static struct address_space_operations btrfs_aops = {
3709 .readpage = btrfs_readpage,
3710 .writepage = btrfs_writepage,
3711 .writepages = btrfs_writepages,
3712 .readpages = btrfs_readpages,
3713 .sync_page = block_sync_page,
3715 .direct_IO = btrfs_direct_IO,
3716 .invalidatepage = btrfs_invalidatepage,
3717 .releasepage = btrfs_releasepage,
3718 .set_page_dirty = btrfs_set_page_dirty,
3721 static struct address_space_operations btrfs_symlink_aops = {
3722 .readpage = btrfs_readpage,
3723 .writepage = btrfs_writepage,
3724 .invalidatepage = btrfs_invalidatepage,
3725 .releasepage = btrfs_releasepage,
3728 static struct inode_operations btrfs_file_inode_operations = {
3729 .truncate = btrfs_truncate,
3730 .getattr = btrfs_getattr,
3731 .setattr = btrfs_setattr,
3732 .setxattr = btrfs_setxattr,
3733 .getxattr = btrfs_getxattr,
3734 .listxattr = btrfs_listxattr,
3735 .removexattr = btrfs_removexattr,
3736 .permission = btrfs_permission,
3738 static struct inode_operations btrfs_special_inode_operations = {
3739 .getattr = btrfs_getattr,
3740 .setattr = btrfs_setattr,
3741 .permission = btrfs_permission,
3742 .setxattr = btrfs_setxattr,
3743 .getxattr = btrfs_getxattr,
3744 .listxattr = btrfs_listxattr,
3745 .removexattr = btrfs_removexattr,
3747 static struct inode_operations btrfs_symlink_inode_operations = {
3748 .readlink = generic_readlink,
3749 .follow_link = page_follow_link_light,
3750 .put_link = page_put_link,
3751 .permission = btrfs_permission,