2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
41 #include "transaction.h"
42 #include "btrfs_inode.h"
44 #include "print-tree.h"
47 struct btrfs_iget_args {
49 struct btrfs_root *root;
52 static struct inode_operations btrfs_dir_inode_operations;
53 static struct inode_operations btrfs_symlink_inode_operations;
54 static struct inode_operations btrfs_dir_ro_inode_operations;
55 static struct inode_operations btrfs_special_inode_operations;
56 static struct inode_operations btrfs_file_inode_operations;
57 static struct address_space_operations btrfs_aops;
58 static struct address_space_operations btrfs_symlink_aops;
59 static struct file_operations btrfs_dir_file_operations;
60 static struct extent_io_ops btrfs_extent_io_ops;
62 static struct kmem_cache *btrfs_inode_cachep;
63 struct kmem_cache *btrfs_trans_handle_cachep;
64 struct kmem_cache *btrfs_transaction_cachep;
65 struct kmem_cache *btrfs_bit_radix_cachep;
66 struct kmem_cache *btrfs_path_cachep;
69 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
70 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
71 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
72 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
73 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
74 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
75 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
76 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
88 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
89 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
90 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
98 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
100 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
104 static int cow_file_range(struct inode *inode, u64 start, u64 end)
106 struct btrfs_root *root = BTRFS_I(inode)->root;
107 struct btrfs_trans_handle *trans;
111 u64 blocksize = root->sectorsize;
112 u64 orig_start = start;
114 struct btrfs_key ins;
117 trans = btrfs_start_transaction(root, 1);
119 btrfs_set_trans_block_group(trans, inode);
121 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
122 num_bytes = max(blocksize, num_bytes);
123 ret = btrfs_drop_extents(trans, root, inode,
124 start, start + num_bytes, start, &alloc_hint);
125 orig_num_bytes = num_bytes;
127 if (alloc_hint == EXTENT_MAP_INLINE)
130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
132 while(num_bytes > 0) {
133 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
134 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
136 root->root_key.objectid,
138 inode->i_ino, start, 0,
139 alloc_hint, (u64)-1, &ins, 1);
144 cur_alloc_size = ins.offset;
145 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
146 start, ins.objectid, ins.offset,
148 inode->i_blocks += ins.offset >> 9;
149 btrfs_check_file(root, inode);
150 if (num_bytes < cur_alloc_size) {
151 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
155 num_bytes -= cur_alloc_size;
156 alloc_hint = ins.objectid + ins.offset;
157 start += cur_alloc_size;
159 btrfs_drop_extent_cache(inode, orig_start,
160 orig_start + orig_num_bytes - 1);
161 btrfs_add_ordered_inode(inode);
162 btrfs_update_inode(trans, root, inode);
164 btrfs_end_transaction(trans, root);
168 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
176 struct btrfs_root *root = BTRFS_I(inode)->root;
177 struct btrfs_block_group_cache *block_group;
178 struct extent_buffer *leaf;
180 struct btrfs_path *path;
181 struct btrfs_file_extent_item *item;
184 struct btrfs_key found_key;
186 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
187 path = btrfs_alloc_path();
190 ret = btrfs_lookup_file_extent(NULL, root, path,
191 inode->i_ino, start, 0);
193 btrfs_free_path(path);
199 if (path->slots[0] == 0)
204 leaf = path->nodes[0];
205 item = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
208 /* are we inside the extent that was found? */
209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
210 found_type = btrfs_key_type(&found_key);
211 if (found_key.objectid != inode->i_ino ||
212 found_type != BTRFS_EXTENT_DATA_KEY)
215 found_type = btrfs_file_extent_type(leaf, item);
216 extent_start = found_key.offset;
217 if (found_type == BTRFS_FILE_EXTENT_REG) {
218 u64 extent_num_bytes;
220 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
221 extent_end = extent_start + extent_num_bytes;
224 if (loops && start != extent_start)
227 if (start < extent_start || start >= extent_end)
230 cow_end = min(end, extent_end - 1);
231 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
235 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
241 * we may be called by the resizer, make sure we're inside
242 * the limits of the FS
244 block_group = btrfs_lookup_block_group(root->fs_info,
246 if (!block_group || block_group->ro)
255 btrfs_free_path(path);
258 btrfs_release_path(root, path);
263 cow_file_range(inode, start, end);
268 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
270 struct btrfs_root *root = BTRFS_I(inode)->root;
273 if (btrfs_test_opt(root, NODATACOW) ||
274 btrfs_test_flag(inode, NODATACOW))
275 ret = run_delalloc_nocow(inode, start, end);
277 ret = cow_file_range(inode, start, end);
282 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
283 unsigned long old, unsigned long bits)
286 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
287 struct btrfs_root *root = BTRFS_I(inode)->root;
288 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
289 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
290 root->fs_info->delalloc_bytes += end - start + 1;
291 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
296 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
297 unsigned long old, unsigned long bits)
299 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
300 struct btrfs_root *root = BTRFS_I(inode)->root;
303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
304 if (end - start + 1 > root->fs_info->delalloc_bytes) {
305 printk("warning: delalloc account %Lu %Lu\n",
306 end - start + 1, root->fs_info->delalloc_bytes);
307 root->fs_info->delalloc_bytes = 0;
308 BTRFS_I(inode)->delalloc_bytes = 0;
310 root->fs_info->delalloc_bytes -= end - start + 1;
311 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
313 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
318 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
319 size_t size, struct bio *bio)
321 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
322 struct btrfs_mapping_tree *map_tree;
323 u64 logical = bio->bi_sector << 9;
328 length = bio->bi_size;
329 map_tree = &root->fs_info->mapping_tree;
331 ret = btrfs_map_block(map_tree, READ, logical,
332 &map_length, NULL, 0);
334 if (map_length < length + size) {
340 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
343 struct btrfs_root *root = BTRFS_I(inode)->root;
344 struct btrfs_trans_handle *trans;
348 ret = btrfs_csum_one_bio(root, bio, &sums);
351 trans = btrfs_start_transaction(root, 1);
353 btrfs_set_trans_block_group(trans, inode);
354 mutex_lock(&BTRFS_I(inode)->csum_mutex);
355 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
356 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
358 ret = btrfs_end_transaction(trans, root);
363 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
366 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
369 struct btrfs_root *root = BTRFS_I(inode)->root;
372 if (!(rw & (1 << BIO_RW))) {
373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
378 if (btrfs_test_opt(root, NODATASUM) ||
379 btrfs_test_flag(inode, NODATASUM)) {
383 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
384 inode, rw, bio, mirror_num,
385 __btrfs_submit_bio_hook);
387 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
390 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
393 struct inode *inode = page->mapping->host;
394 struct btrfs_root *root = BTRFS_I(inode)->root;
395 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
396 struct btrfs_csum_item *item;
397 struct btrfs_path *path = NULL;
400 if (btrfs_test_opt(root, NODATASUM) ||
401 btrfs_test_flag(inode, NODATASUM))
404 path = btrfs_alloc_path();
405 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
408 /* a csum that isn't present is a preallocated region. */
409 if (ret == -ENOENT || ret == -EFBIG)
412 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
415 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
417 set_state_private(io_tree, start, csum);
420 btrfs_free_path(path);
424 struct io_failure_record {
432 int btrfs_io_failed_hook(struct bio *failed_bio,
433 struct page *page, u64 start, u64 end,
434 struct extent_state *state)
436 struct io_failure_record *failrec = NULL;
438 struct extent_map *em;
439 struct inode *inode = page->mapping->host;
440 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
441 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
448 ret = get_state_private(failure_tree, start, &private);
450 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
453 failrec->start = start;
454 failrec->len = end - start + 1;
455 failrec->last_mirror = 0;
457 spin_lock(&em_tree->lock);
458 em = lookup_extent_mapping(em_tree, start, failrec->len);
459 if (em->start > start || em->start + em->len < start) {
463 spin_unlock(&em_tree->lock);
465 if (!em || IS_ERR(em)) {
469 logical = start - em->start;
470 logical = em->block_start + logical;
471 failrec->logical = logical;
473 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
474 EXTENT_DIRTY, GFP_NOFS);
475 set_state_private(failure_tree, start,
476 (u64)(unsigned long)failrec);
478 failrec = (struct io_failure_record *)(unsigned long)private;
480 num_copies = btrfs_num_copies(
481 &BTRFS_I(inode)->root->fs_info->mapping_tree,
482 failrec->logical, failrec->len);
483 failrec->last_mirror++;
485 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
486 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
489 if (state && state->start != failrec->start)
491 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
493 if (!state || failrec->last_mirror > num_copies) {
494 set_state_private(failure_tree, failrec->start, 0);
495 clear_extent_bits(failure_tree, failrec->start,
496 failrec->start + failrec->len - 1,
497 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
501 bio = bio_alloc(GFP_NOFS, 1);
502 bio->bi_private = state;
503 bio->bi_end_io = failed_bio->bi_end_io;
504 bio->bi_sector = failrec->logical >> 9;
505 bio->bi_bdev = failed_bio->bi_bdev;
507 bio_add_page(bio, page, failrec->len, start - page_offset(page));
508 if (failed_bio->bi_rw & (1 << BIO_RW))
513 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
514 failrec->last_mirror);
518 int btrfs_clean_io_failures(struct inode *inode, u64 start)
522 struct io_failure_record *failure;
526 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
527 (u64)-1, 1, EXTENT_DIRTY)) {
528 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
529 start, &private_failure);
531 failure = (struct io_failure_record *)(unsigned long)
533 set_state_private(&BTRFS_I(inode)->io_failure_tree,
535 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
537 failure->start + failure->len - 1,
538 EXTENT_DIRTY | EXTENT_LOCKED,
546 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
547 struct extent_state *state)
549 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
550 struct inode *inode = page->mapping->host;
551 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
553 u64 private = ~(u32)0;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
559 if (btrfs_test_opt(root, NODATASUM) ||
560 btrfs_test_flag(inode, NODATASUM))
562 if (state && state->start == start) {
563 private = state->private;
566 ret = get_state_private(io_tree, start, &private);
568 local_irq_save(flags);
569 kaddr = kmap_atomic(page, KM_IRQ0);
573 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
574 btrfs_csum_final(csum, (char *)&csum);
575 if (csum != private) {
578 kunmap_atomic(kaddr, KM_IRQ0);
579 local_irq_restore(flags);
581 /* if the io failure tree for this inode is non-empty,
582 * check to see if we've recovered from a failed IO
584 btrfs_clean_io_failures(inode, start);
588 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
589 page->mapping->host->i_ino, (unsigned long long)start, csum,
591 memset(kaddr + offset, 1, end - start + 1);
592 flush_dcache_page(page);
593 kunmap_atomic(kaddr, KM_IRQ0);
594 local_irq_restore(flags);
600 void btrfs_read_locked_inode(struct inode *inode)
602 struct btrfs_path *path;
603 struct extent_buffer *leaf;
604 struct btrfs_inode_item *inode_item;
605 struct btrfs_timespec *tspec;
606 struct btrfs_root *root = BTRFS_I(inode)->root;
607 struct btrfs_key location;
608 u64 alloc_group_block;
612 path = btrfs_alloc_path();
614 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
616 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
620 leaf = path->nodes[0];
621 inode_item = btrfs_item_ptr(leaf, path->slots[0],
622 struct btrfs_inode_item);
624 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
625 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
626 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
627 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
628 inode->i_size = btrfs_inode_size(leaf, inode_item);
630 tspec = btrfs_inode_atime(inode_item);
631 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
632 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
634 tspec = btrfs_inode_mtime(inode_item);
635 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
636 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
638 tspec = btrfs_inode_ctime(inode_item);
639 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
640 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
642 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
643 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
645 rdev = btrfs_inode_rdev(leaf, inode_item);
647 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
648 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
650 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
651 if (!BTRFS_I(inode)->block_group) {
652 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
654 BTRFS_BLOCK_GROUP_METADATA, 0);
656 btrfs_free_path(path);
659 switch (inode->i_mode & S_IFMT) {
661 inode->i_mapping->a_ops = &btrfs_aops;
662 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
663 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
664 inode->i_fop = &btrfs_file_operations;
665 inode->i_op = &btrfs_file_inode_operations;
668 inode->i_fop = &btrfs_dir_file_operations;
669 if (root == root->fs_info->tree_root)
670 inode->i_op = &btrfs_dir_ro_inode_operations;
672 inode->i_op = &btrfs_dir_inode_operations;
675 inode->i_op = &btrfs_symlink_inode_operations;
676 inode->i_mapping->a_ops = &btrfs_symlink_aops;
677 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
680 init_special_inode(inode, inode->i_mode, rdev);
686 btrfs_free_path(path);
687 make_bad_inode(inode);
690 static void fill_inode_item(struct extent_buffer *leaf,
691 struct btrfs_inode_item *item,
694 btrfs_set_inode_uid(leaf, item, inode->i_uid);
695 btrfs_set_inode_gid(leaf, item, inode->i_gid);
696 btrfs_set_inode_size(leaf, item, inode->i_size);
697 btrfs_set_inode_mode(leaf, item, inode->i_mode);
698 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
700 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
701 inode->i_atime.tv_sec);
702 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
703 inode->i_atime.tv_nsec);
705 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
706 inode->i_mtime.tv_sec);
707 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
708 inode->i_mtime.tv_nsec);
710 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
711 inode->i_ctime.tv_sec);
712 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
713 inode->i_ctime.tv_nsec);
715 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
716 btrfs_set_inode_generation(leaf, item, inode->i_generation);
717 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
718 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
719 btrfs_set_inode_block_group(leaf, item,
720 BTRFS_I(inode)->block_group->key.objectid);
723 int btrfs_update_inode(struct btrfs_trans_handle *trans,
724 struct btrfs_root *root,
727 struct btrfs_inode_item *inode_item;
728 struct btrfs_path *path;
729 struct extent_buffer *leaf;
732 path = btrfs_alloc_path();
734 ret = btrfs_lookup_inode(trans, root, path,
735 &BTRFS_I(inode)->location, 1);
742 leaf = path->nodes[0];
743 inode_item = btrfs_item_ptr(leaf, path->slots[0],
744 struct btrfs_inode_item);
746 fill_inode_item(leaf, inode_item, inode);
747 btrfs_mark_buffer_dirty(leaf);
748 btrfs_set_inode_last_trans(trans, inode);
751 btrfs_free_path(path);
756 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
757 struct btrfs_root *root,
759 struct dentry *dentry)
761 struct btrfs_path *path;
762 const char *name = dentry->d_name.name;
763 int name_len = dentry->d_name.len;
765 struct extent_buffer *leaf;
766 struct btrfs_dir_item *di;
767 struct btrfs_key key;
769 path = btrfs_alloc_path();
775 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
785 leaf = path->nodes[0];
786 btrfs_dir_item_key_to_cpu(leaf, di, &key);
787 ret = btrfs_delete_one_dir_name(trans, root, path, di);
790 btrfs_release_path(root, path);
792 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
793 key.objectid, name, name_len, -1);
802 ret = btrfs_delete_one_dir_name(trans, root, path, di);
803 btrfs_release_path(root, path);
805 dentry->d_inode->i_ctime = dir->i_ctime;
806 ret = btrfs_del_inode_ref(trans, root, name, name_len,
807 dentry->d_inode->i_ino,
808 dentry->d_parent->d_inode->i_ino);
810 printk("failed to delete reference to %.*s, "
811 "inode %lu parent %lu\n", name_len, name,
812 dentry->d_inode->i_ino,
813 dentry->d_parent->d_inode->i_ino);
816 btrfs_free_path(path);
818 dir->i_size -= name_len * 2;
819 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
820 btrfs_update_inode(trans, root, dir);
821 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
822 dentry->d_inode->i_nlink--;
824 drop_nlink(dentry->d_inode);
826 ret = btrfs_update_inode(trans, root, dentry->d_inode);
827 dir->i_sb->s_dirt = 1;
832 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
834 struct btrfs_root *root;
835 struct btrfs_trans_handle *trans;
836 struct inode *inode = dentry->d_inode;
838 unsigned long nr = 0;
840 root = BTRFS_I(dir)->root;
842 ret = btrfs_check_free_space(root, 1, 1);
846 trans = btrfs_start_transaction(root, 1);
848 btrfs_set_trans_block_group(trans, dir);
849 ret = btrfs_unlink_trans(trans, root, dir, dentry);
850 nr = trans->blocks_used;
852 if (inode->i_nlink == 0) {
853 /* if the inode isn't linked anywhere,
854 * we don't need to worry about
857 btrfs_del_ordered_inode(inode, 1);
860 btrfs_end_transaction_throttle(trans, root);
862 btrfs_btree_balance_dirty(root, nr);
866 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
868 struct inode *inode = dentry->d_inode;
871 struct btrfs_root *root = BTRFS_I(dir)->root;
872 struct btrfs_trans_handle *trans;
873 unsigned long nr = 0;
875 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
879 ret = btrfs_check_free_space(root, 1, 1);
883 trans = btrfs_start_transaction(root, 1);
884 btrfs_set_trans_block_group(trans, dir);
886 /* now the directory is empty */
887 err = btrfs_unlink_trans(trans, root, dir, dentry);
892 nr = trans->blocks_used;
893 ret = btrfs_end_transaction_throttle(trans, root);
895 btrfs_btree_balance_dirty(root, nr);
903 * this can truncate away extent items, csum items and directory items.
904 * It starts at a high offset and removes keys until it can't find
905 * any higher than i_size.
907 * csum items that cross the new i_size are truncated to the new size
910 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
911 struct btrfs_root *root,
916 struct btrfs_path *path;
917 struct btrfs_key key;
918 struct btrfs_key found_key;
920 struct extent_buffer *leaf;
921 struct btrfs_file_extent_item *fi;
922 u64 extent_start = 0;
923 u64 extent_num_bytes = 0;
929 int pending_del_nr = 0;
930 int pending_del_slot = 0;
931 int extent_type = -1;
932 u64 mask = root->sectorsize - 1;
934 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
935 path = btrfs_alloc_path();
939 /* FIXME, add redo link to tree so we don't leak on crash */
940 key.objectid = inode->i_ino;
941 key.offset = (u64)-1;
944 btrfs_init_path(path);
946 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
951 BUG_ON(path->slots[0] == 0);
957 leaf = path->nodes[0];
958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
959 found_type = btrfs_key_type(&found_key);
961 if (found_key.objectid != inode->i_ino)
964 if (found_type < min_type)
967 item_end = found_key.offset;
968 if (found_type == BTRFS_EXTENT_DATA_KEY) {
969 fi = btrfs_item_ptr(leaf, path->slots[0],
970 struct btrfs_file_extent_item);
971 extent_type = btrfs_file_extent_type(leaf, fi);
972 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
974 btrfs_file_extent_num_bytes(leaf, fi);
975 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
976 struct btrfs_item *item = btrfs_item_nr(leaf,
978 item_end += btrfs_file_extent_inline_len(leaf,
983 if (found_type == BTRFS_CSUM_ITEM_KEY) {
984 ret = btrfs_csum_truncate(trans, root, path,
988 if (item_end < inode->i_size) {
989 if (found_type == BTRFS_DIR_ITEM_KEY) {
990 found_type = BTRFS_INODE_ITEM_KEY;
991 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
992 found_type = BTRFS_CSUM_ITEM_KEY;
993 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
994 found_type = BTRFS_XATTR_ITEM_KEY;
995 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
996 found_type = BTRFS_INODE_REF_KEY;
997 } else if (found_type) {
1002 btrfs_set_key_type(&key, found_type);
1005 if (found_key.offset >= inode->i_size)
1011 /* FIXME, shrink the extent if the ref count is only 1 */
1012 if (found_type != BTRFS_EXTENT_DATA_KEY)
1015 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1017 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1019 u64 orig_num_bytes =
1020 btrfs_file_extent_num_bytes(leaf, fi);
1021 extent_num_bytes = inode->i_size -
1022 found_key.offset + root->sectorsize - 1;
1023 extent_num_bytes = extent_num_bytes &
1024 ~((u64)root->sectorsize - 1);
1025 btrfs_set_file_extent_num_bytes(leaf, fi,
1027 num_dec = (orig_num_bytes -
1029 if (extent_start != 0)
1030 dec_i_blocks(inode, num_dec);
1031 btrfs_mark_buffer_dirty(leaf);
1034 btrfs_file_extent_disk_num_bytes(leaf,
1036 /* FIXME blocksize != 4096 */
1037 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1038 if (extent_start != 0) {
1040 dec_i_blocks(inode, num_dec);
1042 root_gen = btrfs_header_generation(leaf);
1043 root_owner = btrfs_header_owner(leaf);
1045 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1047 u32 newsize = inode->i_size - found_key.offset;
1048 dec_i_blocks(inode, item_end + 1 -
1049 found_key.offset - newsize);
1051 btrfs_file_extent_calc_inline_size(newsize);
1052 ret = btrfs_truncate_item(trans, root, path,
1056 dec_i_blocks(inode, item_end + 1 -
1062 if (!pending_del_nr) {
1063 /* no pending yet, add ourselves */
1064 pending_del_slot = path->slots[0];
1066 } else if (pending_del_nr &&
1067 path->slots[0] + 1 == pending_del_slot) {
1068 /* hop on the pending chunk */
1070 pending_del_slot = path->slots[0];
1072 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1078 ret = btrfs_free_extent(trans, root, extent_start,
1081 root_gen, inode->i_ino,
1082 found_key.offset, 0);
1086 if (path->slots[0] == 0) {
1089 btrfs_release_path(root, path);
1094 if (pending_del_nr &&
1095 path->slots[0] + 1 != pending_del_slot) {
1096 struct btrfs_key debug;
1098 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1100 ret = btrfs_del_items(trans, root, path,
1105 btrfs_release_path(root, path);
1111 if (pending_del_nr) {
1112 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1115 btrfs_free_path(path);
1116 inode->i_sb->s_dirt = 1;
1120 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1124 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1125 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1126 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1129 WARN_ON(!PageLocked(page));
1130 set_page_extent_mapped(page);
1132 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1133 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1134 page_end, GFP_NOFS);
1136 if (zero_start != PAGE_CACHE_SIZE) {
1138 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1139 flush_dcache_page(page);
1142 set_page_dirty(page);
1143 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1149 * taken from block_truncate_page, but does cow as it zeros out
1150 * any bytes left in the last page in the file.
1152 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1154 struct inode *inode = mapping->host;
1155 struct btrfs_root *root = BTRFS_I(inode)->root;
1156 u32 blocksize = root->sectorsize;
1157 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1158 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1163 if ((offset & (blocksize - 1)) == 0)
1168 page = grab_cache_page(mapping, index);
1171 if (!PageUptodate(page)) {
1172 ret = btrfs_readpage(NULL, page);
1174 if (page->mapping != mapping) {
1176 page_cache_release(page);
1179 if (!PageUptodate(page)) {
1185 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1186 wait_on_page_writeback(page);
1187 ret = btrfs_cow_one_page(inode, page, offset);
1190 page_cache_release(page);
1195 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1197 struct inode *inode = dentry->d_inode;
1200 err = inode_change_ok(inode, attr);
1204 if (S_ISREG(inode->i_mode) &&
1205 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1206 struct btrfs_trans_handle *trans;
1207 struct btrfs_root *root = BTRFS_I(inode)->root;
1208 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1210 u64 mask = root->sectorsize - 1;
1211 u64 hole_start = (inode->i_size + mask) & ~mask;
1212 u64 block_end = (attr->ia_size + mask) & ~mask;
1216 if (attr->ia_size <= hole_start)
1219 err = btrfs_check_free_space(root, 1, 0);
1223 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1225 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1226 hole_size = block_end - hole_start;
1228 trans = btrfs_start_transaction(root, 1);
1229 btrfs_set_trans_block_group(trans, inode);
1230 err = btrfs_drop_extents(trans, root, inode,
1231 hole_start, block_end, hole_start,
1234 if (alloc_hint != EXTENT_MAP_INLINE) {
1235 err = btrfs_insert_file_extent(trans, root,
1239 btrfs_drop_extent_cache(inode, hole_start,
1241 btrfs_check_file(root, inode);
1243 btrfs_end_transaction(trans, root);
1244 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1249 err = inode_setattr(inode, attr);
1254 void btrfs_delete_inode(struct inode *inode)
1256 struct btrfs_trans_handle *trans;
1257 struct btrfs_root *root = BTRFS_I(inode)->root;
1261 truncate_inode_pages(&inode->i_data, 0);
1262 if (is_bad_inode(inode)) {
1267 trans = btrfs_start_transaction(root, 1);
1269 btrfs_set_trans_block_group(trans, inode);
1270 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1272 goto no_delete_lock;
1274 nr = trans->blocks_used;
1277 btrfs_end_transaction(trans, root);
1278 btrfs_btree_balance_dirty(root, nr);
1282 nr = trans->blocks_used;
1283 btrfs_end_transaction(trans, root);
1284 btrfs_btree_balance_dirty(root, nr);
1290 * this returns the key found in the dir entry in the location pointer.
1291 * If no dir entries were found, location->objectid is 0.
1293 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1294 struct btrfs_key *location)
1296 const char *name = dentry->d_name.name;
1297 int namelen = dentry->d_name.len;
1298 struct btrfs_dir_item *di;
1299 struct btrfs_path *path;
1300 struct btrfs_root *root = BTRFS_I(dir)->root;
1303 if (namelen == 1 && strcmp(name, ".") == 0) {
1304 location->objectid = dir->i_ino;
1305 location->type = BTRFS_INODE_ITEM_KEY;
1306 location->offset = 0;
1309 path = btrfs_alloc_path();
1312 if (namelen == 2 && strcmp(name, "..") == 0) {
1313 struct btrfs_key key;
1314 struct extent_buffer *leaf;
1318 key.objectid = dir->i_ino;
1319 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1325 leaf = path->nodes[0];
1326 slot = path->slots[0];
1327 nritems = btrfs_header_nritems(leaf);
1328 if (slot >= nritems)
1331 btrfs_item_key_to_cpu(leaf, &key, slot);
1332 if (key.objectid != dir->i_ino ||
1333 key.type != BTRFS_INODE_REF_KEY) {
1336 location->objectid = key.offset;
1337 location->type = BTRFS_INODE_ITEM_KEY;
1338 location->offset = 0;
1342 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1346 if (!di || IS_ERR(di)) {
1349 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1351 btrfs_free_path(path);
1354 location->objectid = 0;
1359 * when we hit a tree root in a directory, the btrfs part of the inode
1360 * needs to be changed to reflect the root directory of the tree root. This
1361 * is kind of like crossing a mount point.
1363 static int fixup_tree_root_location(struct btrfs_root *root,
1364 struct btrfs_key *location,
1365 struct btrfs_root **sub_root,
1366 struct dentry *dentry)
1368 struct btrfs_path *path;
1369 struct btrfs_root_item *ri;
1371 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1373 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1376 path = btrfs_alloc_path();
1379 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1380 dentry->d_name.name,
1381 dentry->d_name.len);
1382 if (IS_ERR(*sub_root))
1383 return PTR_ERR(*sub_root);
1385 ri = &(*sub_root)->root_item;
1386 location->objectid = btrfs_root_dirid(ri);
1387 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1388 location->offset = 0;
1390 btrfs_free_path(path);
1394 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1396 struct btrfs_iget_args *args = p;
1397 inode->i_ino = args->ino;
1398 BTRFS_I(inode)->root = args->root;
1399 BTRFS_I(inode)->delalloc_bytes = 0;
1400 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1401 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1402 inode->i_mapping, GFP_NOFS);
1403 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1404 inode->i_mapping, GFP_NOFS);
1405 mutex_init(&BTRFS_I(inode)->csum_mutex);
1406 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1410 static int btrfs_find_actor(struct inode *inode, void *opaque)
1412 struct btrfs_iget_args *args = opaque;
1413 return (args->ino == inode->i_ino &&
1414 args->root == BTRFS_I(inode)->root);
1417 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1420 struct btrfs_iget_args args;
1421 args.ino = objectid;
1422 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1427 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1430 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1431 struct btrfs_root *root)
1433 struct inode *inode;
1434 struct btrfs_iget_args args;
1435 args.ino = objectid;
1438 inode = iget5_locked(s, objectid, btrfs_find_actor,
1439 btrfs_init_locked_inode,
1444 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1445 struct nameidata *nd)
1447 struct inode * inode;
1448 struct btrfs_inode *bi = BTRFS_I(dir);
1449 struct btrfs_root *root = bi->root;
1450 struct btrfs_root *sub_root = root;
1451 struct btrfs_key location;
1454 if (dentry->d_name.len > BTRFS_NAME_LEN)
1455 return ERR_PTR(-ENAMETOOLONG);
1457 ret = btrfs_inode_by_name(dir, dentry, &location);
1460 return ERR_PTR(ret);
1463 if (location.objectid) {
1464 ret = fixup_tree_root_location(root, &location, &sub_root,
1467 return ERR_PTR(ret);
1469 return ERR_PTR(-ENOENT);
1470 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1473 return ERR_PTR(-EACCES);
1474 if (inode->i_state & I_NEW) {
1475 /* the inode and parent dir are two different roots */
1476 if (sub_root != root) {
1478 sub_root->inode = inode;
1480 BTRFS_I(inode)->root = sub_root;
1481 memcpy(&BTRFS_I(inode)->location, &location,
1483 btrfs_read_locked_inode(inode);
1484 unlock_new_inode(inode);
1487 return d_splice_alias(inode, dentry);
1490 static unsigned char btrfs_filetype_table[] = {
1491 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1494 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1496 struct inode *inode = filp->f_dentry->d_inode;
1497 struct btrfs_root *root = BTRFS_I(inode)->root;
1498 struct btrfs_item *item;
1499 struct btrfs_dir_item *di;
1500 struct btrfs_key key;
1501 struct btrfs_key found_key;
1502 struct btrfs_path *path;
1505 struct extent_buffer *leaf;
1508 unsigned char d_type;
1513 int key_type = BTRFS_DIR_INDEX_KEY;
1518 /* FIXME, use a real flag for deciding about the key type */
1519 if (root->fs_info->tree_root == root)
1520 key_type = BTRFS_DIR_ITEM_KEY;
1522 /* special case for "." */
1523 if (filp->f_pos == 0) {
1524 over = filldir(dirent, ".", 1,
1532 key.objectid = inode->i_ino;
1533 path = btrfs_alloc_path();
1536 /* special case for .., just use the back ref */
1537 if (filp->f_pos == 1) {
1538 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1540 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1542 leaf = path->nodes[0];
1543 slot = path->slots[0];
1544 nritems = btrfs_header_nritems(leaf);
1545 if (slot >= nritems) {
1546 btrfs_release_path(root, path);
1547 goto read_dir_items;
1549 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1550 btrfs_release_path(root, path);
1551 if (found_key.objectid != key.objectid ||
1552 found_key.type != BTRFS_INODE_REF_KEY)
1553 goto read_dir_items;
1554 over = filldir(dirent, "..", 2,
1555 2, found_key.offset, DT_DIR);
1562 btrfs_set_key_type(&key, key_type);
1563 key.offset = filp->f_pos;
1565 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1570 leaf = path->nodes[0];
1571 nritems = btrfs_header_nritems(leaf);
1572 slot = path->slots[0];
1573 if (advance || slot >= nritems) {
1574 if (slot >= nritems -1) {
1575 ret = btrfs_next_leaf(root, path);
1578 leaf = path->nodes[0];
1579 nritems = btrfs_header_nritems(leaf);
1580 slot = path->slots[0];
1587 item = btrfs_item_nr(leaf, slot);
1588 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1590 if (found_key.objectid != key.objectid)
1592 if (btrfs_key_type(&found_key) != key_type)
1594 if (found_key.offset < filp->f_pos)
1597 filp->f_pos = found_key.offset;
1599 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1601 di_total = btrfs_item_size(leaf, item);
1602 while(di_cur < di_total) {
1603 struct btrfs_key location;
1605 name_len = btrfs_dir_name_len(leaf, di);
1606 if (name_len < 32) {
1607 name_ptr = tmp_name;
1609 name_ptr = kmalloc(name_len, GFP_NOFS);
1612 read_extent_buffer(leaf, name_ptr,
1613 (unsigned long)(di + 1), name_len);
1615 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1616 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1617 over = filldir(dirent, name_ptr, name_len,
1622 if (name_ptr != tmp_name)
1627 di_len = btrfs_dir_name_len(leaf, di) +
1628 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1630 di = (struct btrfs_dir_item *)((char *)di + di_len);
1633 if (key_type == BTRFS_DIR_INDEX_KEY)
1634 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1640 btrfs_free_path(path);
1644 int btrfs_write_inode(struct inode *inode, int wait)
1646 struct btrfs_root *root = BTRFS_I(inode)->root;
1647 struct btrfs_trans_handle *trans;
1651 trans = btrfs_start_transaction(root, 1);
1652 btrfs_set_trans_block_group(trans, inode);
1653 ret = btrfs_commit_transaction(trans, root);
1659 * This is somewhat expensive, updating the tree every time the
1660 * inode changes. But, it is most likely to find the inode in cache.
1661 * FIXME, needs more benchmarking...there are no reasons other than performance
1662 * to keep or drop this code.
1664 void btrfs_dirty_inode(struct inode *inode)
1666 struct btrfs_root *root = BTRFS_I(inode)->root;
1667 struct btrfs_trans_handle *trans;
1669 trans = btrfs_start_transaction(root, 1);
1670 btrfs_set_trans_block_group(trans, inode);
1671 btrfs_update_inode(trans, root, inode);
1672 btrfs_end_transaction(trans, root);
1675 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1676 struct btrfs_root *root,
1677 const char *name, int name_len,
1680 struct btrfs_block_group_cache *group,
1683 struct inode *inode;
1684 struct btrfs_inode_item *inode_item;
1685 struct btrfs_block_group_cache *new_inode_group;
1686 struct btrfs_key *location;
1687 struct btrfs_path *path;
1688 struct btrfs_inode_ref *ref;
1689 struct btrfs_key key[2];
1695 path = btrfs_alloc_path();
1698 inode = new_inode(root->fs_info->sb);
1700 return ERR_PTR(-ENOMEM);
1702 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1703 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1704 inode->i_mapping, GFP_NOFS);
1705 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1706 inode->i_mapping, GFP_NOFS);
1707 mutex_init(&BTRFS_I(inode)->csum_mutex);
1708 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1709 BTRFS_I(inode)->delalloc_bytes = 0;
1710 BTRFS_I(inode)->root = root;
1716 new_inode_group = btrfs_find_block_group(root, group, 0,
1717 BTRFS_BLOCK_GROUP_METADATA, owner);
1718 if (!new_inode_group) {
1719 printk("find_block group failed\n");
1720 new_inode_group = group;
1722 BTRFS_I(inode)->block_group = new_inode_group;
1723 BTRFS_I(inode)->flags = 0;
1725 key[0].objectid = objectid;
1726 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1729 key[1].objectid = objectid;
1730 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1731 key[1].offset = ref_objectid;
1733 sizes[0] = sizeof(struct btrfs_inode_item);
1734 sizes[1] = name_len + sizeof(*ref);
1736 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1740 if (objectid > root->highest_inode)
1741 root->highest_inode = objectid;
1743 inode->i_uid = current->fsuid;
1744 inode->i_gid = current->fsgid;
1745 inode->i_mode = mode;
1746 inode->i_ino = objectid;
1747 inode->i_blocks = 0;
1748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1749 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1750 struct btrfs_inode_item);
1751 fill_inode_item(path->nodes[0], inode_item, inode);
1753 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1754 struct btrfs_inode_ref);
1755 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1756 ptr = (unsigned long)(ref + 1);
1757 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1759 btrfs_mark_buffer_dirty(path->nodes[0]);
1760 btrfs_free_path(path);
1762 location = &BTRFS_I(inode)->location;
1763 location->objectid = objectid;
1764 location->offset = 0;
1765 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1767 insert_inode_hash(inode);
1770 btrfs_free_path(path);
1771 return ERR_PTR(ret);
1774 static inline u8 btrfs_inode_type(struct inode *inode)
1776 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1779 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1780 struct dentry *dentry, struct inode *inode,
1784 struct btrfs_key key;
1785 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1786 struct inode *parent_inode;
1788 key.objectid = inode->i_ino;
1789 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1792 ret = btrfs_insert_dir_item(trans, root,
1793 dentry->d_name.name, dentry->d_name.len,
1794 dentry->d_parent->d_inode->i_ino,
1795 &key, btrfs_inode_type(inode));
1798 ret = btrfs_insert_inode_ref(trans, root,
1799 dentry->d_name.name,
1802 dentry->d_parent->d_inode->i_ino);
1804 parent_inode = dentry->d_parent->d_inode;
1805 parent_inode->i_size += dentry->d_name.len * 2;
1806 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1807 ret = btrfs_update_inode(trans, root,
1808 dentry->d_parent->d_inode);
1813 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1814 struct dentry *dentry, struct inode *inode,
1817 int err = btrfs_add_link(trans, dentry, inode, backref);
1819 d_instantiate(dentry, inode);
1827 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1828 int mode, dev_t rdev)
1830 struct btrfs_trans_handle *trans;
1831 struct btrfs_root *root = BTRFS_I(dir)->root;
1832 struct inode *inode = NULL;
1836 unsigned long nr = 0;
1838 if (!new_valid_dev(rdev))
1841 err = btrfs_check_free_space(root, 1, 0);
1845 trans = btrfs_start_transaction(root, 1);
1846 btrfs_set_trans_block_group(trans, dir);
1848 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1854 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1856 dentry->d_parent->d_inode->i_ino, objectid,
1857 BTRFS_I(dir)->block_group, mode);
1858 err = PTR_ERR(inode);
1862 btrfs_set_trans_block_group(trans, inode);
1863 err = btrfs_add_nondir(trans, dentry, inode, 0);
1867 inode->i_op = &btrfs_special_inode_operations;
1868 init_special_inode(inode, inode->i_mode, rdev);
1869 btrfs_update_inode(trans, root, inode);
1871 dir->i_sb->s_dirt = 1;
1872 btrfs_update_inode_block_group(trans, inode);
1873 btrfs_update_inode_block_group(trans, dir);
1875 nr = trans->blocks_used;
1876 btrfs_end_transaction_throttle(trans, root);
1879 inode_dec_link_count(inode);
1882 btrfs_btree_balance_dirty(root, nr);
1886 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1887 int mode, struct nameidata *nd)
1889 struct btrfs_trans_handle *trans;
1890 struct btrfs_root *root = BTRFS_I(dir)->root;
1891 struct inode *inode = NULL;
1894 unsigned long nr = 0;
1897 err = btrfs_check_free_space(root, 1, 0);
1900 trans = btrfs_start_transaction(root, 1);
1901 btrfs_set_trans_block_group(trans, dir);
1903 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1909 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1911 dentry->d_parent->d_inode->i_ino,
1912 objectid, BTRFS_I(dir)->block_group, mode);
1913 err = PTR_ERR(inode);
1917 btrfs_set_trans_block_group(trans, inode);
1918 err = btrfs_add_nondir(trans, dentry, inode, 0);
1922 inode->i_mapping->a_ops = &btrfs_aops;
1923 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1924 inode->i_fop = &btrfs_file_operations;
1925 inode->i_op = &btrfs_file_inode_operations;
1926 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1927 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1928 inode->i_mapping, GFP_NOFS);
1929 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1930 inode->i_mapping, GFP_NOFS);
1931 mutex_init(&BTRFS_I(inode)->csum_mutex);
1932 BTRFS_I(inode)->delalloc_bytes = 0;
1933 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1934 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1936 dir->i_sb->s_dirt = 1;
1937 btrfs_update_inode_block_group(trans, inode);
1938 btrfs_update_inode_block_group(trans, dir);
1940 nr = trans->blocks_used;
1941 btrfs_end_transaction_throttle(trans, root);
1944 inode_dec_link_count(inode);
1947 btrfs_btree_balance_dirty(root, nr);
1951 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1952 struct dentry *dentry)
1954 struct btrfs_trans_handle *trans;
1955 struct btrfs_root *root = BTRFS_I(dir)->root;
1956 struct inode *inode = old_dentry->d_inode;
1957 unsigned long nr = 0;
1961 if (inode->i_nlink == 0)
1964 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1969 err = btrfs_check_free_space(root, 1, 0);
1972 trans = btrfs_start_transaction(root, 1);
1974 btrfs_set_trans_block_group(trans, dir);
1975 atomic_inc(&inode->i_count);
1976 err = btrfs_add_nondir(trans, dentry, inode, 1);
1981 dir->i_sb->s_dirt = 1;
1982 btrfs_update_inode_block_group(trans, dir);
1983 err = btrfs_update_inode(trans, root, inode);
1988 nr = trans->blocks_used;
1989 btrfs_end_transaction_throttle(trans, root);
1992 inode_dec_link_count(inode);
1995 btrfs_btree_balance_dirty(root, nr);
1999 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2001 struct inode *inode = NULL;
2002 struct btrfs_trans_handle *trans;
2003 struct btrfs_root *root = BTRFS_I(dir)->root;
2005 int drop_on_err = 0;
2007 unsigned long nr = 1;
2009 err = btrfs_check_free_space(root, 1, 0);
2013 trans = btrfs_start_transaction(root, 1);
2014 btrfs_set_trans_block_group(trans, dir);
2016 if (IS_ERR(trans)) {
2017 err = PTR_ERR(trans);
2021 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2027 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2029 dentry->d_parent->d_inode->i_ino, objectid,
2030 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2031 if (IS_ERR(inode)) {
2032 err = PTR_ERR(inode);
2037 inode->i_op = &btrfs_dir_inode_operations;
2038 inode->i_fop = &btrfs_dir_file_operations;
2039 btrfs_set_trans_block_group(trans, inode);
2042 err = btrfs_update_inode(trans, root, inode);
2046 err = btrfs_add_link(trans, dentry, inode, 0);
2050 d_instantiate(dentry, inode);
2052 dir->i_sb->s_dirt = 1;
2053 btrfs_update_inode_block_group(trans, inode);
2054 btrfs_update_inode_block_group(trans, dir);
2057 nr = trans->blocks_used;
2058 btrfs_end_transaction_throttle(trans, root);
2063 btrfs_btree_balance_dirty(root, nr);
2067 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2068 struct extent_map *existing,
2069 struct extent_map *em)
2074 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2076 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2079 if (!real_blocks && em->block_start != existing->block_start)
2082 new_end = max(existing->start + existing->len, em->start + em->len);
2084 if (existing->start >= em->start) {
2085 if (em->start + em->len < existing->start)
2088 start_diff = existing->start - em->start;
2089 if (real_blocks && em->block_start + start_diff !=
2090 existing->block_start)
2093 em->len = new_end - em->start;
2095 remove_extent_mapping(em_tree, existing);
2096 /* free for the tree */
2097 free_extent_map(existing);
2098 ret = add_extent_mapping(em_tree, em);
2100 } else if (em->start > existing->start) {
2102 if (existing->start + existing->len < em->start)
2105 start_diff = em->start - existing->start;
2106 if (real_blocks && existing->block_start + start_diff !=
2110 remove_extent_mapping(em_tree, existing);
2111 em->block_start = existing->block_start;
2112 em->start = existing->start;
2113 em->len = new_end - existing->start;
2114 free_extent_map(existing);
2116 ret = add_extent_mapping(em_tree, em);
2123 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2124 existing->start, existing->len, existing->block_start,
2125 em->start, em->len, em->block_start);
2129 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2130 size_t pg_offset, u64 start, u64 len,
2136 u64 extent_start = 0;
2138 u64 objectid = inode->i_ino;
2140 struct btrfs_path *path;
2141 struct btrfs_root *root = BTRFS_I(inode)->root;
2142 struct btrfs_file_extent_item *item;
2143 struct extent_buffer *leaf;
2144 struct btrfs_key found_key;
2145 struct extent_map *em = NULL;
2146 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2147 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2148 struct btrfs_trans_handle *trans = NULL;
2150 path = btrfs_alloc_path();
2154 spin_lock(&em_tree->lock);
2155 em = lookup_extent_mapping(em_tree, start, len);
2157 em->bdev = root->fs_info->fs_devices->latest_bdev;
2158 spin_unlock(&em_tree->lock);
2161 if (em->start > start || em->start + em->len <= start)
2162 free_extent_map(em);
2163 else if (em->block_start == EXTENT_MAP_INLINE && page)
2164 free_extent_map(em);
2168 em = alloc_extent_map(GFP_NOFS);
2174 em->start = EXTENT_MAP_HOLE;
2176 em->bdev = root->fs_info->fs_devices->latest_bdev;
2177 ret = btrfs_lookup_file_extent(trans, root, path,
2178 objectid, start, trans != NULL);
2185 if (path->slots[0] == 0)
2190 leaf = path->nodes[0];
2191 item = btrfs_item_ptr(leaf, path->slots[0],
2192 struct btrfs_file_extent_item);
2193 /* are we inside the extent that was found? */
2194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2195 found_type = btrfs_key_type(&found_key);
2196 if (found_key.objectid != objectid ||
2197 found_type != BTRFS_EXTENT_DATA_KEY) {
2201 found_type = btrfs_file_extent_type(leaf, item);
2202 extent_start = found_key.offset;
2203 if (found_type == BTRFS_FILE_EXTENT_REG) {
2204 extent_end = extent_start +
2205 btrfs_file_extent_num_bytes(leaf, item);
2207 if (start < extent_start || start >= extent_end) {
2209 if (start < extent_start) {
2210 if (start + len <= extent_start)
2212 em->len = extent_end - extent_start;
2218 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2220 em->start = extent_start;
2221 em->len = extent_end - extent_start;
2222 em->block_start = EXTENT_MAP_HOLE;
2225 bytenr += btrfs_file_extent_offset(leaf, item);
2226 em->block_start = bytenr;
2227 em->start = extent_start;
2228 em->len = extent_end - extent_start;
2230 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2235 size_t extent_offset;
2238 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2240 extent_end = (extent_start + size + root->sectorsize - 1) &
2241 ~((u64)root->sectorsize - 1);
2242 if (start < extent_start || start >= extent_end) {
2244 if (start < extent_start) {
2245 if (start + len <= extent_start)
2247 em->len = extent_end - extent_start;
2253 em->block_start = EXTENT_MAP_INLINE;
2256 em->start = extent_start;
2261 page_start = page_offset(page) + pg_offset;
2262 extent_offset = page_start - extent_start;
2263 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2264 size - extent_offset);
2265 em->start = extent_start + extent_offset;
2266 em->len = (copy_size + root->sectorsize - 1) &
2267 ~((u64)root->sectorsize - 1);
2269 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2270 if (create == 0 && !PageUptodate(page)) {
2271 read_extent_buffer(leaf, map + pg_offset, ptr,
2273 flush_dcache_page(page);
2274 } else if (create && PageUptodate(page)) {
2277 free_extent_map(em);
2279 btrfs_release_path(root, path);
2280 trans = btrfs_start_transaction(root, 1);
2283 write_extent_buffer(leaf, map + pg_offset, ptr,
2285 btrfs_mark_buffer_dirty(leaf);
2288 set_extent_uptodate(io_tree, em->start,
2289 extent_map_end(em) - 1, GFP_NOFS);
2292 printk("unkknown found_type %d\n", found_type);
2299 em->block_start = EXTENT_MAP_HOLE;
2301 btrfs_release_path(root, path);
2302 if (em->start > start || extent_map_end(em) <= start) {
2303 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2309 spin_lock(&em_tree->lock);
2310 ret = add_extent_mapping(em_tree, em);
2311 /* it is possible that someone inserted the extent into the tree
2312 * while we had the lock dropped. It is also possible that
2313 * an overlapping map exists in the tree
2315 if (ret == -EEXIST) {
2316 struct extent_map *existing;
2317 existing = lookup_extent_mapping(em_tree, start, len);
2318 if (existing && (existing->start > start ||
2319 existing->start + existing->len <= start)) {
2320 free_extent_map(existing);
2324 existing = lookup_extent_mapping(em_tree, em->start,
2327 err = merge_extent_mapping(em_tree, existing,
2329 free_extent_map(existing);
2331 free_extent_map(em);
2336 printk("failing to insert %Lu %Lu\n",
2338 free_extent_map(em);
2342 free_extent_map(em);
2346 spin_unlock(&em_tree->lock);
2348 btrfs_free_path(path);
2350 ret = btrfs_end_transaction(trans, root);
2355 free_extent_map(em);
2357 return ERR_PTR(err);
2362 #if 0 /* waiting for O_DIRECT reads */
2363 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2364 struct buffer_head *bh_result, int create)
2366 struct extent_map *em;
2367 u64 start = (u64)iblock << inode->i_blkbits;
2368 struct btrfs_multi_bio *multi = NULL;
2369 struct btrfs_root *root = BTRFS_I(inode)->root;
2375 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2377 if (!em || IS_ERR(em))
2380 if (em->start > start || em->start + em->len <= start) {
2384 if (em->block_start == EXTENT_MAP_INLINE) {
2389 len = em->start + em->len - start;
2390 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2392 if (em->block_start == EXTENT_MAP_HOLE ||
2393 em->block_start == EXTENT_MAP_DELALLOC) {
2394 bh_result->b_size = len;
2398 logical = start - em->start;
2399 logical = em->block_start + logical;
2402 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2403 logical, &map_length, &multi, 0);
2405 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2406 bh_result->b_size = min(map_length, len);
2408 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2409 set_buffer_mapped(bh_result);
2412 free_extent_map(em);
2417 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2418 const struct iovec *iov, loff_t offset,
2419 unsigned long nr_segs)
2423 struct file *file = iocb->ki_filp;
2424 struct inode *inode = file->f_mapping->host;
2429 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2430 offset, nr_segs, btrfs_get_block, NULL);
2434 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2436 return extent_bmap(mapping, iblock, btrfs_get_extent);
2439 int btrfs_readpage(struct file *file, struct page *page)
2441 struct extent_io_tree *tree;
2442 tree = &BTRFS_I(page->mapping->host)->io_tree;
2443 return extent_read_full_page(tree, page, btrfs_get_extent);
2446 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2448 struct extent_io_tree *tree;
2451 if (current->flags & PF_MEMALLOC) {
2452 redirty_page_for_writepage(wbc, page);
2456 tree = &BTRFS_I(page->mapping->host)->io_tree;
2457 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2460 static int btrfs_writepages(struct address_space *mapping,
2461 struct writeback_control *wbc)
2463 struct extent_io_tree *tree;
2464 tree = &BTRFS_I(mapping->host)->io_tree;
2465 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2469 btrfs_readpages(struct file *file, struct address_space *mapping,
2470 struct list_head *pages, unsigned nr_pages)
2472 struct extent_io_tree *tree;
2473 tree = &BTRFS_I(mapping->host)->io_tree;
2474 return extent_readpages(tree, mapping, pages, nr_pages,
2478 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2480 struct extent_io_tree *tree;
2481 struct extent_map_tree *map;
2484 tree = &BTRFS_I(page->mapping->host)->io_tree;
2485 map = &BTRFS_I(page->mapping->host)->extent_tree;
2486 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2488 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2489 ClearPagePrivate(page);
2490 set_page_private(page, 0);
2491 page_cache_release(page);
2496 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2498 struct extent_io_tree *tree;
2500 tree = &BTRFS_I(page->mapping->host)->io_tree;
2501 extent_invalidatepage(tree, page, offset);
2502 btrfs_releasepage(page, GFP_NOFS);
2503 if (PagePrivate(page)) {
2504 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2505 ClearPagePrivate(page);
2506 set_page_private(page, 0);
2507 page_cache_release(page);
2512 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2513 * called from a page fault handler when a page is first dirtied. Hence we must
2514 * be careful to check for EOF conditions here. We set the page up correctly
2515 * for a written page which means we get ENOSPC checking when writing into
2516 * holes and correct delalloc and unwritten extent mapping on filesystems that
2517 * support these features.
2519 * We are not allowed to take the i_mutex here so we have to play games to
2520 * protect against truncate races as the page could now be beyond EOF. Because
2521 * vmtruncate() writes the inode size before removing pages, once we have the
2522 * page lock we can determine safely if the page is beyond EOF. If it is not
2523 * beyond EOF, then the page is guaranteed safe against truncation until we
2526 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2528 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2529 struct btrfs_root *root = BTRFS_I(inode)->root;
2535 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2542 wait_on_page_writeback(page);
2543 size = i_size_read(inode);
2544 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2546 if ((page->mapping != inode->i_mapping) ||
2547 (page_start > size)) {
2548 /* page got truncated out from underneath us */
2552 /* page is wholly or partially inside EOF */
2553 if (page_start + PAGE_CACHE_SIZE > size)
2554 end = size & ~PAGE_CACHE_MASK;
2556 end = PAGE_CACHE_SIZE;
2558 ret = btrfs_cow_one_page(inode, page, end);
2566 static void btrfs_truncate(struct inode *inode)
2568 struct btrfs_root *root = BTRFS_I(inode)->root;
2570 struct btrfs_trans_handle *trans;
2573 if (!S_ISREG(inode->i_mode))
2575 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2578 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2580 trans = btrfs_start_transaction(root, 1);
2581 btrfs_set_trans_block_group(trans, inode);
2583 /* FIXME, add redo link to tree so we don't leak on crash */
2584 ret = btrfs_truncate_in_trans(trans, root, inode,
2585 BTRFS_EXTENT_DATA_KEY);
2586 btrfs_update_inode(trans, root, inode);
2587 nr = trans->blocks_used;
2589 ret = btrfs_end_transaction_throttle(trans, root);
2591 btrfs_btree_balance_dirty(root, nr);
2595 * Invalidate a single dcache entry at the root of the filesystem.
2596 * Needed after creation of snapshot or subvolume.
2598 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2601 struct dentry *alias, *entry;
2604 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2608 /* change me if btrfs ever gets a d_hash operation */
2609 qstr.hash = full_name_hash(qstr.name, qstr.len);
2610 entry = d_lookup(alias, &qstr);
2613 d_invalidate(entry);
2619 int btrfs_create_subvol_root(struct btrfs_root *new_root,
2620 struct btrfs_trans_handle *trans, u64 new_dirid,
2621 struct btrfs_block_group_cache *block_group)
2623 struct inode *inode;
2626 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2627 new_dirid, block_group, S_IFDIR | 0700);
2629 return PTR_ERR(inode);
2630 inode->i_op = &btrfs_dir_inode_operations;
2631 inode->i_fop = &btrfs_dir_file_operations;
2632 new_root->inode = inode;
2634 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2639 return btrfs_update_inode(trans, new_root, inode);
2642 unsigned long btrfs_force_ra(struct address_space *mapping,
2643 struct file_ra_state *ra, struct file *file,
2644 pgoff_t offset, pgoff_t last_index)
2646 pgoff_t req_size = last_index - offset + 1;
2648 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2649 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2652 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2653 return offset + req_size;
2657 struct inode *btrfs_alloc_inode(struct super_block *sb)
2659 struct btrfs_inode *ei;
2661 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2665 ei->ordered_trans = 0;
2666 return &ei->vfs_inode;
2669 void btrfs_destroy_inode(struct inode *inode)
2671 WARN_ON(!list_empty(&inode->i_dentry));
2672 WARN_ON(inode->i_data.nrpages);
2674 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2675 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2678 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2679 static void init_once(struct kmem_cache * cachep, void *foo)
2681 static void init_once(void * foo, struct kmem_cache * cachep,
2682 unsigned long flags)
2685 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2687 inode_init_once(&ei->vfs_inode);
2690 void btrfs_destroy_cachep(void)
2692 if (btrfs_inode_cachep)
2693 kmem_cache_destroy(btrfs_inode_cachep);
2694 if (btrfs_trans_handle_cachep)
2695 kmem_cache_destroy(btrfs_trans_handle_cachep);
2696 if (btrfs_transaction_cachep)
2697 kmem_cache_destroy(btrfs_transaction_cachep);
2698 if (btrfs_bit_radix_cachep)
2699 kmem_cache_destroy(btrfs_bit_radix_cachep);
2700 if (btrfs_path_cachep)
2701 kmem_cache_destroy(btrfs_path_cachep);
2704 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2705 unsigned long extra_flags,
2706 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2707 void (*ctor)(struct kmem_cache *, void *)
2709 void (*ctor)(void *, struct kmem_cache *,
2714 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2715 SLAB_MEM_SPREAD | extra_flags), ctor
2716 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2722 int btrfs_init_cachep(void)
2724 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2725 sizeof(struct btrfs_inode),
2727 if (!btrfs_inode_cachep)
2729 btrfs_trans_handle_cachep =
2730 btrfs_cache_create("btrfs_trans_handle_cache",
2731 sizeof(struct btrfs_trans_handle),
2733 if (!btrfs_trans_handle_cachep)
2735 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2736 sizeof(struct btrfs_transaction),
2738 if (!btrfs_transaction_cachep)
2740 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2741 sizeof(struct btrfs_path),
2743 if (!btrfs_path_cachep)
2745 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2746 SLAB_DESTROY_BY_RCU, NULL);
2747 if (!btrfs_bit_radix_cachep)
2751 btrfs_destroy_cachep();
2755 static int btrfs_getattr(struct vfsmount *mnt,
2756 struct dentry *dentry, struct kstat *stat)
2758 struct inode *inode = dentry->d_inode;
2759 generic_fillattr(inode, stat);
2760 stat->blksize = PAGE_CACHE_SIZE;
2761 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2765 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2766 struct inode * new_dir,struct dentry *new_dentry)
2768 struct btrfs_trans_handle *trans;
2769 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2770 struct inode *new_inode = new_dentry->d_inode;
2771 struct inode *old_inode = old_dentry->d_inode;
2772 struct timespec ctime = CURRENT_TIME;
2775 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2776 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2780 ret = btrfs_check_free_space(root, 1, 0);
2784 trans = btrfs_start_transaction(root, 1);
2786 btrfs_set_trans_block_group(trans, new_dir);
2788 old_dentry->d_inode->i_nlink++;
2789 old_dir->i_ctime = old_dir->i_mtime = ctime;
2790 new_dir->i_ctime = new_dir->i_mtime = ctime;
2791 old_inode->i_ctime = ctime;
2793 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2798 new_inode->i_ctime = CURRENT_TIME;
2799 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2803 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2808 btrfs_end_transaction(trans, root);
2813 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2814 const char *symname)
2816 struct btrfs_trans_handle *trans;
2817 struct btrfs_root *root = BTRFS_I(dir)->root;
2818 struct btrfs_path *path;
2819 struct btrfs_key key;
2820 struct inode *inode = NULL;
2827 struct btrfs_file_extent_item *ei;
2828 struct extent_buffer *leaf;
2829 unsigned long nr = 0;
2831 name_len = strlen(symname) + 1;
2832 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2833 return -ENAMETOOLONG;
2835 err = btrfs_check_free_space(root, 1, 0);
2839 trans = btrfs_start_transaction(root, 1);
2840 btrfs_set_trans_block_group(trans, dir);
2842 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2848 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2850 dentry->d_parent->d_inode->i_ino, objectid,
2851 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2852 err = PTR_ERR(inode);
2856 btrfs_set_trans_block_group(trans, inode);
2857 err = btrfs_add_nondir(trans, dentry, inode, 0);
2861 inode->i_mapping->a_ops = &btrfs_aops;
2862 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2863 inode->i_fop = &btrfs_file_operations;
2864 inode->i_op = &btrfs_file_inode_operations;
2865 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2866 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2867 inode->i_mapping, GFP_NOFS);
2868 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2869 inode->i_mapping, GFP_NOFS);
2870 mutex_init(&BTRFS_I(inode)->csum_mutex);
2871 BTRFS_I(inode)->delalloc_bytes = 0;
2872 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
2873 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2875 dir->i_sb->s_dirt = 1;
2876 btrfs_update_inode_block_group(trans, inode);
2877 btrfs_update_inode_block_group(trans, dir);
2881 path = btrfs_alloc_path();
2883 key.objectid = inode->i_ino;
2885 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2886 datasize = btrfs_file_extent_calc_inline_size(name_len);
2887 err = btrfs_insert_empty_item(trans, root, path, &key,
2893 leaf = path->nodes[0];
2894 ei = btrfs_item_ptr(leaf, path->slots[0],
2895 struct btrfs_file_extent_item);
2896 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2897 btrfs_set_file_extent_type(leaf, ei,
2898 BTRFS_FILE_EXTENT_INLINE);
2899 ptr = btrfs_file_extent_inline_start(ei);
2900 write_extent_buffer(leaf, symname, ptr, name_len);
2901 btrfs_mark_buffer_dirty(leaf);
2902 btrfs_free_path(path);
2904 inode->i_op = &btrfs_symlink_inode_operations;
2905 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2906 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2907 inode->i_size = name_len - 1;
2908 err = btrfs_update_inode(trans, root, inode);
2913 nr = trans->blocks_used;
2914 btrfs_end_transaction_throttle(trans, root);
2917 inode_dec_link_count(inode);
2920 btrfs_btree_balance_dirty(root, nr);
2924 static int btrfs_permission(struct inode *inode, int mask,
2925 struct nameidata *nd)
2927 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2929 return generic_permission(inode, mask, NULL);
2932 static struct inode_operations btrfs_dir_inode_operations = {
2933 .lookup = btrfs_lookup,
2934 .create = btrfs_create,
2935 .unlink = btrfs_unlink,
2937 .mkdir = btrfs_mkdir,
2938 .rmdir = btrfs_rmdir,
2939 .rename = btrfs_rename,
2940 .symlink = btrfs_symlink,
2941 .setattr = btrfs_setattr,
2942 .mknod = btrfs_mknod,
2943 .setxattr = generic_setxattr,
2944 .getxattr = generic_getxattr,
2945 .listxattr = btrfs_listxattr,
2946 .removexattr = generic_removexattr,
2947 .permission = btrfs_permission,
2949 static struct inode_operations btrfs_dir_ro_inode_operations = {
2950 .lookup = btrfs_lookup,
2951 .permission = btrfs_permission,
2953 static struct file_operations btrfs_dir_file_operations = {
2954 .llseek = generic_file_llseek,
2955 .read = generic_read_dir,
2956 .readdir = btrfs_readdir,
2957 .unlocked_ioctl = btrfs_ioctl,
2958 #ifdef CONFIG_COMPAT
2959 .compat_ioctl = btrfs_ioctl,
2961 .release = btrfs_release_file,
2964 static struct extent_io_ops btrfs_extent_io_ops = {
2965 .fill_delalloc = run_delalloc_range,
2966 .submit_bio_hook = btrfs_submit_bio_hook,
2967 .merge_bio_hook = btrfs_merge_bio_hook,
2968 .readpage_io_hook = btrfs_readpage_io_hook,
2969 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2970 .readpage_io_failed_hook = btrfs_io_failed_hook,
2971 .set_bit_hook = btrfs_set_bit_hook,
2972 .clear_bit_hook = btrfs_clear_bit_hook,
2975 static struct address_space_operations btrfs_aops = {
2976 .readpage = btrfs_readpage,
2977 .writepage = btrfs_writepage,
2978 .writepages = btrfs_writepages,
2979 .readpages = btrfs_readpages,
2980 .sync_page = block_sync_page,
2982 .direct_IO = btrfs_direct_IO,
2983 .invalidatepage = btrfs_invalidatepage,
2984 .releasepage = btrfs_releasepage,
2985 .set_page_dirty = __set_page_dirty_nobuffers,
2988 static struct address_space_operations btrfs_symlink_aops = {
2989 .readpage = btrfs_readpage,
2990 .writepage = btrfs_writepage,
2991 .invalidatepage = btrfs_invalidatepage,
2992 .releasepage = btrfs_releasepage,
2995 static struct inode_operations btrfs_file_inode_operations = {
2996 .truncate = btrfs_truncate,
2997 .getattr = btrfs_getattr,
2998 .setattr = btrfs_setattr,
2999 .setxattr = generic_setxattr,
3000 .getxattr = generic_getxattr,
3001 .listxattr = btrfs_listxattr,
3002 .removexattr = generic_removexattr,
3003 .permission = btrfs_permission,
3005 static struct inode_operations btrfs_special_inode_operations = {
3006 .getattr = btrfs_getattr,
3007 .setattr = btrfs_setattr,
3008 .permission = btrfs_permission,
3010 static struct inode_operations btrfs_symlink_inode_operations = {
3011 .readlink = generic_readlink,
3012 .follow_link = page_follow_link_light,
3013 .put_link = page_put_link,
3014 .permission = btrfs_permission,