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_count_snapshots_in_path(root, path, inode->i_ino,
258 * we may be called by the resizer, make sure we're inside
259 * the limits of the FS
261 block_group = btrfs_lookup_block_group(root->fs_info,
263 if (!block_group || block_group->ro)
272 btrfs_free_path(path);
275 btrfs_release_path(root, path);
280 cow_file_range(inode, start, end);
285 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
287 struct btrfs_root *root = BTRFS_I(inode)->root;
290 if (btrfs_test_opt(root, NODATACOW) ||
291 btrfs_test_flag(inode, NODATACOW))
292 ret = run_delalloc_nocow(inode, start, end);
294 ret = cow_file_range(inode, start, end);
299 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
300 unsigned long old, unsigned long bits)
303 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
304 struct btrfs_root *root = BTRFS_I(inode)->root;
305 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
306 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
307 root->fs_info->delalloc_bytes += end - start + 1;
308 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
313 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
314 unsigned long old, unsigned long bits)
316 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
317 struct btrfs_root *root = BTRFS_I(inode)->root;
320 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
321 if (end - start + 1 > root->fs_info->delalloc_bytes) {
322 printk("warning: delalloc account %Lu %Lu\n",
323 end - start + 1, root->fs_info->delalloc_bytes);
324 root->fs_info->delalloc_bytes = 0;
325 BTRFS_I(inode)->delalloc_bytes = 0;
327 root->fs_info->delalloc_bytes -= end - start + 1;
328 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
330 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
335 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
336 size_t size, struct bio *bio)
338 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
339 struct btrfs_mapping_tree *map_tree;
340 u64 logical = bio->bi_sector << 9;
345 length = bio->bi_size;
346 map_tree = &root->fs_info->mapping_tree;
348 ret = btrfs_map_block(map_tree, READ, logical,
349 &map_length, NULL, 0);
351 if (map_length < length + size) {
357 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
360 struct btrfs_root *root = BTRFS_I(inode)->root;
363 ret = btrfs_csum_one_bio(root, inode, bio);
366 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
369 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
372 struct btrfs_root *root = BTRFS_I(inode)->root;
375 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
378 if (!(rw & (1 << BIO_RW))) {
382 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
383 inode, rw, bio, mirror_num,
384 __btrfs_submit_bio_hook);
386 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
389 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
390 struct inode *inode, u64 file_offset,
391 struct list_head *list)
393 struct list_head *cur;
394 struct btrfs_ordered_sum *sum;
396 btrfs_set_trans_block_group(trans, inode);
397 list_for_each(cur, list) {
398 sum = list_entry(cur, struct btrfs_ordered_sum, list);
399 mutex_lock(&BTRFS_I(inode)->csum_mutex);
400 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
402 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
407 struct btrfs_writepage_fixup {
409 struct btrfs_work work;
412 /* see btrfs_writepage_start_hook for details on why this is required */
413 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
415 struct btrfs_writepage_fixup *fixup;
416 struct btrfs_ordered_extent *ordered;
422 fixup = container_of(work, struct btrfs_writepage_fixup, work);
426 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
427 ClearPageChecked(page);
431 inode = page->mapping->host;
432 page_start = page_offset(page);
433 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
435 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
437 /* already ordered? We're done */
438 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
439 EXTENT_ORDERED, 0)) {
443 ordered = btrfs_lookup_ordered_extent(inode, page_start);
445 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
448 btrfs_start_ordered_extent(inode, ordered, 1);
452 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
454 ClearPageChecked(page);
456 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
459 page_cache_release(page);
463 * There are a few paths in the higher layers of the kernel that directly
464 * set the page dirty bit without asking the filesystem if it is a
465 * good idea. This causes problems because we want to make sure COW
466 * properly happens and the data=ordered rules are followed.
468 * In our case any range that doesn't have the EXTENT_ORDERED bit set
469 * hasn't been properly setup for IO. We kick off an async process
470 * to fix it up. The async helper will wait for ordered extents, set
471 * the delalloc bit and make it safe to write the page.
473 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
475 struct inode *inode = page->mapping->host;
476 struct btrfs_writepage_fixup *fixup;
477 struct btrfs_root *root = BTRFS_I(inode)->root;
480 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
485 if (PageChecked(page))
488 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
492 SetPageChecked(page);
493 page_cache_get(page);
494 fixup->work.func = btrfs_writepage_fixup_worker;
496 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
500 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
502 struct btrfs_root *root = BTRFS_I(inode)->root;
503 struct btrfs_trans_handle *trans;
504 struct btrfs_ordered_extent *ordered_extent;
505 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
506 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
507 struct extent_map *em;
508 struct extent_map *em_orig;
512 struct list_head list;
513 struct btrfs_key ins;
517 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
521 trans = btrfs_join_transaction(root, 1);
523 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
524 BUG_ON(!ordered_extent);
526 lock_extent(io_tree, ordered_extent->file_offset,
527 ordered_extent->file_offset + ordered_extent->len - 1,
530 INIT_LIST_HEAD(&list);
532 ins.objectid = ordered_extent->start;
533 ins.offset = ordered_extent->len;
534 ins.type = BTRFS_EXTENT_ITEM_KEY;
536 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
537 trans->transid, inode->i_ino,
538 ordered_extent->file_offset, &ins);
541 mutex_lock(&BTRFS_I(inode)->extent_mutex);
543 spin_lock(&em_tree->lock);
544 clear_start = ordered_extent->file_offset;
545 clear_end = ordered_extent->file_offset + ordered_extent->len;
546 em = lookup_extent_mapping(em_tree, clear_start,
547 ordered_extent->len);
549 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
550 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
551 rb = rb_next(&em->rb_node);
554 em = rb_entry(rb, struct extent_map, rb_node);
556 free_extent_map(em_orig);
557 spin_unlock(&em_tree->lock);
559 ret = btrfs_drop_extents(trans, root, inode,
560 ordered_extent->file_offset,
561 ordered_extent->file_offset +
563 ordered_extent->file_offset, &alloc_hint);
565 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
566 ordered_extent->file_offset,
567 ordered_extent->start,
569 ordered_extent->len, 0);
572 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
573 ordered_extent->file_offset +
574 ordered_extent->len - 1);
575 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
577 inode->i_blocks += ordered_extent->len >> 9;
578 unlock_extent(io_tree, ordered_extent->file_offset,
579 ordered_extent->file_offset + ordered_extent->len - 1,
581 add_pending_csums(trans, inode, ordered_extent->file_offset,
582 &ordered_extent->list);
584 btrfs_ordered_update_i_size(inode, ordered_extent);
585 btrfs_remove_ordered_extent(inode, ordered_extent);
588 btrfs_put_ordered_extent(ordered_extent);
589 /* once for the tree */
590 btrfs_put_ordered_extent(ordered_extent);
592 btrfs_update_inode(trans, root, inode);
593 btrfs_end_transaction(trans, root);
597 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
598 struct extent_state *state, int uptodate)
600 return btrfs_finish_ordered_io(page->mapping->host, start, end);
603 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
606 struct inode *inode = page->mapping->host;
607 struct btrfs_root *root = BTRFS_I(inode)->root;
608 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
609 struct btrfs_csum_item *item;
610 struct btrfs_path *path = NULL;
613 if (btrfs_test_opt(root, NODATASUM) ||
614 btrfs_test_flag(inode, NODATASUM))
618 * It is possible there is an ordered extent that has
619 * not yet finished for this range in the file. If so,
620 * that extent will have a csum cached, and it will insert
621 * the sum after all the blocks in the extent are fully
622 * on disk. So, look for an ordered extent and use the
623 * sum if found. We have to do this before looking in the
624 * btree because csum items are pre-inserted based on
625 * the file size. btrfs_lookup_csum might find an item
626 * that still hasn't been fully filled.
628 ret = btrfs_find_ordered_sum(inode, start, &csum);
633 path = btrfs_alloc_path();
634 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
637 /* a csum that isn't present is a preallocated region. */
638 if (ret == -ENOENT || ret == -EFBIG)
641 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
645 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
648 set_state_private(io_tree, start, csum);
651 btrfs_free_path(path);
655 struct io_failure_record {
663 int btrfs_io_failed_hook(struct bio *failed_bio,
664 struct page *page, u64 start, u64 end,
665 struct extent_state *state)
667 struct io_failure_record *failrec = NULL;
669 struct extent_map *em;
670 struct inode *inode = page->mapping->host;
671 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
672 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
679 ret = get_state_private(failure_tree, start, &private);
681 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
684 failrec->start = start;
685 failrec->len = end - start + 1;
686 failrec->last_mirror = 0;
688 spin_lock(&em_tree->lock);
689 em = lookup_extent_mapping(em_tree, start, failrec->len);
690 if (em->start > start || em->start + em->len < start) {
694 spin_unlock(&em_tree->lock);
696 if (!em || IS_ERR(em)) {
700 logical = start - em->start;
701 logical = em->block_start + logical;
702 failrec->logical = logical;
704 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
705 EXTENT_DIRTY, GFP_NOFS);
706 set_state_private(failure_tree, start,
707 (u64)(unsigned long)failrec);
709 failrec = (struct io_failure_record *)(unsigned long)private;
711 num_copies = btrfs_num_copies(
712 &BTRFS_I(inode)->root->fs_info->mapping_tree,
713 failrec->logical, failrec->len);
714 failrec->last_mirror++;
716 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
717 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
720 if (state && state->start != failrec->start)
722 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
724 if (!state || failrec->last_mirror > num_copies) {
725 set_state_private(failure_tree, failrec->start, 0);
726 clear_extent_bits(failure_tree, failrec->start,
727 failrec->start + failrec->len - 1,
728 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
732 bio = bio_alloc(GFP_NOFS, 1);
733 bio->bi_private = state;
734 bio->bi_end_io = failed_bio->bi_end_io;
735 bio->bi_sector = failrec->logical >> 9;
736 bio->bi_bdev = failed_bio->bi_bdev;
738 bio_add_page(bio, page, failrec->len, start - page_offset(page));
739 if (failed_bio->bi_rw & (1 << BIO_RW))
744 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
745 failrec->last_mirror);
749 int btrfs_clean_io_failures(struct inode *inode, u64 start)
753 struct io_failure_record *failure;
757 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
758 (u64)-1, 1, EXTENT_DIRTY)) {
759 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
760 start, &private_failure);
762 failure = (struct io_failure_record *)(unsigned long)
764 set_state_private(&BTRFS_I(inode)->io_failure_tree,
766 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
768 failure->start + failure->len - 1,
769 EXTENT_DIRTY | EXTENT_LOCKED,
777 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
778 struct extent_state *state)
780 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
781 struct inode *inode = page->mapping->host;
782 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
784 u64 private = ~(u32)0;
786 struct btrfs_root *root = BTRFS_I(inode)->root;
790 if (btrfs_test_opt(root, NODATASUM) ||
791 btrfs_test_flag(inode, NODATASUM))
793 if (state && state->start == start) {
794 private = state->private;
797 ret = get_state_private(io_tree, start, &private);
799 local_irq_save(flags);
800 kaddr = kmap_atomic(page, KM_IRQ0);
804 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
805 btrfs_csum_final(csum, (char *)&csum);
806 if (csum != private) {
809 kunmap_atomic(kaddr, KM_IRQ0);
810 local_irq_restore(flags);
812 /* if the io failure tree for this inode is non-empty,
813 * check to see if we've recovered from a failed IO
815 btrfs_clean_io_failures(inode, start);
819 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
820 page->mapping->host->i_ino, (unsigned long long)start, csum,
822 memset(kaddr + offset, 1, end - start + 1);
823 flush_dcache_page(page);
824 kunmap_atomic(kaddr, KM_IRQ0);
825 local_irq_restore(flags);
832 * This creates an orphan entry for the given inode in case something goes
833 * wrong in the middle of an unlink/truncate.
835 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
837 struct btrfs_root *root = BTRFS_I(inode)->root;
840 spin_lock(&root->orphan_lock);
842 /* already on the orphan list, we're good */
843 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
844 spin_unlock(&root->orphan_lock);
848 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
850 spin_unlock(&root->orphan_lock);
853 * insert an orphan item to track this unlinked/truncated file
855 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
861 * We have done the truncate/delete so we can go ahead and remove the orphan
862 * item for this particular inode.
864 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
866 struct btrfs_root *root = BTRFS_I(inode)->root;
869 spin_lock(&root->orphan_lock);
871 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
872 spin_unlock(&root->orphan_lock);
876 list_del_init(&BTRFS_I(inode)->i_orphan);
878 spin_unlock(&root->orphan_lock);
882 spin_unlock(&root->orphan_lock);
884 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
890 * this cleans up any orphans that may be left on the list from the last use
893 void btrfs_orphan_cleanup(struct btrfs_root *root)
895 struct btrfs_path *path;
896 struct extent_buffer *leaf;
897 struct btrfs_item *item;
898 struct btrfs_key key, found_key;
899 struct btrfs_trans_handle *trans;
901 int ret = 0, nr_unlink = 0, nr_truncate = 0;
903 /* don't do orphan cleanup if the fs is readonly. */
904 if (root->inode->i_sb->s_flags & MS_RDONLY)
907 path = btrfs_alloc_path();
912 key.objectid = BTRFS_ORPHAN_OBJECTID;
913 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
914 key.offset = (u64)-1;
916 trans = btrfs_start_transaction(root, 1);
917 btrfs_set_trans_block_group(trans, root->inode);
920 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
922 printk(KERN_ERR "Error searching slot for orphan: %d"
928 * if ret == 0 means we found what we were searching for, which
929 * is weird, but possible, so only screw with path if we didnt
930 * find the key and see if we have stuff that matches
933 if (path->slots[0] == 0)
938 /* pull out the item */
939 leaf = path->nodes[0];
940 item = btrfs_item_nr(leaf, path->slots[0]);
941 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
943 /* make sure the item matches what we want */
944 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
946 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
949 /* release the path since we're done with it */
950 btrfs_release_path(root, path);
953 * this is where we are basically btrfs_lookup, without the
954 * crossing root thing. we store the inode number in the
955 * offset of the orphan item.
957 inode = btrfs_iget_locked(root->inode->i_sb,
958 found_key.offset, root);
962 if (inode->i_state & I_NEW) {
963 BTRFS_I(inode)->root = root;
965 /* have to set the location manually */
966 BTRFS_I(inode)->location.objectid = inode->i_ino;
967 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
968 BTRFS_I(inode)->location.offset = 0;
970 btrfs_read_locked_inode(inode);
971 unlock_new_inode(inode);
975 * add this inode to the orphan list so btrfs_orphan_del does
976 * the proper thing when we hit it
978 spin_lock(&root->orphan_lock);
979 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
980 spin_unlock(&root->orphan_lock);
983 * if this is a bad inode, means we actually succeeded in
984 * removing the inode, but not the orphan record, which means
985 * we need to manually delete the orphan since iput will just
988 if (is_bad_inode(inode)) {
989 btrfs_orphan_del(trans, inode);
994 /* if we have links, this was a truncate, lets do that */
995 if (inode->i_nlink) {
997 btrfs_truncate(inode);
1002 /* this will do delete_inode and everything for us */
1007 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1009 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1011 btrfs_free_path(path);
1012 btrfs_end_transaction(trans, root);
1015 void btrfs_read_locked_inode(struct inode *inode)
1017 struct btrfs_path *path;
1018 struct extent_buffer *leaf;
1019 struct btrfs_inode_item *inode_item;
1020 struct btrfs_timespec *tspec;
1021 struct btrfs_root *root = BTRFS_I(inode)->root;
1022 struct btrfs_key location;
1023 u64 alloc_group_block;
1027 path = btrfs_alloc_path();
1029 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1031 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1035 leaf = path->nodes[0];
1036 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1037 struct btrfs_inode_item);
1039 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1040 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1041 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1042 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1043 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1045 tspec = btrfs_inode_atime(inode_item);
1046 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1047 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1049 tspec = btrfs_inode_mtime(inode_item);
1050 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1051 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1053 tspec = btrfs_inode_ctime(inode_item);
1054 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1055 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1057 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1058 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
1060 rdev = btrfs_inode_rdev(leaf, inode_item);
1062 BTRFS_I(inode)->index_cnt = (u64)-1;
1064 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1065 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1067 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1068 if (!BTRFS_I(inode)->block_group) {
1069 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1071 BTRFS_BLOCK_GROUP_METADATA, 0);
1073 btrfs_free_path(path);
1076 switch (inode->i_mode & S_IFMT) {
1078 inode->i_mapping->a_ops = &btrfs_aops;
1079 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1080 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1081 inode->i_fop = &btrfs_file_operations;
1082 inode->i_op = &btrfs_file_inode_operations;
1085 inode->i_fop = &btrfs_dir_file_operations;
1086 if (root == root->fs_info->tree_root)
1087 inode->i_op = &btrfs_dir_ro_inode_operations;
1089 inode->i_op = &btrfs_dir_inode_operations;
1092 inode->i_op = &btrfs_symlink_inode_operations;
1093 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1094 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1097 init_special_inode(inode, inode->i_mode, rdev);
1103 btrfs_free_path(path);
1104 make_bad_inode(inode);
1107 static void fill_inode_item(struct extent_buffer *leaf,
1108 struct btrfs_inode_item *item,
1109 struct inode *inode)
1111 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1112 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1113 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1114 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1115 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1117 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1118 inode->i_atime.tv_sec);
1119 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1120 inode->i_atime.tv_nsec);
1122 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1123 inode->i_mtime.tv_sec);
1124 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1125 inode->i_mtime.tv_nsec);
1127 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1128 inode->i_ctime.tv_sec);
1129 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1130 inode->i_ctime.tv_nsec);
1132 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1133 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1134 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1135 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1136 btrfs_set_inode_block_group(leaf, item,
1137 BTRFS_I(inode)->block_group->key.objectid);
1140 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root,
1142 struct inode *inode)
1144 struct btrfs_inode_item *inode_item;
1145 struct btrfs_path *path;
1146 struct extent_buffer *leaf;
1149 path = btrfs_alloc_path();
1151 ret = btrfs_lookup_inode(trans, root, path,
1152 &BTRFS_I(inode)->location, 1);
1159 leaf = path->nodes[0];
1160 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1161 struct btrfs_inode_item);
1163 fill_inode_item(leaf, inode_item, inode);
1164 btrfs_mark_buffer_dirty(leaf);
1165 btrfs_set_inode_last_trans(trans, inode);
1168 btrfs_free_path(path);
1173 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1174 struct btrfs_root *root,
1176 struct dentry *dentry)
1178 struct btrfs_path *path;
1179 const char *name = dentry->d_name.name;
1180 int name_len = dentry->d_name.len;
1182 struct extent_buffer *leaf;
1183 struct btrfs_dir_item *di;
1184 struct btrfs_key key;
1187 path = btrfs_alloc_path();
1193 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1194 name, name_len, -1);
1203 leaf = path->nodes[0];
1204 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1205 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1208 btrfs_release_path(root, path);
1210 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1211 dentry->d_inode->i_ino,
1212 dentry->d_parent->d_inode->i_ino, &index);
1214 printk("failed to delete reference to %.*s, "
1215 "inode %lu parent %lu\n", name_len, name,
1216 dentry->d_inode->i_ino,
1217 dentry->d_parent->d_inode->i_ino);
1221 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1222 index, name, name_len, -1);
1231 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1232 btrfs_release_path(root, path);
1234 dentry->d_inode->i_ctime = dir->i_ctime;
1236 btrfs_free_path(path);
1238 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1239 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1240 btrfs_update_inode(trans, root, dir);
1241 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1242 dentry->d_inode->i_nlink--;
1244 drop_nlink(dentry->d_inode);
1246 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1247 dir->i_sb->s_dirt = 1;
1252 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1254 struct btrfs_root *root;
1255 struct btrfs_trans_handle *trans;
1256 struct inode *inode = dentry->d_inode;
1258 unsigned long nr = 0;
1260 root = BTRFS_I(dir)->root;
1262 ret = btrfs_check_free_space(root, 1, 1);
1266 trans = btrfs_start_transaction(root, 1);
1268 btrfs_set_trans_block_group(trans, dir);
1269 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1271 if (inode->i_nlink == 0)
1272 ret = btrfs_orphan_add(trans, inode);
1274 nr = trans->blocks_used;
1276 btrfs_end_transaction_throttle(trans, root);
1278 btrfs_btree_balance_dirty(root, nr);
1282 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1284 struct inode *inode = dentry->d_inode;
1287 struct btrfs_root *root = BTRFS_I(dir)->root;
1288 struct btrfs_trans_handle *trans;
1289 unsigned long nr = 0;
1291 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1295 ret = btrfs_check_free_space(root, 1, 1);
1299 trans = btrfs_start_transaction(root, 1);
1300 btrfs_set_trans_block_group(trans, dir);
1302 err = btrfs_orphan_add(trans, inode);
1306 /* now the directory is empty */
1307 err = btrfs_unlink_trans(trans, root, dir, dentry);
1309 btrfs_i_size_write(inode, 0);
1313 nr = trans->blocks_used;
1314 ret = btrfs_end_transaction_throttle(trans, root);
1316 btrfs_btree_balance_dirty(root, nr);
1324 * this can truncate away extent items, csum items and directory items.
1325 * It starts at a high offset and removes keys until it can't find
1326 * any higher than i_size.
1328 * csum items that cross the new i_size are truncated to the new size
1331 * min_type is the minimum key type to truncate down to. If set to 0, this
1332 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1334 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1335 struct btrfs_root *root,
1336 struct inode *inode,
1340 struct btrfs_path *path;
1341 struct btrfs_key key;
1342 struct btrfs_key found_key;
1344 struct extent_buffer *leaf;
1345 struct btrfs_file_extent_item *fi;
1346 u64 extent_start = 0;
1347 u64 extent_num_bytes = 0;
1353 int pending_del_nr = 0;
1354 int pending_del_slot = 0;
1355 int extent_type = -1;
1356 u64 mask = root->sectorsize - 1;
1358 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1359 path = btrfs_alloc_path();
1363 /* FIXME, add redo link to tree so we don't leak on crash */
1364 key.objectid = inode->i_ino;
1365 key.offset = (u64)-1;
1368 btrfs_init_path(path);
1370 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1375 BUG_ON(path->slots[0] == 0);
1381 leaf = path->nodes[0];
1382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1383 found_type = btrfs_key_type(&found_key);
1385 if (found_key.objectid != inode->i_ino)
1388 if (found_type < min_type)
1391 item_end = found_key.offset;
1392 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1393 fi = btrfs_item_ptr(leaf, path->slots[0],
1394 struct btrfs_file_extent_item);
1395 extent_type = btrfs_file_extent_type(leaf, fi);
1396 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1398 btrfs_file_extent_num_bytes(leaf, fi);
1399 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1400 struct btrfs_item *item = btrfs_item_nr(leaf,
1402 item_end += btrfs_file_extent_inline_len(leaf,
1407 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1408 ret = btrfs_csum_truncate(trans, root, path,
1412 if (item_end < inode->i_size) {
1413 if (found_type == BTRFS_DIR_ITEM_KEY) {
1414 found_type = BTRFS_INODE_ITEM_KEY;
1415 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1416 found_type = BTRFS_CSUM_ITEM_KEY;
1417 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1418 found_type = BTRFS_XATTR_ITEM_KEY;
1419 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1420 found_type = BTRFS_INODE_REF_KEY;
1421 } else if (found_type) {
1426 btrfs_set_key_type(&key, found_type);
1429 if (found_key.offset >= inode->i_size)
1435 /* FIXME, shrink the extent if the ref count is only 1 */
1436 if (found_type != BTRFS_EXTENT_DATA_KEY)
1439 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1441 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1443 u64 orig_num_bytes =
1444 btrfs_file_extent_num_bytes(leaf, fi);
1445 extent_num_bytes = inode->i_size -
1446 found_key.offset + root->sectorsize - 1;
1447 extent_num_bytes = extent_num_bytes &
1448 ~((u64)root->sectorsize - 1);
1449 btrfs_set_file_extent_num_bytes(leaf, fi,
1451 num_dec = (orig_num_bytes -
1453 if (extent_start != 0)
1454 dec_i_blocks(inode, num_dec);
1455 btrfs_mark_buffer_dirty(leaf);
1458 btrfs_file_extent_disk_num_bytes(leaf,
1460 /* FIXME blocksize != 4096 */
1461 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1462 if (extent_start != 0) {
1464 dec_i_blocks(inode, num_dec);
1466 root_gen = btrfs_header_generation(leaf);
1467 root_owner = btrfs_header_owner(leaf);
1469 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1471 u32 newsize = inode->i_size - found_key.offset;
1472 dec_i_blocks(inode, item_end + 1 -
1473 found_key.offset - newsize);
1475 btrfs_file_extent_calc_inline_size(newsize);
1476 ret = btrfs_truncate_item(trans, root, path,
1480 dec_i_blocks(inode, item_end + 1 -
1486 if (!pending_del_nr) {
1487 /* no pending yet, add ourselves */
1488 pending_del_slot = path->slots[0];
1490 } else if (pending_del_nr &&
1491 path->slots[0] + 1 == pending_del_slot) {
1492 /* hop on the pending chunk */
1494 pending_del_slot = path->slots[0];
1496 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1502 ret = btrfs_free_extent(trans, root, extent_start,
1505 root_gen, inode->i_ino,
1506 found_key.offset, 0);
1510 if (path->slots[0] == 0) {
1513 btrfs_release_path(root, path);
1518 if (pending_del_nr &&
1519 path->slots[0] + 1 != pending_del_slot) {
1520 struct btrfs_key debug;
1522 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1524 ret = btrfs_del_items(trans, root, path,
1529 btrfs_release_path(root, path);
1535 if (pending_del_nr) {
1536 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1539 btrfs_free_path(path);
1540 inode->i_sb->s_dirt = 1;
1545 * taken from block_truncate_page, but does cow as it zeros out
1546 * any bytes left in the last page in the file.
1548 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1550 struct inode *inode = mapping->host;
1551 struct btrfs_root *root = BTRFS_I(inode)->root;
1552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1553 struct btrfs_ordered_extent *ordered;
1555 u32 blocksize = root->sectorsize;
1556 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1557 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1563 if ((offset & (blocksize - 1)) == 0)
1568 page = grab_cache_page(mapping, index);
1572 page_start = page_offset(page);
1573 page_end = page_start + PAGE_CACHE_SIZE - 1;
1575 if (!PageUptodate(page)) {
1576 ret = btrfs_readpage(NULL, page);
1578 if (page->mapping != mapping) {
1580 page_cache_release(page);
1583 if (!PageUptodate(page)) {
1588 wait_on_page_writeback(page);
1590 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1591 set_page_extent_mapped(page);
1593 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1595 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1597 page_cache_release(page);
1598 btrfs_start_ordered_extent(inode, ordered, 1);
1599 btrfs_put_ordered_extent(ordered);
1603 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1604 page_end, GFP_NOFS);
1606 if (offset != PAGE_CACHE_SIZE) {
1608 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1609 flush_dcache_page(page);
1612 ClearPageChecked(page);
1613 set_page_dirty(page);
1614 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1618 page_cache_release(page);
1623 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1625 struct inode *inode = dentry->d_inode;
1628 err = inode_change_ok(inode, attr);
1632 if (S_ISREG(inode->i_mode) &&
1633 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1634 struct btrfs_trans_handle *trans;
1635 struct btrfs_root *root = BTRFS_I(inode)->root;
1636 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1638 u64 mask = root->sectorsize - 1;
1639 u64 hole_start = (inode->i_size + mask) & ~mask;
1640 u64 block_end = (attr->ia_size + mask) & ~mask;
1644 if (attr->ia_size <= hole_start)
1647 err = btrfs_check_free_space(root, 1, 0);
1651 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1653 hole_size = block_end - hole_start;
1654 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1655 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1657 trans = btrfs_start_transaction(root, 1);
1658 btrfs_set_trans_block_group(trans, inode);
1659 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1660 err = btrfs_drop_extents(trans, root, inode,
1661 hole_start, block_end, hole_start,
1664 if (alloc_hint != EXTENT_MAP_INLINE) {
1665 err = btrfs_insert_file_extent(trans, root,
1669 btrfs_drop_extent_cache(inode, hole_start,
1671 btrfs_check_file(root, inode);
1673 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1674 btrfs_end_transaction(trans, root);
1675 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1680 err = inode_setattr(inode, attr);
1682 if (!err && ((attr->ia_valid & ATTR_MODE)))
1683 err = btrfs_acl_chmod(inode);
1688 void btrfs_delete_inode(struct inode *inode)
1690 struct btrfs_trans_handle *trans;
1691 struct btrfs_root *root = BTRFS_I(inode)->root;
1695 truncate_inode_pages(&inode->i_data, 0);
1696 if (is_bad_inode(inode)) {
1697 btrfs_orphan_del(NULL, inode);
1700 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1702 btrfs_i_size_write(inode, 0);
1703 trans = btrfs_start_transaction(root, 1);
1705 btrfs_set_trans_block_group(trans, inode);
1706 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1708 btrfs_orphan_del(NULL, inode);
1709 goto no_delete_lock;
1712 btrfs_orphan_del(trans, inode);
1714 nr = trans->blocks_used;
1717 btrfs_end_transaction(trans, root);
1718 btrfs_btree_balance_dirty(root, nr);
1722 nr = trans->blocks_used;
1723 btrfs_end_transaction(trans, root);
1724 btrfs_btree_balance_dirty(root, nr);
1730 * this returns the key found in the dir entry in the location pointer.
1731 * If no dir entries were found, location->objectid is 0.
1733 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1734 struct btrfs_key *location)
1736 const char *name = dentry->d_name.name;
1737 int namelen = dentry->d_name.len;
1738 struct btrfs_dir_item *di;
1739 struct btrfs_path *path;
1740 struct btrfs_root *root = BTRFS_I(dir)->root;
1743 if (namelen == 1 && strcmp(name, ".") == 0) {
1744 location->objectid = dir->i_ino;
1745 location->type = BTRFS_INODE_ITEM_KEY;
1746 location->offset = 0;
1749 path = btrfs_alloc_path();
1752 if (namelen == 2 && strcmp(name, "..") == 0) {
1753 struct btrfs_key key;
1754 struct extent_buffer *leaf;
1758 key.objectid = dir->i_ino;
1759 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1765 leaf = path->nodes[0];
1766 slot = path->slots[0];
1767 nritems = btrfs_header_nritems(leaf);
1768 if (slot >= nritems)
1771 btrfs_item_key_to_cpu(leaf, &key, slot);
1772 if (key.objectid != dir->i_ino ||
1773 key.type != BTRFS_INODE_REF_KEY) {
1776 location->objectid = key.offset;
1777 location->type = BTRFS_INODE_ITEM_KEY;
1778 location->offset = 0;
1782 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1786 if (!di || IS_ERR(di)) {
1789 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1791 btrfs_free_path(path);
1794 location->objectid = 0;
1799 * when we hit a tree root in a directory, the btrfs part of the inode
1800 * needs to be changed to reflect the root directory of the tree root. This
1801 * is kind of like crossing a mount point.
1803 static int fixup_tree_root_location(struct btrfs_root *root,
1804 struct btrfs_key *location,
1805 struct btrfs_root **sub_root,
1806 struct dentry *dentry)
1808 struct btrfs_root_item *ri;
1810 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1812 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1815 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1816 dentry->d_name.name,
1817 dentry->d_name.len);
1818 if (IS_ERR(*sub_root))
1819 return PTR_ERR(*sub_root);
1821 ri = &(*sub_root)->root_item;
1822 location->objectid = btrfs_root_dirid(ri);
1823 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1824 location->offset = 0;
1829 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1831 struct btrfs_iget_args *args = p;
1832 inode->i_ino = args->ino;
1833 BTRFS_I(inode)->root = args->root;
1834 BTRFS_I(inode)->delalloc_bytes = 0;
1835 BTRFS_I(inode)->disk_i_size = 0;
1836 BTRFS_I(inode)->index_cnt = (u64)-1;
1837 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1838 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1839 inode->i_mapping, GFP_NOFS);
1840 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1841 inode->i_mapping, GFP_NOFS);
1842 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1843 mutex_init(&BTRFS_I(inode)->csum_mutex);
1844 mutex_init(&BTRFS_I(inode)->extent_mutex);
1848 static int btrfs_find_actor(struct inode *inode, void *opaque)
1850 struct btrfs_iget_args *args = opaque;
1851 return (args->ino == inode->i_ino &&
1852 args->root == BTRFS_I(inode)->root);
1855 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1858 struct btrfs_iget_args args;
1859 args.ino = objectid;
1860 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1865 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1868 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1869 struct btrfs_root *root)
1871 struct inode *inode;
1872 struct btrfs_iget_args args;
1873 args.ino = objectid;
1876 inode = iget5_locked(s, objectid, btrfs_find_actor,
1877 btrfs_init_locked_inode,
1882 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1883 struct nameidata *nd)
1885 struct inode * inode;
1886 struct btrfs_inode *bi = BTRFS_I(dir);
1887 struct btrfs_root *root = bi->root;
1888 struct btrfs_root *sub_root = root;
1889 struct btrfs_key location;
1890 int ret, do_orphan = 0;
1892 if (dentry->d_name.len > BTRFS_NAME_LEN)
1893 return ERR_PTR(-ENAMETOOLONG);
1895 ret = btrfs_inode_by_name(dir, dentry, &location);
1898 return ERR_PTR(ret);
1901 if (location.objectid) {
1902 ret = fixup_tree_root_location(root, &location, &sub_root,
1905 return ERR_PTR(ret);
1907 return ERR_PTR(-ENOENT);
1909 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1912 return ERR_PTR(-EACCES);
1913 if (inode->i_state & I_NEW) {
1914 /* the inode and parent dir are two different roots */
1915 if (sub_root != root) {
1917 sub_root->inode = inode;
1920 BTRFS_I(inode)->root = sub_root;
1921 memcpy(&BTRFS_I(inode)->location, &location,
1923 btrfs_read_locked_inode(inode);
1924 unlock_new_inode(inode);
1928 if (unlikely(do_orphan))
1929 btrfs_orphan_cleanup(sub_root);
1931 return d_splice_alias(inode, dentry);
1934 static unsigned char btrfs_filetype_table[] = {
1935 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1938 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1940 struct inode *inode = filp->f_dentry->d_inode;
1941 struct btrfs_root *root = BTRFS_I(inode)->root;
1942 struct btrfs_item *item;
1943 struct btrfs_dir_item *di;
1944 struct btrfs_key key;
1945 struct btrfs_key found_key;
1946 struct btrfs_path *path;
1949 struct extent_buffer *leaf;
1952 unsigned char d_type;
1957 int key_type = BTRFS_DIR_INDEX_KEY;
1962 /* FIXME, use a real flag for deciding about the key type */
1963 if (root->fs_info->tree_root == root)
1964 key_type = BTRFS_DIR_ITEM_KEY;
1966 /* special case for "." */
1967 if (filp->f_pos == 0) {
1968 over = filldir(dirent, ".", 1,
1976 key.objectid = inode->i_ino;
1977 path = btrfs_alloc_path();
1980 /* special case for .., just use the back ref */
1981 if (filp->f_pos == 1) {
1982 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1984 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1986 leaf = path->nodes[0];
1987 slot = path->slots[0];
1988 nritems = btrfs_header_nritems(leaf);
1989 if (slot >= nritems) {
1990 btrfs_release_path(root, path);
1991 goto read_dir_items;
1993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1994 btrfs_release_path(root, path);
1995 if (found_key.objectid != key.objectid ||
1996 found_key.type != BTRFS_INODE_REF_KEY)
1997 goto read_dir_items;
1998 over = filldir(dirent, "..", 2,
1999 2, found_key.offset, DT_DIR);
2006 btrfs_set_key_type(&key, key_type);
2007 key.offset = filp->f_pos;
2009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2014 leaf = path->nodes[0];
2015 nritems = btrfs_header_nritems(leaf);
2016 slot = path->slots[0];
2017 if (advance || slot >= nritems) {
2018 if (slot >= nritems -1) {
2019 ret = btrfs_next_leaf(root, path);
2022 leaf = path->nodes[0];
2023 nritems = btrfs_header_nritems(leaf);
2024 slot = path->slots[0];
2031 item = btrfs_item_nr(leaf, slot);
2032 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2034 if (found_key.objectid != key.objectid)
2036 if (btrfs_key_type(&found_key) != key_type)
2038 if (found_key.offset < filp->f_pos)
2041 filp->f_pos = found_key.offset;
2043 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2045 di_total = btrfs_item_size(leaf, item);
2046 while(di_cur < di_total) {
2047 struct btrfs_key location;
2049 name_len = btrfs_dir_name_len(leaf, di);
2050 if (name_len < 32) {
2051 name_ptr = tmp_name;
2053 name_ptr = kmalloc(name_len, GFP_NOFS);
2056 read_extent_buffer(leaf, name_ptr,
2057 (unsigned long)(di + 1), name_len);
2059 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2060 btrfs_dir_item_key_to_cpu(leaf, di, &location);
2061 over = filldir(dirent, name_ptr, name_len,
2066 if (name_ptr != tmp_name)
2071 di_len = btrfs_dir_name_len(leaf, di) +
2072 btrfs_dir_data_len(leaf, di) +sizeof(*di);
2074 di = (struct btrfs_dir_item *)((char *)di + di_len);
2077 if (key_type == BTRFS_DIR_INDEX_KEY)
2078 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2084 btrfs_free_path(path);
2088 int btrfs_write_inode(struct inode *inode, int wait)
2090 struct btrfs_root *root = BTRFS_I(inode)->root;
2091 struct btrfs_trans_handle *trans;
2095 trans = btrfs_join_transaction(root, 1);
2096 btrfs_set_trans_block_group(trans, inode);
2097 ret = btrfs_commit_transaction(trans, root);
2103 * This is somewhat expensive, updating the tree every time the
2104 * inode changes. But, it is most likely to find the inode in cache.
2105 * FIXME, needs more benchmarking...there are no reasons other than performance
2106 * to keep or drop this code.
2108 void btrfs_dirty_inode(struct inode *inode)
2110 struct btrfs_root *root = BTRFS_I(inode)->root;
2111 struct btrfs_trans_handle *trans;
2113 trans = btrfs_join_transaction(root, 1);
2114 btrfs_set_trans_block_group(trans, inode);
2115 btrfs_update_inode(trans, root, inode);
2116 btrfs_end_transaction(trans, root);
2119 static int btrfs_set_inode_index_count(struct inode *inode)
2121 struct btrfs_root *root = BTRFS_I(inode)->root;
2122 struct btrfs_key key, found_key;
2123 struct btrfs_path *path;
2124 struct extent_buffer *leaf;
2127 key.objectid = inode->i_ino;
2128 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2129 key.offset = (u64)-1;
2131 path = btrfs_alloc_path();
2135 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2138 /* FIXME: we should be able to handle this */
2144 * MAGIC NUMBER EXPLANATION:
2145 * since we search a directory based on f_pos we have to start at 2
2146 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2147 * else has to start at 2
2149 if (path->slots[0] == 0) {
2150 BTRFS_I(inode)->index_cnt = 2;
2156 leaf = path->nodes[0];
2157 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2159 if (found_key.objectid != inode->i_ino ||
2160 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2161 BTRFS_I(inode)->index_cnt = 2;
2165 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2167 btrfs_free_path(path);
2171 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2175 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2176 ret = btrfs_set_inode_index_count(dir);
2181 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2182 BTRFS_I(dir)->index_cnt++;
2187 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2188 struct btrfs_root *root,
2190 const char *name, int name_len,
2193 struct btrfs_block_group_cache *group,
2196 struct inode *inode;
2197 struct btrfs_inode_item *inode_item;
2198 struct btrfs_block_group_cache *new_inode_group;
2199 struct btrfs_key *location;
2200 struct btrfs_path *path;
2201 struct btrfs_inode_ref *ref;
2202 struct btrfs_key key[2];
2208 path = btrfs_alloc_path();
2211 inode = new_inode(root->fs_info->sb);
2213 return ERR_PTR(-ENOMEM);
2216 ret = btrfs_set_inode_index(dir, inode);
2218 return ERR_PTR(ret);
2220 BTRFS_I(inode)->index = 0;
2223 * index_cnt is ignored for everything but a dir,
2224 * btrfs_get_inode_index_count has an explanation for the magic
2227 BTRFS_I(inode)->index_cnt = 2;
2229 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2230 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2231 inode->i_mapping, GFP_NOFS);
2232 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2233 inode->i_mapping, GFP_NOFS);
2234 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2235 mutex_init(&BTRFS_I(inode)->csum_mutex);
2236 mutex_init(&BTRFS_I(inode)->extent_mutex);
2237 BTRFS_I(inode)->delalloc_bytes = 0;
2238 BTRFS_I(inode)->disk_i_size = 0;
2239 BTRFS_I(inode)->root = root;
2245 new_inode_group = btrfs_find_block_group(root, group, 0,
2246 BTRFS_BLOCK_GROUP_METADATA, owner);
2247 if (!new_inode_group) {
2248 printk("find_block group failed\n");
2249 new_inode_group = group;
2251 BTRFS_I(inode)->block_group = new_inode_group;
2252 BTRFS_I(inode)->flags = 0;
2254 key[0].objectid = objectid;
2255 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2258 key[1].objectid = objectid;
2259 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2260 key[1].offset = ref_objectid;
2262 sizes[0] = sizeof(struct btrfs_inode_item);
2263 sizes[1] = name_len + sizeof(*ref);
2265 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2269 if (objectid > root->highest_inode)
2270 root->highest_inode = objectid;
2272 inode->i_uid = current->fsuid;
2273 inode->i_gid = current->fsgid;
2274 inode->i_mode = mode;
2275 inode->i_ino = objectid;
2276 inode->i_blocks = 0;
2277 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2278 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2279 struct btrfs_inode_item);
2280 fill_inode_item(path->nodes[0], inode_item, inode);
2282 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2283 struct btrfs_inode_ref);
2284 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2285 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
2286 ptr = (unsigned long)(ref + 1);
2287 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2289 btrfs_mark_buffer_dirty(path->nodes[0]);
2290 btrfs_free_path(path);
2292 location = &BTRFS_I(inode)->location;
2293 location->objectid = objectid;
2294 location->offset = 0;
2295 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2297 insert_inode_hash(inode);
2301 BTRFS_I(dir)->index_cnt--;
2302 btrfs_free_path(path);
2303 return ERR_PTR(ret);
2306 static inline u8 btrfs_inode_type(struct inode *inode)
2308 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2311 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2312 struct dentry *dentry, struct inode *inode,
2316 struct btrfs_key key;
2317 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2318 struct inode *parent_inode = dentry->d_parent->d_inode;
2320 key.objectid = inode->i_ino;
2321 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2324 ret = btrfs_insert_dir_item(trans, root,
2325 dentry->d_name.name, dentry->d_name.len,
2326 dentry->d_parent->d_inode->i_ino,
2327 &key, btrfs_inode_type(inode),
2328 BTRFS_I(inode)->index);
2331 ret = btrfs_insert_inode_ref(trans, root,
2332 dentry->d_name.name,
2335 parent_inode->i_ino,
2336 BTRFS_I(inode)->index);
2338 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2339 dentry->d_name.len * 2);
2340 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2341 ret = btrfs_update_inode(trans, root,
2342 dentry->d_parent->d_inode);
2347 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2348 struct dentry *dentry, struct inode *inode,
2351 int err = btrfs_add_link(trans, dentry, inode, backref);
2353 d_instantiate(dentry, inode);
2361 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2362 int mode, dev_t rdev)
2364 struct btrfs_trans_handle *trans;
2365 struct btrfs_root *root = BTRFS_I(dir)->root;
2366 struct inode *inode = NULL;
2370 unsigned long nr = 0;
2372 if (!new_valid_dev(rdev))
2375 err = btrfs_check_free_space(root, 1, 0);
2379 trans = btrfs_start_transaction(root, 1);
2380 btrfs_set_trans_block_group(trans, dir);
2382 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2388 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2390 dentry->d_parent->d_inode->i_ino, objectid,
2391 BTRFS_I(dir)->block_group, mode);
2392 err = PTR_ERR(inode);
2396 err = btrfs_init_acl(inode, dir);
2402 btrfs_set_trans_block_group(trans, inode);
2403 err = btrfs_add_nondir(trans, dentry, inode, 0);
2407 inode->i_op = &btrfs_special_inode_operations;
2408 init_special_inode(inode, inode->i_mode, rdev);
2409 btrfs_update_inode(trans, root, inode);
2411 dir->i_sb->s_dirt = 1;
2412 btrfs_update_inode_block_group(trans, inode);
2413 btrfs_update_inode_block_group(trans, dir);
2415 nr = trans->blocks_used;
2416 btrfs_end_transaction_throttle(trans, root);
2419 inode_dec_link_count(inode);
2422 btrfs_btree_balance_dirty(root, nr);
2426 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2427 int mode, struct nameidata *nd)
2429 struct btrfs_trans_handle *trans;
2430 struct btrfs_root *root = BTRFS_I(dir)->root;
2431 struct inode *inode = NULL;
2434 unsigned long nr = 0;
2437 err = btrfs_check_free_space(root, 1, 0);
2440 trans = btrfs_start_transaction(root, 1);
2441 btrfs_set_trans_block_group(trans, dir);
2443 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2449 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2451 dentry->d_parent->d_inode->i_ino,
2452 objectid, BTRFS_I(dir)->block_group, mode);
2453 err = PTR_ERR(inode);
2457 err = btrfs_init_acl(inode, dir);
2463 btrfs_set_trans_block_group(trans, inode);
2464 err = btrfs_add_nondir(trans, dentry, inode, 0);
2468 inode->i_mapping->a_ops = &btrfs_aops;
2469 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2470 inode->i_fop = &btrfs_file_operations;
2471 inode->i_op = &btrfs_file_inode_operations;
2472 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2473 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2474 inode->i_mapping, GFP_NOFS);
2475 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2476 inode->i_mapping, GFP_NOFS);
2477 mutex_init(&BTRFS_I(inode)->csum_mutex);
2478 mutex_init(&BTRFS_I(inode)->extent_mutex);
2479 BTRFS_I(inode)->delalloc_bytes = 0;
2480 BTRFS_I(inode)->disk_i_size = 0;
2481 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2482 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2484 dir->i_sb->s_dirt = 1;
2485 btrfs_update_inode_block_group(trans, inode);
2486 btrfs_update_inode_block_group(trans, dir);
2488 nr = trans->blocks_used;
2489 btrfs_end_transaction_throttle(trans, root);
2492 inode_dec_link_count(inode);
2495 btrfs_btree_balance_dirty(root, nr);
2499 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2500 struct dentry *dentry)
2502 struct btrfs_trans_handle *trans;
2503 struct btrfs_root *root = BTRFS_I(dir)->root;
2504 struct inode *inode = old_dentry->d_inode;
2505 unsigned long nr = 0;
2509 if (inode->i_nlink == 0)
2512 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2517 err = btrfs_check_free_space(root, 1, 0);
2520 err = btrfs_set_inode_index(dir, inode);
2524 trans = btrfs_start_transaction(root, 1);
2526 btrfs_set_trans_block_group(trans, dir);
2527 atomic_inc(&inode->i_count);
2529 err = btrfs_add_nondir(trans, dentry, inode, 1);
2534 dir->i_sb->s_dirt = 1;
2535 btrfs_update_inode_block_group(trans, dir);
2536 err = btrfs_update_inode(trans, root, inode);
2541 nr = trans->blocks_used;
2542 btrfs_end_transaction_throttle(trans, root);
2545 inode_dec_link_count(inode);
2548 btrfs_btree_balance_dirty(root, nr);
2552 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2554 struct inode *inode = NULL;
2555 struct btrfs_trans_handle *trans;
2556 struct btrfs_root *root = BTRFS_I(dir)->root;
2558 int drop_on_err = 0;
2560 unsigned long nr = 1;
2562 err = btrfs_check_free_space(root, 1, 0);
2566 trans = btrfs_start_transaction(root, 1);
2567 btrfs_set_trans_block_group(trans, dir);
2569 if (IS_ERR(trans)) {
2570 err = PTR_ERR(trans);
2574 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2580 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2582 dentry->d_parent->d_inode->i_ino, objectid,
2583 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2584 if (IS_ERR(inode)) {
2585 err = PTR_ERR(inode);
2591 err = btrfs_init_acl(inode, dir);
2595 inode->i_op = &btrfs_dir_inode_operations;
2596 inode->i_fop = &btrfs_dir_file_operations;
2597 btrfs_set_trans_block_group(trans, inode);
2599 btrfs_i_size_write(inode, 0);
2600 err = btrfs_update_inode(trans, root, inode);
2604 err = btrfs_add_link(trans, dentry, inode, 0);
2608 d_instantiate(dentry, inode);
2610 dir->i_sb->s_dirt = 1;
2611 btrfs_update_inode_block_group(trans, inode);
2612 btrfs_update_inode_block_group(trans, dir);
2615 nr = trans->blocks_used;
2616 btrfs_end_transaction_throttle(trans, root);
2621 btrfs_btree_balance_dirty(root, nr);
2625 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2626 struct extent_map *existing,
2627 struct extent_map *em,
2628 u64 map_start, u64 map_len)
2632 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2633 start_diff = map_start - em->start;
2634 em->start = map_start;
2636 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2637 em->block_start += start_diff;
2638 return add_extent_mapping(em_tree, em);
2641 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2642 size_t pg_offset, u64 start, u64 len,
2648 u64 extent_start = 0;
2650 u64 objectid = inode->i_ino;
2652 struct btrfs_path *path = NULL;
2653 struct btrfs_root *root = BTRFS_I(inode)->root;
2654 struct btrfs_file_extent_item *item;
2655 struct extent_buffer *leaf;
2656 struct btrfs_key found_key;
2657 struct extent_map *em = NULL;
2658 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2659 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2660 struct btrfs_trans_handle *trans = NULL;
2663 spin_lock(&em_tree->lock);
2664 em = lookup_extent_mapping(em_tree, start, len);
2666 em->bdev = root->fs_info->fs_devices->latest_bdev;
2667 spin_unlock(&em_tree->lock);
2670 if (em->start > start || em->start + em->len <= start)
2671 free_extent_map(em);
2672 else if (em->block_start == EXTENT_MAP_INLINE && page)
2673 free_extent_map(em);
2677 em = alloc_extent_map(GFP_NOFS);
2682 em->bdev = root->fs_info->fs_devices->latest_bdev;
2683 em->start = EXTENT_MAP_HOLE;
2687 path = btrfs_alloc_path();
2691 ret = btrfs_lookup_file_extent(trans, root, path,
2692 objectid, start, trans != NULL);
2699 if (path->slots[0] == 0)
2704 leaf = path->nodes[0];
2705 item = btrfs_item_ptr(leaf, path->slots[0],
2706 struct btrfs_file_extent_item);
2707 /* are we inside the extent that was found? */
2708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2709 found_type = btrfs_key_type(&found_key);
2710 if (found_key.objectid != objectid ||
2711 found_type != BTRFS_EXTENT_DATA_KEY) {
2715 found_type = btrfs_file_extent_type(leaf, item);
2716 extent_start = found_key.offset;
2717 if (found_type == BTRFS_FILE_EXTENT_REG) {
2718 extent_end = extent_start +
2719 btrfs_file_extent_num_bytes(leaf, item);
2721 if (start < extent_start || start >= extent_end) {
2723 if (start < extent_start) {
2724 if (start + len <= extent_start)
2726 em->len = extent_end - extent_start;
2732 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2734 em->start = extent_start;
2735 em->len = extent_end - extent_start;
2736 em->block_start = EXTENT_MAP_HOLE;
2739 bytenr += btrfs_file_extent_offset(leaf, item);
2740 em->block_start = bytenr;
2741 em->start = extent_start;
2742 em->len = extent_end - extent_start;
2744 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2749 size_t extent_offset;
2752 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2754 extent_end = (extent_start + size + root->sectorsize - 1) &
2755 ~((u64)root->sectorsize - 1);
2756 if (start < extent_start || start >= extent_end) {
2758 if (start < extent_start) {
2759 if (start + len <= extent_start)
2761 em->len = extent_end - extent_start;
2767 em->block_start = EXTENT_MAP_INLINE;
2770 em->start = extent_start;
2775 page_start = page_offset(page) + pg_offset;
2776 extent_offset = page_start - extent_start;
2777 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2778 size - extent_offset);
2779 em->start = extent_start + extent_offset;
2780 em->len = (copy_size + root->sectorsize - 1) &
2781 ~((u64)root->sectorsize - 1);
2783 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2784 if (create == 0 && !PageUptodate(page)) {
2785 read_extent_buffer(leaf, map + pg_offset, ptr,
2787 flush_dcache_page(page);
2788 } else if (create && PageUptodate(page)) {
2791 free_extent_map(em);
2793 btrfs_release_path(root, path);
2794 trans = btrfs_join_transaction(root, 1);
2797 write_extent_buffer(leaf, map + pg_offset, ptr,
2799 btrfs_mark_buffer_dirty(leaf);
2802 set_extent_uptodate(io_tree, em->start,
2803 extent_map_end(em) - 1, GFP_NOFS);
2806 printk("unkknown found_type %d\n", found_type);
2813 em->block_start = EXTENT_MAP_HOLE;
2815 btrfs_release_path(root, path);
2816 if (em->start > start || extent_map_end(em) <= start) {
2817 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2823 spin_lock(&em_tree->lock);
2824 ret = add_extent_mapping(em_tree, em);
2825 /* it is possible that someone inserted the extent into the tree
2826 * while we had the lock dropped. It is also possible that
2827 * an overlapping map exists in the tree
2829 if (ret == -EEXIST) {
2830 struct extent_map *existing;
2834 existing = lookup_extent_mapping(em_tree, start, len);
2835 if (existing && (existing->start > start ||
2836 existing->start + existing->len <= start)) {
2837 free_extent_map(existing);
2841 existing = lookup_extent_mapping(em_tree, em->start,
2844 err = merge_extent_mapping(em_tree, existing,
2847 free_extent_map(existing);
2849 free_extent_map(em);
2854 printk("failing to insert %Lu %Lu\n",
2856 free_extent_map(em);
2860 free_extent_map(em);
2865 spin_unlock(&em_tree->lock);
2868 btrfs_free_path(path);
2870 ret = btrfs_end_transaction(trans, root);
2876 free_extent_map(em);
2878 return ERR_PTR(err);
2883 #if 0 /* waiting for O_DIRECT reads */
2884 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2885 struct buffer_head *bh_result, int create)
2887 struct extent_map *em;
2888 u64 start = (u64)iblock << inode->i_blkbits;
2889 struct btrfs_multi_bio *multi = NULL;
2890 struct btrfs_root *root = BTRFS_I(inode)->root;
2896 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2898 if (!em || IS_ERR(em))
2901 if (em->start > start || em->start + em->len <= start) {
2905 if (em->block_start == EXTENT_MAP_INLINE) {
2910 len = em->start + em->len - start;
2911 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2913 if (em->block_start == EXTENT_MAP_HOLE ||
2914 em->block_start == EXTENT_MAP_DELALLOC) {
2915 bh_result->b_size = len;
2919 logical = start - em->start;
2920 logical = em->block_start + logical;
2923 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2924 logical, &map_length, &multi, 0);
2926 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2927 bh_result->b_size = min(map_length, len);
2929 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2930 set_buffer_mapped(bh_result);
2933 free_extent_map(em);
2938 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2939 const struct iovec *iov, loff_t offset,
2940 unsigned long nr_segs)
2944 struct file *file = iocb->ki_filp;
2945 struct inode *inode = file->f_mapping->host;
2950 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2951 offset, nr_segs, btrfs_get_block, NULL);
2955 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2957 return extent_bmap(mapping, iblock, btrfs_get_extent);
2960 int btrfs_readpage(struct file *file, struct page *page)
2962 struct extent_io_tree *tree;
2963 tree = &BTRFS_I(page->mapping->host)->io_tree;
2964 return extent_read_full_page(tree, page, btrfs_get_extent);
2967 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2969 struct extent_io_tree *tree;
2972 if (current->flags & PF_MEMALLOC) {
2973 redirty_page_for_writepage(wbc, page);
2977 tree = &BTRFS_I(page->mapping->host)->io_tree;
2978 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2981 int btrfs_writepages(struct address_space *mapping,
2982 struct writeback_control *wbc)
2984 struct extent_io_tree *tree;
2985 tree = &BTRFS_I(mapping->host)->io_tree;
2986 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2990 btrfs_readpages(struct file *file, struct address_space *mapping,
2991 struct list_head *pages, unsigned nr_pages)
2993 struct extent_io_tree *tree;
2994 tree = &BTRFS_I(mapping->host)->io_tree;
2995 return extent_readpages(tree, mapping, pages, nr_pages,
2998 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3000 struct extent_io_tree *tree;
3001 struct extent_map_tree *map;
3004 tree = &BTRFS_I(page->mapping->host)->io_tree;
3005 map = &BTRFS_I(page->mapping->host)->extent_tree;
3006 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3008 ClearPagePrivate(page);
3009 set_page_private(page, 0);
3010 page_cache_release(page);
3015 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3017 return __btrfs_releasepage(page, gfp_flags);
3020 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3022 struct extent_io_tree *tree;
3023 struct btrfs_ordered_extent *ordered;
3024 u64 page_start = page_offset(page);
3025 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3027 wait_on_page_writeback(page);
3028 tree = &BTRFS_I(page->mapping->host)->io_tree;
3030 btrfs_releasepage(page, GFP_NOFS);
3034 lock_extent(tree, page_start, page_end, GFP_NOFS);
3035 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3039 * IO on this page will never be started, so we need
3040 * to account for any ordered extents now
3042 clear_extent_bit(tree, page_start, page_end,
3043 EXTENT_DIRTY | EXTENT_DELALLOC |
3044 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3045 btrfs_finish_ordered_io(page->mapping->host,
3046 page_start, page_end);
3047 btrfs_put_ordered_extent(ordered);
3048 lock_extent(tree, page_start, page_end, GFP_NOFS);
3050 clear_extent_bit(tree, page_start, page_end,
3051 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3054 __btrfs_releasepage(page, GFP_NOFS);
3056 ClearPageChecked(page);
3057 if (PagePrivate(page)) {
3058 ClearPagePrivate(page);
3059 set_page_private(page, 0);
3060 page_cache_release(page);
3065 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3066 * called from a page fault handler when a page is first dirtied. Hence we must
3067 * be careful to check for EOF conditions here. We set the page up correctly
3068 * for a written page which means we get ENOSPC checking when writing into
3069 * holes and correct delalloc and unwritten extent mapping on filesystems that
3070 * support these features.
3072 * We are not allowed to take the i_mutex here so we have to play games to
3073 * protect against truncate races as the page could now be beyond EOF. Because
3074 * vmtruncate() writes the inode size before removing pages, once we have the
3075 * page lock we can determine safely if the page is beyond EOF. If it is not
3076 * beyond EOF, then the page is guaranteed safe against truncation until we
3079 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3081 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3082 struct btrfs_root *root = BTRFS_I(inode)->root;
3083 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3084 struct btrfs_ordered_extent *ordered;
3086 unsigned long zero_start;
3092 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3099 size = i_size_read(inode);
3100 page_start = page_offset(page);
3101 page_end = page_start + PAGE_CACHE_SIZE - 1;
3103 if ((page->mapping != inode->i_mapping) ||
3104 (page_start >= size)) {
3105 /* page got truncated out from underneath us */
3108 wait_on_page_writeback(page);
3110 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3111 set_page_extent_mapped(page);
3114 * we can't set the delalloc bits if there are pending ordered
3115 * extents. Drop our locks and wait for them to finish
3117 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3119 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3121 btrfs_start_ordered_extent(inode, ordered, 1);
3122 btrfs_put_ordered_extent(ordered);
3126 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3127 page_end, GFP_NOFS);
3130 /* page is wholly or partially inside EOF */
3131 if (page_start + PAGE_CACHE_SIZE > size)
3132 zero_start = size & ~PAGE_CACHE_MASK;
3134 zero_start = PAGE_CACHE_SIZE;
3136 if (zero_start != PAGE_CACHE_SIZE) {
3138 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3139 flush_dcache_page(page);
3142 ClearPageChecked(page);
3143 set_page_dirty(page);
3144 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3152 static void btrfs_truncate(struct inode *inode)
3154 struct btrfs_root *root = BTRFS_I(inode)->root;
3156 struct btrfs_trans_handle *trans;
3158 u64 mask = root->sectorsize - 1;
3160 if (!S_ISREG(inode->i_mode))
3162 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3165 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3166 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3168 trans = btrfs_start_transaction(root, 1);
3169 btrfs_set_trans_block_group(trans, inode);
3170 btrfs_i_size_write(inode, inode->i_size);
3172 ret = btrfs_orphan_add(trans, inode);
3175 /* FIXME, add redo link to tree so we don't leak on crash */
3176 ret = btrfs_truncate_in_trans(trans, root, inode,
3177 BTRFS_EXTENT_DATA_KEY);
3178 btrfs_update_inode(trans, root, inode);
3180 ret = btrfs_orphan_del(trans, inode);
3184 nr = trans->blocks_used;
3185 ret = btrfs_end_transaction_throttle(trans, root);
3187 btrfs_btree_balance_dirty(root, nr);
3191 * Invalidate a single dcache entry at the root of the filesystem.
3192 * Needed after creation of snapshot or subvolume.
3194 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3197 struct dentry *alias, *entry;
3200 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3204 /* change me if btrfs ever gets a d_hash operation */
3205 qstr.hash = full_name_hash(qstr.name, qstr.len);
3206 entry = d_lookup(alias, &qstr);
3209 d_invalidate(entry);
3215 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3216 struct btrfs_trans_handle *trans, u64 new_dirid,
3217 struct btrfs_block_group_cache *block_group)
3219 struct inode *inode;
3221 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3222 new_dirid, block_group, S_IFDIR | 0700);
3224 return PTR_ERR(inode);
3225 inode->i_op = &btrfs_dir_inode_operations;
3226 inode->i_fop = &btrfs_dir_file_operations;
3227 new_root->inode = inode;
3230 btrfs_i_size_write(inode, 0);
3232 return btrfs_update_inode(trans, new_root, inode);
3235 unsigned long btrfs_force_ra(struct address_space *mapping,
3236 struct file_ra_state *ra, struct file *file,
3237 pgoff_t offset, pgoff_t last_index)
3239 pgoff_t req_size = last_index - offset + 1;
3241 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3242 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3245 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3246 return offset + req_size;
3250 struct inode *btrfs_alloc_inode(struct super_block *sb)
3252 struct btrfs_inode *ei;
3254 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3258 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3259 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3260 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3261 INIT_LIST_HEAD(&ei->i_orphan);
3262 return &ei->vfs_inode;
3265 void btrfs_destroy_inode(struct inode *inode)
3267 struct btrfs_ordered_extent *ordered;
3268 WARN_ON(!list_empty(&inode->i_dentry));
3269 WARN_ON(inode->i_data.nrpages);
3271 if (BTRFS_I(inode)->i_acl &&
3272 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3273 posix_acl_release(BTRFS_I(inode)->i_acl);
3274 if (BTRFS_I(inode)->i_default_acl &&
3275 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3276 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3278 spin_lock(&BTRFS_I(inode)->root->orphan_lock);
3279 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3280 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3281 " list\n", inode->i_ino);
3284 spin_unlock(&BTRFS_I(inode)->root->orphan_lock);
3287 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3291 printk("found ordered extent %Lu %Lu\n",
3292 ordered->file_offset, ordered->len);
3293 btrfs_remove_ordered_extent(inode, ordered);
3294 btrfs_put_ordered_extent(ordered);
3295 btrfs_put_ordered_extent(ordered);
3298 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3299 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3302 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3303 static void init_once(struct kmem_cache * cachep, void *foo)
3305 static void init_once(void * foo, struct kmem_cache * cachep,
3306 unsigned long flags)
3309 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3311 inode_init_once(&ei->vfs_inode);
3314 void btrfs_destroy_cachep(void)
3316 if (btrfs_inode_cachep)
3317 kmem_cache_destroy(btrfs_inode_cachep);
3318 if (btrfs_trans_handle_cachep)
3319 kmem_cache_destroy(btrfs_trans_handle_cachep);
3320 if (btrfs_transaction_cachep)
3321 kmem_cache_destroy(btrfs_transaction_cachep);
3322 if (btrfs_bit_radix_cachep)
3323 kmem_cache_destroy(btrfs_bit_radix_cachep);
3324 if (btrfs_path_cachep)
3325 kmem_cache_destroy(btrfs_path_cachep);
3328 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3329 unsigned long extra_flags,
3330 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3331 void (*ctor)(struct kmem_cache *, void *)
3333 void (*ctor)(void *, struct kmem_cache *,
3338 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3339 SLAB_MEM_SPREAD | extra_flags), ctor
3340 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3346 int btrfs_init_cachep(void)
3348 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3349 sizeof(struct btrfs_inode),
3351 if (!btrfs_inode_cachep)
3353 btrfs_trans_handle_cachep =
3354 btrfs_cache_create("btrfs_trans_handle_cache",
3355 sizeof(struct btrfs_trans_handle),
3357 if (!btrfs_trans_handle_cachep)
3359 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3360 sizeof(struct btrfs_transaction),
3362 if (!btrfs_transaction_cachep)
3364 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3365 sizeof(struct btrfs_path),
3367 if (!btrfs_path_cachep)
3369 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3370 SLAB_DESTROY_BY_RCU, NULL);
3371 if (!btrfs_bit_radix_cachep)
3375 btrfs_destroy_cachep();
3379 static int btrfs_getattr(struct vfsmount *mnt,
3380 struct dentry *dentry, struct kstat *stat)
3382 struct inode *inode = dentry->d_inode;
3383 generic_fillattr(inode, stat);
3384 stat->blksize = PAGE_CACHE_SIZE;
3385 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3389 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3390 struct inode * new_dir,struct dentry *new_dentry)
3392 struct btrfs_trans_handle *trans;
3393 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3394 struct inode *new_inode = new_dentry->d_inode;
3395 struct inode *old_inode = old_dentry->d_inode;
3396 struct timespec ctime = CURRENT_TIME;
3399 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3400 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3404 ret = btrfs_check_free_space(root, 1, 0);
3408 trans = btrfs_start_transaction(root, 1);
3410 btrfs_set_trans_block_group(trans, new_dir);
3412 old_dentry->d_inode->i_nlink++;
3413 old_dir->i_ctime = old_dir->i_mtime = ctime;
3414 new_dir->i_ctime = new_dir->i_mtime = ctime;
3415 old_inode->i_ctime = ctime;
3417 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3422 new_inode->i_ctime = CURRENT_TIME;
3423 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3426 if (new_inode->i_nlink == 0) {
3427 ret = btrfs_orphan_add(trans, new_inode);
3432 ret = btrfs_set_inode_index(new_dir, old_inode);
3436 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3441 btrfs_end_transaction(trans, root);
3446 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3447 const char *symname)
3449 struct btrfs_trans_handle *trans;
3450 struct btrfs_root *root = BTRFS_I(dir)->root;
3451 struct btrfs_path *path;
3452 struct btrfs_key key;
3453 struct inode *inode = NULL;
3460 struct btrfs_file_extent_item *ei;
3461 struct extent_buffer *leaf;
3462 unsigned long nr = 0;
3464 name_len = strlen(symname) + 1;
3465 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3466 return -ENAMETOOLONG;
3468 err = btrfs_check_free_space(root, 1, 0);
3472 trans = btrfs_start_transaction(root, 1);
3473 btrfs_set_trans_block_group(trans, dir);
3475 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3481 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3483 dentry->d_parent->d_inode->i_ino, objectid,
3484 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3485 err = PTR_ERR(inode);
3489 err = btrfs_init_acl(inode, dir);
3495 btrfs_set_trans_block_group(trans, inode);
3496 err = btrfs_add_nondir(trans, dentry, inode, 0);
3500 inode->i_mapping->a_ops = &btrfs_aops;
3501 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3502 inode->i_fop = &btrfs_file_operations;
3503 inode->i_op = &btrfs_file_inode_operations;
3504 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3505 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3506 inode->i_mapping, GFP_NOFS);
3507 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3508 inode->i_mapping, GFP_NOFS);
3509 mutex_init(&BTRFS_I(inode)->csum_mutex);
3510 mutex_init(&BTRFS_I(inode)->extent_mutex);
3511 BTRFS_I(inode)->delalloc_bytes = 0;
3512 BTRFS_I(inode)->disk_i_size = 0;
3513 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3514 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3516 dir->i_sb->s_dirt = 1;
3517 btrfs_update_inode_block_group(trans, inode);
3518 btrfs_update_inode_block_group(trans, dir);
3522 path = btrfs_alloc_path();
3524 key.objectid = inode->i_ino;
3526 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3527 datasize = btrfs_file_extent_calc_inline_size(name_len);
3528 err = btrfs_insert_empty_item(trans, root, path, &key,
3534 leaf = path->nodes[0];
3535 ei = btrfs_item_ptr(leaf, path->slots[0],
3536 struct btrfs_file_extent_item);
3537 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3538 btrfs_set_file_extent_type(leaf, ei,
3539 BTRFS_FILE_EXTENT_INLINE);
3540 ptr = btrfs_file_extent_inline_start(ei);
3541 write_extent_buffer(leaf, symname, ptr, name_len);
3542 btrfs_mark_buffer_dirty(leaf);
3543 btrfs_free_path(path);
3545 inode->i_op = &btrfs_symlink_inode_operations;
3546 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3547 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3548 btrfs_i_size_write(inode, name_len - 1);
3549 err = btrfs_update_inode(trans, root, inode);
3554 nr = trans->blocks_used;
3555 btrfs_end_transaction_throttle(trans, root);
3558 inode_dec_link_count(inode);
3561 btrfs_btree_balance_dirty(root, nr);
3565 static int btrfs_set_page_dirty(struct page *page)
3567 return __set_page_dirty_nobuffers(page);
3570 static int btrfs_permission(struct inode *inode, int mask,
3571 struct nameidata *nd)
3573 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3575 return generic_permission(inode, mask, btrfs_check_acl);
3578 static struct inode_operations btrfs_dir_inode_operations = {
3579 .lookup = btrfs_lookup,
3580 .create = btrfs_create,
3581 .unlink = btrfs_unlink,
3583 .mkdir = btrfs_mkdir,
3584 .rmdir = btrfs_rmdir,
3585 .rename = btrfs_rename,
3586 .symlink = btrfs_symlink,
3587 .setattr = btrfs_setattr,
3588 .mknod = btrfs_mknod,
3589 .setxattr = generic_setxattr,
3590 .getxattr = generic_getxattr,
3591 .listxattr = btrfs_listxattr,
3592 .removexattr = generic_removexattr,
3593 .permission = btrfs_permission,
3595 static struct inode_operations btrfs_dir_ro_inode_operations = {
3596 .lookup = btrfs_lookup,
3597 .permission = btrfs_permission,
3599 static struct file_operations btrfs_dir_file_operations = {
3600 .llseek = generic_file_llseek,
3601 .read = generic_read_dir,
3602 .readdir = btrfs_readdir,
3603 .unlocked_ioctl = btrfs_ioctl,
3604 #ifdef CONFIG_COMPAT
3605 .compat_ioctl = btrfs_ioctl,
3607 .release = btrfs_release_file,
3610 static struct extent_io_ops btrfs_extent_io_ops = {
3611 .fill_delalloc = run_delalloc_range,
3612 .submit_bio_hook = btrfs_submit_bio_hook,
3613 .merge_bio_hook = btrfs_merge_bio_hook,
3614 .readpage_io_hook = btrfs_readpage_io_hook,
3615 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3616 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3617 .writepage_start_hook = btrfs_writepage_start_hook,
3618 .readpage_io_failed_hook = btrfs_io_failed_hook,
3619 .set_bit_hook = btrfs_set_bit_hook,
3620 .clear_bit_hook = btrfs_clear_bit_hook,
3623 static struct address_space_operations btrfs_aops = {
3624 .readpage = btrfs_readpage,
3625 .writepage = btrfs_writepage,
3626 .writepages = btrfs_writepages,
3627 .readpages = btrfs_readpages,
3628 .sync_page = block_sync_page,
3630 .direct_IO = btrfs_direct_IO,
3631 .invalidatepage = btrfs_invalidatepage,
3632 .releasepage = btrfs_releasepage,
3633 .set_page_dirty = btrfs_set_page_dirty,
3636 static struct address_space_operations btrfs_symlink_aops = {
3637 .readpage = btrfs_readpage,
3638 .writepage = btrfs_writepage,
3639 .invalidatepage = btrfs_invalidatepage,
3640 .releasepage = btrfs_releasepage,
3643 static struct inode_operations btrfs_file_inode_operations = {
3644 .truncate = btrfs_truncate,
3645 .getattr = btrfs_getattr,
3646 .setattr = btrfs_setattr,
3647 .setxattr = generic_setxattr,
3648 .getxattr = generic_getxattr,
3649 .listxattr = btrfs_listxattr,
3650 .removexattr = generic_removexattr,
3651 .permission = btrfs_permission,
3653 static struct inode_operations btrfs_special_inode_operations = {
3654 .getattr = btrfs_getattr,
3655 .setattr = btrfs_setattr,
3656 .permission = btrfs_permission,
3657 .setxattr = generic_setxattr,
3658 .getxattr = generic_getxattr,
3659 .listxattr = btrfs_listxattr,
3660 .removexattr = generic_removexattr,
3662 static struct inode_operations btrfs_symlink_inode_operations = {
3663 .readlink = generic_readlink,
3664 .follow_link = page_follow_link_light,
3665 .put_link = page_put_link,
3666 .permission = btrfs_permission,