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"
52 struct btrfs_iget_args {
54 struct btrfs_root *root;
57 static struct inode_operations btrfs_dir_inode_operations;
58 static struct inode_operations btrfs_symlink_inode_operations;
59 static struct inode_operations btrfs_dir_ro_inode_operations;
60 static struct inode_operations btrfs_special_inode_operations;
61 static struct inode_operations btrfs_file_inode_operations;
62 static struct address_space_operations btrfs_aops;
63 static struct address_space_operations btrfs_symlink_aops;
64 static struct file_operations btrfs_dir_file_operations;
65 static struct extent_io_ops btrfs_extent_io_ops;
67 static struct kmem_cache *btrfs_inode_cachep;
68 struct kmem_cache *btrfs_trans_handle_cachep;
69 struct kmem_cache *btrfs_transaction_cachep;
70 struct kmem_cache *btrfs_bit_radix_cachep;
71 struct kmem_cache *btrfs_path_cachep;
74 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
84 static void btrfs_truncate(struct inode *inode);
87 * a very lame attempt at stopping writes when the FS is 85% full. There
88 * are countless ways this is incorrect, but it is better than nothing.
90 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
99 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
100 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
101 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
109 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
111 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
116 * when extent_io.c finds a delayed allocation range in the file,
117 * the call backs end up in this code. The basic idea is to
118 * allocate extents on disk for the range, and create ordered data structs
119 * in ram to track those extents.
121 static int cow_file_range(struct inode *inode, u64 start, u64 end)
123 struct btrfs_root *root = BTRFS_I(inode)->root;
124 struct btrfs_trans_handle *trans;
128 u64 blocksize = root->sectorsize;
130 struct btrfs_key ins;
131 struct extent_map *em;
132 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
135 trans = btrfs_join_transaction(root, 1);
137 btrfs_set_trans_block_group(trans, inode);
139 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
140 num_bytes = max(blocksize, num_bytes);
141 orig_num_bytes = num_bytes;
143 if (alloc_hint == EXTENT_MAP_INLINE)
146 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
147 mutex_lock(&BTRFS_I(inode)->extent_mutex);
148 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
149 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
151 while(num_bytes > 0) {
152 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
153 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
154 root->sectorsize, 0, alloc_hint,
160 em = alloc_extent_map(GFP_NOFS);
162 em->len = ins.offset;
163 em->block_start = ins.objectid;
164 em->bdev = root->fs_info->fs_devices->latest_bdev;
165 mutex_lock(&BTRFS_I(inode)->extent_mutex);
166 set_bit(EXTENT_FLAG_PINNED, &em->flags);
168 spin_lock(&em_tree->lock);
169 ret = add_extent_mapping(em_tree, em);
170 spin_unlock(&em_tree->lock);
171 if (ret != -EEXIST) {
175 btrfs_drop_extent_cache(inode, start,
176 start + ins.offset - 1, 0);
178 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
180 cur_alloc_size = ins.offset;
181 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
184 if (num_bytes < cur_alloc_size) {
185 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
189 num_bytes -= cur_alloc_size;
190 alloc_hint = ins.objectid + ins.offset;
191 start += cur_alloc_size;
194 btrfs_end_transaction(trans, root);
199 * when nowcow writeback call back. This checks for snapshots or COW copies
200 * of the extents that exist in the file, and COWs the file as required.
202 * If no cow copies or snapshots exist, we write directly to the existing
205 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
212 struct btrfs_root *root = BTRFS_I(inode)->root;
213 struct btrfs_block_group_cache *block_group;
214 struct btrfs_trans_handle *trans;
215 struct extent_buffer *leaf;
217 struct btrfs_path *path;
218 struct btrfs_file_extent_item *item;
221 struct btrfs_key found_key;
223 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
224 path = btrfs_alloc_path();
226 trans = btrfs_join_transaction(root, 1);
229 ret = btrfs_lookup_file_extent(NULL, root, path,
230 inode->i_ino, start, 0);
237 if (path->slots[0] == 0)
242 leaf = path->nodes[0];
243 item = btrfs_item_ptr(leaf, path->slots[0],
244 struct btrfs_file_extent_item);
246 /* are we inside the extent that was found? */
247 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
248 found_type = btrfs_key_type(&found_key);
249 if (found_key.objectid != inode->i_ino ||
250 found_type != BTRFS_EXTENT_DATA_KEY)
253 found_type = btrfs_file_extent_type(leaf, item);
254 extent_start = found_key.offset;
255 if (found_type == BTRFS_FILE_EXTENT_REG) {
256 u64 extent_num_bytes;
258 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
259 extent_end = extent_start + extent_num_bytes;
262 if (loops && start != extent_start)
265 if (start < extent_start || start >= extent_end)
268 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
272 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
275 * we may be called by the resizer, make sure we're inside
276 * the limits of the FS
278 block_group = btrfs_lookup_block_group(root->fs_info,
280 if (!block_group || block_group->ro)
283 bytenr += btrfs_file_extent_offset(leaf, item);
284 extent_num_bytes = min(end + 1, extent_end) - start;
285 ret = btrfs_add_ordered_extent(inode, start, bytenr,
286 extent_num_bytes, 1);
292 btrfs_release_path(root, path);
300 btrfs_end_transaction(trans, root);
301 btrfs_free_path(path);
302 return cow_file_range(inode, start, end);
306 btrfs_end_transaction(trans, root);
307 btrfs_free_path(path);
312 * extent_io.c call back to do delayed allocation processing
314 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
316 struct btrfs_root *root = BTRFS_I(inode)->root;
319 if (btrfs_test_opt(root, NODATACOW) ||
320 btrfs_test_flag(inode, NODATACOW))
321 ret = run_delalloc_nocow(inode, start, end);
323 ret = cow_file_range(inode, start, end);
329 * extent_io.c set_bit_hook, used to track delayed allocation
330 * bytes in this file, and to maintain the list of inodes that
331 * have pending delalloc work to be done.
333 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
334 unsigned long old, unsigned long bits)
337 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
340 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
341 root->fs_info->delalloc_bytes += end - start + 1;
342 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
343 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
344 &root->fs_info->delalloc_inodes);
346 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
352 * extent_io.c clear_bit_hook, see set_bit_hook for why
354 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
355 unsigned long old, unsigned long bits)
357 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
358 struct btrfs_root *root = BTRFS_I(inode)->root;
361 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
362 if (end - start + 1 > root->fs_info->delalloc_bytes) {
363 printk("warning: delalloc account %Lu %Lu\n",
364 end - start + 1, root->fs_info->delalloc_bytes);
365 root->fs_info->delalloc_bytes = 0;
366 BTRFS_I(inode)->delalloc_bytes = 0;
368 root->fs_info->delalloc_bytes -= end - start + 1;
369 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
371 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
372 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
373 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
375 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
381 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
382 * we don't create bios that span stripes or chunks
384 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
385 size_t size, struct bio *bio)
387 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
388 struct btrfs_mapping_tree *map_tree;
389 u64 logical = bio->bi_sector << 9;
394 length = bio->bi_size;
395 map_tree = &root->fs_info->mapping_tree;
397 ret = btrfs_map_block(map_tree, READ, logical,
398 &map_length, NULL, 0);
400 if (map_length < length + size) {
407 * in order to insert checksums into the metadata in large chunks,
408 * we wait until bio submission time. All the pages in the bio are
409 * checksummed and sums are attached onto the ordered extent record.
411 * At IO completion time the cums attached on the ordered extent record
412 * are inserted into the btree
414 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
417 struct btrfs_root *root = BTRFS_I(inode)->root;
420 ret = btrfs_csum_one_bio(root, inode, bio);
423 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
427 * extent_io.c submission hook. This does the right thing for csum calculation on write,
428 * or reading the csums from the tree before a read
430 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
433 struct btrfs_root *root = BTRFS_I(inode)->root;
436 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
439 if (btrfs_test_opt(root, NODATASUM) ||
440 btrfs_test_flag(inode, NODATASUM)) {
444 if (!(rw & (1 << BIO_RW))) {
445 btrfs_lookup_bio_sums(root, inode, bio);
448 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
449 inode, rw, bio, mirror_num,
450 __btrfs_submit_bio_hook);
452 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
456 * given a list of ordered sums record them in the inode. This happens
457 * at IO completion time based on sums calculated at bio submission time.
459 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
460 struct inode *inode, u64 file_offset,
461 struct list_head *list)
463 struct list_head *cur;
464 struct btrfs_ordered_sum *sum;
466 btrfs_set_trans_block_group(trans, inode);
467 list_for_each(cur, list) {
468 sum = list_entry(cur, struct btrfs_ordered_sum, list);
469 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
475 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
477 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
481 /* see btrfs_writepage_start_hook for details on why this is required */
482 struct btrfs_writepage_fixup {
484 struct btrfs_work work;
487 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
489 struct btrfs_writepage_fixup *fixup;
490 struct btrfs_ordered_extent *ordered;
496 fixup = container_of(work, struct btrfs_writepage_fixup, work);
500 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
501 ClearPageChecked(page);
505 inode = page->mapping->host;
506 page_start = page_offset(page);
507 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
509 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
511 /* already ordered? We're done */
512 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
513 EXTENT_ORDERED, 0)) {
517 ordered = btrfs_lookup_ordered_extent(inode, page_start);
519 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
522 btrfs_start_ordered_extent(inode, ordered, 1);
526 btrfs_set_extent_delalloc(inode, page_start, page_end);
527 ClearPageChecked(page);
529 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
532 page_cache_release(page);
536 * There are a few paths in the higher layers of the kernel that directly
537 * set the page dirty bit without asking the filesystem if it is a
538 * good idea. This causes problems because we want to make sure COW
539 * properly happens and the data=ordered rules are followed.
541 * In our case any range that doesn't have the EXTENT_ORDERED bit set
542 * hasn't been properly setup for IO. We kick off an async process
543 * to fix it up. The async helper will wait for ordered extents, set
544 * the delalloc bit and make it safe to write the page.
546 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
548 struct inode *inode = page->mapping->host;
549 struct btrfs_writepage_fixup *fixup;
550 struct btrfs_root *root = BTRFS_I(inode)->root;
553 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
558 if (PageChecked(page))
561 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
565 SetPageChecked(page);
566 page_cache_get(page);
567 fixup->work.func = btrfs_writepage_fixup_worker;
569 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
573 /* as ordered data IO finishes, this gets called so we can finish
574 * an ordered extent if the range of bytes in the file it covers are
577 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
579 struct btrfs_root *root = BTRFS_I(inode)->root;
580 struct btrfs_trans_handle *trans;
581 struct btrfs_ordered_extent *ordered_extent;
582 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
583 struct btrfs_file_extent_item *extent_item;
584 struct btrfs_path *path = NULL;
585 struct extent_buffer *leaf;
587 struct list_head list;
588 struct btrfs_key ins;
591 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
595 trans = btrfs_join_transaction(root, 1);
597 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
598 BUG_ON(!ordered_extent);
599 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
602 path = btrfs_alloc_path();
605 lock_extent(io_tree, ordered_extent->file_offset,
606 ordered_extent->file_offset + ordered_extent->len - 1,
609 INIT_LIST_HEAD(&list);
611 mutex_lock(&BTRFS_I(inode)->extent_mutex);
613 ret = btrfs_drop_extents(trans, root, inode,
614 ordered_extent->file_offset,
615 ordered_extent->file_offset +
617 ordered_extent->file_offset, &alloc_hint);
620 ins.objectid = inode->i_ino;
621 ins.offset = ordered_extent->file_offset;
622 ins.type = BTRFS_EXTENT_DATA_KEY;
623 ret = btrfs_insert_empty_item(trans, root, path, &ins,
624 sizeof(*extent_item));
626 leaf = path->nodes[0];
627 extent_item = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_file_extent_item);
629 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
630 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
631 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
632 ordered_extent->start);
633 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
634 ordered_extent->len);
635 btrfs_set_file_extent_offset(leaf, extent_item, 0);
636 btrfs_set_file_extent_num_bytes(leaf, extent_item,
637 ordered_extent->len);
638 btrfs_mark_buffer_dirty(leaf);
640 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
641 ordered_extent->file_offset +
642 ordered_extent->len - 1, 0);
643 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
645 ins.objectid = ordered_extent->start;
646 ins.offset = ordered_extent->len;
647 ins.type = BTRFS_EXTENT_ITEM_KEY;
648 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
649 root->root_key.objectid,
650 trans->transid, inode->i_ino,
651 ordered_extent->file_offset, &ins);
653 btrfs_release_path(root, path);
655 inode->i_blocks += ordered_extent->len >> 9;
656 unlock_extent(io_tree, ordered_extent->file_offset,
657 ordered_extent->file_offset + ordered_extent->len - 1,
660 add_pending_csums(trans, inode, ordered_extent->file_offset,
661 &ordered_extent->list);
663 mutex_lock(&BTRFS_I(inode)->extent_mutex);
664 btrfs_ordered_update_i_size(inode, ordered_extent);
665 btrfs_update_inode(trans, root, inode);
666 btrfs_remove_ordered_extent(inode, ordered_extent);
667 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
670 btrfs_put_ordered_extent(ordered_extent);
671 /* once for the tree */
672 btrfs_put_ordered_extent(ordered_extent);
674 btrfs_end_transaction(trans, root);
676 btrfs_free_path(path);
680 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
681 struct extent_state *state, int uptodate)
683 return btrfs_finish_ordered_io(page->mapping->host, start, end);
687 * When IO fails, either with EIO or csum verification fails, we
688 * try other mirrors that might have a good copy of the data. This
689 * io_failure_record is used to record state as we go through all the
690 * mirrors. If another mirror has good data, the page is set up to date
691 * and things continue. If a good mirror can't be found, the original
692 * bio end_io callback is called to indicate things have failed.
694 struct io_failure_record {
702 int btrfs_io_failed_hook(struct bio *failed_bio,
703 struct page *page, u64 start, u64 end,
704 struct extent_state *state)
706 struct io_failure_record *failrec = NULL;
708 struct extent_map *em;
709 struct inode *inode = page->mapping->host;
710 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
711 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
718 ret = get_state_private(failure_tree, start, &private);
720 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
723 failrec->start = start;
724 failrec->len = end - start + 1;
725 failrec->last_mirror = 0;
727 spin_lock(&em_tree->lock);
728 em = lookup_extent_mapping(em_tree, start, failrec->len);
729 if (em->start > start || em->start + em->len < start) {
733 spin_unlock(&em_tree->lock);
735 if (!em || IS_ERR(em)) {
739 logical = start - em->start;
740 logical = em->block_start + logical;
741 failrec->logical = logical;
743 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
744 EXTENT_DIRTY, GFP_NOFS);
745 set_state_private(failure_tree, start,
746 (u64)(unsigned long)failrec);
748 failrec = (struct io_failure_record *)(unsigned long)private;
750 num_copies = btrfs_num_copies(
751 &BTRFS_I(inode)->root->fs_info->mapping_tree,
752 failrec->logical, failrec->len);
753 failrec->last_mirror++;
755 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
756 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
759 if (state && state->start != failrec->start)
761 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
763 if (!state || failrec->last_mirror > num_copies) {
764 set_state_private(failure_tree, failrec->start, 0);
765 clear_extent_bits(failure_tree, failrec->start,
766 failrec->start + failrec->len - 1,
767 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
771 bio = bio_alloc(GFP_NOFS, 1);
772 bio->bi_private = state;
773 bio->bi_end_io = failed_bio->bi_end_io;
774 bio->bi_sector = failrec->logical >> 9;
775 bio->bi_bdev = failed_bio->bi_bdev;
777 bio_add_page(bio, page, failrec->len, start - page_offset(page));
778 if (failed_bio->bi_rw & (1 << BIO_RW))
783 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
784 failrec->last_mirror);
789 * each time an IO finishes, we do a fast check in the IO failure tree
790 * to see if we need to process or clean up an io_failure_record
792 int btrfs_clean_io_failures(struct inode *inode, u64 start)
796 struct io_failure_record *failure;
800 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
801 (u64)-1, 1, EXTENT_DIRTY)) {
802 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
803 start, &private_failure);
805 failure = (struct io_failure_record *)(unsigned long)
807 set_state_private(&BTRFS_I(inode)->io_failure_tree,
809 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
811 failure->start + failure->len - 1,
812 EXTENT_DIRTY | EXTENT_LOCKED,
821 * when reads are done, we need to check csums to verify the data is correct
822 * if there's a match, we allow the bio to finish. If not, we go through
823 * the io_failure_record routines to find good copies
825 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
826 struct extent_state *state)
828 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
829 struct inode *inode = page->mapping->host;
830 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
832 u64 private = ~(u32)0;
834 struct btrfs_root *root = BTRFS_I(inode)->root;
838 if (btrfs_test_opt(root, NODATASUM) ||
839 btrfs_test_flag(inode, NODATASUM))
841 if (state && state->start == start) {
842 private = state->private;
845 ret = get_state_private(io_tree, start, &private);
847 local_irq_save(flags);
848 kaddr = kmap_atomic(page, KM_IRQ0);
852 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
853 btrfs_csum_final(csum, (char *)&csum);
854 if (csum != private) {
857 kunmap_atomic(kaddr, KM_IRQ0);
858 local_irq_restore(flags);
860 /* if the io failure tree for this inode is non-empty,
861 * check to see if we've recovered from a failed IO
863 btrfs_clean_io_failures(inode, start);
867 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
868 page->mapping->host->i_ino, (unsigned long long)start, csum,
870 memset(kaddr + offset, 1, end - start + 1);
871 flush_dcache_page(page);
872 kunmap_atomic(kaddr, KM_IRQ0);
873 local_irq_restore(flags);
880 * This creates an orphan entry for the given inode in case something goes
881 * wrong in the middle of an unlink/truncate.
883 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
885 struct btrfs_root *root = BTRFS_I(inode)->root;
888 spin_lock(&root->list_lock);
890 /* already on the orphan list, we're good */
891 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
892 spin_unlock(&root->list_lock);
896 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
898 spin_unlock(&root->list_lock);
901 * insert an orphan item to track this unlinked/truncated file
903 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
909 * We have done the truncate/delete so we can go ahead and remove the orphan
910 * item for this particular inode.
912 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
914 struct btrfs_root *root = BTRFS_I(inode)->root;
917 spin_lock(&root->list_lock);
919 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
920 spin_unlock(&root->list_lock);
924 list_del_init(&BTRFS_I(inode)->i_orphan);
926 spin_unlock(&root->list_lock);
930 spin_unlock(&root->list_lock);
932 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
938 * this cleans up any orphans that may be left on the list from the last use
941 void btrfs_orphan_cleanup(struct btrfs_root *root)
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_item *item;
946 struct btrfs_key key, found_key;
947 struct btrfs_trans_handle *trans;
949 int ret = 0, nr_unlink = 0, nr_truncate = 0;
951 /* don't do orphan cleanup if the fs is readonly. */
952 if (root->fs_info->sb->s_flags & MS_RDONLY)
955 path = btrfs_alloc_path();
960 key.objectid = BTRFS_ORPHAN_OBJECTID;
961 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
962 key.offset = (u64)-1;
966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
968 printk(KERN_ERR "Error searching slot for orphan: %d"
974 * if ret == 0 means we found what we were searching for, which
975 * is weird, but possible, so only screw with path if we didnt
976 * find the key and see if we have stuff that matches
979 if (path->slots[0] == 0)
984 /* pull out the item */
985 leaf = path->nodes[0];
986 item = btrfs_item_nr(leaf, path->slots[0]);
987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
989 /* make sure the item matches what we want */
990 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
992 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
995 /* release the path since we're done with it */
996 btrfs_release_path(root, path);
999 * this is where we are basically btrfs_lookup, without the
1000 * crossing root thing. we store the inode number in the
1001 * offset of the orphan item.
1003 inode = btrfs_iget_locked(root->fs_info->sb,
1004 found_key.offset, root);
1008 if (inode->i_state & I_NEW) {
1009 BTRFS_I(inode)->root = root;
1011 /* have to set the location manually */
1012 BTRFS_I(inode)->location.objectid = inode->i_ino;
1013 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1014 BTRFS_I(inode)->location.offset = 0;
1016 btrfs_read_locked_inode(inode);
1017 unlock_new_inode(inode);
1021 * add this inode to the orphan list so btrfs_orphan_del does
1022 * the proper thing when we hit it
1024 spin_lock(&root->list_lock);
1025 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1026 spin_unlock(&root->list_lock);
1029 * if this is a bad inode, means we actually succeeded in
1030 * removing the inode, but not the orphan record, which means
1031 * we need to manually delete the orphan since iput will just
1032 * do a destroy_inode
1034 if (is_bad_inode(inode)) {
1035 trans = btrfs_start_transaction(root, 1);
1036 btrfs_orphan_del(trans, inode);
1037 btrfs_end_transaction(trans, root);
1042 /* if we have links, this was a truncate, lets do that */
1043 if (inode->i_nlink) {
1045 btrfs_truncate(inode);
1050 /* this will do delete_inode and everything for us */
1055 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1057 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1059 btrfs_free_path(path);
1063 * read an inode from the btree into the in-memory inode
1065 void btrfs_read_locked_inode(struct inode *inode)
1067 struct btrfs_path *path;
1068 struct extent_buffer *leaf;
1069 struct btrfs_inode_item *inode_item;
1070 struct btrfs_timespec *tspec;
1071 struct btrfs_root *root = BTRFS_I(inode)->root;
1072 struct btrfs_key location;
1073 u64 alloc_group_block;
1077 path = btrfs_alloc_path();
1079 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1081 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1085 leaf = path->nodes[0];
1086 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_inode_item);
1089 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1090 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1091 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1092 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1093 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1095 tspec = btrfs_inode_atime(inode_item);
1096 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1097 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1099 tspec = btrfs_inode_mtime(inode_item);
1100 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1101 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1103 tspec = btrfs_inode_ctime(inode_item);
1104 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1107 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1108 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1109 inode->i_generation = BTRFS_I(inode)->generation;
1111 rdev = btrfs_inode_rdev(leaf, inode_item);
1113 BTRFS_I(inode)->index_cnt = (u64)-1;
1115 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1116 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1118 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1119 if (!BTRFS_I(inode)->block_group) {
1120 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1122 BTRFS_BLOCK_GROUP_METADATA, 0);
1124 btrfs_free_path(path);
1127 switch (inode->i_mode & S_IFMT) {
1129 inode->i_mapping->a_ops = &btrfs_aops;
1130 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1131 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1132 inode->i_fop = &btrfs_file_operations;
1133 inode->i_op = &btrfs_file_inode_operations;
1136 inode->i_fop = &btrfs_dir_file_operations;
1137 if (root == root->fs_info->tree_root)
1138 inode->i_op = &btrfs_dir_ro_inode_operations;
1140 inode->i_op = &btrfs_dir_inode_operations;
1143 inode->i_op = &btrfs_symlink_inode_operations;
1144 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1145 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1148 init_special_inode(inode, inode->i_mode, rdev);
1154 btrfs_free_path(path);
1155 make_bad_inode(inode);
1159 * given a leaf and an inode, copy the inode fields into the leaf
1161 static void fill_inode_item(struct btrfs_trans_handle *trans,
1162 struct extent_buffer *leaf,
1163 struct btrfs_inode_item *item,
1164 struct inode *inode)
1166 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1167 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1168 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1169 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1170 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1172 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1173 inode->i_atime.tv_sec);
1174 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1175 inode->i_atime.tv_nsec);
1177 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1178 inode->i_mtime.tv_sec);
1179 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1180 inode->i_mtime.tv_nsec);
1182 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1183 inode->i_ctime.tv_sec);
1184 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1185 inode->i_ctime.tv_nsec);
1187 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1188 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1189 btrfs_set_inode_transid(leaf, item, trans->transid);
1190 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1191 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1192 btrfs_set_inode_block_group(leaf, item,
1193 BTRFS_I(inode)->block_group->key.objectid);
1197 * copy everything in the in-memory inode into the btree.
1199 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1200 struct btrfs_root *root,
1201 struct inode *inode)
1203 struct btrfs_inode_item *inode_item;
1204 struct btrfs_path *path;
1205 struct extent_buffer *leaf;
1208 path = btrfs_alloc_path();
1210 ret = btrfs_lookup_inode(trans, root, path,
1211 &BTRFS_I(inode)->location, 1);
1218 leaf = path->nodes[0];
1219 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1220 struct btrfs_inode_item);
1222 fill_inode_item(trans, leaf, inode_item, inode);
1223 btrfs_mark_buffer_dirty(leaf);
1224 btrfs_set_inode_last_trans(trans, inode);
1227 btrfs_free_path(path);
1233 * unlink helper that gets used here in inode.c and in the tree logging
1234 * recovery code. It remove a link in a directory with a given name, and
1235 * also drops the back refs in the inode to the directory
1237 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root,
1239 struct inode *dir, struct inode *inode,
1240 const char *name, int name_len)
1242 struct btrfs_path *path;
1244 struct extent_buffer *leaf;
1245 struct btrfs_dir_item *di;
1246 struct btrfs_key key;
1249 path = btrfs_alloc_path();
1255 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1256 name, name_len, -1);
1265 leaf = path->nodes[0];
1266 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1267 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1270 btrfs_release_path(root, path);
1272 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1274 dir->i_ino, &index);
1276 printk("failed to delete reference to %.*s, "
1277 "inode %lu parent %lu\n", name_len, name,
1278 inode->i_ino, dir->i_ino);
1282 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1283 index, name, name_len, -1);
1292 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1293 btrfs_release_path(root, path);
1295 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1297 BUG_ON(ret != 0 && ret != -ENOENT);
1299 BTRFS_I(dir)->log_dirty_trans = trans->transid;
1301 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1305 btrfs_free_path(path);
1309 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1310 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1311 btrfs_update_inode(trans, root, dir);
1312 btrfs_drop_nlink(inode);
1313 ret = btrfs_update_inode(trans, root, inode);
1314 dir->i_sb->s_dirt = 1;
1319 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1321 struct btrfs_root *root;
1322 struct btrfs_trans_handle *trans;
1323 struct inode *inode = dentry->d_inode;
1325 unsigned long nr = 0;
1327 root = BTRFS_I(dir)->root;
1329 ret = btrfs_check_free_space(root, 1, 1);
1333 trans = btrfs_start_transaction(root, 1);
1335 btrfs_set_trans_block_group(trans, dir);
1336 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1337 dentry->d_name.name, dentry->d_name.len);
1339 if (inode->i_nlink == 0)
1340 ret = btrfs_orphan_add(trans, inode);
1342 nr = trans->blocks_used;
1344 btrfs_end_transaction_throttle(trans, root);
1346 btrfs_btree_balance_dirty(root, nr);
1350 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1352 struct inode *inode = dentry->d_inode;
1355 struct btrfs_root *root = BTRFS_I(dir)->root;
1356 struct btrfs_trans_handle *trans;
1357 unsigned long nr = 0;
1359 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1363 ret = btrfs_check_free_space(root, 1, 1);
1367 trans = btrfs_start_transaction(root, 1);
1368 btrfs_set_trans_block_group(trans, dir);
1370 err = btrfs_orphan_add(trans, inode);
1374 /* now the directory is empty */
1375 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1376 dentry->d_name.name, dentry->d_name.len);
1378 btrfs_i_size_write(inode, 0);
1382 nr = trans->blocks_used;
1383 ret = btrfs_end_transaction_throttle(trans, root);
1385 btrfs_btree_balance_dirty(root, nr);
1393 * this can truncate away extent items, csum items and directory items.
1394 * It starts at a high offset and removes keys until it can't find
1395 * any higher than new_size
1397 * csum items that cross the new i_size are truncated to the new size
1400 * min_type is the minimum key type to truncate down to. If set to 0, this
1401 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1403 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1404 struct btrfs_root *root,
1405 struct inode *inode,
1406 u64 new_size, u32 min_type)
1409 struct btrfs_path *path;
1410 struct btrfs_key key;
1411 struct btrfs_key found_key;
1413 struct extent_buffer *leaf;
1414 struct btrfs_file_extent_item *fi;
1415 u64 extent_start = 0;
1416 u64 extent_num_bytes = 0;
1422 int pending_del_nr = 0;
1423 int pending_del_slot = 0;
1424 int extent_type = -1;
1425 u64 mask = root->sectorsize - 1;
1428 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
1429 path = btrfs_alloc_path();
1433 /* FIXME, add redo link to tree so we don't leak on crash */
1434 key.objectid = inode->i_ino;
1435 key.offset = (u64)-1;
1438 btrfs_init_path(path);
1440 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1445 /* there are no items in the tree for us to truncate, we're
1448 if (path->slots[0] == 0) {
1457 leaf = path->nodes[0];
1458 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1459 found_type = btrfs_key_type(&found_key);
1461 if (found_key.objectid != inode->i_ino)
1464 if (found_type < min_type)
1467 item_end = found_key.offset;
1468 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1469 fi = btrfs_item_ptr(leaf, path->slots[0],
1470 struct btrfs_file_extent_item);
1471 extent_type = btrfs_file_extent_type(leaf, fi);
1472 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1474 btrfs_file_extent_num_bytes(leaf, fi);
1475 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1476 struct btrfs_item *item = btrfs_item_nr(leaf,
1478 item_end += btrfs_file_extent_inline_len(leaf,
1483 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1484 ret = btrfs_csum_truncate(trans, root, path,
1488 if (item_end < new_size) {
1489 if (found_type == BTRFS_DIR_ITEM_KEY) {
1490 found_type = BTRFS_INODE_ITEM_KEY;
1491 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1492 found_type = BTRFS_CSUM_ITEM_KEY;
1493 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1494 found_type = BTRFS_XATTR_ITEM_KEY;
1495 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1496 found_type = BTRFS_INODE_REF_KEY;
1497 } else if (found_type) {
1502 btrfs_set_key_type(&key, found_type);
1505 if (found_key.offset >= new_size)
1511 /* FIXME, shrink the extent if the ref count is only 1 */
1512 if (found_type != BTRFS_EXTENT_DATA_KEY)
1515 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1517 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1519 u64 orig_num_bytes =
1520 btrfs_file_extent_num_bytes(leaf, fi);
1521 extent_num_bytes = new_size -
1522 found_key.offset + root->sectorsize - 1;
1523 extent_num_bytes = extent_num_bytes &
1524 ~((u64)root->sectorsize - 1);
1525 btrfs_set_file_extent_num_bytes(leaf, fi,
1527 num_dec = (orig_num_bytes -
1529 if (root->ref_cows && extent_start != 0)
1530 dec_i_blocks(inode, num_dec);
1531 btrfs_mark_buffer_dirty(leaf);
1534 btrfs_file_extent_disk_num_bytes(leaf,
1536 /* FIXME blocksize != 4096 */
1537 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1538 if (extent_start != 0) {
1541 dec_i_blocks(inode, num_dec);
1543 root_gen = btrfs_header_generation(leaf);
1544 root_owner = btrfs_header_owner(leaf);
1546 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1548 u32 size = new_size - found_key.offset;
1550 if (root->ref_cows) {
1551 dec_i_blocks(inode, item_end + 1 -
1552 found_key.offset - size);
1555 btrfs_file_extent_calc_inline_size(size);
1556 ret = btrfs_truncate_item(trans, root, path,
1559 } else if (root->ref_cows) {
1560 dec_i_blocks(inode, item_end + 1 -
1566 if (!pending_del_nr) {
1567 /* no pending yet, add ourselves */
1568 pending_del_slot = path->slots[0];
1570 } else if (pending_del_nr &&
1571 path->slots[0] + 1 == pending_del_slot) {
1572 /* hop on the pending chunk */
1574 pending_del_slot = path->slots[0];
1576 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1582 ret = btrfs_free_extent(trans, root, extent_start,
1584 leaf->start, root_owner,
1585 root_gen, inode->i_ino,
1586 found_key.offset, 0);
1590 if (path->slots[0] == 0) {
1593 btrfs_release_path(root, path);
1598 if (pending_del_nr &&
1599 path->slots[0] + 1 != pending_del_slot) {
1600 struct btrfs_key debug;
1602 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1604 ret = btrfs_del_items(trans, root, path,
1609 btrfs_release_path(root, path);
1615 if (pending_del_nr) {
1616 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1619 btrfs_free_path(path);
1620 inode->i_sb->s_dirt = 1;
1625 * taken from block_truncate_page, but does cow as it zeros out
1626 * any bytes left in the last page in the file.
1628 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1630 struct inode *inode = mapping->host;
1631 struct btrfs_root *root = BTRFS_I(inode)->root;
1632 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1633 struct btrfs_ordered_extent *ordered;
1635 u32 blocksize = root->sectorsize;
1636 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1637 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1643 if ((offset & (blocksize - 1)) == 0)
1648 page = grab_cache_page(mapping, index);
1652 page_start = page_offset(page);
1653 page_end = page_start + PAGE_CACHE_SIZE - 1;
1655 if (!PageUptodate(page)) {
1656 ret = btrfs_readpage(NULL, page);
1658 if (page->mapping != mapping) {
1660 page_cache_release(page);
1663 if (!PageUptodate(page)) {
1668 wait_on_page_writeback(page);
1670 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1671 set_page_extent_mapped(page);
1673 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1675 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1677 page_cache_release(page);
1678 btrfs_start_ordered_extent(inode, ordered, 1);
1679 btrfs_put_ordered_extent(ordered);
1683 btrfs_set_extent_delalloc(inode, page_start, page_end);
1685 if (offset != PAGE_CACHE_SIZE) {
1687 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1688 flush_dcache_page(page);
1691 ClearPageChecked(page);
1692 set_page_dirty(page);
1693 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1697 page_cache_release(page);
1702 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1704 struct inode *inode = dentry->d_inode;
1707 err = inode_change_ok(inode, attr);
1711 if (S_ISREG(inode->i_mode) &&
1712 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1713 struct btrfs_trans_handle *trans;
1714 struct btrfs_root *root = BTRFS_I(inode)->root;
1715 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1717 u64 mask = root->sectorsize - 1;
1718 u64 hole_start = (inode->i_size + mask) & ~mask;
1719 u64 block_end = (attr->ia_size + mask) & ~mask;
1723 if (attr->ia_size <= hole_start)
1726 err = btrfs_check_free_space(root, 1, 0);
1730 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1732 hole_size = block_end - hole_start;
1734 struct btrfs_ordered_extent *ordered;
1735 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1737 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1738 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1740 unlock_extent(io_tree, hole_start,
1741 block_end - 1, GFP_NOFS);
1742 btrfs_put_ordered_extent(ordered);
1748 trans = btrfs_start_transaction(root, 1);
1749 btrfs_set_trans_block_group(trans, inode);
1750 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1751 err = btrfs_drop_extents(trans, root, inode,
1752 hole_start, block_end, hole_start,
1755 if (alloc_hint != EXTENT_MAP_INLINE) {
1756 err = btrfs_insert_file_extent(trans, root,
1760 btrfs_drop_extent_cache(inode, hole_start,
1762 btrfs_check_file(root, inode);
1764 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1765 btrfs_end_transaction(trans, root);
1766 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1771 err = inode_setattr(inode, attr);
1773 if (!err && ((attr->ia_valid & ATTR_MODE)))
1774 err = btrfs_acl_chmod(inode);
1779 void btrfs_delete_inode(struct inode *inode)
1781 struct btrfs_trans_handle *trans;
1782 struct btrfs_root *root = BTRFS_I(inode)->root;
1786 truncate_inode_pages(&inode->i_data, 0);
1787 if (is_bad_inode(inode)) {
1788 btrfs_orphan_del(NULL, inode);
1791 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1793 btrfs_i_size_write(inode, 0);
1794 trans = btrfs_start_transaction(root, 1);
1796 btrfs_set_trans_block_group(trans, inode);
1797 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
1799 btrfs_orphan_del(NULL, inode);
1800 goto no_delete_lock;
1803 btrfs_orphan_del(trans, inode);
1805 nr = trans->blocks_used;
1808 btrfs_end_transaction(trans, root);
1809 btrfs_btree_balance_dirty(root, nr);
1813 nr = trans->blocks_used;
1814 btrfs_end_transaction(trans, root);
1815 btrfs_btree_balance_dirty(root, nr);
1821 * this returns the key found in the dir entry in the location pointer.
1822 * If no dir entries were found, location->objectid is 0.
1824 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1825 struct btrfs_key *location)
1827 const char *name = dentry->d_name.name;
1828 int namelen = dentry->d_name.len;
1829 struct btrfs_dir_item *di;
1830 struct btrfs_path *path;
1831 struct btrfs_root *root = BTRFS_I(dir)->root;
1834 path = btrfs_alloc_path();
1837 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1841 if (!di || IS_ERR(di)) {
1844 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1846 btrfs_free_path(path);
1849 location->objectid = 0;
1854 * when we hit a tree root in a directory, the btrfs part of the inode
1855 * needs to be changed to reflect the root directory of the tree root. This
1856 * is kind of like crossing a mount point.
1858 static int fixup_tree_root_location(struct btrfs_root *root,
1859 struct btrfs_key *location,
1860 struct btrfs_root **sub_root,
1861 struct dentry *dentry)
1863 struct btrfs_root_item *ri;
1865 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1867 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1870 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1871 dentry->d_name.name,
1872 dentry->d_name.len);
1873 if (IS_ERR(*sub_root))
1874 return PTR_ERR(*sub_root);
1876 ri = &(*sub_root)->root_item;
1877 location->objectid = btrfs_root_dirid(ri);
1878 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1879 location->offset = 0;
1884 static noinline void init_btrfs_i(struct inode *inode)
1886 struct btrfs_inode *bi = BTRFS_I(inode);
1889 bi->i_default_acl = NULL;
1893 bi->logged_trans = 0;
1894 bi->delalloc_bytes = 0;
1895 bi->disk_i_size = 0;
1897 bi->index_cnt = (u64)-1;
1898 bi->log_dirty_trans = 0;
1899 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1900 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1901 inode->i_mapping, GFP_NOFS);
1902 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1903 inode->i_mapping, GFP_NOFS);
1904 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
1905 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1906 mutex_init(&BTRFS_I(inode)->csum_mutex);
1907 mutex_init(&BTRFS_I(inode)->extent_mutex);
1908 mutex_init(&BTRFS_I(inode)->log_mutex);
1911 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1913 struct btrfs_iget_args *args = p;
1914 inode->i_ino = args->ino;
1915 init_btrfs_i(inode);
1916 BTRFS_I(inode)->root = args->root;
1920 static int btrfs_find_actor(struct inode *inode, void *opaque)
1922 struct btrfs_iget_args *args = opaque;
1923 return (args->ino == inode->i_ino &&
1924 args->root == BTRFS_I(inode)->root);
1927 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1928 struct btrfs_root *root, int wait)
1930 struct inode *inode;
1931 struct btrfs_iget_args args;
1932 args.ino = objectid;
1936 inode = ilookup5(s, objectid, btrfs_find_actor,
1939 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
1945 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1946 struct btrfs_root *root)
1948 struct inode *inode;
1949 struct btrfs_iget_args args;
1950 args.ino = objectid;
1953 inode = iget5_locked(s, objectid, btrfs_find_actor,
1954 btrfs_init_locked_inode,
1959 /* Get an inode object given its location and corresponding root.
1960 * Returns in *is_new if the inode was read from disk
1962 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1963 struct btrfs_root *root, int *is_new)
1965 struct inode *inode;
1967 inode = btrfs_iget_locked(s, location->objectid, root);
1969 return ERR_PTR(-EACCES);
1971 if (inode->i_state & I_NEW) {
1972 BTRFS_I(inode)->root = root;
1973 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1974 btrfs_read_locked_inode(inode);
1975 unlock_new_inode(inode);
1986 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1987 struct nameidata *nd)
1989 struct inode * inode;
1990 struct btrfs_inode *bi = BTRFS_I(dir);
1991 struct btrfs_root *root = bi->root;
1992 struct btrfs_root *sub_root = root;
1993 struct btrfs_key location;
1994 int ret, new, do_orphan = 0;
1996 if (dentry->d_name.len > BTRFS_NAME_LEN)
1997 return ERR_PTR(-ENAMETOOLONG);
1999 ret = btrfs_inode_by_name(dir, dentry, &location);
2002 return ERR_PTR(ret);
2005 if (location.objectid) {
2006 ret = fixup_tree_root_location(root, &location, &sub_root,
2009 return ERR_PTR(ret);
2011 return ERR_PTR(-ENOENT);
2012 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2014 return ERR_CAST(inode);
2016 /* the inode and parent dir are two different roots */
2017 if (new && root != sub_root) {
2019 sub_root->inode = inode;
2024 if (unlikely(do_orphan))
2025 btrfs_orphan_cleanup(sub_root);
2027 return d_splice_alias(inode, dentry);
2030 static unsigned char btrfs_filetype_table[] = {
2031 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2034 static int btrfs_real_readdir(struct file *filp, void *dirent,
2037 struct inode *inode = filp->f_dentry->d_inode;
2038 struct btrfs_root *root = BTRFS_I(inode)->root;
2039 struct btrfs_item *item;
2040 struct btrfs_dir_item *di;
2041 struct btrfs_key key;
2042 struct btrfs_key found_key;
2043 struct btrfs_path *path;
2046 struct extent_buffer *leaf;
2049 unsigned char d_type;
2054 int key_type = BTRFS_DIR_INDEX_KEY;
2059 /* FIXME, use a real flag for deciding about the key type */
2060 if (root->fs_info->tree_root == root)
2061 key_type = BTRFS_DIR_ITEM_KEY;
2063 /* special case for "." */
2064 if (filp->f_pos == 0) {
2065 over = filldir(dirent, ".", 1,
2072 /* special case for .., just use the back ref */
2073 if (filp->f_pos == 1) {
2074 u64 pino = parent_ino(filp->f_path.dentry);
2075 over = filldir(dirent, "..", 2,
2082 path = btrfs_alloc_path();
2085 btrfs_set_key_type(&key, key_type);
2086 key.offset = filp->f_pos;
2087 key.objectid = inode->i_ino;
2089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2095 leaf = path->nodes[0];
2096 nritems = btrfs_header_nritems(leaf);
2097 slot = path->slots[0];
2098 if (advance || slot >= nritems) {
2099 if (slot >= nritems - 1) {
2100 ret = btrfs_next_leaf(root, path);
2103 leaf = path->nodes[0];
2104 nritems = btrfs_header_nritems(leaf);
2105 slot = path->slots[0];
2112 item = btrfs_item_nr(leaf, slot);
2113 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2115 if (found_key.objectid != key.objectid)
2117 if (btrfs_key_type(&found_key) != key_type)
2119 if (found_key.offset < filp->f_pos)
2122 filp->f_pos = found_key.offset;
2124 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2126 di_total = btrfs_item_size(leaf, item);
2128 while (di_cur < di_total) {
2129 struct btrfs_key location;
2131 name_len = btrfs_dir_name_len(leaf, di);
2132 if (name_len <= sizeof(tmp_name)) {
2133 name_ptr = tmp_name;
2135 name_ptr = kmalloc(name_len, GFP_NOFS);
2141 read_extent_buffer(leaf, name_ptr,
2142 (unsigned long)(di + 1), name_len);
2144 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2145 btrfs_dir_item_key_to_cpu(leaf, di, &location);
2146 over = filldir(dirent, name_ptr, name_len,
2147 found_key.offset, location.objectid,
2150 if (name_ptr != tmp_name)
2156 di_len = btrfs_dir_name_len(leaf, di) +
2157 btrfs_dir_data_len(leaf, di) + sizeof(*di);
2159 di = (struct btrfs_dir_item *)((char *)di + di_len);
2163 /* Reached end of directory/root. Bump pos past the last item. */
2164 if (key_type == BTRFS_DIR_INDEX_KEY)
2165 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2171 btrfs_free_path(path);
2175 int btrfs_write_inode(struct inode *inode, int wait)
2177 struct btrfs_root *root = BTRFS_I(inode)->root;
2178 struct btrfs_trans_handle *trans;
2181 if (root->fs_info->closing > 1)
2185 trans = btrfs_join_transaction(root, 1);
2186 btrfs_set_trans_block_group(trans, inode);
2187 ret = btrfs_commit_transaction(trans, root);
2193 * This is somewhat expensive, updating the tree every time the
2194 * inode changes. But, it is most likely to find the inode in cache.
2195 * FIXME, needs more benchmarking...there are no reasons other than performance
2196 * to keep or drop this code.
2198 void btrfs_dirty_inode(struct inode *inode)
2200 struct btrfs_root *root = BTRFS_I(inode)->root;
2201 struct btrfs_trans_handle *trans;
2203 trans = btrfs_join_transaction(root, 1);
2204 btrfs_set_trans_block_group(trans, inode);
2205 btrfs_update_inode(trans, root, inode);
2206 btrfs_end_transaction(trans, root);
2210 * find the highest existing sequence number in a directory
2211 * and then set the in-memory index_cnt variable to reflect
2212 * free sequence numbers
2214 static int btrfs_set_inode_index_count(struct inode *inode)
2216 struct btrfs_root *root = BTRFS_I(inode)->root;
2217 struct btrfs_key key, found_key;
2218 struct btrfs_path *path;
2219 struct extent_buffer *leaf;
2222 key.objectid = inode->i_ino;
2223 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2224 key.offset = (u64)-1;
2226 path = btrfs_alloc_path();
2230 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2233 /* FIXME: we should be able to handle this */
2239 * MAGIC NUMBER EXPLANATION:
2240 * since we search a directory based on f_pos we have to start at 2
2241 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2242 * else has to start at 2
2244 if (path->slots[0] == 0) {
2245 BTRFS_I(inode)->index_cnt = 2;
2251 leaf = path->nodes[0];
2252 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2254 if (found_key.objectid != inode->i_ino ||
2255 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2256 BTRFS_I(inode)->index_cnt = 2;
2260 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2262 btrfs_free_path(path);
2267 * helper to find a free sequence number in a given directory. This current
2268 * code is very simple, later versions will do smarter things in the btree
2270 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2275 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2276 ret = btrfs_set_inode_index_count(dir);
2282 *index = BTRFS_I(dir)->index_cnt;
2283 BTRFS_I(dir)->index_cnt++;
2288 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *root,
2291 const char *name, int name_len,
2294 struct btrfs_block_group_cache *group,
2295 int mode, u64 *index)
2297 struct inode *inode;
2298 struct btrfs_inode_item *inode_item;
2299 struct btrfs_block_group_cache *new_inode_group;
2300 struct btrfs_key *location;
2301 struct btrfs_path *path;
2302 struct btrfs_inode_ref *ref;
2303 struct btrfs_key key[2];
2309 path = btrfs_alloc_path();
2312 inode = new_inode(root->fs_info->sb);
2314 return ERR_PTR(-ENOMEM);
2317 ret = btrfs_set_inode_index(dir, inode, index);
2319 return ERR_PTR(ret);
2322 * index_cnt is ignored for everything but a dir,
2323 * btrfs_get_inode_index_count has an explanation for the magic
2326 init_btrfs_i(inode);
2327 BTRFS_I(inode)->index_cnt = 2;
2328 BTRFS_I(inode)->root = root;
2329 BTRFS_I(inode)->generation = trans->transid;
2335 new_inode_group = btrfs_find_block_group(root, group, 0,
2336 BTRFS_BLOCK_GROUP_METADATA, owner);
2337 if (!new_inode_group) {
2338 printk("find_block group failed\n");
2339 new_inode_group = group;
2341 BTRFS_I(inode)->block_group = new_inode_group;
2343 key[0].objectid = objectid;
2344 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2347 key[1].objectid = objectid;
2348 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2349 key[1].offset = ref_objectid;
2351 sizes[0] = sizeof(struct btrfs_inode_item);
2352 sizes[1] = name_len + sizeof(*ref);
2354 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2358 if (objectid > root->highest_inode)
2359 root->highest_inode = objectid;
2361 inode->i_uid = current->fsuid;
2362 inode->i_gid = current->fsgid;
2363 inode->i_mode = mode;
2364 inode->i_ino = objectid;
2365 inode->i_blocks = 0;
2366 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2367 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2368 struct btrfs_inode_item);
2369 fill_inode_item(trans, path->nodes[0], inode_item, inode);
2371 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2372 struct btrfs_inode_ref);
2373 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2374 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2375 ptr = (unsigned long)(ref + 1);
2376 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2378 btrfs_mark_buffer_dirty(path->nodes[0]);
2379 btrfs_free_path(path);
2381 location = &BTRFS_I(inode)->location;
2382 location->objectid = objectid;
2383 location->offset = 0;
2384 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2386 insert_inode_hash(inode);
2390 BTRFS_I(dir)->index_cnt--;
2391 btrfs_free_path(path);
2392 return ERR_PTR(ret);
2395 static inline u8 btrfs_inode_type(struct inode *inode)
2397 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2401 * utility function to add 'inode' into 'parent_inode' with
2402 * a give name and a given sequence number.
2403 * if 'add_backref' is true, also insert a backref from the
2404 * inode to the parent directory.
2406 int btrfs_add_link(struct btrfs_trans_handle *trans,
2407 struct inode *parent_inode, struct inode *inode,
2408 const char *name, int name_len, int add_backref, u64 index)
2411 struct btrfs_key key;
2412 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
2414 key.objectid = inode->i_ino;
2415 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2418 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2419 parent_inode->i_ino,
2420 &key, btrfs_inode_type(inode),
2424 ret = btrfs_insert_inode_ref(trans, root,
2427 parent_inode->i_ino,
2430 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2432 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2433 ret = btrfs_update_inode(trans, root, parent_inode);
2438 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2439 struct dentry *dentry, struct inode *inode,
2440 int backref, u64 index)
2442 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2443 inode, dentry->d_name.name,
2444 dentry->d_name.len, backref, index);
2446 d_instantiate(dentry, inode);
2454 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2455 int mode, dev_t rdev)
2457 struct btrfs_trans_handle *trans;
2458 struct btrfs_root *root = BTRFS_I(dir)->root;
2459 struct inode *inode = NULL;
2463 unsigned long nr = 0;
2466 if (!new_valid_dev(rdev))
2469 err = btrfs_check_free_space(root, 1, 0);
2473 trans = btrfs_start_transaction(root, 1);
2474 btrfs_set_trans_block_group(trans, dir);
2476 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2482 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2484 dentry->d_parent->d_inode->i_ino, objectid,
2485 BTRFS_I(dir)->block_group, mode, &index);
2486 err = PTR_ERR(inode);
2490 err = btrfs_init_acl(inode, dir);
2496 btrfs_set_trans_block_group(trans, inode);
2497 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2501 inode->i_op = &btrfs_special_inode_operations;
2502 init_special_inode(inode, inode->i_mode, rdev);
2503 btrfs_update_inode(trans, root, inode);
2505 dir->i_sb->s_dirt = 1;
2506 btrfs_update_inode_block_group(trans, inode);
2507 btrfs_update_inode_block_group(trans, dir);
2509 nr = trans->blocks_used;
2510 btrfs_end_transaction_throttle(trans, root);
2513 inode_dec_link_count(inode);
2516 btrfs_btree_balance_dirty(root, nr);
2520 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2521 int mode, struct nameidata *nd)
2523 struct btrfs_trans_handle *trans;
2524 struct btrfs_root *root = BTRFS_I(dir)->root;
2525 struct inode *inode = NULL;
2528 unsigned long nr = 0;
2532 err = btrfs_check_free_space(root, 1, 0);
2535 trans = btrfs_start_transaction(root, 1);
2536 btrfs_set_trans_block_group(trans, dir);
2538 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2544 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2546 dentry->d_parent->d_inode->i_ino,
2547 objectid, BTRFS_I(dir)->block_group, mode,
2549 err = PTR_ERR(inode);
2553 err = btrfs_init_acl(inode, dir);
2559 btrfs_set_trans_block_group(trans, inode);
2560 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2564 inode->i_mapping->a_ops = &btrfs_aops;
2565 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2566 inode->i_fop = &btrfs_file_operations;
2567 inode->i_op = &btrfs_file_inode_operations;
2568 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2570 dir->i_sb->s_dirt = 1;
2571 btrfs_update_inode_block_group(trans, inode);
2572 btrfs_update_inode_block_group(trans, dir);
2574 nr = trans->blocks_used;
2575 btrfs_end_transaction_throttle(trans, root);
2578 inode_dec_link_count(inode);
2581 btrfs_btree_balance_dirty(root, nr);
2585 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2586 struct dentry *dentry)
2588 struct btrfs_trans_handle *trans;
2589 struct btrfs_root *root = BTRFS_I(dir)->root;
2590 struct inode *inode = old_dentry->d_inode;
2592 unsigned long nr = 0;
2596 if (inode->i_nlink == 0)
2599 btrfs_inc_nlink(inode);
2600 err = btrfs_check_free_space(root, 1, 0);
2603 err = btrfs_set_inode_index(dir, inode, &index);
2607 trans = btrfs_start_transaction(root, 1);
2609 btrfs_set_trans_block_group(trans, dir);
2610 atomic_inc(&inode->i_count);
2612 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
2617 dir->i_sb->s_dirt = 1;
2618 btrfs_update_inode_block_group(trans, dir);
2619 err = btrfs_update_inode(trans, root, inode);
2624 nr = trans->blocks_used;
2625 btrfs_end_transaction_throttle(trans, root);
2628 inode_dec_link_count(inode);
2631 btrfs_btree_balance_dirty(root, nr);
2635 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2637 struct inode *inode = NULL;
2638 struct btrfs_trans_handle *trans;
2639 struct btrfs_root *root = BTRFS_I(dir)->root;
2641 int drop_on_err = 0;
2644 unsigned long nr = 1;
2646 err = btrfs_check_free_space(root, 1, 0);
2650 trans = btrfs_start_transaction(root, 1);
2651 btrfs_set_trans_block_group(trans, dir);
2653 if (IS_ERR(trans)) {
2654 err = PTR_ERR(trans);
2658 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2664 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2666 dentry->d_parent->d_inode->i_ino, objectid,
2667 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2669 if (IS_ERR(inode)) {
2670 err = PTR_ERR(inode);
2676 err = btrfs_init_acl(inode, dir);
2680 inode->i_op = &btrfs_dir_inode_operations;
2681 inode->i_fop = &btrfs_dir_file_operations;
2682 btrfs_set_trans_block_group(trans, inode);
2684 btrfs_i_size_write(inode, 0);
2685 err = btrfs_update_inode(trans, root, inode);
2689 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2690 inode, dentry->d_name.name,
2691 dentry->d_name.len, 0, index);
2695 d_instantiate(dentry, inode);
2697 dir->i_sb->s_dirt = 1;
2698 btrfs_update_inode_block_group(trans, inode);
2699 btrfs_update_inode_block_group(trans, dir);
2702 nr = trans->blocks_used;
2703 btrfs_end_transaction_throttle(trans, root);
2708 btrfs_btree_balance_dirty(root, nr);
2712 /* helper for btfs_get_extent. Given an existing extent in the tree,
2713 * and an extent that you want to insert, deal with overlap and insert
2714 * the new extent into the tree.
2716 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2717 struct extent_map *existing,
2718 struct extent_map *em,
2719 u64 map_start, u64 map_len)
2723 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2724 start_diff = map_start - em->start;
2725 em->start = map_start;
2727 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2728 em->block_start += start_diff;
2729 return add_extent_mapping(em_tree, em);
2733 * a bit scary, this does extent mapping from logical file offset to the disk.
2734 * the ugly parts come from merging extents from the disk with the
2735 * in-ram representation. This gets more complex because of the data=ordered code,
2736 * where the in-ram extents might be locked pending data=ordered completion.
2738 * This also copies inline extents directly into the page.
2740 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2741 size_t pg_offset, u64 start, u64 len,
2747 u64 extent_start = 0;
2749 u64 objectid = inode->i_ino;
2751 struct btrfs_path *path = NULL;
2752 struct btrfs_root *root = BTRFS_I(inode)->root;
2753 struct btrfs_file_extent_item *item;
2754 struct extent_buffer *leaf;
2755 struct btrfs_key found_key;
2756 struct extent_map *em = NULL;
2757 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2759 struct btrfs_trans_handle *trans = NULL;
2762 spin_lock(&em_tree->lock);
2763 em = lookup_extent_mapping(em_tree, start, len);
2765 em->bdev = root->fs_info->fs_devices->latest_bdev;
2766 spin_unlock(&em_tree->lock);
2769 if (em->start > start || em->start + em->len <= start)
2770 free_extent_map(em);
2771 else if (em->block_start == EXTENT_MAP_INLINE && page)
2772 free_extent_map(em);
2776 em = alloc_extent_map(GFP_NOFS);
2781 em->bdev = root->fs_info->fs_devices->latest_bdev;
2782 em->start = EXTENT_MAP_HOLE;
2786 path = btrfs_alloc_path();
2790 ret = btrfs_lookup_file_extent(trans, root, path,
2791 objectid, start, trans != NULL);
2798 if (path->slots[0] == 0)
2803 leaf = path->nodes[0];
2804 item = btrfs_item_ptr(leaf, path->slots[0],
2805 struct btrfs_file_extent_item);
2806 /* are we inside the extent that was found? */
2807 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2808 found_type = btrfs_key_type(&found_key);
2809 if (found_key.objectid != objectid ||
2810 found_type != BTRFS_EXTENT_DATA_KEY) {
2814 found_type = btrfs_file_extent_type(leaf, item);
2815 extent_start = found_key.offset;
2816 if (found_type == BTRFS_FILE_EXTENT_REG) {
2817 extent_end = extent_start +
2818 btrfs_file_extent_num_bytes(leaf, item);
2820 if (start < extent_start || start >= extent_end) {
2822 if (start < extent_start) {
2823 if (start + len <= extent_start)
2825 em->len = extent_end - extent_start;
2831 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2833 em->start = extent_start;
2834 em->len = extent_end - extent_start;
2835 em->block_start = EXTENT_MAP_HOLE;
2838 bytenr += btrfs_file_extent_offset(leaf, item);
2839 em->block_start = bytenr;
2840 em->start = extent_start;
2841 em->len = extent_end - extent_start;
2843 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2848 size_t extent_offset;
2851 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2853 extent_end = (extent_start + size + root->sectorsize - 1) &
2854 ~((u64)root->sectorsize - 1);
2855 if (start < extent_start || start >= extent_end) {
2857 if (start < extent_start) {
2858 if (start + len <= extent_start)
2860 em->len = extent_end - extent_start;
2866 em->block_start = EXTENT_MAP_INLINE;
2869 em->start = extent_start;
2874 page_start = page_offset(page) + pg_offset;
2875 extent_offset = page_start - extent_start;
2876 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2877 size - extent_offset);
2878 em->start = extent_start + extent_offset;
2879 em->len = (copy_size + root->sectorsize - 1) &
2880 ~((u64)root->sectorsize - 1);
2882 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2883 if (create == 0 && !PageUptodate(page)) {
2884 read_extent_buffer(leaf, map + pg_offset, ptr,
2886 flush_dcache_page(page);
2887 } else if (create && PageUptodate(page)) {
2890 free_extent_map(em);
2892 btrfs_release_path(root, path);
2893 trans = btrfs_join_transaction(root, 1);
2896 write_extent_buffer(leaf, map + pg_offset, ptr,
2898 btrfs_mark_buffer_dirty(leaf);
2901 set_extent_uptodate(io_tree, em->start,
2902 extent_map_end(em) - 1, GFP_NOFS);
2905 printk("unkknown found_type %d\n", found_type);
2912 em->block_start = EXTENT_MAP_HOLE;
2914 btrfs_release_path(root, path);
2915 if (em->start > start || extent_map_end(em) <= start) {
2916 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2922 spin_lock(&em_tree->lock);
2923 ret = add_extent_mapping(em_tree, em);
2924 /* it is possible that someone inserted the extent into the tree
2925 * while we had the lock dropped. It is also possible that
2926 * an overlapping map exists in the tree
2928 if (ret == -EEXIST) {
2929 struct extent_map *existing;
2933 existing = lookup_extent_mapping(em_tree, start, len);
2934 if (existing && (existing->start > start ||
2935 existing->start + existing->len <= start)) {
2936 free_extent_map(existing);
2940 existing = lookup_extent_mapping(em_tree, em->start,
2943 err = merge_extent_mapping(em_tree, existing,
2946 free_extent_map(existing);
2948 free_extent_map(em);
2953 printk("failing to insert %Lu %Lu\n",
2955 free_extent_map(em);
2959 free_extent_map(em);
2964 spin_unlock(&em_tree->lock);
2967 btrfs_free_path(path);
2969 ret = btrfs_end_transaction(trans, root);
2975 free_extent_map(em);
2977 return ERR_PTR(err);
2982 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2983 const struct iovec *iov, loff_t offset,
2984 unsigned long nr_segs)
2989 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2991 return extent_bmap(mapping, iblock, btrfs_get_extent);
2994 int btrfs_readpage(struct file *file, struct page *page)
2996 struct extent_io_tree *tree;
2997 tree = &BTRFS_I(page->mapping->host)->io_tree;
2998 return extent_read_full_page(tree, page, btrfs_get_extent);
3001 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3003 struct extent_io_tree *tree;
3006 if (current->flags & PF_MEMALLOC) {
3007 redirty_page_for_writepage(wbc, page);
3011 tree = &BTRFS_I(page->mapping->host)->io_tree;
3012 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3015 int btrfs_writepages(struct address_space *mapping,
3016 struct writeback_control *wbc)
3018 struct extent_io_tree *tree;
3019 tree = &BTRFS_I(mapping->host)->io_tree;
3020 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3024 btrfs_readpages(struct file *file, struct address_space *mapping,
3025 struct list_head *pages, unsigned nr_pages)
3027 struct extent_io_tree *tree;
3028 tree = &BTRFS_I(mapping->host)->io_tree;
3029 return extent_readpages(tree, mapping, pages, nr_pages,
3032 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3034 struct extent_io_tree *tree;
3035 struct extent_map_tree *map;
3038 tree = &BTRFS_I(page->mapping->host)->io_tree;
3039 map = &BTRFS_I(page->mapping->host)->extent_tree;
3040 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3042 ClearPagePrivate(page);
3043 set_page_private(page, 0);
3044 page_cache_release(page);
3049 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3051 if (PageWriteback(page) || PageDirty(page))
3053 return __btrfs_releasepage(page, gfp_flags);
3056 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3058 struct extent_io_tree *tree;
3059 struct btrfs_ordered_extent *ordered;
3060 u64 page_start = page_offset(page);
3061 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3063 wait_on_page_writeback(page);
3064 tree = &BTRFS_I(page->mapping->host)->io_tree;
3066 btrfs_releasepage(page, GFP_NOFS);
3070 lock_extent(tree, page_start, page_end, GFP_NOFS);
3071 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3075 * IO on this page will never be started, so we need
3076 * to account for any ordered extents now
3078 clear_extent_bit(tree, page_start, page_end,
3079 EXTENT_DIRTY | EXTENT_DELALLOC |
3080 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3081 btrfs_finish_ordered_io(page->mapping->host,
3082 page_start, page_end);
3083 btrfs_put_ordered_extent(ordered);
3084 lock_extent(tree, page_start, page_end, GFP_NOFS);
3086 clear_extent_bit(tree, page_start, page_end,
3087 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3090 __btrfs_releasepage(page, GFP_NOFS);
3092 ClearPageChecked(page);
3093 if (PagePrivate(page)) {
3094 ClearPagePrivate(page);
3095 set_page_private(page, 0);
3096 page_cache_release(page);
3101 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3102 * called from a page fault handler when a page is first dirtied. Hence we must
3103 * be careful to check for EOF conditions here. We set the page up correctly
3104 * for a written page which means we get ENOSPC checking when writing into
3105 * holes and correct delalloc and unwritten extent mapping on filesystems that
3106 * support these features.
3108 * We are not allowed to take the i_mutex here so we have to play games to
3109 * protect against truncate races as the page could now be beyond EOF. Because
3110 * vmtruncate() writes the inode size before removing pages, once we have the
3111 * page lock we can determine safely if the page is beyond EOF. If it is not
3112 * beyond EOF, then the page is guaranteed safe against truncation until we
3115 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3117 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3118 struct btrfs_root *root = BTRFS_I(inode)->root;
3119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3120 struct btrfs_ordered_extent *ordered;
3122 unsigned long zero_start;
3128 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3135 size = i_size_read(inode);
3136 page_start = page_offset(page);
3137 page_end = page_start + PAGE_CACHE_SIZE - 1;
3139 if ((page->mapping != inode->i_mapping) ||
3140 (page_start >= size)) {
3141 /* page got truncated out from underneath us */
3144 wait_on_page_writeback(page);
3146 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3147 set_page_extent_mapped(page);
3150 * we can't set the delalloc bits if there are pending ordered
3151 * extents. Drop our locks and wait for them to finish
3153 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3155 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3157 btrfs_start_ordered_extent(inode, ordered, 1);
3158 btrfs_put_ordered_extent(ordered);
3162 btrfs_set_extent_delalloc(inode, page_start, page_end);
3165 /* page is wholly or partially inside EOF */
3166 if (page_start + PAGE_CACHE_SIZE > size)
3167 zero_start = size & ~PAGE_CACHE_MASK;
3169 zero_start = PAGE_CACHE_SIZE;
3171 if (zero_start != PAGE_CACHE_SIZE) {
3173 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3174 flush_dcache_page(page);
3177 ClearPageChecked(page);
3178 set_page_dirty(page);
3179 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3187 static void btrfs_truncate(struct inode *inode)
3189 struct btrfs_root *root = BTRFS_I(inode)->root;
3191 struct btrfs_trans_handle *trans;
3193 u64 mask = root->sectorsize - 1;
3195 if (!S_ISREG(inode->i_mode))
3197 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3200 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3201 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3203 trans = btrfs_start_transaction(root, 1);
3204 btrfs_set_trans_block_group(trans, inode);
3205 btrfs_i_size_write(inode, inode->i_size);
3207 ret = btrfs_orphan_add(trans, inode);
3210 /* FIXME, add redo link to tree so we don't leak on crash */
3211 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
3212 BTRFS_EXTENT_DATA_KEY);
3213 btrfs_update_inode(trans, root, inode);
3215 ret = btrfs_orphan_del(trans, inode);
3219 nr = trans->blocks_used;
3220 ret = btrfs_end_transaction_throttle(trans, root);
3222 btrfs_btree_balance_dirty(root, nr);
3226 * Invalidate a single dcache entry at the root of the filesystem.
3227 * Needed after creation of snapshot or subvolume.
3229 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3232 struct dentry *alias, *entry;
3235 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3239 /* change me if btrfs ever gets a d_hash operation */
3240 qstr.hash = full_name_hash(qstr.name, qstr.len);
3241 entry = d_lookup(alias, &qstr);
3244 d_invalidate(entry);
3251 * create a new subvolume directory/inode (helper for the ioctl).
3253 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3254 struct btrfs_trans_handle *trans, u64 new_dirid,
3255 struct btrfs_block_group_cache *block_group)
3257 struct inode *inode;
3260 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3261 new_dirid, block_group, S_IFDIR | 0700, &index);
3263 return PTR_ERR(inode);
3264 inode->i_op = &btrfs_dir_inode_operations;
3265 inode->i_fop = &btrfs_dir_file_operations;
3266 new_root->inode = inode;
3269 btrfs_i_size_write(inode, 0);
3271 return btrfs_update_inode(trans, new_root, inode);
3274 /* helper function for file defrag and space balancing. This
3275 * forces readahead on a given range of bytes in an inode
3277 unsigned long btrfs_force_ra(struct address_space *mapping,
3278 struct file_ra_state *ra, struct file *file,
3279 pgoff_t offset, pgoff_t last_index)
3281 pgoff_t req_size = last_index - offset + 1;
3283 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3284 return offset + req_size;
3287 struct inode *btrfs_alloc_inode(struct super_block *sb)
3289 struct btrfs_inode *ei;
3291 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3295 ei->logged_trans = 0;
3296 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3297 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3298 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3299 INIT_LIST_HEAD(&ei->i_orphan);
3300 return &ei->vfs_inode;
3303 void btrfs_destroy_inode(struct inode *inode)
3305 struct btrfs_ordered_extent *ordered;
3306 WARN_ON(!list_empty(&inode->i_dentry));
3307 WARN_ON(inode->i_data.nrpages);
3309 if (BTRFS_I(inode)->i_acl &&
3310 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3311 posix_acl_release(BTRFS_I(inode)->i_acl);
3312 if (BTRFS_I(inode)->i_default_acl &&
3313 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3314 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3316 spin_lock(&BTRFS_I(inode)->root->list_lock);
3317 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3318 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3319 " list\n", inode->i_ino);
3322 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3325 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3329 printk("found ordered extent %Lu %Lu\n",
3330 ordered->file_offset, ordered->len);
3331 btrfs_remove_ordered_extent(inode, ordered);
3332 btrfs_put_ordered_extent(ordered);
3333 btrfs_put_ordered_extent(ordered);
3336 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
3337 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3340 static void init_once(void *foo)
3342 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3344 inode_init_once(&ei->vfs_inode);
3347 void btrfs_destroy_cachep(void)
3349 if (btrfs_inode_cachep)
3350 kmem_cache_destroy(btrfs_inode_cachep);
3351 if (btrfs_trans_handle_cachep)
3352 kmem_cache_destroy(btrfs_trans_handle_cachep);
3353 if (btrfs_transaction_cachep)
3354 kmem_cache_destroy(btrfs_transaction_cachep);
3355 if (btrfs_bit_radix_cachep)
3356 kmem_cache_destroy(btrfs_bit_radix_cachep);
3357 if (btrfs_path_cachep)
3358 kmem_cache_destroy(btrfs_path_cachep);
3361 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3362 unsigned long extra_flags,
3363 void (*ctor)(void *))
3365 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3366 SLAB_MEM_SPREAD | extra_flags), ctor);
3369 int btrfs_init_cachep(void)
3371 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3372 sizeof(struct btrfs_inode),
3374 if (!btrfs_inode_cachep)
3376 btrfs_trans_handle_cachep =
3377 btrfs_cache_create("btrfs_trans_handle_cache",
3378 sizeof(struct btrfs_trans_handle),
3380 if (!btrfs_trans_handle_cachep)
3382 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3383 sizeof(struct btrfs_transaction),
3385 if (!btrfs_transaction_cachep)
3387 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3388 sizeof(struct btrfs_path),
3390 if (!btrfs_path_cachep)
3392 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3393 SLAB_DESTROY_BY_RCU, NULL);
3394 if (!btrfs_bit_radix_cachep)
3398 btrfs_destroy_cachep();
3402 static int btrfs_getattr(struct vfsmount *mnt,
3403 struct dentry *dentry, struct kstat *stat)
3405 struct inode *inode = dentry->d_inode;
3406 generic_fillattr(inode, stat);
3407 stat->blksize = PAGE_CACHE_SIZE;
3408 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3412 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3413 struct inode * new_dir,struct dentry *new_dentry)
3415 struct btrfs_trans_handle *trans;
3416 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3417 struct inode *new_inode = new_dentry->d_inode;
3418 struct inode *old_inode = old_dentry->d_inode;
3419 struct timespec ctime = CURRENT_TIME;
3423 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3424 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3428 ret = btrfs_check_free_space(root, 1, 0);
3432 trans = btrfs_start_transaction(root, 1);
3434 btrfs_set_trans_block_group(trans, new_dir);
3436 btrfs_inc_nlink(old_dentry->d_inode);
3437 old_dir->i_ctime = old_dir->i_mtime = ctime;
3438 new_dir->i_ctime = new_dir->i_mtime = ctime;
3439 old_inode->i_ctime = ctime;
3441 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3442 old_dentry->d_name.name,
3443 old_dentry->d_name.len);
3448 new_inode->i_ctime = CURRENT_TIME;
3449 ret = btrfs_unlink_inode(trans, root, new_dir,
3450 new_dentry->d_inode,
3451 new_dentry->d_name.name,
3452 new_dentry->d_name.len);
3455 if (new_inode->i_nlink == 0) {
3456 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
3462 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
3466 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3467 old_inode, new_dentry->d_name.name,
3468 new_dentry->d_name.len, 1, index);
3473 btrfs_end_transaction_throttle(trans, root);
3479 * some fairly slow code that needs optimization. This walks the list
3480 * of all the inodes with pending delalloc and forces them to disk.
3482 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3484 struct list_head *head = &root->fs_info->delalloc_inodes;
3485 struct btrfs_inode *binode;
3486 struct inode *inode;
3487 unsigned long flags;
3489 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3490 while(!list_empty(head)) {
3491 binode = list_entry(head->next, struct btrfs_inode,
3493 inode = igrab(&binode->vfs_inode);
3495 list_del_init(&binode->delalloc_inodes);
3496 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3498 filemap_flush(inode->i_mapping);
3502 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3504 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3506 /* the filemap_flush will queue IO into the worker threads, but
3507 * we have to make sure the IO is actually started and that
3508 * ordered extents get created before we return
3510 atomic_inc(&root->fs_info->async_submit_draining);
3511 while(atomic_read(&root->fs_info->nr_async_submits)) {
3512 wait_event(root->fs_info->async_submit_wait,
3513 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3515 atomic_dec(&root->fs_info->async_submit_draining);
3519 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3520 const char *symname)
3522 struct btrfs_trans_handle *trans;
3523 struct btrfs_root *root = BTRFS_I(dir)->root;
3524 struct btrfs_path *path;
3525 struct btrfs_key key;
3526 struct inode *inode = NULL;
3534 struct btrfs_file_extent_item *ei;
3535 struct extent_buffer *leaf;
3536 unsigned long nr = 0;
3538 name_len = strlen(symname) + 1;
3539 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3540 return -ENAMETOOLONG;
3542 err = btrfs_check_free_space(root, 1, 0);
3546 trans = btrfs_start_transaction(root, 1);
3547 btrfs_set_trans_block_group(trans, dir);
3549 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3555 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3557 dentry->d_parent->d_inode->i_ino, objectid,
3558 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3560 err = PTR_ERR(inode);
3564 err = btrfs_init_acl(inode, dir);
3570 btrfs_set_trans_block_group(trans, inode);
3571 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3575 inode->i_mapping->a_ops = &btrfs_aops;
3576 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3577 inode->i_fop = &btrfs_file_operations;
3578 inode->i_op = &btrfs_file_inode_operations;
3579 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3581 dir->i_sb->s_dirt = 1;
3582 btrfs_update_inode_block_group(trans, inode);
3583 btrfs_update_inode_block_group(trans, dir);
3587 path = btrfs_alloc_path();
3589 key.objectid = inode->i_ino;
3591 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3592 datasize = btrfs_file_extent_calc_inline_size(name_len);
3593 err = btrfs_insert_empty_item(trans, root, path, &key,
3599 leaf = path->nodes[0];
3600 ei = btrfs_item_ptr(leaf, path->slots[0],
3601 struct btrfs_file_extent_item);
3602 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3603 btrfs_set_file_extent_type(leaf, ei,
3604 BTRFS_FILE_EXTENT_INLINE);
3605 ptr = btrfs_file_extent_inline_start(ei);
3606 write_extent_buffer(leaf, symname, ptr, name_len);
3607 btrfs_mark_buffer_dirty(leaf);
3608 btrfs_free_path(path);
3610 inode->i_op = &btrfs_symlink_inode_operations;
3611 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3612 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3613 btrfs_i_size_write(inode, name_len - 1);
3614 err = btrfs_update_inode(trans, root, inode);
3619 nr = trans->blocks_used;
3620 btrfs_end_transaction_throttle(trans, root);
3623 inode_dec_link_count(inode);
3626 btrfs_btree_balance_dirty(root, nr);
3630 static int btrfs_set_page_dirty(struct page *page)
3632 return __set_page_dirty_nobuffers(page);
3635 static int btrfs_permission(struct inode *inode, int mask)
3637 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3639 return generic_permission(inode, mask, btrfs_check_acl);
3642 static struct inode_operations btrfs_dir_inode_operations = {
3643 .lookup = btrfs_lookup,
3644 .create = btrfs_create,
3645 .unlink = btrfs_unlink,
3647 .mkdir = btrfs_mkdir,
3648 .rmdir = btrfs_rmdir,
3649 .rename = btrfs_rename,
3650 .symlink = btrfs_symlink,
3651 .setattr = btrfs_setattr,
3652 .mknod = btrfs_mknod,
3653 .setxattr = btrfs_setxattr,
3654 .getxattr = btrfs_getxattr,
3655 .listxattr = btrfs_listxattr,
3656 .removexattr = btrfs_removexattr,
3657 .permission = btrfs_permission,
3659 static struct inode_operations btrfs_dir_ro_inode_operations = {
3660 .lookup = btrfs_lookup,
3661 .permission = btrfs_permission,
3663 static struct file_operations btrfs_dir_file_operations = {
3664 .llseek = generic_file_llseek,
3665 .read = generic_read_dir,
3666 .readdir = btrfs_real_readdir,
3667 .unlocked_ioctl = btrfs_ioctl,
3668 #ifdef CONFIG_COMPAT
3669 .compat_ioctl = btrfs_ioctl,
3671 .release = btrfs_release_file,
3672 .fsync = btrfs_sync_file,
3675 static struct extent_io_ops btrfs_extent_io_ops = {
3676 .fill_delalloc = run_delalloc_range,
3677 .submit_bio_hook = btrfs_submit_bio_hook,
3678 .merge_bio_hook = btrfs_merge_bio_hook,
3679 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3680 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3681 .writepage_start_hook = btrfs_writepage_start_hook,
3682 .readpage_io_failed_hook = btrfs_io_failed_hook,
3683 .set_bit_hook = btrfs_set_bit_hook,
3684 .clear_bit_hook = btrfs_clear_bit_hook,
3687 static struct address_space_operations btrfs_aops = {
3688 .readpage = btrfs_readpage,
3689 .writepage = btrfs_writepage,
3690 .writepages = btrfs_writepages,
3691 .readpages = btrfs_readpages,
3692 .sync_page = block_sync_page,
3694 .direct_IO = btrfs_direct_IO,
3695 .invalidatepage = btrfs_invalidatepage,
3696 .releasepage = btrfs_releasepage,
3697 .set_page_dirty = btrfs_set_page_dirty,
3700 static struct address_space_operations btrfs_symlink_aops = {
3701 .readpage = btrfs_readpage,
3702 .writepage = btrfs_writepage,
3703 .invalidatepage = btrfs_invalidatepage,
3704 .releasepage = btrfs_releasepage,
3707 static struct inode_operations btrfs_file_inode_operations = {
3708 .truncate = btrfs_truncate,
3709 .getattr = btrfs_getattr,
3710 .setattr = btrfs_setattr,
3711 .setxattr = btrfs_setxattr,
3712 .getxattr = btrfs_getxattr,
3713 .listxattr = btrfs_listxattr,
3714 .removexattr = btrfs_removexattr,
3715 .permission = btrfs_permission,
3717 static struct inode_operations btrfs_special_inode_operations = {
3718 .getattr = btrfs_getattr,
3719 .setattr = btrfs_setattr,
3720 .permission = btrfs_permission,
3721 .setxattr = btrfs_setxattr,
3722 .getxattr = btrfs_getxattr,
3723 .listxattr = btrfs_listxattr,
3724 .removexattr = btrfs_removexattr,
3726 static struct inode_operations btrfs_symlink_inode_operations = {
3727 .readlink = generic_readlink,
3728 .follow_link = page_follow_link_light,
3729 .put_link = page_put_link,
3730 .permission = btrfs_permission,