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"
51 #include "ref-cache.h"
53 struct btrfs_iget_args {
55 struct btrfs_root *root;
58 static struct inode_operations btrfs_dir_inode_operations;
59 static struct inode_operations btrfs_symlink_inode_operations;
60 static struct inode_operations btrfs_dir_ro_inode_operations;
61 static struct inode_operations btrfs_special_inode_operations;
62 static struct inode_operations btrfs_file_inode_operations;
63 static struct address_space_operations btrfs_aops;
64 static struct address_space_operations btrfs_symlink_aops;
65 static struct file_operations btrfs_dir_file_operations;
66 static struct extent_io_ops btrfs_extent_io_ops;
68 static struct kmem_cache *btrfs_inode_cachep;
69 struct kmem_cache *btrfs_trans_handle_cachep;
70 struct kmem_cache *btrfs_transaction_cachep;
71 struct kmem_cache *btrfs_bit_radix_cachep;
72 struct kmem_cache *btrfs_path_cachep;
75 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85 static void btrfs_truncate(struct inode *inode);
88 * a very lame attempt at stopping writes when the FS is 85% full. There
89 * are countless ways this is incorrect, but it is better than nothing.
91 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
100 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
101 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
102 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
110 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
112 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
117 * when extent_io.c finds a delayed allocation range in the file,
118 * the call backs end up in this code. The basic idea is to
119 * allocate extents on disk for the range, and create ordered data structs
120 * in ram to track those extents.
122 static int cow_file_range(struct inode *inode, u64 start, u64 end)
124 struct btrfs_root *root = BTRFS_I(inode)->root;
125 struct btrfs_trans_handle *trans;
129 u64 blocksize = root->sectorsize;
131 struct btrfs_key ins;
132 struct extent_map *em;
133 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
136 trans = btrfs_join_transaction(root, 1);
138 btrfs_set_trans_block_group(trans, inode);
140 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
141 num_bytes = max(blocksize, num_bytes);
142 orig_num_bytes = num_bytes;
144 if (alloc_hint == EXTENT_MAP_INLINE)
147 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
148 mutex_lock(&BTRFS_I(inode)->extent_mutex);
149 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
150 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
152 while(num_bytes > 0) {
153 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
154 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
155 root->sectorsize, 0, alloc_hint,
161 em = alloc_extent_map(GFP_NOFS);
163 em->len = ins.offset;
164 em->block_start = ins.objectid;
165 em->bdev = root->fs_info->fs_devices->latest_bdev;
166 mutex_lock(&BTRFS_I(inode)->extent_mutex);
167 set_bit(EXTENT_FLAG_PINNED, &em->flags);
169 spin_lock(&em_tree->lock);
170 ret = add_extent_mapping(em_tree, em);
171 spin_unlock(&em_tree->lock);
172 if (ret != -EEXIST) {
176 btrfs_drop_extent_cache(inode, start,
177 start + ins.offset - 1, 0);
179 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
181 cur_alloc_size = ins.offset;
182 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
185 if (num_bytes < cur_alloc_size) {
186 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
190 num_bytes -= cur_alloc_size;
191 alloc_hint = ins.objectid + ins.offset;
192 start += cur_alloc_size;
195 btrfs_end_transaction(trans, root);
200 * when nowcow writeback call back. This checks for snapshots or COW copies
201 * of the extents that exist in the file, and COWs the file as required.
203 * If no cow copies or snapshots exist, we write directly to the existing
206 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
213 struct btrfs_root *root = BTRFS_I(inode)->root;
214 struct btrfs_block_group_cache *block_group;
215 struct btrfs_trans_handle *trans;
216 struct extent_buffer *leaf;
218 struct btrfs_path *path;
219 struct btrfs_file_extent_item *item;
222 struct btrfs_key found_key;
224 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
225 path = btrfs_alloc_path();
227 trans = btrfs_join_transaction(root, 1);
230 ret = btrfs_lookup_file_extent(NULL, root, path,
231 inode->i_ino, start, 0);
238 if (path->slots[0] == 0)
243 leaf = path->nodes[0];
244 item = btrfs_item_ptr(leaf, path->slots[0],
245 struct btrfs_file_extent_item);
247 /* are we inside the extent that was found? */
248 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
249 found_type = btrfs_key_type(&found_key);
250 if (found_key.objectid != inode->i_ino ||
251 found_type != BTRFS_EXTENT_DATA_KEY)
254 found_type = btrfs_file_extent_type(leaf, item);
255 extent_start = found_key.offset;
256 if (found_type == BTRFS_FILE_EXTENT_REG) {
257 u64 extent_num_bytes;
259 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
260 extent_end = extent_start + extent_num_bytes;
263 if (loops && start != extent_start)
266 if (start < extent_start || start >= extent_end)
269 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
273 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
276 * we may be called by the resizer, make sure we're inside
277 * the limits of the FS
279 block_group = btrfs_lookup_block_group(root->fs_info,
281 if (!block_group || block_group->ro)
284 bytenr += btrfs_file_extent_offset(leaf, item);
285 extent_num_bytes = min(end + 1, extent_end) - start;
286 ret = btrfs_add_ordered_extent(inode, start, bytenr,
287 extent_num_bytes, 1);
293 btrfs_release_path(root, path);
301 btrfs_end_transaction(trans, root);
302 btrfs_free_path(path);
303 return cow_file_range(inode, start, end);
307 btrfs_end_transaction(trans, root);
308 btrfs_free_path(path);
313 * extent_io.c call back to do delayed allocation processing
315 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
317 struct btrfs_root *root = BTRFS_I(inode)->root;
320 if (btrfs_test_opt(root, NODATACOW) ||
321 btrfs_test_flag(inode, NODATACOW))
322 ret = run_delalloc_nocow(inode, start, end);
324 ret = cow_file_range(inode, start, end);
330 * extent_io.c set_bit_hook, used to track delayed allocation
331 * bytes in this file, and to maintain the list of inodes that
332 * have pending delalloc work to be done.
334 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
335 unsigned long old, unsigned long bits)
338 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
339 struct btrfs_root *root = BTRFS_I(inode)->root;
340 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
341 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
342 root->fs_info->delalloc_bytes += end - start + 1;
343 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
344 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
345 &root->fs_info->delalloc_inodes);
347 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
353 * extent_io.c clear_bit_hook, see set_bit_hook for why
355 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
356 unsigned long old, unsigned long bits)
358 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
359 struct btrfs_root *root = BTRFS_I(inode)->root;
362 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
363 if (end - start + 1 > root->fs_info->delalloc_bytes) {
364 printk("warning: delalloc account %Lu %Lu\n",
365 end - start + 1, root->fs_info->delalloc_bytes);
366 root->fs_info->delalloc_bytes = 0;
367 BTRFS_I(inode)->delalloc_bytes = 0;
369 root->fs_info->delalloc_bytes -= end - start + 1;
370 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
372 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
373 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
374 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
376 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
382 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
383 * we don't create bios that span stripes or chunks
385 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
386 size_t size, struct bio *bio)
388 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
389 struct btrfs_mapping_tree *map_tree;
390 u64 logical = (u64)bio->bi_sector << 9;
395 length = bio->bi_size;
396 map_tree = &root->fs_info->mapping_tree;
398 ret = btrfs_map_block(map_tree, READ, logical,
399 &map_length, NULL, 0);
401 if (map_length < length + size) {
408 * in order to insert checksums into the metadata in large chunks,
409 * we wait until bio submission time. All the pages in the bio are
410 * checksummed and sums are attached onto the ordered extent record.
412 * At IO completion time the cums attached on the ordered extent record
413 * are inserted into the btree
415 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
418 struct btrfs_root *root = BTRFS_I(inode)->root;
421 ret = btrfs_csum_one_bio(root, inode, bio);
424 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
428 * extent_io.c submission hook. This does the right thing for csum calculation on write,
429 * or reading the csums from the tree before a read
431 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
434 struct btrfs_root *root = BTRFS_I(inode)->root;
437 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
440 if (btrfs_test_opt(root, NODATASUM) ||
441 btrfs_test_flag(inode, NODATASUM)) {
445 if (!(rw & (1 << BIO_RW))) {
446 btrfs_lookup_bio_sums(root, inode, bio);
449 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
450 inode, rw, bio, mirror_num,
451 __btrfs_submit_bio_hook);
453 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
457 * given a list of ordered sums record them in the inode. This happens
458 * at IO completion time based on sums calculated at bio submission time.
460 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
461 struct inode *inode, u64 file_offset,
462 struct list_head *list)
464 struct list_head *cur;
465 struct btrfs_ordered_sum *sum;
467 btrfs_set_trans_block_group(trans, inode);
468 list_for_each(cur, list) {
469 sum = list_entry(cur, struct btrfs_ordered_sum, list);
470 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
476 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
478 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
482 /* see btrfs_writepage_start_hook for details on why this is required */
483 struct btrfs_writepage_fixup {
485 struct btrfs_work work;
488 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
490 struct btrfs_writepage_fixup *fixup;
491 struct btrfs_ordered_extent *ordered;
497 fixup = container_of(work, struct btrfs_writepage_fixup, work);
501 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
502 ClearPageChecked(page);
506 inode = page->mapping->host;
507 page_start = page_offset(page);
508 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
510 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
512 /* already ordered? We're done */
513 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
514 EXTENT_ORDERED, 0)) {
518 ordered = btrfs_lookup_ordered_extent(inode, page_start);
520 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
523 btrfs_start_ordered_extent(inode, ordered, 1);
527 btrfs_set_extent_delalloc(inode, page_start, page_end);
528 ClearPageChecked(page);
530 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
533 page_cache_release(page);
537 * There are a few paths in the higher layers of the kernel that directly
538 * set the page dirty bit without asking the filesystem if it is a
539 * good idea. This causes problems because we want to make sure COW
540 * properly happens and the data=ordered rules are followed.
542 * In our case any range that doesn't have the EXTENT_ORDERED bit set
543 * hasn't been properly setup for IO. We kick off an async process
544 * to fix it up. The async helper will wait for ordered extents, set
545 * the delalloc bit and make it safe to write the page.
547 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
549 struct inode *inode = page->mapping->host;
550 struct btrfs_writepage_fixup *fixup;
551 struct btrfs_root *root = BTRFS_I(inode)->root;
554 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
559 if (PageChecked(page))
562 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
566 SetPageChecked(page);
567 page_cache_get(page);
568 fixup->work.func = btrfs_writepage_fixup_worker;
570 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
574 /* as ordered data IO finishes, this gets called so we can finish
575 * an ordered extent if the range of bytes in the file it covers are
578 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
580 struct btrfs_root *root = BTRFS_I(inode)->root;
581 struct btrfs_trans_handle *trans;
582 struct btrfs_ordered_extent *ordered_extent;
583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
584 struct btrfs_file_extent_item *extent_item;
585 struct btrfs_path *path = NULL;
586 struct extent_buffer *leaf;
588 struct list_head list;
589 struct btrfs_key ins;
592 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
596 trans = btrfs_join_transaction(root, 1);
598 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
599 BUG_ON(!ordered_extent);
600 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
603 path = btrfs_alloc_path();
606 lock_extent(io_tree, ordered_extent->file_offset,
607 ordered_extent->file_offset + ordered_extent->len - 1,
610 INIT_LIST_HEAD(&list);
612 mutex_lock(&BTRFS_I(inode)->extent_mutex);
614 ret = btrfs_drop_extents(trans, root, inode,
615 ordered_extent->file_offset,
616 ordered_extent->file_offset +
618 ordered_extent->file_offset, &alloc_hint);
621 ins.objectid = inode->i_ino;
622 ins.offset = ordered_extent->file_offset;
623 ins.type = BTRFS_EXTENT_DATA_KEY;
624 ret = btrfs_insert_empty_item(trans, root, path, &ins,
625 sizeof(*extent_item));
627 leaf = path->nodes[0];
628 extent_item = btrfs_item_ptr(leaf, path->slots[0],
629 struct btrfs_file_extent_item);
630 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
631 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
632 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
633 ordered_extent->start);
634 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
635 ordered_extent->len);
636 btrfs_set_file_extent_offset(leaf, extent_item, 0);
637 btrfs_set_file_extent_num_bytes(leaf, extent_item,
638 ordered_extent->len);
639 btrfs_mark_buffer_dirty(leaf);
641 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
642 ordered_extent->file_offset +
643 ordered_extent->len - 1, 0);
644 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
646 ins.objectid = ordered_extent->start;
647 ins.offset = ordered_extent->len;
648 ins.type = BTRFS_EXTENT_ITEM_KEY;
649 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
650 root->root_key.objectid,
651 trans->transid, inode->i_ino, &ins);
653 btrfs_release_path(root, path);
655 inode_add_bytes(inode, ordered_extent->len);
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_set_bytes(inode, btrfs_inode_nbytes(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_nbytes(leaf, item, inode_get_bytes(inode));
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 * when truncating bytes in a file, it is possible to avoid reading
1394 * the leaves that contain only checksum items. This can be the
1395 * majority of the IO required to delete a large file, but it must
1396 * be done carefully.
1398 * The keys in the level just above the leaves are checked to make sure
1399 * the lowest key in a given leaf is a csum key, and starts at an offset
1400 * after the new size.
1402 * Then the key for the next leaf is checked to make sure it also has
1403 * a checksum item for the same file. If it does, we know our target leaf
1404 * contains only checksum items, and it can be safely freed without reading
1407 * This is just an optimization targeted at large files. It may do
1408 * nothing. It will return 0 unless things went badly.
1410 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1411 struct btrfs_root *root,
1412 struct btrfs_path *path,
1413 struct inode *inode, u64 new_size)
1415 struct btrfs_key key;
1418 struct btrfs_key found_key;
1419 struct btrfs_key other_key;
1420 struct btrfs_leaf_ref *ref;
1424 path->lowest_level = 1;
1425 key.objectid = inode->i_ino;
1426 key.type = BTRFS_CSUM_ITEM_KEY;
1427 key.offset = new_size;
1429 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1433 if (path->nodes[1] == NULL) {
1438 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1439 nritems = btrfs_header_nritems(path->nodes[1]);
1444 if (path->slots[1] >= nritems)
1447 /* did we find a key greater than anything we want to delete? */
1448 if (found_key.objectid > inode->i_ino ||
1449 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1452 /* we check the next key in the node to make sure the leave contains
1453 * only checksum items. This comparison doesn't work if our
1454 * leaf is the last one in the node
1456 if (path->slots[1] + 1 >= nritems) {
1458 /* search forward from the last key in the node, this
1459 * will bring us into the next node in the tree
1461 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1463 /* unlikely, but we inc below, so check to be safe */
1464 if (found_key.offset == (u64)-1)
1467 /* search_forward needs a path with locks held, do the
1468 * search again for the original key. It is possible
1469 * this will race with a balance and return a path that
1470 * we could modify, but this drop is just an optimization
1471 * and is allowed to miss some leaves.
1473 btrfs_release_path(root, path);
1476 /* setup a max key for search_forward */
1477 other_key.offset = (u64)-1;
1478 other_key.type = key.type;
1479 other_key.objectid = key.objectid;
1481 path->keep_locks = 1;
1482 ret = btrfs_search_forward(root, &found_key, &other_key,
1484 path->keep_locks = 0;
1485 if (ret || found_key.objectid != key.objectid ||
1486 found_key.type != key.type) {
1491 key.offset = found_key.offset;
1492 btrfs_release_path(root, path);
1497 /* we know there's one more slot after us in the tree,
1498 * read that key so we can verify it is also a checksum item
1500 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1502 if (found_key.objectid < inode->i_ino)
1505 if (found_key.type != key.type || found_key.offset < new_size)
1509 * if the key for the next leaf isn't a csum key from this objectid,
1510 * we can't be sure there aren't good items inside this leaf.
1513 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1516 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1517 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
1519 * it is safe to delete this leaf, it contains only
1520 * csum items from this inode at an offset >= new_size
1522 ret = btrfs_del_leaf(trans, root, path, leaf_start);
1525 if (root->ref_cows && leaf_gen < trans->transid) {
1526 ref = btrfs_alloc_leaf_ref(root, 0);
1528 ref->root_gen = root->root_key.offset;
1529 ref->bytenr = leaf_start;
1531 ref->generation = leaf_gen;
1534 ret = btrfs_add_leaf_ref(root, ref, 0);
1536 btrfs_free_leaf_ref(root, ref);
1542 btrfs_release_path(root, path);
1544 if (other_key.objectid == inode->i_ino &&
1545 other_key.type == key.type && other_key.offset > key.offset) {
1546 key.offset = other_key.offset;
1552 /* fixup any changes we've made to the path */
1553 path->lowest_level = 0;
1554 path->keep_locks = 0;
1555 btrfs_release_path(root, path);
1560 * this can truncate away extent items, csum items and directory items.
1561 * It starts at a high offset and removes keys until it can't find
1562 * any higher than new_size
1564 * csum items that cross the new i_size are truncated to the new size
1567 * min_type is the minimum key type to truncate down to. If set to 0, this
1568 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1570 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1571 struct btrfs_root *root,
1572 struct inode *inode,
1573 u64 new_size, u32 min_type)
1576 struct btrfs_path *path;
1577 struct btrfs_key key;
1578 struct btrfs_key found_key;
1580 struct extent_buffer *leaf;
1581 struct btrfs_file_extent_item *fi;
1582 u64 extent_start = 0;
1583 u64 extent_num_bytes = 0;
1589 int pending_del_nr = 0;
1590 int pending_del_slot = 0;
1591 int extent_type = -1;
1592 u64 mask = root->sectorsize - 1;
1595 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
1596 path = btrfs_alloc_path();
1600 /* FIXME, add redo link to tree so we don't leak on crash */
1601 key.objectid = inode->i_ino;
1602 key.offset = (u64)-1;
1605 btrfs_init_path(path);
1607 ret = drop_csum_leaves(trans, root, path, inode, new_size);
1611 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1616 /* there are no items in the tree for us to truncate, we're
1619 if (path->slots[0] == 0) {
1628 leaf = path->nodes[0];
1629 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1630 found_type = btrfs_key_type(&found_key);
1632 if (found_key.objectid != inode->i_ino)
1635 if (found_type < min_type)
1638 item_end = found_key.offset;
1639 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1640 fi = btrfs_item_ptr(leaf, path->slots[0],
1641 struct btrfs_file_extent_item);
1642 extent_type = btrfs_file_extent_type(leaf, fi);
1643 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1645 btrfs_file_extent_num_bytes(leaf, fi);
1646 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1647 struct btrfs_item *item = btrfs_item_nr(leaf,
1649 item_end += btrfs_file_extent_inline_len(leaf,
1654 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1655 ret = btrfs_csum_truncate(trans, root, path,
1659 if (item_end < new_size) {
1660 if (found_type == BTRFS_DIR_ITEM_KEY) {
1661 found_type = BTRFS_INODE_ITEM_KEY;
1662 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1663 found_type = BTRFS_CSUM_ITEM_KEY;
1664 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1665 found_type = BTRFS_XATTR_ITEM_KEY;
1666 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1667 found_type = BTRFS_INODE_REF_KEY;
1668 } else if (found_type) {
1673 btrfs_set_key_type(&key, found_type);
1676 if (found_key.offset >= new_size)
1682 /* FIXME, shrink the extent if the ref count is only 1 */
1683 if (found_type != BTRFS_EXTENT_DATA_KEY)
1686 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1688 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1690 u64 orig_num_bytes =
1691 btrfs_file_extent_num_bytes(leaf, fi);
1692 extent_num_bytes = new_size -
1693 found_key.offset + root->sectorsize - 1;
1694 extent_num_bytes = extent_num_bytes &
1695 ~((u64)root->sectorsize - 1);
1696 btrfs_set_file_extent_num_bytes(leaf, fi,
1698 num_dec = (orig_num_bytes -
1700 if (root->ref_cows && extent_start != 0)
1701 inode_sub_bytes(inode, num_dec);
1702 btrfs_mark_buffer_dirty(leaf);
1705 btrfs_file_extent_disk_num_bytes(leaf,
1707 /* FIXME blocksize != 4096 */
1708 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1709 if (extent_start != 0) {
1712 inode_sub_bytes(inode, num_dec);
1714 root_gen = btrfs_header_generation(leaf);
1715 root_owner = btrfs_header_owner(leaf);
1717 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1719 u32 size = new_size - found_key.offset;
1721 if (root->ref_cows) {
1722 inode_sub_bytes(inode, item_end + 1 -
1726 btrfs_file_extent_calc_inline_size(size);
1727 ret = btrfs_truncate_item(trans, root, path,
1730 } else if (root->ref_cows) {
1731 inode_sub_bytes(inode, item_end + 1 -
1737 if (!pending_del_nr) {
1738 /* no pending yet, add ourselves */
1739 pending_del_slot = path->slots[0];
1741 } else if (pending_del_nr &&
1742 path->slots[0] + 1 == pending_del_slot) {
1743 /* hop on the pending chunk */
1745 pending_del_slot = path->slots[0];
1747 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1753 ret = btrfs_free_extent(trans, root, extent_start,
1755 leaf->start, root_owner,
1756 root_gen, inode->i_ino, 0);
1760 if (path->slots[0] == 0) {
1763 btrfs_release_path(root, path);
1768 if (pending_del_nr &&
1769 path->slots[0] + 1 != pending_del_slot) {
1770 struct btrfs_key debug;
1772 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1774 ret = btrfs_del_items(trans, root, path,
1779 btrfs_release_path(root, path);
1785 if (pending_del_nr) {
1786 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1789 btrfs_free_path(path);
1790 inode->i_sb->s_dirt = 1;
1795 * taken from block_truncate_page, but does cow as it zeros out
1796 * any bytes left in the last page in the file.
1798 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1800 struct inode *inode = mapping->host;
1801 struct btrfs_root *root = BTRFS_I(inode)->root;
1802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1803 struct btrfs_ordered_extent *ordered;
1805 u32 blocksize = root->sectorsize;
1806 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1807 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1813 if ((offset & (blocksize - 1)) == 0)
1818 page = grab_cache_page(mapping, index);
1822 page_start = page_offset(page);
1823 page_end = page_start + PAGE_CACHE_SIZE - 1;
1825 if (!PageUptodate(page)) {
1826 ret = btrfs_readpage(NULL, page);
1828 if (page->mapping != mapping) {
1830 page_cache_release(page);
1833 if (!PageUptodate(page)) {
1838 wait_on_page_writeback(page);
1840 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1841 set_page_extent_mapped(page);
1843 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1845 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1847 page_cache_release(page);
1848 btrfs_start_ordered_extent(inode, ordered, 1);
1849 btrfs_put_ordered_extent(ordered);
1853 btrfs_set_extent_delalloc(inode, page_start, page_end);
1855 if (offset != PAGE_CACHE_SIZE) {
1857 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1858 flush_dcache_page(page);
1861 ClearPageChecked(page);
1862 set_page_dirty(page);
1863 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1867 page_cache_release(page);
1872 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1874 struct inode *inode = dentry->d_inode;
1877 err = inode_change_ok(inode, attr);
1881 if (S_ISREG(inode->i_mode) &&
1882 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1883 struct btrfs_trans_handle *trans;
1884 struct btrfs_root *root = BTRFS_I(inode)->root;
1885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1887 u64 mask = root->sectorsize - 1;
1888 u64 hole_start = (inode->i_size + mask) & ~mask;
1889 u64 block_end = (attr->ia_size + mask) & ~mask;
1893 if (attr->ia_size <= hole_start)
1896 err = btrfs_check_free_space(root, 1, 0);
1900 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1902 hole_size = block_end - hole_start;
1904 struct btrfs_ordered_extent *ordered;
1905 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1907 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1908 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1910 unlock_extent(io_tree, hole_start,
1911 block_end - 1, GFP_NOFS);
1912 btrfs_put_ordered_extent(ordered);
1918 trans = btrfs_start_transaction(root, 1);
1919 btrfs_set_trans_block_group(trans, inode);
1920 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1921 err = btrfs_drop_extents(trans, root, inode,
1922 hole_start, block_end, hole_start,
1925 if (alloc_hint != EXTENT_MAP_INLINE) {
1926 err = btrfs_insert_file_extent(trans, root,
1930 btrfs_drop_extent_cache(inode, hole_start,
1932 btrfs_check_file(root, inode);
1934 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1935 btrfs_end_transaction(trans, root);
1936 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1941 err = inode_setattr(inode, attr);
1943 if (!err && ((attr->ia_valid & ATTR_MODE)))
1944 err = btrfs_acl_chmod(inode);
1949 void btrfs_delete_inode(struct inode *inode)
1951 struct btrfs_trans_handle *trans;
1952 struct btrfs_root *root = BTRFS_I(inode)->root;
1956 truncate_inode_pages(&inode->i_data, 0);
1957 if (is_bad_inode(inode)) {
1958 btrfs_orphan_del(NULL, inode);
1961 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1963 btrfs_i_size_write(inode, 0);
1964 trans = btrfs_start_transaction(root, 1);
1966 btrfs_set_trans_block_group(trans, inode);
1967 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
1969 btrfs_orphan_del(NULL, inode);
1970 goto no_delete_lock;
1973 btrfs_orphan_del(trans, inode);
1975 nr = trans->blocks_used;
1978 btrfs_end_transaction(trans, root);
1979 btrfs_btree_balance_dirty(root, nr);
1983 nr = trans->blocks_used;
1984 btrfs_end_transaction(trans, root);
1985 btrfs_btree_balance_dirty(root, nr);
1991 * this returns the key found in the dir entry in the location pointer.
1992 * If no dir entries were found, location->objectid is 0.
1994 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1995 struct btrfs_key *location)
1997 const char *name = dentry->d_name.name;
1998 int namelen = dentry->d_name.len;
1999 struct btrfs_dir_item *di;
2000 struct btrfs_path *path;
2001 struct btrfs_root *root = BTRFS_I(dir)->root;
2004 path = btrfs_alloc_path();
2007 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2011 if (!di || IS_ERR(di)) {
2014 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
2016 btrfs_free_path(path);
2019 location->objectid = 0;
2024 * when we hit a tree root in a directory, the btrfs part of the inode
2025 * needs to be changed to reflect the root directory of the tree root. This
2026 * is kind of like crossing a mount point.
2028 static int fixup_tree_root_location(struct btrfs_root *root,
2029 struct btrfs_key *location,
2030 struct btrfs_root **sub_root,
2031 struct dentry *dentry)
2033 struct btrfs_root_item *ri;
2035 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2037 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2040 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2041 dentry->d_name.name,
2042 dentry->d_name.len);
2043 if (IS_ERR(*sub_root))
2044 return PTR_ERR(*sub_root);
2046 ri = &(*sub_root)->root_item;
2047 location->objectid = btrfs_root_dirid(ri);
2048 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2049 location->offset = 0;
2054 static noinline void init_btrfs_i(struct inode *inode)
2056 struct btrfs_inode *bi = BTRFS_I(inode);
2059 bi->i_default_acl = NULL;
2063 bi->logged_trans = 0;
2064 bi->delalloc_bytes = 0;
2065 bi->disk_i_size = 0;
2067 bi->index_cnt = (u64)-1;
2068 bi->log_dirty_trans = 0;
2069 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2070 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2071 inode->i_mapping, GFP_NOFS);
2072 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2073 inode->i_mapping, GFP_NOFS);
2074 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2075 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2076 mutex_init(&BTRFS_I(inode)->csum_mutex);
2077 mutex_init(&BTRFS_I(inode)->extent_mutex);
2078 mutex_init(&BTRFS_I(inode)->log_mutex);
2081 static int btrfs_init_locked_inode(struct inode *inode, void *p)
2083 struct btrfs_iget_args *args = p;
2084 inode->i_ino = args->ino;
2085 init_btrfs_i(inode);
2086 BTRFS_I(inode)->root = args->root;
2090 static int btrfs_find_actor(struct inode *inode, void *opaque)
2092 struct btrfs_iget_args *args = opaque;
2093 return (args->ino == inode->i_ino &&
2094 args->root == BTRFS_I(inode)->root);
2097 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2098 struct btrfs_root *root, int wait)
2100 struct inode *inode;
2101 struct btrfs_iget_args args;
2102 args.ino = objectid;
2106 inode = ilookup5(s, objectid, btrfs_find_actor,
2109 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2115 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2116 struct btrfs_root *root)
2118 struct inode *inode;
2119 struct btrfs_iget_args args;
2120 args.ino = objectid;
2123 inode = iget5_locked(s, objectid, btrfs_find_actor,
2124 btrfs_init_locked_inode,
2129 /* Get an inode object given its location and corresponding root.
2130 * Returns in *is_new if the inode was read from disk
2132 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2133 struct btrfs_root *root, int *is_new)
2135 struct inode *inode;
2137 inode = btrfs_iget_locked(s, location->objectid, root);
2139 return ERR_PTR(-EACCES);
2141 if (inode->i_state & I_NEW) {
2142 BTRFS_I(inode)->root = root;
2143 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2144 btrfs_read_locked_inode(inode);
2145 unlock_new_inode(inode);
2156 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2157 struct nameidata *nd)
2159 struct inode * inode;
2160 struct btrfs_inode *bi = BTRFS_I(dir);
2161 struct btrfs_root *root = bi->root;
2162 struct btrfs_root *sub_root = root;
2163 struct btrfs_key location;
2164 int ret, new, do_orphan = 0;
2166 if (dentry->d_name.len > BTRFS_NAME_LEN)
2167 return ERR_PTR(-ENAMETOOLONG);
2169 ret = btrfs_inode_by_name(dir, dentry, &location);
2172 return ERR_PTR(ret);
2175 if (location.objectid) {
2176 ret = fixup_tree_root_location(root, &location, &sub_root,
2179 return ERR_PTR(ret);
2181 return ERR_PTR(-ENOENT);
2182 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2184 return ERR_CAST(inode);
2186 /* the inode and parent dir are two different roots */
2187 if (new && root != sub_root) {
2189 sub_root->inode = inode;
2194 if (unlikely(do_orphan))
2195 btrfs_orphan_cleanup(sub_root);
2197 return d_splice_alias(inode, dentry);
2200 static unsigned char btrfs_filetype_table[] = {
2201 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2204 static int btrfs_real_readdir(struct file *filp, void *dirent,
2207 struct inode *inode = filp->f_dentry->d_inode;
2208 struct btrfs_root *root = BTRFS_I(inode)->root;
2209 struct btrfs_item *item;
2210 struct btrfs_dir_item *di;
2211 struct btrfs_key key;
2212 struct btrfs_key found_key;
2213 struct btrfs_path *path;
2216 struct extent_buffer *leaf;
2219 unsigned char d_type;
2224 int key_type = BTRFS_DIR_INDEX_KEY;
2229 /* FIXME, use a real flag for deciding about the key type */
2230 if (root->fs_info->tree_root == root)
2231 key_type = BTRFS_DIR_ITEM_KEY;
2233 /* special case for "." */
2234 if (filp->f_pos == 0) {
2235 over = filldir(dirent, ".", 1,
2242 /* special case for .., just use the back ref */
2243 if (filp->f_pos == 1) {
2244 u64 pino = parent_ino(filp->f_path.dentry);
2245 over = filldir(dirent, "..", 2,
2252 path = btrfs_alloc_path();
2255 btrfs_set_key_type(&key, key_type);
2256 key.offset = filp->f_pos;
2257 key.objectid = inode->i_ino;
2259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2265 leaf = path->nodes[0];
2266 nritems = btrfs_header_nritems(leaf);
2267 slot = path->slots[0];
2268 if (advance || slot >= nritems) {
2269 if (slot >= nritems - 1) {
2270 ret = btrfs_next_leaf(root, path);
2273 leaf = path->nodes[0];
2274 nritems = btrfs_header_nritems(leaf);
2275 slot = path->slots[0];
2282 item = btrfs_item_nr(leaf, slot);
2283 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2285 if (found_key.objectid != key.objectid)
2287 if (btrfs_key_type(&found_key) != key_type)
2289 if (found_key.offset < filp->f_pos)
2292 filp->f_pos = found_key.offset;
2294 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2296 di_total = btrfs_item_size(leaf, item);
2298 while (di_cur < di_total) {
2299 struct btrfs_key location;
2301 name_len = btrfs_dir_name_len(leaf, di);
2302 if (name_len <= sizeof(tmp_name)) {
2303 name_ptr = tmp_name;
2305 name_ptr = kmalloc(name_len, GFP_NOFS);
2311 read_extent_buffer(leaf, name_ptr,
2312 (unsigned long)(di + 1), name_len);
2314 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2315 btrfs_dir_item_key_to_cpu(leaf, di, &location);
2316 over = filldir(dirent, name_ptr, name_len,
2317 found_key.offset, location.objectid,
2320 if (name_ptr != tmp_name)
2326 di_len = btrfs_dir_name_len(leaf, di) +
2327 btrfs_dir_data_len(leaf, di) + sizeof(*di);
2329 di = (struct btrfs_dir_item *)((char *)di + di_len);
2333 /* Reached end of directory/root. Bump pos past the last item. */
2334 if (key_type == BTRFS_DIR_INDEX_KEY)
2335 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2341 btrfs_free_path(path);
2345 int btrfs_write_inode(struct inode *inode, int wait)
2347 struct btrfs_root *root = BTRFS_I(inode)->root;
2348 struct btrfs_trans_handle *trans;
2351 if (root->fs_info->closing > 1)
2355 trans = btrfs_join_transaction(root, 1);
2356 btrfs_set_trans_block_group(trans, inode);
2357 ret = btrfs_commit_transaction(trans, root);
2363 * This is somewhat expensive, updating the tree every time the
2364 * inode changes. But, it is most likely to find the inode in cache.
2365 * FIXME, needs more benchmarking...there are no reasons other than performance
2366 * to keep or drop this code.
2368 void btrfs_dirty_inode(struct inode *inode)
2370 struct btrfs_root *root = BTRFS_I(inode)->root;
2371 struct btrfs_trans_handle *trans;
2373 trans = btrfs_join_transaction(root, 1);
2374 btrfs_set_trans_block_group(trans, inode);
2375 btrfs_update_inode(trans, root, inode);
2376 btrfs_end_transaction(trans, root);
2380 * find the highest existing sequence number in a directory
2381 * and then set the in-memory index_cnt variable to reflect
2382 * free sequence numbers
2384 static int btrfs_set_inode_index_count(struct inode *inode)
2386 struct btrfs_root *root = BTRFS_I(inode)->root;
2387 struct btrfs_key key, found_key;
2388 struct btrfs_path *path;
2389 struct extent_buffer *leaf;
2392 key.objectid = inode->i_ino;
2393 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2394 key.offset = (u64)-1;
2396 path = btrfs_alloc_path();
2400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2403 /* FIXME: we should be able to handle this */
2409 * MAGIC NUMBER EXPLANATION:
2410 * since we search a directory based on f_pos we have to start at 2
2411 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2412 * else has to start at 2
2414 if (path->slots[0] == 0) {
2415 BTRFS_I(inode)->index_cnt = 2;
2421 leaf = path->nodes[0];
2422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2424 if (found_key.objectid != inode->i_ino ||
2425 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2426 BTRFS_I(inode)->index_cnt = 2;
2430 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2432 btrfs_free_path(path);
2437 * helper to find a free sequence number in a given directory. This current
2438 * code is very simple, later versions will do smarter things in the btree
2440 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2445 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2446 ret = btrfs_set_inode_index_count(dir);
2452 *index = BTRFS_I(dir)->index_cnt;
2453 BTRFS_I(dir)->index_cnt++;
2458 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2461 const char *name, int name_len,
2464 struct btrfs_block_group_cache *group,
2465 int mode, u64 *index)
2467 struct inode *inode;
2468 struct btrfs_inode_item *inode_item;
2469 struct btrfs_block_group_cache *new_inode_group;
2470 struct btrfs_key *location;
2471 struct btrfs_path *path;
2472 struct btrfs_inode_ref *ref;
2473 struct btrfs_key key[2];
2479 path = btrfs_alloc_path();
2482 inode = new_inode(root->fs_info->sb);
2484 return ERR_PTR(-ENOMEM);
2487 ret = btrfs_set_inode_index(dir, inode, index);
2489 return ERR_PTR(ret);
2492 * index_cnt is ignored for everything but a dir,
2493 * btrfs_get_inode_index_count has an explanation for the magic
2496 init_btrfs_i(inode);
2497 BTRFS_I(inode)->index_cnt = 2;
2498 BTRFS_I(inode)->root = root;
2499 BTRFS_I(inode)->generation = trans->transid;
2505 new_inode_group = btrfs_find_block_group(root, group, 0,
2506 BTRFS_BLOCK_GROUP_METADATA, owner);
2507 if (!new_inode_group) {
2508 printk("find_block group failed\n");
2509 new_inode_group = group;
2511 BTRFS_I(inode)->block_group = new_inode_group;
2513 key[0].objectid = objectid;
2514 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2517 key[1].objectid = objectid;
2518 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2519 key[1].offset = ref_objectid;
2521 sizes[0] = sizeof(struct btrfs_inode_item);
2522 sizes[1] = name_len + sizeof(*ref);
2524 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2528 if (objectid > root->highest_inode)
2529 root->highest_inode = objectid;
2531 inode->i_uid = current->fsuid;
2532 inode->i_gid = current->fsgid;
2533 inode->i_mode = mode;
2534 inode->i_ino = objectid;
2535 inode_set_bytes(inode, 0);
2536 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2537 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2538 struct btrfs_inode_item);
2539 fill_inode_item(trans, path->nodes[0], inode_item, inode);
2541 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2542 struct btrfs_inode_ref);
2543 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2544 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2545 ptr = (unsigned long)(ref + 1);
2546 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2548 btrfs_mark_buffer_dirty(path->nodes[0]);
2549 btrfs_free_path(path);
2551 location = &BTRFS_I(inode)->location;
2552 location->objectid = objectid;
2553 location->offset = 0;
2554 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2556 insert_inode_hash(inode);
2560 BTRFS_I(dir)->index_cnt--;
2561 btrfs_free_path(path);
2562 return ERR_PTR(ret);
2565 static inline u8 btrfs_inode_type(struct inode *inode)
2567 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2571 * utility function to add 'inode' into 'parent_inode' with
2572 * a give name and a given sequence number.
2573 * if 'add_backref' is true, also insert a backref from the
2574 * inode to the parent directory.
2576 int btrfs_add_link(struct btrfs_trans_handle *trans,
2577 struct inode *parent_inode, struct inode *inode,
2578 const char *name, int name_len, int add_backref, u64 index)
2581 struct btrfs_key key;
2582 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
2584 key.objectid = inode->i_ino;
2585 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2588 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2589 parent_inode->i_ino,
2590 &key, btrfs_inode_type(inode),
2594 ret = btrfs_insert_inode_ref(trans, root,
2597 parent_inode->i_ino,
2600 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2602 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2603 ret = btrfs_update_inode(trans, root, parent_inode);
2608 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2609 struct dentry *dentry, struct inode *inode,
2610 int backref, u64 index)
2612 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2613 inode, dentry->d_name.name,
2614 dentry->d_name.len, backref, index);
2616 d_instantiate(dentry, inode);
2624 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2625 int mode, dev_t rdev)
2627 struct btrfs_trans_handle *trans;
2628 struct btrfs_root *root = BTRFS_I(dir)->root;
2629 struct inode *inode = NULL;
2633 unsigned long nr = 0;
2636 if (!new_valid_dev(rdev))
2639 err = btrfs_check_free_space(root, 1, 0);
2643 trans = btrfs_start_transaction(root, 1);
2644 btrfs_set_trans_block_group(trans, dir);
2646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2652 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2654 dentry->d_parent->d_inode->i_ino, objectid,
2655 BTRFS_I(dir)->block_group, mode, &index);
2656 err = PTR_ERR(inode);
2660 err = btrfs_init_acl(inode, dir);
2666 btrfs_set_trans_block_group(trans, inode);
2667 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2671 inode->i_op = &btrfs_special_inode_operations;
2672 init_special_inode(inode, inode->i_mode, rdev);
2673 btrfs_update_inode(trans, root, inode);
2675 dir->i_sb->s_dirt = 1;
2676 btrfs_update_inode_block_group(trans, inode);
2677 btrfs_update_inode_block_group(trans, dir);
2679 nr = trans->blocks_used;
2680 btrfs_end_transaction_throttle(trans, root);
2683 inode_dec_link_count(inode);
2686 btrfs_btree_balance_dirty(root, nr);
2690 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2691 int mode, struct nameidata *nd)
2693 struct btrfs_trans_handle *trans;
2694 struct btrfs_root *root = BTRFS_I(dir)->root;
2695 struct inode *inode = NULL;
2698 unsigned long nr = 0;
2702 err = btrfs_check_free_space(root, 1, 0);
2705 trans = btrfs_start_transaction(root, 1);
2706 btrfs_set_trans_block_group(trans, dir);
2708 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2714 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2716 dentry->d_parent->d_inode->i_ino,
2717 objectid, BTRFS_I(dir)->block_group, mode,
2719 err = PTR_ERR(inode);
2723 err = btrfs_init_acl(inode, dir);
2729 btrfs_set_trans_block_group(trans, inode);
2730 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2734 inode->i_mapping->a_ops = &btrfs_aops;
2735 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2736 inode->i_fop = &btrfs_file_operations;
2737 inode->i_op = &btrfs_file_inode_operations;
2738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2740 dir->i_sb->s_dirt = 1;
2741 btrfs_update_inode_block_group(trans, inode);
2742 btrfs_update_inode_block_group(trans, dir);
2744 nr = trans->blocks_used;
2745 btrfs_end_transaction_throttle(trans, root);
2748 inode_dec_link_count(inode);
2751 btrfs_btree_balance_dirty(root, nr);
2755 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2756 struct dentry *dentry)
2758 struct btrfs_trans_handle *trans;
2759 struct btrfs_root *root = BTRFS_I(dir)->root;
2760 struct inode *inode = old_dentry->d_inode;
2762 unsigned long nr = 0;
2766 if (inode->i_nlink == 0)
2769 btrfs_inc_nlink(inode);
2770 err = btrfs_check_free_space(root, 1, 0);
2773 err = btrfs_set_inode_index(dir, inode, &index);
2777 trans = btrfs_start_transaction(root, 1);
2779 btrfs_set_trans_block_group(trans, dir);
2780 atomic_inc(&inode->i_count);
2782 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
2787 dir->i_sb->s_dirt = 1;
2788 btrfs_update_inode_block_group(trans, dir);
2789 err = btrfs_update_inode(trans, root, inode);
2794 nr = trans->blocks_used;
2795 btrfs_end_transaction_throttle(trans, root);
2798 inode_dec_link_count(inode);
2801 btrfs_btree_balance_dirty(root, nr);
2805 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2807 struct inode *inode = NULL;
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2811 int drop_on_err = 0;
2814 unsigned long nr = 1;
2816 err = btrfs_check_free_space(root, 1, 0);
2820 trans = btrfs_start_transaction(root, 1);
2821 btrfs_set_trans_block_group(trans, dir);
2823 if (IS_ERR(trans)) {
2824 err = PTR_ERR(trans);
2828 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2834 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2836 dentry->d_parent->d_inode->i_ino, objectid,
2837 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2839 if (IS_ERR(inode)) {
2840 err = PTR_ERR(inode);
2846 err = btrfs_init_acl(inode, dir);
2850 inode->i_op = &btrfs_dir_inode_operations;
2851 inode->i_fop = &btrfs_dir_file_operations;
2852 btrfs_set_trans_block_group(trans, inode);
2854 btrfs_i_size_write(inode, 0);
2855 err = btrfs_update_inode(trans, root, inode);
2859 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2860 inode, dentry->d_name.name,
2861 dentry->d_name.len, 0, index);
2865 d_instantiate(dentry, inode);
2867 dir->i_sb->s_dirt = 1;
2868 btrfs_update_inode_block_group(trans, inode);
2869 btrfs_update_inode_block_group(trans, dir);
2872 nr = trans->blocks_used;
2873 btrfs_end_transaction_throttle(trans, root);
2878 btrfs_btree_balance_dirty(root, nr);
2882 /* helper for btfs_get_extent. Given an existing extent in the tree,
2883 * and an extent that you want to insert, deal with overlap and insert
2884 * the new extent into the tree.
2886 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2887 struct extent_map *existing,
2888 struct extent_map *em,
2889 u64 map_start, u64 map_len)
2893 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2894 start_diff = map_start - em->start;
2895 em->start = map_start;
2897 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2898 em->block_start += start_diff;
2899 return add_extent_mapping(em_tree, em);
2903 * a bit scary, this does extent mapping from logical file offset to the disk.
2904 * the ugly parts come from merging extents from the disk with the
2905 * in-ram representation. This gets more complex because of the data=ordered code,
2906 * where the in-ram extents might be locked pending data=ordered completion.
2908 * This also copies inline extents directly into the page.
2910 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2911 size_t pg_offset, u64 start, u64 len,
2917 u64 extent_start = 0;
2919 u64 objectid = inode->i_ino;
2921 struct btrfs_path *path = NULL;
2922 struct btrfs_root *root = BTRFS_I(inode)->root;
2923 struct btrfs_file_extent_item *item;
2924 struct extent_buffer *leaf;
2925 struct btrfs_key found_key;
2926 struct extent_map *em = NULL;
2927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2929 struct btrfs_trans_handle *trans = NULL;
2932 spin_lock(&em_tree->lock);
2933 em = lookup_extent_mapping(em_tree, start, len);
2935 em->bdev = root->fs_info->fs_devices->latest_bdev;
2936 spin_unlock(&em_tree->lock);
2939 if (em->start > start || em->start + em->len <= start)
2940 free_extent_map(em);
2941 else if (em->block_start == EXTENT_MAP_INLINE && page)
2942 free_extent_map(em);
2946 em = alloc_extent_map(GFP_NOFS);
2951 em->bdev = root->fs_info->fs_devices->latest_bdev;
2952 em->start = EXTENT_MAP_HOLE;
2956 path = btrfs_alloc_path();
2960 ret = btrfs_lookup_file_extent(trans, root, path,
2961 objectid, start, trans != NULL);
2968 if (path->slots[0] == 0)
2973 leaf = path->nodes[0];
2974 item = btrfs_item_ptr(leaf, path->slots[0],
2975 struct btrfs_file_extent_item);
2976 /* are we inside the extent that was found? */
2977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2978 found_type = btrfs_key_type(&found_key);
2979 if (found_key.objectid != objectid ||
2980 found_type != BTRFS_EXTENT_DATA_KEY) {
2984 found_type = btrfs_file_extent_type(leaf, item);
2985 extent_start = found_key.offset;
2986 if (found_type == BTRFS_FILE_EXTENT_REG) {
2987 extent_end = extent_start +
2988 btrfs_file_extent_num_bytes(leaf, item);
2990 if (start < extent_start || start >= extent_end) {
2992 if (start < extent_start) {
2993 if (start + len <= extent_start)
2995 em->len = extent_end - extent_start;
3001 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3003 em->start = extent_start;
3004 em->len = extent_end - extent_start;
3005 em->block_start = EXTENT_MAP_HOLE;
3008 bytenr += btrfs_file_extent_offset(leaf, item);
3009 em->block_start = bytenr;
3010 em->start = extent_start;
3011 em->len = extent_end - extent_start;
3013 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3018 size_t extent_offset;
3021 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
3023 extent_end = (extent_start + size + root->sectorsize - 1) &
3024 ~((u64)root->sectorsize - 1);
3025 if (start < extent_start || start >= extent_end) {
3027 if (start < extent_start) {
3028 if (start + len <= extent_start)
3030 em->len = extent_end - extent_start;
3036 em->block_start = EXTENT_MAP_INLINE;
3039 em->start = extent_start;
3044 page_start = page_offset(page) + pg_offset;
3045 extent_offset = page_start - extent_start;
3046 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3047 size - extent_offset);
3048 em->start = extent_start + extent_offset;
3049 em->len = (copy_size + root->sectorsize - 1) &
3050 ~((u64)root->sectorsize - 1);
3052 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
3053 if (create == 0 && !PageUptodate(page)) {
3054 read_extent_buffer(leaf, map + pg_offset, ptr,
3056 flush_dcache_page(page);
3057 } else if (create && PageUptodate(page)) {
3060 free_extent_map(em);
3062 btrfs_release_path(root, path);
3063 trans = btrfs_join_transaction(root, 1);
3066 write_extent_buffer(leaf, map + pg_offset, ptr,
3068 btrfs_mark_buffer_dirty(leaf);
3071 set_extent_uptodate(io_tree, em->start,
3072 extent_map_end(em) - 1, GFP_NOFS);
3075 printk("unkknown found_type %d\n", found_type);
3082 em->block_start = EXTENT_MAP_HOLE;
3084 btrfs_release_path(root, path);
3085 if (em->start > start || extent_map_end(em) <= start) {
3086 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
3092 spin_lock(&em_tree->lock);
3093 ret = add_extent_mapping(em_tree, em);
3094 /* it is possible that someone inserted the extent into the tree
3095 * while we had the lock dropped. It is also possible that
3096 * an overlapping map exists in the tree
3098 if (ret == -EEXIST) {
3099 struct extent_map *existing;
3103 existing = lookup_extent_mapping(em_tree, start, len);
3104 if (existing && (existing->start > start ||
3105 existing->start + existing->len <= start)) {
3106 free_extent_map(existing);
3110 existing = lookup_extent_mapping(em_tree, em->start,
3113 err = merge_extent_mapping(em_tree, existing,
3116 free_extent_map(existing);
3118 free_extent_map(em);
3123 printk("failing to insert %Lu %Lu\n",
3125 free_extent_map(em);
3129 free_extent_map(em);
3134 spin_unlock(&em_tree->lock);
3137 btrfs_free_path(path);
3139 ret = btrfs_end_transaction(trans, root);
3145 free_extent_map(em);
3147 return ERR_PTR(err);
3152 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3153 const struct iovec *iov, loff_t offset,
3154 unsigned long nr_segs)
3159 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
3161 return extent_bmap(mapping, iblock, btrfs_get_extent);
3164 int btrfs_readpage(struct file *file, struct page *page)
3166 struct extent_io_tree *tree;
3167 tree = &BTRFS_I(page->mapping->host)->io_tree;
3168 return extent_read_full_page(tree, page, btrfs_get_extent);
3171 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3173 struct extent_io_tree *tree;
3176 if (current->flags & PF_MEMALLOC) {
3177 redirty_page_for_writepage(wbc, page);
3181 tree = &BTRFS_I(page->mapping->host)->io_tree;
3182 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3185 int btrfs_writepages(struct address_space *mapping,
3186 struct writeback_control *wbc)
3188 struct extent_io_tree *tree;
3189 tree = &BTRFS_I(mapping->host)->io_tree;
3190 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3194 btrfs_readpages(struct file *file, struct address_space *mapping,
3195 struct list_head *pages, unsigned nr_pages)
3197 struct extent_io_tree *tree;
3198 tree = &BTRFS_I(mapping->host)->io_tree;
3199 return extent_readpages(tree, mapping, pages, nr_pages,
3202 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3204 struct extent_io_tree *tree;
3205 struct extent_map_tree *map;
3208 tree = &BTRFS_I(page->mapping->host)->io_tree;
3209 map = &BTRFS_I(page->mapping->host)->extent_tree;
3210 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3212 ClearPagePrivate(page);
3213 set_page_private(page, 0);
3214 page_cache_release(page);
3219 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3221 if (PageWriteback(page) || PageDirty(page))
3223 return __btrfs_releasepage(page, gfp_flags);
3226 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3228 struct extent_io_tree *tree;
3229 struct btrfs_ordered_extent *ordered;
3230 u64 page_start = page_offset(page);
3231 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3233 wait_on_page_writeback(page);
3234 tree = &BTRFS_I(page->mapping->host)->io_tree;
3236 btrfs_releasepage(page, GFP_NOFS);
3240 lock_extent(tree, page_start, page_end, GFP_NOFS);
3241 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3245 * IO on this page will never be started, so we need
3246 * to account for any ordered extents now
3248 clear_extent_bit(tree, page_start, page_end,
3249 EXTENT_DIRTY | EXTENT_DELALLOC |
3250 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3251 btrfs_finish_ordered_io(page->mapping->host,
3252 page_start, page_end);
3253 btrfs_put_ordered_extent(ordered);
3254 lock_extent(tree, page_start, page_end, GFP_NOFS);
3256 clear_extent_bit(tree, page_start, page_end,
3257 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3260 __btrfs_releasepage(page, GFP_NOFS);
3262 ClearPageChecked(page);
3263 if (PagePrivate(page)) {
3264 ClearPagePrivate(page);
3265 set_page_private(page, 0);
3266 page_cache_release(page);
3271 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3272 * called from a page fault handler when a page is first dirtied. Hence we must
3273 * be careful to check for EOF conditions here. We set the page up correctly
3274 * for a written page which means we get ENOSPC checking when writing into
3275 * holes and correct delalloc and unwritten extent mapping on filesystems that
3276 * support these features.
3278 * We are not allowed to take the i_mutex here so we have to play games to
3279 * protect against truncate races as the page could now be beyond EOF. Because
3280 * vmtruncate() writes the inode size before removing pages, once we have the
3281 * page lock we can determine safely if the page is beyond EOF. If it is not
3282 * beyond EOF, then the page is guaranteed safe against truncation until we
3285 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3287 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3288 struct btrfs_root *root = BTRFS_I(inode)->root;
3289 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3290 struct btrfs_ordered_extent *ordered;
3292 unsigned long zero_start;
3298 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3305 size = i_size_read(inode);
3306 page_start = page_offset(page);
3307 page_end = page_start + PAGE_CACHE_SIZE - 1;
3309 if ((page->mapping != inode->i_mapping) ||
3310 (page_start >= size)) {
3311 /* page got truncated out from underneath us */
3314 wait_on_page_writeback(page);
3316 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3317 set_page_extent_mapped(page);
3320 * we can't set the delalloc bits if there are pending ordered
3321 * extents. Drop our locks and wait for them to finish
3323 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3325 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3327 btrfs_start_ordered_extent(inode, ordered, 1);
3328 btrfs_put_ordered_extent(ordered);
3332 btrfs_set_extent_delalloc(inode, page_start, page_end);
3335 /* page is wholly or partially inside EOF */
3336 if (page_start + PAGE_CACHE_SIZE > size)
3337 zero_start = size & ~PAGE_CACHE_MASK;
3339 zero_start = PAGE_CACHE_SIZE;
3341 if (zero_start != PAGE_CACHE_SIZE) {
3343 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3344 flush_dcache_page(page);
3347 ClearPageChecked(page);
3348 set_page_dirty(page);
3349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3357 static void btrfs_truncate(struct inode *inode)
3359 struct btrfs_root *root = BTRFS_I(inode)->root;
3361 struct btrfs_trans_handle *trans;
3363 u64 mask = root->sectorsize - 1;
3365 if (!S_ISREG(inode->i_mode))
3367 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3370 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3371 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3373 trans = btrfs_start_transaction(root, 1);
3374 btrfs_set_trans_block_group(trans, inode);
3375 btrfs_i_size_write(inode, inode->i_size);
3377 ret = btrfs_orphan_add(trans, inode);
3380 /* FIXME, add redo link to tree so we don't leak on crash */
3381 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
3382 BTRFS_EXTENT_DATA_KEY);
3383 btrfs_update_inode(trans, root, inode);
3385 ret = btrfs_orphan_del(trans, inode);
3389 nr = trans->blocks_used;
3390 ret = btrfs_end_transaction_throttle(trans, root);
3392 btrfs_btree_balance_dirty(root, nr);
3396 * Invalidate a single dcache entry at the root of the filesystem.
3397 * Needed after creation of snapshot or subvolume.
3399 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3402 struct dentry *alias, *entry;
3405 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3409 /* change me if btrfs ever gets a d_hash operation */
3410 qstr.hash = full_name_hash(qstr.name, qstr.len);
3411 entry = d_lookup(alias, &qstr);
3414 d_invalidate(entry);
3421 * create a new subvolume directory/inode (helper for the ioctl).
3423 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3424 struct btrfs_trans_handle *trans, u64 new_dirid,
3425 struct btrfs_block_group_cache *block_group)
3427 struct inode *inode;
3430 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3431 new_dirid, block_group, S_IFDIR | 0700, &index);
3433 return PTR_ERR(inode);
3434 inode->i_op = &btrfs_dir_inode_operations;
3435 inode->i_fop = &btrfs_dir_file_operations;
3436 new_root->inode = inode;
3439 btrfs_i_size_write(inode, 0);
3441 return btrfs_update_inode(trans, new_root, inode);
3444 /* helper function for file defrag and space balancing. This
3445 * forces readahead on a given range of bytes in an inode
3447 unsigned long btrfs_force_ra(struct address_space *mapping,
3448 struct file_ra_state *ra, struct file *file,
3449 pgoff_t offset, pgoff_t last_index)
3451 pgoff_t req_size = last_index - offset + 1;
3453 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3454 return offset + req_size;
3457 struct inode *btrfs_alloc_inode(struct super_block *sb)
3459 struct btrfs_inode *ei;
3461 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3465 ei->logged_trans = 0;
3466 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3467 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3468 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3469 INIT_LIST_HEAD(&ei->i_orphan);
3470 return &ei->vfs_inode;
3473 void btrfs_destroy_inode(struct inode *inode)
3475 struct btrfs_ordered_extent *ordered;
3476 WARN_ON(!list_empty(&inode->i_dentry));
3477 WARN_ON(inode->i_data.nrpages);
3479 if (BTRFS_I(inode)->i_acl &&
3480 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3481 posix_acl_release(BTRFS_I(inode)->i_acl);
3482 if (BTRFS_I(inode)->i_default_acl &&
3483 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3484 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3486 spin_lock(&BTRFS_I(inode)->root->list_lock);
3487 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3488 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3489 " list\n", inode->i_ino);
3492 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3495 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3499 printk("found ordered extent %Lu %Lu\n",
3500 ordered->file_offset, ordered->len);
3501 btrfs_remove_ordered_extent(inode, ordered);
3502 btrfs_put_ordered_extent(ordered);
3503 btrfs_put_ordered_extent(ordered);
3506 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
3507 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3510 static void init_once(void *foo)
3512 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3514 inode_init_once(&ei->vfs_inode);
3517 void btrfs_destroy_cachep(void)
3519 if (btrfs_inode_cachep)
3520 kmem_cache_destroy(btrfs_inode_cachep);
3521 if (btrfs_trans_handle_cachep)
3522 kmem_cache_destroy(btrfs_trans_handle_cachep);
3523 if (btrfs_transaction_cachep)
3524 kmem_cache_destroy(btrfs_transaction_cachep);
3525 if (btrfs_bit_radix_cachep)
3526 kmem_cache_destroy(btrfs_bit_radix_cachep);
3527 if (btrfs_path_cachep)
3528 kmem_cache_destroy(btrfs_path_cachep);
3531 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3532 unsigned long extra_flags,
3533 void (*ctor)(void *))
3535 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3536 SLAB_MEM_SPREAD | extra_flags), ctor);
3539 int btrfs_init_cachep(void)
3541 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3542 sizeof(struct btrfs_inode),
3544 if (!btrfs_inode_cachep)
3546 btrfs_trans_handle_cachep =
3547 btrfs_cache_create("btrfs_trans_handle_cache",
3548 sizeof(struct btrfs_trans_handle),
3550 if (!btrfs_trans_handle_cachep)
3552 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3553 sizeof(struct btrfs_transaction),
3555 if (!btrfs_transaction_cachep)
3557 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3558 sizeof(struct btrfs_path),
3560 if (!btrfs_path_cachep)
3562 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3563 SLAB_DESTROY_BY_RCU, NULL);
3564 if (!btrfs_bit_radix_cachep)
3568 btrfs_destroy_cachep();
3572 static int btrfs_getattr(struct vfsmount *mnt,
3573 struct dentry *dentry, struct kstat *stat)
3575 struct inode *inode = dentry->d_inode;
3576 generic_fillattr(inode, stat);
3577 stat->blksize = PAGE_CACHE_SIZE;
3578 stat->blocks = (inode_get_bytes(inode) +
3579 BTRFS_I(inode)->delalloc_bytes) >> 9;
3583 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3584 struct inode * new_dir,struct dentry *new_dentry)
3586 struct btrfs_trans_handle *trans;
3587 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3588 struct inode *new_inode = new_dentry->d_inode;
3589 struct inode *old_inode = old_dentry->d_inode;
3590 struct timespec ctime = CURRENT_TIME;
3594 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3595 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3599 ret = btrfs_check_free_space(root, 1, 0);
3603 trans = btrfs_start_transaction(root, 1);
3605 btrfs_set_trans_block_group(trans, new_dir);
3607 btrfs_inc_nlink(old_dentry->d_inode);
3608 old_dir->i_ctime = old_dir->i_mtime = ctime;
3609 new_dir->i_ctime = new_dir->i_mtime = ctime;
3610 old_inode->i_ctime = ctime;
3612 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3613 old_dentry->d_name.name,
3614 old_dentry->d_name.len);
3619 new_inode->i_ctime = CURRENT_TIME;
3620 ret = btrfs_unlink_inode(trans, root, new_dir,
3621 new_dentry->d_inode,
3622 new_dentry->d_name.name,
3623 new_dentry->d_name.len);
3626 if (new_inode->i_nlink == 0) {
3627 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
3633 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
3637 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3638 old_inode, new_dentry->d_name.name,
3639 new_dentry->d_name.len, 1, index);
3644 btrfs_end_transaction_throttle(trans, root);
3650 * some fairly slow code that needs optimization. This walks the list
3651 * of all the inodes with pending delalloc and forces them to disk.
3653 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3655 struct list_head *head = &root->fs_info->delalloc_inodes;
3656 struct btrfs_inode *binode;
3657 struct inode *inode;
3658 unsigned long flags;
3660 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3661 while(!list_empty(head)) {
3662 binode = list_entry(head->next, struct btrfs_inode,
3664 inode = igrab(&binode->vfs_inode);
3666 list_del_init(&binode->delalloc_inodes);
3667 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3669 filemap_flush(inode->i_mapping);
3673 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3675 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3677 /* the filemap_flush will queue IO into the worker threads, but
3678 * we have to make sure the IO is actually started and that
3679 * ordered extents get created before we return
3681 atomic_inc(&root->fs_info->async_submit_draining);
3682 while(atomic_read(&root->fs_info->nr_async_submits)) {
3683 wait_event(root->fs_info->async_submit_wait,
3684 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3686 atomic_dec(&root->fs_info->async_submit_draining);
3690 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3691 const char *symname)
3693 struct btrfs_trans_handle *trans;
3694 struct btrfs_root *root = BTRFS_I(dir)->root;
3695 struct btrfs_path *path;
3696 struct btrfs_key key;
3697 struct inode *inode = NULL;
3705 struct btrfs_file_extent_item *ei;
3706 struct extent_buffer *leaf;
3707 unsigned long nr = 0;
3709 name_len = strlen(symname) + 1;
3710 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3711 return -ENAMETOOLONG;
3713 err = btrfs_check_free_space(root, 1, 0);
3717 trans = btrfs_start_transaction(root, 1);
3718 btrfs_set_trans_block_group(trans, dir);
3720 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3726 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3728 dentry->d_parent->d_inode->i_ino, objectid,
3729 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3731 err = PTR_ERR(inode);
3735 err = btrfs_init_acl(inode, dir);
3741 btrfs_set_trans_block_group(trans, inode);
3742 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3746 inode->i_mapping->a_ops = &btrfs_aops;
3747 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3748 inode->i_fop = &btrfs_file_operations;
3749 inode->i_op = &btrfs_file_inode_operations;
3750 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3752 dir->i_sb->s_dirt = 1;
3753 btrfs_update_inode_block_group(trans, inode);
3754 btrfs_update_inode_block_group(trans, dir);
3758 path = btrfs_alloc_path();
3760 key.objectid = inode->i_ino;
3762 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3763 datasize = btrfs_file_extent_calc_inline_size(name_len);
3764 err = btrfs_insert_empty_item(trans, root, path, &key,
3770 leaf = path->nodes[0];
3771 ei = btrfs_item_ptr(leaf, path->slots[0],
3772 struct btrfs_file_extent_item);
3773 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3774 btrfs_set_file_extent_type(leaf, ei,
3775 BTRFS_FILE_EXTENT_INLINE);
3776 ptr = btrfs_file_extent_inline_start(ei);
3777 write_extent_buffer(leaf, symname, ptr, name_len);
3778 btrfs_mark_buffer_dirty(leaf);
3779 btrfs_free_path(path);
3781 inode->i_op = &btrfs_symlink_inode_operations;
3782 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3783 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3784 btrfs_i_size_write(inode, name_len - 1);
3785 err = btrfs_update_inode(trans, root, inode);
3790 nr = trans->blocks_used;
3791 btrfs_end_transaction_throttle(trans, root);
3794 inode_dec_link_count(inode);
3797 btrfs_btree_balance_dirty(root, nr);
3801 static int btrfs_set_page_dirty(struct page *page)
3803 return __set_page_dirty_nobuffers(page);
3806 static int btrfs_permission(struct inode *inode, int mask)
3808 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3810 return generic_permission(inode, mask, btrfs_check_acl);
3813 static struct inode_operations btrfs_dir_inode_operations = {
3814 .lookup = btrfs_lookup,
3815 .create = btrfs_create,
3816 .unlink = btrfs_unlink,
3818 .mkdir = btrfs_mkdir,
3819 .rmdir = btrfs_rmdir,
3820 .rename = btrfs_rename,
3821 .symlink = btrfs_symlink,
3822 .setattr = btrfs_setattr,
3823 .mknod = btrfs_mknod,
3824 .setxattr = btrfs_setxattr,
3825 .getxattr = btrfs_getxattr,
3826 .listxattr = btrfs_listxattr,
3827 .removexattr = btrfs_removexattr,
3828 .permission = btrfs_permission,
3830 static struct inode_operations btrfs_dir_ro_inode_operations = {
3831 .lookup = btrfs_lookup,
3832 .permission = btrfs_permission,
3834 static struct file_operations btrfs_dir_file_operations = {
3835 .llseek = generic_file_llseek,
3836 .read = generic_read_dir,
3837 .readdir = btrfs_real_readdir,
3838 .unlocked_ioctl = btrfs_ioctl,
3839 #ifdef CONFIG_COMPAT
3840 .compat_ioctl = btrfs_ioctl,
3842 .release = btrfs_release_file,
3843 .fsync = btrfs_sync_file,
3846 static struct extent_io_ops btrfs_extent_io_ops = {
3847 .fill_delalloc = run_delalloc_range,
3848 .submit_bio_hook = btrfs_submit_bio_hook,
3849 .merge_bio_hook = btrfs_merge_bio_hook,
3850 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3851 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3852 .writepage_start_hook = btrfs_writepage_start_hook,
3853 .readpage_io_failed_hook = btrfs_io_failed_hook,
3854 .set_bit_hook = btrfs_set_bit_hook,
3855 .clear_bit_hook = btrfs_clear_bit_hook,
3858 static struct address_space_operations btrfs_aops = {
3859 .readpage = btrfs_readpage,
3860 .writepage = btrfs_writepage,
3861 .writepages = btrfs_writepages,
3862 .readpages = btrfs_readpages,
3863 .sync_page = block_sync_page,
3865 .direct_IO = btrfs_direct_IO,
3866 .invalidatepage = btrfs_invalidatepage,
3867 .releasepage = btrfs_releasepage,
3868 .set_page_dirty = btrfs_set_page_dirty,
3871 static struct address_space_operations btrfs_symlink_aops = {
3872 .readpage = btrfs_readpage,
3873 .writepage = btrfs_writepage,
3874 .invalidatepage = btrfs_invalidatepage,
3875 .releasepage = btrfs_releasepage,
3878 static struct inode_operations btrfs_file_inode_operations = {
3879 .truncate = btrfs_truncate,
3880 .getattr = btrfs_getattr,
3881 .setattr = btrfs_setattr,
3882 .setxattr = btrfs_setxattr,
3883 .getxattr = btrfs_getxattr,
3884 .listxattr = btrfs_listxattr,
3885 .removexattr = btrfs_removexattr,
3886 .permission = btrfs_permission,
3888 static struct inode_operations btrfs_special_inode_operations = {
3889 .getattr = btrfs_getattr,
3890 .setattr = btrfs_setattr,
3891 .permission = btrfs_permission,
3892 .setxattr = btrfs_setxattr,
3893 .getxattr = btrfs_getxattr,
3894 .listxattr = btrfs_listxattr,
3895 .removexattr = btrfs_removexattr,
3897 static struct inode_operations btrfs_symlink_inode_operations = {
3898 .readlink = generic_readlink,
3899 .follow_link = page_follow_link_light,
3900 .put_link = page_put_link,
3901 .permission = btrfs_permission,