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/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
44 #include <linux/posix_acl_xattr.h>
45 #include <linux/uio.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
51 #include "ordered-data.h"
55 #include "compression.h"
57 #include "free-space-cache.h"
58 #include "inode-map.h"
64 struct btrfs_iget_args {
65 struct btrfs_key *location;
66 struct btrfs_root *root;
69 struct btrfs_dio_data {
70 u64 outstanding_extents;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
76 static const struct inode_operations btrfs_dir_inode_operations;
77 static const struct inode_operations btrfs_symlink_inode_operations;
78 static const struct inode_operations btrfs_dir_ro_inode_operations;
79 static const struct inode_operations btrfs_special_inode_operations;
80 static const struct inode_operations btrfs_file_inode_operations;
81 static const struct address_space_operations btrfs_aops;
82 static const struct address_space_operations btrfs_symlink_aops;
83 static const struct file_operations btrfs_dir_file_operations;
84 static struct extent_io_ops btrfs_extent_io_ops;
86 static struct kmem_cache *btrfs_inode_cachep;
87 static struct kmem_cache *btrfs_delalloc_work_cachep;
88 struct kmem_cache *btrfs_trans_handle_cachep;
89 struct kmem_cache *btrfs_transaction_cachep;
90 struct kmem_cache *btrfs_path_cachep;
91 struct kmem_cache *btrfs_free_space_cachep;
94 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
95 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
104 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
105 static int btrfs_truncate(struct inode *inode);
106 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
107 static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
109 u64 start, u64 end, int *page_started,
110 unsigned long *nr_written, int unlock);
111 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
112 u64 len, u64 orig_start,
113 u64 block_start, u64 block_len,
114 u64 orig_block_len, u64 ram_bytes,
117 static int btrfs_dirty_inode(struct inode *inode);
119 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120 void btrfs_test_inode_set_ops(struct inode *inode)
122 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
126 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
127 struct inode *inode, struct inode *dir,
128 const struct qstr *qstr)
132 err = btrfs_init_acl(trans, inode, dir);
134 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
143 static int insert_inline_extent(struct btrfs_trans_handle *trans,
144 struct btrfs_path *path, int extent_inserted,
145 struct btrfs_root *root, struct inode *inode,
146 u64 start, size_t size, size_t compressed_size,
148 struct page **compressed_pages)
150 struct extent_buffer *leaf;
151 struct page *page = NULL;
154 struct btrfs_file_extent_item *ei;
157 size_t cur_size = size;
158 unsigned long offset;
160 if (compressed_size && compressed_pages)
161 cur_size = compressed_size;
163 inode_add_bytes(inode, size);
165 if (!extent_inserted) {
166 struct btrfs_key key;
169 key.objectid = btrfs_ino(inode);
171 key.type = BTRFS_EXTENT_DATA_KEY;
173 datasize = btrfs_file_extent_calc_inline_size(cur_size);
174 path->leave_spinning = 1;
175 ret = btrfs_insert_empty_item(trans, root, path, &key,
182 leaf = path->nodes[0];
183 ei = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
186 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
187 btrfs_set_file_extent_encryption(leaf, ei, 0);
188 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
189 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
190 ptr = btrfs_file_extent_inline_start(ei);
192 if (compress_type != BTRFS_COMPRESS_NONE) {
195 while (compressed_size > 0) {
196 cpage = compressed_pages[i];
197 cur_size = min_t(unsigned long, compressed_size,
200 kaddr = kmap_atomic(cpage);
201 write_extent_buffer(leaf, kaddr, ptr, cur_size);
202 kunmap_atomic(kaddr);
206 compressed_size -= cur_size;
208 btrfs_set_file_extent_compression(leaf, ei,
211 page = find_get_page(inode->i_mapping,
212 start >> PAGE_CACHE_SHIFT);
213 btrfs_set_file_extent_compression(leaf, ei, 0);
214 kaddr = kmap_atomic(page);
215 offset = start & (PAGE_CACHE_SIZE - 1);
216 write_extent_buffer(leaf, kaddr + offset, ptr, size);
217 kunmap_atomic(kaddr);
218 page_cache_release(page);
220 btrfs_mark_buffer_dirty(leaf);
221 btrfs_release_path(path);
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
232 BTRFS_I(inode)->disk_i_size = inode->i_size;
233 ret = btrfs_update_inode(trans, root, inode);
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
246 static noinline int cow_file_range_inline(struct btrfs_root *root,
247 struct inode *inode, u64 start,
248 u64 end, size_t compressed_size,
250 struct page **compressed_pages)
252 struct btrfs_trans_handle *trans;
253 u64 isize = i_size_read(inode);
254 u64 actual_end = min(end + 1, isize);
255 u64 inline_len = actual_end - start;
256 u64 aligned_end = ALIGN(end, root->sectorsize);
257 u64 data_len = inline_len;
259 struct btrfs_path *path;
260 int extent_inserted = 0;
261 u32 extent_item_size;
264 data_len = compressed_size;
267 actual_end > PAGE_CACHE_SIZE ||
268 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
270 (actual_end & (root->sectorsize - 1)) == 0) ||
272 data_len > root->fs_info->max_inline) {
276 path = btrfs_alloc_path();
280 trans = btrfs_join_transaction(root);
282 btrfs_free_path(path);
283 return PTR_ERR(trans);
285 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
287 if (compressed_size && compressed_pages)
288 extent_item_size = btrfs_file_extent_calc_inline_size(
291 extent_item_size = btrfs_file_extent_calc_inline_size(
294 ret = __btrfs_drop_extents(trans, root, inode, path,
295 start, aligned_end, NULL,
296 1, 1, extent_item_size, &extent_inserted);
298 btrfs_abort_transaction(trans, root, ret);
302 if (isize > actual_end)
303 inline_len = min_t(u64, isize, actual_end);
304 ret = insert_inline_extent(trans, path, extent_inserted,
306 inline_len, compressed_size,
307 compress_type, compressed_pages);
308 if (ret && ret != -ENOSPC) {
309 btrfs_abort_transaction(trans, root, ret);
311 } else if (ret == -ENOSPC) {
316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
317 btrfs_delalloc_release_metadata(inode, end + 1 - start);
318 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
321 * Don't forget to free the reserved space, as for inlined extent
322 * it won't count as data extent, free them directly here.
323 * And at reserve time, it's always aligned to page size, so
324 * just free one page here.
326 btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE);
327 btrfs_free_path(path);
328 btrfs_end_transaction(trans, root);
332 struct async_extent {
337 unsigned long nr_pages;
339 struct list_head list;
344 struct btrfs_root *root;
345 struct page *locked_page;
348 struct list_head extents;
349 struct btrfs_work work;
352 static noinline int add_async_extent(struct async_cow *cow,
353 u64 start, u64 ram_size,
356 unsigned long nr_pages,
359 struct async_extent *async_extent;
361 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
362 BUG_ON(!async_extent); /* -ENOMEM */
363 async_extent->start = start;
364 async_extent->ram_size = ram_size;
365 async_extent->compressed_size = compressed_size;
366 async_extent->pages = pages;
367 async_extent->nr_pages = nr_pages;
368 async_extent->compress_type = compress_type;
369 list_add_tail(&async_extent->list, &cow->extents);
373 static inline int inode_need_compress(struct inode *inode)
375 struct btrfs_root *root = BTRFS_I(inode)->root;
378 if (btrfs_test_opt(root, FORCE_COMPRESS))
380 /* bad compression ratios */
381 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 if (btrfs_test_opt(root, COMPRESS) ||
384 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
385 BTRFS_I(inode)->force_compress)
391 * we create compressed extents in two phases. The first
392 * phase compresses a range of pages that have already been
393 * locked (both pages and state bits are locked).
395 * This is done inside an ordered work queue, and the compression
396 * is spread across many cpus. The actual IO submission is step
397 * two, and the ordered work queue takes care of making sure that
398 * happens in the same order things were put onto the queue by
399 * writepages and friends.
401 * If this code finds it can't get good compression, it puts an
402 * entry onto the work queue to write the uncompressed bytes. This
403 * makes sure that both compressed inodes and uncompressed inodes
404 * are written in the same order that the flusher thread sent them
407 static noinline void compress_file_range(struct inode *inode,
408 struct page *locked_page,
410 struct async_cow *async_cow,
413 struct btrfs_root *root = BTRFS_I(inode)->root;
415 u64 blocksize = root->sectorsize;
417 u64 isize = i_size_read(inode);
419 struct page **pages = NULL;
420 unsigned long nr_pages;
421 unsigned long nr_pages_ret = 0;
422 unsigned long total_compressed = 0;
423 unsigned long total_in = 0;
424 unsigned long max_compressed = 128 * 1024;
425 unsigned long max_uncompressed = 128 * 1024;
428 int compress_type = root->fs_info->compress_type;
431 /* if this is a small write inside eof, kick off a defrag */
432 if ((end - start + 1) < 16 * 1024 &&
433 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
434 btrfs_add_inode_defrag(NULL, inode);
436 actual_end = min_t(u64, isize, end + 1);
439 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
440 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
443 * we don't want to send crud past the end of i_size through
444 * compression, that's just a waste of CPU time. So, if the
445 * end of the file is before the start of our current
446 * requested range of bytes, we bail out to the uncompressed
447 * cleanup code that can deal with all of this.
449 * It isn't really the fastest way to fix things, but this is a
450 * very uncommon corner.
452 if (actual_end <= start)
453 goto cleanup_and_bail_uncompressed;
455 total_compressed = actual_end - start;
458 * skip compression for a small file range(<=blocksize) that
459 * isn't an inline extent, since it dosen't save disk space at all.
461 if (total_compressed <= blocksize &&
462 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
463 goto cleanup_and_bail_uncompressed;
465 /* we want to make sure that amount of ram required to uncompress
466 * an extent is reasonable, so we limit the total size in ram
467 * of a compressed extent to 128k. This is a crucial number
468 * because it also controls how easily we can spread reads across
469 * cpus for decompression.
471 * We also want to make sure the amount of IO required to do
472 * a random read is reasonably small, so we limit the size of
473 * a compressed extent to 128k.
475 total_compressed = min(total_compressed, max_uncompressed);
476 num_bytes = ALIGN(end - start + 1, blocksize);
477 num_bytes = max(blocksize, num_bytes);
482 * we do compression for mount -o compress and when the
483 * inode has not been flagged as nocompress. This flag can
484 * change at any time if we discover bad compression ratios.
486 if (inode_need_compress(inode)) {
488 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
490 /* just bail out to the uncompressed code */
494 if (BTRFS_I(inode)->force_compress)
495 compress_type = BTRFS_I(inode)->force_compress;
498 * we need to call clear_page_dirty_for_io on each
499 * page in the range. Otherwise applications with the file
500 * mmap'd can wander in and change the page contents while
501 * we are compressing them.
503 * If the compression fails for any reason, we set the pages
504 * dirty again later on.
506 extent_range_clear_dirty_for_io(inode, start, end);
508 ret = btrfs_compress_pages(compress_type,
509 inode->i_mapping, start,
510 total_compressed, pages,
511 nr_pages, &nr_pages_ret,
517 unsigned long offset = total_compressed &
518 (PAGE_CACHE_SIZE - 1);
519 struct page *page = pages[nr_pages_ret - 1];
522 /* zero the tail end of the last page, we might be
523 * sending it down to disk
526 kaddr = kmap_atomic(page);
527 memset(kaddr + offset, 0,
528 PAGE_CACHE_SIZE - offset);
529 kunmap_atomic(kaddr);
536 /* lets try to make an inline extent */
537 if (ret || total_in < (actual_end - start)) {
538 /* we didn't compress the entire range, try
539 * to make an uncompressed inline extent.
541 ret = cow_file_range_inline(root, inode, start, end,
544 /* try making a compressed inline extent */
545 ret = cow_file_range_inline(root, inode, start, end,
547 compress_type, pages);
550 unsigned long clear_flags = EXTENT_DELALLOC |
552 unsigned long page_error_op;
554 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
555 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
558 * inline extent creation worked or returned error,
559 * we don't need to create any more async work items.
560 * Unlock and free up our temp pages.
562 extent_clear_unlock_delalloc(inode, start, end, NULL,
563 clear_flags, PAGE_UNLOCK |
574 * we aren't doing an inline extent round the compressed size
575 * up to a block size boundary so the allocator does sane
578 total_compressed = ALIGN(total_compressed, blocksize);
581 * one last check to make sure the compression is really a
582 * win, compare the page count read with the blocks on disk
584 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
585 if (total_compressed >= total_in) {
588 num_bytes = total_in;
591 if (!will_compress && pages) {
593 * the compression code ran but failed to make things smaller,
594 * free any pages it allocated and our page pointer array
596 for (i = 0; i < nr_pages_ret; i++) {
597 WARN_ON(pages[i]->mapping);
598 page_cache_release(pages[i]);
602 total_compressed = 0;
605 /* flag the file so we don't compress in the future */
606 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
607 !(BTRFS_I(inode)->force_compress)) {
608 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
614 /* the async work queues will take care of doing actual
615 * allocation on disk for these compressed pages,
616 * and will submit them to the elevator.
618 add_async_extent(async_cow, start, num_bytes,
619 total_compressed, pages, nr_pages_ret,
622 if (start + num_bytes < end) {
629 cleanup_and_bail_uncompressed:
631 * No compression, but we still need to write the pages in
632 * the file we've been given so far. redirty the locked
633 * page if it corresponds to our extent and set things up
634 * for the async work queue to run cow_file_range to do
635 * the normal delalloc dance
637 if (page_offset(locked_page) >= start &&
638 page_offset(locked_page) <= end) {
639 __set_page_dirty_nobuffers(locked_page);
640 /* unlocked later on in the async handlers */
643 extent_range_redirty_for_io(inode, start, end);
644 add_async_extent(async_cow, start, end - start + 1,
645 0, NULL, 0, BTRFS_COMPRESS_NONE);
652 for (i = 0; i < nr_pages_ret; i++) {
653 WARN_ON(pages[i]->mapping);
654 page_cache_release(pages[i]);
659 static void free_async_extent_pages(struct async_extent *async_extent)
663 if (!async_extent->pages)
666 for (i = 0; i < async_extent->nr_pages; i++) {
667 WARN_ON(async_extent->pages[i]->mapping);
668 page_cache_release(async_extent->pages[i]);
670 kfree(async_extent->pages);
671 async_extent->nr_pages = 0;
672 async_extent->pages = NULL;
676 * phase two of compressed writeback. This is the ordered portion
677 * of the code, which only gets called in the order the work was
678 * queued. We walk all the async extents created by compress_file_range
679 * and send them down to the disk.
681 static noinline void submit_compressed_extents(struct inode *inode,
682 struct async_cow *async_cow)
684 struct async_extent *async_extent;
686 struct btrfs_key ins;
687 struct extent_map *em;
688 struct btrfs_root *root = BTRFS_I(inode)->root;
689 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
690 struct extent_io_tree *io_tree;
694 while (!list_empty(&async_cow->extents)) {
695 async_extent = list_entry(async_cow->extents.next,
696 struct async_extent, list);
697 list_del(&async_extent->list);
699 io_tree = &BTRFS_I(inode)->io_tree;
702 /* did the compression code fall back to uncompressed IO? */
703 if (!async_extent->pages) {
704 int page_started = 0;
705 unsigned long nr_written = 0;
707 lock_extent(io_tree, async_extent->start,
708 async_extent->start +
709 async_extent->ram_size - 1);
711 /* allocate blocks */
712 ret = cow_file_range(inode, async_cow->locked_page,
714 async_extent->start +
715 async_extent->ram_size - 1,
716 &page_started, &nr_written, 0);
721 * if page_started, cow_file_range inserted an
722 * inline extent and took care of all the unlocking
723 * and IO for us. Otherwise, we need to submit
724 * all those pages down to the drive.
726 if (!page_started && !ret)
727 extent_write_locked_range(io_tree,
728 inode, async_extent->start,
729 async_extent->start +
730 async_extent->ram_size - 1,
734 unlock_page(async_cow->locked_page);
740 lock_extent(io_tree, async_extent->start,
741 async_extent->start + async_extent->ram_size - 1);
743 ret = btrfs_reserve_extent(root,
744 async_extent->compressed_size,
745 async_extent->compressed_size,
746 0, alloc_hint, &ins, 1, 1);
748 free_async_extent_pages(async_extent);
750 if (ret == -ENOSPC) {
751 unlock_extent(io_tree, async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1);
756 * we need to redirty the pages if we decide to
757 * fallback to uncompressed IO, otherwise we
758 * will not submit these pages down to lower
761 extent_range_redirty_for_io(inode,
763 async_extent->start +
764 async_extent->ram_size - 1);
771 * here we're doing allocation and writeback of the
774 btrfs_drop_extent_cache(inode, async_extent->start,
775 async_extent->start +
776 async_extent->ram_size - 1, 0);
778 em = alloc_extent_map();
781 goto out_free_reserve;
783 em->start = async_extent->start;
784 em->len = async_extent->ram_size;
785 em->orig_start = em->start;
786 em->mod_start = em->start;
787 em->mod_len = em->len;
789 em->block_start = ins.objectid;
790 em->block_len = ins.offset;
791 em->orig_block_len = ins.offset;
792 em->ram_bytes = async_extent->ram_size;
793 em->bdev = root->fs_info->fs_devices->latest_bdev;
794 em->compress_type = async_extent->compress_type;
795 set_bit(EXTENT_FLAG_PINNED, &em->flags);
796 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
800 write_lock(&em_tree->lock);
801 ret = add_extent_mapping(em_tree, em, 1);
802 write_unlock(&em_tree->lock);
803 if (ret != -EEXIST) {
807 btrfs_drop_extent_cache(inode, async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1, 0);
813 goto out_free_reserve;
815 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->ram_size,
820 BTRFS_ORDERED_COMPRESSED,
821 async_extent->compress_type);
823 btrfs_drop_extent_cache(inode, async_extent->start,
824 async_extent->start +
825 async_extent->ram_size - 1, 0);
826 goto out_free_reserve;
830 * clear dirty, set writeback and unlock the pages.
832 extent_clear_unlock_delalloc(inode, async_extent->start,
833 async_extent->start +
834 async_extent->ram_size - 1,
835 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
836 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
838 ret = btrfs_submit_compressed_write(inode,
840 async_extent->ram_size,
842 ins.offset, async_extent->pages,
843 async_extent->nr_pages);
845 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
846 struct page *p = async_extent->pages[0];
847 const u64 start = async_extent->start;
848 const u64 end = start + async_extent->ram_size - 1;
850 p->mapping = inode->i_mapping;
851 tree->ops->writepage_end_io_hook(p, start, end,
854 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
857 free_async_extent_pages(async_extent);
859 alloc_hint = ins.objectid + ins.offset;
865 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
867 extent_clear_unlock_delalloc(inode, async_extent->start,
868 async_extent->start +
869 async_extent->ram_size - 1,
870 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
871 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
872 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
873 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
875 free_async_extent_pages(async_extent);
880 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
883 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
884 struct extent_map *em;
887 read_lock(&em_tree->lock);
888 em = search_extent_mapping(em_tree, start, num_bytes);
891 * if block start isn't an actual block number then find the
892 * first block in this inode and use that as a hint. If that
893 * block is also bogus then just don't worry about it.
895 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
897 em = search_extent_mapping(em_tree, 0, 0);
898 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
899 alloc_hint = em->block_start;
903 alloc_hint = em->block_start;
907 read_unlock(&em_tree->lock);
913 * when extent_io.c finds a delayed allocation range in the file,
914 * the call backs end up in this code. The basic idea is to
915 * allocate extents on disk for the range, and create ordered data structs
916 * in ram to track those extents.
918 * locked_page is the page that writepage had locked already. We use
919 * it to make sure we don't do extra locks or unlocks.
921 * *page_started is set to one if we unlock locked_page and do everything
922 * required to start IO on it. It may be clean and already done with
925 static noinline int cow_file_range(struct inode *inode,
926 struct page *locked_page,
927 u64 start, u64 end, int *page_started,
928 unsigned long *nr_written,
931 struct btrfs_root *root = BTRFS_I(inode)->root;
934 unsigned long ram_size;
937 u64 blocksize = root->sectorsize;
938 struct btrfs_key ins;
939 struct extent_map *em;
940 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
943 if (btrfs_is_free_space_inode(inode)) {
949 num_bytes = ALIGN(end - start + 1, blocksize);
950 num_bytes = max(blocksize, num_bytes);
951 disk_num_bytes = num_bytes;
953 /* if this is a small write inside eof, kick off defrag */
954 if (num_bytes < 64 * 1024 &&
955 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
956 btrfs_add_inode_defrag(NULL, inode);
959 /* lets try to make an inline extent */
960 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
963 extent_clear_unlock_delalloc(inode, start, end, NULL,
964 EXTENT_LOCKED | EXTENT_DELALLOC |
965 EXTENT_DEFRAG, PAGE_UNLOCK |
966 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
969 *nr_written = *nr_written +
970 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
973 } else if (ret < 0) {
978 BUG_ON(disk_num_bytes >
979 btrfs_super_total_bytes(root->fs_info->super_copy));
981 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
982 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
984 while (disk_num_bytes > 0) {
987 cur_alloc_size = disk_num_bytes;
988 ret = btrfs_reserve_extent(root, cur_alloc_size,
989 root->sectorsize, 0, alloc_hint,
994 em = alloc_extent_map();
1000 em->orig_start = em->start;
1001 ram_size = ins.offset;
1002 em->len = ins.offset;
1003 em->mod_start = em->start;
1004 em->mod_len = em->len;
1006 em->block_start = ins.objectid;
1007 em->block_len = ins.offset;
1008 em->orig_block_len = ins.offset;
1009 em->ram_bytes = ram_size;
1010 em->bdev = root->fs_info->fs_devices->latest_bdev;
1011 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1012 em->generation = -1;
1015 write_lock(&em_tree->lock);
1016 ret = add_extent_mapping(em_tree, em, 1);
1017 write_unlock(&em_tree->lock);
1018 if (ret != -EEXIST) {
1019 free_extent_map(em);
1022 btrfs_drop_extent_cache(inode, start,
1023 start + ram_size - 1, 0);
1028 cur_alloc_size = ins.offset;
1029 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1030 ram_size, cur_alloc_size, 0);
1032 goto out_drop_extent_cache;
1034 if (root->root_key.objectid ==
1035 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1036 ret = btrfs_reloc_clone_csums(inode, start,
1039 goto out_drop_extent_cache;
1042 if (disk_num_bytes < cur_alloc_size)
1045 /* we're not doing compressed IO, don't unlock the first
1046 * page (which the caller expects to stay locked), don't
1047 * clear any dirty bits and don't set any writeback bits
1049 * Do set the Private2 bit so we know this page was properly
1050 * setup for writepage
1052 op = unlock ? PAGE_UNLOCK : 0;
1053 op |= PAGE_SET_PRIVATE2;
1055 extent_clear_unlock_delalloc(inode, start,
1056 start + ram_size - 1, locked_page,
1057 EXTENT_LOCKED | EXTENT_DELALLOC,
1059 disk_num_bytes -= cur_alloc_size;
1060 num_bytes -= cur_alloc_size;
1061 alloc_hint = ins.objectid + ins.offset;
1062 start += cur_alloc_size;
1067 out_drop_extent_cache:
1068 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1070 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
1072 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1073 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1074 EXTENT_DELALLOC | EXTENT_DEFRAG,
1075 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1076 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1081 * work queue call back to started compression on a file and pages
1083 static noinline void async_cow_start(struct btrfs_work *work)
1085 struct async_cow *async_cow;
1087 async_cow = container_of(work, struct async_cow, work);
1089 compress_file_range(async_cow->inode, async_cow->locked_page,
1090 async_cow->start, async_cow->end, async_cow,
1092 if (num_added == 0) {
1093 btrfs_add_delayed_iput(async_cow->inode);
1094 async_cow->inode = NULL;
1099 * work queue call back to submit previously compressed pages
1101 static noinline void async_cow_submit(struct btrfs_work *work)
1103 struct async_cow *async_cow;
1104 struct btrfs_root *root;
1105 unsigned long nr_pages;
1107 async_cow = container_of(work, struct async_cow, work);
1109 root = async_cow->root;
1110 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1114 * atomic_sub_return implies a barrier for waitqueue_active
1116 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
1118 waitqueue_active(&root->fs_info->async_submit_wait))
1119 wake_up(&root->fs_info->async_submit_wait);
1121 if (async_cow->inode)
1122 submit_compressed_extents(async_cow->inode, async_cow);
1125 static noinline void async_cow_free(struct btrfs_work *work)
1127 struct async_cow *async_cow;
1128 async_cow = container_of(work, struct async_cow, work);
1129 if (async_cow->inode)
1130 btrfs_add_delayed_iput(async_cow->inode);
1134 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1135 u64 start, u64 end, int *page_started,
1136 unsigned long *nr_written)
1138 struct async_cow *async_cow;
1139 struct btrfs_root *root = BTRFS_I(inode)->root;
1140 unsigned long nr_pages;
1142 int limit = 10 * 1024 * 1024;
1144 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1145 1, 0, NULL, GFP_NOFS);
1146 while (start < end) {
1147 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1148 BUG_ON(!async_cow); /* -ENOMEM */
1149 async_cow->inode = igrab(inode);
1150 async_cow->root = root;
1151 async_cow->locked_page = locked_page;
1152 async_cow->start = start;
1154 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1155 !btrfs_test_opt(root, FORCE_COMPRESS))
1158 cur_end = min(end, start + 512 * 1024 - 1);
1160 async_cow->end = cur_end;
1161 INIT_LIST_HEAD(&async_cow->extents);
1163 btrfs_init_work(&async_cow->work,
1164 btrfs_delalloc_helper,
1165 async_cow_start, async_cow_submit,
1168 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1170 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1172 btrfs_queue_work(root->fs_info->delalloc_workers,
1175 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1176 wait_event(root->fs_info->async_submit_wait,
1177 (atomic_read(&root->fs_info->async_delalloc_pages) <
1181 while (atomic_read(&root->fs_info->async_submit_draining) &&
1182 atomic_read(&root->fs_info->async_delalloc_pages)) {
1183 wait_event(root->fs_info->async_submit_wait,
1184 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1188 *nr_written += nr_pages;
1189 start = cur_end + 1;
1195 static noinline int csum_exist_in_range(struct btrfs_root *root,
1196 u64 bytenr, u64 num_bytes)
1199 struct btrfs_ordered_sum *sums;
1202 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1203 bytenr + num_bytes - 1, &list, 0);
1204 if (ret == 0 && list_empty(&list))
1207 while (!list_empty(&list)) {
1208 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1209 list_del(&sums->list);
1216 * when nowcow writeback call back. This checks for snapshots or COW copies
1217 * of the extents that exist in the file, and COWs the file as required.
1219 * If no cow copies or snapshots exist, we write directly to the existing
1222 static noinline int run_delalloc_nocow(struct inode *inode,
1223 struct page *locked_page,
1224 u64 start, u64 end, int *page_started, int force,
1225 unsigned long *nr_written)
1227 struct btrfs_root *root = BTRFS_I(inode)->root;
1228 struct btrfs_trans_handle *trans;
1229 struct extent_buffer *leaf;
1230 struct btrfs_path *path;
1231 struct btrfs_file_extent_item *fi;
1232 struct btrfs_key found_key;
1247 u64 ino = btrfs_ino(inode);
1249 path = btrfs_alloc_path();
1251 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1252 EXTENT_LOCKED | EXTENT_DELALLOC |
1253 EXTENT_DO_ACCOUNTING |
1254 EXTENT_DEFRAG, PAGE_UNLOCK |
1256 PAGE_SET_WRITEBACK |
1257 PAGE_END_WRITEBACK);
1261 nolock = btrfs_is_free_space_inode(inode);
1264 trans = btrfs_join_transaction_nolock(root);
1266 trans = btrfs_join_transaction(root);
1268 if (IS_ERR(trans)) {
1269 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1270 EXTENT_LOCKED | EXTENT_DELALLOC |
1271 EXTENT_DO_ACCOUNTING |
1272 EXTENT_DEFRAG, PAGE_UNLOCK |
1274 PAGE_SET_WRITEBACK |
1275 PAGE_END_WRITEBACK);
1276 btrfs_free_path(path);
1277 return PTR_ERR(trans);
1280 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1282 cow_start = (u64)-1;
1285 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1289 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1290 leaf = path->nodes[0];
1291 btrfs_item_key_to_cpu(leaf, &found_key,
1292 path->slots[0] - 1);
1293 if (found_key.objectid == ino &&
1294 found_key.type == BTRFS_EXTENT_DATA_KEY)
1299 leaf = path->nodes[0];
1300 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1301 ret = btrfs_next_leaf(root, path);
1306 leaf = path->nodes[0];
1312 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1314 if (found_key.objectid > ino)
1316 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1317 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1321 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1322 found_key.offset > end)
1325 if (found_key.offset > cur_offset) {
1326 extent_end = found_key.offset;
1331 fi = btrfs_item_ptr(leaf, path->slots[0],
1332 struct btrfs_file_extent_item);
1333 extent_type = btrfs_file_extent_type(leaf, fi);
1335 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1336 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1337 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1338 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1339 extent_offset = btrfs_file_extent_offset(leaf, fi);
1340 extent_end = found_key.offset +
1341 btrfs_file_extent_num_bytes(leaf, fi);
1343 btrfs_file_extent_disk_num_bytes(leaf, fi);
1344 if (extent_end <= start) {
1348 if (disk_bytenr == 0)
1350 if (btrfs_file_extent_compression(leaf, fi) ||
1351 btrfs_file_extent_encryption(leaf, fi) ||
1352 btrfs_file_extent_other_encoding(leaf, fi))
1354 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1356 if (btrfs_extent_readonly(root, disk_bytenr))
1358 if (btrfs_cross_ref_exist(trans, root, ino,
1360 extent_offset, disk_bytenr))
1362 disk_bytenr += extent_offset;
1363 disk_bytenr += cur_offset - found_key.offset;
1364 num_bytes = min(end + 1, extent_end) - cur_offset;
1366 * if there are pending snapshots for this root,
1367 * we fall into common COW way.
1370 err = btrfs_start_write_no_snapshoting(root);
1375 * force cow if csum exists in the range.
1376 * this ensure that csum for a given extent are
1377 * either valid or do not exist.
1379 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1382 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1383 extent_end = found_key.offset +
1384 btrfs_file_extent_inline_len(leaf,
1385 path->slots[0], fi);
1386 extent_end = ALIGN(extent_end, root->sectorsize);
1391 if (extent_end <= start) {
1393 if (!nolock && nocow)
1394 btrfs_end_write_no_snapshoting(root);
1398 if (cow_start == (u64)-1)
1399 cow_start = cur_offset;
1400 cur_offset = extent_end;
1401 if (cur_offset > end)
1407 btrfs_release_path(path);
1408 if (cow_start != (u64)-1) {
1409 ret = cow_file_range(inode, locked_page,
1410 cow_start, found_key.offset - 1,
1411 page_started, nr_written, 1);
1413 if (!nolock && nocow)
1414 btrfs_end_write_no_snapshoting(root);
1417 cow_start = (u64)-1;
1420 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1421 struct extent_map *em;
1422 struct extent_map_tree *em_tree;
1423 em_tree = &BTRFS_I(inode)->extent_tree;
1424 em = alloc_extent_map();
1425 BUG_ON(!em); /* -ENOMEM */
1426 em->start = cur_offset;
1427 em->orig_start = found_key.offset - extent_offset;
1428 em->len = num_bytes;
1429 em->block_len = num_bytes;
1430 em->block_start = disk_bytenr;
1431 em->orig_block_len = disk_num_bytes;
1432 em->ram_bytes = ram_bytes;
1433 em->bdev = root->fs_info->fs_devices->latest_bdev;
1434 em->mod_start = em->start;
1435 em->mod_len = em->len;
1436 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1437 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1438 em->generation = -1;
1440 write_lock(&em_tree->lock);
1441 ret = add_extent_mapping(em_tree, em, 1);
1442 write_unlock(&em_tree->lock);
1443 if (ret != -EEXIST) {
1444 free_extent_map(em);
1447 btrfs_drop_extent_cache(inode, em->start,
1448 em->start + em->len - 1, 0);
1450 type = BTRFS_ORDERED_PREALLOC;
1452 type = BTRFS_ORDERED_NOCOW;
1455 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1456 num_bytes, num_bytes, type);
1457 BUG_ON(ret); /* -ENOMEM */
1459 if (root->root_key.objectid ==
1460 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1461 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1464 if (!nolock && nocow)
1465 btrfs_end_write_no_snapshoting(root);
1470 extent_clear_unlock_delalloc(inode, cur_offset,
1471 cur_offset + num_bytes - 1,
1472 locked_page, EXTENT_LOCKED |
1473 EXTENT_DELALLOC, PAGE_UNLOCK |
1475 if (!nolock && nocow)
1476 btrfs_end_write_no_snapshoting(root);
1477 cur_offset = extent_end;
1478 if (cur_offset > end)
1481 btrfs_release_path(path);
1483 if (cur_offset <= end && cow_start == (u64)-1) {
1484 cow_start = cur_offset;
1488 if (cow_start != (u64)-1) {
1489 ret = cow_file_range(inode, locked_page, cow_start, end,
1490 page_started, nr_written, 1);
1496 err = btrfs_end_transaction(trans, root);
1500 if (ret && cur_offset < end)
1501 extent_clear_unlock_delalloc(inode, cur_offset, end,
1502 locked_page, EXTENT_LOCKED |
1503 EXTENT_DELALLOC | EXTENT_DEFRAG |
1504 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1506 PAGE_SET_WRITEBACK |
1507 PAGE_END_WRITEBACK);
1508 btrfs_free_path(path);
1512 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1515 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1516 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1520 * @defrag_bytes is a hint value, no spinlock held here,
1521 * if is not zero, it means the file is defragging.
1522 * Force cow if given extent needs to be defragged.
1524 if (BTRFS_I(inode)->defrag_bytes &&
1525 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1526 EXTENT_DEFRAG, 0, NULL))
1533 * extent_io.c call back to do delayed allocation processing
1535 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1536 u64 start, u64 end, int *page_started,
1537 unsigned long *nr_written)
1540 int force_cow = need_force_cow(inode, start, end);
1542 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1543 ret = run_delalloc_nocow(inode, locked_page, start, end,
1544 page_started, 1, nr_written);
1545 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1546 ret = run_delalloc_nocow(inode, locked_page, start, end,
1547 page_started, 0, nr_written);
1548 } else if (!inode_need_compress(inode)) {
1549 ret = cow_file_range(inode, locked_page, start, end,
1550 page_started, nr_written, 1);
1552 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1553 &BTRFS_I(inode)->runtime_flags);
1554 ret = cow_file_range_async(inode, locked_page, start, end,
1555 page_started, nr_written);
1560 static void btrfs_split_extent_hook(struct inode *inode,
1561 struct extent_state *orig, u64 split)
1565 /* not delalloc, ignore it */
1566 if (!(orig->state & EXTENT_DELALLOC))
1569 size = orig->end - orig->start + 1;
1570 if (size > BTRFS_MAX_EXTENT_SIZE) {
1575 * See the explanation in btrfs_merge_extent_hook, the same
1576 * applies here, just in reverse.
1578 new_size = orig->end - split + 1;
1579 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1580 BTRFS_MAX_EXTENT_SIZE);
1581 new_size = split - orig->start;
1582 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1583 BTRFS_MAX_EXTENT_SIZE);
1584 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1585 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1589 spin_lock(&BTRFS_I(inode)->lock);
1590 BTRFS_I(inode)->outstanding_extents++;
1591 spin_unlock(&BTRFS_I(inode)->lock);
1595 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1596 * extents so we can keep track of new extents that are just merged onto old
1597 * extents, such as when we are doing sequential writes, so we can properly
1598 * account for the metadata space we'll need.
1600 static void btrfs_merge_extent_hook(struct inode *inode,
1601 struct extent_state *new,
1602 struct extent_state *other)
1604 u64 new_size, old_size;
1607 /* not delalloc, ignore it */
1608 if (!(other->state & EXTENT_DELALLOC))
1611 if (new->start > other->start)
1612 new_size = new->end - other->start + 1;
1614 new_size = other->end - new->start + 1;
1616 /* we're not bigger than the max, unreserve the space and go */
1617 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1618 spin_lock(&BTRFS_I(inode)->lock);
1619 BTRFS_I(inode)->outstanding_extents--;
1620 spin_unlock(&BTRFS_I(inode)->lock);
1625 * We have to add up either side to figure out how many extents were
1626 * accounted for before we merged into one big extent. If the number of
1627 * extents we accounted for is <= the amount we need for the new range
1628 * then we can return, otherwise drop. Think of it like this
1632 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1633 * need 2 outstanding extents, on one side we have 1 and the other side
1634 * we have 1 so they are == and we can return. But in this case
1636 * [MAX_SIZE+4k][MAX_SIZE+4k]
1638 * Each range on their own accounts for 2 extents, but merged together
1639 * they are only 3 extents worth of accounting, so we need to drop in
1642 old_size = other->end - other->start + 1;
1643 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1644 BTRFS_MAX_EXTENT_SIZE);
1645 old_size = new->end - new->start + 1;
1646 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1647 BTRFS_MAX_EXTENT_SIZE);
1649 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1650 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1653 spin_lock(&BTRFS_I(inode)->lock);
1654 BTRFS_I(inode)->outstanding_extents--;
1655 spin_unlock(&BTRFS_I(inode)->lock);
1658 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1659 struct inode *inode)
1661 spin_lock(&root->delalloc_lock);
1662 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1663 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1664 &root->delalloc_inodes);
1665 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1666 &BTRFS_I(inode)->runtime_flags);
1667 root->nr_delalloc_inodes++;
1668 if (root->nr_delalloc_inodes == 1) {
1669 spin_lock(&root->fs_info->delalloc_root_lock);
1670 BUG_ON(!list_empty(&root->delalloc_root));
1671 list_add_tail(&root->delalloc_root,
1672 &root->fs_info->delalloc_roots);
1673 spin_unlock(&root->fs_info->delalloc_root_lock);
1676 spin_unlock(&root->delalloc_lock);
1679 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1680 struct inode *inode)
1682 spin_lock(&root->delalloc_lock);
1683 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1684 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1685 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1686 &BTRFS_I(inode)->runtime_flags);
1687 root->nr_delalloc_inodes--;
1688 if (!root->nr_delalloc_inodes) {
1689 spin_lock(&root->fs_info->delalloc_root_lock);
1690 BUG_ON(list_empty(&root->delalloc_root));
1691 list_del_init(&root->delalloc_root);
1692 spin_unlock(&root->fs_info->delalloc_root_lock);
1695 spin_unlock(&root->delalloc_lock);
1699 * extent_io.c set_bit_hook, used to track delayed allocation
1700 * bytes in this file, and to maintain the list of inodes that
1701 * have pending delalloc work to be done.
1703 static void btrfs_set_bit_hook(struct inode *inode,
1704 struct extent_state *state, unsigned *bits)
1707 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1710 * set_bit and clear bit hooks normally require _irqsave/restore
1711 * but in this case, we are only testing for the DELALLOC
1712 * bit, which is only set or cleared with irqs on
1714 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1715 struct btrfs_root *root = BTRFS_I(inode)->root;
1716 u64 len = state->end + 1 - state->start;
1717 bool do_list = !btrfs_is_free_space_inode(inode);
1719 if (*bits & EXTENT_FIRST_DELALLOC) {
1720 *bits &= ~EXTENT_FIRST_DELALLOC;
1722 spin_lock(&BTRFS_I(inode)->lock);
1723 BTRFS_I(inode)->outstanding_extents++;
1724 spin_unlock(&BTRFS_I(inode)->lock);
1727 /* For sanity tests */
1728 if (btrfs_test_is_dummy_root(root))
1731 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1732 root->fs_info->delalloc_batch);
1733 spin_lock(&BTRFS_I(inode)->lock);
1734 BTRFS_I(inode)->delalloc_bytes += len;
1735 if (*bits & EXTENT_DEFRAG)
1736 BTRFS_I(inode)->defrag_bytes += len;
1737 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1738 &BTRFS_I(inode)->runtime_flags))
1739 btrfs_add_delalloc_inodes(root, inode);
1740 spin_unlock(&BTRFS_I(inode)->lock);
1745 * extent_io.c clear_bit_hook, see set_bit_hook for why
1747 static void btrfs_clear_bit_hook(struct inode *inode,
1748 struct extent_state *state,
1751 u64 len = state->end + 1 - state->start;
1752 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1753 BTRFS_MAX_EXTENT_SIZE);
1755 spin_lock(&BTRFS_I(inode)->lock);
1756 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1757 BTRFS_I(inode)->defrag_bytes -= len;
1758 spin_unlock(&BTRFS_I(inode)->lock);
1761 * set_bit and clear bit hooks normally require _irqsave/restore
1762 * but in this case, we are only testing for the DELALLOC
1763 * bit, which is only set or cleared with irqs on
1765 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1766 struct btrfs_root *root = BTRFS_I(inode)->root;
1767 bool do_list = !btrfs_is_free_space_inode(inode);
1769 if (*bits & EXTENT_FIRST_DELALLOC) {
1770 *bits &= ~EXTENT_FIRST_DELALLOC;
1771 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1772 spin_lock(&BTRFS_I(inode)->lock);
1773 BTRFS_I(inode)->outstanding_extents -= num_extents;
1774 spin_unlock(&BTRFS_I(inode)->lock);
1778 * We don't reserve metadata space for space cache inodes so we
1779 * don't need to call dellalloc_release_metadata if there is an
1782 if (*bits & EXTENT_DO_ACCOUNTING &&
1783 root != root->fs_info->tree_root)
1784 btrfs_delalloc_release_metadata(inode, len);
1786 /* For sanity tests. */
1787 if (btrfs_test_is_dummy_root(root))
1790 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1791 && do_list && !(state->state & EXTENT_NORESERVE))
1792 btrfs_free_reserved_data_space_noquota(inode,
1795 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1796 root->fs_info->delalloc_batch);
1797 spin_lock(&BTRFS_I(inode)->lock);
1798 BTRFS_I(inode)->delalloc_bytes -= len;
1799 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1800 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1801 &BTRFS_I(inode)->runtime_flags))
1802 btrfs_del_delalloc_inode(root, inode);
1803 spin_unlock(&BTRFS_I(inode)->lock);
1808 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1809 * we don't create bios that span stripes or chunks
1811 int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
1812 size_t size, struct bio *bio,
1813 unsigned long bio_flags)
1815 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1816 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1821 if (bio_flags & EXTENT_BIO_COMPRESSED)
1824 length = bio->bi_iter.bi_size;
1825 map_length = length;
1826 ret = btrfs_map_block(root->fs_info, rw, logical,
1827 &map_length, NULL, 0);
1828 /* Will always return 0 with map_multi == NULL */
1830 if (map_length < length + size)
1836 * in order to insert checksums into the metadata in large chunks,
1837 * we wait until bio submission time. All the pages in the bio are
1838 * checksummed and sums are attached onto the ordered extent record.
1840 * At IO completion time the cums attached on the ordered extent record
1841 * are inserted into the btree
1843 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1844 struct bio *bio, int mirror_num,
1845 unsigned long bio_flags,
1848 struct btrfs_root *root = BTRFS_I(inode)->root;
1851 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1852 BUG_ON(ret); /* -ENOMEM */
1857 * in order to insert checksums into the metadata in large chunks,
1858 * we wait until bio submission time. All the pages in the bio are
1859 * checksummed and sums are attached onto the ordered extent record.
1861 * At IO completion time the cums attached on the ordered extent record
1862 * are inserted into the btree
1864 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1865 int mirror_num, unsigned long bio_flags,
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1873 bio->bi_error = ret;
1880 * extent_io.c submission hook. This does the right thing for csum calculation
1881 * on write, or reading the csums from the tree before a read
1883 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1884 int mirror_num, unsigned long bio_flags,
1887 struct btrfs_root *root = BTRFS_I(inode)->root;
1888 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1891 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1893 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1895 if (btrfs_is_free_space_inode(inode))
1896 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1898 if (!(rw & REQ_WRITE)) {
1899 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1903 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1904 ret = btrfs_submit_compressed_read(inode, bio,
1908 } else if (!skip_sum) {
1909 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1914 } else if (async && !skip_sum) {
1915 /* csum items have already been cloned */
1916 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1918 /* we're doing a write, do the async checksumming */
1919 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1920 inode, rw, bio, mirror_num,
1921 bio_flags, bio_offset,
1922 __btrfs_submit_bio_start,
1923 __btrfs_submit_bio_done);
1925 } else if (!skip_sum) {
1926 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1932 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1936 bio->bi_error = ret;
1943 * given a list of ordered sums record them in the inode. This happens
1944 * at IO completion time based on sums calculated at bio submission time.
1946 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1947 struct inode *inode, u64 file_offset,
1948 struct list_head *list)
1950 struct btrfs_ordered_sum *sum;
1952 list_for_each_entry(sum, list, list) {
1953 trans->adding_csums = 1;
1954 btrfs_csum_file_blocks(trans,
1955 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1956 trans->adding_csums = 0;
1961 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1962 struct extent_state **cached_state)
1964 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
1965 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1966 cached_state, GFP_NOFS);
1969 /* see btrfs_writepage_start_hook for details on why this is required */
1970 struct btrfs_writepage_fixup {
1972 struct btrfs_work work;
1975 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1977 struct btrfs_writepage_fixup *fixup;
1978 struct btrfs_ordered_extent *ordered;
1979 struct extent_state *cached_state = NULL;
1981 struct inode *inode;
1986 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1990 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1991 ClearPageChecked(page);
1995 inode = page->mapping->host;
1996 page_start = page_offset(page);
1997 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1999 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2002 /* already ordered? We're done */
2003 if (PagePrivate2(page))
2006 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2008 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2009 page_end, &cached_state, GFP_NOFS);
2011 btrfs_start_ordered_extent(inode, ordered, 1);
2012 btrfs_put_ordered_extent(ordered);
2016 ret = btrfs_delalloc_reserve_space(inode, page_start,
2019 mapping_set_error(page->mapping, ret);
2020 end_extent_writepage(page, ret, page_start, page_end);
2021 ClearPageChecked(page);
2025 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
2026 ClearPageChecked(page);
2027 set_page_dirty(page);
2029 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2030 &cached_state, GFP_NOFS);
2033 page_cache_release(page);
2038 * There are a few paths in the higher layers of the kernel that directly
2039 * set the page dirty bit without asking the filesystem if it is a
2040 * good idea. This causes problems because we want to make sure COW
2041 * properly happens and the data=ordered rules are followed.
2043 * In our case any range that doesn't have the ORDERED bit set
2044 * hasn't been properly setup for IO. We kick off an async process
2045 * to fix it up. The async helper will wait for ordered extents, set
2046 * the delalloc bit and make it safe to write the page.
2048 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2050 struct inode *inode = page->mapping->host;
2051 struct btrfs_writepage_fixup *fixup;
2052 struct btrfs_root *root = BTRFS_I(inode)->root;
2054 /* this page is properly in the ordered list */
2055 if (TestClearPagePrivate2(page))
2058 if (PageChecked(page))
2061 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2065 SetPageChecked(page);
2066 page_cache_get(page);
2067 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2068 btrfs_writepage_fixup_worker, NULL, NULL);
2070 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
2074 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2075 struct inode *inode, u64 file_pos,
2076 u64 disk_bytenr, u64 disk_num_bytes,
2077 u64 num_bytes, u64 ram_bytes,
2078 u8 compression, u8 encryption,
2079 u16 other_encoding, int extent_type)
2081 struct btrfs_root *root = BTRFS_I(inode)->root;
2082 struct btrfs_file_extent_item *fi;
2083 struct btrfs_path *path;
2084 struct extent_buffer *leaf;
2085 struct btrfs_key ins;
2086 int extent_inserted = 0;
2089 path = btrfs_alloc_path();
2094 * we may be replacing one extent in the tree with another.
2095 * The new extent is pinned in the extent map, and we don't want
2096 * to drop it from the cache until it is completely in the btree.
2098 * So, tell btrfs_drop_extents to leave this extent in the cache.
2099 * the caller is expected to unpin it and allow it to be merged
2102 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2103 file_pos + num_bytes, NULL, 0,
2104 1, sizeof(*fi), &extent_inserted);
2108 if (!extent_inserted) {
2109 ins.objectid = btrfs_ino(inode);
2110 ins.offset = file_pos;
2111 ins.type = BTRFS_EXTENT_DATA_KEY;
2113 path->leave_spinning = 1;
2114 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2119 leaf = path->nodes[0];
2120 fi = btrfs_item_ptr(leaf, path->slots[0],
2121 struct btrfs_file_extent_item);
2122 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2123 btrfs_set_file_extent_type(leaf, fi, extent_type);
2124 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2125 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2126 btrfs_set_file_extent_offset(leaf, fi, 0);
2127 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2128 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2129 btrfs_set_file_extent_compression(leaf, fi, compression);
2130 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2131 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2133 btrfs_mark_buffer_dirty(leaf);
2134 btrfs_release_path(path);
2136 inode_add_bytes(inode, num_bytes);
2138 ins.objectid = disk_bytenr;
2139 ins.offset = disk_num_bytes;
2140 ins.type = BTRFS_EXTENT_ITEM_KEY;
2141 ret = btrfs_alloc_reserved_file_extent(trans, root,
2142 root->root_key.objectid,
2143 btrfs_ino(inode), file_pos,
2146 * Release the reserved range from inode dirty range map, as it is
2147 * already moved into delayed_ref_head
2149 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2151 btrfs_free_path(path);
2156 /* snapshot-aware defrag */
2157 struct sa_defrag_extent_backref {
2158 struct rb_node node;
2159 struct old_sa_defrag_extent *old;
2168 struct old_sa_defrag_extent {
2169 struct list_head list;
2170 struct new_sa_defrag_extent *new;
2179 struct new_sa_defrag_extent {
2180 struct rb_root root;
2181 struct list_head head;
2182 struct btrfs_path *path;
2183 struct inode *inode;
2191 static int backref_comp(struct sa_defrag_extent_backref *b1,
2192 struct sa_defrag_extent_backref *b2)
2194 if (b1->root_id < b2->root_id)
2196 else if (b1->root_id > b2->root_id)
2199 if (b1->inum < b2->inum)
2201 else if (b1->inum > b2->inum)
2204 if (b1->file_pos < b2->file_pos)
2206 else if (b1->file_pos > b2->file_pos)
2210 * [------------------------------] ===> (a range of space)
2211 * |<--->| |<---->| =============> (fs/file tree A)
2212 * |<---------------------------->| ===> (fs/file tree B)
2214 * A range of space can refer to two file extents in one tree while
2215 * refer to only one file extent in another tree.
2217 * So we may process a disk offset more than one time(two extents in A)
2218 * and locate at the same extent(one extent in B), then insert two same
2219 * backrefs(both refer to the extent in B).
2224 static void backref_insert(struct rb_root *root,
2225 struct sa_defrag_extent_backref *backref)
2227 struct rb_node **p = &root->rb_node;
2228 struct rb_node *parent = NULL;
2229 struct sa_defrag_extent_backref *entry;
2234 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2236 ret = backref_comp(backref, entry);
2240 p = &(*p)->rb_right;
2243 rb_link_node(&backref->node, parent, p);
2244 rb_insert_color(&backref->node, root);
2248 * Note the backref might has changed, and in this case we just return 0.
2250 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2253 struct btrfs_file_extent_item *extent;
2254 struct btrfs_fs_info *fs_info;
2255 struct old_sa_defrag_extent *old = ctx;
2256 struct new_sa_defrag_extent *new = old->new;
2257 struct btrfs_path *path = new->path;
2258 struct btrfs_key key;
2259 struct btrfs_root *root;
2260 struct sa_defrag_extent_backref *backref;
2261 struct extent_buffer *leaf;
2262 struct inode *inode = new->inode;
2268 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2269 inum == btrfs_ino(inode))
2272 key.objectid = root_id;
2273 key.type = BTRFS_ROOT_ITEM_KEY;
2274 key.offset = (u64)-1;
2276 fs_info = BTRFS_I(inode)->root->fs_info;
2277 root = btrfs_read_fs_root_no_name(fs_info, &key);
2279 if (PTR_ERR(root) == -ENOENT)
2282 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2283 inum, offset, root_id);
2284 return PTR_ERR(root);
2287 key.objectid = inum;
2288 key.type = BTRFS_EXTENT_DATA_KEY;
2289 if (offset > (u64)-1 << 32)
2292 key.offset = offset;
2294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2295 if (WARN_ON(ret < 0))
2302 leaf = path->nodes[0];
2303 slot = path->slots[0];
2305 if (slot >= btrfs_header_nritems(leaf)) {
2306 ret = btrfs_next_leaf(root, path);
2309 } else if (ret > 0) {
2318 btrfs_item_key_to_cpu(leaf, &key, slot);
2320 if (key.objectid > inum)
2323 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2326 extent = btrfs_item_ptr(leaf, slot,
2327 struct btrfs_file_extent_item);
2329 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2333 * 'offset' refers to the exact key.offset,
2334 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2335 * (key.offset - extent_offset).
2337 if (key.offset != offset)
2340 extent_offset = btrfs_file_extent_offset(leaf, extent);
2341 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2343 if (extent_offset >= old->extent_offset + old->offset +
2344 old->len || extent_offset + num_bytes <=
2345 old->extent_offset + old->offset)
2350 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2356 backref->root_id = root_id;
2357 backref->inum = inum;
2358 backref->file_pos = offset;
2359 backref->num_bytes = num_bytes;
2360 backref->extent_offset = extent_offset;
2361 backref->generation = btrfs_file_extent_generation(leaf, extent);
2363 backref_insert(&new->root, backref);
2366 btrfs_release_path(path);
2371 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2372 struct new_sa_defrag_extent *new)
2374 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2375 struct old_sa_defrag_extent *old, *tmp;
2380 list_for_each_entry_safe(old, tmp, &new->head, list) {
2381 ret = iterate_inodes_from_logical(old->bytenr +
2382 old->extent_offset, fs_info,
2383 path, record_one_backref,
2385 if (ret < 0 && ret != -ENOENT)
2388 /* no backref to be processed for this extent */
2390 list_del(&old->list);
2395 if (list_empty(&new->head))
2401 static int relink_is_mergable(struct extent_buffer *leaf,
2402 struct btrfs_file_extent_item *fi,
2403 struct new_sa_defrag_extent *new)
2405 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2408 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2411 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2414 if (btrfs_file_extent_encryption(leaf, fi) ||
2415 btrfs_file_extent_other_encoding(leaf, fi))
2422 * Note the backref might has changed, and in this case we just return 0.
2424 static noinline int relink_extent_backref(struct btrfs_path *path,
2425 struct sa_defrag_extent_backref *prev,
2426 struct sa_defrag_extent_backref *backref)
2428 struct btrfs_file_extent_item *extent;
2429 struct btrfs_file_extent_item *item;
2430 struct btrfs_ordered_extent *ordered;
2431 struct btrfs_trans_handle *trans;
2432 struct btrfs_fs_info *fs_info;
2433 struct btrfs_root *root;
2434 struct btrfs_key key;
2435 struct extent_buffer *leaf;
2436 struct old_sa_defrag_extent *old = backref->old;
2437 struct new_sa_defrag_extent *new = old->new;
2438 struct inode *src_inode = new->inode;
2439 struct inode *inode;
2440 struct extent_state *cached = NULL;
2449 if (prev && prev->root_id == backref->root_id &&
2450 prev->inum == backref->inum &&
2451 prev->file_pos + prev->num_bytes == backref->file_pos)
2454 /* step 1: get root */
2455 key.objectid = backref->root_id;
2456 key.type = BTRFS_ROOT_ITEM_KEY;
2457 key.offset = (u64)-1;
2459 fs_info = BTRFS_I(src_inode)->root->fs_info;
2460 index = srcu_read_lock(&fs_info->subvol_srcu);
2462 root = btrfs_read_fs_root_no_name(fs_info, &key);
2464 srcu_read_unlock(&fs_info->subvol_srcu, index);
2465 if (PTR_ERR(root) == -ENOENT)
2467 return PTR_ERR(root);
2470 if (btrfs_root_readonly(root)) {
2471 srcu_read_unlock(&fs_info->subvol_srcu, index);
2475 /* step 2: get inode */
2476 key.objectid = backref->inum;
2477 key.type = BTRFS_INODE_ITEM_KEY;
2480 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2481 if (IS_ERR(inode)) {
2482 srcu_read_unlock(&fs_info->subvol_srcu, index);
2486 srcu_read_unlock(&fs_info->subvol_srcu, index);
2488 /* step 3: relink backref */
2489 lock_start = backref->file_pos;
2490 lock_end = backref->file_pos + backref->num_bytes - 1;
2491 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2494 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2496 btrfs_put_ordered_extent(ordered);
2500 trans = btrfs_join_transaction(root);
2501 if (IS_ERR(trans)) {
2502 ret = PTR_ERR(trans);
2506 key.objectid = backref->inum;
2507 key.type = BTRFS_EXTENT_DATA_KEY;
2508 key.offset = backref->file_pos;
2510 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2513 } else if (ret > 0) {
2518 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2519 struct btrfs_file_extent_item);
2521 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2522 backref->generation)
2525 btrfs_release_path(path);
2527 start = backref->file_pos;
2528 if (backref->extent_offset < old->extent_offset + old->offset)
2529 start += old->extent_offset + old->offset -
2530 backref->extent_offset;
2532 len = min(backref->extent_offset + backref->num_bytes,
2533 old->extent_offset + old->offset + old->len);
2534 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2536 ret = btrfs_drop_extents(trans, root, inode, start,
2541 key.objectid = btrfs_ino(inode);
2542 key.type = BTRFS_EXTENT_DATA_KEY;
2545 path->leave_spinning = 1;
2547 struct btrfs_file_extent_item *fi;
2549 struct btrfs_key found_key;
2551 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2556 leaf = path->nodes[0];
2557 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2559 fi = btrfs_item_ptr(leaf, path->slots[0],
2560 struct btrfs_file_extent_item);
2561 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2563 if (extent_len + found_key.offset == start &&
2564 relink_is_mergable(leaf, fi, new)) {
2565 btrfs_set_file_extent_num_bytes(leaf, fi,
2567 btrfs_mark_buffer_dirty(leaf);
2568 inode_add_bytes(inode, len);
2574 btrfs_release_path(path);
2579 ret = btrfs_insert_empty_item(trans, root, path, &key,
2582 btrfs_abort_transaction(trans, root, ret);
2586 leaf = path->nodes[0];
2587 item = btrfs_item_ptr(leaf, path->slots[0],
2588 struct btrfs_file_extent_item);
2589 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2590 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2591 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2592 btrfs_set_file_extent_num_bytes(leaf, item, len);
2593 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2594 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2595 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2596 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2597 btrfs_set_file_extent_encryption(leaf, item, 0);
2598 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2600 btrfs_mark_buffer_dirty(leaf);
2601 inode_add_bytes(inode, len);
2602 btrfs_release_path(path);
2604 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2606 backref->root_id, backref->inum,
2607 new->file_pos); /* start - extent_offset */
2609 btrfs_abort_transaction(trans, root, ret);
2615 btrfs_release_path(path);
2616 path->leave_spinning = 0;
2617 btrfs_end_transaction(trans, root);
2619 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2625 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2627 struct old_sa_defrag_extent *old, *tmp;
2632 list_for_each_entry_safe(old, tmp, &new->head, list) {
2638 static void relink_file_extents(struct new_sa_defrag_extent *new)
2640 struct btrfs_path *path;
2641 struct sa_defrag_extent_backref *backref;
2642 struct sa_defrag_extent_backref *prev = NULL;
2643 struct inode *inode;
2644 struct btrfs_root *root;
2645 struct rb_node *node;
2649 root = BTRFS_I(inode)->root;
2651 path = btrfs_alloc_path();
2655 if (!record_extent_backrefs(path, new)) {
2656 btrfs_free_path(path);
2659 btrfs_release_path(path);
2662 node = rb_first(&new->root);
2665 rb_erase(node, &new->root);
2667 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2669 ret = relink_extent_backref(path, prev, backref);
2682 btrfs_free_path(path);
2684 free_sa_defrag_extent(new);
2686 atomic_dec(&root->fs_info->defrag_running);
2687 wake_up(&root->fs_info->transaction_wait);
2690 static struct new_sa_defrag_extent *
2691 record_old_file_extents(struct inode *inode,
2692 struct btrfs_ordered_extent *ordered)
2694 struct btrfs_root *root = BTRFS_I(inode)->root;
2695 struct btrfs_path *path;
2696 struct btrfs_key key;
2697 struct old_sa_defrag_extent *old;
2698 struct new_sa_defrag_extent *new;
2701 new = kmalloc(sizeof(*new), GFP_NOFS);
2706 new->file_pos = ordered->file_offset;
2707 new->len = ordered->len;
2708 new->bytenr = ordered->start;
2709 new->disk_len = ordered->disk_len;
2710 new->compress_type = ordered->compress_type;
2711 new->root = RB_ROOT;
2712 INIT_LIST_HEAD(&new->head);
2714 path = btrfs_alloc_path();
2718 key.objectid = btrfs_ino(inode);
2719 key.type = BTRFS_EXTENT_DATA_KEY;
2720 key.offset = new->file_pos;
2722 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2725 if (ret > 0 && path->slots[0] > 0)
2728 /* find out all the old extents for the file range */
2730 struct btrfs_file_extent_item *extent;
2731 struct extent_buffer *l;
2740 slot = path->slots[0];
2742 if (slot >= btrfs_header_nritems(l)) {
2743 ret = btrfs_next_leaf(root, path);
2751 btrfs_item_key_to_cpu(l, &key, slot);
2753 if (key.objectid != btrfs_ino(inode))
2755 if (key.type != BTRFS_EXTENT_DATA_KEY)
2757 if (key.offset >= new->file_pos + new->len)
2760 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2762 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2763 if (key.offset + num_bytes < new->file_pos)
2766 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2770 extent_offset = btrfs_file_extent_offset(l, extent);
2772 old = kmalloc(sizeof(*old), GFP_NOFS);
2776 offset = max(new->file_pos, key.offset);
2777 end = min(new->file_pos + new->len, key.offset + num_bytes);
2779 old->bytenr = disk_bytenr;
2780 old->extent_offset = extent_offset;
2781 old->offset = offset - key.offset;
2782 old->len = end - offset;
2785 list_add_tail(&old->list, &new->head);
2791 btrfs_free_path(path);
2792 atomic_inc(&root->fs_info->defrag_running);
2797 btrfs_free_path(path);
2799 free_sa_defrag_extent(new);
2803 static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2806 struct btrfs_block_group_cache *cache;
2808 cache = btrfs_lookup_block_group(root->fs_info, start);
2811 spin_lock(&cache->lock);
2812 cache->delalloc_bytes -= len;
2813 spin_unlock(&cache->lock);
2815 btrfs_put_block_group(cache);
2818 /* as ordered data IO finishes, this gets called so we can finish
2819 * an ordered extent if the range of bytes in the file it covers are
2822 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2824 struct inode *inode = ordered_extent->inode;
2825 struct btrfs_root *root = BTRFS_I(inode)->root;
2826 struct btrfs_trans_handle *trans = NULL;
2827 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2828 struct extent_state *cached_state = NULL;
2829 struct new_sa_defrag_extent *new = NULL;
2830 int compress_type = 0;
2832 u64 logical_len = ordered_extent->len;
2834 bool truncated = false;
2836 nolock = btrfs_is_free_space_inode(inode);
2838 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2843 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2844 ordered_extent->file_offset +
2845 ordered_extent->len - 1);
2847 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2849 logical_len = ordered_extent->truncated_len;
2850 /* Truncated the entire extent, don't bother adding */
2855 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2856 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2859 * For mwrite(mmap + memset to write) case, we still reserve
2860 * space for NOCOW range.
2861 * As NOCOW won't cause a new delayed ref, just free the space
2863 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2864 ordered_extent->len);
2865 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2867 trans = btrfs_join_transaction_nolock(root);
2869 trans = btrfs_join_transaction(root);
2870 if (IS_ERR(trans)) {
2871 ret = PTR_ERR(trans);
2875 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2876 ret = btrfs_update_inode_fallback(trans, root, inode);
2877 if (ret) /* -ENOMEM or corruption */
2878 btrfs_abort_transaction(trans, root, ret);
2882 lock_extent_bits(io_tree, ordered_extent->file_offset,
2883 ordered_extent->file_offset + ordered_extent->len - 1,
2886 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2887 ordered_extent->file_offset + ordered_extent->len - 1,
2888 EXTENT_DEFRAG, 1, cached_state);
2890 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2891 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2892 /* the inode is shared */
2893 new = record_old_file_extents(inode, ordered_extent);
2895 clear_extent_bit(io_tree, ordered_extent->file_offset,
2896 ordered_extent->file_offset + ordered_extent->len - 1,
2897 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2901 trans = btrfs_join_transaction_nolock(root);
2903 trans = btrfs_join_transaction(root);
2904 if (IS_ERR(trans)) {
2905 ret = PTR_ERR(trans);
2910 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2912 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2913 compress_type = ordered_extent->compress_type;
2914 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2915 BUG_ON(compress_type);
2916 ret = btrfs_mark_extent_written(trans, inode,
2917 ordered_extent->file_offset,
2918 ordered_extent->file_offset +
2921 BUG_ON(root == root->fs_info->tree_root);
2922 ret = insert_reserved_file_extent(trans, inode,
2923 ordered_extent->file_offset,
2924 ordered_extent->start,
2925 ordered_extent->disk_len,
2926 logical_len, logical_len,
2927 compress_type, 0, 0,
2928 BTRFS_FILE_EXTENT_REG);
2930 btrfs_release_delalloc_bytes(root,
2931 ordered_extent->start,
2932 ordered_extent->disk_len);
2934 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2935 ordered_extent->file_offset, ordered_extent->len,
2938 btrfs_abort_transaction(trans, root, ret);
2942 add_pending_csums(trans, inode, ordered_extent->file_offset,
2943 &ordered_extent->list);
2945 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2946 ret = btrfs_update_inode_fallback(trans, root, inode);
2947 if (ret) { /* -ENOMEM or corruption */
2948 btrfs_abort_transaction(trans, root, ret);
2953 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2954 ordered_extent->file_offset +
2955 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2957 if (root != root->fs_info->tree_root)
2958 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2960 btrfs_end_transaction(trans, root);
2962 if (ret || truncated) {
2966 start = ordered_extent->file_offset + logical_len;
2968 start = ordered_extent->file_offset;
2969 end = ordered_extent->file_offset + ordered_extent->len - 1;
2970 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2972 /* Drop the cache for the part of the extent we didn't write. */
2973 btrfs_drop_extent_cache(inode, start, end, 0);
2976 * If the ordered extent had an IOERR or something else went
2977 * wrong we need to return the space for this ordered extent
2978 * back to the allocator. We only free the extent in the
2979 * truncated case if we didn't write out the extent at all.
2981 if ((ret || !logical_len) &&
2982 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2983 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2984 btrfs_free_reserved_extent(root, ordered_extent->start,
2985 ordered_extent->disk_len, 1);
2990 * This needs to be done to make sure anybody waiting knows we are done
2991 * updating everything for this ordered extent.
2993 btrfs_remove_ordered_extent(inode, ordered_extent);
2995 /* for snapshot-aware defrag */
2998 free_sa_defrag_extent(new);
2999 atomic_dec(&root->fs_info->defrag_running);
3001 relink_file_extents(new);
3006 btrfs_put_ordered_extent(ordered_extent);
3007 /* once for the tree */
3008 btrfs_put_ordered_extent(ordered_extent);
3013 static void finish_ordered_fn(struct btrfs_work *work)
3015 struct btrfs_ordered_extent *ordered_extent;
3016 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3017 btrfs_finish_ordered_io(ordered_extent);
3020 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
3021 struct extent_state *state, int uptodate)
3023 struct inode *inode = page->mapping->host;
3024 struct btrfs_root *root = BTRFS_I(inode)->root;
3025 struct btrfs_ordered_extent *ordered_extent = NULL;
3026 struct btrfs_workqueue *wq;
3027 btrfs_work_func_t func;
3029 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3031 ClearPagePrivate2(page);
3032 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3033 end - start + 1, uptodate))
3036 if (btrfs_is_free_space_inode(inode)) {
3037 wq = root->fs_info->endio_freespace_worker;
3038 func = btrfs_freespace_write_helper;
3040 wq = root->fs_info->endio_write_workers;
3041 func = btrfs_endio_write_helper;
3044 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3046 btrfs_queue_work(wq, &ordered_extent->work);
3051 static int __readpage_endio_check(struct inode *inode,
3052 struct btrfs_io_bio *io_bio,
3053 int icsum, struct page *page,
3054 int pgoff, u64 start, size_t len)
3060 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3062 kaddr = kmap_atomic(page);
3063 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3064 btrfs_csum_final(csum, (char *)&csum);
3065 if (csum != csum_expected)
3068 kunmap_atomic(kaddr);
3071 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3072 "csum failed ino %llu off %llu csum %u expected csum %u",
3073 btrfs_ino(inode), start, csum, csum_expected);
3074 memset(kaddr + pgoff, 1, len);
3075 flush_dcache_page(page);
3076 kunmap_atomic(kaddr);
3077 if (csum_expected == 0)
3083 * when reads are done, we need to check csums to verify the data is correct
3084 * if there's a match, we allow the bio to finish. If not, the code in
3085 * extent_io.c will try to find good copies for us.
3087 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3088 u64 phy_offset, struct page *page,
3089 u64 start, u64 end, int mirror)
3091 size_t offset = start - page_offset(page);
3092 struct inode *inode = page->mapping->host;
3093 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3094 struct btrfs_root *root = BTRFS_I(inode)->root;
3096 if (PageChecked(page)) {
3097 ClearPageChecked(page);
3101 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3104 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3105 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
3106 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
3111 phy_offset >>= inode->i_sb->s_blocksize_bits;
3112 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3113 start, (size_t)(end - start + 1));
3116 struct delayed_iput {
3117 struct list_head list;
3118 struct inode *inode;
3121 /* JDM: If this is fs-wide, why can't we add a pointer to
3122 * btrfs_inode instead and avoid the allocation? */
3123 void btrfs_add_delayed_iput(struct inode *inode)
3125 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
3126 struct delayed_iput *delayed;
3128 if (atomic_add_unless(&inode->i_count, -1, 1))
3131 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
3132 delayed->inode = inode;
3134 spin_lock(&fs_info->delayed_iput_lock);
3135 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
3136 spin_unlock(&fs_info->delayed_iput_lock);
3139 void btrfs_run_delayed_iputs(struct btrfs_root *root)
3142 struct btrfs_fs_info *fs_info = root->fs_info;
3143 struct delayed_iput *delayed;
3146 spin_lock(&fs_info->delayed_iput_lock);
3147 empty = list_empty(&fs_info->delayed_iputs);
3148 spin_unlock(&fs_info->delayed_iput_lock);
3152 down_read(&fs_info->delayed_iput_sem);
3154 spin_lock(&fs_info->delayed_iput_lock);
3155 list_splice_init(&fs_info->delayed_iputs, &list);
3156 spin_unlock(&fs_info->delayed_iput_lock);
3158 while (!list_empty(&list)) {
3159 delayed = list_entry(list.next, struct delayed_iput, list);
3160 list_del(&delayed->list);
3161 iput(delayed->inode);
3165 up_read(&root->fs_info->delayed_iput_sem);
3169 * This is called in transaction commit time. If there are no orphan
3170 * files in the subvolume, it removes orphan item and frees block_rsv
3173 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root)
3176 struct btrfs_block_rsv *block_rsv;
3179 if (atomic_read(&root->orphan_inodes) ||
3180 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3183 spin_lock(&root->orphan_lock);
3184 if (atomic_read(&root->orphan_inodes)) {
3185 spin_unlock(&root->orphan_lock);
3189 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3190 spin_unlock(&root->orphan_lock);
3194 block_rsv = root->orphan_block_rsv;
3195 root->orphan_block_rsv = NULL;
3196 spin_unlock(&root->orphan_lock);
3198 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
3199 btrfs_root_refs(&root->root_item) > 0) {
3200 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3201 root->root_key.objectid);
3203 btrfs_abort_transaction(trans, root, ret);
3205 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3210 WARN_ON(block_rsv->size > 0);
3211 btrfs_free_block_rsv(root, block_rsv);
3216 * This creates an orphan entry for the given inode in case something goes
3217 * wrong in the middle of an unlink/truncate.
3219 * NOTE: caller of this function should reserve 5 units of metadata for
3222 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3224 struct btrfs_root *root = BTRFS_I(inode)->root;
3225 struct btrfs_block_rsv *block_rsv = NULL;
3230 if (!root->orphan_block_rsv) {
3231 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
3236 spin_lock(&root->orphan_lock);
3237 if (!root->orphan_block_rsv) {
3238 root->orphan_block_rsv = block_rsv;
3239 } else if (block_rsv) {
3240 btrfs_free_block_rsv(root, block_rsv);
3244 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3245 &BTRFS_I(inode)->runtime_flags)) {
3248 * For proper ENOSPC handling, we should do orphan
3249 * cleanup when mounting. But this introduces backward
3250 * compatibility issue.
3252 if (!xchg(&root->orphan_item_inserted, 1))
3258 atomic_inc(&root->orphan_inodes);
3261 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3262 &BTRFS_I(inode)->runtime_flags))
3264 spin_unlock(&root->orphan_lock);
3266 /* grab metadata reservation from transaction handle */
3268 ret = btrfs_orphan_reserve_metadata(trans, inode);
3269 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
3272 /* insert an orphan item to track this unlinked/truncated file */
3274 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
3276 atomic_dec(&root->orphan_inodes);
3278 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3279 &BTRFS_I(inode)->runtime_flags);
3280 btrfs_orphan_release_metadata(inode);
3282 if (ret != -EEXIST) {
3283 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3284 &BTRFS_I(inode)->runtime_flags);
3285 btrfs_abort_transaction(trans, root, ret);
3292 /* insert an orphan item to track subvolume contains orphan files */
3294 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3295 root->root_key.objectid);
3296 if (ret && ret != -EEXIST) {
3297 btrfs_abort_transaction(trans, root, ret);
3305 * We have done the truncate/delete so we can go ahead and remove the orphan
3306 * item for this particular inode.
3308 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3309 struct inode *inode)
3311 struct btrfs_root *root = BTRFS_I(inode)->root;
3312 int delete_item = 0;
3313 int release_rsv = 0;
3316 spin_lock(&root->orphan_lock);
3317 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3318 &BTRFS_I(inode)->runtime_flags))
3321 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3322 &BTRFS_I(inode)->runtime_flags))
3324 spin_unlock(&root->orphan_lock);
3327 atomic_dec(&root->orphan_inodes);
3329 ret = btrfs_del_orphan_item(trans, root,
3334 btrfs_orphan_release_metadata(inode);
3340 * this cleans up any orphans that may be left on the list from the last use
3343 int btrfs_orphan_cleanup(struct btrfs_root *root)
3345 struct btrfs_path *path;
3346 struct extent_buffer *leaf;
3347 struct btrfs_key key, found_key;
3348 struct btrfs_trans_handle *trans;
3349 struct inode *inode;
3350 u64 last_objectid = 0;
3351 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3353 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3356 path = btrfs_alloc_path();
3363 key.objectid = BTRFS_ORPHAN_OBJECTID;
3364 key.type = BTRFS_ORPHAN_ITEM_KEY;
3365 key.offset = (u64)-1;
3368 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3373 * if ret == 0 means we found what we were searching for, which
3374 * is weird, but possible, so only screw with path if we didn't
3375 * find the key and see if we have stuff that matches
3379 if (path->slots[0] == 0)
3384 /* pull out the item */
3385 leaf = path->nodes[0];
3386 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3388 /* make sure the item matches what we want */
3389 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3391 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3394 /* release the path since we're done with it */
3395 btrfs_release_path(path);
3398 * this is where we are basically btrfs_lookup, without the
3399 * crossing root thing. we store the inode number in the
3400 * offset of the orphan item.
3403 if (found_key.offset == last_objectid) {
3404 btrfs_err(root->fs_info,
3405 "Error removing orphan entry, stopping orphan cleanup");
3410 last_objectid = found_key.offset;
3412 found_key.objectid = found_key.offset;
3413 found_key.type = BTRFS_INODE_ITEM_KEY;
3414 found_key.offset = 0;
3415 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
3416 ret = PTR_ERR_OR_ZERO(inode);
3417 if (ret && ret != -ESTALE)
3420 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3421 struct btrfs_root *dead_root;
3422 struct btrfs_fs_info *fs_info = root->fs_info;
3423 int is_dead_root = 0;
3426 * this is an orphan in the tree root. Currently these
3427 * could come from 2 sources:
3428 * a) a snapshot deletion in progress
3429 * b) a free space cache inode
3430 * We need to distinguish those two, as the snapshot
3431 * orphan must not get deleted.
3432 * find_dead_roots already ran before us, so if this
3433 * is a snapshot deletion, we should find the root
3434 * in the dead_roots list
3436 spin_lock(&fs_info->trans_lock);
3437 list_for_each_entry(dead_root, &fs_info->dead_roots,
3439 if (dead_root->root_key.objectid ==
3440 found_key.objectid) {
3445 spin_unlock(&fs_info->trans_lock);
3447 /* prevent this orphan from being found again */
3448 key.offset = found_key.objectid - 1;
3453 * Inode is already gone but the orphan item is still there,
3454 * kill the orphan item.
3456 if (ret == -ESTALE) {
3457 trans = btrfs_start_transaction(root, 1);
3458 if (IS_ERR(trans)) {
3459 ret = PTR_ERR(trans);
3462 btrfs_debug(root->fs_info, "auto deleting %Lu",
3463 found_key.objectid);
3464 ret = btrfs_del_orphan_item(trans, root,
3465 found_key.objectid);
3466 btrfs_end_transaction(trans, root);
3473 * add this inode to the orphan list so btrfs_orphan_del does
3474 * the proper thing when we hit it
3476 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3477 &BTRFS_I(inode)->runtime_flags);
3478 atomic_inc(&root->orphan_inodes);
3480 /* if we have links, this was a truncate, lets do that */
3481 if (inode->i_nlink) {
3482 if (WARN_ON(!S_ISREG(inode->i_mode))) {
3488 /* 1 for the orphan item deletion. */
3489 trans = btrfs_start_transaction(root, 1);
3490 if (IS_ERR(trans)) {
3492 ret = PTR_ERR(trans);
3495 ret = btrfs_orphan_add(trans, inode);
3496 btrfs_end_transaction(trans, root);
3502 ret = btrfs_truncate(inode);
3504 btrfs_orphan_del(NULL, inode);
3509 /* this will do delete_inode and everything for us */
3514 /* release the path since we're done with it */
3515 btrfs_release_path(path);
3517 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3519 if (root->orphan_block_rsv)
3520 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3523 if (root->orphan_block_rsv ||
3524 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3525 trans = btrfs_join_transaction(root);
3527 btrfs_end_transaction(trans, root);
3531 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
3533 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
3537 btrfs_err(root->fs_info,
3538 "could not do orphan cleanup %d", ret);
3539 btrfs_free_path(path);
3544 * very simple check to peek ahead in the leaf looking for xattrs. If we
3545 * don't find any xattrs, we know there can't be any acls.
3547 * slot is the slot the inode is in, objectid is the objectid of the inode
3549 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3550 int slot, u64 objectid,
3551 int *first_xattr_slot)
3553 u32 nritems = btrfs_header_nritems(leaf);
3554 struct btrfs_key found_key;
3555 static u64 xattr_access = 0;
3556 static u64 xattr_default = 0;
3559 if (!xattr_access) {
3560 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3561 strlen(POSIX_ACL_XATTR_ACCESS));
3562 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3563 strlen(POSIX_ACL_XATTR_DEFAULT));
3567 *first_xattr_slot = -1;
3568 while (slot < nritems) {
3569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3571 /* we found a different objectid, there must not be acls */
3572 if (found_key.objectid != objectid)
3575 /* we found an xattr, assume we've got an acl */
3576 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3577 if (*first_xattr_slot == -1)
3578 *first_xattr_slot = slot;
3579 if (found_key.offset == xattr_access ||
3580 found_key.offset == xattr_default)
3585 * we found a key greater than an xattr key, there can't
3586 * be any acls later on
3588 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3595 * it goes inode, inode backrefs, xattrs, extents,
3596 * so if there are a ton of hard links to an inode there can
3597 * be a lot of backrefs. Don't waste time searching too hard,
3598 * this is just an optimization
3603 /* we hit the end of the leaf before we found an xattr or
3604 * something larger than an xattr. We have to assume the inode
3607 if (*first_xattr_slot == -1)
3608 *first_xattr_slot = slot;
3613 * read an inode from the btree into the in-memory inode
3615 static void btrfs_read_locked_inode(struct inode *inode)
3617 struct btrfs_path *path;
3618 struct extent_buffer *leaf;
3619 struct btrfs_inode_item *inode_item;
3620 struct btrfs_root *root = BTRFS_I(inode)->root;
3621 struct btrfs_key location;
3626 bool filled = false;
3627 int first_xattr_slot;
3629 ret = btrfs_fill_inode(inode, &rdev);
3633 path = btrfs_alloc_path();
3637 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3639 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3643 leaf = path->nodes[0];
3648 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3649 struct btrfs_inode_item);
3650 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3651 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3652 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3653 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3654 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3656 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3657 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3659 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3660 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3662 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3663 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3665 BTRFS_I(inode)->i_otime.tv_sec =
3666 btrfs_timespec_sec(leaf, &inode_item->otime);
3667 BTRFS_I(inode)->i_otime.tv_nsec =
3668 btrfs_timespec_nsec(leaf, &inode_item->otime);
3670 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3671 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3672 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3674 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3675 inode->i_generation = BTRFS_I(inode)->generation;
3677 rdev = btrfs_inode_rdev(leaf, inode_item);
3679 BTRFS_I(inode)->index_cnt = (u64)-1;
3680 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3684 * If we were modified in the current generation and evicted from memory
3685 * and then re-read we need to do a full sync since we don't have any
3686 * idea about which extents were modified before we were evicted from
3689 * This is required for both inode re-read from disk and delayed inode
3690 * in delayed_nodes_tree.
3692 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3693 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3694 &BTRFS_I(inode)->runtime_flags);
3697 * We don't persist the id of the transaction where an unlink operation
3698 * against the inode was last made. So here we assume the inode might
3699 * have been evicted, and therefore the exact value of last_unlink_trans
3700 * lost, and set it to last_trans to avoid metadata inconsistencies
3701 * between the inode and its parent if the inode is fsync'ed and the log
3702 * replayed. For example, in the scenario:
3705 * ln mydir/foo mydir/bar
3708 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3709 * xfs_io -c fsync mydir/foo
3711 * mount fs, triggers fsync log replay
3713 * We must make sure that when we fsync our inode foo we also log its
3714 * parent inode, otherwise after log replay the parent still has the
3715 * dentry with the "bar" name but our inode foo has a link count of 1
3716 * and doesn't have an inode ref with the name "bar" anymore.
3718 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3719 * but it guarantees correctness at the expense of ocassional full
3720 * transaction commits on fsync if our inode is a directory, or if our
3721 * inode is not a directory, logging its parent unnecessarily.
3723 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3726 if (inode->i_nlink != 1 ||
3727 path->slots[0] >= btrfs_header_nritems(leaf))
3730 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3731 if (location.objectid != btrfs_ino(inode))
3734 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3735 if (location.type == BTRFS_INODE_REF_KEY) {
3736 struct btrfs_inode_ref *ref;
3738 ref = (struct btrfs_inode_ref *)ptr;
3739 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3740 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3741 struct btrfs_inode_extref *extref;
3743 extref = (struct btrfs_inode_extref *)ptr;
3744 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3749 * try to precache a NULL acl entry for files that don't have
3750 * any xattrs or acls
3752 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3753 btrfs_ino(inode), &first_xattr_slot);
3754 if (first_xattr_slot != -1) {
3755 path->slots[0] = first_xattr_slot;
3756 ret = btrfs_load_inode_props(inode, path);
3758 btrfs_err(root->fs_info,
3759 "error loading props for ino %llu (root %llu): %d",
3761 root->root_key.objectid, ret);
3763 btrfs_free_path(path);
3766 cache_no_acl(inode);
3768 switch (inode->i_mode & S_IFMT) {
3770 inode->i_mapping->a_ops = &btrfs_aops;
3771 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3772 inode->i_fop = &btrfs_file_operations;
3773 inode->i_op = &btrfs_file_inode_operations;
3776 inode->i_fop = &btrfs_dir_file_operations;
3777 if (root == root->fs_info->tree_root)
3778 inode->i_op = &btrfs_dir_ro_inode_operations;
3780 inode->i_op = &btrfs_dir_inode_operations;
3783 inode->i_op = &btrfs_symlink_inode_operations;
3784 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3787 inode->i_op = &btrfs_special_inode_operations;
3788 init_special_inode(inode, inode->i_mode, rdev);
3792 btrfs_update_iflags(inode);
3796 btrfs_free_path(path);
3797 make_bad_inode(inode);
3801 * given a leaf and an inode, copy the inode fields into the leaf
3803 static void fill_inode_item(struct btrfs_trans_handle *trans,
3804 struct extent_buffer *leaf,
3805 struct btrfs_inode_item *item,
3806 struct inode *inode)
3808 struct btrfs_map_token token;
3810 btrfs_init_map_token(&token);
3812 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3813 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3814 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3816 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3817 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3819 btrfs_set_token_timespec_sec(leaf, &item->atime,
3820 inode->i_atime.tv_sec, &token);
3821 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3822 inode->i_atime.tv_nsec, &token);
3824 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3825 inode->i_mtime.tv_sec, &token);
3826 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3827 inode->i_mtime.tv_nsec, &token);
3829 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3830 inode->i_ctime.tv_sec, &token);
3831 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3832 inode->i_ctime.tv_nsec, &token);
3834 btrfs_set_token_timespec_sec(leaf, &item->otime,
3835 BTRFS_I(inode)->i_otime.tv_sec, &token);
3836 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3837 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3839 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3841 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3843 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3844 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3845 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3846 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3847 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3851 * copy everything in the in-memory inode into the btree.
3853 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root, struct inode *inode)
3856 struct btrfs_inode_item *inode_item;
3857 struct btrfs_path *path;
3858 struct extent_buffer *leaf;
3861 path = btrfs_alloc_path();
3865 path->leave_spinning = 1;
3866 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3874 leaf = path->nodes[0];
3875 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3876 struct btrfs_inode_item);
3878 fill_inode_item(trans, leaf, inode_item, inode);
3879 btrfs_mark_buffer_dirty(leaf);
3880 btrfs_set_inode_last_trans(trans, inode);
3883 btrfs_free_path(path);
3888 * copy everything in the in-memory inode into the btree.
3890 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3891 struct btrfs_root *root, struct inode *inode)
3896 * If the inode is a free space inode, we can deadlock during commit
3897 * if we put it into the delayed code.
3899 * The data relocation inode should also be directly updated
3902 if (!btrfs_is_free_space_inode(inode)
3903 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3904 && !root->fs_info->log_root_recovering) {
3905 btrfs_update_root_times(trans, root);
3907 ret = btrfs_delayed_update_inode(trans, root, inode);
3909 btrfs_set_inode_last_trans(trans, inode);
3913 return btrfs_update_inode_item(trans, root, inode);
3916 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3917 struct btrfs_root *root,
3918 struct inode *inode)
3922 ret = btrfs_update_inode(trans, root, inode);
3924 return btrfs_update_inode_item(trans, root, inode);
3929 * unlink helper that gets used here in inode.c and in the tree logging
3930 * recovery code. It remove a link in a directory with a given name, and
3931 * also drops the back refs in the inode to the directory
3933 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3934 struct btrfs_root *root,
3935 struct inode *dir, struct inode *inode,
3936 const char *name, int name_len)
3938 struct btrfs_path *path;
3940 struct extent_buffer *leaf;
3941 struct btrfs_dir_item *di;
3942 struct btrfs_key key;
3944 u64 ino = btrfs_ino(inode);
3945 u64 dir_ino = btrfs_ino(dir);
3947 path = btrfs_alloc_path();
3953 path->leave_spinning = 1;
3954 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3955 name, name_len, -1);
3964 leaf = path->nodes[0];
3965 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3966 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3969 btrfs_release_path(path);
3972 * If we don't have dir index, we have to get it by looking up
3973 * the inode ref, since we get the inode ref, remove it directly,
3974 * it is unnecessary to do delayed deletion.
3976 * But if we have dir index, needn't search inode ref to get it.
3977 * Since the inode ref is close to the inode item, it is better
3978 * that we delay to delete it, and just do this deletion when
3979 * we update the inode item.
3981 if (BTRFS_I(inode)->dir_index) {
3982 ret = btrfs_delayed_delete_inode_ref(inode);
3984 index = BTRFS_I(inode)->dir_index;
3989 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3992 btrfs_info(root->fs_info,
3993 "failed to delete reference to %.*s, inode %llu parent %llu",
3994 name_len, name, ino, dir_ino);
3995 btrfs_abort_transaction(trans, root, ret);
3999 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
4001 btrfs_abort_transaction(trans, root, ret);
4005 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
4007 if (ret != 0 && ret != -ENOENT) {
4008 btrfs_abort_transaction(trans, root, ret);
4012 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4017 btrfs_abort_transaction(trans, root, ret);
4019 btrfs_free_path(path);
4023 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4024 inode_inc_iversion(inode);
4025 inode_inc_iversion(dir);
4026 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
4027 ret = btrfs_update_inode(trans, root, dir);
4032 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4033 struct btrfs_root *root,
4034 struct inode *dir, struct inode *inode,
4035 const char *name, int name_len)
4038 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4041 ret = btrfs_update_inode(trans, root, inode);
4047 * helper to start transaction for unlink and rmdir.
4049 * unlink and rmdir are special in btrfs, they do not always free space, so
4050 * if we cannot make our reservations the normal way try and see if there is
4051 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4052 * allow the unlink to occur.
4054 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4056 struct btrfs_root *root = BTRFS_I(dir)->root;
4059 * 1 for the possible orphan item
4060 * 1 for the dir item
4061 * 1 for the dir index
4062 * 1 for the inode ref
4065 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
4068 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4070 struct btrfs_root *root = BTRFS_I(dir)->root;
4071 struct btrfs_trans_handle *trans;
4072 struct inode *inode = d_inode(dentry);
4075 trans = __unlink_start_trans(dir);
4077 return PTR_ERR(trans);
4079 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
4081 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
4082 dentry->d_name.name, dentry->d_name.len);
4086 if (inode->i_nlink == 0) {
4087 ret = btrfs_orphan_add(trans, inode);
4093 btrfs_end_transaction(trans, root);
4094 btrfs_btree_balance_dirty(root);
4098 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4099 struct btrfs_root *root,
4100 struct inode *dir, u64 objectid,
4101 const char *name, int name_len)
4103 struct btrfs_path *path;
4104 struct extent_buffer *leaf;
4105 struct btrfs_dir_item *di;
4106 struct btrfs_key key;
4109 u64 dir_ino = btrfs_ino(dir);
4111 path = btrfs_alloc_path();
4115 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4116 name, name_len, -1);
4117 if (IS_ERR_OR_NULL(di)) {
4125 leaf = path->nodes[0];
4126 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4127 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4128 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4130 btrfs_abort_transaction(trans, root, ret);
4133 btrfs_release_path(path);
4135 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4136 objectid, root->root_key.objectid,
4137 dir_ino, &index, name, name_len);
4139 if (ret != -ENOENT) {
4140 btrfs_abort_transaction(trans, root, ret);
4143 di = btrfs_search_dir_index_item(root, path, dir_ino,
4145 if (IS_ERR_OR_NULL(di)) {
4150 btrfs_abort_transaction(trans, root, ret);
4154 leaf = path->nodes[0];
4155 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4156 btrfs_release_path(path);
4159 btrfs_release_path(path);
4161 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
4163 btrfs_abort_transaction(trans, root, ret);
4167 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4168 inode_inc_iversion(dir);
4169 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
4170 ret = btrfs_update_inode_fallback(trans, root, dir);
4172 btrfs_abort_transaction(trans, root, ret);
4174 btrfs_free_path(path);
4178 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4180 struct inode *inode = d_inode(dentry);
4182 struct btrfs_root *root = BTRFS_I(dir)->root;
4183 struct btrfs_trans_handle *trans;
4185 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4187 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4190 trans = __unlink_start_trans(dir);
4192 return PTR_ERR(trans);
4194 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4195 err = btrfs_unlink_subvol(trans, root, dir,
4196 BTRFS_I(inode)->location.objectid,
4197 dentry->d_name.name,
4198 dentry->d_name.len);
4202 err = btrfs_orphan_add(trans, inode);
4206 /* now the directory is empty */
4207 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
4208 dentry->d_name.name, dentry->d_name.len);
4210 btrfs_i_size_write(inode, 0);
4212 btrfs_end_transaction(trans, root);
4213 btrfs_btree_balance_dirty(root);
4218 static int truncate_space_check(struct btrfs_trans_handle *trans,
4219 struct btrfs_root *root,
4224 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4225 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4226 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4228 trans->bytes_reserved += bytes_deleted;
4233 static int truncate_inline_extent(struct inode *inode,
4234 struct btrfs_path *path,
4235 struct btrfs_key *found_key,
4239 struct extent_buffer *leaf = path->nodes[0];
4240 int slot = path->slots[0];
4241 struct btrfs_file_extent_item *fi;
4242 u32 size = (u32)(new_size - found_key->offset);
4243 struct btrfs_root *root = BTRFS_I(inode)->root;
4245 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4247 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4248 loff_t offset = new_size;
4249 loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE);
4252 * Zero out the remaining of the last page of our inline extent,
4253 * instead of directly truncating our inline extent here - that
4254 * would be much more complex (decompressing all the data, then
4255 * compressing the truncated data, which might be bigger than
4256 * the size of the inline extent, resize the extent, etc).
4257 * We release the path because to get the page we might need to
4258 * read the extent item from disk (data not in the page cache).
4260 btrfs_release_path(path);
4261 return btrfs_truncate_page(inode, offset, page_end - offset, 0);
4264 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4265 size = btrfs_file_extent_calc_inline_size(size);
4266 btrfs_truncate_item(root, path, size, 1);
4268 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4269 inode_sub_bytes(inode, item_end + 1 - new_size);
4275 * this can truncate away extent items, csum items and directory items.
4276 * It starts at a high offset and removes keys until it can't find
4277 * any higher than new_size
4279 * csum items that cross the new i_size are truncated to the new size
4282 * min_type is the minimum key type to truncate down to. If set to 0, this
4283 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4285 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4286 struct btrfs_root *root,
4287 struct inode *inode,
4288 u64 new_size, u32 min_type)
4290 struct btrfs_path *path;
4291 struct extent_buffer *leaf;
4292 struct btrfs_file_extent_item *fi;
4293 struct btrfs_key key;
4294 struct btrfs_key found_key;
4295 u64 extent_start = 0;
4296 u64 extent_num_bytes = 0;
4297 u64 extent_offset = 0;
4299 u64 last_size = new_size;
4300 u32 found_type = (u8)-1;
4303 int pending_del_nr = 0;
4304 int pending_del_slot = 0;
4305 int extent_type = -1;
4308 u64 ino = btrfs_ino(inode);
4309 u64 bytes_deleted = 0;
4311 bool should_throttle = 0;
4312 bool should_end = 0;
4314 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4317 * for non-free space inodes and ref cows, we want to back off from
4320 if (!btrfs_is_free_space_inode(inode) &&
4321 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4324 path = btrfs_alloc_path();
4330 * We want to drop from the next block forward in case this new size is
4331 * not block aligned since we will be keeping the last block of the
4332 * extent just the way it is.
4334 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4335 root == root->fs_info->tree_root)
4336 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4337 root->sectorsize), (u64)-1, 0);
4340 * This function is also used to drop the items in the log tree before
4341 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4342 * it is used to drop the loged items. So we shouldn't kill the delayed
4345 if (min_type == 0 && root == BTRFS_I(inode)->root)
4346 btrfs_kill_delayed_inode_items(inode);
4349 key.offset = (u64)-1;
4354 * with a 16K leaf size and 128MB extents, you can actually queue
4355 * up a huge file in a single leaf. Most of the time that
4356 * bytes_deleted is > 0, it will be huge by the time we get here
4358 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4359 if (btrfs_should_end_transaction(trans, root)) {
4366 path->leave_spinning = 1;
4367 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4374 /* there are no items in the tree for us to truncate, we're
4377 if (path->slots[0] == 0)
4384 leaf = path->nodes[0];
4385 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4386 found_type = found_key.type;
4388 if (found_key.objectid != ino)
4391 if (found_type < min_type)
4394 item_end = found_key.offset;
4395 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4396 fi = btrfs_item_ptr(leaf, path->slots[0],
4397 struct btrfs_file_extent_item);
4398 extent_type = btrfs_file_extent_type(leaf, fi);
4399 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4401 btrfs_file_extent_num_bytes(leaf, fi);
4402 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4403 item_end += btrfs_file_extent_inline_len(leaf,
4404 path->slots[0], fi);
4408 if (found_type > min_type) {
4411 if (item_end < new_size)
4413 if (found_key.offset >= new_size)
4419 /* FIXME, shrink the extent if the ref count is only 1 */
4420 if (found_type != BTRFS_EXTENT_DATA_KEY)
4424 last_size = found_key.offset;
4426 last_size = new_size;
4428 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4430 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4432 u64 orig_num_bytes =
4433 btrfs_file_extent_num_bytes(leaf, fi);
4434 extent_num_bytes = ALIGN(new_size -
4437 btrfs_set_file_extent_num_bytes(leaf, fi,
4439 num_dec = (orig_num_bytes -
4441 if (test_bit(BTRFS_ROOT_REF_COWS,
4444 inode_sub_bytes(inode, num_dec);
4445 btrfs_mark_buffer_dirty(leaf);
4448 btrfs_file_extent_disk_num_bytes(leaf,
4450 extent_offset = found_key.offset -
4451 btrfs_file_extent_offset(leaf, fi);
4453 /* FIXME blocksize != 4096 */
4454 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4455 if (extent_start != 0) {
4457 if (test_bit(BTRFS_ROOT_REF_COWS,
4459 inode_sub_bytes(inode, num_dec);
4462 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4464 * we can't truncate inline items that have had
4468 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4469 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4472 * Need to release path in order to truncate a
4473 * compressed extent. So delete any accumulated
4474 * extent items so far.
4476 if (btrfs_file_extent_compression(leaf, fi) !=
4477 BTRFS_COMPRESS_NONE && pending_del_nr) {
4478 err = btrfs_del_items(trans, root, path,
4482 btrfs_abort_transaction(trans,
4490 err = truncate_inline_extent(inode, path,
4495 btrfs_abort_transaction(trans,
4499 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4501 inode_sub_bytes(inode, item_end + 1 - new_size);
4506 if (!pending_del_nr) {
4507 /* no pending yet, add ourselves */
4508 pending_del_slot = path->slots[0];
4510 } else if (pending_del_nr &&
4511 path->slots[0] + 1 == pending_del_slot) {
4512 /* hop on the pending chunk */
4514 pending_del_slot = path->slots[0];
4521 should_throttle = 0;
4524 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4525 root == root->fs_info->tree_root)) {
4526 btrfs_set_path_blocking(path);
4527 bytes_deleted += extent_num_bytes;
4528 ret = btrfs_free_extent(trans, root, extent_start,
4529 extent_num_bytes, 0,
4530 btrfs_header_owner(leaf),
4531 ino, extent_offset);
4533 if (btrfs_should_throttle_delayed_refs(trans, root))
4534 btrfs_async_run_delayed_refs(root,
4535 trans->delayed_ref_updates * 2, 0);
4537 if (truncate_space_check(trans, root,
4538 extent_num_bytes)) {
4541 if (btrfs_should_throttle_delayed_refs(trans,
4543 should_throttle = 1;
4548 if (found_type == BTRFS_INODE_ITEM_KEY)
4551 if (path->slots[0] == 0 ||
4552 path->slots[0] != pending_del_slot ||
4553 should_throttle || should_end) {
4554 if (pending_del_nr) {
4555 ret = btrfs_del_items(trans, root, path,
4559 btrfs_abort_transaction(trans,
4565 btrfs_release_path(path);
4566 if (should_throttle) {
4567 unsigned long updates = trans->delayed_ref_updates;
4569 trans->delayed_ref_updates = 0;
4570 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4576 * if we failed to refill our space rsv, bail out
4577 * and let the transaction restart
4589 if (pending_del_nr) {
4590 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4593 btrfs_abort_transaction(trans, root, ret);
4596 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4597 btrfs_ordered_update_i_size(inode, last_size, NULL);
4599 btrfs_free_path(path);
4601 if (be_nice && bytes_deleted > 32 * 1024 * 1024) {
4602 unsigned long updates = trans->delayed_ref_updates;
4604 trans->delayed_ref_updates = 0;
4605 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4614 * btrfs_truncate_page - read, zero a chunk and write a page
4615 * @inode - inode that we're zeroing
4616 * @from - the offset to start zeroing
4617 * @len - the length to zero, 0 to zero the entire range respective to the
4619 * @front - zero up to the offset instead of from the offset on
4621 * This will find the page for the "from" offset and cow the page and zero the
4622 * part we want to zero. This is used with truncate and hole punching.
4624 int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4627 struct address_space *mapping = inode->i_mapping;
4628 struct btrfs_root *root = BTRFS_I(inode)->root;
4629 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4630 struct btrfs_ordered_extent *ordered;
4631 struct extent_state *cached_state = NULL;
4633 u32 blocksize = root->sectorsize;
4634 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4635 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4637 gfp_t mask = btrfs_alloc_write_mask(mapping);
4642 if ((offset & (blocksize - 1)) == 0 &&
4643 (!len || ((len & (blocksize - 1)) == 0)))
4645 ret = btrfs_delalloc_reserve_space(inode,
4646 round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE);
4651 page = find_or_create_page(mapping, index, mask);
4653 btrfs_delalloc_release_space(inode,
4654 round_down(from, PAGE_CACHE_SIZE),
4660 page_start = page_offset(page);
4661 page_end = page_start + PAGE_CACHE_SIZE - 1;
4663 if (!PageUptodate(page)) {
4664 ret = btrfs_readpage(NULL, page);
4666 if (page->mapping != mapping) {
4668 page_cache_release(page);
4671 if (!PageUptodate(page)) {
4676 wait_on_page_writeback(page);
4678 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
4679 set_page_extent_mapped(page);
4681 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4683 unlock_extent_cached(io_tree, page_start, page_end,
4684 &cached_state, GFP_NOFS);
4686 page_cache_release(page);
4687 btrfs_start_ordered_extent(inode, ordered, 1);
4688 btrfs_put_ordered_extent(ordered);
4692 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
4693 EXTENT_DIRTY | EXTENT_DELALLOC |
4694 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4695 0, 0, &cached_state, GFP_NOFS);
4697 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4700 unlock_extent_cached(io_tree, page_start, page_end,
4701 &cached_state, GFP_NOFS);
4705 if (offset != PAGE_CACHE_SIZE) {
4707 len = PAGE_CACHE_SIZE - offset;
4710 memset(kaddr, 0, offset);
4712 memset(kaddr + offset, 0, len);
4713 flush_dcache_page(page);
4716 ClearPageChecked(page);
4717 set_page_dirty(page);
4718 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4723 btrfs_delalloc_release_space(inode, page_start,
4726 page_cache_release(page);
4731 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4732 u64 offset, u64 len)
4734 struct btrfs_trans_handle *trans;
4738 * Still need to make sure the inode looks like it's been updated so
4739 * that any holes get logged if we fsync.
4741 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4742 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4743 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4744 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4749 * 1 - for the one we're dropping
4750 * 1 - for the one we're adding
4751 * 1 - for updating the inode.
4753 trans = btrfs_start_transaction(root, 3);
4755 return PTR_ERR(trans);
4757 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4759 btrfs_abort_transaction(trans, root, ret);
4760 btrfs_end_transaction(trans, root);
4764 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4765 0, 0, len, 0, len, 0, 0, 0);
4767 btrfs_abort_transaction(trans, root, ret);
4769 btrfs_update_inode(trans, root, inode);
4770 btrfs_end_transaction(trans, root);
4775 * This function puts in dummy file extents for the area we're creating a hole
4776 * for. So if we are truncating this file to a larger size we need to insert
4777 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4778 * the range between oldsize and size
4780 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4782 struct btrfs_root *root = BTRFS_I(inode)->root;
4783 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4784 struct extent_map *em = NULL;
4785 struct extent_state *cached_state = NULL;
4786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4787 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4788 u64 block_end = ALIGN(size, root->sectorsize);
4795 * If our size started in the middle of a page we need to zero out the
4796 * rest of the page before we expand the i_size, otherwise we could
4797 * expose stale data.
4799 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4803 if (size <= hole_start)
4807 struct btrfs_ordered_extent *ordered;
4809 lock_extent_bits(io_tree, hole_start, block_end - 1,
4811 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4812 block_end - hole_start);
4815 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4816 &cached_state, GFP_NOFS);
4817 btrfs_start_ordered_extent(inode, ordered, 1);
4818 btrfs_put_ordered_extent(ordered);
4821 cur_offset = hole_start;
4823 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4824 block_end - cur_offset, 0);
4830 last_byte = min(extent_map_end(em), block_end);
4831 last_byte = ALIGN(last_byte , root->sectorsize);
4832 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4833 struct extent_map *hole_em;
4834 hole_size = last_byte - cur_offset;
4836 err = maybe_insert_hole(root, inode, cur_offset,
4840 btrfs_drop_extent_cache(inode, cur_offset,
4841 cur_offset + hole_size - 1, 0);
4842 hole_em = alloc_extent_map();
4844 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4845 &BTRFS_I(inode)->runtime_flags);
4848 hole_em->start = cur_offset;
4849 hole_em->len = hole_size;
4850 hole_em->orig_start = cur_offset;
4852 hole_em->block_start = EXTENT_MAP_HOLE;
4853 hole_em->block_len = 0;
4854 hole_em->orig_block_len = 0;
4855 hole_em->ram_bytes = hole_size;
4856 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4857 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4858 hole_em->generation = root->fs_info->generation;
4861 write_lock(&em_tree->lock);
4862 err = add_extent_mapping(em_tree, hole_em, 1);
4863 write_unlock(&em_tree->lock);
4866 btrfs_drop_extent_cache(inode, cur_offset,
4870 free_extent_map(hole_em);
4873 free_extent_map(em);
4875 cur_offset = last_byte;
4876 if (cur_offset >= block_end)
4879 free_extent_map(em);
4880 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4885 static int wait_snapshoting_atomic_t(atomic_t *a)
4891 static void wait_for_snapshot_creation(struct btrfs_root *root)
4896 ret = btrfs_start_write_no_snapshoting(root);
4899 wait_on_atomic_t(&root->will_be_snapshoted,
4900 wait_snapshoting_atomic_t,
4901 TASK_UNINTERRUPTIBLE);
4905 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4907 struct btrfs_root *root = BTRFS_I(inode)->root;
4908 struct btrfs_trans_handle *trans;
4909 loff_t oldsize = i_size_read(inode);
4910 loff_t newsize = attr->ia_size;
4911 int mask = attr->ia_valid;
4915 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4916 * special case where we need to update the times despite not having
4917 * these flags set. For all other operations the VFS set these flags
4918 * explicitly if it wants a timestamp update.
4920 if (newsize != oldsize) {
4921 inode_inc_iversion(inode);
4922 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4923 inode->i_ctime = inode->i_mtime =
4924 current_fs_time(inode->i_sb);
4927 if (newsize > oldsize) {
4928 truncate_pagecache(inode, newsize);
4930 * Don't do an expanding truncate while snapshoting is ongoing.
4931 * This is to ensure the snapshot captures a fully consistent
4932 * state of this file - if the snapshot captures this expanding
4933 * truncation, it must capture all writes that happened before
4936 wait_for_snapshot_creation(root);
4937 ret = btrfs_cont_expand(inode, oldsize, newsize);
4939 btrfs_end_write_no_snapshoting(root);
4943 trans = btrfs_start_transaction(root, 1);
4944 if (IS_ERR(trans)) {
4945 btrfs_end_write_no_snapshoting(root);
4946 return PTR_ERR(trans);
4949 i_size_write(inode, newsize);
4950 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4951 ret = btrfs_update_inode(trans, root, inode);
4952 btrfs_end_write_no_snapshoting(root);
4953 btrfs_end_transaction(trans, root);
4957 * We're truncating a file that used to have good data down to
4958 * zero. Make sure it gets into the ordered flush list so that
4959 * any new writes get down to disk quickly.
4962 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4963 &BTRFS_I(inode)->runtime_flags);
4966 * 1 for the orphan item we're going to add
4967 * 1 for the orphan item deletion.
4969 trans = btrfs_start_transaction(root, 2);
4971 return PTR_ERR(trans);
4974 * We need to do this in case we fail at _any_ point during the
4975 * actual truncate. Once we do the truncate_setsize we could
4976 * invalidate pages which forces any outstanding ordered io to
4977 * be instantly completed which will give us extents that need
4978 * to be truncated. If we fail to get an orphan inode down we
4979 * could have left over extents that were never meant to live,
4980 * so we need to garuntee from this point on that everything
4981 * will be consistent.
4983 ret = btrfs_orphan_add(trans, inode);
4984 btrfs_end_transaction(trans, root);
4988 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4989 truncate_setsize(inode, newsize);
4991 /* Disable nonlocked read DIO to avoid the end less truncate */
4992 btrfs_inode_block_unlocked_dio(inode);
4993 inode_dio_wait(inode);
4994 btrfs_inode_resume_unlocked_dio(inode);
4996 ret = btrfs_truncate(inode);
4997 if (ret && inode->i_nlink) {
5001 * failed to truncate, disk_i_size is only adjusted down
5002 * as we remove extents, so it should represent the true
5003 * size of the inode, so reset the in memory size and
5004 * delete our orphan entry.
5006 trans = btrfs_join_transaction(root);
5007 if (IS_ERR(trans)) {
5008 btrfs_orphan_del(NULL, inode);
5011 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5012 err = btrfs_orphan_del(trans, inode);
5014 btrfs_abort_transaction(trans, root, err);
5015 btrfs_end_transaction(trans, root);
5022 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5024 struct inode *inode = d_inode(dentry);
5025 struct btrfs_root *root = BTRFS_I(inode)->root;
5028 if (btrfs_root_readonly(root))
5031 err = inode_change_ok(inode, attr);
5035 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5036 err = btrfs_setsize(inode, attr);
5041 if (attr->ia_valid) {
5042 setattr_copy(inode, attr);
5043 inode_inc_iversion(inode);
5044 err = btrfs_dirty_inode(inode);
5046 if (!err && attr->ia_valid & ATTR_MODE)
5047 err = posix_acl_chmod(inode, inode->i_mode);
5054 * While truncating the inode pages during eviction, we get the VFS calling
5055 * btrfs_invalidatepage() against each page of the inode. This is slow because
5056 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5057 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5058 * extent_state structures over and over, wasting lots of time.
5060 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5061 * those expensive operations on a per page basis and do only the ordered io
5062 * finishing, while we release here the extent_map and extent_state structures,
5063 * without the excessive merging and splitting.
5065 static void evict_inode_truncate_pages(struct inode *inode)
5067 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5068 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5069 struct rb_node *node;
5071 ASSERT(inode->i_state & I_FREEING);
5072 truncate_inode_pages_final(&inode->i_data);
5074 write_lock(&map_tree->lock);
5075 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5076 struct extent_map *em;
5078 node = rb_first(&map_tree->map);
5079 em = rb_entry(node, struct extent_map, rb_node);
5080 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5081 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5082 remove_extent_mapping(map_tree, em);
5083 free_extent_map(em);
5084 if (need_resched()) {
5085 write_unlock(&map_tree->lock);
5087 write_lock(&map_tree->lock);
5090 write_unlock(&map_tree->lock);
5093 * Keep looping until we have no more ranges in the io tree.
5094 * We can have ongoing bios started by readpages (called from readahead)
5095 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5096 * still in progress (unlocked the pages in the bio but did not yet
5097 * unlocked the ranges in the io tree). Therefore this means some
5098 * ranges can still be locked and eviction started because before
5099 * submitting those bios, which are executed by a separate task (work
5100 * queue kthread), inode references (inode->i_count) were not taken
5101 * (which would be dropped in the end io callback of each bio).
5102 * Therefore here we effectively end up waiting for those bios and
5103 * anyone else holding locked ranges without having bumped the inode's
5104 * reference count - if we don't do it, when they access the inode's
5105 * io_tree to unlock a range it may be too late, leading to an
5106 * use-after-free issue.
5108 spin_lock(&io_tree->lock);
5109 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5110 struct extent_state *state;
5111 struct extent_state *cached_state = NULL;
5115 node = rb_first(&io_tree->state);
5116 state = rb_entry(node, struct extent_state, rb_node);
5117 start = state->start;
5119 spin_unlock(&io_tree->lock);
5121 lock_extent_bits(io_tree, start, end, &cached_state);
5124 * If still has DELALLOC flag, the extent didn't reach disk,
5125 * and its reserved space won't be freed by delayed_ref.
5126 * So we need to free its reserved space here.
5127 * (Refer to comment in btrfs_invalidatepage, case 2)
5129 * Note, end is the bytenr of last byte, so we need + 1 here.
5131 if (state->state & EXTENT_DELALLOC)
5132 btrfs_qgroup_free_data(inode, start, end - start + 1);
5134 clear_extent_bit(io_tree, start, end,
5135 EXTENT_LOCKED | EXTENT_DIRTY |
5136 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5137 EXTENT_DEFRAG, 1, 1,
5138 &cached_state, GFP_NOFS);
5141 spin_lock(&io_tree->lock);
5143 spin_unlock(&io_tree->lock);
5146 void btrfs_evict_inode(struct inode *inode)
5148 struct btrfs_trans_handle *trans;
5149 struct btrfs_root *root = BTRFS_I(inode)->root;
5150 struct btrfs_block_rsv *rsv, *global_rsv;
5151 int steal_from_global = 0;
5152 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5155 trace_btrfs_inode_evict(inode);
5157 evict_inode_truncate_pages(inode);
5159 if (inode->i_nlink &&
5160 ((btrfs_root_refs(&root->root_item) != 0 &&
5161 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5162 btrfs_is_free_space_inode(inode)))
5165 if (is_bad_inode(inode)) {
5166 btrfs_orphan_del(NULL, inode);
5169 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5170 if (!special_file(inode->i_mode))
5171 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5173 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5175 if (root->fs_info->log_root_recovering) {
5176 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
5177 &BTRFS_I(inode)->runtime_flags));
5181 if (inode->i_nlink > 0) {
5182 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5183 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5187 ret = btrfs_commit_inode_delayed_inode(inode);
5189 btrfs_orphan_del(NULL, inode);
5193 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
5195 btrfs_orphan_del(NULL, inode);
5198 rsv->size = min_size;
5200 global_rsv = &root->fs_info->global_block_rsv;
5202 btrfs_i_size_write(inode, 0);
5205 * This is a bit simpler than btrfs_truncate since we've already
5206 * reserved our space for our orphan item in the unlink, so we just
5207 * need to reserve some slack space in case we add bytes and update
5208 * inode item when doing the truncate.
5211 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5212 BTRFS_RESERVE_FLUSH_LIMIT);
5215 * Try and steal from the global reserve since we will
5216 * likely not use this space anyway, we want to try as
5217 * hard as possible to get this to work.
5220 steal_from_global++;
5222 steal_from_global = 0;
5226 * steal_from_global == 0: we reserved stuff, hooray!
5227 * steal_from_global == 1: we didn't reserve stuff, boo!
5228 * steal_from_global == 2: we've committed, still not a lot of
5229 * room but maybe we'll have room in the global reserve this
5231 * steal_from_global == 3: abandon all hope!
5233 if (steal_from_global > 2) {
5234 btrfs_warn(root->fs_info,
5235 "Could not get space for a delete, will truncate on mount %d",
5237 btrfs_orphan_del(NULL, inode);
5238 btrfs_free_block_rsv(root, rsv);
5242 trans = btrfs_join_transaction(root);
5243 if (IS_ERR(trans)) {
5244 btrfs_orphan_del(NULL, inode);
5245 btrfs_free_block_rsv(root, rsv);
5250 * We can't just steal from the global reserve, we need tomake
5251 * sure there is room to do it, if not we need to commit and try
5254 if (steal_from_global) {
5255 if (!btrfs_check_space_for_delayed_refs(trans, root))
5256 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5263 * Couldn't steal from the global reserve, we have too much
5264 * pending stuff built up, commit the transaction and try it
5268 ret = btrfs_commit_transaction(trans, root);
5270 btrfs_orphan_del(NULL, inode);
5271 btrfs_free_block_rsv(root, rsv);
5276 steal_from_global = 0;
5279 trans->block_rsv = rsv;
5281 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5282 if (ret != -ENOSPC && ret != -EAGAIN)
5285 trans->block_rsv = &root->fs_info->trans_block_rsv;
5286 btrfs_end_transaction(trans, root);
5288 btrfs_btree_balance_dirty(root);
5291 btrfs_free_block_rsv(root, rsv);
5294 * Errors here aren't a big deal, it just means we leave orphan items
5295 * in the tree. They will be cleaned up on the next mount.
5298 trans->block_rsv = root->orphan_block_rsv;
5299 btrfs_orphan_del(trans, inode);
5301 btrfs_orphan_del(NULL, inode);
5304 trans->block_rsv = &root->fs_info->trans_block_rsv;
5305 if (!(root == root->fs_info->tree_root ||
5306 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5307 btrfs_return_ino(root, btrfs_ino(inode));
5309 btrfs_end_transaction(trans, root);
5310 btrfs_btree_balance_dirty(root);
5312 btrfs_remove_delayed_node(inode);
5318 * this returns the key found in the dir entry in the location pointer.
5319 * If no dir entries were found, location->objectid is 0.
5321 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5322 struct btrfs_key *location)
5324 const char *name = dentry->d_name.name;
5325 int namelen = dentry->d_name.len;
5326 struct btrfs_dir_item *di;
5327 struct btrfs_path *path;
5328 struct btrfs_root *root = BTRFS_I(dir)->root;
5331 path = btrfs_alloc_path();
5335 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
5340 if (IS_ERR_OR_NULL(di))
5343 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5345 btrfs_free_path(path);
5348 location->objectid = 0;
5353 * when we hit a tree root in a directory, the btrfs part of the inode
5354 * needs to be changed to reflect the root directory of the tree root. This
5355 * is kind of like crossing a mount point.
5357 static int fixup_tree_root_location(struct btrfs_root *root,
5359 struct dentry *dentry,
5360 struct btrfs_key *location,
5361 struct btrfs_root **sub_root)
5363 struct btrfs_path *path;
5364 struct btrfs_root *new_root;
5365 struct btrfs_root_ref *ref;
5366 struct extent_buffer *leaf;
5367 struct btrfs_key key;
5371 path = btrfs_alloc_path();
5378 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5379 key.type = BTRFS_ROOT_REF_KEY;
5380 key.offset = location->objectid;
5382 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5390 leaf = path->nodes[0];
5391 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5392 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
5393 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5396 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5397 (unsigned long)(ref + 1),
5398 dentry->d_name.len);
5402 btrfs_release_path(path);
5404 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5405 if (IS_ERR(new_root)) {
5406 err = PTR_ERR(new_root);
5410 *sub_root = new_root;
5411 location->objectid = btrfs_root_dirid(&new_root->root_item);
5412 location->type = BTRFS_INODE_ITEM_KEY;
5413 location->offset = 0;
5416 btrfs_free_path(path);
5420 static void inode_tree_add(struct inode *inode)
5422 struct btrfs_root *root = BTRFS_I(inode)->root;
5423 struct btrfs_inode *entry;
5425 struct rb_node *parent;
5426 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5427 u64 ino = btrfs_ino(inode);
5429 if (inode_unhashed(inode))
5432 spin_lock(&root->inode_lock);
5433 p = &root->inode_tree.rb_node;
5436 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5438 if (ino < btrfs_ino(&entry->vfs_inode))
5439 p = &parent->rb_left;
5440 else if (ino > btrfs_ino(&entry->vfs_inode))
5441 p = &parent->rb_right;
5443 WARN_ON(!(entry->vfs_inode.i_state &
5444 (I_WILL_FREE | I_FREEING)));
5445 rb_replace_node(parent, new, &root->inode_tree);
5446 RB_CLEAR_NODE(parent);
5447 spin_unlock(&root->inode_lock);
5451 rb_link_node(new, parent, p);
5452 rb_insert_color(new, &root->inode_tree);
5453 spin_unlock(&root->inode_lock);
5456 static void inode_tree_del(struct inode *inode)
5458 struct btrfs_root *root = BTRFS_I(inode)->root;
5461 spin_lock(&root->inode_lock);
5462 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5463 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5464 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
5465 empty = RB_EMPTY_ROOT(&root->inode_tree);
5467 spin_unlock(&root->inode_lock);
5469 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5470 synchronize_srcu(&root->fs_info->subvol_srcu);
5471 spin_lock(&root->inode_lock);
5472 empty = RB_EMPTY_ROOT(&root->inode_tree);
5473 spin_unlock(&root->inode_lock);
5475 btrfs_add_dead_root(root);
5479 void btrfs_invalidate_inodes(struct btrfs_root *root)
5481 struct rb_node *node;
5482 struct rb_node *prev;
5483 struct btrfs_inode *entry;
5484 struct inode *inode;
5487 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5488 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5490 spin_lock(&root->inode_lock);
5492 node = root->inode_tree.rb_node;
5496 entry = rb_entry(node, struct btrfs_inode, rb_node);
5498 if (objectid < btrfs_ino(&entry->vfs_inode))
5499 node = node->rb_left;
5500 else if (objectid > btrfs_ino(&entry->vfs_inode))
5501 node = node->rb_right;
5507 entry = rb_entry(prev, struct btrfs_inode, rb_node);
5508 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
5512 prev = rb_next(prev);
5516 entry = rb_entry(node, struct btrfs_inode, rb_node);
5517 objectid = btrfs_ino(&entry->vfs_inode) + 1;
5518 inode = igrab(&entry->vfs_inode);
5520 spin_unlock(&root->inode_lock);
5521 if (atomic_read(&inode->i_count) > 1)
5522 d_prune_aliases(inode);
5524 * btrfs_drop_inode will have it removed from
5525 * the inode cache when its usage count
5530 spin_lock(&root->inode_lock);
5534 if (cond_resched_lock(&root->inode_lock))
5537 node = rb_next(node);
5539 spin_unlock(&root->inode_lock);
5542 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5544 struct btrfs_iget_args *args = p;
5545 inode->i_ino = args->location->objectid;
5546 memcpy(&BTRFS_I(inode)->location, args->location,
5547 sizeof(*args->location));
5548 BTRFS_I(inode)->root = args->root;
5552 static int btrfs_find_actor(struct inode *inode, void *opaque)
5554 struct btrfs_iget_args *args = opaque;
5555 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5556 args->root == BTRFS_I(inode)->root;
5559 static struct inode *btrfs_iget_locked(struct super_block *s,
5560 struct btrfs_key *location,
5561 struct btrfs_root *root)
5563 struct inode *inode;
5564 struct btrfs_iget_args args;
5565 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5567 args.location = location;
5570 inode = iget5_locked(s, hashval, btrfs_find_actor,
5571 btrfs_init_locked_inode,
5576 /* Get an inode object given its location and corresponding root.
5577 * Returns in *is_new if the inode was read from disk
5579 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5580 struct btrfs_root *root, int *new)
5582 struct inode *inode;
5584 inode = btrfs_iget_locked(s, location, root);
5586 return ERR_PTR(-ENOMEM);
5588 if (inode->i_state & I_NEW) {
5589 btrfs_read_locked_inode(inode);
5590 if (!is_bad_inode(inode)) {
5591 inode_tree_add(inode);
5592 unlock_new_inode(inode);
5596 unlock_new_inode(inode);
5598 inode = ERR_PTR(-ESTALE);
5605 static struct inode *new_simple_dir(struct super_block *s,
5606 struct btrfs_key *key,
5607 struct btrfs_root *root)
5609 struct inode *inode = new_inode(s);
5612 return ERR_PTR(-ENOMEM);
5614 BTRFS_I(inode)->root = root;
5615 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5616 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5618 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5619 inode->i_op = &btrfs_dir_ro_inode_operations;
5620 inode->i_fop = &simple_dir_operations;
5621 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5622 inode->i_mtime = CURRENT_TIME;
5623 inode->i_atime = inode->i_mtime;
5624 inode->i_ctime = inode->i_mtime;
5625 BTRFS_I(inode)->i_otime = inode->i_mtime;
5630 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5632 struct inode *inode;
5633 struct btrfs_root *root = BTRFS_I(dir)->root;
5634 struct btrfs_root *sub_root = root;
5635 struct btrfs_key location;
5639 if (dentry->d_name.len > BTRFS_NAME_LEN)
5640 return ERR_PTR(-ENAMETOOLONG);
5642 ret = btrfs_inode_by_name(dir, dentry, &location);
5644 return ERR_PTR(ret);
5646 if (location.objectid == 0)
5647 return ERR_PTR(-ENOENT);
5649 if (location.type == BTRFS_INODE_ITEM_KEY) {
5650 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5654 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5656 index = srcu_read_lock(&root->fs_info->subvol_srcu);
5657 ret = fixup_tree_root_location(root, dir, dentry,
5658 &location, &sub_root);
5661 inode = ERR_PTR(ret);
5663 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5665 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5667 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5669 if (!IS_ERR(inode) && root != sub_root) {
5670 down_read(&root->fs_info->cleanup_work_sem);
5671 if (!(inode->i_sb->s_flags & MS_RDONLY))
5672 ret = btrfs_orphan_cleanup(sub_root);
5673 up_read(&root->fs_info->cleanup_work_sem);
5676 inode = ERR_PTR(ret);
5683 static int btrfs_dentry_delete(const struct dentry *dentry)
5685 struct btrfs_root *root;
5686 struct inode *inode = d_inode(dentry);
5688 if (!inode && !IS_ROOT(dentry))
5689 inode = d_inode(dentry->d_parent);
5692 root = BTRFS_I(inode)->root;
5693 if (btrfs_root_refs(&root->root_item) == 0)
5696 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5702 static void btrfs_dentry_release(struct dentry *dentry)
5704 kfree(dentry->d_fsdata);
5707 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5710 struct inode *inode;
5712 inode = btrfs_lookup_dentry(dir, dentry);
5713 if (IS_ERR(inode)) {
5714 if (PTR_ERR(inode) == -ENOENT)
5717 return ERR_CAST(inode);
5720 return d_splice_alias(inode, dentry);
5723 unsigned char btrfs_filetype_table[] = {
5724 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5727 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5729 struct inode *inode = file_inode(file);
5730 struct btrfs_root *root = BTRFS_I(inode)->root;
5731 struct btrfs_item *item;
5732 struct btrfs_dir_item *di;
5733 struct btrfs_key key;
5734 struct btrfs_key found_key;
5735 struct btrfs_path *path;
5736 struct list_head ins_list;
5737 struct list_head del_list;
5739 struct extent_buffer *leaf;
5741 unsigned char d_type;
5746 int key_type = BTRFS_DIR_INDEX_KEY;
5750 int is_curr = 0; /* ctx->pos points to the current index? */
5752 /* FIXME, use a real flag for deciding about the key type */
5753 if (root->fs_info->tree_root == root)
5754 key_type = BTRFS_DIR_ITEM_KEY;
5756 if (!dir_emit_dots(file, ctx))
5759 path = btrfs_alloc_path();
5765 if (key_type == BTRFS_DIR_INDEX_KEY) {
5766 INIT_LIST_HEAD(&ins_list);
5767 INIT_LIST_HEAD(&del_list);
5768 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5771 key.type = key_type;
5772 key.offset = ctx->pos;
5773 key.objectid = btrfs_ino(inode);
5775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5780 leaf = path->nodes[0];
5781 slot = path->slots[0];
5782 if (slot >= btrfs_header_nritems(leaf)) {
5783 ret = btrfs_next_leaf(root, path);
5791 item = btrfs_item_nr(slot);
5792 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5794 if (found_key.objectid != key.objectid)
5796 if (found_key.type != key_type)
5798 if (found_key.offset < ctx->pos)
5800 if (key_type == BTRFS_DIR_INDEX_KEY &&
5801 btrfs_should_delete_dir_index(&del_list,
5805 ctx->pos = found_key.offset;
5808 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5810 di_total = btrfs_item_size(leaf, item);
5812 while (di_cur < di_total) {
5813 struct btrfs_key location;
5815 if (verify_dir_item(root, leaf, di))
5818 name_len = btrfs_dir_name_len(leaf, di);
5819 if (name_len <= sizeof(tmp_name)) {
5820 name_ptr = tmp_name;
5822 name_ptr = kmalloc(name_len, GFP_NOFS);
5828 read_extent_buffer(leaf, name_ptr,
5829 (unsigned long)(di + 1), name_len);
5831 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5832 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5835 /* is this a reference to our own snapshot? If so
5838 * In contrast to old kernels, we insert the snapshot's
5839 * dir item and dir index after it has been created, so
5840 * we won't find a reference to our own snapshot. We
5841 * still keep the following code for backward
5844 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5845 location.objectid == root->root_key.objectid) {
5849 over = !dir_emit(ctx, name_ptr, name_len,
5850 location.objectid, d_type);
5853 if (name_ptr != tmp_name)
5858 di_len = btrfs_dir_name_len(leaf, di) +
5859 btrfs_dir_data_len(leaf, di) + sizeof(*di);
5861 di = (struct btrfs_dir_item *)((char *)di + di_len);
5867 if (key_type == BTRFS_DIR_INDEX_KEY) {
5870 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5875 /* Reached end of directory/root. Bump pos past the last item. */
5879 * Stop new entries from being returned after we return the last
5882 * New directory entries are assigned a strictly increasing
5883 * offset. This means that new entries created during readdir
5884 * are *guaranteed* to be seen in the future by that readdir.
5885 * This has broken buggy programs which operate on names as
5886 * they're returned by readdir. Until we re-use freed offsets
5887 * we have this hack to stop new entries from being returned
5888 * under the assumption that they'll never reach this huge
5891 * This is being careful not to overflow 32bit loff_t unless the
5892 * last entry requires it because doing so has broken 32bit apps
5895 if (key_type == BTRFS_DIR_INDEX_KEY) {
5896 if (ctx->pos >= INT_MAX)
5897 ctx->pos = LLONG_MAX;
5904 if (key_type == BTRFS_DIR_INDEX_KEY)
5905 btrfs_put_delayed_items(&ins_list, &del_list);
5906 btrfs_free_path(path);
5910 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5912 struct btrfs_root *root = BTRFS_I(inode)->root;
5913 struct btrfs_trans_handle *trans;
5915 bool nolock = false;
5917 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5920 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5923 if (wbc->sync_mode == WB_SYNC_ALL) {
5925 trans = btrfs_join_transaction_nolock(root);
5927 trans = btrfs_join_transaction(root);
5929 return PTR_ERR(trans);
5930 ret = btrfs_commit_transaction(trans, root);
5936 * This is somewhat expensive, updating the tree every time the
5937 * inode changes. But, it is most likely to find the inode in cache.
5938 * FIXME, needs more benchmarking...there are no reasons other than performance
5939 * to keep or drop this code.
5941 static int btrfs_dirty_inode(struct inode *inode)
5943 struct btrfs_root *root = BTRFS_I(inode)->root;
5944 struct btrfs_trans_handle *trans;
5947 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5950 trans = btrfs_join_transaction(root);
5952 return PTR_ERR(trans);
5954 ret = btrfs_update_inode(trans, root, inode);
5955 if (ret && ret == -ENOSPC) {
5956 /* whoops, lets try again with the full transaction */
5957 btrfs_end_transaction(trans, root);
5958 trans = btrfs_start_transaction(root, 1);
5960 return PTR_ERR(trans);
5962 ret = btrfs_update_inode(trans, root, inode);
5964 btrfs_end_transaction(trans, root);
5965 if (BTRFS_I(inode)->delayed_node)
5966 btrfs_balance_delayed_items(root);
5972 * This is a copy of file_update_time. We need this so we can return error on
5973 * ENOSPC for updating the inode in the case of file write and mmap writes.
5975 static int btrfs_update_time(struct inode *inode, struct timespec *now,
5978 struct btrfs_root *root = BTRFS_I(inode)->root;
5980 if (btrfs_root_readonly(root))
5983 if (flags & S_VERSION)
5984 inode_inc_iversion(inode);
5985 if (flags & S_CTIME)
5986 inode->i_ctime = *now;
5987 if (flags & S_MTIME)
5988 inode->i_mtime = *now;
5989 if (flags & S_ATIME)
5990 inode->i_atime = *now;
5991 return btrfs_dirty_inode(inode);
5995 * find the highest existing sequence number in a directory
5996 * and then set the in-memory index_cnt variable to reflect
5997 * free sequence numbers
5999 static int btrfs_set_inode_index_count(struct inode *inode)
6001 struct btrfs_root *root = BTRFS_I(inode)->root;
6002 struct btrfs_key key, found_key;
6003 struct btrfs_path *path;
6004 struct extent_buffer *leaf;
6007 key.objectid = btrfs_ino(inode);
6008 key.type = BTRFS_DIR_INDEX_KEY;
6009 key.offset = (u64)-1;
6011 path = btrfs_alloc_path();
6015 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6018 /* FIXME: we should be able to handle this */
6024 * MAGIC NUMBER EXPLANATION:
6025 * since we search a directory based on f_pos we have to start at 2
6026 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6027 * else has to start at 2
6029 if (path->slots[0] == 0) {
6030 BTRFS_I(inode)->index_cnt = 2;
6036 leaf = path->nodes[0];
6037 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6039 if (found_key.objectid != btrfs_ino(inode) ||
6040 found_key.type != BTRFS_DIR_INDEX_KEY) {
6041 BTRFS_I(inode)->index_cnt = 2;
6045 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6047 btrfs_free_path(path);
6052 * helper to find a free sequence number in a given directory. This current
6053 * code is very simple, later versions will do smarter things in the btree
6055 int btrfs_set_inode_index(struct inode *dir, u64 *index)
6059 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
6060 ret = btrfs_inode_delayed_dir_index_count(dir);
6062 ret = btrfs_set_inode_index_count(dir);
6068 *index = BTRFS_I(dir)->index_cnt;
6069 BTRFS_I(dir)->index_cnt++;
6074 static int btrfs_insert_inode_locked(struct inode *inode)
6076 struct btrfs_iget_args args;
6077 args.location = &BTRFS_I(inode)->location;
6078 args.root = BTRFS_I(inode)->root;
6080 return insert_inode_locked4(inode,
6081 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6082 btrfs_find_actor, &args);
6085 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6086 struct btrfs_root *root,
6088 const char *name, int name_len,
6089 u64 ref_objectid, u64 objectid,
6090 umode_t mode, u64 *index)
6092 struct inode *inode;
6093 struct btrfs_inode_item *inode_item;
6094 struct btrfs_key *location;
6095 struct btrfs_path *path;
6096 struct btrfs_inode_ref *ref;
6097 struct btrfs_key key[2];
6099 int nitems = name ? 2 : 1;
6103 path = btrfs_alloc_path();
6105 return ERR_PTR(-ENOMEM);
6107 inode = new_inode(root->fs_info->sb);
6109 btrfs_free_path(path);
6110 return ERR_PTR(-ENOMEM);
6114 * O_TMPFILE, set link count to 0, so that after this point,
6115 * we fill in an inode item with the correct link count.
6118 set_nlink(inode, 0);
6121 * we have to initialize this early, so we can reclaim the inode
6122 * number if we fail afterwards in this function.
6124 inode->i_ino = objectid;
6127 trace_btrfs_inode_request(dir);
6129 ret = btrfs_set_inode_index(dir, index);
6131 btrfs_free_path(path);
6133 return ERR_PTR(ret);
6139 * index_cnt is ignored for everything but a dir,
6140 * btrfs_get_inode_index_count has an explanation for the magic
6143 BTRFS_I(inode)->index_cnt = 2;
6144 BTRFS_I(inode)->dir_index = *index;
6145 BTRFS_I(inode)->root = root;
6146 BTRFS_I(inode)->generation = trans->transid;
6147 inode->i_generation = BTRFS_I(inode)->generation;
6150 * We could have gotten an inode number from somebody who was fsynced
6151 * and then removed in this same transaction, so let's just set full
6152 * sync since it will be a full sync anyway and this will blow away the
6153 * old info in the log.
6155 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6157 key[0].objectid = objectid;
6158 key[0].type = BTRFS_INODE_ITEM_KEY;
6161 sizes[0] = sizeof(struct btrfs_inode_item);
6165 * Start new inodes with an inode_ref. This is slightly more
6166 * efficient for small numbers of hard links since they will
6167 * be packed into one item. Extended refs will kick in if we
6168 * add more hard links than can fit in the ref item.
6170 key[1].objectid = objectid;
6171 key[1].type = BTRFS_INODE_REF_KEY;
6172 key[1].offset = ref_objectid;
6174 sizes[1] = name_len + sizeof(*ref);
6177 location = &BTRFS_I(inode)->location;
6178 location->objectid = objectid;
6179 location->offset = 0;
6180 location->type = BTRFS_INODE_ITEM_KEY;
6182 ret = btrfs_insert_inode_locked(inode);
6186 path->leave_spinning = 1;
6187 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6191 inode_init_owner(inode, dir, mode);
6192 inode_set_bytes(inode, 0);
6194 inode->i_mtime = CURRENT_TIME;
6195 inode->i_atime = inode->i_mtime;
6196 inode->i_ctime = inode->i_mtime;
6197 BTRFS_I(inode)->i_otime = inode->i_mtime;
6199 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6200 struct btrfs_inode_item);
6201 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6202 sizeof(*inode_item));
6203 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6206 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6207 struct btrfs_inode_ref);
6208 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6209 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6210 ptr = (unsigned long)(ref + 1);
6211 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6214 btrfs_mark_buffer_dirty(path->nodes[0]);
6215 btrfs_free_path(path);
6217 btrfs_inherit_iflags(inode, dir);
6219 if (S_ISREG(mode)) {
6220 if (btrfs_test_opt(root, NODATASUM))
6221 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6222 if (btrfs_test_opt(root, NODATACOW))
6223 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6224 BTRFS_INODE_NODATASUM;
6227 inode_tree_add(inode);
6229 trace_btrfs_inode_new(inode);
6230 btrfs_set_inode_last_trans(trans, inode);
6232 btrfs_update_root_times(trans, root);
6234 ret = btrfs_inode_inherit_props(trans, inode, dir);
6236 btrfs_err(root->fs_info,
6237 "error inheriting props for ino %llu (root %llu): %d",
6238 btrfs_ino(inode), root->root_key.objectid, ret);
6243 unlock_new_inode(inode);
6246 BTRFS_I(dir)->index_cnt--;
6247 btrfs_free_path(path);
6249 return ERR_PTR(ret);
6252 static inline u8 btrfs_inode_type(struct inode *inode)
6254 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6258 * utility function to add 'inode' into 'parent_inode' with
6259 * a give name and a given sequence number.
6260 * if 'add_backref' is true, also insert a backref from the
6261 * inode to the parent directory.
6263 int btrfs_add_link(struct btrfs_trans_handle *trans,
6264 struct inode *parent_inode, struct inode *inode,
6265 const char *name, int name_len, int add_backref, u64 index)
6268 struct btrfs_key key;
6269 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
6270 u64 ino = btrfs_ino(inode);
6271 u64 parent_ino = btrfs_ino(parent_inode);
6273 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6274 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6277 key.type = BTRFS_INODE_ITEM_KEY;
6281 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6282 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6283 key.objectid, root->root_key.objectid,
6284 parent_ino, index, name, name_len);
6285 } else if (add_backref) {
6286 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6290 /* Nothing to clean up yet */
6294 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6296 btrfs_inode_type(inode), index);
6297 if (ret == -EEXIST || ret == -EOVERFLOW)
6300 btrfs_abort_transaction(trans, root, ret);
6304 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6306 inode_inc_iversion(parent_inode);
6307 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
6308 ret = btrfs_update_inode(trans, root, parent_inode);
6310 btrfs_abort_transaction(trans, root, ret);
6314 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6317 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6318 key.objectid, root->root_key.objectid,
6319 parent_ino, &local_index, name, name_len);
6321 } else if (add_backref) {
6325 err = btrfs_del_inode_ref(trans, root, name, name_len,
6326 ino, parent_ino, &local_index);
6331 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6332 struct inode *dir, struct dentry *dentry,
6333 struct inode *inode, int backref, u64 index)
6335 int err = btrfs_add_link(trans, dir, inode,
6336 dentry->d_name.name, dentry->d_name.len,
6343 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6344 umode_t mode, dev_t rdev)
6346 struct btrfs_trans_handle *trans;
6347 struct btrfs_root *root = BTRFS_I(dir)->root;
6348 struct inode *inode = NULL;
6355 * 2 for inode item and ref
6357 * 1 for xattr if selinux is on
6359 trans = btrfs_start_transaction(root, 5);
6361 return PTR_ERR(trans);
6363 err = btrfs_find_free_ino(root, &objectid);
6367 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6368 dentry->d_name.len, btrfs_ino(dir), objectid,
6370 if (IS_ERR(inode)) {
6371 err = PTR_ERR(inode);
6376 * If the active LSM wants to access the inode during
6377 * d_instantiate it needs these. Smack checks to see
6378 * if the filesystem supports xattrs by looking at the
6381 inode->i_op = &btrfs_special_inode_operations;
6382 init_special_inode(inode, inode->i_mode, rdev);
6384 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6386 goto out_unlock_inode;
6388 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6390 goto out_unlock_inode;
6392 btrfs_update_inode(trans, root, inode);
6393 unlock_new_inode(inode);
6394 d_instantiate(dentry, inode);
6398 btrfs_end_transaction(trans, root);
6399 btrfs_balance_delayed_items(root);
6400 btrfs_btree_balance_dirty(root);
6402 inode_dec_link_count(inode);
6409 unlock_new_inode(inode);
6414 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6415 umode_t mode, bool excl)
6417 struct btrfs_trans_handle *trans;
6418 struct btrfs_root *root = BTRFS_I(dir)->root;
6419 struct inode *inode = NULL;
6420 int drop_inode_on_err = 0;
6426 * 2 for inode item and ref
6428 * 1 for xattr if selinux is on
6430 trans = btrfs_start_transaction(root, 5);
6432 return PTR_ERR(trans);
6434 err = btrfs_find_free_ino(root, &objectid);
6438 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6439 dentry->d_name.len, btrfs_ino(dir), objectid,
6441 if (IS_ERR(inode)) {
6442 err = PTR_ERR(inode);
6445 drop_inode_on_err = 1;
6447 * If the active LSM wants to access the inode during
6448 * d_instantiate it needs these. Smack checks to see
6449 * if the filesystem supports xattrs by looking at the
6452 inode->i_fop = &btrfs_file_operations;
6453 inode->i_op = &btrfs_file_inode_operations;
6454 inode->i_mapping->a_ops = &btrfs_aops;
6456 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6458 goto out_unlock_inode;
6460 err = btrfs_update_inode(trans, root, inode);
6462 goto out_unlock_inode;
6464 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6466 goto out_unlock_inode;
6468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6469 unlock_new_inode(inode);
6470 d_instantiate(dentry, inode);
6473 btrfs_end_transaction(trans, root);
6474 if (err && drop_inode_on_err) {
6475 inode_dec_link_count(inode);
6478 btrfs_balance_delayed_items(root);
6479 btrfs_btree_balance_dirty(root);
6483 unlock_new_inode(inode);
6488 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6489 struct dentry *dentry)
6491 struct btrfs_trans_handle *trans = NULL;
6492 struct btrfs_root *root = BTRFS_I(dir)->root;
6493 struct inode *inode = d_inode(old_dentry);
6498 /* do not allow sys_link's with other subvols of the same device */
6499 if (root->objectid != BTRFS_I(inode)->root->objectid)
6502 if (inode->i_nlink >= BTRFS_LINK_MAX)
6505 err = btrfs_set_inode_index(dir, &index);
6510 * 2 items for inode and inode ref
6511 * 2 items for dir items
6512 * 1 item for parent inode
6514 trans = btrfs_start_transaction(root, 5);
6515 if (IS_ERR(trans)) {
6516 err = PTR_ERR(trans);
6521 /* There are several dir indexes for this inode, clear the cache. */
6522 BTRFS_I(inode)->dir_index = 0ULL;
6524 inode_inc_iversion(inode);
6525 inode->i_ctime = CURRENT_TIME;
6527 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6529 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
6534 struct dentry *parent = dentry->d_parent;
6535 err = btrfs_update_inode(trans, root, inode);
6538 if (inode->i_nlink == 1) {
6540 * If new hard link count is 1, it's a file created
6541 * with open(2) O_TMPFILE flag.
6543 err = btrfs_orphan_del(trans, inode);
6547 d_instantiate(dentry, inode);
6548 btrfs_log_new_name(trans, inode, NULL, parent);
6551 btrfs_balance_delayed_items(root);
6554 btrfs_end_transaction(trans, root);
6556 inode_dec_link_count(inode);
6559 btrfs_btree_balance_dirty(root);
6563 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6565 struct inode *inode = NULL;
6566 struct btrfs_trans_handle *trans;
6567 struct btrfs_root *root = BTRFS_I(dir)->root;
6569 int drop_on_err = 0;
6574 * 2 items for inode and ref
6575 * 2 items for dir items
6576 * 1 for xattr if selinux is on
6578 trans = btrfs_start_transaction(root, 5);
6580 return PTR_ERR(trans);
6582 err = btrfs_find_free_ino(root, &objectid);
6586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6587 dentry->d_name.len, btrfs_ino(dir), objectid,
6588 S_IFDIR | mode, &index);
6589 if (IS_ERR(inode)) {
6590 err = PTR_ERR(inode);
6595 /* these must be set before we unlock the inode */
6596 inode->i_op = &btrfs_dir_inode_operations;
6597 inode->i_fop = &btrfs_dir_file_operations;
6599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6601 goto out_fail_inode;
6603 btrfs_i_size_write(inode, 0);
6604 err = btrfs_update_inode(trans, root, inode);
6606 goto out_fail_inode;
6608 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6609 dentry->d_name.len, 0, index);
6611 goto out_fail_inode;
6613 d_instantiate(dentry, inode);
6615 * mkdir is special. We're unlocking after we call d_instantiate
6616 * to avoid a race with nfsd calling d_instantiate.
6618 unlock_new_inode(inode);
6622 btrfs_end_transaction(trans, root);
6624 inode_dec_link_count(inode);
6627 btrfs_balance_delayed_items(root);
6628 btrfs_btree_balance_dirty(root);
6632 unlock_new_inode(inode);
6636 /* Find next extent map of a given extent map, caller needs to ensure locks */
6637 static struct extent_map *next_extent_map(struct extent_map *em)
6639 struct rb_node *next;
6641 next = rb_next(&em->rb_node);
6644 return container_of(next, struct extent_map, rb_node);
6647 static struct extent_map *prev_extent_map(struct extent_map *em)
6649 struct rb_node *prev;
6651 prev = rb_prev(&em->rb_node);
6654 return container_of(prev, struct extent_map, rb_node);
6657 /* helper for btfs_get_extent. Given an existing extent in the tree,
6658 * the existing extent is the nearest extent to map_start,
6659 * and an extent that you want to insert, deal with overlap and insert
6660 * the best fitted new extent into the tree.
6662 static int merge_extent_mapping(struct extent_map_tree *em_tree,
6663 struct extent_map *existing,
6664 struct extent_map *em,
6667 struct extent_map *prev;
6668 struct extent_map *next;
6673 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6675 if (existing->start > map_start) {
6677 prev = prev_extent_map(next);
6680 next = next_extent_map(prev);
6683 start = prev ? extent_map_end(prev) : em->start;
6684 start = max_t(u64, start, em->start);
6685 end = next ? next->start : extent_map_end(em);
6686 end = min_t(u64, end, extent_map_end(em));
6687 start_diff = start - em->start;
6689 em->len = end - start;
6690 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6691 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6692 em->block_start += start_diff;
6693 em->block_len -= start_diff;
6695 return add_extent_mapping(em_tree, em, 0);
6698 static noinline int uncompress_inline(struct btrfs_path *path,
6699 struct inode *inode, struct page *page,
6700 size_t pg_offset, u64 extent_offset,
6701 struct btrfs_file_extent_item *item)
6704 struct extent_buffer *leaf = path->nodes[0];
6707 unsigned long inline_size;
6711 WARN_ON(pg_offset != 0);
6712 compress_type = btrfs_file_extent_compression(leaf, item);
6713 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6714 inline_size = btrfs_file_extent_inline_item_len(leaf,
6715 btrfs_item_nr(path->slots[0]));
6716 tmp = kmalloc(inline_size, GFP_NOFS);
6719 ptr = btrfs_file_extent_inline_start(item);
6721 read_extent_buffer(leaf, tmp, ptr, inline_size);
6723 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
6724 ret = btrfs_decompress(compress_type, tmp, page,
6725 extent_offset, inline_size, max_size);
6731 * a bit scary, this does extent mapping from logical file offset to the disk.
6732 * the ugly parts come from merging extents from the disk with the in-ram
6733 * representation. This gets more complex because of the data=ordered code,
6734 * where the in-ram extents might be locked pending data=ordered completion.
6736 * This also copies inline extents directly into the page.
6739 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6740 size_t pg_offset, u64 start, u64 len,
6745 u64 extent_start = 0;
6747 u64 objectid = btrfs_ino(inode);
6749 struct btrfs_path *path = NULL;
6750 struct btrfs_root *root = BTRFS_I(inode)->root;
6751 struct btrfs_file_extent_item *item;
6752 struct extent_buffer *leaf;
6753 struct btrfs_key found_key;
6754 struct extent_map *em = NULL;
6755 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6756 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6757 struct btrfs_trans_handle *trans = NULL;
6758 const bool new_inline = !page || create;
6761 read_lock(&em_tree->lock);
6762 em = lookup_extent_mapping(em_tree, start, len);
6764 em->bdev = root->fs_info->fs_devices->latest_bdev;
6765 read_unlock(&em_tree->lock);
6768 if (em->start > start || em->start + em->len <= start)
6769 free_extent_map(em);
6770 else if (em->block_start == EXTENT_MAP_INLINE && page)
6771 free_extent_map(em);
6775 em = alloc_extent_map();
6780 em->bdev = root->fs_info->fs_devices->latest_bdev;
6781 em->start = EXTENT_MAP_HOLE;
6782 em->orig_start = EXTENT_MAP_HOLE;
6784 em->block_len = (u64)-1;
6787 path = btrfs_alloc_path();
6793 * Chances are we'll be called again, so go ahead and do
6799 ret = btrfs_lookup_file_extent(trans, root, path,
6800 objectid, start, trans != NULL);
6807 if (path->slots[0] == 0)
6812 leaf = path->nodes[0];
6813 item = btrfs_item_ptr(leaf, path->slots[0],
6814 struct btrfs_file_extent_item);
6815 /* are we inside the extent that was found? */
6816 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6817 found_type = found_key.type;
6818 if (found_key.objectid != objectid ||
6819 found_type != BTRFS_EXTENT_DATA_KEY) {
6821 * If we backup past the first extent we want to move forward
6822 * and see if there is an extent in front of us, otherwise we'll
6823 * say there is a hole for our whole search range which can
6830 found_type = btrfs_file_extent_type(leaf, item);
6831 extent_start = found_key.offset;
6832 if (found_type == BTRFS_FILE_EXTENT_REG ||
6833 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6834 extent_end = extent_start +
6835 btrfs_file_extent_num_bytes(leaf, item);
6836 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6838 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6839 extent_end = ALIGN(extent_start + size, root->sectorsize);
6842 if (start >= extent_end) {
6844 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6845 ret = btrfs_next_leaf(root, path);
6852 leaf = path->nodes[0];
6854 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6855 if (found_key.objectid != objectid ||
6856 found_key.type != BTRFS_EXTENT_DATA_KEY)
6858 if (start + len <= found_key.offset)
6860 if (start > found_key.offset)
6863 em->orig_start = start;
6864 em->len = found_key.offset - start;
6868 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6870 if (found_type == BTRFS_FILE_EXTENT_REG ||
6871 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6873 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6877 size_t extent_offset;
6883 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6884 extent_offset = page_offset(page) + pg_offset - extent_start;
6885 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
6886 size - extent_offset);
6887 em->start = extent_start + extent_offset;
6888 em->len = ALIGN(copy_size, root->sectorsize);
6889 em->orig_block_len = em->len;
6890 em->orig_start = em->start;
6891 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6892 if (create == 0 && !PageUptodate(page)) {
6893 if (btrfs_file_extent_compression(leaf, item) !=
6894 BTRFS_COMPRESS_NONE) {
6895 ret = uncompress_inline(path, inode, page,
6897 extent_offset, item);
6904 read_extent_buffer(leaf, map + pg_offset, ptr,
6906 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6907 memset(map + pg_offset + copy_size, 0,
6908 PAGE_CACHE_SIZE - pg_offset -
6913 flush_dcache_page(page);
6914 } else if (create && PageUptodate(page)) {
6918 free_extent_map(em);
6921 btrfs_release_path(path);
6922 trans = btrfs_join_transaction(root);
6925 return ERR_CAST(trans);
6929 write_extent_buffer(leaf, map + pg_offset, ptr,
6932 btrfs_mark_buffer_dirty(leaf);
6934 set_extent_uptodate(io_tree, em->start,
6935 extent_map_end(em) - 1, NULL, GFP_NOFS);
6940 em->orig_start = start;
6943 em->block_start = EXTENT_MAP_HOLE;
6944 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
6946 btrfs_release_path(path);
6947 if (em->start > start || extent_map_end(em) <= start) {
6948 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6949 em->start, em->len, start, len);
6955 write_lock(&em_tree->lock);
6956 ret = add_extent_mapping(em_tree, em, 0);
6957 /* it is possible that someone inserted the extent into the tree
6958 * while we had the lock dropped. It is also possible that
6959 * an overlapping map exists in the tree
6961 if (ret == -EEXIST) {
6962 struct extent_map *existing;
6966 existing = search_extent_mapping(em_tree, start, len);
6968 * existing will always be non-NULL, since there must be
6969 * extent causing the -EEXIST.
6971 if (start >= extent_map_end(existing) ||
6972 start <= existing->start) {
6974 * The existing extent map is the one nearest to
6975 * the [start, start + len) range which overlaps
6977 err = merge_extent_mapping(em_tree, existing,
6979 free_extent_map(existing);
6981 free_extent_map(em);
6985 free_extent_map(em);
6990 write_unlock(&em_tree->lock);
6993 trace_btrfs_get_extent(root, em);
6995 btrfs_free_path(path);
6997 ret = btrfs_end_transaction(trans, root);
7002 free_extent_map(em);
7003 return ERR_PTR(err);
7005 BUG_ON(!em); /* Error is always set */
7009 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7010 size_t pg_offset, u64 start, u64 len,
7013 struct extent_map *em;
7014 struct extent_map *hole_em = NULL;
7015 u64 range_start = start;
7021 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7028 * - a pre-alloc extent,
7029 * there might actually be delalloc bytes behind it.
7031 if (em->block_start != EXTENT_MAP_HOLE &&
7032 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7038 /* check to see if we've wrapped (len == -1 or similar) */
7047 /* ok, we didn't find anything, lets look for delalloc */
7048 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7049 end, len, EXTENT_DELALLOC, 1);
7050 found_end = range_start + found;
7051 if (found_end < range_start)
7052 found_end = (u64)-1;
7055 * we didn't find anything useful, return
7056 * the original results from get_extent()
7058 if (range_start > end || found_end <= start) {
7064 /* adjust the range_start to make sure it doesn't
7065 * go backwards from the start they passed in
7067 range_start = max(start, range_start);
7068 found = found_end - range_start;
7071 u64 hole_start = start;
7074 em = alloc_extent_map();
7080 * when btrfs_get_extent can't find anything it
7081 * returns one huge hole
7083 * make sure what it found really fits our range, and
7084 * adjust to make sure it is based on the start from
7088 u64 calc_end = extent_map_end(hole_em);
7090 if (calc_end <= start || (hole_em->start > end)) {
7091 free_extent_map(hole_em);
7094 hole_start = max(hole_em->start, start);
7095 hole_len = calc_end - hole_start;
7099 if (hole_em && range_start > hole_start) {
7100 /* our hole starts before our delalloc, so we
7101 * have to return just the parts of the hole
7102 * that go until the delalloc starts
7104 em->len = min(hole_len,
7105 range_start - hole_start);
7106 em->start = hole_start;
7107 em->orig_start = hole_start;
7109 * don't adjust block start at all,
7110 * it is fixed at EXTENT_MAP_HOLE
7112 em->block_start = hole_em->block_start;
7113 em->block_len = hole_len;
7114 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7115 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7117 em->start = range_start;
7119 em->orig_start = range_start;
7120 em->block_start = EXTENT_MAP_DELALLOC;
7121 em->block_len = found;
7123 } else if (hole_em) {
7128 free_extent_map(hole_em);
7130 free_extent_map(em);
7131 return ERR_PTR(err);
7136 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7139 struct btrfs_root *root = BTRFS_I(inode)->root;
7140 struct extent_map *em;
7141 struct btrfs_key ins;
7145 alloc_hint = get_extent_allocation_hint(inode, start, len);
7146 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
7147 alloc_hint, &ins, 1, 1);
7149 return ERR_PTR(ret);
7151 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
7152 ins.offset, ins.offset, ins.offset, 0);
7154 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
7158 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
7159 ins.offset, ins.offset, 0);
7161 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
7162 free_extent_map(em);
7163 return ERR_PTR(ret);
7170 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7171 * block must be cow'd
7173 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7174 u64 *orig_start, u64 *orig_block_len,
7177 struct btrfs_trans_handle *trans;
7178 struct btrfs_path *path;
7180 struct extent_buffer *leaf;
7181 struct btrfs_root *root = BTRFS_I(inode)->root;
7182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7183 struct btrfs_file_extent_item *fi;
7184 struct btrfs_key key;
7191 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7193 path = btrfs_alloc_path();
7197 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
7202 slot = path->slots[0];
7205 /* can't find the item, must cow */
7212 leaf = path->nodes[0];
7213 btrfs_item_key_to_cpu(leaf, &key, slot);
7214 if (key.objectid != btrfs_ino(inode) ||
7215 key.type != BTRFS_EXTENT_DATA_KEY) {
7216 /* not our file or wrong item type, must cow */
7220 if (key.offset > offset) {
7221 /* Wrong offset, must cow */
7225 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7226 found_type = btrfs_file_extent_type(leaf, fi);
7227 if (found_type != BTRFS_FILE_EXTENT_REG &&
7228 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7229 /* not a regular extent, must cow */
7233 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7236 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7237 if (extent_end <= offset)
7240 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7241 if (disk_bytenr == 0)
7244 if (btrfs_file_extent_compression(leaf, fi) ||
7245 btrfs_file_extent_encryption(leaf, fi) ||
7246 btrfs_file_extent_other_encoding(leaf, fi))
7249 backref_offset = btrfs_file_extent_offset(leaf, fi);
7252 *orig_start = key.offset - backref_offset;
7253 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7254 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7257 if (btrfs_extent_readonly(root, disk_bytenr))
7260 num_bytes = min(offset + *len, extent_end) - offset;
7261 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7264 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7265 ret = test_range_bit(io_tree, offset, range_end,
7266 EXTENT_DELALLOC, 0, NULL);
7273 btrfs_release_path(path);
7276 * look for other files referencing this extent, if we
7277 * find any we must cow
7279 trans = btrfs_join_transaction(root);
7280 if (IS_ERR(trans)) {
7285 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7286 key.offset - backref_offset, disk_bytenr);
7287 btrfs_end_transaction(trans, root);
7294 * adjust disk_bytenr and num_bytes to cover just the bytes
7295 * in this extent we are about to write. If there
7296 * are any csums in that range we have to cow in order
7297 * to keep the csums correct
7299 disk_bytenr += backref_offset;
7300 disk_bytenr += offset - key.offset;
7301 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7304 * all of the above have passed, it is safe to overwrite this extent
7310 btrfs_free_path(path);
7314 bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7316 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7318 void **pagep = NULL;
7319 struct page *page = NULL;
7323 start_idx = start >> PAGE_CACHE_SHIFT;
7326 * end is the last byte in the last page. end == start is legal
7328 end_idx = end >> PAGE_CACHE_SHIFT;
7332 /* Most of the code in this while loop is lifted from
7333 * find_get_page. It's been modified to begin searching from a
7334 * page and return just the first page found in that range. If the
7335 * found idx is less than or equal to the end idx then we know that
7336 * a page exists. If no pages are found or if those pages are
7337 * outside of the range then we're fine (yay!) */
7338 while (page == NULL &&
7339 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7340 page = radix_tree_deref_slot(pagep);
7341 if (unlikely(!page))
7344 if (radix_tree_exception(page)) {
7345 if (radix_tree_deref_retry(page)) {
7350 * Otherwise, shmem/tmpfs must be storing a swap entry
7351 * here as an exceptional entry: so return it without
7352 * attempting to raise page count.
7355 break; /* TODO: Is this relevant for this use case? */
7358 if (!page_cache_get_speculative(page)) {
7364 * Has the page moved?
7365 * This is part of the lockless pagecache protocol. See
7366 * include/linux/pagemap.h for details.
7368 if (unlikely(page != *pagep)) {
7369 page_cache_release(page);
7375 if (page->index <= end_idx)
7377 page_cache_release(page);
7384 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7385 struct extent_state **cached_state, int writing)
7387 struct btrfs_ordered_extent *ordered;
7391 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7394 * We're concerned with the entire range that we're going to be
7395 * doing DIO to, so we need to make sure theres no ordered
7396 * extents in this range.
7398 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7399 lockend - lockstart + 1);
7402 * We need to make sure there are no buffered pages in this
7403 * range either, we could have raced between the invalidate in
7404 * generic_file_direct_write and locking the extent. The
7405 * invalidate needs to happen so that reads after a write do not
7410 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
7413 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7414 cached_state, GFP_NOFS);
7417 btrfs_start_ordered_extent(inode, ordered, 1);
7418 btrfs_put_ordered_extent(ordered);
7421 * We could trigger writeback for this range (and wait
7422 * for it to complete) and then invalidate the pages for
7423 * this range (through invalidate_inode_pages2_range()),
7424 * but that can lead us to a deadlock with a concurrent
7425 * call to readpages() (a buffered read or a defrag call
7426 * triggered a readahead) on a page lock due to an
7427 * ordered dio extent we created before but did not have
7428 * yet a corresponding bio submitted (whence it can not
7429 * complete), which makes readpages() wait for that
7430 * ordered extent to complete while holding a lock on
7443 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7444 u64 len, u64 orig_start,
7445 u64 block_start, u64 block_len,
7446 u64 orig_block_len, u64 ram_bytes,
7449 struct extent_map_tree *em_tree;
7450 struct extent_map *em;
7451 struct btrfs_root *root = BTRFS_I(inode)->root;
7454 em_tree = &BTRFS_I(inode)->extent_tree;
7455 em = alloc_extent_map();
7457 return ERR_PTR(-ENOMEM);
7460 em->orig_start = orig_start;
7461 em->mod_start = start;
7464 em->block_len = block_len;
7465 em->block_start = block_start;
7466 em->bdev = root->fs_info->fs_devices->latest_bdev;
7467 em->orig_block_len = orig_block_len;
7468 em->ram_bytes = ram_bytes;
7469 em->generation = -1;
7470 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7471 if (type == BTRFS_ORDERED_PREALLOC)
7472 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7475 btrfs_drop_extent_cache(inode, em->start,
7476 em->start + em->len - 1, 0);
7477 write_lock(&em_tree->lock);
7478 ret = add_extent_mapping(em_tree, em, 1);
7479 write_unlock(&em_tree->lock);
7480 } while (ret == -EEXIST);
7483 free_extent_map(em);
7484 return ERR_PTR(ret);
7490 static void adjust_dio_outstanding_extents(struct inode *inode,
7491 struct btrfs_dio_data *dio_data,
7494 unsigned num_extents;
7496 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7497 BTRFS_MAX_EXTENT_SIZE);
7499 * If we have an outstanding_extents count still set then we're
7500 * within our reservation, otherwise we need to adjust our inode
7501 * counter appropriately.
7503 if (dio_data->outstanding_extents) {
7504 dio_data->outstanding_extents -= num_extents;
7506 spin_lock(&BTRFS_I(inode)->lock);
7507 BTRFS_I(inode)->outstanding_extents += num_extents;
7508 spin_unlock(&BTRFS_I(inode)->lock);
7512 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7513 struct buffer_head *bh_result, int create)
7515 struct extent_map *em;
7516 struct btrfs_root *root = BTRFS_I(inode)->root;
7517 struct extent_state *cached_state = NULL;
7518 struct btrfs_dio_data *dio_data = NULL;
7519 u64 start = iblock << inode->i_blkbits;
7520 u64 lockstart, lockend;
7521 u64 len = bh_result->b_size;
7522 int unlock_bits = EXTENT_LOCKED;
7526 unlock_bits |= EXTENT_DIRTY;
7528 len = min_t(u64, len, root->sectorsize);
7531 lockend = start + len - 1;
7533 if (current->journal_info) {
7535 * Need to pull our outstanding extents and set journal_info to NULL so
7536 * that anything that needs to check if there's a transction doesn't get
7539 dio_data = current->journal_info;
7540 current->journal_info = NULL;
7544 * If this errors out it's because we couldn't invalidate pagecache for
7545 * this range and we need to fallback to buffered.
7547 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7553 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7560 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7561 * io. INLINE is special, and we could probably kludge it in here, but
7562 * it's still buffered so for safety lets just fall back to the generic
7565 * For COMPRESSED we _have_ to read the entire extent in so we can
7566 * decompress it, so there will be buffering required no matter what we
7567 * do, so go ahead and fallback to buffered.
7569 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7570 * to buffered IO. Don't blame me, this is the price we pay for using
7573 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7574 em->block_start == EXTENT_MAP_INLINE) {
7575 free_extent_map(em);
7580 /* Just a good old fashioned hole, return */
7581 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7582 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7583 free_extent_map(em);
7588 * We don't allocate a new extent in the following cases
7590 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7592 * 2) The extent is marked as PREALLOC. We're good to go here and can
7593 * just use the extent.
7597 len = min(len, em->len - (start - em->start));
7598 lockstart = start + len;
7602 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7603 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7604 em->block_start != EXTENT_MAP_HOLE)) {
7606 u64 block_start, orig_start, orig_block_len, ram_bytes;
7608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7609 type = BTRFS_ORDERED_PREALLOC;
7611 type = BTRFS_ORDERED_NOCOW;
7612 len = min(len, em->len - (start - em->start));
7613 block_start = em->block_start + (start - em->start);
7615 if (can_nocow_extent(inode, start, &len, &orig_start,
7616 &orig_block_len, &ram_bytes) == 1) {
7617 if (type == BTRFS_ORDERED_PREALLOC) {
7618 free_extent_map(em);
7619 em = create_pinned_em(inode, start, len,
7630 ret = btrfs_add_ordered_extent_dio(inode, start,
7631 block_start, len, len, type);
7633 free_extent_map(em);
7641 * this will cow the extent, reset the len in case we changed
7644 len = bh_result->b_size;
7645 free_extent_map(em);
7646 em = btrfs_new_extent_direct(inode, start, len);
7651 len = min(len, em->len - (start - em->start));
7653 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7655 bh_result->b_size = len;
7656 bh_result->b_bdev = em->bdev;
7657 set_buffer_mapped(bh_result);
7659 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7660 set_buffer_new(bh_result);
7663 * Need to update the i_size under the extent lock so buffered
7664 * readers will get the updated i_size when we unlock.
7666 if (start + len > i_size_read(inode))
7667 i_size_write(inode, start + len);
7669 adjust_dio_outstanding_extents(inode, dio_data, len);
7670 btrfs_free_reserved_data_space(inode, start, len);
7671 WARN_ON(dio_data->reserve < len);
7672 dio_data->reserve -= len;
7673 dio_data->unsubmitted_oe_range_end = start + len;
7674 current->journal_info = dio_data;
7678 * In the case of write we need to clear and unlock the entire range,
7679 * in the case of read we need to unlock only the end area that we
7680 * aren't using if there is any left over space.
7682 if (lockstart < lockend) {
7683 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7684 lockend, unlock_bits, 1, 0,
7685 &cached_state, GFP_NOFS);
7687 free_extent_state(cached_state);
7690 free_extent_map(em);
7695 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7696 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7699 current->journal_info = dio_data;
7701 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7702 * write less data then expected, so that we don't underflow our inode's
7703 * outstanding extents counter.
7705 if (create && dio_data)
7706 adjust_dio_outstanding_extents(inode, dio_data, len);
7711 static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7712 int rw, int mirror_num)
7714 struct btrfs_root *root = BTRFS_I(inode)->root;
7717 BUG_ON(rw & REQ_WRITE);
7721 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7722 BTRFS_WQ_ENDIO_DIO_REPAIR);
7726 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7732 static int btrfs_check_dio_repairable(struct inode *inode,
7733 struct bio *failed_bio,
7734 struct io_failure_record *failrec,
7739 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7740 failrec->logical, failrec->len);
7741 if (num_copies == 1) {
7743 * we only have a single copy of the data, so don't bother with
7744 * all the retry and error correction code that follows. no
7745 * matter what the error is, it is very likely to persist.
7747 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7748 num_copies, failrec->this_mirror, failed_mirror);
7752 failrec->failed_mirror = failed_mirror;
7753 failrec->this_mirror++;
7754 if (failrec->this_mirror == failed_mirror)
7755 failrec->this_mirror++;
7757 if (failrec->this_mirror > num_copies) {
7758 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7759 num_copies, failrec->this_mirror, failed_mirror);
7766 static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7767 struct page *page, u64 start, u64 end,
7768 int failed_mirror, bio_end_io_t *repair_endio,
7771 struct io_failure_record *failrec;
7777 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7779 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7783 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7786 free_io_failure(inode, failrec);
7790 if (failed_bio->bi_vcnt > 1)
7791 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7793 read_mode = READ_SYNC;
7795 isector = start - btrfs_io_bio(failed_bio)->logical;
7796 isector >>= inode->i_sb->s_blocksize_bits;
7797 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7798 0, isector, repair_endio, repair_arg);
7800 free_io_failure(inode, failrec);
7804 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7805 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7806 read_mode, failrec->this_mirror, failrec->in_validation);
7808 ret = submit_dio_repair_bio(inode, bio, read_mode,
7809 failrec->this_mirror);
7811 free_io_failure(inode, failrec);
7818 struct btrfs_retry_complete {
7819 struct completion done;
7820 struct inode *inode;
7825 static void btrfs_retry_endio_nocsum(struct bio *bio)
7827 struct btrfs_retry_complete *done = bio->bi_private;
7828 struct bio_vec *bvec;
7835 bio_for_each_segment_all(bvec, bio, i)
7836 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7838 complete(&done->done);
7842 static int __btrfs_correct_data_nocsum(struct inode *inode,
7843 struct btrfs_io_bio *io_bio)
7845 struct bio_vec *bvec;
7846 struct btrfs_retry_complete done;
7851 start = io_bio->logical;
7854 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7858 init_completion(&done.done);
7860 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7861 start + bvec->bv_len - 1,
7863 btrfs_retry_endio_nocsum, &done);
7867 wait_for_completion(&done.done);
7869 if (!done.uptodate) {
7870 /* We might have another mirror, so try again */
7874 start += bvec->bv_len;
7880 static void btrfs_retry_endio(struct bio *bio)
7882 struct btrfs_retry_complete *done = bio->bi_private;
7883 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7884 struct bio_vec *bvec;
7893 bio_for_each_segment_all(bvec, bio, i) {
7894 ret = __readpage_endio_check(done->inode, io_bio, i,
7896 done->start, bvec->bv_len);
7898 clean_io_failure(done->inode, done->start,
7904 done->uptodate = uptodate;
7906 complete(&done->done);
7910 static int __btrfs_subio_endio_read(struct inode *inode,
7911 struct btrfs_io_bio *io_bio, int err)
7913 struct bio_vec *bvec;
7914 struct btrfs_retry_complete done;
7921 start = io_bio->logical;
7924 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7925 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7926 0, start, bvec->bv_len);
7932 init_completion(&done.done);
7934 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start,
7935 start + bvec->bv_len - 1,
7937 btrfs_retry_endio, &done);
7943 wait_for_completion(&done.done);
7945 if (!done.uptodate) {
7946 /* We might have another mirror, so try again */
7950 offset += bvec->bv_len;
7951 start += bvec->bv_len;
7957 static int btrfs_subio_endio_read(struct inode *inode,
7958 struct btrfs_io_bio *io_bio, int err)
7960 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7964 return __btrfs_correct_data_nocsum(inode, io_bio);
7968 return __btrfs_subio_endio_read(inode, io_bio, err);
7972 static void btrfs_endio_direct_read(struct bio *bio)
7974 struct btrfs_dio_private *dip = bio->bi_private;
7975 struct inode *inode = dip->inode;
7976 struct bio *dio_bio;
7977 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7978 int err = bio->bi_error;
7980 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
7981 err = btrfs_subio_endio_read(inode, io_bio, err);
7983 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
7984 dip->logical_offset + dip->bytes - 1);
7985 dio_bio = dip->dio_bio;
7989 dio_end_io(dio_bio, bio->bi_error);
7992 io_bio->end_io(io_bio, err);
7996 static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8001 struct btrfs_root *root = BTRFS_I(inode)->root;
8002 struct btrfs_ordered_extent *ordered = NULL;
8003 u64 ordered_offset = offset;
8004 u64 ordered_bytes = bytes;
8008 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8015 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8016 finish_ordered_fn, NULL, NULL);
8017 btrfs_queue_work(root->fs_info->endio_write_workers,
8021 * our bio might span multiple ordered extents. If we haven't
8022 * completed the accounting for the whole dio, go back and try again
8024 if (ordered_offset < offset + bytes) {
8025 ordered_bytes = offset + bytes - ordered_offset;
8031 static void btrfs_endio_direct_write(struct bio *bio)
8033 struct btrfs_dio_private *dip = bio->bi_private;
8034 struct bio *dio_bio = dip->dio_bio;
8036 btrfs_endio_direct_write_update_ordered(dip->inode,
8037 dip->logical_offset,
8043 dio_end_io(dio_bio, bio->bi_error);
8047 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8048 struct bio *bio, int mirror_num,
8049 unsigned long bio_flags, u64 offset)
8052 struct btrfs_root *root = BTRFS_I(inode)->root;
8053 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
8054 BUG_ON(ret); /* -ENOMEM */
8058 static void btrfs_end_dio_bio(struct bio *bio)
8060 struct btrfs_dio_private *dip = bio->bi_private;
8061 int err = bio->bi_error;
8064 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8065 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8066 btrfs_ino(dip->inode), bio->bi_rw,
8067 (unsigned long long)bio->bi_iter.bi_sector,
8068 bio->bi_iter.bi_size, err);
8070 if (dip->subio_endio)
8071 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
8077 * before atomic variable goto zero, we must make sure
8078 * dip->errors is perceived to be set.
8080 smp_mb__before_atomic();
8083 /* if there are more bios still pending for this dio, just exit */
8084 if (!atomic_dec_and_test(&dip->pending_bios))
8088 bio_io_error(dip->orig_bio);
8090 dip->dio_bio->bi_error = 0;
8091 bio_endio(dip->orig_bio);
8097 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8098 u64 first_sector, gfp_t gfp_flags)
8101 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
8103 bio_associate_current(bio);
8107 static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8108 struct inode *inode,
8109 struct btrfs_dio_private *dip,
8113 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8114 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8118 * We load all the csum data we need when we submit
8119 * the first bio to reduce the csum tree search and
8122 if (dip->logical_offset == file_offset) {
8123 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8129 if (bio == dip->orig_bio)
8132 file_offset -= dip->logical_offset;
8133 file_offset >>= inode->i_sb->s_blocksize_bits;
8134 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8139 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8140 int rw, u64 file_offset, int skip_sum,
8143 struct btrfs_dio_private *dip = bio->bi_private;
8144 int write = rw & REQ_WRITE;
8145 struct btrfs_root *root = BTRFS_I(inode)->root;
8149 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8154 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8155 BTRFS_WQ_ENDIO_DATA);
8163 if (write && async_submit) {
8164 ret = btrfs_wq_submit_bio(root->fs_info,
8165 inode, rw, bio, 0, 0,
8167 __btrfs_submit_bio_start_direct_io,
8168 __btrfs_submit_bio_done);
8172 * If we aren't doing async submit, calculate the csum of the
8175 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8179 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8185 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
8191 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8194 struct inode *inode = dip->inode;
8195 struct btrfs_root *root = BTRFS_I(inode)->root;
8197 struct bio *orig_bio = dip->orig_bio;
8198 struct bio_vec *bvec = orig_bio->bi_io_vec;
8199 u64 start_sector = orig_bio->bi_iter.bi_sector;
8200 u64 file_offset = dip->logical_offset;
8205 int async_submit = 0;
8207 map_length = orig_bio->bi_iter.bi_size;
8208 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
8209 &map_length, NULL, 0);
8213 if (map_length >= orig_bio->bi_iter.bi_size) {
8215 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
8219 /* async crcs make it difficult to collect full stripe writes. */
8220 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
8225 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8229 bio->bi_private = dip;
8230 bio->bi_end_io = btrfs_end_dio_bio;
8231 btrfs_io_bio(bio)->logical = file_offset;
8232 atomic_inc(&dip->pending_bios);
8234 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
8235 if (map_length < submit_len + bvec->bv_len ||
8236 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
8237 bvec->bv_offset) < bvec->bv_len) {
8239 * inc the count before we submit the bio so
8240 * we know the end IO handler won't happen before
8241 * we inc the count. Otherwise, the dip might get freed
8242 * before we're done setting it up
8244 atomic_inc(&dip->pending_bios);
8245 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8246 file_offset, skip_sum,
8250 atomic_dec(&dip->pending_bios);
8254 start_sector += submit_len >> 9;
8255 file_offset += submit_len;
8260 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8261 start_sector, GFP_NOFS);
8264 bio->bi_private = dip;
8265 bio->bi_end_io = btrfs_end_dio_bio;
8266 btrfs_io_bio(bio)->logical = file_offset;
8268 map_length = orig_bio->bi_iter.bi_size;
8269 ret = btrfs_map_block(root->fs_info, rw,
8271 &map_length, NULL, 0);
8277 submit_len += bvec->bv_len;
8284 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
8293 * before atomic variable goto zero, we must
8294 * make sure dip->errors is perceived to be set.
8296 smp_mb__before_atomic();
8297 if (atomic_dec_and_test(&dip->pending_bios))
8298 bio_io_error(dip->orig_bio);
8300 /* bio_end_io() will handle error, so we needn't return it */
8304 static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8305 struct inode *inode, loff_t file_offset)
8307 struct btrfs_dio_private *dip = NULL;
8308 struct bio *io_bio = NULL;
8309 struct btrfs_io_bio *btrfs_bio;
8311 int write = rw & REQ_WRITE;
8314 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8316 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
8322 dip = kzalloc(sizeof(*dip), GFP_NOFS);
8328 dip->private = dio_bio->bi_private;
8330 dip->logical_offset = file_offset;
8331 dip->bytes = dio_bio->bi_iter.bi_size;
8332 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8333 io_bio->bi_private = dip;
8334 dip->orig_bio = io_bio;
8335 dip->dio_bio = dio_bio;
8336 atomic_set(&dip->pending_bios, 0);
8337 btrfs_bio = btrfs_io_bio(io_bio);
8338 btrfs_bio->logical = file_offset;
8341 io_bio->bi_end_io = btrfs_endio_direct_write;
8343 io_bio->bi_end_io = btrfs_endio_direct_read;
8344 dip->subio_endio = btrfs_subio_endio_read;
8348 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8349 * even if we fail to submit a bio, because in such case we do the
8350 * corresponding error handling below and it must not be done a second
8351 * time by btrfs_direct_IO().
8354 struct btrfs_dio_data *dio_data = current->journal_info;
8356 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8358 dio_data->unsubmitted_oe_range_start =
8359 dio_data->unsubmitted_oe_range_end;
8362 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8366 if (btrfs_bio->end_io)
8367 btrfs_bio->end_io(btrfs_bio, ret);
8371 * If we arrived here it means either we failed to submit the dip
8372 * or we either failed to clone the dio_bio or failed to allocate the
8373 * dip. If we cloned the dio_bio and allocated the dip, we can just
8374 * call bio_endio against our io_bio so that we get proper resource
8375 * cleanup if we fail to submit the dip, otherwise, we must do the
8376 * same as btrfs_endio_direct_[write|read] because we can't call these
8377 * callbacks - they require an allocated dip and a clone of dio_bio.
8379 if (io_bio && dip) {
8380 io_bio->bi_error = -EIO;
8383 * The end io callbacks free our dip, do the final put on io_bio
8384 * and all the cleanup and final put for dio_bio (through
8391 btrfs_endio_direct_write_update_ordered(inode,
8393 dio_bio->bi_iter.bi_size,
8396 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8397 file_offset + dio_bio->bi_iter.bi_size - 1);
8399 dio_bio->bi_error = -EIO;
8401 * Releases and cleans up our dio_bio, no need to bio_put()
8402 * nor bio_endio()/bio_io_error() against dio_bio.
8404 dio_end_io(dio_bio, ret);
8411 static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
8412 const struct iov_iter *iter, loff_t offset)
8416 unsigned blocksize_mask = root->sectorsize - 1;
8417 ssize_t retval = -EINVAL;
8419 if (offset & blocksize_mask)
8422 if (iov_iter_alignment(iter) & blocksize_mask)
8425 /* If this is a write we don't need to check anymore */
8426 if (iov_iter_rw(iter) == WRITE)
8429 * Check to make sure we don't have duplicate iov_base's in this
8430 * iovec, if so return EINVAL, otherwise we'll get csum errors
8431 * when reading back.
8433 for (seg = 0; seg < iter->nr_segs; seg++) {
8434 for (i = seg + 1; i < iter->nr_segs; i++) {
8435 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8444 static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
8447 struct file *file = iocb->ki_filp;
8448 struct inode *inode = file->f_mapping->host;
8449 struct btrfs_root *root = BTRFS_I(inode)->root;
8450 struct btrfs_dio_data dio_data = { 0 };
8454 bool relock = false;
8457 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
8460 inode_dio_begin(inode);
8461 smp_mb__after_atomic();
8464 * The generic stuff only does filemap_write_and_wait_range, which
8465 * isn't enough if we've written compressed pages to this area, so
8466 * we need to flush the dirty pages again to make absolutely sure
8467 * that any outstanding dirty pages are on disk.
8469 count = iov_iter_count(iter);
8470 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8471 &BTRFS_I(inode)->runtime_flags))
8472 filemap_fdatawrite_range(inode->i_mapping, offset,
8473 offset + count - 1);
8475 if (iov_iter_rw(iter) == WRITE) {
8477 * If the write DIO is beyond the EOF, we need update
8478 * the isize, but it is protected by i_mutex. So we can
8479 * not unlock the i_mutex at this case.
8481 if (offset + count <= inode->i_size) {
8482 mutex_unlock(&inode->i_mutex);
8485 ret = btrfs_delalloc_reserve_space(inode, offset, count);
8488 dio_data.outstanding_extents = div64_u64(count +
8489 BTRFS_MAX_EXTENT_SIZE - 1,
8490 BTRFS_MAX_EXTENT_SIZE);
8493 * We need to know how many extents we reserved so that we can
8494 * do the accounting properly if we go over the number we
8495 * originally calculated. Abuse current->journal_info for this.
8497 dio_data.reserve = round_up(count, root->sectorsize);
8498 dio_data.unsubmitted_oe_range_start = (u64)offset;
8499 dio_data.unsubmitted_oe_range_end = (u64)offset;
8500 current->journal_info = &dio_data;
8501 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8502 &BTRFS_I(inode)->runtime_flags)) {
8503 inode_dio_end(inode);
8504 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8508 ret = __blockdev_direct_IO(iocb, inode,
8509 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
8510 iter, offset, btrfs_get_blocks_direct, NULL,
8511 btrfs_submit_direct, flags);
8512 if (iov_iter_rw(iter) == WRITE) {
8513 current->journal_info = NULL;
8514 if (ret < 0 && ret != -EIOCBQUEUED) {
8515 if (dio_data.reserve)
8516 btrfs_delalloc_release_space(inode, offset,
8519 * On error we might have left some ordered extents
8520 * without submitting corresponding bios for them, so
8521 * cleanup them up to avoid other tasks getting them
8522 * and waiting for them to complete forever.
8524 if (dio_data.unsubmitted_oe_range_start <
8525 dio_data.unsubmitted_oe_range_end)
8526 btrfs_endio_direct_write_update_ordered(inode,
8527 dio_data.unsubmitted_oe_range_start,
8528 dio_data.unsubmitted_oe_range_end -
8529 dio_data.unsubmitted_oe_range_start,
8531 } else if (ret >= 0 && (size_t)ret < count)
8532 btrfs_delalloc_release_space(inode, offset,
8533 count - (size_t)ret);
8537 inode_dio_end(inode);
8539 mutex_lock(&inode->i_mutex);
8544 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8546 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8547 __u64 start, __u64 len)
8551 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8555 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
8558 int btrfs_readpage(struct file *file, struct page *page)
8560 struct extent_io_tree *tree;
8561 tree = &BTRFS_I(page->mapping->host)->io_tree;
8562 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
8565 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8567 struct extent_io_tree *tree;
8570 if (current->flags & PF_MEMALLOC) {
8571 redirty_page_for_writepage(wbc, page);
8575 tree = &BTRFS_I(page->mapping->host)->io_tree;
8576 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8579 static int btrfs_writepages(struct address_space *mapping,
8580 struct writeback_control *wbc)
8582 struct extent_io_tree *tree;
8584 tree = &BTRFS_I(mapping->host)->io_tree;
8585 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8589 btrfs_readpages(struct file *file, struct address_space *mapping,
8590 struct list_head *pages, unsigned nr_pages)
8592 struct extent_io_tree *tree;
8593 tree = &BTRFS_I(mapping->host)->io_tree;
8594 return extent_readpages(tree, mapping, pages, nr_pages,
8597 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8599 struct extent_io_tree *tree;
8600 struct extent_map_tree *map;
8603 tree = &BTRFS_I(page->mapping->host)->io_tree;
8604 map = &BTRFS_I(page->mapping->host)->extent_tree;
8605 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
8607 ClearPagePrivate(page);
8608 set_page_private(page, 0);
8609 page_cache_release(page);
8614 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8616 if (PageWriteback(page) || PageDirty(page))
8618 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
8621 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8622 unsigned int length)
8624 struct inode *inode = page->mapping->host;
8625 struct extent_io_tree *tree;
8626 struct btrfs_ordered_extent *ordered;
8627 struct extent_state *cached_state = NULL;
8628 u64 page_start = page_offset(page);
8629 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
8630 int inode_evicting = inode->i_state & I_FREEING;
8633 * we have the page locked, so new writeback can't start,
8634 * and the dirty bit won't be cleared while we are here.
8636 * Wait for IO on this page so that we can safely clear
8637 * the PagePrivate2 bit and do ordered accounting
8639 wait_on_page_writeback(page);
8641 tree = &BTRFS_I(inode)->io_tree;
8643 btrfs_releasepage(page, GFP_NOFS);
8647 if (!inode_evicting)
8648 lock_extent_bits(tree, page_start, page_end, &cached_state);
8649 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8652 * IO on this page will never be started, so we need
8653 * to account for any ordered extents now
8655 if (!inode_evicting)
8656 clear_extent_bit(tree, page_start, page_end,
8657 EXTENT_DIRTY | EXTENT_DELALLOC |
8658 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8659 EXTENT_DEFRAG, 1, 0, &cached_state,
8662 * whoever cleared the private bit is responsible
8663 * for the finish_ordered_io
8665 if (TestClearPagePrivate2(page)) {
8666 struct btrfs_ordered_inode_tree *tree;
8669 tree = &BTRFS_I(inode)->ordered_tree;
8671 spin_lock_irq(&tree->lock);
8672 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8673 new_len = page_start - ordered->file_offset;
8674 if (new_len < ordered->truncated_len)
8675 ordered->truncated_len = new_len;
8676 spin_unlock_irq(&tree->lock);
8678 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8680 PAGE_CACHE_SIZE, 1))
8681 btrfs_finish_ordered_io(ordered);
8683 btrfs_put_ordered_extent(ordered);
8684 if (!inode_evicting) {
8685 cached_state = NULL;
8686 lock_extent_bits(tree, page_start, page_end,
8692 * Qgroup reserved space handler
8693 * Page here will be either
8694 * 1) Already written to disk
8695 * In this case, its reserved space is released from data rsv map
8696 * and will be freed by delayed_ref handler finally.
8697 * So even we call qgroup_free_data(), it won't decrease reserved
8699 * 2) Not written to disk
8700 * This means the reserved space should be freed here.
8702 btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE);
8703 if (!inode_evicting) {
8704 clear_extent_bit(tree, page_start, page_end,
8705 EXTENT_LOCKED | EXTENT_DIRTY |
8706 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8707 EXTENT_DEFRAG, 1, 1,
8708 &cached_state, GFP_NOFS);
8710 __btrfs_releasepage(page, GFP_NOFS);
8713 ClearPageChecked(page);
8714 if (PagePrivate(page)) {
8715 ClearPagePrivate(page);
8716 set_page_private(page, 0);
8717 page_cache_release(page);
8722 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8723 * called from a page fault handler when a page is first dirtied. Hence we must
8724 * be careful to check for EOF conditions here. We set the page up correctly
8725 * for a written page which means we get ENOSPC checking when writing into
8726 * holes and correct delalloc and unwritten extent mapping on filesystems that
8727 * support these features.
8729 * We are not allowed to take the i_mutex here so we have to play games to
8730 * protect against truncate races as the page could now be beyond EOF. Because
8731 * vmtruncate() writes the inode size before removing pages, once we have the
8732 * page lock we can determine safely if the page is beyond EOF. If it is not
8733 * beyond EOF, then the page is guaranteed safe against truncation until we
8736 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
8738 struct page *page = vmf->page;
8739 struct inode *inode = file_inode(vma->vm_file);
8740 struct btrfs_root *root = BTRFS_I(inode)->root;
8741 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8742 struct btrfs_ordered_extent *ordered;
8743 struct extent_state *cached_state = NULL;
8745 unsigned long zero_start;
8752 sb_start_pagefault(inode->i_sb);
8753 page_start = page_offset(page);
8754 page_end = page_start + PAGE_CACHE_SIZE - 1;
8756 ret = btrfs_delalloc_reserve_space(inode, page_start,
8759 ret = file_update_time(vma->vm_file);
8765 else /* -ENOSPC, -EIO, etc */
8766 ret = VM_FAULT_SIGBUS;
8772 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
8775 size = i_size_read(inode);
8777 if ((page->mapping != inode->i_mapping) ||
8778 (page_start >= size)) {
8779 /* page got truncated out from underneath us */
8782 wait_on_page_writeback(page);
8784 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
8785 set_page_extent_mapped(page);
8788 * we can't set the delalloc bits if there are pending ordered
8789 * extents. Drop our locks and wait for them to finish
8791 ordered = btrfs_lookup_ordered_extent(inode, page_start);
8793 unlock_extent_cached(io_tree, page_start, page_end,
8794 &cached_state, GFP_NOFS);
8796 btrfs_start_ordered_extent(inode, ordered, 1);
8797 btrfs_put_ordered_extent(ordered);
8802 * XXX - page_mkwrite gets called every time the page is dirtied, even
8803 * if it was already dirty, so for space accounting reasons we need to
8804 * clear any delalloc bits for the range we are fixing to save. There
8805 * is probably a better way to do this, but for now keep consistent with
8806 * prepare_pages in the normal write path.
8808 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
8809 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
8811 0, 0, &cached_state, GFP_NOFS);
8813 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
8816 unlock_extent_cached(io_tree, page_start, page_end,
8817 &cached_state, GFP_NOFS);
8818 ret = VM_FAULT_SIGBUS;
8823 /* page is wholly or partially inside EOF */
8824 if (page_start + PAGE_CACHE_SIZE > size)
8825 zero_start = size & ~PAGE_CACHE_MASK;
8827 zero_start = PAGE_CACHE_SIZE;
8829 if (zero_start != PAGE_CACHE_SIZE) {
8831 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
8832 flush_dcache_page(page);
8835 ClearPageChecked(page);
8836 set_page_dirty(page);
8837 SetPageUptodate(page);
8839 BTRFS_I(inode)->last_trans = root->fs_info->generation;
8840 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
8841 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
8843 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
8847 sb_end_pagefault(inode->i_sb);
8848 return VM_FAULT_LOCKED;
8852 btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE);
8854 sb_end_pagefault(inode->i_sb);
8858 static int btrfs_truncate(struct inode *inode)
8860 struct btrfs_root *root = BTRFS_I(inode)->root;
8861 struct btrfs_block_rsv *rsv;
8864 struct btrfs_trans_handle *trans;
8865 u64 mask = root->sectorsize - 1;
8866 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
8868 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8874 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8875 * 3 things going on here
8877 * 1) We need to reserve space for our orphan item and the space to
8878 * delete our orphan item. Lord knows we don't want to have a dangling
8879 * orphan item because we didn't reserve space to remove it.
8881 * 2) We need to reserve space to update our inode.
8883 * 3) We need to have something to cache all the space that is going to
8884 * be free'd up by the truncate operation, but also have some slack
8885 * space reserved in case it uses space during the truncate (thank you
8886 * very much snapshotting).
8888 * And we need these to all be seperate. The fact is we can use alot of
8889 * space doing the truncate, and we have no earthly idea how much space
8890 * we will use, so we need the truncate reservation to be seperate so it
8891 * doesn't end up using space reserved for updating the inode or
8892 * removing the orphan item. We also need to be able to stop the
8893 * transaction and start a new one, which means we need to be able to
8894 * update the inode several times, and we have no idea of knowing how
8895 * many times that will be, so we can't just reserve 1 item for the
8896 * entirety of the opration, so that has to be done seperately as well.
8897 * Then there is the orphan item, which does indeed need to be held on
8898 * to for the whole operation, and we need nobody to touch this reserved
8899 * space except the orphan code.
8901 * So that leaves us with
8903 * 1) root->orphan_block_rsv - for the orphan deletion.
8904 * 2) rsv - for the truncate reservation, which we will steal from the
8905 * transaction reservation.
8906 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8907 * updating the inode.
8909 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
8912 rsv->size = min_size;
8916 * 1 for the truncate slack space
8917 * 1 for updating the inode.
8919 trans = btrfs_start_transaction(root, 2);
8920 if (IS_ERR(trans)) {
8921 err = PTR_ERR(trans);
8925 /* Migrate the slack space for the truncate to our reserve */
8926 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8931 * So if we truncate and then write and fsync we normally would just
8932 * write the extents that changed, which is a problem if we need to
8933 * first truncate that entire inode. So set this flag so we write out
8934 * all of the extents in the inode to the sync log so we're completely
8937 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
8938 trans->block_rsv = rsv;
8941 ret = btrfs_truncate_inode_items(trans, root, inode,
8943 BTRFS_EXTENT_DATA_KEY);
8944 if (ret != -ENOSPC && ret != -EAGAIN) {
8949 trans->block_rsv = &root->fs_info->trans_block_rsv;
8950 ret = btrfs_update_inode(trans, root, inode);
8956 btrfs_end_transaction(trans, root);
8957 btrfs_btree_balance_dirty(root);
8959 trans = btrfs_start_transaction(root, 2);
8960 if (IS_ERR(trans)) {
8961 ret = err = PTR_ERR(trans);
8966 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8968 BUG_ON(ret); /* shouldn't happen */
8969 trans->block_rsv = rsv;
8972 if (ret == 0 && inode->i_nlink > 0) {
8973 trans->block_rsv = root->orphan_block_rsv;
8974 ret = btrfs_orphan_del(trans, inode);
8980 trans->block_rsv = &root->fs_info->trans_block_rsv;
8981 ret = btrfs_update_inode(trans, root, inode);
8985 ret = btrfs_end_transaction(trans, root);
8986 btrfs_btree_balance_dirty(root);
8990 btrfs_free_block_rsv(root, rsv);
8999 * create a new subvolume directory/inode (helper for the ioctl).
9001 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9002 struct btrfs_root *new_root,
9003 struct btrfs_root *parent_root,
9006 struct inode *inode;
9010 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9011 new_dirid, new_dirid,
9012 S_IFDIR | (~current_umask() & S_IRWXUGO),
9015 return PTR_ERR(inode);
9016 inode->i_op = &btrfs_dir_inode_operations;
9017 inode->i_fop = &btrfs_dir_file_operations;
9019 set_nlink(inode, 1);
9020 btrfs_i_size_write(inode, 0);
9021 unlock_new_inode(inode);
9023 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9025 btrfs_err(new_root->fs_info,
9026 "error inheriting subvolume %llu properties: %d",
9027 new_root->root_key.objectid, err);
9029 err = btrfs_update_inode(trans, new_root, inode);
9035 struct inode *btrfs_alloc_inode(struct super_block *sb)
9037 struct btrfs_inode *ei;
9038 struct inode *inode;
9040 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9047 ei->last_sub_trans = 0;
9048 ei->logged_trans = 0;
9049 ei->delalloc_bytes = 0;
9050 ei->defrag_bytes = 0;
9051 ei->disk_i_size = 0;
9054 ei->index_cnt = (u64)-1;
9056 ei->last_unlink_trans = 0;
9057 ei->last_log_commit = 0;
9059 spin_lock_init(&ei->lock);
9060 ei->outstanding_extents = 0;
9061 ei->reserved_extents = 0;
9063 ei->runtime_flags = 0;
9064 ei->force_compress = BTRFS_COMPRESS_NONE;
9066 ei->delayed_node = NULL;
9068 ei->i_otime.tv_sec = 0;
9069 ei->i_otime.tv_nsec = 0;
9071 inode = &ei->vfs_inode;
9072 extent_map_tree_init(&ei->extent_tree);
9073 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9074 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
9075 ei->io_tree.track_uptodate = 1;
9076 ei->io_failure_tree.track_uptodate = 1;
9077 atomic_set(&ei->sync_writers, 0);
9078 mutex_init(&ei->log_mutex);
9079 mutex_init(&ei->delalloc_mutex);
9080 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9081 INIT_LIST_HEAD(&ei->delalloc_inodes);
9082 RB_CLEAR_NODE(&ei->rb_node);
9087 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9088 void btrfs_test_destroy_inode(struct inode *inode)
9090 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9091 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9095 static void btrfs_i_callback(struct rcu_head *head)
9097 struct inode *inode = container_of(head, struct inode, i_rcu);
9098 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9101 void btrfs_destroy_inode(struct inode *inode)
9103 struct btrfs_ordered_extent *ordered;
9104 struct btrfs_root *root = BTRFS_I(inode)->root;
9106 WARN_ON(!hlist_empty(&inode->i_dentry));
9107 WARN_ON(inode->i_data.nrpages);
9108 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9109 WARN_ON(BTRFS_I(inode)->reserved_extents);
9110 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9111 WARN_ON(BTRFS_I(inode)->csum_bytes);
9112 WARN_ON(BTRFS_I(inode)->defrag_bytes);
9115 * This can happen where we create an inode, but somebody else also
9116 * created the same inode and we need to destroy the one we already
9122 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9123 &BTRFS_I(inode)->runtime_flags)) {
9124 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
9126 atomic_dec(&root->orphan_inodes);
9130 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9134 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
9135 ordered->file_offset, ordered->len);
9136 btrfs_remove_ordered_extent(inode, ordered);
9137 btrfs_put_ordered_extent(ordered);
9138 btrfs_put_ordered_extent(ordered);
9141 btrfs_qgroup_check_reserved_leak(inode);
9142 inode_tree_del(inode);
9143 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9145 call_rcu(&inode->i_rcu, btrfs_i_callback);
9148 int btrfs_drop_inode(struct inode *inode)
9150 struct btrfs_root *root = BTRFS_I(inode)->root;
9155 /* the snap/subvol tree is on deleting */
9156 if (btrfs_root_refs(&root->root_item) == 0)
9159 return generic_drop_inode(inode);
9162 static void init_once(void *foo)
9164 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9166 inode_init_once(&ei->vfs_inode);
9169 void btrfs_destroy_cachep(void)
9172 * Make sure all delayed rcu free inodes are flushed before we
9176 if (btrfs_inode_cachep)
9177 kmem_cache_destroy(btrfs_inode_cachep);
9178 if (btrfs_trans_handle_cachep)
9179 kmem_cache_destroy(btrfs_trans_handle_cachep);
9180 if (btrfs_transaction_cachep)
9181 kmem_cache_destroy(btrfs_transaction_cachep);
9182 if (btrfs_path_cachep)
9183 kmem_cache_destroy(btrfs_path_cachep);
9184 if (btrfs_free_space_cachep)
9185 kmem_cache_destroy(btrfs_free_space_cachep);
9186 if (btrfs_delalloc_work_cachep)
9187 kmem_cache_destroy(btrfs_delalloc_work_cachep);
9190 int btrfs_init_cachep(void)
9192 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9193 sizeof(struct btrfs_inode), 0,
9194 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
9195 if (!btrfs_inode_cachep)
9198 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9199 sizeof(struct btrfs_trans_handle), 0,
9200 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9201 if (!btrfs_trans_handle_cachep)
9204 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
9205 sizeof(struct btrfs_transaction), 0,
9206 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9207 if (!btrfs_transaction_cachep)
9210 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9211 sizeof(struct btrfs_path), 0,
9212 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9213 if (!btrfs_path_cachep)
9216 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9217 sizeof(struct btrfs_free_space), 0,
9218 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
9219 if (!btrfs_free_space_cachep)
9222 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
9223 sizeof(struct btrfs_delalloc_work), 0,
9224 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
9226 if (!btrfs_delalloc_work_cachep)
9231 btrfs_destroy_cachep();
9235 static int btrfs_getattr(struct vfsmount *mnt,
9236 struct dentry *dentry, struct kstat *stat)
9239 struct inode *inode = d_inode(dentry);
9240 u32 blocksize = inode->i_sb->s_blocksize;
9242 generic_fillattr(inode, stat);
9243 stat->dev = BTRFS_I(inode)->root->anon_dev;
9244 stat->blksize = PAGE_CACHE_SIZE;
9246 spin_lock(&BTRFS_I(inode)->lock);
9247 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9248 spin_unlock(&BTRFS_I(inode)->lock);
9249 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
9250 ALIGN(delalloc_bytes, blocksize)) >> 9;
9254 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9255 struct inode *new_dir, struct dentry *new_dentry)
9257 struct btrfs_trans_handle *trans;
9258 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9259 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9260 struct inode *new_inode = d_inode(new_dentry);
9261 struct inode *old_inode = d_inode(old_dentry);
9262 struct timespec ctime = CURRENT_TIME;
9266 u64 old_ino = btrfs_ino(old_inode);
9268 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9271 /* we only allow rename subvolume link between subvolumes */
9272 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9275 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9276 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
9279 if (S_ISDIR(old_inode->i_mode) && new_inode &&
9280 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9284 /* check for collisions, even if the name isn't there */
9285 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9286 new_dentry->d_name.name,
9287 new_dentry->d_name.len);
9290 if (ret == -EEXIST) {
9292 * eexist without a new_inode */
9293 if (WARN_ON(!new_inode)) {
9297 /* maybe -EOVERFLOW */
9304 * we're using rename to replace one file with another. Start IO on it
9305 * now so we don't add too much work to the end of the transaction
9307 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9308 filemap_flush(old_inode->i_mapping);
9310 /* close the racy window with snapshot create/destroy ioctl */
9311 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9312 down_read(&root->fs_info->subvol_sem);
9314 * We want to reserve the absolute worst case amount of items. So if
9315 * both inodes are subvols and we need to unlink them then that would
9316 * require 4 item modifications, but if they are both normal inodes it
9317 * would require 5 item modifications, so we'll assume their normal
9318 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9319 * should cover the worst case number of items we'll modify.
9321 trans = btrfs_start_transaction(root, 11);
9322 if (IS_ERR(trans)) {
9323 ret = PTR_ERR(trans);
9328 btrfs_record_root_in_trans(trans, dest);
9330 ret = btrfs_set_inode_index(new_dir, &index);
9334 BTRFS_I(old_inode)->dir_index = 0ULL;
9335 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9336 /* force full log commit if subvolume involved. */
9337 btrfs_set_log_full_commit(root->fs_info, trans);
9339 ret = btrfs_insert_inode_ref(trans, dest,
9340 new_dentry->d_name.name,
9341 new_dentry->d_name.len,
9343 btrfs_ino(new_dir), index);
9347 * this is an ugly little race, but the rename is required
9348 * to make sure that if we crash, the inode is either at the
9349 * old name or the new one. pinning the log transaction lets
9350 * us make sure we don't allow a log commit to come in after
9351 * we unlink the name but before we add the new name back in.
9353 btrfs_pin_log_trans(root);
9356 inode_inc_iversion(old_dir);
9357 inode_inc_iversion(new_dir);
9358 inode_inc_iversion(old_inode);
9359 old_dir->i_ctime = old_dir->i_mtime = ctime;
9360 new_dir->i_ctime = new_dir->i_mtime = ctime;
9361 old_inode->i_ctime = ctime;
9363 if (old_dentry->d_parent != new_dentry->d_parent)
9364 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9366 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9367 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9368 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9369 old_dentry->d_name.name,
9370 old_dentry->d_name.len);
9372 ret = __btrfs_unlink_inode(trans, root, old_dir,
9373 d_inode(old_dentry),
9374 old_dentry->d_name.name,
9375 old_dentry->d_name.len);
9377 ret = btrfs_update_inode(trans, root, old_inode);
9380 btrfs_abort_transaction(trans, root, ret);
9385 inode_inc_iversion(new_inode);
9386 new_inode->i_ctime = CURRENT_TIME;
9387 if (unlikely(btrfs_ino(new_inode) ==
9388 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9389 root_objectid = BTRFS_I(new_inode)->location.objectid;
9390 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9392 new_dentry->d_name.name,
9393 new_dentry->d_name.len);
9394 BUG_ON(new_inode->i_nlink == 0);
9396 ret = btrfs_unlink_inode(trans, dest, new_dir,
9397 d_inode(new_dentry),
9398 new_dentry->d_name.name,
9399 new_dentry->d_name.len);
9401 if (!ret && new_inode->i_nlink == 0)
9402 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
9404 btrfs_abort_transaction(trans, root, ret);
9409 ret = btrfs_add_link(trans, new_dir, old_inode,
9410 new_dentry->d_name.name,
9411 new_dentry->d_name.len, 0, index);
9413 btrfs_abort_transaction(trans, root, ret);
9417 if (old_inode->i_nlink == 1)
9418 BTRFS_I(old_inode)->dir_index = index;
9420 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9421 struct dentry *parent = new_dentry->d_parent;
9422 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9423 btrfs_end_log_trans(root);
9426 btrfs_end_transaction(trans, root);
9428 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9429 up_read(&root->fs_info->subvol_sem);
9434 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9435 struct inode *new_dir, struct dentry *new_dentry,
9438 if (flags & ~RENAME_NOREPLACE)
9441 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
9444 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9446 struct btrfs_delalloc_work *delalloc_work;
9447 struct inode *inode;
9449 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9451 inode = delalloc_work->inode;
9452 filemap_flush(inode->i_mapping);
9453 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9454 &BTRFS_I(inode)->runtime_flags))
9455 filemap_flush(inode->i_mapping);
9457 if (delalloc_work->delay_iput)
9458 btrfs_add_delayed_iput(inode);
9461 complete(&delalloc_work->completion);
9464 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9467 struct btrfs_delalloc_work *work;
9469 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
9473 init_completion(&work->completion);
9474 INIT_LIST_HEAD(&work->list);
9475 work->inode = inode;
9476 work->delay_iput = delay_iput;
9477 WARN_ON_ONCE(!inode);
9478 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9479 btrfs_run_delalloc_work, NULL, NULL);
9484 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9486 wait_for_completion(&work->completion);
9487 kmem_cache_free(btrfs_delalloc_work_cachep, work);
9491 * some fairly slow code that needs optimization. This walks the list
9492 * of all the inodes with pending delalloc and forces them to disk.
9494 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9497 struct btrfs_inode *binode;
9498 struct inode *inode;
9499 struct btrfs_delalloc_work *work, *next;
9500 struct list_head works;
9501 struct list_head splice;
9504 INIT_LIST_HEAD(&works);
9505 INIT_LIST_HEAD(&splice);
9507 mutex_lock(&root->delalloc_mutex);
9508 spin_lock(&root->delalloc_lock);
9509 list_splice_init(&root->delalloc_inodes, &splice);
9510 while (!list_empty(&splice)) {
9511 binode = list_entry(splice.next, struct btrfs_inode,
9514 list_move_tail(&binode->delalloc_inodes,
9515 &root->delalloc_inodes);
9516 inode = igrab(&binode->vfs_inode);
9518 cond_resched_lock(&root->delalloc_lock);
9521 spin_unlock(&root->delalloc_lock);
9523 work = btrfs_alloc_delalloc_work(inode, delay_iput);
9526 btrfs_add_delayed_iput(inode);
9532 list_add_tail(&work->list, &works);
9533 btrfs_queue_work(root->fs_info->flush_workers,
9536 if (nr != -1 && ret >= nr)
9539 spin_lock(&root->delalloc_lock);
9541 spin_unlock(&root->delalloc_lock);
9544 list_for_each_entry_safe(work, next, &works, list) {
9545 list_del_init(&work->list);
9546 btrfs_wait_and_free_delalloc_work(work);
9549 if (!list_empty_careful(&splice)) {
9550 spin_lock(&root->delalloc_lock);
9551 list_splice_tail(&splice, &root->delalloc_inodes);
9552 spin_unlock(&root->delalloc_lock);
9554 mutex_unlock(&root->delalloc_mutex);
9558 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
9562 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
9565 ret = __start_delalloc_inodes(root, delay_iput, -1);
9569 * the filemap_flush will queue IO into the worker threads, but
9570 * we have to make sure the IO is actually started and that
9571 * ordered extents get created before we return
9573 atomic_inc(&root->fs_info->async_submit_draining);
9574 while (atomic_read(&root->fs_info->nr_async_submits) ||
9575 atomic_read(&root->fs_info->async_delalloc_pages)) {
9576 wait_event(root->fs_info->async_submit_wait,
9577 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
9578 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
9580 atomic_dec(&root->fs_info->async_submit_draining);
9584 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
9587 struct btrfs_root *root;
9588 struct list_head splice;
9591 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
9594 INIT_LIST_HEAD(&splice);
9596 mutex_lock(&fs_info->delalloc_root_mutex);
9597 spin_lock(&fs_info->delalloc_root_lock);
9598 list_splice_init(&fs_info->delalloc_roots, &splice);
9599 while (!list_empty(&splice) && nr) {
9600 root = list_first_entry(&splice, struct btrfs_root,
9602 root = btrfs_grab_fs_root(root);
9604 list_move_tail(&root->delalloc_root,
9605 &fs_info->delalloc_roots);
9606 spin_unlock(&fs_info->delalloc_root_lock);
9608 ret = __start_delalloc_inodes(root, delay_iput, nr);
9609 btrfs_put_fs_root(root);
9617 spin_lock(&fs_info->delalloc_root_lock);
9619 spin_unlock(&fs_info->delalloc_root_lock);
9622 atomic_inc(&fs_info->async_submit_draining);
9623 while (atomic_read(&fs_info->nr_async_submits) ||
9624 atomic_read(&fs_info->async_delalloc_pages)) {
9625 wait_event(fs_info->async_submit_wait,
9626 (atomic_read(&fs_info->nr_async_submits) == 0 &&
9627 atomic_read(&fs_info->async_delalloc_pages) == 0));
9629 atomic_dec(&fs_info->async_submit_draining);
9631 if (!list_empty_careful(&splice)) {
9632 spin_lock(&fs_info->delalloc_root_lock);
9633 list_splice_tail(&splice, &fs_info->delalloc_roots);
9634 spin_unlock(&fs_info->delalloc_root_lock);
9636 mutex_unlock(&fs_info->delalloc_root_mutex);
9640 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9641 const char *symname)
9643 struct btrfs_trans_handle *trans;
9644 struct btrfs_root *root = BTRFS_I(dir)->root;
9645 struct btrfs_path *path;
9646 struct btrfs_key key;
9647 struct inode *inode = NULL;
9655 struct btrfs_file_extent_item *ei;
9656 struct extent_buffer *leaf;
9658 name_len = strlen(symname);
9659 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
9660 return -ENAMETOOLONG;
9663 * 2 items for inode item and ref
9664 * 2 items for dir items
9665 * 1 item for updating parent inode item
9666 * 1 item for the inline extent item
9667 * 1 item for xattr if selinux is on
9669 trans = btrfs_start_transaction(root, 7);
9671 return PTR_ERR(trans);
9673 err = btrfs_find_free_ino(root, &objectid);
9677 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9678 dentry->d_name.len, btrfs_ino(dir), objectid,
9679 S_IFLNK|S_IRWXUGO, &index);
9680 if (IS_ERR(inode)) {
9681 err = PTR_ERR(inode);
9686 * If the active LSM wants to access the inode during
9687 * d_instantiate it needs these. Smack checks to see
9688 * if the filesystem supports xattrs by looking at the
9691 inode->i_fop = &btrfs_file_operations;
9692 inode->i_op = &btrfs_file_inode_operations;
9693 inode->i_mapping->a_ops = &btrfs_aops;
9694 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9696 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9698 goto out_unlock_inode;
9700 path = btrfs_alloc_path();
9703 goto out_unlock_inode;
9705 key.objectid = btrfs_ino(inode);
9707 key.type = BTRFS_EXTENT_DATA_KEY;
9708 datasize = btrfs_file_extent_calc_inline_size(name_len);
9709 err = btrfs_insert_empty_item(trans, root, path, &key,
9712 btrfs_free_path(path);
9713 goto out_unlock_inode;
9715 leaf = path->nodes[0];
9716 ei = btrfs_item_ptr(leaf, path->slots[0],
9717 struct btrfs_file_extent_item);
9718 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9719 btrfs_set_file_extent_type(leaf, ei,
9720 BTRFS_FILE_EXTENT_INLINE);
9721 btrfs_set_file_extent_encryption(leaf, ei, 0);
9722 btrfs_set_file_extent_compression(leaf, ei, 0);
9723 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9724 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9726 ptr = btrfs_file_extent_inline_start(ei);
9727 write_extent_buffer(leaf, symname, ptr, name_len);
9728 btrfs_mark_buffer_dirty(leaf);
9729 btrfs_free_path(path);
9731 inode->i_op = &btrfs_symlink_inode_operations;
9732 inode->i_mapping->a_ops = &btrfs_symlink_aops;
9733 inode_set_bytes(inode, name_len);
9734 btrfs_i_size_write(inode, name_len);
9735 err = btrfs_update_inode(trans, root, inode);
9737 * Last step, add directory indexes for our symlink inode. This is the
9738 * last step to avoid extra cleanup of these indexes if an error happens
9742 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
9745 goto out_unlock_inode;
9748 unlock_new_inode(inode);
9749 d_instantiate(dentry, inode);
9752 btrfs_end_transaction(trans, root);
9754 inode_dec_link_count(inode);
9757 btrfs_btree_balance_dirty(root);
9762 unlock_new_inode(inode);
9766 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9767 u64 start, u64 num_bytes, u64 min_size,
9768 loff_t actual_len, u64 *alloc_hint,
9769 struct btrfs_trans_handle *trans)
9771 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9772 struct extent_map *em;
9773 struct btrfs_root *root = BTRFS_I(inode)->root;
9774 struct btrfs_key ins;
9775 u64 cur_offset = start;
9778 u64 last_alloc = (u64)-1;
9780 bool own_trans = true;
9784 while (num_bytes > 0) {
9786 trans = btrfs_start_transaction(root, 3);
9787 if (IS_ERR(trans)) {
9788 ret = PTR_ERR(trans);
9793 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
9794 cur_bytes = max(cur_bytes, min_size);
9796 * If we are severely fragmented we could end up with really
9797 * small allocations, so if the allocator is returning small
9798 * chunks lets make its job easier by only searching for those
9801 cur_bytes = min(cur_bytes, last_alloc);
9802 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
9803 *alloc_hint, &ins, 1, 0);
9806 btrfs_end_transaction(trans, root);
9810 last_alloc = ins.offset;
9811 ret = insert_reserved_file_extent(trans, inode,
9812 cur_offset, ins.objectid,
9813 ins.offset, ins.offset,
9814 ins.offset, 0, 0, 0,
9815 BTRFS_FILE_EXTENT_PREALLOC);
9817 btrfs_free_reserved_extent(root, ins.objectid,
9819 btrfs_abort_transaction(trans, root, ret);
9821 btrfs_end_transaction(trans, root);
9825 btrfs_drop_extent_cache(inode, cur_offset,
9826 cur_offset + ins.offset -1, 0);
9828 em = alloc_extent_map();
9830 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9831 &BTRFS_I(inode)->runtime_flags);
9835 em->start = cur_offset;
9836 em->orig_start = cur_offset;
9837 em->len = ins.offset;
9838 em->block_start = ins.objectid;
9839 em->block_len = ins.offset;
9840 em->orig_block_len = ins.offset;
9841 em->ram_bytes = ins.offset;
9842 em->bdev = root->fs_info->fs_devices->latest_bdev;
9843 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9844 em->generation = trans->transid;
9847 write_lock(&em_tree->lock);
9848 ret = add_extent_mapping(em_tree, em, 1);
9849 write_unlock(&em_tree->lock);
9852 btrfs_drop_extent_cache(inode, cur_offset,
9853 cur_offset + ins.offset - 1,
9856 free_extent_map(em);
9858 num_bytes -= ins.offset;
9859 cur_offset += ins.offset;
9860 *alloc_hint = ins.objectid + ins.offset;
9862 inode_inc_iversion(inode);
9863 inode->i_ctime = CURRENT_TIME;
9864 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
9865 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
9866 (actual_len > inode->i_size) &&
9867 (cur_offset > inode->i_size)) {
9868 if (cur_offset > actual_len)
9869 i_size = actual_len;
9871 i_size = cur_offset;
9872 i_size_write(inode, i_size);
9873 btrfs_ordered_update_i_size(inode, i_size, NULL);
9876 ret = btrfs_update_inode(trans, root, inode);
9879 btrfs_abort_transaction(trans, root, ret);
9881 btrfs_end_transaction(trans, root);
9886 btrfs_end_transaction(trans, root);
9891 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9892 u64 start, u64 num_bytes, u64 min_size,
9893 loff_t actual_len, u64 *alloc_hint)
9895 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9896 min_size, actual_len, alloc_hint,
9900 int btrfs_prealloc_file_range_trans(struct inode *inode,
9901 struct btrfs_trans_handle *trans, int mode,
9902 u64 start, u64 num_bytes, u64 min_size,
9903 loff_t actual_len, u64 *alloc_hint)
9905 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9906 min_size, actual_len, alloc_hint, trans);
9909 static int btrfs_set_page_dirty(struct page *page)
9911 return __set_page_dirty_nobuffers(page);
9914 static int btrfs_permission(struct inode *inode, int mask)
9916 struct btrfs_root *root = BTRFS_I(inode)->root;
9917 umode_t mode = inode->i_mode;
9919 if (mask & MAY_WRITE &&
9920 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9921 if (btrfs_root_readonly(root))
9923 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9926 return generic_permission(inode, mask);
9929 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9931 struct btrfs_trans_handle *trans;
9932 struct btrfs_root *root = BTRFS_I(dir)->root;
9933 struct inode *inode = NULL;
9939 * 5 units required for adding orphan entry
9941 trans = btrfs_start_transaction(root, 5);
9943 return PTR_ERR(trans);
9945 ret = btrfs_find_free_ino(root, &objectid);
9949 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9950 btrfs_ino(dir), objectid, mode, &index);
9951 if (IS_ERR(inode)) {
9952 ret = PTR_ERR(inode);
9957 inode->i_fop = &btrfs_file_operations;
9958 inode->i_op = &btrfs_file_inode_operations;
9960 inode->i_mapping->a_ops = &btrfs_aops;
9961 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9963 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9967 ret = btrfs_update_inode(trans, root, inode);
9970 ret = btrfs_orphan_add(trans, inode);
9975 * We set number of links to 0 in btrfs_new_inode(), and here we set
9976 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9979 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9981 set_nlink(inode, 1);
9982 unlock_new_inode(inode);
9983 d_tmpfile(dentry, inode);
9984 mark_inode_dirty(inode);
9987 btrfs_end_transaction(trans, root);
9990 btrfs_balance_delayed_items(root);
9991 btrfs_btree_balance_dirty(root);
9995 unlock_new_inode(inode);
10000 /* Inspired by filemap_check_errors() */
10001 int btrfs_inode_check_errors(struct inode *inode)
10005 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10006 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10008 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10009 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10015 static const struct inode_operations btrfs_dir_inode_operations = {
10016 .getattr = btrfs_getattr,
10017 .lookup = btrfs_lookup,
10018 .create = btrfs_create,
10019 .unlink = btrfs_unlink,
10020 .link = btrfs_link,
10021 .mkdir = btrfs_mkdir,
10022 .rmdir = btrfs_rmdir,
10023 .rename2 = btrfs_rename2,
10024 .symlink = btrfs_symlink,
10025 .setattr = btrfs_setattr,
10026 .mknod = btrfs_mknod,
10027 .setxattr = btrfs_setxattr,
10028 .getxattr = btrfs_getxattr,
10029 .listxattr = btrfs_listxattr,
10030 .removexattr = btrfs_removexattr,
10031 .permission = btrfs_permission,
10032 .get_acl = btrfs_get_acl,
10033 .set_acl = btrfs_set_acl,
10034 .update_time = btrfs_update_time,
10035 .tmpfile = btrfs_tmpfile,
10037 static const struct inode_operations btrfs_dir_ro_inode_operations = {
10038 .lookup = btrfs_lookup,
10039 .permission = btrfs_permission,
10040 .get_acl = btrfs_get_acl,
10041 .set_acl = btrfs_set_acl,
10042 .update_time = btrfs_update_time,
10045 static const struct file_operations btrfs_dir_file_operations = {
10046 .llseek = generic_file_llseek,
10047 .read = generic_read_dir,
10048 .iterate = btrfs_real_readdir,
10049 .unlocked_ioctl = btrfs_ioctl,
10050 #ifdef CONFIG_COMPAT
10051 .compat_ioctl = btrfs_ioctl,
10053 .release = btrfs_release_file,
10054 .fsync = btrfs_sync_file,
10057 static struct extent_io_ops btrfs_extent_io_ops = {
10058 .fill_delalloc = run_delalloc_range,
10059 .submit_bio_hook = btrfs_submit_bio_hook,
10060 .merge_bio_hook = btrfs_merge_bio_hook,
10061 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10062 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
10063 .writepage_start_hook = btrfs_writepage_start_hook,
10064 .set_bit_hook = btrfs_set_bit_hook,
10065 .clear_bit_hook = btrfs_clear_bit_hook,
10066 .merge_extent_hook = btrfs_merge_extent_hook,
10067 .split_extent_hook = btrfs_split_extent_hook,
10071 * btrfs doesn't support the bmap operation because swapfiles
10072 * use bmap to make a mapping of extents in the file. They assume
10073 * these extents won't change over the life of the file and they
10074 * use the bmap result to do IO directly to the drive.
10076 * the btrfs bmap call would return logical addresses that aren't
10077 * suitable for IO and they also will change frequently as COW
10078 * operations happen. So, swapfile + btrfs == corruption.
10080 * For now we're avoiding this by dropping bmap.
10082 static const struct address_space_operations btrfs_aops = {
10083 .readpage = btrfs_readpage,
10084 .writepage = btrfs_writepage,
10085 .writepages = btrfs_writepages,
10086 .readpages = btrfs_readpages,
10087 .direct_IO = btrfs_direct_IO,
10088 .invalidatepage = btrfs_invalidatepage,
10089 .releasepage = btrfs_releasepage,
10090 .set_page_dirty = btrfs_set_page_dirty,
10091 .error_remove_page = generic_error_remove_page,
10094 static const struct address_space_operations btrfs_symlink_aops = {
10095 .readpage = btrfs_readpage,
10096 .writepage = btrfs_writepage,
10097 .invalidatepage = btrfs_invalidatepage,
10098 .releasepage = btrfs_releasepage,
10101 static const struct inode_operations btrfs_file_inode_operations = {
10102 .getattr = btrfs_getattr,
10103 .setattr = btrfs_setattr,
10104 .setxattr = btrfs_setxattr,
10105 .getxattr = btrfs_getxattr,
10106 .listxattr = btrfs_listxattr,
10107 .removexattr = btrfs_removexattr,
10108 .permission = btrfs_permission,
10109 .fiemap = btrfs_fiemap,
10110 .get_acl = btrfs_get_acl,
10111 .set_acl = btrfs_set_acl,
10112 .update_time = btrfs_update_time,
10114 static const struct inode_operations btrfs_special_inode_operations = {
10115 .getattr = btrfs_getattr,
10116 .setattr = btrfs_setattr,
10117 .permission = btrfs_permission,
10118 .setxattr = btrfs_setxattr,
10119 .getxattr = btrfs_getxattr,
10120 .listxattr = btrfs_listxattr,
10121 .removexattr = btrfs_removexattr,
10122 .get_acl = btrfs_get_acl,
10123 .set_acl = btrfs_set_acl,
10124 .update_time = btrfs_update_time,
10126 static const struct inode_operations btrfs_symlink_inode_operations = {
10127 .readlink = generic_readlink,
10128 .follow_link = page_follow_link_light,
10129 .put_link = page_put_link,
10130 .getattr = btrfs_getattr,
10131 .setattr = btrfs_setattr,
10132 .permission = btrfs_permission,
10133 .setxattr = btrfs_setxattr,
10134 .getxattr = btrfs_getxattr,
10135 .listxattr = btrfs_listxattr,
10136 .removexattr = btrfs_removexattr,
10137 .update_time = btrfs_update_time,
10140 const struct dentry_operations btrfs_dentry_operations = {
10141 .d_delete = btrfs_dentry_delete,
10142 .d_release = btrfs_dentry_release,