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))) {
377 if (!btrfs_test_opt(root, NODATASUM) &&
378 !btrfs_test_flag(inode, NODATASUM)) {
379 btrfs_lookup_bio_sums(root, inode, bio);
384 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
385 inode, rw, bio, mirror_num,
386 __btrfs_submit_bio_hook);
388 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
391 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
392 struct inode *inode, u64 file_offset,
393 struct list_head *list)
395 struct list_head *cur;
396 struct btrfs_ordered_sum *sum;
398 btrfs_set_trans_block_group(trans, inode);
399 list_for_each(cur, list) {
400 sum = list_entry(cur, struct btrfs_ordered_sum, list);
401 mutex_lock(&BTRFS_I(inode)->csum_mutex);
402 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
404 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
409 struct btrfs_writepage_fixup {
411 struct btrfs_work work;
414 /* see btrfs_writepage_start_hook for details on why this is required */
415 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
417 struct btrfs_writepage_fixup *fixup;
418 struct btrfs_ordered_extent *ordered;
424 fixup = container_of(work, struct btrfs_writepage_fixup, work);
428 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
429 ClearPageChecked(page);
433 inode = page->mapping->host;
434 page_start = page_offset(page);
435 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
437 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
439 /* already ordered? We're done */
440 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
441 EXTENT_ORDERED, 0)) {
445 ordered = btrfs_lookup_ordered_extent(inode, page_start);
447 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
450 btrfs_start_ordered_extent(inode, ordered, 1);
454 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
456 ClearPageChecked(page);
458 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
461 page_cache_release(page);
465 * There are a few paths in the higher layers of the kernel that directly
466 * set the page dirty bit without asking the filesystem if it is a
467 * good idea. This causes problems because we want to make sure COW
468 * properly happens and the data=ordered rules are followed.
470 * In our case any range that doesn't have the EXTENT_ORDERED bit set
471 * hasn't been properly setup for IO. We kick off an async process
472 * to fix it up. The async helper will wait for ordered extents, set
473 * the delalloc bit and make it safe to write the page.
475 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
477 struct inode *inode = page->mapping->host;
478 struct btrfs_writepage_fixup *fixup;
479 struct btrfs_root *root = BTRFS_I(inode)->root;
482 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
487 if (PageChecked(page))
490 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
494 SetPageChecked(page);
495 page_cache_get(page);
496 fixup->work.func = btrfs_writepage_fixup_worker;
498 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
502 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
504 struct btrfs_root *root = BTRFS_I(inode)->root;
505 struct btrfs_trans_handle *trans;
506 struct btrfs_ordered_extent *ordered_extent;
507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
509 struct list_head list;
510 struct btrfs_key ins;
513 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
517 trans = btrfs_join_transaction(root, 1);
519 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
520 BUG_ON(!ordered_extent);
522 lock_extent(io_tree, ordered_extent->file_offset,
523 ordered_extent->file_offset + ordered_extent->len - 1,
526 INIT_LIST_HEAD(&list);
528 ins.objectid = ordered_extent->start;
529 ins.offset = ordered_extent->len;
530 ins.type = BTRFS_EXTENT_ITEM_KEY;
532 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
533 trans->transid, inode->i_ino,
534 ordered_extent->file_offset, &ins);
537 mutex_lock(&BTRFS_I(inode)->extent_mutex);
539 ret = btrfs_drop_extents(trans, root, inode,
540 ordered_extent->file_offset,
541 ordered_extent->file_offset +
543 ordered_extent->file_offset, &alloc_hint);
545 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
546 ordered_extent->file_offset,
547 ordered_extent->start,
549 ordered_extent->len, 0);
552 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
553 ordered_extent->file_offset +
554 ordered_extent->len - 1);
555 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
557 inode->i_blocks += ordered_extent->len >> 9;
558 unlock_extent(io_tree, ordered_extent->file_offset,
559 ordered_extent->file_offset + ordered_extent->len - 1,
561 add_pending_csums(trans, inode, ordered_extent->file_offset,
562 &ordered_extent->list);
564 btrfs_ordered_update_i_size(inode, ordered_extent);
565 btrfs_remove_ordered_extent(inode, ordered_extent);
568 btrfs_put_ordered_extent(ordered_extent);
569 /* once for the tree */
570 btrfs_put_ordered_extent(ordered_extent);
572 btrfs_update_inode(trans, root, inode);
573 btrfs_end_transaction(trans, root);
577 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
578 struct extent_state *state, int uptodate)
580 return btrfs_finish_ordered_io(page->mapping->host, start, end);
583 struct io_failure_record {
591 int btrfs_io_failed_hook(struct bio *failed_bio,
592 struct page *page, u64 start, u64 end,
593 struct extent_state *state)
595 struct io_failure_record *failrec = NULL;
597 struct extent_map *em;
598 struct inode *inode = page->mapping->host;
599 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
600 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
607 ret = get_state_private(failure_tree, start, &private);
609 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
612 failrec->start = start;
613 failrec->len = end - start + 1;
614 failrec->last_mirror = 0;
616 spin_lock(&em_tree->lock);
617 em = lookup_extent_mapping(em_tree, start, failrec->len);
618 if (em->start > start || em->start + em->len < start) {
622 spin_unlock(&em_tree->lock);
624 if (!em || IS_ERR(em)) {
628 logical = start - em->start;
629 logical = em->block_start + logical;
630 failrec->logical = logical;
632 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
633 EXTENT_DIRTY, GFP_NOFS);
634 set_state_private(failure_tree, start,
635 (u64)(unsigned long)failrec);
637 failrec = (struct io_failure_record *)(unsigned long)private;
639 num_copies = btrfs_num_copies(
640 &BTRFS_I(inode)->root->fs_info->mapping_tree,
641 failrec->logical, failrec->len);
642 failrec->last_mirror++;
644 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
645 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
648 if (state && state->start != failrec->start)
650 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
652 if (!state || failrec->last_mirror > num_copies) {
653 set_state_private(failure_tree, failrec->start, 0);
654 clear_extent_bits(failure_tree, failrec->start,
655 failrec->start + failrec->len - 1,
656 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
660 bio = bio_alloc(GFP_NOFS, 1);
661 bio->bi_private = state;
662 bio->bi_end_io = failed_bio->bi_end_io;
663 bio->bi_sector = failrec->logical >> 9;
664 bio->bi_bdev = failed_bio->bi_bdev;
666 bio_add_page(bio, page, failrec->len, start - page_offset(page));
667 if (failed_bio->bi_rw & (1 << BIO_RW))
672 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
673 failrec->last_mirror);
677 int btrfs_clean_io_failures(struct inode *inode, u64 start)
681 struct io_failure_record *failure;
685 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
686 (u64)-1, 1, EXTENT_DIRTY)) {
687 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
688 start, &private_failure);
690 failure = (struct io_failure_record *)(unsigned long)
692 set_state_private(&BTRFS_I(inode)->io_failure_tree,
694 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
696 failure->start + failure->len - 1,
697 EXTENT_DIRTY | EXTENT_LOCKED,
705 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
706 struct extent_state *state)
708 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
709 struct inode *inode = page->mapping->host;
710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
712 u64 private = ~(u32)0;
714 struct btrfs_root *root = BTRFS_I(inode)->root;
718 if (btrfs_test_opt(root, NODATASUM) ||
719 btrfs_test_flag(inode, NODATASUM))
721 if (state && state->start == start) {
722 private = state->private;
725 ret = get_state_private(io_tree, start, &private);
727 local_irq_save(flags);
728 kaddr = kmap_atomic(page, KM_IRQ0);
732 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
733 btrfs_csum_final(csum, (char *)&csum);
734 if (csum != private) {
737 kunmap_atomic(kaddr, KM_IRQ0);
738 local_irq_restore(flags);
740 /* if the io failure tree for this inode is non-empty,
741 * check to see if we've recovered from a failed IO
743 btrfs_clean_io_failures(inode, start);
747 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
748 page->mapping->host->i_ino, (unsigned long long)start, csum,
750 memset(kaddr + offset, 1, end - start + 1);
751 flush_dcache_page(page);
752 kunmap_atomic(kaddr, KM_IRQ0);
753 local_irq_restore(flags);
760 * This creates an orphan entry for the given inode in case something goes
761 * wrong in the middle of an unlink/truncate.
763 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
765 struct btrfs_root *root = BTRFS_I(inode)->root;
768 spin_lock(&root->list_lock);
770 /* already on the orphan list, we're good */
771 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
772 spin_unlock(&root->list_lock);
776 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
778 spin_unlock(&root->list_lock);
781 * insert an orphan item to track this unlinked/truncated file
783 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
789 * We have done the truncate/delete so we can go ahead and remove the orphan
790 * item for this particular inode.
792 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
794 struct btrfs_root *root = BTRFS_I(inode)->root;
797 spin_lock(&root->list_lock);
799 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
800 spin_unlock(&root->list_lock);
804 list_del_init(&BTRFS_I(inode)->i_orphan);
806 spin_unlock(&root->list_lock);
810 spin_unlock(&root->list_lock);
812 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
818 * this cleans up any orphans that may be left on the list from the last use
821 void btrfs_orphan_cleanup(struct btrfs_root *root)
823 struct btrfs_path *path;
824 struct extent_buffer *leaf;
825 struct btrfs_item *item;
826 struct btrfs_key key, found_key;
827 struct btrfs_trans_handle *trans;
829 int ret = 0, nr_unlink = 0, nr_truncate = 0;
831 /* don't do orphan cleanup if the fs is readonly. */
832 if (root->inode->i_sb->s_flags & MS_RDONLY)
835 path = btrfs_alloc_path();
840 key.objectid = BTRFS_ORPHAN_OBJECTID;
841 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
842 key.offset = (u64)-1;
844 trans = btrfs_start_transaction(root, 1);
845 btrfs_set_trans_block_group(trans, root->inode);
848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
850 printk(KERN_ERR "Error searching slot for orphan: %d"
856 * if ret == 0 means we found what we were searching for, which
857 * is weird, but possible, so only screw with path if we didnt
858 * find the key and see if we have stuff that matches
861 if (path->slots[0] == 0)
866 /* pull out the item */
867 leaf = path->nodes[0];
868 item = btrfs_item_nr(leaf, path->slots[0]);
869 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
871 /* make sure the item matches what we want */
872 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
874 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
877 /* release the path since we're done with it */
878 btrfs_release_path(root, path);
881 * this is where we are basically btrfs_lookup, without the
882 * crossing root thing. we store the inode number in the
883 * offset of the orphan item.
885 inode = btrfs_iget_locked(root->inode->i_sb,
886 found_key.offset, root);
890 if (inode->i_state & I_NEW) {
891 BTRFS_I(inode)->root = root;
893 /* have to set the location manually */
894 BTRFS_I(inode)->location.objectid = inode->i_ino;
895 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
896 BTRFS_I(inode)->location.offset = 0;
898 btrfs_read_locked_inode(inode);
899 unlock_new_inode(inode);
903 * add this inode to the orphan list so btrfs_orphan_del does
904 * the proper thing when we hit it
906 spin_lock(&root->list_lock);
907 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
908 spin_unlock(&root->list_lock);
911 * if this is a bad inode, means we actually succeeded in
912 * removing the inode, but not the orphan record, which means
913 * we need to manually delete the orphan since iput will just
916 if (is_bad_inode(inode)) {
917 btrfs_orphan_del(trans, inode);
922 /* if we have links, this was a truncate, lets do that */
923 if (inode->i_nlink) {
925 btrfs_truncate(inode);
930 /* this will do delete_inode and everything for us */
935 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
937 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
939 btrfs_free_path(path);
940 btrfs_end_transaction(trans, root);
943 void btrfs_read_locked_inode(struct inode *inode)
945 struct btrfs_path *path;
946 struct extent_buffer *leaf;
947 struct btrfs_inode_item *inode_item;
948 struct btrfs_timespec *tspec;
949 struct btrfs_root *root = BTRFS_I(inode)->root;
950 struct btrfs_key location;
951 u64 alloc_group_block;
955 path = btrfs_alloc_path();
957 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
959 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
963 leaf = path->nodes[0];
964 inode_item = btrfs_item_ptr(leaf, path->slots[0],
965 struct btrfs_inode_item);
967 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
968 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
969 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
970 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
971 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
973 tspec = btrfs_inode_atime(inode_item);
974 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
975 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
977 tspec = btrfs_inode_mtime(inode_item);
978 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
979 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
981 tspec = btrfs_inode_ctime(inode_item);
982 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
983 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
985 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
986 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
988 rdev = btrfs_inode_rdev(leaf, inode_item);
990 BTRFS_I(inode)->index_cnt = (u64)-1;
992 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
993 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
995 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
996 if (!BTRFS_I(inode)->block_group) {
997 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
999 BTRFS_BLOCK_GROUP_METADATA, 0);
1001 btrfs_free_path(path);
1004 switch (inode->i_mode & S_IFMT) {
1006 inode->i_mapping->a_ops = &btrfs_aops;
1007 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1008 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1009 inode->i_fop = &btrfs_file_operations;
1010 inode->i_op = &btrfs_file_inode_operations;
1013 inode->i_fop = &btrfs_dir_file_operations;
1014 if (root == root->fs_info->tree_root)
1015 inode->i_op = &btrfs_dir_ro_inode_operations;
1017 inode->i_op = &btrfs_dir_inode_operations;
1020 inode->i_op = &btrfs_symlink_inode_operations;
1021 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1022 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1025 init_special_inode(inode, inode->i_mode, rdev);
1031 btrfs_free_path(path);
1032 make_bad_inode(inode);
1035 static void fill_inode_item(struct extent_buffer *leaf,
1036 struct btrfs_inode_item *item,
1037 struct inode *inode)
1039 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1040 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1041 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1042 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1043 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1045 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1046 inode->i_atime.tv_sec);
1047 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1048 inode->i_atime.tv_nsec);
1050 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1051 inode->i_mtime.tv_sec);
1052 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1053 inode->i_mtime.tv_nsec);
1055 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1056 inode->i_ctime.tv_sec);
1057 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1058 inode->i_ctime.tv_nsec);
1060 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1061 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1062 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1063 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1064 btrfs_set_inode_block_group(leaf, item,
1065 BTRFS_I(inode)->block_group->key.objectid);
1068 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1069 struct btrfs_root *root,
1070 struct inode *inode)
1072 struct btrfs_inode_item *inode_item;
1073 struct btrfs_path *path;
1074 struct extent_buffer *leaf;
1077 path = btrfs_alloc_path();
1079 ret = btrfs_lookup_inode(trans, root, path,
1080 &BTRFS_I(inode)->location, 1);
1087 leaf = path->nodes[0];
1088 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1089 struct btrfs_inode_item);
1091 fill_inode_item(leaf, inode_item, inode);
1092 btrfs_mark_buffer_dirty(leaf);
1093 btrfs_set_inode_last_trans(trans, inode);
1096 btrfs_free_path(path);
1101 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1102 struct btrfs_root *root,
1104 struct dentry *dentry)
1106 struct btrfs_path *path;
1107 const char *name = dentry->d_name.name;
1108 int name_len = dentry->d_name.len;
1110 struct extent_buffer *leaf;
1111 struct btrfs_dir_item *di;
1112 struct btrfs_key key;
1115 path = btrfs_alloc_path();
1121 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1122 name, name_len, -1);
1131 leaf = path->nodes[0];
1132 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1133 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1136 btrfs_release_path(root, path);
1138 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1139 dentry->d_inode->i_ino,
1140 dentry->d_parent->d_inode->i_ino, &index);
1142 printk("failed to delete reference to %.*s, "
1143 "inode %lu parent %lu\n", name_len, name,
1144 dentry->d_inode->i_ino,
1145 dentry->d_parent->d_inode->i_ino);
1149 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1150 index, name, name_len, -1);
1159 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1160 btrfs_release_path(root, path);
1162 dentry->d_inode->i_ctime = dir->i_ctime;
1164 btrfs_free_path(path);
1166 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1167 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1168 btrfs_update_inode(trans, root, dir);
1169 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1170 dentry->d_inode->i_nlink--;
1172 drop_nlink(dentry->d_inode);
1174 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1175 dir->i_sb->s_dirt = 1;
1180 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1182 struct btrfs_root *root;
1183 struct btrfs_trans_handle *trans;
1184 struct inode *inode = dentry->d_inode;
1186 unsigned long nr = 0;
1188 root = BTRFS_I(dir)->root;
1190 ret = btrfs_check_free_space(root, 1, 1);
1194 trans = btrfs_start_transaction(root, 1);
1196 btrfs_set_trans_block_group(trans, dir);
1197 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1199 if (inode->i_nlink == 0)
1200 ret = btrfs_orphan_add(trans, inode);
1202 nr = trans->blocks_used;
1204 btrfs_end_transaction_throttle(trans, root);
1206 btrfs_btree_balance_dirty(root, nr);
1210 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1212 struct inode *inode = dentry->d_inode;
1215 struct btrfs_root *root = BTRFS_I(dir)->root;
1216 struct btrfs_trans_handle *trans;
1217 unsigned long nr = 0;
1219 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1223 ret = btrfs_check_free_space(root, 1, 1);
1227 trans = btrfs_start_transaction(root, 1);
1228 btrfs_set_trans_block_group(trans, dir);
1230 err = btrfs_orphan_add(trans, inode);
1234 /* now the directory is empty */
1235 err = btrfs_unlink_trans(trans, root, dir, dentry);
1237 btrfs_i_size_write(inode, 0);
1241 nr = trans->blocks_used;
1242 ret = btrfs_end_transaction_throttle(trans, root);
1244 btrfs_btree_balance_dirty(root, nr);
1252 * this can truncate away extent items, csum items and directory items.
1253 * It starts at a high offset and removes keys until it can't find
1254 * any higher than i_size.
1256 * csum items that cross the new i_size are truncated to the new size
1259 * min_type is the minimum key type to truncate down to. If set to 0, this
1260 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1262 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
1264 struct inode *inode,
1268 struct btrfs_path *path;
1269 struct btrfs_key key;
1270 struct btrfs_key found_key;
1272 struct extent_buffer *leaf;
1273 struct btrfs_file_extent_item *fi;
1274 u64 extent_start = 0;
1275 u64 extent_num_bytes = 0;
1281 int pending_del_nr = 0;
1282 int pending_del_slot = 0;
1283 int extent_type = -1;
1284 u64 mask = root->sectorsize - 1;
1286 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1287 path = btrfs_alloc_path();
1291 /* FIXME, add redo link to tree so we don't leak on crash */
1292 key.objectid = inode->i_ino;
1293 key.offset = (u64)-1;
1296 btrfs_init_path(path);
1298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1303 BUG_ON(path->slots[0] == 0);
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1311 found_type = btrfs_key_type(&found_key);
1313 if (found_key.objectid != inode->i_ino)
1316 if (found_type < min_type)
1319 item_end = found_key.offset;
1320 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1321 fi = btrfs_item_ptr(leaf, path->slots[0],
1322 struct btrfs_file_extent_item);
1323 extent_type = btrfs_file_extent_type(leaf, fi);
1324 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1326 btrfs_file_extent_num_bytes(leaf, fi);
1327 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1328 struct btrfs_item *item = btrfs_item_nr(leaf,
1330 item_end += btrfs_file_extent_inline_len(leaf,
1335 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1336 ret = btrfs_csum_truncate(trans, root, path,
1340 if (item_end < inode->i_size) {
1341 if (found_type == BTRFS_DIR_ITEM_KEY) {
1342 found_type = BTRFS_INODE_ITEM_KEY;
1343 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1344 found_type = BTRFS_CSUM_ITEM_KEY;
1345 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1346 found_type = BTRFS_XATTR_ITEM_KEY;
1347 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1348 found_type = BTRFS_INODE_REF_KEY;
1349 } else if (found_type) {
1354 btrfs_set_key_type(&key, found_type);
1357 if (found_key.offset >= inode->i_size)
1363 /* FIXME, shrink the extent if the ref count is only 1 */
1364 if (found_type != BTRFS_EXTENT_DATA_KEY)
1367 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1369 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1371 u64 orig_num_bytes =
1372 btrfs_file_extent_num_bytes(leaf, fi);
1373 extent_num_bytes = inode->i_size -
1374 found_key.offset + root->sectorsize - 1;
1375 extent_num_bytes = extent_num_bytes &
1376 ~((u64)root->sectorsize - 1);
1377 btrfs_set_file_extent_num_bytes(leaf, fi,
1379 num_dec = (orig_num_bytes -
1381 if (extent_start != 0)
1382 dec_i_blocks(inode, num_dec);
1383 btrfs_mark_buffer_dirty(leaf);
1386 btrfs_file_extent_disk_num_bytes(leaf,
1388 /* FIXME blocksize != 4096 */
1389 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1390 if (extent_start != 0) {
1392 dec_i_blocks(inode, num_dec);
1394 root_gen = btrfs_header_generation(leaf);
1395 root_owner = btrfs_header_owner(leaf);
1397 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1399 u32 newsize = inode->i_size - found_key.offset;
1400 dec_i_blocks(inode, item_end + 1 -
1401 found_key.offset - newsize);
1403 btrfs_file_extent_calc_inline_size(newsize);
1404 ret = btrfs_truncate_item(trans, root, path,
1408 dec_i_blocks(inode, item_end + 1 -
1414 if (!pending_del_nr) {
1415 /* no pending yet, add ourselves */
1416 pending_del_slot = path->slots[0];
1418 } else if (pending_del_nr &&
1419 path->slots[0] + 1 == pending_del_slot) {
1420 /* hop on the pending chunk */
1422 pending_del_slot = path->slots[0];
1424 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1430 ret = btrfs_free_extent(trans, root, extent_start,
1433 root_gen, inode->i_ino,
1434 found_key.offset, 0);
1438 if (path->slots[0] == 0) {
1441 btrfs_release_path(root, path);
1446 if (pending_del_nr &&
1447 path->slots[0] + 1 != pending_del_slot) {
1448 struct btrfs_key debug;
1450 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1452 ret = btrfs_del_items(trans, root, path,
1457 btrfs_release_path(root, path);
1463 if (pending_del_nr) {
1464 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1467 btrfs_free_path(path);
1468 inode->i_sb->s_dirt = 1;
1473 * taken from block_truncate_page, but does cow as it zeros out
1474 * any bytes left in the last page in the file.
1476 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1478 struct inode *inode = mapping->host;
1479 struct btrfs_root *root = BTRFS_I(inode)->root;
1480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1481 struct btrfs_ordered_extent *ordered;
1483 u32 blocksize = root->sectorsize;
1484 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1485 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1491 if ((offset & (blocksize - 1)) == 0)
1496 page = grab_cache_page(mapping, index);
1500 page_start = page_offset(page);
1501 page_end = page_start + PAGE_CACHE_SIZE - 1;
1503 if (!PageUptodate(page)) {
1504 ret = btrfs_readpage(NULL, page);
1506 if (page->mapping != mapping) {
1508 page_cache_release(page);
1511 if (!PageUptodate(page)) {
1516 wait_on_page_writeback(page);
1518 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1519 set_page_extent_mapped(page);
1521 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1523 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1525 page_cache_release(page);
1526 btrfs_start_ordered_extent(inode, ordered, 1);
1527 btrfs_put_ordered_extent(ordered);
1531 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1532 page_end, GFP_NOFS);
1534 if (offset != PAGE_CACHE_SIZE) {
1536 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1537 flush_dcache_page(page);
1540 ClearPageChecked(page);
1541 set_page_dirty(page);
1542 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1546 page_cache_release(page);
1551 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1553 struct inode *inode = dentry->d_inode;
1556 err = inode_change_ok(inode, attr);
1560 if (S_ISREG(inode->i_mode) &&
1561 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1562 struct btrfs_trans_handle *trans;
1563 struct btrfs_root *root = BTRFS_I(inode)->root;
1564 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1566 u64 mask = root->sectorsize - 1;
1567 u64 hole_start = (inode->i_size + mask) & ~mask;
1568 u64 block_end = (attr->ia_size + mask) & ~mask;
1572 if (attr->ia_size <= hole_start)
1575 err = btrfs_check_free_space(root, 1, 0);
1579 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1581 hole_size = block_end - hole_start;
1582 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1583 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1585 trans = btrfs_start_transaction(root, 1);
1586 btrfs_set_trans_block_group(trans, inode);
1587 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1588 err = btrfs_drop_extents(trans, root, inode,
1589 hole_start, block_end, hole_start,
1592 if (alloc_hint != EXTENT_MAP_INLINE) {
1593 err = btrfs_insert_file_extent(trans, root,
1597 btrfs_drop_extent_cache(inode, hole_start,
1599 btrfs_check_file(root, inode);
1601 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1602 btrfs_end_transaction(trans, root);
1603 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1608 err = inode_setattr(inode, attr);
1610 if (!err && ((attr->ia_valid & ATTR_MODE)))
1611 err = btrfs_acl_chmod(inode);
1616 void btrfs_delete_inode(struct inode *inode)
1618 struct btrfs_trans_handle *trans;
1619 struct btrfs_root *root = BTRFS_I(inode)->root;
1623 truncate_inode_pages(&inode->i_data, 0);
1624 if (is_bad_inode(inode)) {
1625 btrfs_orphan_del(NULL, inode);
1628 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1630 btrfs_i_size_write(inode, 0);
1631 trans = btrfs_start_transaction(root, 1);
1633 btrfs_set_trans_block_group(trans, inode);
1634 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1636 btrfs_orphan_del(NULL, inode);
1637 goto no_delete_lock;
1640 btrfs_orphan_del(trans, inode);
1642 nr = trans->blocks_used;
1645 btrfs_end_transaction(trans, root);
1646 btrfs_btree_balance_dirty(root, nr);
1650 nr = trans->blocks_used;
1651 btrfs_end_transaction(trans, root);
1652 btrfs_btree_balance_dirty(root, nr);
1658 * this returns the key found in the dir entry in the location pointer.
1659 * If no dir entries were found, location->objectid is 0.
1661 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1662 struct btrfs_key *location)
1664 const char *name = dentry->d_name.name;
1665 int namelen = dentry->d_name.len;
1666 struct btrfs_dir_item *di;
1667 struct btrfs_path *path;
1668 struct btrfs_root *root = BTRFS_I(dir)->root;
1671 if (namelen == 1 && strcmp(name, ".") == 0) {
1672 location->objectid = dir->i_ino;
1673 location->type = BTRFS_INODE_ITEM_KEY;
1674 location->offset = 0;
1677 path = btrfs_alloc_path();
1680 if (namelen == 2 && strcmp(name, "..") == 0) {
1681 struct btrfs_key key;
1682 struct extent_buffer *leaf;
1685 key.objectid = dir->i_ino;
1686 key.offset = (u64)-1;
1687 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1688 if (ret < 0 || path->slots[0] == 0)
1690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1693 leaf = path->nodes[0];
1694 slot = path->slots[0] - 1;
1696 btrfs_item_key_to_cpu(leaf, &key, slot);
1697 if (key.objectid != dir->i_ino ||
1698 key.type != BTRFS_INODE_REF_KEY) {
1701 location->objectid = key.offset;
1702 location->type = BTRFS_INODE_ITEM_KEY;
1703 location->offset = 0;
1707 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1711 if (!di || IS_ERR(di)) {
1714 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1716 btrfs_free_path(path);
1719 location->objectid = 0;
1724 * when we hit a tree root in a directory, the btrfs part of the inode
1725 * needs to be changed to reflect the root directory of the tree root. This
1726 * is kind of like crossing a mount point.
1728 static int fixup_tree_root_location(struct btrfs_root *root,
1729 struct btrfs_key *location,
1730 struct btrfs_root **sub_root,
1731 struct dentry *dentry)
1733 struct btrfs_root_item *ri;
1735 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1737 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1740 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1741 dentry->d_name.name,
1742 dentry->d_name.len);
1743 if (IS_ERR(*sub_root))
1744 return PTR_ERR(*sub_root);
1746 ri = &(*sub_root)->root_item;
1747 location->objectid = btrfs_root_dirid(ri);
1748 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1749 location->offset = 0;
1754 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1756 struct btrfs_iget_args *args = p;
1757 inode->i_ino = args->ino;
1758 BTRFS_I(inode)->root = args->root;
1759 BTRFS_I(inode)->delalloc_bytes = 0;
1760 BTRFS_I(inode)->disk_i_size = 0;
1761 BTRFS_I(inode)->index_cnt = (u64)-1;
1762 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1763 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1764 inode->i_mapping, GFP_NOFS);
1765 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1766 inode->i_mapping, GFP_NOFS);
1767 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1768 mutex_init(&BTRFS_I(inode)->csum_mutex);
1769 mutex_init(&BTRFS_I(inode)->extent_mutex);
1773 static int btrfs_find_actor(struct inode *inode, void *opaque)
1775 struct btrfs_iget_args *args = opaque;
1776 return (args->ino == inode->i_ino &&
1777 args->root == BTRFS_I(inode)->root);
1780 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1783 struct btrfs_iget_args args;
1784 args.ino = objectid;
1785 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1790 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1793 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1794 struct btrfs_root *root)
1796 struct inode *inode;
1797 struct btrfs_iget_args args;
1798 args.ino = objectid;
1801 inode = iget5_locked(s, objectid, btrfs_find_actor,
1802 btrfs_init_locked_inode,
1807 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1808 struct nameidata *nd)
1810 struct inode * inode;
1811 struct btrfs_inode *bi = BTRFS_I(dir);
1812 struct btrfs_root *root = bi->root;
1813 struct btrfs_root *sub_root = root;
1814 struct btrfs_key location;
1815 int ret, do_orphan = 0;
1817 if (dentry->d_name.len > BTRFS_NAME_LEN)
1818 return ERR_PTR(-ENAMETOOLONG);
1820 ret = btrfs_inode_by_name(dir, dentry, &location);
1823 return ERR_PTR(ret);
1826 if (location.objectid) {
1827 ret = fixup_tree_root_location(root, &location, &sub_root,
1830 return ERR_PTR(ret);
1832 return ERR_PTR(-ENOENT);
1834 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1837 return ERR_PTR(-EACCES);
1838 if (inode->i_state & I_NEW) {
1839 /* the inode and parent dir are two different roots */
1840 if (sub_root != root) {
1842 sub_root->inode = inode;
1845 BTRFS_I(inode)->root = sub_root;
1846 memcpy(&BTRFS_I(inode)->location, &location,
1848 btrfs_read_locked_inode(inode);
1849 unlock_new_inode(inode);
1853 if (unlikely(do_orphan))
1854 btrfs_orphan_cleanup(sub_root);
1856 return d_splice_alias(inode, dentry);
1859 static unsigned char btrfs_filetype_table[] = {
1860 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1863 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1865 struct inode *inode = filp->f_dentry->d_inode;
1866 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 struct btrfs_item *item;
1868 struct btrfs_dir_item *di;
1869 struct btrfs_key key;
1870 struct btrfs_key found_key;
1871 struct btrfs_path *path;
1874 struct extent_buffer *leaf;
1877 unsigned char d_type;
1882 int key_type = BTRFS_DIR_INDEX_KEY;
1887 /* FIXME, use a real flag for deciding about the key type */
1888 if (root->fs_info->tree_root == root)
1889 key_type = BTRFS_DIR_ITEM_KEY;
1891 /* special case for "." */
1892 if (filp->f_pos == 0) {
1893 over = filldir(dirent, ".", 1,
1901 key.objectid = inode->i_ino;
1902 path = btrfs_alloc_path();
1905 /* special case for .., just use the back ref */
1906 if (filp->f_pos == 1) {
1907 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1908 key.offset = (u64)-1;
1909 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1910 if (ret < 0 || path->slots[0] == 0) {
1911 btrfs_release_path(root, path);
1912 goto read_dir_items;
1915 leaf = path->nodes[0];
1916 slot = path->slots[0] - 1;
1917 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1918 btrfs_release_path(root, path);
1919 if (found_key.objectid != key.objectid ||
1920 found_key.type != BTRFS_INODE_REF_KEY)
1921 goto read_dir_items;
1922 over = filldir(dirent, "..", 2,
1923 2, found_key.offset, DT_DIR);
1930 btrfs_set_key_type(&key, key_type);
1931 key.offset = filp->f_pos;
1933 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1938 leaf = path->nodes[0];
1939 nritems = btrfs_header_nritems(leaf);
1940 slot = path->slots[0];
1941 if (advance || slot >= nritems) {
1942 if (slot >= nritems -1) {
1943 ret = btrfs_next_leaf(root, path);
1946 leaf = path->nodes[0];
1947 nritems = btrfs_header_nritems(leaf);
1948 slot = path->slots[0];
1955 item = btrfs_item_nr(leaf, slot);
1956 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1958 if (found_key.objectid != key.objectid)
1960 if (btrfs_key_type(&found_key) != key_type)
1962 if (found_key.offset < filp->f_pos)
1965 filp->f_pos = found_key.offset;
1967 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1969 di_total = btrfs_item_size(leaf, item);
1970 while(di_cur < di_total) {
1971 struct btrfs_key location;
1973 name_len = btrfs_dir_name_len(leaf, di);
1974 if (name_len < 32) {
1975 name_ptr = tmp_name;
1977 name_ptr = kmalloc(name_len, GFP_NOFS);
1980 read_extent_buffer(leaf, name_ptr,
1981 (unsigned long)(di + 1), name_len);
1983 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1984 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1985 over = filldir(dirent, name_ptr, name_len,
1990 if (name_ptr != tmp_name)
1995 di_len = btrfs_dir_name_len(leaf, di) +
1996 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1998 di = (struct btrfs_dir_item *)((char *)di + di_len);
2001 if (key_type == BTRFS_DIR_INDEX_KEY)
2002 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2008 btrfs_free_path(path);
2012 int btrfs_write_inode(struct inode *inode, int wait)
2014 struct btrfs_root *root = BTRFS_I(inode)->root;
2015 struct btrfs_trans_handle *trans;
2019 trans = btrfs_join_transaction(root, 1);
2020 btrfs_set_trans_block_group(trans, inode);
2021 ret = btrfs_commit_transaction(trans, root);
2027 * This is somewhat expensive, updating the tree every time the
2028 * inode changes. But, it is most likely to find the inode in cache.
2029 * FIXME, needs more benchmarking...there are no reasons other than performance
2030 * to keep or drop this code.
2032 void btrfs_dirty_inode(struct inode *inode)
2034 struct btrfs_root *root = BTRFS_I(inode)->root;
2035 struct btrfs_trans_handle *trans;
2037 trans = btrfs_join_transaction(root, 1);
2038 btrfs_set_trans_block_group(trans, inode);
2039 btrfs_update_inode(trans, root, inode);
2040 btrfs_end_transaction(trans, root);
2043 static int btrfs_set_inode_index_count(struct inode *inode)
2045 struct btrfs_root *root = BTRFS_I(inode)->root;
2046 struct btrfs_key key, found_key;
2047 struct btrfs_path *path;
2048 struct extent_buffer *leaf;
2051 key.objectid = inode->i_ino;
2052 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2053 key.offset = (u64)-1;
2055 path = btrfs_alloc_path();
2059 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2062 /* FIXME: we should be able to handle this */
2068 * MAGIC NUMBER EXPLANATION:
2069 * since we search a directory based on f_pos we have to start at 2
2070 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2071 * else has to start at 2
2073 if (path->slots[0] == 0) {
2074 BTRFS_I(inode)->index_cnt = 2;
2080 leaf = path->nodes[0];
2081 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2083 if (found_key.objectid != inode->i_ino ||
2084 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2085 BTRFS_I(inode)->index_cnt = 2;
2089 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2091 btrfs_free_path(path);
2095 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2099 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2100 ret = btrfs_set_inode_index_count(dir);
2105 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2106 BTRFS_I(dir)->index_cnt++;
2111 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2112 struct btrfs_root *root,
2114 const char *name, int name_len,
2117 struct btrfs_block_group_cache *group,
2120 struct inode *inode;
2121 struct btrfs_inode_item *inode_item;
2122 struct btrfs_block_group_cache *new_inode_group;
2123 struct btrfs_key *location;
2124 struct btrfs_path *path;
2125 struct btrfs_inode_ref *ref;
2126 struct btrfs_key key[2];
2132 path = btrfs_alloc_path();
2135 inode = new_inode(root->fs_info->sb);
2137 return ERR_PTR(-ENOMEM);
2140 ret = btrfs_set_inode_index(dir, inode);
2142 return ERR_PTR(ret);
2144 BTRFS_I(inode)->index = 0;
2147 * index_cnt is ignored for everything but a dir,
2148 * btrfs_get_inode_index_count has an explanation for the magic
2151 BTRFS_I(inode)->index_cnt = 2;
2153 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2154 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2155 inode->i_mapping, GFP_NOFS);
2156 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2157 inode->i_mapping, GFP_NOFS);
2158 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2159 mutex_init(&BTRFS_I(inode)->csum_mutex);
2160 mutex_init(&BTRFS_I(inode)->extent_mutex);
2161 BTRFS_I(inode)->delalloc_bytes = 0;
2162 BTRFS_I(inode)->disk_i_size = 0;
2163 BTRFS_I(inode)->root = root;
2169 new_inode_group = btrfs_find_block_group(root, group, 0,
2170 BTRFS_BLOCK_GROUP_METADATA, owner);
2171 if (!new_inode_group) {
2172 printk("find_block group failed\n");
2173 new_inode_group = group;
2175 BTRFS_I(inode)->block_group = new_inode_group;
2176 BTRFS_I(inode)->flags = 0;
2178 key[0].objectid = objectid;
2179 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2182 key[1].objectid = objectid;
2183 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2184 key[1].offset = ref_objectid;
2186 sizes[0] = sizeof(struct btrfs_inode_item);
2187 sizes[1] = name_len + sizeof(*ref);
2189 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2193 if (objectid > root->highest_inode)
2194 root->highest_inode = objectid;
2196 inode->i_uid = current->fsuid;
2197 inode->i_gid = current->fsgid;
2198 inode->i_mode = mode;
2199 inode->i_ino = objectid;
2200 inode->i_blocks = 0;
2201 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2202 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2203 struct btrfs_inode_item);
2204 fill_inode_item(path->nodes[0], inode_item, inode);
2206 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2207 struct btrfs_inode_ref);
2208 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2209 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
2210 ptr = (unsigned long)(ref + 1);
2211 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2213 btrfs_mark_buffer_dirty(path->nodes[0]);
2214 btrfs_free_path(path);
2216 location = &BTRFS_I(inode)->location;
2217 location->objectid = objectid;
2218 location->offset = 0;
2219 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2221 insert_inode_hash(inode);
2225 BTRFS_I(dir)->index_cnt--;
2226 btrfs_free_path(path);
2227 return ERR_PTR(ret);
2230 static inline u8 btrfs_inode_type(struct inode *inode)
2232 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2235 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2236 struct dentry *dentry, struct inode *inode,
2240 struct btrfs_key key;
2241 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2242 struct inode *parent_inode = dentry->d_parent->d_inode;
2244 key.objectid = inode->i_ino;
2245 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2248 ret = btrfs_insert_dir_item(trans, root,
2249 dentry->d_name.name, dentry->d_name.len,
2250 dentry->d_parent->d_inode->i_ino,
2251 &key, btrfs_inode_type(inode),
2252 BTRFS_I(inode)->index);
2255 ret = btrfs_insert_inode_ref(trans, root,
2256 dentry->d_name.name,
2259 parent_inode->i_ino,
2260 BTRFS_I(inode)->index);
2262 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2263 dentry->d_name.len * 2);
2264 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2265 ret = btrfs_update_inode(trans, root,
2266 dentry->d_parent->d_inode);
2271 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2272 struct dentry *dentry, struct inode *inode,
2275 int err = btrfs_add_link(trans, dentry, inode, backref);
2277 d_instantiate(dentry, inode);
2285 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2286 int mode, dev_t rdev)
2288 struct btrfs_trans_handle *trans;
2289 struct btrfs_root *root = BTRFS_I(dir)->root;
2290 struct inode *inode = NULL;
2294 unsigned long nr = 0;
2296 if (!new_valid_dev(rdev))
2299 err = btrfs_check_free_space(root, 1, 0);
2303 trans = btrfs_start_transaction(root, 1);
2304 btrfs_set_trans_block_group(trans, dir);
2306 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2312 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2314 dentry->d_parent->d_inode->i_ino, objectid,
2315 BTRFS_I(dir)->block_group, mode);
2316 err = PTR_ERR(inode);
2320 err = btrfs_init_acl(inode, dir);
2326 btrfs_set_trans_block_group(trans, inode);
2327 err = btrfs_add_nondir(trans, dentry, inode, 0);
2331 inode->i_op = &btrfs_special_inode_operations;
2332 init_special_inode(inode, inode->i_mode, rdev);
2333 btrfs_update_inode(trans, root, inode);
2335 dir->i_sb->s_dirt = 1;
2336 btrfs_update_inode_block_group(trans, inode);
2337 btrfs_update_inode_block_group(trans, dir);
2339 nr = trans->blocks_used;
2340 btrfs_end_transaction_throttle(trans, root);
2343 inode_dec_link_count(inode);
2346 btrfs_btree_balance_dirty(root, nr);
2350 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2351 int mode, struct nameidata *nd)
2353 struct btrfs_trans_handle *trans;
2354 struct btrfs_root *root = BTRFS_I(dir)->root;
2355 struct inode *inode = NULL;
2358 unsigned long nr = 0;
2361 err = btrfs_check_free_space(root, 1, 0);
2364 trans = btrfs_start_transaction(root, 1);
2365 btrfs_set_trans_block_group(trans, dir);
2367 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2373 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2375 dentry->d_parent->d_inode->i_ino,
2376 objectid, BTRFS_I(dir)->block_group, mode);
2377 err = PTR_ERR(inode);
2381 err = btrfs_init_acl(inode, dir);
2387 btrfs_set_trans_block_group(trans, inode);
2388 err = btrfs_add_nondir(trans, dentry, inode, 0);
2392 inode->i_mapping->a_ops = &btrfs_aops;
2393 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2394 inode->i_fop = &btrfs_file_operations;
2395 inode->i_op = &btrfs_file_inode_operations;
2396 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2397 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2398 inode->i_mapping, GFP_NOFS);
2399 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2400 inode->i_mapping, GFP_NOFS);
2401 mutex_init(&BTRFS_I(inode)->csum_mutex);
2402 mutex_init(&BTRFS_I(inode)->extent_mutex);
2403 BTRFS_I(inode)->delalloc_bytes = 0;
2404 BTRFS_I(inode)->disk_i_size = 0;
2405 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2406 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2408 dir->i_sb->s_dirt = 1;
2409 btrfs_update_inode_block_group(trans, inode);
2410 btrfs_update_inode_block_group(trans, dir);
2412 nr = trans->blocks_used;
2413 btrfs_end_transaction_throttle(trans, root);
2416 inode_dec_link_count(inode);
2419 btrfs_btree_balance_dirty(root, nr);
2423 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2424 struct dentry *dentry)
2426 struct btrfs_trans_handle *trans;
2427 struct btrfs_root *root = BTRFS_I(dir)->root;
2428 struct inode *inode = old_dentry->d_inode;
2429 unsigned long nr = 0;
2433 if (inode->i_nlink == 0)
2436 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2441 err = btrfs_check_free_space(root, 1, 0);
2444 err = btrfs_set_inode_index(dir, inode);
2448 trans = btrfs_start_transaction(root, 1);
2450 btrfs_set_trans_block_group(trans, dir);
2451 atomic_inc(&inode->i_count);
2453 err = btrfs_add_nondir(trans, dentry, inode, 1);
2458 dir->i_sb->s_dirt = 1;
2459 btrfs_update_inode_block_group(trans, dir);
2460 err = btrfs_update_inode(trans, root, inode);
2465 nr = trans->blocks_used;
2466 btrfs_end_transaction_throttle(trans, root);
2469 inode_dec_link_count(inode);
2472 btrfs_btree_balance_dirty(root, nr);
2476 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2478 struct inode *inode = NULL;
2479 struct btrfs_trans_handle *trans;
2480 struct btrfs_root *root = BTRFS_I(dir)->root;
2482 int drop_on_err = 0;
2484 unsigned long nr = 1;
2486 err = btrfs_check_free_space(root, 1, 0);
2490 trans = btrfs_start_transaction(root, 1);
2491 btrfs_set_trans_block_group(trans, dir);
2493 if (IS_ERR(trans)) {
2494 err = PTR_ERR(trans);
2498 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2504 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2506 dentry->d_parent->d_inode->i_ino, objectid,
2507 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2508 if (IS_ERR(inode)) {
2509 err = PTR_ERR(inode);
2515 err = btrfs_init_acl(inode, dir);
2519 inode->i_op = &btrfs_dir_inode_operations;
2520 inode->i_fop = &btrfs_dir_file_operations;
2521 btrfs_set_trans_block_group(trans, inode);
2523 btrfs_i_size_write(inode, 0);
2524 err = btrfs_update_inode(trans, root, inode);
2528 err = btrfs_add_link(trans, dentry, inode, 0);
2532 d_instantiate(dentry, inode);
2534 dir->i_sb->s_dirt = 1;
2535 btrfs_update_inode_block_group(trans, inode);
2536 btrfs_update_inode_block_group(trans, dir);
2539 nr = trans->blocks_used;
2540 btrfs_end_transaction_throttle(trans, root);
2545 btrfs_btree_balance_dirty(root, nr);
2549 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2550 struct extent_map *existing,
2551 struct extent_map *em,
2552 u64 map_start, u64 map_len)
2556 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2557 start_diff = map_start - em->start;
2558 em->start = map_start;
2560 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2561 em->block_start += start_diff;
2562 return add_extent_mapping(em_tree, em);
2565 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2566 size_t pg_offset, u64 start, u64 len,
2572 u64 extent_start = 0;
2574 u64 objectid = inode->i_ino;
2576 struct btrfs_path *path = NULL;
2577 struct btrfs_root *root = BTRFS_I(inode)->root;
2578 struct btrfs_file_extent_item *item;
2579 struct extent_buffer *leaf;
2580 struct btrfs_key found_key;
2581 struct extent_map *em = NULL;
2582 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2584 struct btrfs_trans_handle *trans = NULL;
2587 spin_lock(&em_tree->lock);
2588 em = lookup_extent_mapping(em_tree, start, len);
2590 em->bdev = root->fs_info->fs_devices->latest_bdev;
2591 spin_unlock(&em_tree->lock);
2594 if (em->start > start || em->start + em->len <= start)
2595 free_extent_map(em);
2596 else if (em->block_start == EXTENT_MAP_INLINE && page)
2597 free_extent_map(em);
2601 em = alloc_extent_map(GFP_NOFS);
2606 em->bdev = root->fs_info->fs_devices->latest_bdev;
2607 em->start = EXTENT_MAP_HOLE;
2611 path = btrfs_alloc_path();
2615 ret = btrfs_lookup_file_extent(trans, root, path,
2616 objectid, start, trans != NULL);
2623 if (path->slots[0] == 0)
2628 leaf = path->nodes[0];
2629 item = btrfs_item_ptr(leaf, path->slots[0],
2630 struct btrfs_file_extent_item);
2631 /* are we inside the extent that was found? */
2632 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2633 found_type = btrfs_key_type(&found_key);
2634 if (found_key.objectid != objectid ||
2635 found_type != BTRFS_EXTENT_DATA_KEY) {
2639 found_type = btrfs_file_extent_type(leaf, item);
2640 extent_start = found_key.offset;
2641 if (found_type == BTRFS_FILE_EXTENT_REG) {
2642 extent_end = extent_start +
2643 btrfs_file_extent_num_bytes(leaf, item);
2645 if (start < extent_start || start >= extent_end) {
2647 if (start < extent_start) {
2648 if (start + len <= extent_start)
2650 em->len = extent_end - extent_start;
2656 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2658 em->start = extent_start;
2659 em->len = extent_end - extent_start;
2660 em->block_start = EXTENT_MAP_HOLE;
2663 bytenr += btrfs_file_extent_offset(leaf, item);
2664 em->block_start = bytenr;
2665 em->start = extent_start;
2666 em->len = extent_end - extent_start;
2668 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2673 size_t extent_offset;
2676 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2678 extent_end = (extent_start + size + root->sectorsize - 1) &
2679 ~((u64)root->sectorsize - 1);
2680 if (start < extent_start || start >= extent_end) {
2682 if (start < extent_start) {
2683 if (start + len <= extent_start)
2685 em->len = extent_end - extent_start;
2691 em->block_start = EXTENT_MAP_INLINE;
2694 em->start = extent_start;
2699 page_start = page_offset(page) + pg_offset;
2700 extent_offset = page_start - extent_start;
2701 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2702 size - extent_offset);
2703 em->start = extent_start + extent_offset;
2704 em->len = (copy_size + root->sectorsize - 1) &
2705 ~((u64)root->sectorsize - 1);
2707 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2708 if (create == 0 && !PageUptodate(page)) {
2709 read_extent_buffer(leaf, map + pg_offset, ptr,
2711 flush_dcache_page(page);
2712 } else if (create && PageUptodate(page)) {
2715 free_extent_map(em);
2717 btrfs_release_path(root, path);
2718 trans = btrfs_join_transaction(root, 1);
2721 write_extent_buffer(leaf, map + pg_offset, ptr,
2723 btrfs_mark_buffer_dirty(leaf);
2726 set_extent_uptodate(io_tree, em->start,
2727 extent_map_end(em) - 1, GFP_NOFS);
2730 printk("unkknown found_type %d\n", found_type);
2737 em->block_start = EXTENT_MAP_HOLE;
2739 btrfs_release_path(root, path);
2740 if (em->start > start || extent_map_end(em) <= start) {
2741 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2747 spin_lock(&em_tree->lock);
2748 ret = add_extent_mapping(em_tree, em);
2749 /* it is possible that someone inserted the extent into the tree
2750 * while we had the lock dropped. It is also possible that
2751 * an overlapping map exists in the tree
2753 if (ret == -EEXIST) {
2754 struct extent_map *existing;
2758 existing = lookup_extent_mapping(em_tree, start, len);
2759 if (existing && (existing->start > start ||
2760 existing->start + existing->len <= start)) {
2761 free_extent_map(existing);
2765 existing = lookup_extent_mapping(em_tree, em->start,
2768 err = merge_extent_mapping(em_tree, existing,
2771 free_extent_map(existing);
2773 free_extent_map(em);
2778 printk("failing to insert %Lu %Lu\n",
2780 free_extent_map(em);
2784 free_extent_map(em);
2789 spin_unlock(&em_tree->lock);
2792 btrfs_free_path(path);
2794 ret = btrfs_end_transaction(trans, root);
2800 free_extent_map(em);
2802 return ERR_PTR(err);
2807 #if 0 /* waiting for O_DIRECT reads */
2808 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2809 struct buffer_head *bh_result, int create)
2811 struct extent_map *em;
2812 u64 start = (u64)iblock << inode->i_blkbits;
2813 struct btrfs_multi_bio *multi = NULL;
2814 struct btrfs_root *root = BTRFS_I(inode)->root;
2820 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2822 if (!em || IS_ERR(em))
2825 if (em->start > start || em->start + em->len <= start) {
2829 if (em->block_start == EXTENT_MAP_INLINE) {
2834 len = em->start + em->len - start;
2835 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2837 if (em->block_start == EXTENT_MAP_HOLE ||
2838 em->block_start == EXTENT_MAP_DELALLOC) {
2839 bh_result->b_size = len;
2843 logical = start - em->start;
2844 logical = em->block_start + logical;
2847 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2848 logical, &map_length, &multi, 0);
2850 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2851 bh_result->b_size = min(map_length, len);
2853 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2854 set_buffer_mapped(bh_result);
2857 free_extent_map(em);
2862 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2863 const struct iovec *iov, loff_t offset,
2864 unsigned long nr_segs)
2868 struct file *file = iocb->ki_filp;
2869 struct inode *inode = file->f_mapping->host;
2874 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2875 offset, nr_segs, btrfs_get_block, NULL);
2879 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2881 return extent_bmap(mapping, iblock, btrfs_get_extent);
2884 int btrfs_readpage(struct file *file, struct page *page)
2886 struct extent_io_tree *tree;
2887 tree = &BTRFS_I(page->mapping->host)->io_tree;
2888 return extent_read_full_page(tree, page, btrfs_get_extent);
2891 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2893 struct extent_io_tree *tree;
2896 if (current->flags & PF_MEMALLOC) {
2897 redirty_page_for_writepage(wbc, page);
2901 tree = &BTRFS_I(page->mapping->host)->io_tree;
2902 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2905 int btrfs_writepages(struct address_space *mapping,
2906 struct writeback_control *wbc)
2908 struct extent_io_tree *tree;
2909 tree = &BTRFS_I(mapping->host)->io_tree;
2910 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2914 btrfs_readpages(struct file *file, struct address_space *mapping,
2915 struct list_head *pages, unsigned nr_pages)
2917 struct extent_io_tree *tree;
2918 tree = &BTRFS_I(mapping->host)->io_tree;
2919 return extent_readpages(tree, mapping, pages, nr_pages,
2922 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2924 struct extent_io_tree *tree;
2925 struct extent_map_tree *map;
2928 tree = &BTRFS_I(page->mapping->host)->io_tree;
2929 map = &BTRFS_I(page->mapping->host)->extent_tree;
2930 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2932 ClearPagePrivate(page);
2933 set_page_private(page, 0);
2934 page_cache_release(page);
2939 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2941 return __btrfs_releasepage(page, gfp_flags);
2944 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2946 struct extent_io_tree *tree;
2947 struct btrfs_ordered_extent *ordered;
2948 u64 page_start = page_offset(page);
2949 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2951 wait_on_page_writeback(page);
2952 tree = &BTRFS_I(page->mapping->host)->io_tree;
2954 btrfs_releasepage(page, GFP_NOFS);
2958 lock_extent(tree, page_start, page_end, GFP_NOFS);
2959 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2963 * IO on this page will never be started, so we need
2964 * to account for any ordered extents now
2966 clear_extent_bit(tree, page_start, page_end,
2967 EXTENT_DIRTY | EXTENT_DELALLOC |
2968 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2969 btrfs_finish_ordered_io(page->mapping->host,
2970 page_start, page_end);
2971 btrfs_put_ordered_extent(ordered);
2972 lock_extent(tree, page_start, page_end, GFP_NOFS);
2974 clear_extent_bit(tree, page_start, page_end,
2975 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2978 __btrfs_releasepage(page, GFP_NOFS);
2980 ClearPageChecked(page);
2981 if (PagePrivate(page)) {
2982 ClearPagePrivate(page);
2983 set_page_private(page, 0);
2984 page_cache_release(page);
2989 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2990 * called from a page fault handler when a page is first dirtied. Hence we must
2991 * be careful to check for EOF conditions here. We set the page up correctly
2992 * for a written page which means we get ENOSPC checking when writing into
2993 * holes and correct delalloc and unwritten extent mapping on filesystems that
2994 * support these features.
2996 * We are not allowed to take the i_mutex here so we have to play games to
2997 * protect against truncate races as the page could now be beyond EOF. Because
2998 * vmtruncate() writes the inode size before removing pages, once we have the
2999 * page lock we can determine safely if the page is beyond EOF. If it is not
3000 * beyond EOF, then the page is guaranteed safe against truncation until we
3003 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3005 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3006 struct btrfs_root *root = BTRFS_I(inode)->root;
3007 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3008 struct btrfs_ordered_extent *ordered;
3010 unsigned long zero_start;
3016 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3023 size = i_size_read(inode);
3024 page_start = page_offset(page);
3025 page_end = page_start + PAGE_CACHE_SIZE - 1;
3027 if ((page->mapping != inode->i_mapping) ||
3028 (page_start >= size)) {
3029 /* page got truncated out from underneath us */
3032 wait_on_page_writeback(page);
3034 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3035 set_page_extent_mapped(page);
3038 * we can't set the delalloc bits if there are pending ordered
3039 * extents. Drop our locks and wait for them to finish
3041 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3045 btrfs_start_ordered_extent(inode, ordered, 1);
3046 btrfs_put_ordered_extent(ordered);
3050 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3051 page_end, GFP_NOFS);
3054 /* page is wholly or partially inside EOF */
3055 if (page_start + PAGE_CACHE_SIZE > size)
3056 zero_start = size & ~PAGE_CACHE_MASK;
3058 zero_start = PAGE_CACHE_SIZE;
3060 if (zero_start != PAGE_CACHE_SIZE) {
3062 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3063 flush_dcache_page(page);
3066 ClearPageChecked(page);
3067 set_page_dirty(page);
3068 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3076 static void btrfs_truncate(struct inode *inode)
3078 struct btrfs_root *root = BTRFS_I(inode)->root;
3080 struct btrfs_trans_handle *trans;
3082 u64 mask = root->sectorsize - 1;
3084 if (!S_ISREG(inode->i_mode))
3086 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3089 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3090 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3092 trans = btrfs_start_transaction(root, 1);
3093 btrfs_set_trans_block_group(trans, inode);
3094 btrfs_i_size_write(inode, inode->i_size);
3096 ret = btrfs_orphan_add(trans, inode);
3099 /* FIXME, add redo link to tree so we don't leak on crash */
3100 ret = btrfs_truncate_in_trans(trans, root, inode,
3101 BTRFS_EXTENT_DATA_KEY);
3102 btrfs_update_inode(trans, root, inode);
3104 ret = btrfs_orphan_del(trans, inode);
3108 nr = trans->blocks_used;
3109 ret = btrfs_end_transaction_throttle(trans, root);
3111 btrfs_btree_balance_dirty(root, nr);
3115 * Invalidate a single dcache entry at the root of the filesystem.
3116 * Needed after creation of snapshot or subvolume.
3118 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3121 struct dentry *alias, *entry;
3124 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3128 /* change me if btrfs ever gets a d_hash operation */
3129 qstr.hash = full_name_hash(qstr.name, qstr.len);
3130 entry = d_lookup(alias, &qstr);
3133 d_invalidate(entry);
3139 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3140 struct btrfs_trans_handle *trans, u64 new_dirid,
3141 struct btrfs_block_group_cache *block_group)
3143 struct inode *inode;
3145 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3146 new_dirid, block_group, S_IFDIR | 0700);
3148 return PTR_ERR(inode);
3149 inode->i_op = &btrfs_dir_inode_operations;
3150 inode->i_fop = &btrfs_dir_file_operations;
3151 new_root->inode = inode;
3154 btrfs_i_size_write(inode, 0);
3156 return btrfs_update_inode(trans, new_root, inode);
3159 unsigned long btrfs_force_ra(struct address_space *mapping,
3160 struct file_ra_state *ra, struct file *file,
3161 pgoff_t offset, pgoff_t last_index)
3163 pgoff_t req_size = last_index - offset + 1;
3165 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3166 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3169 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3170 return offset + req_size;
3174 struct inode *btrfs_alloc_inode(struct super_block *sb)
3176 struct btrfs_inode *ei;
3178 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3182 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3183 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3184 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3185 INIT_LIST_HEAD(&ei->i_orphan);
3186 return &ei->vfs_inode;
3189 void btrfs_destroy_inode(struct inode *inode)
3191 struct btrfs_ordered_extent *ordered;
3192 WARN_ON(!list_empty(&inode->i_dentry));
3193 WARN_ON(inode->i_data.nrpages);
3195 if (BTRFS_I(inode)->i_acl &&
3196 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3197 posix_acl_release(BTRFS_I(inode)->i_acl);
3198 if (BTRFS_I(inode)->i_default_acl &&
3199 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3200 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3202 spin_lock(&BTRFS_I(inode)->root->list_lock);
3203 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3204 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3205 " list\n", inode->i_ino);
3208 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3211 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3215 printk("found ordered extent %Lu %Lu\n",
3216 ordered->file_offset, ordered->len);
3217 btrfs_remove_ordered_extent(inode, ordered);
3218 btrfs_put_ordered_extent(ordered);
3219 btrfs_put_ordered_extent(ordered);
3222 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3223 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3226 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3227 static void init_once(void *foo)
3228 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3229 static void init_once(struct kmem_cache * cachep, void *foo)
3231 static void init_once(void * foo, struct kmem_cache * cachep,
3232 unsigned long flags)
3235 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3237 inode_init_once(&ei->vfs_inode);
3240 void btrfs_destroy_cachep(void)
3242 if (btrfs_inode_cachep)
3243 kmem_cache_destroy(btrfs_inode_cachep);
3244 if (btrfs_trans_handle_cachep)
3245 kmem_cache_destroy(btrfs_trans_handle_cachep);
3246 if (btrfs_transaction_cachep)
3247 kmem_cache_destroy(btrfs_transaction_cachep);
3248 if (btrfs_bit_radix_cachep)
3249 kmem_cache_destroy(btrfs_bit_radix_cachep);
3250 if (btrfs_path_cachep)
3251 kmem_cache_destroy(btrfs_path_cachep);
3254 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3255 unsigned long extra_flags,
3256 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3257 void (*ctor)(void *)
3258 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3259 void (*ctor)(struct kmem_cache *, void *)
3261 void (*ctor)(void *, struct kmem_cache *,
3266 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3267 SLAB_MEM_SPREAD | extra_flags), ctor
3268 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3274 int btrfs_init_cachep(void)
3276 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3277 sizeof(struct btrfs_inode),
3279 if (!btrfs_inode_cachep)
3281 btrfs_trans_handle_cachep =
3282 btrfs_cache_create("btrfs_trans_handle_cache",
3283 sizeof(struct btrfs_trans_handle),
3285 if (!btrfs_trans_handle_cachep)
3287 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3288 sizeof(struct btrfs_transaction),
3290 if (!btrfs_transaction_cachep)
3292 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3293 sizeof(struct btrfs_path),
3295 if (!btrfs_path_cachep)
3297 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3298 SLAB_DESTROY_BY_RCU, NULL);
3299 if (!btrfs_bit_radix_cachep)
3303 btrfs_destroy_cachep();
3307 static int btrfs_getattr(struct vfsmount *mnt,
3308 struct dentry *dentry, struct kstat *stat)
3310 struct inode *inode = dentry->d_inode;
3311 generic_fillattr(inode, stat);
3312 stat->blksize = PAGE_CACHE_SIZE;
3313 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3317 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3318 struct inode * new_dir,struct dentry *new_dentry)
3320 struct btrfs_trans_handle *trans;
3321 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3322 struct inode *new_inode = new_dentry->d_inode;
3323 struct inode *old_inode = old_dentry->d_inode;
3324 struct timespec ctime = CURRENT_TIME;
3327 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3328 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3332 ret = btrfs_check_free_space(root, 1, 0);
3336 trans = btrfs_start_transaction(root, 1);
3338 btrfs_set_trans_block_group(trans, new_dir);
3340 old_dentry->d_inode->i_nlink++;
3341 old_dir->i_ctime = old_dir->i_mtime = ctime;
3342 new_dir->i_ctime = new_dir->i_mtime = ctime;
3343 old_inode->i_ctime = ctime;
3345 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3350 new_inode->i_ctime = CURRENT_TIME;
3351 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3354 if (new_inode->i_nlink == 0) {
3355 ret = btrfs_orphan_add(trans, new_inode);
3360 ret = btrfs_set_inode_index(new_dir, old_inode);
3364 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3369 btrfs_end_transaction_throttle(trans, root);
3374 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3375 const char *symname)
3377 struct btrfs_trans_handle *trans;
3378 struct btrfs_root *root = BTRFS_I(dir)->root;
3379 struct btrfs_path *path;
3380 struct btrfs_key key;
3381 struct inode *inode = NULL;
3388 struct btrfs_file_extent_item *ei;
3389 struct extent_buffer *leaf;
3390 unsigned long nr = 0;
3392 name_len = strlen(symname) + 1;
3393 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3394 return -ENAMETOOLONG;
3396 err = btrfs_check_free_space(root, 1, 0);
3400 trans = btrfs_start_transaction(root, 1);
3401 btrfs_set_trans_block_group(trans, dir);
3403 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3409 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3411 dentry->d_parent->d_inode->i_ino, objectid,
3412 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3413 err = PTR_ERR(inode);
3417 err = btrfs_init_acl(inode, dir);
3423 btrfs_set_trans_block_group(trans, inode);
3424 err = btrfs_add_nondir(trans, dentry, inode, 0);
3428 inode->i_mapping->a_ops = &btrfs_aops;
3429 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3430 inode->i_fop = &btrfs_file_operations;
3431 inode->i_op = &btrfs_file_inode_operations;
3432 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3433 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3434 inode->i_mapping, GFP_NOFS);
3435 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3436 inode->i_mapping, GFP_NOFS);
3437 mutex_init(&BTRFS_I(inode)->csum_mutex);
3438 mutex_init(&BTRFS_I(inode)->extent_mutex);
3439 BTRFS_I(inode)->delalloc_bytes = 0;
3440 BTRFS_I(inode)->disk_i_size = 0;
3441 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3442 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3444 dir->i_sb->s_dirt = 1;
3445 btrfs_update_inode_block_group(trans, inode);
3446 btrfs_update_inode_block_group(trans, dir);
3450 path = btrfs_alloc_path();
3452 key.objectid = inode->i_ino;
3454 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3455 datasize = btrfs_file_extent_calc_inline_size(name_len);
3456 err = btrfs_insert_empty_item(trans, root, path, &key,
3462 leaf = path->nodes[0];
3463 ei = btrfs_item_ptr(leaf, path->slots[0],
3464 struct btrfs_file_extent_item);
3465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3466 btrfs_set_file_extent_type(leaf, ei,
3467 BTRFS_FILE_EXTENT_INLINE);
3468 ptr = btrfs_file_extent_inline_start(ei);
3469 write_extent_buffer(leaf, symname, ptr, name_len);
3470 btrfs_mark_buffer_dirty(leaf);
3471 btrfs_free_path(path);
3473 inode->i_op = &btrfs_symlink_inode_operations;
3474 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3475 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3476 btrfs_i_size_write(inode, name_len - 1);
3477 err = btrfs_update_inode(trans, root, inode);
3482 nr = trans->blocks_used;
3483 btrfs_end_transaction_throttle(trans, root);
3486 inode_dec_link_count(inode);
3489 btrfs_btree_balance_dirty(root, nr);
3493 static int btrfs_set_page_dirty(struct page *page)
3495 return __set_page_dirty_nobuffers(page);
3498 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3499 static int btrfs_permission(struct inode *inode, int mask)
3501 static int btrfs_permission(struct inode *inode, int mask,
3502 struct nameidata *nd)
3505 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3507 return generic_permission(inode, mask, btrfs_check_acl);
3510 static struct inode_operations btrfs_dir_inode_operations = {
3511 .lookup = btrfs_lookup,
3512 .create = btrfs_create,
3513 .unlink = btrfs_unlink,
3515 .mkdir = btrfs_mkdir,
3516 .rmdir = btrfs_rmdir,
3517 .rename = btrfs_rename,
3518 .symlink = btrfs_symlink,
3519 .setattr = btrfs_setattr,
3520 .mknod = btrfs_mknod,
3521 .setxattr = generic_setxattr,
3522 .getxattr = generic_getxattr,
3523 .listxattr = btrfs_listxattr,
3524 .removexattr = generic_removexattr,
3525 .permission = btrfs_permission,
3527 static struct inode_operations btrfs_dir_ro_inode_operations = {
3528 .lookup = btrfs_lookup,
3529 .permission = btrfs_permission,
3531 static struct file_operations btrfs_dir_file_operations = {
3532 .llseek = generic_file_llseek,
3533 .read = generic_read_dir,
3534 .readdir = btrfs_readdir,
3535 .unlocked_ioctl = btrfs_ioctl,
3536 #ifdef CONFIG_COMPAT
3537 .compat_ioctl = btrfs_ioctl,
3539 .release = btrfs_release_file,
3542 static struct extent_io_ops btrfs_extent_io_ops = {
3543 .fill_delalloc = run_delalloc_range,
3544 .submit_bio_hook = btrfs_submit_bio_hook,
3545 .merge_bio_hook = btrfs_merge_bio_hook,
3546 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3547 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3548 .writepage_start_hook = btrfs_writepage_start_hook,
3549 .readpage_io_failed_hook = btrfs_io_failed_hook,
3550 .set_bit_hook = btrfs_set_bit_hook,
3551 .clear_bit_hook = btrfs_clear_bit_hook,
3554 static struct address_space_operations btrfs_aops = {
3555 .readpage = btrfs_readpage,
3556 .writepage = btrfs_writepage,
3557 .writepages = btrfs_writepages,
3558 .readpages = btrfs_readpages,
3559 .sync_page = block_sync_page,
3561 .direct_IO = btrfs_direct_IO,
3562 .invalidatepage = btrfs_invalidatepage,
3563 .releasepage = btrfs_releasepage,
3564 .set_page_dirty = btrfs_set_page_dirty,
3567 static struct address_space_operations btrfs_symlink_aops = {
3568 .readpage = btrfs_readpage,
3569 .writepage = btrfs_writepage,
3570 .invalidatepage = btrfs_invalidatepage,
3571 .releasepage = btrfs_releasepage,
3574 static struct inode_operations btrfs_file_inode_operations = {
3575 .truncate = btrfs_truncate,
3576 .getattr = btrfs_getattr,
3577 .setattr = btrfs_setattr,
3578 .setxattr = generic_setxattr,
3579 .getxattr = generic_getxattr,
3580 .listxattr = btrfs_listxattr,
3581 .removexattr = generic_removexattr,
3582 .permission = btrfs_permission,
3584 static struct inode_operations btrfs_special_inode_operations = {
3585 .getattr = btrfs_getattr,
3586 .setattr = btrfs_setattr,
3587 .permission = btrfs_permission,
3588 .setxattr = generic_setxattr,
3589 .getxattr = generic_getxattr,
3590 .listxattr = btrfs_listxattr,
3591 .removexattr = generic_removexattr,
3593 static struct inode_operations btrfs_symlink_inode_operations = {
3594 .readlink = generic_readlink,
3595 .follow_link = page_follow_link_light,
3596 .put_link = page_put_link,
3597 .permission = btrfs_permission,