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 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
356 ret = btrfs_end_transaction(trans, root);
361 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
364 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
367 struct btrfs_root *root = BTRFS_I(inode)->root;
370 if (!(rw & (1 << BIO_RW))) {
371 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
376 if (btrfs_test_opt(root, NODATASUM) ||
377 btrfs_test_flag(inode, NODATASUM)) {
381 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
382 inode, rw, bio, mirror_num,
383 __btrfs_submit_bio_hook);
385 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
388 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
391 struct inode *inode = page->mapping->host;
392 struct btrfs_root *root = BTRFS_I(inode)->root;
393 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
394 struct btrfs_csum_item *item;
395 struct btrfs_path *path = NULL;
398 if (btrfs_test_opt(root, NODATASUM) ||
399 btrfs_test_flag(inode, NODATASUM))
402 path = btrfs_alloc_path();
403 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
406 /* a csum that isn't present is a preallocated region. */
407 if (ret == -ENOENT || ret == -EFBIG)
410 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
413 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
415 set_state_private(io_tree, start, csum);
418 btrfs_free_path(path);
422 struct io_failure_record {
430 int btrfs_io_failed_hook(struct bio *failed_bio,
431 struct page *page, u64 start, u64 end,
432 struct extent_state *state)
434 struct io_failure_record *failrec = NULL;
436 struct extent_map *em;
437 struct inode *inode = page->mapping->host;
438 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
439 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
446 ret = get_state_private(failure_tree, start, &private);
448 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
451 failrec->start = start;
452 failrec->len = end - start + 1;
453 failrec->last_mirror = 0;
455 spin_lock(&em_tree->lock);
456 em = lookup_extent_mapping(em_tree, start, failrec->len);
457 if (em->start > start || em->start + em->len < start) {
461 spin_unlock(&em_tree->lock);
463 if (!em || IS_ERR(em)) {
467 logical = start - em->start;
468 logical = em->block_start + logical;
469 failrec->logical = logical;
471 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
472 EXTENT_DIRTY, GFP_NOFS);
473 set_state_private(failure_tree, start,
474 (u64)(unsigned long)failrec);
476 failrec = (struct io_failure_record *)(unsigned long)private;
478 num_copies = btrfs_num_copies(
479 &BTRFS_I(inode)->root->fs_info->mapping_tree,
480 failrec->logical, failrec->len);
481 failrec->last_mirror++;
483 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
484 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
487 if (state && state->start != failrec->start)
489 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
491 if (!state || failrec->last_mirror > num_copies) {
492 set_state_private(failure_tree, failrec->start, 0);
493 clear_extent_bits(failure_tree, failrec->start,
494 failrec->start + failrec->len - 1,
495 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
499 bio = bio_alloc(GFP_NOFS, 1);
500 bio->bi_private = state;
501 bio->bi_end_io = failed_bio->bi_end_io;
502 bio->bi_sector = failrec->logical >> 9;
503 bio->bi_bdev = failed_bio->bi_bdev;
505 bio_add_page(bio, page, failrec->len, start - page_offset(page));
506 if (failed_bio->bi_rw & (1 << BIO_RW))
511 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
512 failrec->last_mirror);
516 int btrfs_clean_io_failures(struct inode *inode, u64 start)
520 struct io_failure_record *failure;
524 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
525 (u64)-1, 1, EXTENT_DIRTY)) {
526 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
527 start, &private_failure);
529 failure = (struct io_failure_record *)(unsigned long)
531 set_state_private(&BTRFS_I(inode)->io_failure_tree,
533 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
535 failure->start + failure->len - 1,
536 EXTENT_DIRTY | EXTENT_LOCKED,
544 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
545 struct extent_state *state)
547 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
548 struct inode *inode = page->mapping->host;
549 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
551 u64 private = ~(u32)0;
553 struct btrfs_root *root = BTRFS_I(inode)->root;
557 if (btrfs_test_opt(root, NODATASUM) ||
558 btrfs_test_flag(inode, NODATASUM))
560 if (state && state->start == start) {
561 private = state->private;
564 ret = get_state_private(io_tree, start, &private);
566 local_irq_save(flags);
567 kaddr = kmap_atomic(page, KM_IRQ0);
571 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
572 btrfs_csum_final(csum, (char *)&csum);
573 if (csum != private) {
576 kunmap_atomic(kaddr, KM_IRQ0);
577 local_irq_restore(flags);
579 /* if the io failure tree for this inode is non-empty,
580 * check to see if we've recovered from a failed IO
582 btrfs_clean_io_failures(inode, start);
586 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
587 page->mapping->host->i_ino, (unsigned long long)start, csum,
589 memset(kaddr + offset, 1, end - start + 1);
590 flush_dcache_page(page);
591 kunmap_atomic(kaddr, KM_IRQ0);
592 local_irq_restore(flags);
598 void btrfs_read_locked_inode(struct inode *inode)
600 struct btrfs_path *path;
601 struct extent_buffer *leaf;
602 struct btrfs_inode_item *inode_item;
603 struct btrfs_timespec *tspec;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 struct btrfs_key location;
606 u64 alloc_group_block;
610 path = btrfs_alloc_path();
612 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
614 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
618 leaf = path->nodes[0];
619 inode_item = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_inode_item);
622 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
623 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
624 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
625 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
626 inode->i_size = btrfs_inode_size(leaf, inode_item);
628 tspec = btrfs_inode_atime(inode_item);
629 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
630 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
632 tspec = btrfs_inode_mtime(inode_item);
633 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
634 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
636 tspec = btrfs_inode_ctime(inode_item);
637 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
638 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
640 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
641 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
643 rdev = btrfs_inode_rdev(leaf, inode_item);
645 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
646 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
649 if (!BTRFS_I(inode)->block_group) {
650 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
652 BTRFS_BLOCK_GROUP_METADATA, 0);
654 btrfs_free_path(path);
657 switch (inode->i_mode & S_IFMT) {
659 inode->i_mapping->a_ops = &btrfs_aops;
660 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
661 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
662 inode->i_fop = &btrfs_file_operations;
663 inode->i_op = &btrfs_file_inode_operations;
666 inode->i_fop = &btrfs_dir_file_operations;
667 if (root == root->fs_info->tree_root)
668 inode->i_op = &btrfs_dir_ro_inode_operations;
670 inode->i_op = &btrfs_dir_inode_operations;
673 inode->i_op = &btrfs_symlink_inode_operations;
674 inode->i_mapping->a_ops = &btrfs_symlink_aops;
675 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
678 init_special_inode(inode, inode->i_mode, rdev);
684 btrfs_free_path(path);
685 make_bad_inode(inode);
688 static void fill_inode_item(struct extent_buffer *leaf,
689 struct btrfs_inode_item *item,
692 btrfs_set_inode_uid(leaf, item, inode->i_uid);
693 btrfs_set_inode_gid(leaf, item, inode->i_gid);
694 btrfs_set_inode_size(leaf, item, inode->i_size);
695 btrfs_set_inode_mode(leaf, item, inode->i_mode);
696 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
698 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
699 inode->i_atime.tv_sec);
700 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
701 inode->i_atime.tv_nsec);
703 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
704 inode->i_mtime.tv_sec);
705 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
706 inode->i_mtime.tv_nsec);
708 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
709 inode->i_ctime.tv_sec);
710 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
711 inode->i_ctime.tv_nsec);
713 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
714 btrfs_set_inode_generation(leaf, item, inode->i_generation);
715 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
716 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
717 btrfs_set_inode_block_group(leaf, item,
718 BTRFS_I(inode)->block_group->key.objectid);
721 int btrfs_update_inode(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root,
725 struct btrfs_inode_item *inode_item;
726 struct btrfs_path *path;
727 struct extent_buffer *leaf;
730 path = btrfs_alloc_path();
732 ret = btrfs_lookup_inode(trans, root, path,
733 &BTRFS_I(inode)->location, 1);
740 leaf = path->nodes[0];
741 inode_item = btrfs_item_ptr(leaf, path->slots[0],
742 struct btrfs_inode_item);
744 fill_inode_item(leaf, inode_item, inode);
745 btrfs_mark_buffer_dirty(leaf);
746 btrfs_set_inode_last_trans(trans, inode);
749 btrfs_free_path(path);
754 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
757 struct dentry *dentry)
759 struct btrfs_path *path;
760 const char *name = dentry->d_name.name;
761 int name_len = dentry->d_name.len;
763 struct extent_buffer *leaf;
764 struct btrfs_dir_item *di;
765 struct btrfs_key key;
767 path = btrfs_alloc_path();
773 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
783 leaf = path->nodes[0];
784 btrfs_dir_item_key_to_cpu(leaf, di, &key);
785 ret = btrfs_delete_one_dir_name(trans, root, path, di);
788 btrfs_release_path(root, path);
790 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
791 key.objectid, name, name_len, -1);
800 ret = btrfs_delete_one_dir_name(trans, root, path, di);
801 btrfs_release_path(root, path);
803 dentry->d_inode->i_ctime = dir->i_ctime;
804 ret = btrfs_del_inode_ref(trans, root, name, name_len,
805 dentry->d_inode->i_ino,
806 dentry->d_parent->d_inode->i_ino);
808 printk("failed to delete reference to %.*s, "
809 "inode %lu parent %lu\n", name_len, name,
810 dentry->d_inode->i_ino,
811 dentry->d_parent->d_inode->i_ino);
814 btrfs_free_path(path);
816 dir->i_size -= name_len * 2;
817 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
818 btrfs_update_inode(trans, root, dir);
819 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
820 dentry->d_inode->i_nlink--;
822 drop_nlink(dentry->d_inode);
824 ret = btrfs_update_inode(trans, root, dentry->d_inode);
825 dir->i_sb->s_dirt = 1;
830 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
832 struct btrfs_root *root;
833 struct btrfs_trans_handle *trans;
834 struct inode *inode = dentry->d_inode;
836 unsigned long nr = 0;
838 root = BTRFS_I(dir)->root;
840 ret = btrfs_check_free_space(root, 1, 1);
844 trans = btrfs_start_transaction(root, 1);
846 btrfs_set_trans_block_group(trans, dir);
847 ret = btrfs_unlink_trans(trans, root, dir, dentry);
848 nr = trans->blocks_used;
850 if (inode->i_nlink == 0) {
851 /* if the inode isn't linked anywhere,
852 * we don't need to worry about
855 btrfs_del_ordered_inode(inode, 1);
858 btrfs_end_transaction_throttle(trans, root);
860 btrfs_btree_balance_dirty(root, nr);
864 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
866 struct inode *inode = dentry->d_inode;
869 struct btrfs_root *root = BTRFS_I(dir)->root;
870 struct btrfs_trans_handle *trans;
871 unsigned long nr = 0;
873 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
877 ret = btrfs_check_free_space(root, 1, 1);
881 trans = btrfs_start_transaction(root, 1);
882 btrfs_set_trans_block_group(trans, dir);
884 /* now the directory is empty */
885 err = btrfs_unlink_trans(trans, root, dir, dentry);
890 nr = trans->blocks_used;
891 ret = btrfs_end_transaction_throttle(trans, root);
893 btrfs_btree_balance_dirty(root, nr);
901 * this can truncate away extent items, csum items and directory items.
902 * It starts at a high offset and removes keys until it can't find
903 * any higher than i_size.
905 * csum items that cross the new i_size are truncated to the new size
908 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root,
914 struct btrfs_path *path;
915 struct btrfs_key key;
916 struct btrfs_key found_key;
918 struct extent_buffer *leaf;
919 struct btrfs_file_extent_item *fi;
920 u64 extent_start = 0;
921 u64 extent_num_bytes = 0;
927 int pending_del_nr = 0;
928 int pending_del_slot = 0;
929 int extent_type = -1;
930 u64 mask = root->sectorsize - 1;
932 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
933 path = btrfs_alloc_path();
937 /* FIXME, add redo link to tree so we don't leak on crash */
938 key.objectid = inode->i_ino;
939 key.offset = (u64)-1;
942 btrfs_init_path(path);
944 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
949 BUG_ON(path->slots[0] == 0);
955 leaf = path->nodes[0];
956 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
957 found_type = btrfs_key_type(&found_key);
959 if (found_key.objectid != inode->i_ino)
962 if (found_type < min_type)
965 item_end = found_key.offset;
966 if (found_type == BTRFS_EXTENT_DATA_KEY) {
967 fi = btrfs_item_ptr(leaf, path->slots[0],
968 struct btrfs_file_extent_item);
969 extent_type = btrfs_file_extent_type(leaf, fi);
970 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
972 btrfs_file_extent_num_bytes(leaf, fi);
973 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
974 struct btrfs_item *item = btrfs_item_nr(leaf,
976 item_end += btrfs_file_extent_inline_len(leaf,
981 if (found_type == BTRFS_CSUM_ITEM_KEY) {
982 ret = btrfs_csum_truncate(trans, root, path,
986 if (item_end < inode->i_size) {
987 if (found_type == BTRFS_DIR_ITEM_KEY) {
988 found_type = BTRFS_INODE_ITEM_KEY;
989 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
990 found_type = BTRFS_CSUM_ITEM_KEY;
991 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
992 found_type = BTRFS_XATTR_ITEM_KEY;
993 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
994 found_type = BTRFS_INODE_REF_KEY;
995 } else if (found_type) {
1000 btrfs_set_key_type(&key, found_type);
1003 if (found_key.offset >= inode->i_size)
1009 /* FIXME, shrink the extent if the ref count is only 1 */
1010 if (found_type != BTRFS_EXTENT_DATA_KEY)
1013 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1015 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1017 u64 orig_num_bytes =
1018 btrfs_file_extent_num_bytes(leaf, fi);
1019 extent_num_bytes = inode->i_size -
1020 found_key.offset + root->sectorsize - 1;
1021 extent_num_bytes = extent_num_bytes &
1022 ~((u64)root->sectorsize - 1);
1023 btrfs_set_file_extent_num_bytes(leaf, fi,
1025 num_dec = (orig_num_bytes -
1027 if (extent_start != 0)
1028 dec_i_blocks(inode, num_dec);
1029 btrfs_mark_buffer_dirty(leaf);
1032 btrfs_file_extent_disk_num_bytes(leaf,
1034 /* FIXME blocksize != 4096 */
1035 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1036 if (extent_start != 0) {
1038 dec_i_blocks(inode, num_dec);
1040 root_gen = btrfs_header_generation(leaf);
1041 root_owner = btrfs_header_owner(leaf);
1043 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1045 u32 newsize = inode->i_size - found_key.offset;
1046 dec_i_blocks(inode, item_end + 1 -
1047 found_key.offset - newsize);
1049 btrfs_file_extent_calc_inline_size(newsize);
1050 ret = btrfs_truncate_item(trans, root, path,
1054 dec_i_blocks(inode, item_end + 1 -
1060 if (!pending_del_nr) {
1061 /* no pending yet, add ourselves */
1062 pending_del_slot = path->slots[0];
1064 } else if (pending_del_nr &&
1065 path->slots[0] + 1 == pending_del_slot) {
1066 /* hop on the pending chunk */
1068 pending_del_slot = path->slots[0];
1070 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1076 ret = btrfs_free_extent(trans, root, extent_start,
1079 root_gen, inode->i_ino,
1080 found_key.offset, 0);
1084 if (path->slots[0] == 0) {
1087 btrfs_release_path(root, path);
1092 if (pending_del_nr &&
1093 path->slots[0] + 1 != pending_del_slot) {
1094 struct btrfs_key debug;
1096 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1098 ret = btrfs_del_items(trans, root, path,
1103 btrfs_release_path(root, path);
1109 if (pending_del_nr) {
1110 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1113 btrfs_free_path(path);
1114 inode->i_sb->s_dirt = 1;
1118 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1122 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1123 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1124 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1127 WARN_ON(!PageLocked(page));
1128 set_page_extent_mapped(page);
1130 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1131 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1132 page_end, GFP_NOFS);
1134 if (zero_start != PAGE_CACHE_SIZE) {
1136 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1137 flush_dcache_page(page);
1140 set_page_dirty(page);
1141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1147 * taken from block_truncate_page, but does cow as it zeros out
1148 * any bytes left in the last page in the file.
1150 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1152 struct inode *inode = mapping->host;
1153 struct btrfs_root *root = BTRFS_I(inode)->root;
1154 u32 blocksize = root->sectorsize;
1155 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1156 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1161 if ((offset & (blocksize - 1)) == 0)
1166 page = grab_cache_page(mapping, index);
1169 if (!PageUptodate(page)) {
1170 ret = btrfs_readpage(NULL, page);
1172 if (page->mapping != mapping) {
1174 page_cache_release(page);
1177 if (!PageUptodate(page)) {
1183 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1184 wait_on_page_writeback(page);
1185 ret = btrfs_cow_one_page(inode, page, offset);
1188 page_cache_release(page);
1193 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1195 struct inode *inode = dentry->d_inode;
1198 err = inode_change_ok(inode, attr);
1202 if (S_ISREG(inode->i_mode) &&
1203 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1204 struct btrfs_trans_handle *trans;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1208 u64 mask = root->sectorsize - 1;
1209 u64 hole_start = (inode->i_size + mask) & ~mask;
1210 u64 block_end = (attr->ia_size + mask) & ~mask;
1214 if (attr->ia_size <= hole_start)
1217 err = btrfs_check_free_space(root, 1, 0);
1221 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1223 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1224 hole_size = block_end - hole_start;
1226 trans = btrfs_start_transaction(root, 1);
1227 btrfs_set_trans_block_group(trans, inode);
1228 err = btrfs_drop_extents(trans, root, inode,
1229 hole_start, block_end, hole_start,
1232 if (alloc_hint != EXTENT_MAP_INLINE) {
1233 err = btrfs_insert_file_extent(trans, root,
1237 btrfs_drop_extent_cache(inode, hole_start,
1239 btrfs_check_file(root, inode);
1241 btrfs_end_transaction(trans, root);
1242 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1247 err = inode_setattr(inode, attr);
1252 void btrfs_delete_inode(struct inode *inode)
1254 struct btrfs_trans_handle *trans;
1255 struct btrfs_root *root = BTRFS_I(inode)->root;
1259 truncate_inode_pages(&inode->i_data, 0);
1260 if (is_bad_inode(inode)) {
1265 trans = btrfs_start_transaction(root, 1);
1267 btrfs_set_trans_block_group(trans, inode);
1268 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1270 goto no_delete_lock;
1272 nr = trans->blocks_used;
1275 btrfs_end_transaction(trans, root);
1276 btrfs_btree_balance_dirty(root, nr);
1280 nr = trans->blocks_used;
1281 btrfs_end_transaction(trans, root);
1282 btrfs_btree_balance_dirty(root, nr);
1288 * this returns the key found in the dir entry in the location pointer.
1289 * If no dir entries were found, location->objectid is 0.
1291 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1292 struct btrfs_key *location)
1294 const char *name = dentry->d_name.name;
1295 int namelen = dentry->d_name.len;
1296 struct btrfs_dir_item *di;
1297 struct btrfs_path *path;
1298 struct btrfs_root *root = BTRFS_I(dir)->root;
1301 if (namelen == 1 && strcmp(name, ".") == 0) {
1302 location->objectid = dir->i_ino;
1303 location->type = BTRFS_INODE_ITEM_KEY;
1304 location->offset = 0;
1307 path = btrfs_alloc_path();
1310 if (namelen == 2 && strcmp(name, "..") == 0) {
1311 struct btrfs_key key;
1312 struct extent_buffer *leaf;
1316 key.objectid = dir->i_ino;
1317 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1319 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1323 leaf = path->nodes[0];
1324 slot = path->slots[0];
1325 nritems = btrfs_header_nritems(leaf);
1326 if (slot >= nritems)
1329 btrfs_item_key_to_cpu(leaf, &key, slot);
1330 if (key.objectid != dir->i_ino ||
1331 key.type != BTRFS_INODE_REF_KEY) {
1334 location->objectid = key.offset;
1335 location->type = BTRFS_INODE_ITEM_KEY;
1336 location->offset = 0;
1340 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1344 if (!di || IS_ERR(di)) {
1347 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1349 btrfs_free_path(path);
1352 location->objectid = 0;
1357 * when we hit a tree root in a directory, the btrfs part of the inode
1358 * needs to be changed to reflect the root directory of the tree root. This
1359 * is kind of like crossing a mount point.
1361 static int fixup_tree_root_location(struct btrfs_root *root,
1362 struct btrfs_key *location,
1363 struct btrfs_root **sub_root,
1364 struct dentry *dentry)
1366 struct btrfs_path *path;
1367 struct btrfs_root_item *ri;
1369 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1371 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1374 path = btrfs_alloc_path();
1377 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1378 dentry->d_name.name,
1379 dentry->d_name.len);
1380 if (IS_ERR(*sub_root))
1381 return PTR_ERR(*sub_root);
1383 ri = &(*sub_root)->root_item;
1384 location->objectid = btrfs_root_dirid(ri);
1385 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1386 location->offset = 0;
1388 btrfs_free_path(path);
1392 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1394 struct btrfs_iget_args *args = p;
1395 inode->i_ino = args->ino;
1396 BTRFS_I(inode)->root = args->root;
1397 BTRFS_I(inode)->delalloc_bytes = 0;
1398 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1399 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1400 inode->i_mapping, GFP_NOFS);
1401 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1402 inode->i_mapping, GFP_NOFS);
1403 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1407 static int btrfs_find_actor(struct inode *inode, void *opaque)
1409 struct btrfs_iget_args *args = opaque;
1410 return (args->ino == inode->i_ino &&
1411 args->root == BTRFS_I(inode)->root);
1414 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1417 struct btrfs_iget_args args;
1418 args.ino = objectid;
1419 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1424 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1427 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1428 struct btrfs_root *root)
1430 struct inode *inode;
1431 struct btrfs_iget_args args;
1432 args.ino = objectid;
1435 inode = iget5_locked(s, objectid, btrfs_find_actor,
1436 btrfs_init_locked_inode,
1441 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1442 struct nameidata *nd)
1444 struct inode * inode;
1445 struct btrfs_inode *bi = BTRFS_I(dir);
1446 struct btrfs_root *root = bi->root;
1447 struct btrfs_root *sub_root = root;
1448 struct btrfs_key location;
1451 if (dentry->d_name.len > BTRFS_NAME_LEN)
1452 return ERR_PTR(-ENAMETOOLONG);
1454 ret = btrfs_inode_by_name(dir, dentry, &location);
1457 return ERR_PTR(ret);
1460 if (location.objectid) {
1461 ret = fixup_tree_root_location(root, &location, &sub_root,
1464 return ERR_PTR(ret);
1466 return ERR_PTR(-ENOENT);
1467 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1470 return ERR_PTR(-EACCES);
1471 if (inode->i_state & I_NEW) {
1472 /* the inode and parent dir are two different roots */
1473 if (sub_root != root) {
1475 sub_root->inode = inode;
1477 BTRFS_I(inode)->root = sub_root;
1478 memcpy(&BTRFS_I(inode)->location, &location,
1480 btrfs_read_locked_inode(inode);
1481 unlock_new_inode(inode);
1484 return d_splice_alias(inode, dentry);
1487 static unsigned char btrfs_filetype_table[] = {
1488 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1491 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1493 struct inode *inode = filp->f_dentry->d_inode;
1494 struct btrfs_root *root = BTRFS_I(inode)->root;
1495 struct btrfs_item *item;
1496 struct btrfs_dir_item *di;
1497 struct btrfs_key key;
1498 struct btrfs_key found_key;
1499 struct btrfs_path *path;
1502 struct extent_buffer *leaf;
1505 unsigned char d_type;
1510 int key_type = BTRFS_DIR_INDEX_KEY;
1515 /* FIXME, use a real flag for deciding about the key type */
1516 if (root->fs_info->tree_root == root)
1517 key_type = BTRFS_DIR_ITEM_KEY;
1519 /* special case for "." */
1520 if (filp->f_pos == 0) {
1521 over = filldir(dirent, ".", 1,
1529 key.objectid = inode->i_ino;
1530 path = btrfs_alloc_path();
1533 /* special case for .., just use the back ref */
1534 if (filp->f_pos == 1) {
1535 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1539 leaf = path->nodes[0];
1540 slot = path->slots[0];
1541 nritems = btrfs_header_nritems(leaf);
1542 if (slot >= nritems) {
1543 btrfs_release_path(root, path);
1544 goto read_dir_items;
1546 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1547 btrfs_release_path(root, path);
1548 if (found_key.objectid != key.objectid ||
1549 found_key.type != BTRFS_INODE_REF_KEY)
1550 goto read_dir_items;
1551 over = filldir(dirent, "..", 2,
1552 2, found_key.offset, DT_DIR);
1559 btrfs_set_key_type(&key, key_type);
1560 key.offset = filp->f_pos;
1562 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1567 leaf = path->nodes[0];
1568 nritems = btrfs_header_nritems(leaf);
1569 slot = path->slots[0];
1570 if (advance || slot >= nritems) {
1571 if (slot >= nritems -1) {
1572 ret = btrfs_next_leaf(root, path);
1575 leaf = path->nodes[0];
1576 nritems = btrfs_header_nritems(leaf);
1577 slot = path->slots[0];
1584 item = btrfs_item_nr(leaf, slot);
1585 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1587 if (found_key.objectid != key.objectid)
1589 if (btrfs_key_type(&found_key) != key_type)
1591 if (found_key.offset < filp->f_pos)
1594 filp->f_pos = found_key.offset;
1596 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1598 di_total = btrfs_item_size(leaf, item);
1599 while(di_cur < di_total) {
1600 struct btrfs_key location;
1602 name_len = btrfs_dir_name_len(leaf, di);
1603 if (name_len < 32) {
1604 name_ptr = tmp_name;
1606 name_ptr = kmalloc(name_len, GFP_NOFS);
1609 read_extent_buffer(leaf, name_ptr,
1610 (unsigned long)(di + 1), name_len);
1612 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1613 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1614 over = filldir(dirent, name_ptr, name_len,
1619 if (name_ptr != tmp_name)
1624 di_len = btrfs_dir_name_len(leaf, di) +
1625 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1627 di = (struct btrfs_dir_item *)((char *)di + di_len);
1630 if (key_type == BTRFS_DIR_INDEX_KEY)
1631 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1637 btrfs_free_path(path);
1641 int btrfs_write_inode(struct inode *inode, int wait)
1643 struct btrfs_root *root = BTRFS_I(inode)->root;
1644 struct btrfs_trans_handle *trans;
1648 trans = btrfs_start_transaction(root, 1);
1649 btrfs_set_trans_block_group(trans, inode);
1650 ret = btrfs_commit_transaction(trans, root);
1656 * This is somewhat expensive, updating the tree every time the
1657 * inode changes. But, it is most likely to find the inode in cache.
1658 * FIXME, needs more benchmarking...there are no reasons other than performance
1659 * to keep or drop this code.
1661 void btrfs_dirty_inode(struct inode *inode)
1663 struct btrfs_root *root = BTRFS_I(inode)->root;
1664 struct btrfs_trans_handle *trans;
1666 trans = btrfs_start_transaction(root, 1);
1667 btrfs_set_trans_block_group(trans, inode);
1668 btrfs_update_inode(trans, root, inode);
1669 btrfs_end_transaction(trans, root);
1672 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1673 struct btrfs_root *root,
1674 const char *name, int name_len,
1677 struct btrfs_block_group_cache *group,
1680 struct inode *inode;
1681 struct btrfs_inode_item *inode_item;
1682 struct btrfs_block_group_cache *new_inode_group;
1683 struct btrfs_key *location;
1684 struct btrfs_path *path;
1685 struct btrfs_inode_ref *ref;
1686 struct btrfs_key key[2];
1692 path = btrfs_alloc_path();
1695 inode = new_inode(root->fs_info->sb);
1697 return ERR_PTR(-ENOMEM);
1699 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1700 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1701 inode->i_mapping, GFP_NOFS);
1702 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1703 inode->i_mapping, GFP_NOFS);
1704 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1705 BTRFS_I(inode)->delalloc_bytes = 0;
1706 BTRFS_I(inode)->root = root;
1712 new_inode_group = btrfs_find_block_group(root, group, 0,
1713 BTRFS_BLOCK_GROUP_METADATA, owner);
1714 if (!new_inode_group) {
1715 printk("find_block group failed\n");
1716 new_inode_group = group;
1718 BTRFS_I(inode)->block_group = new_inode_group;
1719 BTRFS_I(inode)->flags = 0;
1721 key[0].objectid = objectid;
1722 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1725 key[1].objectid = objectid;
1726 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1727 key[1].offset = ref_objectid;
1729 sizes[0] = sizeof(struct btrfs_inode_item);
1730 sizes[1] = name_len + sizeof(*ref);
1732 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1736 if (objectid > root->highest_inode)
1737 root->highest_inode = objectid;
1739 inode->i_uid = current->fsuid;
1740 inode->i_gid = current->fsgid;
1741 inode->i_mode = mode;
1742 inode->i_ino = objectid;
1743 inode->i_blocks = 0;
1744 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1745 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1746 struct btrfs_inode_item);
1747 fill_inode_item(path->nodes[0], inode_item, inode);
1749 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1750 struct btrfs_inode_ref);
1751 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1752 ptr = (unsigned long)(ref + 1);
1753 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1755 btrfs_mark_buffer_dirty(path->nodes[0]);
1756 btrfs_free_path(path);
1758 location = &BTRFS_I(inode)->location;
1759 location->objectid = objectid;
1760 location->offset = 0;
1761 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1763 insert_inode_hash(inode);
1766 btrfs_free_path(path);
1767 return ERR_PTR(ret);
1770 static inline u8 btrfs_inode_type(struct inode *inode)
1772 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1775 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1776 struct dentry *dentry, struct inode *inode,
1780 struct btrfs_key key;
1781 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1782 struct inode *parent_inode;
1784 key.objectid = inode->i_ino;
1785 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1788 ret = btrfs_insert_dir_item(trans, root,
1789 dentry->d_name.name, dentry->d_name.len,
1790 dentry->d_parent->d_inode->i_ino,
1791 &key, btrfs_inode_type(inode));
1794 ret = btrfs_insert_inode_ref(trans, root,
1795 dentry->d_name.name,
1798 dentry->d_parent->d_inode->i_ino);
1800 parent_inode = dentry->d_parent->d_inode;
1801 parent_inode->i_size += dentry->d_name.len * 2;
1802 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1803 ret = btrfs_update_inode(trans, root,
1804 dentry->d_parent->d_inode);
1809 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1810 struct dentry *dentry, struct inode *inode,
1813 int err = btrfs_add_link(trans, dentry, inode, backref);
1815 d_instantiate(dentry, inode);
1823 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1824 int mode, dev_t rdev)
1826 struct btrfs_trans_handle *trans;
1827 struct btrfs_root *root = BTRFS_I(dir)->root;
1828 struct inode *inode = NULL;
1832 unsigned long nr = 0;
1834 if (!new_valid_dev(rdev))
1837 err = btrfs_check_free_space(root, 1, 0);
1841 trans = btrfs_start_transaction(root, 1);
1842 btrfs_set_trans_block_group(trans, dir);
1844 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1850 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1852 dentry->d_parent->d_inode->i_ino, objectid,
1853 BTRFS_I(dir)->block_group, mode);
1854 err = PTR_ERR(inode);
1858 btrfs_set_trans_block_group(trans, inode);
1859 err = btrfs_add_nondir(trans, dentry, inode, 0);
1863 inode->i_op = &btrfs_special_inode_operations;
1864 init_special_inode(inode, inode->i_mode, rdev);
1865 btrfs_update_inode(trans, root, inode);
1867 dir->i_sb->s_dirt = 1;
1868 btrfs_update_inode_block_group(trans, inode);
1869 btrfs_update_inode_block_group(trans, dir);
1871 nr = trans->blocks_used;
1872 btrfs_end_transaction_throttle(trans, root);
1875 inode_dec_link_count(inode);
1878 btrfs_btree_balance_dirty(root, nr);
1882 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1883 int mode, struct nameidata *nd)
1885 struct btrfs_trans_handle *trans;
1886 struct btrfs_root *root = BTRFS_I(dir)->root;
1887 struct inode *inode = NULL;
1890 unsigned long nr = 0;
1893 err = btrfs_check_free_space(root, 1, 0);
1896 trans = btrfs_start_transaction(root, 1);
1897 btrfs_set_trans_block_group(trans, dir);
1899 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1905 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1907 dentry->d_parent->d_inode->i_ino,
1908 objectid, BTRFS_I(dir)->block_group, mode);
1909 err = PTR_ERR(inode);
1913 btrfs_set_trans_block_group(trans, inode);
1914 err = btrfs_add_nondir(trans, dentry, inode, 0);
1918 inode->i_mapping->a_ops = &btrfs_aops;
1919 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1920 inode->i_fop = &btrfs_file_operations;
1921 inode->i_op = &btrfs_file_inode_operations;
1922 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1923 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1924 inode->i_mapping, GFP_NOFS);
1925 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1926 inode->i_mapping, GFP_NOFS);
1927 BTRFS_I(inode)->delalloc_bytes = 0;
1928 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1929 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1931 dir->i_sb->s_dirt = 1;
1932 btrfs_update_inode_block_group(trans, inode);
1933 btrfs_update_inode_block_group(trans, dir);
1935 nr = trans->blocks_used;
1936 btrfs_end_transaction_throttle(trans, root);
1939 inode_dec_link_count(inode);
1942 btrfs_btree_balance_dirty(root, nr);
1946 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1947 struct dentry *dentry)
1949 struct btrfs_trans_handle *trans;
1950 struct btrfs_root *root = BTRFS_I(dir)->root;
1951 struct inode *inode = old_dentry->d_inode;
1952 unsigned long nr = 0;
1956 if (inode->i_nlink == 0)
1959 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1964 err = btrfs_check_free_space(root, 1, 0);
1967 trans = btrfs_start_transaction(root, 1);
1969 btrfs_set_trans_block_group(trans, dir);
1970 atomic_inc(&inode->i_count);
1971 err = btrfs_add_nondir(trans, dentry, inode, 1);
1976 dir->i_sb->s_dirt = 1;
1977 btrfs_update_inode_block_group(trans, dir);
1978 err = btrfs_update_inode(trans, root, inode);
1983 nr = trans->blocks_used;
1984 btrfs_end_transaction_throttle(trans, root);
1987 inode_dec_link_count(inode);
1990 btrfs_btree_balance_dirty(root, nr);
1994 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1996 struct inode *inode = NULL;
1997 struct btrfs_trans_handle *trans;
1998 struct btrfs_root *root = BTRFS_I(dir)->root;
2000 int drop_on_err = 0;
2002 unsigned long nr = 1;
2004 err = btrfs_check_free_space(root, 1, 0);
2008 trans = btrfs_start_transaction(root, 1);
2009 btrfs_set_trans_block_group(trans, dir);
2011 if (IS_ERR(trans)) {
2012 err = PTR_ERR(trans);
2016 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2022 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2024 dentry->d_parent->d_inode->i_ino, objectid,
2025 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2026 if (IS_ERR(inode)) {
2027 err = PTR_ERR(inode);
2032 inode->i_op = &btrfs_dir_inode_operations;
2033 inode->i_fop = &btrfs_dir_file_operations;
2034 btrfs_set_trans_block_group(trans, inode);
2037 err = btrfs_update_inode(trans, root, inode);
2041 err = btrfs_add_link(trans, dentry, inode, 0);
2045 d_instantiate(dentry, inode);
2047 dir->i_sb->s_dirt = 1;
2048 btrfs_update_inode_block_group(trans, inode);
2049 btrfs_update_inode_block_group(trans, dir);
2052 nr = trans->blocks_used;
2053 btrfs_end_transaction_throttle(trans, root);
2058 btrfs_btree_balance_dirty(root, nr);
2062 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2063 struct extent_map *existing,
2064 struct extent_map *em)
2069 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2071 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2074 if (!real_blocks && em->block_start != existing->block_start)
2077 new_end = max(existing->start + existing->len, em->start + em->len);
2079 if (existing->start >= em->start) {
2080 if (em->start + em->len < existing->start)
2083 start_diff = existing->start - em->start;
2084 if (real_blocks && em->block_start + start_diff !=
2085 existing->block_start)
2088 em->len = new_end - em->start;
2090 remove_extent_mapping(em_tree, existing);
2091 /* free for the tree */
2092 free_extent_map(existing);
2093 ret = add_extent_mapping(em_tree, em);
2095 } else if (em->start > existing->start) {
2097 if (existing->start + existing->len < em->start)
2100 start_diff = em->start - existing->start;
2101 if (real_blocks && existing->block_start + start_diff !=
2105 remove_extent_mapping(em_tree, existing);
2106 em->block_start = existing->block_start;
2107 em->start = existing->start;
2108 em->len = new_end - existing->start;
2109 free_extent_map(existing);
2111 ret = add_extent_mapping(em_tree, em);
2118 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2119 existing->start, existing->len, existing->block_start,
2120 em->start, em->len, em->block_start);
2124 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2125 size_t pg_offset, u64 start, u64 len,
2131 u64 extent_start = 0;
2133 u64 objectid = inode->i_ino;
2135 struct btrfs_path *path;
2136 struct btrfs_root *root = BTRFS_I(inode)->root;
2137 struct btrfs_file_extent_item *item;
2138 struct extent_buffer *leaf;
2139 struct btrfs_key found_key;
2140 struct extent_map *em = NULL;
2141 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2142 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2143 struct btrfs_trans_handle *trans = NULL;
2145 path = btrfs_alloc_path();
2149 spin_lock(&em_tree->lock);
2150 em = lookup_extent_mapping(em_tree, start, len);
2152 em->bdev = root->fs_info->fs_devices->latest_bdev;
2153 spin_unlock(&em_tree->lock);
2156 if (em->start > start || em->start + em->len <= start)
2157 free_extent_map(em);
2158 else if (em->block_start == EXTENT_MAP_INLINE && page)
2159 free_extent_map(em);
2163 em = alloc_extent_map(GFP_NOFS);
2169 em->start = EXTENT_MAP_HOLE;
2171 em->bdev = root->fs_info->fs_devices->latest_bdev;
2172 ret = btrfs_lookup_file_extent(trans, root, path,
2173 objectid, start, trans != NULL);
2180 if (path->slots[0] == 0)
2185 leaf = path->nodes[0];
2186 item = btrfs_item_ptr(leaf, path->slots[0],
2187 struct btrfs_file_extent_item);
2188 /* are we inside the extent that was found? */
2189 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2190 found_type = btrfs_key_type(&found_key);
2191 if (found_key.objectid != objectid ||
2192 found_type != BTRFS_EXTENT_DATA_KEY) {
2196 found_type = btrfs_file_extent_type(leaf, item);
2197 extent_start = found_key.offset;
2198 if (found_type == BTRFS_FILE_EXTENT_REG) {
2199 extent_end = extent_start +
2200 btrfs_file_extent_num_bytes(leaf, item);
2202 if (start < extent_start || start >= extent_end) {
2204 if (start < extent_start) {
2205 if (start + len <= extent_start)
2207 em->len = extent_end - extent_start;
2213 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2215 em->start = extent_start;
2216 em->len = extent_end - extent_start;
2217 em->block_start = EXTENT_MAP_HOLE;
2220 bytenr += btrfs_file_extent_offset(leaf, item);
2221 em->block_start = bytenr;
2222 em->start = extent_start;
2223 em->len = extent_end - extent_start;
2225 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2230 size_t extent_offset;
2233 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2235 extent_end = (extent_start + size + root->sectorsize - 1) &
2236 ~((u64)root->sectorsize - 1);
2237 if (start < extent_start || start >= extent_end) {
2239 if (start < extent_start) {
2240 if (start + len <= extent_start)
2242 em->len = extent_end - extent_start;
2248 em->block_start = EXTENT_MAP_INLINE;
2251 em->start = extent_start;
2256 page_start = page_offset(page) + pg_offset;
2257 extent_offset = page_start - extent_start;
2258 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2259 size - extent_offset);
2260 em->start = extent_start + extent_offset;
2261 em->len = (copy_size + root->sectorsize - 1) &
2262 ~((u64)root->sectorsize - 1);
2264 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2265 if (create == 0 && !PageUptodate(page)) {
2266 read_extent_buffer(leaf, map + pg_offset, ptr,
2268 flush_dcache_page(page);
2269 } else if (create && PageUptodate(page)) {
2272 free_extent_map(em);
2274 btrfs_release_path(root, path);
2275 trans = btrfs_start_transaction(root, 1);
2278 write_extent_buffer(leaf, map + pg_offset, ptr,
2280 btrfs_mark_buffer_dirty(leaf);
2283 set_extent_uptodate(io_tree, em->start,
2284 extent_map_end(em) - 1, GFP_NOFS);
2287 printk("unkknown found_type %d\n", found_type);
2294 em->block_start = EXTENT_MAP_HOLE;
2296 btrfs_release_path(root, path);
2297 if (em->start > start || extent_map_end(em) <= start) {
2298 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2304 spin_lock(&em_tree->lock);
2305 ret = add_extent_mapping(em_tree, em);
2306 /* it is possible that someone inserted the extent into the tree
2307 * while we had the lock dropped. It is also possible that
2308 * an overlapping map exists in the tree
2310 if (ret == -EEXIST) {
2311 struct extent_map *existing;
2312 existing = lookup_extent_mapping(em_tree, start, len);
2313 if (existing && (existing->start > start ||
2314 existing->start + existing->len <= start)) {
2315 free_extent_map(existing);
2319 existing = lookup_extent_mapping(em_tree, em->start,
2322 err = merge_extent_mapping(em_tree, existing,
2324 free_extent_map(existing);
2326 free_extent_map(em);
2331 printk("failing to insert %Lu %Lu\n",
2333 free_extent_map(em);
2337 free_extent_map(em);
2341 spin_unlock(&em_tree->lock);
2343 btrfs_free_path(path);
2345 ret = btrfs_end_transaction(trans, root);
2350 free_extent_map(em);
2352 return ERR_PTR(err);
2357 #if 0 /* waiting for O_DIRECT reads */
2358 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2359 struct buffer_head *bh_result, int create)
2361 struct extent_map *em;
2362 u64 start = (u64)iblock << inode->i_blkbits;
2363 struct btrfs_multi_bio *multi = NULL;
2364 struct btrfs_root *root = BTRFS_I(inode)->root;
2370 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2372 if (!em || IS_ERR(em))
2375 if (em->start > start || em->start + em->len <= start) {
2379 if (em->block_start == EXTENT_MAP_INLINE) {
2384 len = em->start + em->len - start;
2385 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2387 if (em->block_start == EXTENT_MAP_HOLE ||
2388 em->block_start == EXTENT_MAP_DELALLOC) {
2389 bh_result->b_size = len;
2393 logical = start - em->start;
2394 logical = em->block_start + logical;
2397 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2398 logical, &map_length, &multi, 0);
2400 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2401 bh_result->b_size = min(map_length, len);
2403 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2404 set_buffer_mapped(bh_result);
2407 free_extent_map(em);
2412 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2413 const struct iovec *iov, loff_t offset,
2414 unsigned long nr_segs)
2418 struct file *file = iocb->ki_filp;
2419 struct inode *inode = file->f_mapping->host;
2424 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2425 offset, nr_segs, btrfs_get_block, NULL);
2429 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2431 return extent_bmap(mapping, iblock, btrfs_get_extent);
2434 int btrfs_readpage(struct file *file, struct page *page)
2436 struct extent_io_tree *tree;
2437 tree = &BTRFS_I(page->mapping->host)->io_tree;
2438 return extent_read_full_page(tree, page, btrfs_get_extent);
2441 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2443 struct extent_io_tree *tree;
2446 if (current->flags & PF_MEMALLOC) {
2447 redirty_page_for_writepage(wbc, page);
2451 tree = &BTRFS_I(page->mapping->host)->io_tree;
2452 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2455 static int btrfs_writepages(struct address_space *mapping,
2456 struct writeback_control *wbc)
2458 struct extent_io_tree *tree;
2459 tree = &BTRFS_I(mapping->host)->io_tree;
2460 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2464 btrfs_readpages(struct file *file, struct address_space *mapping,
2465 struct list_head *pages, unsigned nr_pages)
2467 struct extent_io_tree *tree;
2468 tree = &BTRFS_I(mapping->host)->io_tree;
2469 return extent_readpages(tree, mapping, pages, nr_pages,
2473 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2475 struct extent_io_tree *tree;
2476 struct extent_map_tree *map;
2479 tree = &BTRFS_I(page->mapping->host)->io_tree;
2480 map = &BTRFS_I(page->mapping->host)->extent_tree;
2481 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2483 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2484 ClearPagePrivate(page);
2485 set_page_private(page, 0);
2486 page_cache_release(page);
2491 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2493 struct extent_io_tree *tree;
2495 tree = &BTRFS_I(page->mapping->host)->io_tree;
2496 extent_invalidatepage(tree, page, offset);
2497 btrfs_releasepage(page, GFP_NOFS);
2498 if (PagePrivate(page)) {
2499 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2500 ClearPagePrivate(page);
2501 set_page_private(page, 0);
2502 page_cache_release(page);
2507 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2508 * called from a page fault handler when a page is first dirtied. Hence we must
2509 * be careful to check for EOF conditions here. We set the page up correctly
2510 * for a written page which means we get ENOSPC checking when writing into
2511 * holes and correct delalloc and unwritten extent mapping on filesystems that
2512 * support these features.
2514 * We are not allowed to take the i_mutex here so we have to play games to
2515 * protect against truncate races as the page could now be beyond EOF. Because
2516 * vmtruncate() writes the inode size before removing pages, once we have the
2517 * page lock we can determine safely if the page is beyond EOF. If it is not
2518 * beyond EOF, then the page is guaranteed safe against truncation until we
2521 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2523 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2524 struct btrfs_root *root = BTRFS_I(inode)->root;
2530 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2537 wait_on_page_writeback(page);
2538 size = i_size_read(inode);
2539 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2541 if ((page->mapping != inode->i_mapping) ||
2542 (page_start > size)) {
2543 /* page got truncated out from underneath us */
2547 /* page is wholly or partially inside EOF */
2548 if (page_start + PAGE_CACHE_SIZE > size)
2549 end = size & ~PAGE_CACHE_MASK;
2551 end = PAGE_CACHE_SIZE;
2553 ret = btrfs_cow_one_page(inode, page, end);
2561 static void btrfs_truncate(struct inode *inode)
2563 struct btrfs_root *root = BTRFS_I(inode)->root;
2565 struct btrfs_trans_handle *trans;
2568 if (!S_ISREG(inode->i_mode))
2570 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2573 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2575 trans = btrfs_start_transaction(root, 1);
2576 btrfs_set_trans_block_group(trans, inode);
2578 /* FIXME, add redo link to tree so we don't leak on crash */
2579 ret = btrfs_truncate_in_trans(trans, root, inode,
2580 BTRFS_EXTENT_DATA_KEY);
2581 btrfs_update_inode(trans, root, inode);
2582 nr = trans->blocks_used;
2584 ret = btrfs_end_transaction_throttle(trans, root);
2586 btrfs_btree_balance_dirty(root, nr);
2590 * Invalidate a single dcache entry at the root of the filesystem.
2591 * Needed after creation of snapshot or subvolume.
2593 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2596 struct dentry *alias, *entry;
2599 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2603 /* change me if btrfs ever gets a d_hash operation */
2604 qstr.hash = full_name_hash(qstr.name, qstr.len);
2605 entry = d_lookup(alias, &qstr);
2608 d_invalidate(entry);
2614 int btrfs_create_subvol_root(struct btrfs_root *new_root,
2615 struct btrfs_trans_handle *trans, u64 new_dirid,
2616 struct btrfs_block_group_cache *block_group)
2618 struct inode *inode;
2621 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2622 new_dirid, block_group, S_IFDIR | 0700);
2624 return PTR_ERR(inode);
2625 inode->i_op = &btrfs_dir_inode_operations;
2626 inode->i_fop = &btrfs_dir_file_operations;
2627 new_root->inode = inode;
2629 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2634 return btrfs_update_inode(trans, new_root, inode);
2637 unsigned long btrfs_force_ra(struct address_space *mapping,
2638 struct file_ra_state *ra, struct file *file,
2639 pgoff_t offset, pgoff_t last_index)
2641 pgoff_t req_size = last_index - offset + 1;
2643 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2644 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2647 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2648 return offset + req_size;
2652 struct inode *btrfs_alloc_inode(struct super_block *sb)
2654 struct btrfs_inode *ei;
2656 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2660 ei->ordered_trans = 0;
2661 return &ei->vfs_inode;
2664 void btrfs_destroy_inode(struct inode *inode)
2666 WARN_ON(!list_empty(&inode->i_dentry));
2667 WARN_ON(inode->i_data.nrpages);
2669 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2670 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2673 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2674 static void init_once(struct kmem_cache * cachep, void *foo)
2676 static void init_once(void * foo, struct kmem_cache * cachep,
2677 unsigned long flags)
2680 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2682 inode_init_once(&ei->vfs_inode);
2685 void btrfs_destroy_cachep(void)
2687 if (btrfs_inode_cachep)
2688 kmem_cache_destroy(btrfs_inode_cachep);
2689 if (btrfs_trans_handle_cachep)
2690 kmem_cache_destroy(btrfs_trans_handle_cachep);
2691 if (btrfs_transaction_cachep)
2692 kmem_cache_destroy(btrfs_transaction_cachep);
2693 if (btrfs_bit_radix_cachep)
2694 kmem_cache_destroy(btrfs_bit_radix_cachep);
2695 if (btrfs_path_cachep)
2696 kmem_cache_destroy(btrfs_path_cachep);
2699 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2700 unsigned long extra_flags,
2701 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2702 void (*ctor)(struct kmem_cache *, void *)
2704 void (*ctor)(void *, struct kmem_cache *,
2709 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2710 SLAB_MEM_SPREAD | extra_flags), ctor
2711 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2717 int btrfs_init_cachep(void)
2719 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2720 sizeof(struct btrfs_inode),
2722 if (!btrfs_inode_cachep)
2724 btrfs_trans_handle_cachep =
2725 btrfs_cache_create("btrfs_trans_handle_cache",
2726 sizeof(struct btrfs_trans_handle),
2728 if (!btrfs_trans_handle_cachep)
2730 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2731 sizeof(struct btrfs_transaction),
2733 if (!btrfs_transaction_cachep)
2735 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2736 sizeof(struct btrfs_path),
2738 if (!btrfs_path_cachep)
2740 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2741 SLAB_DESTROY_BY_RCU, NULL);
2742 if (!btrfs_bit_radix_cachep)
2746 btrfs_destroy_cachep();
2750 static int btrfs_getattr(struct vfsmount *mnt,
2751 struct dentry *dentry, struct kstat *stat)
2753 struct inode *inode = dentry->d_inode;
2754 generic_fillattr(inode, stat);
2755 stat->blksize = PAGE_CACHE_SIZE;
2756 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2760 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2761 struct inode * new_dir,struct dentry *new_dentry)
2763 struct btrfs_trans_handle *trans;
2764 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2765 struct inode *new_inode = new_dentry->d_inode;
2766 struct inode *old_inode = old_dentry->d_inode;
2767 struct timespec ctime = CURRENT_TIME;
2770 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2771 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2775 ret = btrfs_check_free_space(root, 1, 0);
2779 trans = btrfs_start_transaction(root, 1);
2781 btrfs_set_trans_block_group(trans, new_dir);
2783 old_dentry->d_inode->i_nlink++;
2784 old_dir->i_ctime = old_dir->i_mtime = ctime;
2785 new_dir->i_ctime = new_dir->i_mtime = ctime;
2786 old_inode->i_ctime = ctime;
2788 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2793 new_inode->i_ctime = CURRENT_TIME;
2794 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2798 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2803 btrfs_end_transaction(trans, root);
2808 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2809 const char *symname)
2811 struct btrfs_trans_handle *trans;
2812 struct btrfs_root *root = BTRFS_I(dir)->root;
2813 struct btrfs_path *path;
2814 struct btrfs_key key;
2815 struct inode *inode = NULL;
2822 struct btrfs_file_extent_item *ei;
2823 struct extent_buffer *leaf;
2824 unsigned long nr = 0;
2826 name_len = strlen(symname) + 1;
2827 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2828 return -ENAMETOOLONG;
2830 err = btrfs_check_free_space(root, 1, 0);
2834 trans = btrfs_start_transaction(root, 1);
2835 btrfs_set_trans_block_group(trans, dir);
2837 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2843 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2845 dentry->d_parent->d_inode->i_ino, objectid,
2846 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2847 err = PTR_ERR(inode);
2851 btrfs_set_trans_block_group(trans, inode);
2852 err = btrfs_add_nondir(trans, dentry, inode, 0);
2856 inode->i_mapping->a_ops = &btrfs_aops;
2857 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2858 inode->i_fop = &btrfs_file_operations;
2859 inode->i_op = &btrfs_file_inode_operations;
2860 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2861 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2862 inode->i_mapping, GFP_NOFS);
2863 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2864 inode->i_mapping, GFP_NOFS);
2865 BTRFS_I(inode)->delalloc_bytes = 0;
2866 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
2867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2869 dir->i_sb->s_dirt = 1;
2870 btrfs_update_inode_block_group(trans, inode);
2871 btrfs_update_inode_block_group(trans, dir);
2875 path = btrfs_alloc_path();
2877 key.objectid = inode->i_ino;
2879 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2880 datasize = btrfs_file_extent_calc_inline_size(name_len);
2881 err = btrfs_insert_empty_item(trans, root, path, &key,
2887 leaf = path->nodes[0];
2888 ei = btrfs_item_ptr(leaf, path->slots[0],
2889 struct btrfs_file_extent_item);
2890 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2891 btrfs_set_file_extent_type(leaf, ei,
2892 BTRFS_FILE_EXTENT_INLINE);
2893 ptr = btrfs_file_extent_inline_start(ei);
2894 write_extent_buffer(leaf, symname, ptr, name_len);
2895 btrfs_mark_buffer_dirty(leaf);
2896 btrfs_free_path(path);
2898 inode->i_op = &btrfs_symlink_inode_operations;
2899 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2900 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2901 inode->i_size = name_len - 1;
2902 err = btrfs_update_inode(trans, root, inode);
2907 nr = trans->blocks_used;
2908 btrfs_end_transaction_throttle(trans, root);
2911 inode_dec_link_count(inode);
2914 btrfs_btree_balance_dirty(root, nr);
2918 static int btrfs_permission(struct inode *inode, int mask,
2919 struct nameidata *nd)
2921 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2923 return generic_permission(inode, mask, NULL);
2926 static struct inode_operations btrfs_dir_inode_operations = {
2927 .lookup = btrfs_lookup,
2928 .create = btrfs_create,
2929 .unlink = btrfs_unlink,
2931 .mkdir = btrfs_mkdir,
2932 .rmdir = btrfs_rmdir,
2933 .rename = btrfs_rename,
2934 .symlink = btrfs_symlink,
2935 .setattr = btrfs_setattr,
2936 .mknod = btrfs_mknod,
2937 .setxattr = generic_setxattr,
2938 .getxattr = generic_getxattr,
2939 .listxattr = btrfs_listxattr,
2940 .removexattr = generic_removexattr,
2941 .permission = btrfs_permission,
2943 static struct inode_operations btrfs_dir_ro_inode_operations = {
2944 .lookup = btrfs_lookup,
2945 .permission = btrfs_permission,
2947 static struct file_operations btrfs_dir_file_operations = {
2948 .llseek = generic_file_llseek,
2949 .read = generic_read_dir,
2950 .readdir = btrfs_readdir,
2951 .unlocked_ioctl = btrfs_ioctl,
2952 #ifdef CONFIG_COMPAT
2953 .compat_ioctl = btrfs_ioctl,
2955 .release = btrfs_release_file,
2958 static struct extent_io_ops btrfs_extent_io_ops = {
2959 .fill_delalloc = run_delalloc_range,
2960 .submit_bio_hook = btrfs_submit_bio_hook,
2961 .merge_bio_hook = btrfs_merge_bio_hook,
2962 .readpage_io_hook = btrfs_readpage_io_hook,
2963 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2964 .readpage_io_failed_hook = btrfs_io_failed_hook,
2965 .set_bit_hook = btrfs_set_bit_hook,
2966 .clear_bit_hook = btrfs_clear_bit_hook,
2969 static struct address_space_operations btrfs_aops = {
2970 .readpage = btrfs_readpage,
2971 .writepage = btrfs_writepage,
2972 .writepages = btrfs_writepages,
2973 .readpages = btrfs_readpages,
2974 .sync_page = block_sync_page,
2976 .direct_IO = btrfs_direct_IO,
2977 .invalidatepage = btrfs_invalidatepage,
2978 .releasepage = btrfs_releasepage,
2979 .set_page_dirty = __set_page_dirty_nobuffers,
2982 static struct address_space_operations btrfs_symlink_aops = {
2983 .readpage = btrfs_readpage,
2984 .writepage = btrfs_writepage,
2985 .invalidatepage = btrfs_invalidatepage,
2986 .releasepage = btrfs_releasepage,
2989 static struct inode_operations btrfs_file_inode_operations = {
2990 .truncate = btrfs_truncate,
2991 .getattr = btrfs_getattr,
2992 .setattr = btrfs_setattr,
2993 .setxattr = generic_setxattr,
2994 .getxattr = generic_getxattr,
2995 .listxattr = btrfs_listxattr,
2996 .removexattr = generic_removexattr,
2997 .permission = btrfs_permission,
2999 static struct inode_operations btrfs_special_inode_operations = {
3000 .getattr = btrfs_getattr,
3001 .setattr = btrfs_setattr,
3002 .permission = btrfs_permission,
3004 static struct inode_operations btrfs_symlink_inode_operations = {
3005 .readlink = generic_readlink,
3006 .follow_link = page_follow_link_light,
3007 .put_link = page_put_link,
3008 .permission = btrfs_permission,