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,
129 root->root_key.objectid,
131 inode->i_ino, start, 0,
132 alloc_hint, (u64)-1, &ins, 1);
137 cur_alloc_size = ins.offset;
138 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
139 start, ins.objectid, ins.offset,
141 inode->i_blocks += ins.offset >> 9;
142 btrfs_check_file(root, inode);
143 num_bytes -= cur_alloc_size;
144 alloc_hint = ins.objectid + ins.offset;
145 start += cur_alloc_size;
147 btrfs_drop_extent_cache(inode, orig_start,
148 orig_start + orig_num_bytes - 1);
149 btrfs_add_ordered_inode(inode);
150 btrfs_update_inode(trans, root, inode);
152 btrfs_end_transaction(trans, root);
156 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
164 struct btrfs_root *root = BTRFS_I(inode)->root;
165 struct extent_buffer *leaf;
167 struct btrfs_path *path;
168 struct btrfs_file_extent_item *item;
171 struct btrfs_key found_key;
173 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
174 path = btrfs_alloc_path();
177 ret = btrfs_lookup_file_extent(NULL, root, path,
178 inode->i_ino, start, 0);
180 btrfs_free_path(path);
186 if (path->slots[0] == 0)
191 leaf = path->nodes[0];
192 item = btrfs_item_ptr(leaf, path->slots[0],
193 struct btrfs_file_extent_item);
195 /* are we inside the extent that was found? */
196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
197 found_type = btrfs_key_type(&found_key);
198 if (found_key.objectid != inode->i_ino ||
199 found_type != BTRFS_EXTENT_DATA_KEY) {
203 found_type = btrfs_file_extent_type(leaf, item);
204 extent_start = found_key.offset;
205 if (found_type == BTRFS_FILE_EXTENT_REG) {
206 u64 extent_num_bytes;
208 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
209 extent_end = extent_start + extent_num_bytes;
212 if (loops && start != extent_start)
215 if (start < extent_start || start >= extent_end)
218 cow_end = min(end, extent_end - 1);
219 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
224 * we may be called by the resizer, make sure we're inside
225 * the limits of the FS
227 if (bytenr + extent_num_bytes > total_fs_bytes)
230 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
240 btrfs_free_path(path);
243 btrfs_release_path(root, path);
248 cow_file_range(inode, start, cow_end);
253 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
255 struct btrfs_root *root = BTRFS_I(inode)->root;
257 mutex_lock(&root->fs_info->fs_mutex);
258 if (btrfs_test_opt(root, NODATACOW) ||
259 btrfs_test_flag(inode, NODATACOW))
260 ret = run_delalloc_nocow(inode, start, end);
262 ret = cow_file_range(inode, start, end);
264 mutex_unlock(&root->fs_info->fs_mutex);
268 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
269 unsigned long old, unsigned long bits)
271 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
272 struct btrfs_root *root = BTRFS_I(inode)->root;
273 spin_lock(&root->fs_info->delalloc_lock);
274 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
275 root->fs_info->delalloc_bytes += end - start + 1;
276 spin_unlock(&root->fs_info->delalloc_lock);
281 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
282 unsigned long old, unsigned long bits)
284 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 spin_lock(&root->fs_info->delalloc_lock);
287 if (end - start + 1 > root->fs_info->delalloc_bytes) {
288 printk("warning: delalloc account %Lu %Lu\n",
289 end - start + 1, root->fs_info->delalloc_bytes);
290 root->fs_info->delalloc_bytes = 0;
291 BTRFS_I(inode)->delalloc_bytes = 0;
293 root->fs_info->delalloc_bytes -= end - start + 1;
294 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
296 spin_unlock(&root->fs_info->delalloc_lock);
301 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
302 size_t size, struct bio *bio)
304 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
305 struct btrfs_mapping_tree *map_tree;
306 u64 logical = bio->bi_sector << 9;
309 struct bio_vec *bvec;
313 bio_for_each_segment(bvec, bio, i) {
314 length += bvec->bv_len;
316 map_tree = &root->fs_info->mapping_tree;
318 ret = btrfs_map_block(map_tree, READ, logical,
319 &map_length, NULL, 0);
321 if (map_length < length + size) {
327 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
330 struct btrfs_root *root = BTRFS_I(inode)->root;
331 struct btrfs_trans_handle *trans;
335 ret = btrfs_csum_one_bio(root, bio, &sums);
338 mutex_lock(&root->fs_info->fs_mutex);
339 trans = btrfs_start_transaction(root, 1);
341 btrfs_set_trans_block_group(trans, inode);
342 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
344 ret = btrfs_end_transaction(trans, root);
346 mutex_unlock(&root->fs_info->fs_mutex);
350 return btrfs_map_bio(root, rw, bio, mirror_num);
353 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
356 struct btrfs_root *root = BTRFS_I(inode)->root;
359 if (!(rw & (1 << BIO_RW))) {
360 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
365 if (btrfs_test_opt(root, NODATASUM) ||
366 btrfs_test_flag(inode, NODATASUM)) {
370 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
371 inode, rw, bio, mirror_num,
372 __btrfs_submit_bio_hook);
374 return btrfs_map_bio(root, rw, bio, mirror_num);
377 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
380 struct inode *inode = page->mapping->host;
381 struct btrfs_root *root = BTRFS_I(inode)->root;
382 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
383 struct btrfs_csum_item *item;
384 struct btrfs_path *path = NULL;
386 if (btrfs_test_opt(root, NODATASUM) ||
387 btrfs_test_flag(inode, NODATASUM))
389 mutex_lock(&root->fs_info->fs_mutex);
390 path = btrfs_alloc_path();
391 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
394 /* a csum that isn't present is a preallocated region. */
395 if (ret == -ENOENT || ret == -EFBIG)
398 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
401 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
403 set_state_private(io_tree, start, csum);
406 btrfs_free_path(path);
407 mutex_unlock(&root->fs_info->fs_mutex);
411 struct io_failure_record {
419 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
420 struct page *page, u64 start, u64 end,
421 struct extent_state *state)
423 struct io_failure_record *failrec = NULL;
425 struct extent_map *em;
426 struct inode *inode = page->mapping->host;
427 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
433 ret = get_state_private(failure_tree, start, &private);
435 size_t pg_offset = start - page_offset(page);
436 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
439 failrec->start = start;
440 failrec->len = end - start + 1;
441 failrec->last_mirror = 0;
443 em = btrfs_get_extent(inode, NULL, pg_offset, start,
446 if (!em || IS_ERR(em)) {
450 logical = start - em->start;
451 logical = em->block_start + logical;
452 failrec->logical = logical;
454 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
455 EXTENT_DIRTY, GFP_NOFS);
456 set_state_private(failure_tree, start,
457 (u64)(unsigned long)failrec);
459 failrec = (struct io_failure_record *)(unsigned long)private;
461 num_copies = btrfs_num_copies(
462 &BTRFS_I(inode)->root->fs_info->mapping_tree,
463 failrec->logical, failrec->len);
464 failrec->last_mirror++;
466 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
467 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
470 if (state && state->start != failrec->start)
472 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
474 if (!state || failrec->last_mirror > num_copies) {
475 set_state_private(failure_tree, failrec->start, 0);
476 clear_extent_bits(failure_tree, failrec->start,
477 failrec->start + failrec->len - 1,
478 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
482 bio = bio_alloc(GFP_NOFS, 1);
483 bio->bi_private = state;
484 bio->bi_end_io = failed_bio->bi_end_io;
485 bio->bi_sector = failrec->logical >> 9;
486 bio->bi_bdev = failed_bio->bi_bdev;
487 bio_add_page(bio, page, failrec->len, start - page_offset(page));
488 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
492 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
493 struct extent_state *state)
495 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
496 struct inode *inode = page->mapping->host;
497 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
499 u64 private = ~(u32)0;
501 struct btrfs_root *root = BTRFS_I(inode)->root;
505 if (btrfs_test_opt(root, NODATASUM) ||
506 btrfs_test_flag(inode, NODATASUM))
508 if (state && state->start == start) {
509 private = state->private;
512 ret = get_state_private(io_tree, start, &private);
514 local_irq_save(flags);
515 kaddr = kmap_atomic(page, KM_IRQ0);
519 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
520 btrfs_csum_final(csum, (char *)&csum);
521 if (csum != private) {
524 kunmap_atomic(kaddr, KM_IRQ0);
525 local_irq_restore(flags);
527 /* if the io failure tree for this inode is non-empty,
528 * check to see if we've recovered from a failed IO
531 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
532 (u64)-1, 1, EXTENT_DIRTY)) {
534 struct io_failure_record *failure;
535 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
536 start, &private_failure);
538 failure = (struct io_failure_record *)(unsigned long)
540 set_state_private(&BTRFS_I(inode)->io_failure_tree,
542 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
544 failure->start + failure->len - 1,
545 EXTENT_DIRTY | EXTENT_LOCKED,
553 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
554 page->mapping->host->i_ino, (unsigned long long)start, csum,
556 memset(kaddr + offset, 1, end - start + 1);
557 flush_dcache_page(page);
558 kunmap_atomic(kaddr, KM_IRQ0);
559 local_irq_restore(flags);
563 void btrfs_read_locked_inode(struct inode *inode)
565 struct btrfs_path *path;
566 struct extent_buffer *leaf;
567 struct btrfs_inode_item *inode_item;
568 struct btrfs_timespec *tspec;
569 struct btrfs_root *root = BTRFS_I(inode)->root;
570 struct btrfs_key location;
571 u64 alloc_group_block;
575 path = btrfs_alloc_path();
577 mutex_lock(&root->fs_info->fs_mutex);
578 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
580 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
584 leaf = path->nodes[0];
585 inode_item = btrfs_item_ptr(leaf, path->slots[0],
586 struct btrfs_inode_item);
588 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
589 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
590 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
591 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
592 inode->i_size = btrfs_inode_size(leaf, inode_item);
594 tspec = btrfs_inode_atime(inode_item);
595 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
596 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
598 tspec = btrfs_inode_mtime(inode_item);
599 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
600 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
602 tspec = btrfs_inode_ctime(inode_item);
603 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
604 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
606 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
607 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
609 rdev = btrfs_inode_rdev(leaf, inode_item);
611 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
612 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
614 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
615 if (!BTRFS_I(inode)->block_group) {
616 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
618 BTRFS_BLOCK_GROUP_METADATA, 0);
620 btrfs_free_path(path);
623 mutex_unlock(&root->fs_info->fs_mutex);
625 switch (inode->i_mode & S_IFMT) {
627 inode->i_mapping->a_ops = &btrfs_aops;
628 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
629 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
630 inode->i_fop = &btrfs_file_operations;
631 inode->i_op = &btrfs_file_inode_operations;
634 inode->i_fop = &btrfs_dir_file_operations;
635 if (root == root->fs_info->tree_root)
636 inode->i_op = &btrfs_dir_ro_inode_operations;
638 inode->i_op = &btrfs_dir_inode_operations;
641 inode->i_op = &btrfs_symlink_inode_operations;
642 inode->i_mapping->a_ops = &btrfs_symlink_aops;
643 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
646 init_special_inode(inode, inode->i_mode, rdev);
652 btrfs_release_path(root, path);
653 btrfs_free_path(path);
654 mutex_unlock(&root->fs_info->fs_mutex);
655 make_bad_inode(inode);
658 static void fill_inode_item(struct extent_buffer *leaf,
659 struct btrfs_inode_item *item,
662 btrfs_set_inode_uid(leaf, item, inode->i_uid);
663 btrfs_set_inode_gid(leaf, item, inode->i_gid);
664 btrfs_set_inode_size(leaf, item, inode->i_size);
665 btrfs_set_inode_mode(leaf, item, inode->i_mode);
666 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
668 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
669 inode->i_atime.tv_sec);
670 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
671 inode->i_atime.tv_nsec);
673 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
674 inode->i_mtime.tv_sec);
675 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
676 inode->i_mtime.tv_nsec);
678 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
679 inode->i_ctime.tv_sec);
680 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
681 inode->i_ctime.tv_nsec);
683 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
684 btrfs_set_inode_generation(leaf, item, inode->i_generation);
685 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
686 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
687 btrfs_set_inode_block_group(leaf, item,
688 BTRFS_I(inode)->block_group->key.objectid);
691 int btrfs_update_inode(struct btrfs_trans_handle *trans,
692 struct btrfs_root *root,
695 struct btrfs_inode_item *inode_item;
696 struct btrfs_path *path;
697 struct extent_buffer *leaf;
700 path = btrfs_alloc_path();
702 ret = btrfs_lookup_inode(trans, root, path,
703 &BTRFS_I(inode)->location, 1);
710 leaf = path->nodes[0];
711 inode_item = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_inode_item);
714 fill_inode_item(leaf, inode_item, inode);
715 btrfs_mark_buffer_dirty(leaf);
716 btrfs_set_inode_last_trans(trans, inode);
719 btrfs_release_path(root, path);
720 btrfs_free_path(path);
725 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
728 struct dentry *dentry)
730 struct btrfs_path *path;
731 const char *name = dentry->d_name.name;
732 int name_len = dentry->d_name.len;
734 struct extent_buffer *leaf;
735 struct btrfs_dir_item *di;
736 struct btrfs_key key;
738 path = btrfs_alloc_path();
744 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
754 leaf = path->nodes[0];
755 btrfs_dir_item_key_to_cpu(leaf, di, &key);
756 ret = btrfs_delete_one_dir_name(trans, root, path, di);
759 btrfs_release_path(root, path);
761 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
762 key.objectid, name, name_len, -1);
771 ret = btrfs_delete_one_dir_name(trans, root, path, di);
773 dentry->d_inode->i_ctime = dir->i_ctime;
774 ret = btrfs_del_inode_ref(trans, root, name, name_len,
775 dentry->d_inode->i_ino,
776 dentry->d_parent->d_inode->i_ino);
778 printk("failed to delete reference to %.*s, "
779 "inode %lu parent %lu\n", name_len, name,
780 dentry->d_inode->i_ino,
781 dentry->d_parent->d_inode->i_ino);
784 btrfs_free_path(path);
786 dir->i_size -= name_len * 2;
787 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
788 btrfs_update_inode(trans, root, dir);
789 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
790 dentry->d_inode->i_nlink--;
792 drop_nlink(dentry->d_inode);
794 ret = btrfs_update_inode(trans, root, dentry->d_inode);
795 dir->i_sb->s_dirt = 1;
800 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
802 struct btrfs_root *root;
803 struct btrfs_trans_handle *trans;
804 struct inode *inode = dentry->d_inode;
806 unsigned long nr = 0;
808 root = BTRFS_I(dir)->root;
809 mutex_lock(&root->fs_info->fs_mutex);
811 ret = btrfs_check_free_space(root, 1, 1);
815 trans = btrfs_start_transaction(root, 1);
817 btrfs_set_trans_block_group(trans, dir);
818 ret = btrfs_unlink_trans(trans, root, dir, dentry);
819 nr = trans->blocks_used;
821 if (inode->i_nlink == 0) {
823 /* if the inode isn't linked anywhere,
824 * we don't need to worry about
827 found = btrfs_del_ordered_inode(inode);
829 atomic_dec(&inode->i_count);
833 btrfs_end_transaction(trans, root);
835 mutex_unlock(&root->fs_info->fs_mutex);
836 btrfs_btree_balance_dirty(root, nr);
837 btrfs_throttle(root);
841 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
843 struct inode *inode = dentry->d_inode;
846 struct btrfs_root *root = BTRFS_I(dir)->root;
847 struct btrfs_trans_handle *trans;
848 unsigned long nr = 0;
850 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
853 mutex_lock(&root->fs_info->fs_mutex);
854 ret = btrfs_check_free_space(root, 1, 1);
858 trans = btrfs_start_transaction(root, 1);
859 btrfs_set_trans_block_group(trans, dir);
861 /* now the directory is empty */
862 err = btrfs_unlink_trans(trans, root, dir, dentry);
867 nr = trans->blocks_used;
868 ret = btrfs_end_transaction(trans, root);
870 mutex_unlock(&root->fs_info->fs_mutex);
871 btrfs_btree_balance_dirty(root, nr);
872 btrfs_throttle(root);
880 * this can truncate away extent items, csum items and directory items.
881 * It starts at a high offset and removes keys until it can't find
882 * any higher than i_size.
884 * csum items that cross the new i_size are truncated to the new size
887 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
888 struct btrfs_root *root,
893 struct btrfs_path *path;
894 struct btrfs_key key;
895 struct btrfs_key found_key;
897 struct extent_buffer *leaf;
898 struct btrfs_file_extent_item *fi;
899 u64 extent_start = 0;
900 u64 extent_num_bytes = 0;
906 int pending_del_nr = 0;
907 int pending_del_slot = 0;
908 int extent_type = -1;
910 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
911 path = btrfs_alloc_path();
915 /* FIXME, add redo link to tree so we don't leak on crash */
916 key.objectid = inode->i_ino;
917 key.offset = (u64)-1;
920 btrfs_init_path(path);
922 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
927 BUG_ON(path->slots[0] == 0);
933 leaf = path->nodes[0];
934 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
935 found_type = btrfs_key_type(&found_key);
937 if (found_key.objectid != inode->i_ino)
940 if (found_type < min_type)
943 item_end = found_key.offset;
944 if (found_type == BTRFS_EXTENT_DATA_KEY) {
945 fi = btrfs_item_ptr(leaf, path->slots[0],
946 struct btrfs_file_extent_item);
947 extent_type = btrfs_file_extent_type(leaf, fi);
948 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
950 btrfs_file_extent_num_bytes(leaf, fi);
951 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
952 struct btrfs_item *item = btrfs_item_nr(leaf,
954 item_end += btrfs_file_extent_inline_len(leaf,
959 if (found_type == BTRFS_CSUM_ITEM_KEY) {
960 ret = btrfs_csum_truncate(trans, root, path,
964 if (item_end < inode->i_size) {
965 if (found_type == BTRFS_DIR_ITEM_KEY) {
966 found_type = BTRFS_INODE_ITEM_KEY;
967 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
968 found_type = BTRFS_CSUM_ITEM_KEY;
969 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
970 found_type = BTRFS_XATTR_ITEM_KEY;
971 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
972 found_type = BTRFS_INODE_REF_KEY;
973 } else if (found_type) {
978 btrfs_set_key_type(&key, found_type);
981 if (found_key.offset >= inode->i_size)
987 /* FIXME, shrink the extent if the ref count is only 1 */
988 if (found_type != BTRFS_EXTENT_DATA_KEY)
991 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
993 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
996 btrfs_file_extent_num_bytes(leaf, fi);
997 extent_num_bytes = inode->i_size -
998 found_key.offset + root->sectorsize - 1;
999 extent_num_bytes = extent_num_bytes &
1000 ~((u64)root->sectorsize - 1);
1001 btrfs_set_file_extent_num_bytes(leaf, fi,
1003 num_dec = (orig_num_bytes -
1005 if (extent_start != 0)
1006 dec_i_blocks(inode, num_dec);
1007 btrfs_mark_buffer_dirty(leaf);
1010 btrfs_file_extent_disk_num_bytes(leaf,
1012 /* FIXME blocksize != 4096 */
1013 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1014 if (extent_start != 0) {
1016 dec_i_blocks(inode, num_dec);
1018 root_gen = btrfs_header_generation(leaf);
1019 root_owner = btrfs_header_owner(leaf);
1021 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1023 u32 newsize = inode->i_size - found_key.offset;
1024 dec_i_blocks(inode, item_end + 1 -
1025 found_key.offset - newsize);
1027 btrfs_file_extent_calc_inline_size(newsize);
1028 ret = btrfs_truncate_item(trans, root, path,
1032 dec_i_blocks(inode, item_end + 1 -
1038 if (!pending_del_nr) {
1039 /* no pending yet, add ourselves */
1040 pending_del_slot = path->slots[0];
1042 } else if (pending_del_nr &&
1043 path->slots[0] + 1 == pending_del_slot) {
1044 /* hop on the pending chunk */
1046 pending_del_slot = path->slots[0];
1048 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1054 ret = btrfs_free_extent(trans, root, extent_start,
1057 root_gen, inode->i_ino,
1058 found_key.offset, 0);
1062 if (path->slots[0] == 0) {
1065 btrfs_release_path(root, path);
1070 if (pending_del_nr &&
1071 path->slots[0] + 1 != pending_del_slot) {
1072 struct btrfs_key debug;
1074 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1076 ret = btrfs_del_items(trans, root, path,
1081 btrfs_release_path(root, path);
1087 if (pending_del_nr) {
1088 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1091 btrfs_release_path(root, path);
1092 btrfs_free_path(path);
1093 inode->i_sb->s_dirt = 1;
1097 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1101 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1102 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1103 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1106 WARN_ON(!PageLocked(page));
1107 set_page_extent_mapped(page);
1109 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1110 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1111 page_end, GFP_NOFS);
1113 if (zero_start != PAGE_CACHE_SIZE) {
1115 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1116 flush_dcache_page(page);
1119 set_page_dirty(page);
1120 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1126 * taken from block_truncate_page, but does cow as it zeros out
1127 * any bytes left in the last page in the file.
1129 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1131 struct inode *inode = mapping->host;
1132 struct btrfs_root *root = BTRFS_I(inode)->root;
1133 u32 blocksize = root->sectorsize;
1134 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1135 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1140 if ((offset & (blocksize - 1)) == 0)
1144 page = grab_cache_page(mapping, index);
1147 if (!PageUptodate(page)) {
1148 ret = btrfs_readpage(NULL, page);
1150 if (!PageUptodate(page)) {
1155 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1157 ret = btrfs_cow_one_page(inode, page, offset);
1160 page_cache_release(page);
1165 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1167 struct inode *inode = dentry->d_inode;
1170 err = inode_change_ok(inode, attr);
1174 if (S_ISREG(inode->i_mode) &&
1175 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1176 struct btrfs_trans_handle *trans;
1177 struct btrfs_root *root = BTRFS_I(inode)->root;
1178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1180 u64 mask = root->sectorsize - 1;
1181 u64 hole_start = (inode->i_size + mask) & ~mask;
1182 u64 block_end = (attr->ia_size + mask) & ~mask;
1186 if (attr->ia_size <= hole_start)
1189 mutex_lock(&root->fs_info->fs_mutex);
1190 err = btrfs_check_free_space(root, 1, 0);
1191 mutex_unlock(&root->fs_info->fs_mutex);
1195 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1197 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1198 hole_size = block_end - hole_start;
1200 mutex_lock(&root->fs_info->fs_mutex);
1201 trans = btrfs_start_transaction(root, 1);
1202 btrfs_set_trans_block_group(trans, inode);
1203 err = btrfs_drop_extents(trans, root, inode,
1204 hole_start, block_end, hole_start,
1207 if (alloc_hint != EXTENT_MAP_INLINE) {
1208 err = btrfs_insert_file_extent(trans, root,
1212 btrfs_drop_extent_cache(inode, hole_start,
1214 btrfs_check_file(root, inode);
1216 btrfs_end_transaction(trans, root);
1217 mutex_unlock(&root->fs_info->fs_mutex);
1218 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1223 err = inode_setattr(inode, attr);
1228 void btrfs_put_inode(struct inode *inode)
1232 if (!BTRFS_I(inode)->ordered_trans) {
1236 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1237 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1240 ret = btrfs_del_ordered_inode(inode);
1242 atomic_dec(&inode->i_count);
1246 void btrfs_delete_inode(struct inode *inode)
1248 struct btrfs_trans_handle *trans;
1249 struct btrfs_root *root = BTRFS_I(inode)->root;
1253 truncate_inode_pages(&inode->i_data, 0);
1254 if (is_bad_inode(inode)) {
1259 mutex_lock(&root->fs_info->fs_mutex);
1260 trans = btrfs_start_transaction(root, 1);
1262 btrfs_set_trans_block_group(trans, inode);
1263 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1265 goto no_delete_lock;
1267 nr = trans->blocks_used;
1270 btrfs_end_transaction(trans, root);
1271 mutex_unlock(&root->fs_info->fs_mutex);
1272 btrfs_btree_balance_dirty(root, nr);
1273 btrfs_throttle(root);
1277 nr = trans->blocks_used;
1278 btrfs_end_transaction(trans, root);
1279 mutex_unlock(&root->fs_info->fs_mutex);
1280 btrfs_btree_balance_dirty(root, nr);
1281 btrfs_throttle(root);
1287 * this returns the key found in the dir entry in the location pointer.
1288 * If no dir entries were found, location->objectid is 0.
1290 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1291 struct btrfs_key *location)
1293 const char *name = dentry->d_name.name;
1294 int namelen = dentry->d_name.len;
1295 struct btrfs_dir_item *di;
1296 struct btrfs_path *path;
1297 struct btrfs_root *root = BTRFS_I(dir)->root;
1300 if (namelen == 1 && strcmp(name, ".") == 0) {
1301 location->objectid = dir->i_ino;
1302 location->type = BTRFS_INODE_ITEM_KEY;
1303 location->offset = 0;
1306 path = btrfs_alloc_path();
1309 if (namelen == 2 && strcmp(name, "..") == 0) {
1310 struct btrfs_key key;
1311 struct extent_buffer *leaf;
1315 key.objectid = dir->i_ino;
1316 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1322 leaf = path->nodes[0];
1323 slot = path->slots[0];
1324 nritems = btrfs_header_nritems(leaf);
1325 if (slot >= nritems)
1328 btrfs_item_key_to_cpu(leaf, &key, slot);
1329 if (key.objectid != dir->i_ino ||
1330 key.type != BTRFS_INODE_REF_KEY) {
1333 location->objectid = key.offset;
1334 location->type = BTRFS_INODE_ITEM_KEY;
1335 location->offset = 0;
1339 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1343 if (!di || IS_ERR(di)) {
1346 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1348 btrfs_free_path(path);
1351 location->objectid = 0;
1356 * when we hit a tree root in a directory, the btrfs part of the inode
1357 * needs to be changed to reflect the root directory of the tree root. This
1358 * is kind of like crossing a mount point.
1360 static int fixup_tree_root_location(struct btrfs_root *root,
1361 struct btrfs_key *location,
1362 struct btrfs_root **sub_root,
1363 struct dentry *dentry)
1365 struct btrfs_path *path;
1366 struct btrfs_root_item *ri;
1368 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1370 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1373 path = btrfs_alloc_path();
1375 mutex_lock(&root->fs_info->fs_mutex);
1377 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1378 dentry->d_name.name,
1379 dentry->d_name.len);
1380 if (IS_ERR(*sub_root))
1381 return PTR_ERR(*sub_root);
1383 ri = &(*sub_root)->root_item;
1384 location->objectid = btrfs_root_dirid(ri);
1385 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1386 location->offset = 0;
1388 btrfs_free_path(path);
1389 mutex_unlock(&root->fs_info->fs_mutex);
1393 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1395 struct btrfs_iget_args *args = p;
1396 inode->i_ino = args->ino;
1397 BTRFS_I(inode)->root = args->root;
1398 BTRFS_I(inode)->delalloc_bytes = 0;
1399 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1400 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1401 inode->i_mapping, GFP_NOFS);
1402 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1403 inode->i_mapping, GFP_NOFS);
1407 static int btrfs_find_actor(struct inode *inode, void *opaque)
1409 struct btrfs_iget_args *args = opaque;
1410 return (args->ino == inode->i_ino &&
1411 args->root == BTRFS_I(inode)->root);
1414 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1417 struct btrfs_iget_args args;
1418 args.ino = objectid;
1419 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1424 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1427 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1428 struct btrfs_root *root)
1430 struct inode *inode;
1431 struct btrfs_iget_args args;
1432 args.ino = objectid;
1435 inode = iget5_locked(s, objectid, btrfs_find_actor,
1436 btrfs_init_locked_inode,
1441 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1442 struct nameidata *nd)
1444 struct inode * inode;
1445 struct btrfs_inode *bi = BTRFS_I(dir);
1446 struct btrfs_root *root = bi->root;
1447 struct btrfs_root *sub_root = root;
1448 struct btrfs_key location;
1451 if (dentry->d_name.len > BTRFS_NAME_LEN)
1452 return ERR_PTR(-ENAMETOOLONG);
1454 mutex_lock(&root->fs_info->fs_mutex);
1455 ret = btrfs_inode_by_name(dir, dentry, &location);
1456 mutex_unlock(&root->fs_info->fs_mutex);
1459 return ERR_PTR(ret);
1462 if (location.objectid) {
1463 ret = fixup_tree_root_location(root, &location, &sub_root,
1466 return ERR_PTR(ret);
1468 return ERR_PTR(-ENOENT);
1469 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1472 return ERR_PTR(-EACCES);
1473 if (inode->i_state & I_NEW) {
1474 /* the inode and parent dir are two different roots */
1475 if (sub_root != root) {
1477 sub_root->inode = inode;
1479 BTRFS_I(inode)->root = sub_root;
1480 memcpy(&BTRFS_I(inode)->location, &location,
1482 btrfs_read_locked_inode(inode);
1483 unlock_new_inode(inode);
1486 return d_splice_alias(inode, dentry);
1489 static unsigned char btrfs_filetype_table[] = {
1490 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1493 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1495 struct inode *inode = filp->f_dentry->d_inode;
1496 struct btrfs_root *root = BTRFS_I(inode)->root;
1497 struct btrfs_item *item;
1498 struct btrfs_dir_item *di;
1499 struct btrfs_key key;
1500 struct btrfs_key found_key;
1501 struct btrfs_path *path;
1504 struct extent_buffer *leaf;
1507 unsigned char d_type;
1512 int key_type = BTRFS_DIR_INDEX_KEY;
1517 /* FIXME, use a real flag for deciding about the key type */
1518 if (root->fs_info->tree_root == root)
1519 key_type = BTRFS_DIR_ITEM_KEY;
1521 /* special case for "." */
1522 if (filp->f_pos == 0) {
1523 over = filldir(dirent, ".", 1,
1531 mutex_lock(&root->fs_info->fs_mutex);
1532 key.objectid = inode->i_ino;
1533 path = btrfs_alloc_path();
1536 /* special case for .., just use the back ref */
1537 if (filp->f_pos == 1) {
1538 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1540 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1542 leaf = path->nodes[0];
1543 slot = path->slots[0];
1544 nritems = btrfs_header_nritems(leaf);
1545 if (slot >= nritems) {
1546 btrfs_release_path(root, path);
1547 goto read_dir_items;
1549 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1550 btrfs_release_path(root, path);
1551 if (found_key.objectid != key.objectid ||
1552 found_key.type != BTRFS_INODE_REF_KEY)
1553 goto read_dir_items;
1554 over = filldir(dirent, "..", 2,
1555 2, found_key.offset, DT_DIR);
1562 btrfs_set_key_type(&key, key_type);
1563 key.offset = filp->f_pos;
1565 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1570 leaf = path->nodes[0];
1571 nritems = btrfs_header_nritems(leaf);
1572 slot = path->slots[0];
1573 if (advance || slot >= nritems) {
1574 if (slot >= nritems -1) {
1575 ret = btrfs_next_leaf(root, path);
1578 leaf = path->nodes[0];
1579 nritems = btrfs_header_nritems(leaf);
1580 slot = path->slots[0];
1587 item = btrfs_item_nr(leaf, slot);
1588 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1590 if (found_key.objectid != key.objectid)
1592 if (btrfs_key_type(&found_key) != key_type)
1594 if (found_key.offset < filp->f_pos)
1597 filp->f_pos = found_key.offset;
1599 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1601 di_total = btrfs_item_size(leaf, item);
1602 while(di_cur < di_total) {
1603 struct btrfs_key location;
1605 name_len = btrfs_dir_name_len(leaf, di);
1606 if (name_len < 32) {
1607 name_ptr = tmp_name;
1609 name_ptr = kmalloc(name_len, GFP_NOFS);
1612 read_extent_buffer(leaf, name_ptr,
1613 (unsigned long)(di + 1), name_len);
1615 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1616 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1617 over = filldir(dirent, name_ptr, name_len,
1622 if (name_ptr != tmp_name)
1627 di_len = btrfs_dir_name_len(leaf, di) +
1628 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1630 di = (struct btrfs_dir_item *)((char *)di + di_len);
1633 if (key_type == BTRFS_DIR_INDEX_KEY)
1634 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1640 btrfs_release_path(root, path);
1641 btrfs_free_path(path);
1642 mutex_unlock(&root->fs_info->fs_mutex);
1646 int btrfs_write_inode(struct inode *inode, int wait)
1648 struct btrfs_root *root = BTRFS_I(inode)->root;
1649 struct btrfs_trans_handle *trans;
1653 mutex_lock(&root->fs_info->fs_mutex);
1654 trans = btrfs_start_transaction(root, 1);
1655 btrfs_set_trans_block_group(trans, inode);
1656 ret = btrfs_commit_transaction(trans, root);
1657 mutex_unlock(&root->fs_info->fs_mutex);
1663 * This is somewhat expensive, updating the tree every time the
1664 * inode changes. But, it is most likely to find the inode in cache.
1665 * FIXME, needs more benchmarking...there are no reasons other than performance
1666 * to keep or drop this code.
1668 void btrfs_dirty_inode(struct inode *inode)
1670 struct btrfs_root *root = BTRFS_I(inode)->root;
1671 struct btrfs_trans_handle *trans;
1673 mutex_lock(&root->fs_info->fs_mutex);
1674 trans = btrfs_start_transaction(root, 1);
1675 btrfs_set_trans_block_group(trans, inode);
1676 btrfs_update_inode(trans, root, inode);
1677 btrfs_end_transaction(trans, root);
1678 mutex_unlock(&root->fs_info->fs_mutex);
1681 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
1683 const char *name, int name_len,
1686 struct btrfs_block_group_cache *group,
1689 struct inode *inode;
1690 struct btrfs_inode_item *inode_item;
1691 struct btrfs_block_group_cache *new_inode_group;
1692 struct btrfs_key *location;
1693 struct btrfs_path *path;
1694 struct btrfs_inode_ref *ref;
1695 struct btrfs_key key[2];
1701 path = btrfs_alloc_path();
1704 inode = new_inode(root->fs_info->sb);
1706 return ERR_PTR(-ENOMEM);
1708 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1709 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1710 inode->i_mapping, GFP_NOFS);
1711 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1712 inode->i_mapping, GFP_NOFS);
1713 BTRFS_I(inode)->delalloc_bytes = 0;
1714 BTRFS_I(inode)->root = root;
1720 new_inode_group = btrfs_find_block_group(root, group, 0,
1721 BTRFS_BLOCK_GROUP_METADATA, owner);
1722 if (!new_inode_group) {
1723 printk("find_block group failed\n");
1724 new_inode_group = group;
1726 BTRFS_I(inode)->block_group = new_inode_group;
1727 BTRFS_I(inode)->flags = 0;
1729 key[0].objectid = objectid;
1730 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1733 key[1].objectid = objectid;
1734 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1735 key[1].offset = ref_objectid;
1737 sizes[0] = sizeof(struct btrfs_inode_item);
1738 sizes[1] = name_len + sizeof(*ref);
1740 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1744 if (objectid > root->highest_inode)
1745 root->highest_inode = objectid;
1747 inode->i_uid = current->fsuid;
1748 inode->i_gid = current->fsgid;
1749 inode->i_mode = mode;
1750 inode->i_ino = objectid;
1751 inode->i_blocks = 0;
1752 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1753 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1754 struct btrfs_inode_item);
1755 fill_inode_item(path->nodes[0], inode_item, inode);
1757 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1758 struct btrfs_inode_ref);
1759 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1760 ptr = (unsigned long)(ref + 1);
1761 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1763 btrfs_mark_buffer_dirty(path->nodes[0]);
1764 btrfs_free_path(path);
1766 location = &BTRFS_I(inode)->location;
1767 location->objectid = objectid;
1768 location->offset = 0;
1769 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1771 insert_inode_hash(inode);
1774 btrfs_free_path(path);
1775 return ERR_PTR(ret);
1778 static inline u8 btrfs_inode_type(struct inode *inode)
1780 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1783 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1784 struct dentry *dentry, struct inode *inode,
1788 struct btrfs_key key;
1789 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1790 struct inode *parent_inode;
1792 key.objectid = inode->i_ino;
1793 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1796 ret = btrfs_insert_dir_item(trans, root,
1797 dentry->d_name.name, dentry->d_name.len,
1798 dentry->d_parent->d_inode->i_ino,
1799 &key, btrfs_inode_type(inode));
1802 ret = btrfs_insert_inode_ref(trans, root,
1803 dentry->d_name.name,
1806 dentry->d_parent->d_inode->i_ino);
1808 parent_inode = dentry->d_parent->d_inode;
1809 parent_inode->i_size += dentry->d_name.len * 2;
1810 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1811 ret = btrfs_update_inode(trans, root,
1812 dentry->d_parent->d_inode);
1817 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1818 struct dentry *dentry, struct inode *inode,
1821 int err = btrfs_add_link(trans, dentry, inode, backref);
1823 d_instantiate(dentry, inode);
1831 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1832 int mode, dev_t rdev)
1834 struct btrfs_trans_handle *trans;
1835 struct btrfs_root *root = BTRFS_I(dir)->root;
1836 struct inode *inode = NULL;
1840 unsigned long nr = 0;
1842 if (!new_valid_dev(rdev))
1845 mutex_lock(&root->fs_info->fs_mutex);
1846 err = btrfs_check_free_space(root, 1, 0);
1850 trans = btrfs_start_transaction(root, 1);
1851 btrfs_set_trans_block_group(trans, dir);
1853 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1859 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1861 dentry->d_parent->d_inode->i_ino, objectid,
1862 BTRFS_I(dir)->block_group, mode);
1863 err = PTR_ERR(inode);
1867 btrfs_set_trans_block_group(trans, inode);
1868 err = btrfs_add_nondir(trans, dentry, inode, 0);
1872 inode->i_op = &btrfs_special_inode_operations;
1873 init_special_inode(inode, inode->i_mode, rdev);
1874 btrfs_update_inode(trans, root, inode);
1876 dir->i_sb->s_dirt = 1;
1877 btrfs_update_inode_block_group(trans, inode);
1878 btrfs_update_inode_block_group(trans, dir);
1880 nr = trans->blocks_used;
1881 btrfs_end_transaction(trans, root);
1883 mutex_unlock(&root->fs_info->fs_mutex);
1886 inode_dec_link_count(inode);
1889 btrfs_btree_balance_dirty(root, nr);
1890 btrfs_throttle(root);
1894 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1895 int mode, struct nameidata *nd)
1897 struct btrfs_trans_handle *trans;
1898 struct btrfs_root *root = BTRFS_I(dir)->root;
1899 struct inode *inode = NULL;
1902 unsigned long nr = 0;
1905 mutex_lock(&root->fs_info->fs_mutex);
1906 err = btrfs_check_free_space(root, 1, 0);
1909 trans = btrfs_start_transaction(root, 1);
1910 btrfs_set_trans_block_group(trans, dir);
1912 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1918 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1920 dentry->d_parent->d_inode->i_ino,
1921 objectid, BTRFS_I(dir)->block_group, mode);
1922 err = PTR_ERR(inode);
1926 btrfs_set_trans_block_group(trans, inode);
1927 err = btrfs_add_nondir(trans, dentry, inode, 0);
1931 inode->i_mapping->a_ops = &btrfs_aops;
1932 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1933 inode->i_fop = &btrfs_file_operations;
1934 inode->i_op = &btrfs_file_inode_operations;
1935 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1936 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1937 inode->i_mapping, GFP_NOFS);
1938 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1939 inode->i_mapping, GFP_NOFS);
1940 BTRFS_I(inode)->delalloc_bytes = 0;
1941 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1943 dir->i_sb->s_dirt = 1;
1944 btrfs_update_inode_block_group(trans, inode);
1945 btrfs_update_inode_block_group(trans, dir);
1947 nr = trans->blocks_used;
1948 btrfs_end_transaction(trans, root);
1950 mutex_unlock(&root->fs_info->fs_mutex);
1953 inode_dec_link_count(inode);
1956 btrfs_btree_balance_dirty(root, nr);
1957 btrfs_throttle(root);
1961 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1962 struct dentry *dentry)
1964 struct btrfs_trans_handle *trans;
1965 struct btrfs_root *root = BTRFS_I(dir)->root;
1966 struct inode *inode = old_dentry->d_inode;
1967 unsigned long nr = 0;
1971 if (inode->i_nlink == 0)
1974 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1979 mutex_lock(&root->fs_info->fs_mutex);
1980 err = btrfs_check_free_space(root, 1, 0);
1983 trans = btrfs_start_transaction(root, 1);
1985 btrfs_set_trans_block_group(trans, dir);
1986 atomic_inc(&inode->i_count);
1987 err = btrfs_add_nondir(trans, dentry, inode, 1);
1992 dir->i_sb->s_dirt = 1;
1993 btrfs_update_inode_block_group(trans, dir);
1994 err = btrfs_update_inode(trans, root, inode);
1999 nr = trans->blocks_used;
2000 btrfs_end_transaction(trans, root);
2002 mutex_unlock(&root->fs_info->fs_mutex);
2005 inode_dec_link_count(inode);
2008 btrfs_btree_balance_dirty(root, nr);
2009 btrfs_throttle(root);
2013 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2015 struct inode *inode;
2016 struct btrfs_trans_handle *trans;
2017 struct btrfs_root *root = BTRFS_I(dir)->root;
2019 int drop_on_err = 0;
2021 unsigned long nr = 1;
2023 mutex_lock(&root->fs_info->fs_mutex);
2024 err = btrfs_check_free_space(root, 1, 0);
2028 trans = btrfs_start_transaction(root, 1);
2029 btrfs_set_trans_block_group(trans, dir);
2031 if (IS_ERR(trans)) {
2032 err = PTR_ERR(trans);
2036 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2042 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2044 dentry->d_parent->d_inode->i_ino, objectid,
2045 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2046 if (IS_ERR(inode)) {
2047 err = PTR_ERR(inode);
2052 inode->i_op = &btrfs_dir_inode_operations;
2053 inode->i_fop = &btrfs_dir_file_operations;
2054 btrfs_set_trans_block_group(trans, inode);
2057 err = btrfs_update_inode(trans, root, inode);
2061 err = btrfs_add_link(trans, dentry, inode, 0);
2065 d_instantiate(dentry, inode);
2067 dir->i_sb->s_dirt = 1;
2068 btrfs_update_inode_block_group(trans, inode);
2069 btrfs_update_inode_block_group(trans, dir);
2072 nr = trans->blocks_used;
2073 btrfs_end_transaction(trans, root);
2076 mutex_unlock(&root->fs_info->fs_mutex);
2079 btrfs_btree_balance_dirty(root, nr);
2080 btrfs_throttle(root);
2084 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2085 size_t pg_offset, u64 start, u64 len,
2091 u64 extent_start = 0;
2093 u64 objectid = inode->i_ino;
2095 struct btrfs_path *path;
2096 struct btrfs_root *root = BTRFS_I(inode)->root;
2097 struct btrfs_file_extent_item *item;
2098 struct extent_buffer *leaf;
2099 struct btrfs_key found_key;
2100 struct extent_map *em = NULL;
2101 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2102 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2103 struct btrfs_trans_handle *trans = NULL;
2105 path = btrfs_alloc_path();
2107 mutex_lock(&root->fs_info->fs_mutex);
2110 spin_lock(&em_tree->lock);
2111 em = lookup_extent_mapping(em_tree, start, len);
2112 spin_unlock(&em_tree->lock);
2115 if (em->start > start) {
2116 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2117 start, len, em->start, em->len);
2120 if (em->block_start == EXTENT_MAP_INLINE && page)
2121 free_extent_map(em);
2125 em = alloc_extent_map(GFP_NOFS);
2131 em->start = EXTENT_MAP_HOLE;
2133 em->bdev = inode->i_sb->s_bdev;
2134 ret = btrfs_lookup_file_extent(trans, root, path,
2135 objectid, start, trans != NULL);
2142 if (path->slots[0] == 0)
2147 leaf = path->nodes[0];
2148 item = btrfs_item_ptr(leaf, path->slots[0],
2149 struct btrfs_file_extent_item);
2150 /* are we inside the extent that was found? */
2151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2152 found_type = btrfs_key_type(&found_key);
2153 if (found_key.objectid != objectid ||
2154 found_type != BTRFS_EXTENT_DATA_KEY) {
2158 found_type = btrfs_file_extent_type(leaf, item);
2159 extent_start = found_key.offset;
2160 if (found_type == BTRFS_FILE_EXTENT_REG) {
2161 extent_end = extent_start +
2162 btrfs_file_extent_num_bytes(leaf, item);
2164 if (start < extent_start || start >= extent_end) {
2166 if (start < extent_start) {
2167 if (start + len <= extent_start)
2169 em->len = extent_end - extent_start;
2175 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2177 em->start = extent_start;
2178 em->len = extent_end - extent_start;
2179 em->block_start = EXTENT_MAP_HOLE;
2182 bytenr += btrfs_file_extent_offset(leaf, item);
2183 em->block_start = bytenr;
2184 em->start = extent_start;
2185 em->len = extent_end - extent_start;
2187 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2192 size_t extent_offset;
2195 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2197 extent_end = (extent_start + size + root->sectorsize - 1) &
2198 ~((u64)root->sectorsize - 1);
2199 if (start < extent_start || start >= extent_end) {
2201 if (start < extent_start) {
2202 if (start + len <= extent_start)
2204 em->len = extent_end - extent_start;
2210 em->block_start = EXTENT_MAP_INLINE;
2213 em->start = extent_start;
2218 page_start = page_offset(page) + pg_offset;
2219 extent_offset = page_start - extent_start;
2220 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2221 size - extent_offset);
2222 em->start = extent_start + extent_offset;
2223 em->len = (copy_size + root->sectorsize - 1) &
2224 ~((u64)root->sectorsize - 1);
2226 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2227 if (create == 0 && !PageUptodate(page)) {
2228 read_extent_buffer(leaf, map + pg_offset, ptr,
2230 flush_dcache_page(page);
2231 } else if (create && PageUptodate(page)) {
2234 free_extent_map(em);
2236 btrfs_release_path(root, path);
2237 trans = btrfs_start_transaction(root, 1);
2240 write_extent_buffer(leaf, map + pg_offset, ptr,
2242 btrfs_mark_buffer_dirty(leaf);
2245 set_extent_uptodate(io_tree, em->start,
2246 extent_map_end(em) - 1, GFP_NOFS);
2249 printk("unkknown found_type %d\n", found_type);
2256 em->block_start = EXTENT_MAP_HOLE;
2258 btrfs_release_path(root, path);
2259 if (em->start > start || extent_map_end(em) <= start) {
2260 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2266 spin_lock(&em_tree->lock);
2267 ret = add_extent_mapping(em_tree, em);
2268 if (ret == -EEXIST) {
2269 free_extent_map(em);
2270 em = lookup_extent_mapping(em_tree, start, len);
2273 printk("failing to insert %Lu %Lu\n", start, len);
2276 spin_unlock(&em_tree->lock);
2278 btrfs_free_path(path);
2280 ret = btrfs_end_transaction(trans, root);
2284 mutex_unlock(&root->fs_info->fs_mutex);
2286 free_extent_map(em);
2288 return ERR_PTR(err);
2293 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2294 struct buffer_head *bh_result, int create)
2296 struct extent_map *em;
2297 u64 start = (u64)iblock << inode->i_blkbits;
2298 struct btrfs_multi_bio *multi = NULL;
2299 struct btrfs_root *root = BTRFS_I(inode)->root;
2305 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2307 if (!em || IS_ERR(em))
2310 if (em->start > start || em->start + em->len <= start)
2313 if (em->block_start == EXTENT_MAP_INLINE) {
2318 if (em->block_start == EXTENT_MAP_HOLE ||
2319 em->block_start == EXTENT_MAP_DELALLOC) {
2323 len = em->start + em->len - start;
2324 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2326 logical = start - em->start;
2327 logical = em->block_start + logical;
2330 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2331 logical, &map_length, &multi, 0);
2333 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2334 bh_result->b_size = min(map_length, len);
2335 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2336 set_buffer_mapped(bh_result);
2339 free_extent_map(em);
2343 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2344 const struct iovec *iov, loff_t offset,
2345 unsigned long nr_segs)
2347 struct file *file = iocb->ki_filp;
2348 struct inode *inode = file->f_mapping->host;
2353 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2354 offset, nr_segs, btrfs_get_block, NULL);
2357 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2359 return extent_bmap(mapping, iblock, btrfs_get_extent);
2362 int btrfs_readpage(struct file *file, struct page *page)
2364 struct extent_io_tree *tree;
2365 tree = &BTRFS_I(page->mapping->host)->io_tree;
2366 return extent_read_full_page(tree, page, btrfs_get_extent);
2369 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2371 struct extent_io_tree *tree;
2374 if (current->flags & PF_MEMALLOC) {
2375 redirty_page_for_writepage(wbc, page);
2379 tree = &BTRFS_I(page->mapping->host)->io_tree;
2380 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2383 static int btrfs_writepages(struct address_space *mapping,
2384 struct writeback_control *wbc)
2386 struct extent_io_tree *tree;
2387 tree = &BTRFS_I(mapping->host)->io_tree;
2388 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2392 btrfs_readpages(struct file *file, struct address_space *mapping,
2393 struct list_head *pages, unsigned nr_pages)
2395 struct extent_io_tree *tree;
2396 tree = &BTRFS_I(mapping->host)->io_tree;
2397 return extent_readpages(tree, mapping, pages, nr_pages,
2401 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2403 struct extent_io_tree *tree;
2404 struct extent_map_tree *map;
2407 tree = &BTRFS_I(page->mapping->host)->io_tree;
2408 map = &BTRFS_I(page->mapping->host)->extent_tree;
2409 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2411 ClearPagePrivate(page);
2412 set_page_private(page, 0);
2413 page_cache_release(page);
2418 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2420 struct extent_io_tree *tree;
2422 tree = &BTRFS_I(page->mapping->host)->io_tree;
2423 extent_invalidatepage(tree, page, offset);
2424 btrfs_releasepage(page, GFP_NOFS);
2428 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2429 * called from a page fault handler when a page is first dirtied. Hence we must
2430 * be careful to check for EOF conditions here. We set the page up correctly
2431 * for a written page which means we get ENOSPC checking when writing into
2432 * holes and correct delalloc and unwritten extent mapping on filesystems that
2433 * support these features.
2435 * We are not allowed to take the i_mutex here so we have to play games to
2436 * protect against truncate races as the page could now be beyond EOF. Because
2437 * vmtruncate() writes the inode size before removing pages, once we have the
2438 * page lock we can determine safely if the page is beyond EOF. If it is not
2439 * beyond EOF, then the page is guaranteed safe against truncation until we
2442 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2444 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2445 struct btrfs_root *root = BTRFS_I(inode)->root;
2451 mutex_lock(&root->fs_info->fs_mutex);
2452 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2453 mutex_unlock(&root->fs_info->fs_mutex);
2460 wait_on_page_writeback(page);
2461 size = i_size_read(inode);
2462 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2464 if ((page->mapping != inode->i_mapping) ||
2465 (page_start > size)) {
2466 /* page got truncated out from underneath us */
2470 /* page is wholly or partially inside EOF */
2471 if (page_start + PAGE_CACHE_SIZE > size)
2472 end = size & ~PAGE_CACHE_MASK;
2474 end = PAGE_CACHE_SIZE;
2476 ret = btrfs_cow_one_page(inode, page, end);
2484 static void btrfs_truncate(struct inode *inode)
2486 struct btrfs_root *root = BTRFS_I(inode)->root;
2488 struct btrfs_trans_handle *trans;
2491 if (!S_ISREG(inode->i_mode))
2493 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2496 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2498 mutex_lock(&root->fs_info->fs_mutex);
2499 trans = btrfs_start_transaction(root, 1);
2500 btrfs_set_trans_block_group(trans, inode);
2502 /* FIXME, add redo link to tree so we don't leak on crash */
2503 ret = btrfs_truncate_in_trans(trans, root, inode,
2504 BTRFS_EXTENT_DATA_KEY);
2505 btrfs_update_inode(trans, root, inode);
2506 nr = trans->blocks_used;
2508 ret = btrfs_end_transaction(trans, root);
2510 mutex_unlock(&root->fs_info->fs_mutex);
2511 btrfs_btree_balance_dirty(root, nr);
2512 btrfs_throttle(root);
2515 static int noinline create_subvol(struct btrfs_root *root, char *name,
2518 struct btrfs_trans_handle *trans;
2519 struct btrfs_key key;
2520 struct btrfs_root_item root_item;
2521 struct btrfs_inode_item *inode_item;
2522 struct extent_buffer *leaf;
2523 struct btrfs_root *new_root = root;
2524 struct inode *inode;
2529 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2530 unsigned long nr = 1;
2532 mutex_lock(&root->fs_info->fs_mutex);
2533 ret = btrfs_check_free_space(root, 1, 0);
2537 trans = btrfs_start_transaction(root, 1);
2540 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2545 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2546 objectid, trans->transid, 0, 0,
2549 return PTR_ERR(leaf);
2551 btrfs_set_header_nritems(leaf, 0);
2552 btrfs_set_header_level(leaf, 0);
2553 btrfs_set_header_bytenr(leaf, leaf->start);
2554 btrfs_set_header_generation(leaf, trans->transid);
2555 btrfs_set_header_owner(leaf, objectid);
2557 write_extent_buffer(leaf, root->fs_info->fsid,
2558 (unsigned long)btrfs_header_fsid(leaf),
2560 btrfs_mark_buffer_dirty(leaf);
2562 inode_item = &root_item.inode;
2563 memset(inode_item, 0, sizeof(*inode_item));
2564 inode_item->generation = cpu_to_le64(1);
2565 inode_item->size = cpu_to_le64(3);
2566 inode_item->nlink = cpu_to_le32(1);
2567 inode_item->nblocks = cpu_to_le64(1);
2568 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2570 btrfs_set_root_bytenr(&root_item, leaf->start);
2571 btrfs_set_root_level(&root_item, 0);
2572 btrfs_set_root_refs(&root_item, 1);
2573 btrfs_set_root_used(&root_item, 0);
2575 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2576 root_item.drop_level = 0;
2578 free_extent_buffer(leaf);
2581 btrfs_set_root_dirid(&root_item, new_dirid);
2583 key.objectid = objectid;
2585 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2586 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2592 * insert the directory item
2594 key.offset = (u64)-1;
2595 dir = root->fs_info->sb->s_root->d_inode;
2596 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2597 name, namelen, dir->i_ino, &key,
2602 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2603 name, namelen, objectid,
2604 root->fs_info->sb->s_root->d_inode->i_ino);
2608 ret = btrfs_commit_transaction(trans, root);
2612 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2615 trans = btrfs_start_transaction(new_root, 1);
2618 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2620 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2623 inode->i_op = &btrfs_dir_inode_operations;
2624 inode->i_fop = &btrfs_dir_file_operations;
2625 new_root->inode = inode;
2627 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2631 ret = btrfs_update_inode(trans, new_root, inode);
2635 nr = trans->blocks_used;
2636 err = btrfs_commit_transaction(trans, new_root);
2640 mutex_unlock(&root->fs_info->fs_mutex);
2641 btrfs_btree_balance_dirty(root, nr);
2642 btrfs_throttle(root);
2646 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2648 struct btrfs_pending_snapshot *pending_snapshot;
2649 struct btrfs_trans_handle *trans;
2652 unsigned long nr = 0;
2654 if (!root->ref_cows)
2657 mutex_lock(&root->fs_info->fs_mutex);
2658 ret = btrfs_check_free_space(root, 1, 0);
2662 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2663 if (!pending_snapshot) {
2667 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2668 if (!pending_snapshot->name) {
2670 kfree(pending_snapshot);
2673 memcpy(pending_snapshot->name, name, namelen);
2674 pending_snapshot->name[namelen] = '\0';
2675 trans = btrfs_start_transaction(root, 1);
2677 pending_snapshot->root = root;
2678 list_add(&pending_snapshot->list,
2679 &trans->transaction->pending_snapshots);
2680 ret = btrfs_update_inode(trans, root, root->inode);
2681 err = btrfs_commit_transaction(trans, root);
2684 mutex_unlock(&root->fs_info->fs_mutex);
2685 btrfs_btree_balance_dirty(root, nr);
2686 btrfs_throttle(root);
2690 unsigned long btrfs_force_ra(struct address_space *mapping,
2691 struct file_ra_state *ra, struct file *file,
2692 pgoff_t offset, pgoff_t last_index)
2696 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2697 req_size = last_index - offset + 1;
2698 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2701 req_size = min(last_index - offset + 1, (pgoff_t)128);
2702 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2703 return offset + req_size;
2707 int btrfs_defrag_file(struct file *file) {
2708 struct inode *inode = fdentry(file)->d_inode;
2709 struct btrfs_root *root = BTRFS_I(inode)->root;
2710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2712 unsigned long last_index;
2713 unsigned long ra_index = 0;
2719 mutex_lock(&root->fs_info->fs_mutex);
2720 ret = btrfs_check_free_space(root, inode->i_size, 0);
2721 mutex_unlock(&root->fs_info->fs_mutex);
2725 mutex_lock(&inode->i_mutex);
2726 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2727 for (i = 0; i <= last_index; i++) {
2728 if (i == ra_index) {
2729 ra_index = btrfs_force_ra(inode->i_mapping,
2731 file, ra_index, last_index);
2733 page = grab_cache_page(inode->i_mapping, i);
2736 if (!PageUptodate(page)) {
2737 btrfs_readpage(NULL, page);
2739 if (!PageUptodate(page)) {
2741 page_cache_release(page);
2745 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2746 page_end = page_start + PAGE_CACHE_SIZE - 1;
2748 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2749 set_extent_delalloc(io_tree, page_start,
2750 page_end, GFP_NOFS);
2752 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2753 set_page_dirty(page);
2755 page_cache_release(page);
2756 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2760 mutex_unlock(&inode->i_mutex);
2764 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2768 struct btrfs_ioctl_vol_args *vol_args;
2769 struct btrfs_trans_handle *trans;
2775 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2780 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2784 namelen = strlen(vol_args->name);
2785 if (namelen > BTRFS_VOL_NAME_MAX) {
2790 sizestr = vol_args->name;
2791 if (!strcmp(sizestr, "max"))
2792 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2794 if (sizestr[0] == '-') {
2797 } else if (sizestr[0] == '+') {
2801 new_size = btrfs_parse_size(sizestr);
2802 if (new_size == 0) {
2808 mutex_lock(&root->fs_info->fs_mutex);
2809 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2812 if (new_size > old_size) {
2816 new_size = old_size - new_size;
2817 } else if (mod > 0) {
2818 new_size = old_size + new_size;
2821 if (new_size < 256 * 1024 * 1024) {
2825 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2830 do_div(new_size, root->sectorsize);
2831 new_size *= root->sectorsize;
2833 printk("new size is %Lu\n", new_size);
2834 if (new_size > old_size) {
2835 trans = btrfs_start_transaction(root, 1);
2836 ret = btrfs_grow_extent_tree(trans, root, new_size);
2837 btrfs_commit_transaction(trans, root);
2839 ret = btrfs_shrink_extent_tree(root, new_size);
2843 mutex_unlock(&root->fs_info->fs_mutex);
2849 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2852 struct btrfs_ioctl_vol_args *vol_args;
2853 struct btrfs_dir_item *di;
2854 struct btrfs_path *path;
2859 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2864 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2869 namelen = strlen(vol_args->name);
2870 if (namelen > BTRFS_VOL_NAME_MAX) {
2874 if (strchr(vol_args->name, '/')) {
2879 path = btrfs_alloc_path();
2885 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2886 mutex_lock(&root->fs_info->fs_mutex);
2887 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2889 vol_args->name, namelen, 0);
2890 mutex_unlock(&root->fs_info->fs_mutex);
2891 btrfs_free_path(path);
2893 if (di && !IS_ERR(di)) {
2903 if (root == root->fs_info->tree_root)
2904 ret = create_subvol(root, vol_args->name, namelen);
2906 ret = create_snapshot(root, vol_args->name, namelen);
2912 static int btrfs_ioctl_defrag(struct file *file)
2914 struct inode *inode = fdentry(file)->d_inode;
2915 struct btrfs_root *root = BTRFS_I(inode)->root;
2917 switch (inode->i_mode & S_IFMT) {
2919 mutex_lock(&root->fs_info->fs_mutex);
2920 btrfs_defrag_root(root, 0);
2921 btrfs_defrag_root(root->fs_info->extent_root, 0);
2922 mutex_unlock(&root->fs_info->fs_mutex);
2925 btrfs_defrag_file(file);
2932 long btrfs_ioctl(struct file *file, unsigned int
2933 cmd, unsigned long arg)
2935 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2938 case BTRFS_IOC_SNAP_CREATE:
2939 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2940 case BTRFS_IOC_DEFRAG:
2941 return btrfs_ioctl_defrag(file);
2942 case BTRFS_IOC_RESIZE:
2943 return btrfs_ioctl_resize(root, (void __user *)arg);
2950 * Called inside transaction, so use GFP_NOFS
2952 struct inode *btrfs_alloc_inode(struct super_block *sb)
2954 struct btrfs_inode *ei;
2956 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2960 ei->ordered_trans = 0;
2961 return &ei->vfs_inode;
2964 void btrfs_destroy_inode(struct inode *inode)
2966 WARN_ON(!list_empty(&inode->i_dentry));
2967 WARN_ON(inode->i_data.nrpages);
2969 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2970 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2973 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2974 static void init_once(struct kmem_cache * cachep, void *foo)
2976 static void init_once(void * foo, struct kmem_cache * cachep,
2977 unsigned long flags)
2980 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2982 inode_init_once(&ei->vfs_inode);
2985 void btrfs_destroy_cachep(void)
2987 if (btrfs_inode_cachep)
2988 kmem_cache_destroy(btrfs_inode_cachep);
2989 if (btrfs_trans_handle_cachep)
2990 kmem_cache_destroy(btrfs_trans_handle_cachep);
2991 if (btrfs_transaction_cachep)
2992 kmem_cache_destroy(btrfs_transaction_cachep);
2993 if (btrfs_bit_radix_cachep)
2994 kmem_cache_destroy(btrfs_bit_radix_cachep);
2995 if (btrfs_path_cachep)
2996 kmem_cache_destroy(btrfs_path_cachep);
2999 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3000 unsigned long extra_flags,
3001 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3002 void (*ctor)(struct kmem_cache *, void *)
3004 void (*ctor)(void *, struct kmem_cache *,
3009 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3010 SLAB_MEM_SPREAD | extra_flags), ctor
3011 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3017 int btrfs_init_cachep(void)
3019 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3020 sizeof(struct btrfs_inode),
3022 if (!btrfs_inode_cachep)
3024 btrfs_trans_handle_cachep =
3025 btrfs_cache_create("btrfs_trans_handle_cache",
3026 sizeof(struct btrfs_trans_handle),
3028 if (!btrfs_trans_handle_cachep)
3030 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3031 sizeof(struct btrfs_transaction),
3033 if (!btrfs_transaction_cachep)
3035 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3036 sizeof(struct btrfs_path),
3038 if (!btrfs_path_cachep)
3040 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3041 SLAB_DESTROY_BY_RCU, NULL);
3042 if (!btrfs_bit_radix_cachep)
3046 btrfs_destroy_cachep();
3050 static int btrfs_getattr(struct vfsmount *mnt,
3051 struct dentry *dentry, struct kstat *stat)
3053 struct inode *inode = dentry->d_inode;
3054 generic_fillattr(inode, stat);
3055 stat->blksize = PAGE_CACHE_SIZE;
3056 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3060 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3061 struct inode * new_dir,struct dentry *new_dentry)
3063 struct btrfs_trans_handle *trans;
3064 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3065 struct inode *new_inode = new_dentry->d_inode;
3066 struct inode *old_inode = old_dentry->d_inode;
3067 struct timespec ctime = CURRENT_TIME;
3068 struct btrfs_path *path;
3071 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3072 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3076 mutex_lock(&root->fs_info->fs_mutex);
3077 ret = btrfs_check_free_space(root, 1, 0);
3081 trans = btrfs_start_transaction(root, 1);
3083 btrfs_set_trans_block_group(trans, new_dir);
3084 path = btrfs_alloc_path();
3090 old_dentry->d_inode->i_nlink++;
3091 old_dir->i_ctime = old_dir->i_mtime = ctime;
3092 new_dir->i_ctime = new_dir->i_mtime = ctime;
3093 old_inode->i_ctime = ctime;
3095 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3100 new_inode->i_ctime = CURRENT_TIME;
3101 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3105 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3110 btrfs_free_path(path);
3111 btrfs_end_transaction(trans, root);
3113 mutex_unlock(&root->fs_info->fs_mutex);
3117 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3118 const char *symname)
3120 struct btrfs_trans_handle *trans;
3121 struct btrfs_root *root = BTRFS_I(dir)->root;
3122 struct btrfs_path *path;
3123 struct btrfs_key key;
3124 struct inode *inode = NULL;
3131 struct btrfs_file_extent_item *ei;
3132 struct extent_buffer *leaf;
3133 unsigned long nr = 0;
3135 name_len = strlen(symname) + 1;
3136 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3137 return -ENAMETOOLONG;
3139 mutex_lock(&root->fs_info->fs_mutex);
3140 err = btrfs_check_free_space(root, 1, 0);
3144 trans = btrfs_start_transaction(root, 1);
3145 btrfs_set_trans_block_group(trans, dir);
3147 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3153 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3155 dentry->d_parent->d_inode->i_ino, objectid,
3156 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3157 err = PTR_ERR(inode);
3161 btrfs_set_trans_block_group(trans, inode);
3162 err = btrfs_add_nondir(trans, dentry, inode, 0);
3166 inode->i_mapping->a_ops = &btrfs_aops;
3167 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3168 inode->i_fop = &btrfs_file_operations;
3169 inode->i_op = &btrfs_file_inode_operations;
3170 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3171 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3172 inode->i_mapping, GFP_NOFS);
3173 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3174 inode->i_mapping, GFP_NOFS);
3175 BTRFS_I(inode)->delalloc_bytes = 0;
3176 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3178 dir->i_sb->s_dirt = 1;
3179 btrfs_update_inode_block_group(trans, inode);
3180 btrfs_update_inode_block_group(trans, dir);
3184 path = btrfs_alloc_path();
3186 key.objectid = inode->i_ino;
3188 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3189 datasize = btrfs_file_extent_calc_inline_size(name_len);
3190 err = btrfs_insert_empty_item(trans, root, path, &key,
3196 leaf = path->nodes[0];
3197 ei = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_file_extent_item);
3199 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3200 btrfs_set_file_extent_type(leaf, ei,
3201 BTRFS_FILE_EXTENT_INLINE);
3202 ptr = btrfs_file_extent_inline_start(ei);
3203 write_extent_buffer(leaf, symname, ptr, name_len);
3204 btrfs_mark_buffer_dirty(leaf);
3205 btrfs_free_path(path);
3207 inode->i_op = &btrfs_symlink_inode_operations;
3208 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3209 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3210 inode->i_size = name_len - 1;
3211 err = btrfs_update_inode(trans, root, inode);
3216 nr = trans->blocks_used;
3217 btrfs_end_transaction(trans, root);
3219 mutex_unlock(&root->fs_info->fs_mutex);
3221 inode_dec_link_count(inode);
3224 btrfs_btree_balance_dirty(root, nr);
3225 btrfs_throttle(root);
3229 static int btrfs_permission(struct inode *inode, int mask,
3230 struct nameidata *nd)
3232 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3234 return generic_permission(inode, mask, NULL);
3237 static struct inode_operations btrfs_dir_inode_operations = {
3238 .lookup = btrfs_lookup,
3239 .create = btrfs_create,
3240 .unlink = btrfs_unlink,
3242 .mkdir = btrfs_mkdir,
3243 .rmdir = btrfs_rmdir,
3244 .rename = btrfs_rename,
3245 .symlink = btrfs_symlink,
3246 .setattr = btrfs_setattr,
3247 .mknod = btrfs_mknod,
3248 .setxattr = generic_setxattr,
3249 .getxattr = generic_getxattr,
3250 .listxattr = btrfs_listxattr,
3251 .removexattr = generic_removexattr,
3252 .permission = btrfs_permission,
3254 static struct inode_operations btrfs_dir_ro_inode_operations = {
3255 .lookup = btrfs_lookup,
3256 .permission = btrfs_permission,
3258 static struct file_operations btrfs_dir_file_operations = {
3259 .llseek = generic_file_llseek,
3260 .read = generic_read_dir,
3261 .readdir = btrfs_readdir,
3262 .unlocked_ioctl = btrfs_ioctl,
3263 #ifdef CONFIG_COMPAT
3264 .compat_ioctl = btrfs_ioctl,
3268 static struct extent_io_ops btrfs_extent_io_ops = {
3269 .fill_delalloc = run_delalloc_range,
3270 .submit_bio_hook = btrfs_submit_bio_hook,
3271 .merge_bio_hook = btrfs_merge_bio_hook,
3272 .readpage_io_hook = btrfs_readpage_io_hook,
3273 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3274 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3275 .set_bit_hook = btrfs_set_bit_hook,
3276 .clear_bit_hook = btrfs_clear_bit_hook,
3279 static struct address_space_operations btrfs_aops = {
3280 .readpage = btrfs_readpage,
3281 .writepage = btrfs_writepage,
3282 .writepages = btrfs_writepages,
3283 .readpages = btrfs_readpages,
3284 .sync_page = block_sync_page,
3286 .direct_IO = btrfs_direct_IO,
3287 .invalidatepage = btrfs_invalidatepage,
3288 .releasepage = btrfs_releasepage,
3289 .set_page_dirty = __set_page_dirty_nobuffers,
3292 static struct address_space_operations btrfs_symlink_aops = {
3293 .readpage = btrfs_readpage,
3294 .writepage = btrfs_writepage,
3295 .invalidatepage = btrfs_invalidatepage,
3296 .releasepage = btrfs_releasepage,
3299 static struct inode_operations btrfs_file_inode_operations = {
3300 .truncate = btrfs_truncate,
3301 .getattr = btrfs_getattr,
3302 .setattr = btrfs_setattr,
3303 .setxattr = generic_setxattr,
3304 .getxattr = generic_getxattr,
3305 .listxattr = btrfs_listxattr,
3306 .removexattr = generic_removexattr,
3307 .permission = btrfs_permission,
3309 static struct inode_operations btrfs_special_inode_operations = {
3310 .getattr = btrfs_getattr,
3311 .setattr = btrfs_setattr,
3312 .permission = btrfs_permission,
3314 static struct inode_operations btrfs_symlink_inode_operations = {
3315 .readlink = generic_readlink,
3316 .follow_link = page_follow_link_light,
3317 .put_link = page_put_link,
3318 .permission = btrfs_permission,