2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
46 #include "ordered-data.h"
48 struct btrfs_iget_args {
50 struct btrfs_root *root;
53 static struct inode_operations btrfs_dir_inode_operations;
54 static struct inode_operations btrfs_symlink_inode_operations;
55 static struct inode_operations btrfs_dir_ro_inode_operations;
56 static struct inode_operations btrfs_special_inode_operations;
57 static struct inode_operations btrfs_file_inode_operations;
58 static struct address_space_operations btrfs_aops;
59 static struct address_space_operations btrfs_symlink_aops;
60 static struct file_operations btrfs_dir_file_operations;
61 static struct extent_io_ops btrfs_extent_io_ops;
63 static struct kmem_cache *btrfs_inode_cachep;
64 struct kmem_cache *btrfs_trans_handle_cachep;
65 struct kmem_cache *btrfs_transaction_cachep;
66 struct kmem_cache *btrfs_bit_radix_cachep;
67 struct kmem_cache *btrfs_path_cachep;
70 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
80 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
89 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
99 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
105 static int cow_file_range(struct inode *inode, u64 start, u64 end)
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
112 u64 blocksize = root->sectorsize;
114 struct btrfs_key ins;
115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
119 trans = btrfs_join_transaction(root, 1);
121 btrfs_set_trans_block_group(trans, inode);
123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
124 num_bytes = max(blocksize, num_bytes);
125 orig_num_bytes = num_bytes;
127 if (alloc_hint == EXTENT_MAP_INLINE)
130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
131 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
133 while(num_bytes > 0) {
134 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
135 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
136 root->sectorsize, 0, 0,
142 em = alloc_extent_map(GFP_NOFS);
144 em->len = ins.offset;
145 em->block_start = ins.objectid;
146 em->bdev = root->fs_info->fs_devices->latest_bdev;
148 spin_lock(&em_tree->lock);
149 ret = add_extent_mapping(em_tree, em);
150 spin_unlock(&em_tree->lock);
151 if (ret != -EEXIST) {
155 btrfs_drop_extent_cache(inode, start,
156 start + ins.offset - 1);
159 cur_alloc_size = ins.offset;
160 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
163 if (num_bytes < cur_alloc_size) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
168 num_bytes -= cur_alloc_size;
169 alloc_hint = ins.objectid + ins.offset;
170 start += cur_alloc_size;
173 btrfs_end_transaction(trans, root);
177 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
185 struct btrfs_root *root = BTRFS_I(inode)->root;
186 struct btrfs_block_group_cache *block_group;
187 struct extent_buffer *leaf;
189 struct btrfs_path *path;
190 struct btrfs_file_extent_item *item;
193 struct btrfs_key found_key;
195 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
196 path = btrfs_alloc_path();
199 ret = btrfs_lookup_file_extent(NULL, root, path,
200 inode->i_ino, start, 0);
202 btrfs_free_path(path);
208 if (path->slots[0] == 0)
213 leaf = path->nodes[0];
214 item = btrfs_item_ptr(leaf, path->slots[0],
215 struct btrfs_file_extent_item);
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
219 found_type = btrfs_key_type(&found_key);
220 if (found_key.objectid != inode->i_ino ||
221 found_type != BTRFS_EXTENT_DATA_KEY)
224 found_type = btrfs_file_extent_type(leaf, item);
225 extent_start = found_key.offset;
226 if (found_type == BTRFS_FILE_EXTENT_REG) {
227 u64 extent_num_bytes;
229 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
230 extent_end = extent_start + extent_num_bytes;
233 if (loops && start != extent_start)
236 if (start < extent_start || start >= extent_end)
239 cow_end = min(end, extent_end - 1);
240 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
244 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
253 block_group = btrfs_lookup_block_group(root->fs_info,
255 if (!block_group || block_group->ro)
264 btrfs_free_path(path);
267 btrfs_release_path(root, path);
272 cow_file_range(inode, start, end);
277 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
279 struct btrfs_root *root = BTRFS_I(inode)->root;
282 if (btrfs_test_opt(root, NODATACOW) ||
283 btrfs_test_flag(inode, NODATACOW))
284 ret = run_delalloc_nocow(inode, start, end);
286 ret = cow_file_range(inode, start, end);
291 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
292 unsigned long old, unsigned long bits)
295 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
296 struct btrfs_root *root = BTRFS_I(inode)->root;
297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
298 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
299 root->fs_info->delalloc_bytes += end - start + 1;
300 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
305 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
306 unsigned long old, unsigned long bits)
308 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
309 struct btrfs_root *root = BTRFS_I(inode)->root;
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
313 if (end - start + 1 > root->fs_info->delalloc_bytes) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end - start + 1, root->fs_info->delalloc_bytes);
316 root->fs_info->delalloc_bytes = 0;
317 BTRFS_I(inode)->delalloc_bytes = 0;
319 root->fs_info->delalloc_bytes -= end - start + 1;
320 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
322 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
327 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
328 size_t size, struct bio *bio)
330 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
331 struct btrfs_mapping_tree *map_tree;
332 u64 logical = bio->bi_sector << 9;
337 length = bio->bi_size;
338 map_tree = &root->fs_info->mapping_tree;
340 ret = btrfs_map_block(map_tree, READ, logical,
341 &map_length, NULL, 0);
343 if (map_length < length + size) {
349 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
352 struct btrfs_root *root = BTRFS_I(inode)->root;
355 ret = btrfs_csum_one_bio(root, inode, bio);
358 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
361 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
364 struct btrfs_root *root = BTRFS_I(inode)->root;
367 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
370 if (!(rw & (1 << BIO_RW))) {
374 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
375 inode, rw, bio, mirror_num,
376 __btrfs_submit_bio_hook);
378 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
381 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
382 struct inode *inode, u64 file_offset,
383 struct list_head *list)
385 struct list_head *cur;
386 struct btrfs_ordered_sum *sum;
388 btrfs_set_trans_block_group(trans, inode);
389 list_for_each(cur, list) {
390 sum = list_entry(cur, struct btrfs_ordered_sum, list);
391 mutex_lock(&BTRFS_I(inode)->csum_mutex);
392 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
394 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
399 struct btrfs_writepage_fixup {
401 struct btrfs_work work;
404 /* see btrfs_writepage_start_hook for details on why this is required */
405 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
407 struct btrfs_writepage_fixup *fixup;
408 struct btrfs_ordered_extent *ordered;
414 fixup = container_of(work, struct btrfs_writepage_fixup, work);
418 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
419 ClearPageChecked(page);
423 inode = page->mapping->host;
424 page_start = page_offset(page);
425 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
427 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
428 ordered = btrfs_lookup_ordered_extent(inode, page_start);
432 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
434 ClearPageChecked(page);
436 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
439 page_cache_release(page);
443 * There are a few paths in the higher layers of the kernel that directly
444 * set the page dirty bit without asking the filesystem if it is a
445 * good idea. This causes problems because we want to make sure COW
446 * properly happens and the data=ordered rules are followed.
448 * In our case any range that doesn't have the EXTENT_ORDERED bit set
449 * hasn't been properly setup for IO. We kick off an async process
450 * to fix it up. The async helper will wait for ordered extents, set
451 * the delalloc bit and make it safe to write the page.
453 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
455 struct inode *inode = page->mapping->host;
456 struct btrfs_writepage_fixup *fixup;
457 struct btrfs_root *root = BTRFS_I(inode)->root;
460 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
465 if (PageChecked(page))
468 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
471 printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
472 SetPageChecked(page);
473 page_cache_get(page);
474 fixup->work.func = btrfs_writepage_fixup_worker;
476 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
480 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
482 struct btrfs_root *root = BTRFS_I(inode)->root;
483 struct btrfs_trans_handle *trans;
484 struct btrfs_ordered_extent *ordered_extent;
485 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
487 struct list_head list;
488 struct btrfs_key ins;
491 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
495 trans = btrfs_join_transaction(root, 1);
497 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
498 BUG_ON(!ordered_extent);
500 lock_extent(io_tree, ordered_extent->file_offset,
501 ordered_extent->file_offset + ordered_extent->len - 1,
504 INIT_LIST_HEAD(&list);
506 ins.objectid = ordered_extent->start;
507 ins.offset = ordered_extent->len;
508 ins.type = BTRFS_EXTENT_ITEM_KEY;
509 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
510 trans->transid, inode->i_ino,
511 ordered_extent->file_offset, &ins);
514 mutex_lock(&BTRFS_I(inode)->extent_mutex);
515 ret = btrfs_drop_extents(trans, root, inode,
516 ordered_extent->file_offset,
517 ordered_extent->file_offset +
519 ordered_extent->file_offset, &alloc_hint);
521 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
522 ordered_extent->file_offset,
523 ordered_extent->start,
525 ordered_extent->len, 0);
527 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
528 ordered_extent->file_offset +
529 ordered_extent->len - 1);
530 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
532 inode->i_blocks += ordered_extent->len >> 9;
533 unlock_extent(io_tree, ordered_extent->file_offset,
534 ordered_extent->file_offset + ordered_extent->len - 1,
536 add_pending_csums(trans, inode, ordered_extent->file_offset,
537 &ordered_extent->list);
539 btrfs_ordered_update_i_size(inode, ordered_extent);
540 btrfs_remove_ordered_extent(inode, ordered_extent);
542 btrfs_put_ordered_extent(ordered_extent);
543 /* once for the tree */
544 btrfs_put_ordered_extent(ordered_extent);
546 btrfs_update_inode(trans, root, inode);
547 btrfs_end_transaction(trans, root);
551 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
552 struct extent_state *state, int uptodate)
554 return btrfs_finish_ordered_io(page->mapping->host, start, end);
557 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
560 struct inode *inode = page->mapping->host;
561 struct btrfs_root *root = BTRFS_I(inode)->root;
562 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
563 struct btrfs_csum_item *item;
564 struct btrfs_path *path = NULL;
567 if (btrfs_test_opt(root, NODATASUM) ||
568 btrfs_test_flag(inode, NODATASUM))
571 path = btrfs_alloc_path();
572 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
575 * It is possible there is an ordered extent that has
576 * not yet finished for this range in the file. If so,
577 * that extent will have a csum cached, and it will insert
578 * the sum after all the blocks in the extent are fully
579 * on disk. So, look for an ordered extent and use the
582 ret = btrfs_find_ordered_sum(inode, start, &csum);
587 /* a csum that isn't present is a preallocated region. */
588 if (ret == -ENOENT || ret == -EFBIG)
591 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
595 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
598 set_state_private(io_tree, start, csum);
601 btrfs_free_path(path);
605 struct io_failure_record {
613 int btrfs_io_failed_hook(struct bio *failed_bio,
614 struct page *page, u64 start, u64 end,
615 struct extent_state *state)
617 struct io_failure_record *failrec = NULL;
619 struct extent_map *em;
620 struct inode *inode = page->mapping->host;
621 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
622 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
629 ret = get_state_private(failure_tree, start, &private);
631 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
634 failrec->start = start;
635 failrec->len = end - start + 1;
636 failrec->last_mirror = 0;
638 spin_lock(&em_tree->lock);
639 em = lookup_extent_mapping(em_tree, start, failrec->len);
640 if (em->start > start || em->start + em->len < start) {
644 spin_unlock(&em_tree->lock);
646 if (!em || IS_ERR(em)) {
650 logical = start - em->start;
651 logical = em->block_start + logical;
652 failrec->logical = logical;
654 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
655 EXTENT_DIRTY, GFP_NOFS);
656 set_state_private(failure_tree, start,
657 (u64)(unsigned long)failrec);
659 failrec = (struct io_failure_record *)(unsigned long)private;
661 num_copies = btrfs_num_copies(
662 &BTRFS_I(inode)->root->fs_info->mapping_tree,
663 failrec->logical, failrec->len);
664 failrec->last_mirror++;
666 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
667 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
670 if (state && state->start != failrec->start)
672 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
674 if (!state || failrec->last_mirror > num_copies) {
675 set_state_private(failure_tree, failrec->start, 0);
676 clear_extent_bits(failure_tree, failrec->start,
677 failrec->start + failrec->len - 1,
678 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
682 bio = bio_alloc(GFP_NOFS, 1);
683 bio->bi_private = state;
684 bio->bi_end_io = failed_bio->bi_end_io;
685 bio->bi_sector = failrec->logical >> 9;
686 bio->bi_bdev = failed_bio->bi_bdev;
688 bio_add_page(bio, page, failrec->len, start - page_offset(page));
689 if (failed_bio->bi_rw & (1 << BIO_RW))
694 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
695 failrec->last_mirror);
699 int btrfs_clean_io_failures(struct inode *inode, u64 start)
703 struct io_failure_record *failure;
707 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
708 (u64)-1, 1, EXTENT_DIRTY)) {
709 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
710 start, &private_failure);
712 failure = (struct io_failure_record *)(unsigned long)
714 set_state_private(&BTRFS_I(inode)->io_failure_tree,
716 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
718 failure->start + failure->len - 1,
719 EXTENT_DIRTY | EXTENT_LOCKED,
727 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
728 struct extent_state *state)
730 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
731 struct inode *inode = page->mapping->host;
732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
734 u64 private = ~(u32)0;
736 struct btrfs_root *root = BTRFS_I(inode)->root;
740 if (btrfs_test_opt(root, NODATASUM) ||
741 btrfs_test_flag(inode, NODATASUM))
743 if (state && state->start == start) {
744 private = state->private;
747 ret = get_state_private(io_tree, start, &private);
749 local_irq_save(flags);
750 kaddr = kmap_atomic(page, KM_IRQ0);
754 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
755 btrfs_csum_final(csum, (char *)&csum);
756 if (csum != private) {
759 kunmap_atomic(kaddr, KM_IRQ0);
760 local_irq_restore(flags);
762 /* if the io failure tree for this inode is non-empty,
763 * check to see if we've recovered from a failed IO
765 btrfs_clean_io_failures(inode, start);
769 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
770 page->mapping->host->i_ino, (unsigned long long)start, csum,
772 memset(kaddr + offset, 1, end - start + 1);
773 flush_dcache_page(page);
774 kunmap_atomic(kaddr, KM_IRQ0);
775 local_irq_restore(flags);
781 void btrfs_read_locked_inode(struct inode *inode)
783 struct btrfs_path *path;
784 struct extent_buffer *leaf;
785 struct btrfs_inode_item *inode_item;
786 struct btrfs_timespec *tspec;
787 struct btrfs_root *root = BTRFS_I(inode)->root;
788 struct btrfs_key location;
789 u64 alloc_group_block;
793 path = btrfs_alloc_path();
795 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
797 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
801 leaf = path->nodes[0];
802 inode_item = btrfs_item_ptr(leaf, path->slots[0],
803 struct btrfs_inode_item);
805 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
806 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
807 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
808 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
809 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
811 tspec = btrfs_inode_atime(inode_item);
812 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
813 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
815 tspec = btrfs_inode_mtime(inode_item);
816 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
817 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
819 tspec = btrfs_inode_ctime(inode_item);
820 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
821 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
823 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
824 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
826 rdev = btrfs_inode_rdev(leaf, inode_item);
828 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
829 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
831 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
832 if (!BTRFS_I(inode)->block_group) {
833 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
835 BTRFS_BLOCK_GROUP_METADATA, 0);
837 btrfs_free_path(path);
840 switch (inode->i_mode & S_IFMT) {
842 inode->i_mapping->a_ops = &btrfs_aops;
843 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
844 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
845 inode->i_fop = &btrfs_file_operations;
846 inode->i_op = &btrfs_file_inode_operations;
849 inode->i_fop = &btrfs_dir_file_operations;
850 if (root == root->fs_info->tree_root)
851 inode->i_op = &btrfs_dir_ro_inode_operations;
853 inode->i_op = &btrfs_dir_inode_operations;
856 inode->i_op = &btrfs_symlink_inode_operations;
857 inode->i_mapping->a_ops = &btrfs_symlink_aops;
858 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
861 init_special_inode(inode, inode->i_mode, rdev);
867 btrfs_free_path(path);
868 make_bad_inode(inode);
871 static void fill_inode_item(struct extent_buffer *leaf,
872 struct btrfs_inode_item *item,
875 btrfs_set_inode_uid(leaf, item, inode->i_uid);
876 btrfs_set_inode_gid(leaf, item, inode->i_gid);
877 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
878 btrfs_set_inode_mode(leaf, item, inode->i_mode);
879 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
881 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
882 inode->i_atime.tv_sec);
883 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
884 inode->i_atime.tv_nsec);
886 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
887 inode->i_mtime.tv_sec);
888 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
889 inode->i_mtime.tv_nsec);
891 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
892 inode->i_ctime.tv_sec);
893 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
894 inode->i_ctime.tv_nsec);
896 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
897 btrfs_set_inode_generation(leaf, item, inode->i_generation);
898 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
899 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
900 btrfs_set_inode_block_group(leaf, item,
901 BTRFS_I(inode)->block_group->key.objectid);
904 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
908 struct btrfs_inode_item *inode_item;
909 struct btrfs_path *path;
910 struct extent_buffer *leaf;
913 path = btrfs_alloc_path();
915 ret = btrfs_lookup_inode(trans, root, path,
916 &BTRFS_I(inode)->location, 1);
923 leaf = path->nodes[0];
924 inode_item = btrfs_item_ptr(leaf, path->slots[0],
925 struct btrfs_inode_item);
927 fill_inode_item(leaf, inode_item, inode);
928 btrfs_mark_buffer_dirty(leaf);
929 btrfs_set_inode_last_trans(trans, inode);
932 btrfs_free_path(path);
937 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root,
940 struct dentry *dentry)
942 struct btrfs_path *path;
943 const char *name = dentry->d_name.name;
944 int name_len = dentry->d_name.len;
946 struct extent_buffer *leaf;
947 struct btrfs_dir_item *di;
948 struct btrfs_key key;
950 path = btrfs_alloc_path();
956 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
966 leaf = path->nodes[0];
967 btrfs_dir_item_key_to_cpu(leaf, di, &key);
968 ret = btrfs_delete_one_dir_name(trans, root, path, di);
971 btrfs_release_path(root, path);
973 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
974 key.objectid, name, name_len, -1);
983 ret = btrfs_delete_one_dir_name(trans, root, path, di);
984 btrfs_release_path(root, path);
986 dentry->d_inode->i_ctime = dir->i_ctime;
987 ret = btrfs_del_inode_ref(trans, root, name, name_len,
988 dentry->d_inode->i_ino,
989 dentry->d_parent->d_inode->i_ino);
991 printk("failed to delete reference to %.*s, "
992 "inode %lu parent %lu\n", name_len, name,
993 dentry->d_inode->i_ino,
994 dentry->d_parent->d_inode->i_ino);
997 btrfs_free_path(path);
999 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1000 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1001 btrfs_update_inode(trans, root, dir);
1002 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1003 dentry->d_inode->i_nlink--;
1005 drop_nlink(dentry->d_inode);
1007 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1008 dir->i_sb->s_dirt = 1;
1013 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1015 struct btrfs_root *root;
1016 struct btrfs_trans_handle *trans;
1018 unsigned long nr = 0;
1020 root = BTRFS_I(dir)->root;
1022 ret = btrfs_check_free_space(root, 1, 1);
1026 trans = btrfs_start_transaction(root, 1);
1028 btrfs_set_trans_block_group(trans, dir);
1029 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1030 nr = trans->blocks_used;
1032 btrfs_end_transaction_throttle(trans, root);
1034 btrfs_btree_balance_dirty(root, nr);
1038 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1040 struct inode *inode = dentry->d_inode;
1043 struct btrfs_root *root = BTRFS_I(dir)->root;
1044 struct btrfs_trans_handle *trans;
1045 unsigned long nr = 0;
1047 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1051 ret = btrfs_check_free_space(root, 1, 1);
1055 trans = btrfs_start_transaction(root, 1);
1056 btrfs_set_trans_block_group(trans, dir);
1058 /* now the directory is empty */
1059 err = btrfs_unlink_trans(trans, root, dir, dentry);
1061 btrfs_i_size_write(inode, 0);
1064 nr = trans->blocks_used;
1065 ret = btrfs_end_transaction_throttle(trans, root);
1067 btrfs_btree_balance_dirty(root, nr);
1075 * this can truncate away extent items, csum items and directory items.
1076 * It starts at a high offset and removes keys until it can't find
1077 * any higher than i_size.
1079 * csum items that cross the new i_size are truncated to the new size
1082 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1083 struct btrfs_root *root,
1084 struct inode *inode,
1088 struct btrfs_path *path;
1089 struct btrfs_key key;
1090 struct btrfs_key found_key;
1092 struct extent_buffer *leaf;
1093 struct btrfs_file_extent_item *fi;
1094 u64 extent_start = 0;
1095 u64 extent_num_bytes = 0;
1101 int pending_del_nr = 0;
1102 int pending_del_slot = 0;
1103 int extent_type = -1;
1104 u64 mask = root->sectorsize - 1;
1106 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1107 path = btrfs_alloc_path();
1111 /* FIXME, add redo link to tree so we don't leak on crash */
1112 key.objectid = inode->i_ino;
1113 key.offset = (u64)-1;
1116 btrfs_init_path(path);
1118 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1123 BUG_ON(path->slots[0] == 0);
1129 leaf = path->nodes[0];
1130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1131 found_type = btrfs_key_type(&found_key);
1133 if (found_key.objectid != inode->i_ino)
1136 if (found_type < min_type)
1139 item_end = found_key.offset;
1140 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1141 fi = btrfs_item_ptr(leaf, path->slots[0],
1142 struct btrfs_file_extent_item);
1143 extent_type = btrfs_file_extent_type(leaf, fi);
1144 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1146 btrfs_file_extent_num_bytes(leaf, fi);
1147 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1148 struct btrfs_item *item = btrfs_item_nr(leaf,
1150 item_end += btrfs_file_extent_inline_len(leaf,
1155 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1156 ret = btrfs_csum_truncate(trans, root, path,
1160 if (item_end < inode->i_size) {
1161 if (found_type == BTRFS_DIR_ITEM_KEY) {
1162 found_type = BTRFS_INODE_ITEM_KEY;
1163 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1164 found_type = BTRFS_CSUM_ITEM_KEY;
1165 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1166 found_type = BTRFS_XATTR_ITEM_KEY;
1167 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1168 found_type = BTRFS_INODE_REF_KEY;
1169 } else if (found_type) {
1174 btrfs_set_key_type(&key, found_type);
1177 if (found_key.offset >= inode->i_size)
1183 /* FIXME, shrink the extent if the ref count is only 1 */
1184 if (found_type != BTRFS_EXTENT_DATA_KEY)
1187 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1189 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1191 u64 orig_num_bytes =
1192 btrfs_file_extent_num_bytes(leaf, fi);
1193 extent_num_bytes = inode->i_size -
1194 found_key.offset + root->sectorsize - 1;
1195 extent_num_bytes = extent_num_bytes &
1196 ~((u64)root->sectorsize - 1);
1197 btrfs_set_file_extent_num_bytes(leaf, fi,
1199 num_dec = (orig_num_bytes -
1201 if (extent_start != 0)
1202 dec_i_blocks(inode, num_dec);
1203 btrfs_mark_buffer_dirty(leaf);
1206 btrfs_file_extent_disk_num_bytes(leaf,
1208 /* FIXME blocksize != 4096 */
1209 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1210 if (extent_start != 0) {
1212 dec_i_blocks(inode, num_dec);
1214 root_gen = btrfs_header_generation(leaf);
1215 root_owner = btrfs_header_owner(leaf);
1217 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1219 u32 newsize = inode->i_size - found_key.offset;
1220 dec_i_blocks(inode, item_end + 1 -
1221 found_key.offset - newsize);
1223 btrfs_file_extent_calc_inline_size(newsize);
1224 ret = btrfs_truncate_item(trans, root, path,
1228 dec_i_blocks(inode, item_end + 1 -
1234 if (!pending_del_nr) {
1235 /* no pending yet, add ourselves */
1236 pending_del_slot = path->slots[0];
1238 } else if (pending_del_nr &&
1239 path->slots[0] + 1 == pending_del_slot) {
1240 /* hop on the pending chunk */
1242 pending_del_slot = path->slots[0];
1244 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1250 ret = btrfs_free_extent(trans, root, extent_start,
1253 root_gen, inode->i_ino,
1254 found_key.offset, 0);
1258 if (path->slots[0] == 0) {
1261 btrfs_release_path(root, path);
1266 if (pending_del_nr &&
1267 path->slots[0] + 1 != pending_del_slot) {
1268 struct btrfs_key debug;
1270 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1272 ret = btrfs_del_items(trans, root, path,
1277 btrfs_release_path(root, path);
1283 if (pending_del_nr) {
1284 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1287 btrfs_free_path(path);
1288 inode->i_sb->s_dirt = 1;
1293 * taken from block_truncate_page, but does cow as it zeros out
1294 * any bytes left in the last page in the file.
1296 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1298 struct inode *inode = mapping->host;
1299 struct btrfs_root *root = BTRFS_I(inode)->root;
1300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1301 struct btrfs_ordered_extent *ordered;
1303 u32 blocksize = root->sectorsize;
1304 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1305 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1311 if ((offset & (blocksize - 1)) == 0)
1316 page = grab_cache_page(mapping, index);
1320 page_start = page_offset(page);
1321 page_end = page_start + PAGE_CACHE_SIZE - 1;
1323 if (!PageUptodate(page)) {
1324 ret = btrfs_readpage(NULL, page);
1326 if (page->mapping != mapping) {
1328 page_cache_release(page);
1331 if (!PageUptodate(page)) {
1336 wait_on_page_writeback(page);
1338 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1339 set_page_extent_mapped(page);
1341 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1343 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1345 page_cache_release(page);
1346 btrfs_start_ordered_extent(inode, ordered, 1);
1347 btrfs_put_ordered_extent(ordered);
1351 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1352 page_end, GFP_NOFS);
1354 if (offset != PAGE_CACHE_SIZE) {
1356 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1357 flush_dcache_page(page);
1360 ClearPageChecked(page);
1361 set_page_dirty(page);
1362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1365 page_cache_release(page);
1370 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1372 struct inode *inode = dentry->d_inode;
1375 err = inode_change_ok(inode, attr);
1379 if (S_ISREG(inode->i_mode) &&
1380 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1381 struct btrfs_trans_handle *trans;
1382 struct btrfs_root *root = BTRFS_I(inode)->root;
1383 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1385 u64 mask = root->sectorsize - 1;
1386 u64 hole_start = (inode->i_size + mask) & ~mask;
1387 u64 block_end = (attr->ia_size + mask) & ~mask;
1391 if (attr->ia_size <= hole_start)
1394 err = btrfs_check_free_space(root, 1, 0);
1398 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1400 hole_size = block_end - hole_start;
1401 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1402 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1404 trans = btrfs_start_transaction(root, 1);
1405 btrfs_set_trans_block_group(trans, inode);
1406 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1407 err = btrfs_drop_extents(trans, root, inode,
1408 hole_start, block_end, hole_start,
1411 if (alloc_hint != EXTENT_MAP_INLINE) {
1412 err = btrfs_insert_file_extent(trans, root,
1416 btrfs_drop_extent_cache(inode, hole_start,
1418 btrfs_check_file(root, inode);
1420 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1421 btrfs_end_transaction(trans, root);
1422 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1427 err = inode_setattr(inode, attr);
1432 void btrfs_delete_inode(struct inode *inode)
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
1439 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1440 truncate_inode_pages(&inode->i_data, 0);
1441 if (is_bad_inode(inode)) {
1445 btrfs_i_size_write(inode, 0);
1446 trans = btrfs_start_transaction(root, 1);
1448 btrfs_set_trans_block_group(trans, inode);
1449 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1451 goto no_delete_lock;
1453 nr = trans->blocks_used;
1456 btrfs_end_transaction(trans, root);
1457 btrfs_btree_balance_dirty(root, nr);
1461 nr = trans->blocks_used;
1462 btrfs_end_transaction(trans, root);
1463 btrfs_btree_balance_dirty(root, nr);
1469 * this returns the key found in the dir entry in the location pointer.
1470 * If no dir entries were found, location->objectid is 0.
1472 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1473 struct btrfs_key *location)
1475 const char *name = dentry->d_name.name;
1476 int namelen = dentry->d_name.len;
1477 struct btrfs_dir_item *di;
1478 struct btrfs_path *path;
1479 struct btrfs_root *root = BTRFS_I(dir)->root;
1482 if (namelen == 1 && strcmp(name, ".") == 0) {
1483 location->objectid = dir->i_ino;
1484 location->type = BTRFS_INODE_ITEM_KEY;
1485 location->offset = 0;
1488 path = btrfs_alloc_path();
1491 if (namelen == 2 && strcmp(name, "..") == 0) {
1492 struct btrfs_key key;
1493 struct extent_buffer *leaf;
1497 key.objectid = dir->i_ino;
1498 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1500 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1504 leaf = path->nodes[0];
1505 slot = path->slots[0];
1506 nritems = btrfs_header_nritems(leaf);
1507 if (slot >= nritems)
1510 btrfs_item_key_to_cpu(leaf, &key, slot);
1511 if (key.objectid != dir->i_ino ||
1512 key.type != BTRFS_INODE_REF_KEY) {
1515 location->objectid = key.offset;
1516 location->type = BTRFS_INODE_ITEM_KEY;
1517 location->offset = 0;
1521 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1525 if (!di || IS_ERR(di)) {
1528 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1530 btrfs_free_path(path);
1533 location->objectid = 0;
1538 * when we hit a tree root in a directory, the btrfs part of the inode
1539 * needs to be changed to reflect the root directory of the tree root. This
1540 * is kind of like crossing a mount point.
1542 static int fixup_tree_root_location(struct btrfs_root *root,
1543 struct btrfs_key *location,
1544 struct btrfs_root **sub_root,
1545 struct dentry *dentry)
1547 struct btrfs_path *path;
1548 struct btrfs_root_item *ri;
1550 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1552 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1555 path = btrfs_alloc_path();
1558 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1559 dentry->d_name.name,
1560 dentry->d_name.len);
1561 if (IS_ERR(*sub_root))
1562 return PTR_ERR(*sub_root);
1564 ri = &(*sub_root)->root_item;
1565 location->objectid = btrfs_root_dirid(ri);
1566 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1567 location->offset = 0;
1569 btrfs_free_path(path);
1573 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1575 struct btrfs_iget_args *args = p;
1576 inode->i_ino = args->ino;
1577 BTRFS_I(inode)->root = args->root;
1578 BTRFS_I(inode)->delalloc_bytes = 0;
1579 BTRFS_I(inode)->disk_i_size = 0;
1580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1582 inode->i_mapping, GFP_NOFS);
1583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1584 inode->i_mapping, GFP_NOFS);
1585 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1586 mutex_init(&BTRFS_I(inode)->csum_mutex);
1587 mutex_init(&BTRFS_I(inode)->extent_mutex);
1591 static int btrfs_find_actor(struct inode *inode, void *opaque)
1593 struct btrfs_iget_args *args = opaque;
1594 return (args->ino == inode->i_ino &&
1595 args->root == BTRFS_I(inode)->root);
1598 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1601 struct btrfs_iget_args args;
1602 args.ino = objectid;
1603 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1608 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1611 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1612 struct btrfs_root *root)
1614 struct inode *inode;
1615 struct btrfs_iget_args args;
1616 args.ino = objectid;
1619 inode = iget5_locked(s, objectid, btrfs_find_actor,
1620 btrfs_init_locked_inode,
1625 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1626 struct nameidata *nd)
1628 struct inode * inode;
1629 struct btrfs_inode *bi = BTRFS_I(dir);
1630 struct btrfs_root *root = bi->root;
1631 struct btrfs_root *sub_root = root;
1632 struct btrfs_key location;
1635 if (dentry->d_name.len > BTRFS_NAME_LEN)
1636 return ERR_PTR(-ENAMETOOLONG);
1638 ret = btrfs_inode_by_name(dir, dentry, &location);
1641 return ERR_PTR(ret);
1644 if (location.objectid) {
1645 ret = fixup_tree_root_location(root, &location, &sub_root,
1648 return ERR_PTR(ret);
1650 return ERR_PTR(-ENOENT);
1651 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1654 return ERR_PTR(-EACCES);
1655 if (inode->i_state & I_NEW) {
1656 /* the inode and parent dir are two different roots */
1657 if (sub_root != root) {
1659 sub_root->inode = inode;
1661 BTRFS_I(inode)->root = sub_root;
1662 memcpy(&BTRFS_I(inode)->location, &location,
1664 btrfs_read_locked_inode(inode);
1665 unlock_new_inode(inode);
1668 return d_splice_alias(inode, dentry);
1671 static unsigned char btrfs_filetype_table[] = {
1672 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1675 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1677 struct inode *inode = filp->f_dentry->d_inode;
1678 struct btrfs_root *root = BTRFS_I(inode)->root;
1679 struct btrfs_item *item;
1680 struct btrfs_dir_item *di;
1681 struct btrfs_key key;
1682 struct btrfs_key found_key;
1683 struct btrfs_path *path;
1686 struct extent_buffer *leaf;
1689 unsigned char d_type;
1694 int key_type = BTRFS_DIR_INDEX_KEY;
1699 /* FIXME, use a real flag for deciding about the key type */
1700 if (root->fs_info->tree_root == root)
1701 key_type = BTRFS_DIR_ITEM_KEY;
1703 /* special case for "." */
1704 if (filp->f_pos == 0) {
1705 over = filldir(dirent, ".", 1,
1713 key.objectid = inode->i_ino;
1714 path = btrfs_alloc_path();
1717 /* special case for .., just use the back ref */
1718 if (filp->f_pos == 1) {
1719 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1723 leaf = path->nodes[0];
1724 slot = path->slots[0];
1725 nritems = btrfs_header_nritems(leaf);
1726 if (slot >= nritems) {
1727 btrfs_release_path(root, path);
1728 goto read_dir_items;
1730 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1731 btrfs_release_path(root, path);
1732 if (found_key.objectid != key.objectid ||
1733 found_key.type != BTRFS_INODE_REF_KEY)
1734 goto read_dir_items;
1735 over = filldir(dirent, "..", 2,
1736 2, found_key.offset, DT_DIR);
1743 btrfs_set_key_type(&key, key_type);
1744 key.offset = filp->f_pos;
1746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1751 leaf = path->nodes[0];
1752 nritems = btrfs_header_nritems(leaf);
1753 slot = path->slots[0];
1754 if (advance || slot >= nritems) {
1755 if (slot >= nritems -1) {
1756 ret = btrfs_next_leaf(root, path);
1759 leaf = path->nodes[0];
1760 nritems = btrfs_header_nritems(leaf);
1761 slot = path->slots[0];
1768 item = btrfs_item_nr(leaf, slot);
1769 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1771 if (found_key.objectid != key.objectid)
1773 if (btrfs_key_type(&found_key) != key_type)
1775 if (found_key.offset < filp->f_pos)
1778 filp->f_pos = found_key.offset;
1780 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1782 di_total = btrfs_item_size(leaf, item);
1783 while(di_cur < di_total) {
1784 struct btrfs_key location;
1786 name_len = btrfs_dir_name_len(leaf, di);
1787 if (name_len < 32) {
1788 name_ptr = tmp_name;
1790 name_ptr = kmalloc(name_len, GFP_NOFS);
1793 read_extent_buffer(leaf, name_ptr,
1794 (unsigned long)(di + 1), name_len);
1796 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1797 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1798 over = filldir(dirent, name_ptr, name_len,
1803 if (name_ptr != tmp_name)
1808 di_len = btrfs_dir_name_len(leaf, di) +
1809 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1811 di = (struct btrfs_dir_item *)((char *)di + di_len);
1814 if (key_type == BTRFS_DIR_INDEX_KEY)
1815 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1821 btrfs_free_path(path);
1825 int btrfs_write_inode(struct inode *inode, int wait)
1827 struct btrfs_root *root = BTRFS_I(inode)->root;
1828 struct btrfs_trans_handle *trans;
1832 trans = btrfs_join_transaction(root, 1);
1833 btrfs_set_trans_block_group(trans, inode);
1834 ret = btrfs_commit_transaction(trans, root);
1840 * This is somewhat expensive, updating the tree every time the
1841 * inode changes. But, it is most likely to find the inode in cache.
1842 * FIXME, needs more benchmarking...there are no reasons other than performance
1843 * to keep or drop this code.
1845 void btrfs_dirty_inode(struct inode *inode)
1847 struct btrfs_root *root = BTRFS_I(inode)->root;
1848 struct btrfs_trans_handle *trans;
1850 trans = btrfs_join_transaction(root, 1);
1851 btrfs_set_trans_block_group(trans, inode);
1852 btrfs_update_inode(trans, root, inode);
1853 btrfs_end_transaction(trans, root);
1856 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *root,
1858 const char *name, int name_len,
1861 struct btrfs_block_group_cache *group,
1864 struct inode *inode;
1865 struct btrfs_inode_item *inode_item;
1866 struct btrfs_block_group_cache *new_inode_group;
1867 struct btrfs_key *location;
1868 struct btrfs_path *path;
1869 struct btrfs_inode_ref *ref;
1870 struct btrfs_key key[2];
1876 path = btrfs_alloc_path();
1879 inode = new_inode(root->fs_info->sb);
1881 return ERR_PTR(-ENOMEM);
1883 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1884 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1885 inode->i_mapping, GFP_NOFS);
1886 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1887 inode->i_mapping, GFP_NOFS);
1888 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1889 mutex_init(&BTRFS_I(inode)->csum_mutex);
1890 mutex_init(&BTRFS_I(inode)->extent_mutex);
1891 BTRFS_I(inode)->delalloc_bytes = 0;
1892 BTRFS_I(inode)->disk_i_size = 0;
1893 BTRFS_I(inode)->root = root;
1899 new_inode_group = btrfs_find_block_group(root, group, 0,
1900 BTRFS_BLOCK_GROUP_METADATA, owner);
1901 if (!new_inode_group) {
1902 printk("find_block group failed\n");
1903 new_inode_group = group;
1905 BTRFS_I(inode)->block_group = new_inode_group;
1906 BTRFS_I(inode)->flags = 0;
1908 key[0].objectid = objectid;
1909 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1912 key[1].objectid = objectid;
1913 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1914 key[1].offset = ref_objectid;
1916 sizes[0] = sizeof(struct btrfs_inode_item);
1917 sizes[1] = name_len + sizeof(*ref);
1919 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1923 if (objectid > root->highest_inode)
1924 root->highest_inode = objectid;
1926 inode->i_uid = current->fsuid;
1927 inode->i_gid = current->fsgid;
1928 inode->i_mode = mode;
1929 inode->i_ino = objectid;
1930 inode->i_blocks = 0;
1931 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1932 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1933 struct btrfs_inode_item);
1934 fill_inode_item(path->nodes[0], inode_item, inode);
1936 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1937 struct btrfs_inode_ref);
1938 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1939 ptr = (unsigned long)(ref + 1);
1940 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1942 btrfs_mark_buffer_dirty(path->nodes[0]);
1943 btrfs_free_path(path);
1945 location = &BTRFS_I(inode)->location;
1946 location->objectid = objectid;
1947 location->offset = 0;
1948 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1950 insert_inode_hash(inode);
1953 btrfs_free_path(path);
1954 return ERR_PTR(ret);
1957 static inline u8 btrfs_inode_type(struct inode *inode)
1959 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1962 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1963 struct dentry *dentry, struct inode *inode,
1967 struct btrfs_key key;
1968 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1969 struct inode *parent_inode;
1971 key.objectid = inode->i_ino;
1972 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1975 ret = btrfs_insert_dir_item(trans, root,
1976 dentry->d_name.name, dentry->d_name.len,
1977 dentry->d_parent->d_inode->i_ino,
1978 &key, btrfs_inode_type(inode));
1981 ret = btrfs_insert_inode_ref(trans, root,
1982 dentry->d_name.name,
1985 dentry->d_parent->d_inode->i_ino);
1987 parent_inode = dentry->d_parent->d_inode;
1988 btrfs_i_size_write(parent_inode, parent_inode->i_size +
1989 dentry->d_name.len * 2);
1990 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1991 ret = btrfs_update_inode(trans, root,
1992 dentry->d_parent->d_inode);
1997 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1998 struct dentry *dentry, struct inode *inode,
2001 int err = btrfs_add_link(trans, dentry, inode, backref);
2003 d_instantiate(dentry, inode);
2011 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2012 int mode, dev_t rdev)
2014 struct btrfs_trans_handle *trans;
2015 struct btrfs_root *root = BTRFS_I(dir)->root;
2016 struct inode *inode = NULL;
2020 unsigned long nr = 0;
2022 if (!new_valid_dev(rdev))
2025 err = btrfs_check_free_space(root, 1, 0);
2029 trans = btrfs_start_transaction(root, 1);
2030 btrfs_set_trans_block_group(trans, dir);
2032 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2038 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2040 dentry->d_parent->d_inode->i_ino, objectid,
2041 BTRFS_I(dir)->block_group, mode);
2042 err = PTR_ERR(inode);
2046 btrfs_set_trans_block_group(trans, inode);
2047 err = btrfs_add_nondir(trans, dentry, inode, 0);
2051 inode->i_op = &btrfs_special_inode_operations;
2052 init_special_inode(inode, inode->i_mode, rdev);
2053 btrfs_update_inode(trans, root, inode);
2055 dir->i_sb->s_dirt = 1;
2056 btrfs_update_inode_block_group(trans, inode);
2057 btrfs_update_inode_block_group(trans, dir);
2059 nr = trans->blocks_used;
2060 btrfs_end_transaction_throttle(trans, root);
2063 inode_dec_link_count(inode);
2066 btrfs_btree_balance_dirty(root, nr);
2070 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2071 int mode, struct nameidata *nd)
2073 struct btrfs_trans_handle *trans;
2074 struct btrfs_root *root = BTRFS_I(dir)->root;
2075 struct inode *inode = NULL;
2078 unsigned long nr = 0;
2081 err = btrfs_check_free_space(root, 1, 0);
2084 trans = btrfs_start_transaction(root, 1);
2085 btrfs_set_trans_block_group(trans, dir);
2087 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2093 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2095 dentry->d_parent->d_inode->i_ino,
2096 objectid, BTRFS_I(dir)->block_group, mode);
2097 err = PTR_ERR(inode);
2101 btrfs_set_trans_block_group(trans, inode);
2102 err = btrfs_add_nondir(trans, dentry, inode, 0);
2106 inode->i_mapping->a_ops = &btrfs_aops;
2107 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2108 inode->i_fop = &btrfs_file_operations;
2109 inode->i_op = &btrfs_file_inode_operations;
2110 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2111 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2112 inode->i_mapping, GFP_NOFS);
2113 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2114 inode->i_mapping, GFP_NOFS);
2115 mutex_init(&BTRFS_I(inode)->csum_mutex);
2116 mutex_init(&BTRFS_I(inode)->extent_mutex);
2117 BTRFS_I(inode)->delalloc_bytes = 0;
2118 BTRFS_I(inode)->disk_i_size = 0;
2119 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2120 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2122 dir->i_sb->s_dirt = 1;
2123 btrfs_update_inode_block_group(trans, inode);
2124 btrfs_update_inode_block_group(trans, dir);
2126 nr = trans->blocks_used;
2127 btrfs_end_transaction_throttle(trans, root);
2130 inode_dec_link_count(inode);
2133 btrfs_btree_balance_dirty(root, nr);
2137 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2138 struct dentry *dentry)
2140 struct btrfs_trans_handle *trans;
2141 struct btrfs_root *root = BTRFS_I(dir)->root;
2142 struct inode *inode = old_dentry->d_inode;
2143 unsigned long nr = 0;
2147 if (inode->i_nlink == 0)
2150 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2155 err = btrfs_check_free_space(root, 1, 0);
2158 trans = btrfs_start_transaction(root, 1);
2160 btrfs_set_trans_block_group(trans, dir);
2161 atomic_inc(&inode->i_count);
2162 err = btrfs_add_nondir(trans, dentry, inode, 1);
2167 dir->i_sb->s_dirt = 1;
2168 btrfs_update_inode_block_group(trans, dir);
2169 err = btrfs_update_inode(trans, root, inode);
2174 nr = trans->blocks_used;
2175 btrfs_end_transaction_throttle(trans, root);
2178 inode_dec_link_count(inode);
2181 btrfs_btree_balance_dirty(root, nr);
2185 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2187 struct inode *inode = NULL;
2188 struct btrfs_trans_handle *trans;
2189 struct btrfs_root *root = BTRFS_I(dir)->root;
2191 int drop_on_err = 0;
2193 unsigned long nr = 1;
2195 err = btrfs_check_free_space(root, 1, 0);
2199 trans = btrfs_start_transaction(root, 1);
2200 btrfs_set_trans_block_group(trans, dir);
2202 if (IS_ERR(trans)) {
2203 err = PTR_ERR(trans);
2207 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2213 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2215 dentry->d_parent->d_inode->i_ino, objectid,
2216 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2217 if (IS_ERR(inode)) {
2218 err = PTR_ERR(inode);
2223 inode->i_op = &btrfs_dir_inode_operations;
2224 inode->i_fop = &btrfs_dir_file_operations;
2225 btrfs_set_trans_block_group(trans, inode);
2227 btrfs_i_size_write(inode, 0);
2228 err = btrfs_update_inode(trans, root, inode);
2232 err = btrfs_add_link(trans, dentry, inode, 0);
2236 d_instantiate(dentry, inode);
2238 dir->i_sb->s_dirt = 1;
2239 btrfs_update_inode_block_group(trans, inode);
2240 btrfs_update_inode_block_group(trans, dir);
2243 nr = trans->blocks_used;
2244 btrfs_end_transaction_throttle(trans, root);
2249 btrfs_btree_balance_dirty(root, nr);
2253 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2254 struct extent_map *existing,
2255 struct extent_map *em,
2256 u64 map_start, u64 map_len)
2260 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2261 start_diff = map_start - em->start;
2262 em->start = map_start;
2264 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2265 em->block_start += start_diff;
2266 return add_extent_mapping(em_tree, em);
2269 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2270 size_t pg_offset, u64 start, u64 len,
2276 u64 extent_start = 0;
2278 u64 objectid = inode->i_ino;
2280 struct btrfs_path *path;
2281 struct btrfs_root *root = BTRFS_I(inode)->root;
2282 struct btrfs_file_extent_item *item;
2283 struct extent_buffer *leaf;
2284 struct btrfs_key found_key;
2285 struct extent_map *em = NULL;
2286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2287 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2288 struct btrfs_trans_handle *trans = NULL;
2290 path = btrfs_alloc_path();
2294 spin_lock(&em_tree->lock);
2295 em = lookup_extent_mapping(em_tree, start, len);
2297 em->bdev = root->fs_info->fs_devices->latest_bdev;
2298 spin_unlock(&em_tree->lock);
2301 if (em->start > start || em->start + em->len <= start)
2302 free_extent_map(em);
2303 else if (em->block_start == EXTENT_MAP_INLINE && page)
2304 free_extent_map(em);
2308 em = alloc_extent_map(GFP_NOFS);
2313 em->bdev = root->fs_info->fs_devices->latest_bdev;
2314 em->start = EXTENT_MAP_HOLE;
2316 ret = btrfs_lookup_file_extent(trans, root, path,
2317 objectid, start, trans != NULL);
2324 if (path->slots[0] == 0)
2329 leaf = path->nodes[0];
2330 item = btrfs_item_ptr(leaf, path->slots[0],
2331 struct btrfs_file_extent_item);
2332 /* are we inside the extent that was found? */
2333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2334 found_type = btrfs_key_type(&found_key);
2335 if (found_key.objectid != objectid ||
2336 found_type != BTRFS_EXTENT_DATA_KEY) {
2340 found_type = btrfs_file_extent_type(leaf, item);
2341 extent_start = found_key.offset;
2342 if (found_type == BTRFS_FILE_EXTENT_REG) {
2343 extent_end = extent_start +
2344 btrfs_file_extent_num_bytes(leaf, item);
2346 if (start < extent_start || start >= extent_end) {
2348 if (start < extent_start) {
2349 if (start + len <= extent_start)
2351 em->len = extent_end - extent_start;
2357 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2359 em->start = extent_start;
2360 em->len = extent_end - extent_start;
2361 em->block_start = EXTENT_MAP_HOLE;
2364 bytenr += btrfs_file_extent_offset(leaf, item);
2365 em->block_start = bytenr;
2366 em->start = extent_start;
2367 em->len = extent_end - extent_start;
2369 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2374 size_t extent_offset;
2377 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2379 extent_end = (extent_start + size + root->sectorsize - 1) &
2380 ~((u64)root->sectorsize - 1);
2381 if (start < extent_start || start >= extent_end) {
2383 if (start < extent_start) {
2384 if (start + len <= extent_start)
2386 em->len = extent_end - extent_start;
2392 em->block_start = EXTENT_MAP_INLINE;
2395 em->start = extent_start;
2400 page_start = page_offset(page) + pg_offset;
2401 extent_offset = page_start - extent_start;
2402 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2403 size - extent_offset);
2404 em->start = extent_start + extent_offset;
2405 em->len = (copy_size + root->sectorsize - 1) &
2406 ~((u64)root->sectorsize - 1);
2408 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2409 if (create == 0 && !PageUptodate(page)) {
2410 read_extent_buffer(leaf, map + pg_offset, ptr,
2412 flush_dcache_page(page);
2413 } else if (create && PageUptodate(page)) {
2416 free_extent_map(em);
2418 btrfs_release_path(root, path);
2419 trans = btrfs_join_transaction(root, 1);
2422 write_extent_buffer(leaf, map + pg_offset, ptr,
2424 btrfs_mark_buffer_dirty(leaf);
2427 set_extent_uptodate(io_tree, em->start,
2428 extent_map_end(em) - 1, GFP_NOFS);
2431 printk("unkknown found_type %d\n", found_type);
2438 em->block_start = EXTENT_MAP_HOLE;
2440 btrfs_release_path(root, path);
2441 if (em->start > start || extent_map_end(em) <= start) {
2442 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2448 spin_lock(&em_tree->lock);
2449 ret = add_extent_mapping(em_tree, em);
2450 /* it is possible that someone inserted the extent into the tree
2451 * while we had the lock dropped. It is also possible that
2452 * an overlapping map exists in the tree
2454 if (ret == -EEXIST) {
2455 struct extent_map *existing;
2459 existing = lookup_extent_mapping(em_tree, start, len);
2460 if (existing && (existing->start > start ||
2461 existing->start + existing->len <= start)) {
2462 free_extent_map(existing);
2466 existing = lookup_extent_mapping(em_tree, em->start,
2469 err = merge_extent_mapping(em_tree, existing,
2472 free_extent_map(existing);
2474 free_extent_map(em);
2479 printk("failing to insert %Lu %Lu\n",
2481 free_extent_map(em);
2485 free_extent_map(em);
2490 spin_unlock(&em_tree->lock);
2492 btrfs_free_path(path);
2494 ret = btrfs_end_transaction(trans, root);
2500 free_extent_map(em);
2502 return ERR_PTR(err);
2507 #if 0 /* waiting for O_DIRECT reads */
2508 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2509 struct buffer_head *bh_result, int create)
2511 struct extent_map *em;
2512 u64 start = (u64)iblock << inode->i_blkbits;
2513 struct btrfs_multi_bio *multi = NULL;
2514 struct btrfs_root *root = BTRFS_I(inode)->root;
2520 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2522 if (!em || IS_ERR(em))
2525 if (em->start > start || em->start + em->len <= start) {
2529 if (em->block_start == EXTENT_MAP_INLINE) {
2534 len = em->start + em->len - start;
2535 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2537 if (em->block_start == EXTENT_MAP_HOLE ||
2538 em->block_start == EXTENT_MAP_DELALLOC) {
2539 bh_result->b_size = len;
2543 logical = start - em->start;
2544 logical = em->block_start + logical;
2547 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2548 logical, &map_length, &multi, 0);
2550 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2551 bh_result->b_size = min(map_length, len);
2553 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2554 set_buffer_mapped(bh_result);
2557 free_extent_map(em);
2562 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2563 const struct iovec *iov, loff_t offset,
2564 unsigned long nr_segs)
2568 struct file *file = iocb->ki_filp;
2569 struct inode *inode = file->f_mapping->host;
2574 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2575 offset, nr_segs, btrfs_get_block, NULL);
2579 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2581 return extent_bmap(mapping, iblock, btrfs_get_extent);
2584 int btrfs_readpage(struct file *file, struct page *page)
2586 struct extent_io_tree *tree;
2587 tree = &BTRFS_I(page->mapping->host)->io_tree;
2588 return extent_read_full_page(tree, page, btrfs_get_extent);
2591 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2593 struct extent_io_tree *tree;
2596 if (current->flags & PF_MEMALLOC) {
2597 redirty_page_for_writepage(wbc, page);
2601 tree = &BTRFS_I(page->mapping->host)->io_tree;
2602 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2605 static int btrfs_writepages(struct address_space *mapping,
2606 struct writeback_control *wbc)
2608 struct extent_io_tree *tree;
2609 tree = &BTRFS_I(mapping->host)->io_tree;
2610 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2614 btrfs_readpages(struct file *file, struct address_space *mapping,
2615 struct list_head *pages, unsigned nr_pages)
2617 struct extent_io_tree *tree;
2618 tree = &BTRFS_I(mapping->host)->io_tree;
2619 return extent_readpages(tree, mapping, pages, nr_pages,
2622 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2624 struct extent_io_tree *tree;
2625 struct extent_map_tree *map;
2628 tree = &BTRFS_I(page->mapping->host)->io_tree;
2629 map = &BTRFS_I(page->mapping->host)->extent_tree;
2630 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2632 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2633 ClearPagePrivate(page);
2634 set_page_private(page, 0);
2635 page_cache_release(page);
2640 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2642 return __btrfs_releasepage(page, gfp_flags);
2645 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2647 struct extent_io_tree *tree;
2648 struct btrfs_ordered_extent *ordered;
2649 u64 page_start = page_offset(page);
2650 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2652 wait_on_page_writeback(page);
2653 tree = &BTRFS_I(page->mapping->host)->io_tree;
2655 btrfs_releasepage(page, GFP_NOFS);
2659 lock_extent(tree, page_start, page_end, GFP_NOFS);
2660 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2664 * IO on this page will never be started, so we need
2665 * to account for any ordered extents now
2667 clear_extent_bit(tree, page_start, page_end,
2668 EXTENT_DIRTY | EXTENT_DELALLOC |
2669 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2670 btrfs_finish_ordered_io(page->mapping->host,
2671 page_start, page_end);
2672 btrfs_put_ordered_extent(ordered);
2673 lock_extent(tree, page_start, page_end, GFP_NOFS);
2675 clear_extent_bit(tree, page_start, page_end,
2676 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2679 __btrfs_releasepage(page, GFP_NOFS);
2681 if (PagePrivate(page)) {
2682 invalidate_extent_lru(tree, page_offset(page),
2684 ClearPagePrivate(page);
2685 set_page_private(page, 0);
2686 page_cache_release(page);
2691 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2692 * called from a page fault handler when a page is first dirtied. Hence we must
2693 * be careful to check for EOF conditions here. We set the page up correctly
2694 * for a written page which means we get ENOSPC checking when writing into
2695 * holes and correct delalloc and unwritten extent mapping on filesystems that
2696 * support these features.
2698 * We are not allowed to take the i_mutex here so we have to play games to
2699 * protect against truncate races as the page could now be beyond EOF. Because
2700 * vmtruncate() writes the inode size before removing pages, once we have the
2701 * page lock we can determine safely if the page is beyond EOF. If it is not
2702 * beyond EOF, then the page is guaranteed safe against truncation until we
2705 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2707 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2708 struct btrfs_root *root = BTRFS_I(inode)->root;
2709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2710 struct btrfs_ordered_extent *ordered;
2712 unsigned long zero_start;
2718 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2725 size = i_size_read(inode);
2726 page_start = page_offset(page);
2727 page_end = page_start + PAGE_CACHE_SIZE - 1;
2729 if ((page->mapping != inode->i_mapping) ||
2730 (page_start >= size)) {
2731 /* page got truncated out from underneath us */
2734 wait_on_page_writeback(page);
2736 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2737 set_page_extent_mapped(page);
2740 * we can't set the delalloc bits if there are pending ordered
2741 * extents. Drop our locks and wait for them to finish
2743 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2745 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2747 btrfs_start_ordered_extent(inode, ordered, 1);
2748 btrfs_put_ordered_extent(ordered);
2752 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2753 page_end, GFP_NOFS);
2756 /* page is wholly or partially inside EOF */
2757 if (page_start + PAGE_CACHE_SIZE > size)
2758 zero_start = size & ~PAGE_CACHE_MASK;
2760 zero_start = PAGE_CACHE_SIZE;
2762 if (zero_start != PAGE_CACHE_SIZE) {
2764 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2765 flush_dcache_page(page);
2768 ClearPageChecked(page);
2769 set_page_dirty(page);
2770 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2778 static void btrfs_truncate(struct inode *inode)
2780 struct btrfs_root *root = BTRFS_I(inode)->root;
2782 struct btrfs_trans_handle *trans;
2784 u64 mask = root->sectorsize - 1;
2786 if (!S_ISREG(inode->i_mode))
2788 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2791 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2793 trans = btrfs_start_transaction(root, 1);
2794 btrfs_set_trans_block_group(trans, inode);
2795 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2796 btrfs_i_size_write(inode, inode->i_size);
2798 /* FIXME, add redo link to tree so we don't leak on crash */
2799 ret = btrfs_truncate_in_trans(trans, root, inode,
2800 BTRFS_EXTENT_DATA_KEY);
2801 btrfs_update_inode(trans, root, inode);
2802 nr = trans->blocks_used;
2804 ret = btrfs_end_transaction_throttle(trans, root);
2806 btrfs_btree_balance_dirty(root, nr);
2810 * Invalidate a single dcache entry at the root of the filesystem.
2811 * Needed after creation of snapshot or subvolume.
2813 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2816 struct dentry *alias, *entry;
2819 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2823 /* change me if btrfs ever gets a d_hash operation */
2824 qstr.hash = full_name_hash(qstr.name, qstr.len);
2825 entry = d_lookup(alias, &qstr);
2828 d_invalidate(entry);
2834 int btrfs_create_subvol_root(struct btrfs_root *new_root,
2835 struct btrfs_trans_handle *trans, u64 new_dirid,
2836 struct btrfs_block_group_cache *block_group)
2838 struct inode *inode;
2841 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2842 new_dirid, block_group, S_IFDIR | 0700);
2844 return PTR_ERR(inode);
2845 inode->i_op = &btrfs_dir_inode_operations;
2846 inode->i_fop = &btrfs_dir_file_operations;
2847 new_root->inode = inode;
2849 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2852 btrfs_i_size_write(inode, 0);
2854 return btrfs_update_inode(trans, new_root, inode);
2857 unsigned long btrfs_force_ra(struct address_space *mapping,
2858 struct file_ra_state *ra, struct file *file,
2859 pgoff_t offset, pgoff_t last_index)
2861 pgoff_t req_size = last_index - offset + 1;
2863 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2864 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2867 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2868 return offset + req_size;
2872 struct inode *btrfs_alloc_inode(struct super_block *sb)
2874 struct btrfs_inode *ei;
2876 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2880 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
2881 return &ei->vfs_inode;
2884 void btrfs_destroy_inode(struct inode *inode)
2886 struct btrfs_ordered_extent *ordered;
2887 WARN_ON(!list_empty(&inode->i_dentry));
2888 WARN_ON(inode->i_data.nrpages);
2891 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2895 printk("found ordered extent %Lu %Lu\n",
2896 ordered->file_offset, ordered->len);
2897 btrfs_remove_ordered_extent(inode, ordered);
2898 btrfs_put_ordered_extent(ordered);
2899 btrfs_put_ordered_extent(ordered);
2902 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2903 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2906 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2907 static void init_once(struct kmem_cache * cachep, void *foo)
2909 static void init_once(void * foo, struct kmem_cache * cachep,
2910 unsigned long flags)
2913 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2915 inode_init_once(&ei->vfs_inode);
2918 void btrfs_destroy_cachep(void)
2920 if (btrfs_inode_cachep)
2921 kmem_cache_destroy(btrfs_inode_cachep);
2922 if (btrfs_trans_handle_cachep)
2923 kmem_cache_destroy(btrfs_trans_handle_cachep);
2924 if (btrfs_transaction_cachep)
2925 kmem_cache_destroy(btrfs_transaction_cachep);
2926 if (btrfs_bit_radix_cachep)
2927 kmem_cache_destroy(btrfs_bit_radix_cachep);
2928 if (btrfs_path_cachep)
2929 kmem_cache_destroy(btrfs_path_cachep);
2932 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2933 unsigned long extra_flags,
2934 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2935 void (*ctor)(struct kmem_cache *, void *)
2937 void (*ctor)(void *, struct kmem_cache *,
2942 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2943 SLAB_MEM_SPREAD | extra_flags), ctor
2944 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2950 int btrfs_init_cachep(void)
2952 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2953 sizeof(struct btrfs_inode),
2955 if (!btrfs_inode_cachep)
2957 btrfs_trans_handle_cachep =
2958 btrfs_cache_create("btrfs_trans_handle_cache",
2959 sizeof(struct btrfs_trans_handle),
2961 if (!btrfs_trans_handle_cachep)
2963 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2964 sizeof(struct btrfs_transaction),
2966 if (!btrfs_transaction_cachep)
2968 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2969 sizeof(struct btrfs_path),
2971 if (!btrfs_path_cachep)
2973 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2974 SLAB_DESTROY_BY_RCU, NULL);
2975 if (!btrfs_bit_radix_cachep)
2979 btrfs_destroy_cachep();
2983 static int btrfs_getattr(struct vfsmount *mnt,
2984 struct dentry *dentry, struct kstat *stat)
2986 struct inode *inode = dentry->d_inode;
2987 generic_fillattr(inode, stat);
2988 stat->blksize = PAGE_CACHE_SIZE;
2989 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2993 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2994 struct inode * new_dir,struct dentry *new_dentry)
2996 struct btrfs_trans_handle *trans;
2997 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2998 struct inode *new_inode = new_dentry->d_inode;
2999 struct inode *old_inode = old_dentry->d_inode;
3000 struct timespec ctime = CURRENT_TIME;
3003 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3004 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3008 ret = btrfs_check_free_space(root, 1, 0);
3012 trans = btrfs_start_transaction(root, 1);
3014 btrfs_set_trans_block_group(trans, new_dir);
3016 old_dentry->d_inode->i_nlink++;
3017 old_dir->i_ctime = old_dir->i_mtime = ctime;
3018 new_dir->i_ctime = new_dir->i_mtime = ctime;
3019 old_inode->i_ctime = ctime;
3021 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3026 new_inode->i_ctime = CURRENT_TIME;
3027 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3031 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3036 btrfs_end_transaction(trans, root);
3041 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3042 const char *symname)
3044 struct btrfs_trans_handle *trans;
3045 struct btrfs_root *root = BTRFS_I(dir)->root;
3046 struct btrfs_path *path;
3047 struct btrfs_key key;
3048 struct inode *inode = NULL;
3055 struct btrfs_file_extent_item *ei;
3056 struct extent_buffer *leaf;
3057 unsigned long nr = 0;
3059 name_len = strlen(symname) + 1;
3060 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3061 return -ENAMETOOLONG;
3063 err = btrfs_check_free_space(root, 1, 0);
3067 trans = btrfs_start_transaction(root, 1);
3068 btrfs_set_trans_block_group(trans, dir);
3070 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3076 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3078 dentry->d_parent->d_inode->i_ino, objectid,
3079 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3080 err = PTR_ERR(inode);
3084 btrfs_set_trans_block_group(trans, inode);
3085 err = btrfs_add_nondir(trans, dentry, inode, 0);
3089 inode->i_mapping->a_ops = &btrfs_aops;
3090 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3091 inode->i_fop = &btrfs_file_operations;
3092 inode->i_op = &btrfs_file_inode_operations;
3093 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3094 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3095 inode->i_mapping, GFP_NOFS);
3096 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3097 inode->i_mapping, GFP_NOFS);
3098 mutex_init(&BTRFS_I(inode)->csum_mutex);
3099 mutex_init(&BTRFS_I(inode)->extent_mutex);
3100 BTRFS_I(inode)->delalloc_bytes = 0;
3101 BTRFS_I(inode)->disk_i_size = 0;
3102 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3103 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3105 dir->i_sb->s_dirt = 1;
3106 btrfs_update_inode_block_group(trans, inode);
3107 btrfs_update_inode_block_group(trans, dir);
3111 path = btrfs_alloc_path();
3113 key.objectid = inode->i_ino;
3115 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3116 datasize = btrfs_file_extent_calc_inline_size(name_len);
3117 err = btrfs_insert_empty_item(trans, root, path, &key,
3123 leaf = path->nodes[0];
3124 ei = btrfs_item_ptr(leaf, path->slots[0],
3125 struct btrfs_file_extent_item);
3126 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3127 btrfs_set_file_extent_type(leaf, ei,
3128 BTRFS_FILE_EXTENT_INLINE);
3129 ptr = btrfs_file_extent_inline_start(ei);
3130 write_extent_buffer(leaf, symname, ptr, name_len);
3131 btrfs_mark_buffer_dirty(leaf);
3132 btrfs_free_path(path);
3134 inode->i_op = &btrfs_symlink_inode_operations;
3135 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3136 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3137 btrfs_i_size_write(inode, name_len - 1);
3138 err = btrfs_update_inode(trans, root, inode);
3143 nr = trans->blocks_used;
3144 btrfs_end_transaction_throttle(trans, root);
3147 inode_dec_link_count(inode);
3150 btrfs_btree_balance_dirty(root, nr);
3154 static int btrfs_set_page_dirty(struct page *page)
3156 return __set_page_dirty_nobuffers(page);
3159 static int btrfs_permission(struct inode *inode, int mask,
3160 struct nameidata *nd)
3162 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3164 return generic_permission(inode, mask, NULL);
3167 static struct inode_operations btrfs_dir_inode_operations = {
3168 .lookup = btrfs_lookup,
3169 .create = btrfs_create,
3170 .unlink = btrfs_unlink,
3172 .mkdir = btrfs_mkdir,
3173 .rmdir = btrfs_rmdir,
3174 .rename = btrfs_rename,
3175 .symlink = btrfs_symlink,
3176 .setattr = btrfs_setattr,
3177 .mknod = btrfs_mknod,
3178 .setxattr = generic_setxattr,
3179 .getxattr = generic_getxattr,
3180 .listxattr = btrfs_listxattr,
3181 .removexattr = generic_removexattr,
3182 .permission = btrfs_permission,
3184 static struct inode_operations btrfs_dir_ro_inode_operations = {
3185 .lookup = btrfs_lookup,
3186 .permission = btrfs_permission,
3188 static struct file_operations btrfs_dir_file_operations = {
3189 .llseek = generic_file_llseek,
3190 .read = generic_read_dir,
3191 .readdir = btrfs_readdir,
3192 .unlocked_ioctl = btrfs_ioctl,
3193 #ifdef CONFIG_COMPAT
3194 .compat_ioctl = btrfs_ioctl,
3196 .release = btrfs_release_file,
3199 static struct extent_io_ops btrfs_extent_io_ops = {
3200 .fill_delalloc = run_delalloc_range,
3201 .submit_bio_hook = btrfs_submit_bio_hook,
3202 .merge_bio_hook = btrfs_merge_bio_hook,
3203 .readpage_io_hook = btrfs_readpage_io_hook,
3204 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3205 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3206 .writepage_start_hook = btrfs_writepage_start_hook,
3207 .readpage_io_failed_hook = btrfs_io_failed_hook,
3208 .set_bit_hook = btrfs_set_bit_hook,
3209 .clear_bit_hook = btrfs_clear_bit_hook,
3212 static struct address_space_operations btrfs_aops = {
3213 .readpage = btrfs_readpage,
3214 .writepage = btrfs_writepage,
3215 .writepages = btrfs_writepages,
3216 .readpages = btrfs_readpages,
3217 .sync_page = block_sync_page,
3219 .direct_IO = btrfs_direct_IO,
3220 .invalidatepage = btrfs_invalidatepage,
3221 .releasepage = btrfs_releasepage,
3222 .set_page_dirty = btrfs_set_page_dirty,
3225 static struct address_space_operations btrfs_symlink_aops = {
3226 .readpage = btrfs_readpage,
3227 .writepage = btrfs_writepage,
3228 .invalidatepage = btrfs_invalidatepage,
3229 .releasepage = btrfs_releasepage,
3232 static struct inode_operations btrfs_file_inode_operations = {
3233 .truncate = btrfs_truncate,
3234 .getattr = btrfs_getattr,
3235 .setattr = btrfs_setattr,
3236 .setxattr = generic_setxattr,
3237 .getxattr = generic_getxattr,
3238 .listxattr = btrfs_listxattr,
3239 .removexattr = generic_removexattr,
3240 .permission = btrfs_permission,
3242 static struct inode_operations btrfs_special_inode_operations = {
3243 .getattr = btrfs_getattr,
3244 .setattr = btrfs_setattr,
3245 .permission = btrfs_permission,
3247 static struct inode_operations btrfs_symlink_inode_operations = {
3248 .readlink = generic_readlink,
3249 .follow_link = page_follow_link_light,
3250 .put_link = page_put_link,
3251 .permission = btrfs_permission,