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;
498 bio_add_page(bio, page, failrec->len, start - page_offset(page));
499 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
503 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
504 struct extent_state *state)
506 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
507 struct inode *inode = page->mapping->host;
508 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
510 u64 private = ~(u32)0;
512 struct btrfs_root *root = BTRFS_I(inode)->root;
516 if (btrfs_test_opt(root, NODATASUM) ||
517 btrfs_test_flag(inode, NODATASUM))
519 if (state && state->start == start) {
520 private = state->private;
523 ret = get_state_private(io_tree, start, &private);
525 local_irq_save(flags);
526 kaddr = kmap_atomic(page, KM_IRQ0);
530 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
531 btrfs_csum_final(csum, (char *)&csum);
532 if (csum != private) {
535 kunmap_atomic(kaddr, KM_IRQ0);
536 local_irq_restore(flags);
538 /* if the io failure tree for this inode is non-empty,
539 * check to see if we've recovered from a failed IO
542 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
543 (u64)-1, 1, EXTENT_DIRTY)) {
545 struct io_failure_record *failure;
546 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
547 start, &private_failure);
549 failure = (struct io_failure_record *)(unsigned long)
551 set_state_private(&BTRFS_I(inode)->io_failure_tree,
553 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
555 failure->start + failure->len - 1,
556 EXTENT_DIRTY | EXTENT_LOCKED,
564 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
565 page->mapping->host->i_ino, (unsigned long long)start, csum,
567 memset(kaddr + offset, 1, end - start + 1);
568 flush_dcache_page(page);
569 kunmap_atomic(kaddr, KM_IRQ0);
570 local_irq_restore(flags);
576 void btrfs_read_locked_inode(struct inode *inode)
578 struct btrfs_path *path;
579 struct extent_buffer *leaf;
580 struct btrfs_inode_item *inode_item;
581 struct btrfs_timespec *tspec;
582 struct btrfs_root *root = BTRFS_I(inode)->root;
583 struct btrfs_key location;
584 u64 alloc_group_block;
588 path = btrfs_alloc_path();
590 mutex_lock(&root->fs_info->fs_mutex);
591 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
593 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
597 leaf = path->nodes[0];
598 inode_item = btrfs_item_ptr(leaf, path->slots[0],
599 struct btrfs_inode_item);
601 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
602 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
603 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
604 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
605 inode->i_size = btrfs_inode_size(leaf, inode_item);
607 tspec = btrfs_inode_atime(inode_item);
608 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
609 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
611 tspec = btrfs_inode_mtime(inode_item);
612 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
613 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
615 tspec = btrfs_inode_ctime(inode_item);
616 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
617 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
619 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
620 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
622 rdev = btrfs_inode_rdev(leaf, inode_item);
624 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
625 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
627 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
628 if (!BTRFS_I(inode)->block_group) {
629 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
631 BTRFS_BLOCK_GROUP_METADATA, 0);
633 btrfs_free_path(path);
636 mutex_unlock(&root->fs_info->fs_mutex);
638 switch (inode->i_mode & S_IFMT) {
640 inode->i_mapping->a_ops = &btrfs_aops;
641 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
642 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
643 inode->i_fop = &btrfs_file_operations;
644 inode->i_op = &btrfs_file_inode_operations;
647 inode->i_fop = &btrfs_dir_file_operations;
648 if (root == root->fs_info->tree_root)
649 inode->i_op = &btrfs_dir_ro_inode_operations;
651 inode->i_op = &btrfs_dir_inode_operations;
654 inode->i_op = &btrfs_symlink_inode_operations;
655 inode->i_mapping->a_ops = &btrfs_symlink_aops;
656 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
659 init_special_inode(inode, inode->i_mode, rdev);
665 btrfs_release_path(root, path);
666 btrfs_free_path(path);
667 mutex_unlock(&root->fs_info->fs_mutex);
668 make_bad_inode(inode);
671 static void fill_inode_item(struct extent_buffer *leaf,
672 struct btrfs_inode_item *item,
675 btrfs_set_inode_uid(leaf, item, inode->i_uid);
676 btrfs_set_inode_gid(leaf, item, inode->i_gid);
677 btrfs_set_inode_size(leaf, item, inode->i_size);
678 btrfs_set_inode_mode(leaf, item, inode->i_mode);
679 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
681 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
682 inode->i_atime.tv_sec);
683 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
684 inode->i_atime.tv_nsec);
686 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
687 inode->i_mtime.tv_sec);
688 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
689 inode->i_mtime.tv_nsec);
691 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
692 inode->i_ctime.tv_sec);
693 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
694 inode->i_ctime.tv_nsec);
696 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
697 btrfs_set_inode_generation(leaf, item, inode->i_generation);
698 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
699 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
700 btrfs_set_inode_block_group(leaf, item,
701 BTRFS_I(inode)->block_group->key.objectid);
704 int btrfs_update_inode(struct btrfs_trans_handle *trans,
705 struct btrfs_root *root,
708 struct btrfs_inode_item *inode_item;
709 struct btrfs_path *path;
710 struct extent_buffer *leaf;
713 path = btrfs_alloc_path();
715 ret = btrfs_lookup_inode(trans, root, path,
716 &BTRFS_I(inode)->location, 1);
723 leaf = path->nodes[0];
724 inode_item = btrfs_item_ptr(leaf, path->slots[0],
725 struct btrfs_inode_item);
727 fill_inode_item(leaf, inode_item, inode);
728 btrfs_mark_buffer_dirty(leaf);
729 btrfs_set_inode_last_trans(trans, inode);
732 btrfs_release_path(root, path);
733 btrfs_free_path(path);
738 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root,
741 struct dentry *dentry)
743 struct btrfs_path *path;
744 const char *name = dentry->d_name.name;
745 int name_len = dentry->d_name.len;
747 struct extent_buffer *leaf;
748 struct btrfs_dir_item *di;
749 struct btrfs_key key;
751 path = btrfs_alloc_path();
757 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
767 leaf = path->nodes[0];
768 btrfs_dir_item_key_to_cpu(leaf, di, &key);
769 ret = btrfs_delete_one_dir_name(trans, root, path, di);
772 btrfs_release_path(root, path);
774 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
775 key.objectid, name, name_len, -1);
784 ret = btrfs_delete_one_dir_name(trans, root, path, di);
786 dentry->d_inode->i_ctime = dir->i_ctime;
787 ret = btrfs_del_inode_ref(trans, root, name, name_len,
788 dentry->d_inode->i_ino,
789 dentry->d_parent->d_inode->i_ino);
791 printk("failed to delete reference to %.*s, "
792 "inode %lu parent %lu\n", name_len, name,
793 dentry->d_inode->i_ino,
794 dentry->d_parent->d_inode->i_ino);
797 btrfs_free_path(path);
799 dir->i_size -= name_len * 2;
800 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
801 btrfs_update_inode(trans, root, dir);
802 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
803 dentry->d_inode->i_nlink--;
805 drop_nlink(dentry->d_inode);
807 ret = btrfs_update_inode(trans, root, dentry->d_inode);
808 dir->i_sb->s_dirt = 1;
813 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
815 struct btrfs_root *root;
816 struct btrfs_trans_handle *trans;
817 struct inode *inode = dentry->d_inode;
819 unsigned long nr = 0;
821 root = BTRFS_I(dir)->root;
822 mutex_lock(&root->fs_info->fs_mutex);
824 ret = btrfs_check_free_space(root, 1, 1);
828 trans = btrfs_start_transaction(root, 1);
830 btrfs_set_trans_block_group(trans, dir);
831 ret = btrfs_unlink_trans(trans, root, dir, dentry);
832 nr = trans->blocks_used;
834 if (inode->i_nlink == 0) {
836 /* if the inode isn't linked anywhere,
837 * we don't need to worry about
840 found = btrfs_del_ordered_inode(inode);
842 atomic_dec(&inode->i_count);
846 btrfs_end_transaction(trans, root);
848 mutex_unlock(&root->fs_info->fs_mutex);
849 btrfs_btree_balance_dirty(root, nr);
850 btrfs_throttle(root);
854 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
856 struct inode *inode = dentry->d_inode;
859 struct btrfs_root *root = BTRFS_I(dir)->root;
860 struct btrfs_trans_handle *trans;
861 unsigned long nr = 0;
863 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
866 mutex_lock(&root->fs_info->fs_mutex);
867 ret = btrfs_check_free_space(root, 1, 1);
871 trans = btrfs_start_transaction(root, 1);
872 btrfs_set_trans_block_group(trans, dir);
874 /* now the directory is empty */
875 err = btrfs_unlink_trans(trans, root, dir, dentry);
880 nr = trans->blocks_used;
881 ret = btrfs_end_transaction(trans, root);
883 mutex_unlock(&root->fs_info->fs_mutex);
884 btrfs_btree_balance_dirty(root, nr);
885 btrfs_throttle(root);
893 * this can truncate away extent items, csum items and directory items.
894 * It starts at a high offset and removes keys until it can't find
895 * any higher than i_size.
897 * csum items that cross the new i_size are truncated to the new size
900 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
901 struct btrfs_root *root,
906 struct btrfs_path *path;
907 struct btrfs_key key;
908 struct btrfs_key found_key;
910 struct extent_buffer *leaf;
911 struct btrfs_file_extent_item *fi;
912 u64 extent_start = 0;
913 u64 extent_num_bytes = 0;
919 int pending_del_nr = 0;
920 int pending_del_slot = 0;
921 int extent_type = -1;
922 u64 mask = root->sectorsize - 1;
924 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
925 path = btrfs_alloc_path();
929 /* FIXME, add redo link to tree so we don't leak on crash */
930 key.objectid = inode->i_ino;
931 key.offset = (u64)-1;
934 btrfs_init_path(path);
936 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
941 BUG_ON(path->slots[0] == 0);
947 leaf = path->nodes[0];
948 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
949 found_type = btrfs_key_type(&found_key);
951 if (found_key.objectid != inode->i_ino)
954 if (found_type < min_type)
957 item_end = found_key.offset;
958 if (found_type == BTRFS_EXTENT_DATA_KEY) {
959 fi = btrfs_item_ptr(leaf, path->slots[0],
960 struct btrfs_file_extent_item);
961 extent_type = btrfs_file_extent_type(leaf, fi);
962 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
964 btrfs_file_extent_num_bytes(leaf, fi);
965 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
966 struct btrfs_item *item = btrfs_item_nr(leaf,
968 item_end += btrfs_file_extent_inline_len(leaf,
973 if (found_type == BTRFS_CSUM_ITEM_KEY) {
974 ret = btrfs_csum_truncate(trans, root, path,
978 if (item_end < inode->i_size) {
979 if (found_type == BTRFS_DIR_ITEM_KEY) {
980 found_type = BTRFS_INODE_ITEM_KEY;
981 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
982 found_type = BTRFS_CSUM_ITEM_KEY;
983 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
984 found_type = BTRFS_XATTR_ITEM_KEY;
985 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
986 found_type = BTRFS_INODE_REF_KEY;
987 } else if (found_type) {
992 btrfs_set_key_type(&key, found_type);
995 if (found_key.offset >= inode->i_size)
1001 /* FIXME, shrink the extent if the ref count is only 1 */
1002 if (found_type != BTRFS_EXTENT_DATA_KEY)
1005 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1007 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1009 u64 orig_num_bytes =
1010 btrfs_file_extent_num_bytes(leaf, fi);
1011 extent_num_bytes = inode->i_size -
1012 found_key.offset + root->sectorsize - 1;
1013 extent_num_bytes = extent_num_bytes &
1014 ~((u64)root->sectorsize - 1);
1015 btrfs_set_file_extent_num_bytes(leaf, fi,
1017 num_dec = (orig_num_bytes -
1019 if (extent_start != 0)
1020 dec_i_blocks(inode, num_dec);
1021 btrfs_mark_buffer_dirty(leaf);
1024 btrfs_file_extent_disk_num_bytes(leaf,
1026 /* FIXME blocksize != 4096 */
1027 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1028 if (extent_start != 0) {
1030 dec_i_blocks(inode, num_dec);
1032 root_gen = btrfs_header_generation(leaf);
1033 root_owner = btrfs_header_owner(leaf);
1035 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1037 u32 newsize = inode->i_size - found_key.offset;
1038 dec_i_blocks(inode, item_end + 1 -
1039 found_key.offset - newsize);
1041 btrfs_file_extent_calc_inline_size(newsize);
1042 ret = btrfs_truncate_item(trans, root, path,
1046 dec_i_blocks(inode, item_end + 1 -
1052 if (!pending_del_nr) {
1053 /* no pending yet, add ourselves */
1054 pending_del_slot = path->slots[0];
1056 } else if (pending_del_nr &&
1057 path->slots[0] + 1 == pending_del_slot) {
1058 /* hop on the pending chunk */
1060 pending_del_slot = path->slots[0];
1062 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1068 ret = btrfs_free_extent(trans, root, extent_start,
1071 root_gen, inode->i_ino,
1072 found_key.offset, 0);
1076 if (path->slots[0] == 0) {
1079 btrfs_release_path(root, path);
1084 if (pending_del_nr &&
1085 path->slots[0] + 1 != pending_del_slot) {
1086 struct btrfs_key debug;
1088 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1090 ret = btrfs_del_items(trans, root, path,
1095 btrfs_release_path(root, path);
1101 if (pending_del_nr) {
1102 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1105 btrfs_release_path(root, path);
1106 btrfs_free_path(path);
1107 inode->i_sb->s_dirt = 1;
1111 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1115 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1116 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1117 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1120 WARN_ON(!PageLocked(page));
1121 set_page_extent_mapped(page);
1123 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1124 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1125 page_end, GFP_NOFS);
1127 if (zero_start != PAGE_CACHE_SIZE) {
1129 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1130 flush_dcache_page(page);
1133 set_page_dirty(page);
1134 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1140 * taken from block_truncate_page, but does cow as it zeros out
1141 * any bytes left in the last page in the file.
1143 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1145 struct inode *inode = mapping->host;
1146 struct btrfs_root *root = BTRFS_I(inode)->root;
1147 u32 blocksize = root->sectorsize;
1148 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1149 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1154 if ((offset & (blocksize - 1)) == 0)
1158 page = grab_cache_page(mapping, index);
1161 if (!PageUptodate(page)) {
1162 ret = btrfs_readpage(NULL, page);
1164 if (!PageUptodate(page)) {
1169 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1171 ret = btrfs_cow_one_page(inode, page, offset);
1174 page_cache_release(page);
1179 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1181 struct inode *inode = dentry->d_inode;
1184 err = inode_change_ok(inode, attr);
1188 if (S_ISREG(inode->i_mode) &&
1189 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1190 struct btrfs_trans_handle *trans;
1191 struct btrfs_root *root = BTRFS_I(inode)->root;
1192 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1194 u64 mask = root->sectorsize - 1;
1195 u64 hole_start = (inode->i_size + mask) & ~mask;
1196 u64 block_end = (attr->ia_size + mask) & ~mask;
1200 if (attr->ia_size <= hole_start)
1203 mutex_lock(&root->fs_info->fs_mutex);
1204 err = btrfs_check_free_space(root, 1, 0);
1205 mutex_unlock(&root->fs_info->fs_mutex);
1209 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1211 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1212 hole_size = block_end - hole_start;
1214 mutex_lock(&root->fs_info->fs_mutex);
1215 trans = btrfs_start_transaction(root, 1);
1216 btrfs_set_trans_block_group(trans, inode);
1217 err = btrfs_drop_extents(trans, root, inode,
1218 hole_start, block_end, hole_start,
1221 if (alloc_hint != EXTENT_MAP_INLINE) {
1222 err = btrfs_insert_file_extent(trans, root,
1226 btrfs_drop_extent_cache(inode, hole_start,
1228 btrfs_check_file(root, inode);
1230 btrfs_end_transaction(trans, root);
1231 mutex_unlock(&root->fs_info->fs_mutex);
1232 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1237 err = inode_setattr(inode, attr);
1242 void btrfs_put_inode(struct inode *inode)
1246 if (!BTRFS_I(inode)->ordered_trans) {
1250 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1251 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1254 ret = btrfs_del_ordered_inode(inode);
1256 atomic_dec(&inode->i_count);
1260 void btrfs_delete_inode(struct inode *inode)
1262 struct btrfs_trans_handle *trans;
1263 struct btrfs_root *root = BTRFS_I(inode)->root;
1267 truncate_inode_pages(&inode->i_data, 0);
1268 if (is_bad_inode(inode)) {
1273 mutex_lock(&root->fs_info->fs_mutex);
1274 trans = btrfs_start_transaction(root, 1);
1276 btrfs_set_trans_block_group(trans, inode);
1277 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1279 goto no_delete_lock;
1281 nr = trans->blocks_used;
1284 btrfs_end_transaction(trans, root);
1285 mutex_unlock(&root->fs_info->fs_mutex);
1286 btrfs_btree_balance_dirty(root, nr);
1287 btrfs_throttle(root);
1291 nr = trans->blocks_used;
1292 btrfs_end_transaction(trans, root);
1293 mutex_unlock(&root->fs_info->fs_mutex);
1294 btrfs_btree_balance_dirty(root, nr);
1295 btrfs_throttle(root);
1301 * this returns the key found in the dir entry in the location pointer.
1302 * If no dir entries were found, location->objectid is 0.
1304 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1305 struct btrfs_key *location)
1307 const char *name = dentry->d_name.name;
1308 int namelen = dentry->d_name.len;
1309 struct btrfs_dir_item *di;
1310 struct btrfs_path *path;
1311 struct btrfs_root *root = BTRFS_I(dir)->root;
1314 if (namelen == 1 && strcmp(name, ".") == 0) {
1315 location->objectid = dir->i_ino;
1316 location->type = BTRFS_INODE_ITEM_KEY;
1317 location->offset = 0;
1320 path = btrfs_alloc_path();
1323 if (namelen == 2 && strcmp(name, "..") == 0) {
1324 struct btrfs_key key;
1325 struct extent_buffer *leaf;
1329 key.objectid = dir->i_ino;
1330 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1332 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1336 leaf = path->nodes[0];
1337 slot = path->slots[0];
1338 nritems = btrfs_header_nritems(leaf);
1339 if (slot >= nritems)
1342 btrfs_item_key_to_cpu(leaf, &key, slot);
1343 if (key.objectid != dir->i_ino ||
1344 key.type != BTRFS_INODE_REF_KEY) {
1347 location->objectid = key.offset;
1348 location->type = BTRFS_INODE_ITEM_KEY;
1349 location->offset = 0;
1353 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1357 if (!di || IS_ERR(di)) {
1360 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1362 btrfs_free_path(path);
1365 location->objectid = 0;
1370 * when we hit a tree root in a directory, the btrfs part of the inode
1371 * needs to be changed to reflect the root directory of the tree root. This
1372 * is kind of like crossing a mount point.
1374 static int fixup_tree_root_location(struct btrfs_root *root,
1375 struct btrfs_key *location,
1376 struct btrfs_root **sub_root,
1377 struct dentry *dentry)
1379 struct btrfs_path *path;
1380 struct btrfs_root_item *ri;
1382 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1384 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1387 path = btrfs_alloc_path();
1389 mutex_lock(&root->fs_info->fs_mutex);
1391 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1392 dentry->d_name.name,
1393 dentry->d_name.len);
1394 if (IS_ERR(*sub_root))
1395 return PTR_ERR(*sub_root);
1397 ri = &(*sub_root)->root_item;
1398 location->objectid = btrfs_root_dirid(ri);
1399 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1400 location->offset = 0;
1402 btrfs_free_path(path);
1403 mutex_unlock(&root->fs_info->fs_mutex);
1407 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1409 struct btrfs_iget_args *args = p;
1410 inode->i_ino = args->ino;
1411 BTRFS_I(inode)->root = args->root;
1412 BTRFS_I(inode)->delalloc_bytes = 0;
1413 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1414 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1415 inode->i_mapping, GFP_NOFS);
1416 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1417 inode->i_mapping, GFP_NOFS);
1421 static int btrfs_find_actor(struct inode *inode, void *opaque)
1423 struct btrfs_iget_args *args = opaque;
1424 return (args->ino == inode->i_ino &&
1425 args->root == BTRFS_I(inode)->root);
1428 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1431 struct btrfs_iget_args args;
1432 args.ino = objectid;
1433 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1438 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1441 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1442 struct btrfs_root *root)
1444 struct inode *inode;
1445 struct btrfs_iget_args args;
1446 args.ino = objectid;
1449 inode = iget5_locked(s, objectid, btrfs_find_actor,
1450 btrfs_init_locked_inode,
1455 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1456 struct nameidata *nd)
1458 struct inode * inode;
1459 struct btrfs_inode *bi = BTRFS_I(dir);
1460 struct btrfs_root *root = bi->root;
1461 struct btrfs_root *sub_root = root;
1462 struct btrfs_key location;
1465 if (dentry->d_name.len > BTRFS_NAME_LEN)
1466 return ERR_PTR(-ENAMETOOLONG);
1468 mutex_lock(&root->fs_info->fs_mutex);
1469 ret = btrfs_inode_by_name(dir, dentry, &location);
1470 mutex_unlock(&root->fs_info->fs_mutex);
1473 return ERR_PTR(ret);
1476 if (location.objectid) {
1477 ret = fixup_tree_root_location(root, &location, &sub_root,
1480 return ERR_PTR(ret);
1482 return ERR_PTR(-ENOENT);
1483 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1486 return ERR_PTR(-EACCES);
1487 if (inode->i_state & I_NEW) {
1488 /* the inode and parent dir are two different roots */
1489 if (sub_root != root) {
1491 sub_root->inode = inode;
1493 BTRFS_I(inode)->root = sub_root;
1494 memcpy(&BTRFS_I(inode)->location, &location,
1496 btrfs_read_locked_inode(inode);
1497 unlock_new_inode(inode);
1500 return d_splice_alias(inode, dentry);
1503 static unsigned char btrfs_filetype_table[] = {
1504 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1507 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1509 struct inode *inode = filp->f_dentry->d_inode;
1510 struct btrfs_root *root = BTRFS_I(inode)->root;
1511 struct btrfs_item *item;
1512 struct btrfs_dir_item *di;
1513 struct btrfs_key key;
1514 struct btrfs_key found_key;
1515 struct btrfs_path *path;
1518 struct extent_buffer *leaf;
1521 unsigned char d_type;
1526 int key_type = BTRFS_DIR_INDEX_KEY;
1531 /* FIXME, use a real flag for deciding about the key type */
1532 if (root->fs_info->tree_root == root)
1533 key_type = BTRFS_DIR_ITEM_KEY;
1535 /* special case for "." */
1536 if (filp->f_pos == 0) {
1537 over = filldir(dirent, ".", 1,
1545 mutex_lock(&root->fs_info->fs_mutex);
1546 key.objectid = inode->i_ino;
1547 path = btrfs_alloc_path();
1550 /* special case for .., just use the back ref */
1551 if (filp->f_pos == 1) {
1552 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1554 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1556 leaf = path->nodes[0];
1557 slot = path->slots[0];
1558 nritems = btrfs_header_nritems(leaf);
1559 if (slot >= nritems) {
1560 btrfs_release_path(root, path);
1561 goto read_dir_items;
1563 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1564 btrfs_release_path(root, path);
1565 if (found_key.objectid != key.objectid ||
1566 found_key.type != BTRFS_INODE_REF_KEY)
1567 goto read_dir_items;
1568 over = filldir(dirent, "..", 2,
1569 2, found_key.offset, DT_DIR);
1576 btrfs_set_key_type(&key, key_type);
1577 key.offset = filp->f_pos;
1579 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1584 leaf = path->nodes[0];
1585 nritems = btrfs_header_nritems(leaf);
1586 slot = path->slots[0];
1587 if (advance || slot >= nritems) {
1588 if (slot >= nritems -1) {
1589 ret = btrfs_next_leaf(root, path);
1592 leaf = path->nodes[0];
1593 nritems = btrfs_header_nritems(leaf);
1594 slot = path->slots[0];
1601 item = btrfs_item_nr(leaf, slot);
1602 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1604 if (found_key.objectid != key.objectid)
1606 if (btrfs_key_type(&found_key) != key_type)
1608 if (found_key.offset < filp->f_pos)
1611 filp->f_pos = found_key.offset;
1613 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1615 di_total = btrfs_item_size(leaf, item);
1616 while(di_cur < di_total) {
1617 struct btrfs_key location;
1619 name_len = btrfs_dir_name_len(leaf, di);
1620 if (name_len < 32) {
1621 name_ptr = tmp_name;
1623 name_ptr = kmalloc(name_len, GFP_NOFS);
1626 read_extent_buffer(leaf, name_ptr,
1627 (unsigned long)(di + 1), name_len);
1629 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1630 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1631 over = filldir(dirent, name_ptr, name_len,
1636 if (name_ptr != tmp_name)
1641 di_len = btrfs_dir_name_len(leaf, di) +
1642 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1644 di = (struct btrfs_dir_item *)((char *)di + di_len);
1647 if (key_type == BTRFS_DIR_INDEX_KEY)
1648 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1654 btrfs_release_path(root, path);
1655 btrfs_free_path(path);
1656 mutex_unlock(&root->fs_info->fs_mutex);
1660 int btrfs_write_inode(struct inode *inode, int wait)
1662 struct btrfs_root *root = BTRFS_I(inode)->root;
1663 struct btrfs_trans_handle *trans;
1667 mutex_lock(&root->fs_info->fs_mutex);
1668 trans = btrfs_start_transaction(root, 1);
1669 btrfs_set_trans_block_group(trans, inode);
1670 ret = btrfs_commit_transaction(trans, root);
1671 mutex_unlock(&root->fs_info->fs_mutex);
1677 * This is somewhat expensive, updating the tree every time the
1678 * inode changes. But, it is most likely to find the inode in cache.
1679 * FIXME, needs more benchmarking...there are no reasons other than performance
1680 * to keep or drop this code.
1682 void btrfs_dirty_inode(struct inode *inode)
1684 struct btrfs_root *root = BTRFS_I(inode)->root;
1685 struct btrfs_trans_handle *trans;
1687 mutex_lock(&root->fs_info->fs_mutex);
1688 trans = btrfs_start_transaction(root, 1);
1689 btrfs_set_trans_block_group(trans, inode);
1690 btrfs_update_inode(trans, root, inode);
1691 btrfs_end_transaction(trans, root);
1692 mutex_unlock(&root->fs_info->fs_mutex);
1695 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1696 struct btrfs_root *root,
1697 const char *name, int name_len,
1700 struct btrfs_block_group_cache *group,
1703 struct inode *inode;
1704 struct btrfs_inode_item *inode_item;
1705 struct btrfs_block_group_cache *new_inode_group;
1706 struct btrfs_key *location;
1707 struct btrfs_path *path;
1708 struct btrfs_inode_ref *ref;
1709 struct btrfs_key key[2];
1715 path = btrfs_alloc_path();
1718 inode = new_inode(root->fs_info->sb);
1720 return ERR_PTR(-ENOMEM);
1722 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1723 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1724 inode->i_mapping, GFP_NOFS);
1725 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1726 inode->i_mapping, GFP_NOFS);
1727 BTRFS_I(inode)->delalloc_bytes = 0;
1728 BTRFS_I(inode)->root = root;
1734 new_inode_group = btrfs_find_block_group(root, group, 0,
1735 BTRFS_BLOCK_GROUP_METADATA, owner);
1736 if (!new_inode_group) {
1737 printk("find_block group failed\n");
1738 new_inode_group = group;
1740 BTRFS_I(inode)->block_group = new_inode_group;
1741 BTRFS_I(inode)->flags = 0;
1743 key[0].objectid = objectid;
1744 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1747 key[1].objectid = objectid;
1748 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1749 key[1].offset = ref_objectid;
1751 sizes[0] = sizeof(struct btrfs_inode_item);
1752 sizes[1] = name_len + sizeof(*ref);
1754 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1758 if (objectid > root->highest_inode)
1759 root->highest_inode = objectid;
1761 inode->i_uid = current->fsuid;
1762 inode->i_gid = current->fsgid;
1763 inode->i_mode = mode;
1764 inode->i_ino = objectid;
1765 inode->i_blocks = 0;
1766 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1767 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1768 struct btrfs_inode_item);
1769 fill_inode_item(path->nodes[0], inode_item, inode);
1771 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1772 struct btrfs_inode_ref);
1773 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1774 ptr = (unsigned long)(ref + 1);
1775 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1777 btrfs_mark_buffer_dirty(path->nodes[0]);
1778 btrfs_free_path(path);
1780 location = &BTRFS_I(inode)->location;
1781 location->objectid = objectid;
1782 location->offset = 0;
1783 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1785 insert_inode_hash(inode);
1788 btrfs_free_path(path);
1789 return ERR_PTR(ret);
1792 static inline u8 btrfs_inode_type(struct inode *inode)
1794 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1797 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1798 struct dentry *dentry, struct inode *inode,
1802 struct btrfs_key key;
1803 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1804 struct inode *parent_inode;
1806 key.objectid = inode->i_ino;
1807 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1810 ret = btrfs_insert_dir_item(trans, root,
1811 dentry->d_name.name, dentry->d_name.len,
1812 dentry->d_parent->d_inode->i_ino,
1813 &key, btrfs_inode_type(inode));
1816 ret = btrfs_insert_inode_ref(trans, root,
1817 dentry->d_name.name,
1820 dentry->d_parent->d_inode->i_ino);
1822 parent_inode = dentry->d_parent->d_inode;
1823 parent_inode->i_size += dentry->d_name.len * 2;
1824 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1825 ret = btrfs_update_inode(trans, root,
1826 dentry->d_parent->d_inode);
1831 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1832 struct dentry *dentry, struct inode *inode,
1835 int err = btrfs_add_link(trans, dentry, inode, backref);
1837 d_instantiate(dentry, inode);
1845 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1846 int mode, dev_t rdev)
1848 struct btrfs_trans_handle *trans;
1849 struct btrfs_root *root = BTRFS_I(dir)->root;
1850 struct inode *inode = NULL;
1854 unsigned long nr = 0;
1856 if (!new_valid_dev(rdev))
1859 mutex_lock(&root->fs_info->fs_mutex);
1860 err = btrfs_check_free_space(root, 1, 0);
1864 trans = btrfs_start_transaction(root, 1);
1865 btrfs_set_trans_block_group(trans, dir);
1867 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1873 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1875 dentry->d_parent->d_inode->i_ino, objectid,
1876 BTRFS_I(dir)->block_group, mode);
1877 err = PTR_ERR(inode);
1881 btrfs_set_trans_block_group(trans, inode);
1882 err = btrfs_add_nondir(trans, dentry, inode, 0);
1886 inode->i_op = &btrfs_special_inode_operations;
1887 init_special_inode(inode, inode->i_mode, rdev);
1888 btrfs_update_inode(trans, root, inode);
1890 dir->i_sb->s_dirt = 1;
1891 btrfs_update_inode_block_group(trans, inode);
1892 btrfs_update_inode_block_group(trans, dir);
1894 nr = trans->blocks_used;
1895 btrfs_end_transaction(trans, root);
1897 mutex_unlock(&root->fs_info->fs_mutex);
1900 inode_dec_link_count(inode);
1903 btrfs_btree_balance_dirty(root, nr);
1904 btrfs_throttle(root);
1908 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1909 int mode, struct nameidata *nd)
1911 struct btrfs_trans_handle *trans;
1912 struct btrfs_root *root = BTRFS_I(dir)->root;
1913 struct inode *inode = NULL;
1916 unsigned long nr = 0;
1919 mutex_lock(&root->fs_info->fs_mutex);
1920 err = btrfs_check_free_space(root, 1, 0);
1923 trans = btrfs_start_transaction(root, 1);
1924 btrfs_set_trans_block_group(trans, dir);
1926 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1932 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1934 dentry->d_parent->d_inode->i_ino,
1935 objectid, BTRFS_I(dir)->block_group, mode);
1936 err = PTR_ERR(inode);
1940 btrfs_set_trans_block_group(trans, inode);
1941 err = btrfs_add_nondir(trans, dentry, inode, 0);
1945 inode->i_mapping->a_ops = &btrfs_aops;
1946 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1947 inode->i_fop = &btrfs_file_operations;
1948 inode->i_op = &btrfs_file_inode_operations;
1949 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1950 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1951 inode->i_mapping, GFP_NOFS);
1952 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1953 inode->i_mapping, GFP_NOFS);
1954 BTRFS_I(inode)->delalloc_bytes = 0;
1955 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1957 dir->i_sb->s_dirt = 1;
1958 btrfs_update_inode_block_group(trans, inode);
1959 btrfs_update_inode_block_group(trans, dir);
1961 nr = trans->blocks_used;
1962 btrfs_end_transaction(trans, root);
1964 mutex_unlock(&root->fs_info->fs_mutex);
1967 inode_dec_link_count(inode);
1970 btrfs_btree_balance_dirty(root, nr);
1971 btrfs_throttle(root);
1975 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1976 struct dentry *dentry)
1978 struct btrfs_trans_handle *trans;
1979 struct btrfs_root *root = BTRFS_I(dir)->root;
1980 struct inode *inode = old_dentry->d_inode;
1981 unsigned long nr = 0;
1985 if (inode->i_nlink == 0)
1988 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1993 mutex_lock(&root->fs_info->fs_mutex);
1994 err = btrfs_check_free_space(root, 1, 0);
1997 trans = btrfs_start_transaction(root, 1);
1999 btrfs_set_trans_block_group(trans, dir);
2000 atomic_inc(&inode->i_count);
2001 err = btrfs_add_nondir(trans, dentry, inode, 1);
2006 dir->i_sb->s_dirt = 1;
2007 btrfs_update_inode_block_group(trans, dir);
2008 err = btrfs_update_inode(trans, root, inode);
2013 nr = trans->blocks_used;
2014 btrfs_end_transaction(trans, root);
2016 mutex_unlock(&root->fs_info->fs_mutex);
2019 inode_dec_link_count(inode);
2022 btrfs_btree_balance_dirty(root, nr);
2023 btrfs_throttle(root);
2027 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2029 struct inode *inode;
2030 struct btrfs_trans_handle *trans;
2031 struct btrfs_root *root = BTRFS_I(dir)->root;
2033 int drop_on_err = 0;
2035 unsigned long nr = 1;
2037 mutex_lock(&root->fs_info->fs_mutex);
2038 err = btrfs_check_free_space(root, 1, 0);
2042 trans = btrfs_start_transaction(root, 1);
2043 btrfs_set_trans_block_group(trans, dir);
2045 if (IS_ERR(trans)) {
2046 err = PTR_ERR(trans);
2050 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2056 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2058 dentry->d_parent->d_inode->i_ino, objectid,
2059 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2060 if (IS_ERR(inode)) {
2061 err = PTR_ERR(inode);
2066 inode->i_op = &btrfs_dir_inode_operations;
2067 inode->i_fop = &btrfs_dir_file_operations;
2068 btrfs_set_trans_block_group(trans, inode);
2071 err = btrfs_update_inode(trans, root, inode);
2075 err = btrfs_add_link(trans, dentry, inode, 0);
2079 d_instantiate(dentry, inode);
2081 dir->i_sb->s_dirt = 1;
2082 btrfs_update_inode_block_group(trans, inode);
2083 btrfs_update_inode_block_group(trans, dir);
2086 nr = trans->blocks_used;
2087 btrfs_end_transaction(trans, root);
2090 mutex_unlock(&root->fs_info->fs_mutex);
2093 btrfs_btree_balance_dirty(root, nr);
2094 btrfs_throttle(root);
2098 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2099 struct extent_map *existing,
2100 struct extent_map *em)
2105 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2107 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2110 if (!real_blocks && em->block_start != existing->block_start)
2113 new_end = max(existing->start + existing->len, em->start + em->len);
2115 if (existing->start >= em->start) {
2116 if (em->start + em->len < existing->start)
2119 start_diff = existing->start - em->start;
2120 if (real_blocks && em->block_start + start_diff !=
2121 existing->block_start)
2124 em->len = new_end - em->start;
2126 remove_extent_mapping(em_tree, existing);
2127 /* free for the tree */
2128 free_extent_map(existing);
2129 ret = add_extent_mapping(em_tree, em);
2131 } else if (em->start > existing->start) {
2133 if (existing->start + existing->len < em->start)
2136 start_diff = em->start - existing->start;
2137 if (real_blocks && existing->block_start + start_diff !=
2141 remove_extent_mapping(em_tree, existing);
2142 em->block_start = existing->block_start;
2143 em->start = existing->start;
2144 em->len = new_end - existing->start;
2145 free_extent_map(existing);
2147 ret = add_extent_mapping(em_tree, em);
2154 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2155 existing->start, existing->len, existing->block_start,
2156 em->start, em->len, em->block_start);
2160 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2161 size_t pg_offset, u64 start, u64 len,
2167 u64 extent_start = 0;
2169 u64 objectid = inode->i_ino;
2171 struct btrfs_path *path;
2172 struct btrfs_root *root = BTRFS_I(inode)->root;
2173 struct btrfs_file_extent_item *item;
2174 struct extent_buffer *leaf;
2175 struct btrfs_key found_key;
2176 struct extent_map *em = NULL;
2177 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2179 struct btrfs_trans_handle *trans = NULL;
2181 path = btrfs_alloc_path();
2183 mutex_lock(&root->fs_info->fs_mutex);
2186 spin_lock(&em_tree->lock);
2187 em = lookup_extent_mapping(em_tree, start, len);
2188 spin_unlock(&em_tree->lock);
2191 if (em->start > start || em->start + em->len <= start)
2192 free_extent_map(em);
2193 else if (em->block_start == EXTENT_MAP_INLINE && page)
2194 free_extent_map(em);
2198 em = alloc_extent_map(GFP_NOFS);
2204 em->start = EXTENT_MAP_HOLE;
2206 em->bdev = inode->i_sb->s_bdev;
2207 ret = btrfs_lookup_file_extent(trans, root, path,
2208 objectid, start, trans != NULL);
2215 if (path->slots[0] == 0)
2220 leaf = path->nodes[0];
2221 item = btrfs_item_ptr(leaf, path->slots[0],
2222 struct btrfs_file_extent_item);
2223 /* are we inside the extent that was found? */
2224 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2225 found_type = btrfs_key_type(&found_key);
2226 if (found_key.objectid != objectid ||
2227 found_type != BTRFS_EXTENT_DATA_KEY) {
2231 found_type = btrfs_file_extent_type(leaf, item);
2232 extent_start = found_key.offset;
2233 if (found_type == BTRFS_FILE_EXTENT_REG) {
2234 extent_end = extent_start +
2235 btrfs_file_extent_num_bytes(leaf, item);
2237 if (start < extent_start || start >= extent_end) {
2239 if (start < extent_start) {
2240 if (start + len <= extent_start)
2242 em->len = extent_end - extent_start;
2248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2250 em->start = extent_start;
2251 em->len = extent_end - extent_start;
2252 em->block_start = EXTENT_MAP_HOLE;
2255 bytenr += btrfs_file_extent_offset(leaf, item);
2256 em->block_start = bytenr;
2257 em->start = extent_start;
2258 em->len = extent_end - extent_start;
2260 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2265 size_t extent_offset;
2268 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2270 extent_end = (extent_start + size + root->sectorsize - 1) &
2271 ~((u64)root->sectorsize - 1);
2272 if (start < extent_start || start >= extent_end) {
2274 if (start < extent_start) {
2275 if (start + len <= extent_start)
2277 em->len = extent_end - extent_start;
2283 em->block_start = EXTENT_MAP_INLINE;
2286 em->start = extent_start;
2291 page_start = page_offset(page) + pg_offset;
2292 extent_offset = page_start - extent_start;
2293 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2294 size - extent_offset);
2295 em->start = extent_start + extent_offset;
2296 em->len = (copy_size + root->sectorsize - 1) &
2297 ~((u64)root->sectorsize - 1);
2299 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2300 if (create == 0 && !PageUptodate(page)) {
2301 read_extent_buffer(leaf, map + pg_offset, ptr,
2303 flush_dcache_page(page);
2304 } else if (create && PageUptodate(page)) {
2307 free_extent_map(em);
2309 btrfs_release_path(root, path);
2310 trans = btrfs_start_transaction(root, 1);
2313 write_extent_buffer(leaf, map + pg_offset, ptr,
2315 btrfs_mark_buffer_dirty(leaf);
2318 set_extent_uptodate(io_tree, em->start,
2319 extent_map_end(em) - 1, GFP_NOFS);
2322 printk("unkknown found_type %d\n", found_type);
2329 em->block_start = EXTENT_MAP_HOLE;
2331 btrfs_release_path(root, path);
2332 if (em->start > start || extent_map_end(em) <= start) {
2333 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2339 spin_lock(&em_tree->lock);
2340 ret = add_extent_mapping(em_tree, em);
2341 /* it is possible that someone inserted the extent into the tree
2342 * while we had the lock dropped. It is also possible that
2343 * an overlapping map exists in the tree
2345 if (ret == -EEXIST) {
2346 struct extent_map *existing;
2347 existing = lookup_extent_mapping(em_tree, start, len);
2348 if (existing && (existing->start > start ||
2349 existing->start + existing->len <= start)) {
2350 free_extent_map(existing);
2354 existing = lookup_extent_mapping(em_tree, em->start,
2357 err = merge_extent_mapping(em_tree, existing,
2359 free_extent_map(existing);
2361 free_extent_map(em);
2366 printk("failing to insert %Lu %Lu\n",
2368 free_extent_map(em);
2372 free_extent_map(em);
2376 spin_unlock(&em_tree->lock);
2378 btrfs_free_path(path);
2380 ret = btrfs_end_transaction(trans, root);
2384 mutex_unlock(&root->fs_info->fs_mutex);
2386 free_extent_map(em);
2388 return ERR_PTR(err);
2393 #if 0 /* waiting for O_DIRECT reads */
2394 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2395 struct buffer_head *bh_result, int create)
2397 struct extent_map *em;
2398 u64 start = (u64)iblock << inode->i_blkbits;
2399 struct btrfs_multi_bio *multi = NULL;
2400 struct btrfs_root *root = BTRFS_I(inode)->root;
2406 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2408 if (!em || IS_ERR(em))
2411 if (em->start > start || em->start + em->len <= start) {
2415 if (em->block_start == EXTENT_MAP_INLINE) {
2420 len = em->start + em->len - start;
2421 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2423 if (em->block_start == EXTENT_MAP_HOLE ||
2424 em->block_start == EXTENT_MAP_DELALLOC) {
2425 bh_result->b_size = len;
2429 logical = start - em->start;
2430 logical = em->block_start + logical;
2433 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2434 logical, &map_length, &multi, 0);
2436 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2437 bh_result->b_size = min(map_length, len);
2439 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2440 set_buffer_mapped(bh_result);
2443 free_extent_map(em);
2448 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2449 const struct iovec *iov, loff_t offset,
2450 unsigned long nr_segs)
2454 struct file *file = iocb->ki_filp;
2455 struct inode *inode = file->f_mapping->host;
2460 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2461 offset, nr_segs, btrfs_get_block, NULL);
2465 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2467 return extent_bmap(mapping, iblock, btrfs_get_extent);
2470 int btrfs_readpage(struct file *file, struct page *page)
2472 struct extent_io_tree *tree;
2473 tree = &BTRFS_I(page->mapping->host)->io_tree;
2474 return extent_read_full_page(tree, page, btrfs_get_extent);
2477 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2479 struct extent_io_tree *tree;
2482 if (current->flags & PF_MEMALLOC) {
2483 redirty_page_for_writepage(wbc, page);
2487 tree = &BTRFS_I(page->mapping->host)->io_tree;
2488 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2491 static int btrfs_writepages(struct address_space *mapping,
2492 struct writeback_control *wbc)
2494 struct extent_io_tree *tree;
2495 tree = &BTRFS_I(mapping->host)->io_tree;
2496 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2500 btrfs_readpages(struct file *file, struct address_space *mapping,
2501 struct list_head *pages, unsigned nr_pages)
2503 struct extent_io_tree *tree;
2504 tree = &BTRFS_I(mapping->host)->io_tree;
2505 return extent_readpages(tree, mapping, pages, nr_pages,
2509 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2511 struct extent_io_tree *tree;
2512 struct extent_map_tree *map;
2515 tree = &BTRFS_I(page->mapping->host)->io_tree;
2516 map = &BTRFS_I(page->mapping->host)->extent_tree;
2517 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2519 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2520 ClearPagePrivate(page);
2521 set_page_private(page, 0);
2522 page_cache_release(page);
2527 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2529 struct extent_io_tree *tree;
2531 tree = &BTRFS_I(page->mapping->host)->io_tree;
2532 extent_invalidatepage(tree, page, offset);
2533 btrfs_releasepage(page, GFP_NOFS);
2534 if (PagePrivate(page)) {
2535 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2536 ClearPagePrivate(page);
2537 set_page_private(page, 0);
2538 page_cache_release(page);
2543 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2544 * called from a page fault handler when a page is first dirtied. Hence we must
2545 * be careful to check for EOF conditions here. We set the page up correctly
2546 * for a written page which means we get ENOSPC checking when writing into
2547 * holes and correct delalloc and unwritten extent mapping on filesystems that
2548 * support these features.
2550 * We are not allowed to take the i_mutex here so we have to play games to
2551 * protect against truncate races as the page could now be beyond EOF. Because
2552 * vmtruncate() writes the inode size before removing pages, once we have the
2553 * page lock we can determine safely if the page is beyond EOF. If it is not
2554 * beyond EOF, then the page is guaranteed safe against truncation until we
2557 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2559 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2560 struct btrfs_root *root = BTRFS_I(inode)->root;
2566 mutex_lock(&root->fs_info->fs_mutex);
2567 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2568 mutex_unlock(&root->fs_info->fs_mutex);
2575 wait_on_page_writeback(page);
2576 size = i_size_read(inode);
2577 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2579 if ((page->mapping != inode->i_mapping) ||
2580 (page_start > size)) {
2581 /* page got truncated out from underneath us */
2585 /* page is wholly or partially inside EOF */
2586 if (page_start + PAGE_CACHE_SIZE > size)
2587 end = size & ~PAGE_CACHE_MASK;
2589 end = PAGE_CACHE_SIZE;
2591 ret = btrfs_cow_one_page(inode, page, end);
2599 static void btrfs_truncate(struct inode *inode)
2601 struct btrfs_root *root = BTRFS_I(inode)->root;
2603 struct btrfs_trans_handle *trans;
2606 if (!S_ISREG(inode->i_mode))
2608 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2611 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2613 mutex_lock(&root->fs_info->fs_mutex);
2614 trans = btrfs_start_transaction(root, 1);
2615 btrfs_set_trans_block_group(trans, inode);
2617 /* FIXME, add redo link to tree so we don't leak on crash */
2618 ret = btrfs_truncate_in_trans(trans, root, inode,
2619 BTRFS_EXTENT_DATA_KEY);
2620 btrfs_update_inode(trans, root, inode);
2621 nr = trans->blocks_used;
2623 ret = btrfs_end_transaction(trans, root);
2625 mutex_unlock(&root->fs_info->fs_mutex);
2626 btrfs_btree_balance_dirty(root, nr);
2627 btrfs_throttle(root);
2630 static int noinline create_subvol(struct btrfs_root *root, char *name,
2633 struct btrfs_trans_handle *trans;
2634 struct btrfs_key key;
2635 struct btrfs_root_item root_item;
2636 struct btrfs_inode_item *inode_item;
2637 struct extent_buffer *leaf;
2638 struct btrfs_root *new_root = root;
2639 struct inode *inode;
2644 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2645 unsigned long nr = 1;
2647 mutex_lock(&root->fs_info->fs_mutex);
2648 ret = btrfs_check_free_space(root, 1, 0);
2652 trans = btrfs_start_transaction(root, 1);
2655 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2660 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2661 objectid, trans->transid, 0, 0,
2664 return PTR_ERR(leaf);
2666 btrfs_set_header_nritems(leaf, 0);
2667 btrfs_set_header_level(leaf, 0);
2668 btrfs_set_header_bytenr(leaf, leaf->start);
2669 btrfs_set_header_generation(leaf, trans->transid);
2670 btrfs_set_header_owner(leaf, objectid);
2672 write_extent_buffer(leaf, root->fs_info->fsid,
2673 (unsigned long)btrfs_header_fsid(leaf),
2675 btrfs_mark_buffer_dirty(leaf);
2677 inode_item = &root_item.inode;
2678 memset(inode_item, 0, sizeof(*inode_item));
2679 inode_item->generation = cpu_to_le64(1);
2680 inode_item->size = cpu_to_le64(3);
2681 inode_item->nlink = cpu_to_le32(1);
2682 inode_item->nblocks = cpu_to_le64(1);
2683 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2685 btrfs_set_root_bytenr(&root_item, leaf->start);
2686 btrfs_set_root_level(&root_item, 0);
2687 btrfs_set_root_refs(&root_item, 1);
2688 btrfs_set_root_used(&root_item, 0);
2690 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2691 root_item.drop_level = 0;
2693 free_extent_buffer(leaf);
2696 btrfs_set_root_dirid(&root_item, new_dirid);
2698 key.objectid = objectid;
2700 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2701 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2707 * insert the directory item
2709 key.offset = (u64)-1;
2710 dir = root->fs_info->sb->s_root->d_inode;
2711 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2712 name, namelen, dir->i_ino, &key,
2717 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2718 name, namelen, objectid,
2719 root->fs_info->sb->s_root->d_inode->i_ino);
2723 ret = btrfs_commit_transaction(trans, root);
2727 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2730 trans = btrfs_start_transaction(new_root, 1);
2733 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2735 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2738 inode->i_op = &btrfs_dir_inode_operations;
2739 inode->i_fop = &btrfs_dir_file_operations;
2740 new_root->inode = inode;
2742 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2746 ret = btrfs_update_inode(trans, new_root, inode);
2750 nr = trans->blocks_used;
2751 err = btrfs_commit_transaction(trans, new_root);
2755 mutex_unlock(&root->fs_info->fs_mutex);
2756 btrfs_btree_balance_dirty(root, nr);
2757 btrfs_throttle(root);
2761 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2763 struct btrfs_pending_snapshot *pending_snapshot;
2764 struct btrfs_trans_handle *trans;
2767 unsigned long nr = 0;
2769 if (!root->ref_cows)
2772 mutex_lock(&root->fs_info->fs_mutex);
2773 ret = btrfs_check_free_space(root, 1, 0);
2777 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2778 if (!pending_snapshot) {
2782 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2783 if (!pending_snapshot->name) {
2785 kfree(pending_snapshot);
2788 memcpy(pending_snapshot->name, name, namelen);
2789 pending_snapshot->name[namelen] = '\0';
2790 trans = btrfs_start_transaction(root, 1);
2792 pending_snapshot->root = root;
2793 list_add(&pending_snapshot->list,
2794 &trans->transaction->pending_snapshots);
2795 ret = btrfs_update_inode(trans, root, root->inode);
2796 err = btrfs_commit_transaction(trans, root);
2799 mutex_unlock(&root->fs_info->fs_mutex);
2800 btrfs_btree_balance_dirty(root, nr);
2801 btrfs_throttle(root);
2805 unsigned long btrfs_force_ra(struct address_space *mapping,
2806 struct file_ra_state *ra, struct file *file,
2807 pgoff_t offset, pgoff_t last_index)
2811 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2812 req_size = last_index - offset + 1;
2813 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2816 req_size = min(last_index - offset + 1, (pgoff_t)128);
2817 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2818 return offset + req_size;
2822 int btrfs_defrag_file(struct file *file) {
2823 struct inode *inode = fdentry(file)->d_inode;
2824 struct btrfs_root *root = BTRFS_I(inode)->root;
2825 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2827 unsigned long last_index;
2828 unsigned long ra_index = 0;
2834 mutex_lock(&root->fs_info->fs_mutex);
2835 ret = btrfs_check_free_space(root, inode->i_size, 0);
2836 mutex_unlock(&root->fs_info->fs_mutex);
2840 mutex_lock(&inode->i_mutex);
2841 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2842 for (i = 0; i <= last_index; i++) {
2843 if (i == ra_index) {
2844 ra_index = btrfs_force_ra(inode->i_mapping,
2846 file, ra_index, last_index);
2848 page = grab_cache_page(inode->i_mapping, i);
2851 if (!PageUptodate(page)) {
2852 btrfs_readpage(NULL, page);
2854 if (!PageUptodate(page)) {
2856 page_cache_release(page);
2860 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2861 page_end = page_start + PAGE_CACHE_SIZE - 1;
2863 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2864 set_extent_delalloc(io_tree, page_start,
2865 page_end, GFP_NOFS);
2867 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2868 set_page_dirty(page);
2870 page_cache_release(page);
2871 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2875 mutex_unlock(&inode->i_mutex);
2879 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2883 struct btrfs_ioctl_vol_args *vol_args;
2884 struct btrfs_trans_handle *trans;
2890 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2895 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2899 namelen = strlen(vol_args->name);
2900 if (namelen > BTRFS_VOL_NAME_MAX) {
2905 sizestr = vol_args->name;
2906 if (!strcmp(sizestr, "max"))
2907 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2909 if (sizestr[0] == '-') {
2912 } else if (sizestr[0] == '+') {
2916 new_size = btrfs_parse_size(sizestr);
2917 if (new_size == 0) {
2923 mutex_lock(&root->fs_info->fs_mutex);
2924 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2927 if (new_size > old_size) {
2931 new_size = old_size - new_size;
2932 } else if (mod > 0) {
2933 new_size = old_size + new_size;
2936 if (new_size < 256 * 1024 * 1024) {
2940 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2945 do_div(new_size, root->sectorsize);
2946 new_size *= root->sectorsize;
2948 printk("new size is %Lu\n", new_size);
2949 if (new_size > old_size) {
2950 trans = btrfs_start_transaction(root, 1);
2951 ret = btrfs_grow_extent_tree(trans, root, new_size);
2952 btrfs_commit_transaction(trans, root);
2954 ret = btrfs_shrink_extent_tree(root, new_size);
2958 mutex_unlock(&root->fs_info->fs_mutex);
2964 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2967 struct btrfs_ioctl_vol_args *vol_args;
2968 struct btrfs_dir_item *di;
2969 struct btrfs_path *path;
2974 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2979 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2984 namelen = strlen(vol_args->name);
2985 if (namelen > BTRFS_VOL_NAME_MAX) {
2989 if (strchr(vol_args->name, '/')) {
2994 path = btrfs_alloc_path();
3000 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3001 mutex_lock(&root->fs_info->fs_mutex);
3002 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3004 vol_args->name, namelen, 0);
3005 mutex_unlock(&root->fs_info->fs_mutex);
3006 btrfs_free_path(path);
3008 if (di && !IS_ERR(di)) {
3018 if (root == root->fs_info->tree_root)
3019 ret = create_subvol(root, vol_args->name, namelen);
3021 ret = create_snapshot(root, vol_args->name, namelen);
3027 static int btrfs_ioctl_defrag(struct file *file)
3029 struct inode *inode = fdentry(file)->d_inode;
3030 struct btrfs_root *root = BTRFS_I(inode)->root;
3032 switch (inode->i_mode & S_IFMT) {
3034 mutex_lock(&root->fs_info->fs_mutex);
3035 btrfs_defrag_root(root, 0);
3036 btrfs_defrag_root(root->fs_info->extent_root, 0);
3037 mutex_unlock(&root->fs_info->fs_mutex);
3040 btrfs_defrag_file(file);
3047 long btrfs_ioctl(struct file *file, unsigned int
3048 cmd, unsigned long arg)
3050 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3053 case BTRFS_IOC_SNAP_CREATE:
3054 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3055 case BTRFS_IOC_DEFRAG:
3056 return btrfs_ioctl_defrag(file);
3057 case BTRFS_IOC_RESIZE:
3058 return btrfs_ioctl_resize(root, (void __user *)arg);
3065 * Called inside transaction, so use GFP_NOFS
3067 struct inode *btrfs_alloc_inode(struct super_block *sb)
3069 struct btrfs_inode *ei;
3071 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3075 ei->ordered_trans = 0;
3076 return &ei->vfs_inode;
3079 void btrfs_destroy_inode(struct inode *inode)
3081 WARN_ON(!list_empty(&inode->i_dentry));
3082 WARN_ON(inode->i_data.nrpages);
3084 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3085 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3088 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3089 static void init_once(struct kmem_cache * cachep, void *foo)
3091 static void init_once(void * foo, struct kmem_cache * cachep,
3092 unsigned long flags)
3095 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3097 inode_init_once(&ei->vfs_inode);
3100 void btrfs_destroy_cachep(void)
3102 if (btrfs_inode_cachep)
3103 kmem_cache_destroy(btrfs_inode_cachep);
3104 if (btrfs_trans_handle_cachep)
3105 kmem_cache_destroy(btrfs_trans_handle_cachep);
3106 if (btrfs_transaction_cachep)
3107 kmem_cache_destroy(btrfs_transaction_cachep);
3108 if (btrfs_bit_radix_cachep)
3109 kmem_cache_destroy(btrfs_bit_radix_cachep);
3110 if (btrfs_path_cachep)
3111 kmem_cache_destroy(btrfs_path_cachep);
3114 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3115 unsigned long extra_flags,
3116 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3117 void (*ctor)(struct kmem_cache *, void *)
3119 void (*ctor)(void *, struct kmem_cache *,
3124 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3125 SLAB_MEM_SPREAD | extra_flags), ctor
3126 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3132 int btrfs_init_cachep(void)
3134 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3135 sizeof(struct btrfs_inode),
3137 if (!btrfs_inode_cachep)
3139 btrfs_trans_handle_cachep =
3140 btrfs_cache_create("btrfs_trans_handle_cache",
3141 sizeof(struct btrfs_trans_handle),
3143 if (!btrfs_trans_handle_cachep)
3145 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3146 sizeof(struct btrfs_transaction),
3148 if (!btrfs_transaction_cachep)
3150 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3151 sizeof(struct btrfs_path),
3153 if (!btrfs_path_cachep)
3155 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3156 SLAB_DESTROY_BY_RCU, NULL);
3157 if (!btrfs_bit_radix_cachep)
3161 btrfs_destroy_cachep();
3165 static int btrfs_getattr(struct vfsmount *mnt,
3166 struct dentry *dentry, struct kstat *stat)
3168 struct inode *inode = dentry->d_inode;
3169 generic_fillattr(inode, stat);
3170 stat->blksize = PAGE_CACHE_SIZE;
3171 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3175 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3176 struct inode * new_dir,struct dentry *new_dentry)
3178 struct btrfs_trans_handle *trans;
3179 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3180 struct inode *new_inode = new_dentry->d_inode;
3181 struct inode *old_inode = old_dentry->d_inode;
3182 struct timespec ctime = CURRENT_TIME;
3183 struct btrfs_path *path;
3186 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3187 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3191 mutex_lock(&root->fs_info->fs_mutex);
3192 ret = btrfs_check_free_space(root, 1, 0);
3196 trans = btrfs_start_transaction(root, 1);
3198 btrfs_set_trans_block_group(trans, new_dir);
3199 path = btrfs_alloc_path();
3205 old_dentry->d_inode->i_nlink++;
3206 old_dir->i_ctime = old_dir->i_mtime = ctime;
3207 new_dir->i_ctime = new_dir->i_mtime = ctime;
3208 old_inode->i_ctime = ctime;
3210 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3215 new_inode->i_ctime = CURRENT_TIME;
3216 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3220 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3225 btrfs_free_path(path);
3226 btrfs_end_transaction(trans, root);
3228 mutex_unlock(&root->fs_info->fs_mutex);
3232 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3233 const char *symname)
3235 struct btrfs_trans_handle *trans;
3236 struct btrfs_root *root = BTRFS_I(dir)->root;
3237 struct btrfs_path *path;
3238 struct btrfs_key key;
3239 struct inode *inode = NULL;
3246 struct btrfs_file_extent_item *ei;
3247 struct extent_buffer *leaf;
3248 unsigned long nr = 0;
3250 name_len = strlen(symname) + 1;
3251 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3252 return -ENAMETOOLONG;
3254 mutex_lock(&root->fs_info->fs_mutex);
3255 err = btrfs_check_free_space(root, 1, 0);
3259 trans = btrfs_start_transaction(root, 1);
3260 btrfs_set_trans_block_group(trans, dir);
3262 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3268 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3270 dentry->d_parent->d_inode->i_ino, objectid,
3271 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3272 err = PTR_ERR(inode);
3276 btrfs_set_trans_block_group(trans, inode);
3277 err = btrfs_add_nondir(trans, dentry, inode, 0);
3281 inode->i_mapping->a_ops = &btrfs_aops;
3282 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3283 inode->i_fop = &btrfs_file_operations;
3284 inode->i_op = &btrfs_file_inode_operations;
3285 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3286 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3287 inode->i_mapping, GFP_NOFS);
3288 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3289 inode->i_mapping, GFP_NOFS);
3290 BTRFS_I(inode)->delalloc_bytes = 0;
3291 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3293 dir->i_sb->s_dirt = 1;
3294 btrfs_update_inode_block_group(trans, inode);
3295 btrfs_update_inode_block_group(trans, dir);
3299 path = btrfs_alloc_path();
3301 key.objectid = inode->i_ino;
3303 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3304 datasize = btrfs_file_extent_calc_inline_size(name_len);
3305 err = btrfs_insert_empty_item(trans, root, path, &key,
3311 leaf = path->nodes[0];
3312 ei = btrfs_item_ptr(leaf, path->slots[0],
3313 struct btrfs_file_extent_item);
3314 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3315 btrfs_set_file_extent_type(leaf, ei,
3316 BTRFS_FILE_EXTENT_INLINE);
3317 ptr = btrfs_file_extent_inline_start(ei);
3318 write_extent_buffer(leaf, symname, ptr, name_len);
3319 btrfs_mark_buffer_dirty(leaf);
3320 btrfs_free_path(path);
3322 inode->i_op = &btrfs_symlink_inode_operations;
3323 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3324 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3325 inode->i_size = name_len - 1;
3326 err = btrfs_update_inode(trans, root, inode);
3331 nr = trans->blocks_used;
3332 btrfs_end_transaction(trans, root);
3334 mutex_unlock(&root->fs_info->fs_mutex);
3336 inode_dec_link_count(inode);
3339 btrfs_btree_balance_dirty(root, nr);
3340 btrfs_throttle(root);
3344 static int btrfs_permission(struct inode *inode, int mask,
3345 struct nameidata *nd)
3347 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3349 return generic_permission(inode, mask, NULL);
3352 static struct inode_operations btrfs_dir_inode_operations = {
3353 .lookup = btrfs_lookup,
3354 .create = btrfs_create,
3355 .unlink = btrfs_unlink,
3357 .mkdir = btrfs_mkdir,
3358 .rmdir = btrfs_rmdir,
3359 .rename = btrfs_rename,
3360 .symlink = btrfs_symlink,
3361 .setattr = btrfs_setattr,
3362 .mknod = btrfs_mknod,
3363 .setxattr = generic_setxattr,
3364 .getxattr = generic_getxattr,
3365 .listxattr = btrfs_listxattr,
3366 .removexattr = generic_removexattr,
3367 .permission = btrfs_permission,
3369 static struct inode_operations btrfs_dir_ro_inode_operations = {
3370 .lookup = btrfs_lookup,
3371 .permission = btrfs_permission,
3373 static struct file_operations btrfs_dir_file_operations = {
3374 .llseek = generic_file_llseek,
3375 .read = generic_read_dir,
3376 .readdir = btrfs_readdir,
3377 .unlocked_ioctl = btrfs_ioctl,
3378 #ifdef CONFIG_COMPAT
3379 .compat_ioctl = btrfs_ioctl,
3383 static struct extent_io_ops btrfs_extent_io_ops = {
3384 .fill_delalloc = run_delalloc_range,
3385 .submit_bio_hook = btrfs_submit_bio_hook,
3386 .merge_bio_hook = btrfs_merge_bio_hook,
3387 .readpage_io_hook = btrfs_readpage_io_hook,
3388 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3389 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3390 .set_bit_hook = btrfs_set_bit_hook,
3391 .clear_bit_hook = btrfs_clear_bit_hook,
3394 static struct address_space_operations btrfs_aops = {
3395 .readpage = btrfs_readpage,
3396 .writepage = btrfs_writepage,
3397 .writepages = btrfs_writepages,
3398 .readpages = btrfs_readpages,
3399 .sync_page = block_sync_page,
3401 .direct_IO = btrfs_direct_IO,
3402 .invalidatepage = btrfs_invalidatepage,
3403 .releasepage = btrfs_releasepage,
3404 .set_page_dirty = __set_page_dirty_nobuffers,
3407 static struct address_space_operations btrfs_symlink_aops = {
3408 .readpage = btrfs_readpage,
3409 .writepage = btrfs_writepage,
3410 .invalidatepage = btrfs_invalidatepage,
3411 .releasepage = btrfs_releasepage,
3414 static struct inode_operations btrfs_file_inode_operations = {
3415 .truncate = btrfs_truncate,
3416 .getattr = btrfs_getattr,
3417 .setattr = btrfs_setattr,
3418 .setxattr = generic_setxattr,
3419 .getxattr = generic_getxattr,
3420 .listxattr = btrfs_listxattr,
3421 .removexattr = generic_removexattr,
3422 .permission = btrfs_permission,
3424 static struct inode_operations btrfs_special_inode_operations = {
3425 .getattr = btrfs_getattr,
3426 .setattr = btrfs_setattr,
3427 .permission = btrfs_permission,
3429 static struct inode_operations btrfs_symlink_inode_operations = {
3430 .readlink = generic_readlink,
3431 .follow_link = page_follow_link_light,
3432 .put_link = page_put_link,
3433 .permission = btrfs_permission,