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"
49 struct btrfs_iget_args {
51 struct btrfs_root *root;
54 static struct inode_operations btrfs_dir_inode_operations;
55 static struct inode_operations btrfs_symlink_inode_operations;
56 static struct inode_operations btrfs_dir_ro_inode_operations;
57 static struct inode_operations btrfs_special_inode_operations;
58 static struct inode_operations btrfs_file_inode_operations;
59 static struct address_space_operations btrfs_aops;
60 static struct address_space_operations btrfs_symlink_aops;
61 static struct file_operations btrfs_dir_file_operations;
62 static struct extent_io_ops btrfs_extent_io_ops;
64 static struct kmem_cache *btrfs_inode_cachep;
65 struct kmem_cache *btrfs_trans_handle_cachep;
66 struct kmem_cache *btrfs_transaction_cachep;
67 struct kmem_cache *btrfs_bit_radix_cachep;
68 struct kmem_cache *btrfs_path_cachep;
71 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
81 static void btrfs_truncate(struct inode *inode);
83 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
92 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
108 static int cow_file_range(struct inode *inode, u64 start, u64 end)
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
115 u64 blocksize = root->sectorsize;
117 struct btrfs_key ins;
118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
122 trans = btrfs_join_transaction(root, 1);
124 btrfs_set_trans_block_group(trans, inode);
126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
127 num_bytes = max(blocksize, num_bytes);
128 orig_num_bytes = num_bytes;
130 if (alloc_hint == EXTENT_MAP_INLINE)
133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
147 em = alloc_extent_map(GFP_NOFS);
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
167 cur_alloc_size = ins.offset;
168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
181 btrfs_end_transaction(trans, root);
185 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 extent_buffer *leaf;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
201 struct btrfs_key found_key;
203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
204 path = btrfs_alloc_path();
207 ret = btrfs_lookup_file_extent(NULL, root, path,
208 inode->i_ino, start, 0);
210 btrfs_free_path(path);
216 if (path->slots[0] == 0)
221 leaf = path->nodes[0];
222 item = btrfs_item_ptr(leaf, path->slots[0],
223 struct btrfs_file_extent_item);
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
227 found_type = btrfs_key_type(&found_key);
228 if (found_key.objectid != inode->i_ino ||
229 found_type != BTRFS_EXTENT_DATA_KEY)
232 found_type = btrfs_file_extent_type(leaf, item);
233 extent_start = found_key.offset;
234 if (found_type == BTRFS_FILE_EXTENT_REG) {
235 u64 extent_num_bytes;
237 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
238 extent_end = extent_start + extent_num_bytes;
241 if (loops && start != extent_start)
244 if (start < extent_start || start >= extent_end)
247 cow_end = min(end, extent_end - 1);
248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
252 if (btrfs_cross_ref_exists(root, &found_key, bytenr))
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 btrfs_release_path(root, path);
278 cow_file_range(inode, start, end);
283 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
285 struct btrfs_root *root = BTRFS_I(inode)->root;
288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
290 ret = run_delalloc_nocow(inode, start, end);
292 ret = cow_file_range(inode, start, end);
297 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
298 unsigned long old, unsigned long bits)
301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
302 struct btrfs_root *root = BTRFS_I(inode)->root;
303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
305 root->fs_info->delalloc_bytes += end - start + 1;
306 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
311 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
312 unsigned long old, unsigned long bits)
314 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
315 struct btrfs_root *root = BTRFS_I(inode)->root;
318 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
319 if (end - start + 1 > root->fs_info->delalloc_bytes) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end - start + 1, root->fs_info->delalloc_bytes);
322 root->fs_info->delalloc_bytes = 0;
323 BTRFS_I(inode)->delalloc_bytes = 0;
325 root->fs_info->delalloc_bytes -= end - start + 1;
326 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
328 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
333 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
334 size_t size, struct bio *bio)
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
337 struct btrfs_mapping_tree *map_tree;
338 u64 logical = bio->bi_sector << 9;
343 length = bio->bi_size;
344 map_tree = &root->fs_info->mapping_tree;
346 ret = btrfs_map_block(map_tree, READ, logical,
347 &map_length, NULL, 0);
349 if (map_length < length + size) {
355 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
358 struct btrfs_root *root = BTRFS_I(inode)->root;
361 ret = btrfs_csum_one_bio(root, inode, bio);
364 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
367 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
370 struct btrfs_root *root = BTRFS_I(inode)->root;
373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
376 if (!(rw & (1 << BIO_RW))) {
380 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
381 inode, rw, bio, mirror_num,
382 __btrfs_submit_bio_hook);
384 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
387 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
388 struct inode *inode, u64 file_offset,
389 struct list_head *list)
391 struct list_head *cur;
392 struct btrfs_ordered_sum *sum;
394 btrfs_set_trans_block_group(trans, inode);
395 list_for_each(cur, list) {
396 sum = list_entry(cur, struct btrfs_ordered_sum, list);
397 mutex_lock(&BTRFS_I(inode)->csum_mutex);
398 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
400 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
405 struct btrfs_writepage_fixup {
407 struct btrfs_work work;
410 /* see btrfs_writepage_start_hook for details on why this is required */
411 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
413 struct btrfs_writepage_fixup *fixup;
414 struct btrfs_ordered_extent *ordered;
420 fixup = container_of(work, struct btrfs_writepage_fixup, work);
424 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
425 ClearPageChecked(page);
429 inode = page->mapping->host;
430 page_start = page_offset(page);
431 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
433 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
435 /* already ordered? We're done */
436 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
437 EXTENT_ORDERED, 0)) {
441 ordered = btrfs_lookup_ordered_extent(inode, page_start);
443 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
446 btrfs_start_ordered_extent(inode, ordered, 1);
450 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
452 ClearPageChecked(page);
454 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
457 page_cache_release(page);
461 * There are a few paths in the higher layers of the kernel that directly
462 * set the page dirty bit without asking the filesystem if it is a
463 * good idea. This causes problems because we want to make sure COW
464 * properly happens and the data=ordered rules are followed.
466 * In our case any range that doesn't have the EXTENT_ORDERED bit set
467 * hasn't been properly setup for IO. We kick off an async process
468 * to fix it up. The async helper will wait for ordered extents, set
469 * the delalloc bit and make it safe to write the page.
471 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
473 struct inode *inode = page->mapping->host;
474 struct btrfs_writepage_fixup *fixup;
475 struct btrfs_root *root = BTRFS_I(inode)->root;
478 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
483 if (PageChecked(page))
486 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
490 SetPageChecked(page);
491 page_cache_get(page);
492 fixup->work.func = btrfs_writepage_fixup_worker;
494 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
498 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
500 struct btrfs_root *root = BTRFS_I(inode)->root;
501 struct btrfs_trans_handle *trans;
502 struct btrfs_ordered_extent *ordered_extent;
503 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
504 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
505 struct extent_map *em;
506 struct extent_map *em_orig;
510 struct list_head list;
511 struct btrfs_key ins;
515 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
519 trans = btrfs_join_transaction(root, 1);
521 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
522 BUG_ON(!ordered_extent);
524 lock_extent(io_tree, ordered_extent->file_offset,
525 ordered_extent->file_offset + ordered_extent->len - 1,
528 INIT_LIST_HEAD(&list);
530 ins.objectid = ordered_extent->start;
531 ins.offset = ordered_extent->len;
532 ins.type = BTRFS_EXTENT_ITEM_KEY;
534 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
535 trans->transid, inode->i_ino,
536 ordered_extent->file_offset, &ins);
539 mutex_lock(&BTRFS_I(inode)->extent_mutex);
541 spin_lock(&em_tree->lock);
542 clear_start = ordered_extent->file_offset;
543 clear_end = ordered_extent->file_offset + ordered_extent->len;
544 em = lookup_extent_mapping(em_tree, clear_start,
545 ordered_extent->len);
547 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
548 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
549 rb = rb_next(&em->rb_node);
552 em = rb_entry(rb, struct extent_map, rb_node);
554 free_extent_map(em_orig);
555 spin_unlock(&em_tree->lock);
557 ret = btrfs_drop_extents(trans, root, inode,
558 ordered_extent->file_offset,
559 ordered_extent->file_offset +
561 ordered_extent->file_offset, &alloc_hint);
563 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
564 ordered_extent->file_offset,
565 ordered_extent->start,
567 ordered_extent->len, 0);
570 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
571 ordered_extent->file_offset +
572 ordered_extent->len - 1);
573 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
575 inode->i_blocks += ordered_extent->len >> 9;
576 unlock_extent(io_tree, ordered_extent->file_offset,
577 ordered_extent->file_offset + ordered_extent->len - 1,
579 add_pending_csums(trans, inode, ordered_extent->file_offset,
580 &ordered_extent->list);
582 btrfs_ordered_update_i_size(inode, ordered_extent);
583 btrfs_remove_ordered_extent(inode, ordered_extent);
586 btrfs_put_ordered_extent(ordered_extent);
587 /* once for the tree */
588 btrfs_put_ordered_extent(ordered_extent);
590 btrfs_update_inode(trans, root, inode);
591 btrfs_end_transaction(trans, root);
595 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
596 struct extent_state *state, int uptodate)
598 return btrfs_finish_ordered_io(page->mapping->host, start, end);
601 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
604 struct inode *inode = page->mapping->host;
605 struct btrfs_root *root = BTRFS_I(inode)->root;
606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
607 struct btrfs_csum_item *item;
608 struct btrfs_path *path = NULL;
611 if (btrfs_test_opt(root, NODATASUM) ||
612 btrfs_test_flag(inode, NODATASUM))
616 * It is possible there is an ordered extent that has
617 * not yet finished for this range in the file. If so,
618 * that extent will have a csum cached, and it will insert
619 * the sum after all the blocks in the extent are fully
620 * on disk. So, look for an ordered extent and use the
621 * sum if found. We have to do this before looking in the
622 * btree because csum items are pre-inserted based on
623 * the file size. btrfs_lookup_csum might find an item
624 * that still hasn't been fully filled.
626 ret = btrfs_find_ordered_sum(inode, start, &csum);
631 path = btrfs_alloc_path();
632 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
635 /* a csum that isn't present is a preallocated region. */
636 if (ret == -ENOENT || ret == -EFBIG)
639 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
643 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
646 set_state_private(io_tree, start, csum);
649 btrfs_free_path(path);
653 struct io_failure_record {
661 int btrfs_io_failed_hook(struct bio *failed_bio,
662 struct page *page, u64 start, u64 end,
663 struct extent_state *state)
665 struct io_failure_record *failrec = NULL;
667 struct extent_map *em;
668 struct inode *inode = page->mapping->host;
669 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
670 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
677 ret = get_state_private(failure_tree, start, &private);
679 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
682 failrec->start = start;
683 failrec->len = end - start + 1;
684 failrec->last_mirror = 0;
686 spin_lock(&em_tree->lock);
687 em = lookup_extent_mapping(em_tree, start, failrec->len);
688 if (em->start > start || em->start + em->len < start) {
692 spin_unlock(&em_tree->lock);
694 if (!em || IS_ERR(em)) {
698 logical = start - em->start;
699 logical = em->block_start + logical;
700 failrec->logical = logical;
702 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
703 EXTENT_DIRTY, GFP_NOFS);
704 set_state_private(failure_tree, start,
705 (u64)(unsigned long)failrec);
707 failrec = (struct io_failure_record *)(unsigned long)private;
709 num_copies = btrfs_num_copies(
710 &BTRFS_I(inode)->root->fs_info->mapping_tree,
711 failrec->logical, failrec->len);
712 failrec->last_mirror++;
714 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
715 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
718 if (state && state->start != failrec->start)
720 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
722 if (!state || failrec->last_mirror > num_copies) {
723 set_state_private(failure_tree, failrec->start, 0);
724 clear_extent_bits(failure_tree, failrec->start,
725 failrec->start + failrec->len - 1,
726 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
730 bio = bio_alloc(GFP_NOFS, 1);
731 bio->bi_private = state;
732 bio->bi_end_io = failed_bio->bi_end_io;
733 bio->bi_sector = failrec->logical >> 9;
734 bio->bi_bdev = failed_bio->bi_bdev;
736 bio_add_page(bio, page, failrec->len, start - page_offset(page));
737 if (failed_bio->bi_rw & (1 << BIO_RW))
742 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
743 failrec->last_mirror);
747 int btrfs_clean_io_failures(struct inode *inode, u64 start)
751 struct io_failure_record *failure;
755 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
756 (u64)-1, 1, EXTENT_DIRTY)) {
757 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
758 start, &private_failure);
760 failure = (struct io_failure_record *)(unsigned long)
762 set_state_private(&BTRFS_I(inode)->io_failure_tree,
764 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
766 failure->start + failure->len - 1,
767 EXTENT_DIRTY | EXTENT_LOCKED,
775 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
776 struct extent_state *state)
778 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
779 struct inode *inode = page->mapping->host;
780 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
782 u64 private = ~(u32)0;
784 struct btrfs_root *root = BTRFS_I(inode)->root;
788 if (btrfs_test_opt(root, NODATASUM) ||
789 btrfs_test_flag(inode, NODATASUM))
791 if (state && state->start == start) {
792 private = state->private;
795 ret = get_state_private(io_tree, start, &private);
797 local_irq_save(flags);
798 kaddr = kmap_atomic(page, KM_IRQ0);
802 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
803 btrfs_csum_final(csum, (char *)&csum);
804 if (csum != private) {
807 kunmap_atomic(kaddr, KM_IRQ0);
808 local_irq_restore(flags);
810 /* if the io failure tree for this inode is non-empty,
811 * check to see if we've recovered from a failed IO
813 btrfs_clean_io_failures(inode, start);
817 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
818 page->mapping->host->i_ino, (unsigned long long)start, csum,
820 memset(kaddr + offset, 1, end - start + 1);
821 flush_dcache_page(page);
822 kunmap_atomic(kaddr, KM_IRQ0);
823 local_irq_restore(flags);
830 * This creates an orphan entry for the given inode in case something goes
831 * wrong in the middle of an unlink/truncate.
833 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
835 struct btrfs_root *root = BTRFS_I(inode)->root;
838 spin_lock(&root->list_lock);
840 /* already on the orphan list, we're good */
841 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
842 spin_unlock(&root->list_lock);
846 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
848 spin_unlock(&root->list_lock);
851 * insert an orphan item to track this unlinked/truncated file
853 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
859 * We have done the truncate/delete so we can go ahead and remove the orphan
860 * item for this particular inode.
862 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
864 struct btrfs_root *root = BTRFS_I(inode)->root;
867 spin_lock(&root->list_lock);
869 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
870 spin_unlock(&root->list_lock);
874 list_del_init(&BTRFS_I(inode)->i_orphan);
876 spin_unlock(&root->list_lock);
880 spin_unlock(&root->list_lock);
882 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
888 * this cleans up any orphans that may be left on the list from the last use
891 void btrfs_orphan_cleanup(struct btrfs_root *root)
893 struct btrfs_path *path;
894 struct extent_buffer *leaf;
895 struct btrfs_item *item;
896 struct btrfs_key key, found_key;
897 struct btrfs_trans_handle *trans;
899 int ret = 0, nr_unlink = 0, nr_truncate = 0;
901 /* don't do orphan cleanup if the fs is readonly. */
902 if (root->inode->i_sb->s_flags & MS_RDONLY)
905 path = btrfs_alloc_path();
910 key.objectid = BTRFS_ORPHAN_OBJECTID;
911 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
912 key.offset = (u64)-1;
914 trans = btrfs_start_transaction(root, 1);
915 btrfs_set_trans_block_group(trans, root->inode);
918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
920 printk(KERN_ERR "Error searching slot for orphan: %d"
926 * if ret == 0 means we found what we were searching for, which
927 * is weird, but possible, so only screw with path if we didnt
928 * find the key and see if we have stuff that matches
931 if (path->slots[0] == 0)
936 /* pull out the item */
937 leaf = path->nodes[0];
938 item = btrfs_item_nr(leaf, path->slots[0]);
939 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
941 /* make sure the item matches what we want */
942 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
944 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
947 /* release the path since we're done with it */
948 btrfs_release_path(root, path);
951 * this is where we are basically btrfs_lookup, without the
952 * crossing root thing. we store the inode number in the
953 * offset of the orphan item.
955 inode = btrfs_iget_locked(root->inode->i_sb,
956 found_key.offset, root);
960 if (inode->i_state & I_NEW) {
961 BTRFS_I(inode)->root = root;
963 /* have to set the location manually */
964 BTRFS_I(inode)->location.objectid = inode->i_ino;
965 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
966 BTRFS_I(inode)->location.offset = 0;
968 btrfs_read_locked_inode(inode);
969 unlock_new_inode(inode);
973 * add this inode to the orphan list so btrfs_orphan_del does
974 * the proper thing when we hit it
976 spin_lock(&root->list_lock);
977 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
978 spin_unlock(&root->list_lock);
981 * if this is a bad inode, means we actually succeeded in
982 * removing the inode, but not the orphan record, which means
983 * we need to manually delete the orphan since iput will just
986 if (is_bad_inode(inode)) {
987 btrfs_orphan_del(trans, inode);
992 /* if we have links, this was a truncate, lets do that */
993 if (inode->i_nlink) {
995 btrfs_truncate(inode);
1000 /* this will do delete_inode and everything for us */
1005 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1007 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1009 btrfs_free_path(path);
1010 btrfs_end_transaction(trans, root);
1013 void btrfs_read_locked_inode(struct inode *inode)
1015 struct btrfs_path *path;
1016 struct extent_buffer *leaf;
1017 struct btrfs_inode_item *inode_item;
1018 struct btrfs_timespec *tspec;
1019 struct btrfs_root *root = BTRFS_I(inode)->root;
1020 struct btrfs_key location;
1021 u64 alloc_group_block;
1025 path = btrfs_alloc_path();
1027 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1029 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1033 leaf = path->nodes[0];
1034 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_inode_item);
1037 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1038 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1039 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1040 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1041 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1043 tspec = btrfs_inode_atime(inode_item);
1044 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1045 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1047 tspec = btrfs_inode_mtime(inode_item);
1048 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1049 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1051 tspec = btrfs_inode_ctime(inode_item);
1052 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1053 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1055 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1056 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
1058 rdev = btrfs_inode_rdev(leaf, inode_item);
1060 BTRFS_I(inode)->index_cnt = (u64)-1;
1062 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1063 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1065 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1066 if (!BTRFS_I(inode)->block_group) {
1067 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1069 BTRFS_BLOCK_GROUP_METADATA, 0);
1071 btrfs_free_path(path);
1074 switch (inode->i_mode & S_IFMT) {
1076 inode->i_mapping->a_ops = &btrfs_aops;
1077 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1078 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1079 inode->i_fop = &btrfs_file_operations;
1080 inode->i_op = &btrfs_file_inode_operations;
1083 inode->i_fop = &btrfs_dir_file_operations;
1084 if (root == root->fs_info->tree_root)
1085 inode->i_op = &btrfs_dir_ro_inode_operations;
1087 inode->i_op = &btrfs_dir_inode_operations;
1090 inode->i_op = &btrfs_symlink_inode_operations;
1091 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1092 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1095 init_special_inode(inode, inode->i_mode, rdev);
1101 btrfs_free_path(path);
1102 make_bad_inode(inode);
1105 static void fill_inode_item(struct extent_buffer *leaf,
1106 struct btrfs_inode_item *item,
1107 struct inode *inode)
1109 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1110 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1111 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1112 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1113 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1115 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1116 inode->i_atime.tv_sec);
1117 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1118 inode->i_atime.tv_nsec);
1120 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1121 inode->i_mtime.tv_sec);
1122 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1123 inode->i_mtime.tv_nsec);
1125 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1126 inode->i_ctime.tv_sec);
1127 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1128 inode->i_ctime.tv_nsec);
1130 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1131 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1132 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1133 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1134 btrfs_set_inode_block_group(leaf, item,
1135 BTRFS_I(inode)->block_group->key.objectid);
1138 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1139 struct btrfs_root *root,
1140 struct inode *inode)
1142 struct btrfs_inode_item *inode_item;
1143 struct btrfs_path *path;
1144 struct extent_buffer *leaf;
1147 path = btrfs_alloc_path();
1149 ret = btrfs_lookup_inode(trans, root, path,
1150 &BTRFS_I(inode)->location, 1);
1157 leaf = path->nodes[0];
1158 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1159 struct btrfs_inode_item);
1161 fill_inode_item(leaf, inode_item, inode);
1162 btrfs_mark_buffer_dirty(leaf);
1163 btrfs_set_inode_last_trans(trans, inode);
1166 btrfs_free_path(path);
1171 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root,
1174 struct dentry *dentry)
1176 struct btrfs_path *path;
1177 const char *name = dentry->d_name.name;
1178 int name_len = dentry->d_name.len;
1180 struct extent_buffer *leaf;
1181 struct btrfs_dir_item *di;
1182 struct btrfs_key key;
1185 path = btrfs_alloc_path();
1191 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1192 name, name_len, -1);
1201 leaf = path->nodes[0];
1202 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1203 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1206 btrfs_release_path(root, path);
1208 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1209 dentry->d_inode->i_ino,
1210 dentry->d_parent->d_inode->i_ino, &index);
1212 printk("failed to delete reference to %.*s, "
1213 "inode %lu parent %lu\n", name_len, name,
1214 dentry->d_inode->i_ino,
1215 dentry->d_parent->d_inode->i_ino);
1219 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1220 index, name, name_len, -1);
1229 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1230 btrfs_release_path(root, path);
1232 dentry->d_inode->i_ctime = dir->i_ctime;
1234 btrfs_free_path(path);
1236 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1237 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1238 btrfs_update_inode(trans, root, dir);
1239 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1240 dentry->d_inode->i_nlink--;
1242 drop_nlink(dentry->d_inode);
1244 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1245 dir->i_sb->s_dirt = 1;
1250 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1252 struct btrfs_root *root;
1253 struct btrfs_trans_handle *trans;
1254 struct inode *inode = dentry->d_inode;
1256 unsigned long nr = 0;
1258 root = BTRFS_I(dir)->root;
1260 ret = btrfs_check_free_space(root, 1, 1);
1264 trans = btrfs_start_transaction(root, 1);
1266 btrfs_set_trans_block_group(trans, dir);
1267 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1269 if (inode->i_nlink == 0)
1270 ret = btrfs_orphan_add(trans, inode);
1272 nr = trans->blocks_used;
1274 btrfs_end_transaction_throttle(trans, root);
1276 btrfs_btree_balance_dirty(root, nr);
1280 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1282 struct inode *inode = dentry->d_inode;
1285 struct btrfs_root *root = BTRFS_I(dir)->root;
1286 struct btrfs_trans_handle *trans;
1287 unsigned long nr = 0;
1289 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1293 ret = btrfs_check_free_space(root, 1, 1);
1297 trans = btrfs_start_transaction(root, 1);
1298 btrfs_set_trans_block_group(trans, dir);
1300 err = btrfs_orphan_add(trans, inode);
1304 /* now the directory is empty */
1305 err = btrfs_unlink_trans(trans, root, dir, dentry);
1307 btrfs_i_size_write(inode, 0);
1311 nr = trans->blocks_used;
1312 ret = btrfs_end_transaction_throttle(trans, root);
1314 btrfs_btree_balance_dirty(root, nr);
1322 * this can truncate away extent items, csum items and directory items.
1323 * It starts at a high offset and removes keys until it can't find
1324 * any higher than i_size.
1326 * csum items that cross the new i_size are truncated to the new size
1329 * min_type is the minimum key type to truncate down to. If set to 0, this
1330 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1332 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root,
1334 struct inode *inode,
1338 struct btrfs_path *path;
1339 struct btrfs_key key;
1340 struct btrfs_key found_key;
1342 struct extent_buffer *leaf;
1343 struct btrfs_file_extent_item *fi;
1344 u64 extent_start = 0;
1345 u64 extent_num_bytes = 0;
1351 int pending_del_nr = 0;
1352 int pending_del_slot = 0;
1353 int extent_type = -1;
1354 u64 mask = root->sectorsize - 1;
1356 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1357 path = btrfs_alloc_path();
1361 /* FIXME, add redo link to tree so we don't leak on crash */
1362 key.objectid = inode->i_ino;
1363 key.offset = (u64)-1;
1366 btrfs_init_path(path);
1368 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1373 BUG_ON(path->slots[0] == 0);
1379 leaf = path->nodes[0];
1380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1381 found_type = btrfs_key_type(&found_key);
1383 if (found_key.objectid != inode->i_ino)
1386 if (found_type < min_type)
1389 item_end = found_key.offset;
1390 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1391 fi = btrfs_item_ptr(leaf, path->slots[0],
1392 struct btrfs_file_extent_item);
1393 extent_type = btrfs_file_extent_type(leaf, fi);
1394 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1396 btrfs_file_extent_num_bytes(leaf, fi);
1397 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1398 struct btrfs_item *item = btrfs_item_nr(leaf,
1400 item_end += btrfs_file_extent_inline_len(leaf,
1405 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1406 ret = btrfs_csum_truncate(trans, root, path,
1410 if (item_end < inode->i_size) {
1411 if (found_type == BTRFS_DIR_ITEM_KEY) {
1412 found_type = BTRFS_INODE_ITEM_KEY;
1413 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1414 found_type = BTRFS_CSUM_ITEM_KEY;
1415 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1416 found_type = BTRFS_XATTR_ITEM_KEY;
1417 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1418 found_type = BTRFS_INODE_REF_KEY;
1419 } else if (found_type) {
1424 btrfs_set_key_type(&key, found_type);
1427 if (found_key.offset >= inode->i_size)
1433 /* FIXME, shrink the extent if the ref count is only 1 */
1434 if (found_type != BTRFS_EXTENT_DATA_KEY)
1437 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1439 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1441 u64 orig_num_bytes =
1442 btrfs_file_extent_num_bytes(leaf, fi);
1443 extent_num_bytes = inode->i_size -
1444 found_key.offset + root->sectorsize - 1;
1445 extent_num_bytes = extent_num_bytes &
1446 ~((u64)root->sectorsize - 1);
1447 btrfs_set_file_extent_num_bytes(leaf, fi,
1449 num_dec = (orig_num_bytes -
1451 if (extent_start != 0)
1452 dec_i_blocks(inode, num_dec);
1453 btrfs_mark_buffer_dirty(leaf);
1456 btrfs_file_extent_disk_num_bytes(leaf,
1458 /* FIXME blocksize != 4096 */
1459 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1460 if (extent_start != 0) {
1462 dec_i_blocks(inode, num_dec);
1464 root_gen = btrfs_header_generation(leaf);
1465 root_owner = btrfs_header_owner(leaf);
1467 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1469 u32 newsize = inode->i_size - found_key.offset;
1470 dec_i_blocks(inode, item_end + 1 -
1471 found_key.offset - newsize);
1473 btrfs_file_extent_calc_inline_size(newsize);
1474 ret = btrfs_truncate_item(trans, root, path,
1478 dec_i_blocks(inode, item_end + 1 -
1484 if (!pending_del_nr) {
1485 /* no pending yet, add ourselves */
1486 pending_del_slot = path->slots[0];
1488 } else if (pending_del_nr &&
1489 path->slots[0] + 1 == pending_del_slot) {
1490 /* hop on the pending chunk */
1492 pending_del_slot = path->slots[0];
1494 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1500 ret = btrfs_free_extent(trans, root, extent_start,
1503 root_gen, inode->i_ino,
1504 found_key.offset, 0);
1508 if (path->slots[0] == 0) {
1511 btrfs_release_path(root, path);
1516 if (pending_del_nr &&
1517 path->slots[0] + 1 != pending_del_slot) {
1518 struct btrfs_key debug;
1520 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1522 ret = btrfs_del_items(trans, root, path,
1527 btrfs_release_path(root, path);
1533 if (pending_del_nr) {
1534 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1537 btrfs_free_path(path);
1538 inode->i_sb->s_dirt = 1;
1543 * taken from block_truncate_page, but does cow as it zeros out
1544 * any bytes left in the last page in the file.
1546 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1548 struct inode *inode = mapping->host;
1549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1551 struct btrfs_ordered_extent *ordered;
1553 u32 blocksize = root->sectorsize;
1554 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1555 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1561 if ((offset & (blocksize - 1)) == 0)
1566 page = grab_cache_page(mapping, index);
1570 page_start = page_offset(page);
1571 page_end = page_start + PAGE_CACHE_SIZE - 1;
1573 if (!PageUptodate(page)) {
1574 ret = btrfs_readpage(NULL, page);
1576 if (page->mapping != mapping) {
1578 page_cache_release(page);
1581 if (!PageUptodate(page)) {
1586 wait_on_page_writeback(page);
1588 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1589 set_page_extent_mapped(page);
1591 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1593 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1595 page_cache_release(page);
1596 btrfs_start_ordered_extent(inode, ordered, 1);
1597 btrfs_put_ordered_extent(ordered);
1601 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1602 page_end, GFP_NOFS);
1604 if (offset != PAGE_CACHE_SIZE) {
1606 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1607 flush_dcache_page(page);
1610 ClearPageChecked(page);
1611 set_page_dirty(page);
1612 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1616 page_cache_release(page);
1621 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1623 struct inode *inode = dentry->d_inode;
1626 err = inode_change_ok(inode, attr);
1630 if (S_ISREG(inode->i_mode) &&
1631 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1632 struct btrfs_trans_handle *trans;
1633 struct btrfs_root *root = BTRFS_I(inode)->root;
1634 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1636 u64 mask = root->sectorsize - 1;
1637 u64 hole_start = (inode->i_size + mask) & ~mask;
1638 u64 block_end = (attr->ia_size + mask) & ~mask;
1642 if (attr->ia_size <= hole_start)
1645 err = btrfs_check_free_space(root, 1, 0);
1649 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1651 hole_size = block_end - hole_start;
1652 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1653 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1655 trans = btrfs_start_transaction(root, 1);
1656 btrfs_set_trans_block_group(trans, inode);
1657 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1658 err = btrfs_drop_extents(trans, root, inode,
1659 hole_start, block_end, hole_start,
1662 if (alloc_hint != EXTENT_MAP_INLINE) {
1663 err = btrfs_insert_file_extent(trans, root,
1667 btrfs_drop_extent_cache(inode, hole_start,
1669 btrfs_check_file(root, inode);
1671 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1672 btrfs_end_transaction(trans, root);
1673 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1678 err = inode_setattr(inode, attr);
1680 if (!err && ((attr->ia_valid & ATTR_MODE)))
1681 err = btrfs_acl_chmod(inode);
1686 void btrfs_delete_inode(struct inode *inode)
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_root *root = BTRFS_I(inode)->root;
1693 truncate_inode_pages(&inode->i_data, 0);
1694 if (is_bad_inode(inode)) {
1695 btrfs_orphan_del(NULL, inode);
1698 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1700 btrfs_i_size_write(inode, 0);
1701 trans = btrfs_start_transaction(root, 1);
1703 btrfs_set_trans_block_group(trans, inode);
1704 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1706 btrfs_orphan_del(NULL, inode);
1707 goto no_delete_lock;
1710 btrfs_orphan_del(trans, inode);
1712 nr = trans->blocks_used;
1715 btrfs_end_transaction(trans, root);
1716 btrfs_btree_balance_dirty(root, nr);
1720 nr = trans->blocks_used;
1721 btrfs_end_transaction(trans, root);
1722 btrfs_btree_balance_dirty(root, nr);
1728 * this returns the key found in the dir entry in the location pointer.
1729 * If no dir entries were found, location->objectid is 0.
1731 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1732 struct btrfs_key *location)
1734 const char *name = dentry->d_name.name;
1735 int namelen = dentry->d_name.len;
1736 struct btrfs_dir_item *di;
1737 struct btrfs_path *path;
1738 struct btrfs_root *root = BTRFS_I(dir)->root;
1741 if (namelen == 1 && strcmp(name, ".") == 0) {
1742 location->objectid = dir->i_ino;
1743 location->type = BTRFS_INODE_ITEM_KEY;
1744 location->offset = 0;
1747 path = btrfs_alloc_path();
1750 if (namelen == 2 && strcmp(name, "..") == 0) {
1751 struct btrfs_key key;
1752 struct extent_buffer *leaf;
1755 key.objectid = dir->i_ino;
1756 key.offset = (u64)-1;
1757 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1758 if (ret < 0 || path->slots[0] == 0)
1760 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1763 leaf = path->nodes[0];
1764 slot = path->slots[0] - 1;
1766 btrfs_item_key_to_cpu(leaf, &key, slot);
1767 if (key.objectid != dir->i_ino ||
1768 key.type != BTRFS_INODE_REF_KEY) {
1771 location->objectid = key.offset;
1772 location->type = BTRFS_INODE_ITEM_KEY;
1773 location->offset = 0;
1777 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1781 if (!di || IS_ERR(di)) {
1784 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1786 btrfs_free_path(path);
1789 location->objectid = 0;
1794 * when we hit a tree root in a directory, the btrfs part of the inode
1795 * needs to be changed to reflect the root directory of the tree root. This
1796 * is kind of like crossing a mount point.
1798 static int fixup_tree_root_location(struct btrfs_root *root,
1799 struct btrfs_key *location,
1800 struct btrfs_root **sub_root,
1801 struct dentry *dentry)
1803 struct btrfs_root_item *ri;
1805 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1807 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1810 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1811 dentry->d_name.name,
1812 dentry->d_name.len);
1813 if (IS_ERR(*sub_root))
1814 return PTR_ERR(*sub_root);
1816 ri = &(*sub_root)->root_item;
1817 location->objectid = btrfs_root_dirid(ri);
1818 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1819 location->offset = 0;
1824 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1826 struct btrfs_iget_args *args = p;
1827 inode->i_ino = args->ino;
1828 BTRFS_I(inode)->root = args->root;
1829 BTRFS_I(inode)->delalloc_bytes = 0;
1830 BTRFS_I(inode)->disk_i_size = 0;
1831 BTRFS_I(inode)->index_cnt = (u64)-1;
1832 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1833 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1834 inode->i_mapping, GFP_NOFS);
1835 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1836 inode->i_mapping, GFP_NOFS);
1837 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1838 mutex_init(&BTRFS_I(inode)->csum_mutex);
1839 mutex_init(&BTRFS_I(inode)->extent_mutex);
1843 static int btrfs_find_actor(struct inode *inode, void *opaque)
1845 struct btrfs_iget_args *args = opaque;
1846 return (args->ino == inode->i_ino &&
1847 args->root == BTRFS_I(inode)->root);
1850 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1853 struct btrfs_iget_args args;
1854 args.ino = objectid;
1855 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1860 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1863 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1864 struct btrfs_root *root)
1866 struct inode *inode;
1867 struct btrfs_iget_args args;
1868 args.ino = objectid;
1871 inode = iget5_locked(s, objectid, btrfs_find_actor,
1872 btrfs_init_locked_inode,
1877 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1878 struct nameidata *nd)
1880 struct inode * inode;
1881 struct btrfs_inode *bi = BTRFS_I(dir);
1882 struct btrfs_root *root = bi->root;
1883 struct btrfs_root *sub_root = root;
1884 struct btrfs_key location;
1885 int ret, do_orphan = 0;
1887 if (dentry->d_name.len > BTRFS_NAME_LEN)
1888 return ERR_PTR(-ENAMETOOLONG);
1890 ret = btrfs_inode_by_name(dir, dentry, &location);
1893 return ERR_PTR(ret);
1896 if (location.objectid) {
1897 ret = fixup_tree_root_location(root, &location, &sub_root,
1900 return ERR_PTR(ret);
1902 return ERR_PTR(-ENOENT);
1904 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1907 return ERR_PTR(-EACCES);
1908 if (inode->i_state & I_NEW) {
1909 /* the inode and parent dir are two different roots */
1910 if (sub_root != root) {
1912 sub_root->inode = inode;
1915 BTRFS_I(inode)->root = sub_root;
1916 memcpy(&BTRFS_I(inode)->location, &location,
1918 btrfs_read_locked_inode(inode);
1919 unlock_new_inode(inode);
1923 if (unlikely(do_orphan))
1924 btrfs_orphan_cleanup(sub_root);
1926 return d_splice_alias(inode, dentry);
1929 static unsigned char btrfs_filetype_table[] = {
1930 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1933 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1935 struct inode *inode = filp->f_dentry->d_inode;
1936 struct btrfs_root *root = BTRFS_I(inode)->root;
1937 struct btrfs_item *item;
1938 struct btrfs_dir_item *di;
1939 struct btrfs_key key;
1940 struct btrfs_key found_key;
1941 struct btrfs_path *path;
1944 struct extent_buffer *leaf;
1947 unsigned char d_type;
1952 int key_type = BTRFS_DIR_INDEX_KEY;
1957 /* FIXME, use a real flag for deciding about the key type */
1958 if (root->fs_info->tree_root == root)
1959 key_type = BTRFS_DIR_ITEM_KEY;
1961 /* special case for "." */
1962 if (filp->f_pos == 0) {
1963 over = filldir(dirent, ".", 1,
1971 key.objectid = inode->i_ino;
1972 path = btrfs_alloc_path();
1975 /* special case for .., just use the back ref */
1976 if (filp->f_pos == 1) {
1977 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1978 key.offset = (u64)-1;
1979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1980 if (ret < 0 || path->slots[0] == 0) {
1981 btrfs_release_path(root, path);
1982 goto read_dir_items;
1985 leaf = path->nodes[0];
1986 slot = path->slots[0] - 1;
1987 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1988 btrfs_release_path(root, path);
1989 if (found_key.objectid != key.objectid ||
1990 found_key.type != BTRFS_INODE_REF_KEY)
1991 goto read_dir_items;
1992 over = filldir(dirent, "..", 2,
1993 2, found_key.offset, DT_DIR);
2000 btrfs_set_key_type(&key, key_type);
2001 key.offset = filp->f_pos;
2003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2008 leaf = path->nodes[0];
2009 nritems = btrfs_header_nritems(leaf);
2010 slot = path->slots[0];
2011 if (advance || slot >= nritems) {
2012 if (slot >= nritems -1) {
2013 ret = btrfs_next_leaf(root, path);
2016 leaf = path->nodes[0];
2017 nritems = btrfs_header_nritems(leaf);
2018 slot = path->slots[0];
2025 item = btrfs_item_nr(leaf, slot);
2026 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2028 if (found_key.objectid != key.objectid)
2030 if (btrfs_key_type(&found_key) != key_type)
2032 if (found_key.offset < filp->f_pos)
2035 filp->f_pos = found_key.offset;
2037 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2039 di_total = btrfs_item_size(leaf, item);
2040 while(di_cur < di_total) {
2041 struct btrfs_key location;
2043 name_len = btrfs_dir_name_len(leaf, di);
2044 if (name_len < 32) {
2045 name_ptr = tmp_name;
2047 name_ptr = kmalloc(name_len, GFP_NOFS);
2050 read_extent_buffer(leaf, name_ptr,
2051 (unsigned long)(di + 1), name_len);
2053 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2054 btrfs_dir_item_key_to_cpu(leaf, di, &location);
2055 over = filldir(dirent, name_ptr, name_len,
2060 if (name_ptr != tmp_name)
2065 di_len = btrfs_dir_name_len(leaf, di) +
2066 btrfs_dir_data_len(leaf, di) +sizeof(*di);
2068 di = (struct btrfs_dir_item *)((char *)di + di_len);
2071 if (key_type == BTRFS_DIR_INDEX_KEY)
2072 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2078 btrfs_free_path(path);
2082 int btrfs_write_inode(struct inode *inode, int wait)
2084 struct btrfs_root *root = BTRFS_I(inode)->root;
2085 struct btrfs_trans_handle *trans;
2089 trans = btrfs_join_transaction(root, 1);
2090 btrfs_set_trans_block_group(trans, inode);
2091 ret = btrfs_commit_transaction(trans, root);
2097 * This is somewhat expensive, updating the tree every time the
2098 * inode changes. But, it is most likely to find the inode in cache.
2099 * FIXME, needs more benchmarking...there are no reasons other than performance
2100 * to keep or drop this code.
2102 void btrfs_dirty_inode(struct inode *inode)
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_trans_handle *trans;
2107 trans = btrfs_join_transaction(root, 1);
2108 btrfs_set_trans_block_group(trans, inode);
2109 btrfs_update_inode(trans, root, inode);
2110 btrfs_end_transaction(trans, root);
2113 static int btrfs_set_inode_index_count(struct inode *inode)
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct btrfs_key key, found_key;
2117 struct btrfs_path *path;
2118 struct extent_buffer *leaf;
2121 key.objectid = inode->i_ino;
2122 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2123 key.offset = (u64)-1;
2125 path = btrfs_alloc_path();
2129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2132 /* FIXME: we should be able to handle this */
2138 * MAGIC NUMBER EXPLANATION:
2139 * since we search a directory based on f_pos we have to start at 2
2140 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2141 * else has to start at 2
2143 if (path->slots[0] == 0) {
2144 BTRFS_I(inode)->index_cnt = 2;
2150 leaf = path->nodes[0];
2151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2153 if (found_key.objectid != inode->i_ino ||
2154 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2155 BTRFS_I(inode)->index_cnt = 2;
2159 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2161 btrfs_free_path(path);
2165 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2169 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2170 ret = btrfs_set_inode_index_count(dir);
2175 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2176 BTRFS_I(dir)->index_cnt++;
2181 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
2184 const char *name, int name_len,
2187 struct btrfs_block_group_cache *group,
2190 struct inode *inode;
2191 struct btrfs_inode_item *inode_item;
2192 struct btrfs_block_group_cache *new_inode_group;
2193 struct btrfs_key *location;
2194 struct btrfs_path *path;
2195 struct btrfs_inode_ref *ref;
2196 struct btrfs_key key[2];
2202 path = btrfs_alloc_path();
2205 inode = new_inode(root->fs_info->sb);
2207 return ERR_PTR(-ENOMEM);
2210 ret = btrfs_set_inode_index(dir, inode);
2212 return ERR_PTR(ret);
2214 BTRFS_I(inode)->index = 0;
2217 * index_cnt is ignored for everything but a dir,
2218 * btrfs_get_inode_index_count has an explanation for the magic
2221 BTRFS_I(inode)->index_cnt = 2;
2223 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2224 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2225 inode->i_mapping, GFP_NOFS);
2226 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2227 inode->i_mapping, GFP_NOFS);
2228 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2229 mutex_init(&BTRFS_I(inode)->csum_mutex);
2230 mutex_init(&BTRFS_I(inode)->extent_mutex);
2231 BTRFS_I(inode)->delalloc_bytes = 0;
2232 BTRFS_I(inode)->disk_i_size = 0;
2233 BTRFS_I(inode)->root = root;
2239 new_inode_group = btrfs_find_block_group(root, group, 0,
2240 BTRFS_BLOCK_GROUP_METADATA, owner);
2241 if (!new_inode_group) {
2242 printk("find_block group failed\n");
2243 new_inode_group = group;
2245 BTRFS_I(inode)->block_group = new_inode_group;
2246 BTRFS_I(inode)->flags = 0;
2248 key[0].objectid = objectid;
2249 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2252 key[1].objectid = objectid;
2253 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2254 key[1].offset = ref_objectid;
2256 sizes[0] = sizeof(struct btrfs_inode_item);
2257 sizes[1] = name_len + sizeof(*ref);
2259 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2263 if (objectid > root->highest_inode)
2264 root->highest_inode = objectid;
2266 inode->i_uid = current->fsuid;
2267 inode->i_gid = current->fsgid;
2268 inode->i_mode = mode;
2269 inode->i_ino = objectid;
2270 inode->i_blocks = 0;
2271 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2272 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2273 struct btrfs_inode_item);
2274 fill_inode_item(path->nodes[0], inode_item, inode);
2276 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2277 struct btrfs_inode_ref);
2278 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2279 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
2280 ptr = (unsigned long)(ref + 1);
2281 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2283 btrfs_mark_buffer_dirty(path->nodes[0]);
2284 btrfs_free_path(path);
2286 location = &BTRFS_I(inode)->location;
2287 location->objectid = objectid;
2288 location->offset = 0;
2289 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2291 insert_inode_hash(inode);
2295 BTRFS_I(dir)->index_cnt--;
2296 btrfs_free_path(path);
2297 return ERR_PTR(ret);
2300 static inline u8 btrfs_inode_type(struct inode *inode)
2302 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2305 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2306 struct dentry *dentry, struct inode *inode,
2310 struct btrfs_key key;
2311 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2312 struct inode *parent_inode = dentry->d_parent->d_inode;
2314 key.objectid = inode->i_ino;
2315 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2318 ret = btrfs_insert_dir_item(trans, root,
2319 dentry->d_name.name, dentry->d_name.len,
2320 dentry->d_parent->d_inode->i_ino,
2321 &key, btrfs_inode_type(inode),
2322 BTRFS_I(inode)->index);
2325 ret = btrfs_insert_inode_ref(trans, root,
2326 dentry->d_name.name,
2329 parent_inode->i_ino,
2330 BTRFS_I(inode)->index);
2332 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2333 dentry->d_name.len * 2);
2334 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2335 ret = btrfs_update_inode(trans, root,
2336 dentry->d_parent->d_inode);
2341 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2342 struct dentry *dentry, struct inode *inode,
2345 int err = btrfs_add_link(trans, dentry, inode, backref);
2347 d_instantiate(dentry, inode);
2355 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2356 int mode, dev_t rdev)
2358 struct btrfs_trans_handle *trans;
2359 struct btrfs_root *root = BTRFS_I(dir)->root;
2360 struct inode *inode = NULL;
2364 unsigned long nr = 0;
2366 if (!new_valid_dev(rdev))
2369 err = btrfs_check_free_space(root, 1, 0);
2373 trans = btrfs_start_transaction(root, 1);
2374 btrfs_set_trans_block_group(trans, dir);
2376 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2382 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2384 dentry->d_parent->d_inode->i_ino, objectid,
2385 BTRFS_I(dir)->block_group, mode);
2386 err = PTR_ERR(inode);
2390 err = btrfs_init_acl(inode, dir);
2396 btrfs_set_trans_block_group(trans, inode);
2397 err = btrfs_add_nondir(trans, dentry, inode, 0);
2401 inode->i_op = &btrfs_special_inode_operations;
2402 init_special_inode(inode, inode->i_mode, rdev);
2403 btrfs_update_inode(trans, root, inode);
2405 dir->i_sb->s_dirt = 1;
2406 btrfs_update_inode_block_group(trans, inode);
2407 btrfs_update_inode_block_group(trans, dir);
2409 nr = trans->blocks_used;
2410 btrfs_end_transaction_throttle(trans, root);
2413 inode_dec_link_count(inode);
2416 btrfs_btree_balance_dirty(root, nr);
2420 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2421 int mode, struct nameidata *nd)
2423 struct btrfs_trans_handle *trans;
2424 struct btrfs_root *root = BTRFS_I(dir)->root;
2425 struct inode *inode = NULL;
2428 unsigned long nr = 0;
2431 err = btrfs_check_free_space(root, 1, 0);
2434 trans = btrfs_start_transaction(root, 1);
2435 btrfs_set_trans_block_group(trans, dir);
2437 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2443 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2445 dentry->d_parent->d_inode->i_ino,
2446 objectid, BTRFS_I(dir)->block_group, mode);
2447 err = PTR_ERR(inode);
2451 err = btrfs_init_acl(inode, dir);
2457 btrfs_set_trans_block_group(trans, inode);
2458 err = btrfs_add_nondir(trans, dentry, inode, 0);
2462 inode->i_mapping->a_ops = &btrfs_aops;
2463 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2464 inode->i_fop = &btrfs_file_operations;
2465 inode->i_op = &btrfs_file_inode_operations;
2466 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2467 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2468 inode->i_mapping, GFP_NOFS);
2469 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2470 inode->i_mapping, GFP_NOFS);
2471 mutex_init(&BTRFS_I(inode)->csum_mutex);
2472 mutex_init(&BTRFS_I(inode)->extent_mutex);
2473 BTRFS_I(inode)->delalloc_bytes = 0;
2474 BTRFS_I(inode)->disk_i_size = 0;
2475 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2476 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2478 dir->i_sb->s_dirt = 1;
2479 btrfs_update_inode_block_group(trans, inode);
2480 btrfs_update_inode_block_group(trans, dir);
2482 nr = trans->blocks_used;
2483 btrfs_end_transaction_throttle(trans, root);
2486 inode_dec_link_count(inode);
2489 btrfs_btree_balance_dirty(root, nr);
2493 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2494 struct dentry *dentry)
2496 struct btrfs_trans_handle *trans;
2497 struct btrfs_root *root = BTRFS_I(dir)->root;
2498 struct inode *inode = old_dentry->d_inode;
2499 unsigned long nr = 0;
2503 if (inode->i_nlink == 0)
2506 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2511 err = btrfs_check_free_space(root, 1, 0);
2514 err = btrfs_set_inode_index(dir, inode);
2518 trans = btrfs_start_transaction(root, 1);
2520 btrfs_set_trans_block_group(trans, dir);
2521 atomic_inc(&inode->i_count);
2523 err = btrfs_add_nondir(trans, dentry, inode, 1);
2528 dir->i_sb->s_dirt = 1;
2529 btrfs_update_inode_block_group(trans, dir);
2530 err = btrfs_update_inode(trans, root, inode);
2535 nr = trans->blocks_used;
2536 btrfs_end_transaction_throttle(trans, root);
2539 inode_dec_link_count(inode);
2542 btrfs_btree_balance_dirty(root, nr);
2546 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2548 struct inode *inode = NULL;
2549 struct btrfs_trans_handle *trans;
2550 struct btrfs_root *root = BTRFS_I(dir)->root;
2552 int drop_on_err = 0;
2554 unsigned long nr = 1;
2556 err = btrfs_check_free_space(root, 1, 0);
2560 trans = btrfs_start_transaction(root, 1);
2561 btrfs_set_trans_block_group(trans, dir);
2563 if (IS_ERR(trans)) {
2564 err = PTR_ERR(trans);
2568 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2574 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2576 dentry->d_parent->d_inode->i_ino, objectid,
2577 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2578 if (IS_ERR(inode)) {
2579 err = PTR_ERR(inode);
2585 err = btrfs_init_acl(inode, dir);
2589 inode->i_op = &btrfs_dir_inode_operations;
2590 inode->i_fop = &btrfs_dir_file_operations;
2591 btrfs_set_trans_block_group(trans, inode);
2593 btrfs_i_size_write(inode, 0);
2594 err = btrfs_update_inode(trans, root, inode);
2598 err = btrfs_add_link(trans, dentry, inode, 0);
2602 d_instantiate(dentry, inode);
2604 dir->i_sb->s_dirt = 1;
2605 btrfs_update_inode_block_group(trans, inode);
2606 btrfs_update_inode_block_group(trans, dir);
2609 nr = trans->blocks_used;
2610 btrfs_end_transaction_throttle(trans, root);
2615 btrfs_btree_balance_dirty(root, nr);
2619 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2620 struct extent_map *existing,
2621 struct extent_map *em,
2622 u64 map_start, u64 map_len)
2626 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2627 start_diff = map_start - em->start;
2628 em->start = map_start;
2630 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2631 em->block_start += start_diff;
2632 return add_extent_mapping(em_tree, em);
2635 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2636 size_t pg_offset, u64 start, u64 len,
2642 u64 extent_start = 0;
2644 u64 objectid = inode->i_ino;
2646 struct btrfs_path *path = NULL;
2647 struct btrfs_root *root = BTRFS_I(inode)->root;
2648 struct btrfs_file_extent_item *item;
2649 struct extent_buffer *leaf;
2650 struct btrfs_key found_key;
2651 struct extent_map *em = NULL;
2652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2654 struct btrfs_trans_handle *trans = NULL;
2657 spin_lock(&em_tree->lock);
2658 em = lookup_extent_mapping(em_tree, start, len);
2660 em->bdev = root->fs_info->fs_devices->latest_bdev;
2661 spin_unlock(&em_tree->lock);
2664 if (em->start > start || em->start + em->len <= start)
2665 free_extent_map(em);
2666 else if (em->block_start == EXTENT_MAP_INLINE && page)
2667 free_extent_map(em);
2671 em = alloc_extent_map(GFP_NOFS);
2676 em->bdev = root->fs_info->fs_devices->latest_bdev;
2677 em->start = EXTENT_MAP_HOLE;
2681 path = btrfs_alloc_path();
2685 ret = btrfs_lookup_file_extent(trans, root, path,
2686 objectid, start, trans != NULL);
2693 if (path->slots[0] == 0)
2698 leaf = path->nodes[0];
2699 item = btrfs_item_ptr(leaf, path->slots[0],
2700 struct btrfs_file_extent_item);
2701 /* are we inside the extent that was found? */
2702 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2703 found_type = btrfs_key_type(&found_key);
2704 if (found_key.objectid != objectid ||
2705 found_type != BTRFS_EXTENT_DATA_KEY) {
2709 found_type = btrfs_file_extent_type(leaf, item);
2710 extent_start = found_key.offset;
2711 if (found_type == BTRFS_FILE_EXTENT_REG) {
2712 extent_end = extent_start +
2713 btrfs_file_extent_num_bytes(leaf, item);
2715 if (start < extent_start || start >= extent_end) {
2717 if (start < extent_start) {
2718 if (start + len <= extent_start)
2720 em->len = extent_end - extent_start;
2726 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2728 em->start = extent_start;
2729 em->len = extent_end - extent_start;
2730 em->block_start = EXTENT_MAP_HOLE;
2733 bytenr += btrfs_file_extent_offset(leaf, item);
2734 em->block_start = bytenr;
2735 em->start = extent_start;
2736 em->len = extent_end - extent_start;
2738 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2743 size_t extent_offset;
2746 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2748 extent_end = (extent_start + size + root->sectorsize - 1) &
2749 ~((u64)root->sectorsize - 1);
2750 if (start < extent_start || start >= extent_end) {
2752 if (start < extent_start) {
2753 if (start + len <= extent_start)
2755 em->len = extent_end - extent_start;
2761 em->block_start = EXTENT_MAP_INLINE;
2764 em->start = extent_start;
2769 page_start = page_offset(page) + pg_offset;
2770 extent_offset = page_start - extent_start;
2771 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2772 size - extent_offset);
2773 em->start = extent_start + extent_offset;
2774 em->len = (copy_size + root->sectorsize - 1) &
2775 ~((u64)root->sectorsize - 1);
2777 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2778 if (create == 0 && !PageUptodate(page)) {
2779 read_extent_buffer(leaf, map + pg_offset, ptr,
2781 flush_dcache_page(page);
2782 } else if (create && PageUptodate(page)) {
2785 free_extent_map(em);
2787 btrfs_release_path(root, path);
2788 trans = btrfs_join_transaction(root, 1);
2791 write_extent_buffer(leaf, map + pg_offset, ptr,
2793 btrfs_mark_buffer_dirty(leaf);
2796 set_extent_uptodate(io_tree, em->start,
2797 extent_map_end(em) - 1, GFP_NOFS);
2800 printk("unkknown found_type %d\n", found_type);
2807 em->block_start = EXTENT_MAP_HOLE;
2809 btrfs_release_path(root, path);
2810 if (em->start > start || extent_map_end(em) <= start) {
2811 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2817 spin_lock(&em_tree->lock);
2818 ret = add_extent_mapping(em_tree, em);
2819 /* it is possible that someone inserted the extent into the tree
2820 * while we had the lock dropped. It is also possible that
2821 * an overlapping map exists in the tree
2823 if (ret == -EEXIST) {
2824 struct extent_map *existing;
2828 existing = lookup_extent_mapping(em_tree, start, len);
2829 if (existing && (existing->start > start ||
2830 existing->start + existing->len <= start)) {
2831 free_extent_map(existing);
2835 existing = lookup_extent_mapping(em_tree, em->start,
2838 err = merge_extent_mapping(em_tree, existing,
2841 free_extent_map(existing);
2843 free_extent_map(em);
2848 printk("failing to insert %Lu %Lu\n",
2850 free_extent_map(em);
2854 free_extent_map(em);
2859 spin_unlock(&em_tree->lock);
2862 btrfs_free_path(path);
2864 ret = btrfs_end_transaction(trans, root);
2870 free_extent_map(em);
2872 return ERR_PTR(err);
2877 #if 0 /* waiting for O_DIRECT reads */
2878 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2879 struct buffer_head *bh_result, int create)
2881 struct extent_map *em;
2882 u64 start = (u64)iblock << inode->i_blkbits;
2883 struct btrfs_multi_bio *multi = NULL;
2884 struct btrfs_root *root = BTRFS_I(inode)->root;
2890 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2892 if (!em || IS_ERR(em))
2895 if (em->start > start || em->start + em->len <= start) {
2899 if (em->block_start == EXTENT_MAP_INLINE) {
2904 len = em->start + em->len - start;
2905 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2907 if (em->block_start == EXTENT_MAP_HOLE ||
2908 em->block_start == EXTENT_MAP_DELALLOC) {
2909 bh_result->b_size = len;
2913 logical = start - em->start;
2914 logical = em->block_start + logical;
2917 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2918 logical, &map_length, &multi, 0);
2920 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2921 bh_result->b_size = min(map_length, len);
2923 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2924 set_buffer_mapped(bh_result);
2927 free_extent_map(em);
2932 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2933 const struct iovec *iov, loff_t offset,
2934 unsigned long nr_segs)
2938 struct file *file = iocb->ki_filp;
2939 struct inode *inode = file->f_mapping->host;
2944 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2945 offset, nr_segs, btrfs_get_block, NULL);
2949 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2951 return extent_bmap(mapping, iblock, btrfs_get_extent);
2954 int btrfs_readpage(struct file *file, struct page *page)
2956 struct extent_io_tree *tree;
2957 tree = &BTRFS_I(page->mapping->host)->io_tree;
2958 return extent_read_full_page(tree, page, btrfs_get_extent);
2961 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2963 struct extent_io_tree *tree;
2966 if (current->flags & PF_MEMALLOC) {
2967 redirty_page_for_writepage(wbc, page);
2971 tree = &BTRFS_I(page->mapping->host)->io_tree;
2972 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2975 int btrfs_writepages(struct address_space *mapping,
2976 struct writeback_control *wbc)
2978 struct extent_io_tree *tree;
2979 tree = &BTRFS_I(mapping->host)->io_tree;
2980 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2984 btrfs_readpages(struct file *file, struct address_space *mapping,
2985 struct list_head *pages, unsigned nr_pages)
2987 struct extent_io_tree *tree;
2988 tree = &BTRFS_I(mapping->host)->io_tree;
2989 return extent_readpages(tree, mapping, pages, nr_pages,
2992 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2994 struct extent_io_tree *tree;
2995 struct extent_map_tree *map;
2998 tree = &BTRFS_I(page->mapping->host)->io_tree;
2999 map = &BTRFS_I(page->mapping->host)->extent_tree;
3000 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3002 ClearPagePrivate(page);
3003 set_page_private(page, 0);
3004 page_cache_release(page);
3009 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3011 return __btrfs_releasepage(page, gfp_flags);
3014 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3016 struct extent_io_tree *tree;
3017 struct btrfs_ordered_extent *ordered;
3018 u64 page_start = page_offset(page);
3019 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3021 wait_on_page_writeback(page);
3022 tree = &BTRFS_I(page->mapping->host)->io_tree;
3024 btrfs_releasepage(page, GFP_NOFS);
3028 lock_extent(tree, page_start, page_end, GFP_NOFS);
3029 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3033 * IO on this page will never be started, so we need
3034 * to account for any ordered extents now
3036 clear_extent_bit(tree, page_start, page_end,
3037 EXTENT_DIRTY | EXTENT_DELALLOC |
3038 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3039 btrfs_finish_ordered_io(page->mapping->host,
3040 page_start, page_end);
3041 btrfs_put_ordered_extent(ordered);
3042 lock_extent(tree, page_start, page_end, GFP_NOFS);
3044 clear_extent_bit(tree, page_start, page_end,
3045 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3048 __btrfs_releasepage(page, GFP_NOFS);
3050 ClearPageChecked(page);
3051 if (PagePrivate(page)) {
3052 ClearPagePrivate(page);
3053 set_page_private(page, 0);
3054 page_cache_release(page);
3059 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3060 * called from a page fault handler when a page is first dirtied. Hence we must
3061 * be careful to check for EOF conditions here. We set the page up correctly
3062 * for a written page which means we get ENOSPC checking when writing into
3063 * holes and correct delalloc and unwritten extent mapping on filesystems that
3064 * support these features.
3066 * We are not allowed to take the i_mutex here so we have to play games to
3067 * protect against truncate races as the page could now be beyond EOF. Because
3068 * vmtruncate() writes the inode size before removing pages, once we have the
3069 * page lock we can determine safely if the page is beyond EOF. If it is not
3070 * beyond EOF, then the page is guaranteed safe against truncation until we
3073 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3075 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3076 struct btrfs_root *root = BTRFS_I(inode)->root;
3077 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3078 struct btrfs_ordered_extent *ordered;
3080 unsigned long zero_start;
3086 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3093 size = i_size_read(inode);
3094 page_start = page_offset(page);
3095 page_end = page_start + PAGE_CACHE_SIZE - 1;
3097 if ((page->mapping != inode->i_mapping) ||
3098 (page_start >= size)) {
3099 /* page got truncated out from underneath us */
3102 wait_on_page_writeback(page);
3104 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3105 set_page_extent_mapped(page);
3108 * we can't set the delalloc bits if there are pending ordered
3109 * extents. Drop our locks and wait for them to finish
3111 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3113 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3115 btrfs_start_ordered_extent(inode, ordered, 1);
3116 btrfs_put_ordered_extent(ordered);
3120 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3121 page_end, GFP_NOFS);
3124 /* page is wholly or partially inside EOF */
3125 if (page_start + PAGE_CACHE_SIZE > size)
3126 zero_start = size & ~PAGE_CACHE_MASK;
3128 zero_start = PAGE_CACHE_SIZE;
3130 if (zero_start != PAGE_CACHE_SIZE) {
3132 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3133 flush_dcache_page(page);
3136 ClearPageChecked(page);
3137 set_page_dirty(page);
3138 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3146 static void btrfs_truncate(struct inode *inode)
3148 struct btrfs_root *root = BTRFS_I(inode)->root;
3150 struct btrfs_trans_handle *trans;
3152 u64 mask = root->sectorsize - 1;
3154 if (!S_ISREG(inode->i_mode))
3156 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3159 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3160 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3162 trans = btrfs_start_transaction(root, 1);
3163 btrfs_set_trans_block_group(trans, inode);
3164 btrfs_i_size_write(inode, inode->i_size);
3166 ret = btrfs_orphan_add(trans, inode);
3169 /* FIXME, add redo link to tree so we don't leak on crash */
3170 ret = btrfs_truncate_in_trans(trans, root, inode,
3171 BTRFS_EXTENT_DATA_KEY);
3172 btrfs_update_inode(trans, root, inode);
3174 ret = btrfs_orphan_del(trans, inode);
3178 nr = trans->blocks_used;
3179 ret = btrfs_end_transaction_throttle(trans, root);
3181 btrfs_btree_balance_dirty(root, nr);
3185 * Invalidate a single dcache entry at the root of the filesystem.
3186 * Needed after creation of snapshot or subvolume.
3188 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3191 struct dentry *alias, *entry;
3194 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3198 /* change me if btrfs ever gets a d_hash operation */
3199 qstr.hash = full_name_hash(qstr.name, qstr.len);
3200 entry = d_lookup(alias, &qstr);
3203 d_invalidate(entry);
3209 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3210 struct btrfs_trans_handle *trans, u64 new_dirid,
3211 struct btrfs_block_group_cache *block_group)
3213 struct inode *inode;
3215 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3216 new_dirid, block_group, S_IFDIR | 0700);
3218 return PTR_ERR(inode);
3219 inode->i_op = &btrfs_dir_inode_operations;
3220 inode->i_fop = &btrfs_dir_file_operations;
3221 new_root->inode = inode;
3224 btrfs_i_size_write(inode, 0);
3226 return btrfs_update_inode(trans, new_root, inode);
3229 unsigned long btrfs_force_ra(struct address_space *mapping,
3230 struct file_ra_state *ra, struct file *file,
3231 pgoff_t offset, pgoff_t last_index)
3233 pgoff_t req_size = last_index - offset + 1;
3235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3236 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3239 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3240 return offset + req_size;
3244 struct inode *btrfs_alloc_inode(struct super_block *sb)
3246 struct btrfs_inode *ei;
3248 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3252 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3253 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3254 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3255 INIT_LIST_HEAD(&ei->i_orphan);
3256 return &ei->vfs_inode;
3259 void btrfs_destroy_inode(struct inode *inode)
3261 struct btrfs_ordered_extent *ordered;
3262 WARN_ON(!list_empty(&inode->i_dentry));
3263 WARN_ON(inode->i_data.nrpages);
3265 if (BTRFS_I(inode)->i_acl &&
3266 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3267 posix_acl_release(BTRFS_I(inode)->i_acl);
3268 if (BTRFS_I(inode)->i_default_acl &&
3269 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3270 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3272 spin_lock(&BTRFS_I(inode)->root->list_lock);
3273 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3274 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3275 " list\n", inode->i_ino);
3278 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3281 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3285 printk("found ordered extent %Lu %Lu\n",
3286 ordered->file_offset, ordered->len);
3287 btrfs_remove_ordered_extent(inode, ordered);
3288 btrfs_put_ordered_extent(ordered);
3289 btrfs_put_ordered_extent(ordered);
3292 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3293 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3296 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3297 static void init_once(struct kmem_cache * cachep, void *foo)
3299 static void init_once(void * foo, struct kmem_cache * cachep,
3300 unsigned long flags)
3303 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3305 inode_init_once(&ei->vfs_inode);
3308 void btrfs_destroy_cachep(void)
3310 if (btrfs_inode_cachep)
3311 kmem_cache_destroy(btrfs_inode_cachep);
3312 if (btrfs_trans_handle_cachep)
3313 kmem_cache_destroy(btrfs_trans_handle_cachep);
3314 if (btrfs_transaction_cachep)
3315 kmem_cache_destroy(btrfs_transaction_cachep);
3316 if (btrfs_bit_radix_cachep)
3317 kmem_cache_destroy(btrfs_bit_radix_cachep);
3318 if (btrfs_path_cachep)
3319 kmem_cache_destroy(btrfs_path_cachep);
3322 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3323 unsigned long extra_flags,
3324 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3325 void (*ctor)(struct kmem_cache *, void *)
3327 void (*ctor)(void *, struct kmem_cache *,
3332 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3333 SLAB_MEM_SPREAD | extra_flags), ctor
3334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3340 int btrfs_init_cachep(void)
3342 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3343 sizeof(struct btrfs_inode),
3345 if (!btrfs_inode_cachep)
3347 btrfs_trans_handle_cachep =
3348 btrfs_cache_create("btrfs_trans_handle_cache",
3349 sizeof(struct btrfs_trans_handle),
3351 if (!btrfs_trans_handle_cachep)
3353 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3354 sizeof(struct btrfs_transaction),
3356 if (!btrfs_transaction_cachep)
3358 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3359 sizeof(struct btrfs_path),
3361 if (!btrfs_path_cachep)
3363 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3364 SLAB_DESTROY_BY_RCU, NULL);
3365 if (!btrfs_bit_radix_cachep)
3369 btrfs_destroy_cachep();
3373 static int btrfs_getattr(struct vfsmount *mnt,
3374 struct dentry *dentry, struct kstat *stat)
3376 struct inode *inode = dentry->d_inode;
3377 generic_fillattr(inode, stat);
3378 stat->blksize = PAGE_CACHE_SIZE;
3379 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3383 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3384 struct inode * new_dir,struct dentry *new_dentry)
3386 struct btrfs_trans_handle *trans;
3387 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3388 struct inode *new_inode = new_dentry->d_inode;
3389 struct inode *old_inode = old_dentry->d_inode;
3390 struct timespec ctime = CURRENT_TIME;
3393 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3394 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3398 ret = btrfs_check_free_space(root, 1, 0);
3402 trans = btrfs_start_transaction(root, 1);
3404 btrfs_set_trans_block_group(trans, new_dir);
3406 old_dentry->d_inode->i_nlink++;
3407 old_dir->i_ctime = old_dir->i_mtime = ctime;
3408 new_dir->i_ctime = new_dir->i_mtime = ctime;
3409 old_inode->i_ctime = ctime;
3411 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3416 new_inode->i_ctime = CURRENT_TIME;
3417 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3420 if (new_inode->i_nlink == 0) {
3421 ret = btrfs_orphan_add(trans, new_inode);
3426 ret = btrfs_set_inode_index(new_dir, old_inode);
3430 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3435 btrfs_end_transaction_throttle(trans, root);
3440 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3441 const char *symname)
3443 struct btrfs_trans_handle *trans;
3444 struct btrfs_root *root = BTRFS_I(dir)->root;
3445 struct btrfs_path *path;
3446 struct btrfs_key key;
3447 struct inode *inode = NULL;
3454 struct btrfs_file_extent_item *ei;
3455 struct extent_buffer *leaf;
3456 unsigned long nr = 0;
3458 name_len = strlen(symname) + 1;
3459 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3460 return -ENAMETOOLONG;
3462 err = btrfs_check_free_space(root, 1, 0);
3466 trans = btrfs_start_transaction(root, 1);
3467 btrfs_set_trans_block_group(trans, dir);
3469 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3475 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3477 dentry->d_parent->d_inode->i_ino, objectid,
3478 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3479 err = PTR_ERR(inode);
3483 err = btrfs_init_acl(inode, dir);
3489 btrfs_set_trans_block_group(trans, inode);
3490 err = btrfs_add_nondir(trans, dentry, inode, 0);
3494 inode->i_mapping->a_ops = &btrfs_aops;
3495 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3496 inode->i_fop = &btrfs_file_operations;
3497 inode->i_op = &btrfs_file_inode_operations;
3498 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3499 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3500 inode->i_mapping, GFP_NOFS);
3501 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3502 inode->i_mapping, GFP_NOFS);
3503 mutex_init(&BTRFS_I(inode)->csum_mutex);
3504 mutex_init(&BTRFS_I(inode)->extent_mutex);
3505 BTRFS_I(inode)->delalloc_bytes = 0;
3506 BTRFS_I(inode)->disk_i_size = 0;
3507 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3508 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3510 dir->i_sb->s_dirt = 1;
3511 btrfs_update_inode_block_group(trans, inode);
3512 btrfs_update_inode_block_group(trans, dir);
3516 path = btrfs_alloc_path();
3518 key.objectid = inode->i_ino;
3520 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3521 datasize = btrfs_file_extent_calc_inline_size(name_len);
3522 err = btrfs_insert_empty_item(trans, root, path, &key,
3528 leaf = path->nodes[0];
3529 ei = btrfs_item_ptr(leaf, path->slots[0],
3530 struct btrfs_file_extent_item);
3531 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3532 btrfs_set_file_extent_type(leaf, ei,
3533 BTRFS_FILE_EXTENT_INLINE);
3534 ptr = btrfs_file_extent_inline_start(ei);
3535 write_extent_buffer(leaf, symname, ptr, name_len);
3536 btrfs_mark_buffer_dirty(leaf);
3537 btrfs_free_path(path);
3539 inode->i_op = &btrfs_symlink_inode_operations;
3540 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3541 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3542 btrfs_i_size_write(inode, name_len - 1);
3543 err = btrfs_update_inode(trans, root, inode);
3548 nr = trans->blocks_used;
3549 btrfs_end_transaction_throttle(trans, root);
3552 inode_dec_link_count(inode);
3555 btrfs_btree_balance_dirty(root, nr);
3559 static int btrfs_set_page_dirty(struct page *page)
3561 return __set_page_dirty_nobuffers(page);
3564 static int btrfs_permission(struct inode *inode, int mask,
3565 struct nameidata *nd)
3567 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3569 return generic_permission(inode, mask, btrfs_check_acl);
3572 static struct inode_operations btrfs_dir_inode_operations = {
3573 .lookup = btrfs_lookup,
3574 .create = btrfs_create,
3575 .unlink = btrfs_unlink,
3577 .mkdir = btrfs_mkdir,
3578 .rmdir = btrfs_rmdir,
3579 .rename = btrfs_rename,
3580 .symlink = btrfs_symlink,
3581 .setattr = btrfs_setattr,
3582 .mknod = btrfs_mknod,
3583 .setxattr = generic_setxattr,
3584 .getxattr = generic_getxattr,
3585 .listxattr = btrfs_listxattr,
3586 .removexattr = generic_removexattr,
3587 .permission = btrfs_permission,
3589 static struct inode_operations btrfs_dir_ro_inode_operations = {
3590 .lookup = btrfs_lookup,
3591 .permission = btrfs_permission,
3593 static struct file_operations btrfs_dir_file_operations = {
3594 .llseek = generic_file_llseek,
3595 .read = generic_read_dir,
3596 .readdir = btrfs_readdir,
3597 .unlocked_ioctl = btrfs_ioctl,
3598 #ifdef CONFIG_COMPAT
3599 .compat_ioctl = btrfs_ioctl,
3601 .release = btrfs_release_file,
3604 static struct extent_io_ops btrfs_extent_io_ops = {
3605 .fill_delalloc = run_delalloc_range,
3606 .submit_bio_hook = btrfs_submit_bio_hook,
3607 .merge_bio_hook = btrfs_merge_bio_hook,
3608 .readpage_io_hook = btrfs_readpage_io_hook,
3609 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3610 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3611 .writepage_start_hook = btrfs_writepage_start_hook,
3612 .readpage_io_failed_hook = btrfs_io_failed_hook,
3613 .set_bit_hook = btrfs_set_bit_hook,
3614 .clear_bit_hook = btrfs_clear_bit_hook,
3617 static struct address_space_operations btrfs_aops = {
3618 .readpage = btrfs_readpage,
3619 .writepage = btrfs_writepage,
3620 .writepages = btrfs_writepages,
3621 .readpages = btrfs_readpages,
3622 .sync_page = block_sync_page,
3624 .direct_IO = btrfs_direct_IO,
3625 .invalidatepage = btrfs_invalidatepage,
3626 .releasepage = btrfs_releasepage,
3627 .set_page_dirty = btrfs_set_page_dirty,
3630 static struct address_space_operations btrfs_symlink_aops = {
3631 .readpage = btrfs_readpage,
3632 .writepage = btrfs_writepage,
3633 .invalidatepage = btrfs_invalidatepage,
3634 .releasepage = btrfs_releasepage,
3637 static struct inode_operations btrfs_file_inode_operations = {
3638 .truncate = btrfs_truncate,
3639 .getattr = btrfs_getattr,
3640 .setattr = btrfs_setattr,
3641 .setxattr = generic_setxattr,
3642 .getxattr = generic_getxattr,
3643 .listxattr = btrfs_listxattr,
3644 .removexattr = generic_removexattr,
3645 .permission = btrfs_permission,
3647 static struct inode_operations btrfs_special_inode_operations = {
3648 .getattr = btrfs_getattr,
3649 .setattr = btrfs_setattr,
3650 .permission = btrfs_permission,
3651 .setxattr = generic_setxattr,
3652 .getxattr = generic_getxattr,
3653 .listxattr = btrfs_listxattr,
3654 .removexattr = generic_removexattr,
3656 static struct inode_operations btrfs_symlink_inode_operations = {
3657 .readlink = generic_readlink,
3658 .follow_link = page_follow_link_light,
3659 .put_link = page_put_link,
3660 .permission = btrfs_permission,