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/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #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;
77 static const struct inode_operations btrfs_dir_inode_operations;
78 static const struct inode_operations btrfs_symlink_inode_operations;
79 static const struct inode_operations btrfs_dir_ro_inode_operations;
80 static const struct inode_operations btrfs_special_inode_operations;
81 static const struct inode_operations btrfs_file_inode_operations;
82 static const struct address_space_operations btrfs_aops;
83 static const struct address_space_operations btrfs_symlink_aops;
84 static const struct file_operations btrfs_dir_file_operations;
85 static const struct extent_io_ops btrfs_extent_io_ops;
87 static struct kmem_cache *btrfs_inode_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 const 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, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
112 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
118 static int btrfs_dirty_inode(struct inode *inode);
120 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121 void btrfs_test_inode_set_ops(struct inode *inode)
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
127 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
133 err = btrfs_init_acl(trans, inode, dir);
135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
144 static int insert_inline_extent(struct btrfs_trans_handle *trans,
145 struct btrfs_path *path, int extent_inserted,
146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
149 struct page **compressed_pages)
151 struct extent_buffer *leaf;
152 struct page *page = NULL;
155 struct btrfs_file_extent_item *ei;
158 size_t cur_size = size;
159 unsigned long offset;
161 if (compressed_size && compressed_pages)
162 cur_size = compressed_size;
164 inode_add_bytes(inode, size);
166 if (!extent_inserted) {
167 struct btrfs_key key;
170 key.objectid = btrfs_ino(BTRFS_I(inode));
172 key.type = BTRFS_EXTENT_DATA_KEY;
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
193 if (compress_type != BTRFS_COMPRESS_NONE) {
196 while (compressed_size > 0) {
197 cpage = compressed_pages[i];
198 cur_size = min_t(unsigned long, compressed_size,
201 kaddr = kmap_atomic(cpage);
202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
203 kunmap_atomic(kaddr);
207 compressed_size -= cur_size;
209 btrfs_set_file_extent_compression(leaf, ei,
212 page = find_get_page(inode->i_mapping,
213 start >> PAGE_SHIFT);
214 btrfs_set_file_extent_compression(leaf, ei, 0);
215 kaddr = kmap_atomic(page);
216 offset = start & (PAGE_SIZE - 1);
217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
218 kunmap_atomic(kaddr);
221 btrfs_mark_buffer_dirty(leaf);
222 btrfs_release_path(path);
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
233 BTRFS_I(inode)->disk_i_size = inode->i_size;
234 ret = btrfs_update_inode(trans, root, inode);
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
247 static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
251 struct page **compressed_pages)
253 struct btrfs_fs_info *fs_info = root->fs_info;
254 struct btrfs_trans_handle *trans;
255 u64 isize = i_size_read(inode);
256 u64 actual_end = min(end + 1, isize);
257 u64 inline_len = actual_end - start;
258 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
259 u64 data_len = inline_len;
261 struct btrfs_path *path;
262 int extent_inserted = 0;
263 u32 extent_item_size;
266 data_len = compressed_size;
269 actual_end > fs_info->sectorsize ||
270 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
272 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
274 data_len > fs_info->max_inline) {
278 path = btrfs_alloc_path();
282 trans = btrfs_join_transaction(root);
284 btrfs_free_path(path);
285 return PTR_ERR(trans);
287 trans->block_rsv = &fs_info->delalloc_block_rsv;
289 if (compressed_size && compressed_pages)
290 extent_item_size = btrfs_file_extent_calc_inline_size(
293 extent_item_size = btrfs_file_extent_calc_inline_size(
296 ret = __btrfs_drop_extents(trans, root, inode, path,
297 start, aligned_end, NULL,
298 1, 1, extent_item_size, &extent_inserted);
300 btrfs_abort_transaction(trans, ret);
304 if (isize > actual_end)
305 inline_len = min_t(u64, isize, actual_end);
306 ret = insert_inline_extent(trans, path, extent_inserted,
308 inline_len, compressed_size,
309 compress_type, compressed_pages);
310 if (ret && ret != -ENOSPC) {
311 btrfs_abort_transaction(trans, ret);
313 } else if (ret == -ENOSPC) {
318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
319 btrfs_delalloc_release_metadata(inode, end + 1 - start);
320 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
328 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
329 btrfs_free_path(path);
330 btrfs_end_transaction(trans);
334 struct async_extent {
339 unsigned long nr_pages;
341 struct list_head list;
346 struct btrfs_root *root;
347 struct page *locked_page;
350 struct list_head extents;
351 struct btrfs_work work;
354 static noinline int add_async_extent(struct async_cow *cow,
355 u64 start, u64 ram_size,
358 unsigned long nr_pages,
361 struct async_extent *async_extent;
363 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
364 BUG_ON(!async_extent); /* -ENOMEM */
365 async_extent->start = start;
366 async_extent->ram_size = ram_size;
367 async_extent->compressed_size = compressed_size;
368 async_extent->pages = pages;
369 async_extent->nr_pages = nr_pages;
370 async_extent->compress_type = compress_type;
371 list_add_tail(&async_extent->list, &cow->extents);
375 static inline int inode_need_compress(struct inode *inode)
377 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
380 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
382 /* bad compression ratios */
383 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
385 if (btrfs_test_opt(fs_info, COMPRESS) ||
386 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 BTRFS_I(inode)->force_compress)
392 static inline void inode_should_defrag(struct inode *inode,
393 u64 start, u64 end, u64 num_bytes, u64 small_write)
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes < small_write &&
397 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 btrfs_add_inode_defrag(NULL, inode);
402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
415 * are written in the same order that the flusher thread sent them
418 static noinline void compress_file_range(struct inode *inode,
419 struct page *locked_page,
421 struct async_cow *async_cow,
424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
425 struct btrfs_root *root = BTRFS_I(inode)->root;
427 u64 blocksize = fs_info->sectorsize;
429 u64 isize = i_size_read(inode);
431 struct page **pages = NULL;
432 unsigned long nr_pages;
433 unsigned long nr_pages_ret = 0;
434 unsigned long total_compressed = 0;
435 unsigned long total_in = 0;
436 unsigned long max_compressed = SZ_128K;
437 unsigned long max_uncompressed = SZ_128K;
440 int compress_type = fs_info->compress_type;
443 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
445 actual_end = min_t(u64, isize, end + 1);
448 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
449 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
452 * we don't want to send crud past the end of i_size through
453 * compression, that's just a waste of CPU time. So, if the
454 * end of the file is before the start of our current
455 * requested range of bytes, we bail out to the uncompressed
456 * cleanup code that can deal with all of this.
458 * It isn't really the fastest way to fix things, but this is a
459 * very uncommon corner.
461 if (actual_end <= start)
462 goto cleanup_and_bail_uncompressed;
464 total_compressed = actual_end - start;
467 * skip compression for a small file range(<=blocksize) that
468 * isn't an inline extent, since it doesn't save disk space at all.
470 if (total_compressed <= blocksize &&
471 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 goto cleanup_and_bail_uncompressed;
474 /* we want to make sure that amount of ram required to uncompress
475 * an extent is reasonable, so we limit the total size in ram
476 * of a compressed extent to 128k. This is a crucial number
477 * because it also controls how easily we can spread reads across
478 * cpus for decompression.
480 * We also want to make sure the amount of IO required to do
481 * a random read is reasonably small, so we limit the size of
482 * a compressed extent to 128k.
484 total_compressed = min(total_compressed, max_uncompressed);
485 num_bytes = ALIGN(end - start + 1, blocksize);
486 num_bytes = max(blocksize, num_bytes);
491 * we do compression for mount -o compress and when the
492 * inode has not been flagged as nocompress. This flag can
493 * change at any time if we discover bad compression ratios.
495 if (inode_need_compress(inode)) {
497 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
499 /* just bail out to the uncompressed code */
503 if (BTRFS_I(inode)->force_compress)
504 compress_type = BTRFS_I(inode)->force_compress;
507 * we need to call clear_page_dirty_for_io on each
508 * page in the range. Otherwise applications with the file
509 * mmap'd can wander in and change the page contents while
510 * we are compressing them.
512 * If the compression fails for any reason, we set the pages
513 * dirty again later on.
515 extent_range_clear_dirty_for_io(inode, start, end);
517 ret = btrfs_compress_pages(compress_type,
518 inode->i_mapping, start,
519 total_compressed, pages,
520 nr_pages, &nr_pages_ret,
526 unsigned long offset = total_compressed &
528 struct page *page = pages[nr_pages_ret - 1];
531 /* zero the tail end of the last page, we might be
532 * sending it down to disk
535 kaddr = kmap_atomic(page);
536 memset(kaddr + offset, 0,
538 kunmap_atomic(kaddr);
545 /* lets try to make an inline extent */
546 if (ret || total_in < (actual_end - start)) {
547 /* we didn't compress the entire range, try
548 * to make an uncompressed inline extent.
550 ret = cow_file_range_inline(root, inode, start, end,
551 0, BTRFS_COMPRESS_NONE, NULL);
553 /* try making a compressed inline extent */
554 ret = cow_file_range_inline(root, inode, start, end,
556 compress_type, pages);
559 unsigned long clear_flags = EXTENT_DELALLOC |
561 unsigned long page_error_op;
563 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
564 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
567 * inline extent creation worked or returned error,
568 * we don't need to create any more async work items.
569 * Unlock and free up our temp pages.
571 extent_clear_unlock_delalloc(inode, start, end, end,
578 btrfs_free_reserved_data_space_noquota(inode, start,
586 * we aren't doing an inline extent round the compressed size
587 * up to a block size boundary so the allocator does sane
590 total_compressed = ALIGN(total_compressed, blocksize);
593 * one last check to make sure the compression is really a
594 * win, compare the page count read with the blocks on disk
596 total_in = ALIGN(total_in, PAGE_SIZE);
597 if (total_compressed >= total_in) {
600 num_bytes = total_in;
604 * The async work queues will take care of doing actual
605 * allocation on disk for these compressed pages, and
606 * will submit them to the elevator.
608 add_async_extent(async_cow, start, num_bytes,
609 total_compressed, pages, nr_pages_ret,
612 if (start + num_bytes < end) {
623 * the compression code ran but failed to make things smaller,
624 * free any pages it allocated and our page pointer array
626 for (i = 0; i < nr_pages_ret; i++) {
627 WARN_ON(pages[i]->mapping);
632 total_compressed = 0;
635 /* flag the file so we don't compress in the future */
636 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
637 !(BTRFS_I(inode)->force_compress)) {
638 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
641 cleanup_and_bail_uncompressed:
643 * No compression, but we still need to write the pages in the file
644 * we've been given so far. redirty the locked page if it corresponds
645 * to our extent and set things up for the async work queue to run
646 * cow_file_range to do the normal delalloc dance.
648 if (page_offset(locked_page) >= start &&
649 page_offset(locked_page) <= end)
650 __set_page_dirty_nobuffers(locked_page);
651 /* unlocked later on in the async handlers */
654 extent_range_redirty_for_io(inode, start, end);
655 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
656 BTRFS_COMPRESS_NONE);
662 for (i = 0; i < nr_pages_ret; i++) {
663 WARN_ON(pages[i]->mapping);
669 static void free_async_extent_pages(struct async_extent *async_extent)
673 if (!async_extent->pages)
676 for (i = 0; i < async_extent->nr_pages; i++) {
677 WARN_ON(async_extent->pages[i]->mapping);
678 put_page(async_extent->pages[i]);
680 kfree(async_extent->pages);
681 async_extent->nr_pages = 0;
682 async_extent->pages = NULL;
686 * phase two of compressed writeback. This is the ordered portion
687 * of the code, which only gets called in the order the work was
688 * queued. We walk all the async extents created by compress_file_range
689 * and send them down to the disk.
691 static noinline void submit_compressed_extents(struct inode *inode,
692 struct async_cow *async_cow)
694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
695 struct async_extent *async_extent;
697 struct btrfs_key ins;
698 struct extent_map *em;
699 struct btrfs_root *root = BTRFS_I(inode)->root;
700 struct extent_io_tree *io_tree;
704 while (!list_empty(&async_cow->extents)) {
705 async_extent = list_entry(async_cow->extents.next,
706 struct async_extent, list);
707 list_del(&async_extent->list);
709 io_tree = &BTRFS_I(inode)->io_tree;
712 /* did the compression code fall back to uncompressed IO? */
713 if (!async_extent->pages) {
714 int page_started = 0;
715 unsigned long nr_written = 0;
717 lock_extent(io_tree, async_extent->start,
718 async_extent->start +
719 async_extent->ram_size - 1);
721 /* allocate blocks */
722 ret = cow_file_range(inode, async_cow->locked_page,
724 async_extent->start +
725 async_extent->ram_size - 1,
726 async_extent->start +
727 async_extent->ram_size - 1,
728 &page_started, &nr_written, 0,
734 * if page_started, cow_file_range inserted an
735 * inline extent and took care of all the unlocking
736 * and IO for us. Otherwise, we need to submit
737 * all those pages down to the drive.
739 if (!page_started && !ret)
740 extent_write_locked_range(io_tree,
741 inode, async_extent->start,
742 async_extent->start +
743 async_extent->ram_size - 1,
747 unlock_page(async_cow->locked_page);
753 lock_extent(io_tree, async_extent->start,
754 async_extent->start + async_extent->ram_size - 1);
756 ret = btrfs_reserve_extent(root, async_extent->ram_size,
757 async_extent->compressed_size,
758 async_extent->compressed_size,
759 0, alloc_hint, &ins, 1, 1);
761 free_async_extent_pages(async_extent);
763 if (ret == -ENOSPC) {
764 unlock_extent(io_tree, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
769 * we need to redirty the pages if we decide to
770 * fallback to uncompressed IO, otherwise we
771 * will not submit these pages down to lower
774 extent_range_redirty_for_io(inode,
776 async_extent->start +
777 async_extent->ram_size - 1);
784 * here we're doing allocation and writeback of the
787 em = create_io_em(inode, async_extent->start,
788 async_extent->ram_size, /* len */
789 async_extent->start, /* orig_start */
790 ins.objectid, /* block_start */
791 ins.offset, /* block_len */
792 ins.offset, /* orig_block_len */
793 async_extent->ram_size, /* ram_bytes */
794 async_extent->compress_type,
795 BTRFS_ORDERED_COMPRESSED);
797 /* ret value is not necessary due to void function */
798 goto out_free_reserve;
801 ret = btrfs_add_ordered_extent_compress(inode,
804 async_extent->ram_size,
806 BTRFS_ORDERED_COMPRESSED,
807 async_extent->compress_type);
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
812 goto out_free_reserve;
814 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
817 * clear dirty, set writeback and unlock the pages.
819 extent_clear_unlock_delalloc(inode, async_extent->start,
820 async_extent->start +
821 async_extent->ram_size - 1,
822 async_extent->start +
823 async_extent->ram_size - 1,
824 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
825 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
827 ret = btrfs_submit_compressed_write(inode,
829 async_extent->ram_size,
831 ins.offset, async_extent->pages,
832 async_extent->nr_pages);
834 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
835 struct page *p = async_extent->pages[0];
836 const u64 start = async_extent->start;
837 const u64 end = start + async_extent->ram_size - 1;
839 p->mapping = inode->i_mapping;
840 tree->ops->writepage_end_io_hook(p, start, end,
843 extent_clear_unlock_delalloc(inode, start, end, end,
847 free_async_extent_pages(async_extent);
849 alloc_hint = ins.objectid + ins.offset;
855 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
856 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
858 extent_clear_unlock_delalloc(inode, async_extent->start,
859 async_extent->start +
860 async_extent->ram_size - 1,
861 async_extent->start +
862 async_extent->ram_size - 1,
863 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
864 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
865 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
866 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
868 free_async_extent_pages(async_extent);
873 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
876 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 struct extent_map *em;
880 read_lock(&em_tree->lock);
881 em = search_extent_mapping(em_tree, start, num_bytes);
884 * if block start isn't an actual block number then find the
885 * first block in this inode and use that as a hint. If that
886 * block is also bogus then just don't worry about it.
888 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
890 em = search_extent_mapping(em_tree, 0, 0);
891 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
892 alloc_hint = em->block_start;
896 alloc_hint = em->block_start;
900 read_unlock(&em_tree->lock);
906 * when extent_io.c finds a delayed allocation range in the file,
907 * the call backs end up in this code. The basic idea is to
908 * allocate extents on disk for the range, and create ordered data structs
909 * in ram to track those extents.
911 * locked_page is the page that writepage had locked already. We use
912 * it to make sure we don't do extra locks or unlocks.
914 * *page_started is set to one if we unlock locked_page and do everything
915 * required to start IO on it. It may be clean and already done with
918 static noinline int cow_file_range(struct inode *inode,
919 struct page *locked_page,
920 u64 start, u64 end, u64 delalloc_end,
921 int *page_started, unsigned long *nr_written,
922 int unlock, struct btrfs_dedupe_hash *hash)
924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
925 struct btrfs_root *root = BTRFS_I(inode)->root;
928 unsigned long ram_size;
931 u64 blocksize = fs_info->sectorsize;
932 struct btrfs_key ins;
933 struct extent_map *em;
936 if (btrfs_is_free_space_inode(inode)) {
942 num_bytes = ALIGN(end - start + 1, blocksize);
943 num_bytes = max(blocksize, num_bytes);
944 disk_num_bytes = num_bytes;
946 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
949 /* lets try to make an inline extent */
950 ret = cow_file_range_inline(root, inode, start, end, 0,
951 BTRFS_COMPRESS_NONE, NULL);
953 extent_clear_unlock_delalloc(inode, start, end,
955 EXTENT_LOCKED | EXTENT_DELALLOC |
956 EXTENT_DEFRAG, PAGE_UNLOCK |
957 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
959 btrfs_free_reserved_data_space_noquota(inode, start,
961 *nr_written = *nr_written +
962 (end - start + PAGE_SIZE) / PAGE_SIZE;
965 } else if (ret < 0) {
970 BUG_ON(disk_num_bytes >
971 btrfs_super_total_bytes(fs_info->super_copy));
973 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
974 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
976 while (disk_num_bytes > 0) {
979 cur_alloc_size = disk_num_bytes;
980 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
981 fs_info->sectorsize, 0, alloc_hint,
986 ram_size = ins.offset;
987 em = create_io_em(inode, start, ins.offset, /* len */
988 start, /* orig_start */
989 ins.objectid, /* block_start */
990 ins.offset, /* block_len */
991 ins.offset, /* orig_block_len */
992 ram_size, /* ram_bytes */
993 BTRFS_COMPRESS_NONE, /* compress_type */
994 BTRFS_ORDERED_REGULAR /* type */);
999 cur_alloc_size = ins.offset;
1000 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1001 ram_size, cur_alloc_size, 0);
1003 goto out_drop_extent_cache;
1005 if (root->root_key.objectid ==
1006 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1007 ret = btrfs_reloc_clone_csums(inode, start,
1010 goto out_drop_extent_cache;
1013 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1015 if (disk_num_bytes < cur_alloc_size)
1018 /* we're not doing compressed IO, don't unlock the first
1019 * page (which the caller expects to stay locked), don't
1020 * clear any dirty bits and don't set any writeback bits
1022 * Do set the Private2 bit so we know this page was properly
1023 * setup for writepage
1025 op = unlock ? PAGE_UNLOCK : 0;
1026 op |= PAGE_SET_PRIVATE2;
1028 extent_clear_unlock_delalloc(inode, start,
1029 start + ram_size - 1,
1030 delalloc_end, locked_page,
1031 EXTENT_LOCKED | EXTENT_DELALLOC,
1033 disk_num_bytes -= cur_alloc_size;
1034 num_bytes -= cur_alloc_size;
1035 alloc_hint = ins.objectid + ins.offset;
1036 start += cur_alloc_size;
1041 out_drop_extent_cache:
1042 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1044 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1045 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1047 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1049 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1050 EXTENT_DELALLOC | EXTENT_DEFRAG,
1051 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1052 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1057 * work queue call back to started compression on a file and pages
1059 static noinline void async_cow_start(struct btrfs_work *work)
1061 struct async_cow *async_cow;
1063 async_cow = container_of(work, struct async_cow, work);
1065 compress_file_range(async_cow->inode, async_cow->locked_page,
1066 async_cow->start, async_cow->end, async_cow,
1068 if (num_added == 0) {
1069 btrfs_add_delayed_iput(async_cow->inode);
1070 async_cow->inode = NULL;
1075 * work queue call back to submit previously compressed pages
1077 static noinline void async_cow_submit(struct btrfs_work *work)
1079 struct btrfs_fs_info *fs_info;
1080 struct async_cow *async_cow;
1081 struct btrfs_root *root;
1082 unsigned long nr_pages;
1084 async_cow = container_of(work, struct async_cow, work);
1086 root = async_cow->root;
1087 fs_info = root->fs_info;
1088 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1092 * atomic_sub_return implies a barrier for waitqueue_active
1094 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1096 waitqueue_active(&fs_info->async_submit_wait))
1097 wake_up(&fs_info->async_submit_wait);
1099 if (async_cow->inode)
1100 submit_compressed_extents(async_cow->inode, async_cow);
1103 static noinline void async_cow_free(struct btrfs_work *work)
1105 struct async_cow *async_cow;
1106 async_cow = container_of(work, struct async_cow, work);
1107 if (async_cow->inode)
1108 btrfs_add_delayed_iput(async_cow->inode);
1112 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1113 u64 start, u64 end, int *page_started,
1114 unsigned long *nr_written)
1116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1117 struct async_cow *async_cow;
1118 struct btrfs_root *root = BTRFS_I(inode)->root;
1119 unsigned long nr_pages;
1122 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1123 1, 0, NULL, GFP_NOFS);
1124 while (start < end) {
1125 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1126 BUG_ON(!async_cow); /* -ENOMEM */
1127 async_cow->inode = igrab(inode);
1128 async_cow->root = root;
1129 async_cow->locked_page = locked_page;
1130 async_cow->start = start;
1132 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1133 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1136 cur_end = min(end, start + SZ_512K - 1);
1138 async_cow->end = cur_end;
1139 INIT_LIST_HEAD(&async_cow->extents);
1141 btrfs_init_work(&async_cow->work,
1142 btrfs_delalloc_helper,
1143 async_cow_start, async_cow_submit,
1146 nr_pages = (cur_end - start + PAGE_SIZE) >>
1148 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1150 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1152 while (atomic_read(&fs_info->async_submit_draining) &&
1153 atomic_read(&fs_info->async_delalloc_pages)) {
1154 wait_event(fs_info->async_submit_wait,
1155 (atomic_read(&fs_info->async_delalloc_pages) ==
1159 *nr_written += nr_pages;
1160 start = cur_end + 1;
1166 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1167 u64 bytenr, u64 num_bytes)
1170 struct btrfs_ordered_sum *sums;
1173 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1174 bytenr + num_bytes - 1, &list, 0);
1175 if (ret == 0 && list_empty(&list))
1178 while (!list_empty(&list)) {
1179 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1180 list_del(&sums->list);
1187 * when nowcow writeback call back. This checks for snapshots or COW copies
1188 * of the extents that exist in the file, and COWs the file as required.
1190 * If no cow copies or snapshots exist, we write directly to the existing
1193 static noinline int run_delalloc_nocow(struct inode *inode,
1194 struct page *locked_page,
1195 u64 start, u64 end, int *page_started, int force,
1196 unsigned long *nr_written)
1198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 struct extent_buffer *leaf;
1201 struct btrfs_path *path;
1202 struct btrfs_file_extent_item *fi;
1203 struct btrfs_key found_key;
1204 struct extent_map *em;
1219 u64 ino = btrfs_ino(BTRFS_I(inode));
1221 path = btrfs_alloc_path();
1223 extent_clear_unlock_delalloc(inode, start, end, end,
1225 EXTENT_LOCKED | EXTENT_DELALLOC |
1226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
1234 nolock = btrfs_is_free_space_inode(inode);
1236 cow_start = (u64)-1;
1239 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1243 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1244 leaf = path->nodes[0];
1245 btrfs_item_key_to_cpu(leaf, &found_key,
1246 path->slots[0] - 1);
1247 if (found_key.objectid == ino &&
1248 found_key.type == BTRFS_EXTENT_DATA_KEY)
1253 leaf = path->nodes[0];
1254 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1255 ret = btrfs_next_leaf(root, path);
1260 leaf = path->nodes[0];
1266 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1268 if (found_key.objectid > ino)
1270 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1271 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1275 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1276 found_key.offset > end)
1279 if (found_key.offset > cur_offset) {
1280 extent_end = found_key.offset;
1285 fi = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_file_extent_item);
1287 extent_type = btrfs_file_extent_type(leaf, fi);
1289 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1290 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1291 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1292 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1293 extent_offset = btrfs_file_extent_offset(leaf, fi);
1294 extent_end = found_key.offset +
1295 btrfs_file_extent_num_bytes(leaf, fi);
1297 btrfs_file_extent_disk_num_bytes(leaf, fi);
1298 if (extent_end <= start) {
1302 if (disk_bytenr == 0)
1304 if (btrfs_file_extent_compression(leaf, fi) ||
1305 btrfs_file_extent_encryption(leaf, fi) ||
1306 btrfs_file_extent_other_encoding(leaf, fi))
1308 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1310 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1312 if (btrfs_cross_ref_exist(root, ino,
1314 extent_offset, disk_bytenr))
1316 disk_bytenr += extent_offset;
1317 disk_bytenr += cur_offset - found_key.offset;
1318 num_bytes = min(end + 1, extent_end) - cur_offset;
1320 * if there are pending snapshots for this root,
1321 * we fall into common COW way.
1324 err = btrfs_start_write_no_snapshoting(root);
1329 * force cow if csum exists in the range.
1330 * this ensure that csum for a given extent are
1331 * either valid or do not exist.
1333 if (csum_exist_in_range(fs_info, disk_bytenr,
1336 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1339 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1340 extent_end = found_key.offset +
1341 btrfs_file_extent_inline_len(leaf,
1342 path->slots[0], fi);
1343 extent_end = ALIGN(extent_end,
1344 fs_info->sectorsize);
1349 if (extent_end <= start) {
1351 if (!nolock && nocow)
1352 btrfs_end_write_no_snapshoting(root);
1354 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1358 if (cow_start == (u64)-1)
1359 cow_start = cur_offset;
1360 cur_offset = extent_end;
1361 if (cur_offset > end)
1367 btrfs_release_path(path);
1368 if (cow_start != (u64)-1) {
1369 ret = cow_file_range(inode, locked_page,
1370 cow_start, found_key.offset - 1,
1371 end, page_started, nr_written, 1,
1374 if (!nolock && nocow)
1375 btrfs_end_write_no_snapshoting(root);
1377 btrfs_dec_nocow_writers(fs_info,
1381 cow_start = (u64)-1;
1384 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1385 u64 orig_start = found_key.offset - extent_offset;
1387 em = create_io_em(inode, cur_offset, num_bytes,
1389 disk_bytenr, /* block_start */
1390 num_bytes, /* block_len */
1391 disk_num_bytes, /* orig_block_len */
1392 ram_bytes, BTRFS_COMPRESS_NONE,
1393 BTRFS_ORDERED_PREALLOC);
1395 if (!nolock && nocow)
1396 btrfs_end_write_no_snapshoting(root);
1398 btrfs_dec_nocow_writers(fs_info,
1403 free_extent_map(em);
1406 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1407 type = BTRFS_ORDERED_PREALLOC;
1409 type = BTRFS_ORDERED_NOCOW;
1412 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1413 num_bytes, num_bytes, type);
1415 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1416 BUG_ON(ret); /* -ENOMEM */
1418 if (root->root_key.objectid ==
1419 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1423 if (!nolock && nocow)
1424 btrfs_end_write_no_snapshoting(root);
1429 extent_clear_unlock_delalloc(inode, cur_offset,
1430 cur_offset + num_bytes - 1, end,
1431 locked_page, EXTENT_LOCKED |
1433 EXTENT_CLEAR_DATA_RESV,
1434 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1436 if (!nolock && nocow)
1437 btrfs_end_write_no_snapshoting(root);
1438 cur_offset = extent_end;
1439 if (cur_offset > end)
1442 btrfs_release_path(path);
1444 if (cur_offset <= end && cow_start == (u64)-1) {
1445 cow_start = cur_offset;
1449 if (cow_start != (u64)-1) {
1450 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1451 page_started, nr_written, 1, NULL);
1457 if (ret && cur_offset < end)
1458 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1459 locked_page, EXTENT_LOCKED |
1460 EXTENT_DELALLOC | EXTENT_DEFRAG |
1461 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1463 PAGE_SET_WRITEBACK |
1464 PAGE_END_WRITEBACK);
1465 btrfs_free_path(path);
1469 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1472 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1473 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1477 * @defrag_bytes is a hint value, no spinlock held here,
1478 * if is not zero, it means the file is defragging.
1479 * Force cow if given extent needs to be defragged.
1481 if (BTRFS_I(inode)->defrag_bytes &&
1482 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1483 EXTENT_DEFRAG, 0, NULL))
1490 * extent_io.c call back to do delayed allocation processing
1492 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1493 u64 start, u64 end, int *page_started,
1494 unsigned long *nr_written)
1497 int force_cow = need_force_cow(inode, start, end);
1499 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1500 ret = run_delalloc_nocow(inode, locked_page, start, end,
1501 page_started, 1, nr_written);
1502 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1503 ret = run_delalloc_nocow(inode, locked_page, start, end,
1504 page_started, 0, nr_written);
1505 } else if (!inode_need_compress(inode)) {
1506 ret = cow_file_range(inode, locked_page, start, end, end,
1507 page_started, nr_written, 1, NULL);
1509 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1510 &BTRFS_I(inode)->runtime_flags);
1511 ret = cow_file_range_async(inode, locked_page, start, end,
1512 page_started, nr_written);
1517 static void btrfs_split_extent_hook(struct inode *inode,
1518 struct extent_state *orig, u64 split)
1522 /* not delalloc, ignore it */
1523 if (!(orig->state & EXTENT_DELALLOC))
1526 size = orig->end - orig->start + 1;
1527 if (size > BTRFS_MAX_EXTENT_SIZE) {
1532 * See the explanation in btrfs_merge_extent_hook, the same
1533 * applies here, just in reverse.
1535 new_size = orig->end - split + 1;
1536 num_extents = count_max_extents(new_size);
1537 new_size = split - orig->start;
1538 num_extents += count_max_extents(new_size);
1539 if (count_max_extents(size) >= num_extents)
1543 spin_lock(&BTRFS_I(inode)->lock);
1544 BTRFS_I(inode)->outstanding_extents++;
1545 spin_unlock(&BTRFS_I(inode)->lock);
1549 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1550 * extents so we can keep track of new extents that are just merged onto old
1551 * extents, such as when we are doing sequential writes, so we can properly
1552 * account for the metadata space we'll need.
1554 static void btrfs_merge_extent_hook(struct inode *inode,
1555 struct extent_state *new,
1556 struct extent_state *other)
1558 u64 new_size, old_size;
1561 /* not delalloc, ignore it */
1562 if (!(other->state & EXTENT_DELALLOC))
1565 if (new->start > other->start)
1566 new_size = new->end - other->start + 1;
1568 new_size = other->end - new->start + 1;
1570 /* we're not bigger than the max, unreserve the space and go */
1571 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1572 spin_lock(&BTRFS_I(inode)->lock);
1573 BTRFS_I(inode)->outstanding_extents--;
1574 spin_unlock(&BTRFS_I(inode)->lock);
1579 * We have to add up either side to figure out how many extents were
1580 * accounted for before we merged into one big extent. If the number of
1581 * extents we accounted for is <= the amount we need for the new range
1582 * then we can return, otherwise drop. Think of it like this
1586 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1587 * need 2 outstanding extents, on one side we have 1 and the other side
1588 * we have 1 so they are == and we can return. But in this case
1590 * [MAX_SIZE+4k][MAX_SIZE+4k]
1592 * Each range on their own accounts for 2 extents, but merged together
1593 * they are only 3 extents worth of accounting, so we need to drop in
1596 old_size = other->end - other->start + 1;
1597 num_extents = count_max_extents(old_size);
1598 old_size = new->end - new->start + 1;
1599 num_extents += count_max_extents(old_size);
1600 if (count_max_extents(new_size) >= num_extents)
1603 spin_lock(&BTRFS_I(inode)->lock);
1604 BTRFS_I(inode)->outstanding_extents--;
1605 spin_unlock(&BTRFS_I(inode)->lock);
1608 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1609 struct inode *inode)
1611 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1613 spin_lock(&root->delalloc_lock);
1614 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1615 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1616 &root->delalloc_inodes);
1617 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1618 &BTRFS_I(inode)->runtime_flags);
1619 root->nr_delalloc_inodes++;
1620 if (root->nr_delalloc_inodes == 1) {
1621 spin_lock(&fs_info->delalloc_root_lock);
1622 BUG_ON(!list_empty(&root->delalloc_root));
1623 list_add_tail(&root->delalloc_root,
1624 &fs_info->delalloc_roots);
1625 spin_unlock(&fs_info->delalloc_root_lock);
1628 spin_unlock(&root->delalloc_lock);
1631 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1632 struct inode *inode)
1634 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1636 spin_lock(&root->delalloc_lock);
1637 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1638 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1639 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1640 &BTRFS_I(inode)->runtime_flags);
1641 root->nr_delalloc_inodes--;
1642 if (!root->nr_delalloc_inodes) {
1643 spin_lock(&fs_info->delalloc_root_lock);
1644 BUG_ON(list_empty(&root->delalloc_root));
1645 list_del_init(&root->delalloc_root);
1646 spin_unlock(&fs_info->delalloc_root_lock);
1649 spin_unlock(&root->delalloc_lock);
1653 * extent_io.c set_bit_hook, used to track delayed allocation
1654 * bytes in this file, and to maintain the list of inodes that
1655 * have pending delalloc work to be done.
1657 static void btrfs_set_bit_hook(struct inode *inode,
1658 struct extent_state *state, unsigned *bits)
1661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1663 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1666 * set_bit and clear bit hooks normally require _irqsave/restore
1667 * but in this case, we are only testing for the DELALLOC
1668 * bit, which is only set or cleared with irqs on
1670 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1671 struct btrfs_root *root = BTRFS_I(inode)->root;
1672 u64 len = state->end + 1 - state->start;
1673 bool do_list = !btrfs_is_free_space_inode(inode);
1675 if (*bits & EXTENT_FIRST_DELALLOC) {
1676 *bits &= ~EXTENT_FIRST_DELALLOC;
1678 spin_lock(&BTRFS_I(inode)->lock);
1679 BTRFS_I(inode)->outstanding_extents++;
1680 spin_unlock(&BTRFS_I(inode)->lock);
1683 /* For sanity tests */
1684 if (btrfs_is_testing(fs_info))
1687 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1688 fs_info->delalloc_batch);
1689 spin_lock(&BTRFS_I(inode)->lock);
1690 BTRFS_I(inode)->delalloc_bytes += len;
1691 if (*bits & EXTENT_DEFRAG)
1692 BTRFS_I(inode)->defrag_bytes += len;
1693 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1694 &BTRFS_I(inode)->runtime_flags))
1695 btrfs_add_delalloc_inodes(root, inode);
1696 spin_unlock(&BTRFS_I(inode)->lock);
1701 * extent_io.c clear_bit_hook, see set_bit_hook for why
1703 static void btrfs_clear_bit_hook(struct inode *inode,
1704 struct extent_state *state,
1707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1708 u64 len = state->end + 1 - state->start;
1709 u32 num_extents = count_max_extents(len);
1711 spin_lock(&BTRFS_I(inode)->lock);
1712 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1713 BTRFS_I(inode)->defrag_bytes -= len;
1714 spin_unlock(&BTRFS_I(inode)->lock);
1717 * set_bit and clear bit hooks normally require _irqsave/restore
1718 * but in this case, we are only testing for the DELALLOC
1719 * bit, which is only set or cleared with irqs on
1721 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1722 struct btrfs_root *root = BTRFS_I(inode)->root;
1723 bool do_list = !btrfs_is_free_space_inode(inode);
1725 if (*bits & EXTENT_FIRST_DELALLOC) {
1726 *bits &= ~EXTENT_FIRST_DELALLOC;
1727 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1728 spin_lock(&BTRFS_I(inode)->lock);
1729 BTRFS_I(inode)->outstanding_extents -= num_extents;
1730 spin_unlock(&BTRFS_I(inode)->lock);
1734 * We don't reserve metadata space for space cache inodes so we
1735 * don't need to call dellalloc_release_metadata if there is an
1738 if (*bits & EXTENT_DO_ACCOUNTING &&
1739 root != fs_info->tree_root)
1740 btrfs_delalloc_release_metadata(inode, len);
1742 /* For sanity tests. */
1743 if (btrfs_is_testing(fs_info))
1746 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1747 && do_list && !(state->state & EXTENT_NORESERVE)
1748 && (*bits & (EXTENT_DO_ACCOUNTING |
1749 EXTENT_CLEAR_DATA_RESV)))
1750 btrfs_free_reserved_data_space_noquota(inode,
1753 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1754 fs_info->delalloc_batch);
1755 spin_lock(&BTRFS_I(inode)->lock);
1756 BTRFS_I(inode)->delalloc_bytes -= len;
1757 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1758 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1759 &BTRFS_I(inode)->runtime_flags))
1760 btrfs_del_delalloc_inode(root, inode);
1761 spin_unlock(&BTRFS_I(inode)->lock);
1766 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1767 * we don't create bios that span stripes or chunks
1769 * return 1 if page cannot be merged to bio
1770 * return 0 if page can be merged to bio
1771 * return error otherwise
1773 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1774 size_t size, struct bio *bio,
1775 unsigned long bio_flags)
1777 struct inode *inode = page->mapping->host;
1778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1784 if (bio_flags & EXTENT_BIO_COMPRESSED)
1787 length = bio->bi_iter.bi_size;
1788 map_length = length;
1789 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1793 if (map_length < length + size)
1799 * in order to insert checksums into the metadata in large chunks,
1800 * we wait until bio submission time. All the pages in the bio are
1801 * checksummed and sums are attached onto the ordered extent record.
1803 * At IO completion time the cums attached on the ordered extent record
1804 * are inserted into the btree
1806 static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1807 int mirror_num, unsigned long bio_flags,
1812 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1813 BUG_ON(ret); /* -ENOMEM */
1818 * in order to insert checksums into the metadata in large chunks,
1819 * we wait until bio submission time. All the pages in the bio are
1820 * checksummed and sums are attached onto the ordered extent record.
1822 * At IO completion time the cums attached on the ordered extent record
1823 * are inserted into the btree
1825 static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
1826 int mirror_num, unsigned long bio_flags,
1829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1832 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1834 bio->bi_error = ret;
1841 * extent_io.c submission hook. This does the right thing for csum calculation
1842 * on write, or reading the csums from the tree before a read
1844 static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1845 int mirror_num, unsigned long bio_flags,
1848 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1849 struct btrfs_root *root = BTRFS_I(inode)->root;
1850 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1853 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1855 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1857 if (btrfs_is_free_space_inode(inode))
1858 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1860 if (bio_op(bio) != REQ_OP_WRITE) {
1861 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1865 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1866 ret = btrfs_submit_compressed_read(inode, bio,
1870 } else if (!skip_sum) {
1871 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
1876 } else if (async && !skip_sum) {
1877 /* csum items have already been cloned */
1878 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1880 /* we're doing a write, do the async checksumming */
1881 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1882 bio_flags, bio_offset,
1883 __btrfs_submit_bio_start,
1884 __btrfs_submit_bio_done);
1886 } else if (!skip_sum) {
1887 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1893 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1897 bio->bi_error = ret;
1904 * given a list of ordered sums record them in the inode. This happens
1905 * at IO completion time based on sums calculated at bio submission time.
1907 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1908 struct inode *inode, struct list_head *list)
1910 struct btrfs_ordered_sum *sum;
1912 list_for_each_entry(sum, list, list) {
1913 trans->adding_csums = 1;
1914 btrfs_csum_file_blocks(trans,
1915 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1916 trans->adding_csums = 0;
1921 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1922 struct extent_state **cached_state, int dedupe)
1924 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
1925 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1929 /* see btrfs_writepage_start_hook for details on why this is required */
1930 struct btrfs_writepage_fixup {
1932 struct btrfs_work work;
1935 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1937 struct btrfs_writepage_fixup *fixup;
1938 struct btrfs_ordered_extent *ordered;
1939 struct extent_state *cached_state = NULL;
1941 struct inode *inode;
1946 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1950 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1951 ClearPageChecked(page);
1955 inode = page->mapping->host;
1956 page_start = page_offset(page);
1957 page_end = page_offset(page) + PAGE_SIZE - 1;
1959 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
1962 /* already ordered? We're done */
1963 if (PagePrivate2(page))
1966 ordered = btrfs_lookup_ordered_range(inode, page_start,
1969 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1970 page_end, &cached_state, GFP_NOFS);
1972 btrfs_start_ordered_extent(inode, ordered, 1);
1973 btrfs_put_ordered_extent(ordered);
1977 ret = btrfs_delalloc_reserve_space(inode, page_start,
1980 mapping_set_error(page->mapping, ret);
1981 end_extent_writepage(page, ret, page_start, page_end);
1982 ClearPageChecked(page);
1986 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1988 ClearPageChecked(page);
1989 set_page_dirty(page);
1991 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1992 &cached_state, GFP_NOFS);
2000 * There are a few paths in the higher layers of the kernel that directly
2001 * set the page dirty bit without asking the filesystem if it is a
2002 * good idea. This causes problems because we want to make sure COW
2003 * properly happens and the data=ordered rules are followed.
2005 * In our case any range that doesn't have the ORDERED bit set
2006 * hasn't been properly setup for IO. We kick off an async process
2007 * to fix it up. The async helper will wait for ordered extents, set
2008 * the delalloc bit and make it safe to write the page.
2010 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2012 struct inode *inode = page->mapping->host;
2013 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2014 struct btrfs_writepage_fixup *fixup;
2016 /* this page is properly in the ordered list */
2017 if (TestClearPagePrivate2(page))
2020 if (PageChecked(page))
2023 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2027 SetPageChecked(page);
2029 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2030 btrfs_writepage_fixup_worker, NULL, NULL);
2032 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2036 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2037 struct inode *inode, u64 file_pos,
2038 u64 disk_bytenr, u64 disk_num_bytes,
2039 u64 num_bytes, u64 ram_bytes,
2040 u8 compression, u8 encryption,
2041 u16 other_encoding, int extent_type)
2043 struct btrfs_root *root = BTRFS_I(inode)->root;
2044 struct btrfs_file_extent_item *fi;
2045 struct btrfs_path *path;
2046 struct extent_buffer *leaf;
2047 struct btrfs_key ins;
2048 int extent_inserted = 0;
2051 path = btrfs_alloc_path();
2056 * we may be replacing one extent in the tree with another.
2057 * The new extent is pinned in the extent map, and we don't want
2058 * to drop it from the cache until it is completely in the btree.
2060 * So, tell btrfs_drop_extents to leave this extent in the cache.
2061 * the caller is expected to unpin it and allow it to be merged
2064 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2065 file_pos + num_bytes, NULL, 0,
2066 1, sizeof(*fi), &extent_inserted);
2070 if (!extent_inserted) {
2071 ins.objectid = btrfs_ino(BTRFS_I(inode));
2072 ins.offset = file_pos;
2073 ins.type = BTRFS_EXTENT_DATA_KEY;
2075 path->leave_spinning = 1;
2076 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2081 leaf = path->nodes[0];
2082 fi = btrfs_item_ptr(leaf, path->slots[0],
2083 struct btrfs_file_extent_item);
2084 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2085 btrfs_set_file_extent_type(leaf, fi, extent_type);
2086 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2087 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2088 btrfs_set_file_extent_offset(leaf, fi, 0);
2089 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2090 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2091 btrfs_set_file_extent_compression(leaf, fi, compression);
2092 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2093 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2095 btrfs_mark_buffer_dirty(leaf);
2096 btrfs_release_path(path);
2098 inode_add_bytes(inode, num_bytes);
2100 ins.objectid = disk_bytenr;
2101 ins.offset = disk_num_bytes;
2102 ins.type = BTRFS_EXTENT_ITEM_KEY;
2103 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2104 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
2106 * Release the reserved range from inode dirty range map, as it is
2107 * already moved into delayed_ref_head
2109 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2111 btrfs_free_path(path);
2116 /* snapshot-aware defrag */
2117 struct sa_defrag_extent_backref {
2118 struct rb_node node;
2119 struct old_sa_defrag_extent *old;
2128 struct old_sa_defrag_extent {
2129 struct list_head list;
2130 struct new_sa_defrag_extent *new;
2139 struct new_sa_defrag_extent {
2140 struct rb_root root;
2141 struct list_head head;
2142 struct btrfs_path *path;
2143 struct inode *inode;
2151 static int backref_comp(struct sa_defrag_extent_backref *b1,
2152 struct sa_defrag_extent_backref *b2)
2154 if (b1->root_id < b2->root_id)
2156 else if (b1->root_id > b2->root_id)
2159 if (b1->inum < b2->inum)
2161 else if (b1->inum > b2->inum)
2164 if (b1->file_pos < b2->file_pos)
2166 else if (b1->file_pos > b2->file_pos)
2170 * [------------------------------] ===> (a range of space)
2171 * |<--->| |<---->| =============> (fs/file tree A)
2172 * |<---------------------------->| ===> (fs/file tree B)
2174 * A range of space can refer to two file extents in one tree while
2175 * refer to only one file extent in another tree.
2177 * So we may process a disk offset more than one time(two extents in A)
2178 * and locate at the same extent(one extent in B), then insert two same
2179 * backrefs(both refer to the extent in B).
2184 static void backref_insert(struct rb_root *root,
2185 struct sa_defrag_extent_backref *backref)
2187 struct rb_node **p = &root->rb_node;
2188 struct rb_node *parent = NULL;
2189 struct sa_defrag_extent_backref *entry;
2194 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2196 ret = backref_comp(backref, entry);
2200 p = &(*p)->rb_right;
2203 rb_link_node(&backref->node, parent, p);
2204 rb_insert_color(&backref->node, root);
2208 * Note the backref might has changed, and in this case we just return 0.
2210 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2213 struct btrfs_file_extent_item *extent;
2214 struct old_sa_defrag_extent *old = ctx;
2215 struct new_sa_defrag_extent *new = old->new;
2216 struct btrfs_path *path = new->path;
2217 struct btrfs_key key;
2218 struct btrfs_root *root;
2219 struct sa_defrag_extent_backref *backref;
2220 struct extent_buffer *leaf;
2221 struct inode *inode = new->inode;
2222 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2228 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2229 inum == btrfs_ino(BTRFS_I(inode)))
2232 key.objectid = root_id;
2233 key.type = BTRFS_ROOT_ITEM_KEY;
2234 key.offset = (u64)-1;
2236 root = btrfs_read_fs_root_no_name(fs_info, &key);
2238 if (PTR_ERR(root) == -ENOENT)
2241 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2242 inum, offset, root_id);
2243 return PTR_ERR(root);
2246 key.objectid = inum;
2247 key.type = BTRFS_EXTENT_DATA_KEY;
2248 if (offset > (u64)-1 << 32)
2251 key.offset = offset;
2253 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2254 if (WARN_ON(ret < 0))
2261 leaf = path->nodes[0];
2262 slot = path->slots[0];
2264 if (slot >= btrfs_header_nritems(leaf)) {
2265 ret = btrfs_next_leaf(root, path);
2268 } else if (ret > 0) {
2277 btrfs_item_key_to_cpu(leaf, &key, slot);
2279 if (key.objectid > inum)
2282 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2285 extent = btrfs_item_ptr(leaf, slot,
2286 struct btrfs_file_extent_item);
2288 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2292 * 'offset' refers to the exact key.offset,
2293 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2294 * (key.offset - extent_offset).
2296 if (key.offset != offset)
2299 extent_offset = btrfs_file_extent_offset(leaf, extent);
2300 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2302 if (extent_offset >= old->extent_offset + old->offset +
2303 old->len || extent_offset + num_bytes <=
2304 old->extent_offset + old->offset)
2309 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2315 backref->root_id = root_id;
2316 backref->inum = inum;
2317 backref->file_pos = offset;
2318 backref->num_bytes = num_bytes;
2319 backref->extent_offset = extent_offset;
2320 backref->generation = btrfs_file_extent_generation(leaf, extent);
2322 backref_insert(&new->root, backref);
2325 btrfs_release_path(path);
2330 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2331 struct new_sa_defrag_extent *new)
2333 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2334 struct old_sa_defrag_extent *old, *tmp;
2339 list_for_each_entry_safe(old, tmp, &new->head, list) {
2340 ret = iterate_inodes_from_logical(old->bytenr +
2341 old->extent_offset, fs_info,
2342 path, record_one_backref,
2344 if (ret < 0 && ret != -ENOENT)
2347 /* no backref to be processed for this extent */
2349 list_del(&old->list);
2354 if (list_empty(&new->head))
2360 static int relink_is_mergable(struct extent_buffer *leaf,
2361 struct btrfs_file_extent_item *fi,
2362 struct new_sa_defrag_extent *new)
2364 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2367 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2370 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2373 if (btrfs_file_extent_encryption(leaf, fi) ||
2374 btrfs_file_extent_other_encoding(leaf, fi))
2381 * Note the backref might has changed, and in this case we just return 0.
2383 static noinline int relink_extent_backref(struct btrfs_path *path,
2384 struct sa_defrag_extent_backref *prev,
2385 struct sa_defrag_extent_backref *backref)
2387 struct btrfs_file_extent_item *extent;
2388 struct btrfs_file_extent_item *item;
2389 struct btrfs_ordered_extent *ordered;
2390 struct btrfs_trans_handle *trans;
2391 struct btrfs_root *root;
2392 struct btrfs_key key;
2393 struct extent_buffer *leaf;
2394 struct old_sa_defrag_extent *old = backref->old;
2395 struct new_sa_defrag_extent *new = old->new;
2396 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2397 struct inode *inode;
2398 struct extent_state *cached = NULL;
2407 if (prev && prev->root_id == backref->root_id &&
2408 prev->inum == backref->inum &&
2409 prev->file_pos + prev->num_bytes == backref->file_pos)
2412 /* step 1: get root */
2413 key.objectid = backref->root_id;
2414 key.type = BTRFS_ROOT_ITEM_KEY;
2415 key.offset = (u64)-1;
2417 index = srcu_read_lock(&fs_info->subvol_srcu);
2419 root = btrfs_read_fs_root_no_name(fs_info, &key);
2421 srcu_read_unlock(&fs_info->subvol_srcu, index);
2422 if (PTR_ERR(root) == -ENOENT)
2424 return PTR_ERR(root);
2427 if (btrfs_root_readonly(root)) {
2428 srcu_read_unlock(&fs_info->subvol_srcu, index);
2432 /* step 2: get inode */
2433 key.objectid = backref->inum;
2434 key.type = BTRFS_INODE_ITEM_KEY;
2437 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2438 if (IS_ERR(inode)) {
2439 srcu_read_unlock(&fs_info->subvol_srcu, index);
2443 srcu_read_unlock(&fs_info->subvol_srcu, index);
2445 /* step 3: relink backref */
2446 lock_start = backref->file_pos;
2447 lock_end = backref->file_pos + backref->num_bytes - 1;
2448 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2451 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2453 btrfs_put_ordered_extent(ordered);
2457 trans = btrfs_join_transaction(root);
2458 if (IS_ERR(trans)) {
2459 ret = PTR_ERR(trans);
2463 key.objectid = backref->inum;
2464 key.type = BTRFS_EXTENT_DATA_KEY;
2465 key.offset = backref->file_pos;
2467 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2470 } else if (ret > 0) {
2475 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2476 struct btrfs_file_extent_item);
2478 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2479 backref->generation)
2482 btrfs_release_path(path);
2484 start = backref->file_pos;
2485 if (backref->extent_offset < old->extent_offset + old->offset)
2486 start += old->extent_offset + old->offset -
2487 backref->extent_offset;
2489 len = min(backref->extent_offset + backref->num_bytes,
2490 old->extent_offset + old->offset + old->len);
2491 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2493 ret = btrfs_drop_extents(trans, root, inode, start,
2498 key.objectid = btrfs_ino(BTRFS_I(inode));
2499 key.type = BTRFS_EXTENT_DATA_KEY;
2502 path->leave_spinning = 1;
2504 struct btrfs_file_extent_item *fi;
2506 struct btrfs_key found_key;
2508 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2513 leaf = path->nodes[0];
2514 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2516 fi = btrfs_item_ptr(leaf, path->slots[0],
2517 struct btrfs_file_extent_item);
2518 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2520 if (extent_len + found_key.offset == start &&
2521 relink_is_mergable(leaf, fi, new)) {
2522 btrfs_set_file_extent_num_bytes(leaf, fi,
2524 btrfs_mark_buffer_dirty(leaf);
2525 inode_add_bytes(inode, len);
2531 btrfs_release_path(path);
2536 ret = btrfs_insert_empty_item(trans, root, path, &key,
2539 btrfs_abort_transaction(trans, ret);
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0],
2545 struct btrfs_file_extent_item);
2546 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2547 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2548 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2549 btrfs_set_file_extent_num_bytes(leaf, item, len);
2550 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2551 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2552 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2553 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2554 btrfs_set_file_extent_encryption(leaf, item, 0);
2555 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2557 btrfs_mark_buffer_dirty(leaf);
2558 inode_add_bytes(inode, len);
2559 btrfs_release_path(path);
2561 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
2563 backref->root_id, backref->inum,
2564 new->file_pos); /* start - extent_offset */
2566 btrfs_abort_transaction(trans, ret);
2572 btrfs_release_path(path);
2573 path->leave_spinning = 0;
2574 btrfs_end_transaction(trans);
2576 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2582 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2584 struct old_sa_defrag_extent *old, *tmp;
2589 list_for_each_entry_safe(old, tmp, &new->head, list) {
2595 static void relink_file_extents(struct new_sa_defrag_extent *new)
2597 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2598 struct btrfs_path *path;
2599 struct sa_defrag_extent_backref *backref;
2600 struct sa_defrag_extent_backref *prev = NULL;
2601 struct inode *inode;
2602 struct btrfs_root *root;
2603 struct rb_node *node;
2607 root = BTRFS_I(inode)->root;
2609 path = btrfs_alloc_path();
2613 if (!record_extent_backrefs(path, new)) {
2614 btrfs_free_path(path);
2617 btrfs_release_path(path);
2620 node = rb_first(&new->root);
2623 rb_erase(node, &new->root);
2625 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2627 ret = relink_extent_backref(path, prev, backref);
2640 btrfs_free_path(path);
2642 free_sa_defrag_extent(new);
2644 atomic_dec(&fs_info->defrag_running);
2645 wake_up(&fs_info->transaction_wait);
2648 static struct new_sa_defrag_extent *
2649 record_old_file_extents(struct inode *inode,
2650 struct btrfs_ordered_extent *ordered)
2652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2653 struct btrfs_root *root = BTRFS_I(inode)->root;
2654 struct btrfs_path *path;
2655 struct btrfs_key key;
2656 struct old_sa_defrag_extent *old;
2657 struct new_sa_defrag_extent *new;
2660 new = kmalloc(sizeof(*new), GFP_NOFS);
2665 new->file_pos = ordered->file_offset;
2666 new->len = ordered->len;
2667 new->bytenr = ordered->start;
2668 new->disk_len = ordered->disk_len;
2669 new->compress_type = ordered->compress_type;
2670 new->root = RB_ROOT;
2671 INIT_LIST_HEAD(&new->head);
2673 path = btrfs_alloc_path();
2677 key.objectid = btrfs_ino(BTRFS_I(inode));
2678 key.type = BTRFS_EXTENT_DATA_KEY;
2679 key.offset = new->file_pos;
2681 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2684 if (ret > 0 && path->slots[0] > 0)
2687 /* find out all the old extents for the file range */
2689 struct btrfs_file_extent_item *extent;
2690 struct extent_buffer *l;
2699 slot = path->slots[0];
2701 if (slot >= btrfs_header_nritems(l)) {
2702 ret = btrfs_next_leaf(root, path);
2710 btrfs_item_key_to_cpu(l, &key, slot);
2712 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
2714 if (key.type != BTRFS_EXTENT_DATA_KEY)
2716 if (key.offset >= new->file_pos + new->len)
2719 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2721 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2722 if (key.offset + num_bytes < new->file_pos)
2725 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2729 extent_offset = btrfs_file_extent_offset(l, extent);
2731 old = kmalloc(sizeof(*old), GFP_NOFS);
2735 offset = max(new->file_pos, key.offset);
2736 end = min(new->file_pos + new->len, key.offset + num_bytes);
2738 old->bytenr = disk_bytenr;
2739 old->extent_offset = extent_offset;
2740 old->offset = offset - key.offset;
2741 old->len = end - offset;
2744 list_add_tail(&old->list, &new->head);
2750 btrfs_free_path(path);
2751 atomic_inc(&fs_info->defrag_running);
2756 btrfs_free_path(path);
2758 free_sa_defrag_extent(new);
2762 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2765 struct btrfs_block_group_cache *cache;
2767 cache = btrfs_lookup_block_group(fs_info, start);
2770 spin_lock(&cache->lock);
2771 cache->delalloc_bytes -= len;
2772 spin_unlock(&cache->lock);
2774 btrfs_put_block_group(cache);
2777 /* as ordered data IO finishes, this gets called so we can finish
2778 * an ordered extent if the range of bytes in the file it covers are
2781 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2783 struct inode *inode = ordered_extent->inode;
2784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2785 struct btrfs_root *root = BTRFS_I(inode)->root;
2786 struct btrfs_trans_handle *trans = NULL;
2787 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2788 struct extent_state *cached_state = NULL;
2789 struct new_sa_defrag_extent *new = NULL;
2790 int compress_type = 0;
2792 u64 logical_len = ordered_extent->len;
2794 bool truncated = false;
2796 nolock = btrfs_is_free_space_inode(inode);
2798 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2803 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2804 ordered_extent->file_offset +
2805 ordered_extent->len - 1);
2807 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2809 logical_len = ordered_extent->truncated_len;
2810 /* Truncated the entire extent, don't bother adding */
2815 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2816 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2819 * For mwrite(mmap + memset to write) case, we still reserve
2820 * space for NOCOW range.
2821 * As NOCOW won't cause a new delayed ref, just free the space
2823 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2824 ordered_extent->len);
2825 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2827 trans = btrfs_join_transaction_nolock(root);
2829 trans = btrfs_join_transaction(root);
2830 if (IS_ERR(trans)) {
2831 ret = PTR_ERR(trans);
2835 trans->block_rsv = &fs_info->delalloc_block_rsv;
2836 ret = btrfs_update_inode_fallback(trans, root, inode);
2837 if (ret) /* -ENOMEM or corruption */
2838 btrfs_abort_transaction(trans, ret);
2842 lock_extent_bits(io_tree, ordered_extent->file_offset,
2843 ordered_extent->file_offset + ordered_extent->len - 1,
2846 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2847 ordered_extent->file_offset + ordered_extent->len - 1,
2848 EXTENT_DEFRAG, 1, cached_state);
2850 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2851 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2852 /* the inode is shared */
2853 new = record_old_file_extents(inode, ordered_extent);
2855 clear_extent_bit(io_tree, ordered_extent->file_offset,
2856 ordered_extent->file_offset + ordered_extent->len - 1,
2857 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2861 trans = btrfs_join_transaction_nolock(root);
2863 trans = btrfs_join_transaction(root);
2864 if (IS_ERR(trans)) {
2865 ret = PTR_ERR(trans);
2870 trans->block_rsv = &fs_info->delalloc_block_rsv;
2872 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2873 compress_type = ordered_extent->compress_type;
2874 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2875 BUG_ON(compress_type);
2876 ret = btrfs_mark_extent_written(trans, inode,
2877 ordered_extent->file_offset,
2878 ordered_extent->file_offset +
2881 BUG_ON(root == fs_info->tree_root);
2882 ret = insert_reserved_file_extent(trans, inode,
2883 ordered_extent->file_offset,
2884 ordered_extent->start,
2885 ordered_extent->disk_len,
2886 logical_len, logical_len,
2887 compress_type, 0, 0,
2888 BTRFS_FILE_EXTENT_REG);
2890 btrfs_release_delalloc_bytes(fs_info,
2891 ordered_extent->start,
2892 ordered_extent->disk_len);
2894 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2895 ordered_extent->file_offset, ordered_extent->len,
2898 btrfs_abort_transaction(trans, ret);
2902 add_pending_csums(trans, inode, &ordered_extent->list);
2904 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 ret = btrfs_update_inode_fallback(trans, root, inode);
2906 if (ret) { /* -ENOMEM or corruption */
2907 btrfs_abort_transaction(trans, ret);
2912 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 ordered_extent->file_offset +
2914 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2916 if (root != fs_info->tree_root)
2917 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2919 btrfs_end_transaction(trans);
2921 if (ret || truncated) {
2925 start = ordered_extent->file_offset + logical_len;
2927 start = ordered_extent->file_offset;
2928 end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2931 /* Drop the cache for the part of the extent we didn't write. */
2932 btrfs_drop_extent_cache(inode, start, end, 0);
2935 * If the ordered extent had an IOERR or something else went
2936 * wrong we need to return the space for this ordered extent
2937 * back to the allocator. We only free the extent in the
2938 * truncated case if we didn't write out the extent at all.
2940 if ((ret || !logical_len) &&
2941 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2942 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2943 btrfs_free_reserved_extent(fs_info,
2944 ordered_extent->start,
2945 ordered_extent->disk_len, 1);
2950 * This needs to be done to make sure anybody waiting knows we are done
2951 * updating everything for this ordered extent.
2953 btrfs_remove_ordered_extent(inode, ordered_extent);
2955 /* for snapshot-aware defrag */
2958 free_sa_defrag_extent(new);
2959 atomic_dec(&fs_info->defrag_running);
2961 relink_file_extents(new);
2966 btrfs_put_ordered_extent(ordered_extent);
2967 /* once for the tree */
2968 btrfs_put_ordered_extent(ordered_extent);
2973 static void finish_ordered_fn(struct btrfs_work *work)
2975 struct btrfs_ordered_extent *ordered_extent;
2976 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2977 btrfs_finish_ordered_io(ordered_extent);
2980 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2981 struct extent_state *state, int uptodate)
2983 struct inode *inode = page->mapping->host;
2984 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2985 struct btrfs_ordered_extent *ordered_extent = NULL;
2986 struct btrfs_workqueue *wq;
2987 btrfs_work_func_t func;
2989 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2991 ClearPagePrivate2(page);
2992 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2993 end - start + 1, uptodate))
2996 if (btrfs_is_free_space_inode(inode)) {
2997 wq = fs_info->endio_freespace_worker;
2998 func = btrfs_freespace_write_helper;
3000 wq = fs_info->endio_write_workers;
3001 func = btrfs_endio_write_helper;
3004 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3006 btrfs_queue_work(wq, &ordered_extent->work);
3011 static int __readpage_endio_check(struct inode *inode,
3012 struct btrfs_io_bio *io_bio,
3013 int icsum, struct page *page,
3014 int pgoff, u64 start, size_t len)
3020 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3022 kaddr = kmap_atomic(page);
3023 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3024 btrfs_csum_final(csum, (u8 *)&csum);
3025 if (csum != csum_expected)
3028 kunmap_atomic(kaddr);
3031 btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3032 io_bio->mirror_num);
3033 memset(kaddr + pgoff, 1, len);
3034 flush_dcache_page(page);
3035 kunmap_atomic(kaddr);
3036 if (csum_expected == 0)
3042 * when reads are done, we need to check csums to verify the data is correct
3043 * if there's a match, we allow the bio to finish. If not, the code in
3044 * extent_io.c will try to find good copies for us.
3046 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3047 u64 phy_offset, struct page *page,
3048 u64 start, u64 end, int mirror)
3050 size_t offset = start - page_offset(page);
3051 struct inode *inode = page->mapping->host;
3052 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3053 struct btrfs_root *root = BTRFS_I(inode)->root;
3055 if (PageChecked(page)) {
3056 ClearPageChecked(page);
3060 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3063 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3064 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
3065 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
3069 phy_offset >>= inode->i_sb->s_blocksize_bits;
3070 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3071 start, (size_t)(end - start + 1));
3074 void btrfs_add_delayed_iput(struct inode *inode)
3076 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3077 struct btrfs_inode *binode = BTRFS_I(inode);
3079 if (atomic_add_unless(&inode->i_count, -1, 1))
3082 spin_lock(&fs_info->delayed_iput_lock);
3083 if (binode->delayed_iput_count == 0) {
3084 ASSERT(list_empty(&binode->delayed_iput));
3085 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3087 binode->delayed_iput_count++;
3089 spin_unlock(&fs_info->delayed_iput_lock);
3092 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3095 spin_lock(&fs_info->delayed_iput_lock);
3096 while (!list_empty(&fs_info->delayed_iputs)) {
3097 struct btrfs_inode *inode;
3099 inode = list_first_entry(&fs_info->delayed_iputs,
3100 struct btrfs_inode, delayed_iput);
3101 if (inode->delayed_iput_count) {
3102 inode->delayed_iput_count--;
3103 list_move_tail(&inode->delayed_iput,
3104 &fs_info->delayed_iputs);
3106 list_del_init(&inode->delayed_iput);
3108 spin_unlock(&fs_info->delayed_iput_lock);
3109 iput(&inode->vfs_inode);
3110 spin_lock(&fs_info->delayed_iput_lock);
3112 spin_unlock(&fs_info->delayed_iput_lock);
3116 * This is called in transaction commit time. If there are no orphan
3117 * files in the subvolume, it removes orphan item and frees block_rsv
3120 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root)
3123 struct btrfs_fs_info *fs_info = root->fs_info;
3124 struct btrfs_block_rsv *block_rsv;
3127 if (atomic_read(&root->orphan_inodes) ||
3128 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3131 spin_lock(&root->orphan_lock);
3132 if (atomic_read(&root->orphan_inodes)) {
3133 spin_unlock(&root->orphan_lock);
3137 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3138 spin_unlock(&root->orphan_lock);
3142 block_rsv = root->orphan_block_rsv;
3143 root->orphan_block_rsv = NULL;
3144 spin_unlock(&root->orphan_lock);
3146 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
3147 btrfs_root_refs(&root->root_item) > 0) {
3148 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
3149 root->root_key.objectid);
3151 btrfs_abort_transaction(trans, ret);
3153 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3158 WARN_ON(block_rsv->size > 0);
3159 btrfs_free_block_rsv(fs_info, block_rsv);
3164 * This creates an orphan entry for the given inode in case something goes
3165 * wrong in the middle of an unlink/truncate.
3167 * NOTE: caller of this function should reserve 5 units of metadata for
3170 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3173 struct btrfs_root *root = BTRFS_I(inode)->root;
3174 struct btrfs_block_rsv *block_rsv = NULL;
3179 if (!root->orphan_block_rsv) {
3180 block_rsv = btrfs_alloc_block_rsv(fs_info,
3181 BTRFS_BLOCK_RSV_TEMP);
3186 spin_lock(&root->orphan_lock);
3187 if (!root->orphan_block_rsv) {
3188 root->orphan_block_rsv = block_rsv;
3189 } else if (block_rsv) {
3190 btrfs_free_block_rsv(fs_info, block_rsv);
3194 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3195 &BTRFS_I(inode)->runtime_flags)) {
3198 * For proper ENOSPC handling, we should do orphan
3199 * cleanup when mounting. But this introduces backward
3200 * compatibility issue.
3202 if (!xchg(&root->orphan_item_inserted, 1))
3208 atomic_inc(&root->orphan_inodes);
3211 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3212 &BTRFS_I(inode)->runtime_flags))
3214 spin_unlock(&root->orphan_lock);
3216 /* grab metadata reservation from transaction handle */
3218 ret = btrfs_orphan_reserve_metadata(trans, inode);
3221 atomic_dec(&root->orphan_inodes);
3222 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3223 &BTRFS_I(inode)->runtime_flags);
3225 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 &BTRFS_I(inode)->runtime_flags);
3231 /* insert an orphan item to track this unlinked/truncated file */
3233 ret = btrfs_insert_orphan_item(trans, root,
3234 btrfs_ino(BTRFS_I(inode)));
3236 atomic_dec(&root->orphan_inodes);
3238 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3239 &BTRFS_I(inode)->runtime_flags);
3240 btrfs_orphan_release_metadata(inode);
3242 if (ret != -EEXIST) {
3243 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3244 &BTRFS_I(inode)->runtime_flags);
3245 btrfs_abort_transaction(trans, ret);
3252 /* insert an orphan item to track subvolume contains orphan files */
3254 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
3255 root->root_key.objectid);
3256 if (ret && ret != -EEXIST) {
3257 btrfs_abort_transaction(trans, ret);
3265 * We have done the truncate/delete so we can go ahead and remove the orphan
3266 * item for this particular inode.
3268 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3269 struct inode *inode)
3271 struct btrfs_root *root = BTRFS_I(inode)->root;
3272 int delete_item = 0;
3273 int release_rsv = 0;
3276 spin_lock(&root->orphan_lock);
3277 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3278 &BTRFS_I(inode)->runtime_flags))
3281 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 &BTRFS_I(inode)->runtime_flags))
3284 spin_unlock(&root->orphan_lock);
3287 atomic_dec(&root->orphan_inodes);
3289 ret = btrfs_del_orphan_item(trans, root,
3290 btrfs_ino(BTRFS_I(inode)));
3294 btrfs_orphan_release_metadata(inode);
3300 * this cleans up any orphans that may be left on the list from the last use
3303 int btrfs_orphan_cleanup(struct btrfs_root *root)
3305 struct btrfs_fs_info *fs_info = root->fs_info;
3306 struct btrfs_path *path;
3307 struct extent_buffer *leaf;
3308 struct btrfs_key key, found_key;
3309 struct btrfs_trans_handle *trans;
3310 struct inode *inode;
3311 u64 last_objectid = 0;
3312 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3314 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3317 path = btrfs_alloc_path();
3322 path->reada = READA_BACK;
3324 key.objectid = BTRFS_ORPHAN_OBJECTID;
3325 key.type = BTRFS_ORPHAN_ITEM_KEY;
3326 key.offset = (u64)-1;
3329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3334 * if ret == 0 means we found what we were searching for, which
3335 * is weird, but possible, so only screw with path if we didn't
3336 * find the key and see if we have stuff that matches
3340 if (path->slots[0] == 0)
3345 /* pull out the item */
3346 leaf = path->nodes[0];
3347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3349 /* make sure the item matches what we want */
3350 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3352 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3355 /* release the path since we're done with it */
3356 btrfs_release_path(path);
3359 * this is where we are basically btrfs_lookup, without the
3360 * crossing root thing. we store the inode number in the
3361 * offset of the orphan item.
3364 if (found_key.offset == last_objectid) {
3366 "Error removing orphan entry, stopping orphan cleanup");
3371 last_objectid = found_key.offset;
3373 found_key.objectid = found_key.offset;
3374 found_key.type = BTRFS_INODE_ITEM_KEY;
3375 found_key.offset = 0;
3376 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
3377 ret = PTR_ERR_OR_ZERO(inode);
3378 if (ret && ret != -ENOENT)
3381 if (ret == -ENOENT && root == fs_info->tree_root) {
3382 struct btrfs_root *dead_root;
3383 struct btrfs_fs_info *fs_info = root->fs_info;
3384 int is_dead_root = 0;
3387 * this is an orphan in the tree root. Currently these
3388 * could come from 2 sources:
3389 * a) a snapshot deletion in progress
3390 * b) a free space cache inode
3391 * We need to distinguish those two, as the snapshot
3392 * orphan must not get deleted.
3393 * find_dead_roots already ran before us, so if this
3394 * is a snapshot deletion, we should find the root
3395 * in the dead_roots list
3397 spin_lock(&fs_info->trans_lock);
3398 list_for_each_entry(dead_root, &fs_info->dead_roots,
3400 if (dead_root->root_key.objectid ==
3401 found_key.objectid) {
3406 spin_unlock(&fs_info->trans_lock);
3408 /* prevent this orphan from being found again */
3409 key.offset = found_key.objectid - 1;
3414 * Inode is already gone but the orphan item is still there,
3415 * kill the orphan item.
3417 if (ret == -ENOENT) {
3418 trans = btrfs_start_transaction(root, 1);
3419 if (IS_ERR(trans)) {
3420 ret = PTR_ERR(trans);
3423 btrfs_debug(fs_info, "auto deleting %Lu",
3424 found_key.objectid);
3425 ret = btrfs_del_orphan_item(trans, root,
3426 found_key.objectid);
3427 btrfs_end_transaction(trans);
3434 * add this inode to the orphan list so btrfs_orphan_del does
3435 * the proper thing when we hit it
3437 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3438 &BTRFS_I(inode)->runtime_flags);
3439 atomic_inc(&root->orphan_inodes);
3441 /* if we have links, this was a truncate, lets do that */
3442 if (inode->i_nlink) {
3443 if (WARN_ON(!S_ISREG(inode->i_mode))) {
3449 /* 1 for the orphan item deletion. */
3450 trans = btrfs_start_transaction(root, 1);
3451 if (IS_ERR(trans)) {
3453 ret = PTR_ERR(trans);
3456 ret = btrfs_orphan_add(trans, inode);
3457 btrfs_end_transaction(trans);
3463 ret = btrfs_truncate(inode);
3465 btrfs_orphan_del(NULL, inode);
3470 /* this will do delete_inode and everything for us */
3475 /* release the path since we're done with it */
3476 btrfs_release_path(path);
3478 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3480 if (root->orphan_block_rsv)
3481 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
3484 if (root->orphan_block_rsv ||
3485 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3486 trans = btrfs_join_transaction(root);
3488 btrfs_end_transaction(trans);
3492 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3494 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
3498 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3499 btrfs_free_path(path);
3504 * very simple check to peek ahead in the leaf looking for xattrs. If we
3505 * don't find any xattrs, we know there can't be any acls.
3507 * slot is the slot the inode is in, objectid is the objectid of the inode
3509 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3510 int slot, u64 objectid,
3511 int *first_xattr_slot)
3513 u32 nritems = btrfs_header_nritems(leaf);
3514 struct btrfs_key found_key;
3515 static u64 xattr_access = 0;
3516 static u64 xattr_default = 0;
3519 if (!xattr_access) {
3520 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3521 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3522 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3523 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3527 *first_xattr_slot = -1;
3528 while (slot < nritems) {
3529 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3531 /* we found a different objectid, there must not be acls */
3532 if (found_key.objectid != objectid)
3535 /* we found an xattr, assume we've got an acl */
3536 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3537 if (*first_xattr_slot == -1)
3538 *first_xattr_slot = slot;
3539 if (found_key.offset == xattr_access ||
3540 found_key.offset == xattr_default)
3545 * we found a key greater than an xattr key, there can't
3546 * be any acls later on
3548 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3555 * it goes inode, inode backrefs, xattrs, extents,
3556 * so if there are a ton of hard links to an inode there can
3557 * be a lot of backrefs. Don't waste time searching too hard,
3558 * this is just an optimization
3563 /* we hit the end of the leaf before we found an xattr or
3564 * something larger than an xattr. We have to assume the inode
3567 if (*first_xattr_slot == -1)
3568 *first_xattr_slot = slot;
3573 * read an inode from the btree into the in-memory inode
3575 static int btrfs_read_locked_inode(struct inode *inode)
3577 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3578 struct btrfs_path *path;
3579 struct extent_buffer *leaf;
3580 struct btrfs_inode_item *inode_item;
3581 struct btrfs_root *root = BTRFS_I(inode)->root;
3582 struct btrfs_key location;
3587 bool filled = false;
3588 int first_xattr_slot;
3590 ret = btrfs_fill_inode(inode, &rdev);
3594 path = btrfs_alloc_path();
3600 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3602 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3609 leaf = path->nodes[0];
3614 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3615 struct btrfs_inode_item);
3616 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3617 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3618 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3619 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3620 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3622 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3623 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3625 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3626 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3628 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3629 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3631 BTRFS_I(inode)->i_otime.tv_sec =
3632 btrfs_timespec_sec(leaf, &inode_item->otime);
3633 BTRFS_I(inode)->i_otime.tv_nsec =
3634 btrfs_timespec_nsec(leaf, &inode_item->otime);
3636 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3637 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3638 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3640 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3641 inode->i_generation = BTRFS_I(inode)->generation;
3643 rdev = btrfs_inode_rdev(leaf, inode_item);
3645 BTRFS_I(inode)->index_cnt = (u64)-1;
3646 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3650 * If we were modified in the current generation and evicted from memory
3651 * and then re-read we need to do a full sync since we don't have any
3652 * idea about which extents were modified before we were evicted from
3655 * This is required for both inode re-read from disk and delayed inode
3656 * in delayed_nodes_tree.
3658 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3659 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3660 &BTRFS_I(inode)->runtime_flags);
3663 * We don't persist the id of the transaction where an unlink operation
3664 * against the inode was last made. So here we assume the inode might
3665 * have been evicted, and therefore the exact value of last_unlink_trans
3666 * lost, and set it to last_trans to avoid metadata inconsistencies
3667 * between the inode and its parent if the inode is fsync'ed and the log
3668 * replayed. For example, in the scenario:
3671 * ln mydir/foo mydir/bar
3674 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3675 * xfs_io -c fsync mydir/foo
3677 * mount fs, triggers fsync log replay
3679 * We must make sure that when we fsync our inode foo we also log its
3680 * parent inode, otherwise after log replay the parent still has the
3681 * dentry with the "bar" name but our inode foo has a link count of 1
3682 * and doesn't have an inode ref with the name "bar" anymore.
3684 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3685 * but it guarantees correctness at the expense of occasional full
3686 * transaction commits on fsync if our inode is a directory, or if our
3687 * inode is not a directory, logging its parent unnecessarily.
3689 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3692 if (inode->i_nlink != 1 ||
3693 path->slots[0] >= btrfs_header_nritems(leaf))
3696 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3697 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3700 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3701 if (location.type == BTRFS_INODE_REF_KEY) {
3702 struct btrfs_inode_ref *ref;
3704 ref = (struct btrfs_inode_ref *)ptr;
3705 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3706 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3707 struct btrfs_inode_extref *extref;
3709 extref = (struct btrfs_inode_extref *)ptr;
3710 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3715 * try to precache a NULL acl entry for files that don't have
3716 * any xattrs or acls
3718 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3719 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3720 if (first_xattr_slot != -1) {
3721 path->slots[0] = first_xattr_slot;
3722 ret = btrfs_load_inode_props(inode, path);
3725 "error loading props for ino %llu (root %llu): %d",
3726 btrfs_ino(BTRFS_I(inode)),
3727 root->root_key.objectid, ret);
3729 btrfs_free_path(path);
3732 cache_no_acl(inode);
3734 switch (inode->i_mode & S_IFMT) {
3736 inode->i_mapping->a_ops = &btrfs_aops;
3737 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3738 inode->i_fop = &btrfs_file_operations;
3739 inode->i_op = &btrfs_file_inode_operations;
3742 inode->i_fop = &btrfs_dir_file_operations;
3743 inode->i_op = &btrfs_dir_inode_operations;
3746 inode->i_op = &btrfs_symlink_inode_operations;
3747 inode_nohighmem(inode);
3748 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3751 inode->i_op = &btrfs_special_inode_operations;
3752 init_special_inode(inode, inode->i_mode, rdev);
3756 btrfs_update_iflags(inode);
3760 btrfs_free_path(path);
3761 make_bad_inode(inode);
3766 * given a leaf and an inode, copy the inode fields into the leaf
3768 static void fill_inode_item(struct btrfs_trans_handle *trans,
3769 struct extent_buffer *leaf,
3770 struct btrfs_inode_item *item,
3771 struct inode *inode)
3773 struct btrfs_map_token token;
3775 btrfs_init_map_token(&token);
3777 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3778 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3779 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3781 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3782 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3784 btrfs_set_token_timespec_sec(leaf, &item->atime,
3785 inode->i_atime.tv_sec, &token);
3786 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3787 inode->i_atime.tv_nsec, &token);
3789 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3790 inode->i_mtime.tv_sec, &token);
3791 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3792 inode->i_mtime.tv_nsec, &token);
3794 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3795 inode->i_ctime.tv_sec, &token);
3796 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3797 inode->i_ctime.tv_nsec, &token);
3799 btrfs_set_token_timespec_sec(leaf, &item->otime,
3800 BTRFS_I(inode)->i_otime.tv_sec, &token);
3801 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3802 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3804 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3806 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3808 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3809 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3810 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3811 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3812 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3816 * copy everything in the in-memory inode into the btree.
3818 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3819 struct btrfs_root *root, struct inode *inode)
3821 struct btrfs_inode_item *inode_item;
3822 struct btrfs_path *path;
3823 struct extent_buffer *leaf;
3826 path = btrfs_alloc_path();
3830 path->leave_spinning = 1;
3831 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3839 leaf = path->nodes[0];
3840 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3841 struct btrfs_inode_item);
3843 fill_inode_item(trans, leaf, inode_item, inode);
3844 btrfs_mark_buffer_dirty(leaf);
3845 btrfs_set_inode_last_trans(trans, inode);
3848 btrfs_free_path(path);
3853 * copy everything in the in-memory inode into the btree.
3855 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3856 struct btrfs_root *root, struct inode *inode)
3858 struct btrfs_fs_info *fs_info = root->fs_info;
3862 * If the inode is a free space inode, we can deadlock during commit
3863 * if we put it into the delayed code.
3865 * The data relocation inode should also be directly updated
3868 if (!btrfs_is_free_space_inode(inode)
3869 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3870 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
3871 btrfs_update_root_times(trans, root);
3873 ret = btrfs_delayed_update_inode(trans, root, inode);
3875 btrfs_set_inode_last_trans(trans, inode);
3879 return btrfs_update_inode_item(trans, root, inode);
3882 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root,
3884 struct inode *inode)
3888 ret = btrfs_update_inode(trans, root, inode);
3890 return btrfs_update_inode_item(trans, root, inode);
3895 * unlink helper that gets used here in inode.c and in the tree logging
3896 * recovery code. It remove a link in a directory with a given name, and
3897 * also drops the back refs in the inode to the directory
3899 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3900 struct btrfs_root *root,
3901 struct btrfs_inode *dir,
3902 struct btrfs_inode *inode,
3903 const char *name, int name_len)
3905 struct btrfs_fs_info *fs_info = root->fs_info;
3906 struct btrfs_path *path;
3908 struct extent_buffer *leaf;
3909 struct btrfs_dir_item *di;
3910 struct btrfs_key key;
3912 u64 ino = btrfs_ino(inode);
3913 u64 dir_ino = btrfs_ino(dir);
3915 path = btrfs_alloc_path();
3921 path->leave_spinning = 1;
3922 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3923 name, name_len, -1);
3932 leaf = path->nodes[0];
3933 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3934 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3937 btrfs_release_path(path);
3940 * If we don't have dir index, we have to get it by looking up
3941 * the inode ref, since we get the inode ref, remove it directly,
3942 * it is unnecessary to do delayed deletion.
3944 * But if we have dir index, needn't search inode ref to get it.
3945 * Since the inode ref is close to the inode item, it is better
3946 * that we delay to delete it, and just do this deletion when
3947 * we update the inode item.
3949 if (inode->dir_index) {
3950 ret = btrfs_delayed_delete_inode_ref(inode);
3952 index = inode->dir_index;
3957 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3961 "failed to delete reference to %.*s, inode %llu parent %llu",
3962 name_len, name, ino, dir_ino);
3963 btrfs_abort_transaction(trans, ret);
3967 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
3969 btrfs_abort_transaction(trans, ret);
3973 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3975 if (ret != 0 && ret != -ENOENT) {
3976 btrfs_abort_transaction(trans, ret);
3980 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3985 btrfs_abort_transaction(trans, ret);
3987 btrfs_free_path(path);
3991 btrfs_i_size_write(&dir->vfs_inode,
3992 dir->vfs_inode.i_size - name_len * 2);
3993 inode_inc_iversion(&inode->vfs_inode);
3994 inode_inc_iversion(&dir->vfs_inode);
3995 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3996 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3997 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
4002 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root,
4004 struct btrfs_inode *dir, struct btrfs_inode *inode,
4005 const char *name, int name_len)
4008 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4010 drop_nlink(&inode->vfs_inode);
4011 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
4017 * helper to start transaction for unlink and rmdir.
4019 * unlink and rmdir are special in btrfs, they do not always free space, so
4020 * if we cannot make our reservations the normal way try and see if there is
4021 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4022 * allow the unlink to occur.
4024 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4026 struct btrfs_root *root = BTRFS_I(dir)->root;
4029 * 1 for the possible orphan item
4030 * 1 for the dir item
4031 * 1 for the dir index
4032 * 1 for the inode ref
4035 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
4038 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4040 struct btrfs_root *root = BTRFS_I(dir)->root;
4041 struct btrfs_trans_handle *trans;
4042 struct inode *inode = d_inode(dentry);
4045 trans = __unlink_start_trans(dir);
4047 return PTR_ERR(trans);
4049 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4052 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4053 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4054 dentry->d_name.len);
4058 if (inode->i_nlink == 0) {
4059 ret = btrfs_orphan_add(trans, inode);
4065 btrfs_end_transaction(trans);
4066 btrfs_btree_balance_dirty(root->fs_info);
4070 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4071 struct btrfs_root *root,
4072 struct inode *dir, u64 objectid,
4073 const char *name, int name_len)
4075 struct btrfs_fs_info *fs_info = root->fs_info;
4076 struct btrfs_path *path;
4077 struct extent_buffer *leaf;
4078 struct btrfs_dir_item *di;
4079 struct btrfs_key key;
4082 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4084 path = btrfs_alloc_path();
4088 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4089 name, name_len, -1);
4090 if (IS_ERR_OR_NULL(di)) {
4098 leaf = path->nodes[0];
4099 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4100 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4101 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4103 btrfs_abort_transaction(trans, ret);
4106 btrfs_release_path(path);
4108 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4109 root->root_key.objectid, dir_ino,
4110 &index, name, name_len);
4112 if (ret != -ENOENT) {
4113 btrfs_abort_transaction(trans, ret);
4116 di = btrfs_search_dir_index_item(root, path, dir_ino,
4118 if (IS_ERR_OR_NULL(di)) {
4123 btrfs_abort_transaction(trans, ret);
4127 leaf = path->nodes[0];
4128 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4129 btrfs_release_path(path);
4132 btrfs_release_path(path);
4134 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
4136 btrfs_abort_transaction(trans, ret);
4140 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4141 inode_inc_iversion(dir);
4142 dir->i_mtime = dir->i_ctime = current_time(dir);
4143 ret = btrfs_update_inode_fallback(trans, root, dir);
4145 btrfs_abort_transaction(trans, ret);
4147 btrfs_free_path(path);
4151 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4153 struct inode *inode = d_inode(dentry);
4155 struct btrfs_root *root = BTRFS_I(dir)->root;
4156 struct btrfs_trans_handle *trans;
4157 u64 last_unlink_trans;
4159 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4161 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4164 trans = __unlink_start_trans(dir);
4166 return PTR_ERR(trans);
4168 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4169 err = btrfs_unlink_subvol(trans, root, dir,
4170 BTRFS_I(inode)->location.objectid,
4171 dentry->d_name.name,
4172 dentry->d_name.len);
4176 err = btrfs_orphan_add(trans, inode);
4180 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4182 /* now the directory is empty */
4183 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4184 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4185 dentry->d_name.len);
4187 btrfs_i_size_write(inode, 0);
4189 * Propagate the last_unlink_trans value of the deleted dir to
4190 * its parent directory. This is to prevent an unrecoverable
4191 * log tree in the case we do something like this:
4193 * 2) create snapshot under dir foo
4194 * 3) delete the snapshot
4197 * 6) fsync foo or some file inside foo
4199 if (last_unlink_trans >= trans->transid)
4200 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4203 btrfs_end_transaction(trans);
4204 btrfs_btree_balance_dirty(root->fs_info);
4209 static int truncate_space_check(struct btrfs_trans_handle *trans,
4210 struct btrfs_root *root,
4213 struct btrfs_fs_info *fs_info = root->fs_info;
4217 * This is only used to apply pressure to the enospc system, we don't
4218 * intend to use this reservation at all.
4220 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
4221 bytes_deleted *= fs_info->nodesize;
4222 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
4223 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4225 trace_btrfs_space_reservation(fs_info, "transaction",
4228 trans->bytes_reserved += bytes_deleted;
4234 static int truncate_inline_extent(struct inode *inode,
4235 struct btrfs_path *path,
4236 struct btrfs_key *found_key,
4240 struct extent_buffer *leaf = path->nodes[0];
4241 int slot = path->slots[0];
4242 struct btrfs_file_extent_item *fi;
4243 u32 size = (u32)(new_size - found_key->offset);
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4246 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4248 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4249 loff_t offset = new_size;
4250 loff_t page_end = ALIGN(offset, PAGE_SIZE);
4253 * Zero out the remaining of the last page of our inline extent,
4254 * instead of directly truncating our inline extent here - that
4255 * would be much more complex (decompressing all the data, then
4256 * compressing the truncated data, which might be bigger than
4257 * the size of the inline extent, resize the extent, etc).
4258 * We release the path because to get the page we might need to
4259 * read the extent item from disk (data not in the page cache).
4261 btrfs_release_path(path);
4262 return btrfs_truncate_block(inode, offset, page_end - offset,
4266 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4267 size = btrfs_file_extent_calc_inline_size(size);
4268 btrfs_truncate_item(root->fs_info, path, size, 1);
4270 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4271 inode_sub_bytes(inode, item_end + 1 - new_size);
4277 * this can truncate away extent items, csum items and directory items.
4278 * It starts at a high offset and removes keys until it can't find
4279 * any higher than new_size
4281 * csum items that cross the new i_size are truncated to the new size
4284 * min_type is the minimum key type to truncate down to. If set to 0, this
4285 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4287 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4288 struct btrfs_root *root,
4289 struct inode *inode,
4290 u64 new_size, u32 min_type)
4292 struct btrfs_fs_info *fs_info = root->fs_info;
4293 struct btrfs_path *path;
4294 struct extent_buffer *leaf;
4295 struct btrfs_file_extent_item *fi;
4296 struct btrfs_key key;
4297 struct btrfs_key found_key;
4298 u64 extent_start = 0;
4299 u64 extent_num_bytes = 0;
4300 u64 extent_offset = 0;
4302 u64 last_size = new_size;
4303 u32 found_type = (u8)-1;
4306 int pending_del_nr = 0;
4307 int pending_del_slot = 0;
4308 int extent_type = -1;
4311 u64 ino = btrfs_ino(BTRFS_I(inode));
4312 u64 bytes_deleted = 0;
4314 bool should_throttle = 0;
4315 bool should_end = 0;
4317 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4320 * for non-free space inodes and ref cows, we want to back off from
4323 if (!btrfs_is_free_space_inode(inode) &&
4324 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4327 path = btrfs_alloc_path();
4330 path->reada = READA_BACK;
4333 * We want to drop from the next block forward in case this new size is
4334 * not block aligned since we will be keeping the last block of the
4335 * extent just the way it is.
4337 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4338 root == fs_info->tree_root)
4339 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4340 fs_info->sectorsize),
4344 * This function is also used to drop the items in the log tree before
4345 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4346 * it is used to drop the loged items. So we shouldn't kill the delayed
4349 if (min_type == 0 && root == BTRFS_I(inode)->root)
4350 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
4353 key.offset = (u64)-1;
4358 * with a 16K leaf size and 128MB extents, you can actually queue
4359 * up a huge file in a single leaf. Most of the time that
4360 * bytes_deleted is > 0, it will be huge by the time we get here
4362 if (be_nice && bytes_deleted > SZ_32M) {
4363 if (btrfs_should_end_transaction(trans)) {
4370 path->leave_spinning = 1;
4371 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4378 /* there are no items in the tree for us to truncate, we're
4381 if (path->slots[0] == 0)
4388 leaf = path->nodes[0];
4389 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4390 found_type = found_key.type;
4392 if (found_key.objectid != ino)
4395 if (found_type < min_type)
4398 item_end = found_key.offset;
4399 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4400 fi = btrfs_item_ptr(leaf, path->slots[0],
4401 struct btrfs_file_extent_item);
4402 extent_type = btrfs_file_extent_type(leaf, fi);
4403 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4405 btrfs_file_extent_num_bytes(leaf, fi);
4406 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4407 item_end += btrfs_file_extent_inline_len(leaf,
4408 path->slots[0], fi);
4412 if (found_type > min_type) {
4415 if (item_end < new_size) {
4417 * With NO_HOLES mode, for the following mapping
4419 * [0-4k][hole][8k-12k]
4421 * if truncating isize down to 6k, it ends up
4424 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4425 last_size = new_size;
4428 if (found_key.offset >= new_size)
4434 /* FIXME, shrink the extent if the ref count is only 1 */
4435 if (found_type != BTRFS_EXTENT_DATA_KEY)
4439 last_size = found_key.offset;
4441 last_size = new_size;
4443 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4445 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4447 u64 orig_num_bytes =
4448 btrfs_file_extent_num_bytes(leaf, fi);
4449 extent_num_bytes = ALIGN(new_size -
4451 fs_info->sectorsize);
4452 btrfs_set_file_extent_num_bytes(leaf, fi,
4454 num_dec = (orig_num_bytes -
4456 if (test_bit(BTRFS_ROOT_REF_COWS,
4459 inode_sub_bytes(inode, num_dec);
4460 btrfs_mark_buffer_dirty(leaf);
4463 btrfs_file_extent_disk_num_bytes(leaf,
4465 extent_offset = found_key.offset -
4466 btrfs_file_extent_offset(leaf, fi);
4468 /* FIXME blocksize != 4096 */
4469 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4470 if (extent_start != 0) {
4472 if (test_bit(BTRFS_ROOT_REF_COWS,
4474 inode_sub_bytes(inode, num_dec);
4477 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4479 * we can't truncate inline items that have had
4483 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4484 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4487 * Need to release path in order to truncate a
4488 * compressed extent. So delete any accumulated
4489 * extent items so far.
4491 if (btrfs_file_extent_compression(leaf, fi) !=
4492 BTRFS_COMPRESS_NONE && pending_del_nr) {
4493 err = btrfs_del_items(trans, root, path,
4497 btrfs_abort_transaction(trans,
4504 err = truncate_inline_extent(inode, path,
4509 btrfs_abort_transaction(trans, err);
4512 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4514 inode_sub_bytes(inode, item_end + 1 - new_size);
4519 if (!pending_del_nr) {
4520 /* no pending yet, add ourselves */
4521 pending_del_slot = path->slots[0];
4523 } else if (pending_del_nr &&
4524 path->slots[0] + 1 == pending_del_slot) {
4525 /* hop on the pending chunk */
4527 pending_del_slot = path->slots[0];
4534 should_throttle = 0;
4537 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4538 root == fs_info->tree_root)) {
4539 btrfs_set_path_blocking(path);
4540 bytes_deleted += extent_num_bytes;
4541 ret = btrfs_free_extent(trans, fs_info, extent_start,
4542 extent_num_bytes, 0,
4543 btrfs_header_owner(leaf),
4544 ino, extent_offset);
4546 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4547 btrfs_async_run_delayed_refs(fs_info,
4548 trans->delayed_ref_updates * 2,
4551 if (truncate_space_check(trans, root,
4552 extent_num_bytes)) {
4555 if (btrfs_should_throttle_delayed_refs(trans,
4557 should_throttle = 1;
4561 if (found_type == BTRFS_INODE_ITEM_KEY)
4564 if (path->slots[0] == 0 ||
4565 path->slots[0] != pending_del_slot ||
4566 should_throttle || should_end) {
4567 if (pending_del_nr) {
4568 ret = btrfs_del_items(trans, root, path,
4572 btrfs_abort_transaction(trans, ret);
4577 btrfs_release_path(path);
4578 if (should_throttle) {
4579 unsigned long updates = trans->delayed_ref_updates;
4581 trans->delayed_ref_updates = 0;
4582 ret = btrfs_run_delayed_refs(trans,
4590 * if we failed to refill our space rsv, bail out
4591 * and let the transaction restart
4603 if (pending_del_nr) {
4604 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4607 btrfs_abort_transaction(trans, ret);
4610 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4611 btrfs_ordered_update_i_size(inode, last_size, NULL);
4613 btrfs_free_path(path);
4616 /* only inline file may have last_size != new_size */
4617 if (new_size >= fs_info->sectorsize ||
4618 new_size > fs_info->max_inline)
4619 ASSERT(last_size == new_size);
4622 if (be_nice && bytes_deleted > SZ_32M) {
4623 unsigned long updates = trans->delayed_ref_updates;
4625 trans->delayed_ref_updates = 0;
4626 ret = btrfs_run_delayed_refs(trans, fs_info,
4636 * btrfs_truncate_block - read, zero a chunk and write a block
4637 * @inode - inode that we're zeroing
4638 * @from - the offset to start zeroing
4639 * @len - the length to zero, 0 to zero the entire range respective to the
4641 * @front - zero up to the offset instead of from the offset on
4643 * This will find the block for the "from" offset and cow the block and zero the
4644 * part we want to zero. This is used with truncate and hole punching.
4646 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4650 struct address_space *mapping = inode->i_mapping;
4651 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4652 struct btrfs_ordered_extent *ordered;
4653 struct extent_state *cached_state = NULL;
4655 u32 blocksize = fs_info->sectorsize;
4656 pgoff_t index = from >> PAGE_SHIFT;
4657 unsigned offset = from & (blocksize - 1);
4659 gfp_t mask = btrfs_alloc_write_mask(mapping);
4664 if ((offset & (blocksize - 1)) == 0 &&
4665 (!len || ((len & (blocksize - 1)) == 0)))
4668 ret = btrfs_delalloc_reserve_space(inode,
4669 round_down(from, blocksize), blocksize);
4674 page = find_or_create_page(mapping, index, mask);
4676 btrfs_delalloc_release_space(inode,
4677 round_down(from, blocksize),
4683 block_start = round_down(from, blocksize);
4684 block_end = block_start + blocksize - 1;
4686 if (!PageUptodate(page)) {
4687 ret = btrfs_readpage(NULL, page);
4689 if (page->mapping != mapping) {
4694 if (!PageUptodate(page)) {
4699 wait_on_page_writeback(page);
4701 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
4702 set_page_extent_mapped(page);
4704 ordered = btrfs_lookup_ordered_extent(inode, block_start);
4706 unlock_extent_cached(io_tree, block_start, block_end,
4707 &cached_state, GFP_NOFS);
4710 btrfs_start_ordered_extent(inode, ordered, 1);
4711 btrfs_put_ordered_extent(ordered);
4715 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
4716 EXTENT_DIRTY | EXTENT_DELALLOC |
4717 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4718 0, 0, &cached_state, GFP_NOFS);
4720 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
4723 unlock_extent_cached(io_tree, block_start, block_end,
4724 &cached_state, GFP_NOFS);
4728 if (offset != blocksize) {
4730 len = blocksize - offset;
4733 memset(kaddr + (block_start - page_offset(page)),
4736 memset(kaddr + (block_start - page_offset(page)) + offset,
4738 flush_dcache_page(page);
4741 ClearPageChecked(page);
4742 set_page_dirty(page);
4743 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
4748 btrfs_delalloc_release_space(inode, block_start,
4756 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4757 u64 offset, u64 len)
4759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4760 struct btrfs_trans_handle *trans;
4764 * Still need to make sure the inode looks like it's been updated so
4765 * that any holes get logged if we fsync.
4767 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4768 BTRFS_I(inode)->last_trans = fs_info->generation;
4769 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4770 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4775 * 1 - for the one we're dropping
4776 * 1 - for the one we're adding
4777 * 1 - for updating the inode.
4779 trans = btrfs_start_transaction(root, 3);
4781 return PTR_ERR(trans);
4783 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4785 btrfs_abort_transaction(trans, ret);
4786 btrfs_end_transaction(trans);
4790 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4791 offset, 0, 0, len, 0, len, 0, 0, 0);
4793 btrfs_abort_transaction(trans, ret);
4795 btrfs_update_inode(trans, root, inode);
4796 btrfs_end_transaction(trans);
4801 * This function puts in dummy file extents for the area we're creating a hole
4802 * for. So if we are truncating this file to a larger size we need to insert
4803 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4804 * the range between oldsize and size
4806 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4809 struct btrfs_root *root = BTRFS_I(inode)->root;
4810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4811 struct extent_map *em = NULL;
4812 struct extent_state *cached_state = NULL;
4813 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4814 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4815 u64 block_end = ALIGN(size, fs_info->sectorsize);
4822 * If our size started in the middle of a block we need to zero out the
4823 * rest of the block before we expand the i_size, otherwise we could
4824 * expose stale data.
4826 err = btrfs_truncate_block(inode, oldsize, 0, 0);
4830 if (size <= hole_start)
4834 struct btrfs_ordered_extent *ordered;
4836 lock_extent_bits(io_tree, hole_start, block_end - 1,
4838 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4839 block_end - hole_start);
4842 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4843 &cached_state, GFP_NOFS);
4844 btrfs_start_ordered_extent(inode, ordered, 1);
4845 btrfs_put_ordered_extent(ordered);
4848 cur_offset = hole_start;
4850 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4851 block_end - cur_offset, 0);
4857 last_byte = min(extent_map_end(em), block_end);
4858 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4859 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4860 struct extent_map *hole_em;
4861 hole_size = last_byte - cur_offset;
4863 err = maybe_insert_hole(root, inode, cur_offset,
4867 btrfs_drop_extent_cache(inode, cur_offset,
4868 cur_offset + hole_size - 1, 0);
4869 hole_em = alloc_extent_map();
4871 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4872 &BTRFS_I(inode)->runtime_flags);
4875 hole_em->start = cur_offset;
4876 hole_em->len = hole_size;
4877 hole_em->orig_start = cur_offset;
4879 hole_em->block_start = EXTENT_MAP_HOLE;
4880 hole_em->block_len = 0;
4881 hole_em->orig_block_len = 0;
4882 hole_em->ram_bytes = hole_size;
4883 hole_em->bdev = fs_info->fs_devices->latest_bdev;
4884 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4885 hole_em->generation = fs_info->generation;
4888 write_lock(&em_tree->lock);
4889 err = add_extent_mapping(em_tree, hole_em, 1);
4890 write_unlock(&em_tree->lock);
4893 btrfs_drop_extent_cache(inode, cur_offset,
4897 free_extent_map(hole_em);
4900 free_extent_map(em);
4902 cur_offset = last_byte;
4903 if (cur_offset >= block_end)
4906 free_extent_map(em);
4907 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4912 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4914 struct btrfs_root *root = BTRFS_I(inode)->root;
4915 struct btrfs_trans_handle *trans;
4916 loff_t oldsize = i_size_read(inode);
4917 loff_t newsize = attr->ia_size;
4918 int mask = attr->ia_valid;
4922 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4923 * special case where we need to update the times despite not having
4924 * these flags set. For all other operations the VFS set these flags
4925 * explicitly if it wants a timestamp update.
4927 if (newsize != oldsize) {
4928 inode_inc_iversion(inode);
4929 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4930 inode->i_ctime = inode->i_mtime =
4931 current_time(inode);
4934 if (newsize > oldsize) {
4936 * Don't do an expanding truncate while snapshoting is ongoing.
4937 * This is to ensure the snapshot captures a fully consistent
4938 * state of this file - if the snapshot captures this expanding
4939 * truncation, it must capture all writes that happened before
4942 btrfs_wait_for_snapshot_creation(root);
4943 ret = btrfs_cont_expand(inode, oldsize, newsize);
4945 btrfs_end_write_no_snapshoting(root);
4949 trans = btrfs_start_transaction(root, 1);
4950 if (IS_ERR(trans)) {
4951 btrfs_end_write_no_snapshoting(root);
4952 return PTR_ERR(trans);
4955 i_size_write(inode, newsize);
4956 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4957 pagecache_isize_extended(inode, oldsize, newsize);
4958 ret = btrfs_update_inode(trans, root, inode);
4959 btrfs_end_write_no_snapshoting(root);
4960 btrfs_end_transaction(trans);
4964 * We're truncating a file that used to have good data down to
4965 * zero. Make sure it gets into the ordered flush list so that
4966 * any new writes get down to disk quickly.
4969 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4970 &BTRFS_I(inode)->runtime_flags);
4973 * 1 for the orphan item we're going to add
4974 * 1 for the orphan item deletion.
4976 trans = btrfs_start_transaction(root, 2);
4978 return PTR_ERR(trans);
4981 * We need to do this in case we fail at _any_ point during the
4982 * actual truncate. Once we do the truncate_setsize we could
4983 * invalidate pages which forces any outstanding ordered io to
4984 * be instantly completed which will give us extents that need
4985 * to be truncated. If we fail to get an orphan inode down we
4986 * could have left over extents that were never meant to live,
4987 * so we need to guarantee from this point on that everything
4988 * will be consistent.
4990 ret = btrfs_orphan_add(trans, inode);
4991 btrfs_end_transaction(trans);
4995 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4996 truncate_setsize(inode, newsize);
4998 /* Disable nonlocked read DIO to avoid the end less truncate */
4999 btrfs_inode_block_unlocked_dio(inode);
5000 inode_dio_wait(inode);
5001 btrfs_inode_resume_unlocked_dio(inode);
5003 ret = btrfs_truncate(inode);
5004 if (ret && inode->i_nlink) {
5007 /* To get a stable disk_i_size */
5008 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5010 btrfs_orphan_del(NULL, inode);
5015 * failed to truncate, disk_i_size is only adjusted down
5016 * as we remove extents, so it should represent the true
5017 * size of the inode, so reset the in memory size and
5018 * delete our orphan entry.
5020 trans = btrfs_join_transaction(root);
5021 if (IS_ERR(trans)) {
5022 btrfs_orphan_del(NULL, inode);
5025 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5026 err = btrfs_orphan_del(trans, inode);
5028 btrfs_abort_transaction(trans, err);
5029 btrfs_end_transaction(trans);
5036 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5038 struct inode *inode = d_inode(dentry);
5039 struct btrfs_root *root = BTRFS_I(inode)->root;
5042 if (btrfs_root_readonly(root))
5045 err = setattr_prepare(dentry, attr);
5049 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5050 err = btrfs_setsize(inode, attr);
5055 if (attr->ia_valid) {
5056 setattr_copy(inode, attr);
5057 inode_inc_iversion(inode);
5058 err = btrfs_dirty_inode(inode);
5060 if (!err && attr->ia_valid & ATTR_MODE)
5061 err = posix_acl_chmod(inode, inode->i_mode);
5068 * While truncating the inode pages during eviction, we get the VFS calling
5069 * btrfs_invalidatepage() against each page of the inode. This is slow because
5070 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5071 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5072 * extent_state structures over and over, wasting lots of time.
5074 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5075 * those expensive operations on a per page basis and do only the ordered io
5076 * finishing, while we release here the extent_map and extent_state structures,
5077 * without the excessive merging and splitting.
5079 static void evict_inode_truncate_pages(struct inode *inode)
5081 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5082 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5083 struct rb_node *node;
5085 ASSERT(inode->i_state & I_FREEING);
5086 truncate_inode_pages_final(&inode->i_data);
5088 write_lock(&map_tree->lock);
5089 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5090 struct extent_map *em;
5092 node = rb_first(&map_tree->map);
5093 em = rb_entry(node, struct extent_map, rb_node);
5094 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5095 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5096 remove_extent_mapping(map_tree, em);
5097 free_extent_map(em);
5098 if (need_resched()) {
5099 write_unlock(&map_tree->lock);
5101 write_lock(&map_tree->lock);
5104 write_unlock(&map_tree->lock);
5107 * Keep looping until we have no more ranges in the io tree.
5108 * We can have ongoing bios started by readpages (called from readahead)
5109 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5110 * still in progress (unlocked the pages in the bio but did not yet
5111 * unlocked the ranges in the io tree). Therefore this means some
5112 * ranges can still be locked and eviction started because before
5113 * submitting those bios, which are executed by a separate task (work
5114 * queue kthread), inode references (inode->i_count) were not taken
5115 * (which would be dropped in the end io callback of each bio).
5116 * Therefore here we effectively end up waiting for those bios and
5117 * anyone else holding locked ranges without having bumped the inode's
5118 * reference count - if we don't do it, when they access the inode's
5119 * io_tree to unlock a range it may be too late, leading to an
5120 * use-after-free issue.
5122 spin_lock(&io_tree->lock);
5123 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5124 struct extent_state *state;
5125 struct extent_state *cached_state = NULL;
5129 node = rb_first(&io_tree->state);
5130 state = rb_entry(node, struct extent_state, rb_node);
5131 start = state->start;
5133 spin_unlock(&io_tree->lock);
5135 lock_extent_bits(io_tree, start, end, &cached_state);
5138 * If still has DELALLOC flag, the extent didn't reach disk,
5139 * and its reserved space won't be freed by delayed_ref.
5140 * So we need to free its reserved space here.
5141 * (Refer to comment in btrfs_invalidatepage, case 2)
5143 * Note, end is the bytenr of last byte, so we need + 1 here.
5145 if (state->state & EXTENT_DELALLOC)
5146 btrfs_qgroup_free_data(inode, start, end - start + 1);
5148 clear_extent_bit(io_tree, start, end,
5149 EXTENT_LOCKED | EXTENT_DIRTY |
5150 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5151 EXTENT_DEFRAG, 1, 1,
5152 &cached_state, GFP_NOFS);
5155 spin_lock(&io_tree->lock);
5157 spin_unlock(&io_tree->lock);
5160 void btrfs_evict_inode(struct inode *inode)
5162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5163 struct btrfs_trans_handle *trans;
5164 struct btrfs_root *root = BTRFS_I(inode)->root;
5165 struct btrfs_block_rsv *rsv, *global_rsv;
5166 int steal_from_global = 0;
5170 trace_btrfs_inode_evict(inode);
5173 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5177 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
5179 evict_inode_truncate_pages(inode);
5181 if (inode->i_nlink &&
5182 ((btrfs_root_refs(&root->root_item) != 0 &&
5183 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5184 btrfs_is_free_space_inode(inode)))
5187 if (is_bad_inode(inode)) {
5188 btrfs_orphan_del(NULL, inode);
5191 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5192 if (!special_file(inode->i_mode))
5193 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5195 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5197 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
5198 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
5199 &BTRFS_I(inode)->runtime_flags));
5203 if (inode->i_nlink > 0) {
5204 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5205 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5209 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5211 btrfs_orphan_del(NULL, inode);
5215 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5217 btrfs_orphan_del(NULL, inode);
5220 rsv->size = min_size;
5222 global_rsv = &fs_info->global_block_rsv;
5224 btrfs_i_size_write(inode, 0);
5227 * This is a bit simpler than btrfs_truncate since we've already
5228 * reserved our space for our orphan item in the unlink, so we just
5229 * need to reserve some slack space in case we add bytes and update
5230 * inode item when doing the truncate.
5233 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5234 BTRFS_RESERVE_FLUSH_LIMIT);
5237 * Try and steal from the global reserve since we will
5238 * likely not use this space anyway, we want to try as
5239 * hard as possible to get this to work.
5242 steal_from_global++;
5244 steal_from_global = 0;
5248 * steal_from_global == 0: we reserved stuff, hooray!
5249 * steal_from_global == 1: we didn't reserve stuff, boo!
5250 * steal_from_global == 2: we've committed, still not a lot of
5251 * room but maybe we'll have room in the global reserve this
5253 * steal_from_global == 3: abandon all hope!
5255 if (steal_from_global > 2) {
5257 "Could not get space for a delete, will truncate on mount %d",
5259 btrfs_orphan_del(NULL, inode);
5260 btrfs_free_block_rsv(fs_info, rsv);
5264 trans = btrfs_join_transaction(root);
5265 if (IS_ERR(trans)) {
5266 btrfs_orphan_del(NULL, inode);
5267 btrfs_free_block_rsv(fs_info, rsv);
5272 * We can't just steal from the global reserve, we need to make
5273 * sure there is room to do it, if not we need to commit and try
5276 if (steal_from_global) {
5277 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
5278 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5285 * Couldn't steal from the global reserve, we have too much
5286 * pending stuff built up, commit the transaction and try it
5290 ret = btrfs_commit_transaction(trans);
5292 btrfs_orphan_del(NULL, inode);
5293 btrfs_free_block_rsv(fs_info, rsv);
5298 steal_from_global = 0;
5301 trans->block_rsv = rsv;
5303 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5304 if (ret != -ENOSPC && ret != -EAGAIN)
5307 trans->block_rsv = &fs_info->trans_block_rsv;
5308 btrfs_end_transaction(trans);
5310 btrfs_btree_balance_dirty(fs_info);
5313 btrfs_free_block_rsv(fs_info, rsv);
5316 * Errors here aren't a big deal, it just means we leave orphan items
5317 * in the tree. They will be cleaned up on the next mount.
5320 trans->block_rsv = root->orphan_block_rsv;
5321 btrfs_orphan_del(trans, inode);
5323 btrfs_orphan_del(NULL, inode);
5326 trans->block_rsv = &fs_info->trans_block_rsv;
5327 if (!(root == fs_info->tree_root ||
5328 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5329 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
5331 btrfs_end_transaction(trans);
5332 btrfs_btree_balance_dirty(fs_info);
5334 btrfs_remove_delayed_node(BTRFS_I(inode));
5339 * this returns the key found in the dir entry in the location pointer.
5340 * If no dir entries were found, location->objectid is 0.
5342 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5343 struct btrfs_key *location)
5345 const char *name = dentry->d_name.name;
5346 int namelen = dentry->d_name.len;
5347 struct btrfs_dir_item *di;
5348 struct btrfs_path *path;
5349 struct btrfs_root *root = BTRFS_I(dir)->root;
5352 path = btrfs_alloc_path();
5356 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5361 if (IS_ERR_OR_NULL(di))
5364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5366 btrfs_free_path(path);
5369 location->objectid = 0;
5374 * when we hit a tree root in a directory, the btrfs part of the inode
5375 * needs to be changed to reflect the root directory of the tree root. This
5376 * is kind of like crossing a mount point.
5378 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5380 struct dentry *dentry,
5381 struct btrfs_key *location,
5382 struct btrfs_root **sub_root)
5384 struct btrfs_path *path;
5385 struct btrfs_root *new_root;
5386 struct btrfs_root_ref *ref;
5387 struct extent_buffer *leaf;
5388 struct btrfs_key key;
5392 path = btrfs_alloc_path();
5399 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5400 key.type = BTRFS_ROOT_REF_KEY;
5401 key.offset = location->objectid;
5403 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5410 leaf = path->nodes[0];
5411 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5412 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
5413 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5416 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5417 (unsigned long)(ref + 1),
5418 dentry->d_name.len);
5422 btrfs_release_path(path);
5424 new_root = btrfs_read_fs_root_no_name(fs_info, location);
5425 if (IS_ERR(new_root)) {
5426 err = PTR_ERR(new_root);
5430 *sub_root = new_root;
5431 location->objectid = btrfs_root_dirid(&new_root->root_item);
5432 location->type = BTRFS_INODE_ITEM_KEY;
5433 location->offset = 0;
5436 btrfs_free_path(path);
5440 static void inode_tree_add(struct inode *inode)
5442 struct btrfs_root *root = BTRFS_I(inode)->root;
5443 struct btrfs_inode *entry;
5445 struct rb_node *parent;
5446 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5447 u64 ino = btrfs_ino(BTRFS_I(inode));
5449 if (inode_unhashed(inode))
5452 spin_lock(&root->inode_lock);
5453 p = &root->inode_tree.rb_node;
5456 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5458 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5459 p = &parent->rb_left;
5460 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5461 p = &parent->rb_right;
5463 WARN_ON(!(entry->vfs_inode.i_state &
5464 (I_WILL_FREE | I_FREEING)));
5465 rb_replace_node(parent, new, &root->inode_tree);
5466 RB_CLEAR_NODE(parent);
5467 spin_unlock(&root->inode_lock);
5471 rb_link_node(new, parent, p);
5472 rb_insert_color(new, &root->inode_tree);
5473 spin_unlock(&root->inode_lock);
5476 static void inode_tree_del(struct inode *inode)
5478 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5479 struct btrfs_root *root = BTRFS_I(inode)->root;
5482 spin_lock(&root->inode_lock);
5483 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5484 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5485 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
5486 empty = RB_EMPTY_ROOT(&root->inode_tree);
5488 spin_unlock(&root->inode_lock);
5490 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5491 synchronize_srcu(&fs_info->subvol_srcu);
5492 spin_lock(&root->inode_lock);
5493 empty = RB_EMPTY_ROOT(&root->inode_tree);
5494 spin_unlock(&root->inode_lock);
5496 btrfs_add_dead_root(root);
5500 void btrfs_invalidate_inodes(struct btrfs_root *root)
5502 struct btrfs_fs_info *fs_info = root->fs_info;
5503 struct rb_node *node;
5504 struct rb_node *prev;
5505 struct btrfs_inode *entry;
5506 struct inode *inode;
5509 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
5510 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5512 spin_lock(&root->inode_lock);
5514 node = root->inode_tree.rb_node;
5518 entry = rb_entry(node, struct btrfs_inode, rb_node);
5520 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5521 node = node->rb_left;
5522 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5523 node = node->rb_right;
5529 entry = rb_entry(prev, struct btrfs_inode, rb_node);
5530 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
5534 prev = rb_next(prev);
5538 entry = rb_entry(node, struct btrfs_inode, rb_node);
5539 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
5540 inode = igrab(&entry->vfs_inode);
5542 spin_unlock(&root->inode_lock);
5543 if (atomic_read(&inode->i_count) > 1)
5544 d_prune_aliases(inode);
5546 * btrfs_drop_inode will have it removed from
5547 * the inode cache when its usage count
5552 spin_lock(&root->inode_lock);
5556 if (cond_resched_lock(&root->inode_lock))
5559 node = rb_next(node);
5561 spin_unlock(&root->inode_lock);
5564 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5566 struct btrfs_iget_args *args = p;
5567 inode->i_ino = args->location->objectid;
5568 memcpy(&BTRFS_I(inode)->location, args->location,
5569 sizeof(*args->location));
5570 BTRFS_I(inode)->root = args->root;
5574 static int btrfs_find_actor(struct inode *inode, void *opaque)
5576 struct btrfs_iget_args *args = opaque;
5577 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5578 args->root == BTRFS_I(inode)->root;
5581 static struct inode *btrfs_iget_locked(struct super_block *s,
5582 struct btrfs_key *location,
5583 struct btrfs_root *root)
5585 struct inode *inode;
5586 struct btrfs_iget_args args;
5587 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5589 args.location = location;
5592 inode = iget5_locked(s, hashval, btrfs_find_actor,
5593 btrfs_init_locked_inode,
5598 /* Get an inode object given its location and corresponding root.
5599 * Returns in *is_new if the inode was read from disk
5601 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5602 struct btrfs_root *root, int *new)
5604 struct inode *inode;
5606 inode = btrfs_iget_locked(s, location, root);
5608 return ERR_PTR(-ENOMEM);
5610 if (inode->i_state & I_NEW) {
5613 ret = btrfs_read_locked_inode(inode);
5614 if (!is_bad_inode(inode)) {
5615 inode_tree_add(inode);
5616 unlock_new_inode(inode);
5620 unlock_new_inode(inode);
5623 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
5630 static struct inode *new_simple_dir(struct super_block *s,
5631 struct btrfs_key *key,
5632 struct btrfs_root *root)
5634 struct inode *inode = new_inode(s);
5637 return ERR_PTR(-ENOMEM);
5639 BTRFS_I(inode)->root = root;
5640 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5641 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5643 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5644 inode->i_op = &btrfs_dir_ro_inode_operations;
5645 inode->i_opflags &= ~IOP_XATTR;
5646 inode->i_fop = &simple_dir_operations;
5647 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5648 inode->i_mtime = current_time(inode);
5649 inode->i_atime = inode->i_mtime;
5650 inode->i_ctime = inode->i_mtime;
5651 BTRFS_I(inode)->i_otime = inode->i_mtime;
5656 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5658 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5659 struct inode *inode;
5660 struct btrfs_root *root = BTRFS_I(dir)->root;
5661 struct btrfs_root *sub_root = root;
5662 struct btrfs_key location;
5666 if (dentry->d_name.len > BTRFS_NAME_LEN)
5667 return ERR_PTR(-ENAMETOOLONG);
5669 ret = btrfs_inode_by_name(dir, dentry, &location);
5671 return ERR_PTR(ret);
5673 if (location.objectid == 0)
5674 return ERR_PTR(-ENOENT);
5676 if (location.type == BTRFS_INODE_ITEM_KEY) {
5677 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5681 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5683 index = srcu_read_lock(&fs_info->subvol_srcu);
5684 ret = fixup_tree_root_location(fs_info, dir, dentry,
5685 &location, &sub_root);
5688 inode = ERR_PTR(ret);
5690 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5692 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5694 srcu_read_unlock(&fs_info->subvol_srcu, index);
5696 if (!IS_ERR(inode) && root != sub_root) {
5697 down_read(&fs_info->cleanup_work_sem);
5698 if (!(inode->i_sb->s_flags & MS_RDONLY))
5699 ret = btrfs_orphan_cleanup(sub_root);
5700 up_read(&fs_info->cleanup_work_sem);
5703 inode = ERR_PTR(ret);
5710 static int btrfs_dentry_delete(const struct dentry *dentry)
5712 struct btrfs_root *root;
5713 struct inode *inode = d_inode(dentry);
5715 if (!inode && !IS_ROOT(dentry))
5716 inode = d_inode(dentry->d_parent);
5719 root = BTRFS_I(inode)->root;
5720 if (btrfs_root_refs(&root->root_item) == 0)
5723 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5729 static void btrfs_dentry_release(struct dentry *dentry)
5731 kfree(dentry->d_fsdata);
5734 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5737 struct inode *inode;
5739 inode = btrfs_lookup_dentry(dir, dentry);
5740 if (IS_ERR(inode)) {
5741 if (PTR_ERR(inode) == -ENOENT)
5744 return ERR_CAST(inode);
5747 return d_splice_alias(inode, dentry);
5750 unsigned char btrfs_filetype_table[] = {
5751 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5754 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5756 struct inode *inode = file_inode(file);
5757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5758 struct btrfs_root *root = BTRFS_I(inode)->root;
5759 struct btrfs_item *item;
5760 struct btrfs_dir_item *di;
5761 struct btrfs_key key;
5762 struct btrfs_key found_key;
5763 struct btrfs_path *path;
5764 struct list_head ins_list;
5765 struct list_head del_list;
5767 struct extent_buffer *leaf;
5769 unsigned char d_type;
5775 struct btrfs_key location;
5777 if (!dir_emit_dots(file, ctx))
5780 path = btrfs_alloc_path();
5784 path->reada = READA_FORWARD;
5786 INIT_LIST_HEAD(&ins_list);
5787 INIT_LIST_HEAD(&del_list);
5788 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
5790 key.type = BTRFS_DIR_INDEX_KEY;
5791 key.offset = ctx->pos;
5792 key.objectid = btrfs_ino(BTRFS_I(inode));
5794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5799 leaf = path->nodes[0];
5800 slot = path->slots[0];
5801 if (slot >= btrfs_header_nritems(leaf)) {
5802 ret = btrfs_next_leaf(root, path);
5810 item = btrfs_item_nr(slot);
5811 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5813 if (found_key.objectid != key.objectid)
5815 if (found_key.type != BTRFS_DIR_INDEX_KEY)
5817 if (found_key.offset < ctx->pos)
5819 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5822 ctx->pos = found_key.offset;
5824 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5825 if (verify_dir_item(fs_info, leaf, di))
5828 name_len = btrfs_dir_name_len(leaf, di);
5829 if (name_len <= sizeof(tmp_name)) {
5830 name_ptr = tmp_name;
5832 name_ptr = kmalloc(name_len, GFP_KERNEL);
5838 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5841 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5842 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5844 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5847 if (name_ptr != tmp_name)
5857 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5862 * Stop new entries from being returned after we return the last
5865 * New directory entries are assigned a strictly increasing
5866 * offset. This means that new entries created during readdir
5867 * are *guaranteed* to be seen in the future by that readdir.
5868 * This has broken buggy programs which operate on names as
5869 * they're returned by readdir. Until we re-use freed offsets
5870 * we have this hack to stop new entries from being returned
5871 * under the assumption that they'll never reach this huge
5874 * This is being careful not to overflow 32bit loff_t unless the
5875 * last entry requires it because doing so has broken 32bit apps
5878 if (ctx->pos >= INT_MAX)
5879 ctx->pos = LLONG_MAX;
5886 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
5887 btrfs_free_path(path);
5891 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5893 struct btrfs_root *root = BTRFS_I(inode)->root;
5894 struct btrfs_trans_handle *trans;
5896 bool nolock = false;
5898 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5901 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5904 if (wbc->sync_mode == WB_SYNC_ALL) {
5906 trans = btrfs_join_transaction_nolock(root);
5908 trans = btrfs_join_transaction(root);
5910 return PTR_ERR(trans);
5911 ret = btrfs_commit_transaction(trans);
5917 * This is somewhat expensive, updating the tree every time the
5918 * inode changes. But, it is most likely to find the inode in cache.
5919 * FIXME, needs more benchmarking...there are no reasons other than performance
5920 * to keep or drop this code.
5922 static int btrfs_dirty_inode(struct inode *inode)
5924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5925 struct btrfs_root *root = BTRFS_I(inode)->root;
5926 struct btrfs_trans_handle *trans;
5929 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5932 trans = btrfs_join_transaction(root);
5934 return PTR_ERR(trans);
5936 ret = btrfs_update_inode(trans, root, inode);
5937 if (ret && ret == -ENOSPC) {
5938 /* whoops, lets try again with the full transaction */
5939 btrfs_end_transaction(trans);
5940 trans = btrfs_start_transaction(root, 1);
5942 return PTR_ERR(trans);
5944 ret = btrfs_update_inode(trans, root, inode);
5946 btrfs_end_transaction(trans);
5947 if (BTRFS_I(inode)->delayed_node)
5948 btrfs_balance_delayed_items(fs_info);
5954 * This is a copy of file_update_time. We need this so we can return error on
5955 * ENOSPC for updating the inode in the case of file write and mmap writes.
5957 static int btrfs_update_time(struct inode *inode, struct timespec *now,
5960 struct btrfs_root *root = BTRFS_I(inode)->root;
5962 if (btrfs_root_readonly(root))
5965 if (flags & S_VERSION)
5966 inode_inc_iversion(inode);
5967 if (flags & S_CTIME)
5968 inode->i_ctime = *now;
5969 if (flags & S_MTIME)
5970 inode->i_mtime = *now;
5971 if (flags & S_ATIME)
5972 inode->i_atime = *now;
5973 return btrfs_dirty_inode(inode);
5977 * find the highest existing sequence number in a directory
5978 * and then set the in-memory index_cnt variable to reflect
5979 * free sequence numbers
5981 static int btrfs_set_inode_index_count(struct inode *inode)
5983 struct btrfs_root *root = BTRFS_I(inode)->root;
5984 struct btrfs_key key, found_key;
5985 struct btrfs_path *path;
5986 struct extent_buffer *leaf;
5989 key.objectid = btrfs_ino(BTRFS_I(inode));
5990 key.type = BTRFS_DIR_INDEX_KEY;
5991 key.offset = (u64)-1;
5993 path = btrfs_alloc_path();
5997 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6000 /* FIXME: we should be able to handle this */
6006 * MAGIC NUMBER EXPLANATION:
6007 * since we search a directory based on f_pos we have to start at 2
6008 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6009 * else has to start at 2
6011 if (path->slots[0] == 0) {
6012 BTRFS_I(inode)->index_cnt = 2;
6018 leaf = path->nodes[0];
6019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6021 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
6022 found_key.type != BTRFS_DIR_INDEX_KEY) {
6023 BTRFS_I(inode)->index_cnt = 2;
6027 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6029 btrfs_free_path(path);
6034 * helper to find a free sequence number in a given directory. This current
6035 * code is very simple, later versions will do smarter things in the btree
6037 int btrfs_set_inode_index(struct inode *dir, u64 *index)
6041 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
6042 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
6044 ret = btrfs_set_inode_index_count(dir);
6050 *index = BTRFS_I(dir)->index_cnt;
6051 BTRFS_I(dir)->index_cnt++;
6056 static int btrfs_insert_inode_locked(struct inode *inode)
6058 struct btrfs_iget_args args;
6059 args.location = &BTRFS_I(inode)->location;
6060 args.root = BTRFS_I(inode)->root;
6062 return insert_inode_locked4(inode,
6063 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6064 btrfs_find_actor, &args);
6067 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6068 struct btrfs_root *root,
6070 const char *name, int name_len,
6071 u64 ref_objectid, u64 objectid,
6072 umode_t mode, u64 *index)
6074 struct btrfs_fs_info *fs_info = root->fs_info;
6075 struct inode *inode;
6076 struct btrfs_inode_item *inode_item;
6077 struct btrfs_key *location;
6078 struct btrfs_path *path;
6079 struct btrfs_inode_ref *ref;
6080 struct btrfs_key key[2];
6082 int nitems = name ? 2 : 1;
6086 path = btrfs_alloc_path();
6088 return ERR_PTR(-ENOMEM);
6090 inode = new_inode(fs_info->sb);
6092 btrfs_free_path(path);
6093 return ERR_PTR(-ENOMEM);
6097 * O_TMPFILE, set link count to 0, so that after this point,
6098 * we fill in an inode item with the correct link count.
6101 set_nlink(inode, 0);
6104 * we have to initialize this early, so we can reclaim the inode
6105 * number if we fail afterwards in this function.
6107 inode->i_ino = objectid;
6110 trace_btrfs_inode_request(dir);
6112 ret = btrfs_set_inode_index(dir, index);
6114 btrfs_free_path(path);
6116 return ERR_PTR(ret);
6122 * index_cnt is ignored for everything but a dir,
6123 * btrfs_get_inode_index_count has an explanation for the magic
6126 BTRFS_I(inode)->index_cnt = 2;
6127 BTRFS_I(inode)->dir_index = *index;
6128 BTRFS_I(inode)->root = root;
6129 BTRFS_I(inode)->generation = trans->transid;
6130 inode->i_generation = BTRFS_I(inode)->generation;
6133 * We could have gotten an inode number from somebody who was fsynced
6134 * and then removed in this same transaction, so let's just set full
6135 * sync since it will be a full sync anyway and this will blow away the
6136 * old info in the log.
6138 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6140 key[0].objectid = objectid;
6141 key[0].type = BTRFS_INODE_ITEM_KEY;
6144 sizes[0] = sizeof(struct btrfs_inode_item);
6148 * Start new inodes with an inode_ref. This is slightly more
6149 * efficient for small numbers of hard links since they will
6150 * be packed into one item. Extended refs will kick in if we
6151 * add more hard links than can fit in the ref item.
6153 key[1].objectid = objectid;
6154 key[1].type = BTRFS_INODE_REF_KEY;
6155 key[1].offset = ref_objectid;
6157 sizes[1] = name_len + sizeof(*ref);
6160 location = &BTRFS_I(inode)->location;
6161 location->objectid = objectid;
6162 location->offset = 0;
6163 location->type = BTRFS_INODE_ITEM_KEY;
6165 ret = btrfs_insert_inode_locked(inode);
6169 path->leave_spinning = 1;
6170 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6174 inode_init_owner(inode, dir, mode);
6175 inode_set_bytes(inode, 0);
6177 inode->i_mtime = current_time(inode);
6178 inode->i_atime = inode->i_mtime;
6179 inode->i_ctime = inode->i_mtime;
6180 BTRFS_I(inode)->i_otime = inode->i_mtime;
6182 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6183 struct btrfs_inode_item);
6184 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6185 sizeof(*inode_item));
6186 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6189 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6190 struct btrfs_inode_ref);
6191 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6192 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6193 ptr = (unsigned long)(ref + 1);
6194 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6197 btrfs_mark_buffer_dirty(path->nodes[0]);
6198 btrfs_free_path(path);
6200 btrfs_inherit_iflags(inode, dir);
6202 if (S_ISREG(mode)) {
6203 if (btrfs_test_opt(fs_info, NODATASUM))
6204 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6205 if (btrfs_test_opt(fs_info, NODATACOW))
6206 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6207 BTRFS_INODE_NODATASUM;
6210 inode_tree_add(inode);
6212 trace_btrfs_inode_new(inode);
6213 btrfs_set_inode_last_trans(trans, inode);
6215 btrfs_update_root_times(trans, root);
6217 ret = btrfs_inode_inherit_props(trans, inode, dir);
6220 "error inheriting props for ino %llu (root %llu): %d",
6221 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
6226 unlock_new_inode(inode);
6229 BTRFS_I(dir)->index_cnt--;
6230 btrfs_free_path(path);
6232 return ERR_PTR(ret);
6235 static inline u8 btrfs_inode_type(struct inode *inode)
6237 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6241 * utility function to add 'inode' into 'parent_inode' with
6242 * a give name and a given sequence number.
6243 * if 'add_backref' is true, also insert a backref from the
6244 * inode to the parent directory.
6246 int btrfs_add_link(struct btrfs_trans_handle *trans,
6247 struct inode *parent_inode, struct inode *inode,
6248 const char *name, int name_len, int add_backref, u64 index)
6250 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6252 struct btrfs_key key;
6253 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
6254 u64 ino = btrfs_ino(BTRFS_I(inode));
6255 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
6257 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6258 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6261 key.type = BTRFS_INODE_ITEM_KEY;
6265 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6266 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6267 root->root_key.objectid, parent_ino,
6268 index, name, name_len);
6269 } else if (add_backref) {
6270 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6274 /* Nothing to clean up yet */
6278 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6280 btrfs_inode_type(inode), index);
6281 if (ret == -EEXIST || ret == -EOVERFLOW)
6284 btrfs_abort_transaction(trans, ret);
6288 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6290 inode_inc_iversion(parent_inode);
6291 parent_inode->i_mtime = parent_inode->i_ctime =
6292 current_time(parent_inode);
6293 ret = btrfs_update_inode(trans, root, parent_inode);
6295 btrfs_abort_transaction(trans, ret);
6299 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6302 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6303 root->root_key.objectid, parent_ino,
6304 &local_index, name, name_len);
6306 } else if (add_backref) {
6310 err = btrfs_del_inode_ref(trans, root, name, name_len,
6311 ino, parent_ino, &local_index);
6316 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6317 struct inode *dir, struct dentry *dentry,
6318 struct inode *inode, int backref, u64 index)
6320 int err = btrfs_add_link(trans, dir, inode,
6321 dentry->d_name.name, dentry->d_name.len,
6328 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6329 umode_t mode, dev_t rdev)
6331 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6332 struct btrfs_trans_handle *trans;
6333 struct btrfs_root *root = BTRFS_I(dir)->root;
6334 struct inode *inode = NULL;
6341 * 2 for inode item and ref
6343 * 1 for xattr if selinux is on
6345 trans = btrfs_start_transaction(root, 5);
6347 return PTR_ERR(trans);
6349 err = btrfs_find_free_ino(root, &objectid);
6353 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6354 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6356 if (IS_ERR(inode)) {
6357 err = PTR_ERR(inode);
6362 * If the active LSM wants to access the inode during
6363 * d_instantiate it needs these. Smack checks to see
6364 * if the filesystem supports xattrs by looking at the
6367 inode->i_op = &btrfs_special_inode_operations;
6368 init_special_inode(inode, inode->i_mode, rdev);
6370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6372 goto out_unlock_inode;
6374 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6376 goto out_unlock_inode;
6378 btrfs_update_inode(trans, root, inode);
6379 unlock_new_inode(inode);
6380 d_instantiate(dentry, inode);
6384 btrfs_end_transaction(trans);
6385 btrfs_balance_delayed_items(fs_info);
6386 btrfs_btree_balance_dirty(fs_info);
6388 inode_dec_link_count(inode);
6395 unlock_new_inode(inode);
6400 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6401 umode_t mode, bool excl)
6403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6404 struct btrfs_trans_handle *trans;
6405 struct btrfs_root *root = BTRFS_I(dir)->root;
6406 struct inode *inode = NULL;
6407 int drop_inode_on_err = 0;
6413 * 2 for inode item and ref
6415 * 1 for xattr if selinux is on
6417 trans = btrfs_start_transaction(root, 5);
6419 return PTR_ERR(trans);
6421 err = btrfs_find_free_ino(root, &objectid);
6425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6426 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6428 if (IS_ERR(inode)) {
6429 err = PTR_ERR(inode);
6432 drop_inode_on_err = 1;
6434 * If the active LSM wants to access the inode during
6435 * d_instantiate it needs these. Smack checks to see
6436 * if the filesystem supports xattrs by looking at the
6439 inode->i_fop = &btrfs_file_operations;
6440 inode->i_op = &btrfs_file_inode_operations;
6441 inode->i_mapping->a_ops = &btrfs_aops;
6443 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6445 goto out_unlock_inode;
6447 err = btrfs_update_inode(trans, root, inode);
6449 goto out_unlock_inode;
6451 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6453 goto out_unlock_inode;
6455 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6456 unlock_new_inode(inode);
6457 d_instantiate(dentry, inode);
6460 btrfs_end_transaction(trans);
6461 if (err && drop_inode_on_err) {
6462 inode_dec_link_count(inode);
6465 btrfs_balance_delayed_items(fs_info);
6466 btrfs_btree_balance_dirty(fs_info);
6470 unlock_new_inode(inode);
6475 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6476 struct dentry *dentry)
6478 struct btrfs_trans_handle *trans = NULL;
6479 struct btrfs_root *root = BTRFS_I(dir)->root;
6480 struct inode *inode = d_inode(old_dentry);
6481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6486 /* do not allow sys_link's with other subvols of the same device */
6487 if (root->objectid != BTRFS_I(inode)->root->objectid)
6490 if (inode->i_nlink >= BTRFS_LINK_MAX)
6493 err = btrfs_set_inode_index(dir, &index);
6498 * 2 items for inode and inode ref
6499 * 2 items for dir items
6500 * 1 item for parent inode
6502 trans = btrfs_start_transaction(root, 5);
6503 if (IS_ERR(trans)) {
6504 err = PTR_ERR(trans);
6509 /* There are several dir indexes for this inode, clear the cache. */
6510 BTRFS_I(inode)->dir_index = 0ULL;
6512 inode_inc_iversion(inode);
6513 inode->i_ctime = current_time(inode);
6515 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6517 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
6522 struct dentry *parent = dentry->d_parent;
6523 err = btrfs_update_inode(trans, root, inode);
6526 if (inode->i_nlink == 1) {
6528 * If new hard link count is 1, it's a file created
6529 * with open(2) O_TMPFILE flag.
6531 err = btrfs_orphan_del(trans, inode);
6535 d_instantiate(dentry, inode);
6536 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
6539 btrfs_balance_delayed_items(fs_info);
6542 btrfs_end_transaction(trans);
6544 inode_dec_link_count(inode);
6547 btrfs_btree_balance_dirty(fs_info);
6551 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6553 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6554 struct inode *inode = NULL;
6555 struct btrfs_trans_handle *trans;
6556 struct btrfs_root *root = BTRFS_I(dir)->root;
6558 int drop_on_err = 0;
6563 * 2 items for inode and ref
6564 * 2 items for dir items
6565 * 1 for xattr if selinux is on
6567 trans = btrfs_start_transaction(root, 5);
6569 return PTR_ERR(trans);
6571 err = btrfs_find_free_ino(root, &objectid);
6575 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6576 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6577 S_IFDIR | mode, &index);
6578 if (IS_ERR(inode)) {
6579 err = PTR_ERR(inode);
6584 /* these must be set before we unlock the inode */
6585 inode->i_op = &btrfs_dir_inode_operations;
6586 inode->i_fop = &btrfs_dir_file_operations;
6588 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6590 goto out_fail_inode;
6592 btrfs_i_size_write(inode, 0);
6593 err = btrfs_update_inode(trans, root, inode);
6595 goto out_fail_inode;
6597 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6598 dentry->d_name.len, 0, index);
6600 goto out_fail_inode;
6602 d_instantiate(dentry, inode);
6604 * mkdir is special. We're unlocking after we call d_instantiate
6605 * to avoid a race with nfsd calling d_instantiate.
6607 unlock_new_inode(inode);
6611 btrfs_end_transaction(trans);
6613 inode_dec_link_count(inode);
6616 btrfs_balance_delayed_items(fs_info);
6617 btrfs_btree_balance_dirty(fs_info);
6621 unlock_new_inode(inode);
6625 /* Find next extent map of a given extent map, caller needs to ensure locks */
6626 static struct extent_map *next_extent_map(struct extent_map *em)
6628 struct rb_node *next;
6630 next = rb_next(&em->rb_node);
6633 return container_of(next, struct extent_map, rb_node);
6636 static struct extent_map *prev_extent_map(struct extent_map *em)
6638 struct rb_node *prev;
6640 prev = rb_prev(&em->rb_node);
6643 return container_of(prev, struct extent_map, rb_node);
6646 /* helper for btfs_get_extent. Given an existing extent in the tree,
6647 * the existing extent is the nearest extent to map_start,
6648 * and an extent that you want to insert, deal with overlap and insert
6649 * the best fitted new extent into the tree.
6651 static int merge_extent_mapping(struct extent_map_tree *em_tree,
6652 struct extent_map *existing,
6653 struct extent_map *em,
6656 struct extent_map *prev;
6657 struct extent_map *next;
6662 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6664 if (existing->start > map_start) {
6666 prev = prev_extent_map(next);
6669 next = next_extent_map(prev);
6672 start = prev ? extent_map_end(prev) : em->start;
6673 start = max_t(u64, start, em->start);
6674 end = next ? next->start : extent_map_end(em);
6675 end = min_t(u64, end, extent_map_end(em));
6676 start_diff = start - em->start;
6678 em->len = end - start;
6679 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6680 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6681 em->block_start += start_diff;
6682 em->block_len -= start_diff;
6684 return add_extent_mapping(em_tree, em, 0);
6687 static noinline int uncompress_inline(struct btrfs_path *path,
6689 size_t pg_offset, u64 extent_offset,
6690 struct btrfs_file_extent_item *item)
6693 struct extent_buffer *leaf = path->nodes[0];
6696 unsigned long inline_size;
6700 WARN_ON(pg_offset != 0);
6701 compress_type = btrfs_file_extent_compression(leaf, item);
6702 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6703 inline_size = btrfs_file_extent_inline_item_len(leaf,
6704 btrfs_item_nr(path->slots[0]));
6705 tmp = kmalloc(inline_size, GFP_NOFS);
6708 ptr = btrfs_file_extent_inline_start(item);
6710 read_extent_buffer(leaf, tmp, ptr, inline_size);
6712 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6713 ret = btrfs_decompress(compress_type, tmp, page,
6714 extent_offset, inline_size, max_size);
6720 * a bit scary, this does extent mapping from logical file offset to the disk.
6721 * the ugly parts come from merging extents from the disk with the in-ram
6722 * representation. This gets more complex because of the data=ordered code,
6723 * where the in-ram extents might be locked pending data=ordered completion.
6725 * This also copies inline extents directly into the page.
6728 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6729 size_t pg_offset, u64 start, u64 len,
6732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6735 u64 extent_start = 0;
6737 u64 objectid = btrfs_ino(BTRFS_I(inode));
6739 struct btrfs_path *path = NULL;
6740 struct btrfs_root *root = BTRFS_I(inode)->root;
6741 struct btrfs_file_extent_item *item;
6742 struct extent_buffer *leaf;
6743 struct btrfs_key found_key;
6744 struct extent_map *em = NULL;
6745 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6746 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6747 struct btrfs_trans_handle *trans = NULL;
6748 const bool new_inline = !page || create;
6751 read_lock(&em_tree->lock);
6752 em = lookup_extent_mapping(em_tree, start, len);
6754 em->bdev = fs_info->fs_devices->latest_bdev;
6755 read_unlock(&em_tree->lock);
6758 if (em->start > start || em->start + em->len <= start)
6759 free_extent_map(em);
6760 else if (em->block_start == EXTENT_MAP_INLINE && page)
6761 free_extent_map(em);
6765 em = alloc_extent_map();
6770 em->bdev = fs_info->fs_devices->latest_bdev;
6771 em->start = EXTENT_MAP_HOLE;
6772 em->orig_start = EXTENT_MAP_HOLE;
6774 em->block_len = (u64)-1;
6777 path = btrfs_alloc_path();
6783 * Chances are we'll be called again, so go ahead and do
6786 path->reada = READA_FORWARD;
6789 ret = btrfs_lookup_file_extent(trans, root, path,
6790 objectid, start, trans != NULL);
6797 if (path->slots[0] == 0)
6802 leaf = path->nodes[0];
6803 item = btrfs_item_ptr(leaf, path->slots[0],
6804 struct btrfs_file_extent_item);
6805 /* are we inside the extent that was found? */
6806 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6807 found_type = found_key.type;
6808 if (found_key.objectid != objectid ||
6809 found_type != BTRFS_EXTENT_DATA_KEY) {
6811 * If we backup past the first extent we want to move forward
6812 * and see if there is an extent in front of us, otherwise we'll
6813 * say there is a hole for our whole search range which can
6820 found_type = btrfs_file_extent_type(leaf, item);
6821 extent_start = found_key.offset;
6822 if (found_type == BTRFS_FILE_EXTENT_REG ||
6823 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6824 extent_end = extent_start +
6825 btrfs_file_extent_num_bytes(leaf, item);
6826 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6828 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6829 extent_end = ALIGN(extent_start + size,
6830 fs_info->sectorsize);
6833 if (start >= extent_end) {
6835 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6836 ret = btrfs_next_leaf(root, path);
6843 leaf = path->nodes[0];
6845 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6846 if (found_key.objectid != objectid ||
6847 found_key.type != BTRFS_EXTENT_DATA_KEY)
6849 if (start + len <= found_key.offset)
6851 if (start > found_key.offset)
6854 em->orig_start = start;
6855 em->len = found_key.offset - start;
6859 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6861 if (found_type == BTRFS_FILE_EXTENT_REG ||
6862 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6864 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6868 size_t extent_offset;
6874 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6875 extent_offset = page_offset(page) + pg_offset - extent_start;
6876 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6877 size - extent_offset);
6878 em->start = extent_start + extent_offset;
6879 em->len = ALIGN(copy_size, fs_info->sectorsize);
6880 em->orig_block_len = em->len;
6881 em->orig_start = em->start;
6882 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6883 if (create == 0 && !PageUptodate(page)) {
6884 if (btrfs_file_extent_compression(leaf, item) !=
6885 BTRFS_COMPRESS_NONE) {
6886 ret = uncompress_inline(path, page, pg_offset,
6887 extent_offset, item);
6894 read_extent_buffer(leaf, map + pg_offset, ptr,
6896 if (pg_offset + copy_size < PAGE_SIZE) {
6897 memset(map + pg_offset + copy_size, 0,
6898 PAGE_SIZE - pg_offset -
6903 flush_dcache_page(page);
6904 } else if (create && PageUptodate(page)) {
6908 free_extent_map(em);
6911 btrfs_release_path(path);
6912 trans = btrfs_join_transaction(root);
6915 return ERR_CAST(trans);
6919 write_extent_buffer(leaf, map + pg_offset, ptr,
6922 btrfs_mark_buffer_dirty(leaf);
6924 set_extent_uptodate(io_tree, em->start,
6925 extent_map_end(em) - 1, NULL, GFP_NOFS);
6930 em->orig_start = start;
6933 em->block_start = EXTENT_MAP_HOLE;
6934 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
6936 btrfs_release_path(path);
6937 if (em->start > start || extent_map_end(em) <= start) {
6939 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6940 em->start, em->len, start, len);
6946 write_lock(&em_tree->lock);
6947 ret = add_extent_mapping(em_tree, em, 0);
6948 /* it is possible that someone inserted the extent into the tree
6949 * while we had the lock dropped. It is also possible that
6950 * an overlapping map exists in the tree
6952 if (ret == -EEXIST) {
6953 struct extent_map *existing;
6957 existing = search_extent_mapping(em_tree, start, len);
6959 * existing will always be non-NULL, since there must be
6960 * extent causing the -EEXIST.
6962 if (existing->start == em->start &&
6963 extent_map_end(existing) >= extent_map_end(em) &&
6964 em->block_start == existing->block_start) {
6966 * The existing extent map already encompasses the
6967 * entire extent map we tried to add.
6969 free_extent_map(em);
6973 } else if (start >= extent_map_end(existing) ||
6974 start <= existing->start) {
6976 * The existing extent map is the one nearest to
6977 * the [start, start + len) range which overlaps
6979 err = merge_extent_mapping(em_tree, existing,
6981 free_extent_map(existing);
6983 free_extent_map(em);
6987 free_extent_map(em);
6992 write_unlock(&em_tree->lock);
6995 trace_btrfs_get_extent(root, BTRFS_I(inode), em);
6997 btrfs_free_path(path);
6999 ret = btrfs_end_transaction(trans);
7004 free_extent_map(em);
7005 return ERR_PTR(err);
7007 BUG_ON(!em); /* Error is always set */
7011 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7012 size_t pg_offset, u64 start, u64 len,
7015 struct extent_map *em;
7016 struct extent_map *hole_em = NULL;
7017 u64 range_start = start;
7023 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7030 * - a pre-alloc extent,
7031 * there might actually be delalloc bytes behind it.
7033 if (em->block_start != EXTENT_MAP_HOLE &&
7034 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7040 /* check to see if we've wrapped (len == -1 or similar) */
7049 /* ok, we didn't find anything, lets look for delalloc */
7050 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7051 end, len, EXTENT_DELALLOC, 1);
7052 found_end = range_start + found;
7053 if (found_end < range_start)
7054 found_end = (u64)-1;
7057 * we didn't find anything useful, return
7058 * the original results from get_extent()
7060 if (range_start > end || found_end <= start) {
7066 /* adjust the range_start to make sure it doesn't
7067 * go backwards from the start they passed in
7069 range_start = max(start, range_start);
7070 found = found_end - range_start;
7073 u64 hole_start = start;
7076 em = alloc_extent_map();
7082 * when btrfs_get_extent can't find anything it
7083 * returns one huge hole
7085 * make sure what it found really fits our range, and
7086 * adjust to make sure it is based on the start from
7090 u64 calc_end = extent_map_end(hole_em);
7092 if (calc_end <= start || (hole_em->start > end)) {
7093 free_extent_map(hole_em);
7096 hole_start = max(hole_em->start, start);
7097 hole_len = calc_end - hole_start;
7101 if (hole_em && range_start > hole_start) {
7102 /* our hole starts before our delalloc, so we
7103 * have to return just the parts of the hole
7104 * that go until the delalloc starts
7106 em->len = min(hole_len,
7107 range_start - hole_start);
7108 em->start = hole_start;
7109 em->orig_start = hole_start;
7111 * don't adjust block start at all,
7112 * it is fixed at EXTENT_MAP_HOLE
7114 em->block_start = hole_em->block_start;
7115 em->block_len = hole_len;
7116 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7117 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7119 em->start = range_start;
7121 em->orig_start = range_start;
7122 em->block_start = EXTENT_MAP_DELALLOC;
7123 em->block_len = found;
7125 } else if (hole_em) {
7130 free_extent_map(hole_em);
7132 free_extent_map(em);
7133 return ERR_PTR(err);
7138 static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7141 const u64 orig_start,
7142 const u64 block_start,
7143 const u64 block_len,
7144 const u64 orig_block_len,
7145 const u64 ram_bytes,
7148 struct extent_map *em = NULL;
7151 if (type != BTRFS_ORDERED_NOCOW) {
7152 em = create_io_em(inode, start, len, orig_start,
7153 block_start, block_len, orig_block_len,
7155 BTRFS_COMPRESS_NONE, /* compress_type */
7160 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7161 len, block_len, type);
7164 free_extent_map(em);
7165 btrfs_drop_extent_cache(inode, start,
7166 start + len - 1, 0);
7175 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7179 struct btrfs_root *root = BTRFS_I(inode)->root;
7180 struct extent_map *em;
7181 struct btrfs_key ins;
7185 alloc_hint = get_extent_allocation_hint(inode, start, len);
7186 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7187 0, alloc_hint, &ins, 1, 1);
7189 return ERR_PTR(ret);
7191 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7192 ins.objectid, ins.offset, ins.offset,
7193 ins.offset, BTRFS_ORDERED_REGULAR);
7194 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7196 btrfs_free_reserved_extent(fs_info, ins.objectid,
7203 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7204 * block must be cow'd
7206 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7207 u64 *orig_start, u64 *orig_block_len,
7210 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7211 struct btrfs_path *path;
7213 struct extent_buffer *leaf;
7214 struct btrfs_root *root = BTRFS_I(inode)->root;
7215 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7216 struct btrfs_file_extent_item *fi;
7217 struct btrfs_key key;
7224 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7226 path = btrfs_alloc_path();
7230 ret = btrfs_lookup_file_extent(NULL, root, path,
7231 btrfs_ino(BTRFS_I(inode)), offset, 0);
7235 slot = path->slots[0];
7238 /* can't find the item, must cow */
7245 leaf = path->nodes[0];
7246 btrfs_item_key_to_cpu(leaf, &key, slot);
7247 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7248 key.type != BTRFS_EXTENT_DATA_KEY) {
7249 /* not our file or wrong item type, must cow */
7253 if (key.offset > offset) {
7254 /* Wrong offset, must cow */
7258 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7259 found_type = btrfs_file_extent_type(leaf, fi);
7260 if (found_type != BTRFS_FILE_EXTENT_REG &&
7261 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7262 /* not a regular extent, must cow */
7266 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7269 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7270 if (extent_end <= offset)
7273 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7274 if (disk_bytenr == 0)
7277 if (btrfs_file_extent_compression(leaf, fi) ||
7278 btrfs_file_extent_encryption(leaf, fi) ||
7279 btrfs_file_extent_other_encoding(leaf, fi))
7282 backref_offset = btrfs_file_extent_offset(leaf, fi);
7285 *orig_start = key.offset - backref_offset;
7286 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7287 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7290 if (btrfs_extent_readonly(fs_info, disk_bytenr))
7293 num_bytes = min(offset + *len, extent_end) - offset;
7294 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7297 range_end = round_up(offset + num_bytes,
7298 root->fs_info->sectorsize) - 1;
7299 ret = test_range_bit(io_tree, offset, range_end,
7300 EXTENT_DELALLOC, 0, NULL);
7307 btrfs_release_path(path);
7310 * look for other files referencing this extent, if we
7311 * find any we must cow
7314 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
7315 key.offset - backref_offset, disk_bytenr);
7322 * adjust disk_bytenr and num_bytes to cover just the bytes
7323 * in this extent we are about to write. If there
7324 * are any csums in that range we have to cow in order
7325 * to keep the csums correct
7327 disk_bytenr += backref_offset;
7328 disk_bytenr += offset - key.offset;
7329 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7332 * all of the above have passed, it is safe to overwrite this extent
7338 btrfs_free_path(path);
7342 bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7344 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7346 void **pagep = NULL;
7347 struct page *page = NULL;
7351 start_idx = start >> PAGE_SHIFT;
7354 * end is the last byte in the last page. end == start is legal
7356 end_idx = end >> PAGE_SHIFT;
7360 /* Most of the code in this while loop is lifted from
7361 * find_get_page. It's been modified to begin searching from a
7362 * page and return just the first page found in that range. If the
7363 * found idx is less than or equal to the end idx then we know that
7364 * a page exists. If no pages are found or if those pages are
7365 * outside of the range then we're fine (yay!) */
7366 while (page == NULL &&
7367 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7368 page = radix_tree_deref_slot(pagep);
7369 if (unlikely(!page))
7372 if (radix_tree_exception(page)) {
7373 if (radix_tree_deref_retry(page)) {
7378 * Otherwise, shmem/tmpfs must be storing a swap entry
7379 * here as an exceptional entry: so return it without
7380 * attempting to raise page count.
7383 break; /* TODO: Is this relevant for this use case? */
7386 if (!page_cache_get_speculative(page)) {
7392 * Has the page moved?
7393 * This is part of the lockless pagecache protocol. See
7394 * include/linux/pagemap.h for details.
7396 if (unlikely(page != *pagep)) {
7403 if (page->index <= end_idx)
7412 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7413 struct extent_state **cached_state, int writing)
7415 struct btrfs_ordered_extent *ordered;
7419 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7422 * We're concerned with the entire range that we're going to be
7423 * doing DIO to, so we need to make sure there's no ordered
7424 * extents in this range.
7426 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7427 lockend - lockstart + 1);
7430 * We need to make sure there are no buffered pages in this
7431 * range either, we could have raced between the invalidate in
7432 * generic_file_direct_write and locking the extent. The
7433 * invalidate needs to happen so that reads after a write do not
7438 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
7441 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7442 cached_state, GFP_NOFS);
7446 * If we are doing a DIO read and the ordered extent we
7447 * found is for a buffered write, we can not wait for it
7448 * to complete and retry, because if we do so we can
7449 * deadlock with concurrent buffered writes on page
7450 * locks. This happens only if our DIO read covers more
7451 * than one extent map, if at this point has already
7452 * created an ordered extent for a previous extent map
7453 * and locked its range in the inode's io tree, and a
7454 * concurrent write against that previous extent map's
7455 * range and this range started (we unlock the ranges
7456 * in the io tree only when the bios complete and
7457 * buffered writes always lock pages before attempting
7458 * to lock range in the io tree).
7461 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7462 btrfs_start_ordered_extent(inode, ordered, 1);
7465 btrfs_put_ordered_extent(ordered);
7468 * We could trigger writeback for this range (and wait
7469 * for it to complete) and then invalidate the pages for
7470 * this range (through invalidate_inode_pages2_range()),
7471 * but that can lead us to a deadlock with a concurrent
7472 * call to readpages() (a buffered read or a defrag call
7473 * triggered a readahead) on a page lock due to an
7474 * ordered dio extent we created before but did not have
7475 * yet a corresponding bio submitted (whence it can not
7476 * complete), which makes readpages() wait for that
7477 * ordered extent to complete while holding a lock on
7492 /* The callers of this must take lock_extent() */
7493 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7494 u64 orig_start, u64 block_start,
7495 u64 block_len, u64 orig_block_len,
7496 u64 ram_bytes, int compress_type,
7499 struct extent_map_tree *em_tree;
7500 struct extent_map *em;
7501 struct btrfs_root *root = BTRFS_I(inode)->root;
7504 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7505 type == BTRFS_ORDERED_COMPRESSED ||
7506 type == BTRFS_ORDERED_NOCOW ||
7507 type == BTRFS_ORDERED_REGULAR);
7509 em_tree = &BTRFS_I(inode)->extent_tree;
7510 em = alloc_extent_map();
7512 return ERR_PTR(-ENOMEM);
7515 em->orig_start = orig_start;
7517 em->block_len = block_len;
7518 em->block_start = block_start;
7519 em->bdev = root->fs_info->fs_devices->latest_bdev;
7520 em->orig_block_len = orig_block_len;
7521 em->ram_bytes = ram_bytes;
7522 em->generation = -1;
7523 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7524 if (type == BTRFS_ORDERED_PREALLOC) {
7525 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7526 } else if (type == BTRFS_ORDERED_COMPRESSED) {
7527 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7528 em->compress_type = compress_type;
7532 btrfs_drop_extent_cache(inode, em->start,
7533 em->start + em->len - 1, 0);
7534 write_lock(&em_tree->lock);
7535 ret = add_extent_mapping(em_tree, em, 1);
7536 write_unlock(&em_tree->lock);
7538 * The caller has taken lock_extent(), who could race with us
7541 } while (ret == -EEXIST);
7544 free_extent_map(em);
7545 return ERR_PTR(ret);
7548 /* em got 2 refs now, callers needs to do free_extent_map once. */
7552 static void adjust_dio_outstanding_extents(struct inode *inode,
7553 struct btrfs_dio_data *dio_data,
7556 unsigned num_extents = count_max_extents(len);
7559 * If we have an outstanding_extents count still set then we're
7560 * within our reservation, otherwise we need to adjust our inode
7561 * counter appropriately.
7563 if (dio_data->outstanding_extents >= num_extents) {
7564 dio_data->outstanding_extents -= num_extents;
7567 * If dio write length has been split due to no large enough
7568 * contiguous space, we need to compensate our inode counter
7571 u64 num_needed = num_extents - dio_data->outstanding_extents;
7573 spin_lock(&BTRFS_I(inode)->lock);
7574 BTRFS_I(inode)->outstanding_extents += num_needed;
7575 spin_unlock(&BTRFS_I(inode)->lock);
7579 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7580 struct buffer_head *bh_result, int create)
7582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7583 struct extent_map *em;
7584 struct extent_state *cached_state = NULL;
7585 struct btrfs_dio_data *dio_data = NULL;
7586 u64 start = iblock << inode->i_blkbits;
7587 u64 lockstart, lockend;
7588 u64 len = bh_result->b_size;
7589 int unlock_bits = EXTENT_LOCKED;
7593 unlock_bits |= EXTENT_DIRTY;
7595 len = min_t(u64, len, fs_info->sectorsize);
7598 lockend = start + len - 1;
7600 if (current->journal_info) {
7602 * Need to pull our outstanding extents and set journal_info to NULL so
7603 * that anything that needs to check if there's a transaction doesn't get
7606 dio_data = current->journal_info;
7607 current->journal_info = NULL;
7611 * If this errors out it's because we couldn't invalidate pagecache for
7612 * this range and we need to fallback to buffered.
7614 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7620 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7627 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7628 * io. INLINE is special, and we could probably kludge it in here, but
7629 * it's still buffered so for safety lets just fall back to the generic
7632 * For COMPRESSED we _have_ to read the entire extent in so we can
7633 * decompress it, so there will be buffering required no matter what we
7634 * do, so go ahead and fallback to buffered.
7636 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7637 * to buffered IO. Don't blame me, this is the price we pay for using
7640 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7641 em->block_start == EXTENT_MAP_INLINE) {
7642 free_extent_map(em);
7647 /* Just a good old fashioned hole, return */
7648 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7649 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7650 free_extent_map(em);
7655 * We don't allocate a new extent in the following cases
7657 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7659 * 2) The extent is marked as PREALLOC. We're good to go here and can
7660 * just use the extent.
7664 len = min(len, em->len - (start - em->start));
7665 lockstart = start + len;
7669 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7670 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7671 em->block_start != EXTENT_MAP_HOLE)) {
7673 u64 block_start, orig_start, orig_block_len, ram_bytes;
7675 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7676 type = BTRFS_ORDERED_PREALLOC;
7678 type = BTRFS_ORDERED_NOCOW;
7679 len = min(len, em->len - (start - em->start));
7680 block_start = em->block_start + (start - em->start);
7682 if (can_nocow_extent(inode, start, &len, &orig_start,
7683 &orig_block_len, &ram_bytes) == 1 &&
7684 btrfs_inc_nocow_writers(fs_info, block_start)) {
7685 struct extent_map *em2;
7687 em2 = btrfs_create_dio_extent(inode, start, len,
7688 orig_start, block_start,
7689 len, orig_block_len,
7691 btrfs_dec_nocow_writers(fs_info, block_start);
7692 if (type == BTRFS_ORDERED_PREALLOC) {
7693 free_extent_map(em);
7696 if (em2 && IS_ERR(em2)) {
7701 * For inode marked NODATACOW or extent marked PREALLOC,
7702 * use the existing or preallocated extent, so does not
7703 * need to adjust btrfs_space_info's bytes_may_use.
7705 btrfs_free_reserved_data_space_noquota(inode,
7712 * this will cow the extent, reset the len in case we changed
7715 len = bh_result->b_size;
7716 free_extent_map(em);
7717 em = btrfs_new_extent_direct(inode, start, len);
7722 len = min(len, em->len - (start - em->start));
7724 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7726 bh_result->b_size = len;
7727 bh_result->b_bdev = em->bdev;
7728 set_buffer_mapped(bh_result);
7730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7731 set_buffer_new(bh_result);
7734 * Need to update the i_size under the extent lock so buffered
7735 * readers will get the updated i_size when we unlock.
7737 if (!dio_data->overwrite && start + len > i_size_read(inode))
7738 i_size_write(inode, start + len);
7740 adjust_dio_outstanding_extents(inode, dio_data, len);
7741 WARN_ON(dio_data->reserve < len);
7742 dio_data->reserve -= len;
7743 dio_data->unsubmitted_oe_range_end = start + len;
7744 current->journal_info = dio_data;
7748 * In the case of write we need to clear and unlock the entire range,
7749 * in the case of read we need to unlock only the end area that we
7750 * aren't using if there is any left over space.
7752 if (lockstart < lockend) {
7753 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7754 lockend, unlock_bits, 1, 0,
7755 &cached_state, GFP_NOFS);
7757 free_extent_state(cached_state);
7760 free_extent_map(em);
7765 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7766 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7769 current->journal_info = dio_data;
7771 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7772 * write less data then expected, so that we don't underflow our inode's
7773 * outstanding extents counter.
7775 if (create && dio_data)
7776 adjust_dio_outstanding_extents(inode, dio_data, len);
7781 static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7784 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7787 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7791 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
7795 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
7801 static int btrfs_check_dio_repairable(struct inode *inode,
7802 struct bio *failed_bio,
7803 struct io_failure_record *failrec,
7806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7809 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
7810 if (num_copies == 1) {
7812 * we only have a single copy of the data, so don't bother with
7813 * all the retry and error correction code that follows. no
7814 * matter what the error is, it is very likely to persist.
7816 btrfs_debug(fs_info,
7817 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7818 num_copies, failrec->this_mirror, failed_mirror);
7822 failrec->failed_mirror = failed_mirror;
7823 failrec->this_mirror++;
7824 if (failrec->this_mirror == failed_mirror)
7825 failrec->this_mirror++;
7827 if (failrec->this_mirror > num_copies) {
7828 btrfs_debug(fs_info,
7829 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7830 num_copies, failrec->this_mirror, failed_mirror);
7837 static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7838 struct page *page, unsigned int pgoff,
7839 u64 start, u64 end, int failed_mirror,
7840 bio_end_io_t *repair_endio, void *repair_arg)
7842 struct io_failure_record *failrec;
7848 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
7850 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7854 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7857 free_io_failure(inode, failrec);
7861 if ((failed_bio->bi_vcnt > 1)
7862 || (failed_bio->bi_io_vec->bv_len
7863 > btrfs_inode_sectorsize(inode)))
7864 read_mode |= REQ_FAILFAST_DEV;
7866 isector = start - btrfs_io_bio(failed_bio)->logical;
7867 isector >>= inode->i_sb->s_blocksize_bits;
7868 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7869 pgoff, isector, repair_endio, repair_arg);
7871 free_io_failure(inode, failrec);
7874 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
7876 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7877 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7878 read_mode, failrec->this_mirror, failrec->in_validation);
7880 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7882 free_io_failure(inode, failrec);
7889 struct btrfs_retry_complete {
7890 struct completion done;
7891 struct inode *inode;
7896 static void btrfs_retry_endio_nocsum(struct bio *bio)
7898 struct btrfs_retry_complete *done = bio->bi_private;
7899 struct inode *inode;
7900 struct bio_vec *bvec;
7906 ASSERT(bio->bi_vcnt == 1);
7907 inode = bio->bi_io_vec->bv_page->mapping->host;
7908 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
7911 bio_for_each_segment_all(bvec, bio, i)
7912 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7914 complete(&done->done);
7918 static int __btrfs_correct_data_nocsum(struct inode *inode,
7919 struct btrfs_io_bio *io_bio)
7921 struct btrfs_fs_info *fs_info;
7922 struct bio_vec *bvec;
7923 struct btrfs_retry_complete done;
7931 fs_info = BTRFS_I(inode)->root->fs_info;
7932 sectorsize = fs_info->sectorsize;
7934 start = io_bio->logical;
7937 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7938 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7939 pgoff = bvec->bv_offset;
7941 next_block_or_try_again:
7944 init_completion(&done.done);
7946 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7947 pgoff, start, start + sectorsize - 1,
7949 btrfs_retry_endio_nocsum, &done);
7953 wait_for_completion(&done.done);
7955 if (!done.uptodate) {
7956 /* We might have another mirror, so try again */
7957 goto next_block_or_try_again;
7960 start += sectorsize;
7963 pgoff += sectorsize;
7964 goto next_block_or_try_again;
7971 static void btrfs_retry_endio(struct bio *bio)
7973 struct btrfs_retry_complete *done = bio->bi_private;
7974 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7975 struct inode *inode;
7976 struct bio_vec *bvec;
7987 start = done->start;
7989 ASSERT(bio->bi_vcnt == 1);
7990 inode = bio->bi_io_vec->bv_page->mapping->host;
7991 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
7993 bio_for_each_segment_all(bvec, bio, i) {
7994 ret = __readpage_endio_check(done->inode, io_bio, i,
7995 bvec->bv_page, bvec->bv_offset,
7996 done->start, bvec->bv_len);
7998 clean_io_failure(done->inode, done->start,
7999 bvec->bv_page, bvec->bv_offset);
8004 done->uptodate = uptodate;
8006 complete(&done->done);
8010 static int __btrfs_subio_endio_read(struct inode *inode,
8011 struct btrfs_io_bio *io_bio, int err)
8013 struct btrfs_fs_info *fs_info;
8014 struct bio_vec *bvec;
8015 struct btrfs_retry_complete done;
8025 fs_info = BTRFS_I(inode)->root->fs_info;
8026 sectorsize = fs_info->sectorsize;
8029 start = io_bio->logical;
8032 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
8033 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8035 pgoff = bvec->bv_offset;
8037 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8038 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8039 bvec->bv_page, pgoff, start,
8046 init_completion(&done.done);
8048 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8049 pgoff, start, start + sectorsize - 1,
8051 btrfs_retry_endio, &done);
8057 wait_for_completion(&done.done);
8059 if (!done.uptodate) {
8060 /* We might have another mirror, so try again */
8064 offset += sectorsize;
8065 start += sectorsize;
8070 pgoff += sectorsize;
8078 static int btrfs_subio_endio_read(struct inode *inode,
8079 struct btrfs_io_bio *io_bio, int err)
8081 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8085 return __btrfs_correct_data_nocsum(inode, io_bio);
8089 return __btrfs_subio_endio_read(inode, io_bio, err);
8093 static void btrfs_endio_direct_read(struct bio *bio)
8095 struct btrfs_dio_private *dip = bio->bi_private;
8096 struct inode *inode = dip->inode;
8097 struct bio *dio_bio;
8098 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8099 int err = bio->bi_error;
8101 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8102 err = btrfs_subio_endio_read(inode, io_bio, err);
8104 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
8105 dip->logical_offset + dip->bytes - 1);
8106 dio_bio = dip->dio_bio;
8110 dio_bio->bi_error = bio->bi_error;
8111 dio_end_io(dio_bio, bio->bi_error);
8114 io_bio->end_io(io_bio, err);
8118 static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8124 struct btrfs_ordered_extent *ordered = NULL;
8125 u64 ordered_offset = offset;
8126 u64 ordered_bytes = bytes;
8130 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8137 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8138 finish_ordered_fn, NULL, NULL);
8139 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
8142 * our bio might span multiple ordered extents. If we haven't
8143 * completed the accounting for the whole dio, go back and try again
8145 if (ordered_offset < offset + bytes) {
8146 ordered_bytes = offset + bytes - ordered_offset;
8152 static void btrfs_endio_direct_write(struct bio *bio)
8154 struct btrfs_dio_private *dip = bio->bi_private;
8155 struct bio *dio_bio = dip->dio_bio;
8157 btrfs_endio_direct_write_update_ordered(dip->inode,
8158 dip->logical_offset,
8164 dio_bio->bi_error = bio->bi_error;
8165 dio_end_io(dio_bio, bio->bi_error);
8169 static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
8170 struct bio *bio, int mirror_num,
8171 unsigned long bio_flags, u64 offset)
8174 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
8175 BUG_ON(ret); /* -ENOMEM */
8179 static void btrfs_end_dio_bio(struct bio *bio)
8181 struct btrfs_dio_private *dip = bio->bi_private;
8182 int err = bio->bi_error;
8185 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8186 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8187 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8189 (unsigned long long)bio->bi_iter.bi_sector,
8190 bio->bi_iter.bi_size, err);
8192 if (dip->subio_endio)
8193 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
8199 * before atomic variable goto zero, we must make sure
8200 * dip->errors is perceived to be set.
8202 smp_mb__before_atomic();
8205 /* if there are more bios still pending for this dio, just exit */
8206 if (!atomic_dec_and_test(&dip->pending_bios))
8210 bio_io_error(dip->orig_bio);
8212 dip->dio_bio->bi_error = 0;
8213 bio_endio(dip->orig_bio);
8219 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8220 u64 first_sector, gfp_t gfp_flags)
8223 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
8225 bio_associate_current(bio);
8229 static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8230 struct btrfs_dio_private *dip,
8234 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8235 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8239 * We load all the csum data we need when we submit
8240 * the first bio to reduce the csum tree search and
8243 if (dip->logical_offset == file_offset) {
8244 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
8250 if (bio == dip->orig_bio)
8253 file_offset -= dip->logical_offset;
8254 file_offset >>= inode->i_sb->s_blocksize_bits;
8255 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8260 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8261 u64 file_offset, int skip_sum,
8264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8265 struct btrfs_dio_private *dip = bio->bi_private;
8266 bool write = bio_op(bio) == REQ_OP_WRITE;
8270 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8275 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8283 if (write && async_submit) {
8284 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8286 __btrfs_submit_bio_start_direct_io,
8287 __btrfs_submit_bio_done);
8291 * If we aren't doing async submit, calculate the csum of the
8294 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
8298 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
8304 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
8310 static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8313 struct inode *inode = dip->inode;
8314 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8315 struct btrfs_root *root = BTRFS_I(inode)->root;
8317 struct bio *orig_bio = dip->orig_bio;
8318 struct bio_vec *bvec;
8319 u64 start_sector = orig_bio->bi_iter.bi_sector;
8320 u64 file_offset = dip->logical_offset;
8323 u32 blocksize = fs_info->sectorsize;
8324 int async_submit = 0;
8329 map_length = orig_bio->bi_iter.bi_size;
8330 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8331 &map_length, NULL, 0);
8335 if (map_length >= orig_bio->bi_iter.bi_size) {
8337 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
8341 /* async crcs make it difficult to collect full stripe writes. */
8342 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
8347 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8351 bio->bi_opf = orig_bio->bi_opf;
8352 bio->bi_private = dip;
8353 bio->bi_end_io = btrfs_end_dio_bio;
8354 btrfs_io_bio(bio)->logical = file_offset;
8355 atomic_inc(&dip->pending_bios);
8357 bio_for_each_segment_all(bvec, orig_bio, j) {
8358 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8361 if (unlikely(map_length < submit_len + blocksize ||
8362 bio_add_page(bio, bvec->bv_page, blocksize,
8363 bvec->bv_offset + (i * blocksize)) < blocksize)) {
8365 * inc the count before we submit the bio so
8366 * we know the end IO handler won't happen before
8367 * we inc the count. Otherwise, the dip might get freed
8368 * before we're done setting it up
8370 atomic_inc(&dip->pending_bios);
8371 ret = __btrfs_submit_dio_bio(bio, inode,
8372 file_offset, skip_sum,
8376 atomic_dec(&dip->pending_bios);
8380 start_sector += submit_len >> 9;
8381 file_offset += submit_len;
8385 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8386 start_sector, GFP_NOFS);
8389 bio->bi_opf = orig_bio->bi_opf;
8390 bio->bi_private = dip;
8391 bio->bi_end_io = btrfs_end_dio_bio;
8392 btrfs_io_bio(bio)->logical = file_offset;
8394 map_length = orig_bio->bi_iter.bi_size;
8395 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8397 &map_length, NULL, 0);
8405 submit_len += blocksize;
8414 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8423 * before atomic variable goto zero, we must
8424 * make sure dip->errors is perceived to be set.
8426 smp_mb__before_atomic();
8427 if (atomic_dec_and_test(&dip->pending_bios))
8428 bio_io_error(dip->orig_bio);
8430 /* bio_end_io() will handle error, so we needn't return it */
8434 static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8437 struct btrfs_dio_private *dip = NULL;
8438 struct bio *io_bio = NULL;
8439 struct btrfs_io_bio *btrfs_bio;
8441 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8444 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8446 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
8452 dip = kzalloc(sizeof(*dip), GFP_NOFS);
8458 dip->private = dio_bio->bi_private;
8460 dip->logical_offset = file_offset;
8461 dip->bytes = dio_bio->bi_iter.bi_size;
8462 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8463 io_bio->bi_private = dip;
8464 dip->orig_bio = io_bio;
8465 dip->dio_bio = dio_bio;
8466 atomic_set(&dip->pending_bios, 0);
8467 btrfs_bio = btrfs_io_bio(io_bio);
8468 btrfs_bio->logical = file_offset;
8471 io_bio->bi_end_io = btrfs_endio_direct_write;
8473 io_bio->bi_end_io = btrfs_endio_direct_read;
8474 dip->subio_endio = btrfs_subio_endio_read;
8478 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8479 * even if we fail to submit a bio, because in such case we do the
8480 * corresponding error handling below and it must not be done a second
8481 * time by btrfs_direct_IO().
8484 struct btrfs_dio_data *dio_data = current->journal_info;
8486 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8488 dio_data->unsubmitted_oe_range_start =
8489 dio_data->unsubmitted_oe_range_end;
8492 ret = btrfs_submit_direct_hook(dip, skip_sum);
8496 if (btrfs_bio->end_io)
8497 btrfs_bio->end_io(btrfs_bio, ret);
8501 * If we arrived here it means either we failed to submit the dip
8502 * or we either failed to clone the dio_bio or failed to allocate the
8503 * dip. If we cloned the dio_bio and allocated the dip, we can just
8504 * call bio_endio against our io_bio so that we get proper resource
8505 * cleanup if we fail to submit the dip, otherwise, we must do the
8506 * same as btrfs_endio_direct_[write|read] because we can't call these
8507 * callbacks - they require an allocated dip and a clone of dio_bio.
8509 if (io_bio && dip) {
8510 io_bio->bi_error = -EIO;
8513 * The end io callbacks free our dip, do the final put on io_bio
8514 * and all the cleanup and final put for dio_bio (through
8521 btrfs_endio_direct_write_update_ordered(inode,
8523 dio_bio->bi_iter.bi_size,
8526 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8527 file_offset + dio_bio->bi_iter.bi_size - 1);
8529 dio_bio->bi_error = -EIO;
8531 * Releases and cleans up our dio_bio, no need to bio_put()
8532 * nor bio_endio()/bio_io_error() against dio_bio.
8534 dio_end_io(dio_bio, ret);
8541 static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8543 const struct iov_iter *iter, loff_t offset)
8547 unsigned int blocksize_mask = fs_info->sectorsize - 1;
8548 ssize_t retval = -EINVAL;
8550 if (offset & blocksize_mask)
8553 if (iov_iter_alignment(iter) & blocksize_mask)
8556 /* If this is a write we don't need to check anymore */
8557 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
8560 * Check to make sure we don't have duplicate iov_base's in this
8561 * iovec, if so return EINVAL, otherwise we'll get csum errors
8562 * when reading back.
8564 for (seg = 0; seg < iter->nr_segs; seg++) {
8565 for (i = seg + 1; i < iter->nr_segs; i++) {
8566 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8575 static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8577 struct file *file = iocb->ki_filp;
8578 struct inode *inode = file->f_mapping->host;
8579 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8580 struct btrfs_dio_data dio_data = { 0 };
8581 loff_t offset = iocb->ki_pos;
8585 bool relock = false;
8588 if (check_direct_IO(fs_info, iocb, iter, offset))
8591 inode_dio_begin(inode);
8592 smp_mb__after_atomic();
8595 * The generic stuff only does filemap_write_and_wait_range, which
8596 * isn't enough if we've written compressed pages to this area, so
8597 * we need to flush the dirty pages again to make absolutely sure
8598 * that any outstanding dirty pages are on disk.
8600 count = iov_iter_count(iter);
8601 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8602 &BTRFS_I(inode)->runtime_flags))
8603 filemap_fdatawrite_range(inode->i_mapping, offset,
8604 offset + count - 1);
8606 if (iov_iter_rw(iter) == WRITE) {
8608 * If the write DIO is beyond the EOF, we need update
8609 * the isize, but it is protected by i_mutex. So we can
8610 * not unlock the i_mutex at this case.
8612 if (offset + count <= inode->i_size) {
8613 dio_data.overwrite = 1;
8614 inode_unlock(inode);
8617 ret = btrfs_delalloc_reserve_space(inode, offset, count);
8620 dio_data.outstanding_extents = count_max_extents(count);
8623 * We need to know how many extents we reserved so that we can
8624 * do the accounting properly if we go over the number we
8625 * originally calculated. Abuse current->journal_info for this.
8627 dio_data.reserve = round_up(count,
8628 fs_info->sectorsize);
8629 dio_data.unsubmitted_oe_range_start = (u64)offset;
8630 dio_data.unsubmitted_oe_range_end = (u64)offset;
8631 current->journal_info = &dio_data;
8632 down_read(&BTRFS_I(inode)->dio_sem);
8633 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8634 &BTRFS_I(inode)->runtime_flags)) {
8635 inode_dio_end(inode);
8636 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8640 ret = __blockdev_direct_IO(iocb, inode,
8641 fs_info->fs_devices->latest_bdev,
8642 iter, btrfs_get_blocks_direct, NULL,
8643 btrfs_submit_direct, flags);
8644 if (iov_iter_rw(iter) == WRITE) {
8645 up_read(&BTRFS_I(inode)->dio_sem);
8646 current->journal_info = NULL;
8647 if (ret < 0 && ret != -EIOCBQUEUED) {
8648 if (dio_data.reserve)
8649 btrfs_delalloc_release_space(inode, offset,
8652 * On error we might have left some ordered extents
8653 * without submitting corresponding bios for them, so
8654 * cleanup them up to avoid other tasks getting them
8655 * and waiting for them to complete forever.
8657 if (dio_data.unsubmitted_oe_range_start <
8658 dio_data.unsubmitted_oe_range_end)
8659 btrfs_endio_direct_write_update_ordered(inode,
8660 dio_data.unsubmitted_oe_range_start,
8661 dio_data.unsubmitted_oe_range_end -
8662 dio_data.unsubmitted_oe_range_start,
8664 } else if (ret >= 0 && (size_t)ret < count)
8665 btrfs_delalloc_release_space(inode, offset,
8666 count - (size_t)ret);
8670 inode_dio_end(inode);
8677 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8679 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8680 __u64 start, __u64 len)
8684 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8688 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
8691 int btrfs_readpage(struct file *file, struct page *page)
8693 struct extent_io_tree *tree;
8694 tree = &BTRFS_I(page->mapping->host)->io_tree;
8695 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
8698 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8700 struct extent_io_tree *tree;
8701 struct inode *inode = page->mapping->host;
8704 if (current->flags & PF_MEMALLOC) {
8705 redirty_page_for_writepage(wbc, page);
8711 * If we are under memory pressure we will call this directly from the
8712 * VM, we need to make sure we have the inode referenced for the ordered
8713 * extent. If not just return like we didn't do anything.
8715 if (!igrab(inode)) {
8716 redirty_page_for_writepage(wbc, page);
8717 return AOP_WRITEPAGE_ACTIVATE;
8719 tree = &BTRFS_I(page->mapping->host)->io_tree;
8720 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8721 btrfs_add_delayed_iput(inode);
8725 static int btrfs_writepages(struct address_space *mapping,
8726 struct writeback_control *wbc)
8728 struct extent_io_tree *tree;
8730 tree = &BTRFS_I(mapping->host)->io_tree;
8731 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8735 btrfs_readpages(struct file *file, struct address_space *mapping,
8736 struct list_head *pages, unsigned nr_pages)
8738 struct extent_io_tree *tree;
8739 tree = &BTRFS_I(mapping->host)->io_tree;
8740 return extent_readpages(tree, mapping, pages, nr_pages,
8743 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8745 struct extent_io_tree *tree;
8746 struct extent_map_tree *map;
8749 tree = &BTRFS_I(page->mapping->host)->io_tree;
8750 map = &BTRFS_I(page->mapping->host)->extent_tree;
8751 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
8753 ClearPagePrivate(page);
8754 set_page_private(page, 0);
8760 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8762 if (PageWriteback(page) || PageDirty(page))
8764 return __btrfs_releasepage(page, gfp_flags);
8767 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8768 unsigned int length)
8770 struct inode *inode = page->mapping->host;
8771 struct extent_io_tree *tree;
8772 struct btrfs_ordered_extent *ordered;
8773 struct extent_state *cached_state = NULL;
8774 u64 page_start = page_offset(page);
8775 u64 page_end = page_start + PAGE_SIZE - 1;
8778 int inode_evicting = inode->i_state & I_FREEING;
8781 * we have the page locked, so new writeback can't start,
8782 * and the dirty bit won't be cleared while we are here.
8784 * Wait for IO on this page so that we can safely clear
8785 * the PagePrivate2 bit and do ordered accounting
8787 wait_on_page_writeback(page);
8789 tree = &BTRFS_I(inode)->io_tree;
8791 btrfs_releasepage(page, GFP_NOFS);
8795 if (!inode_evicting)
8796 lock_extent_bits(tree, page_start, page_end, &cached_state);
8799 ordered = btrfs_lookup_ordered_range(inode, start,
8800 page_end - start + 1);
8802 end = min(page_end, ordered->file_offset + ordered->len - 1);
8804 * IO on this page will never be started, so we need
8805 * to account for any ordered extents now
8807 if (!inode_evicting)
8808 clear_extent_bit(tree, start, end,
8809 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8811 EXTENT_DEFRAG, 1, 0, &cached_state,
8814 * whoever cleared the private bit is responsible
8815 * for the finish_ordered_io
8817 if (TestClearPagePrivate2(page)) {
8818 struct btrfs_ordered_inode_tree *tree;
8821 tree = &BTRFS_I(inode)->ordered_tree;
8823 spin_lock_irq(&tree->lock);
8824 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8825 new_len = start - ordered->file_offset;
8826 if (new_len < ordered->truncated_len)
8827 ordered->truncated_len = new_len;
8828 spin_unlock_irq(&tree->lock);
8830 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8832 end - start + 1, 1))
8833 btrfs_finish_ordered_io(ordered);
8835 btrfs_put_ordered_extent(ordered);
8836 if (!inode_evicting) {
8837 cached_state = NULL;
8838 lock_extent_bits(tree, start, end,
8843 if (start < page_end)
8848 * Qgroup reserved space handler
8849 * Page here will be either
8850 * 1) Already written to disk
8851 * In this case, its reserved space is released from data rsv map
8852 * and will be freed by delayed_ref handler finally.
8853 * So even we call qgroup_free_data(), it won't decrease reserved
8855 * 2) Not written to disk
8856 * This means the reserved space should be freed here. However,
8857 * if a truncate invalidates the page (by clearing PageDirty)
8858 * and the page is accounted for while allocating extent
8859 * in btrfs_check_data_free_space() we let delayed_ref to
8860 * free the entire extent.
8862 if (PageDirty(page))
8863 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
8864 if (!inode_evicting) {
8865 clear_extent_bit(tree, page_start, page_end,
8866 EXTENT_LOCKED | EXTENT_DIRTY |
8867 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8868 EXTENT_DEFRAG, 1, 1,
8869 &cached_state, GFP_NOFS);
8871 __btrfs_releasepage(page, GFP_NOFS);
8874 ClearPageChecked(page);
8875 if (PagePrivate(page)) {
8876 ClearPagePrivate(page);
8877 set_page_private(page, 0);
8883 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8884 * called from a page fault handler when a page is first dirtied. Hence we must
8885 * be careful to check for EOF conditions here. We set the page up correctly
8886 * for a written page which means we get ENOSPC checking when writing into
8887 * holes and correct delalloc and unwritten extent mapping on filesystems that
8888 * support these features.
8890 * We are not allowed to take the i_mutex here so we have to play games to
8891 * protect against truncate races as the page could now be beyond EOF. Because
8892 * vmtruncate() writes the inode size before removing pages, once we have the
8893 * page lock we can determine safely if the page is beyond EOF. If it is not
8894 * beyond EOF, then the page is guaranteed safe against truncation until we
8897 int btrfs_page_mkwrite(struct vm_fault *vmf)
8899 struct page *page = vmf->page;
8900 struct inode *inode = file_inode(vmf->vma->vm_file);
8901 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8903 struct btrfs_ordered_extent *ordered;
8904 struct extent_state *cached_state = NULL;
8906 unsigned long zero_start;
8915 reserved_space = PAGE_SIZE;
8917 sb_start_pagefault(inode->i_sb);
8918 page_start = page_offset(page);
8919 page_end = page_start + PAGE_SIZE - 1;
8923 * Reserving delalloc space after obtaining the page lock can lead to
8924 * deadlock. For example, if a dirty page is locked by this function
8925 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8926 * dirty page write out, then the btrfs_writepage() function could
8927 * end up waiting indefinitely to get a lock on the page currently
8928 * being processed by btrfs_page_mkwrite() function.
8930 ret = btrfs_delalloc_reserve_space(inode, page_start,
8933 ret = file_update_time(vmf->vma->vm_file);
8939 else /* -ENOSPC, -EIO, etc */
8940 ret = VM_FAULT_SIGBUS;
8946 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
8949 size = i_size_read(inode);
8951 if ((page->mapping != inode->i_mapping) ||
8952 (page_start >= size)) {
8953 /* page got truncated out from underneath us */
8956 wait_on_page_writeback(page);
8958 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
8959 set_page_extent_mapped(page);
8962 * we can't set the delalloc bits if there are pending ordered
8963 * extents. Drop our locks and wait for them to finish
8965 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
8967 unlock_extent_cached(io_tree, page_start, page_end,
8968 &cached_state, GFP_NOFS);
8970 btrfs_start_ordered_extent(inode, ordered, 1);
8971 btrfs_put_ordered_extent(ordered);
8975 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
8976 reserved_space = round_up(size - page_start,
8977 fs_info->sectorsize);
8978 if (reserved_space < PAGE_SIZE) {
8979 end = page_start + reserved_space - 1;
8980 spin_lock(&BTRFS_I(inode)->lock);
8981 BTRFS_I(inode)->outstanding_extents++;
8982 spin_unlock(&BTRFS_I(inode)->lock);
8983 btrfs_delalloc_release_space(inode, page_start,
8984 PAGE_SIZE - reserved_space);
8989 * page_mkwrite gets called when the page is firstly dirtied after it's
8990 * faulted in, but write(2) could also dirty a page and set delalloc
8991 * bits, thus in this case for space account reason, we still need to
8992 * clear any delalloc bits within this page range since we have to
8993 * reserve data&meta space before lock_page() (see above comments).
8995 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8996 EXTENT_DIRTY | EXTENT_DELALLOC |
8997 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
8998 0, 0, &cached_state, GFP_NOFS);
9000 ret = btrfs_set_extent_delalloc(inode, page_start, end,
9003 unlock_extent_cached(io_tree, page_start, page_end,
9004 &cached_state, GFP_NOFS);
9005 ret = VM_FAULT_SIGBUS;
9010 /* page is wholly or partially inside EOF */
9011 if (page_start + PAGE_SIZE > size)
9012 zero_start = size & ~PAGE_MASK;
9014 zero_start = PAGE_SIZE;
9016 if (zero_start != PAGE_SIZE) {
9018 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
9019 flush_dcache_page(page);
9022 ClearPageChecked(page);
9023 set_page_dirty(page);
9024 SetPageUptodate(page);
9026 BTRFS_I(inode)->last_trans = fs_info->generation;
9027 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
9028 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
9030 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
9034 sb_end_pagefault(inode->i_sb);
9035 return VM_FAULT_LOCKED;
9039 btrfs_delalloc_release_space(inode, page_start, reserved_space);
9041 sb_end_pagefault(inode->i_sb);
9045 static int btrfs_truncate(struct inode *inode)
9047 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9048 struct btrfs_root *root = BTRFS_I(inode)->root;
9049 struct btrfs_block_rsv *rsv;
9052 struct btrfs_trans_handle *trans;
9053 u64 mask = fs_info->sectorsize - 1;
9054 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
9056 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9062 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9063 * 3 things going on here
9065 * 1) We need to reserve space for our orphan item and the space to
9066 * delete our orphan item. Lord knows we don't want to have a dangling
9067 * orphan item because we didn't reserve space to remove it.
9069 * 2) We need to reserve space to update our inode.
9071 * 3) We need to have something to cache all the space that is going to
9072 * be free'd up by the truncate operation, but also have some slack
9073 * space reserved in case it uses space during the truncate (thank you
9074 * very much snapshotting).
9076 * And we need these to all be separate. The fact is we can use a lot of
9077 * space doing the truncate, and we have no earthly idea how much space
9078 * we will use, so we need the truncate reservation to be separate so it
9079 * doesn't end up using space reserved for updating the inode or
9080 * removing the orphan item. We also need to be able to stop the
9081 * transaction and start a new one, which means we need to be able to
9082 * update the inode several times, and we have no idea of knowing how
9083 * many times that will be, so we can't just reserve 1 item for the
9084 * entirety of the operation, so that has to be done separately as well.
9085 * Then there is the orphan item, which does indeed need to be held on
9086 * to for the whole operation, and we need nobody to touch this reserved
9087 * space except the orphan code.
9089 * So that leaves us with
9091 * 1) root->orphan_block_rsv - for the orphan deletion.
9092 * 2) rsv - for the truncate reservation, which we will steal from the
9093 * transaction reservation.
9094 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9095 * updating the inode.
9097 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
9100 rsv->size = min_size;
9104 * 1 for the truncate slack space
9105 * 1 for updating the inode.
9107 trans = btrfs_start_transaction(root, 2);
9108 if (IS_ERR(trans)) {
9109 err = PTR_ERR(trans);
9113 /* Migrate the slack space for the truncate to our reserve */
9114 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
9119 * So if we truncate and then write and fsync we normally would just
9120 * write the extents that changed, which is a problem if we need to
9121 * first truncate that entire inode. So set this flag so we write out
9122 * all of the extents in the inode to the sync log so we're completely
9125 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
9126 trans->block_rsv = rsv;
9129 ret = btrfs_truncate_inode_items(trans, root, inode,
9131 BTRFS_EXTENT_DATA_KEY);
9132 if (ret != -ENOSPC && ret != -EAGAIN) {
9137 trans->block_rsv = &fs_info->trans_block_rsv;
9138 ret = btrfs_update_inode(trans, root, inode);
9144 btrfs_end_transaction(trans);
9145 btrfs_btree_balance_dirty(fs_info);
9147 trans = btrfs_start_transaction(root, 2);
9148 if (IS_ERR(trans)) {
9149 ret = err = PTR_ERR(trans);
9154 btrfs_block_rsv_release(fs_info, rsv, -1);
9155 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
9157 BUG_ON(ret); /* shouldn't happen */
9158 trans->block_rsv = rsv;
9161 if (ret == 0 && inode->i_nlink > 0) {
9162 trans->block_rsv = root->orphan_block_rsv;
9163 ret = btrfs_orphan_del(trans, inode);
9169 trans->block_rsv = &fs_info->trans_block_rsv;
9170 ret = btrfs_update_inode(trans, root, inode);
9174 ret = btrfs_end_transaction(trans);
9175 btrfs_btree_balance_dirty(fs_info);
9178 btrfs_free_block_rsv(fs_info, rsv);
9187 * create a new subvolume directory/inode (helper for the ioctl).
9189 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9190 struct btrfs_root *new_root,
9191 struct btrfs_root *parent_root,
9194 struct inode *inode;
9198 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9199 new_dirid, new_dirid,
9200 S_IFDIR | (~current_umask() & S_IRWXUGO),
9203 return PTR_ERR(inode);
9204 inode->i_op = &btrfs_dir_inode_operations;
9205 inode->i_fop = &btrfs_dir_file_operations;
9207 set_nlink(inode, 1);
9208 btrfs_i_size_write(inode, 0);
9209 unlock_new_inode(inode);
9211 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9213 btrfs_err(new_root->fs_info,
9214 "error inheriting subvolume %llu properties: %d",
9215 new_root->root_key.objectid, err);
9217 err = btrfs_update_inode(trans, new_root, inode);
9223 struct inode *btrfs_alloc_inode(struct super_block *sb)
9225 struct btrfs_inode *ei;
9226 struct inode *inode;
9228 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9235 ei->last_sub_trans = 0;
9236 ei->logged_trans = 0;
9237 ei->delalloc_bytes = 0;
9238 ei->defrag_bytes = 0;
9239 ei->disk_i_size = 0;
9242 ei->index_cnt = (u64)-1;
9244 ei->last_unlink_trans = 0;
9245 ei->last_log_commit = 0;
9246 ei->delayed_iput_count = 0;
9248 spin_lock_init(&ei->lock);
9249 ei->outstanding_extents = 0;
9250 ei->reserved_extents = 0;
9252 ei->runtime_flags = 0;
9253 ei->force_compress = BTRFS_COMPRESS_NONE;
9255 ei->delayed_node = NULL;
9257 ei->i_otime.tv_sec = 0;
9258 ei->i_otime.tv_nsec = 0;
9260 inode = &ei->vfs_inode;
9261 extent_map_tree_init(&ei->extent_tree);
9262 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9263 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
9264 ei->io_tree.track_uptodate = 1;
9265 ei->io_failure_tree.track_uptodate = 1;
9266 atomic_set(&ei->sync_writers, 0);
9267 mutex_init(&ei->log_mutex);
9268 mutex_init(&ei->delalloc_mutex);
9269 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9270 INIT_LIST_HEAD(&ei->delalloc_inodes);
9271 INIT_LIST_HEAD(&ei->delayed_iput);
9272 RB_CLEAR_NODE(&ei->rb_node);
9273 init_rwsem(&ei->dio_sem);
9278 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9279 void btrfs_test_destroy_inode(struct inode *inode)
9281 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9282 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9286 static void btrfs_i_callback(struct rcu_head *head)
9288 struct inode *inode = container_of(head, struct inode, i_rcu);
9289 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9292 void btrfs_destroy_inode(struct inode *inode)
9294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9295 struct btrfs_ordered_extent *ordered;
9296 struct btrfs_root *root = BTRFS_I(inode)->root;
9298 WARN_ON(!hlist_empty(&inode->i_dentry));
9299 WARN_ON(inode->i_data.nrpages);
9300 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9301 WARN_ON(BTRFS_I(inode)->reserved_extents);
9302 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9303 WARN_ON(BTRFS_I(inode)->csum_bytes);
9304 WARN_ON(BTRFS_I(inode)->defrag_bytes);
9307 * This can happen where we create an inode, but somebody else also
9308 * created the same inode and we need to destroy the one we already
9314 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9315 &BTRFS_I(inode)->runtime_flags)) {
9316 btrfs_info(fs_info, "inode %llu still on the orphan list",
9317 btrfs_ino(BTRFS_I(inode)));
9318 atomic_dec(&root->orphan_inodes);
9322 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9327 "found ordered extent %llu %llu on inode cleanup",
9328 ordered->file_offset, ordered->len);
9329 btrfs_remove_ordered_extent(inode, ordered);
9330 btrfs_put_ordered_extent(ordered);
9331 btrfs_put_ordered_extent(ordered);
9334 btrfs_qgroup_check_reserved_leak(inode);
9335 inode_tree_del(inode);
9336 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9338 call_rcu(&inode->i_rcu, btrfs_i_callback);
9341 int btrfs_drop_inode(struct inode *inode)
9343 struct btrfs_root *root = BTRFS_I(inode)->root;
9348 /* the snap/subvol tree is on deleting */
9349 if (btrfs_root_refs(&root->root_item) == 0)
9352 return generic_drop_inode(inode);
9355 static void init_once(void *foo)
9357 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9359 inode_init_once(&ei->vfs_inode);
9362 void btrfs_destroy_cachep(void)
9365 * Make sure all delayed rcu free inodes are flushed before we
9369 kmem_cache_destroy(btrfs_inode_cachep);
9370 kmem_cache_destroy(btrfs_trans_handle_cachep);
9371 kmem_cache_destroy(btrfs_transaction_cachep);
9372 kmem_cache_destroy(btrfs_path_cachep);
9373 kmem_cache_destroy(btrfs_free_space_cachep);
9376 int btrfs_init_cachep(void)
9378 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9379 sizeof(struct btrfs_inode), 0,
9380 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9382 if (!btrfs_inode_cachep)
9385 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9386 sizeof(struct btrfs_trans_handle), 0,
9387 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9388 if (!btrfs_trans_handle_cachep)
9391 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
9392 sizeof(struct btrfs_transaction), 0,
9393 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9394 if (!btrfs_transaction_cachep)
9397 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9398 sizeof(struct btrfs_path), 0,
9399 SLAB_MEM_SPREAD, NULL);
9400 if (!btrfs_path_cachep)
9403 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9404 sizeof(struct btrfs_free_space), 0,
9405 SLAB_MEM_SPREAD, NULL);
9406 if (!btrfs_free_space_cachep)
9411 btrfs_destroy_cachep();
9415 static int btrfs_getattr(struct vfsmount *mnt,
9416 struct dentry *dentry, struct kstat *stat)
9419 struct inode *inode = d_inode(dentry);
9420 u32 blocksize = inode->i_sb->s_blocksize;
9422 generic_fillattr(inode, stat);
9423 stat->dev = BTRFS_I(inode)->root->anon_dev;
9425 spin_lock(&BTRFS_I(inode)->lock);
9426 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9427 spin_unlock(&BTRFS_I(inode)->lock);
9428 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
9429 ALIGN(delalloc_bytes, blocksize)) >> 9;
9433 static int btrfs_rename_exchange(struct inode *old_dir,
9434 struct dentry *old_dentry,
9435 struct inode *new_dir,
9436 struct dentry *new_dentry)
9438 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9439 struct btrfs_trans_handle *trans;
9440 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9441 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9442 struct inode *new_inode = new_dentry->d_inode;
9443 struct inode *old_inode = old_dentry->d_inode;
9444 struct timespec ctime = current_time(old_inode);
9445 struct dentry *parent;
9446 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9447 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
9452 bool root_log_pinned = false;
9453 bool dest_log_pinned = false;
9455 /* we only allow rename subvolume link between subvolumes */
9456 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9459 /* close the race window with snapshot create/destroy ioctl */
9460 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9461 down_read(&fs_info->subvol_sem);
9462 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9463 down_read(&fs_info->subvol_sem);
9466 * We want to reserve the absolute worst case amount of items. So if
9467 * both inodes are subvols and we need to unlink them then that would
9468 * require 4 item modifications, but if they are both normal inodes it
9469 * would require 5 item modifications, so we'll assume their normal
9470 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9471 * should cover the worst case number of items we'll modify.
9473 trans = btrfs_start_transaction(root, 12);
9474 if (IS_ERR(trans)) {
9475 ret = PTR_ERR(trans);
9480 * We need to find a free sequence number both in the source and
9481 * in the destination directory for the exchange.
9483 ret = btrfs_set_inode_index(new_dir, &old_idx);
9486 ret = btrfs_set_inode_index(old_dir, &new_idx);
9490 BTRFS_I(old_inode)->dir_index = 0ULL;
9491 BTRFS_I(new_inode)->dir_index = 0ULL;
9493 /* Reference for the source. */
9494 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9495 /* force full log commit if subvolume involved. */
9496 btrfs_set_log_full_commit(fs_info, trans);
9498 btrfs_pin_log_trans(root);
9499 root_log_pinned = true;
9500 ret = btrfs_insert_inode_ref(trans, dest,
9501 new_dentry->d_name.name,
9502 new_dentry->d_name.len,
9504 btrfs_ino(BTRFS_I(new_dir)),
9510 /* And now for the dest. */
9511 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9512 /* force full log commit if subvolume involved. */
9513 btrfs_set_log_full_commit(fs_info, trans);
9515 btrfs_pin_log_trans(dest);
9516 dest_log_pinned = true;
9517 ret = btrfs_insert_inode_ref(trans, root,
9518 old_dentry->d_name.name,
9519 old_dentry->d_name.len,
9521 btrfs_ino(BTRFS_I(old_dir)),
9527 /* Update inode version and ctime/mtime. */
9528 inode_inc_iversion(old_dir);
9529 inode_inc_iversion(new_dir);
9530 inode_inc_iversion(old_inode);
9531 inode_inc_iversion(new_inode);
9532 old_dir->i_ctime = old_dir->i_mtime = ctime;
9533 new_dir->i_ctime = new_dir->i_mtime = ctime;
9534 old_inode->i_ctime = ctime;
9535 new_inode->i_ctime = ctime;
9537 if (old_dentry->d_parent != new_dentry->d_parent) {
9538 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9539 BTRFS_I(old_inode), 1);
9540 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9541 BTRFS_I(new_inode), 1);
9544 /* src is a subvolume */
9545 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9546 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9547 ret = btrfs_unlink_subvol(trans, root, old_dir,
9549 old_dentry->d_name.name,
9550 old_dentry->d_name.len);
9551 } else { /* src is an inode */
9552 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9553 BTRFS_I(old_dentry->d_inode),
9554 old_dentry->d_name.name,
9555 old_dentry->d_name.len);
9557 ret = btrfs_update_inode(trans, root, old_inode);
9560 btrfs_abort_transaction(trans, ret);
9564 /* dest is a subvolume */
9565 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9566 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9567 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9569 new_dentry->d_name.name,
9570 new_dentry->d_name.len);
9571 } else { /* dest is an inode */
9572 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9573 BTRFS_I(new_dentry->d_inode),
9574 new_dentry->d_name.name,
9575 new_dentry->d_name.len);
9577 ret = btrfs_update_inode(trans, dest, new_inode);
9580 btrfs_abort_transaction(trans, ret);
9584 ret = btrfs_add_link(trans, new_dir, old_inode,
9585 new_dentry->d_name.name,
9586 new_dentry->d_name.len, 0, old_idx);
9588 btrfs_abort_transaction(trans, ret);
9592 ret = btrfs_add_link(trans, old_dir, new_inode,
9593 old_dentry->d_name.name,
9594 old_dentry->d_name.len, 0, new_idx);
9596 btrfs_abort_transaction(trans, ret);
9600 if (old_inode->i_nlink == 1)
9601 BTRFS_I(old_inode)->dir_index = old_idx;
9602 if (new_inode->i_nlink == 1)
9603 BTRFS_I(new_inode)->dir_index = new_idx;
9605 if (root_log_pinned) {
9606 parent = new_dentry->d_parent;
9607 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9609 btrfs_end_log_trans(root);
9610 root_log_pinned = false;
9612 if (dest_log_pinned) {
9613 parent = old_dentry->d_parent;
9614 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9616 btrfs_end_log_trans(dest);
9617 dest_log_pinned = false;
9621 * If we have pinned a log and an error happened, we unpin tasks
9622 * trying to sync the log and force them to fallback to a transaction
9623 * commit if the log currently contains any of the inodes involved in
9624 * this rename operation (to ensure we do not persist a log with an
9625 * inconsistent state for any of these inodes or leading to any
9626 * inconsistencies when replayed). If the transaction was aborted, the
9627 * abortion reason is propagated to userspace when attempting to commit
9628 * the transaction. If the log does not contain any of these inodes, we
9629 * allow the tasks to sync it.
9631 if (ret && (root_log_pinned || dest_log_pinned)) {
9632 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9633 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9634 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9636 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9637 btrfs_set_log_full_commit(fs_info, trans);
9639 if (root_log_pinned) {
9640 btrfs_end_log_trans(root);
9641 root_log_pinned = false;
9643 if (dest_log_pinned) {
9644 btrfs_end_log_trans(dest);
9645 dest_log_pinned = false;
9648 ret = btrfs_end_transaction(trans);
9650 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9651 up_read(&fs_info->subvol_sem);
9652 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9653 up_read(&fs_info->subvol_sem);
9658 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9659 struct btrfs_root *root,
9661 struct dentry *dentry)
9664 struct inode *inode;
9668 ret = btrfs_find_free_ino(root, &objectid);
9672 inode = btrfs_new_inode(trans, root, dir,
9673 dentry->d_name.name,
9675 btrfs_ino(BTRFS_I(dir)),
9677 S_IFCHR | WHITEOUT_MODE,
9680 if (IS_ERR(inode)) {
9681 ret = PTR_ERR(inode);
9685 inode->i_op = &btrfs_special_inode_operations;
9686 init_special_inode(inode, inode->i_mode,
9689 ret = btrfs_init_inode_security(trans, inode, dir,
9694 ret = btrfs_add_nondir(trans, dir, dentry,
9699 ret = btrfs_update_inode(trans, root, inode);
9701 unlock_new_inode(inode);
9703 inode_dec_link_count(inode);
9709 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9710 struct inode *new_dir, struct dentry *new_dentry,
9713 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9714 struct btrfs_trans_handle *trans;
9715 unsigned int trans_num_items;
9716 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9717 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9718 struct inode *new_inode = d_inode(new_dentry);
9719 struct inode *old_inode = d_inode(old_dentry);
9723 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9724 bool log_pinned = false;
9726 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9729 /* we only allow rename subvolume link between subvolumes */
9730 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9733 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9734 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
9737 if (S_ISDIR(old_inode->i_mode) && new_inode &&
9738 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9742 /* check for collisions, even if the name isn't there */
9743 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9744 new_dentry->d_name.name,
9745 new_dentry->d_name.len);
9748 if (ret == -EEXIST) {
9750 * eexist without a new_inode */
9751 if (WARN_ON(!new_inode)) {
9755 /* maybe -EOVERFLOW */
9762 * we're using rename to replace one file with another. Start IO on it
9763 * now so we don't add too much work to the end of the transaction
9765 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9766 filemap_flush(old_inode->i_mapping);
9768 /* close the racy window with snapshot create/destroy ioctl */
9769 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9770 down_read(&fs_info->subvol_sem);
9772 * We want to reserve the absolute worst case amount of items. So if
9773 * both inodes are subvols and we need to unlink them then that would
9774 * require 4 item modifications, but if they are both normal inodes it
9775 * would require 5 item modifications, so we'll assume they are normal
9776 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9777 * should cover the worst case number of items we'll modify.
9778 * If our rename has the whiteout flag, we need more 5 units for the
9779 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9780 * when selinux is enabled).
9782 trans_num_items = 11;
9783 if (flags & RENAME_WHITEOUT)
9784 trans_num_items += 5;
9785 trans = btrfs_start_transaction(root, trans_num_items);
9786 if (IS_ERR(trans)) {
9787 ret = PTR_ERR(trans);
9792 btrfs_record_root_in_trans(trans, dest);
9794 ret = btrfs_set_inode_index(new_dir, &index);
9798 BTRFS_I(old_inode)->dir_index = 0ULL;
9799 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9800 /* force full log commit if subvolume involved. */
9801 btrfs_set_log_full_commit(fs_info, trans);
9803 btrfs_pin_log_trans(root);
9805 ret = btrfs_insert_inode_ref(trans, dest,
9806 new_dentry->d_name.name,
9807 new_dentry->d_name.len,
9809 btrfs_ino(BTRFS_I(new_dir)), index);
9814 inode_inc_iversion(old_dir);
9815 inode_inc_iversion(new_dir);
9816 inode_inc_iversion(old_inode);
9817 old_dir->i_ctime = old_dir->i_mtime =
9818 new_dir->i_ctime = new_dir->i_mtime =
9819 old_inode->i_ctime = current_time(old_dir);
9821 if (old_dentry->d_parent != new_dentry->d_parent)
9822 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9823 BTRFS_I(old_inode), 1);
9825 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9826 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9827 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9828 old_dentry->d_name.name,
9829 old_dentry->d_name.len);
9831 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9832 BTRFS_I(d_inode(old_dentry)),
9833 old_dentry->d_name.name,
9834 old_dentry->d_name.len);
9836 ret = btrfs_update_inode(trans, root, old_inode);
9839 btrfs_abort_transaction(trans, ret);
9844 inode_inc_iversion(new_inode);
9845 new_inode->i_ctime = current_time(new_inode);
9846 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
9847 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9848 root_objectid = BTRFS_I(new_inode)->location.objectid;
9849 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9851 new_dentry->d_name.name,
9852 new_dentry->d_name.len);
9853 BUG_ON(new_inode->i_nlink == 0);
9855 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9856 BTRFS_I(d_inode(new_dentry)),
9857 new_dentry->d_name.name,
9858 new_dentry->d_name.len);
9860 if (!ret && new_inode->i_nlink == 0)
9861 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
9863 btrfs_abort_transaction(trans, ret);
9868 ret = btrfs_add_link(trans, new_dir, old_inode,
9869 new_dentry->d_name.name,
9870 new_dentry->d_name.len, 0, index);
9872 btrfs_abort_transaction(trans, ret);
9876 if (old_inode->i_nlink == 1)
9877 BTRFS_I(old_inode)->dir_index = index;
9880 struct dentry *parent = new_dentry->d_parent;
9882 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9884 btrfs_end_log_trans(root);
9888 if (flags & RENAME_WHITEOUT) {
9889 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9893 btrfs_abort_transaction(trans, ret);
9899 * If we have pinned the log and an error happened, we unpin tasks
9900 * trying to sync the log and force them to fallback to a transaction
9901 * commit if the log currently contains any of the inodes involved in
9902 * this rename operation (to ensure we do not persist a log with an
9903 * inconsistent state for any of these inodes or leading to any
9904 * inconsistencies when replayed). If the transaction was aborted, the
9905 * abortion reason is propagated to userspace when attempting to commit
9906 * the transaction. If the log does not contain any of these inodes, we
9907 * allow the tasks to sync it.
9909 if (ret && log_pinned) {
9910 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9911 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9912 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9914 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9915 btrfs_set_log_full_commit(fs_info, trans);
9917 btrfs_end_log_trans(root);
9920 btrfs_end_transaction(trans);
9922 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9923 up_read(&fs_info->subvol_sem);
9928 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9929 struct inode *new_dir, struct dentry *new_dentry,
9932 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9935 if (flags & RENAME_EXCHANGE)
9936 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9939 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
9942 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9944 struct btrfs_delalloc_work *delalloc_work;
9945 struct inode *inode;
9947 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9949 inode = delalloc_work->inode;
9950 filemap_flush(inode->i_mapping);
9951 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9952 &BTRFS_I(inode)->runtime_flags))
9953 filemap_flush(inode->i_mapping);
9955 if (delalloc_work->delay_iput)
9956 btrfs_add_delayed_iput(inode);
9959 complete(&delalloc_work->completion);
9962 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9965 struct btrfs_delalloc_work *work;
9967 work = kmalloc(sizeof(*work), GFP_NOFS);
9971 init_completion(&work->completion);
9972 INIT_LIST_HEAD(&work->list);
9973 work->inode = inode;
9974 work->delay_iput = delay_iput;
9975 WARN_ON_ONCE(!inode);
9976 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9977 btrfs_run_delalloc_work, NULL, NULL);
9982 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9984 wait_for_completion(&work->completion);
9989 * some fairly slow code that needs optimization. This walks the list
9990 * of all the inodes with pending delalloc and forces them to disk.
9992 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9995 struct btrfs_inode *binode;
9996 struct inode *inode;
9997 struct btrfs_delalloc_work *work, *next;
9998 struct list_head works;
9999 struct list_head splice;
10002 INIT_LIST_HEAD(&works);
10003 INIT_LIST_HEAD(&splice);
10005 mutex_lock(&root->delalloc_mutex);
10006 spin_lock(&root->delalloc_lock);
10007 list_splice_init(&root->delalloc_inodes, &splice);
10008 while (!list_empty(&splice)) {
10009 binode = list_entry(splice.next, struct btrfs_inode,
10012 list_move_tail(&binode->delalloc_inodes,
10013 &root->delalloc_inodes);
10014 inode = igrab(&binode->vfs_inode);
10016 cond_resched_lock(&root->delalloc_lock);
10019 spin_unlock(&root->delalloc_lock);
10021 work = btrfs_alloc_delalloc_work(inode, delay_iput);
10024 btrfs_add_delayed_iput(inode);
10030 list_add_tail(&work->list, &works);
10031 btrfs_queue_work(root->fs_info->flush_workers,
10034 if (nr != -1 && ret >= nr)
10037 spin_lock(&root->delalloc_lock);
10039 spin_unlock(&root->delalloc_lock);
10042 list_for_each_entry_safe(work, next, &works, list) {
10043 list_del_init(&work->list);
10044 btrfs_wait_and_free_delalloc_work(work);
10047 if (!list_empty_careful(&splice)) {
10048 spin_lock(&root->delalloc_lock);
10049 list_splice_tail(&splice, &root->delalloc_inodes);
10050 spin_unlock(&root->delalloc_lock);
10052 mutex_unlock(&root->delalloc_mutex);
10056 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10058 struct btrfs_fs_info *fs_info = root->fs_info;
10061 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10064 ret = __start_delalloc_inodes(root, delay_iput, -1);
10068 * the filemap_flush will queue IO into the worker threads, but
10069 * we have to make sure the IO is actually started and that
10070 * ordered extents get created before we return
10072 atomic_inc(&fs_info->async_submit_draining);
10073 while (atomic_read(&fs_info->nr_async_submits) ||
10074 atomic_read(&fs_info->async_delalloc_pages)) {
10075 wait_event(fs_info->async_submit_wait,
10076 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10077 atomic_read(&fs_info->async_delalloc_pages) == 0));
10079 atomic_dec(&fs_info->async_submit_draining);
10083 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10086 struct btrfs_root *root;
10087 struct list_head splice;
10090 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10093 INIT_LIST_HEAD(&splice);
10095 mutex_lock(&fs_info->delalloc_root_mutex);
10096 spin_lock(&fs_info->delalloc_root_lock);
10097 list_splice_init(&fs_info->delalloc_roots, &splice);
10098 while (!list_empty(&splice) && nr) {
10099 root = list_first_entry(&splice, struct btrfs_root,
10101 root = btrfs_grab_fs_root(root);
10103 list_move_tail(&root->delalloc_root,
10104 &fs_info->delalloc_roots);
10105 spin_unlock(&fs_info->delalloc_root_lock);
10107 ret = __start_delalloc_inodes(root, delay_iput, nr);
10108 btrfs_put_fs_root(root);
10116 spin_lock(&fs_info->delalloc_root_lock);
10118 spin_unlock(&fs_info->delalloc_root_lock);
10121 atomic_inc(&fs_info->async_submit_draining);
10122 while (atomic_read(&fs_info->nr_async_submits) ||
10123 atomic_read(&fs_info->async_delalloc_pages)) {
10124 wait_event(fs_info->async_submit_wait,
10125 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10126 atomic_read(&fs_info->async_delalloc_pages) == 0));
10128 atomic_dec(&fs_info->async_submit_draining);
10130 if (!list_empty_careful(&splice)) {
10131 spin_lock(&fs_info->delalloc_root_lock);
10132 list_splice_tail(&splice, &fs_info->delalloc_roots);
10133 spin_unlock(&fs_info->delalloc_root_lock);
10135 mutex_unlock(&fs_info->delalloc_root_mutex);
10139 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10140 const char *symname)
10142 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10143 struct btrfs_trans_handle *trans;
10144 struct btrfs_root *root = BTRFS_I(dir)->root;
10145 struct btrfs_path *path;
10146 struct btrfs_key key;
10147 struct inode *inode = NULL;
10149 int drop_inode = 0;
10155 struct btrfs_file_extent_item *ei;
10156 struct extent_buffer *leaf;
10158 name_len = strlen(symname);
10159 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10160 return -ENAMETOOLONG;
10163 * 2 items for inode item and ref
10164 * 2 items for dir items
10165 * 1 item for updating parent inode item
10166 * 1 item for the inline extent item
10167 * 1 item for xattr if selinux is on
10169 trans = btrfs_start_transaction(root, 7);
10171 return PTR_ERR(trans);
10173 err = btrfs_find_free_ino(root, &objectid);
10177 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
10178 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10179 objectid, S_IFLNK|S_IRWXUGO, &index);
10180 if (IS_ERR(inode)) {
10181 err = PTR_ERR(inode);
10186 * If the active LSM wants to access the inode during
10187 * d_instantiate it needs these. Smack checks to see
10188 * if the filesystem supports xattrs by looking at the
10191 inode->i_fop = &btrfs_file_operations;
10192 inode->i_op = &btrfs_file_inode_operations;
10193 inode->i_mapping->a_ops = &btrfs_aops;
10194 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10196 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10198 goto out_unlock_inode;
10200 path = btrfs_alloc_path();
10203 goto out_unlock_inode;
10205 key.objectid = btrfs_ino(BTRFS_I(inode));
10207 key.type = BTRFS_EXTENT_DATA_KEY;
10208 datasize = btrfs_file_extent_calc_inline_size(name_len);
10209 err = btrfs_insert_empty_item(trans, root, path, &key,
10212 btrfs_free_path(path);
10213 goto out_unlock_inode;
10215 leaf = path->nodes[0];
10216 ei = btrfs_item_ptr(leaf, path->slots[0],
10217 struct btrfs_file_extent_item);
10218 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10219 btrfs_set_file_extent_type(leaf, ei,
10220 BTRFS_FILE_EXTENT_INLINE);
10221 btrfs_set_file_extent_encryption(leaf, ei, 0);
10222 btrfs_set_file_extent_compression(leaf, ei, 0);
10223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10224 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10226 ptr = btrfs_file_extent_inline_start(ei);
10227 write_extent_buffer(leaf, symname, ptr, name_len);
10228 btrfs_mark_buffer_dirty(leaf);
10229 btrfs_free_path(path);
10231 inode->i_op = &btrfs_symlink_inode_operations;
10232 inode_nohighmem(inode);
10233 inode->i_mapping->a_ops = &btrfs_symlink_aops;
10234 inode_set_bytes(inode, name_len);
10235 btrfs_i_size_write(inode, name_len);
10236 err = btrfs_update_inode(trans, root, inode);
10238 * Last step, add directory indexes for our symlink inode. This is the
10239 * last step to avoid extra cleanup of these indexes if an error happens
10243 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
10246 goto out_unlock_inode;
10249 unlock_new_inode(inode);
10250 d_instantiate(dentry, inode);
10253 btrfs_end_transaction(trans);
10255 inode_dec_link_count(inode);
10258 btrfs_btree_balance_dirty(fs_info);
10263 unlock_new_inode(inode);
10267 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10268 u64 start, u64 num_bytes, u64 min_size,
10269 loff_t actual_len, u64 *alloc_hint,
10270 struct btrfs_trans_handle *trans)
10272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10274 struct extent_map *em;
10275 struct btrfs_root *root = BTRFS_I(inode)->root;
10276 struct btrfs_key ins;
10277 u64 cur_offset = start;
10280 u64 last_alloc = (u64)-1;
10282 bool own_trans = true;
10283 u64 end = start + num_bytes - 1;
10287 while (num_bytes > 0) {
10289 trans = btrfs_start_transaction(root, 3);
10290 if (IS_ERR(trans)) {
10291 ret = PTR_ERR(trans);
10296 cur_bytes = min_t(u64, num_bytes, SZ_256M);
10297 cur_bytes = max(cur_bytes, min_size);
10299 * If we are severely fragmented we could end up with really
10300 * small allocations, so if the allocator is returning small
10301 * chunks lets make its job easier by only searching for those
10304 cur_bytes = min(cur_bytes, last_alloc);
10305 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10306 min_size, 0, *alloc_hint, &ins, 1, 0);
10309 btrfs_end_transaction(trans);
10312 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10314 last_alloc = ins.offset;
10315 ret = insert_reserved_file_extent(trans, inode,
10316 cur_offset, ins.objectid,
10317 ins.offset, ins.offset,
10318 ins.offset, 0, 0, 0,
10319 BTRFS_FILE_EXTENT_PREALLOC);
10321 btrfs_free_reserved_extent(fs_info, ins.objectid,
10323 btrfs_abort_transaction(trans, ret);
10325 btrfs_end_transaction(trans);
10329 btrfs_drop_extent_cache(inode, cur_offset,
10330 cur_offset + ins.offset -1, 0);
10332 em = alloc_extent_map();
10334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10335 &BTRFS_I(inode)->runtime_flags);
10339 em->start = cur_offset;
10340 em->orig_start = cur_offset;
10341 em->len = ins.offset;
10342 em->block_start = ins.objectid;
10343 em->block_len = ins.offset;
10344 em->orig_block_len = ins.offset;
10345 em->ram_bytes = ins.offset;
10346 em->bdev = fs_info->fs_devices->latest_bdev;
10347 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10348 em->generation = trans->transid;
10351 write_lock(&em_tree->lock);
10352 ret = add_extent_mapping(em_tree, em, 1);
10353 write_unlock(&em_tree->lock);
10354 if (ret != -EEXIST)
10356 btrfs_drop_extent_cache(inode, cur_offset,
10357 cur_offset + ins.offset - 1,
10360 free_extent_map(em);
10362 num_bytes -= ins.offset;
10363 cur_offset += ins.offset;
10364 *alloc_hint = ins.objectid + ins.offset;
10366 inode_inc_iversion(inode);
10367 inode->i_ctime = current_time(inode);
10368 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
10369 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
10370 (actual_len > inode->i_size) &&
10371 (cur_offset > inode->i_size)) {
10372 if (cur_offset > actual_len)
10373 i_size = actual_len;
10375 i_size = cur_offset;
10376 i_size_write(inode, i_size);
10377 btrfs_ordered_update_i_size(inode, i_size, NULL);
10380 ret = btrfs_update_inode(trans, root, inode);
10383 btrfs_abort_transaction(trans, ret);
10385 btrfs_end_transaction(trans);
10390 btrfs_end_transaction(trans);
10392 if (cur_offset < end)
10393 btrfs_free_reserved_data_space(inode, cur_offset,
10394 end - cur_offset + 1);
10398 int btrfs_prealloc_file_range(struct inode *inode, int mode,
10399 u64 start, u64 num_bytes, u64 min_size,
10400 loff_t actual_len, u64 *alloc_hint)
10402 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10403 min_size, actual_len, alloc_hint,
10407 int btrfs_prealloc_file_range_trans(struct inode *inode,
10408 struct btrfs_trans_handle *trans, int mode,
10409 u64 start, u64 num_bytes, u64 min_size,
10410 loff_t actual_len, u64 *alloc_hint)
10412 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10413 min_size, actual_len, alloc_hint, trans);
10416 static int btrfs_set_page_dirty(struct page *page)
10418 return __set_page_dirty_nobuffers(page);
10421 static int btrfs_permission(struct inode *inode, int mask)
10423 struct btrfs_root *root = BTRFS_I(inode)->root;
10424 umode_t mode = inode->i_mode;
10426 if (mask & MAY_WRITE &&
10427 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10428 if (btrfs_root_readonly(root))
10430 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10433 return generic_permission(inode, mask);
10436 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10438 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10439 struct btrfs_trans_handle *trans;
10440 struct btrfs_root *root = BTRFS_I(dir)->root;
10441 struct inode *inode = NULL;
10447 * 5 units required for adding orphan entry
10449 trans = btrfs_start_transaction(root, 5);
10451 return PTR_ERR(trans);
10453 ret = btrfs_find_free_ino(root, &objectid);
10457 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10458 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
10459 if (IS_ERR(inode)) {
10460 ret = PTR_ERR(inode);
10465 inode->i_fop = &btrfs_file_operations;
10466 inode->i_op = &btrfs_file_inode_operations;
10468 inode->i_mapping->a_ops = &btrfs_aops;
10469 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10471 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10475 ret = btrfs_update_inode(trans, root, inode);
10478 ret = btrfs_orphan_add(trans, inode);
10483 * We set number of links to 0 in btrfs_new_inode(), and here we set
10484 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10487 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10489 set_nlink(inode, 1);
10490 unlock_new_inode(inode);
10491 d_tmpfile(dentry, inode);
10492 mark_inode_dirty(inode);
10495 btrfs_end_transaction(trans);
10498 btrfs_balance_delayed_items(fs_info);
10499 btrfs_btree_balance_dirty(fs_info);
10503 unlock_new_inode(inode);
10508 static const struct inode_operations btrfs_dir_inode_operations = {
10509 .getattr = btrfs_getattr,
10510 .lookup = btrfs_lookup,
10511 .create = btrfs_create,
10512 .unlink = btrfs_unlink,
10513 .link = btrfs_link,
10514 .mkdir = btrfs_mkdir,
10515 .rmdir = btrfs_rmdir,
10516 .rename = btrfs_rename2,
10517 .symlink = btrfs_symlink,
10518 .setattr = btrfs_setattr,
10519 .mknod = btrfs_mknod,
10520 .listxattr = btrfs_listxattr,
10521 .permission = btrfs_permission,
10522 .get_acl = btrfs_get_acl,
10523 .set_acl = btrfs_set_acl,
10524 .update_time = btrfs_update_time,
10525 .tmpfile = btrfs_tmpfile,
10527 static const struct inode_operations btrfs_dir_ro_inode_operations = {
10528 .lookup = btrfs_lookup,
10529 .permission = btrfs_permission,
10530 .update_time = btrfs_update_time,
10533 static const struct file_operations btrfs_dir_file_operations = {
10534 .llseek = generic_file_llseek,
10535 .read = generic_read_dir,
10536 .iterate_shared = btrfs_real_readdir,
10537 .unlocked_ioctl = btrfs_ioctl,
10538 #ifdef CONFIG_COMPAT
10539 .compat_ioctl = btrfs_compat_ioctl,
10541 .release = btrfs_release_file,
10542 .fsync = btrfs_sync_file,
10545 static const struct extent_io_ops btrfs_extent_io_ops = {
10546 .fill_delalloc = run_delalloc_range,
10547 .submit_bio_hook = btrfs_submit_bio_hook,
10548 .merge_bio_hook = btrfs_merge_bio_hook,
10549 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10550 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
10551 .writepage_start_hook = btrfs_writepage_start_hook,
10552 .set_bit_hook = btrfs_set_bit_hook,
10553 .clear_bit_hook = btrfs_clear_bit_hook,
10554 .merge_extent_hook = btrfs_merge_extent_hook,
10555 .split_extent_hook = btrfs_split_extent_hook,
10559 * btrfs doesn't support the bmap operation because swapfiles
10560 * use bmap to make a mapping of extents in the file. They assume
10561 * these extents won't change over the life of the file and they
10562 * use the bmap result to do IO directly to the drive.
10564 * the btrfs bmap call would return logical addresses that aren't
10565 * suitable for IO and they also will change frequently as COW
10566 * operations happen. So, swapfile + btrfs == corruption.
10568 * For now we're avoiding this by dropping bmap.
10570 static const struct address_space_operations btrfs_aops = {
10571 .readpage = btrfs_readpage,
10572 .writepage = btrfs_writepage,
10573 .writepages = btrfs_writepages,
10574 .readpages = btrfs_readpages,
10575 .direct_IO = btrfs_direct_IO,
10576 .invalidatepage = btrfs_invalidatepage,
10577 .releasepage = btrfs_releasepage,
10578 .set_page_dirty = btrfs_set_page_dirty,
10579 .error_remove_page = generic_error_remove_page,
10582 static const struct address_space_operations btrfs_symlink_aops = {
10583 .readpage = btrfs_readpage,
10584 .writepage = btrfs_writepage,
10585 .invalidatepage = btrfs_invalidatepage,
10586 .releasepage = btrfs_releasepage,
10589 static const struct inode_operations btrfs_file_inode_operations = {
10590 .getattr = btrfs_getattr,
10591 .setattr = btrfs_setattr,
10592 .listxattr = btrfs_listxattr,
10593 .permission = btrfs_permission,
10594 .fiemap = btrfs_fiemap,
10595 .get_acl = btrfs_get_acl,
10596 .set_acl = btrfs_set_acl,
10597 .update_time = btrfs_update_time,
10599 static const struct inode_operations btrfs_special_inode_operations = {
10600 .getattr = btrfs_getattr,
10601 .setattr = btrfs_setattr,
10602 .permission = btrfs_permission,
10603 .listxattr = btrfs_listxattr,
10604 .get_acl = btrfs_get_acl,
10605 .set_acl = btrfs_set_acl,
10606 .update_time = btrfs_update_time,
10608 static const struct inode_operations btrfs_symlink_inode_operations = {
10609 .get_link = page_get_link,
10610 .getattr = btrfs_getattr,
10611 .setattr = btrfs_setattr,
10612 .permission = btrfs_permission,
10613 .listxattr = btrfs_listxattr,
10614 .update_time = btrfs_update_time,
10617 const struct dentry_operations btrfs_dentry_operations = {
10618 .d_delete = btrfs_dentry_delete,
10619 .d_release = btrfs_dentry_release,