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_readpage_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;
448 ret = get_state_private(failure_tree, start, &private);
450 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
453 failrec->start = start;
454 failrec->len = end - start + 1;
455 failrec->last_mirror = 0;
457 spin_lock(&em_tree->lock);
458 em = lookup_extent_mapping(em_tree, start, failrec->len);
459 if (em->start > start || em->start + em->len < start) {
463 spin_unlock(&em_tree->lock);
465 if (!em || IS_ERR(em)) {
469 logical = start - em->start;
470 logical = em->block_start + logical;
471 failrec->logical = logical;
473 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
474 EXTENT_DIRTY, GFP_NOFS);
475 set_state_private(failure_tree, start,
476 (u64)(unsigned long)failrec);
478 failrec = (struct io_failure_record *)(unsigned long)private;
480 num_copies = btrfs_num_copies(
481 &BTRFS_I(inode)->root->fs_info->mapping_tree,
482 failrec->logical, failrec->len);
483 failrec->last_mirror++;
485 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
486 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
489 if (state && state->start != failrec->start)
491 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
493 if (!state || failrec->last_mirror > num_copies) {
494 set_state_private(failure_tree, failrec->start, 0);
495 clear_extent_bits(failure_tree, failrec->start,
496 failrec->start + failrec->len - 1,
497 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
501 bio = bio_alloc(GFP_NOFS, 1);
502 bio->bi_private = state;
503 bio->bi_end_io = failed_bio->bi_end_io;
504 bio->bi_sector = failrec->logical >> 9;
505 bio->bi_bdev = failed_bio->bi_bdev;
507 bio_add_page(bio, page, failrec->len, start - page_offset(page));
508 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
512 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
513 struct extent_state *state)
515 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
516 struct inode *inode = page->mapping->host;
517 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
519 u64 private = ~(u32)0;
521 struct btrfs_root *root = BTRFS_I(inode)->root;
525 if (btrfs_test_opt(root, NODATASUM) ||
526 btrfs_test_flag(inode, NODATASUM))
528 if (state && state->start == start) {
529 private = state->private;
532 ret = get_state_private(io_tree, start, &private);
534 local_irq_save(flags);
535 kaddr = kmap_atomic(page, KM_IRQ0);
539 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
540 btrfs_csum_final(csum, (char *)&csum);
541 if (csum != private) {
544 kunmap_atomic(kaddr, KM_IRQ0);
545 local_irq_restore(flags);
547 /* if the io failure tree for this inode is non-empty,
548 * check to see if we've recovered from a failed IO
551 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
552 (u64)-1, 1, EXTENT_DIRTY)) {
554 struct io_failure_record *failure;
555 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
556 start, &private_failure);
558 failure = (struct io_failure_record *)(unsigned long)
560 set_state_private(&BTRFS_I(inode)->io_failure_tree,
562 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
564 failure->start + failure->len - 1,
565 EXTENT_DIRTY | EXTENT_LOCKED,
573 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
574 page->mapping->host->i_ino, (unsigned long long)start, csum,
576 memset(kaddr + offset, 1, end - start + 1);
577 flush_dcache_page(page);
578 kunmap_atomic(kaddr, KM_IRQ0);
579 local_irq_restore(flags);
585 void btrfs_read_locked_inode(struct inode *inode)
587 struct btrfs_path *path;
588 struct extent_buffer *leaf;
589 struct btrfs_inode_item *inode_item;
590 struct btrfs_timespec *tspec;
591 struct btrfs_root *root = BTRFS_I(inode)->root;
592 struct btrfs_key location;
593 u64 alloc_group_block;
597 path = btrfs_alloc_path();
599 mutex_lock(&root->fs_info->fs_mutex);
600 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
602 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
606 leaf = path->nodes[0];
607 inode_item = btrfs_item_ptr(leaf, path->slots[0],
608 struct btrfs_inode_item);
610 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
611 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
612 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
613 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
614 inode->i_size = btrfs_inode_size(leaf, inode_item);
616 tspec = btrfs_inode_atime(inode_item);
617 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
618 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
620 tspec = btrfs_inode_mtime(inode_item);
621 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
622 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
624 tspec = btrfs_inode_ctime(inode_item);
625 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
626 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
628 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
629 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
631 rdev = btrfs_inode_rdev(leaf, inode_item);
633 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
634 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
636 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
637 if (!BTRFS_I(inode)->block_group) {
638 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
640 BTRFS_BLOCK_GROUP_METADATA, 0);
642 btrfs_free_path(path);
645 mutex_unlock(&root->fs_info->fs_mutex);
647 switch (inode->i_mode & S_IFMT) {
649 inode->i_mapping->a_ops = &btrfs_aops;
650 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
651 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
652 inode->i_fop = &btrfs_file_operations;
653 inode->i_op = &btrfs_file_inode_operations;
656 inode->i_fop = &btrfs_dir_file_operations;
657 if (root == root->fs_info->tree_root)
658 inode->i_op = &btrfs_dir_ro_inode_operations;
660 inode->i_op = &btrfs_dir_inode_operations;
663 inode->i_op = &btrfs_symlink_inode_operations;
664 inode->i_mapping->a_ops = &btrfs_symlink_aops;
665 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
668 init_special_inode(inode, inode->i_mode, rdev);
674 btrfs_release_path(root, path);
675 btrfs_free_path(path);
676 mutex_unlock(&root->fs_info->fs_mutex);
677 make_bad_inode(inode);
680 static void fill_inode_item(struct extent_buffer *leaf,
681 struct btrfs_inode_item *item,
684 btrfs_set_inode_uid(leaf, item, inode->i_uid);
685 btrfs_set_inode_gid(leaf, item, inode->i_gid);
686 btrfs_set_inode_size(leaf, item, inode->i_size);
687 btrfs_set_inode_mode(leaf, item, inode->i_mode);
688 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
690 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
691 inode->i_atime.tv_sec);
692 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
693 inode->i_atime.tv_nsec);
695 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
696 inode->i_mtime.tv_sec);
697 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
698 inode->i_mtime.tv_nsec);
700 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
701 inode->i_ctime.tv_sec);
702 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
703 inode->i_ctime.tv_nsec);
705 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
706 btrfs_set_inode_generation(leaf, item, inode->i_generation);
707 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
708 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
709 btrfs_set_inode_block_group(leaf, item,
710 BTRFS_I(inode)->block_group->key.objectid);
713 int btrfs_update_inode(struct btrfs_trans_handle *trans,
714 struct btrfs_root *root,
717 struct btrfs_inode_item *inode_item;
718 struct btrfs_path *path;
719 struct extent_buffer *leaf;
722 path = btrfs_alloc_path();
724 ret = btrfs_lookup_inode(trans, root, path,
725 &BTRFS_I(inode)->location, 1);
732 leaf = path->nodes[0];
733 inode_item = btrfs_item_ptr(leaf, path->slots[0],
734 struct btrfs_inode_item);
736 fill_inode_item(leaf, inode_item, inode);
737 btrfs_mark_buffer_dirty(leaf);
738 btrfs_set_inode_last_trans(trans, inode);
741 btrfs_release_path(root, path);
742 btrfs_free_path(path);
747 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
750 struct dentry *dentry)
752 struct btrfs_path *path;
753 const char *name = dentry->d_name.name;
754 int name_len = dentry->d_name.len;
756 struct extent_buffer *leaf;
757 struct btrfs_dir_item *di;
758 struct btrfs_key key;
760 path = btrfs_alloc_path();
766 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
776 leaf = path->nodes[0];
777 btrfs_dir_item_key_to_cpu(leaf, di, &key);
778 ret = btrfs_delete_one_dir_name(trans, root, path, di);
781 btrfs_release_path(root, path);
783 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
784 key.objectid, name, name_len, -1);
793 ret = btrfs_delete_one_dir_name(trans, root, path, di);
795 dentry->d_inode->i_ctime = dir->i_ctime;
796 ret = btrfs_del_inode_ref(trans, root, name, name_len,
797 dentry->d_inode->i_ino,
798 dentry->d_parent->d_inode->i_ino);
800 printk("failed to delete reference to %.*s, "
801 "inode %lu parent %lu\n", name_len, name,
802 dentry->d_inode->i_ino,
803 dentry->d_parent->d_inode->i_ino);
806 btrfs_free_path(path);
808 dir->i_size -= name_len * 2;
809 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
810 btrfs_update_inode(trans, root, dir);
811 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
812 dentry->d_inode->i_nlink--;
814 drop_nlink(dentry->d_inode);
816 ret = btrfs_update_inode(trans, root, dentry->d_inode);
817 dir->i_sb->s_dirt = 1;
822 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
824 struct btrfs_root *root;
825 struct btrfs_trans_handle *trans;
826 struct inode *inode = dentry->d_inode;
828 unsigned long nr = 0;
830 root = BTRFS_I(dir)->root;
831 mutex_lock(&root->fs_info->fs_mutex);
833 ret = btrfs_check_free_space(root, 1, 1);
837 trans = btrfs_start_transaction(root, 1);
839 btrfs_set_trans_block_group(trans, dir);
840 ret = btrfs_unlink_trans(trans, root, dir, dentry);
841 nr = trans->blocks_used;
843 if (inode->i_nlink == 0) {
845 /* if the inode isn't linked anywhere,
846 * we don't need to worry about
849 found = btrfs_del_ordered_inode(inode);
851 atomic_dec(&inode->i_count);
855 btrfs_end_transaction(trans, root);
857 mutex_unlock(&root->fs_info->fs_mutex);
858 btrfs_btree_balance_dirty(root, nr);
859 btrfs_throttle(root);
863 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
865 struct inode *inode = dentry->d_inode;
868 struct btrfs_root *root = BTRFS_I(dir)->root;
869 struct btrfs_trans_handle *trans;
870 unsigned long nr = 0;
872 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
875 mutex_lock(&root->fs_info->fs_mutex);
876 ret = btrfs_check_free_space(root, 1, 1);
880 trans = btrfs_start_transaction(root, 1);
881 btrfs_set_trans_block_group(trans, dir);
883 /* now the directory is empty */
884 err = btrfs_unlink_trans(trans, root, dir, dentry);
889 nr = trans->blocks_used;
890 ret = btrfs_end_transaction(trans, root);
892 mutex_unlock(&root->fs_info->fs_mutex);
893 btrfs_btree_balance_dirty(root, nr);
894 btrfs_throttle(root);
902 * this can truncate away extent items, csum items and directory items.
903 * It starts at a high offset and removes keys until it can't find
904 * any higher than i_size.
906 * csum items that cross the new i_size are truncated to the new size
909 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
910 struct btrfs_root *root,
915 struct btrfs_path *path;
916 struct btrfs_key key;
917 struct btrfs_key found_key;
919 struct extent_buffer *leaf;
920 struct btrfs_file_extent_item *fi;
921 u64 extent_start = 0;
922 u64 extent_num_bytes = 0;
928 int pending_del_nr = 0;
929 int pending_del_slot = 0;
930 int extent_type = -1;
931 u64 mask = root->sectorsize - 1;
933 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
934 path = btrfs_alloc_path();
938 /* FIXME, add redo link to tree so we don't leak on crash */
939 key.objectid = inode->i_ino;
940 key.offset = (u64)-1;
943 btrfs_init_path(path);
945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
950 BUG_ON(path->slots[0] == 0);
956 leaf = path->nodes[0];
957 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
958 found_type = btrfs_key_type(&found_key);
960 if (found_key.objectid != inode->i_ino)
963 if (found_type < min_type)
966 item_end = found_key.offset;
967 if (found_type == BTRFS_EXTENT_DATA_KEY) {
968 fi = btrfs_item_ptr(leaf, path->slots[0],
969 struct btrfs_file_extent_item);
970 extent_type = btrfs_file_extent_type(leaf, fi);
971 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
973 btrfs_file_extent_num_bytes(leaf, fi);
974 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
975 struct btrfs_item *item = btrfs_item_nr(leaf,
977 item_end += btrfs_file_extent_inline_len(leaf,
982 if (found_type == BTRFS_CSUM_ITEM_KEY) {
983 ret = btrfs_csum_truncate(trans, root, path,
987 if (item_end < inode->i_size) {
988 if (found_type == BTRFS_DIR_ITEM_KEY) {
989 found_type = BTRFS_INODE_ITEM_KEY;
990 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
991 found_type = BTRFS_CSUM_ITEM_KEY;
992 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
993 found_type = BTRFS_XATTR_ITEM_KEY;
994 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
995 found_type = BTRFS_INODE_REF_KEY;
996 } else if (found_type) {
1001 btrfs_set_key_type(&key, found_type);
1004 if (found_key.offset >= inode->i_size)
1010 /* FIXME, shrink the extent if the ref count is only 1 */
1011 if (found_type != BTRFS_EXTENT_DATA_KEY)
1014 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1016 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1018 u64 orig_num_bytes =
1019 btrfs_file_extent_num_bytes(leaf, fi);
1020 extent_num_bytes = inode->i_size -
1021 found_key.offset + root->sectorsize - 1;
1022 extent_num_bytes = extent_num_bytes &
1023 ~((u64)root->sectorsize - 1);
1024 btrfs_set_file_extent_num_bytes(leaf, fi,
1026 num_dec = (orig_num_bytes -
1028 if (extent_start != 0)
1029 dec_i_blocks(inode, num_dec);
1030 btrfs_mark_buffer_dirty(leaf);
1033 btrfs_file_extent_disk_num_bytes(leaf,
1035 /* FIXME blocksize != 4096 */
1036 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1037 if (extent_start != 0) {
1039 dec_i_blocks(inode, num_dec);
1041 root_gen = btrfs_header_generation(leaf);
1042 root_owner = btrfs_header_owner(leaf);
1044 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1046 u32 newsize = inode->i_size - found_key.offset;
1047 dec_i_blocks(inode, item_end + 1 -
1048 found_key.offset - newsize);
1050 btrfs_file_extent_calc_inline_size(newsize);
1051 ret = btrfs_truncate_item(trans, root, path,
1055 dec_i_blocks(inode, item_end + 1 -
1061 if (!pending_del_nr) {
1062 /* no pending yet, add ourselves */
1063 pending_del_slot = path->slots[0];
1065 } else if (pending_del_nr &&
1066 path->slots[0] + 1 == pending_del_slot) {
1067 /* hop on the pending chunk */
1069 pending_del_slot = path->slots[0];
1071 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1077 ret = btrfs_free_extent(trans, root, extent_start,
1080 root_gen, inode->i_ino,
1081 found_key.offset, 0);
1085 if (path->slots[0] == 0) {
1088 btrfs_release_path(root, path);
1093 if (pending_del_nr &&
1094 path->slots[0] + 1 != pending_del_slot) {
1095 struct btrfs_key debug;
1097 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1099 ret = btrfs_del_items(trans, root, path,
1104 btrfs_release_path(root, path);
1110 if (pending_del_nr) {
1111 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1114 btrfs_release_path(root, path);
1115 btrfs_free_path(path);
1116 inode->i_sb->s_dirt = 1;
1120 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1124 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1125 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1126 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1129 WARN_ON(!PageLocked(page));
1130 set_page_extent_mapped(page);
1132 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1133 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1134 page_end, GFP_NOFS);
1136 if (zero_start != PAGE_CACHE_SIZE) {
1138 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1139 flush_dcache_page(page);
1142 set_page_dirty(page);
1143 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1149 * taken from block_truncate_page, but does cow as it zeros out
1150 * any bytes left in the last page in the file.
1152 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1154 struct inode *inode = mapping->host;
1155 struct btrfs_root *root = BTRFS_I(inode)->root;
1156 u32 blocksize = root->sectorsize;
1157 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1158 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1163 if ((offset & (blocksize - 1)) == 0)
1167 page = grab_cache_page(mapping, index);
1170 if (!PageUptodate(page)) {
1171 ret = btrfs_readpage(NULL, page);
1173 if (!PageUptodate(page)) {
1178 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1180 ret = btrfs_cow_one_page(inode, page, offset);
1183 page_cache_release(page);
1188 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1190 struct inode *inode = dentry->d_inode;
1193 err = inode_change_ok(inode, attr);
1197 if (S_ISREG(inode->i_mode) &&
1198 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1199 struct btrfs_trans_handle *trans;
1200 struct btrfs_root *root = BTRFS_I(inode)->root;
1201 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1203 u64 mask = root->sectorsize - 1;
1204 u64 hole_start = (inode->i_size + mask) & ~mask;
1205 u64 block_end = (attr->ia_size + mask) & ~mask;
1209 if (attr->ia_size <= hole_start)
1212 mutex_lock(&root->fs_info->fs_mutex);
1213 err = btrfs_check_free_space(root, 1, 0);
1214 mutex_unlock(&root->fs_info->fs_mutex);
1218 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1220 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1221 hole_size = block_end - hole_start;
1223 mutex_lock(&root->fs_info->fs_mutex);
1224 trans = btrfs_start_transaction(root, 1);
1225 btrfs_set_trans_block_group(trans, inode);
1226 err = btrfs_drop_extents(trans, root, inode,
1227 hole_start, block_end, hole_start,
1230 if (alloc_hint != EXTENT_MAP_INLINE) {
1231 err = btrfs_insert_file_extent(trans, root,
1235 btrfs_drop_extent_cache(inode, hole_start,
1237 btrfs_check_file(root, inode);
1239 btrfs_end_transaction(trans, root);
1240 mutex_unlock(&root->fs_info->fs_mutex);
1241 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1246 err = inode_setattr(inode, attr);
1251 void btrfs_put_inode(struct inode *inode)
1255 if (!BTRFS_I(inode)->ordered_trans) {
1259 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1260 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1263 ret = btrfs_del_ordered_inode(inode);
1265 atomic_dec(&inode->i_count);
1269 void btrfs_delete_inode(struct inode *inode)
1271 struct btrfs_trans_handle *trans;
1272 struct btrfs_root *root = BTRFS_I(inode)->root;
1276 truncate_inode_pages(&inode->i_data, 0);
1277 if (is_bad_inode(inode)) {
1282 mutex_lock(&root->fs_info->fs_mutex);
1283 trans = btrfs_start_transaction(root, 1);
1285 btrfs_set_trans_block_group(trans, inode);
1286 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1288 goto no_delete_lock;
1290 nr = trans->blocks_used;
1293 btrfs_end_transaction(trans, root);
1294 mutex_unlock(&root->fs_info->fs_mutex);
1295 btrfs_btree_balance_dirty(root, nr);
1296 btrfs_throttle(root);
1300 nr = trans->blocks_used;
1301 btrfs_end_transaction(trans, root);
1302 mutex_unlock(&root->fs_info->fs_mutex);
1303 btrfs_btree_balance_dirty(root, nr);
1304 btrfs_throttle(root);
1310 * this returns the key found in the dir entry in the location pointer.
1311 * If no dir entries were found, location->objectid is 0.
1313 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1314 struct btrfs_key *location)
1316 const char *name = dentry->d_name.name;
1317 int namelen = dentry->d_name.len;
1318 struct btrfs_dir_item *di;
1319 struct btrfs_path *path;
1320 struct btrfs_root *root = BTRFS_I(dir)->root;
1323 if (namelen == 1 && strcmp(name, ".") == 0) {
1324 location->objectid = dir->i_ino;
1325 location->type = BTRFS_INODE_ITEM_KEY;
1326 location->offset = 0;
1329 path = btrfs_alloc_path();
1332 if (namelen == 2 && strcmp(name, "..") == 0) {
1333 struct btrfs_key key;
1334 struct extent_buffer *leaf;
1338 key.objectid = dir->i_ino;
1339 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1341 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1345 leaf = path->nodes[0];
1346 slot = path->slots[0];
1347 nritems = btrfs_header_nritems(leaf);
1348 if (slot >= nritems)
1351 btrfs_item_key_to_cpu(leaf, &key, slot);
1352 if (key.objectid != dir->i_ino ||
1353 key.type != BTRFS_INODE_REF_KEY) {
1356 location->objectid = key.offset;
1357 location->type = BTRFS_INODE_ITEM_KEY;
1358 location->offset = 0;
1362 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1366 if (!di || IS_ERR(di)) {
1369 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1371 btrfs_free_path(path);
1374 location->objectid = 0;
1379 * when we hit a tree root in a directory, the btrfs part of the inode
1380 * needs to be changed to reflect the root directory of the tree root. This
1381 * is kind of like crossing a mount point.
1383 static int fixup_tree_root_location(struct btrfs_root *root,
1384 struct btrfs_key *location,
1385 struct btrfs_root **sub_root,
1386 struct dentry *dentry)
1388 struct btrfs_path *path;
1389 struct btrfs_root_item *ri;
1391 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1393 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1396 path = btrfs_alloc_path();
1398 mutex_lock(&root->fs_info->fs_mutex);
1400 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1401 dentry->d_name.name,
1402 dentry->d_name.len);
1403 if (IS_ERR(*sub_root))
1404 return PTR_ERR(*sub_root);
1406 ri = &(*sub_root)->root_item;
1407 location->objectid = btrfs_root_dirid(ri);
1408 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1409 location->offset = 0;
1411 btrfs_free_path(path);
1412 mutex_unlock(&root->fs_info->fs_mutex);
1416 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1418 struct btrfs_iget_args *args = p;
1419 inode->i_ino = args->ino;
1420 BTRFS_I(inode)->root = args->root;
1421 BTRFS_I(inode)->delalloc_bytes = 0;
1422 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1423 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1424 inode->i_mapping, GFP_NOFS);
1425 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1426 inode->i_mapping, GFP_NOFS);
1427 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1431 static int btrfs_find_actor(struct inode *inode, void *opaque)
1433 struct btrfs_iget_args *args = opaque;
1434 return (args->ino == inode->i_ino &&
1435 args->root == BTRFS_I(inode)->root);
1438 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1441 struct btrfs_iget_args args;
1442 args.ino = objectid;
1443 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1448 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1451 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1452 struct btrfs_root *root)
1454 struct inode *inode;
1455 struct btrfs_iget_args args;
1456 args.ino = objectid;
1459 inode = iget5_locked(s, objectid, btrfs_find_actor,
1460 btrfs_init_locked_inode,
1465 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1466 struct nameidata *nd)
1468 struct inode * inode;
1469 struct btrfs_inode *bi = BTRFS_I(dir);
1470 struct btrfs_root *root = bi->root;
1471 struct btrfs_root *sub_root = root;
1472 struct btrfs_key location;
1475 if (dentry->d_name.len > BTRFS_NAME_LEN)
1476 return ERR_PTR(-ENAMETOOLONG);
1478 mutex_lock(&root->fs_info->fs_mutex);
1479 ret = btrfs_inode_by_name(dir, dentry, &location);
1480 mutex_unlock(&root->fs_info->fs_mutex);
1483 return ERR_PTR(ret);
1486 if (location.objectid) {
1487 ret = fixup_tree_root_location(root, &location, &sub_root,
1490 return ERR_PTR(ret);
1492 return ERR_PTR(-ENOENT);
1493 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1496 return ERR_PTR(-EACCES);
1497 if (inode->i_state & I_NEW) {
1498 /* the inode and parent dir are two different roots */
1499 if (sub_root != root) {
1501 sub_root->inode = inode;
1503 BTRFS_I(inode)->root = sub_root;
1504 memcpy(&BTRFS_I(inode)->location, &location,
1506 btrfs_read_locked_inode(inode);
1507 unlock_new_inode(inode);
1510 return d_splice_alias(inode, dentry);
1513 static unsigned char btrfs_filetype_table[] = {
1514 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1517 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1519 struct inode *inode = filp->f_dentry->d_inode;
1520 struct btrfs_root *root = BTRFS_I(inode)->root;
1521 struct btrfs_item *item;
1522 struct btrfs_dir_item *di;
1523 struct btrfs_key key;
1524 struct btrfs_key found_key;
1525 struct btrfs_path *path;
1528 struct extent_buffer *leaf;
1531 unsigned char d_type;
1536 int key_type = BTRFS_DIR_INDEX_KEY;
1541 /* FIXME, use a real flag for deciding about the key type */
1542 if (root->fs_info->tree_root == root)
1543 key_type = BTRFS_DIR_ITEM_KEY;
1545 /* special case for "." */
1546 if (filp->f_pos == 0) {
1547 over = filldir(dirent, ".", 1,
1555 mutex_lock(&root->fs_info->fs_mutex);
1556 key.objectid = inode->i_ino;
1557 path = btrfs_alloc_path();
1560 /* special case for .., just use the back ref */
1561 if (filp->f_pos == 1) {
1562 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1564 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1566 leaf = path->nodes[0];
1567 slot = path->slots[0];
1568 nritems = btrfs_header_nritems(leaf);
1569 if (slot >= nritems) {
1570 btrfs_release_path(root, path);
1571 goto read_dir_items;
1573 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1574 btrfs_release_path(root, path);
1575 if (found_key.objectid != key.objectid ||
1576 found_key.type != BTRFS_INODE_REF_KEY)
1577 goto read_dir_items;
1578 over = filldir(dirent, "..", 2,
1579 2, found_key.offset, DT_DIR);
1586 btrfs_set_key_type(&key, key_type);
1587 key.offset = filp->f_pos;
1589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1594 leaf = path->nodes[0];
1595 nritems = btrfs_header_nritems(leaf);
1596 slot = path->slots[0];
1597 if (advance || slot >= nritems) {
1598 if (slot >= nritems -1) {
1599 ret = btrfs_next_leaf(root, path);
1602 leaf = path->nodes[0];
1603 nritems = btrfs_header_nritems(leaf);
1604 slot = path->slots[0];
1611 item = btrfs_item_nr(leaf, slot);
1612 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1614 if (found_key.objectid != key.objectid)
1616 if (btrfs_key_type(&found_key) != key_type)
1618 if (found_key.offset < filp->f_pos)
1621 filp->f_pos = found_key.offset;
1623 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1625 di_total = btrfs_item_size(leaf, item);
1626 while(di_cur < di_total) {
1627 struct btrfs_key location;
1629 name_len = btrfs_dir_name_len(leaf, di);
1630 if (name_len < 32) {
1631 name_ptr = tmp_name;
1633 name_ptr = kmalloc(name_len, GFP_NOFS);
1636 read_extent_buffer(leaf, name_ptr,
1637 (unsigned long)(di + 1), name_len);
1639 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1640 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1641 over = filldir(dirent, name_ptr, name_len,
1646 if (name_ptr != tmp_name)
1651 di_len = btrfs_dir_name_len(leaf, di) +
1652 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1654 di = (struct btrfs_dir_item *)((char *)di + di_len);
1657 if (key_type == BTRFS_DIR_INDEX_KEY)
1658 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1664 btrfs_release_path(root, path);
1665 btrfs_free_path(path);
1666 mutex_unlock(&root->fs_info->fs_mutex);
1670 int btrfs_write_inode(struct inode *inode, int wait)
1672 struct btrfs_root *root = BTRFS_I(inode)->root;
1673 struct btrfs_trans_handle *trans;
1677 mutex_lock(&root->fs_info->fs_mutex);
1678 trans = btrfs_start_transaction(root, 1);
1679 btrfs_set_trans_block_group(trans, inode);
1680 ret = btrfs_commit_transaction(trans, root);
1681 mutex_unlock(&root->fs_info->fs_mutex);
1687 * This is somewhat expensive, updating the tree every time the
1688 * inode changes. But, it is most likely to find the inode in cache.
1689 * FIXME, needs more benchmarking...there are no reasons other than performance
1690 * to keep or drop this code.
1692 void btrfs_dirty_inode(struct inode *inode)
1694 struct btrfs_root *root = BTRFS_I(inode)->root;
1695 struct btrfs_trans_handle *trans;
1697 mutex_lock(&root->fs_info->fs_mutex);
1698 trans = btrfs_start_transaction(root, 1);
1699 btrfs_set_trans_block_group(trans, inode);
1700 btrfs_update_inode(trans, root, inode);
1701 btrfs_end_transaction(trans, root);
1702 mutex_unlock(&root->fs_info->fs_mutex);
1705 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1706 struct btrfs_root *root,
1707 const char *name, int name_len,
1710 struct btrfs_block_group_cache *group,
1713 struct inode *inode;
1714 struct btrfs_inode_item *inode_item;
1715 struct btrfs_block_group_cache *new_inode_group;
1716 struct btrfs_key *location;
1717 struct btrfs_path *path;
1718 struct btrfs_inode_ref *ref;
1719 struct btrfs_key key[2];
1725 path = btrfs_alloc_path();
1728 inode = new_inode(root->fs_info->sb);
1730 return ERR_PTR(-ENOMEM);
1732 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1733 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1734 inode->i_mapping, GFP_NOFS);
1735 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1736 inode->i_mapping, GFP_NOFS);
1737 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1738 BTRFS_I(inode)->delalloc_bytes = 0;
1739 BTRFS_I(inode)->root = root;
1745 new_inode_group = btrfs_find_block_group(root, group, 0,
1746 BTRFS_BLOCK_GROUP_METADATA, owner);
1747 if (!new_inode_group) {
1748 printk("find_block group failed\n");
1749 new_inode_group = group;
1751 BTRFS_I(inode)->block_group = new_inode_group;
1752 BTRFS_I(inode)->flags = 0;
1754 key[0].objectid = objectid;
1755 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1758 key[1].objectid = objectid;
1759 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1760 key[1].offset = ref_objectid;
1762 sizes[0] = sizeof(struct btrfs_inode_item);
1763 sizes[1] = name_len + sizeof(*ref);
1765 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1769 if (objectid > root->highest_inode)
1770 root->highest_inode = objectid;
1772 inode->i_uid = current->fsuid;
1773 inode->i_gid = current->fsgid;
1774 inode->i_mode = mode;
1775 inode->i_ino = objectid;
1776 inode->i_blocks = 0;
1777 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1778 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1779 struct btrfs_inode_item);
1780 fill_inode_item(path->nodes[0], inode_item, inode);
1782 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1783 struct btrfs_inode_ref);
1784 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1785 ptr = (unsigned long)(ref + 1);
1786 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1788 btrfs_mark_buffer_dirty(path->nodes[0]);
1789 btrfs_free_path(path);
1791 location = &BTRFS_I(inode)->location;
1792 location->objectid = objectid;
1793 location->offset = 0;
1794 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1796 insert_inode_hash(inode);
1799 btrfs_free_path(path);
1800 return ERR_PTR(ret);
1803 static inline u8 btrfs_inode_type(struct inode *inode)
1805 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1808 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1809 struct dentry *dentry, struct inode *inode,
1813 struct btrfs_key key;
1814 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1815 struct inode *parent_inode;
1817 key.objectid = inode->i_ino;
1818 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1821 ret = btrfs_insert_dir_item(trans, root,
1822 dentry->d_name.name, dentry->d_name.len,
1823 dentry->d_parent->d_inode->i_ino,
1824 &key, btrfs_inode_type(inode));
1827 ret = btrfs_insert_inode_ref(trans, root,
1828 dentry->d_name.name,
1831 dentry->d_parent->d_inode->i_ino);
1833 parent_inode = dentry->d_parent->d_inode;
1834 parent_inode->i_size += dentry->d_name.len * 2;
1835 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1836 ret = btrfs_update_inode(trans, root,
1837 dentry->d_parent->d_inode);
1842 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1843 struct dentry *dentry, struct inode *inode,
1846 int err = btrfs_add_link(trans, dentry, inode, backref);
1848 d_instantiate(dentry, inode);
1856 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1857 int mode, dev_t rdev)
1859 struct btrfs_trans_handle *trans;
1860 struct btrfs_root *root = BTRFS_I(dir)->root;
1861 struct inode *inode = NULL;
1865 unsigned long nr = 0;
1867 if (!new_valid_dev(rdev))
1870 mutex_lock(&root->fs_info->fs_mutex);
1871 err = btrfs_check_free_space(root, 1, 0);
1875 trans = btrfs_start_transaction(root, 1);
1876 btrfs_set_trans_block_group(trans, dir);
1878 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1884 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1886 dentry->d_parent->d_inode->i_ino, objectid,
1887 BTRFS_I(dir)->block_group, mode);
1888 err = PTR_ERR(inode);
1892 btrfs_set_trans_block_group(trans, inode);
1893 err = btrfs_add_nondir(trans, dentry, inode, 0);
1897 inode->i_op = &btrfs_special_inode_operations;
1898 init_special_inode(inode, inode->i_mode, rdev);
1899 btrfs_update_inode(trans, root, inode);
1901 dir->i_sb->s_dirt = 1;
1902 btrfs_update_inode_block_group(trans, inode);
1903 btrfs_update_inode_block_group(trans, dir);
1905 nr = trans->blocks_used;
1906 btrfs_end_transaction(trans, root);
1908 mutex_unlock(&root->fs_info->fs_mutex);
1911 inode_dec_link_count(inode);
1914 btrfs_btree_balance_dirty(root, nr);
1915 btrfs_throttle(root);
1919 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1920 int mode, struct nameidata *nd)
1922 struct btrfs_trans_handle *trans;
1923 struct btrfs_root *root = BTRFS_I(dir)->root;
1924 struct inode *inode = NULL;
1927 unsigned long nr = 0;
1930 mutex_lock(&root->fs_info->fs_mutex);
1931 err = btrfs_check_free_space(root, 1, 0);
1934 trans = btrfs_start_transaction(root, 1);
1935 btrfs_set_trans_block_group(trans, dir);
1937 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1943 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1945 dentry->d_parent->d_inode->i_ino,
1946 objectid, BTRFS_I(dir)->block_group, mode);
1947 err = PTR_ERR(inode);
1951 btrfs_set_trans_block_group(trans, inode);
1952 err = btrfs_add_nondir(trans, dentry, inode, 0);
1956 inode->i_mapping->a_ops = &btrfs_aops;
1957 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1958 inode->i_fop = &btrfs_file_operations;
1959 inode->i_op = &btrfs_file_inode_operations;
1960 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1961 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1962 inode->i_mapping, GFP_NOFS);
1963 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1964 inode->i_mapping, GFP_NOFS);
1965 BTRFS_I(inode)->delalloc_bytes = 0;
1966 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
1967 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1969 dir->i_sb->s_dirt = 1;
1970 btrfs_update_inode_block_group(trans, inode);
1971 btrfs_update_inode_block_group(trans, dir);
1973 nr = trans->blocks_used;
1974 btrfs_end_transaction(trans, root);
1976 mutex_unlock(&root->fs_info->fs_mutex);
1979 inode_dec_link_count(inode);
1982 btrfs_btree_balance_dirty(root, nr);
1983 btrfs_throttle(root);
1987 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1988 struct dentry *dentry)
1990 struct btrfs_trans_handle *trans;
1991 struct btrfs_root *root = BTRFS_I(dir)->root;
1992 struct inode *inode = old_dentry->d_inode;
1993 unsigned long nr = 0;
1997 if (inode->i_nlink == 0)
2000 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2005 mutex_lock(&root->fs_info->fs_mutex);
2006 err = btrfs_check_free_space(root, 1, 0);
2009 trans = btrfs_start_transaction(root, 1);
2011 btrfs_set_trans_block_group(trans, dir);
2012 atomic_inc(&inode->i_count);
2013 err = btrfs_add_nondir(trans, dentry, inode, 1);
2018 dir->i_sb->s_dirt = 1;
2019 btrfs_update_inode_block_group(trans, dir);
2020 err = btrfs_update_inode(trans, root, inode);
2025 nr = trans->blocks_used;
2026 btrfs_end_transaction(trans, root);
2028 mutex_unlock(&root->fs_info->fs_mutex);
2031 inode_dec_link_count(inode);
2034 btrfs_btree_balance_dirty(root, nr);
2035 btrfs_throttle(root);
2039 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2041 struct inode *inode = NULL;
2042 struct btrfs_trans_handle *trans;
2043 struct btrfs_root *root = BTRFS_I(dir)->root;
2045 int drop_on_err = 0;
2047 unsigned long nr = 1;
2049 mutex_lock(&root->fs_info->fs_mutex);
2050 err = btrfs_check_free_space(root, 1, 0);
2054 trans = btrfs_start_transaction(root, 1);
2055 btrfs_set_trans_block_group(trans, dir);
2057 if (IS_ERR(trans)) {
2058 err = PTR_ERR(trans);
2062 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2068 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2070 dentry->d_parent->d_inode->i_ino, objectid,
2071 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2072 if (IS_ERR(inode)) {
2073 err = PTR_ERR(inode);
2078 inode->i_op = &btrfs_dir_inode_operations;
2079 inode->i_fop = &btrfs_dir_file_operations;
2080 btrfs_set_trans_block_group(trans, inode);
2083 err = btrfs_update_inode(trans, root, inode);
2087 err = btrfs_add_link(trans, dentry, inode, 0);
2091 d_instantiate(dentry, inode);
2093 dir->i_sb->s_dirt = 1;
2094 btrfs_update_inode_block_group(trans, inode);
2095 btrfs_update_inode_block_group(trans, dir);
2098 nr = trans->blocks_used;
2099 btrfs_end_transaction(trans, root);
2102 mutex_unlock(&root->fs_info->fs_mutex);
2105 btrfs_btree_balance_dirty(root, nr);
2106 btrfs_throttle(root);
2110 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2111 struct extent_map *existing,
2112 struct extent_map *em)
2117 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2119 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2122 if (!real_blocks && em->block_start != existing->block_start)
2125 new_end = max(existing->start + existing->len, em->start + em->len);
2127 if (existing->start >= em->start) {
2128 if (em->start + em->len < existing->start)
2131 start_diff = existing->start - em->start;
2132 if (real_blocks && em->block_start + start_diff !=
2133 existing->block_start)
2136 em->len = new_end - em->start;
2138 remove_extent_mapping(em_tree, existing);
2139 /* free for the tree */
2140 free_extent_map(existing);
2141 ret = add_extent_mapping(em_tree, em);
2143 } else if (em->start > existing->start) {
2145 if (existing->start + existing->len < em->start)
2148 start_diff = em->start - existing->start;
2149 if (real_blocks && existing->block_start + start_diff !=
2153 remove_extent_mapping(em_tree, existing);
2154 em->block_start = existing->block_start;
2155 em->start = existing->start;
2156 em->len = new_end - existing->start;
2157 free_extent_map(existing);
2159 ret = add_extent_mapping(em_tree, em);
2166 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2167 existing->start, existing->len, existing->block_start,
2168 em->start, em->len, em->block_start);
2172 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2173 size_t pg_offset, u64 start, u64 len,
2179 u64 extent_start = 0;
2181 u64 objectid = inode->i_ino;
2183 struct btrfs_path *path;
2184 struct btrfs_root *root = BTRFS_I(inode)->root;
2185 struct btrfs_file_extent_item *item;
2186 struct extent_buffer *leaf;
2187 struct btrfs_key found_key;
2188 struct extent_map *em = NULL;
2189 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2190 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2191 struct btrfs_trans_handle *trans = NULL;
2193 path = btrfs_alloc_path();
2195 mutex_lock(&root->fs_info->fs_mutex);
2198 spin_lock(&em_tree->lock);
2199 em = lookup_extent_mapping(em_tree, start, len);
2201 em->bdev = root->fs_info->fs_devices->latest_bdev;
2202 spin_unlock(&em_tree->lock);
2205 if (em->start > start || em->start + em->len <= start)
2206 free_extent_map(em);
2207 else if (em->block_start == EXTENT_MAP_INLINE && page)
2208 free_extent_map(em);
2212 em = alloc_extent_map(GFP_NOFS);
2218 em->start = EXTENT_MAP_HOLE;
2220 em->bdev = root->fs_info->fs_devices->latest_bdev;
2221 ret = btrfs_lookup_file_extent(trans, root, path,
2222 objectid, start, trans != NULL);
2229 if (path->slots[0] == 0)
2234 leaf = path->nodes[0];
2235 item = btrfs_item_ptr(leaf, path->slots[0],
2236 struct btrfs_file_extent_item);
2237 /* are we inside the extent that was found? */
2238 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2239 found_type = btrfs_key_type(&found_key);
2240 if (found_key.objectid != objectid ||
2241 found_type != BTRFS_EXTENT_DATA_KEY) {
2245 found_type = btrfs_file_extent_type(leaf, item);
2246 extent_start = found_key.offset;
2247 if (found_type == BTRFS_FILE_EXTENT_REG) {
2248 extent_end = extent_start +
2249 btrfs_file_extent_num_bytes(leaf, item);
2251 if (start < extent_start || start >= extent_end) {
2253 if (start < extent_start) {
2254 if (start + len <= extent_start)
2256 em->len = extent_end - extent_start;
2262 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2264 em->start = extent_start;
2265 em->len = extent_end - extent_start;
2266 em->block_start = EXTENT_MAP_HOLE;
2269 bytenr += btrfs_file_extent_offset(leaf, item);
2270 em->block_start = bytenr;
2271 em->start = extent_start;
2272 em->len = extent_end - extent_start;
2274 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2279 size_t extent_offset;
2282 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2284 extent_end = (extent_start + size + root->sectorsize - 1) &
2285 ~((u64)root->sectorsize - 1);
2286 if (start < extent_start || start >= extent_end) {
2288 if (start < extent_start) {
2289 if (start + len <= extent_start)
2291 em->len = extent_end - extent_start;
2297 em->block_start = EXTENT_MAP_INLINE;
2300 em->start = extent_start;
2305 page_start = page_offset(page) + pg_offset;
2306 extent_offset = page_start - extent_start;
2307 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2308 size - extent_offset);
2309 em->start = extent_start + extent_offset;
2310 em->len = (copy_size + root->sectorsize - 1) &
2311 ~((u64)root->sectorsize - 1);
2313 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2314 if (create == 0 && !PageUptodate(page)) {
2315 read_extent_buffer(leaf, map + pg_offset, ptr,
2317 flush_dcache_page(page);
2318 } else if (create && PageUptodate(page)) {
2321 free_extent_map(em);
2323 btrfs_release_path(root, path);
2324 trans = btrfs_start_transaction(root, 1);
2327 write_extent_buffer(leaf, map + pg_offset, ptr,
2329 btrfs_mark_buffer_dirty(leaf);
2332 set_extent_uptodate(io_tree, em->start,
2333 extent_map_end(em) - 1, GFP_NOFS);
2336 printk("unkknown found_type %d\n", found_type);
2343 em->block_start = EXTENT_MAP_HOLE;
2345 btrfs_release_path(root, path);
2346 if (em->start > start || extent_map_end(em) <= start) {
2347 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2353 spin_lock(&em_tree->lock);
2354 ret = add_extent_mapping(em_tree, em);
2355 /* it is possible that someone inserted the extent into the tree
2356 * while we had the lock dropped. It is also possible that
2357 * an overlapping map exists in the tree
2359 if (ret == -EEXIST) {
2360 struct extent_map *existing;
2361 existing = lookup_extent_mapping(em_tree, start, len);
2362 if (existing && (existing->start > start ||
2363 existing->start + existing->len <= start)) {
2364 free_extent_map(existing);
2368 existing = lookup_extent_mapping(em_tree, em->start,
2371 err = merge_extent_mapping(em_tree, existing,
2373 free_extent_map(existing);
2375 free_extent_map(em);
2380 printk("failing to insert %Lu %Lu\n",
2382 free_extent_map(em);
2386 free_extent_map(em);
2390 spin_unlock(&em_tree->lock);
2392 btrfs_free_path(path);
2394 ret = btrfs_end_transaction(trans, root);
2398 mutex_unlock(&root->fs_info->fs_mutex);
2400 free_extent_map(em);
2402 return ERR_PTR(err);
2407 #if 0 /* waiting for O_DIRECT reads */
2408 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2409 struct buffer_head *bh_result, int create)
2411 struct extent_map *em;
2412 u64 start = (u64)iblock << inode->i_blkbits;
2413 struct btrfs_multi_bio *multi = NULL;
2414 struct btrfs_root *root = BTRFS_I(inode)->root;
2420 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2422 if (!em || IS_ERR(em))
2425 if (em->start > start || em->start + em->len <= start) {
2429 if (em->block_start == EXTENT_MAP_INLINE) {
2434 len = em->start + em->len - start;
2435 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2437 if (em->block_start == EXTENT_MAP_HOLE ||
2438 em->block_start == EXTENT_MAP_DELALLOC) {
2439 bh_result->b_size = len;
2443 logical = start - em->start;
2444 logical = em->block_start + logical;
2447 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2448 logical, &map_length, &multi, 0);
2450 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2451 bh_result->b_size = min(map_length, len);
2453 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2454 set_buffer_mapped(bh_result);
2457 free_extent_map(em);
2462 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2463 const struct iovec *iov, loff_t offset,
2464 unsigned long nr_segs)
2468 struct file *file = iocb->ki_filp;
2469 struct inode *inode = file->f_mapping->host;
2474 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2475 offset, nr_segs, btrfs_get_block, NULL);
2479 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2481 return extent_bmap(mapping, iblock, btrfs_get_extent);
2484 int btrfs_readpage(struct file *file, struct page *page)
2486 struct extent_io_tree *tree;
2487 tree = &BTRFS_I(page->mapping->host)->io_tree;
2488 return extent_read_full_page(tree, page, btrfs_get_extent);
2491 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2493 struct extent_io_tree *tree;
2496 if (current->flags & PF_MEMALLOC) {
2497 redirty_page_for_writepage(wbc, page);
2501 tree = &BTRFS_I(page->mapping->host)->io_tree;
2502 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2505 static int btrfs_writepages(struct address_space *mapping,
2506 struct writeback_control *wbc)
2508 struct extent_io_tree *tree;
2509 tree = &BTRFS_I(mapping->host)->io_tree;
2510 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2514 btrfs_readpages(struct file *file, struct address_space *mapping,
2515 struct list_head *pages, unsigned nr_pages)
2517 struct extent_io_tree *tree;
2518 tree = &BTRFS_I(mapping->host)->io_tree;
2519 return extent_readpages(tree, mapping, pages, nr_pages,
2523 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2525 struct extent_io_tree *tree;
2526 struct extent_map_tree *map;
2529 tree = &BTRFS_I(page->mapping->host)->io_tree;
2530 map = &BTRFS_I(page->mapping->host)->extent_tree;
2531 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2533 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2534 ClearPagePrivate(page);
2535 set_page_private(page, 0);
2536 page_cache_release(page);
2541 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2543 struct extent_io_tree *tree;
2545 tree = &BTRFS_I(page->mapping->host)->io_tree;
2546 extent_invalidatepage(tree, page, offset);
2547 btrfs_releasepage(page, GFP_NOFS);
2548 if (PagePrivate(page)) {
2549 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2550 ClearPagePrivate(page);
2551 set_page_private(page, 0);
2552 page_cache_release(page);
2557 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2558 * called from a page fault handler when a page is first dirtied. Hence we must
2559 * be careful to check for EOF conditions here. We set the page up correctly
2560 * for a written page which means we get ENOSPC checking when writing into
2561 * holes and correct delalloc and unwritten extent mapping on filesystems that
2562 * support these features.
2564 * We are not allowed to take the i_mutex here so we have to play games to
2565 * protect against truncate races as the page could now be beyond EOF. Because
2566 * vmtruncate() writes the inode size before removing pages, once we have the
2567 * page lock we can determine safely if the page is beyond EOF. If it is not
2568 * beyond EOF, then the page is guaranteed safe against truncation until we
2571 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2573 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2574 struct btrfs_root *root = BTRFS_I(inode)->root;
2580 mutex_lock(&root->fs_info->fs_mutex);
2581 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2582 mutex_unlock(&root->fs_info->fs_mutex);
2589 wait_on_page_writeback(page);
2590 size = i_size_read(inode);
2591 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2593 if ((page->mapping != inode->i_mapping) ||
2594 (page_start > size)) {
2595 /* page got truncated out from underneath us */
2599 /* page is wholly or partially inside EOF */
2600 if (page_start + PAGE_CACHE_SIZE > size)
2601 end = size & ~PAGE_CACHE_MASK;
2603 end = PAGE_CACHE_SIZE;
2605 ret = btrfs_cow_one_page(inode, page, end);
2613 static void btrfs_truncate(struct inode *inode)
2615 struct btrfs_root *root = BTRFS_I(inode)->root;
2617 struct btrfs_trans_handle *trans;
2620 if (!S_ISREG(inode->i_mode))
2622 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2625 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2627 mutex_lock(&root->fs_info->fs_mutex);
2628 trans = btrfs_start_transaction(root, 1);
2629 btrfs_set_trans_block_group(trans, inode);
2631 /* FIXME, add redo link to tree so we don't leak on crash */
2632 ret = btrfs_truncate_in_trans(trans, root, inode,
2633 BTRFS_EXTENT_DATA_KEY);
2634 btrfs_update_inode(trans, root, inode);
2635 nr = trans->blocks_used;
2637 ret = btrfs_end_transaction(trans, root);
2639 mutex_unlock(&root->fs_info->fs_mutex);
2640 btrfs_btree_balance_dirty(root, nr);
2641 btrfs_throttle(root);
2644 static int noinline create_subvol(struct btrfs_root *root, char *name,
2647 struct btrfs_trans_handle *trans;
2648 struct btrfs_key key;
2649 struct btrfs_root_item root_item;
2650 struct btrfs_inode_item *inode_item;
2651 struct extent_buffer *leaf;
2652 struct btrfs_root *new_root = root;
2653 struct inode *inode;
2658 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2659 unsigned long nr = 1;
2661 mutex_lock(&root->fs_info->fs_mutex);
2662 ret = btrfs_check_free_space(root, 1, 0);
2666 trans = btrfs_start_transaction(root, 1);
2669 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2674 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2675 objectid, trans->transid, 0, 0,
2678 return PTR_ERR(leaf);
2680 btrfs_set_header_nritems(leaf, 0);
2681 btrfs_set_header_level(leaf, 0);
2682 btrfs_set_header_bytenr(leaf, leaf->start);
2683 btrfs_set_header_generation(leaf, trans->transid);
2684 btrfs_set_header_owner(leaf, objectid);
2686 write_extent_buffer(leaf, root->fs_info->fsid,
2687 (unsigned long)btrfs_header_fsid(leaf),
2689 btrfs_mark_buffer_dirty(leaf);
2691 inode_item = &root_item.inode;
2692 memset(inode_item, 0, sizeof(*inode_item));
2693 inode_item->generation = cpu_to_le64(1);
2694 inode_item->size = cpu_to_le64(3);
2695 inode_item->nlink = cpu_to_le32(1);
2696 inode_item->nblocks = cpu_to_le64(1);
2697 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2699 btrfs_set_root_bytenr(&root_item, leaf->start);
2700 btrfs_set_root_level(&root_item, 0);
2701 btrfs_set_root_refs(&root_item, 1);
2702 btrfs_set_root_used(&root_item, 0);
2704 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2705 root_item.drop_level = 0;
2707 free_extent_buffer(leaf);
2710 btrfs_set_root_dirid(&root_item, new_dirid);
2712 key.objectid = objectid;
2714 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2715 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2721 * insert the directory item
2723 key.offset = (u64)-1;
2724 dir = root->fs_info->sb->s_root->d_inode;
2725 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2726 name, namelen, dir->i_ino, &key,
2731 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2732 name, namelen, objectid,
2733 root->fs_info->sb->s_root->d_inode->i_ino);
2737 ret = btrfs_commit_transaction(trans, root);
2741 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2744 trans = btrfs_start_transaction(new_root, 1);
2747 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2749 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2752 inode->i_op = &btrfs_dir_inode_operations;
2753 inode->i_fop = &btrfs_dir_file_operations;
2754 new_root->inode = inode;
2756 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2760 ret = btrfs_update_inode(trans, new_root, inode);
2764 nr = trans->blocks_used;
2765 err = btrfs_commit_transaction(trans, new_root);
2769 mutex_unlock(&root->fs_info->fs_mutex);
2770 btrfs_btree_balance_dirty(root, nr);
2771 btrfs_throttle(root);
2775 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2777 struct btrfs_pending_snapshot *pending_snapshot;
2778 struct btrfs_trans_handle *trans;
2781 unsigned long nr = 0;
2783 if (!root->ref_cows)
2786 mutex_lock(&root->fs_info->fs_mutex);
2787 ret = btrfs_check_free_space(root, 1, 0);
2791 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2792 if (!pending_snapshot) {
2796 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2797 if (!pending_snapshot->name) {
2799 kfree(pending_snapshot);
2802 memcpy(pending_snapshot->name, name, namelen);
2803 pending_snapshot->name[namelen] = '\0';
2804 trans = btrfs_start_transaction(root, 1);
2806 pending_snapshot->root = root;
2807 list_add(&pending_snapshot->list,
2808 &trans->transaction->pending_snapshots);
2809 ret = btrfs_update_inode(trans, root, root->inode);
2810 err = btrfs_commit_transaction(trans, root);
2813 mutex_unlock(&root->fs_info->fs_mutex);
2814 btrfs_btree_balance_dirty(root, nr);
2815 btrfs_throttle(root);
2819 unsigned long btrfs_force_ra(struct address_space *mapping,
2820 struct file_ra_state *ra, struct file *file,
2821 pgoff_t offset, pgoff_t last_index)
2823 pgoff_t req_size = last_index - offset + 1;
2825 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2826 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2829 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2830 return offset + req_size;
2834 int btrfs_defrag_file(struct file *file) {
2835 struct inode *inode = fdentry(file)->d_inode;
2836 struct btrfs_root *root = BTRFS_I(inode)->root;
2837 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2839 unsigned long last_index;
2840 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2841 unsigned long total_read = 0;
2847 mutex_lock(&root->fs_info->fs_mutex);
2848 ret = btrfs_check_free_space(root, inode->i_size, 0);
2849 mutex_unlock(&root->fs_info->fs_mutex);
2853 mutex_lock(&inode->i_mutex);
2854 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2855 for (i = 0; i <= last_index; i++) {
2856 if (total_read % ra_pages == 0) {
2857 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2858 min(last_index, i + ra_pages - 1));
2861 page = grab_cache_page(inode->i_mapping, i);
2864 if (!PageUptodate(page)) {
2865 btrfs_readpage(NULL, page);
2867 if (!PageUptodate(page)) {
2869 page_cache_release(page);
2874 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2875 ClearPageDirty(page);
2877 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2879 wait_on_page_writeback(page);
2880 set_page_extent_mapped(page);
2882 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2883 page_end = page_start + PAGE_CACHE_SIZE - 1;
2885 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2886 set_extent_delalloc(io_tree, page_start,
2887 page_end, GFP_NOFS);
2889 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2890 set_page_dirty(page);
2892 page_cache_release(page);
2893 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2897 mutex_unlock(&inode->i_mutex);
2901 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2906 struct btrfs_ioctl_vol_args *vol_args;
2907 struct btrfs_trans_handle *trans;
2908 struct btrfs_device *device = NULL;
2910 char *devstr = NULL;
2915 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2920 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2924 namelen = strlen(vol_args->name);
2925 if (namelen > BTRFS_VOL_NAME_MAX) {
2930 mutex_lock(&root->fs_info->fs_mutex);
2931 sizestr = vol_args->name;
2932 devstr = strchr(sizestr, ':');
2935 sizestr = devstr + 1;
2937 devstr = vol_args->name;
2938 devid = simple_strtoull(devstr, &end, 10);
2939 printk("resizing devid %Lu\n", devid);
2941 device = btrfs_find_device(root, devid, NULL);
2943 printk("resizer unable to find device %Lu\n", devid);
2947 if (!strcmp(sizestr, "max"))
2948 new_size = device->bdev->bd_inode->i_size;
2950 if (sizestr[0] == '-') {
2953 } else if (sizestr[0] == '+') {
2957 new_size = btrfs_parse_size(sizestr);
2958 if (new_size == 0) {
2964 old_size = device->total_bytes;
2967 if (new_size > old_size) {
2971 new_size = old_size - new_size;
2972 } else if (mod > 0) {
2973 new_size = old_size + new_size;
2976 if (new_size < 256 * 1024 * 1024) {
2980 if (new_size > device->bdev->bd_inode->i_size) {
2985 do_div(new_size, root->sectorsize);
2986 new_size *= root->sectorsize;
2988 printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
2990 if (new_size > old_size) {
2991 trans = btrfs_start_transaction(root, 1);
2992 ret = btrfs_grow_device(trans, device, new_size);
2993 btrfs_commit_transaction(trans, root);
2995 ret = btrfs_shrink_device(device, new_size);
2999 mutex_unlock(&root->fs_info->fs_mutex);
3005 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3008 struct btrfs_ioctl_vol_args *vol_args;
3009 struct btrfs_dir_item *di;
3010 struct btrfs_path *path;
3015 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3020 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3025 namelen = strlen(vol_args->name);
3026 if (namelen > BTRFS_VOL_NAME_MAX) {
3030 if (strchr(vol_args->name, '/')) {
3035 path = btrfs_alloc_path();
3041 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3042 mutex_lock(&root->fs_info->fs_mutex);
3043 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3045 vol_args->name, namelen, 0);
3046 mutex_unlock(&root->fs_info->fs_mutex);
3047 btrfs_free_path(path);
3049 if (di && !IS_ERR(di)) {
3059 if (root == root->fs_info->tree_root)
3060 ret = create_subvol(root, vol_args->name, namelen);
3062 ret = create_snapshot(root, vol_args->name, namelen);
3068 static int btrfs_ioctl_defrag(struct file *file)
3070 struct inode *inode = fdentry(file)->d_inode;
3071 struct btrfs_root *root = BTRFS_I(inode)->root;
3073 switch (inode->i_mode & S_IFMT) {
3075 mutex_lock(&root->fs_info->fs_mutex);
3076 btrfs_defrag_root(root, 0);
3077 btrfs_defrag_root(root->fs_info->extent_root, 0);
3078 mutex_unlock(&root->fs_info->fs_mutex);
3081 btrfs_defrag_file(file);
3088 long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3090 struct btrfs_ioctl_vol_args *vol_args;
3093 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3098 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3102 ret = btrfs_init_new_device(root, vol_args->name);
3109 long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
3111 struct btrfs_ioctl_vol_args *vol_args;
3114 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3119 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3123 ret = btrfs_rm_device(root, vol_args->name);
3130 int dup_item_to_inode(struct btrfs_trans_handle *trans,
3131 struct btrfs_root *root,
3132 struct btrfs_path *path,
3133 struct extent_buffer *leaf,
3135 struct btrfs_key *key,
3139 int len = btrfs_item_size_nr(leaf, slot);
3140 struct btrfs_key ckey = *key;
3143 dup = kmalloc(len, GFP_NOFS);
3147 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
3148 btrfs_release_path(root, path);
3150 ckey.objectid = destino;
3151 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
3156 long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
3158 struct inode *inode = fdentry(file)->d_inode;
3159 struct btrfs_root *root = BTRFS_I(inode)->root;
3160 struct file *src_file;
3162 struct btrfs_trans_handle *trans;
3165 struct btrfs_path *path;
3166 struct btrfs_key key;
3167 struct extent_buffer *leaf;
3171 src_file = fget(src_fd);
3174 src = src_file->f_dentry->d_inode;
3177 if (src->i_sb != inode->i_sb)
3181 mutex_lock(&inode->i_mutex);
3182 mutex_lock(&src->i_mutex);
3184 mutex_lock(&src->i_mutex);
3185 mutex_lock(&inode->i_mutex);
3192 /* do any pending delalloc/csum calc on src, one way or
3193 another, and lock file content */
3195 filemap_write_and_wait(src->i_mapping);
3196 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3197 if (BTRFS_I(src)->delalloc_bytes == 0)
3199 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3202 mutex_lock(&root->fs_info->fs_mutex);
3203 trans = btrfs_start_transaction(root, 0);
3204 path = btrfs_alloc_path();
3210 key.type = BTRFS_EXTENT_DATA_KEY;
3211 key.objectid = src->i_ino;
3217 * note the key will change type as we walk through the
3220 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3224 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3225 ret = btrfs_next_leaf(root, path);
3231 leaf = path->nodes[0];
3232 slot = path->slots[0];
3233 btrfs_item_key_to_cpu(leaf, &key, slot);
3234 nritems = btrfs_header_nritems(leaf);
3236 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
3237 key.objectid != src->i_ino)
3240 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3241 struct btrfs_file_extent_item *extent;
3244 extent = btrfs_item_ptr(leaf, slot,
3245 struct btrfs_file_extent_item);
3246 found_type = btrfs_file_extent_type(leaf, extent);
3247 if (found_type == BTRFS_FILE_EXTENT_REG) {
3248 u64 len = btrfs_file_extent_num_bytes(leaf,
3250 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
3252 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
3254 u64 off = btrfs_file_extent_offset(leaf,
3256 btrfs_insert_file_extent(trans, root,
3259 /* ds == 0 means there's a hole */
3261 btrfs_inc_extent_ref(trans, root,
3263 root->root_key.objectid,
3267 pos = key.offset + len;
3268 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3269 ret = dup_item_to_inode(trans, root, path,
3274 pos = key.offset + btrfs_item_size_nr(leaf,
3277 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
3278 ret = dup_item_to_inode(trans, root, path, leaf,
3279 slot, &key, inode->i_ino);
3285 btrfs_release_path(root, path);
3290 btrfs_free_path(path);
3292 inode->i_blocks = src->i_blocks;
3293 i_size_write(inode, src->i_size);
3294 btrfs_update_inode(trans, root, inode);
3296 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3298 btrfs_end_transaction(trans, root);
3299 mutex_unlock(&root->fs_info->fs_mutex);
3302 mutex_unlock(&src->i_mutex);
3303 mutex_unlock(&inode->i_mutex);
3309 long btrfs_ioctl(struct file *file, unsigned int
3310 cmd, unsigned long arg)
3312 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3315 case BTRFS_IOC_SNAP_CREATE:
3316 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3317 case BTRFS_IOC_DEFRAG:
3318 return btrfs_ioctl_defrag(file);
3319 case BTRFS_IOC_RESIZE:
3320 return btrfs_ioctl_resize(root, (void __user *)arg);
3321 case BTRFS_IOC_ADD_DEV:
3322 return btrfs_ioctl_add_dev(root, (void __user *)arg);
3323 case BTRFS_IOC_RM_DEV:
3324 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
3325 case BTRFS_IOC_BALANCE:
3326 return btrfs_balance(root->fs_info->dev_root);
3327 case BTRFS_IOC_CLONE:
3328 return btrfs_ioctl_clone(file, arg);
3335 * Called inside transaction, so use GFP_NOFS
3337 struct inode *btrfs_alloc_inode(struct super_block *sb)
3339 struct btrfs_inode *ei;
3341 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3345 ei->ordered_trans = 0;
3346 return &ei->vfs_inode;
3349 void btrfs_destroy_inode(struct inode *inode)
3351 WARN_ON(!list_empty(&inode->i_dentry));
3352 WARN_ON(inode->i_data.nrpages);
3354 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3355 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3358 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3359 static void init_once(struct kmem_cache * cachep, void *foo)
3361 static void init_once(void * foo, struct kmem_cache * cachep,
3362 unsigned long flags)
3365 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3367 inode_init_once(&ei->vfs_inode);
3370 void btrfs_destroy_cachep(void)
3372 if (btrfs_inode_cachep)
3373 kmem_cache_destroy(btrfs_inode_cachep);
3374 if (btrfs_trans_handle_cachep)
3375 kmem_cache_destroy(btrfs_trans_handle_cachep);
3376 if (btrfs_transaction_cachep)
3377 kmem_cache_destroy(btrfs_transaction_cachep);
3378 if (btrfs_bit_radix_cachep)
3379 kmem_cache_destroy(btrfs_bit_radix_cachep);
3380 if (btrfs_path_cachep)
3381 kmem_cache_destroy(btrfs_path_cachep);
3384 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3385 unsigned long extra_flags,
3386 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3387 void (*ctor)(struct kmem_cache *, void *)
3389 void (*ctor)(void *, struct kmem_cache *,
3394 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3395 SLAB_MEM_SPREAD | extra_flags), ctor
3396 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3402 int btrfs_init_cachep(void)
3404 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3405 sizeof(struct btrfs_inode),
3407 if (!btrfs_inode_cachep)
3409 btrfs_trans_handle_cachep =
3410 btrfs_cache_create("btrfs_trans_handle_cache",
3411 sizeof(struct btrfs_trans_handle),
3413 if (!btrfs_trans_handle_cachep)
3415 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3416 sizeof(struct btrfs_transaction),
3418 if (!btrfs_transaction_cachep)
3420 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3421 sizeof(struct btrfs_path),
3423 if (!btrfs_path_cachep)
3425 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3426 SLAB_DESTROY_BY_RCU, NULL);
3427 if (!btrfs_bit_radix_cachep)
3431 btrfs_destroy_cachep();
3435 static int btrfs_getattr(struct vfsmount *mnt,
3436 struct dentry *dentry, struct kstat *stat)
3438 struct inode *inode = dentry->d_inode;
3439 generic_fillattr(inode, stat);
3440 stat->blksize = PAGE_CACHE_SIZE;
3441 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3445 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3446 struct inode * new_dir,struct dentry *new_dentry)
3448 struct btrfs_trans_handle *trans;
3449 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3450 struct inode *new_inode = new_dentry->d_inode;
3451 struct inode *old_inode = old_dentry->d_inode;
3452 struct timespec ctime = CURRENT_TIME;
3453 struct btrfs_path *path;
3456 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3457 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3461 mutex_lock(&root->fs_info->fs_mutex);
3462 ret = btrfs_check_free_space(root, 1, 0);
3466 trans = btrfs_start_transaction(root, 1);
3468 btrfs_set_trans_block_group(trans, new_dir);
3469 path = btrfs_alloc_path();
3475 old_dentry->d_inode->i_nlink++;
3476 old_dir->i_ctime = old_dir->i_mtime = ctime;
3477 new_dir->i_ctime = new_dir->i_mtime = ctime;
3478 old_inode->i_ctime = ctime;
3480 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3485 new_inode->i_ctime = CURRENT_TIME;
3486 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3490 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3495 btrfs_free_path(path);
3496 btrfs_end_transaction(trans, root);
3498 mutex_unlock(&root->fs_info->fs_mutex);
3502 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3503 const char *symname)
3505 struct btrfs_trans_handle *trans;
3506 struct btrfs_root *root = BTRFS_I(dir)->root;
3507 struct btrfs_path *path;
3508 struct btrfs_key key;
3509 struct inode *inode = NULL;
3516 struct btrfs_file_extent_item *ei;
3517 struct extent_buffer *leaf;
3518 unsigned long nr = 0;
3520 name_len = strlen(symname) + 1;
3521 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3522 return -ENAMETOOLONG;
3524 mutex_lock(&root->fs_info->fs_mutex);
3525 err = btrfs_check_free_space(root, 1, 0);
3529 trans = btrfs_start_transaction(root, 1);
3530 btrfs_set_trans_block_group(trans, dir);
3532 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3538 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3540 dentry->d_parent->d_inode->i_ino, objectid,
3541 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3542 err = PTR_ERR(inode);
3546 btrfs_set_trans_block_group(trans, inode);
3547 err = btrfs_add_nondir(trans, dentry, inode, 0);
3551 inode->i_mapping->a_ops = &btrfs_aops;
3552 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3553 inode->i_fop = &btrfs_file_operations;
3554 inode->i_op = &btrfs_file_inode_operations;
3555 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3556 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3557 inode->i_mapping, GFP_NOFS);
3558 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3559 inode->i_mapping, GFP_NOFS);
3560 BTRFS_I(inode)->delalloc_bytes = 0;
3561 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
3562 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3564 dir->i_sb->s_dirt = 1;
3565 btrfs_update_inode_block_group(trans, inode);
3566 btrfs_update_inode_block_group(trans, dir);
3570 path = btrfs_alloc_path();
3572 key.objectid = inode->i_ino;
3574 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3575 datasize = btrfs_file_extent_calc_inline_size(name_len);
3576 err = btrfs_insert_empty_item(trans, root, path, &key,
3582 leaf = path->nodes[0];
3583 ei = btrfs_item_ptr(leaf, path->slots[0],
3584 struct btrfs_file_extent_item);
3585 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3586 btrfs_set_file_extent_type(leaf, ei,
3587 BTRFS_FILE_EXTENT_INLINE);
3588 ptr = btrfs_file_extent_inline_start(ei);
3589 write_extent_buffer(leaf, symname, ptr, name_len);
3590 btrfs_mark_buffer_dirty(leaf);
3591 btrfs_free_path(path);
3593 inode->i_op = &btrfs_symlink_inode_operations;
3594 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3595 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3596 inode->i_size = name_len - 1;
3597 err = btrfs_update_inode(trans, root, inode);
3602 nr = trans->blocks_used;
3603 btrfs_end_transaction(trans, root);
3605 mutex_unlock(&root->fs_info->fs_mutex);
3607 inode_dec_link_count(inode);
3610 btrfs_btree_balance_dirty(root, nr);
3611 btrfs_throttle(root);
3615 static int btrfs_permission(struct inode *inode, int mask,
3616 struct nameidata *nd)
3618 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3620 return generic_permission(inode, mask, NULL);
3623 static struct inode_operations btrfs_dir_inode_operations = {
3624 .lookup = btrfs_lookup,
3625 .create = btrfs_create,
3626 .unlink = btrfs_unlink,
3628 .mkdir = btrfs_mkdir,
3629 .rmdir = btrfs_rmdir,
3630 .rename = btrfs_rename,
3631 .symlink = btrfs_symlink,
3632 .setattr = btrfs_setattr,
3633 .mknod = btrfs_mknod,
3634 .setxattr = generic_setxattr,
3635 .getxattr = generic_getxattr,
3636 .listxattr = btrfs_listxattr,
3637 .removexattr = generic_removexattr,
3638 .permission = btrfs_permission,
3640 static struct inode_operations btrfs_dir_ro_inode_operations = {
3641 .lookup = btrfs_lookup,
3642 .permission = btrfs_permission,
3644 static struct file_operations btrfs_dir_file_operations = {
3645 .llseek = generic_file_llseek,
3646 .read = generic_read_dir,
3647 .readdir = btrfs_readdir,
3648 .unlocked_ioctl = btrfs_ioctl,
3649 #ifdef CONFIG_COMPAT
3650 .compat_ioctl = btrfs_ioctl,
3654 static struct extent_io_ops btrfs_extent_io_ops = {
3655 .fill_delalloc = run_delalloc_range,
3656 .submit_bio_hook = btrfs_submit_bio_hook,
3657 .merge_bio_hook = btrfs_merge_bio_hook,
3658 .readpage_io_hook = btrfs_readpage_io_hook,
3659 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3660 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3661 .set_bit_hook = btrfs_set_bit_hook,
3662 .clear_bit_hook = btrfs_clear_bit_hook,
3665 static struct address_space_operations btrfs_aops = {
3666 .readpage = btrfs_readpage,
3667 .writepage = btrfs_writepage,
3668 .writepages = btrfs_writepages,
3669 .readpages = btrfs_readpages,
3670 .sync_page = block_sync_page,
3672 .direct_IO = btrfs_direct_IO,
3673 .invalidatepage = btrfs_invalidatepage,
3674 .releasepage = btrfs_releasepage,
3675 .set_page_dirty = __set_page_dirty_nobuffers,
3678 static struct address_space_operations btrfs_symlink_aops = {
3679 .readpage = btrfs_readpage,
3680 .writepage = btrfs_writepage,
3681 .invalidatepage = btrfs_invalidatepage,
3682 .releasepage = btrfs_releasepage,
3685 static struct inode_operations btrfs_file_inode_operations = {
3686 .truncate = btrfs_truncate,
3687 .getattr = btrfs_getattr,
3688 .setattr = btrfs_setattr,
3689 .setxattr = generic_setxattr,
3690 .getxattr = generic_getxattr,
3691 .listxattr = btrfs_listxattr,
3692 .removexattr = generic_removexattr,
3693 .permission = btrfs_permission,
3695 static struct inode_operations btrfs_special_inode_operations = {
3696 .getattr = btrfs_getattr,
3697 .setattr = btrfs_setattr,
3698 .permission = btrfs_permission,
3700 static struct inode_operations btrfs_symlink_inode_operations = {
3701 .readlink = generic_readlink,
3702 .follow_link = page_follow_link_light,
3703 .put_link = page_put_link,
3704 .permission = btrfs_permission,