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,
82 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
83 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
95 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
96 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
98 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
102 static int cow_file_range(struct inode *inode, u64 start, u64 end)
104 struct btrfs_root *root = BTRFS_I(inode)->root;
105 struct btrfs_trans_handle *trans;
109 u64 blocksize = root->sectorsize;
110 u64 orig_start = start;
112 struct btrfs_key ins;
115 trans = btrfs_start_transaction(root, 1);
117 btrfs_set_trans_block_group(trans, inode);
119 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
120 num_bytes = max(blocksize, num_bytes);
121 ret = btrfs_drop_extents(trans, root, inode,
122 start, start + num_bytes, start, &alloc_hint);
123 orig_num_bytes = num_bytes;
125 if (alloc_hint == EXTENT_MAP_INLINE)
128 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
130 while(num_bytes > 0) {
131 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
132 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
134 root->root_key.objectid,
136 inode->i_ino, start, 0,
137 alloc_hint, (u64)-1, &ins, 1);
142 cur_alloc_size = ins.offset;
143 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
144 start, ins.objectid, ins.offset,
146 inode->i_blocks += ins.offset >> 9;
147 btrfs_check_file(root, inode);
148 if (num_bytes < cur_alloc_size) {
149 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
153 num_bytes -= cur_alloc_size;
154 alloc_hint = ins.objectid + ins.offset;
155 start += cur_alloc_size;
157 btrfs_drop_extent_cache(inode, orig_start,
158 orig_start + orig_num_bytes - 1);
159 btrfs_add_ordered_inode(inode);
160 btrfs_update_inode(trans, root, inode);
162 btrfs_end_transaction(trans, root);
166 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
174 struct btrfs_root *root = BTRFS_I(inode)->root;
175 struct btrfs_block_group_cache *block_group;
176 struct extent_buffer *leaf;
178 struct btrfs_path *path;
179 struct btrfs_file_extent_item *item;
182 struct btrfs_key found_key;
184 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
185 path = btrfs_alloc_path();
188 ret = btrfs_lookup_file_extent(NULL, root, path,
189 inode->i_ino, start, 0);
191 btrfs_free_path(path);
197 if (path->slots[0] == 0)
202 leaf = path->nodes[0];
203 item = btrfs_item_ptr(leaf, path->slots[0],
204 struct btrfs_file_extent_item);
206 /* are we inside the extent that was found? */
207 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
208 found_type = btrfs_key_type(&found_key);
209 if (found_key.objectid != inode->i_ino ||
210 found_type != BTRFS_EXTENT_DATA_KEY)
213 found_type = btrfs_file_extent_type(leaf, item);
214 extent_start = found_key.offset;
215 if (found_type == BTRFS_FILE_EXTENT_REG) {
216 u64 extent_num_bytes;
218 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
219 extent_end = extent_start + extent_num_bytes;
222 if (loops && start != extent_start)
225 if (start < extent_start || start >= extent_end)
228 cow_end = min(end, extent_end - 1);
229 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
233 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
239 * we may be called by the resizer, make sure we're inside
240 * the limits of the FS
242 block_group = btrfs_lookup_block_group(root->fs_info,
244 if (!block_group || block_group->ro)
253 btrfs_free_path(path);
256 btrfs_release_path(root, path);
261 cow_file_range(inode, start, end);
266 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
268 struct btrfs_root *root = BTRFS_I(inode)->root;
270 mutex_lock(&root->fs_info->fs_mutex);
271 if (btrfs_test_opt(root, NODATACOW) ||
272 btrfs_test_flag(inode, NODATACOW))
273 ret = run_delalloc_nocow(inode, start, end);
275 ret = cow_file_range(inode, start, end);
277 mutex_unlock(&root->fs_info->fs_mutex);
281 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
282 unsigned long old, unsigned long bits)
285 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
286 struct btrfs_root *root = BTRFS_I(inode)->root;
287 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
288 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
289 root->fs_info->delalloc_bytes += end - start + 1;
290 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
295 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
296 unsigned long old, unsigned long bits)
298 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
299 struct btrfs_root *root = BTRFS_I(inode)->root;
302 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
303 if (end - start + 1 > root->fs_info->delalloc_bytes) {
304 printk("warning: delalloc account %Lu %Lu\n",
305 end - start + 1, root->fs_info->delalloc_bytes);
306 root->fs_info->delalloc_bytes = 0;
307 BTRFS_I(inode)->delalloc_bytes = 0;
309 root->fs_info->delalloc_bytes -= end - start + 1;
310 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
312 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
317 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
318 size_t size, struct bio *bio)
320 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
321 struct btrfs_mapping_tree *map_tree;
322 u64 logical = bio->bi_sector << 9;
327 length = bio->bi_size;
328 map_tree = &root->fs_info->mapping_tree;
330 ret = btrfs_map_block(map_tree, READ, logical,
331 &map_length, NULL, 0);
333 if (map_length < length + size) {
339 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
347 ret = btrfs_csum_one_bio(root, bio, &sums);
350 mutex_lock(&root->fs_info->fs_mutex);
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);
358 mutex_unlock(&root->fs_info->fs_mutex);
362 return btrfs_map_bio(root, rw, bio, mirror_num);
365 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
368 struct btrfs_root *root = BTRFS_I(inode)->root;
371 if (!(rw & (1 << BIO_RW))) {
372 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
377 if (btrfs_test_opt(root, NODATASUM) ||
378 btrfs_test_flag(inode, NODATASUM)) {
382 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
383 inode, rw, bio, mirror_num,
384 __btrfs_submit_bio_hook);
386 return btrfs_map_bio(root, rw, bio, mirror_num);
389 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
392 struct inode *inode = page->mapping->host;
393 struct btrfs_root *root = BTRFS_I(inode)->root;
394 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
395 struct btrfs_csum_item *item;
396 struct btrfs_path *path = NULL;
399 if (btrfs_test_opt(root, NODATASUM) ||
400 btrfs_test_flag(inode, NODATASUM))
403 mutex_lock(&root->fs_info->fs_mutex);
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);
421 mutex_unlock(&root->fs_info->fs_mutex);
425 struct io_failure_record {
433 int btrfs_io_failed_hook(struct bio *failed_bio,
434 struct page *page, u64 start, u64 end,
435 struct extent_state *state)
437 struct io_failure_record *failrec = NULL;
439 struct extent_map *em;
440 struct inode *inode = page->mapping->host;
441 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
442 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
449 ret = get_state_private(failure_tree, start, &private);
451 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
454 failrec->start = start;
455 failrec->len = end - start + 1;
456 failrec->last_mirror = 0;
458 spin_lock(&em_tree->lock);
459 em = lookup_extent_mapping(em_tree, start, failrec->len);
460 if (em->start > start || em->start + em->len < start) {
464 spin_unlock(&em_tree->lock);
466 if (!em || IS_ERR(em)) {
470 logical = start - em->start;
471 logical = em->block_start + logical;
472 failrec->logical = logical;
474 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
475 EXTENT_DIRTY, GFP_NOFS);
476 set_state_private(failure_tree, start,
477 (u64)(unsigned long)failrec);
479 failrec = (struct io_failure_record *)(unsigned long)private;
481 num_copies = btrfs_num_copies(
482 &BTRFS_I(inode)->root->fs_info->mapping_tree,
483 failrec->logical, failrec->len);
484 failrec->last_mirror++;
486 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
487 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
490 if (state && state->start != failrec->start)
492 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
494 if (!state || failrec->last_mirror > num_copies) {
495 set_state_private(failure_tree, failrec->start, 0);
496 clear_extent_bits(failure_tree, failrec->start,
497 failrec->start + failrec->len - 1,
498 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
502 bio = bio_alloc(GFP_NOFS, 1);
503 bio->bi_private = state;
504 bio->bi_end_io = failed_bio->bi_end_io;
505 bio->bi_sector = failrec->logical >> 9;
506 bio->bi_bdev = failed_bio->bi_bdev;
508 bio_add_page(bio, page, failrec->len, start - page_offset(page));
509 if (failed_bio->bi_rw & (1 << BIO_RW))
514 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
515 failrec->last_mirror);
519 int btrfs_clean_io_failures(struct inode *inode, u64 start)
523 struct io_failure_record *failure;
527 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
528 (u64)-1, 1, EXTENT_DIRTY)) {
529 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
530 start, &private_failure);
532 failure = (struct io_failure_record *)(unsigned long)
534 set_state_private(&BTRFS_I(inode)->io_failure_tree,
536 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
538 failure->start + failure->len - 1,
539 EXTENT_DIRTY | EXTENT_LOCKED,
547 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
548 struct extent_state *state)
550 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
551 struct inode *inode = page->mapping->host;
552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
554 u64 private = ~(u32)0;
556 struct btrfs_root *root = BTRFS_I(inode)->root;
560 if (btrfs_test_opt(root, NODATASUM) ||
561 btrfs_test_flag(inode, NODATASUM))
563 if (state && state->start == start) {
564 private = state->private;
567 ret = get_state_private(io_tree, start, &private);
569 local_irq_save(flags);
570 kaddr = kmap_atomic(page, KM_IRQ0);
574 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
575 btrfs_csum_final(csum, (char *)&csum);
576 if (csum != private) {
579 kunmap_atomic(kaddr, KM_IRQ0);
580 local_irq_restore(flags);
582 /* if the io failure tree for this inode is non-empty,
583 * check to see if we've recovered from a failed IO
585 btrfs_clean_io_failures(inode, start);
589 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
590 page->mapping->host->i_ino, (unsigned long long)start, csum,
592 memset(kaddr + offset, 1, end - start + 1);
593 flush_dcache_page(page);
594 kunmap_atomic(kaddr, KM_IRQ0);
595 local_irq_restore(flags);
601 void btrfs_read_locked_inode(struct inode *inode)
603 struct btrfs_path *path;
604 struct extent_buffer *leaf;
605 struct btrfs_inode_item *inode_item;
606 struct btrfs_timespec *tspec;
607 struct btrfs_root *root = BTRFS_I(inode)->root;
608 struct btrfs_key location;
609 u64 alloc_group_block;
613 path = btrfs_alloc_path();
615 mutex_lock(&root->fs_info->fs_mutex);
616 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
618 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
622 leaf = path->nodes[0];
623 inode_item = btrfs_item_ptr(leaf, path->slots[0],
624 struct btrfs_inode_item);
626 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
627 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
628 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
629 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
630 inode->i_size = btrfs_inode_size(leaf, inode_item);
632 tspec = btrfs_inode_atime(inode_item);
633 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
634 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
636 tspec = btrfs_inode_mtime(inode_item);
637 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
638 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
640 tspec = btrfs_inode_ctime(inode_item);
641 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
642 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
644 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
645 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
647 rdev = btrfs_inode_rdev(leaf, inode_item);
649 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
650 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
652 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
653 if (!BTRFS_I(inode)->block_group) {
654 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
656 BTRFS_BLOCK_GROUP_METADATA, 0);
658 btrfs_free_path(path);
661 mutex_unlock(&root->fs_info->fs_mutex);
663 switch (inode->i_mode & S_IFMT) {
665 inode->i_mapping->a_ops = &btrfs_aops;
666 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
667 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
668 inode->i_fop = &btrfs_file_operations;
669 inode->i_op = &btrfs_file_inode_operations;
672 inode->i_fop = &btrfs_dir_file_operations;
673 if (root == root->fs_info->tree_root)
674 inode->i_op = &btrfs_dir_ro_inode_operations;
676 inode->i_op = &btrfs_dir_inode_operations;
679 inode->i_op = &btrfs_symlink_inode_operations;
680 inode->i_mapping->a_ops = &btrfs_symlink_aops;
681 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
684 init_special_inode(inode, inode->i_mode, rdev);
690 btrfs_release_path(root, path);
691 btrfs_free_path(path);
692 mutex_unlock(&root->fs_info->fs_mutex);
693 make_bad_inode(inode);
696 static void fill_inode_item(struct extent_buffer *leaf,
697 struct btrfs_inode_item *item,
700 btrfs_set_inode_uid(leaf, item, inode->i_uid);
701 btrfs_set_inode_gid(leaf, item, inode->i_gid);
702 btrfs_set_inode_size(leaf, item, inode->i_size);
703 btrfs_set_inode_mode(leaf, item, inode->i_mode);
704 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
706 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
707 inode->i_atime.tv_sec);
708 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
709 inode->i_atime.tv_nsec);
711 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
712 inode->i_mtime.tv_sec);
713 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
714 inode->i_mtime.tv_nsec);
716 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
717 inode->i_ctime.tv_sec);
718 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
719 inode->i_ctime.tv_nsec);
721 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
722 btrfs_set_inode_generation(leaf, item, inode->i_generation);
723 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
724 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
725 btrfs_set_inode_block_group(leaf, item,
726 BTRFS_I(inode)->block_group->key.objectid);
729 int btrfs_update_inode(struct btrfs_trans_handle *trans,
730 struct btrfs_root *root,
733 struct btrfs_inode_item *inode_item;
734 struct btrfs_path *path;
735 struct extent_buffer *leaf;
738 path = btrfs_alloc_path();
740 ret = btrfs_lookup_inode(trans, root, path,
741 &BTRFS_I(inode)->location, 1);
748 leaf = path->nodes[0];
749 inode_item = btrfs_item_ptr(leaf, path->slots[0],
750 struct btrfs_inode_item);
752 fill_inode_item(leaf, inode_item, inode);
753 btrfs_mark_buffer_dirty(leaf);
754 btrfs_set_inode_last_trans(trans, inode);
757 btrfs_release_path(root, path);
758 btrfs_free_path(path);
763 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
764 struct btrfs_root *root,
766 struct dentry *dentry)
768 struct btrfs_path *path;
769 const char *name = dentry->d_name.name;
770 int name_len = dentry->d_name.len;
772 struct extent_buffer *leaf;
773 struct btrfs_dir_item *di;
774 struct btrfs_key key;
776 path = btrfs_alloc_path();
782 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
792 leaf = path->nodes[0];
793 btrfs_dir_item_key_to_cpu(leaf, di, &key);
794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
797 btrfs_release_path(root, path);
799 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
800 key.objectid, name, name_len, -1);
809 ret = btrfs_delete_one_dir_name(trans, root, path, di);
811 dentry->d_inode->i_ctime = dir->i_ctime;
812 ret = btrfs_del_inode_ref(trans, root, name, name_len,
813 dentry->d_inode->i_ino,
814 dentry->d_parent->d_inode->i_ino);
816 printk("failed to delete reference to %.*s, "
817 "inode %lu parent %lu\n", name_len, name,
818 dentry->d_inode->i_ino,
819 dentry->d_parent->d_inode->i_ino);
822 btrfs_free_path(path);
824 dir->i_size -= name_len * 2;
825 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
826 btrfs_update_inode(trans, root, dir);
827 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
828 dentry->d_inode->i_nlink--;
830 drop_nlink(dentry->d_inode);
832 ret = btrfs_update_inode(trans, root, dentry->d_inode);
833 dir->i_sb->s_dirt = 1;
838 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
840 struct btrfs_root *root;
841 struct btrfs_trans_handle *trans;
842 struct inode *inode = dentry->d_inode;
844 unsigned long nr = 0;
846 root = BTRFS_I(dir)->root;
847 mutex_lock(&root->fs_info->fs_mutex);
849 ret = btrfs_check_free_space(root, 1, 1);
853 trans = btrfs_start_transaction(root, 1);
855 btrfs_set_trans_block_group(trans, dir);
856 ret = btrfs_unlink_trans(trans, root, dir, dentry);
857 nr = trans->blocks_used;
859 if (inode->i_nlink == 0) {
861 /* if the inode isn't linked anywhere,
862 * we don't need to worry about
865 found = btrfs_del_ordered_inode(inode);
867 atomic_dec(&inode->i_count);
871 btrfs_end_transaction(trans, root);
873 mutex_unlock(&root->fs_info->fs_mutex);
874 btrfs_btree_balance_dirty(root, nr);
875 btrfs_throttle(root);
879 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
881 struct inode *inode = dentry->d_inode;
884 struct btrfs_root *root = BTRFS_I(dir)->root;
885 struct btrfs_trans_handle *trans;
886 unsigned long nr = 0;
888 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
891 mutex_lock(&root->fs_info->fs_mutex);
892 ret = btrfs_check_free_space(root, 1, 1);
896 trans = btrfs_start_transaction(root, 1);
897 btrfs_set_trans_block_group(trans, dir);
899 /* now the directory is empty */
900 err = btrfs_unlink_trans(trans, root, dir, dentry);
905 nr = trans->blocks_used;
906 ret = btrfs_end_transaction(trans, root);
908 mutex_unlock(&root->fs_info->fs_mutex);
909 btrfs_btree_balance_dirty(root, nr);
910 btrfs_throttle(root);
918 * this can truncate away extent items, csum items and directory items.
919 * It starts at a high offset and removes keys until it can't find
920 * any higher than i_size.
922 * csum items that cross the new i_size are truncated to the new size
925 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
926 struct btrfs_root *root,
931 struct btrfs_path *path;
932 struct btrfs_key key;
933 struct btrfs_key found_key;
935 struct extent_buffer *leaf;
936 struct btrfs_file_extent_item *fi;
937 u64 extent_start = 0;
938 u64 extent_num_bytes = 0;
944 int pending_del_nr = 0;
945 int pending_del_slot = 0;
946 int extent_type = -1;
947 u64 mask = root->sectorsize - 1;
949 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
950 path = btrfs_alloc_path();
954 /* FIXME, add redo link to tree so we don't leak on crash */
955 key.objectid = inode->i_ino;
956 key.offset = (u64)-1;
959 btrfs_init_path(path);
961 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
966 BUG_ON(path->slots[0] == 0);
972 leaf = path->nodes[0];
973 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
974 found_type = btrfs_key_type(&found_key);
976 if (found_key.objectid != inode->i_ino)
979 if (found_type < min_type)
982 item_end = found_key.offset;
983 if (found_type == BTRFS_EXTENT_DATA_KEY) {
984 fi = btrfs_item_ptr(leaf, path->slots[0],
985 struct btrfs_file_extent_item);
986 extent_type = btrfs_file_extent_type(leaf, fi);
987 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
989 btrfs_file_extent_num_bytes(leaf, fi);
990 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
991 struct btrfs_item *item = btrfs_item_nr(leaf,
993 item_end += btrfs_file_extent_inline_len(leaf,
998 if (found_type == BTRFS_CSUM_ITEM_KEY) {
999 ret = btrfs_csum_truncate(trans, root, path,
1003 if (item_end < inode->i_size) {
1004 if (found_type == BTRFS_DIR_ITEM_KEY) {
1005 found_type = BTRFS_INODE_ITEM_KEY;
1006 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1007 found_type = BTRFS_CSUM_ITEM_KEY;
1008 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1009 found_type = BTRFS_XATTR_ITEM_KEY;
1010 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1011 found_type = BTRFS_INODE_REF_KEY;
1012 } else if (found_type) {
1017 btrfs_set_key_type(&key, found_type);
1020 if (found_key.offset >= inode->i_size)
1026 /* FIXME, shrink the extent if the ref count is only 1 */
1027 if (found_type != BTRFS_EXTENT_DATA_KEY)
1030 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1032 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1034 u64 orig_num_bytes =
1035 btrfs_file_extent_num_bytes(leaf, fi);
1036 extent_num_bytes = inode->i_size -
1037 found_key.offset + root->sectorsize - 1;
1038 extent_num_bytes = extent_num_bytes &
1039 ~((u64)root->sectorsize - 1);
1040 btrfs_set_file_extent_num_bytes(leaf, fi,
1042 num_dec = (orig_num_bytes -
1044 if (extent_start != 0)
1045 dec_i_blocks(inode, num_dec);
1046 btrfs_mark_buffer_dirty(leaf);
1049 btrfs_file_extent_disk_num_bytes(leaf,
1051 /* FIXME blocksize != 4096 */
1052 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1053 if (extent_start != 0) {
1055 dec_i_blocks(inode, num_dec);
1057 root_gen = btrfs_header_generation(leaf);
1058 root_owner = btrfs_header_owner(leaf);
1060 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1062 u32 newsize = inode->i_size - found_key.offset;
1063 dec_i_blocks(inode, item_end + 1 -
1064 found_key.offset - newsize);
1066 btrfs_file_extent_calc_inline_size(newsize);
1067 ret = btrfs_truncate_item(trans, root, path,
1071 dec_i_blocks(inode, item_end + 1 -
1077 if (!pending_del_nr) {
1078 /* no pending yet, add ourselves */
1079 pending_del_slot = path->slots[0];
1081 } else if (pending_del_nr &&
1082 path->slots[0] + 1 == pending_del_slot) {
1083 /* hop on the pending chunk */
1085 pending_del_slot = path->slots[0];
1087 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1093 ret = btrfs_free_extent(trans, root, extent_start,
1096 root_gen, inode->i_ino,
1097 found_key.offset, 0);
1101 if (path->slots[0] == 0) {
1104 btrfs_release_path(root, path);
1109 if (pending_del_nr &&
1110 path->slots[0] + 1 != pending_del_slot) {
1111 struct btrfs_key debug;
1113 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1115 ret = btrfs_del_items(trans, root, path,
1120 btrfs_release_path(root, path);
1126 if (pending_del_nr) {
1127 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1130 btrfs_release_path(root, path);
1131 btrfs_free_path(path);
1132 inode->i_sb->s_dirt = 1;
1136 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1140 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1141 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1142 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1145 WARN_ON(!PageLocked(page));
1146 set_page_extent_mapped(page);
1148 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1149 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1150 page_end, GFP_NOFS);
1152 if (zero_start != PAGE_CACHE_SIZE) {
1154 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1155 flush_dcache_page(page);
1158 set_page_dirty(page);
1159 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1165 * taken from block_truncate_page, but does cow as it zeros out
1166 * any bytes left in the last page in the file.
1168 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1170 struct inode *inode = mapping->host;
1171 struct btrfs_root *root = BTRFS_I(inode)->root;
1172 u32 blocksize = root->sectorsize;
1173 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1174 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1179 if ((offset & (blocksize - 1)) == 0)
1184 page = grab_cache_page(mapping, index);
1187 if (!PageUptodate(page)) {
1188 ret = btrfs_readpage(NULL, page);
1190 if (page->mapping != mapping) {
1192 page_cache_release(page);
1195 if (!PageUptodate(page)) {
1201 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1202 wait_on_page_writeback(page);
1203 ret = btrfs_cow_one_page(inode, page, offset);
1206 page_cache_release(page);
1211 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1213 struct inode *inode = dentry->d_inode;
1216 err = inode_change_ok(inode, attr);
1220 if (S_ISREG(inode->i_mode) &&
1221 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1222 struct btrfs_trans_handle *trans;
1223 struct btrfs_root *root = BTRFS_I(inode)->root;
1224 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1226 u64 mask = root->sectorsize - 1;
1227 u64 hole_start = (inode->i_size + mask) & ~mask;
1228 u64 block_end = (attr->ia_size + mask) & ~mask;
1232 if (attr->ia_size <= hole_start)
1235 mutex_lock(&root->fs_info->fs_mutex);
1236 err = btrfs_check_free_space(root, 1, 0);
1237 mutex_unlock(&root->fs_info->fs_mutex);
1241 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1243 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1244 hole_size = block_end - hole_start;
1246 mutex_lock(&root->fs_info->fs_mutex);
1247 trans = btrfs_start_transaction(root, 1);
1248 btrfs_set_trans_block_group(trans, inode);
1249 err = btrfs_drop_extents(trans, root, inode,
1250 hole_start, block_end, hole_start,
1253 if (alloc_hint != EXTENT_MAP_INLINE) {
1254 err = btrfs_insert_file_extent(trans, root,
1258 btrfs_drop_extent_cache(inode, hole_start,
1260 btrfs_check_file(root, inode);
1262 btrfs_end_transaction(trans, root);
1263 mutex_unlock(&root->fs_info->fs_mutex);
1264 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1269 err = inode_setattr(inode, attr);
1274 void btrfs_put_inode(struct inode *inode)
1278 if (!BTRFS_I(inode)->ordered_trans) {
1282 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1283 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1286 ret = btrfs_del_ordered_inode(inode);
1288 atomic_dec(&inode->i_count);
1292 void btrfs_delete_inode(struct inode *inode)
1294 struct btrfs_trans_handle *trans;
1295 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 truncate_inode_pages(&inode->i_data, 0);
1300 if (is_bad_inode(inode)) {
1305 mutex_lock(&root->fs_info->fs_mutex);
1306 trans = btrfs_start_transaction(root, 1);
1308 btrfs_set_trans_block_group(trans, inode);
1309 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1311 goto no_delete_lock;
1313 nr = trans->blocks_used;
1316 btrfs_end_transaction(trans, root);
1317 mutex_unlock(&root->fs_info->fs_mutex);
1318 btrfs_btree_balance_dirty(root, nr);
1319 btrfs_throttle(root);
1323 nr = trans->blocks_used;
1324 btrfs_end_transaction(trans, root);
1325 mutex_unlock(&root->fs_info->fs_mutex);
1326 btrfs_btree_balance_dirty(root, nr);
1327 btrfs_throttle(root);
1333 * this returns the key found in the dir entry in the location pointer.
1334 * If no dir entries were found, location->objectid is 0.
1336 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1337 struct btrfs_key *location)
1339 const char *name = dentry->d_name.name;
1340 int namelen = dentry->d_name.len;
1341 struct btrfs_dir_item *di;
1342 struct btrfs_path *path;
1343 struct btrfs_root *root = BTRFS_I(dir)->root;
1346 if (namelen == 1 && strcmp(name, ".") == 0) {
1347 location->objectid = dir->i_ino;
1348 location->type = BTRFS_INODE_ITEM_KEY;
1349 location->offset = 0;
1352 path = btrfs_alloc_path();
1355 if (namelen == 2 && strcmp(name, "..") == 0) {
1356 struct btrfs_key key;
1357 struct extent_buffer *leaf;
1361 key.objectid = dir->i_ino;
1362 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1368 leaf = path->nodes[0];
1369 slot = path->slots[0];
1370 nritems = btrfs_header_nritems(leaf);
1371 if (slot >= nritems)
1374 btrfs_item_key_to_cpu(leaf, &key, slot);
1375 if (key.objectid != dir->i_ino ||
1376 key.type != BTRFS_INODE_REF_KEY) {
1379 location->objectid = key.offset;
1380 location->type = BTRFS_INODE_ITEM_KEY;
1381 location->offset = 0;
1385 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1389 if (!di || IS_ERR(di)) {
1392 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1394 btrfs_free_path(path);
1397 location->objectid = 0;
1402 * when we hit a tree root in a directory, the btrfs part of the inode
1403 * needs to be changed to reflect the root directory of the tree root. This
1404 * is kind of like crossing a mount point.
1406 static int fixup_tree_root_location(struct btrfs_root *root,
1407 struct btrfs_key *location,
1408 struct btrfs_root **sub_root,
1409 struct dentry *dentry)
1411 struct btrfs_path *path;
1412 struct btrfs_root_item *ri;
1414 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1416 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1419 path = btrfs_alloc_path();
1421 mutex_lock(&root->fs_info->fs_mutex);
1423 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1424 dentry->d_name.name,
1425 dentry->d_name.len);
1426 if (IS_ERR(*sub_root))
1427 return PTR_ERR(*sub_root);
1429 ri = &(*sub_root)->root_item;
1430 location->objectid = btrfs_root_dirid(ri);
1431 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1432 location->offset = 0;
1434 btrfs_free_path(path);
1435 mutex_unlock(&root->fs_info->fs_mutex);
1439 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1441 struct btrfs_iget_args *args = p;
1442 inode->i_ino = args->ino;
1443 BTRFS_I(inode)->root = args->root;
1444 BTRFS_I(inode)->delalloc_bytes = 0;
1445 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1446 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1447 inode->i_mapping, GFP_NOFS);
1448 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1449 inode->i_mapping, GFP_NOFS);
1450 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1454 static int btrfs_find_actor(struct inode *inode, void *opaque)
1456 struct btrfs_iget_args *args = opaque;
1457 return (args->ino == inode->i_ino &&
1458 args->root == BTRFS_I(inode)->root);
1461 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1464 struct btrfs_iget_args args;
1465 args.ino = objectid;
1466 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1471 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1474 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1475 struct btrfs_root *root)
1477 struct inode *inode;
1478 struct btrfs_iget_args args;
1479 args.ino = objectid;
1482 inode = iget5_locked(s, objectid, btrfs_find_actor,
1483 btrfs_init_locked_inode,
1488 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1489 struct nameidata *nd)
1491 struct inode * inode;
1492 struct btrfs_inode *bi = BTRFS_I(dir);
1493 struct btrfs_root *root = bi->root;
1494 struct btrfs_root *sub_root = root;
1495 struct btrfs_key location;
1498 if (dentry->d_name.len > BTRFS_NAME_LEN)
1499 return ERR_PTR(-ENAMETOOLONG);
1501 mutex_lock(&root->fs_info->fs_mutex);
1502 ret = btrfs_inode_by_name(dir, dentry, &location);
1503 mutex_unlock(&root->fs_info->fs_mutex);
1506 return ERR_PTR(ret);
1509 if (location.objectid) {
1510 ret = fixup_tree_root_location(root, &location, &sub_root,
1513 return ERR_PTR(ret);
1515 return ERR_PTR(-ENOENT);
1516 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1519 return ERR_PTR(-EACCES);
1520 if (inode->i_state & I_NEW) {
1521 /* the inode and parent dir are two different roots */
1522 if (sub_root != root) {
1524 sub_root->inode = inode;
1526 BTRFS_I(inode)->root = sub_root;
1527 memcpy(&BTRFS_I(inode)->location, &location,
1529 btrfs_read_locked_inode(inode);
1530 unlock_new_inode(inode);
1533 return d_splice_alias(inode, dentry);
1536 static unsigned char btrfs_filetype_table[] = {
1537 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1540 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1542 struct inode *inode = filp->f_dentry->d_inode;
1543 struct btrfs_root *root = BTRFS_I(inode)->root;
1544 struct btrfs_item *item;
1545 struct btrfs_dir_item *di;
1546 struct btrfs_key key;
1547 struct btrfs_key found_key;
1548 struct btrfs_path *path;
1551 struct extent_buffer *leaf;
1554 unsigned char d_type;
1559 int key_type = BTRFS_DIR_INDEX_KEY;
1564 /* FIXME, use a real flag for deciding about the key type */
1565 if (root->fs_info->tree_root == root)
1566 key_type = BTRFS_DIR_ITEM_KEY;
1568 /* special case for "." */
1569 if (filp->f_pos == 0) {
1570 over = filldir(dirent, ".", 1,
1578 mutex_lock(&root->fs_info->fs_mutex);
1579 key.objectid = inode->i_ino;
1580 path = btrfs_alloc_path();
1583 /* special case for .., just use the back ref */
1584 if (filp->f_pos == 1) {
1585 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1587 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1589 leaf = path->nodes[0];
1590 slot = path->slots[0];
1591 nritems = btrfs_header_nritems(leaf);
1592 if (slot >= nritems) {
1593 btrfs_release_path(root, path);
1594 goto read_dir_items;
1596 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1597 btrfs_release_path(root, path);
1598 if (found_key.objectid != key.objectid ||
1599 found_key.type != BTRFS_INODE_REF_KEY)
1600 goto read_dir_items;
1601 over = filldir(dirent, "..", 2,
1602 2, found_key.offset, DT_DIR);
1609 btrfs_set_key_type(&key, key_type);
1610 key.offset = filp->f_pos;
1612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1617 leaf = path->nodes[0];
1618 nritems = btrfs_header_nritems(leaf);
1619 slot = path->slots[0];
1620 if (advance || slot >= nritems) {
1621 if (slot >= nritems -1) {
1622 ret = btrfs_next_leaf(root, path);
1625 leaf = path->nodes[0];
1626 nritems = btrfs_header_nritems(leaf);
1627 slot = path->slots[0];
1634 item = btrfs_item_nr(leaf, slot);
1635 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1637 if (found_key.objectid != key.objectid)
1639 if (btrfs_key_type(&found_key) != key_type)
1641 if (found_key.offset < filp->f_pos)
1644 filp->f_pos = found_key.offset;
1646 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1648 di_total = btrfs_item_size(leaf, item);
1649 while(di_cur < di_total) {
1650 struct btrfs_key location;
1652 name_len = btrfs_dir_name_len(leaf, di);
1653 if (name_len < 32) {
1654 name_ptr = tmp_name;
1656 name_ptr = kmalloc(name_len, GFP_NOFS);
1659 read_extent_buffer(leaf, name_ptr,
1660 (unsigned long)(di + 1), name_len);
1662 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1663 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1664 over = filldir(dirent, name_ptr, name_len,
1669 if (name_ptr != tmp_name)
1674 di_len = btrfs_dir_name_len(leaf, di) +
1675 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1677 di = (struct btrfs_dir_item *)((char *)di + di_len);
1680 if (key_type == BTRFS_DIR_INDEX_KEY)
1681 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1687 btrfs_release_path(root, path);
1688 btrfs_free_path(path);
1689 mutex_unlock(&root->fs_info->fs_mutex);
1693 int btrfs_write_inode(struct inode *inode, int wait)
1695 struct btrfs_root *root = BTRFS_I(inode)->root;
1696 struct btrfs_trans_handle *trans;
1700 mutex_lock(&root->fs_info->fs_mutex);
1701 trans = btrfs_start_transaction(root, 1);
1702 btrfs_set_trans_block_group(trans, inode);
1703 ret = btrfs_commit_transaction(trans, root);
1704 mutex_unlock(&root->fs_info->fs_mutex);
1710 * This is somewhat expensive, updating the tree every time the
1711 * inode changes. But, it is most likely to find the inode in cache.
1712 * FIXME, needs more benchmarking...there are no reasons other than performance
1713 * to keep or drop this code.
1715 void btrfs_dirty_inode(struct inode *inode)
1717 struct btrfs_root *root = BTRFS_I(inode)->root;
1718 struct btrfs_trans_handle *trans;
1720 mutex_lock(&root->fs_info->fs_mutex);
1721 trans = btrfs_start_transaction(root, 1);
1722 btrfs_set_trans_block_group(trans, inode);
1723 btrfs_update_inode(trans, root, inode);
1724 btrfs_end_transaction(trans, root);
1725 mutex_unlock(&root->fs_info->fs_mutex);
1728 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root,
1730 const char *name, int name_len,
1733 struct btrfs_block_group_cache *group,
1736 struct inode *inode;
1737 struct btrfs_inode_item *inode_item;
1738 struct btrfs_block_group_cache *new_inode_group;
1739 struct btrfs_key *location;
1740 struct btrfs_path *path;
1741 struct btrfs_inode_ref *ref;
1742 struct btrfs_key key[2];
1748 path = btrfs_alloc_path();
1751 inode = new_inode(root->fs_info->sb);
1753 return ERR_PTR(-ENOMEM);
1755 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1756 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1757 inode->i_mapping, GFP_NOFS);
1758 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1759 inode->i_mapping, GFP_NOFS);
1760 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1761 BTRFS_I(inode)->delalloc_bytes = 0;
1762 BTRFS_I(inode)->root = root;
1768 new_inode_group = btrfs_find_block_group(root, group, 0,
1769 BTRFS_BLOCK_GROUP_METADATA, owner);
1770 if (!new_inode_group) {
1771 printk("find_block group failed\n");
1772 new_inode_group = group;
1774 BTRFS_I(inode)->block_group = new_inode_group;
1775 BTRFS_I(inode)->flags = 0;
1777 key[0].objectid = objectid;
1778 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1781 key[1].objectid = objectid;
1782 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1783 key[1].offset = ref_objectid;
1785 sizes[0] = sizeof(struct btrfs_inode_item);
1786 sizes[1] = name_len + sizeof(*ref);
1788 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1792 if (objectid > root->highest_inode)
1793 root->highest_inode = objectid;
1795 inode->i_uid = current->fsuid;
1796 inode->i_gid = current->fsgid;
1797 inode->i_mode = mode;
1798 inode->i_ino = objectid;
1799 inode->i_blocks = 0;
1800 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1801 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1802 struct btrfs_inode_item);
1803 fill_inode_item(path->nodes[0], inode_item, inode);
1805 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1806 struct btrfs_inode_ref);
1807 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1808 ptr = (unsigned long)(ref + 1);
1809 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1811 btrfs_mark_buffer_dirty(path->nodes[0]);
1812 btrfs_free_path(path);
1814 location = &BTRFS_I(inode)->location;
1815 location->objectid = objectid;
1816 location->offset = 0;
1817 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1819 insert_inode_hash(inode);
1822 btrfs_free_path(path);
1823 return ERR_PTR(ret);
1826 static inline u8 btrfs_inode_type(struct inode *inode)
1828 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1831 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1832 struct dentry *dentry, struct inode *inode,
1836 struct btrfs_key key;
1837 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1838 struct inode *parent_inode;
1840 key.objectid = inode->i_ino;
1841 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1844 ret = btrfs_insert_dir_item(trans, root,
1845 dentry->d_name.name, dentry->d_name.len,
1846 dentry->d_parent->d_inode->i_ino,
1847 &key, btrfs_inode_type(inode));
1850 ret = btrfs_insert_inode_ref(trans, root,
1851 dentry->d_name.name,
1854 dentry->d_parent->d_inode->i_ino);
1856 parent_inode = dentry->d_parent->d_inode;
1857 parent_inode->i_size += dentry->d_name.len * 2;
1858 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1859 ret = btrfs_update_inode(trans, root,
1860 dentry->d_parent->d_inode);
1865 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1866 struct dentry *dentry, struct inode *inode,
1869 int err = btrfs_add_link(trans, dentry, inode, backref);
1871 d_instantiate(dentry, inode);
1879 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1880 int mode, dev_t rdev)
1882 struct btrfs_trans_handle *trans;
1883 struct btrfs_root *root = BTRFS_I(dir)->root;
1884 struct inode *inode = NULL;
1888 unsigned long nr = 0;
1890 if (!new_valid_dev(rdev))
1893 mutex_lock(&root->fs_info->fs_mutex);
1894 err = btrfs_check_free_space(root, 1, 0);
1898 trans = btrfs_start_transaction(root, 1);
1899 btrfs_set_trans_block_group(trans, dir);
1901 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1907 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1909 dentry->d_parent->d_inode->i_ino, objectid,
1910 BTRFS_I(dir)->block_group, mode);
1911 err = PTR_ERR(inode);
1915 btrfs_set_trans_block_group(trans, inode);
1916 err = btrfs_add_nondir(trans, dentry, inode, 0);
1920 inode->i_op = &btrfs_special_inode_operations;
1921 init_special_inode(inode, inode->i_mode, rdev);
1922 btrfs_update_inode(trans, root, inode);
1924 dir->i_sb->s_dirt = 1;
1925 btrfs_update_inode_block_group(trans, inode);
1926 btrfs_update_inode_block_group(trans, dir);
1928 nr = trans->blocks_used;
1929 btrfs_end_transaction(trans, root);
1931 mutex_unlock(&root->fs_info->fs_mutex);
1934 inode_dec_link_count(inode);
1937 btrfs_btree_balance_dirty(root, nr);
1938 btrfs_throttle(root);
1942 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1943 int mode, struct nameidata *nd)
1945 struct btrfs_trans_handle *trans;
1946 struct btrfs_root *root = BTRFS_I(dir)->root;
1947 struct inode *inode = NULL;
1950 unsigned long nr = 0;
1953 mutex_lock(&root->fs_info->fs_mutex);
1954 err = btrfs_check_free_space(root, 1, 0);
1957 trans = btrfs_start_transaction(root, 1);
1958 btrfs_set_trans_block_group(trans, dir);
1960 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1966 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1968 dentry->d_parent->d_inode->i_ino,
1969 objectid, BTRFS_I(dir)->block_group, mode);
1970 err = PTR_ERR(inode);
1974 btrfs_set_trans_block_group(trans, inode);
1975 err = btrfs_add_nondir(trans, dentry, inode, 0);
1979 inode->i_mapping->a_ops = &btrfs_aops;
1980 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1981 inode->i_fop = &btrfs_file_operations;
1982 inode->i_op = &btrfs_file_inode_operations;
1983 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1984 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1985 inode->i_mapping, GFP_NOFS);
1986 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1987 inode->i_mapping, GFP_NOFS);
1988 BTRFS_I(inode)->delalloc_bytes = 0;
1989 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1990 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1992 dir->i_sb->s_dirt = 1;
1993 btrfs_update_inode_block_group(trans, inode);
1994 btrfs_update_inode_block_group(trans, dir);
1996 nr = trans->blocks_used;
1997 btrfs_end_transaction(trans, root);
1999 mutex_unlock(&root->fs_info->fs_mutex);
2002 inode_dec_link_count(inode);
2005 btrfs_btree_balance_dirty(root, nr);
2006 btrfs_throttle(root);
2010 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2011 struct dentry *dentry)
2013 struct btrfs_trans_handle *trans;
2014 struct btrfs_root *root = BTRFS_I(dir)->root;
2015 struct inode *inode = old_dentry->d_inode;
2016 unsigned long nr = 0;
2020 if (inode->i_nlink == 0)
2023 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2028 mutex_lock(&root->fs_info->fs_mutex);
2029 err = btrfs_check_free_space(root, 1, 0);
2032 trans = btrfs_start_transaction(root, 1);
2034 btrfs_set_trans_block_group(trans, dir);
2035 atomic_inc(&inode->i_count);
2036 err = btrfs_add_nondir(trans, dentry, inode, 1);
2041 dir->i_sb->s_dirt = 1;
2042 btrfs_update_inode_block_group(trans, dir);
2043 err = btrfs_update_inode(trans, root, inode);
2048 nr = trans->blocks_used;
2049 btrfs_end_transaction(trans, root);
2051 mutex_unlock(&root->fs_info->fs_mutex);
2054 inode_dec_link_count(inode);
2057 btrfs_btree_balance_dirty(root, nr);
2058 btrfs_throttle(root);
2062 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2064 struct inode *inode = NULL;
2065 struct btrfs_trans_handle *trans;
2066 struct btrfs_root *root = BTRFS_I(dir)->root;
2068 int drop_on_err = 0;
2070 unsigned long nr = 1;
2072 mutex_lock(&root->fs_info->fs_mutex);
2073 err = btrfs_check_free_space(root, 1, 0);
2077 trans = btrfs_start_transaction(root, 1);
2078 btrfs_set_trans_block_group(trans, dir);
2080 if (IS_ERR(trans)) {
2081 err = PTR_ERR(trans);
2085 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2091 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2093 dentry->d_parent->d_inode->i_ino, objectid,
2094 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2095 if (IS_ERR(inode)) {
2096 err = PTR_ERR(inode);
2101 inode->i_op = &btrfs_dir_inode_operations;
2102 inode->i_fop = &btrfs_dir_file_operations;
2103 btrfs_set_trans_block_group(trans, inode);
2106 err = btrfs_update_inode(trans, root, inode);
2110 err = btrfs_add_link(trans, dentry, inode, 0);
2114 d_instantiate(dentry, inode);
2116 dir->i_sb->s_dirt = 1;
2117 btrfs_update_inode_block_group(trans, inode);
2118 btrfs_update_inode_block_group(trans, dir);
2121 nr = trans->blocks_used;
2122 btrfs_end_transaction(trans, root);
2125 mutex_unlock(&root->fs_info->fs_mutex);
2128 btrfs_btree_balance_dirty(root, nr);
2129 btrfs_throttle(root);
2133 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2134 struct extent_map *existing,
2135 struct extent_map *em)
2140 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2142 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2145 if (!real_blocks && em->block_start != existing->block_start)
2148 new_end = max(existing->start + existing->len, em->start + em->len);
2150 if (existing->start >= em->start) {
2151 if (em->start + em->len < existing->start)
2154 start_diff = existing->start - em->start;
2155 if (real_blocks && em->block_start + start_diff !=
2156 existing->block_start)
2159 em->len = new_end - em->start;
2161 remove_extent_mapping(em_tree, existing);
2162 /* free for the tree */
2163 free_extent_map(existing);
2164 ret = add_extent_mapping(em_tree, em);
2166 } else if (em->start > existing->start) {
2168 if (existing->start + existing->len < em->start)
2171 start_diff = em->start - existing->start;
2172 if (real_blocks && existing->block_start + start_diff !=
2176 remove_extent_mapping(em_tree, existing);
2177 em->block_start = existing->block_start;
2178 em->start = existing->start;
2179 em->len = new_end - existing->start;
2180 free_extent_map(existing);
2182 ret = add_extent_mapping(em_tree, em);
2189 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2190 existing->start, existing->len, existing->block_start,
2191 em->start, em->len, em->block_start);
2195 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2196 size_t pg_offset, u64 start, u64 len,
2202 u64 extent_start = 0;
2204 u64 objectid = inode->i_ino;
2206 struct btrfs_path *path;
2207 struct btrfs_root *root = BTRFS_I(inode)->root;
2208 struct btrfs_file_extent_item *item;
2209 struct extent_buffer *leaf;
2210 struct btrfs_key found_key;
2211 struct extent_map *em = NULL;
2212 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2213 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2214 struct btrfs_trans_handle *trans = NULL;
2216 path = btrfs_alloc_path();
2218 mutex_lock(&root->fs_info->fs_mutex);
2221 spin_lock(&em_tree->lock);
2222 em = lookup_extent_mapping(em_tree, start, len);
2224 em->bdev = root->fs_info->fs_devices->latest_bdev;
2225 spin_unlock(&em_tree->lock);
2228 if (em->start > start || em->start + em->len <= start)
2229 free_extent_map(em);
2230 else if (em->block_start == EXTENT_MAP_INLINE && page)
2231 free_extent_map(em);
2235 em = alloc_extent_map(GFP_NOFS);
2241 em->start = EXTENT_MAP_HOLE;
2243 em->bdev = root->fs_info->fs_devices->latest_bdev;
2244 ret = btrfs_lookup_file_extent(trans, root, path,
2245 objectid, start, trans != NULL);
2252 if (path->slots[0] == 0)
2257 leaf = path->nodes[0];
2258 item = btrfs_item_ptr(leaf, path->slots[0],
2259 struct btrfs_file_extent_item);
2260 /* are we inside the extent that was found? */
2261 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2262 found_type = btrfs_key_type(&found_key);
2263 if (found_key.objectid != objectid ||
2264 found_type != BTRFS_EXTENT_DATA_KEY) {
2268 found_type = btrfs_file_extent_type(leaf, item);
2269 extent_start = found_key.offset;
2270 if (found_type == BTRFS_FILE_EXTENT_REG) {
2271 extent_end = extent_start +
2272 btrfs_file_extent_num_bytes(leaf, item);
2274 if (start < extent_start || start >= extent_end) {
2276 if (start < extent_start) {
2277 if (start + len <= extent_start)
2279 em->len = extent_end - extent_start;
2285 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2287 em->start = extent_start;
2288 em->len = extent_end - extent_start;
2289 em->block_start = EXTENT_MAP_HOLE;
2292 bytenr += btrfs_file_extent_offset(leaf, item);
2293 em->block_start = bytenr;
2294 em->start = extent_start;
2295 em->len = extent_end - extent_start;
2297 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2302 size_t extent_offset;
2305 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2307 extent_end = (extent_start + size + root->sectorsize - 1) &
2308 ~((u64)root->sectorsize - 1);
2309 if (start < extent_start || start >= extent_end) {
2311 if (start < extent_start) {
2312 if (start + len <= extent_start)
2314 em->len = extent_end - extent_start;
2320 em->block_start = EXTENT_MAP_INLINE;
2323 em->start = extent_start;
2328 page_start = page_offset(page) + pg_offset;
2329 extent_offset = page_start - extent_start;
2330 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2331 size - extent_offset);
2332 em->start = extent_start + extent_offset;
2333 em->len = (copy_size + root->sectorsize - 1) &
2334 ~((u64)root->sectorsize - 1);
2336 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2337 if (create == 0 && !PageUptodate(page)) {
2338 read_extent_buffer(leaf, map + pg_offset, ptr,
2340 flush_dcache_page(page);
2341 } else if (create && PageUptodate(page)) {
2344 free_extent_map(em);
2346 btrfs_release_path(root, path);
2347 trans = btrfs_start_transaction(root, 1);
2350 write_extent_buffer(leaf, map + pg_offset, ptr,
2352 btrfs_mark_buffer_dirty(leaf);
2355 set_extent_uptodate(io_tree, em->start,
2356 extent_map_end(em) - 1, GFP_NOFS);
2359 printk("unkknown found_type %d\n", found_type);
2366 em->block_start = EXTENT_MAP_HOLE;
2368 btrfs_release_path(root, path);
2369 if (em->start > start || extent_map_end(em) <= start) {
2370 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2376 spin_lock(&em_tree->lock);
2377 ret = add_extent_mapping(em_tree, em);
2378 /* it is possible that someone inserted the extent into the tree
2379 * while we had the lock dropped. It is also possible that
2380 * an overlapping map exists in the tree
2382 if (ret == -EEXIST) {
2383 struct extent_map *existing;
2384 existing = lookup_extent_mapping(em_tree, start, len);
2385 if (existing && (existing->start > start ||
2386 existing->start + existing->len <= start)) {
2387 free_extent_map(existing);
2391 existing = lookup_extent_mapping(em_tree, em->start,
2394 err = merge_extent_mapping(em_tree, existing,
2396 free_extent_map(existing);
2398 free_extent_map(em);
2403 printk("failing to insert %Lu %Lu\n",
2405 free_extent_map(em);
2409 free_extent_map(em);
2413 spin_unlock(&em_tree->lock);
2415 btrfs_free_path(path);
2417 ret = btrfs_end_transaction(trans, root);
2421 mutex_unlock(&root->fs_info->fs_mutex);
2423 free_extent_map(em);
2425 return ERR_PTR(err);
2430 #if 0 /* waiting for O_DIRECT reads */
2431 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2432 struct buffer_head *bh_result, int create)
2434 struct extent_map *em;
2435 u64 start = (u64)iblock << inode->i_blkbits;
2436 struct btrfs_multi_bio *multi = NULL;
2437 struct btrfs_root *root = BTRFS_I(inode)->root;
2443 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2445 if (!em || IS_ERR(em))
2448 if (em->start > start || em->start + em->len <= start) {
2452 if (em->block_start == EXTENT_MAP_INLINE) {
2457 len = em->start + em->len - start;
2458 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2460 if (em->block_start == EXTENT_MAP_HOLE ||
2461 em->block_start == EXTENT_MAP_DELALLOC) {
2462 bh_result->b_size = len;
2466 logical = start - em->start;
2467 logical = em->block_start + logical;
2470 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2471 logical, &map_length, &multi, 0);
2473 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2474 bh_result->b_size = min(map_length, len);
2476 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2477 set_buffer_mapped(bh_result);
2480 free_extent_map(em);
2485 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2486 const struct iovec *iov, loff_t offset,
2487 unsigned long nr_segs)
2491 struct file *file = iocb->ki_filp;
2492 struct inode *inode = file->f_mapping->host;
2497 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2498 offset, nr_segs, btrfs_get_block, NULL);
2502 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2504 return extent_bmap(mapping, iblock, btrfs_get_extent);
2507 int btrfs_readpage(struct file *file, struct page *page)
2509 struct extent_io_tree *tree;
2510 tree = &BTRFS_I(page->mapping->host)->io_tree;
2511 return extent_read_full_page(tree, page, btrfs_get_extent);
2514 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2516 struct extent_io_tree *tree;
2519 if (current->flags & PF_MEMALLOC) {
2520 redirty_page_for_writepage(wbc, page);
2524 tree = &BTRFS_I(page->mapping->host)->io_tree;
2525 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2528 static int btrfs_writepages(struct address_space *mapping,
2529 struct writeback_control *wbc)
2531 struct extent_io_tree *tree;
2532 tree = &BTRFS_I(mapping->host)->io_tree;
2533 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2537 btrfs_readpages(struct file *file, struct address_space *mapping,
2538 struct list_head *pages, unsigned nr_pages)
2540 struct extent_io_tree *tree;
2541 tree = &BTRFS_I(mapping->host)->io_tree;
2542 return extent_readpages(tree, mapping, pages, nr_pages,
2546 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2548 struct extent_io_tree *tree;
2549 struct extent_map_tree *map;
2552 tree = &BTRFS_I(page->mapping->host)->io_tree;
2553 map = &BTRFS_I(page->mapping->host)->extent_tree;
2554 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2556 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2557 ClearPagePrivate(page);
2558 set_page_private(page, 0);
2559 page_cache_release(page);
2564 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2566 struct extent_io_tree *tree;
2568 tree = &BTRFS_I(page->mapping->host)->io_tree;
2569 extent_invalidatepage(tree, page, offset);
2570 btrfs_releasepage(page, GFP_NOFS);
2571 if (PagePrivate(page)) {
2572 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2573 ClearPagePrivate(page);
2574 set_page_private(page, 0);
2575 page_cache_release(page);
2580 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2581 * called from a page fault handler when a page is first dirtied. Hence we must
2582 * be careful to check for EOF conditions here. We set the page up correctly
2583 * for a written page which means we get ENOSPC checking when writing into
2584 * holes and correct delalloc and unwritten extent mapping on filesystems that
2585 * support these features.
2587 * We are not allowed to take the i_mutex here so we have to play games to
2588 * protect against truncate races as the page could now be beyond EOF. Because
2589 * vmtruncate() writes the inode size before removing pages, once we have the
2590 * page lock we can determine safely if the page is beyond EOF. If it is not
2591 * beyond EOF, then the page is guaranteed safe against truncation until we
2594 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2596 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2597 struct btrfs_root *root = BTRFS_I(inode)->root;
2603 mutex_lock(&root->fs_info->fs_mutex);
2604 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2605 mutex_unlock(&root->fs_info->fs_mutex);
2612 wait_on_page_writeback(page);
2613 size = i_size_read(inode);
2614 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2616 if ((page->mapping != inode->i_mapping) ||
2617 (page_start > size)) {
2618 /* page got truncated out from underneath us */
2622 /* page is wholly or partially inside EOF */
2623 if (page_start + PAGE_CACHE_SIZE > size)
2624 end = size & ~PAGE_CACHE_MASK;
2626 end = PAGE_CACHE_SIZE;
2628 ret = btrfs_cow_one_page(inode, page, end);
2636 static void btrfs_truncate(struct inode *inode)
2638 struct btrfs_root *root = BTRFS_I(inode)->root;
2640 struct btrfs_trans_handle *trans;
2643 if (!S_ISREG(inode->i_mode))
2645 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2648 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2650 mutex_lock(&root->fs_info->fs_mutex);
2651 trans = btrfs_start_transaction(root, 1);
2652 btrfs_set_trans_block_group(trans, inode);
2654 /* FIXME, add redo link to tree so we don't leak on crash */
2655 ret = btrfs_truncate_in_trans(trans, root, inode,
2656 BTRFS_EXTENT_DATA_KEY);
2657 btrfs_update_inode(trans, root, inode);
2658 nr = trans->blocks_used;
2660 ret = btrfs_end_transaction(trans, root);
2662 mutex_unlock(&root->fs_info->fs_mutex);
2663 btrfs_btree_balance_dirty(root, nr);
2664 btrfs_throttle(root);
2667 static int noinline create_subvol(struct btrfs_root *root, char *name,
2670 struct btrfs_trans_handle *trans;
2671 struct btrfs_key key;
2672 struct btrfs_root_item root_item;
2673 struct btrfs_inode_item *inode_item;
2674 struct extent_buffer *leaf;
2675 struct btrfs_root *new_root = root;
2676 struct inode *inode;
2681 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2682 unsigned long nr = 1;
2684 mutex_lock(&root->fs_info->fs_mutex);
2685 ret = btrfs_check_free_space(root, 1, 0);
2689 trans = btrfs_start_transaction(root, 1);
2692 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2697 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2698 objectid, trans->transid, 0, 0,
2701 return PTR_ERR(leaf);
2703 btrfs_set_header_nritems(leaf, 0);
2704 btrfs_set_header_level(leaf, 0);
2705 btrfs_set_header_bytenr(leaf, leaf->start);
2706 btrfs_set_header_generation(leaf, trans->transid);
2707 btrfs_set_header_owner(leaf, objectid);
2709 write_extent_buffer(leaf, root->fs_info->fsid,
2710 (unsigned long)btrfs_header_fsid(leaf),
2712 btrfs_mark_buffer_dirty(leaf);
2714 inode_item = &root_item.inode;
2715 memset(inode_item, 0, sizeof(*inode_item));
2716 inode_item->generation = cpu_to_le64(1);
2717 inode_item->size = cpu_to_le64(3);
2718 inode_item->nlink = cpu_to_le32(1);
2719 inode_item->nblocks = cpu_to_le64(1);
2720 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2722 btrfs_set_root_bytenr(&root_item, leaf->start);
2723 btrfs_set_root_level(&root_item, 0);
2724 btrfs_set_root_refs(&root_item, 1);
2725 btrfs_set_root_used(&root_item, 0);
2727 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2728 root_item.drop_level = 0;
2730 free_extent_buffer(leaf);
2733 btrfs_set_root_dirid(&root_item, new_dirid);
2735 key.objectid = objectid;
2737 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2738 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2744 * insert the directory item
2746 key.offset = (u64)-1;
2747 dir = root->fs_info->sb->s_root->d_inode;
2748 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2749 name, namelen, dir->i_ino, &key,
2754 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2755 name, namelen, objectid,
2756 root->fs_info->sb->s_root->d_inode->i_ino);
2760 ret = btrfs_commit_transaction(trans, root);
2764 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2767 trans = btrfs_start_transaction(new_root, 1);
2770 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2772 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2775 inode->i_op = &btrfs_dir_inode_operations;
2776 inode->i_fop = &btrfs_dir_file_operations;
2777 new_root->inode = inode;
2779 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2783 ret = btrfs_update_inode(trans, new_root, inode);
2787 nr = trans->blocks_used;
2788 err = btrfs_commit_transaction(trans, new_root);
2792 mutex_unlock(&root->fs_info->fs_mutex);
2793 btrfs_btree_balance_dirty(root, nr);
2794 btrfs_throttle(root);
2798 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2800 struct btrfs_pending_snapshot *pending_snapshot;
2801 struct btrfs_trans_handle *trans;
2804 unsigned long nr = 0;
2806 if (!root->ref_cows)
2809 mutex_lock(&root->fs_info->fs_mutex);
2810 ret = btrfs_check_free_space(root, 1, 0);
2814 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2815 if (!pending_snapshot) {
2819 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2820 if (!pending_snapshot->name) {
2822 kfree(pending_snapshot);
2825 memcpy(pending_snapshot->name, name, namelen);
2826 pending_snapshot->name[namelen] = '\0';
2827 trans = btrfs_start_transaction(root, 1);
2829 pending_snapshot->root = root;
2830 list_add(&pending_snapshot->list,
2831 &trans->transaction->pending_snapshots);
2832 ret = btrfs_update_inode(trans, root, root->inode);
2833 err = btrfs_commit_transaction(trans, root);
2836 mutex_unlock(&root->fs_info->fs_mutex);
2837 btrfs_btree_balance_dirty(root, nr);
2838 btrfs_throttle(root);
2842 unsigned long btrfs_force_ra(struct address_space *mapping,
2843 struct file_ra_state *ra, struct file *file,
2844 pgoff_t offset, pgoff_t last_index)
2846 pgoff_t req_size = last_index - offset + 1;
2848 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2849 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2852 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2853 return offset + req_size;
2857 int btrfs_defrag_file(struct file *file) {
2858 struct inode *inode = fdentry(file)->d_inode;
2859 struct btrfs_root *root = BTRFS_I(inode)->root;
2860 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2862 unsigned long last_index;
2863 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2864 unsigned long total_read = 0;
2870 mutex_lock(&root->fs_info->fs_mutex);
2871 ret = btrfs_check_free_space(root, inode->i_size, 0);
2872 mutex_unlock(&root->fs_info->fs_mutex);
2876 mutex_lock(&inode->i_mutex);
2877 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2878 for (i = 0; i <= last_index; i++) {
2879 if (total_read % ra_pages == 0) {
2880 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2881 min(last_index, i + ra_pages - 1));
2884 page = grab_cache_page(inode->i_mapping, i);
2887 if (!PageUptodate(page)) {
2888 btrfs_readpage(NULL, page);
2890 if (!PageUptodate(page)) {
2892 page_cache_release(page);
2897 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2898 ClearPageDirty(page);
2900 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2902 wait_on_page_writeback(page);
2903 set_page_extent_mapped(page);
2905 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2906 page_end = page_start + PAGE_CACHE_SIZE - 1;
2908 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2909 set_extent_delalloc(io_tree, page_start,
2910 page_end, GFP_NOFS);
2912 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2913 set_page_dirty(page);
2915 page_cache_release(page);
2916 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2920 mutex_unlock(&inode->i_mutex);
2924 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2929 struct btrfs_ioctl_vol_args *vol_args;
2930 struct btrfs_trans_handle *trans;
2931 struct btrfs_device *device = NULL;
2933 char *devstr = NULL;
2938 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2943 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2947 namelen = strlen(vol_args->name);
2948 if (namelen > BTRFS_VOL_NAME_MAX) {
2953 mutex_lock(&root->fs_info->fs_mutex);
2954 sizestr = vol_args->name;
2955 devstr = strchr(sizestr, ':');
2958 sizestr = devstr + 1;
2960 devstr = vol_args->name;
2961 devid = simple_strtoull(devstr, &end, 10);
2962 printk("resizing devid %Lu\n", devid);
2964 device = btrfs_find_device(root, devid, NULL);
2966 printk("resizer unable to find device %Lu\n", devid);
2970 if (!strcmp(sizestr, "max"))
2971 new_size = device->bdev->bd_inode->i_size;
2973 if (sizestr[0] == '-') {
2976 } else if (sizestr[0] == '+') {
2980 new_size = btrfs_parse_size(sizestr);
2981 if (new_size == 0) {
2987 old_size = device->total_bytes;
2990 if (new_size > old_size) {
2994 new_size = old_size - new_size;
2995 } else if (mod > 0) {
2996 new_size = old_size + new_size;
2999 if (new_size < 256 * 1024 * 1024) {
3003 if (new_size > device->bdev->bd_inode->i_size) {
3008 do_div(new_size, root->sectorsize);
3009 new_size *= root->sectorsize;
3011 printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
3013 if (new_size > old_size) {
3014 trans = btrfs_start_transaction(root, 1);
3015 ret = btrfs_grow_device(trans, device, new_size);
3016 btrfs_commit_transaction(trans, root);
3018 ret = btrfs_shrink_device(device, new_size);
3022 mutex_unlock(&root->fs_info->fs_mutex);
3028 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3031 struct btrfs_ioctl_vol_args *vol_args;
3032 struct btrfs_dir_item *di;
3033 struct btrfs_path *path;
3038 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3043 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3048 namelen = strlen(vol_args->name);
3049 if (namelen > BTRFS_VOL_NAME_MAX) {
3053 if (strchr(vol_args->name, '/')) {
3058 path = btrfs_alloc_path();
3064 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3065 mutex_lock(&root->fs_info->fs_mutex);
3066 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3068 vol_args->name, namelen, 0);
3069 mutex_unlock(&root->fs_info->fs_mutex);
3070 btrfs_free_path(path);
3072 if (di && !IS_ERR(di)) {
3082 if (root == root->fs_info->tree_root)
3083 ret = create_subvol(root, vol_args->name, namelen);
3085 ret = create_snapshot(root, vol_args->name, namelen);
3091 static int btrfs_ioctl_defrag(struct file *file)
3093 struct inode *inode = fdentry(file)->d_inode;
3094 struct btrfs_root *root = BTRFS_I(inode)->root;
3096 switch (inode->i_mode & S_IFMT) {
3098 mutex_lock(&root->fs_info->fs_mutex);
3099 btrfs_defrag_root(root, 0);
3100 btrfs_defrag_root(root->fs_info->extent_root, 0);
3101 mutex_unlock(&root->fs_info->fs_mutex);
3104 btrfs_defrag_file(file);
3111 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3113 struct btrfs_ioctl_vol_args *vol_args;
3116 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3121 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3125 ret = btrfs_init_new_device(root, vol_args->name);
3132 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
3134 struct btrfs_ioctl_vol_args *vol_args;
3137 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3142 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3146 ret = btrfs_rm_device(root, vol_args->name);
3153 int dup_item_to_inode(struct btrfs_trans_handle *trans,
3154 struct btrfs_root *root,
3155 struct btrfs_path *path,
3156 struct extent_buffer *leaf,
3158 struct btrfs_key *key,
3162 int len = btrfs_item_size_nr(leaf, slot);
3163 struct btrfs_key ckey = *key;
3166 dup = kmalloc(len, GFP_NOFS);
3170 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
3171 btrfs_release_path(root, path);
3173 ckey.objectid = destino;
3174 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
3179 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
3181 struct inode *inode = fdentry(file)->d_inode;
3182 struct btrfs_root *root = BTRFS_I(inode)->root;
3183 struct file *src_file;
3185 struct btrfs_trans_handle *trans;
3188 struct btrfs_path *path;
3189 struct btrfs_key key;
3190 struct extent_buffer *leaf;
3194 src_file = fget(src_fd);
3197 src = src_file->f_dentry->d_inode;
3200 if (src->i_sb != inode->i_sb)
3204 mutex_lock(&inode->i_mutex);
3205 mutex_lock(&src->i_mutex);
3207 mutex_lock(&src->i_mutex);
3208 mutex_lock(&inode->i_mutex);
3215 /* do any pending delalloc/csum calc on src, one way or
3216 another, and lock file content */
3218 filemap_write_and_wait(src->i_mapping);
3219 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3220 if (BTRFS_I(src)->delalloc_bytes == 0)
3222 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3225 mutex_lock(&root->fs_info->fs_mutex);
3226 trans = btrfs_start_transaction(root, 0);
3227 path = btrfs_alloc_path();
3233 key.type = BTRFS_EXTENT_DATA_KEY;
3234 key.objectid = src->i_ino;
3240 * note the key will change type as we walk through the
3243 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3247 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3248 ret = btrfs_next_leaf(root, path);
3254 leaf = path->nodes[0];
3255 slot = path->slots[0];
3256 btrfs_item_key_to_cpu(leaf, &key, slot);
3257 nritems = btrfs_header_nritems(leaf);
3259 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
3260 key.objectid != src->i_ino)
3263 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3264 struct btrfs_file_extent_item *extent;
3267 extent = btrfs_item_ptr(leaf, slot,
3268 struct btrfs_file_extent_item);
3269 found_type = btrfs_file_extent_type(leaf, extent);
3270 if (found_type == BTRFS_FILE_EXTENT_REG) {
3271 u64 len = btrfs_file_extent_num_bytes(leaf,
3273 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
3275 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
3277 u64 off = btrfs_file_extent_offset(leaf,
3279 btrfs_insert_file_extent(trans, root,
3282 /* ds == 0 means there's a hole */
3284 btrfs_inc_extent_ref(trans, root,
3286 root->root_key.objectid,
3290 pos = key.offset + len;
3291 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3292 ret = dup_item_to_inode(trans, root, path,
3297 pos = key.offset + btrfs_item_size_nr(leaf,
3300 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
3301 ret = dup_item_to_inode(trans, root, path, leaf,
3302 slot, &key, inode->i_ino);
3308 btrfs_release_path(root, path);
3313 btrfs_free_path(path);
3315 inode->i_blocks = src->i_blocks;
3316 i_size_write(inode, src->i_size);
3317 btrfs_update_inode(trans, root, inode);
3319 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3321 btrfs_end_transaction(trans, root);
3322 mutex_unlock(&root->fs_info->fs_mutex);
3325 mutex_unlock(&src->i_mutex);
3326 mutex_unlock(&inode->i_mutex);
3332 long btrfs_ioctl(struct file *file, unsigned int
3333 cmd, unsigned long arg)
3335 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3338 case BTRFS_IOC_SNAP_CREATE:
3339 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3340 case BTRFS_IOC_DEFRAG:
3341 return btrfs_ioctl_defrag(file);
3342 case BTRFS_IOC_RESIZE:
3343 return btrfs_ioctl_resize(root, (void __user *)arg);
3344 case BTRFS_IOC_ADD_DEV:
3345 return btrfs_ioctl_add_dev(root, (void __user *)arg);
3346 case BTRFS_IOC_RM_DEV:
3347 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
3348 case BTRFS_IOC_BALANCE:
3349 return btrfs_balance(root->fs_info->dev_root);
3350 case BTRFS_IOC_CLONE:
3351 return btrfs_ioctl_clone(file, arg);
3358 * Called inside transaction, so use GFP_NOFS
3360 struct inode *btrfs_alloc_inode(struct super_block *sb)
3362 struct btrfs_inode *ei;
3364 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3368 ei->ordered_trans = 0;
3369 return &ei->vfs_inode;
3372 void btrfs_destroy_inode(struct inode *inode)
3374 WARN_ON(!list_empty(&inode->i_dentry));
3375 WARN_ON(inode->i_data.nrpages);
3377 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3378 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3381 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3382 static void init_once(struct kmem_cache * cachep, void *foo)
3384 static void init_once(void * foo, struct kmem_cache * cachep,
3385 unsigned long flags)
3388 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3390 inode_init_once(&ei->vfs_inode);
3393 void btrfs_destroy_cachep(void)
3395 if (btrfs_inode_cachep)
3396 kmem_cache_destroy(btrfs_inode_cachep);
3397 if (btrfs_trans_handle_cachep)
3398 kmem_cache_destroy(btrfs_trans_handle_cachep);
3399 if (btrfs_transaction_cachep)
3400 kmem_cache_destroy(btrfs_transaction_cachep);
3401 if (btrfs_bit_radix_cachep)
3402 kmem_cache_destroy(btrfs_bit_radix_cachep);
3403 if (btrfs_path_cachep)
3404 kmem_cache_destroy(btrfs_path_cachep);
3407 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3408 unsigned long extra_flags,
3409 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3410 void (*ctor)(struct kmem_cache *, void *)
3412 void (*ctor)(void *, struct kmem_cache *,
3417 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3418 SLAB_MEM_SPREAD | extra_flags), ctor
3419 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3425 int btrfs_init_cachep(void)
3427 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3428 sizeof(struct btrfs_inode),
3430 if (!btrfs_inode_cachep)
3432 btrfs_trans_handle_cachep =
3433 btrfs_cache_create("btrfs_trans_handle_cache",
3434 sizeof(struct btrfs_trans_handle),
3436 if (!btrfs_trans_handle_cachep)
3438 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3439 sizeof(struct btrfs_transaction),
3441 if (!btrfs_transaction_cachep)
3443 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3444 sizeof(struct btrfs_path),
3446 if (!btrfs_path_cachep)
3448 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3449 SLAB_DESTROY_BY_RCU, NULL);
3450 if (!btrfs_bit_radix_cachep)
3454 btrfs_destroy_cachep();
3458 static int btrfs_getattr(struct vfsmount *mnt,
3459 struct dentry *dentry, struct kstat *stat)
3461 struct inode *inode = dentry->d_inode;
3462 generic_fillattr(inode, stat);
3463 stat->blksize = PAGE_CACHE_SIZE;
3464 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3468 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3469 struct inode * new_dir,struct dentry *new_dentry)
3471 struct btrfs_trans_handle *trans;
3472 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3473 struct inode *new_inode = new_dentry->d_inode;
3474 struct inode *old_inode = old_dentry->d_inode;
3475 struct timespec ctime = CURRENT_TIME;
3476 struct btrfs_path *path;
3479 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3480 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3484 mutex_lock(&root->fs_info->fs_mutex);
3485 ret = btrfs_check_free_space(root, 1, 0);
3489 trans = btrfs_start_transaction(root, 1);
3491 btrfs_set_trans_block_group(trans, new_dir);
3492 path = btrfs_alloc_path();
3498 old_dentry->d_inode->i_nlink++;
3499 old_dir->i_ctime = old_dir->i_mtime = ctime;
3500 new_dir->i_ctime = new_dir->i_mtime = ctime;
3501 old_inode->i_ctime = ctime;
3503 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3508 new_inode->i_ctime = CURRENT_TIME;
3509 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3513 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3518 btrfs_free_path(path);
3519 btrfs_end_transaction(trans, root);
3521 mutex_unlock(&root->fs_info->fs_mutex);
3525 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3526 const char *symname)
3528 struct btrfs_trans_handle *trans;
3529 struct btrfs_root *root = BTRFS_I(dir)->root;
3530 struct btrfs_path *path;
3531 struct btrfs_key key;
3532 struct inode *inode = NULL;
3539 struct btrfs_file_extent_item *ei;
3540 struct extent_buffer *leaf;
3541 unsigned long nr = 0;
3543 name_len = strlen(symname) + 1;
3544 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3545 return -ENAMETOOLONG;
3547 mutex_lock(&root->fs_info->fs_mutex);
3548 err = btrfs_check_free_space(root, 1, 0);
3552 trans = btrfs_start_transaction(root, 1);
3553 btrfs_set_trans_block_group(trans, dir);
3555 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3561 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3563 dentry->d_parent->d_inode->i_ino, objectid,
3564 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3565 err = PTR_ERR(inode);
3569 btrfs_set_trans_block_group(trans, inode);
3570 err = btrfs_add_nondir(trans, dentry, inode, 0);
3574 inode->i_mapping->a_ops = &btrfs_aops;
3575 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3576 inode->i_fop = &btrfs_file_operations;
3577 inode->i_op = &btrfs_file_inode_operations;
3578 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3579 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3580 inode->i_mapping, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3582 inode->i_mapping, GFP_NOFS);
3583 BTRFS_I(inode)->delalloc_bytes = 0;
3584 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
3585 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3587 dir->i_sb->s_dirt = 1;
3588 btrfs_update_inode_block_group(trans, inode);
3589 btrfs_update_inode_block_group(trans, dir);
3593 path = btrfs_alloc_path();
3595 key.objectid = inode->i_ino;
3597 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3598 datasize = btrfs_file_extent_calc_inline_size(name_len);
3599 err = btrfs_insert_empty_item(trans, root, path, &key,
3605 leaf = path->nodes[0];
3606 ei = btrfs_item_ptr(leaf, path->slots[0],
3607 struct btrfs_file_extent_item);
3608 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3609 btrfs_set_file_extent_type(leaf, ei,
3610 BTRFS_FILE_EXTENT_INLINE);
3611 ptr = btrfs_file_extent_inline_start(ei);
3612 write_extent_buffer(leaf, symname, ptr, name_len);
3613 btrfs_mark_buffer_dirty(leaf);
3614 btrfs_free_path(path);
3616 inode->i_op = &btrfs_symlink_inode_operations;
3617 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3618 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3619 inode->i_size = name_len - 1;
3620 err = btrfs_update_inode(trans, root, inode);
3625 nr = trans->blocks_used;
3626 btrfs_end_transaction(trans, root);
3628 mutex_unlock(&root->fs_info->fs_mutex);
3630 inode_dec_link_count(inode);
3633 btrfs_btree_balance_dirty(root, nr);
3634 btrfs_throttle(root);
3638 static int btrfs_permission(struct inode *inode, int mask,
3639 struct nameidata *nd)
3641 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3643 return generic_permission(inode, mask, NULL);
3646 static struct inode_operations btrfs_dir_inode_operations = {
3647 .lookup = btrfs_lookup,
3648 .create = btrfs_create,
3649 .unlink = btrfs_unlink,
3651 .mkdir = btrfs_mkdir,
3652 .rmdir = btrfs_rmdir,
3653 .rename = btrfs_rename,
3654 .symlink = btrfs_symlink,
3655 .setattr = btrfs_setattr,
3656 .mknod = btrfs_mknod,
3657 .setxattr = generic_setxattr,
3658 .getxattr = generic_getxattr,
3659 .listxattr = btrfs_listxattr,
3660 .removexattr = generic_removexattr,
3661 .permission = btrfs_permission,
3663 static struct inode_operations btrfs_dir_ro_inode_operations = {
3664 .lookup = btrfs_lookup,
3665 .permission = btrfs_permission,
3667 static struct file_operations btrfs_dir_file_operations = {
3668 .llseek = generic_file_llseek,
3669 .read = generic_read_dir,
3670 .readdir = btrfs_readdir,
3671 .unlocked_ioctl = btrfs_ioctl,
3672 #ifdef CONFIG_COMPAT
3673 .compat_ioctl = btrfs_ioctl,
3677 static struct extent_io_ops btrfs_extent_io_ops = {
3678 .fill_delalloc = run_delalloc_range,
3679 .submit_bio_hook = btrfs_submit_bio_hook,
3680 .merge_bio_hook = btrfs_merge_bio_hook,
3681 .readpage_io_hook = btrfs_readpage_io_hook,
3682 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3683 .readpage_io_failed_hook = btrfs_io_failed_hook,
3684 .set_bit_hook = btrfs_set_bit_hook,
3685 .clear_bit_hook = btrfs_clear_bit_hook,
3688 static struct address_space_operations btrfs_aops = {
3689 .readpage = btrfs_readpage,
3690 .writepage = btrfs_writepage,
3691 .writepages = btrfs_writepages,
3692 .readpages = btrfs_readpages,
3693 .sync_page = block_sync_page,
3695 .direct_IO = btrfs_direct_IO,
3696 .invalidatepage = btrfs_invalidatepage,
3697 .releasepage = btrfs_releasepage,
3698 .set_page_dirty = __set_page_dirty_nobuffers,
3701 static struct address_space_operations btrfs_symlink_aops = {
3702 .readpage = btrfs_readpage,
3703 .writepage = btrfs_writepage,
3704 .invalidatepage = btrfs_invalidatepage,
3705 .releasepage = btrfs_releasepage,
3708 static struct inode_operations btrfs_file_inode_operations = {
3709 .truncate = btrfs_truncate,
3710 .getattr = btrfs_getattr,
3711 .setattr = btrfs_setattr,
3712 .setxattr = generic_setxattr,
3713 .getxattr = generic_getxattr,
3714 .listxattr = btrfs_listxattr,
3715 .removexattr = generic_removexattr,
3716 .permission = btrfs_permission,
3718 static struct inode_operations btrfs_special_inode_operations = {
3719 .getattr = btrfs_getattr,
3720 .setattr = btrfs_setattr,
3721 .permission = btrfs_permission,
3723 static struct inode_operations btrfs_symlink_inode_operations = {
3724 .readlink = generic_readlink,
3725 .follow_link = page_follow_link_light,
3726 .put_link = page_put_link,
3727 .permission = btrfs_permission,