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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/xattr.h>
38 #include <linux/posix_acl.h>
39 #include <linux/falloc.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "ref-cache.h"
52 #include "compression.h"
55 struct btrfs_iget_args {
57 struct btrfs_root *root;
60 static struct inode_operations btrfs_dir_inode_operations;
61 static struct inode_operations btrfs_symlink_inode_operations;
62 static struct inode_operations btrfs_dir_ro_inode_operations;
63 static struct inode_operations btrfs_special_inode_operations;
64 static struct inode_operations btrfs_file_inode_operations;
65 static struct address_space_operations btrfs_aops;
66 static struct address_space_operations btrfs_symlink_aops;
67 static struct file_operations btrfs_dir_file_operations;
68 static struct extent_io_ops btrfs_extent_io_ops;
70 static struct kmem_cache *btrfs_inode_cachep;
71 struct kmem_cache *btrfs_trans_handle_cachep;
72 struct kmem_cache *btrfs_transaction_cachep;
73 struct kmem_cache *btrfs_bit_radix_cachep;
74 struct kmem_cache *btrfs_path_cachep;
77 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
87 static void btrfs_truncate(struct inode *inode);
88 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
89 static noinline int cow_file_range(struct inode *inode,
90 struct page *locked_page,
91 u64 start, u64 end, int *page_started,
92 unsigned long *nr_written, int unlock);
94 static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
98 err = btrfs_init_acl(inode, dir);
100 err = btrfs_xattr_security_init(inode, dir);
105 * a very lame attempt at stopping writes when the FS is 85% full. There
106 * are countless ways this is incorrect, but it is better than nothing.
108 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
116 spin_lock(&root->fs_info->delalloc_lock);
117 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
118 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
126 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
128 spin_unlock(&root->fs_info->delalloc_lock);
133 * this does all the hard work for inserting an inline extent into
134 * the btree. The caller should have done a btrfs_drop_extents so that
135 * no overlapping inline items exist in the btree
137 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root, struct inode *inode,
139 u64 start, size_t size, size_t compressed_size,
140 struct page **compressed_pages)
142 struct btrfs_key key;
143 struct btrfs_path *path;
144 struct extent_buffer *leaf;
145 struct page *page = NULL;
148 struct btrfs_file_extent_item *ei;
151 size_t cur_size = size;
153 unsigned long offset;
154 int use_compress = 0;
156 if (compressed_size && compressed_pages) {
158 cur_size = compressed_size;
161 path = btrfs_alloc_path();
165 btrfs_set_trans_block_group(trans, inode);
167 key.objectid = inode->i_ino;
169 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
170 datasize = btrfs_file_extent_calc_inline_size(cur_size);
172 inode_add_bytes(inode, size);
173 ret = btrfs_insert_empty_item(trans, root, path, &key,
180 leaf = path->nodes[0];
181 ei = btrfs_item_ptr(leaf, path->slots[0],
182 struct btrfs_file_extent_item);
183 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
184 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
185 btrfs_set_file_extent_encryption(leaf, ei, 0);
186 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
187 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
188 ptr = btrfs_file_extent_inline_start(ei);
193 while (compressed_size > 0) {
194 cpage = compressed_pages[i];
195 cur_size = min_t(unsigned long, compressed_size,
199 write_extent_buffer(leaf, kaddr, ptr, cur_size);
204 compressed_size -= cur_size;
206 btrfs_set_file_extent_compression(leaf, ei,
207 BTRFS_COMPRESS_ZLIB);
209 page = find_get_page(inode->i_mapping,
210 start >> PAGE_CACHE_SHIFT);
211 btrfs_set_file_extent_compression(leaf, ei, 0);
212 kaddr = kmap_atomic(page, KM_USER0);
213 offset = start & (PAGE_CACHE_SIZE - 1);
214 write_extent_buffer(leaf, kaddr + offset, ptr, size);
215 kunmap_atomic(kaddr, KM_USER0);
216 page_cache_release(page);
218 btrfs_mark_buffer_dirty(leaf);
219 btrfs_free_path(path);
221 BTRFS_I(inode)->disk_i_size = inode->i_size;
222 btrfs_update_inode(trans, root, inode);
225 btrfs_free_path(path);
231 * conditionally insert an inline extent into the file. This
232 * does the checks required to make sure the data is small enough
233 * to fit as an inline extent.
235 static int cow_file_range_inline(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct inode *inode, u64 start, u64 end,
238 size_t compressed_size,
239 struct page **compressed_pages)
241 u64 isize = i_size_read(inode);
242 u64 actual_end = min(end + 1, isize);
243 u64 inline_len = actual_end - start;
244 u64 aligned_end = (end + root->sectorsize - 1) &
245 ~((u64)root->sectorsize - 1);
247 u64 data_len = inline_len;
251 data_len = compressed_size;
254 actual_end >= PAGE_CACHE_SIZE ||
255 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
257 (actual_end & (root->sectorsize - 1)) == 0) ||
259 data_len > root->fs_info->max_inline) {
263 ret = btrfs_drop_extents(trans, root, inode, start,
264 aligned_end, start, &hint_byte);
267 if (isize > actual_end)
268 inline_len = min_t(u64, isize, actual_end);
269 ret = insert_inline_extent(trans, root, inode, start,
270 inline_len, compressed_size,
273 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
277 struct async_extent {
282 unsigned long nr_pages;
283 struct list_head list;
288 struct btrfs_root *root;
289 struct page *locked_page;
292 struct list_head extents;
293 struct btrfs_work work;
296 static noinline int add_async_extent(struct async_cow *cow,
297 u64 start, u64 ram_size,
300 unsigned long nr_pages)
302 struct async_extent *async_extent;
304 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
305 async_extent->start = start;
306 async_extent->ram_size = ram_size;
307 async_extent->compressed_size = compressed_size;
308 async_extent->pages = pages;
309 async_extent->nr_pages = nr_pages;
310 list_add_tail(&async_extent->list, &cow->extents);
315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
328 * are written in the same order that pdflush sent them down.
330 static noinline int compress_file_range(struct inode *inode,
331 struct page *locked_page,
333 struct async_cow *async_cow,
336 struct btrfs_root *root = BTRFS_I(inode)->root;
337 struct btrfs_trans_handle *trans;
341 u64 blocksize = root->sectorsize;
343 u64 isize = i_size_read(inode);
345 struct page **pages = NULL;
346 unsigned long nr_pages;
347 unsigned long nr_pages_ret = 0;
348 unsigned long total_compressed = 0;
349 unsigned long total_in = 0;
350 unsigned long max_compressed = 128 * 1024;
351 unsigned long max_uncompressed = 128 * 1024;
357 actual_end = min_t(u64, isize, end + 1);
360 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
361 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
363 total_compressed = actual_end - start;
365 /* we want to make sure that amount of ram required to uncompress
366 * an extent is reasonable, so we limit the total size in ram
367 * of a compressed extent to 128k. This is a crucial number
368 * because it also controls how easily we can spread reads across
369 * cpus for decompression.
371 * We also want to make sure the amount of IO required to do
372 * a random read is reasonably small, so we limit the size of
373 * a compressed extent to 128k.
375 total_compressed = min(total_compressed, max_uncompressed);
376 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
377 num_bytes = max(blocksize, num_bytes);
378 disk_num_bytes = num_bytes;
383 * we do compression for mount -o compress and when the
384 * inode has not been flagged as nocompress. This flag can
385 * change at any time if we discover bad compression ratios.
387 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
388 btrfs_test_opt(root, COMPRESS)) {
390 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
392 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
393 total_compressed, pages,
394 nr_pages, &nr_pages_ret,
400 unsigned long offset = total_compressed &
401 (PAGE_CACHE_SIZE - 1);
402 struct page *page = pages[nr_pages_ret - 1];
405 /* zero the tail end of the last page, we might be
406 * sending it down to disk
409 kaddr = kmap_atomic(page, KM_USER0);
410 memset(kaddr + offset, 0,
411 PAGE_CACHE_SIZE - offset);
412 kunmap_atomic(kaddr, KM_USER0);
418 trans = btrfs_join_transaction(root, 1);
420 btrfs_set_trans_block_group(trans, inode);
422 /* lets try to make an inline extent */
423 if (ret || total_in < (actual_end - start)) {
424 /* we didn't compress the entire range, try
425 * to make an uncompressed inline extent.
427 ret = cow_file_range_inline(trans, root, inode,
428 start, end, 0, NULL);
430 /* try making a compressed inline extent */
431 ret = cow_file_range_inline(trans, root, inode,
433 total_compressed, pages);
435 btrfs_end_transaction(trans, root);
438 * inline extent creation worked, we don't need
439 * to create any more async work items. Unlock
440 * and free up our temp pages.
442 extent_clear_unlock_delalloc(inode,
443 &BTRFS_I(inode)->io_tree,
444 start, end, NULL, 1, 0,
453 * we aren't doing an inline extent round the compressed size
454 * up to a block size boundary so the allocator does sane
457 total_compressed = (total_compressed + blocksize - 1) &
461 * one last check to make sure the compression is really a
462 * win, compare the page count read with the blocks on disk
464 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
465 ~(PAGE_CACHE_SIZE - 1);
466 if (total_compressed >= total_in) {
469 disk_num_bytes = total_compressed;
470 num_bytes = total_in;
473 if (!will_compress && pages) {
475 * the compression code ran but failed to make things smaller,
476 * free any pages it allocated and our page pointer array
478 for (i = 0; i < nr_pages_ret; i++) {
479 WARN_ON(pages[i]->mapping);
480 page_cache_release(pages[i]);
484 total_compressed = 0;
487 /* flag the file so we don't compress in the future */
488 btrfs_set_flag(inode, NOCOMPRESS);
493 /* the async work queues will take care of doing actual
494 * allocation on disk for these compressed pages,
495 * and will submit them to the elevator.
497 add_async_extent(async_cow, start, num_bytes,
498 total_compressed, pages, nr_pages_ret);
500 if (start + num_bytes < end && start + num_bytes < actual_end) {
508 * No compression, but we still need to write the pages in
509 * the file we've been given so far. redirty the locked
510 * page if it corresponds to our extent and set things up
511 * for the async work queue to run cow_file_range to do
512 * the normal delalloc dance
514 if (page_offset(locked_page) >= start &&
515 page_offset(locked_page) <= end) {
516 __set_page_dirty_nobuffers(locked_page);
517 /* unlocked later on in the async handlers */
519 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
527 for (i = 0; i < nr_pages_ret; i++) {
528 WARN_ON(pages[i]->mapping);
529 page_cache_release(pages[i]);
537 * phase two of compressed writeback. This is the ordered portion
538 * of the code, which only gets called in the order the work was
539 * queued. We walk all the async extents created by compress_file_range
540 * and send them down to the disk.
542 static noinline int submit_compressed_extents(struct inode *inode,
543 struct async_cow *async_cow)
545 struct async_extent *async_extent;
547 struct btrfs_trans_handle *trans;
548 struct btrfs_key ins;
549 struct extent_map *em;
550 struct btrfs_root *root = BTRFS_I(inode)->root;
551 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
552 struct extent_io_tree *io_tree;
555 if (list_empty(&async_cow->extents))
558 trans = btrfs_join_transaction(root, 1);
560 while (!list_empty(&async_cow->extents)) {
561 async_extent = list_entry(async_cow->extents.next,
562 struct async_extent, list);
563 list_del(&async_extent->list);
565 io_tree = &BTRFS_I(inode)->io_tree;
567 /* did the compression code fall back to uncompressed IO? */
568 if (!async_extent->pages) {
569 int page_started = 0;
570 unsigned long nr_written = 0;
572 lock_extent(io_tree, async_extent->start,
573 async_extent->start +
574 async_extent->ram_size - 1, GFP_NOFS);
576 /* allocate blocks */
577 cow_file_range(inode, async_cow->locked_page,
579 async_extent->start +
580 async_extent->ram_size - 1,
581 &page_started, &nr_written, 0);
584 * if page_started, cow_file_range inserted an
585 * inline extent and took care of all the unlocking
586 * and IO for us. Otherwise, we need to submit
587 * all those pages down to the drive.
590 extent_write_locked_range(io_tree,
591 inode, async_extent->start,
592 async_extent->start +
593 async_extent->ram_size - 1,
601 lock_extent(io_tree, async_extent->start,
602 async_extent->start + async_extent->ram_size - 1,
605 * here we're doing allocation and writeback of the
608 btrfs_drop_extent_cache(inode, async_extent->start,
609 async_extent->start +
610 async_extent->ram_size - 1, 0);
612 ret = btrfs_reserve_extent(trans, root,
613 async_extent->compressed_size,
614 async_extent->compressed_size,
618 em = alloc_extent_map(GFP_NOFS);
619 em->start = async_extent->start;
620 em->len = async_extent->ram_size;
621 em->orig_start = em->start;
623 em->block_start = ins.objectid;
624 em->block_len = ins.offset;
625 em->bdev = root->fs_info->fs_devices->latest_bdev;
626 set_bit(EXTENT_FLAG_PINNED, &em->flags);
627 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
630 spin_lock(&em_tree->lock);
631 ret = add_extent_mapping(em_tree, em);
632 spin_unlock(&em_tree->lock);
633 if (ret != -EEXIST) {
637 btrfs_drop_extent_cache(inode, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, 0);
642 ret = btrfs_add_ordered_extent(inode, async_extent->start,
644 async_extent->ram_size,
646 BTRFS_ORDERED_COMPRESSED);
649 btrfs_end_transaction(trans, root);
652 * clear dirty, set writeback and unlock the pages.
654 extent_clear_unlock_delalloc(inode,
655 &BTRFS_I(inode)->io_tree,
657 async_extent->start +
658 async_extent->ram_size - 1,
659 NULL, 1, 1, 0, 1, 1, 0);
661 ret = btrfs_submit_compressed_write(inode,
663 async_extent->ram_size,
665 ins.offset, async_extent->pages,
666 async_extent->nr_pages);
669 trans = btrfs_join_transaction(root, 1);
670 alloc_hint = ins.objectid + ins.offset;
675 btrfs_end_transaction(trans, root);
680 * when extent_io.c finds a delayed allocation range in the file,
681 * the call backs end up in this code. The basic idea is to
682 * allocate extents on disk for the range, and create ordered data structs
683 * in ram to track those extents.
685 * locked_page is the page that writepage had locked already. We use
686 * it to make sure we don't do extra locks or unlocks.
688 * *page_started is set to one if we unlock locked_page and do everything
689 * required to start IO on it. It may be clean and already done with
692 static noinline int cow_file_range(struct inode *inode,
693 struct page *locked_page,
694 u64 start, u64 end, int *page_started,
695 unsigned long *nr_written,
698 struct btrfs_root *root = BTRFS_I(inode)->root;
699 struct btrfs_trans_handle *trans;
702 unsigned long ram_size;
705 u64 blocksize = root->sectorsize;
707 u64 isize = i_size_read(inode);
708 struct btrfs_key ins;
709 struct extent_map *em;
710 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
713 trans = btrfs_join_transaction(root, 1);
715 btrfs_set_trans_block_group(trans, inode);
717 actual_end = min_t(u64, isize, end + 1);
719 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
720 num_bytes = max(blocksize, num_bytes);
721 disk_num_bytes = num_bytes;
725 /* lets try to make an inline extent */
726 ret = cow_file_range_inline(trans, root, inode,
727 start, end, 0, NULL);
729 extent_clear_unlock_delalloc(inode,
730 &BTRFS_I(inode)->io_tree,
731 start, end, NULL, 1, 1,
733 *nr_written = *nr_written +
734 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
741 BUG_ON(disk_num_bytes >
742 btrfs_super_total_bytes(&root->fs_info->super_copy));
744 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
746 while (disk_num_bytes > 0) {
747 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
748 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
749 root->sectorsize, 0, alloc_hint,
753 em = alloc_extent_map(GFP_NOFS);
755 em->orig_start = em->start;
757 ram_size = ins.offset;
758 em->len = ins.offset;
760 em->block_start = ins.objectid;
761 em->block_len = ins.offset;
762 em->bdev = root->fs_info->fs_devices->latest_bdev;
763 set_bit(EXTENT_FLAG_PINNED, &em->flags);
766 spin_lock(&em_tree->lock);
767 ret = add_extent_mapping(em_tree, em);
768 spin_unlock(&em_tree->lock);
769 if (ret != -EEXIST) {
773 btrfs_drop_extent_cache(inode, start,
774 start + ram_size - 1, 0);
777 cur_alloc_size = ins.offset;
778 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
779 ram_size, cur_alloc_size, 0);
782 if (root->root_key.objectid ==
783 BTRFS_DATA_RELOC_TREE_OBJECTID) {
784 ret = btrfs_reloc_clone_csums(inode, start,
789 if (disk_num_bytes < cur_alloc_size)
792 /* we're not doing compressed IO, don't unlock the first
793 * page (which the caller expects to stay locked), don't
794 * clear any dirty bits and don't set any writeback bits
796 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
797 start, start + ram_size - 1,
798 locked_page, unlock, 1,
800 disk_num_bytes -= cur_alloc_size;
801 num_bytes -= cur_alloc_size;
802 alloc_hint = ins.objectid + ins.offset;
803 start += cur_alloc_size;
807 btrfs_end_transaction(trans, root);
813 * work queue call back to started compression on a file and pages
815 static noinline void async_cow_start(struct btrfs_work *work)
817 struct async_cow *async_cow;
819 async_cow = container_of(work, struct async_cow, work);
821 compress_file_range(async_cow->inode, async_cow->locked_page,
822 async_cow->start, async_cow->end, async_cow,
825 async_cow->inode = NULL;
829 * work queue call back to submit previously compressed pages
831 static noinline void async_cow_submit(struct btrfs_work *work)
833 struct async_cow *async_cow;
834 struct btrfs_root *root;
835 unsigned long nr_pages;
837 async_cow = container_of(work, struct async_cow, work);
839 root = async_cow->root;
840 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
843 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
845 if (atomic_read(&root->fs_info->async_delalloc_pages) <
847 waitqueue_active(&root->fs_info->async_submit_wait))
848 wake_up(&root->fs_info->async_submit_wait);
850 if (async_cow->inode)
851 submit_compressed_extents(async_cow->inode, async_cow);
854 static noinline void async_cow_free(struct btrfs_work *work)
856 struct async_cow *async_cow;
857 async_cow = container_of(work, struct async_cow, work);
861 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
862 u64 start, u64 end, int *page_started,
863 unsigned long *nr_written)
865 struct async_cow *async_cow;
866 struct btrfs_root *root = BTRFS_I(inode)->root;
867 unsigned long nr_pages;
869 int limit = 10 * 1024 * 1042;
871 if (!btrfs_test_opt(root, COMPRESS)) {
872 return cow_file_range(inode, locked_page, start, end,
873 page_started, nr_written, 1);
876 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
877 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
878 while (start < end) {
879 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
880 async_cow->inode = inode;
881 async_cow->root = root;
882 async_cow->locked_page = locked_page;
883 async_cow->start = start;
885 if (btrfs_test_flag(inode, NOCOMPRESS))
888 cur_end = min(end, start + 512 * 1024 - 1);
890 async_cow->end = cur_end;
891 INIT_LIST_HEAD(&async_cow->extents);
893 async_cow->work.func = async_cow_start;
894 async_cow->work.ordered_func = async_cow_submit;
895 async_cow->work.ordered_free = async_cow_free;
896 async_cow->work.flags = 0;
898 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
900 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
902 btrfs_queue_worker(&root->fs_info->delalloc_workers,
905 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
906 wait_event(root->fs_info->async_submit_wait,
907 (atomic_read(&root->fs_info->async_delalloc_pages) <
911 while (atomic_read(&root->fs_info->async_submit_draining) &&
912 atomic_read(&root->fs_info->async_delalloc_pages)) {
913 wait_event(root->fs_info->async_submit_wait,
914 (atomic_read(&root->fs_info->async_delalloc_pages) ==
918 *nr_written += nr_pages;
925 static noinline int csum_exist_in_range(struct btrfs_root *root,
926 u64 bytenr, u64 num_bytes)
929 struct btrfs_ordered_sum *sums;
932 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
933 bytenr + num_bytes - 1, &list);
934 if (ret == 0 && list_empty(&list))
937 while (!list_empty(&list)) {
938 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
939 list_del(&sums->list);
946 * when nowcow writeback call back. This checks for snapshots or COW copies
947 * of the extents that exist in the file, and COWs the file as required.
949 * If no cow copies or snapshots exist, we write directly to the existing
952 static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
953 u64 start, u64 end, int *page_started, int force,
954 unsigned long *nr_written)
956 struct btrfs_root *root = BTRFS_I(inode)->root;
957 struct btrfs_trans_handle *trans;
958 struct extent_buffer *leaf;
959 struct btrfs_path *path;
960 struct btrfs_file_extent_item *fi;
961 struct btrfs_key found_key;
973 path = btrfs_alloc_path();
975 trans = btrfs_join_transaction(root, 1);
981 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
984 if (ret > 0 && path->slots[0] > 0 && check_prev) {
985 leaf = path->nodes[0];
986 btrfs_item_key_to_cpu(leaf, &found_key,
988 if (found_key.objectid == inode->i_ino &&
989 found_key.type == BTRFS_EXTENT_DATA_KEY)
994 leaf = path->nodes[0];
995 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
996 ret = btrfs_next_leaf(root, path);
1001 leaf = path->nodes[0];
1007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1009 if (found_key.objectid > inode->i_ino ||
1010 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1011 found_key.offset > end)
1014 if (found_key.offset > cur_offset) {
1015 extent_end = found_key.offset;
1019 fi = btrfs_item_ptr(leaf, path->slots[0],
1020 struct btrfs_file_extent_item);
1021 extent_type = btrfs_file_extent_type(leaf, fi);
1023 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1024 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1025 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1026 extent_end = found_key.offset +
1027 btrfs_file_extent_num_bytes(leaf, fi);
1028 if (extent_end <= start) {
1032 if (disk_bytenr == 0)
1034 if (btrfs_file_extent_compression(leaf, fi) ||
1035 btrfs_file_extent_encryption(leaf, fi) ||
1036 btrfs_file_extent_other_encoding(leaf, fi))
1038 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1040 if (btrfs_extent_readonly(root, disk_bytenr))
1042 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1045 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
1046 disk_bytenr += cur_offset - found_key.offset;
1047 num_bytes = min(end + 1, extent_end) - cur_offset;
1049 * force cow if csum exists in the range.
1050 * this ensure that csum for a given extent are
1051 * either valid or do not exist.
1053 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1056 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1057 extent_end = found_key.offset +
1058 btrfs_file_extent_inline_len(leaf, fi);
1059 extent_end = ALIGN(extent_end, root->sectorsize);
1064 if (extent_end <= start) {
1069 if (cow_start == (u64)-1)
1070 cow_start = cur_offset;
1071 cur_offset = extent_end;
1072 if (cur_offset > end)
1078 btrfs_release_path(root, path);
1079 if (cow_start != (u64)-1) {
1080 ret = cow_file_range(inode, locked_page, cow_start,
1081 found_key.offset - 1, page_started,
1084 cow_start = (u64)-1;
1087 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1088 struct extent_map *em;
1089 struct extent_map_tree *em_tree;
1090 em_tree = &BTRFS_I(inode)->extent_tree;
1091 em = alloc_extent_map(GFP_NOFS);
1092 em->start = cur_offset;
1093 em->orig_start = em->start;
1094 em->len = num_bytes;
1095 em->block_len = num_bytes;
1096 em->block_start = disk_bytenr;
1097 em->bdev = root->fs_info->fs_devices->latest_bdev;
1098 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1100 spin_lock(&em_tree->lock);
1101 ret = add_extent_mapping(em_tree, em);
1102 spin_unlock(&em_tree->lock);
1103 if (ret != -EEXIST) {
1104 free_extent_map(em);
1107 btrfs_drop_extent_cache(inode, em->start,
1108 em->start + em->len - 1, 0);
1110 type = BTRFS_ORDERED_PREALLOC;
1112 type = BTRFS_ORDERED_NOCOW;
1115 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1116 num_bytes, num_bytes, type);
1119 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1120 cur_offset, cur_offset + num_bytes - 1,
1121 locked_page, 1, 1, 1, 0, 0, 0);
1122 cur_offset = extent_end;
1123 if (cur_offset > end)
1126 btrfs_release_path(root, path);
1128 if (cur_offset <= end && cow_start == (u64)-1)
1129 cow_start = cur_offset;
1130 if (cow_start != (u64)-1) {
1131 ret = cow_file_range(inode, locked_page, cow_start, end,
1132 page_started, nr_written, 1);
1136 ret = btrfs_end_transaction(trans, root);
1138 btrfs_free_path(path);
1143 * extent_io.c call back to do delayed allocation processing
1145 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1146 u64 start, u64 end, int *page_started,
1147 unsigned long *nr_written)
1151 if (btrfs_test_flag(inode, NODATACOW))
1152 ret = run_delalloc_nocow(inode, locked_page, start, end,
1153 page_started, 1, nr_written);
1154 else if (btrfs_test_flag(inode, PREALLOC))
1155 ret = run_delalloc_nocow(inode, locked_page, start, end,
1156 page_started, 0, nr_written);
1158 ret = cow_file_range_async(inode, locked_page, start, end,
1159 page_started, nr_written);
1165 * extent_io.c set_bit_hook, used to track delayed allocation
1166 * bytes in this file, and to maintain the list of inodes that
1167 * have pending delalloc work to be done.
1169 static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1170 unsigned long old, unsigned long bits)
1173 * set_bit and clear bit hooks normally require _irqsave/restore
1174 * but in this case, we are only testeing for the DELALLOC
1175 * bit, which is only set or cleared with irqs on
1177 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1178 struct btrfs_root *root = BTRFS_I(inode)->root;
1179 spin_lock(&root->fs_info->delalloc_lock);
1180 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
1181 root->fs_info->delalloc_bytes += end - start + 1;
1182 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1183 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1184 &root->fs_info->delalloc_inodes);
1186 spin_unlock(&root->fs_info->delalloc_lock);
1192 * extent_io.c clear_bit_hook, see set_bit_hook for why
1194 static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1195 unsigned long old, unsigned long bits)
1198 * set_bit and clear bit hooks normally require _irqsave/restore
1199 * but in this case, we are only testeing for the DELALLOC
1200 * bit, which is only set or cleared with irqs on
1202 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1203 struct btrfs_root *root = BTRFS_I(inode)->root;
1205 spin_lock(&root->fs_info->delalloc_lock);
1206 if (end - start + 1 > root->fs_info->delalloc_bytes) {
1207 printk(KERN_INFO "btrfs warning: delalloc account "
1209 (unsigned long long)end - start + 1,
1210 (unsigned long long)
1211 root->fs_info->delalloc_bytes);
1212 root->fs_info->delalloc_bytes = 0;
1213 BTRFS_I(inode)->delalloc_bytes = 0;
1215 root->fs_info->delalloc_bytes -= end - start + 1;
1216 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
1218 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1219 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1220 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1222 spin_unlock(&root->fs_info->delalloc_lock);
1228 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1229 * we don't create bios that span stripes or chunks
1231 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1232 size_t size, struct bio *bio,
1233 unsigned long bio_flags)
1235 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1236 struct btrfs_mapping_tree *map_tree;
1237 u64 logical = (u64)bio->bi_sector << 9;
1242 if (bio_flags & EXTENT_BIO_COMPRESSED)
1245 length = bio->bi_size;
1246 map_tree = &root->fs_info->mapping_tree;
1247 map_length = length;
1248 ret = btrfs_map_block(map_tree, READ, logical,
1249 &map_length, NULL, 0);
1251 if (map_length < length + size)
1257 * in order to insert checksums into the metadata in large chunks,
1258 * we wait until bio submission time. All the pages in the bio are
1259 * checksummed and sums are attached onto the ordered extent record.
1261 * At IO completion time the cums attached on the ordered extent record
1262 * are inserted into the btree
1264 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1265 struct bio *bio, int mirror_num,
1266 unsigned long bio_flags)
1268 struct btrfs_root *root = BTRFS_I(inode)->root;
1271 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1277 * in order to insert checksums into the metadata in large chunks,
1278 * we wait until bio submission time. All the pages in the bio are
1279 * checksummed and sums are attached onto the ordered extent record.
1281 * At IO completion time the cums attached on the ordered extent record
1282 * are inserted into the btree
1284 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1285 int mirror_num, unsigned long bio_flags)
1287 struct btrfs_root *root = BTRFS_I(inode)->root;
1288 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1292 * extent_io.c submission hook. This does the right thing for csum calculation
1293 * on write, or reading the csums from the tree before a read
1295 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1296 int mirror_num, unsigned long bio_flags)
1298 struct btrfs_root *root = BTRFS_I(inode)->root;
1302 skip_sum = btrfs_test_flag(inode, NODATASUM);
1304 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1307 if (!(rw & (1 << BIO_RW))) {
1308 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1309 return btrfs_submit_compressed_read(inode, bio,
1310 mirror_num, bio_flags);
1311 } else if (!skip_sum)
1312 btrfs_lookup_bio_sums(root, inode, bio, NULL);
1314 } else if (!skip_sum) {
1315 /* csum items have already been cloned */
1316 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1318 /* we're doing a write, do the async checksumming */
1319 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1320 inode, rw, bio, mirror_num,
1321 bio_flags, __btrfs_submit_bio_start,
1322 __btrfs_submit_bio_done);
1326 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1330 * given a list of ordered sums record them in the inode. This happens
1331 * at IO completion time based on sums calculated at bio submission time.
1333 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1334 struct inode *inode, u64 file_offset,
1335 struct list_head *list)
1337 struct btrfs_ordered_sum *sum;
1339 btrfs_set_trans_block_group(trans, inode);
1341 list_for_each_entry(sum, list, list) {
1342 btrfs_csum_file_blocks(trans,
1343 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1348 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1350 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1352 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1356 /* see btrfs_writepage_start_hook for details on why this is required */
1357 struct btrfs_writepage_fixup {
1359 struct btrfs_work work;
1362 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1364 struct btrfs_writepage_fixup *fixup;
1365 struct btrfs_ordered_extent *ordered;
1367 struct inode *inode;
1371 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1375 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1376 ClearPageChecked(page);
1380 inode = page->mapping->host;
1381 page_start = page_offset(page);
1382 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1384 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1386 /* already ordered? We're done */
1387 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1388 EXTENT_ORDERED, 0)) {
1392 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1394 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1395 page_end, GFP_NOFS);
1397 btrfs_start_ordered_extent(inode, ordered, 1);
1401 btrfs_set_extent_delalloc(inode, page_start, page_end);
1402 ClearPageChecked(page);
1404 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1407 page_cache_release(page);
1411 * There are a few paths in the higher layers of the kernel that directly
1412 * set the page dirty bit without asking the filesystem if it is a
1413 * good idea. This causes problems because we want to make sure COW
1414 * properly happens and the data=ordered rules are followed.
1416 * In our case any range that doesn't have the ORDERED bit set
1417 * hasn't been properly setup for IO. We kick off an async process
1418 * to fix it up. The async helper will wait for ordered extents, set
1419 * the delalloc bit and make it safe to write the page.
1421 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1423 struct inode *inode = page->mapping->host;
1424 struct btrfs_writepage_fixup *fixup;
1425 struct btrfs_root *root = BTRFS_I(inode)->root;
1428 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1433 if (PageChecked(page))
1436 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1440 SetPageChecked(page);
1441 page_cache_get(page);
1442 fixup->work.func = btrfs_writepage_fixup_worker;
1444 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1448 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1449 struct inode *inode, u64 file_pos,
1450 u64 disk_bytenr, u64 disk_num_bytes,
1451 u64 num_bytes, u64 ram_bytes,
1452 u8 compression, u8 encryption,
1453 u16 other_encoding, int extent_type)
1455 struct btrfs_root *root = BTRFS_I(inode)->root;
1456 struct btrfs_file_extent_item *fi;
1457 struct btrfs_path *path;
1458 struct extent_buffer *leaf;
1459 struct btrfs_key ins;
1463 path = btrfs_alloc_path();
1466 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1467 file_pos + num_bytes, file_pos, &hint);
1470 ins.objectid = inode->i_ino;
1471 ins.offset = file_pos;
1472 ins.type = BTRFS_EXTENT_DATA_KEY;
1473 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1475 leaf = path->nodes[0];
1476 fi = btrfs_item_ptr(leaf, path->slots[0],
1477 struct btrfs_file_extent_item);
1478 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1479 btrfs_set_file_extent_type(leaf, fi, extent_type);
1480 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1481 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1482 btrfs_set_file_extent_offset(leaf, fi, 0);
1483 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1484 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1485 btrfs_set_file_extent_compression(leaf, fi, compression);
1486 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1487 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1488 btrfs_mark_buffer_dirty(leaf);
1490 inode_add_bytes(inode, num_bytes);
1491 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1493 ins.objectid = disk_bytenr;
1494 ins.offset = disk_num_bytes;
1495 ins.type = BTRFS_EXTENT_ITEM_KEY;
1496 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1497 root->root_key.objectid,
1498 trans->transid, inode->i_ino, &ins);
1501 btrfs_free_path(path);
1505 /* as ordered data IO finishes, this gets called so we can finish
1506 * an ordered extent if the range of bytes in the file it covers are
1509 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1511 struct btrfs_root *root = BTRFS_I(inode)->root;
1512 struct btrfs_trans_handle *trans;
1513 struct btrfs_ordered_extent *ordered_extent;
1514 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1518 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
1522 trans = btrfs_join_transaction(root, 1);
1524 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1525 BUG_ON(!ordered_extent);
1526 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1529 lock_extent(io_tree, ordered_extent->file_offset,
1530 ordered_extent->file_offset + ordered_extent->len - 1,
1533 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1535 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1537 ret = btrfs_mark_extent_written(trans, root, inode,
1538 ordered_extent->file_offset,
1539 ordered_extent->file_offset +
1540 ordered_extent->len);
1543 ret = insert_reserved_file_extent(trans, inode,
1544 ordered_extent->file_offset,
1545 ordered_extent->start,
1546 ordered_extent->disk_len,
1547 ordered_extent->len,
1548 ordered_extent->len,
1550 BTRFS_FILE_EXTENT_REG);
1553 unlock_extent(io_tree, ordered_extent->file_offset,
1554 ordered_extent->file_offset + ordered_extent->len - 1,
1557 add_pending_csums(trans, inode, ordered_extent->file_offset,
1558 &ordered_extent->list);
1560 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1561 btrfs_ordered_update_i_size(inode, ordered_extent);
1562 btrfs_update_inode(trans, root, inode);
1563 btrfs_remove_ordered_extent(inode, ordered_extent);
1564 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1567 btrfs_put_ordered_extent(ordered_extent);
1568 /* once for the tree */
1569 btrfs_put_ordered_extent(ordered_extent);
1571 btrfs_end_transaction(trans, root);
1575 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1576 struct extent_state *state, int uptodate)
1578 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1582 * When IO fails, either with EIO or csum verification fails, we
1583 * try other mirrors that might have a good copy of the data. This
1584 * io_failure_record is used to record state as we go through all the
1585 * mirrors. If another mirror has good data, the page is set up to date
1586 * and things continue. If a good mirror can't be found, the original
1587 * bio end_io callback is called to indicate things have failed.
1589 struct io_failure_record {
1594 unsigned long bio_flags;
1598 static int btrfs_io_failed_hook(struct bio *failed_bio,
1599 struct page *page, u64 start, u64 end,
1600 struct extent_state *state)
1602 struct io_failure_record *failrec = NULL;
1604 struct extent_map *em;
1605 struct inode *inode = page->mapping->host;
1606 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1607 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1614 ret = get_state_private(failure_tree, start, &private);
1616 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1619 failrec->start = start;
1620 failrec->len = end - start + 1;
1621 failrec->last_mirror = 0;
1622 failrec->bio_flags = 0;
1624 spin_lock(&em_tree->lock);
1625 em = lookup_extent_mapping(em_tree, start, failrec->len);
1626 if (em->start > start || em->start + em->len < start) {
1627 free_extent_map(em);
1630 spin_unlock(&em_tree->lock);
1632 if (!em || IS_ERR(em)) {
1636 logical = start - em->start;
1637 logical = em->block_start + logical;
1638 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1639 logical = em->block_start;
1640 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1642 failrec->logical = logical;
1643 free_extent_map(em);
1644 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1645 EXTENT_DIRTY, GFP_NOFS);
1646 set_state_private(failure_tree, start,
1647 (u64)(unsigned long)failrec);
1649 failrec = (struct io_failure_record *)(unsigned long)private;
1651 num_copies = btrfs_num_copies(
1652 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1653 failrec->logical, failrec->len);
1654 failrec->last_mirror++;
1656 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1657 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1660 if (state && state->start != failrec->start)
1662 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1664 if (!state || failrec->last_mirror > num_copies) {
1665 set_state_private(failure_tree, failrec->start, 0);
1666 clear_extent_bits(failure_tree, failrec->start,
1667 failrec->start + failrec->len - 1,
1668 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1672 bio = bio_alloc(GFP_NOFS, 1);
1673 bio->bi_private = state;
1674 bio->bi_end_io = failed_bio->bi_end_io;
1675 bio->bi_sector = failrec->logical >> 9;
1676 bio->bi_bdev = failed_bio->bi_bdev;
1679 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1680 if (failed_bio->bi_rw & (1 << BIO_RW))
1685 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1686 failrec->last_mirror,
1687 failrec->bio_flags);
1692 * each time an IO finishes, we do a fast check in the IO failure tree
1693 * to see if we need to process or clean up an io_failure_record
1695 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1698 u64 private_failure;
1699 struct io_failure_record *failure;
1703 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1704 (u64)-1, 1, EXTENT_DIRTY)) {
1705 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1706 start, &private_failure);
1708 failure = (struct io_failure_record *)(unsigned long)
1710 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1712 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1714 failure->start + failure->len - 1,
1715 EXTENT_DIRTY | EXTENT_LOCKED,
1724 * when reads are done, we need to check csums to verify the data is correct
1725 * if there's a match, we allow the bio to finish. If not, we go through
1726 * the io_failure_record routines to find good copies
1728 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1729 struct extent_state *state)
1731 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1732 struct inode *inode = page->mapping->host;
1733 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1735 u64 private = ~(u32)0;
1737 struct btrfs_root *root = BTRFS_I(inode)->root;
1740 if (PageChecked(page)) {
1741 ClearPageChecked(page);
1744 if (btrfs_test_flag(inode, NODATASUM))
1747 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1748 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1749 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1754 if (state && state->start == start) {
1755 private = state->private;
1758 ret = get_state_private(io_tree, start, &private);
1760 kaddr = kmap_atomic(page, KM_USER0);
1764 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1765 btrfs_csum_final(csum, (char *)&csum);
1766 if (csum != private)
1769 kunmap_atomic(kaddr, KM_USER0);
1771 /* if the io failure tree for this inode is non-empty,
1772 * check to see if we've recovered from a failed IO
1774 btrfs_clean_io_failures(inode, start);
1778 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1779 "private %llu\n", page->mapping->host->i_ino,
1780 (unsigned long long)start, csum,
1781 (unsigned long long)private);
1782 memset(kaddr + offset, 1, end - start + 1);
1783 flush_dcache_page(page);
1784 kunmap_atomic(kaddr, KM_USER0);
1791 * This creates an orphan entry for the given inode in case something goes
1792 * wrong in the middle of an unlink/truncate.
1794 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1796 struct btrfs_root *root = BTRFS_I(inode)->root;
1799 spin_lock(&root->list_lock);
1801 /* already on the orphan list, we're good */
1802 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
1803 spin_unlock(&root->list_lock);
1807 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1809 spin_unlock(&root->list_lock);
1812 * insert an orphan item to track this unlinked/truncated file
1814 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1820 * We have done the truncate/delete so we can go ahead and remove the orphan
1821 * item for this particular inode.
1823 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1825 struct btrfs_root *root = BTRFS_I(inode)->root;
1828 spin_lock(&root->list_lock);
1830 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
1831 spin_unlock(&root->list_lock);
1835 list_del_init(&BTRFS_I(inode)->i_orphan);
1837 spin_unlock(&root->list_lock);
1841 spin_unlock(&root->list_lock);
1843 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1849 * this cleans up any orphans that may be left on the list from the last use
1852 void btrfs_orphan_cleanup(struct btrfs_root *root)
1854 struct btrfs_path *path;
1855 struct extent_buffer *leaf;
1856 struct btrfs_item *item;
1857 struct btrfs_key key, found_key;
1858 struct btrfs_trans_handle *trans;
1859 struct inode *inode;
1860 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1862 path = btrfs_alloc_path();
1867 key.objectid = BTRFS_ORPHAN_OBJECTID;
1868 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1869 key.offset = (u64)-1;
1873 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1875 printk(KERN_ERR "Error searching slot for orphan: %d"
1881 * if ret == 0 means we found what we were searching for, which
1882 * is weird, but possible, so only screw with path if we didnt
1883 * find the key and see if we have stuff that matches
1886 if (path->slots[0] == 0)
1891 /* pull out the item */
1892 leaf = path->nodes[0];
1893 item = btrfs_item_nr(leaf, path->slots[0]);
1894 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1896 /* make sure the item matches what we want */
1897 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1899 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1902 /* release the path since we're done with it */
1903 btrfs_release_path(root, path);
1906 * this is where we are basically btrfs_lookup, without the
1907 * crossing root thing. we store the inode number in the
1908 * offset of the orphan item.
1910 inode = btrfs_iget_locked(root->fs_info->sb,
1911 found_key.offset, root);
1915 if (inode->i_state & I_NEW) {
1916 BTRFS_I(inode)->root = root;
1918 /* have to set the location manually */
1919 BTRFS_I(inode)->location.objectid = inode->i_ino;
1920 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1921 BTRFS_I(inode)->location.offset = 0;
1923 btrfs_read_locked_inode(inode);
1924 unlock_new_inode(inode);
1928 * add this inode to the orphan list so btrfs_orphan_del does
1929 * the proper thing when we hit it
1931 spin_lock(&root->list_lock);
1932 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1933 spin_unlock(&root->list_lock);
1936 * if this is a bad inode, means we actually succeeded in
1937 * removing the inode, but not the orphan record, which means
1938 * we need to manually delete the orphan since iput will just
1939 * do a destroy_inode
1941 if (is_bad_inode(inode)) {
1942 trans = btrfs_start_transaction(root, 1);
1943 btrfs_orphan_del(trans, inode);
1944 btrfs_end_transaction(trans, root);
1949 /* if we have links, this was a truncate, lets do that */
1950 if (inode->i_nlink) {
1952 btrfs_truncate(inode);
1957 /* this will do delete_inode and everything for us */
1962 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1964 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1966 btrfs_free_path(path);
1970 * read an inode from the btree into the in-memory inode
1972 void btrfs_read_locked_inode(struct inode *inode)
1974 struct btrfs_path *path;
1975 struct extent_buffer *leaf;
1976 struct btrfs_inode_item *inode_item;
1977 struct btrfs_timespec *tspec;
1978 struct btrfs_root *root = BTRFS_I(inode)->root;
1979 struct btrfs_key location;
1980 u64 alloc_group_block;
1984 path = btrfs_alloc_path();
1986 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1988 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1992 leaf = path->nodes[0];
1993 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1994 struct btrfs_inode_item);
1996 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1997 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1998 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1999 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2000 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2002 tspec = btrfs_inode_atime(inode_item);
2003 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2004 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2006 tspec = btrfs_inode_mtime(inode_item);
2007 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2008 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2010 tspec = btrfs_inode_ctime(inode_item);
2011 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2012 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2014 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2015 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2016 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2017 inode->i_generation = BTRFS_I(inode)->generation;
2019 rdev = btrfs_inode_rdev(leaf, inode_item);
2021 BTRFS_I(inode)->index_cnt = (u64)-1;
2022 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2024 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2026 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2027 alloc_group_block, 0);
2028 btrfs_free_path(path);
2031 switch (inode->i_mode & S_IFMT) {
2033 inode->i_mapping->a_ops = &btrfs_aops;
2034 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2035 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2036 inode->i_fop = &btrfs_file_operations;
2037 inode->i_op = &btrfs_file_inode_operations;
2040 inode->i_fop = &btrfs_dir_file_operations;
2041 if (root == root->fs_info->tree_root)
2042 inode->i_op = &btrfs_dir_ro_inode_operations;
2044 inode->i_op = &btrfs_dir_inode_operations;
2047 inode->i_op = &btrfs_symlink_inode_operations;
2048 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2049 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2052 inode->i_op = &btrfs_special_inode_operations;
2053 init_special_inode(inode, inode->i_mode, rdev);
2059 btrfs_free_path(path);
2060 make_bad_inode(inode);
2064 * given a leaf and an inode, copy the inode fields into the leaf
2066 static void fill_inode_item(struct btrfs_trans_handle *trans,
2067 struct extent_buffer *leaf,
2068 struct btrfs_inode_item *item,
2069 struct inode *inode)
2071 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2072 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2073 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2074 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2075 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2077 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2078 inode->i_atime.tv_sec);
2079 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2080 inode->i_atime.tv_nsec);
2082 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2083 inode->i_mtime.tv_sec);
2084 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2085 inode->i_mtime.tv_nsec);
2087 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2088 inode->i_ctime.tv_sec);
2089 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2090 inode->i_ctime.tv_nsec);
2092 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2093 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2094 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2095 btrfs_set_inode_transid(leaf, item, trans->transid);
2096 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2097 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2098 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2102 * copy everything in the in-memory inode into the btree.
2104 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2105 struct btrfs_root *root, struct inode *inode)
2107 struct btrfs_inode_item *inode_item;
2108 struct btrfs_path *path;
2109 struct extent_buffer *leaf;
2112 path = btrfs_alloc_path();
2114 ret = btrfs_lookup_inode(trans, root, path,
2115 &BTRFS_I(inode)->location, 1);
2122 btrfs_unlock_up_safe(path, 1);
2123 leaf = path->nodes[0];
2124 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2125 struct btrfs_inode_item);
2127 fill_inode_item(trans, leaf, inode_item, inode);
2128 btrfs_mark_buffer_dirty(leaf);
2129 btrfs_set_inode_last_trans(trans, inode);
2132 btrfs_free_path(path);
2138 * unlink helper that gets used here in inode.c and in the tree logging
2139 * recovery code. It remove a link in a directory with a given name, and
2140 * also drops the back refs in the inode to the directory
2142 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root,
2144 struct inode *dir, struct inode *inode,
2145 const char *name, int name_len)
2147 struct btrfs_path *path;
2149 struct extent_buffer *leaf;
2150 struct btrfs_dir_item *di;
2151 struct btrfs_key key;
2154 path = btrfs_alloc_path();
2160 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2161 name, name_len, -1);
2170 leaf = path->nodes[0];
2171 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2172 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2175 btrfs_release_path(root, path);
2177 ret = btrfs_del_inode_ref(trans, root, name, name_len,
2179 dir->i_ino, &index);
2181 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2182 "inode %lu parent %lu\n", name_len, name,
2183 inode->i_ino, dir->i_ino);
2187 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2188 index, name, name_len, -1);
2197 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2198 btrfs_release_path(root, path);
2200 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2202 BUG_ON(ret != 0 && ret != -ENOENT);
2204 BTRFS_I(dir)->log_dirty_trans = trans->transid;
2206 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2210 btrfs_free_path(path);
2214 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2215 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2216 btrfs_update_inode(trans, root, dir);
2217 btrfs_drop_nlink(inode);
2218 ret = btrfs_update_inode(trans, root, inode);
2219 dir->i_sb->s_dirt = 1;
2224 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2226 struct btrfs_root *root;
2227 struct btrfs_trans_handle *trans;
2228 struct inode *inode = dentry->d_inode;
2230 unsigned long nr = 0;
2232 root = BTRFS_I(dir)->root;
2234 ret = btrfs_check_free_space(root, 1, 1);
2238 trans = btrfs_start_transaction(root, 1);
2240 btrfs_set_trans_block_group(trans, dir);
2241 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2242 dentry->d_name.name, dentry->d_name.len);
2244 if (inode->i_nlink == 0)
2245 ret = btrfs_orphan_add(trans, inode);
2247 nr = trans->blocks_used;
2249 btrfs_end_transaction_throttle(trans, root);
2251 btrfs_btree_balance_dirty(root, nr);
2255 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2257 struct inode *inode = dentry->d_inode;
2260 struct btrfs_root *root = BTRFS_I(dir)->root;
2261 struct btrfs_trans_handle *trans;
2262 unsigned long nr = 0;
2265 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2266 * the root of a subvolume or snapshot
2268 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2269 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
2273 ret = btrfs_check_free_space(root, 1, 1);
2277 trans = btrfs_start_transaction(root, 1);
2278 btrfs_set_trans_block_group(trans, dir);
2280 err = btrfs_orphan_add(trans, inode);
2284 /* now the directory is empty */
2285 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2286 dentry->d_name.name, dentry->d_name.len);
2288 btrfs_i_size_write(inode, 0);
2291 nr = trans->blocks_used;
2292 ret = btrfs_end_transaction_throttle(trans, root);
2294 btrfs_btree_balance_dirty(root, nr);
2303 * when truncating bytes in a file, it is possible to avoid reading
2304 * the leaves that contain only checksum items. This can be the
2305 * majority of the IO required to delete a large file, but it must
2306 * be done carefully.
2308 * The keys in the level just above the leaves are checked to make sure
2309 * the lowest key in a given leaf is a csum key, and starts at an offset
2310 * after the new size.
2312 * Then the key for the next leaf is checked to make sure it also has
2313 * a checksum item for the same file. If it does, we know our target leaf
2314 * contains only checksum items, and it can be safely freed without reading
2317 * This is just an optimization targeted at large files. It may do
2318 * nothing. It will return 0 unless things went badly.
2320 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2321 struct btrfs_root *root,
2322 struct btrfs_path *path,
2323 struct inode *inode, u64 new_size)
2325 struct btrfs_key key;
2328 struct btrfs_key found_key;
2329 struct btrfs_key other_key;
2330 struct btrfs_leaf_ref *ref;
2334 path->lowest_level = 1;
2335 key.objectid = inode->i_ino;
2336 key.type = BTRFS_CSUM_ITEM_KEY;
2337 key.offset = new_size;
2339 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2343 if (path->nodes[1] == NULL) {
2348 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2349 nritems = btrfs_header_nritems(path->nodes[1]);
2354 if (path->slots[1] >= nritems)
2357 /* did we find a key greater than anything we want to delete? */
2358 if (found_key.objectid > inode->i_ino ||
2359 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2362 /* we check the next key in the node to make sure the leave contains
2363 * only checksum items. This comparison doesn't work if our
2364 * leaf is the last one in the node
2366 if (path->slots[1] + 1 >= nritems) {
2368 /* search forward from the last key in the node, this
2369 * will bring us into the next node in the tree
2371 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2373 /* unlikely, but we inc below, so check to be safe */
2374 if (found_key.offset == (u64)-1)
2377 /* search_forward needs a path with locks held, do the
2378 * search again for the original key. It is possible
2379 * this will race with a balance and return a path that
2380 * we could modify, but this drop is just an optimization
2381 * and is allowed to miss some leaves.
2383 btrfs_release_path(root, path);
2386 /* setup a max key for search_forward */
2387 other_key.offset = (u64)-1;
2388 other_key.type = key.type;
2389 other_key.objectid = key.objectid;
2391 path->keep_locks = 1;
2392 ret = btrfs_search_forward(root, &found_key, &other_key,
2394 path->keep_locks = 0;
2395 if (ret || found_key.objectid != key.objectid ||
2396 found_key.type != key.type) {
2401 key.offset = found_key.offset;
2402 btrfs_release_path(root, path);
2407 /* we know there's one more slot after us in the tree,
2408 * read that key so we can verify it is also a checksum item
2410 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2412 if (found_key.objectid < inode->i_ino)
2415 if (found_key.type != key.type || found_key.offset < new_size)
2419 * if the key for the next leaf isn't a csum key from this objectid,
2420 * we can't be sure there aren't good items inside this leaf.
2423 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2426 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2427 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
2429 * it is safe to delete this leaf, it contains only
2430 * csum items from this inode at an offset >= new_size
2432 ret = btrfs_del_leaf(trans, root, path, leaf_start);
2435 if (root->ref_cows && leaf_gen < trans->transid) {
2436 ref = btrfs_alloc_leaf_ref(root, 0);
2438 ref->root_gen = root->root_key.offset;
2439 ref->bytenr = leaf_start;
2441 ref->generation = leaf_gen;
2444 btrfs_sort_leaf_ref(ref);
2446 ret = btrfs_add_leaf_ref(root, ref, 0);
2448 btrfs_free_leaf_ref(root, ref);
2454 btrfs_release_path(root, path);
2456 if (other_key.objectid == inode->i_ino &&
2457 other_key.type == key.type && other_key.offset > key.offset) {
2458 key.offset = other_key.offset;
2464 /* fixup any changes we've made to the path */
2465 path->lowest_level = 0;
2466 path->keep_locks = 0;
2467 btrfs_release_path(root, path);
2474 * this can truncate away extent items, csum items and directory items.
2475 * It starts at a high offset and removes keys until it can't find
2476 * any higher than new_size
2478 * csum items that cross the new i_size are truncated to the new size
2481 * min_type is the minimum key type to truncate down to. If set to 0, this
2482 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2484 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2485 struct btrfs_root *root,
2486 struct inode *inode,
2487 u64 new_size, u32 min_type)
2490 struct btrfs_path *path;
2491 struct btrfs_key key;
2492 struct btrfs_key found_key;
2494 struct extent_buffer *leaf;
2495 struct btrfs_file_extent_item *fi;
2496 u64 extent_start = 0;
2497 u64 extent_num_bytes = 0;
2503 int pending_del_nr = 0;
2504 int pending_del_slot = 0;
2505 int extent_type = -1;
2507 u64 mask = root->sectorsize - 1;
2510 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2511 path = btrfs_alloc_path();
2515 /* FIXME, add redo link to tree so we don't leak on crash */
2516 key.objectid = inode->i_ino;
2517 key.offset = (u64)-1;
2520 btrfs_init_path(path);
2523 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2528 /* there are no items in the tree for us to truncate, we're
2531 if (path->slots[0] == 0) {
2540 leaf = path->nodes[0];
2541 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2542 found_type = btrfs_key_type(&found_key);
2545 if (found_key.objectid != inode->i_ino)
2548 if (found_type < min_type)
2551 item_end = found_key.offset;
2552 if (found_type == BTRFS_EXTENT_DATA_KEY) {
2553 fi = btrfs_item_ptr(leaf, path->slots[0],
2554 struct btrfs_file_extent_item);
2555 extent_type = btrfs_file_extent_type(leaf, fi);
2556 encoding = btrfs_file_extent_compression(leaf, fi);
2557 encoding |= btrfs_file_extent_encryption(leaf, fi);
2558 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2560 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2562 btrfs_file_extent_num_bytes(leaf, fi);
2563 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2564 item_end += btrfs_file_extent_inline_len(leaf,
2569 if (item_end < new_size) {
2570 if (found_type == BTRFS_DIR_ITEM_KEY)
2571 found_type = BTRFS_INODE_ITEM_KEY;
2572 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
2573 found_type = BTRFS_EXTENT_DATA_KEY;
2574 else if (found_type == BTRFS_EXTENT_DATA_KEY)
2575 found_type = BTRFS_XATTR_ITEM_KEY;
2576 else if (found_type == BTRFS_XATTR_ITEM_KEY)
2577 found_type = BTRFS_INODE_REF_KEY;
2578 else if (found_type)
2582 btrfs_set_key_type(&key, found_type);
2585 if (found_key.offset >= new_size)
2591 /* FIXME, shrink the extent if the ref count is only 1 */
2592 if (found_type != BTRFS_EXTENT_DATA_KEY)
2595 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2597 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
2598 if (!del_item && !encoding) {
2599 u64 orig_num_bytes =
2600 btrfs_file_extent_num_bytes(leaf, fi);
2601 extent_num_bytes = new_size -
2602 found_key.offset + root->sectorsize - 1;
2603 extent_num_bytes = extent_num_bytes &
2604 ~((u64)root->sectorsize - 1);
2605 btrfs_set_file_extent_num_bytes(leaf, fi,
2607 num_dec = (orig_num_bytes -
2609 if (root->ref_cows && extent_start != 0)
2610 inode_sub_bytes(inode, num_dec);
2611 btrfs_mark_buffer_dirty(leaf);
2614 btrfs_file_extent_disk_num_bytes(leaf,
2616 /* FIXME blocksize != 4096 */
2617 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2618 if (extent_start != 0) {
2621 inode_sub_bytes(inode, num_dec);
2623 root_gen = btrfs_header_generation(leaf);
2624 root_owner = btrfs_header_owner(leaf);
2626 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2628 * we can't truncate inline items that have had
2632 btrfs_file_extent_compression(leaf, fi) == 0 &&
2633 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2634 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
2635 u32 size = new_size - found_key.offset;
2637 if (root->ref_cows) {
2638 inode_sub_bytes(inode, item_end + 1 -
2642 btrfs_file_extent_calc_inline_size(size);
2643 ret = btrfs_truncate_item(trans, root, path,
2646 } else if (root->ref_cows) {
2647 inode_sub_bytes(inode, item_end + 1 -
2653 if (!pending_del_nr) {
2654 /* no pending yet, add ourselves */
2655 pending_del_slot = path->slots[0];
2657 } else if (pending_del_nr &&
2658 path->slots[0] + 1 == pending_del_slot) {
2659 /* hop on the pending chunk */
2661 pending_del_slot = path->slots[0];
2669 ret = btrfs_free_extent(trans, root, extent_start,
2671 leaf->start, root_owner,
2672 root_gen, inode->i_ino, 0);
2676 if (path->slots[0] == 0) {
2679 btrfs_release_path(root, path);
2684 if (pending_del_nr &&
2685 path->slots[0] + 1 != pending_del_slot) {
2686 struct btrfs_key debug;
2688 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2690 ret = btrfs_del_items(trans, root, path,
2695 btrfs_release_path(root, path);
2701 if (pending_del_nr) {
2702 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2705 btrfs_free_path(path);
2706 inode->i_sb->s_dirt = 1;
2711 * taken from block_truncate_page, but does cow as it zeros out
2712 * any bytes left in the last page in the file.
2714 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2716 struct inode *inode = mapping->host;
2717 struct btrfs_root *root = BTRFS_I(inode)->root;
2718 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2719 struct btrfs_ordered_extent *ordered;
2721 u32 blocksize = root->sectorsize;
2722 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2723 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2729 if ((offset & (blocksize - 1)) == 0)
2734 page = grab_cache_page(mapping, index);
2738 page_start = page_offset(page);
2739 page_end = page_start + PAGE_CACHE_SIZE - 1;
2741 if (!PageUptodate(page)) {
2742 ret = btrfs_readpage(NULL, page);
2744 if (page->mapping != mapping) {
2746 page_cache_release(page);
2749 if (!PageUptodate(page)) {
2754 wait_on_page_writeback(page);
2756 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2757 set_page_extent_mapped(page);
2759 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2761 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2763 page_cache_release(page);
2764 btrfs_start_ordered_extent(inode, ordered, 1);
2765 btrfs_put_ordered_extent(ordered);
2769 btrfs_set_extent_delalloc(inode, page_start, page_end);
2771 if (offset != PAGE_CACHE_SIZE) {
2773 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2774 flush_dcache_page(page);
2777 ClearPageChecked(page);
2778 set_page_dirty(page);
2779 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2783 page_cache_release(page);
2788 int btrfs_cont_expand(struct inode *inode, loff_t size)
2790 struct btrfs_trans_handle *trans;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2793 struct extent_map *em;
2794 u64 mask = root->sectorsize - 1;
2795 u64 hole_start = (inode->i_size + mask) & ~mask;
2796 u64 block_end = (size + mask) & ~mask;
2802 if (size <= hole_start)
2805 err = btrfs_check_free_space(root, 1, 0);
2809 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2812 struct btrfs_ordered_extent *ordered;
2813 btrfs_wait_ordered_range(inode, hole_start,
2814 block_end - hole_start);
2815 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2816 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2819 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2820 btrfs_put_ordered_extent(ordered);
2823 trans = btrfs_start_transaction(root, 1);
2824 btrfs_set_trans_block_group(trans, inode);
2826 cur_offset = hole_start;
2828 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2829 block_end - cur_offset, 0);
2830 BUG_ON(IS_ERR(em) || !em);
2831 last_byte = min(extent_map_end(em), block_end);
2832 last_byte = (last_byte + mask) & ~mask;
2833 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2835 hole_size = last_byte - cur_offset;
2836 err = btrfs_drop_extents(trans, root, inode,
2838 cur_offset + hole_size,
2839 cur_offset, &hint_byte);
2842 err = btrfs_insert_file_extent(trans, root,
2843 inode->i_ino, cur_offset, 0,
2844 0, hole_size, 0, hole_size,
2846 btrfs_drop_extent_cache(inode, hole_start,
2849 free_extent_map(em);
2850 cur_offset = last_byte;
2851 if (err || cur_offset >= block_end)
2855 btrfs_end_transaction(trans, root);
2856 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2860 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2862 struct inode *inode = dentry->d_inode;
2865 err = inode_change_ok(inode, attr);
2869 if (S_ISREG(inode->i_mode) &&
2870 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2871 err = btrfs_cont_expand(inode, attr->ia_size);
2876 err = inode_setattr(inode, attr);
2878 if (!err && ((attr->ia_valid & ATTR_MODE)))
2879 err = btrfs_acl_chmod(inode);
2883 void btrfs_delete_inode(struct inode *inode)
2885 struct btrfs_trans_handle *trans;
2886 struct btrfs_root *root = BTRFS_I(inode)->root;
2890 truncate_inode_pages(&inode->i_data, 0);
2891 if (is_bad_inode(inode)) {
2892 btrfs_orphan_del(NULL, inode);
2895 btrfs_wait_ordered_range(inode, 0, (u64)-1);
2897 btrfs_i_size_write(inode, 0);
2898 trans = btrfs_join_transaction(root, 1);
2900 btrfs_set_trans_block_group(trans, inode);
2901 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
2903 btrfs_orphan_del(NULL, inode);
2904 goto no_delete_lock;
2907 btrfs_orphan_del(trans, inode);
2909 nr = trans->blocks_used;
2912 btrfs_end_transaction(trans, root);
2913 btrfs_btree_balance_dirty(root, nr);
2917 nr = trans->blocks_used;
2918 btrfs_end_transaction(trans, root);
2919 btrfs_btree_balance_dirty(root, nr);
2925 * this returns the key found in the dir entry in the location pointer.
2926 * If no dir entries were found, location->objectid is 0.
2928 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2929 struct btrfs_key *location)
2931 const char *name = dentry->d_name.name;
2932 int namelen = dentry->d_name.len;
2933 struct btrfs_dir_item *di;
2934 struct btrfs_path *path;
2935 struct btrfs_root *root = BTRFS_I(dir)->root;
2938 path = btrfs_alloc_path();
2941 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2946 if (!di || IS_ERR(di))
2949 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
2951 btrfs_free_path(path);
2954 location->objectid = 0;
2959 * when we hit a tree root in a directory, the btrfs part of the inode
2960 * needs to be changed to reflect the root directory of the tree root. This
2961 * is kind of like crossing a mount point.
2963 static int fixup_tree_root_location(struct btrfs_root *root,
2964 struct btrfs_key *location,
2965 struct btrfs_root **sub_root,
2966 struct dentry *dentry)
2968 struct btrfs_root_item *ri;
2970 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2972 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2975 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2976 dentry->d_name.name,
2977 dentry->d_name.len);
2978 if (IS_ERR(*sub_root))
2979 return PTR_ERR(*sub_root);
2981 ri = &(*sub_root)->root_item;
2982 location->objectid = btrfs_root_dirid(ri);
2983 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2984 location->offset = 0;
2989 static noinline void init_btrfs_i(struct inode *inode)
2991 struct btrfs_inode *bi = BTRFS_I(inode);
2994 bi->i_default_acl = NULL;
2999 bi->logged_trans = 0;
3000 bi->delalloc_bytes = 0;
3001 bi->disk_i_size = 0;
3003 bi->index_cnt = (u64)-1;
3004 bi->log_dirty_trans = 0;
3005 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3006 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3007 inode->i_mapping, GFP_NOFS);
3008 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3009 inode->i_mapping, GFP_NOFS);
3010 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3011 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3012 mutex_init(&BTRFS_I(inode)->extent_mutex);
3013 mutex_init(&BTRFS_I(inode)->log_mutex);
3016 static int btrfs_init_locked_inode(struct inode *inode, void *p)
3018 struct btrfs_iget_args *args = p;
3019 inode->i_ino = args->ino;
3020 init_btrfs_i(inode);
3021 BTRFS_I(inode)->root = args->root;
3025 static int btrfs_find_actor(struct inode *inode, void *opaque)
3027 struct btrfs_iget_args *args = opaque;
3028 return args->ino == inode->i_ino &&
3029 args->root == BTRFS_I(inode)->root;
3032 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3033 struct btrfs_root *root, int wait)
3035 struct inode *inode;
3036 struct btrfs_iget_args args;
3037 args.ino = objectid;
3041 inode = ilookup5(s, objectid, btrfs_find_actor,
3044 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3050 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3051 struct btrfs_root *root)
3053 struct inode *inode;
3054 struct btrfs_iget_args args;
3055 args.ino = objectid;
3058 inode = iget5_locked(s, objectid, btrfs_find_actor,
3059 btrfs_init_locked_inode,
3064 /* Get an inode object given its location and corresponding root.
3065 * Returns in *is_new if the inode was read from disk
3067 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3068 struct btrfs_root *root, int *is_new)
3070 struct inode *inode;
3072 inode = btrfs_iget_locked(s, location->objectid, root);
3074 return ERR_PTR(-EACCES);
3076 if (inode->i_state & I_NEW) {
3077 BTRFS_I(inode)->root = root;
3078 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3079 btrfs_read_locked_inode(inode);
3080 unlock_new_inode(inode);
3091 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3093 struct inode *inode;
3094 struct btrfs_inode *bi = BTRFS_I(dir);
3095 struct btrfs_root *root = bi->root;
3096 struct btrfs_root *sub_root = root;
3097 struct btrfs_key location;
3100 if (dentry->d_name.len > BTRFS_NAME_LEN)
3101 return ERR_PTR(-ENAMETOOLONG);
3103 ret = btrfs_inode_by_name(dir, dentry, &location);
3106 return ERR_PTR(ret);
3109 if (location.objectid) {
3110 ret = fixup_tree_root_location(root, &location, &sub_root,
3113 return ERR_PTR(ret);
3115 return ERR_PTR(-ENOENT);
3116 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3118 return ERR_CAST(inode);
3123 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3124 struct nameidata *nd)
3126 struct inode *inode;
3128 if (dentry->d_name.len > BTRFS_NAME_LEN)
3129 return ERR_PTR(-ENAMETOOLONG);
3131 inode = btrfs_lookup_dentry(dir, dentry);
3133 return ERR_CAST(inode);
3135 return d_splice_alias(inode, dentry);
3138 static unsigned char btrfs_filetype_table[] = {
3139 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3142 static int btrfs_real_readdir(struct file *filp, void *dirent,
3145 struct inode *inode = filp->f_dentry->d_inode;
3146 struct btrfs_root *root = BTRFS_I(inode)->root;
3147 struct btrfs_item *item;
3148 struct btrfs_dir_item *di;
3149 struct btrfs_key key;
3150 struct btrfs_key found_key;
3151 struct btrfs_path *path;
3154 struct extent_buffer *leaf;
3157 unsigned char d_type;
3162 int key_type = BTRFS_DIR_INDEX_KEY;
3167 /* FIXME, use a real flag for deciding about the key type */
3168 if (root->fs_info->tree_root == root)
3169 key_type = BTRFS_DIR_ITEM_KEY;
3171 /* special case for "." */
3172 if (filp->f_pos == 0) {
3173 over = filldir(dirent, ".", 1,
3180 /* special case for .., just use the back ref */
3181 if (filp->f_pos == 1) {
3182 u64 pino = parent_ino(filp->f_path.dentry);
3183 over = filldir(dirent, "..", 2,
3189 path = btrfs_alloc_path();
3192 btrfs_set_key_type(&key, key_type);
3193 key.offset = filp->f_pos;
3194 key.objectid = inode->i_ino;
3196 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3202 leaf = path->nodes[0];
3203 nritems = btrfs_header_nritems(leaf);
3204 slot = path->slots[0];
3205 if (advance || slot >= nritems) {
3206 if (slot >= nritems - 1) {
3207 ret = btrfs_next_leaf(root, path);
3210 leaf = path->nodes[0];
3211 nritems = btrfs_header_nritems(leaf);
3212 slot = path->slots[0];
3220 item = btrfs_item_nr(leaf, slot);
3221 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3223 if (found_key.objectid != key.objectid)
3225 if (btrfs_key_type(&found_key) != key_type)
3227 if (found_key.offset < filp->f_pos)
3230 filp->f_pos = found_key.offset;
3232 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3234 di_total = btrfs_item_size(leaf, item);
3236 while (di_cur < di_total) {
3237 struct btrfs_key location;
3239 name_len = btrfs_dir_name_len(leaf, di);
3240 if (name_len <= sizeof(tmp_name)) {
3241 name_ptr = tmp_name;
3243 name_ptr = kmalloc(name_len, GFP_NOFS);
3249 read_extent_buffer(leaf, name_ptr,
3250 (unsigned long)(di + 1), name_len);
3252 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3253 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3255 /* is this a reference to our own snapshot? If so
3258 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3259 location.objectid == root->root_key.objectid) {
3263 over = filldir(dirent, name_ptr, name_len,
3264 found_key.offset, location.objectid,
3268 if (name_ptr != tmp_name)
3273 di_len = btrfs_dir_name_len(leaf, di) +
3274 btrfs_dir_data_len(leaf, di) + sizeof(*di);
3276 di = (struct btrfs_dir_item *)((char *)di + di_len);
3280 /* Reached end of directory/root. Bump pos past the last item. */
3281 if (key_type == BTRFS_DIR_INDEX_KEY)
3282 filp->f_pos = INT_LIMIT(off_t);
3288 btrfs_free_path(path);
3292 int btrfs_write_inode(struct inode *inode, int wait)
3294 struct btrfs_root *root = BTRFS_I(inode)->root;
3295 struct btrfs_trans_handle *trans;
3298 if (root->fs_info->btree_inode == inode)
3302 trans = btrfs_join_transaction(root, 1);
3303 btrfs_set_trans_block_group(trans, inode);
3304 ret = btrfs_commit_transaction(trans, root);
3310 * This is somewhat expensive, updating the tree every time the
3311 * inode changes. But, it is most likely to find the inode in cache.
3312 * FIXME, needs more benchmarking...there are no reasons other than performance
3313 * to keep or drop this code.
3315 void btrfs_dirty_inode(struct inode *inode)
3317 struct btrfs_root *root = BTRFS_I(inode)->root;
3318 struct btrfs_trans_handle *trans;
3320 trans = btrfs_join_transaction(root, 1);
3321 btrfs_set_trans_block_group(trans, inode);
3322 btrfs_update_inode(trans, root, inode);
3323 btrfs_end_transaction(trans, root);
3327 * find the highest existing sequence number in a directory
3328 * and then set the in-memory index_cnt variable to reflect
3329 * free sequence numbers
3331 static int btrfs_set_inode_index_count(struct inode *inode)
3333 struct btrfs_root *root = BTRFS_I(inode)->root;
3334 struct btrfs_key key, found_key;
3335 struct btrfs_path *path;
3336 struct extent_buffer *leaf;
3339 key.objectid = inode->i_ino;
3340 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3341 key.offset = (u64)-1;
3343 path = btrfs_alloc_path();
3347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3350 /* FIXME: we should be able to handle this */
3356 * MAGIC NUMBER EXPLANATION:
3357 * since we search a directory based on f_pos we have to start at 2
3358 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3359 * else has to start at 2
3361 if (path->slots[0] == 0) {
3362 BTRFS_I(inode)->index_cnt = 2;
3368 leaf = path->nodes[0];
3369 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3371 if (found_key.objectid != inode->i_ino ||
3372 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3373 BTRFS_I(inode)->index_cnt = 2;
3377 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3379 btrfs_free_path(path);
3384 * helper to find a free sequence number in a given directory. This current
3385 * code is very simple, later versions will do smarter things in the btree
3387 int btrfs_set_inode_index(struct inode *dir, u64 *index)
3391 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3392 ret = btrfs_set_inode_index_count(dir);
3397 *index = BTRFS_I(dir)->index_cnt;
3398 BTRFS_I(dir)->index_cnt++;
3403 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
3406 const char *name, int name_len,
3407 u64 ref_objectid, u64 objectid,
3408 u64 alloc_hint, int mode, u64 *index)
3410 struct inode *inode;
3411 struct btrfs_inode_item *inode_item;
3412 struct btrfs_key *location;
3413 struct btrfs_path *path;
3414 struct btrfs_inode_ref *ref;
3415 struct btrfs_key key[2];
3421 path = btrfs_alloc_path();
3424 inode = new_inode(root->fs_info->sb);
3426 return ERR_PTR(-ENOMEM);
3429 ret = btrfs_set_inode_index(dir, index);
3431 return ERR_PTR(ret);
3434 * index_cnt is ignored for everything but a dir,
3435 * btrfs_get_inode_index_count has an explanation for the magic
3438 init_btrfs_i(inode);
3439 BTRFS_I(inode)->index_cnt = 2;
3440 BTRFS_I(inode)->root = root;
3441 BTRFS_I(inode)->generation = trans->transid;
3447 BTRFS_I(inode)->block_group =
3448 btrfs_find_block_group(root, 0, alloc_hint, owner);
3449 if ((mode & S_IFREG)) {
3450 if (btrfs_test_opt(root, NODATASUM))
3451 btrfs_set_flag(inode, NODATASUM);
3452 if (btrfs_test_opt(root, NODATACOW))
3453 btrfs_set_flag(inode, NODATACOW);
3456 key[0].objectid = objectid;
3457 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3460 key[1].objectid = objectid;
3461 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3462 key[1].offset = ref_objectid;
3464 sizes[0] = sizeof(struct btrfs_inode_item);
3465 sizes[1] = name_len + sizeof(*ref);
3467 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3471 if (objectid > root->highest_inode)
3472 root->highest_inode = objectid;
3474 inode->i_uid = current_fsuid();
3476 if (dir->i_mode & S_ISGID) {
3477 inode->i_gid = dir->i_gid;
3481 inode->i_gid = current_fsgid();
3483 inode->i_mode = mode;
3484 inode->i_ino = objectid;
3485 inode_set_bytes(inode, 0);
3486 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3487 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3488 struct btrfs_inode_item);
3489 fill_inode_item(trans, path->nodes[0], inode_item, inode);
3491 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3492 struct btrfs_inode_ref);
3493 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
3494 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
3495 ptr = (unsigned long)(ref + 1);
3496 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3498 btrfs_mark_buffer_dirty(path->nodes[0]);
3499 btrfs_free_path(path);
3501 location = &BTRFS_I(inode)->location;
3502 location->objectid = objectid;
3503 location->offset = 0;
3504 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3506 insert_inode_hash(inode);
3510 BTRFS_I(dir)->index_cnt--;
3511 btrfs_free_path(path);
3512 return ERR_PTR(ret);
3515 static inline u8 btrfs_inode_type(struct inode *inode)
3517 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3521 * utility function to add 'inode' into 'parent_inode' with
3522 * a give name and a given sequence number.
3523 * if 'add_backref' is true, also insert a backref from the
3524 * inode to the parent directory.
3526 int btrfs_add_link(struct btrfs_trans_handle *trans,
3527 struct inode *parent_inode, struct inode *inode,
3528 const char *name, int name_len, int add_backref, u64 index)
3531 struct btrfs_key key;
3532 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
3534 key.objectid = inode->i_ino;
3535 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3538 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3539 parent_inode->i_ino,
3540 &key, btrfs_inode_type(inode),
3544 ret = btrfs_insert_inode_ref(trans, root,
3547 parent_inode->i_ino,
3550 btrfs_i_size_write(parent_inode, parent_inode->i_size +
3552 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
3553 ret = btrfs_update_inode(trans, root, parent_inode);
3558 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
3559 struct dentry *dentry, struct inode *inode,
3560 int backref, u64 index)
3562 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3563 inode, dentry->d_name.name,
3564 dentry->d_name.len, backref, index);
3566 d_instantiate(dentry, inode);
3574 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3575 int mode, dev_t rdev)
3577 struct btrfs_trans_handle *trans;
3578 struct btrfs_root *root = BTRFS_I(dir)->root;
3579 struct inode *inode = NULL;
3583 unsigned long nr = 0;
3586 if (!new_valid_dev(rdev))
3589 err = btrfs_check_free_space(root, 1, 0);
3593 trans = btrfs_start_transaction(root, 1);
3594 btrfs_set_trans_block_group(trans, dir);
3596 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3604 dentry->d_parent->d_inode->i_ino, objectid,
3605 BTRFS_I(dir)->block_group, mode, &index);
3606 err = PTR_ERR(inode);
3610 err = btrfs_init_inode_security(inode, dir);
3616 btrfs_set_trans_block_group(trans, inode);
3617 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3621 inode->i_op = &btrfs_special_inode_operations;
3622 init_special_inode(inode, inode->i_mode, rdev);
3623 btrfs_update_inode(trans, root, inode);
3625 dir->i_sb->s_dirt = 1;
3626 btrfs_update_inode_block_group(trans, inode);
3627 btrfs_update_inode_block_group(trans, dir);
3629 nr = trans->blocks_used;
3630 btrfs_end_transaction_throttle(trans, root);
3633 inode_dec_link_count(inode);
3636 btrfs_btree_balance_dirty(root, nr);
3640 static int btrfs_create(struct inode *dir, struct dentry *dentry,
3641 int mode, struct nameidata *nd)
3643 struct btrfs_trans_handle *trans;
3644 struct btrfs_root *root = BTRFS_I(dir)->root;
3645 struct inode *inode = NULL;
3648 unsigned long nr = 0;
3652 err = btrfs_check_free_space(root, 1, 0);
3655 trans = btrfs_start_transaction(root, 1);
3656 btrfs_set_trans_block_group(trans, dir);
3658 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3664 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3666 dentry->d_parent->d_inode->i_ino,
3667 objectid, BTRFS_I(dir)->block_group, mode,
3669 err = PTR_ERR(inode);
3673 err = btrfs_init_inode_security(inode, dir);
3679 btrfs_set_trans_block_group(trans, inode);
3680 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3684 inode->i_mapping->a_ops = &btrfs_aops;
3685 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3686 inode->i_fop = &btrfs_file_operations;
3687 inode->i_op = &btrfs_file_inode_operations;
3688 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3690 dir->i_sb->s_dirt = 1;
3691 btrfs_update_inode_block_group(trans, inode);
3692 btrfs_update_inode_block_group(trans, dir);
3694 nr = trans->blocks_used;
3695 btrfs_end_transaction_throttle(trans, root);
3698 inode_dec_link_count(inode);
3701 btrfs_btree_balance_dirty(root, nr);
3705 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3706 struct dentry *dentry)
3708 struct btrfs_trans_handle *trans;
3709 struct btrfs_root *root = BTRFS_I(dir)->root;
3710 struct inode *inode = old_dentry->d_inode;
3712 unsigned long nr = 0;
3716 if (inode->i_nlink == 0)
3719 btrfs_inc_nlink(inode);
3720 err = btrfs_check_free_space(root, 1, 0);
3723 err = btrfs_set_inode_index(dir, &index);
3727 trans = btrfs_start_transaction(root, 1);
3729 btrfs_set_trans_block_group(trans, dir);
3730 atomic_inc(&inode->i_count);
3732 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
3737 dir->i_sb->s_dirt = 1;
3738 btrfs_update_inode_block_group(trans, dir);
3739 err = btrfs_update_inode(trans, root, inode);
3744 nr = trans->blocks_used;
3745 btrfs_end_transaction_throttle(trans, root);
3748 inode_dec_link_count(inode);
3751 btrfs_btree_balance_dirty(root, nr);
3755 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3757 struct inode *inode = NULL;
3758 struct btrfs_trans_handle *trans;
3759 struct btrfs_root *root = BTRFS_I(dir)->root;
3761 int drop_on_err = 0;
3764 unsigned long nr = 1;
3766 err = btrfs_check_free_space(root, 1, 0);
3770 trans = btrfs_start_transaction(root, 1);
3771 btrfs_set_trans_block_group(trans, dir);
3773 if (IS_ERR(trans)) {
3774 err = PTR_ERR(trans);
3778 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3784 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3786 dentry->d_parent->d_inode->i_ino, objectid,
3787 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3789 if (IS_ERR(inode)) {
3790 err = PTR_ERR(inode);
3796 err = btrfs_init_inode_security(inode, dir);
3800 inode->i_op = &btrfs_dir_inode_operations;
3801 inode->i_fop = &btrfs_dir_file_operations;
3802 btrfs_set_trans_block_group(trans, inode);
3804 btrfs_i_size_write(inode, 0);
3805 err = btrfs_update_inode(trans, root, inode);
3809 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3810 inode, dentry->d_name.name,
3811 dentry->d_name.len, 0, index);
3815 d_instantiate(dentry, inode);
3817 dir->i_sb->s_dirt = 1;
3818 btrfs_update_inode_block_group(trans, inode);
3819 btrfs_update_inode_block_group(trans, dir);
3822 nr = trans->blocks_used;
3823 btrfs_end_transaction_throttle(trans, root);
3828 btrfs_btree_balance_dirty(root, nr);
3832 /* helper for btfs_get_extent. Given an existing extent in the tree,
3833 * and an extent that you want to insert, deal with overlap and insert
3834 * the new extent into the tree.
3836 static int merge_extent_mapping(struct extent_map_tree *em_tree,
3837 struct extent_map *existing,
3838 struct extent_map *em,
3839 u64 map_start, u64 map_len)
3843 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3844 start_diff = map_start - em->start;
3845 em->start = map_start;
3847 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3848 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3849 em->block_start += start_diff;
3850 em->block_len -= start_diff;
3852 return add_extent_mapping(em_tree, em);
3855 static noinline int uncompress_inline(struct btrfs_path *path,
3856 struct inode *inode, struct page *page,
3857 size_t pg_offset, u64 extent_offset,
3858 struct btrfs_file_extent_item *item)
3861 struct extent_buffer *leaf = path->nodes[0];
3864 unsigned long inline_size;
3867 WARN_ON(pg_offset != 0);
3868 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3869 inline_size = btrfs_file_extent_inline_item_len(leaf,
3870 btrfs_item_nr(leaf, path->slots[0]));
3871 tmp = kmalloc(inline_size, GFP_NOFS);
3872 ptr = btrfs_file_extent_inline_start(item);
3874 read_extent_buffer(leaf, tmp, ptr, inline_size);
3876 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
3877 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3878 inline_size, max_size);
3880 char *kaddr = kmap_atomic(page, KM_USER0);
3881 unsigned long copy_size = min_t(u64,
3882 PAGE_CACHE_SIZE - pg_offset,
3883 max_size - extent_offset);
3884 memset(kaddr + pg_offset, 0, copy_size);
3885 kunmap_atomic(kaddr, KM_USER0);
3892 * a bit scary, this does extent mapping from logical file offset to the disk.
3893 * the ugly parts come from merging extents from the disk with the in-ram
3894 * representation. This gets more complex because of the data=ordered code,
3895 * where the in-ram extents might be locked pending data=ordered completion.
3897 * This also copies inline extents directly into the page.
3900 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
3901 size_t pg_offset, u64 start, u64 len,
3907 u64 extent_start = 0;
3909 u64 objectid = inode->i_ino;
3911 struct btrfs_path *path = NULL;
3912 struct btrfs_root *root = BTRFS_I(inode)->root;
3913 struct btrfs_file_extent_item *item;
3914 struct extent_buffer *leaf;
3915 struct btrfs_key found_key;
3916 struct extent_map *em = NULL;
3917 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3918 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3919 struct btrfs_trans_handle *trans = NULL;
3923 spin_lock(&em_tree->lock);
3924 em = lookup_extent_mapping(em_tree, start, len);
3926 em->bdev = root->fs_info->fs_devices->latest_bdev;
3927 spin_unlock(&em_tree->lock);
3930 if (em->start > start || em->start + em->len <= start)
3931 free_extent_map(em);
3932 else if (em->block_start == EXTENT_MAP_INLINE && page)
3933 free_extent_map(em);
3937 em = alloc_extent_map(GFP_NOFS);
3942 em->bdev = root->fs_info->fs_devices->latest_bdev;
3943 em->start = EXTENT_MAP_HOLE;
3944 em->orig_start = EXTENT_MAP_HOLE;
3946 em->block_len = (u64)-1;
3949 path = btrfs_alloc_path();
3953 ret = btrfs_lookup_file_extent(trans, root, path,
3954 objectid, start, trans != NULL);
3961 if (path->slots[0] == 0)
3966 leaf = path->nodes[0];
3967 item = btrfs_item_ptr(leaf, path->slots[0],
3968 struct btrfs_file_extent_item);
3969 /* are we inside the extent that was found? */
3970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3971 found_type = btrfs_key_type(&found_key);
3972 if (found_key.objectid != objectid ||
3973 found_type != BTRFS_EXTENT_DATA_KEY) {
3977 found_type = btrfs_file_extent_type(leaf, item);
3978 extent_start = found_key.offset;
3979 compressed = btrfs_file_extent_compression(leaf, item);
3980 if (found_type == BTRFS_FILE_EXTENT_REG ||
3981 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
3982 extent_end = extent_start +
3983 btrfs_file_extent_num_bytes(leaf, item);
3984 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3986 size = btrfs_file_extent_inline_len(leaf, item);
3987 extent_end = (extent_start + size + root->sectorsize - 1) &
3988 ~((u64)root->sectorsize - 1);
3991 if (start >= extent_end) {
3993 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3994 ret = btrfs_next_leaf(root, path);
4001 leaf = path->nodes[0];
4003 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4004 if (found_key.objectid != objectid ||
4005 found_key.type != BTRFS_EXTENT_DATA_KEY)
4007 if (start + len <= found_key.offset)
4010 em->len = found_key.offset - start;
4014 if (found_type == BTRFS_FILE_EXTENT_REG ||
4015 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
4016 em->start = extent_start;
4017 em->len = extent_end - extent_start;
4018 em->orig_start = extent_start -
4019 btrfs_file_extent_offset(leaf, item);
4020 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4022 em->block_start = EXTENT_MAP_HOLE;
4026 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4027 em->block_start = bytenr;
4028 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4031 bytenr += btrfs_file_extent_offset(leaf, item);
4032 em->block_start = bytenr;
4033 em->block_len = em->len;
4034 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4035 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
4038 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4042 size_t extent_offset;
4045 em->block_start = EXTENT_MAP_INLINE;
4046 if (!page || create) {
4047 em->start = extent_start;
4048 em->len = extent_end - extent_start;
4052 size = btrfs_file_extent_inline_len(leaf, item);
4053 extent_offset = page_offset(page) + pg_offset - extent_start;
4054 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
4055 size - extent_offset);
4056 em->start = extent_start + extent_offset;
4057 em->len = (copy_size + root->sectorsize - 1) &
4058 ~((u64)root->sectorsize - 1);
4059 em->orig_start = EXTENT_MAP_INLINE;
4061 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4062 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
4063 if (create == 0 && !PageUptodate(page)) {
4064 if (btrfs_file_extent_compression(leaf, item) ==
4065 BTRFS_COMPRESS_ZLIB) {
4066 ret = uncompress_inline(path, inode, page,
4068 extent_offset, item);
4072 read_extent_buffer(leaf, map + pg_offset, ptr,
4076 flush_dcache_page(page);
4077 } else if (create && PageUptodate(page)) {
4080 free_extent_map(em);
4082 btrfs_release_path(root, path);
4083 trans = btrfs_join_transaction(root, 1);
4087 write_extent_buffer(leaf, map + pg_offset, ptr,
4090 btrfs_mark_buffer_dirty(leaf);
4092 set_extent_uptodate(io_tree, em->start,
4093 extent_map_end(em) - 1, GFP_NOFS);
4096 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
4103 em->block_start = EXTENT_MAP_HOLE;
4104 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
4106 btrfs_release_path(root, path);
4107 if (em->start > start || extent_map_end(em) <= start) {
4108 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4109 "[%llu %llu]\n", (unsigned long long)em->start,
4110 (unsigned long long)em->len,
4111 (unsigned long long)start,
4112 (unsigned long long)len);
4118 spin_lock(&em_tree->lock);
4119 ret = add_extent_mapping(em_tree, em);
4120 /* it is possible that someone inserted the extent into the tree
4121 * while we had the lock dropped. It is also possible that
4122 * an overlapping map exists in the tree
4124 if (ret == -EEXIST) {
4125 struct extent_map *existing;
4129 existing = lookup_extent_mapping(em_tree, start, len);
4130 if (existing && (existing->start > start ||
4131 existing->start + existing->len <= start)) {
4132 free_extent_map(existing);
4136 existing = lookup_extent_mapping(em_tree, em->start,
4139 err = merge_extent_mapping(em_tree, existing,
4142 free_extent_map(existing);
4144 free_extent_map(em);
4149 free_extent_map(em);
4153 free_extent_map(em);
4158 spin_unlock(&em_tree->lock);
4161 btrfs_free_path(path);
4163 ret = btrfs_end_transaction(trans, root);
4168 free_extent_map(em);
4170 return ERR_PTR(err);
4175 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4176 const struct iovec *iov, loff_t offset,
4177 unsigned long nr_segs)
4182 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4183 __u64 start, __u64 len)
4185 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4188 int btrfs_readpage(struct file *file, struct page *page)
4190 struct extent_io_tree *tree;
4191 tree = &BTRFS_I(page->mapping->host)->io_tree;
4192 return extent_read_full_page(tree, page, btrfs_get_extent);
4195 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4197 struct extent_io_tree *tree;
4200 if (current->flags & PF_MEMALLOC) {
4201 redirty_page_for_writepage(wbc, page);
4205 tree = &BTRFS_I(page->mapping->host)->io_tree;
4206 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4209 int btrfs_writepages(struct address_space *mapping,
4210 struct writeback_control *wbc)
4212 struct extent_io_tree *tree;
4214 tree = &BTRFS_I(mapping->host)->io_tree;
4215 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4219 btrfs_readpages(struct file *file, struct address_space *mapping,
4220 struct list_head *pages, unsigned nr_pages)
4222 struct extent_io_tree *tree;
4223 tree = &BTRFS_I(mapping->host)->io_tree;
4224 return extent_readpages(tree, mapping, pages, nr_pages,
4227 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4229 struct extent_io_tree *tree;
4230 struct extent_map_tree *map;
4233 tree = &BTRFS_I(page->mapping->host)->io_tree;
4234 map = &BTRFS_I(page->mapping->host)->extent_tree;
4235 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
4237 ClearPagePrivate(page);
4238 set_page_private(page, 0);
4239 page_cache_release(page);
4244 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4246 if (PageWriteback(page) || PageDirty(page))
4248 return __btrfs_releasepage(page, gfp_flags);
4251 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4253 struct extent_io_tree *tree;
4254 struct btrfs_ordered_extent *ordered;
4255 u64 page_start = page_offset(page);
4256 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4258 wait_on_page_writeback(page);
4259 tree = &BTRFS_I(page->mapping->host)->io_tree;
4261 btrfs_releasepage(page, GFP_NOFS);
4265 lock_extent(tree, page_start, page_end, GFP_NOFS);
4266 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4270 * IO on this page will never be started, so we need
4271 * to account for any ordered extents now
4273 clear_extent_bit(tree, page_start, page_end,
4274 EXTENT_DIRTY | EXTENT_DELALLOC |
4275 EXTENT_LOCKED, 1, 0, GFP_NOFS);
4276 btrfs_finish_ordered_io(page->mapping->host,
4277 page_start, page_end);
4278 btrfs_put_ordered_extent(ordered);
4279 lock_extent(tree, page_start, page_end, GFP_NOFS);
4281 clear_extent_bit(tree, page_start, page_end,
4282 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4285 __btrfs_releasepage(page, GFP_NOFS);
4287 ClearPageChecked(page);
4288 if (PagePrivate(page)) {
4289 ClearPagePrivate(page);
4290 set_page_private(page, 0);
4291 page_cache_release(page);
4296 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4297 * called from a page fault handler when a page is first dirtied. Hence we must
4298 * be careful to check for EOF conditions here. We set the page up correctly
4299 * for a written page which means we get ENOSPC checking when writing into
4300 * holes and correct delalloc and unwritten extent mapping on filesystems that
4301 * support these features.
4303 * We are not allowed to take the i_mutex here so we have to play games to
4304 * protect against truncate races as the page could now be beyond EOF. Because
4305 * vmtruncate() writes the inode size before removing pages, once we have the
4306 * page lock we can determine safely if the page is beyond EOF. If it is not
4307 * beyond EOF, then the page is guaranteed safe against truncation until we
4310 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4312 struct inode *inode = fdentry(vma->vm_file)->d_inode;
4313 struct btrfs_root *root = BTRFS_I(inode)->root;
4314 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4315 struct btrfs_ordered_extent *ordered;
4317 unsigned long zero_start;
4323 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
4330 size = i_size_read(inode);
4331 page_start = page_offset(page);
4332 page_end = page_start + PAGE_CACHE_SIZE - 1;
4334 if ((page->mapping != inode->i_mapping) ||
4335 (page_start >= size)) {
4336 /* page got truncated out from underneath us */
4339 wait_on_page_writeback(page);
4341 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4342 set_page_extent_mapped(page);
4345 * we can't set the delalloc bits if there are pending ordered
4346 * extents. Drop our locks and wait for them to finish
4348 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4350 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4352 btrfs_start_ordered_extent(inode, ordered, 1);
4353 btrfs_put_ordered_extent(ordered);
4357 btrfs_set_extent_delalloc(inode, page_start, page_end);
4360 /* page is wholly or partially inside EOF */
4361 if (page_start + PAGE_CACHE_SIZE > size)
4362 zero_start = size & ~PAGE_CACHE_MASK;
4364 zero_start = PAGE_CACHE_SIZE;
4366 if (zero_start != PAGE_CACHE_SIZE) {
4368 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4369 flush_dcache_page(page);
4372 ClearPageChecked(page);
4373 set_page_dirty(page);
4374 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4382 static void btrfs_truncate(struct inode *inode)
4384 struct btrfs_root *root = BTRFS_I(inode)->root;
4386 struct btrfs_trans_handle *trans;
4388 u64 mask = root->sectorsize - 1;
4390 if (!S_ISREG(inode->i_mode))
4392 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4395 btrfs_truncate_page(inode->i_mapping, inode->i_size);
4396 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
4398 trans = btrfs_start_transaction(root, 1);
4399 btrfs_set_trans_block_group(trans, inode);
4400 btrfs_i_size_write(inode, inode->i_size);
4402 ret = btrfs_orphan_add(trans, inode);
4405 /* FIXME, add redo link to tree so we don't leak on crash */
4406 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
4407 BTRFS_EXTENT_DATA_KEY);
4408 btrfs_update_inode(trans, root, inode);
4410 ret = btrfs_orphan_del(trans, inode);
4414 nr = trans->blocks_used;
4415 ret = btrfs_end_transaction_throttle(trans, root);
4417 btrfs_btree_balance_dirty(root, nr);
4421 * create a new subvolume directory/inode (helper for the ioctl).
4423 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4424 struct btrfs_root *new_root, struct dentry *dentry,
4425 u64 new_dirid, u64 alloc_hint)
4427 struct inode *inode;
4431 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
4432 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
4434 return PTR_ERR(inode);
4435 inode->i_op = &btrfs_dir_inode_operations;
4436 inode->i_fop = &btrfs_dir_file_operations;
4439 btrfs_i_size_write(inode, 0);
4441 error = btrfs_update_inode(trans, new_root, inode);
4445 d_instantiate(dentry, inode);
4449 /* helper function for file defrag and space balancing. This
4450 * forces readahead on a given range of bytes in an inode
4452 unsigned long btrfs_force_ra(struct address_space *mapping,
4453 struct file_ra_state *ra, struct file *file,
4454 pgoff_t offset, pgoff_t last_index)
4456 pgoff_t req_size = last_index - offset + 1;
4458 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4459 return offset + req_size;
4462 struct inode *btrfs_alloc_inode(struct super_block *sb)
4464 struct btrfs_inode *ei;
4466 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4470 ei->logged_trans = 0;
4471 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4472 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4473 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
4474 INIT_LIST_HEAD(&ei->i_orphan);
4475 return &ei->vfs_inode;
4478 void btrfs_destroy_inode(struct inode *inode)
4480 struct btrfs_ordered_extent *ordered;
4481 WARN_ON(!list_empty(&inode->i_dentry));
4482 WARN_ON(inode->i_data.nrpages);
4484 if (BTRFS_I(inode)->i_acl &&
4485 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4486 posix_acl_release(BTRFS_I(inode)->i_acl);
4487 if (BTRFS_I(inode)->i_default_acl &&
4488 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4489 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4491 spin_lock(&BTRFS_I(inode)->root->list_lock);
4492 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4493 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4494 " list\n", inode->i_ino);
4497 spin_unlock(&BTRFS_I(inode)->root->list_lock);
4500 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4504 printk(KERN_ERR "btrfs found ordered "
4505 "extent %llu %llu on inode cleanup\n",
4506 (unsigned long long)ordered->file_offset,
4507 (unsigned long long)ordered->len);
4508 btrfs_remove_ordered_extent(inode, ordered);
4509 btrfs_put_ordered_extent(ordered);
4510 btrfs_put_ordered_extent(ordered);
4513 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
4514 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4517 static void init_once(void *foo)
4519 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4521 inode_init_once(&ei->vfs_inode);
4524 void btrfs_destroy_cachep(void)
4526 if (btrfs_inode_cachep)
4527 kmem_cache_destroy(btrfs_inode_cachep);
4528 if (btrfs_trans_handle_cachep)
4529 kmem_cache_destroy(btrfs_trans_handle_cachep);
4530 if (btrfs_transaction_cachep)
4531 kmem_cache_destroy(btrfs_transaction_cachep);
4532 if (btrfs_bit_radix_cachep)
4533 kmem_cache_destroy(btrfs_bit_radix_cachep);
4534 if (btrfs_path_cachep)
4535 kmem_cache_destroy(btrfs_path_cachep);
4538 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
4539 unsigned long extra_flags,
4540 void (*ctor)(void *))
4542 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
4543 SLAB_MEM_SPREAD | extra_flags), ctor);
4546 int btrfs_init_cachep(void)
4548 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
4549 sizeof(struct btrfs_inode),
4551 if (!btrfs_inode_cachep)
4553 btrfs_trans_handle_cachep =
4554 btrfs_cache_create("btrfs_trans_handle_cache",
4555 sizeof(struct btrfs_trans_handle),
4557 if (!btrfs_trans_handle_cachep)
4559 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
4560 sizeof(struct btrfs_transaction),
4562 if (!btrfs_transaction_cachep)
4564 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
4565 sizeof(struct btrfs_path),
4567 if (!btrfs_path_cachep)
4569 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
4570 SLAB_DESTROY_BY_RCU, NULL);
4571 if (!btrfs_bit_radix_cachep)
4575 btrfs_destroy_cachep();
4579 static int btrfs_getattr(struct vfsmount *mnt,
4580 struct dentry *dentry, struct kstat *stat)
4582 struct inode *inode = dentry->d_inode;
4583 generic_fillattr(inode, stat);
4584 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
4585 stat->blksize = PAGE_CACHE_SIZE;
4586 stat->blocks = (inode_get_bytes(inode) +
4587 BTRFS_I(inode)->delalloc_bytes) >> 9;
4591 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4592 struct inode *new_dir, struct dentry *new_dentry)
4594 struct btrfs_trans_handle *trans;
4595 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4596 struct inode *new_inode = new_dentry->d_inode;
4597 struct inode *old_inode = old_dentry->d_inode;
4598 struct timespec ctime = CURRENT_TIME;
4602 /* we're not allowed to rename between subvolumes */
4603 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4604 BTRFS_I(new_dir)->root->root_key.objectid)
4607 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4608 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4612 /* to rename a snapshot or subvolume, we need to juggle the
4613 * backrefs. This isn't coded yet
4615 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4618 ret = btrfs_check_free_space(root, 1, 0);
4622 trans = btrfs_start_transaction(root, 1);
4624 btrfs_set_trans_block_group(trans, new_dir);
4626 btrfs_inc_nlink(old_dentry->d_inode);
4627 old_dir->i_ctime = old_dir->i_mtime = ctime;
4628 new_dir->i_ctime = new_dir->i_mtime = ctime;
4629 old_inode->i_ctime = ctime;
4631 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4632 old_dentry->d_name.name,
4633 old_dentry->d_name.len);
4638 new_inode->i_ctime = CURRENT_TIME;
4639 ret = btrfs_unlink_inode(trans, root, new_dir,
4640 new_dentry->d_inode,
4641 new_dentry->d_name.name,
4642 new_dentry->d_name.len);
4645 if (new_inode->i_nlink == 0) {
4646 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4652 ret = btrfs_set_inode_index(new_dir, &index);
4656 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4657 old_inode, new_dentry->d_name.name,
4658 new_dentry->d_name.len, 1, index);
4663 btrfs_end_transaction_throttle(trans, root);
4669 * some fairly slow code that needs optimization. This walks the list
4670 * of all the inodes with pending delalloc and forces them to disk.
4672 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4674 struct list_head *head = &root->fs_info->delalloc_inodes;
4675 struct btrfs_inode *binode;
4676 struct inode *inode;
4678 if (root->fs_info->sb->s_flags & MS_RDONLY)
4681 spin_lock(&root->fs_info->delalloc_lock);
4682 while (!list_empty(head)) {
4683 binode = list_entry(head->next, struct btrfs_inode,
4685 inode = igrab(&binode->vfs_inode);
4687 list_del_init(&binode->delalloc_inodes);
4688 spin_unlock(&root->fs_info->delalloc_lock);
4690 filemap_flush(inode->i_mapping);
4694 spin_lock(&root->fs_info->delalloc_lock);
4696 spin_unlock(&root->fs_info->delalloc_lock);
4698 /* the filemap_flush will queue IO into the worker threads, but
4699 * we have to make sure the IO is actually started and that
4700 * ordered extents get created before we return
4702 atomic_inc(&root->fs_info->async_submit_draining);
4703 while (atomic_read(&root->fs_info->nr_async_submits) ||
4704 atomic_read(&root->fs_info->async_delalloc_pages)) {
4705 wait_event(root->fs_info->async_submit_wait,
4706 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4707 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
4709 atomic_dec(&root->fs_info->async_submit_draining);
4713 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4714 const char *symname)
4716 struct btrfs_trans_handle *trans;
4717 struct btrfs_root *root = BTRFS_I(dir)->root;
4718 struct btrfs_path *path;
4719 struct btrfs_key key;
4720 struct inode *inode = NULL;
4728 struct btrfs_file_extent_item *ei;
4729 struct extent_buffer *leaf;
4730 unsigned long nr = 0;
4732 name_len = strlen(symname) + 1;
4733 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4734 return -ENAMETOOLONG;
4736 err = btrfs_check_free_space(root, 1, 0);
4740 trans = btrfs_start_transaction(root, 1);
4741 btrfs_set_trans_block_group(trans, dir);
4743 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4749 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4751 dentry->d_parent->d_inode->i_ino, objectid,
4752 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4754 err = PTR_ERR(inode);
4758 err = btrfs_init_inode_security(inode, dir);
4764 btrfs_set_trans_block_group(trans, inode);
4765 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4769 inode->i_mapping->a_ops = &btrfs_aops;
4770 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4771 inode->i_fop = &btrfs_file_operations;
4772 inode->i_op = &btrfs_file_inode_operations;
4773 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4775 dir->i_sb->s_dirt = 1;
4776 btrfs_update_inode_block_group(trans, inode);
4777 btrfs_update_inode_block_group(trans, dir);
4781 path = btrfs_alloc_path();
4783 key.objectid = inode->i_ino;
4785 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4786 datasize = btrfs_file_extent_calc_inline_size(name_len);
4787 err = btrfs_insert_empty_item(trans, root, path, &key,
4793 leaf = path->nodes[0];
4794 ei = btrfs_item_ptr(leaf, path->slots[0],
4795 struct btrfs_file_extent_item);
4796 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4797 btrfs_set_file_extent_type(leaf, ei,
4798 BTRFS_FILE_EXTENT_INLINE);
4799 btrfs_set_file_extent_encryption(leaf, ei, 0);
4800 btrfs_set_file_extent_compression(leaf, ei, 0);
4801 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4802 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4804 ptr = btrfs_file_extent_inline_start(ei);
4805 write_extent_buffer(leaf, symname, ptr, name_len);
4806 btrfs_mark_buffer_dirty(leaf);
4807 btrfs_free_path(path);
4809 inode->i_op = &btrfs_symlink_inode_operations;
4810 inode->i_mapping->a_ops = &btrfs_symlink_aops;
4811 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4812 inode_set_bytes(inode, name_len);
4813 btrfs_i_size_write(inode, name_len - 1);
4814 err = btrfs_update_inode(trans, root, inode);
4819 nr = trans->blocks_used;
4820 btrfs_end_transaction_throttle(trans, root);
4823 inode_dec_link_count(inode);
4826 btrfs_btree_balance_dirty(root, nr);
4830 static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4831 u64 alloc_hint, int mode)
4833 struct btrfs_trans_handle *trans;
4834 struct btrfs_root *root = BTRFS_I(inode)->root;
4835 struct btrfs_key ins;
4837 u64 cur_offset = start;
4838 u64 num_bytes = end - start;
4841 trans = btrfs_join_transaction(root, 1);
4843 btrfs_set_trans_block_group(trans, inode);
4845 while (num_bytes > 0) {
4846 alloc_size = min(num_bytes, root->fs_info->max_extent);
4847 ret = btrfs_reserve_extent(trans, root, alloc_size,
4848 root->sectorsize, 0, alloc_hint,
4854 ret = insert_reserved_file_extent(trans, inode,
4855 cur_offset, ins.objectid,
4856 ins.offset, ins.offset,
4857 ins.offset, 0, 0, 0,
4858 BTRFS_FILE_EXTENT_PREALLOC);
4860 num_bytes -= ins.offset;
4861 cur_offset += ins.offset;
4862 alloc_hint = ins.objectid + ins.offset;
4865 if (cur_offset > start) {
4866 inode->i_ctime = CURRENT_TIME;
4867 btrfs_set_flag(inode, PREALLOC);
4868 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4869 cur_offset > i_size_read(inode))
4870 btrfs_i_size_write(inode, cur_offset);
4871 ret = btrfs_update_inode(trans, root, inode);
4875 btrfs_end_transaction(trans, root);
4879 static long btrfs_fallocate(struct inode *inode, int mode,
4880 loff_t offset, loff_t len)
4887 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4888 struct extent_map *em;
4891 alloc_start = offset & ~mask;
4892 alloc_end = (offset + len + mask) & ~mask;
4894 mutex_lock(&inode->i_mutex);
4895 if (alloc_start > inode->i_size) {
4896 ret = btrfs_cont_expand(inode, alloc_start);
4902 struct btrfs_ordered_extent *ordered;
4903 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4904 alloc_end - 1, GFP_NOFS);
4905 ordered = btrfs_lookup_first_ordered_extent(inode,
4908 ordered->file_offset + ordered->len > alloc_start &&
4909 ordered->file_offset < alloc_end) {
4910 btrfs_put_ordered_extent(ordered);
4911 unlock_extent(&BTRFS_I(inode)->io_tree,
4912 alloc_start, alloc_end - 1, GFP_NOFS);
4913 btrfs_wait_ordered_range(inode, alloc_start,
4914 alloc_end - alloc_start);
4917 btrfs_put_ordered_extent(ordered);
4922 cur_offset = alloc_start;
4924 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4925 alloc_end - cur_offset, 0);
4926 BUG_ON(IS_ERR(em) || !em);
4927 last_byte = min(extent_map_end(em), alloc_end);
4928 last_byte = (last_byte + mask) & ~mask;
4929 if (em->block_start == EXTENT_MAP_HOLE) {
4930 ret = prealloc_file_range(inode, cur_offset,
4931 last_byte, alloc_hint, mode);
4933 free_extent_map(em);
4937 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4938 alloc_hint = em->block_start;
4939 free_extent_map(em);
4941 cur_offset = last_byte;
4942 if (cur_offset >= alloc_end) {
4947 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4950 mutex_unlock(&inode->i_mutex);
4954 static int btrfs_set_page_dirty(struct page *page)
4956 return __set_page_dirty_nobuffers(page);
4959 static int btrfs_permission(struct inode *inode, int mask)
4961 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4963 return generic_permission(inode, mask, btrfs_check_acl);
4966 static struct inode_operations btrfs_dir_inode_operations = {
4967 .getattr = btrfs_getattr,
4968 .lookup = btrfs_lookup,
4969 .create = btrfs_create,
4970 .unlink = btrfs_unlink,
4972 .mkdir = btrfs_mkdir,
4973 .rmdir = btrfs_rmdir,
4974 .rename = btrfs_rename,
4975 .symlink = btrfs_symlink,
4976 .setattr = btrfs_setattr,
4977 .mknod = btrfs_mknod,
4978 .setxattr = btrfs_setxattr,
4979 .getxattr = btrfs_getxattr,
4980 .listxattr = btrfs_listxattr,
4981 .removexattr = btrfs_removexattr,
4982 .permission = btrfs_permission,
4984 static struct inode_operations btrfs_dir_ro_inode_operations = {
4985 .lookup = btrfs_lookup,
4986 .permission = btrfs_permission,
4988 static struct file_operations btrfs_dir_file_operations = {
4989 .llseek = generic_file_llseek,
4990 .read = generic_read_dir,
4991 .readdir = btrfs_real_readdir,
4992 .unlocked_ioctl = btrfs_ioctl,
4993 #ifdef CONFIG_COMPAT
4994 .compat_ioctl = btrfs_ioctl,
4996 .release = btrfs_release_file,
4997 .fsync = btrfs_sync_file,
5000 static struct extent_io_ops btrfs_extent_io_ops = {
5001 .fill_delalloc = run_delalloc_range,
5002 .submit_bio_hook = btrfs_submit_bio_hook,
5003 .merge_bio_hook = btrfs_merge_bio_hook,
5004 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
5005 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
5006 .writepage_start_hook = btrfs_writepage_start_hook,
5007 .readpage_io_failed_hook = btrfs_io_failed_hook,
5008 .set_bit_hook = btrfs_set_bit_hook,
5009 .clear_bit_hook = btrfs_clear_bit_hook,
5013 * btrfs doesn't support the bmap operation because swapfiles
5014 * use bmap to make a mapping of extents in the file. They assume
5015 * these extents won't change over the life of the file and they
5016 * use the bmap result to do IO directly to the drive.
5018 * the btrfs bmap call would return logical addresses that aren't
5019 * suitable for IO and they also will change frequently as COW
5020 * operations happen. So, swapfile + btrfs == corruption.
5022 * For now we're avoiding this by dropping bmap.
5024 static struct address_space_operations btrfs_aops = {
5025 .readpage = btrfs_readpage,
5026 .writepage = btrfs_writepage,
5027 .writepages = btrfs_writepages,
5028 .readpages = btrfs_readpages,
5029 .sync_page = block_sync_page,
5030 .direct_IO = btrfs_direct_IO,
5031 .invalidatepage = btrfs_invalidatepage,
5032 .releasepage = btrfs_releasepage,
5033 .set_page_dirty = btrfs_set_page_dirty,
5036 static struct address_space_operations btrfs_symlink_aops = {
5037 .readpage = btrfs_readpage,
5038 .writepage = btrfs_writepage,
5039 .invalidatepage = btrfs_invalidatepage,
5040 .releasepage = btrfs_releasepage,
5043 static struct inode_operations btrfs_file_inode_operations = {
5044 .truncate = btrfs_truncate,
5045 .getattr = btrfs_getattr,
5046 .setattr = btrfs_setattr,
5047 .setxattr = btrfs_setxattr,
5048 .getxattr = btrfs_getxattr,
5049 .listxattr = btrfs_listxattr,
5050 .removexattr = btrfs_removexattr,
5051 .permission = btrfs_permission,
5052 .fallocate = btrfs_fallocate,
5053 .fiemap = btrfs_fiemap,
5055 static struct inode_operations btrfs_special_inode_operations = {
5056 .getattr = btrfs_getattr,
5057 .setattr = btrfs_setattr,
5058 .permission = btrfs_permission,
5059 .setxattr = btrfs_setxattr,
5060 .getxattr = btrfs_getxattr,
5061 .listxattr = btrfs_listxattr,
5062 .removexattr = btrfs_removexattr,
5064 static struct inode_operations btrfs_symlink_inode_operations = {
5065 .readlink = generic_readlink,
5066 .follow_link = page_follow_link_light,
5067 .put_link = page_put_link,
5068 .permission = btrfs_permission,
5069 .setxattr = btrfs_setxattr,
5070 .getxattr = btrfs_getxattr,
5071 .listxattr = btrfs_listxattr,
5072 .removexattr = btrfs_removexattr,