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/bio.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
42 #include "print-tree.h"
45 struct btrfs_iget_args {
47 struct btrfs_root *root;
50 static struct inode_operations btrfs_dir_inode_operations;
51 static struct inode_operations btrfs_symlink_inode_operations;
52 static struct inode_operations btrfs_dir_ro_inode_operations;
53 static struct inode_operations btrfs_special_inode_operations;
54 static struct inode_operations btrfs_file_inode_operations;
55 static struct address_space_operations btrfs_aops;
56 static struct address_space_operations btrfs_symlink_aops;
57 static struct file_operations btrfs_dir_file_operations;
58 static struct extent_io_ops btrfs_extent_io_ops;
60 static struct kmem_cache *btrfs_inode_cachep;
61 struct kmem_cache *btrfs_trans_handle_cachep;
62 struct kmem_cache *btrfs_transaction_cachep;
63 struct kmem_cache *btrfs_bit_radix_cachep;
64 struct kmem_cache *btrfs_path_cachep;
67 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
77 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
95 spin_unlock(&root->fs_info->delalloc_lock);
99 static int cow_file_range(struct inode *inode, u64 start, u64 end)
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
106 u64 blocksize = root->sectorsize;
107 u64 orig_start = start;
109 struct btrfs_key ins;
112 trans = btrfs_start_transaction(root, 1);
114 btrfs_set_trans_block_group(trans, inode);
116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
117 num_bytes = max(blocksize, num_bytes);
118 ret = btrfs_drop_extents(trans, root, inode,
119 start, start + num_bytes, start, &alloc_hint);
120 orig_num_bytes = num_bytes;
122 if (alloc_hint == EXTENT_MAP_INLINE)
125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
139 inode->i_blocks += ins.offset >> 9;
140 btrfs_check_file(root, inode);
141 num_bytes -= cur_alloc_size;
142 alloc_hint = ins.objectid + ins.offset;
143 start += cur_alloc_size;
145 btrfs_drop_extent_cache(inode, orig_start,
146 orig_start + orig_num_bytes - 1);
147 btrfs_add_ordered_inode(inode);
148 btrfs_update_inode(trans, root, inode);
150 btrfs_end_transaction(trans, root);
154 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
162 struct btrfs_root *root = BTRFS_I(inode)->root;
163 struct extent_buffer *leaf;
165 struct btrfs_path *path;
166 struct btrfs_file_extent_item *item;
169 struct btrfs_key found_key;
171 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
172 path = btrfs_alloc_path();
175 ret = btrfs_lookup_file_extent(NULL, root, path,
176 inode->i_ino, start, 0);
178 btrfs_free_path(path);
184 if (path->slots[0] == 0)
189 leaf = path->nodes[0];
190 item = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_file_extent_item);
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
195 found_type = btrfs_key_type(&found_key);
196 if (found_key.objectid != inode->i_ino ||
197 found_type != BTRFS_EXTENT_DATA_KEY) {
201 found_type = btrfs_file_extent_type(leaf, item);
202 extent_start = found_key.offset;
203 if (found_type == BTRFS_FILE_EXTENT_REG) {
204 u64 extent_num_bytes;
206 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
207 extent_end = extent_start + extent_num_bytes;
210 if (loops && start != extent_start)
213 if (start < extent_start || start >= extent_end)
216 cow_end = min(end, extent_end - 1);
217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
225 if (bytenr + extent_num_bytes > total_fs_bytes)
228 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
238 btrfs_free_path(path);
241 btrfs_release_path(root, path);
246 cow_file_range(inode, start, cow_end);
251 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
253 struct btrfs_root *root = BTRFS_I(inode)->root;
255 mutex_lock(&root->fs_info->fs_mutex);
256 if (btrfs_test_opt(root, NODATACOW) ||
257 btrfs_test_flag(inode, NODATACOW))
258 ret = run_delalloc_nocow(inode, start, end);
260 ret = cow_file_range(inode, start, end);
262 mutex_unlock(&root->fs_info->fs_mutex);
266 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
267 unsigned long old, unsigned long bits)
269 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 spin_lock(&root->fs_info->delalloc_lock);
272 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
273 root->fs_info->delalloc_bytes += end - start + 1;
274 spin_unlock(&root->fs_info->delalloc_lock);
279 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
280 unsigned long old, unsigned long bits)
282 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 spin_lock(&root->fs_info->delalloc_lock);
285 if (end - start + 1 > root->fs_info->delalloc_bytes) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end - start + 1, root->fs_info->delalloc_bytes);
288 root->fs_info->delalloc_bytes = 0;
289 BTRFS_I(inode)->delalloc_bytes = 0;
291 root->fs_info->delalloc_bytes -= end - start + 1;
292 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
294 spin_unlock(&root->fs_info->delalloc_lock);
299 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
300 size_t size, struct bio *bio)
302 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
303 struct btrfs_mapping_tree *map_tree;
304 u64 logical = bio->bi_sector << 9;
307 struct bio_vec *bvec;
311 bio_for_each_segment(bvec, bio, i) {
312 length += bvec->bv_len;
314 map_tree = &root->fs_info->mapping_tree;
316 ret = btrfs_map_block(map_tree, READ, logical,
317 &map_length, NULL, 0);
319 if (map_length < length + size) {
325 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
328 struct btrfs_root *root = BTRFS_I(inode)->root;
329 struct btrfs_trans_handle *trans;
332 if (!(rw & (1 << BIO_RW))) {
333 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
338 if (btrfs_test_opt(root, NODATASUM) ||
339 btrfs_test_flag(inode, NODATASUM)) {
343 mutex_lock(&root->fs_info->fs_mutex);
344 trans = btrfs_start_transaction(root, 1);
345 btrfs_set_trans_block_group(trans, inode);
346 btrfs_csum_file_blocks(trans, root, inode, bio);
347 ret = btrfs_end_transaction(trans, root);
349 mutex_unlock(&root->fs_info->fs_mutex);
351 return btrfs_map_bio(root, rw, bio, mirror_num);
354 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
357 struct inode *inode = page->mapping->host;
358 struct btrfs_root *root = BTRFS_I(inode)->root;
359 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
360 struct btrfs_csum_item *item;
361 struct btrfs_path *path = NULL;
363 if (btrfs_test_opt(root, NODATASUM) ||
364 btrfs_test_flag(inode, NODATASUM))
366 mutex_lock(&root->fs_info->fs_mutex);
367 path = btrfs_alloc_path();
368 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
371 /* a csum that isn't present is a preallocated region. */
372 if (ret == -ENOENT || ret == -EFBIG)
375 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
378 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
380 set_state_private(io_tree, start, csum);
383 btrfs_free_path(path);
384 mutex_unlock(&root->fs_info->fs_mutex);
388 struct io_failure_record {
396 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
397 struct page *page, u64 start, u64 end,
398 struct extent_state *state)
400 struct io_failure_record *failrec = NULL;
402 struct extent_map *em;
403 struct inode *inode = page->mapping->host;
404 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
410 ret = get_state_private(failure_tree, start, &private);
412 size_t pg_offset = start - page_offset(page);
413 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
416 failrec->start = start;
417 failrec->len = end - start + 1;
418 failrec->last_mirror = 0;
420 em = btrfs_get_extent(inode, NULL, pg_offset, start,
423 if (!em || IS_ERR(em)) {
427 logical = start - em->start;
428 logical = em->block_start + logical;
429 failrec->logical = logical;
431 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
432 EXTENT_DIRTY, GFP_NOFS);
433 set_state_private(failure_tree, start, (u64)failrec);
435 failrec = (struct io_failure_record *)private;
437 num_copies = btrfs_num_copies(
438 &BTRFS_I(inode)->root->fs_info->mapping_tree,
439 failrec->logical, failrec->len);
440 failrec->last_mirror++;
442 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
443 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
446 if (state && state->start != failrec->start)
448 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
450 if (!state || failrec->last_mirror > num_copies) {
451 set_state_private(failure_tree, failrec->start, 0);
452 clear_extent_bits(failure_tree, failrec->start,
453 failrec->start + failrec->len - 1,
454 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
458 bio = bio_alloc(GFP_NOFS, 1);
459 bio->bi_private = state;
460 bio->bi_end_io = failed_bio->bi_end_io;
461 bio->bi_sector = failrec->logical >> 9;
462 bio->bi_bdev = failed_bio->bi_bdev;
463 bio_add_page(bio, page, failrec->len, start - page_offset(page));
464 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
468 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
469 struct extent_state *state)
471 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
472 struct inode *inode = page->mapping->host;
473 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
475 u64 private = ~(u32)0;
477 struct btrfs_root *root = BTRFS_I(inode)->root;
481 if (btrfs_test_opt(root, NODATASUM) ||
482 btrfs_test_flag(inode, NODATASUM))
484 if (state && state->start == start) {
485 private = state->private;
488 ret = get_state_private(io_tree, start, &private);
490 local_irq_save(flags);
491 kaddr = kmap_atomic(page, KM_IRQ0);
495 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
496 btrfs_csum_final(csum, (char *)&csum);
497 if (csum != private) {
500 kunmap_atomic(kaddr, KM_IRQ0);
501 local_irq_restore(flags);
503 /* if the io failure tree for this inode is non-empty,
504 * check to see if we've recovered from a failed IO
507 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
508 (u64)-1, 1, EXTENT_DIRTY)) {
510 struct io_failure_record *failure;
511 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
512 start, &private_failure);
514 failure = (struct io_failure_record *)private_failure;
515 set_state_private(&BTRFS_I(inode)->io_failure_tree,
517 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
519 failure->start + failure->len - 1,
520 EXTENT_DIRTY | EXTENT_LOCKED,
528 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
529 page->mapping->host->i_ino, (unsigned long long)start, csum,
531 memset(kaddr + offset, 1, end - start + 1);
532 flush_dcache_page(page);
533 kunmap_atomic(kaddr, KM_IRQ0);
534 local_irq_restore(flags);
538 void btrfs_read_locked_inode(struct inode *inode)
540 struct btrfs_path *path;
541 struct extent_buffer *leaf;
542 struct btrfs_inode_item *inode_item;
543 struct btrfs_timespec *tspec;
544 struct btrfs_root *root = BTRFS_I(inode)->root;
545 struct btrfs_key location;
546 u64 alloc_group_block;
550 path = btrfs_alloc_path();
552 mutex_lock(&root->fs_info->fs_mutex);
553 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
555 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
559 leaf = path->nodes[0];
560 inode_item = btrfs_item_ptr(leaf, path->slots[0],
561 struct btrfs_inode_item);
563 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
564 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
565 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
566 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
567 inode->i_size = btrfs_inode_size(leaf, inode_item);
569 tspec = btrfs_inode_atime(inode_item);
570 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
571 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
573 tspec = btrfs_inode_mtime(inode_item);
574 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
575 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
577 tspec = btrfs_inode_ctime(inode_item);
578 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
579 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
581 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
582 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
584 rdev = btrfs_inode_rdev(leaf, inode_item);
586 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
587 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
589 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
590 if (!BTRFS_I(inode)->block_group) {
591 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
593 BTRFS_BLOCK_GROUP_METADATA, 0);
595 btrfs_free_path(path);
598 mutex_unlock(&root->fs_info->fs_mutex);
600 switch (inode->i_mode & S_IFMT) {
602 inode->i_mapping->a_ops = &btrfs_aops;
603 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
604 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
605 inode->i_fop = &btrfs_file_operations;
606 inode->i_op = &btrfs_file_inode_operations;
609 inode->i_fop = &btrfs_dir_file_operations;
610 if (root == root->fs_info->tree_root)
611 inode->i_op = &btrfs_dir_ro_inode_operations;
613 inode->i_op = &btrfs_dir_inode_operations;
616 inode->i_op = &btrfs_symlink_inode_operations;
617 inode->i_mapping->a_ops = &btrfs_symlink_aops;
618 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
621 init_special_inode(inode, inode->i_mode, rdev);
627 btrfs_release_path(root, path);
628 btrfs_free_path(path);
629 mutex_unlock(&root->fs_info->fs_mutex);
630 make_bad_inode(inode);
633 static void fill_inode_item(struct extent_buffer *leaf,
634 struct btrfs_inode_item *item,
637 btrfs_set_inode_uid(leaf, item, inode->i_uid);
638 btrfs_set_inode_gid(leaf, item, inode->i_gid);
639 btrfs_set_inode_size(leaf, item, inode->i_size);
640 btrfs_set_inode_mode(leaf, item, inode->i_mode);
641 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
643 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
644 inode->i_atime.tv_sec);
645 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
646 inode->i_atime.tv_nsec);
648 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
649 inode->i_mtime.tv_sec);
650 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
651 inode->i_mtime.tv_nsec);
653 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
654 inode->i_ctime.tv_sec);
655 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
656 inode->i_ctime.tv_nsec);
658 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
659 btrfs_set_inode_generation(leaf, item, inode->i_generation);
660 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
661 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
662 btrfs_set_inode_block_group(leaf, item,
663 BTRFS_I(inode)->block_group->key.objectid);
666 int btrfs_update_inode(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root,
670 struct btrfs_inode_item *inode_item;
671 struct btrfs_path *path;
672 struct extent_buffer *leaf;
675 path = btrfs_alloc_path();
677 ret = btrfs_lookup_inode(trans, root, path,
678 &BTRFS_I(inode)->location, 1);
685 leaf = path->nodes[0];
686 inode_item = btrfs_item_ptr(leaf, path->slots[0],
687 struct btrfs_inode_item);
689 fill_inode_item(leaf, inode_item, inode);
690 btrfs_mark_buffer_dirty(leaf);
691 btrfs_set_inode_last_trans(trans, inode);
694 btrfs_release_path(root, path);
695 btrfs_free_path(path);
700 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
701 struct btrfs_root *root,
703 struct dentry *dentry)
705 struct btrfs_path *path;
706 const char *name = dentry->d_name.name;
707 int name_len = dentry->d_name.len;
709 struct extent_buffer *leaf;
710 struct btrfs_dir_item *di;
711 struct btrfs_key key;
713 path = btrfs_alloc_path();
719 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
729 leaf = path->nodes[0];
730 btrfs_dir_item_key_to_cpu(leaf, di, &key);
731 ret = btrfs_delete_one_dir_name(trans, root, path, di);
734 btrfs_release_path(root, path);
736 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
737 key.objectid, name, name_len, -1);
746 ret = btrfs_delete_one_dir_name(trans, root, path, di);
748 dentry->d_inode->i_ctime = dir->i_ctime;
749 ret = btrfs_del_inode_ref(trans, root, name, name_len,
750 dentry->d_inode->i_ino,
751 dentry->d_parent->d_inode->i_ino);
753 printk("failed to delete reference to %.*s, "
754 "inode %lu parent %lu\n", name_len, name,
755 dentry->d_inode->i_ino,
756 dentry->d_parent->d_inode->i_ino);
759 btrfs_free_path(path);
761 dir->i_size -= name_len * 2;
762 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
763 btrfs_update_inode(trans, root, dir);
764 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
765 dentry->d_inode->i_nlink--;
767 drop_nlink(dentry->d_inode);
769 ret = btrfs_update_inode(trans, root, dentry->d_inode);
770 dir->i_sb->s_dirt = 1;
775 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
777 struct btrfs_root *root;
778 struct btrfs_trans_handle *trans;
779 struct inode *inode = dentry->d_inode;
781 unsigned long nr = 0;
783 root = BTRFS_I(dir)->root;
784 mutex_lock(&root->fs_info->fs_mutex);
786 ret = btrfs_check_free_space(root, 1, 1);
790 trans = btrfs_start_transaction(root, 1);
792 btrfs_set_trans_block_group(trans, dir);
793 ret = btrfs_unlink_trans(trans, root, dir, dentry);
794 nr = trans->blocks_used;
796 if (inode->i_nlink == 0) {
798 /* if the inode isn't linked anywhere,
799 * we don't need to worry about
802 found = btrfs_del_ordered_inode(inode);
804 atomic_dec(&inode->i_count);
808 btrfs_end_transaction(trans, root);
810 mutex_unlock(&root->fs_info->fs_mutex);
811 btrfs_btree_balance_dirty(root, nr);
812 btrfs_throttle(root);
816 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
818 struct inode *inode = dentry->d_inode;
821 struct btrfs_root *root = BTRFS_I(dir)->root;
822 struct btrfs_trans_handle *trans;
823 unsigned long nr = 0;
825 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
828 mutex_lock(&root->fs_info->fs_mutex);
829 ret = btrfs_check_free_space(root, 1, 1);
833 trans = btrfs_start_transaction(root, 1);
834 btrfs_set_trans_block_group(trans, dir);
836 /* now the directory is empty */
837 err = btrfs_unlink_trans(trans, root, dir, dentry);
842 nr = trans->blocks_used;
843 ret = btrfs_end_transaction(trans, root);
845 mutex_unlock(&root->fs_info->fs_mutex);
846 btrfs_btree_balance_dirty(root, nr);
847 btrfs_throttle(root);
855 * this can truncate away extent items, csum items and directory items.
856 * It starts at a high offset and removes keys until it can't find
857 * any higher than i_size.
859 * csum items that cross the new i_size are truncated to the new size
862 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
863 struct btrfs_root *root,
868 struct btrfs_path *path;
869 struct btrfs_key key;
870 struct btrfs_key found_key;
872 struct extent_buffer *leaf;
873 struct btrfs_file_extent_item *fi;
874 u64 extent_start = 0;
875 u64 extent_num_bytes = 0;
881 int pending_del_nr = 0;
882 int pending_del_slot = 0;
883 int extent_type = -1;
885 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
886 path = btrfs_alloc_path();
890 /* FIXME, add redo link to tree so we don't leak on crash */
891 key.objectid = inode->i_ino;
892 key.offset = (u64)-1;
895 btrfs_init_path(path);
897 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
902 BUG_ON(path->slots[0] == 0);
908 leaf = path->nodes[0];
909 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
910 found_type = btrfs_key_type(&found_key);
912 if (found_key.objectid != inode->i_ino)
915 if (found_type < min_type)
918 item_end = found_key.offset;
919 if (found_type == BTRFS_EXTENT_DATA_KEY) {
920 fi = btrfs_item_ptr(leaf, path->slots[0],
921 struct btrfs_file_extent_item);
922 extent_type = btrfs_file_extent_type(leaf, fi);
923 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
925 btrfs_file_extent_num_bytes(leaf, fi);
926 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
927 struct btrfs_item *item = btrfs_item_nr(leaf,
929 item_end += btrfs_file_extent_inline_len(leaf,
934 if (found_type == BTRFS_CSUM_ITEM_KEY) {
935 ret = btrfs_csum_truncate(trans, root, path,
939 if (item_end < inode->i_size) {
940 if (found_type == BTRFS_DIR_ITEM_KEY) {
941 found_type = BTRFS_INODE_ITEM_KEY;
942 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
943 found_type = BTRFS_CSUM_ITEM_KEY;
944 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
945 found_type = BTRFS_XATTR_ITEM_KEY;
946 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
947 found_type = BTRFS_INODE_REF_KEY;
948 } else if (found_type) {
953 btrfs_set_key_type(&key, found_type);
956 if (found_key.offset >= inode->i_size)
962 /* FIXME, shrink the extent if the ref count is only 1 */
963 if (found_type != BTRFS_EXTENT_DATA_KEY)
966 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
968 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
971 btrfs_file_extent_num_bytes(leaf, fi);
972 extent_num_bytes = inode->i_size -
973 found_key.offset + root->sectorsize - 1;
974 extent_num_bytes = extent_num_bytes &
975 ~((u64)root->sectorsize - 1);
976 btrfs_set_file_extent_num_bytes(leaf, fi,
978 num_dec = (orig_num_bytes -
980 if (extent_start != 0)
981 dec_i_blocks(inode, num_dec);
982 btrfs_mark_buffer_dirty(leaf);
985 btrfs_file_extent_disk_num_bytes(leaf,
987 /* FIXME blocksize != 4096 */
988 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
989 if (extent_start != 0) {
991 dec_i_blocks(inode, num_dec);
993 root_gen = btrfs_header_generation(leaf);
994 root_owner = btrfs_header_owner(leaf);
996 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
998 u32 newsize = inode->i_size - found_key.offset;
999 dec_i_blocks(inode, item_end + 1 -
1000 found_key.offset - newsize);
1002 btrfs_file_extent_calc_inline_size(newsize);
1003 ret = btrfs_truncate_item(trans, root, path,
1007 dec_i_blocks(inode, item_end + 1 -
1013 if (!pending_del_nr) {
1014 /* no pending yet, add ourselves */
1015 pending_del_slot = path->slots[0];
1017 } else if (pending_del_nr &&
1018 path->slots[0] + 1 == pending_del_slot) {
1019 /* hop on the pending chunk */
1021 pending_del_slot = path->slots[0];
1023 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1029 ret = btrfs_free_extent(trans, root, extent_start,
1032 root_gen, inode->i_ino,
1033 found_key.offset, 0);
1037 if (path->slots[0] == 0) {
1040 btrfs_release_path(root, path);
1045 if (pending_del_nr &&
1046 path->slots[0] + 1 != pending_del_slot) {
1047 struct btrfs_key debug;
1049 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1051 ret = btrfs_del_items(trans, root, path,
1056 btrfs_release_path(root, path);
1062 if (pending_del_nr) {
1063 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1066 btrfs_release_path(root, path);
1067 btrfs_free_path(path);
1068 inode->i_sb->s_dirt = 1;
1072 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1077 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1078 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1081 WARN_ON(!PageLocked(page));
1082 set_page_extent_mapped(page);
1084 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1085 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1086 page_end, GFP_NOFS);
1088 if (zero_start != PAGE_CACHE_SIZE) {
1090 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1091 flush_dcache_page(page);
1094 set_page_dirty(page);
1095 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1101 * taken from block_truncate_page, but does cow as it zeros out
1102 * any bytes left in the last page in the file.
1104 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1106 struct inode *inode = mapping->host;
1107 struct btrfs_root *root = BTRFS_I(inode)->root;
1108 u32 blocksize = root->sectorsize;
1109 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1110 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1115 if ((offset & (blocksize - 1)) == 0)
1119 page = grab_cache_page(mapping, index);
1122 if (!PageUptodate(page)) {
1123 ret = btrfs_readpage(NULL, page);
1125 if (!PageUptodate(page)) {
1130 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1132 ret = btrfs_cow_one_page(inode, page, offset);
1135 page_cache_release(page);
1140 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1142 struct inode *inode = dentry->d_inode;
1145 err = inode_change_ok(inode, attr);
1149 if (S_ISREG(inode->i_mode) &&
1150 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1151 struct btrfs_trans_handle *trans;
1152 struct btrfs_root *root = BTRFS_I(inode)->root;
1153 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1155 u64 mask = root->sectorsize - 1;
1156 u64 hole_start = (inode->i_size + mask) & ~mask;
1157 u64 block_end = (attr->ia_size + mask) & ~mask;
1161 if (attr->ia_size <= hole_start)
1164 mutex_lock(&root->fs_info->fs_mutex);
1165 err = btrfs_check_free_space(root, 1, 0);
1166 mutex_unlock(&root->fs_info->fs_mutex);
1170 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1172 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1173 hole_size = block_end - hole_start;
1175 mutex_lock(&root->fs_info->fs_mutex);
1176 trans = btrfs_start_transaction(root, 1);
1177 btrfs_set_trans_block_group(trans, inode);
1178 err = btrfs_drop_extents(trans, root, inode,
1179 hole_start, block_end, hole_start,
1182 if (alloc_hint != EXTENT_MAP_INLINE) {
1183 err = btrfs_insert_file_extent(trans, root,
1187 btrfs_drop_extent_cache(inode, hole_start,
1189 btrfs_check_file(root, inode);
1191 btrfs_end_transaction(trans, root);
1192 mutex_unlock(&root->fs_info->fs_mutex);
1193 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1198 err = inode_setattr(inode, attr);
1203 void btrfs_put_inode(struct inode *inode)
1207 if (!BTRFS_I(inode)->ordered_trans) {
1211 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1212 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1215 ret = btrfs_del_ordered_inode(inode);
1217 atomic_dec(&inode->i_count);
1221 void btrfs_delete_inode(struct inode *inode)
1223 struct btrfs_trans_handle *trans;
1224 struct btrfs_root *root = BTRFS_I(inode)->root;
1228 truncate_inode_pages(&inode->i_data, 0);
1229 if (is_bad_inode(inode)) {
1234 mutex_lock(&root->fs_info->fs_mutex);
1235 trans = btrfs_start_transaction(root, 1);
1237 btrfs_set_trans_block_group(trans, inode);
1238 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1240 goto no_delete_lock;
1242 nr = trans->blocks_used;
1245 btrfs_end_transaction(trans, root);
1246 mutex_unlock(&root->fs_info->fs_mutex);
1247 btrfs_btree_balance_dirty(root, nr);
1248 btrfs_throttle(root);
1252 nr = trans->blocks_used;
1253 btrfs_end_transaction(trans, root);
1254 mutex_unlock(&root->fs_info->fs_mutex);
1255 btrfs_btree_balance_dirty(root, nr);
1256 btrfs_throttle(root);
1262 * this returns the key found in the dir entry in the location pointer.
1263 * If no dir entries were found, location->objectid is 0.
1265 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1266 struct btrfs_key *location)
1268 const char *name = dentry->d_name.name;
1269 int namelen = dentry->d_name.len;
1270 struct btrfs_dir_item *di;
1271 struct btrfs_path *path;
1272 struct btrfs_root *root = BTRFS_I(dir)->root;
1275 if (namelen == 1 && strcmp(name, ".") == 0) {
1276 location->objectid = dir->i_ino;
1277 location->type = BTRFS_INODE_ITEM_KEY;
1278 location->offset = 0;
1281 path = btrfs_alloc_path();
1284 if (namelen == 2 && strcmp(name, "..") == 0) {
1285 struct btrfs_key key;
1286 struct extent_buffer *leaf;
1290 key.objectid = dir->i_ino;
1291 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1297 leaf = path->nodes[0];
1298 slot = path->slots[0];
1299 nritems = btrfs_header_nritems(leaf);
1300 if (slot >= nritems)
1303 btrfs_item_key_to_cpu(leaf, &key, slot);
1304 if (key.objectid != dir->i_ino ||
1305 key.type != BTRFS_INODE_REF_KEY) {
1308 location->objectid = key.offset;
1309 location->type = BTRFS_INODE_ITEM_KEY;
1310 location->offset = 0;
1314 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1318 if (!di || IS_ERR(di)) {
1321 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1323 btrfs_free_path(path);
1326 location->objectid = 0;
1331 * when we hit a tree root in a directory, the btrfs part of the inode
1332 * needs to be changed to reflect the root directory of the tree root. This
1333 * is kind of like crossing a mount point.
1335 static int fixup_tree_root_location(struct btrfs_root *root,
1336 struct btrfs_key *location,
1337 struct btrfs_root **sub_root,
1338 struct dentry *dentry)
1340 struct btrfs_path *path;
1341 struct btrfs_root_item *ri;
1343 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1345 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1348 path = btrfs_alloc_path();
1350 mutex_lock(&root->fs_info->fs_mutex);
1352 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1353 dentry->d_name.name,
1354 dentry->d_name.len);
1355 if (IS_ERR(*sub_root))
1356 return PTR_ERR(*sub_root);
1358 ri = &(*sub_root)->root_item;
1359 location->objectid = btrfs_root_dirid(ri);
1360 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1361 location->offset = 0;
1363 btrfs_free_path(path);
1364 mutex_unlock(&root->fs_info->fs_mutex);
1368 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1370 struct btrfs_iget_args *args = p;
1371 inode->i_ino = args->ino;
1372 BTRFS_I(inode)->root = args->root;
1373 BTRFS_I(inode)->delalloc_bytes = 0;
1374 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1375 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1376 inode->i_mapping, GFP_NOFS);
1377 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1378 inode->i_mapping, GFP_NOFS);
1382 static int btrfs_find_actor(struct inode *inode, void *opaque)
1384 struct btrfs_iget_args *args = opaque;
1385 return (args->ino == inode->i_ino &&
1386 args->root == BTRFS_I(inode)->root);
1389 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1392 struct btrfs_iget_args args;
1393 args.ino = objectid;
1394 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1399 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1402 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1403 struct btrfs_root *root)
1405 struct inode *inode;
1406 struct btrfs_iget_args args;
1407 args.ino = objectid;
1410 inode = iget5_locked(s, objectid, btrfs_find_actor,
1411 btrfs_init_locked_inode,
1416 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1417 struct nameidata *nd)
1419 struct inode * inode;
1420 struct btrfs_inode *bi = BTRFS_I(dir);
1421 struct btrfs_root *root = bi->root;
1422 struct btrfs_root *sub_root = root;
1423 struct btrfs_key location;
1426 if (dentry->d_name.len > BTRFS_NAME_LEN)
1427 return ERR_PTR(-ENAMETOOLONG);
1429 mutex_lock(&root->fs_info->fs_mutex);
1430 ret = btrfs_inode_by_name(dir, dentry, &location);
1431 mutex_unlock(&root->fs_info->fs_mutex);
1434 return ERR_PTR(ret);
1437 if (location.objectid) {
1438 ret = fixup_tree_root_location(root, &location, &sub_root,
1441 return ERR_PTR(ret);
1443 return ERR_PTR(-ENOENT);
1444 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1447 return ERR_PTR(-EACCES);
1448 if (inode->i_state & I_NEW) {
1449 /* the inode and parent dir are two different roots */
1450 if (sub_root != root) {
1452 sub_root->inode = inode;
1454 BTRFS_I(inode)->root = sub_root;
1455 memcpy(&BTRFS_I(inode)->location, &location,
1457 btrfs_read_locked_inode(inode);
1458 unlock_new_inode(inode);
1461 return d_splice_alias(inode, dentry);
1464 static unsigned char btrfs_filetype_table[] = {
1465 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1468 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1470 struct inode *inode = filp->f_dentry->d_inode;
1471 struct btrfs_root *root = BTRFS_I(inode)->root;
1472 struct btrfs_item *item;
1473 struct btrfs_dir_item *di;
1474 struct btrfs_key key;
1475 struct btrfs_key found_key;
1476 struct btrfs_path *path;
1479 struct extent_buffer *leaf;
1482 unsigned char d_type;
1487 int key_type = BTRFS_DIR_INDEX_KEY;
1492 /* FIXME, use a real flag for deciding about the key type */
1493 if (root->fs_info->tree_root == root)
1494 key_type = BTRFS_DIR_ITEM_KEY;
1496 /* special case for "." */
1497 if (filp->f_pos == 0) {
1498 over = filldir(dirent, ".", 1,
1506 mutex_lock(&root->fs_info->fs_mutex);
1507 key.objectid = inode->i_ino;
1508 path = btrfs_alloc_path();
1511 /* special case for .., just use the back ref */
1512 if (filp->f_pos == 1) {
1513 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1515 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1517 leaf = path->nodes[0];
1518 slot = path->slots[0];
1519 nritems = btrfs_header_nritems(leaf);
1520 if (slot >= nritems) {
1521 btrfs_release_path(root, path);
1522 goto read_dir_items;
1524 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1525 btrfs_release_path(root, path);
1526 if (found_key.objectid != key.objectid ||
1527 found_key.type != BTRFS_INODE_REF_KEY)
1528 goto read_dir_items;
1529 over = filldir(dirent, "..", 2,
1530 2, found_key.offset, DT_DIR);
1537 btrfs_set_key_type(&key, key_type);
1538 key.offset = filp->f_pos;
1540 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1545 leaf = path->nodes[0];
1546 nritems = btrfs_header_nritems(leaf);
1547 slot = path->slots[0];
1548 if (advance || slot >= nritems) {
1549 if (slot >= nritems -1) {
1550 ret = btrfs_next_leaf(root, path);
1553 leaf = path->nodes[0];
1554 nritems = btrfs_header_nritems(leaf);
1555 slot = path->slots[0];
1562 item = btrfs_item_nr(leaf, slot);
1563 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1565 if (found_key.objectid != key.objectid)
1567 if (btrfs_key_type(&found_key) != key_type)
1569 if (found_key.offset < filp->f_pos)
1572 filp->f_pos = found_key.offset;
1574 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1576 di_total = btrfs_item_size(leaf, item);
1577 while(di_cur < di_total) {
1578 struct btrfs_key location;
1580 name_len = btrfs_dir_name_len(leaf, di);
1581 if (name_len < 32) {
1582 name_ptr = tmp_name;
1584 name_ptr = kmalloc(name_len, GFP_NOFS);
1587 read_extent_buffer(leaf, name_ptr,
1588 (unsigned long)(di + 1), name_len);
1590 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1591 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1592 over = filldir(dirent, name_ptr, name_len,
1597 if (name_ptr != tmp_name)
1602 di_len = btrfs_dir_name_len(leaf, di) +
1603 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1605 di = (struct btrfs_dir_item *)((char *)di + di_len);
1608 if (key_type == BTRFS_DIR_INDEX_KEY)
1609 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1615 btrfs_release_path(root, path);
1616 btrfs_free_path(path);
1617 mutex_unlock(&root->fs_info->fs_mutex);
1621 int btrfs_write_inode(struct inode *inode, int wait)
1623 struct btrfs_root *root = BTRFS_I(inode)->root;
1624 struct btrfs_trans_handle *trans;
1628 mutex_lock(&root->fs_info->fs_mutex);
1629 trans = btrfs_start_transaction(root, 1);
1630 btrfs_set_trans_block_group(trans, inode);
1631 ret = btrfs_commit_transaction(trans, root);
1632 mutex_unlock(&root->fs_info->fs_mutex);
1638 * This is somewhat expensive, updating the tree every time the
1639 * inode changes. But, it is most likely to find the inode in cache.
1640 * FIXME, needs more benchmarking...there are no reasons other than performance
1641 * to keep or drop this code.
1643 void btrfs_dirty_inode(struct inode *inode)
1645 struct btrfs_root *root = BTRFS_I(inode)->root;
1646 struct btrfs_trans_handle *trans;
1648 mutex_lock(&root->fs_info->fs_mutex);
1649 trans = btrfs_start_transaction(root, 1);
1650 btrfs_set_trans_block_group(trans, inode);
1651 btrfs_update_inode(trans, root, inode);
1652 btrfs_end_transaction(trans, root);
1653 mutex_unlock(&root->fs_info->fs_mutex);
1656 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 const char *name, int name_len,
1661 struct btrfs_block_group_cache *group,
1664 struct inode *inode;
1665 struct btrfs_inode_item *inode_item;
1666 struct btrfs_block_group_cache *new_inode_group;
1667 struct btrfs_key *location;
1668 struct btrfs_path *path;
1669 struct btrfs_inode_ref *ref;
1670 struct btrfs_key key[2];
1676 path = btrfs_alloc_path();
1679 inode = new_inode(root->fs_info->sb);
1681 return ERR_PTR(-ENOMEM);
1683 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1684 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1685 inode->i_mapping, GFP_NOFS);
1686 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1687 inode->i_mapping, GFP_NOFS);
1688 BTRFS_I(inode)->delalloc_bytes = 0;
1689 BTRFS_I(inode)->root = root;
1695 new_inode_group = btrfs_find_block_group(root, group, 0,
1696 BTRFS_BLOCK_GROUP_METADATA, owner);
1697 if (!new_inode_group) {
1698 printk("find_block group failed\n");
1699 new_inode_group = group;
1701 BTRFS_I(inode)->block_group = new_inode_group;
1702 BTRFS_I(inode)->flags = 0;
1704 key[0].objectid = objectid;
1705 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1708 key[1].objectid = objectid;
1709 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1710 key[1].offset = ref_objectid;
1712 sizes[0] = sizeof(struct btrfs_inode_item);
1713 sizes[1] = name_len + sizeof(*ref);
1715 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1719 if (objectid > root->highest_inode)
1720 root->highest_inode = objectid;
1722 inode->i_uid = current->fsuid;
1723 inode->i_gid = current->fsgid;
1724 inode->i_mode = mode;
1725 inode->i_ino = objectid;
1726 inode->i_blocks = 0;
1727 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1728 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1729 struct btrfs_inode_item);
1730 fill_inode_item(path->nodes[0], inode_item, inode);
1732 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1733 struct btrfs_inode_ref);
1734 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1735 ptr = (unsigned long)(ref + 1);
1736 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1738 btrfs_mark_buffer_dirty(path->nodes[0]);
1739 btrfs_free_path(path);
1741 location = &BTRFS_I(inode)->location;
1742 location->objectid = objectid;
1743 location->offset = 0;
1744 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1746 insert_inode_hash(inode);
1749 btrfs_free_path(path);
1750 return ERR_PTR(ret);
1753 static inline u8 btrfs_inode_type(struct inode *inode)
1755 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1758 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1759 struct dentry *dentry, struct inode *inode,
1763 struct btrfs_key key;
1764 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1765 struct inode *parent_inode;
1767 key.objectid = inode->i_ino;
1768 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1771 ret = btrfs_insert_dir_item(trans, root,
1772 dentry->d_name.name, dentry->d_name.len,
1773 dentry->d_parent->d_inode->i_ino,
1774 &key, btrfs_inode_type(inode));
1777 ret = btrfs_insert_inode_ref(trans, root,
1778 dentry->d_name.name,
1781 dentry->d_parent->d_inode->i_ino);
1783 parent_inode = dentry->d_parent->d_inode;
1784 parent_inode->i_size += dentry->d_name.len * 2;
1785 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1786 ret = btrfs_update_inode(trans, root,
1787 dentry->d_parent->d_inode);
1792 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1793 struct dentry *dentry, struct inode *inode,
1796 int err = btrfs_add_link(trans, dentry, inode, backref);
1798 d_instantiate(dentry, inode);
1806 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1807 int mode, dev_t rdev)
1809 struct btrfs_trans_handle *trans;
1810 struct btrfs_root *root = BTRFS_I(dir)->root;
1811 struct inode *inode = NULL;
1815 unsigned long nr = 0;
1817 if (!new_valid_dev(rdev))
1820 mutex_lock(&root->fs_info->fs_mutex);
1821 err = btrfs_check_free_space(root, 1, 0);
1825 trans = btrfs_start_transaction(root, 1);
1826 btrfs_set_trans_block_group(trans, dir);
1828 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1834 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1836 dentry->d_parent->d_inode->i_ino, objectid,
1837 BTRFS_I(dir)->block_group, mode);
1838 err = PTR_ERR(inode);
1842 btrfs_set_trans_block_group(trans, inode);
1843 err = btrfs_add_nondir(trans, dentry, inode, 0);
1847 inode->i_op = &btrfs_special_inode_operations;
1848 init_special_inode(inode, inode->i_mode, rdev);
1849 btrfs_update_inode(trans, root, inode);
1851 dir->i_sb->s_dirt = 1;
1852 btrfs_update_inode_block_group(trans, inode);
1853 btrfs_update_inode_block_group(trans, dir);
1855 nr = trans->blocks_used;
1856 btrfs_end_transaction(trans, root);
1858 mutex_unlock(&root->fs_info->fs_mutex);
1861 inode_dec_link_count(inode);
1864 btrfs_btree_balance_dirty(root, nr);
1865 btrfs_throttle(root);
1869 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1870 int mode, struct nameidata *nd)
1872 struct btrfs_trans_handle *trans;
1873 struct btrfs_root *root = BTRFS_I(dir)->root;
1874 struct inode *inode = NULL;
1877 unsigned long nr = 0;
1880 mutex_lock(&root->fs_info->fs_mutex);
1881 err = btrfs_check_free_space(root, 1, 0);
1884 trans = btrfs_start_transaction(root, 1);
1885 btrfs_set_trans_block_group(trans, dir);
1887 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1893 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1895 dentry->d_parent->d_inode->i_ino,
1896 objectid, BTRFS_I(dir)->block_group, mode);
1897 err = PTR_ERR(inode);
1901 btrfs_set_trans_block_group(trans, inode);
1902 err = btrfs_add_nondir(trans, dentry, inode, 0);
1906 inode->i_mapping->a_ops = &btrfs_aops;
1907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1908 inode->i_fop = &btrfs_file_operations;
1909 inode->i_op = &btrfs_file_inode_operations;
1910 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1911 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1912 inode->i_mapping, GFP_NOFS);
1913 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1914 inode->i_mapping, GFP_NOFS);
1915 BTRFS_I(inode)->delalloc_bytes = 0;
1916 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1918 dir->i_sb->s_dirt = 1;
1919 btrfs_update_inode_block_group(trans, inode);
1920 btrfs_update_inode_block_group(trans, dir);
1922 nr = trans->blocks_used;
1923 btrfs_end_transaction(trans, root);
1925 mutex_unlock(&root->fs_info->fs_mutex);
1928 inode_dec_link_count(inode);
1931 btrfs_btree_balance_dirty(root, nr);
1932 btrfs_throttle(root);
1936 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1937 struct dentry *dentry)
1939 struct btrfs_trans_handle *trans;
1940 struct btrfs_root *root = BTRFS_I(dir)->root;
1941 struct inode *inode = old_dentry->d_inode;
1942 unsigned long nr = 0;
1946 if (inode->i_nlink == 0)
1949 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1954 mutex_lock(&root->fs_info->fs_mutex);
1955 err = btrfs_check_free_space(root, 1, 0);
1958 trans = btrfs_start_transaction(root, 1);
1960 btrfs_set_trans_block_group(trans, dir);
1961 atomic_inc(&inode->i_count);
1962 err = btrfs_add_nondir(trans, dentry, inode, 1);
1967 dir->i_sb->s_dirt = 1;
1968 btrfs_update_inode_block_group(trans, dir);
1969 err = btrfs_update_inode(trans, root, inode);
1974 nr = trans->blocks_used;
1975 btrfs_end_transaction(trans, root);
1977 mutex_unlock(&root->fs_info->fs_mutex);
1980 inode_dec_link_count(inode);
1983 btrfs_btree_balance_dirty(root, nr);
1984 btrfs_throttle(root);
1988 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1990 struct inode *inode;
1991 struct btrfs_trans_handle *trans;
1992 struct btrfs_root *root = BTRFS_I(dir)->root;
1994 int drop_on_err = 0;
1996 unsigned long nr = 1;
1998 mutex_lock(&root->fs_info->fs_mutex);
1999 err = btrfs_check_free_space(root, 1, 0);
2003 trans = btrfs_start_transaction(root, 1);
2004 btrfs_set_trans_block_group(trans, dir);
2006 if (IS_ERR(trans)) {
2007 err = PTR_ERR(trans);
2011 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2017 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2019 dentry->d_parent->d_inode->i_ino, objectid,
2020 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2021 if (IS_ERR(inode)) {
2022 err = PTR_ERR(inode);
2027 inode->i_op = &btrfs_dir_inode_operations;
2028 inode->i_fop = &btrfs_dir_file_operations;
2029 btrfs_set_trans_block_group(trans, inode);
2032 err = btrfs_update_inode(trans, root, inode);
2036 err = btrfs_add_link(trans, dentry, inode, 0);
2040 d_instantiate(dentry, inode);
2042 dir->i_sb->s_dirt = 1;
2043 btrfs_update_inode_block_group(trans, inode);
2044 btrfs_update_inode_block_group(trans, dir);
2047 nr = trans->blocks_used;
2048 btrfs_end_transaction(trans, root);
2051 mutex_unlock(&root->fs_info->fs_mutex);
2054 btrfs_btree_balance_dirty(root, nr);
2055 btrfs_throttle(root);
2059 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2060 size_t pg_offset, u64 start, u64 len,
2066 u64 extent_start = 0;
2068 u64 objectid = inode->i_ino;
2070 struct btrfs_path *path;
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 struct btrfs_file_extent_item *item;
2073 struct extent_buffer *leaf;
2074 struct btrfs_key found_key;
2075 struct extent_map *em = NULL;
2076 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2077 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2078 struct btrfs_trans_handle *trans = NULL;
2080 path = btrfs_alloc_path();
2082 mutex_lock(&root->fs_info->fs_mutex);
2085 spin_lock(&em_tree->lock);
2086 em = lookup_extent_mapping(em_tree, start, len);
2087 spin_unlock(&em_tree->lock);
2090 if (em->start > start) {
2091 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2092 start, len, em->start, em->len);
2095 if (em->block_start == EXTENT_MAP_INLINE && page)
2096 free_extent_map(em);
2100 em = alloc_extent_map(GFP_NOFS);
2106 em->start = EXTENT_MAP_HOLE;
2108 em->bdev = inode->i_sb->s_bdev;
2109 ret = btrfs_lookup_file_extent(trans, root, path,
2110 objectid, start, trans != NULL);
2117 if (path->slots[0] == 0)
2122 leaf = path->nodes[0];
2123 item = btrfs_item_ptr(leaf, path->slots[0],
2124 struct btrfs_file_extent_item);
2125 /* are we inside the extent that was found? */
2126 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2127 found_type = btrfs_key_type(&found_key);
2128 if (found_key.objectid != objectid ||
2129 found_type != BTRFS_EXTENT_DATA_KEY) {
2133 found_type = btrfs_file_extent_type(leaf, item);
2134 extent_start = found_key.offset;
2135 if (found_type == BTRFS_FILE_EXTENT_REG) {
2136 extent_end = extent_start +
2137 btrfs_file_extent_num_bytes(leaf, item);
2139 if (start < extent_start || start >= extent_end) {
2141 if (start < extent_start) {
2142 if (start + len <= extent_start)
2144 em->len = extent_end - extent_start;
2150 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2152 em->start = extent_start;
2153 em->len = extent_end - extent_start;
2154 em->block_start = EXTENT_MAP_HOLE;
2157 bytenr += btrfs_file_extent_offset(leaf, item);
2158 em->block_start = bytenr;
2159 em->start = extent_start;
2160 em->len = extent_end - extent_start;
2162 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2167 size_t extent_offset;
2170 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2172 extent_end = (extent_start + size + root->sectorsize - 1) &
2173 ~((u64)root->sectorsize - 1);
2174 if (start < extent_start || start >= extent_end) {
2176 if (start < extent_start) {
2177 if (start + len <= extent_start)
2179 em->len = extent_end - extent_start;
2185 em->block_start = EXTENT_MAP_INLINE;
2188 em->start = extent_start;
2193 page_start = page_offset(page) + pg_offset;
2194 extent_offset = page_start - extent_start;
2195 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2196 size - extent_offset);
2197 em->start = extent_start + extent_offset;
2198 em->len = (copy_size + root->sectorsize - 1) &
2199 ~((u64)root->sectorsize - 1);
2201 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2202 if (create == 0 && !PageUptodate(page)) {
2203 read_extent_buffer(leaf, map + pg_offset, ptr,
2205 flush_dcache_page(page);
2206 } else if (create && PageUptodate(page)) {
2209 free_extent_map(em);
2211 btrfs_release_path(root, path);
2212 trans = btrfs_start_transaction(root, 1);
2215 write_extent_buffer(leaf, map + pg_offset, ptr,
2217 btrfs_mark_buffer_dirty(leaf);
2220 set_extent_uptodate(io_tree, em->start,
2221 extent_map_end(em) - 1, GFP_NOFS);
2224 printk("unkknown found_type %d\n", found_type);
2231 em->block_start = EXTENT_MAP_HOLE;
2233 btrfs_release_path(root, path);
2234 if (em->start > start || extent_map_end(em) <= start) {
2235 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2241 spin_lock(&em_tree->lock);
2242 ret = add_extent_mapping(em_tree, em);
2243 if (ret == -EEXIST) {
2244 free_extent_map(em);
2245 em = lookup_extent_mapping(em_tree, start, len);
2248 printk("failing to insert %Lu %Lu\n", start, len);
2251 spin_unlock(&em_tree->lock);
2253 btrfs_free_path(path);
2255 ret = btrfs_end_transaction(trans, root);
2259 mutex_unlock(&root->fs_info->fs_mutex);
2261 free_extent_map(em);
2263 return ERR_PTR(err);
2268 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2270 return extent_bmap(mapping, iblock, btrfs_get_extent);
2273 int btrfs_readpage(struct file *file, struct page *page)
2275 struct extent_io_tree *tree;
2276 tree = &BTRFS_I(page->mapping->host)->io_tree;
2277 return extent_read_full_page(tree, page, btrfs_get_extent);
2280 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2282 struct extent_io_tree *tree;
2285 if (current->flags & PF_MEMALLOC) {
2286 redirty_page_for_writepage(wbc, page);
2290 tree = &BTRFS_I(page->mapping->host)->io_tree;
2291 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2294 static int btrfs_writepages(struct address_space *mapping,
2295 struct writeback_control *wbc)
2297 struct extent_io_tree *tree;
2298 tree = &BTRFS_I(mapping->host)->io_tree;
2299 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2303 btrfs_readpages(struct file *file, struct address_space *mapping,
2304 struct list_head *pages, unsigned nr_pages)
2306 struct extent_io_tree *tree;
2307 tree = &BTRFS_I(mapping->host)->io_tree;
2308 return extent_readpages(tree, mapping, pages, nr_pages,
2312 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2314 struct extent_io_tree *tree;
2315 struct extent_map_tree *map;
2318 tree = &BTRFS_I(page->mapping->host)->io_tree;
2319 map = &BTRFS_I(page->mapping->host)->extent_tree;
2320 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2322 ClearPagePrivate(page);
2323 set_page_private(page, 0);
2324 page_cache_release(page);
2329 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2331 struct extent_io_tree *tree;
2333 tree = &BTRFS_I(page->mapping->host)->io_tree;
2334 extent_invalidatepage(tree, page, offset);
2335 btrfs_releasepage(page, GFP_NOFS);
2339 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2340 * called from a page fault handler when a page is first dirtied. Hence we must
2341 * be careful to check for EOF conditions here. We set the page up correctly
2342 * for a written page which means we get ENOSPC checking when writing into
2343 * holes and correct delalloc and unwritten extent mapping on filesystems that
2344 * support these features.
2346 * We are not allowed to take the i_mutex here so we have to play games to
2347 * protect against truncate races as the page could now be beyond EOF. Because
2348 * vmtruncate() writes the inode size before removing pages, once we have the
2349 * page lock we can determine safely if the page is beyond EOF. If it is not
2350 * beyond EOF, then the page is guaranteed safe against truncation until we
2353 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2355 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2356 struct btrfs_root *root = BTRFS_I(inode)->root;
2362 mutex_lock(&root->fs_info->fs_mutex);
2363 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2364 mutex_unlock(&root->fs_info->fs_mutex);
2371 wait_on_page_writeback(page);
2372 size = i_size_read(inode);
2373 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2375 if ((page->mapping != inode->i_mapping) ||
2376 (page_start > size)) {
2377 /* page got truncated out from underneath us */
2381 /* page is wholly or partially inside EOF */
2382 if (page_start + PAGE_CACHE_SIZE > size)
2383 end = size & ~PAGE_CACHE_MASK;
2385 end = PAGE_CACHE_SIZE;
2387 ret = btrfs_cow_one_page(inode, page, end);
2395 static void btrfs_truncate(struct inode *inode)
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2399 struct btrfs_trans_handle *trans;
2402 if (!S_ISREG(inode->i_mode))
2404 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2407 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2409 mutex_lock(&root->fs_info->fs_mutex);
2410 trans = btrfs_start_transaction(root, 1);
2411 btrfs_set_trans_block_group(trans, inode);
2413 /* FIXME, add redo link to tree so we don't leak on crash */
2414 ret = btrfs_truncate_in_trans(trans, root, inode,
2415 BTRFS_EXTENT_DATA_KEY);
2416 btrfs_update_inode(trans, root, inode);
2417 nr = trans->blocks_used;
2419 ret = btrfs_end_transaction(trans, root);
2421 mutex_unlock(&root->fs_info->fs_mutex);
2422 btrfs_btree_balance_dirty(root, nr);
2423 btrfs_throttle(root);
2426 static int noinline create_subvol(struct btrfs_root *root, char *name,
2429 struct btrfs_trans_handle *trans;
2430 struct btrfs_key key;
2431 struct btrfs_root_item root_item;
2432 struct btrfs_inode_item *inode_item;
2433 struct extent_buffer *leaf;
2434 struct btrfs_root *new_root = root;
2435 struct inode *inode;
2440 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2441 unsigned long nr = 1;
2443 mutex_lock(&root->fs_info->fs_mutex);
2444 ret = btrfs_check_free_space(root, 1, 0);
2448 trans = btrfs_start_transaction(root, 1);
2451 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2456 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2457 objectid, trans->transid, 0, 0,
2460 return PTR_ERR(leaf);
2462 btrfs_set_header_nritems(leaf, 0);
2463 btrfs_set_header_level(leaf, 0);
2464 btrfs_set_header_bytenr(leaf, leaf->start);
2465 btrfs_set_header_generation(leaf, trans->transid);
2466 btrfs_set_header_owner(leaf, objectid);
2468 write_extent_buffer(leaf, root->fs_info->fsid,
2469 (unsigned long)btrfs_header_fsid(leaf),
2471 btrfs_mark_buffer_dirty(leaf);
2473 inode_item = &root_item.inode;
2474 memset(inode_item, 0, sizeof(*inode_item));
2475 inode_item->generation = cpu_to_le64(1);
2476 inode_item->size = cpu_to_le64(3);
2477 inode_item->nlink = cpu_to_le32(1);
2478 inode_item->nblocks = cpu_to_le64(1);
2479 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2481 btrfs_set_root_bytenr(&root_item, leaf->start);
2482 btrfs_set_root_level(&root_item, 0);
2483 btrfs_set_root_refs(&root_item, 1);
2484 btrfs_set_root_used(&root_item, 0);
2486 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2487 root_item.drop_level = 0;
2489 free_extent_buffer(leaf);
2492 btrfs_set_root_dirid(&root_item, new_dirid);
2494 key.objectid = objectid;
2496 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2497 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2503 * insert the directory item
2505 key.offset = (u64)-1;
2506 dir = root->fs_info->sb->s_root->d_inode;
2507 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2508 name, namelen, dir->i_ino, &key,
2513 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2514 name, namelen, objectid,
2515 root->fs_info->sb->s_root->d_inode->i_ino);
2519 ret = btrfs_commit_transaction(trans, root);
2523 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2526 trans = btrfs_start_transaction(new_root, 1);
2529 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2531 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2534 inode->i_op = &btrfs_dir_inode_operations;
2535 inode->i_fop = &btrfs_dir_file_operations;
2536 new_root->inode = inode;
2538 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2542 ret = btrfs_update_inode(trans, new_root, inode);
2546 nr = trans->blocks_used;
2547 err = btrfs_commit_transaction(trans, new_root);
2551 mutex_unlock(&root->fs_info->fs_mutex);
2552 btrfs_btree_balance_dirty(root, nr);
2553 btrfs_throttle(root);
2557 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2559 struct btrfs_pending_snapshot *pending_snapshot;
2560 struct btrfs_trans_handle *trans;
2563 unsigned long nr = 0;
2565 if (!root->ref_cows)
2568 mutex_lock(&root->fs_info->fs_mutex);
2569 ret = btrfs_check_free_space(root, 1, 0);
2573 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2574 if (!pending_snapshot) {
2578 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2579 if (!pending_snapshot->name) {
2581 kfree(pending_snapshot);
2584 memcpy(pending_snapshot->name, name, namelen);
2585 pending_snapshot->name[namelen] = '\0';
2586 trans = btrfs_start_transaction(root, 1);
2588 pending_snapshot->root = root;
2589 list_add(&pending_snapshot->list,
2590 &trans->transaction->pending_snapshots);
2591 ret = btrfs_update_inode(trans, root, root->inode);
2592 err = btrfs_commit_transaction(trans, root);
2595 mutex_unlock(&root->fs_info->fs_mutex);
2596 btrfs_btree_balance_dirty(root, nr);
2597 btrfs_throttle(root);
2601 unsigned long btrfs_force_ra(struct address_space *mapping,
2602 struct file_ra_state *ra, struct file *file,
2603 pgoff_t offset, pgoff_t last_index)
2607 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2608 req_size = last_index - offset + 1;
2609 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2612 req_size = min(last_index - offset + 1, (pgoff_t)128);
2613 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2614 return offset + req_size;
2618 int btrfs_defrag_file(struct file *file) {
2619 struct inode *inode = fdentry(file)->d_inode;
2620 struct btrfs_root *root = BTRFS_I(inode)->root;
2621 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2623 unsigned long last_index;
2624 unsigned long ra_index = 0;
2630 mutex_lock(&root->fs_info->fs_mutex);
2631 ret = btrfs_check_free_space(root, inode->i_size, 0);
2632 mutex_unlock(&root->fs_info->fs_mutex);
2636 mutex_lock(&inode->i_mutex);
2637 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2638 for (i = 0; i <= last_index; i++) {
2639 if (i == ra_index) {
2640 ra_index = btrfs_force_ra(inode->i_mapping,
2642 file, ra_index, last_index);
2644 page = grab_cache_page(inode->i_mapping, i);
2647 if (!PageUptodate(page)) {
2648 btrfs_readpage(NULL, page);
2650 if (!PageUptodate(page)) {
2652 page_cache_release(page);
2656 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2657 page_end = page_start + PAGE_CACHE_SIZE - 1;
2659 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2660 set_extent_delalloc(io_tree, page_start,
2661 page_end, GFP_NOFS);
2663 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2664 set_page_dirty(page);
2666 page_cache_release(page);
2667 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2671 mutex_unlock(&inode->i_mutex);
2675 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2679 struct btrfs_ioctl_vol_args *vol_args;
2680 struct btrfs_trans_handle *trans;
2686 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2691 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2695 namelen = strlen(vol_args->name);
2696 if (namelen > BTRFS_VOL_NAME_MAX) {
2701 sizestr = vol_args->name;
2702 if (!strcmp(sizestr, "max"))
2703 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2705 if (sizestr[0] == '-') {
2708 } else if (sizestr[0] == '+') {
2712 new_size = btrfs_parse_size(sizestr);
2713 if (new_size == 0) {
2719 mutex_lock(&root->fs_info->fs_mutex);
2720 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2723 if (new_size > old_size) {
2727 new_size = old_size - new_size;
2728 } else if (mod > 0) {
2729 new_size = old_size + new_size;
2732 if (new_size < 256 * 1024 * 1024) {
2736 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2741 do_div(new_size, root->sectorsize);
2742 new_size *= root->sectorsize;
2744 printk("new size is %Lu\n", new_size);
2745 if (new_size > old_size) {
2746 trans = btrfs_start_transaction(root, 1);
2747 ret = btrfs_grow_extent_tree(trans, root, new_size);
2748 btrfs_commit_transaction(trans, root);
2750 ret = btrfs_shrink_extent_tree(root, new_size);
2754 mutex_unlock(&root->fs_info->fs_mutex);
2760 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2763 struct btrfs_ioctl_vol_args *vol_args;
2764 struct btrfs_dir_item *di;
2765 struct btrfs_path *path;
2770 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2775 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2780 namelen = strlen(vol_args->name);
2781 if (namelen > BTRFS_VOL_NAME_MAX) {
2785 if (strchr(vol_args->name, '/')) {
2790 path = btrfs_alloc_path();
2796 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2797 mutex_lock(&root->fs_info->fs_mutex);
2798 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2800 vol_args->name, namelen, 0);
2801 mutex_unlock(&root->fs_info->fs_mutex);
2802 btrfs_free_path(path);
2804 if (di && !IS_ERR(di)) {
2814 if (root == root->fs_info->tree_root)
2815 ret = create_subvol(root, vol_args->name, namelen);
2817 ret = create_snapshot(root, vol_args->name, namelen);
2823 static int btrfs_ioctl_defrag(struct file *file)
2825 struct inode *inode = fdentry(file)->d_inode;
2826 struct btrfs_root *root = BTRFS_I(inode)->root;
2828 switch (inode->i_mode & S_IFMT) {
2830 mutex_lock(&root->fs_info->fs_mutex);
2831 btrfs_defrag_root(root, 0);
2832 btrfs_defrag_root(root->fs_info->extent_root, 0);
2833 mutex_unlock(&root->fs_info->fs_mutex);
2836 btrfs_defrag_file(file);
2843 long btrfs_ioctl(struct file *file, unsigned int
2844 cmd, unsigned long arg)
2846 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2849 case BTRFS_IOC_SNAP_CREATE:
2850 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2851 case BTRFS_IOC_DEFRAG:
2852 return btrfs_ioctl_defrag(file);
2853 case BTRFS_IOC_RESIZE:
2854 return btrfs_ioctl_resize(root, (void __user *)arg);
2861 * Called inside transaction, so use GFP_NOFS
2863 struct inode *btrfs_alloc_inode(struct super_block *sb)
2865 struct btrfs_inode *ei;
2867 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2871 ei->ordered_trans = 0;
2872 return &ei->vfs_inode;
2875 void btrfs_destroy_inode(struct inode *inode)
2877 WARN_ON(!list_empty(&inode->i_dentry));
2878 WARN_ON(inode->i_data.nrpages);
2880 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2881 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2884 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2885 static void init_once(struct kmem_cache * cachep, void *foo)
2887 static void init_once(void * foo, struct kmem_cache * cachep,
2888 unsigned long flags)
2891 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2893 inode_init_once(&ei->vfs_inode);
2896 void btrfs_destroy_cachep(void)
2898 if (btrfs_inode_cachep)
2899 kmem_cache_destroy(btrfs_inode_cachep);
2900 if (btrfs_trans_handle_cachep)
2901 kmem_cache_destroy(btrfs_trans_handle_cachep);
2902 if (btrfs_transaction_cachep)
2903 kmem_cache_destroy(btrfs_transaction_cachep);
2904 if (btrfs_bit_radix_cachep)
2905 kmem_cache_destroy(btrfs_bit_radix_cachep);
2906 if (btrfs_path_cachep)
2907 kmem_cache_destroy(btrfs_path_cachep);
2910 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2911 unsigned long extra_flags,
2912 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2913 void (*ctor)(struct kmem_cache *, void *)
2915 void (*ctor)(void *, struct kmem_cache *,
2920 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2921 SLAB_MEM_SPREAD | extra_flags), ctor
2922 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2928 int btrfs_init_cachep(void)
2930 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2931 sizeof(struct btrfs_inode),
2933 if (!btrfs_inode_cachep)
2935 btrfs_trans_handle_cachep =
2936 btrfs_cache_create("btrfs_trans_handle_cache",
2937 sizeof(struct btrfs_trans_handle),
2939 if (!btrfs_trans_handle_cachep)
2941 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2942 sizeof(struct btrfs_transaction),
2944 if (!btrfs_transaction_cachep)
2946 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2947 sizeof(struct btrfs_path),
2949 if (!btrfs_path_cachep)
2951 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2952 SLAB_DESTROY_BY_RCU, NULL);
2953 if (!btrfs_bit_radix_cachep)
2957 btrfs_destroy_cachep();
2961 static int btrfs_getattr(struct vfsmount *mnt,
2962 struct dentry *dentry, struct kstat *stat)
2964 struct inode *inode = dentry->d_inode;
2965 generic_fillattr(inode, stat);
2966 stat->blksize = PAGE_CACHE_SIZE;
2967 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2971 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2972 struct inode * new_dir,struct dentry *new_dentry)
2974 struct btrfs_trans_handle *trans;
2975 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2976 struct inode *new_inode = new_dentry->d_inode;
2977 struct inode *old_inode = old_dentry->d_inode;
2978 struct timespec ctime = CURRENT_TIME;
2979 struct btrfs_path *path;
2982 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2983 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2987 mutex_lock(&root->fs_info->fs_mutex);
2988 ret = btrfs_check_free_space(root, 1, 0);
2992 trans = btrfs_start_transaction(root, 1);
2994 btrfs_set_trans_block_group(trans, new_dir);
2995 path = btrfs_alloc_path();
3001 old_dentry->d_inode->i_nlink++;
3002 old_dir->i_ctime = old_dir->i_mtime = ctime;
3003 new_dir->i_ctime = new_dir->i_mtime = ctime;
3004 old_inode->i_ctime = ctime;
3006 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3011 new_inode->i_ctime = CURRENT_TIME;
3012 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3016 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3021 btrfs_free_path(path);
3022 btrfs_end_transaction(trans, root);
3024 mutex_unlock(&root->fs_info->fs_mutex);
3028 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3029 const char *symname)
3031 struct btrfs_trans_handle *trans;
3032 struct btrfs_root *root = BTRFS_I(dir)->root;
3033 struct btrfs_path *path;
3034 struct btrfs_key key;
3035 struct inode *inode = NULL;
3042 struct btrfs_file_extent_item *ei;
3043 struct extent_buffer *leaf;
3044 unsigned long nr = 0;
3046 name_len = strlen(symname) + 1;
3047 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3048 return -ENAMETOOLONG;
3050 mutex_lock(&root->fs_info->fs_mutex);
3051 err = btrfs_check_free_space(root, 1, 0);
3055 trans = btrfs_start_transaction(root, 1);
3056 btrfs_set_trans_block_group(trans, dir);
3058 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3064 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3066 dentry->d_parent->d_inode->i_ino, objectid,
3067 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3068 err = PTR_ERR(inode);
3072 btrfs_set_trans_block_group(trans, inode);
3073 err = btrfs_add_nondir(trans, dentry, inode, 0);
3077 inode->i_mapping->a_ops = &btrfs_aops;
3078 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3079 inode->i_fop = &btrfs_file_operations;
3080 inode->i_op = &btrfs_file_inode_operations;
3081 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3082 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3083 inode->i_mapping, GFP_NOFS);
3084 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3085 inode->i_mapping, GFP_NOFS);
3086 BTRFS_I(inode)->delalloc_bytes = 0;
3087 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3089 dir->i_sb->s_dirt = 1;
3090 btrfs_update_inode_block_group(trans, inode);
3091 btrfs_update_inode_block_group(trans, dir);
3095 path = btrfs_alloc_path();
3097 key.objectid = inode->i_ino;
3099 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3100 datasize = btrfs_file_extent_calc_inline_size(name_len);
3101 err = btrfs_insert_empty_item(trans, root, path, &key,
3107 leaf = path->nodes[0];
3108 ei = btrfs_item_ptr(leaf, path->slots[0],
3109 struct btrfs_file_extent_item);
3110 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3111 btrfs_set_file_extent_type(leaf, ei,
3112 BTRFS_FILE_EXTENT_INLINE);
3113 ptr = btrfs_file_extent_inline_start(ei);
3114 write_extent_buffer(leaf, symname, ptr, name_len);
3115 btrfs_mark_buffer_dirty(leaf);
3116 btrfs_free_path(path);
3118 inode->i_op = &btrfs_symlink_inode_operations;
3119 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3120 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3121 inode->i_size = name_len - 1;
3122 err = btrfs_update_inode(trans, root, inode);
3127 nr = trans->blocks_used;
3128 btrfs_end_transaction(trans, root);
3130 mutex_unlock(&root->fs_info->fs_mutex);
3132 inode_dec_link_count(inode);
3135 btrfs_btree_balance_dirty(root, nr);
3136 btrfs_throttle(root);
3139 static int btrfs_permission(struct inode *inode, int mask,
3140 struct nameidata *nd)
3142 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3144 return generic_permission(inode, mask, NULL);
3147 static struct inode_operations btrfs_dir_inode_operations = {
3148 .lookup = btrfs_lookup,
3149 .create = btrfs_create,
3150 .unlink = btrfs_unlink,
3152 .mkdir = btrfs_mkdir,
3153 .rmdir = btrfs_rmdir,
3154 .rename = btrfs_rename,
3155 .symlink = btrfs_symlink,
3156 .setattr = btrfs_setattr,
3157 .mknod = btrfs_mknod,
3158 .setxattr = generic_setxattr,
3159 .getxattr = generic_getxattr,
3160 .listxattr = btrfs_listxattr,
3161 .removexattr = generic_removexattr,
3162 .permission = btrfs_permission,
3164 static struct inode_operations btrfs_dir_ro_inode_operations = {
3165 .lookup = btrfs_lookup,
3166 .permission = btrfs_permission,
3168 static struct file_operations btrfs_dir_file_operations = {
3169 .llseek = generic_file_llseek,
3170 .read = generic_read_dir,
3171 .readdir = btrfs_readdir,
3172 .unlocked_ioctl = btrfs_ioctl,
3173 #ifdef CONFIG_COMPAT
3174 .compat_ioctl = btrfs_ioctl,
3178 static struct extent_io_ops btrfs_extent_io_ops = {
3179 .fill_delalloc = run_delalloc_range,
3180 .submit_bio_hook = btrfs_submit_bio_hook,
3181 .merge_bio_hook = btrfs_merge_bio_hook,
3182 .readpage_io_hook = btrfs_readpage_io_hook,
3183 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3184 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3185 .set_bit_hook = btrfs_set_bit_hook,
3186 .clear_bit_hook = btrfs_clear_bit_hook,
3189 static struct address_space_operations btrfs_aops = {
3190 .readpage = btrfs_readpage,
3191 .writepage = btrfs_writepage,
3192 .writepages = btrfs_writepages,
3193 .readpages = btrfs_readpages,
3194 .sync_page = block_sync_page,
3196 .invalidatepage = btrfs_invalidatepage,
3197 .releasepage = btrfs_releasepage,
3198 .set_page_dirty = __set_page_dirty_nobuffers,
3201 static struct address_space_operations btrfs_symlink_aops = {
3202 .readpage = btrfs_readpage,
3203 .writepage = btrfs_writepage,
3204 .invalidatepage = btrfs_invalidatepage,
3205 .releasepage = btrfs_releasepage,
3208 static struct inode_operations btrfs_file_inode_operations = {
3209 .truncate = btrfs_truncate,
3210 .getattr = btrfs_getattr,
3211 .setattr = btrfs_setattr,
3212 .setxattr = generic_setxattr,
3213 .getxattr = generic_getxattr,
3214 .listxattr = btrfs_listxattr,
3215 .removexattr = generic_removexattr,
3216 .permission = btrfs_permission,
3218 static struct inode_operations btrfs_special_inode_operations = {
3219 .getattr = btrfs_getattr,
3220 .setattr = btrfs_setattr,
3221 .permission = btrfs_permission,
3223 static struct inode_operations btrfs_symlink_inode_operations = {
3224 .readlink = generic_readlink,
3225 .follow_link = page_follow_link_light,
3226 .put_link = page_put_link,
3227 .permission = btrfs_permission,