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 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
127 while(num_bytes > 0) {
128 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
129 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
131 root->root_key.objectid,
133 inode->i_ino, start, 0,
134 alloc_hint, (u64)-1, &ins, 1);
139 cur_alloc_size = ins.offset;
140 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
141 start, ins.objectid, ins.offset,
143 inode->i_blocks += ins.offset >> 9;
144 btrfs_check_file(root, inode);
145 if (num_bytes < cur_alloc_size) {
146 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
150 num_bytes -= cur_alloc_size;
151 alloc_hint = ins.objectid + ins.offset;
152 start += cur_alloc_size;
154 btrfs_drop_extent_cache(inode, orig_start,
155 orig_start + orig_num_bytes - 1);
156 btrfs_add_ordered_inode(inode);
157 btrfs_update_inode(trans, root, inode);
159 btrfs_end_transaction(trans, root);
163 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
171 struct btrfs_root *root = BTRFS_I(inode)->root;
172 struct extent_buffer *leaf;
174 struct btrfs_path *path;
175 struct btrfs_file_extent_item *item;
178 struct btrfs_key found_key;
180 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
181 path = btrfs_alloc_path();
184 ret = btrfs_lookup_file_extent(NULL, root, path,
185 inode->i_ino, start, 0);
187 btrfs_free_path(path);
193 if (path->slots[0] == 0)
198 leaf = path->nodes[0];
199 item = btrfs_item_ptr(leaf, path->slots[0],
200 struct btrfs_file_extent_item);
202 /* are we inside the extent that was found? */
203 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
204 found_type = btrfs_key_type(&found_key);
205 if (found_key.objectid != inode->i_ino ||
206 found_type != BTRFS_EXTENT_DATA_KEY) {
210 found_type = btrfs_file_extent_type(leaf, item);
211 extent_start = found_key.offset;
212 if (found_type == BTRFS_FILE_EXTENT_REG) {
213 u64 extent_num_bytes;
215 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
216 extent_end = extent_start + extent_num_bytes;
219 if (loops && start != extent_start)
222 if (start < extent_start || start >= extent_end)
225 cow_end = min(end, extent_end - 1);
226 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
231 * we may be called by the resizer, make sure we're inside
232 * the limits of the FS
234 if (bytenr + extent_num_bytes > total_fs_bytes)
237 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
247 btrfs_free_path(path);
250 btrfs_release_path(root, path);
255 cow_file_range(inode, start, cow_end);
260 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
262 struct btrfs_root *root = BTRFS_I(inode)->root;
264 mutex_lock(&root->fs_info->fs_mutex);
265 if (btrfs_test_opt(root, NODATACOW) ||
266 btrfs_test_flag(inode, NODATACOW))
267 ret = run_delalloc_nocow(inode, start, end);
269 ret = cow_file_range(inode, start, end);
271 mutex_unlock(&root->fs_info->fs_mutex);
275 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
276 unsigned long old, unsigned long bits)
278 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 spin_lock(&root->fs_info->delalloc_lock);
281 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
282 root->fs_info->delalloc_bytes += end - start + 1;
283 spin_unlock(&root->fs_info->delalloc_lock);
288 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
289 unsigned long old, unsigned long bits)
291 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
292 struct btrfs_root *root = BTRFS_I(inode)->root;
293 spin_lock(&root->fs_info->delalloc_lock);
294 if (end - start + 1 > root->fs_info->delalloc_bytes) {
295 printk("warning: delalloc account %Lu %Lu\n",
296 end - start + 1, root->fs_info->delalloc_bytes);
297 root->fs_info->delalloc_bytes = 0;
298 BTRFS_I(inode)->delalloc_bytes = 0;
300 root->fs_info->delalloc_bytes -= end - start + 1;
301 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
303 spin_unlock(&root->fs_info->delalloc_lock);
308 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
309 size_t size, struct bio *bio)
311 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
312 struct btrfs_mapping_tree *map_tree;
313 u64 logical = bio->bi_sector << 9;
318 length = bio->bi_size;
319 map_tree = &root->fs_info->mapping_tree;
321 ret = btrfs_map_block(map_tree, READ, logical,
322 &map_length, NULL, 0);
324 if (map_length < length + size) {
330 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
333 struct btrfs_root *root = BTRFS_I(inode)->root;
334 struct btrfs_trans_handle *trans;
338 ret = btrfs_csum_one_bio(root, bio, &sums);
341 mutex_lock(&root->fs_info->fs_mutex);
342 trans = btrfs_start_transaction(root, 1);
344 btrfs_set_trans_block_group(trans, inode);
345 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
347 ret = btrfs_end_transaction(trans, root);
349 mutex_unlock(&root->fs_info->fs_mutex);
353 return btrfs_map_bio(root, rw, bio, mirror_num);
356 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
359 struct btrfs_root *root = BTRFS_I(inode)->root;
362 if (!(rw & (1 << BIO_RW))) {
363 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
368 if (btrfs_test_opt(root, NODATASUM) ||
369 btrfs_test_flag(inode, NODATASUM)) {
373 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
374 inode, rw, bio, mirror_num,
375 __btrfs_submit_bio_hook);
377 return btrfs_map_bio(root, rw, bio, mirror_num);
380 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
383 struct inode *inode = page->mapping->host;
384 struct btrfs_root *root = BTRFS_I(inode)->root;
385 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
386 struct btrfs_csum_item *item;
387 struct btrfs_path *path = NULL;
390 if (btrfs_test_opt(root, NODATASUM) ||
391 btrfs_test_flag(inode, NODATASUM))
394 mutex_lock(&root->fs_info->fs_mutex);
395 path = btrfs_alloc_path();
396 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
399 /* a csum that isn't present is a preallocated region. */
400 if (ret == -ENOENT || ret == -EFBIG)
403 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
406 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
408 set_state_private(io_tree, start, csum);
411 btrfs_free_path(path);
412 mutex_unlock(&root->fs_info->fs_mutex);
416 struct io_failure_record {
424 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
425 struct page *page, u64 start, u64 end,
426 struct extent_state *state)
428 struct io_failure_record *failrec = NULL;
430 struct extent_map *em;
431 struct inode *inode = page->mapping->host;
432 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
433 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
439 ret = get_state_private(failure_tree, start, &private);
441 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
444 failrec->start = start;
445 failrec->len = end - start + 1;
446 failrec->last_mirror = 0;
448 spin_lock(&em_tree->lock);
449 em = lookup_extent_mapping(em_tree, start, failrec->len);
450 if (em->start > start || em->start + em->len < start) {
454 spin_unlock(&em_tree->lock);
456 if (!em || IS_ERR(em)) {
460 logical = start - em->start;
461 logical = em->block_start + logical;
462 failrec->logical = logical;
464 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
465 EXTENT_DIRTY, GFP_NOFS);
466 set_state_private(failure_tree, start,
467 (u64)(unsigned long)failrec);
469 failrec = (struct io_failure_record *)(unsigned long)private;
471 num_copies = btrfs_num_copies(
472 &BTRFS_I(inode)->root->fs_info->mapping_tree,
473 failrec->logical, failrec->len);
474 failrec->last_mirror++;
476 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
477 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
480 if (state && state->start != failrec->start)
482 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
484 if (!state || failrec->last_mirror > num_copies) {
485 set_state_private(failure_tree, failrec->start, 0);
486 clear_extent_bits(failure_tree, failrec->start,
487 failrec->start + failrec->len - 1,
488 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
492 bio = bio_alloc(GFP_NOFS, 1);
493 bio->bi_private = state;
494 bio->bi_end_io = failed_bio->bi_end_io;
495 bio->bi_sector = failrec->logical >> 9;
496 bio->bi_bdev = failed_bio->bi_bdev;
497 bio_add_page(bio, page, failrec->len, start - page_offset(page));
498 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
502 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
503 struct extent_state *state)
505 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
506 struct inode *inode = page->mapping->host;
507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
509 u64 private = ~(u32)0;
511 struct btrfs_root *root = BTRFS_I(inode)->root;
515 if (btrfs_test_opt(root, NODATASUM) ||
516 btrfs_test_flag(inode, NODATASUM))
518 if (state && state->start == start) {
519 private = state->private;
522 ret = get_state_private(io_tree, start, &private);
524 local_irq_save(flags);
525 kaddr = kmap_atomic(page, KM_IRQ0);
529 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
530 btrfs_csum_final(csum, (char *)&csum);
531 if (csum != private) {
534 kunmap_atomic(kaddr, KM_IRQ0);
535 local_irq_restore(flags);
537 /* if the io failure tree for this inode is non-empty,
538 * check to see if we've recovered from a failed IO
541 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
542 (u64)-1, 1, EXTENT_DIRTY)) {
544 struct io_failure_record *failure;
545 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
546 start, &private_failure);
548 failure = (struct io_failure_record *)(unsigned long)
550 set_state_private(&BTRFS_I(inode)->io_failure_tree,
552 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
554 failure->start + failure->len - 1,
555 EXTENT_DIRTY | EXTENT_LOCKED,
563 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
564 page->mapping->host->i_ino, (unsigned long long)start, csum,
566 memset(kaddr + offset, 1, end - start + 1);
567 flush_dcache_page(page);
568 kunmap_atomic(kaddr, KM_IRQ0);
569 local_irq_restore(flags);
575 void btrfs_read_locked_inode(struct inode *inode)
577 struct btrfs_path *path;
578 struct extent_buffer *leaf;
579 struct btrfs_inode_item *inode_item;
580 struct btrfs_timespec *tspec;
581 struct btrfs_root *root = BTRFS_I(inode)->root;
582 struct btrfs_key location;
583 u64 alloc_group_block;
587 path = btrfs_alloc_path();
589 mutex_lock(&root->fs_info->fs_mutex);
590 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
592 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
596 leaf = path->nodes[0];
597 inode_item = btrfs_item_ptr(leaf, path->slots[0],
598 struct btrfs_inode_item);
600 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
601 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
602 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
603 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
604 inode->i_size = btrfs_inode_size(leaf, inode_item);
606 tspec = btrfs_inode_atime(inode_item);
607 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
608 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
610 tspec = btrfs_inode_mtime(inode_item);
611 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
612 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
614 tspec = btrfs_inode_ctime(inode_item);
615 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
616 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
618 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
619 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
621 rdev = btrfs_inode_rdev(leaf, inode_item);
623 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
624 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
626 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
627 if (!BTRFS_I(inode)->block_group) {
628 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
630 BTRFS_BLOCK_GROUP_METADATA, 0);
632 btrfs_free_path(path);
635 mutex_unlock(&root->fs_info->fs_mutex);
637 switch (inode->i_mode & S_IFMT) {
639 inode->i_mapping->a_ops = &btrfs_aops;
640 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
641 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
642 inode->i_fop = &btrfs_file_operations;
643 inode->i_op = &btrfs_file_inode_operations;
646 inode->i_fop = &btrfs_dir_file_operations;
647 if (root == root->fs_info->tree_root)
648 inode->i_op = &btrfs_dir_ro_inode_operations;
650 inode->i_op = &btrfs_dir_inode_operations;
653 inode->i_op = &btrfs_symlink_inode_operations;
654 inode->i_mapping->a_ops = &btrfs_symlink_aops;
655 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
658 init_special_inode(inode, inode->i_mode, rdev);
664 btrfs_release_path(root, path);
665 btrfs_free_path(path);
666 mutex_unlock(&root->fs_info->fs_mutex);
667 make_bad_inode(inode);
670 static void fill_inode_item(struct extent_buffer *leaf,
671 struct btrfs_inode_item *item,
674 btrfs_set_inode_uid(leaf, item, inode->i_uid);
675 btrfs_set_inode_gid(leaf, item, inode->i_gid);
676 btrfs_set_inode_size(leaf, item, inode->i_size);
677 btrfs_set_inode_mode(leaf, item, inode->i_mode);
678 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
680 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
681 inode->i_atime.tv_sec);
682 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
683 inode->i_atime.tv_nsec);
685 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
686 inode->i_mtime.tv_sec);
687 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
688 inode->i_mtime.tv_nsec);
690 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
691 inode->i_ctime.tv_sec);
692 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
693 inode->i_ctime.tv_nsec);
695 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
696 btrfs_set_inode_generation(leaf, item, inode->i_generation);
697 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
698 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
699 btrfs_set_inode_block_group(leaf, item,
700 BTRFS_I(inode)->block_group->key.objectid);
703 int btrfs_update_inode(struct btrfs_trans_handle *trans,
704 struct btrfs_root *root,
707 struct btrfs_inode_item *inode_item;
708 struct btrfs_path *path;
709 struct extent_buffer *leaf;
712 path = btrfs_alloc_path();
714 ret = btrfs_lookup_inode(trans, root, path,
715 &BTRFS_I(inode)->location, 1);
722 leaf = path->nodes[0];
723 inode_item = btrfs_item_ptr(leaf, path->slots[0],
724 struct btrfs_inode_item);
726 fill_inode_item(leaf, inode_item, inode);
727 btrfs_mark_buffer_dirty(leaf);
728 btrfs_set_inode_last_trans(trans, inode);
731 btrfs_release_path(root, path);
732 btrfs_free_path(path);
737 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
738 struct btrfs_root *root,
740 struct dentry *dentry)
742 struct btrfs_path *path;
743 const char *name = dentry->d_name.name;
744 int name_len = dentry->d_name.len;
746 struct extent_buffer *leaf;
747 struct btrfs_dir_item *di;
748 struct btrfs_key key;
750 path = btrfs_alloc_path();
756 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
766 leaf = path->nodes[0];
767 btrfs_dir_item_key_to_cpu(leaf, di, &key);
768 ret = btrfs_delete_one_dir_name(trans, root, path, di);
771 btrfs_release_path(root, path);
773 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
774 key.objectid, name, name_len, -1);
783 ret = btrfs_delete_one_dir_name(trans, root, path, di);
785 dentry->d_inode->i_ctime = dir->i_ctime;
786 ret = btrfs_del_inode_ref(trans, root, name, name_len,
787 dentry->d_inode->i_ino,
788 dentry->d_parent->d_inode->i_ino);
790 printk("failed to delete reference to %.*s, "
791 "inode %lu parent %lu\n", name_len, name,
792 dentry->d_inode->i_ino,
793 dentry->d_parent->d_inode->i_ino);
796 btrfs_free_path(path);
798 dir->i_size -= name_len * 2;
799 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
800 btrfs_update_inode(trans, root, dir);
801 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
802 dentry->d_inode->i_nlink--;
804 drop_nlink(dentry->d_inode);
806 ret = btrfs_update_inode(trans, root, dentry->d_inode);
807 dir->i_sb->s_dirt = 1;
812 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
814 struct btrfs_root *root;
815 struct btrfs_trans_handle *trans;
816 struct inode *inode = dentry->d_inode;
818 unsigned long nr = 0;
820 root = BTRFS_I(dir)->root;
821 mutex_lock(&root->fs_info->fs_mutex);
823 ret = btrfs_check_free_space(root, 1, 1);
827 trans = btrfs_start_transaction(root, 1);
829 btrfs_set_trans_block_group(trans, dir);
830 ret = btrfs_unlink_trans(trans, root, dir, dentry);
831 nr = trans->blocks_used;
833 if (inode->i_nlink == 0) {
835 /* if the inode isn't linked anywhere,
836 * we don't need to worry about
839 found = btrfs_del_ordered_inode(inode);
841 atomic_dec(&inode->i_count);
845 btrfs_end_transaction(trans, root);
847 mutex_unlock(&root->fs_info->fs_mutex);
848 btrfs_btree_balance_dirty(root, nr);
849 btrfs_throttle(root);
853 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
855 struct inode *inode = dentry->d_inode;
858 struct btrfs_root *root = BTRFS_I(dir)->root;
859 struct btrfs_trans_handle *trans;
860 unsigned long nr = 0;
862 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
865 mutex_lock(&root->fs_info->fs_mutex);
866 ret = btrfs_check_free_space(root, 1, 1);
870 trans = btrfs_start_transaction(root, 1);
871 btrfs_set_trans_block_group(trans, dir);
873 /* now the directory is empty */
874 err = btrfs_unlink_trans(trans, root, dir, dentry);
879 nr = trans->blocks_used;
880 ret = btrfs_end_transaction(trans, root);
882 mutex_unlock(&root->fs_info->fs_mutex);
883 btrfs_btree_balance_dirty(root, nr);
884 btrfs_throttle(root);
892 * this can truncate away extent items, csum items and directory items.
893 * It starts at a high offset and removes keys until it can't find
894 * any higher than i_size.
896 * csum items that cross the new i_size are truncated to the new size
899 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
905 struct btrfs_path *path;
906 struct btrfs_key key;
907 struct btrfs_key found_key;
909 struct extent_buffer *leaf;
910 struct btrfs_file_extent_item *fi;
911 u64 extent_start = 0;
912 u64 extent_num_bytes = 0;
918 int pending_del_nr = 0;
919 int pending_del_slot = 0;
920 int extent_type = -1;
921 u64 mask = root->sectorsize - 1;
923 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
924 path = btrfs_alloc_path();
928 /* FIXME, add redo link to tree so we don't leak on crash */
929 key.objectid = inode->i_ino;
930 key.offset = (u64)-1;
933 btrfs_init_path(path);
935 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
940 BUG_ON(path->slots[0] == 0);
946 leaf = path->nodes[0];
947 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
948 found_type = btrfs_key_type(&found_key);
950 if (found_key.objectid != inode->i_ino)
953 if (found_type < min_type)
956 item_end = found_key.offset;
957 if (found_type == BTRFS_EXTENT_DATA_KEY) {
958 fi = btrfs_item_ptr(leaf, path->slots[0],
959 struct btrfs_file_extent_item);
960 extent_type = btrfs_file_extent_type(leaf, fi);
961 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
963 btrfs_file_extent_num_bytes(leaf, fi);
964 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
965 struct btrfs_item *item = btrfs_item_nr(leaf,
967 item_end += btrfs_file_extent_inline_len(leaf,
972 if (found_type == BTRFS_CSUM_ITEM_KEY) {
973 ret = btrfs_csum_truncate(trans, root, path,
977 if (item_end < inode->i_size) {
978 if (found_type == BTRFS_DIR_ITEM_KEY) {
979 found_type = BTRFS_INODE_ITEM_KEY;
980 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
981 found_type = BTRFS_CSUM_ITEM_KEY;
982 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
983 found_type = BTRFS_XATTR_ITEM_KEY;
984 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
985 found_type = BTRFS_INODE_REF_KEY;
986 } else if (found_type) {
991 btrfs_set_key_type(&key, found_type);
994 if (found_key.offset >= inode->i_size)
1000 /* FIXME, shrink the extent if the ref count is only 1 */
1001 if (found_type != BTRFS_EXTENT_DATA_KEY)
1004 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1006 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1008 u64 orig_num_bytes =
1009 btrfs_file_extent_num_bytes(leaf, fi);
1010 extent_num_bytes = inode->i_size -
1011 found_key.offset + root->sectorsize - 1;
1012 extent_num_bytes = extent_num_bytes &
1013 ~((u64)root->sectorsize - 1);
1014 btrfs_set_file_extent_num_bytes(leaf, fi,
1016 num_dec = (orig_num_bytes -
1018 if (extent_start != 0)
1019 dec_i_blocks(inode, num_dec);
1020 btrfs_mark_buffer_dirty(leaf);
1023 btrfs_file_extent_disk_num_bytes(leaf,
1025 /* FIXME blocksize != 4096 */
1026 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1027 if (extent_start != 0) {
1029 dec_i_blocks(inode, num_dec);
1031 root_gen = btrfs_header_generation(leaf);
1032 root_owner = btrfs_header_owner(leaf);
1034 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1036 u32 newsize = inode->i_size - found_key.offset;
1037 dec_i_blocks(inode, item_end + 1 -
1038 found_key.offset - newsize);
1040 btrfs_file_extent_calc_inline_size(newsize);
1041 ret = btrfs_truncate_item(trans, root, path,
1045 dec_i_blocks(inode, item_end + 1 -
1051 if (!pending_del_nr) {
1052 /* no pending yet, add ourselves */
1053 pending_del_slot = path->slots[0];
1055 } else if (pending_del_nr &&
1056 path->slots[0] + 1 == pending_del_slot) {
1057 /* hop on the pending chunk */
1059 pending_del_slot = path->slots[0];
1061 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1067 ret = btrfs_free_extent(trans, root, extent_start,
1070 root_gen, inode->i_ino,
1071 found_key.offset, 0);
1075 if (path->slots[0] == 0) {
1078 btrfs_release_path(root, path);
1083 if (pending_del_nr &&
1084 path->slots[0] + 1 != pending_del_slot) {
1085 struct btrfs_key debug;
1087 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1089 ret = btrfs_del_items(trans, root, path,
1094 btrfs_release_path(root, path);
1100 if (pending_del_nr) {
1101 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1104 btrfs_release_path(root, path);
1105 btrfs_free_path(path);
1106 inode->i_sb->s_dirt = 1;
1110 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1114 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1115 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1116 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1119 WARN_ON(!PageLocked(page));
1120 set_page_extent_mapped(page);
1122 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1123 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1124 page_end, GFP_NOFS);
1126 if (zero_start != PAGE_CACHE_SIZE) {
1128 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1129 flush_dcache_page(page);
1132 set_page_dirty(page);
1133 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1139 * taken from block_truncate_page, but does cow as it zeros out
1140 * any bytes left in the last page in the file.
1142 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1144 struct inode *inode = mapping->host;
1145 struct btrfs_root *root = BTRFS_I(inode)->root;
1146 u32 blocksize = root->sectorsize;
1147 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1148 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1153 if ((offset & (blocksize - 1)) == 0)
1157 page = grab_cache_page(mapping, index);
1160 if (!PageUptodate(page)) {
1161 ret = btrfs_readpage(NULL, page);
1163 if (!PageUptodate(page)) {
1168 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1170 ret = btrfs_cow_one_page(inode, page, offset);
1173 page_cache_release(page);
1178 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1180 struct inode *inode = dentry->d_inode;
1183 err = inode_change_ok(inode, attr);
1187 if (S_ISREG(inode->i_mode) &&
1188 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1189 struct btrfs_trans_handle *trans;
1190 struct btrfs_root *root = BTRFS_I(inode)->root;
1191 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1193 u64 mask = root->sectorsize - 1;
1194 u64 hole_start = (inode->i_size + mask) & ~mask;
1195 u64 block_end = (attr->ia_size + mask) & ~mask;
1199 if (attr->ia_size <= hole_start)
1202 mutex_lock(&root->fs_info->fs_mutex);
1203 err = btrfs_check_free_space(root, 1, 0);
1204 mutex_unlock(&root->fs_info->fs_mutex);
1208 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1210 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1211 hole_size = block_end - hole_start;
1213 mutex_lock(&root->fs_info->fs_mutex);
1214 trans = btrfs_start_transaction(root, 1);
1215 btrfs_set_trans_block_group(trans, inode);
1216 err = btrfs_drop_extents(trans, root, inode,
1217 hole_start, block_end, hole_start,
1220 if (alloc_hint != EXTENT_MAP_INLINE) {
1221 err = btrfs_insert_file_extent(trans, root,
1225 btrfs_drop_extent_cache(inode, hole_start,
1227 btrfs_check_file(root, inode);
1229 btrfs_end_transaction(trans, root);
1230 mutex_unlock(&root->fs_info->fs_mutex);
1231 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1236 err = inode_setattr(inode, attr);
1241 void btrfs_put_inode(struct inode *inode)
1245 if (!BTRFS_I(inode)->ordered_trans) {
1249 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1250 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1253 ret = btrfs_del_ordered_inode(inode);
1255 atomic_dec(&inode->i_count);
1259 void btrfs_delete_inode(struct inode *inode)
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_root *root = BTRFS_I(inode)->root;
1266 truncate_inode_pages(&inode->i_data, 0);
1267 if (is_bad_inode(inode)) {
1272 mutex_lock(&root->fs_info->fs_mutex);
1273 trans = btrfs_start_transaction(root, 1);
1275 btrfs_set_trans_block_group(trans, inode);
1276 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1278 goto no_delete_lock;
1280 nr = trans->blocks_used;
1283 btrfs_end_transaction(trans, root);
1284 mutex_unlock(&root->fs_info->fs_mutex);
1285 btrfs_btree_balance_dirty(root, nr);
1286 btrfs_throttle(root);
1290 nr = trans->blocks_used;
1291 btrfs_end_transaction(trans, root);
1292 mutex_unlock(&root->fs_info->fs_mutex);
1293 btrfs_btree_balance_dirty(root, nr);
1294 btrfs_throttle(root);
1300 * this returns the key found in the dir entry in the location pointer.
1301 * If no dir entries were found, location->objectid is 0.
1303 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1304 struct btrfs_key *location)
1306 const char *name = dentry->d_name.name;
1307 int namelen = dentry->d_name.len;
1308 struct btrfs_dir_item *di;
1309 struct btrfs_path *path;
1310 struct btrfs_root *root = BTRFS_I(dir)->root;
1313 if (namelen == 1 && strcmp(name, ".") == 0) {
1314 location->objectid = dir->i_ino;
1315 location->type = BTRFS_INODE_ITEM_KEY;
1316 location->offset = 0;
1319 path = btrfs_alloc_path();
1322 if (namelen == 2 && strcmp(name, "..") == 0) {
1323 struct btrfs_key key;
1324 struct extent_buffer *leaf;
1328 key.objectid = dir->i_ino;
1329 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1335 leaf = path->nodes[0];
1336 slot = path->slots[0];
1337 nritems = btrfs_header_nritems(leaf);
1338 if (slot >= nritems)
1341 btrfs_item_key_to_cpu(leaf, &key, slot);
1342 if (key.objectid != dir->i_ino ||
1343 key.type != BTRFS_INODE_REF_KEY) {
1346 location->objectid = key.offset;
1347 location->type = BTRFS_INODE_ITEM_KEY;
1348 location->offset = 0;
1352 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1356 if (!di || IS_ERR(di)) {
1359 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1361 btrfs_free_path(path);
1364 location->objectid = 0;
1369 * when we hit a tree root in a directory, the btrfs part of the inode
1370 * needs to be changed to reflect the root directory of the tree root. This
1371 * is kind of like crossing a mount point.
1373 static int fixup_tree_root_location(struct btrfs_root *root,
1374 struct btrfs_key *location,
1375 struct btrfs_root **sub_root,
1376 struct dentry *dentry)
1378 struct btrfs_path *path;
1379 struct btrfs_root_item *ri;
1381 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1383 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1386 path = btrfs_alloc_path();
1388 mutex_lock(&root->fs_info->fs_mutex);
1390 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1391 dentry->d_name.name,
1392 dentry->d_name.len);
1393 if (IS_ERR(*sub_root))
1394 return PTR_ERR(*sub_root);
1396 ri = &(*sub_root)->root_item;
1397 location->objectid = btrfs_root_dirid(ri);
1398 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1399 location->offset = 0;
1401 btrfs_free_path(path);
1402 mutex_unlock(&root->fs_info->fs_mutex);
1406 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1408 struct btrfs_iget_args *args = p;
1409 inode->i_ino = args->ino;
1410 BTRFS_I(inode)->root = args->root;
1411 BTRFS_I(inode)->delalloc_bytes = 0;
1412 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1413 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1414 inode->i_mapping, GFP_NOFS);
1415 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1416 inode->i_mapping, GFP_NOFS);
1420 static int btrfs_find_actor(struct inode *inode, void *opaque)
1422 struct btrfs_iget_args *args = opaque;
1423 return (args->ino == inode->i_ino &&
1424 args->root == BTRFS_I(inode)->root);
1427 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1430 struct btrfs_iget_args args;
1431 args.ino = objectid;
1432 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1437 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1440 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1441 struct btrfs_root *root)
1443 struct inode *inode;
1444 struct btrfs_iget_args args;
1445 args.ino = objectid;
1448 inode = iget5_locked(s, objectid, btrfs_find_actor,
1449 btrfs_init_locked_inode,
1454 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1455 struct nameidata *nd)
1457 struct inode * inode;
1458 struct btrfs_inode *bi = BTRFS_I(dir);
1459 struct btrfs_root *root = bi->root;
1460 struct btrfs_root *sub_root = root;
1461 struct btrfs_key location;
1464 if (dentry->d_name.len > BTRFS_NAME_LEN)
1465 return ERR_PTR(-ENAMETOOLONG);
1467 mutex_lock(&root->fs_info->fs_mutex);
1468 ret = btrfs_inode_by_name(dir, dentry, &location);
1469 mutex_unlock(&root->fs_info->fs_mutex);
1472 return ERR_PTR(ret);
1475 if (location.objectid) {
1476 ret = fixup_tree_root_location(root, &location, &sub_root,
1479 return ERR_PTR(ret);
1481 return ERR_PTR(-ENOENT);
1482 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1485 return ERR_PTR(-EACCES);
1486 if (inode->i_state & I_NEW) {
1487 /* the inode and parent dir are two different roots */
1488 if (sub_root != root) {
1490 sub_root->inode = inode;
1492 BTRFS_I(inode)->root = sub_root;
1493 memcpy(&BTRFS_I(inode)->location, &location,
1495 btrfs_read_locked_inode(inode);
1496 unlock_new_inode(inode);
1499 return d_splice_alias(inode, dentry);
1502 static unsigned char btrfs_filetype_table[] = {
1503 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1506 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1508 struct inode *inode = filp->f_dentry->d_inode;
1509 struct btrfs_root *root = BTRFS_I(inode)->root;
1510 struct btrfs_item *item;
1511 struct btrfs_dir_item *di;
1512 struct btrfs_key key;
1513 struct btrfs_key found_key;
1514 struct btrfs_path *path;
1517 struct extent_buffer *leaf;
1520 unsigned char d_type;
1525 int key_type = BTRFS_DIR_INDEX_KEY;
1530 /* FIXME, use a real flag for deciding about the key type */
1531 if (root->fs_info->tree_root == root)
1532 key_type = BTRFS_DIR_ITEM_KEY;
1534 /* special case for "." */
1535 if (filp->f_pos == 0) {
1536 over = filldir(dirent, ".", 1,
1544 mutex_lock(&root->fs_info->fs_mutex);
1545 key.objectid = inode->i_ino;
1546 path = btrfs_alloc_path();
1549 /* special case for .., just use the back ref */
1550 if (filp->f_pos == 1) {
1551 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1553 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1555 leaf = path->nodes[0];
1556 slot = path->slots[0];
1557 nritems = btrfs_header_nritems(leaf);
1558 if (slot >= nritems) {
1559 btrfs_release_path(root, path);
1560 goto read_dir_items;
1562 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1563 btrfs_release_path(root, path);
1564 if (found_key.objectid != key.objectid ||
1565 found_key.type != BTRFS_INODE_REF_KEY)
1566 goto read_dir_items;
1567 over = filldir(dirent, "..", 2,
1568 2, found_key.offset, DT_DIR);
1575 btrfs_set_key_type(&key, key_type);
1576 key.offset = filp->f_pos;
1578 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1583 leaf = path->nodes[0];
1584 nritems = btrfs_header_nritems(leaf);
1585 slot = path->slots[0];
1586 if (advance || slot >= nritems) {
1587 if (slot >= nritems -1) {
1588 ret = btrfs_next_leaf(root, path);
1591 leaf = path->nodes[0];
1592 nritems = btrfs_header_nritems(leaf);
1593 slot = path->slots[0];
1600 item = btrfs_item_nr(leaf, slot);
1601 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1603 if (found_key.objectid != key.objectid)
1605 if (btrfs_key_type(&found_key) != key_type)
1607 if (found_key.offset < filp->f_pos)
1610 filp->f_pos = found_key.offset;
1612 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1614 di_total = btrfs_item_size(leaf, item);
1615 while(di_cur < di_total) {
1616 struct btrfs_key location;
1618 name_len = btrfs_dir_name_len(leaf, di);
1619 if (name_len < 32) {
1620 name_ptr = tmp_name;
1622 name_ptr = kmalloc(name_len, GFP_NOFS);
1625 read_extent_buffer(leaf, name_ptr,
1626 (unsigned long)(di + 1), name_len);
1628 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1629 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1630 over = filldir(dirent, name_ptr, name_len,
1635 if (name_ptr != tmp_name)
1640 di_len = btrfs_dir_name_len(leaf, di) +
1641 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1643 di = (struct btrfs_dir_item *)((char *)di + di_len);
1646 if (key_type == BTRFS_DIR_INDEX_KEY)
1647 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1653 btrfs_release_path(root, path);
1654 btrfs_free_path(path);
1655 mutex_unlock(&root->fs_info->fs_mutex);
1659 int btrfs_write_inode(struct inode *inode, int wait)
1661 struct btrfs_root *root = BTRFS_I(inode)->root;
1662 struct btrfs_trans_handle *trans;
1666 mutex_lock(&root->fs_info->fs_mutex);
1667 trans = btrfs_start_transaction(root, 1);
1668 btrfs_set_trans_block_group(trans, inode);
1669 ret = btrfs_commit_transaction(trans, root);
1670 mutex_unlock(&root->fs_info->fs_mutex);
1676 * This is somewhat expensive, updating the tree every time the
1677 * inode changes. But, it is most likely to find the inode in cache.
1678 * FIXME, needs more benchmarking...there are no reasons other than performance
1679 * to keep or drop this code.
1681 void btrfs_dirty_inode(struct inode *inode)
1683 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 struct btrfs_trans_handle *trans;
1686 mutex_lock(&root->fs_info->fs_mutex);
1687 trans = btrfs_start_transaction(root, 1);
1688 btrfs_set_trans_block_group(trans, inode);
1689 btrfs_update_inode(trans, root, inode);
1690 btrfs_end_transaction(trans, root);
1691 mutex_unlock(&root->fs_info->fs_mutex);
1694 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
1696 const char *name, int name_len,
1699 struct btrfs_block_group_cache *group,
1702 struct inode *inode;
1703 struct btrfs_inode_item *inode_item;
1704 struct btrfs_block_group_cache *new_inode_group;
1705 struct btrfs_key *location;
1706 struct btrfs_path *path;
1707 struct btrfs_inode_ref *ref;
1708 struct btrfs_key key[2];
1714 path = btrfs_alloc_path();
1717 inode = new_inode(root->fs_info->sb);
1719 return ERR_PTR(-ENOMEM);
1721 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1722 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1723 inode->i_mapping, GFP_NOFS);
1724 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1725 inode->i_mapping, GFP_NOFS);
1726 BTRFS_I(inode)->delalloc_bytes = 0;
1727 BTRFS_I(inode)->root = root;
1733 new_inode_group = btrfs_find_block_group(root, group, 0,
1734 BTRFS_BLOCK_GROUP_METADATA, owner);
1735 if (!new_inode_group) {
1736 printk("find_block group failed\n");
1737 new_inode_group = group;
1739 BTRFS_I(inode)->block_group = new_inode_group;
1740 BTRFS_I(inode)->flags = 0;
1742 key[0].objectid = objectid;
1743 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1746 key[1].objectid = objectid;
1747 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1748 key[1].offset = ref_objectid;
1750 sizes[0] = sizeof(struct btrfs_inode_item);
1751 sizes[1] = name_len + sizeof(*ref);
1753 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1757 if (objectid > root->highest_inode)
1758 root->highest_inode = objectid;
1760 inode->i_uid = current->fsuid;
1761 inode->i_gid = current->fsgid;
1762 inode->i_mode = mode;
1763 inode->i_ino = objectid;
1764 inode->i_blocks = 0;
1765 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1766 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1767 struct btrfs_inode_item);
1768 fill_inode_item(path->nodes[0], inode_item, inode);
1770 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1771 struct btrfs_inode_ref);
1772 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1773 ptr = (unsigned long)(ref + 1);
1774 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1776 btrfs_mark_buffer_dirty(path->nodes[0]);
1777 btrfs_free_path(path);
1779 location = &BTRFS_I(inode)->location;
1780 location->objectid = objectid;
1781 location->offset = 0;
1782 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1784 insert_inode_hash(inode);
1787 btrfs_free_path(path);
1788 return ERR_PTR(ret);
1791 static inline u8 btrfs_inode_type(struct inode *inode)
1793 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1796 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1797 struct dentry *dentry, struct inode *inode,
1801 struct btrfs_key key;
1802 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1803 struct inode *parent_inode;
1805 key.objectid = inode->i_ino;
1806 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1809 ret = btrfs_insert_dir_item(trans, root,
1810 dentry->d_name.name, dentry->d_name.len,
1811 dentry->d_parent->d_inode->i_ino,
1812 &key, btrfs_inode_type(inode));
1815 ret = btrfs_insert_inode_ref(trans, root,
1816 dentry->d_name.name,
1819 dentry->d_parent->d_inode->i_ino);
1821 parent_inode = dentry->d_parent->d_inode;
1822 parent_inode->i_size += dentry->d_name.len * 2;
1823 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1824 ret = btrfs_update_inode(trans, root,
1825 dentry->d_parent->d_inode);
1830 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1831 struct dentry *dentry, struct inode *inode,
1834 int err = btrfs_add_link(trans, dentry, inode, backref);
1836 d_instantiate(dentry, inode);
1844 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1845 int mode, dev_t rdev)
1847 struct btrfs_trans_handle *trans;
1848 struct btrfs_root *root = BTRFS_I(dir)->root;
1849 struct inode *inode = NULL;
1853 unsigned long nr = 0;
1855 if (!new_valid_dev(rdev))
1858 mutex_lock(&root->fs_info->fs_mutex);
1859 err = btrfs_check_free_space(root, 1, 0);
1863 trans = btrfs_start_transaction(root, 1);
1864 btrfs_set_trans_block_group(trans, dir);
1866 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1872 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1874 dentry->d_parent->d_inode->i_ino, objectid,
1875 BTRFS_I(dir)->block_group, mode);
1876 err = PTR_ERR(inode);
1880 btrfs_set_trans_block_group(trans, inode);
1881 err = btrfs_add_nondir(trans, dentry, inode, 0);
1885 inode->i_op = &btrfs_special_inode_operations;
1886 init_special_inode(inode, inode->i_mode, rdev);
1887 btrfs_update_inode(trans, root, inode);
1889 dir->i_sb->s_dirt = 1;
1890 btrfs_update_inode_block_group(trans, inode);
1891 btrfs_update_inode_block_group(trans, dir);
1893 nr = trans->blocks_used;
1894 btrfs_end_transaction(trans, root);
1896 mutex_unlock(&root->fs_info->fs_mutex);
1899 inode_dec_link_count(inode);
1902 btrfs_btree_balance_dirty(root, nr);
1903 btrfs_throttle(root);
1907 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1908 int mode, struct nameidata *nd)
1910 struct btrfs_trans_handle *trans;
1911 struct btrfs_root *root = BTRFS_I(dir)->root;
1912 struct inode *inode = NULL;
1915 unsigned long nr = 0;
1918 mutex_lock(&root->fs_info->fs_mutex);
1919 err = btrfs_check_free_space(root, 1, 0);
1922 trans = btrfs_start_transaction(root, 1);
1923 btrfs_set_trans_block_group(trans, dir);
1925 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1931 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1933 dentry->d_parent->d_inode->i_ino,
1934 objectid, BTRFS_I(dir)->block_group, mode);
1935 err = PTR_ERR(inode);
1939 btrfs_set_trans_block_group(trans, inode);
1940 err = btrfs_add_nondir(trans, dentry, inode, 0);
1944 inode->i_mapping->a_ops = &btrfs_aops;
1945 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1946 inode->i_fop = &btrfs_file_operations;
1947 inode->i_op = &btrfs_file_inode_operations;
1948 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1949 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1950 inode->i_mapping, GFP_NOFS);
1951 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1952 inode->i_mapping, GFP_NOFS);
1953 BTRFS_I(inode)->delalloc_bytes = 0;
1954 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1956 dir->i_sb->s_dirt = 1;
1957 btrfs_update_inode_block_group(trans, inode);
1958 btrfs_update_inode_block_group(trans, dir);
1960 nr = trans->blocks_used;
1961 btrfs_end_transaction(trans, root);
1963 mutex_unlock(&root->fs_info->fs_mutex);
1966 inode_dec_link_count(inode);
1969 btrfs_btree_balance_dirty(root, nr);
1970 btrfs_throttle(root);
1974 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1975 struct dentry *dentry)
1977 struct btrfs_trans_handle *trans;
1978 struct btrfs_root *root = BTRFS_I(dir)->root;
1979 struct inode *inode = old_dentry->d_inode;
1980 unsigned long nr = 0;
1984 if (inode->i_nlink == 0)
1987 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1992 mutex_lock(&root->fs_info->fs_mutex);
1993 err = btrfs_check_free_space(root, 1, 0);
1996 trans = btrfs_start_transaction(root, 1);
1998 btrfs_set_trans_block_group(trans, dir);
1999 atomic_inc(&inode->i_count);
2000 err = btrfs_add_nondir(trans, dentry, inode, 1);
2005 dir->i_sb->s_dirt = 1;
2006 btrfs_update_inode_block_group(trans, dir);
2007 err = btrfs_update_inode(trans, root, inode);
2012 nr = trans->blocks_used;
2013 btrfs_end_transaction(trans, root);
2015 mutex_unlock(&root->fs_info->fs_mutex);
2018 inode_dec_link_count(inode);
2021 btrfs_btree_balance_dirty(root, nr);
2022 btrfs_throttle(root);
2026 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2028 struct inode *inode;
2029 struct btrfs_trans_handle *trans;
2030 struct btrfs_root *root = BTRFS_I(dir)->root;
2032 int drop_on_err = 0;
2034 unsigned long nr = 1;
2036 mutex_lock(&root->fs_info->fs_mutex);
2037 err = btrfs_check_free_space(root, 1, 0);
2041 trans = btrfs_start_transaction(root, 1);
2042 btrfs_set_trans_block_group(trans, dir);
2044 if (IS_ERR(trans)) {
2045 err = PTR_ERR(trans);
2049 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2055 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2057 dentry->d_parent->d_inode->i_ino, objectid,
2058 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2059 if (IS_ERR(inode)) {
2060 err = PTR_ERR(inode);
2065 inode->i_op = &btrfs_dir_inode_operations;
2066 inode->i_fop = &btrfs_dir_file_operations;
2067 btrfs_set_trans_block_group(trans, inode);
2070 err = btrfs_update_inode(trans, root, inode);
2074 err = btrfs_add_link(trans, dentry, inode, 0);
2078 d_instantiate(dentry, inode);
2080 dir->i_sb->s_dirt = 1;
2081 btrfs_update_inode_block_group(trans, inode);
2082 btrfs_update_inode_block_group(trans, dir);
2085 nr = trans->blocks_used;
2086 btrfs_end_transaction(trans, root);
2089 mutex_unlock(&root->fs_info->fs_mutex);
2092 btrfs_btree_balance_dirty(root, nr);
2093 btrfs_throttle(root);
2097 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2098 struct extent_map *existing,
2099 struct extent_map *em)
2104 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2106 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2109 if (!real_blocks && em->block_start != existing->block_start)
2112 new_end = max(existing->start + existing->len, em->start + em->len);
2114 if (existing->start >= em->start) {
2115 if (em->start + em->len < existing->start)
2118 start_diff = existing->start - em->start;
2119 if (real_blocks && em->block_start + start_diff !=
2120 existing->block_start)
2123 em->len = new_end - em->start;
2125 remove_extent_mapping(em_tree, existing);
2126 /* free for the tree */
2127 free_extent_map(existing);
2128 ret = add_extent_mapping(em_tree, em);
2130 } else if (em->start > existing->start) {
2132 if (existing->start + existing->len < em->start)
2135 start_diff = em->start - existing->start;
2136 if (real_blocks && existing->block_start + start_diff !=
2140 remove_extent_mapping(em_tree, existing);
2141 em->block_start = existing->block_start;
2142 em->start = existing->start;
2143 em->len = new_end - existing->start;
2144 free_extent_map(existing);
2146 ret = add_extent_mapping(em_tree, em);
2153 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2154 existing->start, existing->len, existing->block_start,
2155 em->start, em->len, em->block_start);
2159 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2160 size_t pg_offset, u64 start, u64 len,
2166 u64 extent_start = 0;
2168 u64 objectid = inode->i_ino;
2170 struct btrfs_path *path;
2171 struct btrfs_root *root = BTRFS_I(inode)->root;
2172 struct btrfs_file_extent_item *item;
2173 struct extent_buffer *leaf;
2174 struct btrfs_key found_key;
2175 struct extent_map *em = NULL;
2176 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2177 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2178 struct btrfs_trans_handle *trans = NULL;
2180 path = btrfs_alloc_path();
2182 mutex_lock(&root->fs_info->fs_mutex);
2185 spin_lock(&em_tree->lock);
2186 em = lookup_extent_mapping(em_tree, start, len);
2187 spin_unlock(&em_tree->lock);
2190 if (em->start > start) {
2191 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2192 start, len, em->start, em->len);
2195 if (em->block_start == EXTENT_MAP_INLINE && page)
2196 free_extent_map(em);
2200 em = alloc_extent_map(GFP_NOFS);
2206 em->start = EXTENT_MAP_HOLE;
2208 em->bdev = inode->i_sb->s_bdev;
2209 ret = btrfs_lookup_file_extent(trans, root, path,
2210 objectid, start, trans != NULL);
2217 if (path->slots[0] == 0)
2222 leaf = path->nodes[0];
2223 item = btrfs_item_ptr(leaf, path->slots[0],
2224 struct btrfs_file_extent_item);
2225 /* are we inside the extent that was found? */
2226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2227 found_type = btrfs_key_type(&found_key);
2228 if (found_key.objectid != objectid ||
2229 found_type != BTRFS_EXTENT_DATA_KEY) {
2233 found_type = btrfs_file_extent_type(leaf, item);
2234 extent_start = found_key.offset;
2235 if (found_type == BTRFS_FILE_EXTENT_REG) {
2236 extent_end = extent_start +
2237 btrfs_file_extent_num_bytes(leaf, item);
2239 if (start < extent_start || start >= extent_end) {
2241 if (start < extent_start) {
2242 if (start + len <= extent_start)
2244 em->len = extent_end - extent_start;
2250 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2252 em->start = extent_start;
2253 em->len = extent_end - extent_start;
2254 em->block_start = EXTENT_MAP_HOLE;
2257 bytenr += btrfs_file_extent_offset(leaf, item);
2258 em->block_start = bytenr;
2259 em->start = extent_start;
2260 em->len = extent_end - extent_start;
2262 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2267 size_t extent_offset;
2270 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2272 extent_end = (extent_start + size + root->sectorsize - 1) &
2273 ~((u64)root->sectorsize - 1);
2274 if (start < extent_start || start >= extent_end) {
2276 if (start < extent_start) {
2277 if (start + len <= extent_start)
2279 em->len = extent_end - extent_start;
2285 em->block_start = EXTENT_MAP_INLINE;
2288 em->start = extent_start;
2293 page_start = page_offset(page) + pg_offset;
2294 extent_offset = page_start - extent_start;
2295 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2296 size - extent_offset);
2297 em->start = extent_start + extent_offset;
2298 em->len = (copy_size + root->sectorsize - 1) &
2299 ~((u64)root->sectorsize - 1);
2301 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2302 if (create == 0 && !PageUptodate(page)) {
2303 read_extent_buffer(leaf, map + pg_offset, ptr,
2305 flush_dcache_page(page);
2306 } else if (create && PageUptodate(page)) {
2309 free_extent_map(em);
2311 btrfs_release_path(root, path);
2312 trans = btrfs_start_transaction(root, 1);
2315 write_extent_buffer(leaf, map + pg_offset, ptr,
2317 btrfs_mark_buffer_dirty(leaf);
2320 set_extent_uptodate(io_tree, em->start,
2321 extent_map_end(em) - 1, GFP_NOFS);
2324 printk("unkknown found_type %d\n", found_type);
2331 em->block_start = EXTENT_MAP_HOLE;
2333 btrfs_release_path(root, path);
2334 if (em->start > start || extent_map_end(em) <= start) {
2335 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2341 spin_lock(&em_tree->lock);
2342 ret = add_extent_mapping(em_tree, em);
2344 /* it is possible that someone inserted the extent into the tree
2345 * while we had the lock dropped. It is also possible that
2346 * an overlapping map exists in the tree
2348 if (ret == -EEXIST) {
2349 struct extent_map *existing;
2350 existing = lookup_extent_mapping(em_tree, start, len);
2352 existing = lookup_extent_mapping(em_tree, em->start,
2355 err = merge_extent_mapping(em_tree, existing,
2357 free_extent_map(existing);
2359 free_extent_map(em);
2364 printk("failing to insert %Lu %Lu\n",
2366 free_extent_map(em);
2370 free_extent_map(em);
2374 spin_unlock(&em_tree->lock);
2376 btrfs_free_path(path);
2378 ret = btrfs_end_transaction(trans, root);
2382 mutex_unlock(&root->fs_info->fs_mutex);
2384 free_extent_map(em);
2386 return ERR_PTR(err);
2391 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2392 struct buffer_head *bh_result, int create)
2394 struct extent_map *em;
2395 u64 start = (u64)iblock << inode->i_blkbits;
2396 struct btrfs_multi_bio *multi = NULL;
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2403 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2405 if (!em || IS_ERR(em))
2408 if (em->start > start || em->start + em->len <= start)
2411 if (em->block_start == EXTENT_MAP_INLINE) {
2416 if (em->block_start == EXTENT_MAP_HOLE ||
2417 em->block_start == EXTENT_MAP_DELALLOC) {
2421 len = em->start + em->len - start;
2422 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2424 logical = start - em->start;
2425 logical = em->block_start + logical;
2428 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2429 logical, &map_length, &multi, 0);
2431 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2432 bh_result->b_size = min(map_length, len);
2433 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2434 set_buffer_mapped(bh_result);
2437 free_extent_map(em);
2441 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2442 const struct iovec *iov, loff_t offset,
2443 unsigned long nr_segs)
2445 struct file *file = iocb->ki_filp;
2446 struct inode *inode = file->f_mapping->host;
2451 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2452 offset, nr_segs, btrfs_get_block, NULL);
2455 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2457 return extent_bmap(mapping, iblock, btrfs_get_extent);
2460 int btrfs_readpage(struct file *file, struct page *page)
2462 struct extent_io_tree *tree;
2463 tree = &BTRFS_I(page->mapping->host)->io_tree;
2464 return extent_read_full_page(tree, page, btrfs_get_extent);
2467 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2469 struct extent_io_tree *tree;
2472 if (current->flags & PF_MEMALLOC) {
2473 redirty_page_for_writepage(wbc, page);
2477 tree = &BTRFS_I(page->mapping->host)->io_tree;
2478 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2481 static int btrfs_writepages(struct address_space *mapping,
2482 struct writeback_control *wbc)
2484 struct extent_io_tree *tree;
2485 tree = &BTRFS_I(mapping->host)->io_tree;
2486 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2490 btrfs_readpages(struct file *file, struct address_space *mapping,
2491 struct list_head *pages, unsigned nr_pages)
2493 struct extent_io_tree *tree;
2494 tree = &BTRFS_I(mapping->host)->io_tree;
2495 return extent_readpages(tree, mapping, pages, nr_pages,
2499 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2501 struct extent_io_tree *tree;
2502 struct extent_map_tree *map;
2505 tree = &BTRFS_I(page->mapping->host)->io_tree;
2506 map = &BTRFS_I(page->mapping->host)->extent_tree;
2507 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2509 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2510 ClearPagePrivate(page);
2511 set_page_private(page, 0);
2512 page_cache_release(page);
2517 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2519 struct extent_io_tree *tree;
2521 tree = &BTRFS_I(page->mapping->host)->io_tree;
2522 extent_invalidatepage(tree, page, offset);
2523 btrfs_releasepage(page, GFP_NOFS);
2524 if (PagePrivate(page)) {
2525 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2526 ClearPagePrivate(page);
2527 set_page_private(page, 0);
2528 page_cache_release(page);
2533 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2534 * called from a page fault handler when a page is first dirtied. Hence we must
2535 * be careful to check for EOF conditions here. We set the page up correctly
2536 * for a written page which means we get ENOSPC checking when writing into
2537 * holes and correct delalloc and unwritten extent mapping on filesystems that
2538 * support these features.
2540 * We are not allowed to take the i_mutex here so we have to play games to
2541 * protect against truncate races as the page could now be beyond EOF. Because
2542 * vmtruncate() writes the inode size before removing pages, once we have the
2543 * page lock we can determine safely if the page is beyond EOF. If it is not
2544 * beyond EOF, then the page is guaranteed safe against truncation until we
2547 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2549 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2550 struct btrfs_root *root = BTRFS_I(inode)->root;
2556 mutex_lock(&root->fs_info->fs_mutex);
2557 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2558 mutex_unlock(&root->fs_info->fs_mutex);
2565 wait_on_page_writeback(page);
2566 size = i_size_read(inode);
2567 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2569 if ((page->mapping != inode->i_mapping) ||
2570 (page_start > size)) {
2571 /* page got truncated out from underneath us */
2575 /* page is wholly or partially inside EOF */
2576 if (page_start + PAGE_CACHE_SIZE > size)
2577 end = size & ~PAGE_CACHE_MASK;
2579 end = PAGE_CACHE_SIZE;
2581 ret = btrfs_cow_one_page(inode, page, end);
2589 static void btrfs_truncate(struct inode *inode)
2591 struct btrfs_root *root = BTRFS_I(inode)->root;
2593 struct btrfs_trans_handle *trans;
2596 if (!S_ISREG(inode->i_mode))
2598 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2601 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2603 mutex_lock(&root->fs_info->fs_mutex);
2604 trans = btrfs_start_transaction(root, 1);
2605 btrfs_set_trans_block_group(trans, inode);
2607 /* FIXME, add redo link to tree so we don't leak on crash */
2608 ret = btrfs_truncate_in_trans(trans, root, inode,
2609 BTRFS_EXTENT_DATA_KEY);
2610 btrfs_update_inode(trans, root, inode);
2611 nr = trans->blocks_used;
2613 ret = btrfs_end_transaction(trans, root);
2615 mutex_unlock(&root->fs_info->fs_mutex);
2616 btrfs_btree_balance_dirty(root, nr);
2617 btrfs_throttle(root);
2620 static int noinline create_subvol(struct btrfs_root *root, char *name,
2623 struct btrfs_trans_handle *trans;
2624 struct btrfs_key key;
2625 struct btrfs_root_item root_item;
2626 struct btrfs_inode_item *inode_item;
2627 struct extent_buffer *leaf;
2628 struct btrfs_root *new_root = root;
2629 struct inode *inode;
2634 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2635 unsigned long nr = 1;
2637 mutex_lock(&root->fs_info->fs_mutex);
2638 ret = btrfs_check_free_space(root, 1, 0);
2642 trans = btrfs_start_transaction(root, 1);
2645 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2650 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2651 objectid, trans->transid, 0, 0,
2654 return PTR_ERR(leaf);
2656 btrfs_set_header_nritems(leaf, 0);
2657 btrfs_set_header_level(leaf, 0);
2658 btrfs_set_header_bytenr(leaf, leaf->start);
2659 btrfs_set_header_generation(leaf, trans->transid);
2660 btrfs_set_header_owner(leaf, objectid);
2662 write_extent_buffer(leaf, root->fs_info->fsid,
2663 (unsigned long)btrfs_header_fsid(leaf),
2665 btrfs_mark_buffer_dirty(leaf);
2667 inode_item = &root_item.inode;
2668 memset(inode_item, 0, sizeof(*inode_item));
2669 inode_item->generation = cpu_to_le64(1);
2670 inode_item->size = cpu_to_le64(3);
2671 inode_item->nlink = cpu_to_le32(1);
2672 inode_item->nblocks = cpu_to_le64(1);
2673 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2675 btrfs_set_root_bytenr(&root_item, leaf->start);
2676 btrfs_set_root_level(&root_item, 0);
2677 btrfs_set_root_refs(&root_item, 1);
2678 btrfs_set_root_used(&root_item, 0);
2680 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2681 root_item.drop_level = 0;
2683 free_extent_buffer(leaf);
2686 btrfs_set_root_dirid(&root_item, new_dirid);
2688 key.objectid = objectid;
2690 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2691 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2697 * insert the directory item
2699 key.offset = (u64)-1;
2700 dir = root->fs_info->sb->s_root->d_inode;
2701 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2702 name, namelen, dir->i_ino, &key,
2707 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2708 name, namelen, objectid,
2709 root->fs_info->sb->s_root->d_inode->i_ino);
2713 ret = btrfs_commit_transaction(trans, root);
2717 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2720 trans = btrfs_start_transaction(new_root, 1);
2723 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2725 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2728 inode->i_op = &btrfs_dir_inode_operations;
2729 inode->i_fop = &btrfs_dir_file_operations;
2730 new_root->inode = inode;
2732 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2736 ret = btrfs_update_inode(trans, new_root, inode);
2740 nr = trans->blocks_used;
2741 err = btrfs_commit_transaction(trans, new_root);
2745 mutex_unlock(&root->fs_info->fs_mutex);
2746 btrfs_btree_balance_dirty(root, nr);
2747 btrfs_throttle(root);
2751 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2753 struct btrfs_pending_snapshot *pending_snapshot;
2754 struct btrfs_trans_handle *trans;
2757 unsigned long nr = 0;
2759 if (!root->ref_cows)
2762 mutex_lock(&root->fs_info->fs_mutex);
2763 ret = btrfs_check_free_space(root, 1, 0);
2767 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2768 if (!pending_snapshot) {
2772 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2773 if (!pending_snapshot->name) {
2775 kfree(pending_snapshot);
2778 memcpy(pending_snapshot->name, name, namelen);
2779 pending_snapshot->name[namelen] = '\0';
2780 trans = btrfs_start_transaction(root, 1);
2782 pending_snapshot->root = root;
2783 list_add(&pending_snapshot->list,
2784 &trans->transaction->pending_snapshots);
2785 ret = btrfs_update_inode(trans, root, root->inode);
2786 err = btrfs_commit_transaction(trans, root);
2789 mutex_unlock(&root->fs_info->fs_mutex);
2790 btrfs_btree_balance_dirty(root, nr);
2791 btrfs_throttle(root);
2795 unsigned long btrfs_force_ra(struct address_space *mapping,
2796 struct file_ra_state *ra, struct file *file,
2797 pgoff_t offset, pgoff_t last_index)
2801 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2802 req_size = last_index - offset + 1;
2803 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2806 req_size = min(last_index - offset + 1, (pgoff_t)128);
2807 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2808 return offset + req_size;
2812 int btrfs_defrag_file(struct file *file) {
2813 struct inode *inode = fdentry(file)->d_inode;
2814 struct btrfs_root *root = BTRFS_I(inode)->root;
2815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2817 unsigned long last_index;
2818 unsigned long ra_index = 0;
2824 mutex_lock(&root->fs_info->fs_mutex);
2825 ret = btrfs_check_free_space(root, inode->i_size, 0);
2826 mutex_unlock(&root->fs_info->fs_mutex);
2830 mutex_lock(&inode->i_mutex);
2831 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2832 for (i = 0; i <= last_index; i++) {
2833 if (i == ra_index) {
2834 ra_index = btrfs_force_ra(inode->i_mapping,
2836 file, ra_index, last_index);
2838 page = grab_cache_page(inode->i_mapping, i);
2841 if (!PageUptodate(page)) {
2842 btrfs_readpage(NULL, page);
2844 if (!PageUptodate(page)) {
2846 page_cache_release(page);
2850 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2851 page_end = page_start + PAGE_CACHE_SIZE - 1;
2853 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2854 set_extent_delalloc(io_tree, page_start,
2855 page_end, GFP_NOFS);
2857 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2858 set_page_dirty(page);
2860 page_cache_release(page);
2861 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2865 mutex_unlock(&inode->i_mutex);
2869 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2873 struct btrfs_ioctl_vol_args *vol_args;
2874 struct btrfs_trans_handle *trans;
2880 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2885 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2889 namelen = strlen(vol_args->name);
2890 if (namelen > BTRFS_VOL_NAME_MAX) {
2895 sizestr = vol_args->name;
2896 if (!strcmp(sizestr, "max"))
2897 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2899 if (sizestr[0] == '-') {
2902 } else if (sizestr[0] == '+') {
2906 new_size = btrfs_parse_size(sizestr);
2907 if (new_size == 0) {
2913 mutex_lock(&root->fs_info->fs_mutex);
2914 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2917 if (new_size > old_size) {
2921 new_size = old_size - new_size;
2922 } else if (mod > 0) {
2923 new_size = old_size + new_size;
2926 if (new_size < 256 * 1024 * 1024) {
2930 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2935 do_div(new_size, root->sectorsize);
2936 new_size *= root->sectorsize;
2938 printk("new size is %Lu\n", new_size);
2939 if (new_size > old_size) {
2940 trans = btrfs_start_transaction(root, 1);
2941 ret = btrfs_grow_extent_tree(trans, root, new_size);
2942 btrfs_commit_transaction(trans, root);
2944 ret = btrfs_shrink_extent_tree(root, new_size);
2948 mutex_unlock(&root->fs_info->fs_mutex);
2954 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2957 struct btrfs_ioctl_vol_args *vol_args;
2958 struct btrfs_dir_item *di;
2959 struct btrfs_path *path;
2964 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2969 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2974 namelen = strlen(vol_args->name);
2975 if (namelen > BTRFS_VOL_NAME_MAX) {
2979 if (strchr(vol_args->name, '/')) {
2984 path = btrfs_alloc_path();
2990 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2991 mutex_lock(&root->fs_info->fs_mutex);
2992 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2994 vol_args->name, namelen, 0);
2995 mutex_unlock(&root->fs_info->fs_mutex);
2996 btrfs_free_path(path);
2998 if (di && !IS_ERR(di)) {
3008 if (root == root->fs_info->tree_root)
3009 ret = create_subvol(root, vol_args->name, namelen);
3011 ret = create_snapshot(root, vol_args->name, namelen);
3017 static int btrfs_ioctl_defrag(struct file *file)
3019 struct inode *inode = fdentry(file)->d_inode;
3020 struct btrfs_root *root = BTRFS_I(inode)->root;
3022 switch (inode->i_mode & S_IFMT) {
3024 mutex_lock(&root->fs_info->fs_mutex);
3025 btrfs_defrag_root(root, 0);
3026 btrfs_defrag_root(root->fs_info->extent_root, 0);
3027 mutex_unlock(&root->fs_info->fs_mutex);
3030 btrfs_defrag_file(file);
3037 long btrfs_ioctl(struct file *file, unsigned int
3038 cmd, unsigned long arg)
3040 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3043 case BTRFS_IOC_SNAP_CREATE:
3044 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3045 case BTRFS_IOC_DEFRAG:
3046 return btrfs_ioctl_defrag(file);
3047 case BTRFS_IOC_RESIZE:
3048 return btrfs_ioctl_resize(root, (void __user *)arg);
3055 * Called inside transaction, so use GFP_NOFS
3057 struct inode *btrfs_alloc_inode(struct super_block *sb)
3059 struct btrfs_inode *ei;
3061 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3065 ei->ordered_trans = 0;
3066 return &ei->vfs_inode;
3069 void btrfs_destroy_inode(struct inode *inode)
3071 WARN_ON(!list_empty(&inode->i_dentry));
3072 WARN_ON(inode->i_data.nrpages);
3074 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3075 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3078 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3079 static void init_once(struct kmem_cache * cachep, void *foo)
3081 static void init_once(void * foo, struct kmem_cache * cachep,
3082 unsigned long flags)
3085 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3087 inode_init_once(&ei->vfs_inode);
3090 void btrfs_destroy_cachep(void)
3092 if (btrfs_inode_cachep)
3093 kmem_cache_destroy(btrfs_inode_cachep);
3094 if (btrfs_trans_handle_cachep)
3095 kmem_cache_destroy(btrfs_trans_handle_cachep);
3096 if (btrfs_transaction_cachep)
3097 kmem_cache_destroy(btrfs_transaction_cachep);
3098 if (btrfs_bit_radix_cachep)
3099 kmem_cache_destroy(btrfs_bit_radix_cachep);
3100 if (btrfs_path_cachep)
3101 kmem_cache_destroy(btrfs_path_cachep);
3104 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3105 unsigned long extra_flags,
3106 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3107 void (*ctor)(struct kmem_cache *, void *)
3109 void (*ctor)(void *, struct kmem_cache *,
3114 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3115 SLAB_MEM_SPREAD | extra_flags), ctor
3116 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3122 int btrfs_init_cachep(void)
3124 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3125 sizeof(struct btrfs_inode),
3127 if (!btrfs_inode_cachep)
3129 btrfs_trans_handle_cachep =
3130 btrfs_cache_create("btrfs_trans_handle_cache",
3131 sizeof(struct btrfs_trans_handle),
3133 if (!btrfs_trans_handle_cachep)
3135 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3136 sizeof(struct btrfs_transaction),
3138 if (!btrfs_transaction_cachep)
3140 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3141 sizeof(struct btrfs_path),
3143 if (!btrfs_path_cachep)
3145 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3146 SLAB_DESTROY_BY_RCU, NULL);
3147 if (!btrfs_bit_radix_cachep)
3151 btrfs_destroy_cachep();
3155 static int btrfs_getattr(struct vfsmount *mnt,
3156 struct dentry *dentry, struct kstat *stat)
3158 struct inode *inode = dentry->d_inode;
3159 generic_fillattr(inode, stat);
3160 stat->blksize = PAGE_CACHE_SIZE;
3161 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3165 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3166 struct inode * new_dir,struct dentry *new_dentry)
3168 struct btrfs_trans_handle *trans;
3169 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3170 struct inode *new_inode = new_dentry->d_inode;
3171 struct inode *old_inode = old_dentry->d_inode;
3172 struct timespec ctime = CURRENT_TIME;
3173 struct btrfs_path *path;
3176 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3177 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3181 mutex_lock(&root->fs_info->fs_mutex);
3182 ret = btrfs_check_free_space(root, 1, 0);
3186 trans = btrfs_start_transaction(root, 1);
3188 btrfs_set_trans_block_group(trans, new_dir);
3189 path = btrfs_alloc_path();
3195 old_dentry->d_inode->i_nlink++;
3196 old_dir->i_ctime = old_dir->i_mtime = ctime;
3197 new_dir->i_ctime = new_dir->i_mtime = ctime;
3198 old_inode->i_ctime = ctime;
3200 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3205 new_inode->i_ctime = CURRENT_TIME;
3206 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3210 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3215 btrfs_free_path(path);
3216 btrfs_end_transaction(trans, root);
3218 mutex_unlock(&root->fs_info->fs_mutex);
3222 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3223 const char *symname)
3225 struct btrfs_trans_handle *trans;
3226 struct btrfs_root *root = BTRFS_I(dir)->root;
3227 struct btrfs_path *path;
3228 struct btrfs_key key;
3229 struct inode *inode = NULL;
3236 struct btrfs_file_extent_item *ei;
3237 struct extent_buffer *leaf;
3238 unsigned long nr = 0;
3240 name_len = strlen(symname) + 1;
3241 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3242 return -ENAMETOOLONG;
3244 mutex_lock(&root->fs_info->fs_mutex);
3245 err = btrfs_check_free_space(root, 1, 0);
3249 trans = btrfs_start_transaction(root, 1);
3250 btrfs_set_trans_block_group(trans, dir);
3252 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3258 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3260 dentry->d_parent->d_inode->i_ino, objectid,
3261 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3262 err = PTR_ERR(inode);
3266 btrfs_set_trans_block_group(trans, inode);
3267 err = btrfs_add_nondir(trans, dentry, inode, 0);
3271 inode->i_mapping->a_ops = &btrfs_aops;
3272 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3273 inode->i_fop = &btrfs_file_operations;
3274 inode->i_op = &btrfs_file_inode_operations;
3275 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3276 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3277 inode->i_mapping, GFP_NOFS);
3278 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3279 inode->i_mapping, GFP_NOFS);
3280 BTRFS_I(inode)->delalloc_bytes = 0;
3281 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3283 dir->i_sb->s_dirt = 1;
3284 btrfs_update_inode_block_group(trans, inode);
3285 btrfs_update_inode_block_group(trans, dir);
3289 path = btrfs_alloc_path();
3291 key.objectid = inode->i_ino;
3293 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3294 datasize = btrfs_file_extent_calc_inline_size(name_len);
3295 err = btrfs_insert_empty_item(trans, root, path, &key,
3301 leaf = path->nodes[0];
3302 ei = btrfs_item_ptr(leaf, path->slots[0],
3303 struct btrfs_file_extent_item);
3304 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3305 btrfs_set_file_extent_type(leaf, ei,
3306 BTRFS_FILE_EXTENT_INLINE);
3307 ptr = btrfs_file_extent_inline_start(ei);
3308 write_extent_buffer(leaf, symname, ptr, name_len);
3309 btrfs_mark_buffer_dirty(leaf);
3310 btrfs_free_path(path);
3312 inode->i_op = &btrfs_symlink_inode_operations;
3313 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3314 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3315 inode->i_size = name_len - 1;
3316 err = btrfs_update_inode(trans, root, inode);
3321 nr = trans->blocks_used;
3322 btrfs_end_transaction(trans, root);
3324 mutex_unlock(&root->fs_info->fs_mutex);
3326 inode_dec_link_count(inode);
3329 btrfs_btree_balance_dirty(root, nr);
3330 btrfs_throttle(root);
3334 static int btrfs_permission(struct inode *inode, int mask,
3335 struct nameidata *nd)
3337 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3339 return generic_permission(inode, mask, NULL);
3342 static struct inode_operations btrfs_dir_inode_operations = {
3343 .lookup = btrfs_lookup,
3344 .create = btrfs_create,
3345 .unlink = btrfs_unlink,
3347 .mkdir = btrfs_mkdir,
3348 .rmdir = btrfs_rmdir,
3349 .rename = btrfs_rename,
3350 .symlink = btrfs_symlink,
3351 .setattr = btrfs_setattr,
3352 .mknod = btrfs_mknod,
3353 .setxattr = generic_setxattr,
3354 .getxattr = generic_getxattr,
3355 .listxattr = btrfs_listxattr,
3356 .removexattr = generic_removexattr,
3357 .permission = btrfs_permission,
3359 static struct inode_operations btrfs_dir_ro_inode_operations = {
3360 .lookup = btrfs_lookup,
3361 .permission = btrfs_permission,
3363 static struct file_operations btrfs_dir_file_operations = {
3364 .llseek = generic_file_llseek,
3365 .read = generic_read_dir,
3366 .readdir = btrfs_readdir,
3367 .unlocked_ioctl = btrfs_ioctl,
3368 #ifdef CONFIG_COMPAT
3369 .compat_ioctl = btrfs_ioctl,
3373 static struct extent_io_ops btrfs_extent_io_ops = {
3374 .fill_delalloc = run_delalloc_range,
3375 .submit_bio_hook = btrfs_submit_bio_hook,
3376 .merge_bio_hook = btrfs_merge_bio_hook,
3377 .readpage_io_hook = btrfs_readpage_io_hook,
3378 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3379 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3380 .set_bit_hook = btrfs_set_bit_hook,
3381 .clear_bit_hook = btrfs_clear_bit_hook,
3384 static struct address_space_operations btrfs_aops = {
3385 .readpage = btrfs_readpage,
3386 .writepage = btrfs_writepage,
3387 .writepages = btrfs_writepages,
3388 .readpages = btrfs_readpages,
3389 .sync_page = block_sync_page,
3391 .direct_IO = btrfs_direct_IO,
3392 .invalidatepage = btrfs_invalidatepage,
3393 .releasepage = btrfs_releasepage,
3394 .set_page_dirty = __set_page_dirty_nobuffers,
3397 static struct address_space_operations btrfs_symlink_aops = {
3398 .readpage = btrfs_readpage,
3399 .writepage = btrfs_writepage,
3400 .invalidatepage = btrfs_invalidatepage,
3401 .releasepage = btrfs_releasepage,
3404 static struct inode_operations btrfs_file_inode_operations = {
3405 .truncate = btrfs_truncate,
3406 .getattr = btrfs_getattr,
3407 .setattr = btrfs_setattr,
3408 .setxattr = generic_setxattr,
3409 .getxattr = generic_getxattr,
3410 .listxattr = btrfs_listxattr,
3411 .removexattr = generic_removexattr,
3412 .permission = btrfs_permission,
3414 static struct inode_operations btrfs_special_inode_operations = {
3415 .getattr = btrfs_getattr,
3416 .setattr = btrfs_setattr,
3417 .permission = btrfs_permission,
3419 static struct inode_operations btrfs_symlink_inode_operations = {
3420 .readlink = generic_readlink,
3421 .follow_link = page_follow_link_light,
3422 .put_link = page_put_link,
3423 .permission = btrfs_permission,