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);
93 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
94 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
96 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
100 static int cow_file_range(struct inode *inode, u64 start, u64 end)
102 struct btrfs_root *root = BTRFS_I(inode)->root;
103 struct btrfs_trans_handle *trans;
107 u64 blocksize = root->sectorsize;
108 u64 orig_start = start;
110 struct btrfs_key ins;
113 trans = btrfs_start_transaction(root, 1);
115 btrfs_set_trans_block_group(trans, inode);
117 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
118 num_bytes = max(blocksize, num_bytes);
119 ret = btrfs_drop_extents(trans, root, inode,
120 start, start + num_bytes, start, &alloc_hint);
121 orig_num_bytes = num_bytes;
123 if (alloc_hint == EXTENT_MAP_INLINE)
126 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
128 while(num_bytes > 0) {
129 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
130 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
132 root->root_key.objectid,
134 inode->i_ino, start, 0,
135 alloc_hint, (u64)-1, &ins, 1);
140 cur_alloc_size = ins.offset;
141 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
142 start, ins.objectid, ins.offset,
144 inode->i_blocks += ins.offset >> 9;
145 btrfs_check_file(root, inode);
146 if (num_bytes < cur_alloc_size) {
147 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
151 num_bytes -= cur_alloc_size;
152 alloc_hint = ins.objectid + ins.offset;
153 start += cur_alloc_size;
155 btrfs_drop_extent_cache(inode, orig_start,
156 orig_start + orig_num_bytes - 1);
157 btrfs_add_ordered_inode(inode);
158 btrfs_update_inode(trans, root, inode);
160 btrfs_end_transaction(trans, root);
164 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
172 struct btrfs_root *root = BTRFS_I(inode)->root;
173 struct extent_buffer *leaf;
175 struct btrfs_path *path;
176 struct btrfs_file_extent_item *item;
179 struct btrfs_key found_key;
181 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
182 path = btrfs_alloc_path();
185 ret = btrfs_lookup_file_extent(NULL, root, path,
186 inode->i_ino, start, 0);
188 btrfs_free_path(path);
194 if (path->slots[0] == 0)
199 leaf = path->nodes[0];
200 item = btrfs_item_ptr(leaf, path->slots[0],
201 struct btrfs_file_extent_item);
203 /* are we inside the extent that was found? */
204 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
205 found_type = btrfs_key_type(&found_key);
206 if (found_key.objectid != inode->i_ino ||
207 found_type != BTRFS_EXTENT_DATA_KEY) {
211 found_type = btrfs_file_extent_type(leaf, item);
212 extent_start = found_key.offset;
213 if (found_type == BTRFS_FILE_EXTENT_REG) {
214 u64 extent_num_bytes;
216 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
217 extent_end = extent_start + extent_num_bytes;
220 if (loops && start != extent_start)
223 if (start < extent_start || start >= extent_end)
226 cow_end = min(end, extent_end - 1);
227 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
232 * we may be called by the resizer, make sure we're inside
233 * the limits of the FS
235 if (bytenr + extent_num_bytes > total_fs_bytes)
238 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
248 btrfs_free_path(path);
251 btrfs_release_path(root, path);
256 cow_file_range(inode, start, cow_end);
261 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
263 struct btrfs_root *root = BTRFS_I(inode)->root;
265 mutex_lock(&root->fs_info->fs_mutex);
266 if (btrfs_test_opt(root, NODATACOW) ||
267 btrfs_test_flag(inode, NODATACOW))
268 ret = run_delalloc_nocow(inode, start, end);
270 ret = cow_file_range(inode, start, end);
272 mutex_unlock(&root->fs_info->fs_mutex);
276 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
277 unsigned long old, unsigned long bits)
280 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
283 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
284 root->fs_info->delalloc_bytes += end - start + 1;
285 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
290 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
291 unsigned long old, unsigned long bits)
293 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
294 struct btrfs_root *root = BTRFS_I(inode)->root;
297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
298 if (end - start + 1 > root->fs_info->delalloc_bytes) {
299 printk("warning: delalloc account %Lu %Lu\n",
300 end - start + 1, root->fs_info->delalloc_bytes);
301 root->fs_info->delalloc_bytes = 0;
302 BTRFS_I(inode)->delalloc_bytes = 0;
304 root->fs_info->delalloc_bytes -= end - start + 1;
305 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
307 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
312 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
313 size_t size, struct bio *bio)
315 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
316 struct btrfs_mapping_tree *map_tree;
317 u64 logical = bio->bi_sector << 9;
322 length = bio->bi_size;
323 map_tree = &root->fs_info->mapping_tree;
325 ret = btrfs_map_block(map_tree, READ, logical,
326 &map_length, NULL, 0);
328 if (map_length < length + size) {
334 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
342 ret = btrfs_csum_one_bio(root, bio, &sums);
345 mutex_lock(&root->fs_info->fs_mutex);
346 trans = btrfs_start_transaction(root, 1);
348 btrfs_set_trans_block_group(trans, inode);
349 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
351 ret = btrfs_end_transaction(trans, root);
353 mutex_unlock(&root->fs_info->fs_mutex);
357 return btrfs_map_bio(root, rw, bio, mirror_num);
360 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
363 struct btrfs_root *root = BTRFS_I(inode)->root;
366 if (!(rw & (1 << BIO_RW))) {
367 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
372 if (btrfs_test_opt(root, NODATASUM) ||
373 btrfs_test_flag(inode, NODATASUM)) {
377 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
378 inode, rw, bio, mirror_num,
379 __btrfs_submit_bio_hook);
381 return btrfs_map_bio(root, rw, bio, mirror_num);
384 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
387 struct inode *inode = page->mapping->host;
388 struct btrfs_root *root = BTRFS_I(inode)->root;
389 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
390 struct btrfs_csum_item *item;
391 struct btrfs_path *path = NULL;
394 if (btrfs_test_opt(root, NODATASUM) ||
395 btrfs_test_flag(inode, NODATASUM))
398 mutex_lock(&root->fs_info->fs_mutex);
399 path = btrfs_alloc_path();
400 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
403 /* a csum that isn't present is a preallocated region. */
404 if (ret == -ENOENT || ret == -EFBIG)
407 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
410 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
412 set_state_private(io_tree, start, csum);
415 btrfs_free_path(path);
416 mutex_unlock(&root->fs_info->fs_mutex);
420 struct io_failure_record {
428 int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
429 struct page *page, u64 start, u64 end,
430 struct extent_state *state)
432 struct io_failure_record *failrec = NULL;
434 struct extent_map *em;
435 struct inode *inode = page->mapping->host;
436 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
437 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
443 ret = get_state_private(failure_tree, start, &private);
445 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
448 failrec->start = start;
449 failrec->len = end - start + 1;
450 failrec->last_mirror = 0;
452 spin_lock(&em_tree->lock);
453 em = lookup_extent_mapping(em_tree, start, failrec->len);
454 if (em->start > start || em->start + em->len < start) {
458 spin_unlock(&em_tree->lock);
460 if (!em || IS_ERR(em)) {
464 logical = start - em->start;
465 logical = em->block_start + logical;
466 failrec->logical = logical;
468 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
469 EXTENT_DIRTY, GFP_NOFS);
470 set_state_private(failure_tree, start,
471 (u64)(unsigned long)failrec);
473 failrec = (struct io_failure_record *)(unsigned long)private;
475 num_copies = btrfs_num_copies(
476 &BTRFS_I(inode)->root->fs_info->mapping_tree,
477 failrec->logical, failrec->len);
478 failrec->last_mirror++;
480 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
481 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
484 if (state && state->start != failrec->start)
486 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
488 if (!state || failrec->last_mirror > num_copies) {
489 set_state_private(failure_tree, failrec->start, 0);
490 clear_extent_bits(failure_tree, failrec->start,
491 failrec->start + failrec->len - 1,
492 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
496 bio = bio_alloc(GFP_NOFS, 1);
497 bio->bi_private = state;
498 bio->bi_end_io = failed_bio->bi_end_io;
499 bio->bi_sector = failrec->logical >> 9;
500 bio->bi_bdev = failed_bio->bi_bdev;
502 bio_add_page(bio, page, failrec->len, start - page_offset(page));
503 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
507 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
508 struct extent_state *state)
510 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
511 struct inode *inode = page->mapping->host;
512 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
514 u64 private = ~(u32)0;
516 struct btrfs_root *root = BTRFS_I(inode)->root;
520 if (btrfs_test_opt(root, NODATASUM) ||
521 btrfs_test_flag(inode, NODATASUM))
523 if (state && state->start == start) {
524 private = state->private;
527 ret = get_state_private(io_tree, start, &private);
529 local_irq_save(flags);
530 kaddr = kmap_atomic(page, KM_IRQ0);
534 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
535 btrfs_csum_final(csum, (char *)&csum);
536 if (csum != private) {
539 kunmap_atomic(kaddr, KM_IRQ0);
540 local_irq_restore(flags);
542 /* if the io failure tree for this inode is non-empty,
543 * check to see if we've recovered from a failed IO
546 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
547 (u64)-1, 1, EXTENT_DIRTY)) {
549 struct io_failure_record *failure;
550 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
551 start, &private_failure);
553 failure = (struct io_failure_record *)(unsigned long)
555 set_state_private(&BTRFS_I(inode)->io_failure_tree,
557 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
559 failure->start + failure->len - 1,
560 EXTENT_DIRTY | EXTENT_LOCKED,
568 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
569 page->mapping->host->i_ino, (unsigned long long)start, csum,
571 memset(kaddr + offset, 1, end - start + 1);
572 flush_dcache_page(page);
573 kunmap_atomic(kaddr, KM_IRQ0);
574 local_irq_restore(flags);
580 void btrfs_read_locked_inode(struct inode *inode)
582 struct btrfs_path *path;
583 struct extent_buffer *leaf;
584 struct btrfs_inode_item *inode_item;
585 struct btrfs_timespec *tspec;
586 struct btrfs_root *root = BTRFS_I(inode)->root;
587 struct btrfs_key location;
588 u64 alloc_group_block;
592 path = btrfs_alloc_path();
594 mutex_lock(&root->fs_info->fs_mutex);
595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
601 leaf = path->nodes[0];
602 inode_item = btrfs_item_ptr(leaf, path->slots[0],
603 struct btrfs_inode_item);
605 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
606 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
607 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
608 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
609 inode->i_size = btrfs_inode_size(leaf, inode_item);
611 tspec = btrfs_inode_atime(inode_item);
612 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
613 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
615 tspec = btrfs_inode_mtime(inode_item);
616 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
617 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
619 tspec = btrfs_inode_ctime(inode_item);
620 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
621 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
623 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
624 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
626 rdev = btrfs_inode_rdev(leaf, inode_item);
628 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
629 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
631 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
632 if (!BTRFS_I(inode)->block_group) {
633 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
635 BTRFS_BLOCK_GROUP_METADATA, 0);
637 btrfs_free_path(path);
640 mutex_unlock(&root->fs_info->fs_mutex);
642 switch (inode->i_mode & S_IFMT) {
644 inode->i_mapping->a_ops = &btrfs_aops;
645 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
646 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
647 inode->i_fop = &btrfs_file_operations;
648 inode->i_op = &btrfs_file_inode_operations;
651 inode->i_fop = &btrfs_dir_file_operations;
652 if (root == root->fs_info->tree_root)
653 inode->i_op = &btrfs_dir_ro_inode_operations;
655 inode->i_op = &btrfs_dir_inode_operations;
658 inode->i_op = &btrfs_symlink_inode_operations;
659 inode->i_mapping->a_ops = &btrfs_symlink_aops;
660 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
663 init_special_inode(inode, inode->i_mode, rdev);
669 btrfs_release_path(root, path);
670 btrfs_free_path(path);
671 mutex_unlock(&root->fs_info->fs_mutex);
672 make_bad_inode(inode);
675 static void fill_inode_item(struct extent_buffer *leaf,
676 struct btrfs_inode_item *item,
679 btrfs_set_inode_uid(leaf, item, inode->i_uid);
680 btrfs_set_inode_gid(leaf, item, inode->i_gid);
681 btrfs_set_inode_size(leaf, item, inode->i_size);
682 btrfs_set_inode_mode(leaf, item, inode->i_mode);
683 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
685 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
686 inode->i_atime.tv_sec);
687 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
688 inode->i_atime.tv_nsec);
690 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
691 inode->i_mtime.tv_sec);
692 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
693 inode->i_mtime.tv_nsec);
695 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
696 inode->i_ctime.tv_sec);
697 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
698 inode->i_ctime.tv_nsec);
700 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
701 btrfs_set_inode_generation(leaf, item, inode->i_generation);
702 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
703 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
704 btrfs_set_inode_block_group(leaf, item,
705 BTRFS_I(inode)->block_group->key.objectid);
708 int btrfs_update_inode(struct btrfs_trans_handle *trans,
709 struct btrfs_root *root,
712 struct btrfs_inode_item *inode_item;
713 struct btrfs_path *path;
714 struct extent_buffer *leaf;
717 path = btrfs_alloc_path();
719 ret = btrfs_lookup_inode(trans, root, path,
720 &BTRFS_I(inode)->location, 1);
727 leaf = path->nodes[0];
728 inode_item = btrfs_item_ptr(leaf, path->slots[0],
729 struct btrfs_inode_item);
731 fill_inode_item(leaf, inode_item, inode);
732 btrfs_mark_buffer_dirty(leaf);
733 btrfs_set_inode_last_trans(trans, inode);
736 btrfs_release_path(root, path);
737 btrfs_free_path(path);
742 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
745 struct dentry *dentry)
747 struct btrfs_path *path;
748 const char *name = dentry->d_name.name;
749 int name_len = dentry->d_name.len;
751 struct extent_buffer *leaf;
752 struct btrfs_dir_item *di;
753 struct btrfs_key key;
755 path = btrfs_alloc_path();
761 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
771 leaf = path->nodes[0];
772 btrfs_dir_item_key_to_cpu(leaf, di, &key);
773 ret = btrfs_delete_one_dir_name(trans, root, path, di);
776 btrfs_release_path(root, path);
778 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
779 key.objectid, name, name_len, -1);
788 ret = btrfs_delete_one_dir_name(trans, root, path, di);
790 dentry->d_inode->i_ctime = dir->i_ctime;
791 ret = btrfs_del_inode_ref(trans, root, name, name_len,
792 dentry->d_inode->i_ino,
793 dentry->d_parent->d_inode->i_ino);
795 printk("failed to delete reference to %.*s, "
796 "inode %lu parent %lu\n", name_len, name,
797 dentry->d_inode->i_ino,
798 dentry->d_parent->d_inode->i_ino);
801 btrfs_free_path(path);
803 dir->i_size -= name_len * 2;
804 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
805 btrfs_update_inode(trans, root, dir);
806 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
807 dentry->d_inode->i_nlink--;
809 drop_nlink(dentry->d_inode);
811 ret = btrfs_update_inode(trans, root, dentry->d_inode);
812 dir->i_sb->s_dirt = 1;
817 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
819 struct btrfs_root *root;
820 struct btrfs_trans_handle *trans;
821 struct inode *inode = dentry->d_inode;
823 unsigned long nr = 0;
825 root = BTRFS_I(dir)->root;
826 mutex_lock(&root->fs_info->fs_mutex);
828 ret = btrfs_check_free_space(root, 1, 1);
832 trans = btrfs_start_transaction(root, 1);
834 btrfs_set_trans_block_group(trans, dir);
835 ret = btrfs_unlink_trans(trans, root, dir, dentry);
836 nr = trans->blocks_used;
838 if (inode->i_nlink == 0) {
840 /* if the inode isn't linked anywhere,
841 * we don't need to worry about
844 found = btrfs_del_ordered_inode(inode);
846 atomic_dec(&inode->i_count);
850 btrfs_end_transaction(trans, root);
852 mutex_unlock(&root->fs_info->fs_mutex);
853 btrfs_btree_balance_dirty(root, nr);
854 btrfs_throttle(root);
858 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
860 struct inode *inode = dentry->d_inode;
863 struct btrfs_root *root = BTRFS_I(dir)->root;
864 struct btrfs_trans_handle *trans;
865 unsigned long nr = 0;
867 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
870 mutex_lock(&root->fs_info->fs_mutex);
871 ret = btrfs_check_free_space(root, 1, 1);
875 trans = btrfs_start_transaction(root, 1);
876 btrfs_set_trans_block_group(trans, dir);
878 /* now the directory is empty */
879 err = btrfs_unlink_trans(trans, root, dir, dentry);
884 nr = trans->blocks_used;
885 ret = btrfs_end_transaction(trans, root);
887 mutex_unlock(&root->fs_info->fs_mutex);
888 btrfs_btree_balance_dirty(root, nr);
889 btrfs_throttle(root);
897 * this can truncate away extent items, csum items and directory items.
898 * It starts at a high offset and removes keys until it can't find
899 * any higher than i_size.
901 * csum items that cross the new i_size are truncated to the new size
904 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
910 struct btrfs_path *path;
911 struct btrfs_key key;
912 struct btrfs_key found_key;
914 struct extent_buffer *leaf;
915 struct btrfs_file_extent_item *fi;
916 u64 extent_start = 0;
917 u64 extent_num_bytes = 0;
923 int pending_del_nr = 0;
924 int pending_del_slot = 0;
925 int extent_type = -1;
926 u64 mask = root->sectorsize - 1;
928 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
929 path = btrfs_alloc_path();
933 /* FIXME, add redo link to tree so we don't leak on crash */
934 key.objectid = inode->i_ino;
935 key.offset = (u64)-1;
938 btrfs_init_path(path);
940 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
945 BUG_ON(path->slots[0] == 0);
951 leaf = path->nodes[0];
952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
953 found_type = btrfs_key_type(&found_key);
955 if (found_key.objectid != inode->i_ino)
958 if (found_type < min_type)
961 item_end = found_key.offset;
962 if (found_type == BTRFS_EXTENT_DATA_KEY) {
963 fi = btrfs_item_ptr(leaf, path->slots[0],
964 struct btrfs_file_extent_item);
965 extent_type = btrfs_file_extent_type(leaf, fi);
966 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
968 btrfs_file_extent_num_bytes(leaf, fi);
969 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
970 struct btrfs_item *item = btrfs_item_nr(leaf,
972 item_end += btrfs_file_extent_inline_len(leaf,
977 if (found_type == BTRFS_CSUM_ITEM_KEY) {
978 ret = btrfs_csum_truncate(trans, root, path,
982 if (item_end < inode->i_size) {
983 if (found_type == BTRFS_DIR_ITEM_KEY) {
984 found_type = BTRFS_INODE_ITEM_KEY;
985 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
986 found_type = BTRFS_CSUM_ITEM_KEY;
987 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
988 found_type = BTRFS_XATTR_ITEM_KEY;
989 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
990 found_type = BTRFS_INODE_REF_KEY;
991 } else if (found_type) {
996 btrfs_set_key_type(&key, found_type);
999 if (found_key.offset >= inode->i_size)
1005 /* FIXME, shrink the extent if the ref count is only 1 */
1006 if (found_type != BTRFS_EXTENT_DATA_KEY)
1009 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1011 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1013 u64 orig_num_bytes =
1014 btrfs_file_extent_num_bytes(leaf, fi);
1015 extent_num_bytes = inode->i_size -
1016 found_key.offset + root->sectorsize - 1;
1017 extent_num_bytes = extent_num_bytes &
1018 ~((u64)root->sectorsize - 1);
1019 btrfs_set_file_extent_num_bytes(leaf, fi,
1021 num_dec = (orig_num_bytes -
1023 if (extent_start != 0)
1024 dec_i_blocks(inode, num_dec);
1025 btrfs_mark_buffer_dirty(leaf);
1028 btrfs_file_extent_disk_num_bytes(leaf,
1030 /* FIXME blocksize != 4096 */
1031 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1032 if (extent_start != 0) {
1034 dec_i_blocks(inode, num_dec);
1036 root_gen = btrfs_header_generation(leaf);
1037 root_owner = btrfs_header_owner(leaf);
1039 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1041 u32 newsize = inode->i_size - found_key.offset;
1042 dec_i_blocks(inode, item_end + 1 -
1043 found_key.offset - newsize);
1045 btrfs_file_extent_calc_inline_size(newsize);
1046 ret = btrfs_truncate_item(trans, root, path,
1050 dec_i_blocks(inode, item_end + 1 -
1056 if (!pending_del_nr) {
1057 /* no pending yet, add ourselves */
1058 pending_del_slot = path->slots[0];
1060 } else if (pending_del_nr &&
1061 path->slots[0] + 1 == pending_del_slot) {
1062 /* hop on the pending chunk */
1064 pending_del_slot = path->slots[0];
1066 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1072 ret = btrfs_free_extent(trans, root, extent_start,
1075 root_gen, inode->i_ino,
1076 found_key.offset, 0);
1080 if (path->slots[0] == 0) {
1083 btrfs_release_path(root, path);
1088 if (pending_del_nr &&
1089 path->slots[0] + 1 != pending_del_slot) {
1090 struct btrfs_key debug;
1092 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1094 ret = btrfs_del_items(trans, root, path,
1099 btrfs_release_path(root, path);
1105 if (pending_del_nr) {
1106 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1109 btrfs_release_path(root, path);
1110 btrfs_free_path(path);
1111 inode->i_sb->s_dirt = 1;
1115 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
1119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1120 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1121 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
1124 WARN_ON(!PageLocked(page));
1125 set_page_extent_mapped(page);
1127 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1128 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1129 page_end, GFP_NOFS);
1131 if (zero_start != PAGE_CACHE_SIZE) {
1133 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1134 flush_dcache_page(page);
1137 set_page_dirty(page);
1138 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1144 * taken from block_truncate_page, but does cow as it zeros out
1145 * any bytes left in the last page in the file.
1147 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1149 struct inode *inode = mapping->host;
1150 struct btrfs_root *root = BTRFS_I(inode)->root;
1151 u32 blocksize = root->sectorsize;
1152 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1153 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1158 if ((offset & (blocksize - 1)) == 0)
1162 page = grab_cache_page(mapping, index);
1165 if (!PageUptodate(page)) {
1166 ret = btrfs_readpage(NULL, page);
1168 if (!PageUptodate(page)) {
1173 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1175 ret = btrfs_cow_one_page(inode, page, offset);
1178 page_cache_release(page);
1183 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1185 struct inode *inode = dentry->d_inode;
1188 err = inode_change_ok(inode, attr);
1192 if (S_ISREG(inode->i_mode) &&
1193 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1194 struct btrfs_trans_handle *trans;
1195 struct btrfs_root *root = BTRFS_I(inode)->root;
1196 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1198 u64 mask = root->sectorsize - 1;
1199 u64 hole_start = (inode->i_size + mask) & ~mask;
1200 u64 block_end = (attr->ia_size + mask) & ~mask;
1204 if (attr->ia_size <= hole_start)
1207 mutex_lock(&root->fs_info->fs_mutex);
1208 err = btrfs_check_free_space(root, 1, 0);
1209 mutex_unlock(&root->fs_info->fs_mutex);
1213 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1215 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1216 hole_size = block_end - hole_start;
1218 mutex_lock(&root->fs_info->fs_mutex);
1219 trans = btrfs_start_transaction(root, 1);
1220 btrfs_set_trans_block_group(trans, inode);
1221 err = btrfs_drop_extents(trans, root, inode,
1222 hole_start, block_end, hole_start,
1225 if (alloc_hint != EXTENT_MAP_INLINE) {
1226 err = btrfs_insert_file_extent(trans, root,
1230 btrfs_drop_extent_cache(inode, hole_start,
1232 btrfs_check_file(root, inode);
1234 btrfs_end_transaction(trans, root);
1235 mutex_unlock(&root->fs_info->fs_mutex);
1236 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1241 err = inode_setattr(inode, attr);
1246 void btrfs_put_inode(struct inode *inode)
1250 if (!BTRFS_I(inode)->ordered_trans) {
1254 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1255 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1258 ret = btrfs_del_ordered_inode(inode);
1260 atomic_dec(&inode->i_count);
1264 void btrfs_delete_inode(struct inode *inode)
1266 struct btrfs_trans_handle *trans;
1267 struct btrfs_root *root = BTRFS_I(inode)->root;
1271 truncate_inode_pages(&inode->i_data, 0);
1272 if (is_bad_inode(inode)) {
1277 mutex_lock(&root->fs_info->fs_mutex);
1278 trans = btrfs_start_transaction(root, 1);
1280 btrfs_set_trans_block_group(trans, inode);
1281 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1283 goto no_delete_lock;
1285 nr = trans->blocks_used;
1288 btrfs_end_transaction(trans, root);
1289 mutex_unlock(&root->fs_info->fs_mutex);
1290 btrfs_btree_balance_dirty(root, nr);
1291 btrfs_throttle(root);
1295 nr = trans->blocks_used;
1296 btrfs_end_transaction(trans, root);
1297 mutex_unlock(&root->fs_info->fs_mutex);
1298 btrfs_btree_balance_dirty(root, nr);
1299 btrfs_throttle(root);
1305 * this returns the key found in the dir entry in the location pointer.
1306 * If no dir entries were found, location->objectid is 0.
1308 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1309 struct btrfs_key *location)
1311 const char *name = dentry->d_name.name;
1312 int namelen = dentry->d_name.len;
1313 struct btrfs_dir_item *di;
1314 struct btrfs_path *path;
1315 struct btrfs_root *root = BTRFS_I(dir)->root;
1318 if (namelen == 1 && strcmp(name, ".") == 0) {
1319 location->objectid = dir->i_ino;
1320 location->type = BTRFS_INODE_ITEM_KEY;
1321 location->offset = 0;
1324 path = btrfs_alloc_path();
1327 if (namelen == 2 && strcmp(name, "..") == 0) {
1328 struct btrfs_key key;
1329 struct extent_buffer *leaf;
1333 key.objectid = dir->i_ino;
1334 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1336 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1340 leaf = path->nodes[0];
1341 slot = path->slots[0];
1342 nritems = btrfs_header_nritems(leaf);
1343 if (slot >= nritems)
1346 btrfs_item_key_to_cpu(leaf, &key, slot);
1347 if (key.objectid != dir->i_ino ||
1348 key.type != BTRFS_INODE_REF_KEY) {
1351 location->objectid = key.offset;
1352 location->type = BTRFS_INODE_ITEM_KEY;
1353 location->offset = 0;
1357 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1361 if (!di || IS_ERR(di)) {
1364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1366 btrfs_free_path(path);
1369 location->objectid = 0;
1374 * when we hit a tree root in a directory, the btrfs part of the inode
1375 * needs to be changed to reflect the root directory of the tree root. This
1376 * is kind of like crossing a mount point.
1378 static int fixup_tree_root_location(struct btrfs_root *root,
1379 struct btrfs_key *location,
1380 struct btrfs_root **sub_root,
1381 struct dentry *dentry)
1383 struct btrfs_path *path;
1384 struct btrfs_root_item *ri;
1386 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1388 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1391 path = btrfs_alloc_path();
1393 mutex_lock(&root->fs_info->fs_mutex);
1395 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1396 dentry->d_name.name,
1397 dentry->d_name.len);
1398 if (IS_ERR(*sub_root))
1399 return PTR_ERR(*sub_root);
1401 ri = &(*sub_root)->root_item;
1402 location->objectid = btrfs_root_dirid(ri);
1403 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1404 location->offset = 0;
1406 btrfs_free_path(path);
1407 mutex_unlock(&root->fs_info->fs_mutex);
1411 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1413 struct btrfs_iget_args *args = p;
1414 inode->i_ino = args->ino;
1415 BTRFS_I(inode)->root = args->root;
1416 BTRFS_I(inode)->delalloc_bytes = 0;
1417 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1418 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1419 inode->i_mapping, GFP_NOFS);
1420 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1421 inode->i_mapping, GFP_NOFS);
1425 static int btrfs_find_actor(struct inode *inode, void *opaque)
1427 struct btrfs_iget_args *args = opaque;
1428 return (args->ino == inode->i_ino &&
1429 args->root == BTRFS_I(inode)->root);
1432 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1435 struct btrfs_iget_args args;
1436 args.ino = objectid;
1437 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1442 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1445 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1446 struct btrfs_root *root)
1448 struct inode *inode;
1449 struct btrfs_iget_args args;
1450 args.ino = objectid;
1453 inode = iget5_locked(s, objectid, btrfs_find_actor,
1454 btrfs_init_locked_inode,
1459 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1460 struct nameidata *nd)
1462 struct inode * inode;
1463 struct btrfs_inode *bi = BTRFS_I(dir);
1464 struct btrfs_root *root = bi->root;
1465 struct btrfs_root *sub_root = root;
1466 struct btrfs_key location;
1469 if (dentry->d_name.len > BTRFS_NAME_LEN)
1470 return ERR_PTR(-ENAMETOOLONG);
1472 mutex_lock(&root->fs_info->fs_mutex);
1473 ret = btrfs_inode_by_name(dir, dentry, &location);
1474 mutex_unlock(&root->fs_info->fs_mutex);
1477 return ERR_PTR(ret);
1480 if (location.objectid) {
1481 ret = fixup_tree_root_location(root, &location, &sub_root,
1484 return ERR_PTR(ret);
1486 return ERR_PTR(-ENOENT);
1487 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1490 return ERR_PTR(-EACCES);
1491 if (inode->i_state & I_NEW) {
1492 /* the inode and parent dir are two different roots */
1493 if (sub_root != root) {
1495 sub_root->inode = inode;
1497 BTRFS_I(inode)->root = sub_root;
1498 memcpy(&BTRFS_I(inode)->location, &location,
1500 btrfs_read_locked_inode(inode);
1501 unlock_new_inode(inode);
1504 return d_splice_alias(inode, dentry);
1507 static unsigned char btrfs_filetype_table[] = {
1508 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1511 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1513 struct inode *inode = filp->f_dentry->d_inode;
1514 struct btrfs_root *root = BTRFS_I(inode)->root;
1515 struct btrfs_item *item;
1516 struct btrfs_dir_item *di;
1517 struct btrfs_key key;
1518 struct btrfs_key found_key;
1519 struct btrfs_path *path;
1522 struct extent_buffer *leaf;
1525 unsigned char d_type;
1530 int key_type = BTRFS_DIR_INDEX_KEY;
1535 /* FIXME, use a real flag for deciding about the key type */
1536 if (root->fs_info->tree_root == root)
1537 key_type = BTRFS_DIR_ITEM_KEY;
1539 /* special case for "." */
1540 if (filp->f_pos == 0) {
1541 over = filldir(dirent, ".", 1,
1549 mutex_lock(&root->fs_info->fs_mutex);
1550 key.objectid = inode->i_ino;
1551 path = btrfs_alloc_path();
1554 /* special case for .., just use the back ref */
1555 if (filp->f_pos == 1) {
1556 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1558 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1560 leaf = path->nodes[0];
1561 slot = path->slots[0];
1562 nritems = btrfs_header_nritems(leaf);
1563 if (slot >= nritems) {
1564 btrfs_release_path(root, path);
1565 goto read_dir_items;
1567 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1568 btrfs_release_path(root, path);
1569 if (found_key.objectid != key.objectid ||
1570 found_key.type != BTRFS_INODE_REF_KEY)
1571 goto read_dir_items;
1572 over = filldir(dirent, "..", 2,
1573 2, found_key.offset, DT_DIR);
1580 btrfs_set_key_type(&key, key_type);
1581 key.offset = filp->f_pos;
1583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1588 leaf = path->nodes[0];
1589 nritems = btrfs_header_nritems(leaf);
1590 slot = path->slots[0];
1591 if (advance || slot >= nritems) {
1592 if (slot >= nritems -1) {
1593 ret = btrfs_next_leaf(root, path);
1596 leaf = path->nodes[0];
1597 nritems = btrfs_header_nritems(leaf);
1598 slot = path->slots[0];
1605 item = btrfs_item_nr(leaf, slot);
1606 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1608 if (found_key.objectid != key.objectid)
1610 if (btrfs_key_type(&found_key) != key_type)
1612 if (found_key.offset < filp->f_pos)
1615 filp->f_pos = found_key.offset;
1617 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1619 di_total = btrfs_item_size(leaf, item);
1620 while(di_cur < di_total) {
1621 struct btrfs_key location;
1623 name_len = btrfs_dir_name_len(leaf, di);
1624 if (name_len < 32) {
1625 name_ptr = tmp_name;
1627 name_ptr = kmalloc(name_len, GFP_NOFS);
1630 read_extent_buffer(leaf, name_ptr,
1631 (unsigned long)(di + 1), name_len);
1633 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1634 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1635 over = filldir(dirent, name_ptr, name_len,
1640 if (name_ptr != tmp_name)
1645 di_len = btrfs_dir_name_len(leaf, di) +
1646 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1648 di = (struct btrfs_dir_item *)((char *)di + di_len);
1651 if (key_type == BTRFS_DIR_INDEX_KEY)
1652 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1658 btrfs_release_path(root, path);
1659 btrfs_free_path(path);
1660 mutex_unlock(&root->fs_info->fs_mutex);
1664 int btrfs_write_inode(struct inode *inode, int wait)
1666 struct btrfs_root *root = BTRFS_I(inode)->root;
1667 struct btrfs_trans_handle *trans;
1671 mutex_lock(&root->fs_info->fs_mutex);
1672 trans = btrfs_start_transaction(root, 1);
1673 btrfs_set_trans_block_group(trans, inode);
1674 ret = btrfs_commit_transaction(trans, root);
1675 mutex_unlock(&root->fs_info->fs_mutex);
1681 * This is somewhat expensive, updating the tree every time the
1682 * inode changes. But, it is most likely to find the inode in cache.
1683 * FIXME, needs more benchmarking...there are no reasons other than performance
1684 * to keep or drop this code.
1686 void btrfs_dirty_inode(struct inode *inode)
1688 struct btrfs_root *root = BTRFS_I(inode)->root;
1689 struct btrfs_trans_handle *trans;
1691 mutex_lock(&root->fs_info->fs_mutex);
1692 trans = btrfs_start_transaction(root, 1);
1693 btrfs_set_trans_block_group(trans, inode);
1694 btrfs_update_inode(trans, root, inode);
1695 btrfs_end_transaction(trans, root);
1696 mutex_unlock(&root->fs_info->fs_mutex);
1699 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 const char *name, int name_len,
1704 struct btrfs_block_group_cache *group,
1707 struct inode *inode;
1708 struct btrfs_inode_item *inode_item;
1709 struct btrfs_block_group_cache *new_inode_group;
1710 struct btrfs_key *location;
1711 struct btrfs_path *path;
1712 struct btrfs_inode_ref *ref;
1713 struct btrfs_key key[2];
1719 path = btrfs_alloc_path();
1722 inode = new_inode(root->fs_info->sb);
1724 return ERR_PTR(-ENOMEM);
1726 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1727 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1728 inode->i_mapping, GFP_NOFS);
1729 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1730 inode->i_mapping, GFP_NOFS);
1731 BTRFS_I(inode)->delalloc_bytes = 0;
1732 BTRFS_I(inode)->root = root;
1738 new_inode_group = btrfs_find_block_group(root, group, 0,
1739 BTRFS_BLOCK_GROUP_METADATA, owner);
1740 if (!new_inode_group) {
1741 printk("find_block group failed\n");
1742 new_inode_group = group;
1744 BTRFS_I(inode)->block_group = new_inode_group;
1745 BTRFS_I(inode)->flags = 0;
1747 key[0].objectid = objectid;
1748 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1751 key[1].objectid = objectid;
1752 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1753 key[1].offset = ref_objectid;
1755 sizes[0] = sizeof(struct btrfs_inode_item);
1756 sizes[1] = name_len + sizeof(*ref);
1758 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1762 if (objectid > root->highest_inode)
1763 root->highest_inode = objectid;
1765 inode->i_uid = current->fsuid;
1766 inode->i_gid = current->fsgid;
1767 inode->i_mode = mode;
1768 inode->i_ino = objectid;
1769 inode->i_blocks = 0;
1770 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1771 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1772 struct btrfs_inode_item);
1773 fill_inode_item(path->nodes[0], inode_item, inode);
1775 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1776 struct btrfs_inode_ref);
1777 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1778 ptr = (unsigned long)(ref + 1);
1779 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1781 btrfs_mark_buffer_dirty(path->nodes[0]);
1782 btrfs_free_path(path);
1784 location = &BTRFS_I(inode)->location;
1785 location->objectid = objectid;
1786 location->offset = 0;
1787 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1789 insert_inode_hash(inode);
1792 btrfs_free_path(path);
1793 return ERR_PTR(ret);
1796 static inline u8 btrfs_inode_type(struct inode *inode)
1798 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1801 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1802 struct dentry *dentry, struct inode *inode,
1806 struct btrfs_key key;
1807 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1808 struct inode *parent_inode;
1810 key.objectid = inode->i_ino;
1811 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1814 ret = btrfs_insert_dir_item(trans, root,
1815 dentry->d_name.name, dentry->d_name.len,
1816 dentry->d_parent->d_inode->i_ino,
1817 &key, btrfs_inode_type(inode));
1820 ret = btrfs_insert_inode_ref(trans, root,
1821 dentry->d_name.name,
1824 dentry->d_parent->d_inode->i_ino);
1826 parent_inode = dentry->d_parent->d_inode;
1827 parent_inode->i_size += dentry->d_name.len * 2;
1828 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1829 ret = btrfs_update_inode(trans, root,
1830 dentry->d_parent->d_inode);
1835 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1836 struct dentry *dentry, struct inode *inode,
1839 int err = btrfs_add_link(trans, dentry, inode, backref);
1841 d_instantiate(dentry, inode);
1849 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1850 int mode, dev_t rdev)
1852 struct btrfs_trans_handle *trans;
1853 struct btrfs_root *root = BTRFS_I(dir)->root;
1854 struct inode *inode = NULL;
1858 unsigned long nr = 0;
1860 if (!new_valid_dev(rdev))
1863 mutex_lock(&root->fs_info->fs_mutex);
1864 err = btrfs_check_free_space(root, 1, 0);
1868 trans = btrfs_start_transaction(root, 1);
1869 btrfs_set_trans_block_group(trans, dir);
1871 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1877 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1879 dentry->d_parent->d_inode->i_ino, objectid,
1880 BTRFS_I(dir)->block_group, mode);
1881 err = PTR_ERR(inode);
1885 btrfs_set_trans_block_group(trans, inode);
1886 err = btrfs_add_nondir(trans, dentry, inode, 0);
1890 inode->i_op = &btrfs_special_inode_operations;
1891 init_special_inode(inode, inode->i_mode, rdev);
1892 btrfs_update_inode(trans, root, inode);
1894 dir->i_sb->s_dirt = 1;
1895 btrfs_update_inode_block_group(trans, inode);
1896 btrfs_update_inode_block_group(trans, dir);
1898 nr = trans->blocks_used;
1899 btrfs_end_transaction(trans, root);
1901 mutex_unlock(&root->fs_info->fs_mutex);
1904 inode_dec_link_count(inode);
1907 btrfs_btree_balance_dirty(root, nr);
1908 btrfs_throttle(root);
1912 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1913 int mode, struct nameidata *nd)
1915 struct btrfs_trans_handle *trans;
1916 struct btrfs_root *root = BTRFS_I(dir)->root;
1917 struct inode *inode = NULL;
1920 unsigned long nr = 0;
1923 mutex_lock(&root->fs_info->fs_mutex);
1924 err = btrfs_check_free_space(root, 1, 0);
1927 trans = btrfs_start_transaction(root, 1);
1928 btrfs_set_trans_block_group(trans, dir);
1930 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1936 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1938 dentry->d_parent->d_inode->i_ino,
1939 objectid, BTRFS_I(dir)->block_group, mode);
1940 err = PTR_ERR(inode);
1944 btrfs_set_trans_block_group(trans, inode);
1945 err = btrfs_add_nondir(trans, dentry, inode, 0);
1949 inode->i_mapping->a_ops = &btrfs_aops;
1950 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1951 inode->i_fop = &btrfs_file_operations;
1952 inode->i_op = &btrfs_file_inode_operations;
1953 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1954 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1955 inode->i_mapping, GFP_NOFS);
1956 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1957 inode->i_mapping, GFP_NOFS);
1958 BTRFS_I(inode)->delalloc_bytes = 0;
1959 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1961 dir->i_sb->s_dirt = 1;
1962 btrfs_update_inode_block_group(trans, inode);
1963 btrfs_update_inode_block_group(trans, dir);
1965 nr = trans->blocks_used;
1966 btrfs_end_transaction(trans, root);
1968 mutex_unlock(&root->fs_info->fs_mutex);
1971 inode_dec_link_count(inode);
1974 btrfs_btree_balance_dirty(root, nr);
1975 btrfs_throttle(root);
1979 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1980 struct dentry *dentry)
1982 struct btrfs_trans_handle *trans;
1983 struct btrfs_root *root = BTRFS_I(dir)->root;
1984 struct inode *inode = old_dentry->d_inode;
1985 unsigned long nr = 0;
1989 if (inode->i_nlink == 0)
1992 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1997 mutex_lock(&root->fs_info->fs_mutex);
1998 err = btrfs_check_free_space(root, 1, 0);
2001 trans = btrfs_start_transaction(root, 1);
2003 btrfs_set_trans_block_group(trans, dir);
2004 atomic_inc(&inode->i_count);
2005 err = btrfs_add_nondir(trans, dentry, inode, 1);
2010 dir->i_sb->s_dirt = 1;
2011 btrfs_update_inode_block_group(trans, dir);
2012 err = btrfs_update_inode(trans, root, inode);
2017 nr = trans->blocks_used;
2018 btrfs_end_transaction(trans, root);
2020 mutex_unlock(&root->fs_info->fs_mutex);
2023 inode_dec_link_count(inode);
2026 btrfs_btree_balance_dirty(root, nr);
2027 btrfs_throttle(root);
2031 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2033 struct inode *inode;
2034 struct btrfs_trans_handle *trans;
2035 struct btrfs_root *root = BTRFS_I(dir)->root;
2037 int drop_on_err = 0;
2039 unsigned long nr = 1;
2041 mutex_lock(&root->fs_info->fs_mutex);
2042 err = btrfs_check_free_space(root, 1, 0);
2046 trans = btrfs_start_transaction(root, 1);
2047 btrfs_set_trans_block_group(trans, dir);
2049 if (IS_ERR(trans)) {
2050 err = PTR_ERR(trans);
2054 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2060 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2062 dentry->d_parent->d_inode->i_ino, objectid,
2063 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2064 if (IS_ERR(inode)) {
2065 err = PTR_ERR(inode);
2070 inode->i_op = &btrfs_dir_inode_operations;
2071 inode->i_fop = &btrfs_dir_file_operations;
2072 btrfs_set_trans_block_group(trans, inode);
2075 err = btrfs_update_inode(trans, root, inode);
2079 err = btrfs_add_link(trans, dentry, inode, 0);
2083 d_instantiate(dentry, inode);
2085 dir->i_sb->s_dirt = 1;
2086 btrfs_update_inode_block_group(trans, inode);
2087 btrfs_update_inode_block_group(trans, dir);
2090 nr = trans->blocks_used;
2091 btrfs_end_transaction(trans, root);
2094 mutex_unlock(&root->fs_info->fs_mutex);
2097 btrfs_btree_balance_dirty(root, nr);
2098 btrfs_throttle(root);
2102 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2103 struct extent_map *existing,
2104 struct extent_map *em)
2109 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2111 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2114 if (!real_blocks && em->block_start != existing->block_start)
2117 new_end = max(existing->start + existing->len, em->start + em->len);
2119 if (existing->start >= em->start) {
2120 if (em->start + em->len < existing->start)
2123 start_diff = existing->start - em->start;
2124 if (real_blocks && em->block_start + start_diff !=
2125 existing->block_start)
2128 em->len = new_end - em->start;
2130 remove_extent_mapping(em_tree, existing);
2131 /* free for the tree */
2132 free_extent_map(existing);
2133 ret = add_extent_mapping(em_tree, em);
2135 } else if (em->start > existing->start) {
2137 if (existing->start + existing->len < em->start)
2140 start_diff = em->start - existing->start;
2141 if (real_blocks && existing->block_start + start_diff !=
2145 remove_extent_mapping(em_tree, existing);
2146 em->block_start = existing->block_start;
2147 em->start = existing->start;
2148 em->len = new_end - existing->start;
2149 free_extent_map(existing);
2151 ret = add_extent_mapping(em_tree, em);
2158 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2159 existing->start, existing->len, existing->block_start,
2160 em->start, em->len, em->block_start);
2164 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2165 size_t pg_offset, u64 start, u64 len,
2171 u64 extent_start = 0;
2173 u64 objectid = inode->i_ino;
2175 struct btrfs_path *path;
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
2177 struct btrfs_file_extent_item *item;
2178 struct extent_buffer *leaf;
2179 struct btrfs_key found_key;
2180 struct extent_map *em = NULL;
2181 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2183 struct btrfs_trans_handle *trans = NULL;
2185 path = btrfs_alloc_path();
2187 mutex_lock(&root->fs_info->fs_mutex);
2190 spin_lock(&em_tree->lock);
2191 em = lookup_extent_mapping(em_tree, start, len);
2192 spin_unlock(&em_tree->lock);
2195 if (em->start > start || em->start + em->len <= start)
2196 free_extent_map(em);
2197 else if (em->block_start == EXTENT_MAP_INLINE && page)
2198 free_extent_map(em);
2202 em = alloc_extent_map(GFP_NOFS);
2208 em->start = EXTENT_MAP_HOLE;
2210 em->bdev = inode->i_sb->s_bdev;
2211 ret = btrfs_lookup_file_extent(trans, root, path,
2212 objectid, start, trans != NULL);
2219 if (path->slots[0] == 0)
2224 leaf = path->nodes[0];
2225 item = btrfs_item_ptr(leaf, path->slots[0],
2226 struct btrfs_file_extent_item);
2227 /* are we inside the extent that was found? */
2228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2229 found_type = btrfs_key_type(&found_key);
2230 if (found_key.objectid != objectid ||
2231 found_type != BTRFS_EXTENT_DATA_KEY) {
2235 found_type = btrfs_file_extent_type(leaf, item);
2236 extent_start = found_key.offset;
2237 if (found_type == BTRFS_FILE_EXTENT_REG) {
2238 extent_end = extent_start +
2239 btrfs_file_extent_num_bytes(leaf, item);
2241 if (start < extent_start || start >= extent_end) {
2243 if (start < extent_start) {
2244 if (start + len <= extent_start)
2246 em->len = extent_end - extent_start;
2252 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2254 em->start = extent_start;
2255 em->len = extent_end - extent_start;
2256 em->block_start = EXTENT_MAP_HOLE;
2259 bytenr += btrfs_file_extent_offset(leaf, item);
2260 em->block_start = bytenr;
2261 em->start = extent_start;
2262 em->len = extent_end - extent_start;
2264 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2269 size_t extent_offset;
2272 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2274 extent_end = (extent_start + size + root->sectorsize - 1) &
2275 ~((u64)root->sectorsize - 1);
2276 if (start < extent_start || start >= extent_end) {
2278 if (start < extent_start) {
2279 if (start + len <= extent_start)
2281 em->len = extent_end - extent_start;
2287 em->block_start = EXTENT_MAP_INLINE;
2290 em->start = extent_start;
2295 page_start = page_offset(page) + pg_offset;
2296 extent_offset = page_start - extent_start;
2297 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2298 size - extent_offset);
2299 em->start = extent_start + extent_offset;
2300 em->len = (copy_size + root->sectorsize - 1) &
2301 ~((u64)root->sectorsize - 1);
2303 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2304 if (create == 0 && !PageUptodate(page)) {
2305 read_extent_buffer(leaf, map + pg_offset, ptr,
2307 flush_dcache_page(page);
2308 } else if (create && PageUptodate(page)) {
2311 free_extent_map(em);
2313 btrfs_release_path(root, path);
2314 trans = btrfs_start_transaction(root, 1);
2317 write_extent_buffer(leaf, map + pg_offset, ptr,
2319 btrfs_mark_buffer_dirty(leaf);
2322 set_extent_uptodate(io_tree, em->start,
2323 extent_map_end(em) - 1, GFP_NOFS);
2326 printk("unkknown found_type %d\n", found_type);
2333 em->block_start = EXTENT_MAP_HOLE;
2335 btrfs_release_path(root, path);
2336 if (em->start > start || extent_map_end(em) <= start) {
2337 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2343 spin_lock(&em_tree->lock);
2344 ret = add_extent_mapping(em_tree, em);
2345 /* it is possible that someone inserted the extent into the tree
2346 * while we had the lock dropped. It is also possible that
2347 * an overlapping map exists in the tree
2349 if (ret == -EEXIST) {
2350 struct extent_map *existing;
2351 existing = lookup_extent_mapping(em_tree, start, len);
2352 if (existing && (existing->start > start ||
2353 existing->start + existing->len <= start)) {
2354 free_extent_map(existing);
2358 existing = lookup_extent_mapping(em_tree, em->start,
2361 err = merge_extent_mapping(em_tree, existing,
2363 free_extent_map(existing);
2365 free_extent_map(em);
2370 printk("failing to insert %Lu %Lu\n",
2372 free_extent_map(em);
2376 free_extent_map(em);
2380 spin_unlock(&em_tree->lock);
2382 btrfs_free_path(path);
2384 ret = btrfs_end_transaction(trans, root);
2388 mutex_unlock(&root->fs_info->fs_mutex);
2390 free_extent_map(em);
2392 return ERR_PTR(err);
2397 #if 0 /* waiting for O_DIRECT reads */
2398 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2399 struct buffer_head *bh_result, int create)
2401 struct extent_map *em;
2402 u64 start = (u64)iblock << inode->i_blkbits;
2403 struct btrfs_multi_bio *multi = NULL;
2404 struct btrfs_root *root = BTRFS_I(inode)->root;
2410 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2412 if (!em || IS_ERR(em))
2415 if (em->start > start || em->start + em->len <= start) {
2419 if (em->block_start == EXTENT_MAP_INLINE) {
2424 len = em->start + em->len - start;
2425 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2427 if (em->block_start == EXTENT_MAP_HOLE ||
2428 em->block_start == EXTENT_MAP_DELALLOC) {
2429 bh_result->b_size = len;
2433 logical = start - em->start;
2434 logical = em->block_start + logical;
2437 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2438 logical, &map_length, &multi, 0);
2440 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2441 bh_result->b_size = min(map_length, len);
2443 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2444 set_buffer_mapped(bh_result);
2447 free_extent_map(em);
2452 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2453 const struct iovec *iov, loff_t offset,
2454 unsigned long nr_segs)
2458 struct file *file = iocb->ki_filp;
2459 struct inode *inode = file->f_mapping->host;
2464 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2465 offset, nr_segs, btrfs_get_block, NULL);
2469 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2471 return extent_bmap(mapping, iblock, btrfs_get_extent);
2474 int btrfs_readpage(struct file *file, struct page *page)
2476 struct extent_io_tree *tree;
2477 tree = &BTRFS_I(page->mapping->host)->io_tree;
2478 return extent_read_full_page(tree, page, btrfs_get_extent);
2481 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2483 struct extent_io_tree *tree;
2486 if (current->flags & PF_MEMALLOC) {
2487 redirty_page_for_writepage(wbc, page);
2491 tree = &BTRFS_I(page->mapping->host)->io_tree;
2492 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2495 static int btrfs_writepages(struct address_space *mapping,
2496 struct writeback_control *wbc)
2498 struct extent_io_tree *tree;
2499 tree = &BTRFS_I(mapping->host)->io_tree;
2500 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2504 btrfs_readpages(struct file *file, struct address_space *mapping,
2505 struct list_head *pages, unsigned nr_pages)
2507 struct extent_io_tree *tree;
2508 tree = &BTRFS_I(mapping->host)->io_tree;
2509 return extent_readpages(tree, mapping, pages, nr_pages,
2513 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2515 struct extent_io_tree *tree;
2516 struct extent_map_tree *map;
2519 tree = &BTRFS_I(page->mapping->host)->io_tree;
2520 map = &BTRFS_I(page->mapping->host)->extent_tree;
2521 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2523 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2524 ClearPagePrivate(page);
2525 set_page_private(page, 0);
2526 page_cache_release(page);
2531 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2533 struct extent_io_tree *tree;
2535 tree = &BTRFS_I(page->mapping->host)->io_tree;
2536 extent_invalidatepage(tree, page, offset);
2537 btrfs_releasepage(page, GFP_NOFS);
2538 if (PagePrivate(page)) {
2539 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
2540 ClearPagePrivate(page);
2541 set_page_private(page, 0);
2542 page_cache_release(page);
2547 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2548 * called from a page fault handler when a page is first dirtied. Hence we must
2549 * be careful to check for EOF conditions here. We set the page up correctly
2550 * for a written page which means we get ENOSPC checking when writing into
2551 * holes and correct delalloc and unwritten extent mapping on filesystems that
2552 * support these features.
2554 * We are not allowed to take the i_mutex here so we have to play games to
2555 * protect against truncate races as the page could now be beyond EOF. Because
2556 * vmtruncate() writes the inode size before removing pages, once we have the
2557 * page lock we can determine safely if the page is beyond EOF. If it is not
2558 * beyond EOF, then the page is guaranteed safe against truncation until we
2561 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2563 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2564 struct btrfs_root *root = BTRFS_I(inode)->root;
2570 mutex_lock(&root->fs_info->fs_mutex);
2571 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2572 mutex_unlock(&root->fs_info->fs_mutex);
2579 wait_on_page_writeback(page);
2580 size = i_size_read(inode);
2581 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2583 if ((page->mapping != inode->i_mapping) ||
2584 (page_start > size)) {
2585 /* page got truncated out from underneath us */
2589 /* page is wholly or partially inside EOF */
2590 if (page_start + PAGE_CACHE_SIZE > size)
2591 end = size & ~PAGE_CACHE_MASK;
2593 end = PAGE_CACHE_SIZE;
2595 ret = btrfs_cow_one_page(inode, page, end);
2603 static void btrfs_truncate(struct inode *inode)
2605 struct btrfs_root *root = BTRFS_I(inode)->root;
2607 struct btrfs_trans_handle *trans;
2610 if (!S_ISREG(inode->i_mode))
2612 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2615 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2617 mutex_lock(&root->fs_info->fs_mutex);
2618 trans = btrfs_start_transaction(root, 1);
2619 btrfs_set_trans_block_group(trans, inode);
2621 /* FIXME, add redo link to tree so we don't leak on crash */
2622 ret = btrfs_truncate_in_trans(trans, root, inode,
2623 BTRFS_EXTENT_DATA_KEY);
2624 btrfs_update_inode(trans, root, inode);
2625 nr = trans->blocks_used;
2627 ret = btrfs_end_transaction(trans, root);
2629 mutex_unlock(&root->fs_info->fs_mutex);
2630 btrfs_btree_balance_dirty(root, nr);
2631 btrfs_throttle(root);
2634 static int noinline create_subvol(struct btrfs_root *root, char *name,
2637 struct btrfs_trans_handle *trans;
2638 struct btrfs_key key;
2639 struct btrfs_root_item root_item;
2640 struct btrfs_inode_item *inode_item;
2641 struct extent_buffer *leaf;
2642 struct btrfs_root *new_root = root;
2643 struct inode *inode;
2648 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2649 unsigned long nr = 1;
2651 mutex_lock(&root->fs_info->fs_mutex);
2652 ret = btrfs_check_free_space(root, 1, 0);
2656 trans = btrfs_start_transaction(root, 1);
2659 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2664 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2665 objectid, trans->transid, 0, 0,
2668 return PTR_ERR(leaf);
2670 btrfs_set_header_nritems(leaf, 0);
2671 btrfs_set_header_level(leaf, 0);
2672 btrfs_set_header_bytenr(leaf, leaf->start);
2673 btrfs_set_header_generation(leaf, trans->transid);
2674 btrfs_set_header_owner(leaf, objectid);
2676 write_extent_buffer(leaf, root->fs_info->fsid,
2677 (unsigned long)btrfs_header_fsid(leaf),
2679 btrfs_mark_buffer_dirty(leaf);
2681 inode_item = &root_item.inode;
2682 memset(inode_item, 0, sizeof(*inode_item));
2683 inode_item->generation = cpu_to_le64(1);
2684 inode_item->size = cpu_to_le64(3);
2685 inode_item->nlink = cpu_to_le32(1);
2686 inode_item->nblocks = cpu_to_le64(1);
2687 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2689 btrfs_set_root_bytenr(&root_item, leaf->start);
2690 btrfs_set_root_level(&root_item, 0);
2691 btrfs_set_root_refs(&root_item, 1);
2692 btrfs_set_root_used(&root_item, 0);
2694 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2695 root_item.drop_level = 0;
2697 free_extent_buffer(leaf);
2700 btrfs_set_root_dirid(&root_item, new_dirid);
2702 key.objectid = objectid;
2704 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2705 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2711 * insert the directory item
2713 key.offset = (u64)-1;
2714 dir = root->fs_info->sb->s_root->d_inode;
2715 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2716 name, namelen, dir->i_ino, &key,
2721 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2722 name, namelen, objectid,
2723 root->fs_info->sb->s_root->d_inode->i_ino);
2727 ret = btrfs_commit_transaction(trans, root);
2731 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2734 trans = btrfs_start_transaction(new_root, 1);
2737 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2739 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2742 inode->i_op = &btrfs_dir_inode_operations;
2743 inode->i_fop = &btrfs_dir_file_operations;
2744 new_root->inode = inode;
2746 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2750 ret = btrfs_update_inode(trans, new_root, inode);
2754 nr = trans->blocks_used;
2755 err = btrfs_commit_transaction(trans, new_root);
2759 mutex_unlock(&root->fs_info->fs_mutex);
2760 btrfs_btree_balance_dirty(root, nr);
2761 btrfs_throttle(root);
2765 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2767 struct btrfs_pending_snapshot *pending_snapshot;
2768 struct btrfs_trans_handle *trans;
2771 unsigned long nr = 0;
2773 if (!root->ref_cows)
2776 mutex_lock(&root->fs_info->fs_mutex);
2777 ret = btrfs_check_free_space(root, 1, 0);
2781 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2782 if (!pending_snapshot) {
2786 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2787 if (!pending_snapshot->name) {
2789 kfree(pending_snapshot);
2792 memcpy(pending_snapshot->name, name, namelen);
2793 pending_snapshot->name[namelen] = '\0';
2794 trans = btrfs_start_transaction(root, 1);
2796 pending_snapshot->root = root;
2797 list_add(&pending_snapshot->list,
2798 &trans->transaction->pending_snapshots);
2799 ret = btrfs_update_inode(trans, root, root->inode);
2800 err = btrfs_commit_transaction(trans, root);
2803 mutex_unlock(&root->fs_info->fs_mutex);
2804 btrfs_btree_balance_dirty(root, nr);
2805 btrfs_throttle(root);
2809 unsigned long btrfs_force_ra(struct address_space *mapping,
2810 struct file_ra_state *ra, struct file *file,
2811 pgoff_t offset, pgoff_t last_index)
2815 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2816 req_size = last_index - offset + 1;
2817 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2820 req_size = min(last_index - offset + 1, (pgoff_t)128);
2821 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2822 return offset + req_size;
2826 int btrfs_defrag_file(struct file *file) {
2827 struct inode *inode = fdentry(file)->d_inode;
2828 struct btrfs_root *root = BTRFS_I(inode)->root;
2829 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2831 unsigned long last_index;
2832 unsigned long ra_index = 0;
2838 mutex_lock(&root->fs_info->fs_mutex);
2839 ret = btrfs_check_free_space(root, inode->i_size, 0);
2840 mutex_unlock(&root->fs_info->fs_mutex);
2844 mutex_lock(&inode->i_mutex);
2845 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2846 for (i = 0; i <= last_index; i++) {
2847 if (i == ra_index) {
2848 ra_index = btrfs_force_ra(inode->i_mapping,
2850 file, ra_index, last_index);
2852 page = grab_cache_page(inode->i_mapping, i);
2855 if (!PageUptodate(page)) {
2856 btrfs_readpage(NULL, page);
2858 if (!PageUptodate(page)) {
2860 page_cache_release(page);
2864 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2865 page_end = page_start + PAGE_CACHE_SIZE - 1;
2867 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2868 set_extent_delalloc(io_tree, page_start,
2869 page_end, GFP_NOFS);
2871 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2872 set_page_dirty(page);
2874 page_cache_release(page);
2875 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2879 mutex_unlock(&inode->i_mutex);
2883 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2887 struct btrfs_ioctl_vol_args *vol_args;
2888 struct btrfs_trans_handle *trans;
2894 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2899 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2903 namelen = strlen(vol_args->name);
2904 if (namelen > BTRFS_VOL_NAME_MAX) {
2909 sizestr = vol_args->name;
2910 if (!strcmp(sizestr, "max"))
2911 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2913 if (sizestr[0] == '-') {
2916 } else if (sizestr[0] == '+') {
2920 new_size = btrfs_parse_size(sizestr);
2921 if (new_size == 0) {
2927 mutex_lock(&root->fs_info->fs_mutex);
2928 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2931 if (new_size > old_size) {
2935 new_size = old_size - new_size;
2936 } else if (mod > 0) {
2937 new_size = old_size + new_size;
2940 if (new_size < 256 * 1024 * 1024) {
2944 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2949 do_div(new_size, root->sectorsize);
2950 new_size *= root->sectorsize;
2952 printk("new size is %Lu\n", new_size);
2953 if (new_size > old_size) {
2954 trans = btrfs_start_transaction(root, 1);
2955 ret = btrfs_grow_extent_tree(trans, root, new_size);
2956 btrfs_commit_transaction(trans, root);
2958 ret = btrfs_shrink_extent_tree(root, new_size);
2962 mutex_unlock(&root->fs_info->fs_mutex);
2968 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2971 struct btrfs_ioctl_vol_args *vol_args;
2972 struct btrfs_dir_item *di;
2973 struct btrfs_path *path;
2978 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2983 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2988 namelen = strlen(vol_args->name);
2989 if (namelen > BTRFS_VOL_NAME_MAX) {
2993 if (strchr(vol_args->name, '/')) {
2998 path = btrfs_alloc_path();
3004 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3005 mutex_lock(&root->fs_info->fs_mutex);
3006 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3008 vol_args->name, namelen, 0);
3009 mutex_unlock(&root->fs_info->fs_mutex);
3010 btrfs_free_path(path);
3012 if (di && !IS_ERR(di)) {
3022 if (root == root->fs_info->tree_root)
3023 ret = create_subvol(root, vol_args->name, namelen);
3025 ret = create_snapshot(root, vol_args->name, namelen);
3031 static int btrfs_ioctl_defrag(struct file *file)
3033 struct inode *inode = fdentry(file)->d_inode;
3034 struct btrfs_root *root = BTRFS_I(inode)->root;
3036 switch (inode->i_mode & S_IFMT) {
3038 mutex_lock(&root->fs_info->fs_mutex);
3039 btrfs_defrag_root(root, 0);
3040 btrfs_defrag_root(root->fs_info->extent_root, 0);
3041 mutex_unlock(&root->fs_info->fs_mutex);
3044 btrfs_defrag_file(file);
3051 long btrfs_ioctl(struct file *file, unsigned int
3052 cmd, unsigned long arg)
3054 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3057 case BTRFS_IOC_SNAP_CREATE:
3058 return btrfs_ioctl_snap_create(root, (void __user *)arg);
3059 case BTRFS_IOC_DEFRAG:
3060 return btrfs_ioctl_defrag(file);
3061 case BTRFS_IOC_RESIZE:
3062 return btrfs_ioctl_resize(root, (void __user *)arg);
3069 * Called inside transaction, so use GFP_NOFS
3071 struct inode *btrfs_alloc_inode(struct super_block *sb)
3073 struct btrfs_inode *ei;
3075 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3079 ei->ordered_trans = 0;
3080 return &ei->vfs_inode;
3083 void btrfs_destroy_inode(struct inode *inode)
3085 WARN_ON(!list_empty(&inode->i_dentry));
3086 WARN_ON(inode->i_data.nrpages);
3088 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3089 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3092 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3093 static void init_once(struct kmem_cache * cachep, void *foo)
3095 static void init_once(void * foo, struct kmem_cache * cachep,
3096 unsigned long flags)
3099 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3101 inode_init_once(&ei->vfs_inode);
3104 void btrfs_destroy_cachep(void)
3106 if (btrfs_inode_cachep)
3107 kmem_cache_destroy(btrfs_inode_cachep);
3108 if (btrfs_trans_handle_cachep)
3109 kmem_cache_destroy(btrfs_trans_handle_cachep);
3110 if (btrfs_transaction_cachep)
3111 kmem_cache_destroy(btrfs_transaction_cachep);
3112 if (btrfs_bit_radix_cachep)
3113 kmem_cache_destroy(btrfs_bit_radix_cachep);
3114 if (btrfs_path_cachep)
3115 kmem_cache_destroy(btrfs_path_cachep);
3118 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3119 unsigned long extra_flags,
3120 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3121 void (*ctor)(struct kmem_cache *, void *)
3123 void (*ctor)(void *, struct kmem_cache *,
3128 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3129 SLAB_MEM_SPREAD | extra_flags), ctor
3130 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3136 int btrfs_init_cachep(void)
3138 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3139 sizeof(struct btrfs_inode),
3141 if (!btrfs_inode_cachep)
3143 btrfs_trans_handle_cachep =
3144 btrfs_cache_create("btrfs_trans_handle_cache",
3145 sizeof(struct btrfs_trans_handle),
3147 if (!btrfs_trans_handle_cachep)
3149 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3150 sizeof(struct btrfs_transaction),
3152 if (!btrfs_transaction_cachep)
3154 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3155 sizeof(struct btrfs_path),
3157 if (!btrfs_path_cachep)
3159 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3160 SLAB_DESTROY_BY_RCU, NULL);
3161 if (!btrfs_bit_radix_cachep)
3165 btrfs_destroy_cachep();
3169 static int btrfs_getattr(struct vfsmount *mnt,
3170 struct dentry *dentry, struct kstat *stat)
3172 struct inode *inode = dentry->d_inode;
3173 generic_fillattr(inode, stat);
3174 stat->blksize = PAGE_CACHE_SIZE;
3175 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3179 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3180 struct inode * new_dir,struct dentry *new_dentry)
3182 struct btrfs_trans_handle *trans;
3183 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3184 struct inode *new_inode = new_dentry->d_inode;
3185 struct inode *old_inode = old_dentry->d_inode;
3186 struct timespec ctime = CURRENT_TIME;
3187 struct btrfs_path *path;
3190 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3191 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3195 mutex_lock(&root->fs_info->fs_mutex);
3196 ret = btrfs_check_free_space(root, 1, 0);
3200 trans = btrfs_start_transaction(root, 1);
3202 btrfs_set_trans_block_group(trans, new_dir);
3203 path = btrfs_alloc_path();
3209 old_dentry->d_inode->i_nlink++;
3210 old_dir->i_ctime = old_dir->i_mtime = ctime;
3211 new_dir->i_ctime = new_dir->i_mtime = ctime;
3212 old_inode->i_ctime = ctime;
3214 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3219 new_inode->i_ctime = CURRENT_TIME;
3220 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3224 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
3229 btrfs_free_path(path);
3230 btrfs_end_transaction(trans, root);
3232 mutex_unlock(&root->fs_info->fs_mutex);
3236 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3237 const char *symname)
3239 struct btrfs_trans_handle *trans;
3240 struct btrfs_root *root = BTRFS_I(dir)->root;
3241 struct btrfs_path *path;
3242 struct btrfs_key key;
3243 struct inode *inode = NULL;
3250 struct btrfs_file_extent_item *ei;
3251 struct extent_buffer *leaf;
3252 unsigned long nr = 0;
3254 name_len = strlen(symname) + 1;
3255 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3256 return -ENAMETOOLONG;
3258 mutex_lock(&root->fs_info->fs_mutex);
3259 err = btrfs_check_free_space(root, 1, 0);
3263 trans = btrfs_start_transaction(root, 1);
3264 btrfs_set_trans_block_group(trans, dir);
3266 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3272 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3274 dentry->d_parent->d_inode->i_ino, objectid,
3275 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3276 err = PTR_ERR(inode);
3280 btrfs_set_trans_block_group(trans, inode);
3281 err = btrfs_add_nondir(trans, dentry, inode, 0);
3285 inode->i_mapping->a_ops = &btrfs_aops;
3286 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3287 inode->i_fop = &btrfs_file_operations;
3288 inode->i_op = &btrfs_file_inode_operations;
3289 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3290 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3291 inode->i_mapping, GFP_NOFS);
3292 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3293 inode->i_mapping, GFP_NOFS);
3294 BTRFS_I(inode)->delalloc_bytes = 0;
3295 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3297 dir->i_sb->s_dirt = 1;
3298 btrfs_update_inode_block_group(trans, inode);
3299 btrfs_update_inode_block_group(trans, dir);
3303 path = btrfs_alloc_path();
3305 key.objectid = inode->i_ino;
3307 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3308 datasize = btrfs_file_extent_calc_inline_size(name_len);
3309 err = btrfs_insert_empty_item(trans, root, path, &key,
3315 leaf = path->nodes[0];
3316 ei = btrfs_item_ptr(leaf, path->slots[0],
3317 struct btrfs_file_extent_item);
3318 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3319 btrfs_set_file_extent_type(leaf, ei,
3320 BTRFS_FILE_EXTENT_INLINE);
3321 ptr = btrfs_file_extent_inline_start(ei);
3322 write_extent_buffer(leaf, symname, ptr, name_len);
3323 btrfs_mark_buffer_dirty(leaf);
3324 btrfs_free_path(path);
3326 inode->i_op = &btrfs_symlink_inode_operations;
3327 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3328 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3329 inode->i_size = name_len - 1;
3330 err = btrfs_update_inode(trans, root, inode);
3335 nr = trans->blocks_used;
3336 btrfs_end_transaction(trans, root);
3338 mutex_unlock(&root->fs_info->fs_mutex);
3340 inode_dec_link_count(inode);
3343 btrfs_btree_balance_dirty(root, nr);
3344 btrfs_throttle(root);
3348 static int btrfs_permission(struct inode *inode, int mask,
3349 struct nameidata *nd)
3351 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3353 return generic_permission(inode, mask, NULL);
3356 static struct inode_operations btrfs_dir_inode_operations = {
3357 .lookup = btrfs_lookup,
3358 .create = btrfs_create,
3359 .unlink = btrfs_unlink,
3361 .mkdir = btrfs_mkdir,
3362 .rmdir = btrfs_rmdir,
3363 .rename = btrfs_rename,
3364 .symlink = btrfs_symlink,
3365 .setattr = btrfs_setattr,
3366 .mknod = btrfs_mknod,
3367 .setxattr = generic_setxattr,
3368 .getxattr = generic_getxattr,
3369 .listxattr = btrfs_listxattr,
3370 .removexattr = generic_removexattr,
3371 .permission = btrfs_permission,
3373 static struct inode_operations btrfs_dir_ro_inode_operations = {
3374 .lookup = btrfs_lookup,
3375 .permission = btrfs_permission,
3377 static struct file_operations btrfs_dir_file_operations = {
3378 .llseek = generic_file_llseek,
3379 .read = generic_read_dir,
3380 .readdir = btrfs_readdir,
3381 .unlocked_ioctl = btrfs_ioctl,
3382 #ifdef CONFIG_COMPAT
3383 .compat_ioctl = btrfs_ioctl,
3387 static struct extent_io_ops btrfs_extent_io_ops = {
3388 .fill_delalloc = run_delalloc_range,
3389 .submit_bio_hook = btrfs_submit_bio_hook,
3390 .merge_bio_hook = btrfs_merge_bio_hook,
3391 .readpage_io_hook = btrfs_readpage_io_hook,
3392 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3393 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
3394 .set_bit_hook = btrfs_set_bit_hook,
3395 .clear_bit_hook = btrfs_clear_bit_hook,
3398 static struct address_space_operations btrfs_aops = {
3399 .readpage = btrfs_readpage,
3400 .writepage = btrfs_writepage,
3401 .writepages = btrfs_writepages,
3402 .readpages = btrfs_readpages,
3403 .sync_page = block_sync_page,
3405 .direct_IO = btrfs_direct_IO,
3406 .invalidatepage = btrfs_invalidatepage,
3407 .releasepage = btrfs_releasepage,
3408 .set_page_dirty = __set_page_dirty_nobuffers,
3411 static struct address_space_operations btrfs_symlink_aops = {
3412 .readpage = btrfs_readpage,
3413 .writepage = btrfs_writepage,
3414 .invalidatepage = btrfs_invalidatepage,
3415 .releasepage = btrfs_releasepage,
3418 static struct inode_operations btrfs_file_inode_operations = {
3419 .truncate = btrfs_truncate,
3420 .getattr = btrfs_getattr,
3421 .setattr = btrfs_setattr,
3422 .setxattr = generic_setxattr,
3423 .getxattr = generic_getxattr,
3424 .listxattr = btrfs_listxattr,
3425 .removexattr = generic_removexattr,
3426 .permission = btrfs_permission,
3428 static struct inode_operations btrfs_special_inode_operations = {
3429 .getattr = btrfs_getattr,
3430 .setattr = btrfs_setattr,
3431 .permission = btrfs_permission,
3433 static struct inode_operations btrfs_symlink_inode_operations = {
3434 .readlink = generic_readlink,
3435 .follow_link = page_follow_link_light,
3436 .put_link = page_put_link,
3437 .permission = btrfs_permission,