2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/aio.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/xattr.h>
38 #include <linux/posix_acl.h>
39 #include <linux/falloc.h>
40 #include <linux/slab.h>
41 #include <linux/ratelimit.h>
42 #include <linux/mount.h>
43 #include <linux/btrfs.h>
44 #include <linux/blkdev.h>
45 #include <linux/posix_acl_xattr.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
51 #include "ordered-data.h"
55 #include "compression.h"
57 #include "free-space-cache.h"
58 #include "inode-map.h"
63 struct btrfs_iget_args {
64 struct btrfs_key *location;
65 struct btrfs_root *root;
68 static const struct inode_operations btrfs_dir_inode_operations;
69 static const struct inode_operations btrfs_symlink_inode_operations;
70 static const struct inode_operations btrfs_dir_ro_inode_operations;
71 static const struct inode_operations btrfs_special_inode_operations;
72 static const struct inode_operations btrfs_file_inode_operations;
73 static const struct address_space_operations btrfs_aops;
74 static const struct address_space_operations btrfs_symlink_aops;
75 static const struct file_operations btrfs_dir_file_operations;
76 static struct extent_io_ops btrfs_extent_io_ops;
78 static struct kmem_cache *btrfs_inode_cachep;
79 static struct kmem_cache *btrfs_delalloc_work_cachep;
80 struct kmem_cache *btrfs_trans_handle_cachep;
81 struct kmem_cache *btrfs_transaction_cachep;
82 struct kmem_cache *btrfs_path_cachep;
83 struct kmem_cache *btrfs_free_space_cachep;
86 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
96 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
97 static int btrfs_truncate(struct inode *inode);
98 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
99 static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
103 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
106 u64 orig_block_len, u64 ram_bytes,
109 static int btrfs_dirty_inode(struct inode *inode);
111 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
117 err = btrfs_init_acl(trans, inode, dir);
119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
128 static int insert_inline_extent(struct btrfs_trans_handle *trans,
129 struct btrfs_path *path, int extent_inserted,
130 struct btrfs_root *root, struct inode *inode,
131 u64 start, size_t size, size_t compressed_size,
133 struct page **compressed_pages)
135 struct extent_buffer *leaf;
136 struct page *page = NULL;
139 struct btrfs_file_extent_item *ei;
142 size_t cur_size = size;
143 unsigned long offset;
145 if (compressed_size && compressed_pages)
146 cur_size = compressed_size;
148 inode_add_bytes(inode, size);
150 if (!extent_inserted) {
151 struct btrfs_key key;
154 key.objectid = btrfs_ino(inode);
156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 path->leave_spinning = 1;
160 ret = btrfs_insert_empty_item(trans, root, path, &key,
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
177 if (compress_type != BTRFS_COMPRESS_NONE) {
180 while (compressed_size > 0) {
181 cpage = compressed_pages[i];
182 cur_size = min_t(unsigned long, compressed_size,
185 kaddr = kmap_atomic(cpage);
186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
187 kunmap_atomic(kaddr);
191 compressed_size -= cur_size;
193 btrfs_set_file_extent_compression(leaf, ei,
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
199 kaddr = kmap_atomic(page);
200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
202 kunmap_atomic(kaddr);
203 page_cache_release(page);
205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_release_path(path);
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
217 BTRFS_I(inode)->disk_i_size = inode->i_size;
218 ret = btrfs_update_inode(trans, root, inode);
227 * conditionally insert an inline extent into the file. This
228 * does the checks required to make sure the data is small enough
229 * to fit as an inline extent.
231 static noinline int cow_file_range_inline(struct btrfs_root *root,
232 struct inode *inode, u64 start,
233 u64 end, size_t compressed_size,
235 struct page **compressed_pages)
237 struct btrfs_trans_handle *trans;
238 u64 isize = i_size_read(inode);
239 u64 actual_end = min(end + 1, isize);
240 u64 inline_len = actual_end - start;
241 u64 aligned_end = ALIGN(end, root->sectorsize);
242 u64 data_len = inline_len;
244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
249 data_len = compressed_size;
252 actual_end >= PAGE_CACHE_SIZE ||
253 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
255 (actual_end & (root->sectorsize - 1)) == 0) ||
257 data_len > root->fs_info->max_inline) {
261 path = btrfs_alloc_path();
265 trans = btrfs_join_transaction(root);
267 btrfs_free_path(path);
268 return PTR_ERR(trans);
270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
276 extent_item_size = btrfs_file_extent_calc_inline_size(
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
283 btrfs_abort_transaction(trans, root, ret);
287 if (isize > actual_end)
288 inline_len = min_t(u64, isize, actual_end);
289 ret = insert_inline_extent(trans, path, extent_inserted,
291 inline_len, compressed_size,
292 compress_type, compressed_pages);
293 if (ret && ret != -ENOSPC) {
294 btrfs_abort_transaction(trans, root, ret);
296 } else if (ret == -ENOSPC) {
301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
305 btrfs_free_path(path);
306 btrfs_end_transaction(trans, root);
310 struct async_extent {
315 unsigned long nr_pages;
317 struct list_head list;
322 struct btrfs_root *root;
323 struct page *locked_page;
326 struct list_head extents;
327 struct btrfs_work work;
330 static noinline int add_async_extent(struct async_cow *cow,
331 u64 start, u64 ram_size,
334 unsigned long nr_pages,
337 struct async_extent *async_extent;
339 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
340 BUG_ON(!async_extent); /* -ENOMEM */
341 async_extent->start = start;
342 async_extent->ram_size = ram_size;
343 async_extent->compressed_size = compressed_size;
344 async_extent->pages = pages;
345 async_extent->nr_pages = nr_pages;
346 async_extent->compress_type = compress_type;
347 list_add_tail(&async_extent->list, &cow->extents);
352 * we create compressed extents in two phases. The first
353 * phase compresses a range of pages that have already been
354 * locked (both pages and state bits are locked).
356 * This is done inside an ordered work queue, and the compression
357 * is spread across many cpus. The actual IO submission is step
358 * two, and the ordered work queue takes care of making sure that
359 * happens in the same order things were put onto the queue by
360 * writepages and friends.
362 * If this code finds it can't get good compression, it puts an
363 * entry onto the work queue to write the uncompressed bytes. This
364 * makes sure that both compressed inodes and uncompressed inodes
365 * are written in the same order that the flusher thread sent them
368 static noinline int compress_file_range(struct inode *inode,
369 struct page *locked_page,
371 struct async_cow *async_cow,
374 struct btrfs_root *root = BTRFS_I(inode)->root;
376 u64 blocksize = root->sectorsize;
378 u64 isize = i_size_read(inode);
380 struct page **pages = NULL;
381 unsigned long nr_pages;
382 unsigned long nr_pages_ret = 0;
383 unsigned long total_compressed = 0;
384 unsigned long total_in = 0;
385 unsigned long max_compressed = 128 * 1024;
386 unsigned long max_uncompressed = 128 * 1024;
389 int compress_type = root->fs_info->compress_type;
392 /* if this is a small write inside eof, kick off a defrag */
393 if ((end - start + 1) < 16 * 1024 &&
394 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
395 btrfs_add_inode_defrag(NULL, inode);
398 * skip compression for a small file range(<=blocksize) that
399 * isn't an inline extent, since it dosen't save disk space at all.
401 if ((end - start + 1) <= blocksize &&
402 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
403 goto cleanup_and_bail_uncompressed;
405 actual_end = min_t(u64, isize, end + 1);
408 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
409 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
412 * we don't want to send crud past the end of i_size through
413 * compression, that's just a waste of CPU time. So, if the
414 * end of the file is before the start of our current
415 * requested range of bytes, we bail out to the uncompressed
416 * cleanup code that can deal with all of this.
418 * It isn't really the fastest way to fix things, but this is a
419 * very uncommon corner.
421 if (actual_end <= start)
422 goto cleanup_and_bail_uncompressed;
424 total_compressed = actual_end - start;
426 /* we want to make sure that amount of ram required to uncompress
427 * an extent is reasonable, so we limit the total size in ram
428 * of a compressed extent to 128k. This is a crucial number
429 * because it also controls how easily we can spread reads across
430 * cpus for decompression.
432 * We also want to make sure the amount of IO required to do
433 * a random read is reasonably small, so we limit the size of
434 * a compressed extent to 128k.
436 total_compressed = min(total_compressed, max_uncompressed);
437 num_bytes = ALIGN(end - start + 1, blocksize);
438 num_bytes = max(blocksize, num_bytes);
443 * we do compression for mount -o compress and when the
444 * inode has not been flagged as nocompress. This flag can
445 * change at any time if we discover bad compression ratios.
447 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
448 (btrfs_test_opt(root, COMPRESS) ||
449 (BTRFS_I(inode)->force_compress) ||
450 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
452 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
454 /* just bail out to the uncompressed code */
458 if (BTRFS_I(inode)->force_compress)
459 compress_type = BTRFS_I(inode)->force_compress;
462 * we need to call clear_page_dirty_for_io on each
463 * page in the range. Otherwise applications with the file
464 * mmap'd can wander in and change the page contents while
465 * we are compressing them.
467 * If the compression fails for any reason, we set the pages
468 * dirty again later on.
470 extent_range_clear_dirty_for_io(inode, start, end);
472 ret = btrfs_compress_pages(compress_type,
473 inode->i_mapping, start,
474 total_compressed, pages,
475 nr_pages, &nr_pages_ret,
481 unsigned long offset = total_compressed &
482 (PAGE_CACHE_SIZE - 1);
483 struct page *page = pages[nr_pages_ret - 1];
486 /* zero the tail end of the last page, we might be
487 * sending it down to disk
490 kaddr = kmap_atomic(page);
491 memset(kaddr + offset, 0,
492 PAGE_CACHE_SIZE - offset);
493 kunmap_atomic(kaddr);
500 /* lets try to make an inline extent */
501 if (ret || total_in < (actual_end - start)) {
502 /* we didn't compress the entire range, try
503 * to make an uncompressed inline extent.
505 ret = cow_file_range_inline(root, inode, start, end,
508 /* try making a compressed inline extent */
509 ret = cow_file_range_inline(root, inode, start, end,
511 compress_type, pages);
514 unsigned long clear_flags = EXTENT_DELALLOC |
516 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
519 * inline extent creation worked or returned error,
520 * we don't need to create any more async work items.
521 * Unlock and free up our temp pages.
523 extent_clear_unlock_delalloc(inode, start, end, NULL,
524 clear_flags, PAGE_UNLOCK |
534 * we aren't doing an inline extent round the compressed size
535 * up to a block size boundary so the allocator does sane
538 total_compressed = ALIGN(total_compressed, blocksize);
541 * one last check to make sure the compression is really a
542 * win, compare the page count read with the blocks on disk
544 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
545 if (total_compressed >= total_in) {
548 num_bytes = total_in;
551 if (!will_compress && pages) {
553 * the compression code ran but failed to make things smaller,
554 * free any pages it allocated and our page pointer array
556 for (i = 0; i < nr_pages_ret; i++) {
557 WARN_ON(pages[i]->mapping);
558 page_cache_release(pages[i]);
562 total_compressed = 0;
565 /* flag the file so we don't compress in the future */
566 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
567 !(BTRFS_I(inode)->force_compress)) {
568 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
574 /* the async work queues will take care of doing actual
575 * allocation on disk for these compressed pages,
576 * and will submit them to the elevator.
578 add_async_extent(async_cow, start, num_bytes,
579 total_compressed, pages, nr_pages_ret,
582 if (start + num_bytes < end) {
589 cleanup_and_bail_uncompressed:
591 * No compression, but we still need to write the pages in
592 * the file we've been given so far. redirty the locked
593 * page if it corresponds to our extent and set things up
594 * for the async work queue to run cow_file_range to do
595 * the normal delalloc dance
597 if (page_offset(locked_page) >= start &&
598 page_offset(locked_page) <= end) {
599 __set_page_dirty_nobuffers(locked_page);
600 /* unlocked later on in the async handlers */
603 extent_range_redirty_for_io(inode, start, end);
604 add_async_extent(async_cow, start, end - start + 1,
605 0, NULL, 0, BTRFS_COMPRESS_NONE);
613 for (i = 0; i < nr_pages_ret; i++) {
614 WARN_ON(pages[i]->mapping);
615 page_cache_release(pages[i]);
623 * phase two of compressed writeback. This is the ordered portion
624 * of the code, which only gets called in the order the work was
625 * queued. We walk all the async extents created by compress_file_range
626 * and send them down to the disk.
628 static noinline int submit_compressed_extents(struct inode *inode,
629 struct async_cow *async_cow)
631 struct async_extent *async_extent;
633 struct btrfs_key ins;
634 struct extent_map *em;
635 struct btrfs_root *root = BTRFS_I(inode)->root;
636 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
637 struct extent_io_tree *io_tree;
640 if (list_empty(&async_cow->extents))
644 while (!list_empty(&async_cow->extents)) {
645 async_extent = list_entry(async_cow->extents.next,
646 struct async_extent, list);
647 list_del(&async_extent->list);
649 io_tree = &BTRFS_I(inode)->io_tree;
652 /* did the compression code fall back to uncompressed IO? */
653 if (!async_extent->pages) {
654 int page_started = 0;
655 unsigned long nr_written = 0;
657 lock_extent(io_tree, async_extent->start,
658 async_extent->start +
659 async_extent->ram_size - 1);
661 /* allocate blocks */
662 ret = cow_file_range(inode, async_cow->locked_page,
664 async_extent->start +
665 async_extent->ram_size - 1,
666 &page_started, &nr_written, 0);
671 * if page_started, cow_file_range inserted an
672 * inline extent and took care of all the unlocking
673 * and IO for us. Otherwise, we need to submit
674 * all those pages down to the drive.
676 if (!page_started && !ret)
677 extent_write_locked_range(io_tree,
678 inode, async_extent->start,
679 async_extent->start +
680 async_extent->ram_size - 1,
684 unlock_page(async_cow->locked_page);
690 lock_extent(io_tree, async_extent->start,
691 async_extent->start + async_extent->ram_size - 1);
693 ret = btrfs_reserve_extent(root,
694 async_extent->compressed_size,
695 async_extent->compressed_size,
696 0, alloc_hint, &ins, 1, 1);
700 for (i = 0; i < async_extent->nr_pages; i++) {
701 WARN_ON(async_extent->pages[i]->mapping);
702 page_cache_release(async_extent->pages[i]);
704 kfree(async_extent->pages);
705 async_extent->nr_pages = 0;
706 async_extent->pages = NULL;
708 if (ret == -ENOSPC) {
709 unlock_extent(io_tree, async_extent->start,
710 async_extent->start +
711 async_extent->ram_size - 1);
714 * we need to redirty the pages if we decide to
715 * fallback to uncompressed IO, otherwise we
716 * will not submit these pages down to lower
719 extent_range_redirty_for_io(inode,
721 async_extent->start +
722 async_extent->ram_size - 1);
730 * here we're doing allocation and writeback of the
733 btrfs_drop_extent_cache(inode, async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1, 0);
737 em = alloc_extent_map();
740 goto out_free_reserve;
742 em->start = async_extent->start;
743 em->len = async_extent->ram_size;
744 em->orig_start = em->start;
745 em->mod_start = em->start;
746 em->mod_len = em->len;
748 em->block_start = ins.objectid;
749 em->block_len = ins.offset;
750 em->orig_block_len = ins.offset;
751 em->ram_bytes = async_extent->ram_size;
752 em->bdev = root->fs_info->fs_devices->latest_bdev;
753 em->compress_type = async_extent->compress_type;
754 set_bit(EXTENT_FLAG_PINNED, &em->flags);
755 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
759 write_lock(&em_tree->lock);
760 ret = add_extent_mapping(em_tree, em, 1);
761 write_unlock(&em_tree->lock);
762 if (ret != -EEXIST) {
766 btrfs_drop_extent_cache(inode, async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1, 0);
772 goto out_free_reserve;
774 ret = btrfs_add_ordered_extent_compress(inode,
777 async_extent->ram_size,
779 BTRFS_ORDERED_COMPRESSED,
780 async_extent->compress_type);
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
785 goto out_free_reserve;
789 * clear dirty, set writeback and unlock the pages.
791 extent_clear_unlock_delalloc(inode, async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1,
794 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
795 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
797 ret = btrfs_submit_compressed_write(inode,
799 async_extent->ram_size,
801 ins.offset, async_extent->pages,
802 async_extent->nr_pages);
803 alloc_hint = ins.objectid + ins.offset;
813 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
815 extent_clear_unlock_delalloc(inode, async_extent->start,
816 async_extent->start +
817 async_extent->ram_size - 1,
818 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
819 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
820 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
821 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
826 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
829 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
830 struct extent_map *em;
833 read_lock(&em_tree->lock);
834 em = search_extent_mapping(em_tree, start, num_bytes);
837 * if block start isn't an actual block number then find the
838 * first block in this inode and use that as a hint. If that
839 * block is also bogus then just don't worry about it.
841 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
843 em = search_extent_mapping(em_tree, 0, 0);
844 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
845 alloc_hint = em->block_start;
849 alloc_hint = em->block_start;
853 read_unlock(&em_tree->lock);
859 * when extent_io.c finds a delayed allocation range in the file,
860 * the call backs end up in this code. The basic idea is to
861 * allocate extents on disk for the range, and create ordered data structs
862 * in ram to track those extents.
864 * locked_page is the page that writepage had locked already. We use
865 * it to make sure we don't do extra locks or unlocks.
867 * *page_started is set to one if we unlock locked_page and do everything
868 * required to start IO on it. It may be clean and already done with
871 static noinline int cow_file_range(struct inode *inode,
872 struct page *locked_page,
873 u64 start, u64 end, int *page_started,
874 unsigned long *nr_written,
877 struct btrfs_root *root = BTRFS_I(inode)->root;
880 unsigned long ram_size;
883 u64 blocksize = root->sectorsize;
884 struct btrfs_key ins;
885 struct extent_map *em;
886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
889 if (btrfs_is_free_space_inode(inode)) {
895 num_bytes = ALIGN(end - start + 1, blocksize);
896 num_bytes = max(blocksize, num_bytes);
897 disk_num_bytes = num_bytes;
899 /* if this is a small write inside eof, kick off defrag */
900 if (num_bytes < 64 * 1024 &&
901 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
902 btrfs_add_inode_defrag(NULL, inode);
905 /* lets try to make an inline extent */
906 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
909 extent_clear_unlock_delalloc(inode, start, end, NULL,
910 EXTENT_LOCKED | EXTENT_DELALLOC |
911 EXTENT_DEFRAG, PAGE_UNLOCK |
912 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
915 *nr_written = *nr_written +
916 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
919 } else if (ret < 0) {
924 BUG_ON(disk_num_bytes >
925 btrfs_super_total_bytes(root->fs_info->super_copy));
927 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
928 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
930 while (disk_num_bytes > 0) {
933 cur_alloc_size = disk_num_bytes;
934 ret = btrfs_reserve_extent(root, cur_alloc_size,
935 root->sectorsize, 0, alloc_hint,
940 em = alloc_extent_map();
946 em->orig_start = em->start;
947 ram_size = ins.offset;
948 em->len = ins.offset;
949 em->mod_start = em->start;
950 em->mod_len = em->len;
952 em->block_start = ins.objectid;
953 em->block_len = ins.offset;
954 em->orig_block_len = ins.offset;
955 em->ram_bytes = ram_size;
956 em->bdev = root->fs_info->fs_devices->latest_bdev;
957 set_bit(EXTENT_FLAG_PINNED, &em->flags);
961 write_lock(&em_tree->lock);
962 ret = add_extent_mapping(em_tree, em, 1);
963 write_unlock(&em_tree->lock);
964 if (ret != -EEXIST) {
968 btrfs_drop_extent_cache(inode, start,
969 start + ram_size - 1, 0);
974 cur_alloc_size = ins.offset;
975 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
976 ram_size, cur_alloc_size, 0);
978 goto out_drop_extent_cache;
980 if (root->root_key.objectid ==
981 BTRFS_DATA_RELOC_TREE_OBJECTID) {
982 ret = btrfs_reloc_clone_csums(inode, start,
985 goto out_drop_extent_cache;
988 if (disk_num_bytes < cur_alloc_size)
991 /* we're not doing compressed IO, don't unlock the first
992 * page (which the caller expects to stay locked), don't
993 * clear any dirty bits and don't set any writeback bits
995 * Do set the Private2 bit so we know this page was properly
996 * setup for writepage
998 op = unlock ? PAGE_UNLOCK : 0;
999 op |= PAGE_SET_PRIVATE2;
1001 extent_clear_unlock_delalloc(inode, start,
1002 start + ram_size - 1, locked_page,
1003 EXTENT_LOCKED | EXTENT_DELALLOC,
1005 disk_num_bytes -= cur_alloc_size;
1006 num_bytes -= cur_alloc_size;
1007 alloc_hint = ins.objectid + ins.offset;
1008 start += cur_alloc_size;
1013 out_drop_extent_cache:
1014 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1016 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
1018 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1019 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1020 EXTENT_DELALLOC | EXTENT_DEFRAG,
1021 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1022 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1027 * work queue call back to started compression on a file and pages
1029 static noinline void async_cow_start(struct btrfs_work *work)
1031 struct async_cow *async_cow;
1033 async_cow = container_of(work, struct async_cow, work);
1035 compress_file_range(async_cow->inode, async_cow->locked_page,
1036 async_cow->start, async_cow->end, async_cow,
1038 if (num_added == 0) {
1039 btrfs_add_delayed_iput(async_cow->inode);
1040 async_cow->inode = NULL;
1045 * work queue call back to submit previously compressed pages
1047 static noinline void async_cow_submit(struct btrfs_work *work)
1049 struct async_cow *async_cow;
1050 struct btrfs_root *root;
1051 unsigned long nr_pages;
1053 async_cow = container_of(work, struct async_cow, work);
1055 root = async_cow->root;
1056 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1059 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
1061 waitqueue_active(&root->fs_info->async_submit_wait))
1062 wake_up(&root->fs_info->async_submit_wait);
1064 if (async_cow->inode)
1065 submit_compressed_extents(async_cow->inode, async_cow);
1068 static noinline void async_cow_free(struct btrfs_work *work)
1070 struct async_cow *async_cow;
1071 async_cow = container_of(work, struct async_cow, work);
1072 if (async_cow->inode)
1073 btrfs_add_delayed_iput(async_cow->inode);
1077 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1078 u64 start, u64 end, int *page_started,
1079 unsigned long *nr_written)
1081 struct async_cow *async_cow;
1082 struct btrfs_root *root = BTRFS_I(inode)->root;
1083 unsigned long nr_pages;
1085 int limit = 10 * 1024 * 1024;
1087 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1088 1, 0, NULL, GFP_NOFS);
1089 while (start < end) {
1090 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1091 BUG_ON(!async_cow); /* -ENOMEM */
1092 async_cow->inode = igrab(inode);
1093 async_cow->root = root;
1094 async_cow->locked_page = locked_page;
1095 async_cow->start = start;
1097 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
1100 cur_end = min(end, start + 512 * 1024 - 1);
1102 async_cow->end = cur_end;
1103 INIT_LIST_HEAD(&async_cow->extents);
1105 btrfs_init_work(&async_cow->work,
1106 btrfs_delalloc_helper,
1107 async_cow_start, async_cow_submit,
1110 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1112 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1114 btrfs_queue_work(root->fs_info->delalloc_workers,
1117 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1118 wait_event(root->fs_info->async_submit_wait,
1119 (atomic_read(&root->fs_info->async_delalloc_pages) <
1123 while (atomic_read(&root->fs_info->async_submit_draining) &&
1124 atomic_read(&root->fs_info->async_delalloc_pages)) {
1125 wait_event(root->fs_info->async_submit_wait,
1126 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1130 *nr_written += nr_pages;
1131 start = cur_end + 1;
1137 static noinline int csum_exist_in_range(struct btrfs_root *root,
1138 u64 bytenr, u64 num_bytes)
1141 struct btrfs_ordered_sum *sums;
1144 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1145 bytenr + num_bytes - 1, &list, 0);
1146 if (ret == 0 && list_empty(&list))
1149 while (!list_empty(&list)) {
1150 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1151 list_del(&sums->list);
1158 * when nowcow writeback call back. This checks for snapshots or COW copies
1159 * of the extents that exist in the file, and COWs the file as required.
1161 * If no cow copies or snapshots exist, we write directly to the existing
1164 static noinline int run_delalloc_nocow(struct inode *inode,
1165 struct page *locked_page,
1166 u64 start, u64 end, int *page_started, int force,
1167 unsigned long *nr_written)
1169 struct btrfs_root *root = BTRFS_I(inode)->root;
1170 struct btrfs_trans_handle *trans;
1171 struct extent_buffer *leaf;
1172 struct btrfs_path *path;
1173 struct btrfs_file_extent_item *fi;
1174 struct btrfs_key found_key;
1189 u64 ino = btrfs_ino(inode);
1191 path = btrfs_alloc_path();
1193 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1194 EXTENT_LOCKED | EXTENT_DELALLOC |
1195 EXTENT_DO_ACCOUNTING |
1196 EXTENT_DEFRAG, PAGE_UNLOCK |
1198 PAGE_SET_WRITEBACK |
1199 PAGE_END_WRITEBACK);
1203 nolock = btrfs_is_free_space_inode(inode);
1206 trans = btrfs_join_transaction_nolock(root);
1208 trans = btrfs_join_transaction(root);
1210 if (IS_ERR(trans)) {
1211 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1212 EXTENT_LOCKED | EXTENT_DELALLOC |
1213 EXTENT_DO_ACCOUNTING |
1214 EXTENT_DEFRAG, PAGE_UNLOCK |
1216 PAGE_SET_WRITEBACK |
1217 PAGE_END_WRITEBACK);
1218 btrfs_free_path(path);
1219 return PTR_ERR(trans);
1222 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1224 cow_start = (u64)-1;
1227 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1231 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1232 leaf = path->nodes[0];
1233 btrfs_item_key_to_cpu(leaf, &found_key,
1234 path->slots[0] - 1);
1235 if (found_key.objectid == ino &&
1236 found_key.type == BTRFS_EXTENT_DATA_KEY)
1241 leaf = path->nodes[0];
1242 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1243 ret = btrfs_next_leaf(root, path);
1248 leaf = path->nodes[0];
1254 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1256 if (found_key.objectid > ino ||
1257 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1258 found_key.offset > end)
1261 if (found_key.offset > cur_offset) {
1262 extent_end = found_key.offset;
1267 fi = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_file_extent_item);
1269 extent_type = btrfs_file_extent_type(leaf, fi);
1271 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1272 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1273 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1274 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1275 extent_offset = btrfs_file_extent_offset(leaf, fi);
1276 extent_end = found_key.offset +
1277 btrfs_file_extent_num_bytes(leaf, fi);
1279 btrfs_file_extent_disk_num_bytes(leaf, fi);
1280 if (extent_end <= start) {
1284 if (disk_bytenr == 0)
1286 if (btrfs_file_extent_compression(leaf, fi) ||
1287 btrfs_file_extent_encryption(leaf, fi) ||
1288 btrfs_file_extent_other_encoding(leaf, fi))
1290 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1292 if (btrfs_extent_readonly(root, disk_bytenr))
1294 if (btrfs_cross_ref_exist(trans, root, ino,
1296 extent_offset, disk_bytenr))
1298 disk_bytenr += extent_offset;
1299 disk_bytenr += cur_offset - found_key.offset;
1300 num_bytes = min(end + 1, extent_end) - cur_offset;
1302 * if there are pending snapshots for this root,
1303 * we fall into common COW way.
1306 err = btrfs_start_nocow_write(root);
1311 * force cow if csum exists in the range.
1312 * this ensure that csum for a given extent are
1313 * either valid or do not exist.
1315 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1318 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1319 extent_end = found_key.offset +
1320 btrfs_file_extent_inline_len(leaf,
1321 path->slots[0], fi);
1322 extent_end = ALIGN(extent_end, root->sectorsize);
1327 if (extent_end <= start) {
1329 if (!nolock && nocow)
1330 btrfs_end_nocow_write(root);
1334 if (cow_start == (u64)-1)
1335 cow_start = cur_offset;
1336 cur_offset = extent_end;
1337 if (cur_offset > end)
1343 btrfs_release_path(path);
1344 if (cow_start != (u64)-1) {
1345 ret = cow_file_range(inode, locked_page,
1346 cow_start, found_key.offset - 1,
1347 page_started, nr_written, 1);
1349 if (!nolock && nocow)
1350 btrfs_end_nocow_write(root);
1353 cow_start = (u64)-1;
1356 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1357 struct extent_map *em;
1358 struct extent_map_tree *em_tree;
1359 em_tree = &BTRFS_I(inode)->extent_tree;
1360 em = alloc_extent_map();
1361 BUG_ON(!em); /* -ENOMEM */
1362 em->start = cur_offset;
1363 em->orig_start = found_key.offset - extent_offset;
1364 em->len = num_bytes;
1365 em->block_len = num_bytes;
1366 em->block_start = disk_bytenr;
1367 em->orig_block_len = disk_num_bytes;
1368 em->ram_bytes = ram_bytes;
1369 em->bdev = root->fs_info->fs_devices->latest_bdev;
1370 em->mod_start = em->start;
1371 em->mod_len = em->len;
1372 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1373 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1374 em->generation = -1;
1376 write_lock(&em_tree->lock);
1377 ret = add_extent_mapping(em_tree, em, 1);
1378 write_unlock(&em_tree->lock);
1379 if (ret != -EEXIST) {
1380 free_extent_map(em);
1383 btrfs_drop_extent_cache(inode, em->start,
1384 em->start + em->len - 1, 0);
1386 type = BTRFS_ORDERED_PREALLOC;
1388 type = BTRFS_ORDERED_NOCOW;
1391 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1392 num_bytes, num_bytes, type);
1393 BUG_ON(ret); /* -ENOMEM */
1395 if (root->root_key.objectid ==
1396 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1397 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1400 if (!nolock && nocow)
1401 btrfs_end_nocow_write(root);
1406 extent_clear_unlock_delalloc(inode, cur_offset,
1407 cur_offset + num_bytes - 1,
1408 locked_page, EXTENT_LOCKED |
1409 EXTENT_DELALLOC, PAGE_UNLOCK |
1411 if (!nolock && nocow)
1412 btrfs_end_nocow_write(root);
1413 cur_offset = extent_end;
1414 if (cur_offset > end)
1417 btrfs_release_path(path);
1419 if (cur_offset <= end && cow_start == (u64)-1) {
1420 cow_start = cur_offset;
1424 if (cow_start != (u64)-1) {
1425 ret = cow_file_range(inode, locked_page, cow_start, end,
1426 page_started, nr_written, 1);
1432 err = btrfs_end_transaction(trans, root);
1436 if (ret && cur_offset < end)
1437 extent_clear_unlock_delalloc(inode, cur_offset, end,
1438 locked_page, EXTENT_LOCKED |
1439 EXTENT_DELALLOC | EXTENT_DEFRAG |
1440 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1442 PAGE_SET_WRITEBACK |
1443 PAGE_END_WRITEBACK);
1444 btrfs_free_path(path);
1449 * extent_io.c call back to do delayed allocation processing
1451 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1452 u64 start, u64 end, int *page_started,
1453 unsigned long *nr_written)
1456 struct btrfs_root *root = BTRFS_I(inode)->root;
1458 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
1459 ret = run_delalloc_nocow(inode, locked_page, start, end,
1460 page_started, 1, nr_written);
1461 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
1462 ret = run_delalloc_nocow(inode, locked_page, start, end,
1463 page_started, 0, nr_written);
1464 } else if (!btrfs_test_opt(root, COMPRESS) &&
1465 !(BTRFS_I(inode)->force_compress) &&
1466 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
1467 ret = cow_file_range(inode, locked_page, start, end,
1468 page_started, nr_written, 1);
1470 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1471 &BTRFS_I(inode)->runtime_flags);
1472 ret = cow_file_range_async(inode, locked_page, start, end,
1473 page_started, nr_written);
1478 static void btrfs_split_extent_hook(struct inode *inode,
1479 struct extent_state *orig, u64 split)
1481 /* not delalloc, ignore it */
1482 if (!(orig->state & EXTENT_DELALLOC))
1485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents++;
1487 spin_unlock(&BTRFS_I(inode)->lock);
1491 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1492 * extents so we can keep track of new extents that are just merged onto old
1493 * extents, such as when we are doing sequential writes, so we can properly
1494 * account for the metadata space we'll need.
1496 static void btrfs_merge_extent_hook(struct inode *inode,
1497 struct extent_state *new,
1498 struct extent_state *other)
1500 /* not delalloc, ignore it */
1501 if (!(other->state & EXTENT_DELALLOC))
1504 spin_lock(&BTRFS_I(inode)->lock);
1505 BTRFS_I(inode)->outstanding_extents--;
1506 spin_unlock(&BTRFS_I(inode)->lock);
1509 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1510 struct inode *inode)
1512 spin_lock(&root->delalloc_lock);
1513 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1514 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1515 &root->delalloc_inodes);
1516 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1517 &BTRFS_I(inode)->runtime_flags);
1518 root->nr_delalloc_inodes++;
1519 if (root->nr_delalloc_inodes == 1) {
1520 spin_lock(&root->fs_info->delalloc_root_lock);
1521 BUG_ON(!list_empty(&root->delalloc_root));
1522 list_add_tail(&root->delalloc_root,
1523 &root->fs_info->delalloc_roots);
1524 spin_unlock(&root->fs_info->delalloc_root_lock);
1527 spin_unlock(&root->delalloc_lock);
1530 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1531 struct inode *inode)
1533 spin_lock(&root->delalloc_lock);
1534 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1535 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1536 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1537 &BTRFS_I(inode)->runtime_flags);
1538 root->nr_delalloc_inodes--;
1539 if (!root->nr_delalloc_inodes) {
1540 spin_lock(&root->fs_info->delalloc_root_lock);
1541 BUG_ON(list_empty(&root->delalloc_root));
1542 list_del_init(&root->delalloc_root);
1543 spin_unlock(&root->fs_info->delalloc_root_lock);
1546 spin_unlock(&root->delalloc_lock);
1550 * extent_io.c set_bit_hook, used to track delayed allocation
1551 * bytes in this file, and to maintain the list of inodes that
1552 * have pending delalloc work to be done.
1554 static void btrfs_set_bit_hook(struct inode *inode,
1555 struct extent_state *state, unsigned long *bits)
1559 * set_bit and clear bit hooks normally require _irqsave/restore
1560 * but in this case, we are only testing for the DELALLOC
1561 * bit, which is only set or cleared with irqs on
1563 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1564 struct btrfs_root *root = BTRFS_I(inode)->root;
1565 u64 len = state->end + 1 - state->start;
1566 bool do_list = !btrfs_is_free_space_inode(inode);
1568 if (*bits & EXTENT_FIRST_DELALLOC) {
1569 *bits &= ~EXTENT_FIRST_DELALLOC;
1571 spin_lock(&BTRFS_I(inode)->lock);
1572 BTRFS_I(inode)->outstanding_extents++;
1573 spin_unlock(&BTRFS_I(inode)->lock);
1576 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1577 root->fs_info->delalloc_batch);
1578 spin_lock(&BTRFS_I(inode)->lock);
1579 BTRFS_I(inode)->delalloc_bytes += len;
1580 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1581 &BTRFS_I(inode)->runtime_flags))
1582 btrfs_add_delalloc_inodes(root, inode);
1583 spin_unlock(&BTRFS_I(inode)->lock);
1588 * extent_io.c clear_bit_hook, see set_bit_hook for why
1590 static void btrfs_clear_bit_hook(struct inode *inode,
1591 struct extent_state *state,
1592 unsigned long *bits)
1595 * set_bit and clear bit hooks normally require _irqsave/restore
1596 * but in this case, we are only testing for the DELALLOC
1597 * bit, which is only set or cleared with irqs on
1599 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1600 struct btrfs_root *root = BTRFS_I(inode)->root;
1601 u64 len = state->end + 1 - state->start;
1602 bool do_list = !btrfs_is_free_space_inode(inode);
1604 if (*bits & EXTENT_FIRST_DELALLOC) {
1605 *bits &= ~EXTENT_FIRST_DELALLOC;
1606 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1607 spin_lock(&BTRFS_I(inode)->lock);
1608 BTRFS_I(inode)->outstanding_extents--;
1609 spin_unlock(&BTRFS_I(inode)->lock);
1613 * We don't reserve metadata space for space cache inodes so we
1614 * don't need to call dellalloc_release_metadata if there is an
1617 if (*bits & EXTENT_DO_ACCOUNTING &&
1618 root != root->fs_info->tree_root)
1619 btrfs_delalloc_release_metadata(inode, len);
1621 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1622 && do_list && !(state->state & EXTENT_NORESERVE))
1623 btrfs_free_reserved_data_space(inode, len);
1625 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1626 root->fs_info->delalloc_batch);
1627 spin_lock(&BTRFS_I(inode)->lock);
1628 BTRFS_I(inode)->delalloc_bytes -= len;
1629 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1630 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1631 &BTRFS_I(inode)->runtime_flags))
1632 btrfs_del_delalloc_inode(root, inode);
1633 spin_unlock(&BTRFS_I(inode)->lock);
1638 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1639 * we don't create bios that span stripes or chunks
1641 int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
1642 size_t size, struct bio *bio,
1643 unsigned long bio_flags)
1645 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1646 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1651 if (bio_flags & EXTENT_BIO_COMPRESSED)
1654 length = bio->bi_iter.bi_size;
1655 map_length = length;
1656 ret = btrfs_map_block(root->fs_info, rw, logical,
1657 &map_length, NULL, 0);
1658 /* Will always return 0 with map_multi == NULL */
1660 if (map_length < length + size)
1666 * in order to insert checksums into the metadata in large chunks,
1667 * we wait until bio submission time. All the pages in the bio are
1668 * checksummed and sums are attached onto the ordered extent record.
1670 * At IO completion time the cums attached on the ordered extent record
1671 * are inserted into the btree
1673 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1674 struct bio *bio, int mirror_num,
1675 unsigned long bio_flags,
1678 struct btrfs_root *root = BTRFS_I(inode)->root;
1681 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1682 BUG_ON(ret); /* -ENOMEM */
1687 * in order to insert checksums into the metadata in large chunks,
1688 * we wait until bio submission time. All the pages in the bio are
1689 * checksummed and sums are attached onto the ordered extent record.
1691 * At IO completion time the cums attached on the ordered extent record
1692 * are inserted into the btree
1694 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1695 int mirror_num, unsigned long bio_flags,
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1701 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1703 bio_endio(bio, ret);
1708 * extent_io.c submission hook. This does the right thing for csum calculation
1709 * on write, or reading the csums from the tree before a read
1711 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1712 int mirror_num, unsigned long bio_flags,
1715 struct btrfs_root *root = BTRFS_I(inode)->root;
1719 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1721 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1723 if (btrfs_is_free_space_inode(inode))
1726 if (!(rw & REQ_WRITE)) {
1727 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1731 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1732 ret = btrfs_submit_compressed_read(inode, bio,
1736 } else if (!skip_sum) {
1737 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1742 } else if (async && !skip_sum) {
1743 /* csum items have already been cloned */
1744 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1746 /* we're doing a write, do the async checksumming */
1747 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1748 inode, rw, bio, mirror_num,
1749 bio_flags, bio_offset,
1750 __btrfs_submit_bio_start,
1751 __btrfs_submit_bio_done);
1753 } else if (!skip_sum) {
1754 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1760 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1764 bio_endio(bio, ret);
1769 * given a list of ordered sums record them in the inode. This happens
1770 * at IO completion time based on sums calculated at bio submission time.
1772 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1773 struct inode *inode, u64 file_offset,
1774 struct list_head *list)
1776 struct btrfs_ordered_sum *sum;
1778 list_for_each_entry(sum, list, list) {
1779 trans->adding_csums = 1;
1780 btrfs_csum_file_blocks(trans,
1781 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1782 trans->adding_csums = 0;
1787 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1788 struct extent_state **cached_state)
1790 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
1791 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1792 cached_state, GFP_NOFS);
1795 /* see btrfs_writepage_start_hook for details on why this is required */
1796 struct btrfs_writepage_fixup {
1798 struct btrfs_work work;
1801 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1803 struct btrfs_writepage_fixup *fixup;
1804 struct btrfs_ordered_extent *ordered;
1805 struct extent_state *cached_state = NULL;
1807 struct inode *inode;
1812 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1816 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1817 ClearPageChecked(page);
1821 inode = page->mapping->host;
1822 page_start = page_offset(page);
1823 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1825 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1828 /* already ordered? We're done */
1829 if (PagePrivate2(page))
1832 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1834 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1835 page_end, &cached_state, GFP_NOFS);
1837 btrfs_start_ordered_extent(inode, ordered, 1);
1838 btrfs_put_ordered_extent(ordered);
1842 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1844 mapping_set_error(page->mapping, ret);
1845 end_extent_writepage(page, ret, page_start, page_end);
1846 ClearPageChecked(page);
1850 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1851 ClearPageChecked(page);
1852 set_page_dirty(page);
1854 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1855 &cached_state, GFP_NOFS);
1858 page_cache_release(page);
1863 * There are a few paths in the higher layers of the kernel that directly
1864 * set the page dirty bit without asking the filesystem if it is a
1865 * good idea. This causes problems because we want to make sure COW
1866 * properly happens and the data=ordered rules are followed.
1868 * In our case any range that doesn't have the ORDERED bit set
1869 * hasn't been properly setup for IO. We kick off an async process
1870 * to fix it up. The async helper will wait for ordered extents, set
1871 * the delalloc bit and make it safe to write the page.
1873 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1875 struct inode *inode = page->mapping->host;
1876 struct btrfs_writepage_fixup *fixup;
1877 struct btrfs_root *root = BTRFS_I(inode)->root;
1879 /* this page is properly in the ordered list */
1880 if (TestClearPagePrivate2(page))
1883 if (PageChecked(page))
1886 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1890 SetPageChecked(page);
1891 page_cache_get(page);
1892 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
1893 btrfs_writepage_fixup_worker, NULL, NULL);
1895 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
1899 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1900 struct inode *inode, u64 file_pos,
1901 u64 disk_bytenr, u64 disk_num_bytes,
1902 u64 num_bytes, u64 ram_bytes,
1903 u8 compression, u8 encryption,
1904 u16 other_encoding, int extent_type)
1906 struct btrfs_root *root = BTRFS_I(inode)->root;
1907 struct btrfs_file_extent_item *fi;
1908 struct btrfs_path *path;
1909 struct extent_buffer *leaf;
1910 struct btrfs_key ins;
1911 int extent_inserted = 0;
1914 path = btrfs_alloc_path();
1919 * we may be replacing one extent in the tree with another.
1920 * The new extent is pinned in the extent map, and we don't want
1921 * to drop it from the cache until it is completely in the btree.
1923 * So, tell btrfs_drop_extents to leave this extent in the cache.
1924 * the caller is expected to unpin it and allow it to be merged
1927 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1928 file_pos + num_bytes, NULL, 0,
1929 1, sizeof(*fi), &extent_inserted);
1933 if (!extent_inserted) {
1934 ins.objectid = btrfs_ino(inode);
1935 ins.offset = file_pos;
1936 ins.type = BTRFS_EXTENT_DATA_KEY;
1938 path->leave_spinning = 1;
1939 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1944 leaf = path->nodes[0];
1945 fi = btrfs_item_ptr(leaf, path->slots[0],
1946 struct btrfs_file_extent_item);
1947 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1948 btrfs_set_file_extent_type(leaf, fi, extent_type);
1949 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1950 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1951 btrfs_set_file_extent_offset(leaf, fi, 0);
1952 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1953 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1954 btrfs_set_file_extent_compression(leaf, fi, compression);
1955 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1956 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1958 btrfs_mark_buffer_dirty(leaf);
1959 btrfs_release_path(path);
1961 inode_add_bytes(inode, num_bytes);
1963 ins.objectid = disk_bytenr;
1964 ins.offset = disk_num_bytes;
1965 ins.type = BTRFS_EXTENT_ITEM_KEY;
1966 ret = btrfs_alloc_reserved_file_extent(trans, root,
1967 root->root_key.objectid,
1968 btrfs_ino(inode), file_pos, &ins);
1970 btrfs_free_path(path);
1975 /* snapshot-aware defrag */
1976 struct sa_defrag_extent_backref {
1977 struct rb_node node;
1978 struct old_sa_defrag_extent *old;
1987 struct old_sa_defrag_extent {
1988 struct list_head list;
1989 struct new_sa_defrag_extent *new;
1998 struct new_sa_defrag_extent {
1999 struct rb_root root;
2000 struct list_head head;
2001 struct btrfs_path *path;
2002 struct inode *inode;
2010 static int backref_comp(struct sa_defrag_extent_backref *b1,
2011 struct sa_defrag_extent_backref *b2)
2013 if (b1->root_id < b2->root_id)
2015 else if (b1->root_id > b2->root_id)
2018 if (b1->inum < b2->inum)
2020 else if (b1->inum > b2->inum)
2023 if (b1->file_pos < b2->file_pos)
2025 else if (b1->file_pos > b2->file_pos)
2029 * [------------------------------] ===> (a range of space)
2030 * |<--->| |<---->| =============> (fs/file tree A)
2031 * |<---------------------------->| ===> (fs/file tree B)
2033 * A range of space can refer to two file extents in one tree while
2034 * refer to only one file extent in another tree.
2036 * So we may process a disk offset more than one time(two extents in A)
2037 * and locate at the same extent(one extent in B), then insert two same
2038 * backrefs(both refer to the extent in B).
2043 static void backref_insert(struct rb_root *root,
2044 struct sa_defrag_extent_backref *backref)
2046 struct rb_node **p = &root->rb_node;
2047 struct rb_node *parent = NULL;
2048 struct sa_defrag_extent_backref *entry;
2053 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2055 ret = backref_comp(backref, entry);
2059 p = &(*p)->rb_right;
2062 rb_link_node(&backref->node, parent, p);
2063 rb_insert_color(&backref->node, root);
2067 * Note the backref might has changed, and in this case we just return 0.
2069 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2072 struct btrfs_file_extent_item *extent;
2073 struct btrfs_fs_info *fs_info;
2074 struct old_sa_defrag_extent *old = ctx;
2075 struct new_sa_defrag_extent *new = old->new;
2076 struct btrfs_path *path = new->path;
2077 struct btrfs_key key;
2078 struct btrfs_root *root;
2079 struct sa_defrag_extent_backref *backref;
2080 struct extent_buffer *leaf;
2081 struct inode *inode = new->inode;
2087 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2088 inum == btrfs_ino(inode))
2091 key.objectid = root_id;
2092 key.type = BTRFS_ROOT_ITEM_KEY;
2093 key.offset = (u64)-1;
2095 fs_info = BTRFS_I(inode)->root->fs_info;
2096 root = btrfs_read_fs_root_no_name(fs_info, &key);
2098 if (PTR_ERR(root) == -ENOENT)
2101 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2102 inum, offset, root_id);
2103 return PTR_ERR(root);
2106 key.objectid = inum;
2107 key.type = BTRFS_EXTENT_DATA_KEY;
2108 if (offset > (u64)-1 << 32)
2111 key.offset = offset;
2113 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2114 if (WARN_ON(ret < 0))
2121 leaf = path->nodes[0];
2122 slot = path->slots[0];
2124 if (slot >= btrfs_header_nritems(leaf)) {
2125 ret = btrfs_next_leaf(root, path);
2128 } else if (ret > 0) {
2137 btrfs_item_key_to_cpu(leaf, &key, slot);
2139 if (key.objectid > inum)
2142 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2145 extent = btrfs_item_ptr(leaf, slot,
2146 struct btrfs_file_extent_item);
2148 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2152 * 'offset' refers to the exact key.offset,
2153 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2154 * (key.offset - extent_offset).
2156 if (key.offset != offset)
2159 extent_offset = btrfs_file_extent_offset(leaf, extent);
2160 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2162 if (extent_offset >= old->extent_offset + old->offset +
2163 old->len || extent_offset + num_bytes <=
2164 old->extent_offset + old->offset)
2169 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2175 backref->root_id = root_id;
2176 backref->inum = inum;
2177 backref->file_pos = offset;
2178 backref->num_bytes = num_bytes;
2179 backref->extent_offset = extent_offset;
2180 backref->generation = btrfs_file_extent_generation(leaf, extent);
2182 backref_insert(&new->root, backref);
2185 btrfs_release_path(path);
2190 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2191 struct new_sa_defrag_extent *new)
2193 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2194 struct old_sa_defrag_extent *old, *tmp;
2199 list_for_each_entry_safe(old, tmp, &new->head, list) {
2200 ret = iterate_inodes_from_logical(old->bytenr +
2201 old->extent_offset, fs_info,
2202 path, record_one_backref,
2204 if (ret < 0 && ret != -ENOENT)
2207 /* no backref to be processed for this extent */
2209 list_del(&old->list);
2214 if (list_empty(&new->head))
2220 static int relink_is_mergable(struct extent_buffer *leaf,
2221 struct btrfs_file_extent_item *fi,
2222 struct new_sa_defrag_extent *new)
2224 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2227 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2230 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2233 if (btrfs_file_extent_encryption(leaf, fi) ||
2234 btrfs_file_extent_other_encoding(leaf, fi))
2241 * Note the backref might has changed, and in this case we just return 0.
2243 static noinline int relink_extent_backref(struct btrfs_path *path,
2244 struct sa_defrag_extent_backref *prev,
2245 struct sa_defrag_extent_backref *backref)
2247 struct btrfs_file_extent_item *extent;
2248 struct btrfs_file_extent_item *item;
2249 struct btrfs_ordered_extent *ordered;
2250 struct btrfs_trans_handle *trans;
2251 struct btrfs_fs_info *fs_info;
2252 struct btrfs_root *root;
2253 struct btrfs_key key;
2254 struct extent_buffer *leaf;
2255 struct old_sa_defrag_extent *old = backref->old;
2256 struct new_sa_defrag_extent *new = old->new;
2257 struct inode *src_inode = new->inode;
2258 struct inode *inode;
2259 struct extent_state *cached = NULL;
2268 if (prev && prev->root_id == backref->root_id &&
2269 prev->inum == backref->inum &&
2270 prev->file_pos + prev->num_bytes == backref->file_pos)
2273 /* step 1: get root */
2274 key.objectid = backref->root_id;
2275 key.type = BTRFS_ROOT_ITEM_KEY;
2276 key.offset = (u64)-1;
2278 fs_info = BTRFS_I(src_inode)->root->fs_info;
2279 index = srcu_read_lock(&fs_info->subvol_srcu);
2281 root = btrfs_read_fs_root_no_name(fs_info, &key);
2283 srcu_read_unlock(&fs_info->subvol_srcu, index);
2284 if (PTR_ERR(root) == -ENOENT)
2286 return PTR_ERR(root);
2289 if (btrfs_root_readonly(root)) {
2290 srcu_read_unlock(&fs_info->subvol_srcu, index);
2294 /* step 2: get inode */
2295 key.objectid = backref->inum;
2296 key.type = BTRFS_INODE_ITEM_KEY;
2299 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2300 if (IS_ERR(inode)) {
2301 srcu_read_unlock(&fs_info->subvol_srcu, index);
2305 srcu_read_unlock(&fs_info->subvol_srcu, index);
2307 /* step 3: relink backref */
2308 lock_start = backref->file_pos;
2309 lock_end = backref->file_pos + backref->num_bytes - 1;
2310 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2313 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2315 btrfs_put_ordered_extent(ordered);
2319 trans = btrfs_join_transaction(root);
2320 if (IS_ERR(trans)) {
2321 ret = PTR_ERR(trans);
2325 key.objectid = backref->inum;
2326 key.type = BTRFS_EXTENT_DATA_KEY;
2327 key.offset = backref->file_pos;
2329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2332 } else if (ret > 0) {
2337 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2338 struct btrfs_file_extent_item);
2340 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2341 backref->generation)
2344 btrfs_release_path(path);
2346 start = backref->file_pos;
2347 if (backref->extent_offset < old->extent_offset + old->offset)
2348 start += old->extent_offset + old->offset -
2349 backref->extent_offset;
2351 len = min(backref->extent_offset + backref->num_bytes,
2352 old->extent_offset + old->offset + old->len);
2353 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2355 ret = btrfs_drop_extents(trans, root, inode, start,
2360 key.objectid = btrfs_ino(inode);
2361 key.type = BTRFS_EXTENT_DATA_KEY;
2364 path->leave_spinning = 1;
2366 struct btrfs_file_extent_item *fi;
2368 struct btrfs_key found_key;
2370 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2375 leaf = path->nodes[0];
2376 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2378 fi = btrfs_item_ptr(leaf, path->slots[0],
2379 struct btrfs_file_extent_item);
2380 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2382 if (extent_len + found_key.offset == start &&
2383 relink_is_mergable(leaf, fi, new)) {
2384 btrfs_set_file_extent_num_bytes(leaf, fi,
2386 btrfs_mark_buffer_dirty(leaf);
2387 inode_add_bytes(inode, len);
2393 btrfs_release_path(path);
2398 ret = btrfs_insert_empty_item(trans, root, path, &key,
2401 btrfs_abort_transaction(trans, root, ret);
2405 leaf = path->nodes[0];
2406 item = btrfs_item_ptr(leaf, path->slots[0],
2407 struct btrfs_file_extent_item);
2408 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2409 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2410 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2411 btrfs_set_file_extent_num_bytes(leaf, item, len);
2412 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2413 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2414 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2415 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2416 btrfs_set_file_extent_encryption(leaf, item, 0);
2417 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2419 btrfs_mark_buffer_dirty(leaf);
2420 inode_add_bytes(inode, len);
2421 btrfs_release_path(path);
2423 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2425 backref->root_id, backref->inum,
2426 new->file_pos, 0); /* start - extent_offset */
2428 btrfs_abort_transaction(trans, root, ret);
2434 btrfs_release_path(path);
2435 path->leave_spinning = 0;
2436 btrfs_end_transaction(trans, root);
2438 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2444 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2446 struct old_sa_defrag_extent *old, *tmp;
2451 list_for_each_entry_safe(old, tmp, &new->head, list) {
2452 list_del(&old->list);
2458 static void relink_file_extents(struct new_sa_defrag_extent *new)
2460 struct btrfs_path *path;
2461 struct sa_defrag_extent_backref *backref;
2462 struct sa_defrag_extent_backref *prev = NULL;
2463 struct inode *inode;
2464 struct btrfs_root *root;
2465 struct rb_node *node;
2469 root = BTRFS_I(inode)->root;
2471 path = btrfs_alloc_path();
2475 if (!record_extent_backrefs(path, new)) {
2476 btrfs_free_path(path);
2479 btrfs_release_path(path);
2482 node = rb_first(&new->root);
2485 rb_erase(node, &new->root);
2487 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2489 ret = relink_extent_backref(path, prev, backref);
2502 btrfs_free_path(path);
2504 free_sa_defrag_extent(new);
2506 atomic_dec(&root->fs_info->defrag_running);
2507 wake_up(&root->fs_info->transaction_wait);
2510 static struct new_sa_defrag_extent *
2511 record_old_file_extents(struct inode *inode,
2512 struct btrfs_ordered_extent *ordered)
2514 struct btrfs_root *root = BTRFS_I(inode)->root;
2515 struct btrfs_path *path;
2516 struct btrfs_key key;
2517 struct old_sa_defrag_extent *old;
2518 struct new_sa_defrag_extent *new;
2521 new = kmalloc(sizeof(*new), GFP_NOFS);
2526 new->file_pos = ordered->file_offset;
2527 new->len = ordered->len;
2528 new->bytenr = ordered->start;
2529 new->disk_len = ordered->disk_len;
2530 new->compress_type = ordered->compress_type;
2531 new->root = RB_ROOT;
2532 INIT_LIST_HEAD(&new->head);
2534 path = btrfs_alloc_path();
2538 key.objectid = btrfs_ino(inode);
2539 key.type = BTRFS_EXTENT_DATA_KEY;
2540 key.offset = new->file_pos;
2542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2545 if (ret > 0 && path->slots[0] > 0)
2548 /* find out all the old extents for the file range */
2550 struct btrfs_file_extent_item *extent;
2551 struct extent_buffer *l;
2560 slot = path->slots[0];
2562 if (slot >= btrfs_header_nritems(l)) {
2563 ret = btrfs_next_leaf(root, path);
2571 btrfs_item_key_to_cpu(l, &key, slot);
2573 if (key.objectid != btrfs_ino(inode))
2575 if (key.type != BTRFS_EXTENT_DATA_KEY)
2577 if (key.offset >= new->file_pos + new->len)
2580 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2582 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2583 if (key.offset + num_bytes < new->file_pos)
2586 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2590 extent_offset = btrfs_file_extent_offset(l, extent);
2592 old = kmalloc(sizeof(*old), GFP_NOFS);
2596 offset = max(new->file_pos, key.offset);
2597 end = min(new->file_pos + new->len, key.offset + num_bytes);
2599 old->bytenr = disk_bytenr;
2600 old->extent_offset = extent_offset;
2601 old->offset = offset - key.offset;
2602 old->len = end - offset;
2605 list_add_tail(&old->list, &new->head);
2611 btrfs_free_path(path);
2612 atomic_inc(&root->fs_info->defrag_running);
2617 btrfs_free_path(path);
2619 free_sa_defrag_extent(new);
2623 static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2626 struct btrfs_block_group_cache *cache;
2628 cache = btrfs_lookup_block_group(root->fs_info, start);
2631 spin_lock(&cache->lock);
2632 cache->delalloc_bytes -= len;
2633 spin_unlock(&cache->lock);
2635 btrfs_put_block_group(cache);
2638 /* as ordered data IO finishes, this gets called so we can finish
2639 * an ordered extent if the range of bytes in the file it covers are
2642 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2644 struct inode *inode = ordered_extent->inode;
2645 struct btrfs_root *root = BTRFS_I(inode)->root;
2646 struct btrfs_trans_handle *trans = NULL;
2647 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2648 struct extent_state *cached_state = NULL;
2649 struct new_sa_defrag_extent *new = NULL;
2650 int compress_type = 0;
2652 u64 logical_len = ordered_extent->len;
2654 bool truncated = false;
2656 nolock = btrfs_is_free_space_inode(inode);
2658 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2663 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2665 logical_len = ordered_extent->truncated_len;
2666 /* Truncated the entire extent, don't bother adding */
2671 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2672 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2673 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2675 trans = btrfs_join_transaction_nolock(root);
2677 trans = btrfs_join_transaction(root);
2678 if (IS_ERR(trans)) {
2679 ret = PTR_ERR(trans);
2683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2684 ret = btrfs_update_inode_fallback(trans, root, inode);
2685 if (ret) /* -ENOMEM or corruption */
2686 btrfs_abort_transaction(trans, root, ret);
2690 lock_extent_bits(io_tree, ordered_extent->file_offset,
2691 ordered_extent->file_offset + ordered_extent->len - 1,
2694 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2695 ordered_extent->file_offset + ordered_extent->len - 1,
2696 EXTENT_DEFRAG, 1, cached_state);
2698 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2699 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2700 /* the inode is shared */
2701 new = record_old_file_extents(inode, ordered_extent);
2703 clear_extent_bit(io_tree, ordered_extent->file_offset,
2704 ordered_extent->file_offset + ordered_extent->len - 1,
2705 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2709 trans = btrfs_join_transaction_nolock(root);
2711 trans = btrfs_join_transaction(root);
2712 if (IS_ERR(trans)) {
2713 ret = PTR_ERR(trans);
2718 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2720 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2721 compress_type = ordered_extent->compress_type;
2722 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2723 BUG_ON(compress_type);
2724 ret = btrfs_mark_extent_written(trans, inode,
2725 ordered_extent->file_offset,
2726 ordered_extent->file_offset +
2729 BUG_ON(root == root->fs_info->tree_root);
2730 ret = insert_reserved_file_extent(trans, inode,
2731 ordered_extent->file_offset,
2732 ordered_extent->start,
2733 ordered_extent->disk_len,
2734 logical_len, logical_len,
2735 compress_type, 0, 0,
2736 BTRFS_FILE_EXTENT_REG);
2738 btrfs_release_delalloc_bytes(root,
2739 ordered_extent->start,
2740 ordered_extent->disk_len);
2742 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2743 ordered_extent->file_offset, ordered_extent->len,
2746 btrfs_abort_transaction(trans, root, ret);
2750 add_pending_csums(trans, inode, ordered_extent->file_offset,
2751 &ordered_extent->list);
2753 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2754 ret = btrfs_update_inode_fallback(trans, root, inode);
2755 if (ret) { /* -ENOMEM or corruption */
2756 btrfs_abort_transaction(trans, root, ret);
2761 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2762 ordered_extent->file_offset +
2763 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2765 if (root != root->fs_info->tree_root)
2766 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2768 btrfs_end_transaction(trans, root);
2770 if (ret || truncated) {
2774 start = ordered_extent->file_offset + logical_len;
2776 start = ordered_extent->file_offset;
2777 end = ordered_extent->file_offset + ordered_extent->len - 1;
2778 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2780 /* Drop the cache for the part of the extent we didn't write. */
2781 btrfs_drop_extent_cache(inode, start, end, 0);
2784 * If the ordered extent had an IOERR or something else went
2785 * wrong we need to return the space for this ordered extent
2786 * back to the allocator. We only free the extent in the
2787 * truncated case if we didn't write out the extent at all.
2789 if ((ret || !logical_len) &&
2790 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2791 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2792 btrfs_free_reserved_extent(root, ordered_extent->start,
2793 ordered_extent->disk_len, 1);
2798 * This needs to be done to make sure anybody waiting knows we are done
2799 * updating everything for this ordered extent.
2801 btrfs_remove_ordered_extent(inode, ordered_extent);
2803 /* for snapshot-aware defrag */
2806 free_sa_defrag_extent(new);
2807 atomic_dec(&root->fs_info->defrag_running);
2809 relink_file_extents(new);
2814 btrfs_put_ordered_extent(ordered_extent);
2815 /* once for the tree */
2816 btrfs_put_ordered_extent(ordered_extent);
2821 static void finish_ordered_fn(struct btrfs_work *work)
2823 struct btrfs_ordered_extent *ordered_extent;
2824 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2825 btrfs_finish_ordered_io(ordered_extent);
2828 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2829 struct extent_state *state, int uptodate)
2831 struct inode *inode = page->mapping->host;
2832 struct btrfs_root *root = BTRFS_I(inode)->root;
2833 struct btrfs_ordered_extent *ordered_extent = NULL;
2834 struct btrfs_workqueue *wq;
2835 btrfs_work_func_t func;
2837 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2839 ClearPagePrivate2(page);
2840 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2841 end - start + 1, uptodate))
2844 if (btrfs_is_free_space_inode(inode)) {
2845 wq = root->fs_info->endio_freespace_worker;
2846 func = btrfs_freespace_write_helper;
2848 wq = root->fs_info->endio_write_workers;
2849 func = btrfs_endio_write_helper;
2852 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
2854 btrfs_queue_work(wq, &ordered_extent->work);
2860 * when reads are done, we need to check csums to verify the data is correct
2861 * if there's a match, we allow the bio to finish. If not, the code in
2862 * extent_io.c will try to find good copies for us.
2864 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2865 u64 phy_offset, struct page *page,
2866 u64 start, u64 end, int mirror)
2868 size_t offset = start - page_offset(page);
2869 struct inode *inode = page->mapping->host;
2870 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2872 struct btrfs_root *root = BTRFS_I(inode)->root;
2875 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2876 DEFAULT_RATELIMIT_BURST);
2878 if (PageChecked(page)) {
2879 ClearPageChecked(page);
2883 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
2886 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
2887 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
2888 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2893 phy_offset >>= inode->i_sb->s_blocksize_bits;
2894 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
2896 kaddr = kmap_atomic(page);
2897 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
2898 btrfs_csum_final(csum, (char *)&csum);
2899 if (csum != csum_expected)
2902 kunmap_atomic(kaddr);
2907 if (__ratelimit(&_rs))
2908 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
2909 btrfs_ino(page->mapping->host), start, csum, csum_expected);
2910 memset(kaddr + offset, 1, end - start + 1);
2911 flush_dcache_page(page);
2912 kunmap_atomic(kaddr);
2913 if (csum_expected == 0)
2918 struct delayed_iput {
2919 struct list_head list;
2920 struct inode *inode;
2923 /* JDM: If this is fs-wide, why can't we add a pointer to
2924 * btrfs_inode instead and avoid the allocation? */
2925 void btrfs_add_delayed_iput(struct inode *inode)
2927 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2928 struct delayed_iput *delayed;
2930 if (atomic_add_unless(&inode->i_count, -1, 1))
2933 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2934 delayed->inode = inode;
2936 spin_lock(&fs_info->delayed_iput_lock);
2937 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2938 spin_unlock(&fs_info->delayed_iput_lock);
2941 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2944 struct btrfs_fs_info *fs_info = root->fs_info;
2945 struct delayed_iput *delayed;
2948 spin_lock(&fs_info->delayed_iput_lock);
2949 empty = list_empty(&fs_info->delayed_iputs);
2950 spin_unlock(&fs_info->delayed_iput_lock);
2954 spin_lock(&fs_info->delayed_iput_lock);
2955 list_splice_init(&fs_info->delayed_iputs, &list);
2956 spin_unlock(&fs_info->delayed_iput_lock);
2958 while (!list_empty(&list)) {
2959 delayed = list_entry(list.next, struct delayed_iput, list);
2960 list_del(&delayed->list);
2961 iput(delayed->inode);
2967 * This is called in transaction commit time. If there are no orphan
2968 * files in the subvolume, it removes orphan item and frees block_rsv
2971 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *root)
2974 struct btrfs_block_rsv *block_rsv;
2977 if (atomic_read(&root->orphan_inodes) ||
2978 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2981 spin_lock(&root->orphan_lock);
2982 if (atomic_read(&root->orphan_inodes)) {
2983 spin_unlock(&root->orphan_lock);
2987 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2988 spin_unlock(&root->orphan_lock);
2992 block_rsv = root->orphan_block_rsv;
2993 root->orphan_block_rsv = NULL;
2994 spin_unlock(&root->orphan_lock);
2996 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
2997 btrfs_root_refs(&root->root_item) > 0) {
2998 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2999 root->root_key.objectid);
3001 btrfs_abort_transaction(trans, root, ret);
3003 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3008 WARN_ON(block_rsv->size > 0);
3009 btrfs_free_block_rsv(root, block_rsv);
3014 * This creates an orphan entry for the given inode in case something goes
3015 * wrong in the middle of an unlink/truncate.
3017 * NOTE: caller of this function should reserve 5 units of metadata for
3020 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3022 struct btrfs_root *root = BTRFS_I(inode)->root;
3023 struct btrfs_block_rsv *block_rsv = NULL;
3028 if (!root->orphan_block_rsv) {
3029 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
3034 spin_lock(&root->orphan_lock);
3035 if (!root->orphan_block_rsv) {
3036 root->orphan_block_rsv = block_rsv;
3037 } else if (block_rsv) {
3038 btrfs_free_block_rsv(root, block_rsv);
3042 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3043 &BTRFS_I(inode)->runtime_flags)) {
3046 * For proper ENOSPC handling, we should do orphan
3047 * cleanup when mounting. But this introduces backward
3048 * compatibility issue.
3050 if (!xchg(&root->orphan_item_inserted, 1))
3056 atomic_inc(&root->orphan_inodes);
3059 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3060 &BTRFS_I(inode)->runtime_flags))
3062 spin_unlock(&root->orphan_lock);
3064 /* grab metadata reservation from transaction handle */
3066 ret = btrfs_orphan_reserve_metadata(trans, inode);
3067 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
3070 /* insert an orphan item to track this unlinked/truncated file */
3072 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
3074 atomic_dec(&root->orphan_inodes);
3076 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3077 &BTRFS_I(inode)->runtime_flags);
3078 btrfs_orphan_release_metadata(inode);
3080 if (ret != -EEXIST) {
3081 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3082 &BTRFS_I(inode)->runtime_flags);
3083 btrfs_abort_transaction(trans, root, ret);
3090 /* insert an orphan item to track subvolume contains orphan files */
3092 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3093 root->root_key.objectid);
3094 if (ret && ret != -EEXIST) {
3095 btrfs_abort_transaction(trans, root, ret);
3103 * We have done the truncate/delete so we can go ahead and remove the orphan
3104 * item for this particular inode.
3106 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3107 struct inode *inode)
3109 struct btrfs_root *root = BTRFS_I(inode)->root;
3110 int delete_item = 0;
3111 int release_rsv = 0;
3114 spin_lock(&root->orphan_lock);
3115 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3116 &BTRFS_I(inode)->runtime_flags))
3119 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3120 &BTRFS_I(inode)->runtime_flags))
3122 spin_unlock(&root->orphan_lock);
3125 atomic_dec(&root->orphan_inodes);
3127 ret = btrfs_del_orphan_item(trans, root,
3132 btrfs_orphan_release_metadata(inode);
3138 * this cleans up any orphans that may be left on the list from the last use
3141 int btrfs_orphan_cleanup(struct btrfs_root *root)
3143 struct btrfs_path *path;
3144 struct extent_buffer *leaf;
3145 struct btrfs_key key, found_key;
3146 struct btrfs_trans_handle *trans;
3147 struct inode *inode;
3148 u64 last_objectid = 0;
3149 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3151 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3154 path = btrfs_alloc_path();
3161 key.objectid = BTRFS_ORPHAN_OBJECTID;
3162 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3163 key.offset = (u64)-1;
3166 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3171 * if ret == 0 means we found what we were searching for, which
3172 * is weird, but possible, so only screw with path if we didn't
3173 * find the key and see if we have stuff that matches
3177 if (path->slots[0] == 0)
3182 /* pull out the item */
3183 leaf = path->nodes[0];
3184 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3186 /* make sure the item matches what we want */
3187 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3189 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3192 /* release the path since we're done with it */
3193 btrfs_release_path(path);
3196 * this is where we are basically btrfs_lookup, without the
3197 * crossing root thing. we store the inode number in the
3198 * offset of the orphan item.
3201 if (found_key.offset == last_objectid) {
3202 btrfs_err(root->fs_info,
3203 "Error removing orphan entry, stopping orphan cleanup");
3208 last_objectid = found_key.offset;
3210 found_key.objectid = found_key.offset;
3211 found_key.type = BTRFS_INODE_ITEM_KEY;
3212 found_key.offset = 0;
3213 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
3214 ret = PTR_ERR_OR_ZERO(inode);
3215 if (ret && ret != -ESTALE)
3218 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3219 struct btrfs_root *dead_root;
3220 struct btrfs_fs_info *fs_info = root->fs_info;
3221 int is_dead_root = 0;
3224 * this is an orphan in the tree root. Currently these
3225 * could come from 2 sources:
3226 * a) a snapshot deletion in progress
3227 * b) a free space cache inode
3228 * We need to distinguish those two, as the snapshot
3229 * orphan must not get deleted.
3230 * find_dead_roots already ran before us, so if this
3231 * is a snapshot deletion, we should find the root
3232 * in the dead_roots list
3234 spin_lock(&fs_info->trans_lock);
3235 list_for_each_entry(dead_root, &fs_info->dead_roots,
3237 if (dead_root->root_key.objectid ==
3238 found_key.objectid) {
3243 spin_unlock(&fs_info->trans_lock);
3245 /* prevent this orphan from being found again */
3246 key.offset = found_key.objectid - 1;
3251 * Inode is already gone but the orphan item is still there,
3252 * kill the orphan item.
3254 if (ret == -ESTALE) {
3255 trans = btrfs_start_transaction(root, 1);
3256 if (IS_ERR(trans)) {
3257 ret = PTR_ERR(trans);
3260 btrfs_debug(root->fs_info, "auto deleting %Lu",
3261 found_key.objectid);
3262 ret = btrfs_del_orphan_item(trans, root,
3263 found_key.objectid);
3264 btrfs_end_transaction(trans, root);
3271 * add this inode to the orphan list so btrfs_orphan_del does
3272 * the proper thing when we hit it
3274 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3275 &BTRFS_I(inode)->runtime_flags);
3276 atomic_inc(&root->orphan_inodes);
3278 /* if we have links, this was a truncate, lets do that */
3279 if (inode->i_nlink) {
3280 if (WARN_ON(!S_ISREG(inode->i_mode))) {
3286 /* 1 for the orphan item deletion. */
3287 trans = btrfs_start_transaction(root, 1);
3288 if (IS_ERR(trans)) {
3290 ret = PTR_ERR(trans);
3293 ret = btrfs_orphan_add(trans, inode);
3294 btrfs_end_transaction(trans, root);
3300 ret = btrfs_truncate(inode);
3302 btrfs_orphan_del(NULL, inode);
3307 /* this will do delete_inode and everything for us */
3312 /* release the path since we're done with it */
3313 btrfs_release_path(path);
3315 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3317 if (root->orphan_block_rsv)
3318 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3321 if (root->orphan_block_rsv ||
3322 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3323 trans = btrfs_join_transaction(root);
3325 btrfs_end_transaction(trans, root);
3329 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
3331 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
3335 btrfs_crit(root->fs_info,
3336 "could not do orphan cleanup %d", ret);
3337 btrfs_free_path(path);
3342 * very simple check to peek ahead in the leaf looking for xattrs. If we
3343 * don't find any xattrs, we know there can't be any acls.
3345 * slot is the slot the inode is in, objectid is the objectid of the inode
3347 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3348 int slot, u64 objectid,
3349 int *first_xattr_slot)
3351 u32 nritems = btrfs_header_nritems(leaf);
3352 struct btrfs_key found_key;
3353 static u64 xattr_access = 0;
3354 static u64 xattr_default = 0;
3357 if (!xattr_access) {
3358 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3359 strlen(POSIX_ACL_XATTR_ACCESS));
3360 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3361 strlen(POSIX_ACL_XATTR_DEFAULT));
3365 *first_xattr_slot = -1;
3366 while (slot < nritems) {
3367 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3369 /* we found a different objectid, there must not be acls */
3370 if (found_key.objectid != objectid)
3373 /* we found an xattr, assume we've got an acl */
3374 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3375 if (*first_xattr_slot == -1)
3376 *first_xattr_slot = slot;
3377 if (found_key.offset == xattr_access ||
3378 found_key.offset == xattr_default)
3383 * we found a key greater than an xattr key, there can't
3384 * be any acls later on
3386 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3393 * it goes inode, inode backrefs, xattrs, extents,
3394 * so if there are a ton of hard links to an inode there can
3395 * be a lot of backrefs. Don't waste time searching too hard,
3396 * this is just an optimization
3401 /* we hit the end of the leaf before we found an xattr or
3402 * something larger than an xattr. We have to assume the inode
3405 if (*first_xattr_slot == -1)
3406 *first_xattr_slot = slot;
3411 * read an inode from the btree into the in-memory inode
3413 static void btrfs_read_locked_inode(struct inode *inode)
3415 struct btrfs_path *path;
3416 struct extent_buffer *leaf;
3417 struct btrfs_inode_item *inode_item;
3418 struct btrfs_timespec *tspec;
3419 struct btrfs_root *root = BTRFS_I(inode)->root;
3420 struct btrfs_key location;
3425 bool filled = false;
3426 int first_xattr_slot;
3428 ret = btrfs_fill_inode(inode, &rdev);
3432 path = btrfs_alloc_path();
3436 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3438 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3442 leaf = path->nodes[0];
3447 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3448 struct btrfs_inode_item);
3449 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3450 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3451 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3452 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3453 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3455 tspec = btrfs_inode_atime(inode_item);
3456 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3457 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3459 tspec = btrfs_inode_mtime(inode_item);
3460 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3461 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3463 tspec = btrfs_inode_ctime(inode_item);
3464 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3465 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3467 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3468 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3469 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3472 * If we were modified in the current generation and evicted from memory
3473 * and then re-read we need to do a full sync since we don't have any
3474 * idea about which extents were modified before we were evicted from
3477 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3479 &BTRFS_I(inode)->runtime_flags);
3481 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3482 inode->i_generation = BTRFS_I(inode)->generation;
3484 rdev = btrfs_inode_rdev(leaf, inode_item);
3486 BTRFS_I(inode)->index_cnt = (u64)-1;
3487 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3491 if (inode->i_nlink != 1 ||
3492 path->slots[0] >= btrfs_header_nritems(leaf))
3495 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3496 if (location.objectid != btrfs_ino(inode))
3499 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3500 if (location.type == BTRFS_INODE_REF_KEY) {
3501 struct btrfs_inode_ref *ref;
3503 ref = (struct btrfs_inode_ref *)ptr;
3504 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3505 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3506 struct btrfs_inode_extref *extref;
3508 extref = (struct btrfs_inode_extref *)ptr;
3509 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3514 * try to precache a NULL acl entry for files that don't have
3515 * any xattrs or acls
3517 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3518 btrfs_ino(inode), &first_xattr_slot);
3519 if (first_xattr_slot != -1) {
3520 path->slots[0] = first_xattr_slot;
3521 ret = btrfs_load_inode_props(inode, path);
3523 btrfs_err(root->fs_info,
3524 "error loading props for ino %llu (root %llu): %d",
3526 root->root_key.objectid, ret);
3528 btrfs_free_path(path);
3531 cache_no_acl(inode);
3533 switch (inode->i_mode & S_IFMT) {
3535 inode->i_mapping->a_ops = &btrfs_aops;
3536 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3537 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3538 inode->i_fop = &btrfs_file_operations;
3539 inode->i_op = &btrfs_file_inode_operations;
3542 inode->i_fop = &btrfs_dir_file_operations;
3543 if (root == root->fs_info->tree_root)
3544 inode->i_op = &btrfs_dir_ro_inode_operations;
3546 inode->i_op = &btrfs_dir_inode_operations;
3549 inode->i_op = &btrfs_symlink_inode_operations;
3550 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3551 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3554 inode->i_op = &btrfs_special_inode_operations;
3555 init_special_inode(inode, inode->i_mode, rdev);
3559 btrfs_update_iflags(inode);
3563 btrfs_free_path(path);
3564 make_bad_inode(inode);
3568 * given a leaf and an inode, copy the inode fields into the leaf
3570 static void fill_inode_item(struct btrfs_trans_handle *trans,
3571 struct extent_buffer *leaf,
3572 struct btrfs_inode_item *item,
3573 struct inode *inode)
3575 struct btrfs_map_token token;
3577 btrfs_init_map_token(&token);
3579 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3580 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3581 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3583 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3584 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3586 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3587 inode->i_atime.tv_sec, &token);
3588 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3589 inode->i_atime.tv_nsec, &token);
3591 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3592 inode->i_mtime.tv_sec, &token);
3593 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3594 inode->i_mtime.tv_nsec, &token);
3596 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3597 inode->i_ctime.tv_sec, &token);
3598 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3599 inode->i_ctime.tv_nsec, &token);
3601 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3603 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3605 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3606 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3607 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3608 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3609 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3613 * copy everything in the in-memory inode into the btree.
3615 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root, struct inode *inode)
3618 struct btrfs_inode_item *inode_item;
3619 struct btrfs_path *path;
3620 struct extent_buffer *leaf;
3623 path = btrfs_alloc_path();
3627 path->leave_spinning = 1;
3628 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3636 leaf = path->nodes[0];
3637 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3638 struct btrfs_inode_item);
3640 fill_inode_item(trans, leaf, inode_item, inode);
3641 btrfs_mark_buffer_dirty(leaf);
3642 btrfs_set_inode_last_trans(trans, inode);
3645 btrfs_free_path(path);
3650 * copy everything in the in-memory inode into the btree.
3652 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3653 struct btrfs_root *root, struct inode *inode)
3658 * If the inode is a free space inode, we can deadlock during commit
3659 * if we put it into the delayed code.
3661 * The data relocation inode should also be directly updated
3664 if (!btrfs_is_free_space_inode(inode)
3665 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
3666 btrfs_update_root_times(trans, root);
3668 ret = btrfs_delayed_update_inode(trans, root, inode);
3670 btrfs_set_inode_last_trans(trans, inode);
3674 return btrfs_update_inode_item(trans, root, inode);
3677 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3678 struct btrfs_root *root,
3679 struct inode *inode)
3683 ret = btrfs_update_inode(trans, root, inode);
3685 return btrfs_update_inode_item(trans, root, inode);
3690 * unlink helper that gets used here in inode.c and in the tree logging
3691 * recovery code. It remove a link in a directory with a given name, and
3692 * also drops the back refs in the inode to the directory
3694 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3695 struct btrfs_root *root,
3696 struct inode *dir, struct inode *inode,
3697 const char *name, int name_len)
3699 struct btrfs_path *path;
3701 struct extent_buffer *leaf;
3702 struct btrfs_dir_item *di;
3703 struct btrfs_key key;
3705 u64 ino = btrfs_ino(inode);
3706 u64 dir_ino = btrfs_ino(dir);
3708 path = btrfs_alloc_path();
3714 path->leave_spinning = 1;
3715 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3716 name, name_len, -1);
3725 leaf = path->nodes[0];
3726 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3727 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3730 btrfs_release_path(path);
3733 * If we don't have dir index, we have to get it by looking up
3734 * the inode ref, since we get the inode ref, remove it directly,
3735 * it is unnecessary to do delayed deletion.
3737 * But if we have dir index, needn't search inode ref to get it.
3738 * Since the inode ref is close to the inode item, it is better
3739 * that we delay to delete it, and just do this deletion when
3740 * we update the inode item.
3742 if (BTRFS_I(inode)->dir_index) {
3743 ret = btrfs_delayed_delete_inode_ref(inode);
3745 index = BTRFS_I(inode)->dir_index;
3750 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3753 btrfs_info(root->fs_info,
3754 "failed to delete reference to %.*s, inode %llu parent %llu",
3755 name_len, name, ino, dir_ino);
3756 btrfs_abort_transaction(trans, root, ret);
3760 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3762 btrfs_abort_transaction(trans, root, ret);
3766 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
3768 if (ret != 0 && ret != -ENOENT) {
3769 btrfs_abort_transaction(trans, root, ret);
3773 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3778 btrfs_abort_transaction(trans, root, ret);
3780 btrfs_free_path(path);
3784 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3785 inode_inc_iversion(inode);
3786 inode_inc_iversion(dir);
3787 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3788 ret = btrfs_update_inode(trans, root, dir);
3793 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3794 struct btrfs_root *root,
3795 struct inode *dir, struct inode *inode,
3796 const char *name, int name_len)
3799 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3802 ret = btrfs_update_inode(trans, root, inode);
3808 * helper to start transaction for unlink and rmdir.
3810 * unlink and rmdir are special in btrfs, they do not always free space, so
3811 * if we cannot make our reservations the normal way try and see if there is
3812 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3813 * allow the unlink to occur.
3815 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
3817 struct btrfs_trans_handle *trans;
3818 struct btrfs_root *root = BTRFS_I(dir)->root;
3822 * 1 for the possible orphan item
3823 * 1 for the dir item
3824 * 1 for the dir index
3825 * 1 for the inode ref
3828 trans = btrfs_start_transaction(root, 5);
3829 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3832 if (PTR_ERR(trans) == -ENOSPC) {
3833 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
3835 trans = btrfs_start_transaction(root, 0);
3838 ret = btrfs_cond_migrate_bytes(root->fs_info,
3839 &root->fs_info->trans_block_rsv,
3842 btrfs_end_transaction(trans, root);
3843 return ERR_PTR(ret);
3845 trans->block_rsv = &root->fs_info->trans_block_rsv;
3846 trans->bytes_reserved = num_bytes;
3851 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3853 struct btrfs_root *root = BTRFS_I(dir)->root;
3854 struct btrfs_trans_handle *trans;
3855 struct inode *inode = dentry->d_inode;
3858 trans = __unlink_start_trans(dir);
3860 return PTR_ERR(trans);
3862 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3864 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3865 dentry->d_name.name, dentry->d_name.len);
3869 if (inode->i_nlink == 0) {
3870 ret = btrfs_orphan_add(trans, inode);
3876 btrfs_end_transaction(trans, root);
3877 btrfs_btree_balance_dirty(root);
3881 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root,
3883 struct inode *dir, u64 objectid,
3884 const char *name, int name_len)
3886 struct btrfs_path *path;
3887 struct extent_buffer *leaf;
3888 struct btrfs_dir_item *di;
3889 struct btrfs_key key;
3892 u64 dir_ino = btrfs_ino(dir);
3894 path = btrfs_alloc_path();
3898 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3899 name, name_len, -1);
3900 if (IS_ERR_OR_NULL(di)) {
3908 leaf = path->nodes[0];
3909 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3910 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3911 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3913 btrfs_abort_transaction(trans, root, ret);
3916 btrfs_release_path(path);
3918 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3919 objectid, root->root_key.objectid,
3920 dir_ino, &index, name, name_len);
3922 if (ret != -ENOENT) {
3923 btrfs_abort_transaction(trans, root, ret);
3926 di = btrfs_search_dir_index_item(root, path, dir_ino,
3928 if (IS_ERR_OR_NULL(di)) {
3933 btrfs_abort_transaction(trans, root, ret);
3937 leaf = path->nodes[0];
3938 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3939 btrfs_release_path(path);
3942 btrfs_release_path(path);
3944 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3946 btrfs_abort_transaction(trans, root, ret);
3950 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3951 inode_inc_iversion(dir);
3952 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3953 ret = btrfs_update_inode_fallback(trans, root, dir);
3955 btrfs_abort_transaction(trans, root, ret);
3957 btrfs_free_path(path);
3961 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3963 struct inode *inode = dentry->d_inode;
3965 struct btrfs_root *root = BTRFS_I(dir)->root;
3966 struct btrfs_trans_handle *trans;
3968 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
3970 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3973 trans = __unlink_start_trans(dir);
3975 return PTR_ERR(trans);
3977 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3978 err = btrfs_unlink_subvol(trans, root, dir,
3979 BTRFS_I(inode)->location.objectid,
3980 dentry->d_name.name,
3981 dentry->d_name.len);
3985 err = btrfs_orphan_add(trans, inode);
3989 /* now the directory is empty */
3990 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3991 dentry->d_name.name, dentry->d_name.len);
3993 btrfs_i_size_write(inode, 0);
3995 btrfs_end_transaction(trans, root);
3996 btrfs_btree_balance_dirty(root);
4002 * this can truncate away extent items, csum items and directory items.
4003 * It starts at a high offset and removes keys until it can't find
4004 * any higher than new_size
4006 * csum items that cross the new i_size are truncated to the new size
4009 * min_type is the minimum key type to truncate down to. If set to 0, this
4010 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4012 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4013 struct btrfs_root *root,
4014 struct inode *inode,
4015 u64 new_size, u32 min_type)
4017 struct btrfs_path *path;
4018 struct extent_buffer *leaf;
4019 struct btrfs_file_extent_item *fi;
4020 struct btrfs_key key;
4021 struct btrfs_key found_key;
4022 u64 extent_start = 0;
4023 u64 extent_num_bytes = 0;
4024 u64 extent_offset = 0;
4026 u64 last_size = (u64)-1;
4027 u32 found_type = (u8)-1;
4030 int pending_del_nr = 0;
4031 int pending_del_slot = 0;
4032 int extent_type = -1;
4035 u64 ino = btrfs_ino(inode);
4037 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4039 path = btrfs_alloc_path();
4045 * We want to drop from the next block forward in case this new size is
4046 * not block aligned since we will be keeping the last block of the
4047 * extent just the way it is.
4049 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4050 root == root->fs_info->tree_root)
4051 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4052 root->sectorsize), (u64)-1, 0);
4055 * This function is also used to drop the items in the log tree before
4056 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4057 * it is used to drop the loged items. So we shouldn't kill the delayed
4060 if (min_type == 0 && root == BTRFS_I(inode)->root)
4061 btrfs_kill_delayed_inode_items(inode);
4064 key.offset = (u64)-1;
4068 path->leave_spinning = 1;
4069 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4076 /* there are no items in the tree for us to truncate, we're
4079 if (path->slots[0] == 0)
4086 leaf = path->nodes[0];
4087 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4088 found_type = btrfs_key_type(&found_key);
4090 if (found_key.objectid != ino)
4093 if (found_type < min_type)
4096 item_end = found_key.offset;
4097 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4098 fi = btrfs_item_ptr(leaf, path->slots[0],
4099 struct btrfs_file_extent_item);
4100 extent_type = btrfs_file_extent_type(leaf, fi);
4101 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4103 btrfs_file_extent_num_bytes(leaf, fi);
4104 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4105 item_end += btrfs_file_extent_inline_len(leaf,
4106 path->slots[0], fi);
4110 if (found_type > min_type) {
4113 if (item_end < new_size)
4115 if (found_key.offset >= new_size)
4121 /* FIXME, shrink the extent if the ref count is only 1 */
4122 if (found_type != BTRFS_EXTENT_DATA_KEY)
4126 last_size = found_key.offset;
4128 last_size = new_size;
4130 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4132 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4134 u64 orig_num_bytes =
4135 btrfs_file_extent_num_bytes(leaf, fi);
4136 extent_num_bytes = ALIGN(new_size -
4139 btrfs_set_file_extent_num_bytes(leaf, fi,
4141 num_dec = (orig_num_bytes -
4143 if (test_bit(BTRFS_ROOT_REF_COWS,
4146 inode_sub_bytes(inode, num_dec);
4147 btrfs_mark_buffer_dirty(leaf);
4150 btrfs_file_extent_disk_num_bytes(leaf,
4152 extent_offset = found_key.offset -
4153 btrfs_file_extent_offset(leaf, fi);
4155 /* FIXME blocksize != 4096 */
4156 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4157 if (extent_start != 0) {
4159 if (test_bit(BTRFS_ROOT_REF_COWS,
4161 inode_sub_bytes(inode, num_dec);
4164 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4166 * we can't truncate inline items that have had
4170 btrfs_file_extent_compression(leaf, fi) == 0 &&
4171 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4172 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4173 u32 size = new_size - found_key.offset;
4175 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4176 inode_sub_bytes(inode, item_end + 1 -
4180 * update the ram bytes to properly reflect
4181 * the new size of our item
4183 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4185 btrfs_file_extent_calc_inline_size(size);
4186 btrfs_truncate_item(root, path, size, 1);
4187 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4189 inode_sub_bytes(inode, item_end + 1 -
4195 if (!pending_del_nr) {
4196 /* no pending yet, add ourselves */
4197 pending_del_slot = path->slots[0];
4199 } else if (pending_del_nr &&
4200 path->slots[0] + 1 == pending_del_slot) {
4201 /* hop on the pending chunk */
4203 pending_del_slot = path->slots[0];
4211 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4212 root == root->fs_info->tree_root)) {
4213 btrfs_set_path_blocking(path);
4214 ret = btrfs_free_extent(trans, root, extent_start,
4215 extent_num_bytes, 0,
4216 btrfs_header_owner(leaf),
4217 ino, extent_offset, 0);
4221 if (found_type == BTRFS_INODE_ITEM_KEY)
4224 if (path->slots[0] == 0 ||
4225 path->slots[0] != pending_del_slot) {
4226 if (pending_del_nr) {
4227 ret = btrfs_del_items(trans, root, path,
4231 btrfs_abort_transaction(trans,
4237 btrfs_release_path(path);
4244 if (pending_del_nr) {
4245 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4248 btrfs_abort_transaction(trans, root, ret);
4251 if (last_size != (u64)-1 &&
4252 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4253 btrfs_ordered_update_i_size(inode, last_size, NULL);
4254 btrfs_free_path(path);
4259 * btrfs_truncate_page - read, zero a chunk and write a page
4260 * @inode - inode that we're zeroing
4261 * @from - the offset to start zeroing
4262 * @len - the length to zero, 0 to zero the entire range respective to the
4264 * @front - zero up to the offset instead of from the offset on
4266 * This will find the page for the "from" offset and cow the page and zero the
4267 * part we want to zero. This is used with truncate and hole punching.
4269 int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4272 struct address_space *mapping = inode->i_mapping;
4273 struct btrfs_root *root = BTRFS_I(inode)->root;
4274 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4275 struct btrfs_ordered_extent *ordered;
4276 struct extent_state *cached_state = NULL;
4278 u32 blocksize = root->sectorsize;
4279 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4280 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4282 gfp_t mask = btrfs_alloc_write_mask(mapping);
4287 if ((offset & (blocksize - 1)) == 0 &&
4288 (!len || ((len & (blocksize - 1)) == 0)))
4290 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
4295 page = find_or_create_page(mapping, index, mask);
4297 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
4302 page_start = page_offset(page);
4303 page_end = page_start + PAGE_CACHE_SIZE - 1;
4305 if (!PageUptodate(page)) {
4306 ret = btrfs_readpage(NULL, page);
4308 if (page->mapping != mapping) {
4310 page_cache_release(page);
4313 if (!PageUptodate(page)) {
4318 wait_on_page_writeback(page);
4320 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
4321 set_page_extent_mapped(page);
4323 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4325 unlock_extent_cached(io_tree, page_start, page_end,
4326 &cached_state, GFP_NOFS);
4328 page_cache_release(page);
4329 btrfs_start_ordered_extent(inode, ordered, 1);
4330 btrfs_put_ordered_extent(ordered);
4334 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
4335 EXTENT_DIRTY | EXTENT_DELALLOC |
4336 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4337 0, 0, &cached_state, GFP_NOFS);
4339 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4342 unlock_extent_cached(io_tree, page_start, page_end,
4343 &cached_state, GFP_NOFS);
4347 if (offset != PAGE_CACHE_SIZE) {
4349 len = PAGE_CACHE_SIZE - offset;
4352 memset(kaddr, 0, offset);
4354 memset(kaddr + offset, 0, len);
4355 flush_dcache_page(page);
4358 ClearPageChecked(page);
4359 set_page_dirty(page);
4360 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4365 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
4367 page_cache_release(page);
4372 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4373 u64 offset, u64 len)
4375 struct btrfs_trans_handle *trans;
4379 * Still need to make sure the inode looks like it's been updated so
4380 * that any holes get logged if we fsync.
4382 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4383 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4384 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4385 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4390 * 1 - for the one we're dropping
4391 * 1 - for the one we're adding
4392 * 1 - for updating the inode.
4394 trans = btrfs_start_transaction(root, 3);
4396 return PTR_ERR(trans);
4398 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4400 btrfs_abort_transaction(trans, root, ret);
4401 btrfs_end_transaction(trans, root);
4405 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4406 0, 0, len, 0, len, 0, 0, 0);
4408 btrfs_abort_transaction(trans, root, ret);
4410 btrfs_update_inode(trans, root, inode);
4411 btrfs_end_transaction(trans, root);
4416 * This function puts in dummy file extents for the area we're creating a hole
4417 * for. So if we are truncating this file to a larger size we need to insert
4418 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4419 * the range between oldsize and size
4421 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4423 struct btrfs_root *root = BTRFS_I(inode)->root;
4424 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4425 struct extent_map *em = NULL;
4426 struct extent_state *cached_state = NULL;
4427 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4428 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4429 u64 block_end = ALIGN(size, root->sectorsize);
4436 * If our size started in the middle of a page we need to zero out the
4437 * rest of the page before we expand the i_size, otherwise we could
4438 * expose stale data.
4440 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4444 if (size <= hole_start)
4448 struct btrfs_ordered_extent *ordered;
4450 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
4452 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4453 block_end - hole_start);
4456 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4457 &cached_state, GFP_NOFS);
4458 btrfs_start_ordered_extent(inode, ordered, 1);
4459 btrfs_put_ordered_extent(ordered);
4462 cur_offset = hole_start;
4464 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4465 block_end - cur_offset, 0);
4471 last_byte = min(extent_map_end(em), block_end);
4472 last_byte = ALIGN(last_byte , root->sectorsize);
4473 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4474 struct extent_map *hole_em;
4475 hole_size = last_byte - cur_offset;
4477 err = maybe_insert_hole(root, inode, cur_offset,
4481 btrfs_drop_extent_cache(inode, cur_offset,
4482 cur_offset + hole_size - 1, 0);
4483 hole_em = alloc_extent_map();
4485 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4486 &BTRFS_I(inode)->runtime_flags);
4489 hole_em->start = cur_offset;
4490 hole_em->len = hole_size;
4491 hole_em->orig_start = cur_offset;
4493 hole_em->block_start = EXTENT_MAP_HOLE;
4494 hole_em->block_len = 0;
4495 hole_em->orig_block_len = 0;
4496 hole_em->ram_bytes = hole_size;
4497 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4498 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4499 hole_em->generation = root->fs_info->generation;
4502 write_lock(&em_tree->lock);
4503 err = add_extent_mapping(em_tree, hole_em, 1);
4504 write_unlock(&em_tree->lock);
4507 btrfs_drop_extent_cache(inode, cur_offset,
4511 free_extent_map(hole_em);
4514 free_extent_map(em);
4516 cur_offset = last_byte;
4517 if (cur_offset >= block_end)
4520 free_extent_map(em);
4521 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4526 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4528 struct btrfs_root *root = BTRFS_I(inode)->root;
4529 struct btrfs_trans_handle *trans;
4530 loff_t oldsize = i_size_read(inode);
4531 loff_t newsize = attr->ia_size;
4532 int mask = attr->ia_valid;
4536 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4537 * special case where we need to update the times despite not having
4538 * these flags set. For all other operations the VFS set these flags
4539 * explicitly if it wants a timestamp update.
4541 if (newsize != oldsize) {
4542 inode_inc_iversion(inode);
4543 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4544 inode->i_ctime = inode->i_mtime =
4545 current_fs_time(inode->i_sb);
4548 if (newsize > oldsize) {
4549 truncate_pagecache(inode, newsize);
4550 ret = btrfs_cont_expand(inode, oldsize, newsize);
4554 trans = btrfs_start_transaction(root, 1);
4556 return PTR_ERR(trans);
4558 i_size_write(inode, newsize);
4559 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4560 ret = btrfs_update_inode(trans, root, inode);
4561 btrfs_end_transaction(trans, root);
4565 * We're truncating a file that used to have good data down to
4566 * zero. Make sure it gets into the ordered flush list so that
4567 * any new writes get down to disk quickly.
4570 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4571 &BTRFS_I(inode)->runtime_flags);
4574 * 1 for the orphan item we're going to add
4575 * 1 for the orphan item deletion.
4577 trans = btrfs_start_transaction(root, 2);
4579 return PTR_ERR(trans);
4582 * We need to do this in case we fail at _any_ point during the
4583 * actual truncate. Once we do the truncate_setsize we could
4584 * invalidate pages which forces any outstanding ordered io to
4585 * be instantly completed which will give us extents that need
4586 * to be truncated. If we fail to get an orphan inode down we
4587 * could have left over extents that were never meant to live,
4588 * so we need to garuntee from this point on that everything
4589 * will be consistent.
4591 ret = btrfs_orphan_add(trans, inode);
4592 btrfs_end_transaction(trans, root);
4596 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4597 truncate_setsize(inode, newsize);
4599 /* Disable nonlocked read DIO to avoid the end less truncate */
4600 btrfs_inode_block_unlocked_dio(inode);
4601 inode_dio_wait(inode);
4602 btrfs_inode_resume_unlocked_dio(inode);
4604 ret = btrfs_truncate(inode);
4605 if (ret && inode->i_nlink) {
4609 * failed to truncate, disk_i_size is only adjusted down
4610 * as we remove extents, so it should represent the true
4611 * size of the inode, so reset the in memory size and
4612 * delete our orphan entry.
4614 trans = btrfs_join_transaction(root);
4615 if (IS_ERR(trans)) {
4616 btrfs_orphan_del(NULL, inode);
4619 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4620 err = btrfs_orphan_del(trans, inode);
4622 btrfs_abort_transaction(trans, root, err);
4623 btrfs_end_transaction(trans, root);
4630 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4632 struct inode *inode = dentry->d_inode;
4633 struct btrfs_root *root = BTRFS_I(inode)->root;
4636 if (btrfs_root_readonly(root))
4639 err = inode_change_ok(inode, attr);
4643 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
4644 err = btrfs_setsize(inode, attr);
4649 if (attr->ia_valid) {
4650 setattr_copy(inode, attr);
4651 inode_inc_iversion(inode);
4652 err = btrfs_dirty_inode(inode);
4654 if (!err && attr->ia_valid & ATTR_MODE)
4655 err = posix_acl_chmod(inode, inode->i_mode);
4662 * While truncating the inode pages during eviction, we get the VFS calling
4663 * btrfs_invalidatepage() against each page of the inode. This is slow because
4664 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4665 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4666 * extent_state structures over and over, wasting lots of time.
4668 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4669 * those expensive operations on a per page basis and do only the ordered io
4670 * finishing, while we release here the extent_map and extent_state structures,
4671 * without the excessive merging and splitting.
4673 static void evict_inode_truncate_pages(struct inode *inode)
4675 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4676 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4677 struct rb_node *node;
4679 ASSERT(inode->i_state & I_FREEING);
4680 truncate_inode_pages_final(&inode->i_data);
4682 write_lock(&map_tree->lock);
4683 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4684 struct extent_map *em;
4686 node = rb_first(&map_tree->map);
4687 em = rb_entry(node, struct extent_map, rb_node);
4688 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4689 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
4690 remove_extent_mapping(map_tree, em);
4691 free_extent_map(em);
4692 if (need_resched()) {
4693 write_unlock(&map_tree->lock);
4695 write_lock(&map_tree->lock);
4698 write_unlock(&map_tree->lock);
4700 spin_lock(&io_tree->lock);
4701 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4702 struct extent_state *state;
4703 struct extent_state *cached_state = NULL;
4705 node = rb_first(&io_tree->state);
4706 state = rb_entry(node, struct extent_state, rb_node);
4707 atomic_inc(&state->refs);
4708 spin_unlock(&io_tree->lock);
4710 lock_extent_bits(io_tree, state->start, state->end,
4712 clear_extent_bit(io_tree, state->start, state->end,
4713 EXTENT_LOCKED | EXTENT_DIRTY |
4714 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4715 EXTENT_DEFRAG, 1, 1,
4716 &cached_state, GFP_NOFS);
4717 free_extent_state(state);
4720 spin_lock(&io_tree->lock);
4722 spin_unlock(&io_tree->lock);
4725 void btrfs_evict_inode(struct inode *inode)
4727 struct btrfs_trans_handle *trans;
4728 struct btrfs_root *root = BTRFS_I(inode)->root;
4729 struct btrfs_block_rsv *rsv, *global_rsv;
4730 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
4733 trace_btrfs_inode_evict(inode);
4735 evict_inode_truncate_pages(inode);
4737 if (inode->i_nlink &&
4738 ((btrfs_root_refs(&root->root_item) != 0 &&
4739 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
4740 btrfs_is_free_space_inode(inode)))
4743 if (is_bad_inode(inode)) {
4744 btrfs_orphan_del(NULL, inode);
4747 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4748 btrfs_wait_ordered_range(inode, 0, (u64)-1);
4750 if (root->fs_info->log_root_recovering) {
4751 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
4752 &BTRFS_I(inode)->runtime_flags));
4756 if (inode->i_nlink > 0) {
4757 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4758 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
4762 ret = btrfs_commit_inode_delayed_inode(inode);
4764 btrfs_orphan_del(NULL, inode);
4768 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
4770 btrfs_orphan_del(NULL, inode);
4773 rsv->size = min_size;
4775 global_rsv = &root->fs_info->global_block_rsv;
4777 btrfs_i_size_write(inode, 0);
4780 * This is a bit simpler than btrfs_truncate since we've already
4781 * reserved our space for our orphan item in the unlink, so we just
4782 * need to reserve some slack space in case we add bytes and update
4783 * inode item when doing the truncate.
4786 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4787 BTRFS_RESERVE_FLUSH_LIMIT);
4790 * Try and steal from the global reserve since we will
4791 * likely not use this space anyway, we want to try as
4792 * hard as possible to get this to work.
4795 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4798 btrfs_warn(root->fs_info,
4799 "Could not get space for a delete, will truncate on mount %d",
4801 btrfs_orphan_del(NULL, inode);
4802 btrfs_free_block_rsv(root, rsv);
4806 trans = btrfs_join_transaction(root);
4807 if (IS_ERR(trans)) {
4808 btrfs_orphan_del(NULL, inode);
4809 btrfs_free_block_rsv(root, rsv);
4813 trans->block_rsv = rsv;
4815 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
4819 trans->block_rsv = &root->fs_info->trans_block_rsv;
4820 btrfs_end_transaction(trans, root);
4822 btrfs_btree_balance_dirty(root);
4825 btrfs_free_block_rsv(root, rsv);
4828 * Errors here aren't a big deal, it just means we leave orphan items
4829 * in the tree. They will be cleaned up on the next mount.
4832 trans->block_rsv = root->orphan_block_rsv;
4833 btrfs_orphan_del(trans, inode);
4835 btrfs_orphan_del(NULL, inode);
4838 trans->block_rsv = &root->fs_info->trans_block_rsv;
4839 if (!(root == root->fs_info->tree_root ||
4840 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
4841 btrfs_return_ino(root, btrfs_ino(inode));
4843 btrfs_end_transaction(trans, root);
4844 btrfs_btree_balance_dirty(root);
4846 btrfs_remove_delayed_node(inode);
4852 * this returns the key found in the dir entry in the location pointer.
4853 * If no dir entries were found, location->objectid is 0.
4855 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4856 struct btrfs_key *location)
4858 const char *name = dentry->d_name.name;
4859 int namelen = dentry->d_name.len;
4860 struct btrfs_dir_item *di;
4861 struct btrfs_path *path;
4862 struct btrfs_root *root = BTRFS_I(dir)->root;
4865 path = btrfs_alloc_path();
4869 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
4874 if (IS_ERR_OR_NULL(di))
4877 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
4879 btrfs_free_path(path);
4882 location->objectid = 0;
4887 * when we hit a tree root in a directory, the btrfs part of the inode
4888 * needs to be changed to reflect the root directory of the tree root. This
4889 * is kind of like crossing a mount point.
4891 static int fixup_tree_root_location(struct btrfs_root *root,
4893 struct dentry *dentry,
4894 struct btrfs_key *location,
4895 struct btrfs_root **sub_root)
4897 struct btrfs_path *path;
4898 struct btrfs_root *new_root;
4899 struct btrfs_root_ref *ref;
4900 struct extent_buffer *leaf;
4904 path = btrfs_alloc_path();
4911 ret = btrfs_find_item(root->fs_info->tree_root, path,
4912 BTRFS_I(dir)->root->root_key.objectid,
4913 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
4920 leaf = path->nodes[0];
4921 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4922 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
4923 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4926 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4927 (unsigned long)(ref + 1),
4928 dentry->d_name.len);
4932 btrfs_release_path(path);
4934 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4935 if (IS_ERR(new_root)) {
4936 err = PTR_ERR(new_root);
4940 *sub_root = new_root;
4941 location->objectid = btrfs_root_dirid(&new_root->root_item);
4942 location->type = BTRFS_INODE_ITEM_KEY;
4943 location->offset = 0;
4946 btrfs_free_path(path);
4950 static void inode_tree_add(struct inode *inode)
4952 struct btrfs_root *root = BTRFS_I(inode)->root;
4953 struct btrfs_inode *entry;
4955 struct rb_node *parent;
4956 struct rb_node *new = &BTRFS_I(inode)->rb_node;
4957 u64 ino = btrfs_ino(inode);
4959 if (inode_unhashed(inode))
4962 spin_lock(&root->inode_lock);
4963 p = &root->inode_tree.rb_node;
4966 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4968 if (ino < btrfs_ino(&entry->vfs_inode))
4969 p = &parent->rb_left;
4970 else if (ino > btrfs_ino(&entry->vfs_inode))
4971 p = &parent->rb_right;
4973 WARN_ON(!(entry->vfs_inode.i_state &
4974 (I_WILL_FREE | I_FREEING)));
4975 rb_replace_node(parent, new, &root->inode_tree);
4976 RB_CLEAR_NODE(parent);
4977 spin_unlock(&root->inode_lock);
4981 rb_link_node(new, parent, p);
4982 rb_insert_color(new, &root->inode_tree);
4983 spin_unlock(&root->inode_lock);
4986 static void inode_tree_del(struct inode *inode)
4988 struct btrfs_root *root = BTRFS_I(inode)->root;
4991 spin_lock(&root->inode_lock);
4992 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
4993 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4994 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
4995 empty = RB_EMPTY_ROOT(&root->inode_tree);
4997 spin_unlock(&root->inode_lock);
4999 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5000 synchronize_srcu(&root->fs_info->subvol_srcu);
5001 spin_lock(&root->inode_lock);
5002 empty = RB_EMPTY_ROOT(&root->inode_tree);
5003 spin_unlock(&root->inode_lock);
5005 btrfs_add_dead_root(root);
5009 void btrfs_invalidate_inodes(struct btrfs_root *root)
5011 struct rb_node *node;
5012 struct rb_node *prev;
5013 struct btrfs_inode *entry;
5014 struct inode *inode;
5017 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5018 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5020 spin_lock(&root->inode_lock);
5022 node = root->inode_tree.rb_node;
5026 entry = rb_entry(node, struct btrfs_inode, rb_node);
5028 if (objectid < btrfs_ino(&entry->vfs_inode))
5029 node = node->rb_left;
5030 else if (objectid > btrfs_ino(&entry->vfs_inode))
5031 node = node->rb_right;
5037 entry = rb_entry(prev, struct btrfs_inode, rb_node);
5038 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
5042 prev = rb_next(prev);
5046 entry = rb_entry(node, struct btrfs_inode, rb_node);
5047 objectid = btrfs_ino(&entry->vfs_inode) + 1;
5048 inode = igrab(&entry->vfs_inode);
5050 spin_unlock(&root->inode_lock);
5051 if (atomic_read(&inode->i_count) > 1)
5052 d_prune_aliases(inode);
5054 * btrfs_drop_inode will have it removed from
5055 * the inode cache when its usage count
5060 spin_lock(&root->inode_lock);
5064 if (cond_resched_lock(&root->inode_lock))
5067 node = rb_next(node);
5069 spin_unlock(&root->inode_lock);
5072 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5074 struct btrfs_iget_args *args = p;
5075 inode->i_ino = args->location->objectid;
5076 memcpy(&BTRFS_I(inode)->location, args->location,
5077 sizeof(*args->location));
5078 BTRFS_I(inode)->root = args->root;
5082 static int btrfs_find_actor(struct inode *inode, void *opaque)
5084 struct btrfs_iget_args *args = opaque;
5085 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5086 args->root == BTRFS_I(inode)->root;
5089 static struct inode *btrfs_iget_locked(struct super_block *s,
5090 struct btrfs_key *location,
5091 struct btrfs_root *root)
5093 struct inode *inode;
5094 struct btrfs_iget_args args;
5095 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5097 args.location = location;
5100 inode = iget5_locked(s, hashval, btrfs_find_actor,
5101 btrfs_init_locked_inode,
5106 /* Get an inode object given its location and corresponding root.
5107 * Returns in *is_new if the inode was read from disk
5109 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5110 struct btrfs_root *root, int *new)
5112 struct inode *inode;
5114 inode = btrfs_iget_locked(s, location, root);
5116 return ERR_PTR(-ENOMEM);
5118 if (inode->i_state & I_NEW) {
5119 btrfs_read_locked_inode(inode);
5120 if (!is_bad_inode(inode)) {
5121 inode_tree_add(inode);
5122 unlock_new_inode(inode);
5126 unlock_new_inode(inode);
5128 inode = ERR_PTR(-ESTALE);
5135 static struct inode *new_simple_dir(struct super_block *s,
5136 struct btrfs_key *key,
5137 struct btrfs_root *root)
5139 struct inode *inode = new_inode(s);
5142 return ERR_PTR(-ENOMEM);
5144 BTRFS_I(inode)->root = root;
5145 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5146 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5148 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5149 inode->i_op = &btrfs_dir_ro_inode_operations;
5150 inode->i_fop = &simple_dir_operations;
5151 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5152 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5157 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5159 struct inode *inode;
5160 struct btrfs_root *root = BTRFS_I(dir)->root;
5161 struct btrfs_root *sub_root = root;
5162 struct btrfs_key location;
5166 if (dentry->d_name.len > BTRFS_NAME_LEN)
5167 return ERR_PTR(-ENAMETOOLONG);
5169 ret = btrfs_inode_by_name(dir, dentry, &location);
5171 return ERR_PTR(ret);
5173 if (location.objectid == 0)
5174 return ERR_PTR(-ENOENT);
5176 if (location.type == BTRFS_INODE_ITEM_KEY) {
5177 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5181 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5183 index = srcu_read_lock(&root->fs_info->subvol_srcu);
5184 ret = fixup_tree_root_location(root, dir, dentry,
5185 &location, &sub_root);
5188 inode = ERR_PTR(ret);
5190 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5192 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5194 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5196 if (!IS_ERR(inode) && root != sub_root) {
5197 down_read(&root->fs_info->cleanup_work_sem);
5198 if (!(inode->i_sb->s_flags & MS_RDONLY))
5199 ret = btrfs_orphan_cleanup(sub_root);
5200 up_read(&root->fs_info->cleanup_work_sem);
5203 inode = ERR_PTR(ret);
5206 * If orphan cleanup did remove any orphans, it means the tree
5207 * was modified and therefore the commit root is not the same as
5208 * the current root anymore. This is a problem, because send
5209 * uses the commit root and therefore can see inode items that
5210 * don't exist in the current root anymore, and for example make
5211 * calls to btrfs_iget, which will do tree lookups based on the
5212 * current root and not on the commit root. Those lookups will
5213 * fail, returning a -ESTALE error, and making send fail with
5214 * that error. So make sure a send does not see any orphans we
5215 * have just removed, and that it will see the same inodes
5216 * regardless of whether a transaction commit happened before
5217 * it started (meaning that the commit root will be the same as
5218 * the current root) or not.
5220 if (sub_root->node != sub_root->commit_root) {
5221 u64 sub_flags = btrfs_root_flags(&sub_root->root_item);
5223 if (sub_flags & BTRFS_ROOT_SUBVOL_RDONLY) {
5224 struct extent_buffer *eb;
5227 * Assert we can't have races between dentry
5228 * lookup called through the snapshot creation
5229 * ioctl and the VFS.
5231 ASSERT(mutex_is_locked(&dir->i_mutex));
5233 down_write(&root->fs_info->commit_root_sem);
5234 eb = sub_root->commit_root;
5235 sub_root->commit_root =
5236 btrfs_root_node(sub_root);
5237 up_write(&root->fs_info->commit_root_sem);
5238 free_extent_buffer(eb);
5246 static int btrfs_dentry_delete(const struct dentry *dentry)
5248 struct btrfs_root *root;
5249 struct inode *inode = dentry->d_inode;
5251 if (!inode && !IS_ROOT(dentry))
5252 inode = dentry->d_parent->d_inode;
5255 root = BTRFS_I(inode)->root;
5256 if (btrfs_root_refs(&root->root_item) == 0)
5259 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5265 static void btrfs_dentry_release(struct dentry *dentry)
5267 kfree(dentry->d_fsdata);
5270 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5273 struct inode *inode;
5275 inode = btrfs_lookup_dentry(dir, dentry);
5276 if (IS_ERR(inode)) {
5277 if (PTR_ERR(inode) == -ENOENT)
5280 return ERR_CAST(inode);
5283 return d_materialise_unique(dentry, inode);
5286 unsigned char btrfs_filetype_table[] = {
5287 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5290 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5292 struct inode *inode = file_inode(file);
5293 struct btrfs_root *root = BTRFS_I(inode)->root;
5294 struct btrfs_item *item;
5295 struct btrfs_dir_item *di;
5296 struct btrfs_key key;
5297 struct btrfs_key found_key;
5298 struct btrfs_path *path;
5299 struct list_head ins_list;
5300 struct list_head del_list;
5302 struct extent_buffer *leaf;
5304 unsigned char d_type;
5309 int key_type = BTRFS_DIR_INDEX_KEY;
5313 int is_curr = 0; /* ctx->pos points to the current index? */
5315 /* FIXME, use a real flag for deciding about the key type */
5316 if (root->fs_info->tree_root == root)
5317 key_type = BTRFS_DIR_ITEM_KEY;
5319 if (!dir_emit_dots(file, ctx))
5322 path = btrfs_alloc_path();
5328 if (key_type == BTRFS_DIR_INDEX_KEY) {
5329 INIT_LIST_HEAD(&ins_list);
5330 INIT_LIST_HEAD(&del_list);
5331 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5334 btrfs_set_key_type(&key, key_type);
5335 key.offset = ctx->pos;
5336 key.objectid = btrfs_ino(inode);
5338 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5343 leaf = path->nodes[0];
5344 slot = path->slots[0];
5345 if (slot >= btrfs_header_nritems(leaf)) {
5346 ret = btrfs_next_leaf(root, path);
5354 item = btrfs_item_nr(slot);
5355 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5357 if (found_key.objectid != key.objectid)
5359 if (btrfs_key_type(&found_key) != key_type)
5361 if (found_key.offset < ctx->pos)
5363 if (key_type == BTRFS_DIR_INDEX_KEY &&
5364 btrfs_should_delete_dir_index(&del_list,
5368 ctx->pos = found_key.offset;
5371 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5373 di_total = btrfs_item_size(leaf, item);
5375 while (di_cur < di_total) {
5376 struct btrfs_key location;
5378 if (verify_dir_item(root, leaf, di))
5381 name_len = btrfs_dir_name_len(leaf, di);
5382 if (name_len <= sizeof(tmp_name)) {
5383 name_ptr = tmp_name;
5385 name_ptr = kmalloc(name_len, GFP_NOFS);
5391 read_extent_buffer(leaf, name_ptr,
5392 (unsigned long)(di + 1), name_len);
5394 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5395 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5398 /* is this a reference to our own snapshot? If so
5401 * In contrast to old kernels, we insert the snapshot's
5402 * dir item and dir index after it has been created, so
5403 * we won't find a reference to our own snapshot. We
5404 * still keep the following code for backward
5407 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5408 location.objectid == root->root_key.objectid) {
5412 over = !dir_emit(ctx, name_ptr, name_len,
5413 location.objectid, d_type);
5416 if (name_ptr != tmp_name)
5421 di_len = btrfs_dir_name_len(leaf, di) +
5422 btrfs_dir_data_len(leaf, di) + sizeof(*di);
5424 di = (struct btrfs_dir_item *)((char *)di + di_len);
5430 if (key_type == BTRFS_DIR_INDEX_KEY) {
5433 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5438 /* Reached end of directory/root. Bump pos past the last item. */
5442 * Stop new entries from being returned after we return the last
5445 * New directory entries are assigned a strictly increasing
5446 * offset. This means that new entries created during readdir
5447 * are *guaranteed* to be seen in the future by that readdir.
5448 * This has broken buggy programs which operate on names as
5449 * they're returned by readdir. Until we re-use freed offsets
5450 * we have this hack to stop new entries from being returned
5451 * under the assumption that they'll never reach this huge
5454 * This is being careful not to overflow 32bit loff_t unless the
5455 * last entry requires it because doing so has broken 32bit apps
5458 if (key_type == BTRFS_DIR_INDEX_KEY) {
5459 if (ctx->pos >= INT_MAX)
5460 ctx->pos = LLONG_MAX;
5467 if (key_type == BTRFS_DIR_INDEX_KEY)
5468 btrfs_put_delayed_items(&ins_list, &del_list);
5469 btrfs_free_path(path);
5473 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5475 struct btrfs_root *root = BTRFS_I(inode)->root;
5476 struct btrfs_trans_handle *trans;
5478 bool nolock = false;
5480 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5483 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5486 if (wbc->sync_mode == WB_SYNC_ALL) {
5488 trans = btrfs_join_transaction_nolock(root);
5490 trans = btrfs_join_transaction(root);
5492 return PTR_ERR(trans);
5493 ret = btrfs_commit_transaction(trans, root);
5499 * This is somewhat expensive, updating the tree every time the
5500 * inode changes. But, it is most likely to find the inode in cache.
5501 * FIXME, needs more benchmarking...there are no reasons other than performance
5502 * to keep or drop this code.
5504 static int btrfs_dirty_inode(struct inode *inode)
5506 struct btrfs_root *root = BTRFS_I(inode)->root;
5507 struct btrfs_trans_handle *trans;
5510 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5513 trans = btrfs_join_transaction(root);
5515 return PTR_ERR(trans);
5517 ret = btrfs_update_inode(trans, root, inode);
5518 if (ret && ret == -ENOSPC) {
5519 /* whoops, lets try again with the full transaction */
5520 btrfs_end_transaction(trans, root);
5521 trans = btrfs_start_transaction(root, 1);
5523 return PTR_ERR(trans);
5525 ret = btrfs_update_inode(trans, root, inode);
5527 btrfs_end_transaction(trans, root);
5528 if (BTRFS_I(inode)->delayed_node)
5529 btrfs_balance_delayed_items(root);
5535 * This is a copy of file_update_time. We need this so we can return error on
5536 * ENOSPC for updating the inode in the case of file write and mmap writes.
5538 static int btrfs_update_time(struct inode *inode, struct timespec *now,
5541 struct btrfs_root *root = BTRFS_I(inode)->root;
5543 if (btrfs_root_readonly(root))
5546 if (flags & S_VERSION)
5547 inode_inc_iversion(inode);
5548 if (flags & S_CTIME)
5549 inode->i_ctime = *now;
5550 if (flags & S_MTIME)
5551 inode->i_mtime = *now;
5552 if (flags & S_ATIME)
5553 inode->i_atime = *now;
5554 return btrfs_dirty_inode(inode);
5558 * find the highest existing sequence number in a directory
5559 * and then set the in-memory index_cnt variable to reflect
5560 * free sequence numbers
5562 static int btrfs_set_inode_index_count(struct inode *inode)
5564 struct btrfs_root *root = BTRFS_I(inode)->root;
5565 struct btrfs_key key, found_key;
5566 struct btrfs_path *path;
5567 struct extent_buffer *leaf;
5570 key.objectid = btrfs_ino(inode);
5571 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5572 key.offset = (u64)-1;
5574 path = btrfs_alloc_path();
5578 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5581 /* FIXME: we should be able to handle this */
5587 * MAGIC NUMBER EXPLANATION:
5588 * since we search a directory based on f_pos we have to start at 2
5589 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5590 * else has to start at 2
5592 if (path->slots[0] == 0) {
5593 BTRFS_I(inode)->index_cnt = 2;
5599 leaf = path->nodes[0];
5600 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5602 if (found_key.objectid != btrfs_ino(inode) ||
5603 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5604 BTRFS_I(inode)->index_cnt = 2;
5608 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5610 btrfs_free_path(path);
5615 * helper to find a free sequence number in a given directory. This current
5616 * code is very simple, later versions will do smarter things in the btree
5618 int btrfs_set_inode_index(struct inode *dir, u64 *index)
5622 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
5623 ret = btrfs_inode_delayed_dir_index_count(dir);
5625 ret = btrfs_set_inode_index_count(dir);
5631 *index = BTRFS_I(dir)->index_cnt;
5632 BTRFS_I(dir)->index_cnt++;
5637 static int btrfs_insert_inode_locked(struct inode *inode)
5639 struct btrfs_iget_args args;
5640 args.location = &BTRFS_I(inode)->location;
5641 args.root = BTRFS_I(inode)->root;
5643 return insert_inode_locked4(inode,
5644 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5645 btrfs_find_actor, &args);
5648 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5649 struct btrfs_root *root,
5651 const char *name, int name_len,
5652 u64 ref_objectid, u64 objectid,
5653 umode_t mode, u64 *index)
5655 struct inode *inode;
5656 struct btrfs_inode_item *inode_item;
5657 struct btrfs_key *location;
5658 struct btrfs_path *path;
5659 struct btrfs_inode_ref *ref;
5660 struct btrfs_key key[2];
5662 int nitems = name ? 2 : 1;
5666 path = btrfs_alloc_path();
5668 return ERR_PTR(-ENOMEM);
5670 inode = new_inode(root->fs_info->sb);
5672 btrfs_free_path(path);
5673 return ERR_PTR(-ENOMEM);
5677 * O_TMPFILE, set link count to 0, so that after this point,
5678 * we fill in an inode item with the correct link count.
5681 set_nlink(inode, 0);
5684 * we have to initialize this early, so we can reclaim the inode
5685 * number if we fail afterwards in this function.
5687 inode->i_ino = objectid;
5690 trace_btrfs_inode_request(dir);
5692 ret = btrfs_set_inode_index(dir, index);
5694 btrfs_free_path(path);
5696 return ERR_PTR(ret);
5702 * index_cnt is ignored for everything but a dir,
5703 * btrfs_get_inode_index_count has an explanation for the magic
5706 BTRFS_I(inode)->index_cnt = 2;
5707 BTRFS_I(inode)->dir_index = *index;
5708 BTRFS_I(inode)->root = root;
5709 BTRFS_I(inode)->generation = trans->transid;
5710 inode->i_generation = BTRFS_I(inode)->generation;
5713 * We could have gotten an inode number from somebody who was fsynced
5714 * and then removed in this same transaction, so let's just set full
5715 * sync since it will be a full sync anyway and this will blow away the
5716 * old info in the log.
5718 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5720 key[0].objectid = objectid;
5721 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5724 sizes[0] = sizeof(struct btrfs_inode_item);
5728 * Start new inodes with an inode_ref. This is slightly more
5729 * efficient for small numbers of hard links since they will
5730 * be packed into one item. Extended refs will kick in if we
5731 * add more hard links than can fit in the ref item.
5733 key[1].objectid = objectid;
5734 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5735 key[1].offset = ref_objectid;
5737 sizes[1] = name_len + sizeof(*ref);
5740 location = &BTRFS_I(inode)->location;
5741 location->objectid = objectid;
5742 location->offset = 0;
5743 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5745 ret = btrfs_insert_inode_locked(inode);
5749 path->leave_spinning = 1;
5750 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
5754 inode_init_owner(inode, dir, mode);
5755 inode_set_bytes(inode, 0);
5756 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5757 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5758 struct btrfs_inode_item);
5759 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5760 sizeof(*inode_item));
5761 fill_inode_item(trans, path->nodes[0], inode_item, inode);
5764 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5765 struct btrfs_inode_ref);
5766 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5767 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5768 ptr = (unsigned long)(ref + 1);
5769 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5772 btrfs_mark_buffer_dirty(path->nodes[0]);
5773 btrfs_free_path(path);
5775 btrfs_inherit_iflags(inode, dir);
5777 if (S_ISREG(mode)) {
5778 if (btrfs_test_opt(root, NODATASUM))
5779 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5780 if (btrfs_test_opt(root, NODATACOW))
5781 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5782 BTRFS_INODE_NODATASUM;
5785 inode_tree_add(inode);
5787 trace_btrfs_inode_new(inode);
5788 btrfs_set_inode_last_trans(trans, inode);
5790 btrfs_update_root_times(trans, root);
5792 ret = btrfs_inode_inherit_props(trans, inode, dir);
5794 btrfs_err(root->fs_info,
5795 "error inheriting props for ino %llu (root %llu): %d",
5796 btrfs_ino(inode), root->root_key.objectid, ret);
5801 unlock_new_inode(inode);
5804 BTRFS_I(dir)->index_cnt--;
5805 btrfs_free_path(path);
5807 return ERR_PTR(ret);
5810 static inline u8 btrfs_inode_type(struct inode *inode)
5812 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5816 * utility function to add 'inode' into 'parent_inode' with
5817 * a give name and a given sequence number.
5818 * if 'add_backref' is true, also insert a backref from the
5819 * inode to the parent directory.
5821 int btrfs_add_link(struct btrfs_trans_handle *trans,
5822 struct inode *parent_inode, struct inode *inode,
5823 const char *name, int name_len, int add_backref, u64 index)
5826 struct btrfs_key key;
5827 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5828 u64 ino = btrfs_ino(inode);
5829 u64 parent_ino = btrfs_ino(parent_inode);
5831 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5832 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5835 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5839 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5840 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5841 key.objectid, root->root_key.objectid,
5842 parent_ino, index, name, name_len);
5843 } else if (add_backref) {
5844 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5848 /* Nothing to clean up yet */
5852 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5854 btrfs_inode_type(inode), index);
5855 if (ret == -EEXIST || ret == -EOVERFLOW)
5858 btrfs_abort_transaction(trans, root, ret);
5862 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5864 inode_inc_iversion(parent_inode);
5865 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5866 ret = btrfs_update_inode(trans, root, parent_inode);
5868 btrfs_abort_transaction(trans, root, ret);
5872 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5875 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5876 key.objectid, root->root_key.objectid,
5877 parent_ino, &local_index, name, name_len);
5879 } else if (add_backref) {
5883 err = btrfs_del_inode_ref(trans, root, name, name_len,
5884 ino, parent_ino, &local_index);
5889 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
5890 struct inode *dir, struct dentry *dentry,
5891 struct inode *inode, int backref, u64 index)
5893 int err = btrfs_add_link(trans, dir, inode,
5894 dentry->d_name.name, dentry->d_name.len,
5901 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
5902 umode_t mode, dev_t rdev)
5904 struct btrfs_trans_handle *trans;
5905 struct btrfs_root *root = BTRFS_I(dir)->root;
5906 struct inode *inode = NULL;
5912 if (!new_valid_dev(rdev))
5916 * 2 for inode item and ref
5918 * 1 for xattr if selinux is on
5920 trans = btrfs_start_transaction(root, 5);
5922 return PTR_ERR(trans);
5924 err = btrfs_find_free_ino(root, &objectid);
5928 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5929 dentry->d_name.len, btrfs_ino(dir), objectid,
5931 if (IS_ERR(inode)) {
5932 err = PTR_ERR(inode);
5937 * If the active LSM wants to access the inode during
5938 * d_instantiate it needs these. Smack checks to see
5939 * if the filesystem supports xattrs by looking at the
5942 inode->i_op = &btrfs_special_inode_operations;
5943 init_special_inode(inode, inode->i_mode, rdev);
5945 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5947 goto out_unlock_inode;
5949 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5951 goto out_unlock_inode;
5953 btrfs_update_inode(trans, root, inode);
5954 unlock_new_inode(inode);
5955 d_instantiate(dentry, inode);
5959 btrfs_end_transaction(trans, root);
5960 btrfs_balance_delayed_items(root);
5961 btrfs_btree_balance_dirty(root);
5963 inode_dec_link_count(inode);
5970 unlock_new_inode(inode);
5975 static int btrfs_create(struct inode *dir, struct dentry *dentry,
5976 umode_t mode, bool excl)
5978 struct btrfs_trans_handle *trans;
5979 struct btrfs_root *root = BTRFS_I(dir)->root;
5980 struct inode *inode = NULL;
5981 int drop_inode_on_err = 0;
5987 * 2 for inode item and ref
5989 * 1 for xattr if selinux is on
5991 trans = btrfs_start_transaction(root, 5);
5993 return PTR_ERR(trans);
5995 err = btrfs_find_free_ino(root, &objectid);
5999 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6000 dentry->d_name.len, btrfs_ino(dir), objectid,
6002 if (IS_ERR(inode)) {
6003 err = PTR_ERR(inode);
6006 drop_inode_on_err = 1;
6008 * If the active LSM wants to access the inode during
6009 * d_instantiate it needs these. Smack checks to see
6010 * if the filesystem supports xattrs by looking at the
6013 inode->i_fop = &btrfs_file_operations;
6014 inode->i_op = &btrfs_file_inode_operations;
6015 inode->i_mapping->a_ops = &btrfs_aops;
6016 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
6018 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6020 goto out_unlock_inode;
6022 err = btrfs_update_inode(trans, root, inode);
6024 goto out_unlock_inode;
6026 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6028 goto out_unlock_inode;
6030 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6031 unlock_new_inode(inode);
6032 d_instantiate(dentry, inode);
6035 btrfs_end_transaction(trans, root);
6036 if (err && drop_inode_on_err) {
6037 inode_dec_link_count(inode);
6040 btrfs_balance_delayed_items(root);
6041 btrfs_btree_balance_dirty(root);
6045 unlock_new_inode(inode);
6050 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6051 struct dentry *dentry)
6053 struct btrfs_trans_handle *trans;
6054 struct btrfs_root *root = BTRFS_I(dir)->root;
6055 struct inode *inode = old_dentry->d_inode;
6060 /* do not allow sys_link's with other subvols of the same device */
6061 if (root->objectid != BTRFS_I(inode)->root->objectid)
6064 if (inode->i_nlink >= BTRFS_LINK_MAX)
6067 err = btrfs_set_inode_index(dir, &index);
6072 * 2 items for inode and inode ref
6073 * 2 items for dir items
6074 * 1 item for parent inode
6076 trans = btrfs_start_transaction(root, 5);
6077 if (IS_ERR(trans)) {
6078 err = PTR_ERR(trans);
6082 /* There are several dir indexes for this inode, clear the cache. */
6083 BTRFS_I(inode)->dir_index = 0ULL;
6085 inode_inc_iversion(inode);
6086 inode->i_ctime = CURRENT_TIME;
6088 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6090 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
6095 struct dentry *parent = dentry->d_parent;
6096 err = btrfs_update_inode(trans, root, inode);
6099 if (inode->i_nlink == 1) {
6101 * If new hard link count is 1, it's a file created
6102 * with open(2) O_TMPFILE flag.
6104 err = btrfs_orphan_del(trans, inode);
6108 d_instantiate(dentry, inode);
6109 btrfs_log_new_name(trans, inode, NULL, parent);
6112 btrfs_end_transaction(trans, root);
6113 btrfs_balance_delayed_items(root);
6116 inode_dec_link_count(inode);
6119 btrfs_btree_balance_dirty(root);
6123 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6125 struct inode *inode = NULL;
6126 struct btrfs_trans_handle *trans;
6127 struct btrfs_root *root = BTRFS_I(dir)->root;
6129 int drop_on_err = 0;
6134 * 2 items for inode and ref
6135 * 2 items for dir items
6136 * 1 for xattr if selinux is on
6138 trans = btrfs_start_transaction(root, 5);
6140 return PTR_ERR(trans);
6142 err = btrfs_find_free_ino(root, &objectid);
6146 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6147 dentry->d_name.len, btrfs_ino(dir), objectid,
6148 S_IFDIR | mode, &index);
6149 if (IS_ERR(inode)) {
6150 err = PTR_ERR(inode);
6155 /* these must be set before we unlock the inode */
6156 inode->i_op = &btrfs_dir_inode_operations;
6157 inode->i_fop = &btrfs_dir_file_operations;
6159 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6161 goto out_fail_inode;
6163 btrfs_i_size_write(inode, 0);
6164 err = btrfs_update_inode(trans, root, inode);
6166 goto out_fail_inode;
6168 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6169 dentry->d_name.len, 0, index);
6171 goto out_fail_inode;
6173 d_instantiate(dentry, inode);
6175 * mkdir is special. We're unlocking after we call d_instantiate
6176 * to avoid a race with nfsd calling d_instantiate.
6178 unlock_new_inode(inode);
6182 btrfs_end_transaction(trans, root);
6185 btrfs_balance_delayed_items(root);
6186 btrfs_btree_balance_dirty(root);
6190 unlock_new_inode(inode);
6194 /* helper for btfs_get_extent. Given an existing extent in the tree,
6195 * and an extent that you want to insert, deal with overlap and insert
6196 * the new extent into the tree.
6198 static int merge_extent_mapping(struct extent_map_tree *em_tree,
6199 struct extent_map *existing,
6200 struct extent_map *em,
6205 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6206 start_diff = map_start - em->start;
6207 em->start = map_start;
6208 em->len = existing->start - em->start;
6209 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6210 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6211 em->block_start += start_diff;
6212 em->block_len -= start_diff;
6214 return add_extent_mapping(em_tree, em, 0);
6217 static noinline int uncompress_inline(struct btrfs_path *path,
6218 struct inode *inode, struct page *page,
6219 size_t pg_offset, u64 extent_offset,
6220 struct btrfs_file_extent_item *item)
6223 struct extent_buffer *leaf = path->nodes[0];
6226 unsigned long inline_size;
6230 WARN_ON(pg_offset != 0);
6231 compress_type = btrfs_file_extent_compression(leaf, item);
6232 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6233 inline_size = btrfs_file_extent_inline_item_len(leaf,
6234 btrfs_item_nr(path->slots[0]));
6235 tmp = kmalloc(inline_size, GFP_NOFS);
6238 ptr = btrfs_file_extent_inline_start(item);
6240 read_extent_buffer(leaf, tmp, ptr, inline_size);
6242 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
6243 ret = btrfs_decompress(compress_type, tmp, page,
6244 extent_offset, inline_size, max_size);
6250 * a bit scary, this does extent mapping from logical file offset to the disk.
6251 * the ugly parts come from merging extents from the disk with the in-ram
6252 * representation. This gets more complex because of the data=ordered code,
6253 * where the in-ram extents might be locked pending data=ordered completion.
6255 * This also copies inline extents directly into the page.
6258 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6259 size_t pg_offset, u64 start, u64 len,
6264 u64 extent_start = 0;
6266 u64 objectid = btrfs_ino(inode);
6268 struct btrfs_path *path = NULL;
6269 struct btrfs_root *root = BTRFS_I(inode)->root;
6270 struct btrfs_file_extent_item *item;
6271 struct extent_buffer *leaf;
6272 struct btrfs_key found_key;
6273 struct extent_map *em = NULL;
6274 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6275 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6276 struct btrfs_trans_handle *trans = NULL;
6277 const bool new_inline = !page || create;
6280 read_lock(&em_tree->lock);
6281 em = lookup_extent_mapping(em_tree, start, len);
6283 em->bdev = root->fs_info->fs_devices->latest_bdev;
6284 read_unlock(&em_tree->lock);
6287 if (em->start > start || em->start + em->len <= start)
6288 free_extent_map(em);
6289 else if (em->block_start == EXTENT_MAP_INLINE && page)
6290 free_extent_map(em);
6294 em = alloc_extent_map();
6299 em->bdev = root->fs_info->fs_devices->latest_bdev;
6300 em->start = EXTENT_MAP_HOLE;
6301 em->orig_start = EXTENT_MAP_HOLE;
6303 em->block_len = (u64)-1;
6306 path = btrfs_alloc_path();
6312 * Chances are we'll be called again, so go ahead and do
6318 ret = btrfs_lookup_file_extent(trans, root, path,
6319 objectid, start, trans != NULL);
6326 if (path->slots[0] == 0)
6331 leaf = path->nodes[0];
6332 item = btrfs_item_ptr(leaf, path->slots[0],
6333 struct btrfs_file_extent_item);
6334 /* are we inside the extent that was found? */
6335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6336 found_type = btrfs_key_type(&found_key);
6337 if (found_key.objectid != objectid ||
6338 found_type != BTRFS_EXTENT_DATA_KEY) {
6340 * If we backup past the first extent we want to move forward
6341 * and see if there is an extent in front of us, otherwise we'll
6342 * say there is a hole for our whole search range which can
6349 found_type = btrfs_file_extent_type(leaf, item);
6350 extent_start = found_key.offset;
6351 if (found_type == BTRFS_FILE_EXTENT_REG ||
6352 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6353 extent_end = extent_start +
6354 btrfs_file_extent_num_bytes(leaf, item);
6355 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6357 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6358 extent_end = ALIGN(extent_start + size, root->sectorsize);
6361 if (start >= extent_end) {
6363 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6364 ret = btrfs_next_leaf(root, path);
6371 leaf = path->nodes[0];
6373 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6374 if (found_key.objectid != objectid ||
6375 found_key.type != BTRFS_EXTENT_DATA_KEY)
6377 if (start + len <= found_key.offset)
6379 if (start > found_key.offset)
6382 em->orig_start = start;
6383 em->len = found_key.offset - start;
6387 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6389 if (found_type == BTRFS_FILE_EXTENT_REG ||
6390 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6392 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6396 size_t extent_offset;
6402 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6403 extent_offset = page_offset(page) + pg_offset - extent_start;
6404 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
6405 size - extent_offset);
6406 em->start = extent_start + extent_offset;
6407 em->len = ALIGN(copy_size, root->sectorsize);
6408 em->orig_block_len = em->len;
6409 em->orig_start = em->start;
6410 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6411 if (create == 0 && !PageUptodate(page)) {
6412 if (btrfs_file_extent_compression(leaf, item) !=
6413 BTRFS_COMPRESS_NONE) {
6414 ret = uncompress_inline(path, inode, page,
6416 extent_offset, item);
6423 read_extent_buffer(leaf, map + pg_offset, ptr,
6425 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6426 memset(map + pg_offset + copy_size, 0,
6427 PAGE_CACHE_SIZE - pg_offset -
6432 flush_dcache_page(page);
6433 } else if (create && PageUptodate(page)) {
6437 free_extent_map(em);
6440 btrfs_release_path(path);
6441 trans = btrfs_join_transaction(root);
6444 return ERR_CAST(trans);
6448 write_extent_buffer(leaf, map + pg_offset, ptr,
6451 btrfs_mark_buffer_dirty(leaf);
6453 set_extent_uptodate(io_tree, em->start,
6454 extent_map_end(em) - 1, NULL, GFP_NOFS);
6459 em->orig_start = start;
6462 em->block_start = EXTENT_MAP_HOLE;
6463 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
6465 btrfs_release_path(path);
6466 if (em->start > start || extent_map_end(em) <= start) {
6467 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6468 em->start, em->len, start, len);
6474 write_lock(&em_tree->lock);
6475 ret = add_extent_mapping(em_tree, em, 0);
6476 /* it is possible that someone inserted the extent into the tree
6477 * while we had the lock dropped. It is also possible that
6478 * an overlapping map exists in the tree
6480 if (ret == -EEXIST) {
6481 struct extent_map *existing;
6485 existing = lookup_extent_mapping(em_tree, start, len);
6486 if (existing && (existing->start > start ||
6487 existing->start + existing->len <= start)) {
6488 free_extent_map(existing);
6492 existing = lookup_extent_mapping(em_tree, em->start,
6495 err = merge_extent_mapping(em_tree, existing,
6497 free_extent_map(existing);
6499 free_extent_map(em);
6504 free_extent_map(em);
6508 free_extent_map(em);
6513 write_unlock(&em_tree->lock);
6516 trace_btrfs_get_extent(root, em);
6519 btrfs_free_path(path);
6521 ret = btrfs_end_transaction(trans, root);
6526 free_extent_map(em);
6527 return ERR_PTR(err);
6529 BUG_ON(!em); /* Error is always set */
6533 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6534 size_t pg_offset, u64 start, u64 len,
6537 struct extent_map *em;
6538 struct extent_map *hole_em = NULL;
6539 u64 range_start = start;
6545 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6552 * - a pre-alloc extent,
6553 * there might actually be delalloc bytes behind it.
6555 if (em->block_start != EXTENT_MAP_HOLE &&
6556 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6562 /* check to see if we've wrapped (len == -1 or similar) */
6571 /* ok, we didn't find anything, lets look for delalloc */
6572 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6573 end, len, EXTENT_DELALLOC, 1);
6574 found_end = range_start + found;
6575 if (found_end < range_start)
6576 found_end = (u64)-1;
6579 * we didn't find anything useful, return
6580 * the original results from get_extent()
6582 if (range_start > end || found_end <= start) {
6588 /* adjust the range_start to make sure it doesn't
6589 * go backwards from the start they passed in
6591 range_start = max(start, range_start);
6592 found = found_end - range_start;
6595 u64 hole_start = start;
6598 em = alloc_extent_map();
6604 * when btrfs_get_extent can't find anything it
6605 * returns one huge hole
6607 * make sure what it found really fits our range, and
6608 * adjust to make sure it is based on the start from
6612 u64 calc_end = extent_map_end(hole_em);
6614 if (calc_end <= start || (hole_em->start > end)) {
6615 free_extent_map(hole_em);
6618 hole_start = max(hole_em->start, start);
6619 hole_len = calc_end - hole_start;
6623 if (hole_em && range_start > hole_start) {
6624 /* our hole starts before our delalloc, so we
6625 * have to return just the parts of the hole
6626 * that go until the delalloc starts
6628 em->len = min(hole_len,
6629 range_start - hole_start);
6630 em->start = hole_start;
6631 em->orig_start = hole_start;
6633 * don't adjust block start at all,
6634 * it is fixed at EXTENT_MAP_HOLE
6636 em->block_start = hole_em->block_start;
6637 em->block_len = hole_len;
6638 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6639 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
6641 em->start = range_start;
6643 em->orig_start = range_start;
6644 em->block_start = EXTENT_MAP_DELALLOC;
6645 em->block_len = found;
6647 } else if (hole_em) {
6652 free_extent_map(hole_em);
6654 free_extent_map(em);
6655 return ERR_PTR(err);
6660 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6663 struct btrfs_root *root = BTRFS_I(inode)->root;
6664 struct extent_map *em;
6665 struct btrfs_key ins;
6669 alloc_hint = get_extent_allocation_hint(inode, start, len);
6670 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
6671 alloc_hint, &ins, 1, 1);
6673 return ERR_PTR(ret);
6675 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6676 ins.offset, ins.offset, ins.offset, 0);
6678 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
6682 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6683 ins.offset, ins.offset, 0);
6685 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
6686 free_extent_map(em);
6687 return ERR_PTR(ret);
6694 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6695 * block must be cow'd
6697 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6698 u64 *orig_start, u64 *orig_block_len,
6701 struct btrfs_trans_handle *trans;
6702 struct btrfs_path *path;
6704 struct extent_buffer *leaf;
6705 struct btrfs_root *root = BTRFS_I(inode)->root;
6706 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6707 struct btrfs_file_extent_item *fi;
6708 struct btrfs_key key;
6715 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
6717 path = btrfs_alloc_path();
6721 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
6726 slot = path->slots[0];
6729 /* can't find the item, must cow */
6736 leaf = path->nodes[0];
6737 btrfs_item_key_to_cpu(leaf, &key, slot);
6738 if (key.objectid != btrfs_ino(inode) ||
6739 key.type != BTRFS_EXTENT_DATA_KEY) {
6740 /* not our file or wrong item type, must cow */
6744 if (key.offset > offset) {
6745 /* Wrong offset, must cow */
6749 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6750 found_type = btrfs_file_extent_type(leaf, fi);
6751 if (found_type != BTRFS_FILE_EXTENT_REG &&
6752 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6753 /* not a regular extent, must cow */
6757 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6760 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6761 if (extent_end <= offset)
6764 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6765 if (disk_bytenr == 0)
6768 if (btrfs_file_extent_compression(leaf, fi) ||
6769 btrfs_file_extent_encryption(leaf, fi) ||
6770 btrfs_file_extent_other_encoding(leaf, fi))
6773 backref_offset = btrfs_file_extent_offset(leaf, fi);
6776 *orig_start = key.offset - backref_offset;
6777 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6778 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6781 if (btrfs_extent_readonly(root, disk_bytenr))
6784 num_bytes = min(offset + *len, extent_end) - offset;
6785 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6788 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
6789 ret = test_range_bit(io_tree, offset, range_end,
6790 EXTENT_DELALLOC, 0, NULL);
6797 btrfs_release_path(path);
6800 * look for other files referencing this extent, if we
6801 * find any we must cow
6803 trans = btrfs_join_transaction(root);
6804 if (IS_ERR(trans)) {
6809 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6810 key.offset - backref_offset, disk_bytenr);
6811 btrfs_end_transaction(trans, root);
6818 * adjust disk_bytenr and num_bytes to cover just the bytes
6819 * in this extent we are about to write. If there
6820 * are any csums in that range we have to cow in order
6821 * to keep the csums correct
6823 disk_bytenr += backref_offset;
6824 disk_bytenr += offset - key.offset;
6825 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6828 * all of the above have passed, it is safe to overwrite this extent
6834 btrfs_free_path(path);
6838 bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
6840 struct radix_tree_root *root = &inode->i_mapping->page_tree;
6842 void **pagep = NULL;
6843 struct page *page = NULL;
6847 start_idx = start >> PAGE_CACHE_SHIFT;
6850 * end is the last byte in the last page. end == start is legal
6852 end_idx = end >> PAGE_CACHE_SHIFT;
6856 /* Most of the code in this while loop is lifted from
6857 * find_get_page. It's been modified to begin searching from a
6858 * page and return just the first page found in that range. If the
6859 * found idx is less than or equal to the end idx then we know that
6860 * a page exists. If no pages are found or if those pages are
6861 * outside of the range then we're fine (yay!) */
6862 while (page == NULL &&
6863 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
6864 page = radix_tree_deref_slot(pagep);
6865 if (unlikely(!page))
6868 if (radix_tree_exception(page)) {
6869 if (radix_tree_deref_retry(page)) {
6874 * Otherwise, shmem/tmpfs must be storing a swap entry
6875 * here as an exceptional entry: so return it without
6876 * attempting to raise page count.
6879 break; /* TODO: Is this relevant for this use case? */
6882 if (!page_cache_get_speculative(page)) {
6888 * Has the page moved?
6889 * This is part of the lockless pagecache protocol. See
6890 * include/linux/pagemap.h for details.
6892 if (unlikely(page != *pagep)) {
6893 page_cache_release(page);
6899 if (page->index <= end_idx)
6901 page_cache_release(page);
6908 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6909 struct extent_state **cached_state, int writing)
6911 struct btrfs_ordered_extent *ordered;
6915 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6918 * We're concerned with the entire range that we're going to be
6919 * doing DIO to, so we need to make sure theres no ordered
6920 * extents in this range.
6922 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6923 lockend - lockstart + 1);
6926 * We need to make sure there are no buffered pages in this
6927 * range either, we could have raced between the invalidate in
6928 * generic_file_direct_write and locking the extent. The
6929 * invalidate needs to happen so that reads after a write do not
6934 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
6937 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6938 cached_state, GFP_NOFS);
6941 btrfs_start_ordered_extent(inode, ordered, 1);
6942 btrfs_put_ordered_extent(ordered);
6944 /* Screw you mmap */
6945 ret = filemap_write_and_wait_range(inode->i_mapping,
6952 * If we found a page that couldn't be invalidated just
6953 * fall back to buffered.
6955 ret = invalidate_inode_pages2_range(inode->i_mapping,
6956 lockstart >> PAGE_CACHE_SHIFT,
6957 lockend >> PAGE_CACHE_SHIFT);
6968 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6969 u64 len, u64 orig_start,
6970 u64 block_start, u64 block_len,
6971 u64 orig_block_len, u64 ram_bytes,
6974 struct extent_map_tree *em_tree;
6975 struct extent_map *em;
6976 struct btrfs_root *root = BTRFS_I(inode)->root;
6979 em_tree = &BTRFS_I(inode)->extent_tree;
6980 em = alloc_extent_map();
6982 return ERR_PTR(-ENOMEM);
6985 em->orig_start = orig_start;
6986 em->mod_start = start;
6989 em->block_len = block_len;
6990 em->block_start = block_start;
6991 em->bdev = root->fs_info->fs_devices->latest_bdev;
6992 em->orig_block_len = orig_block_len;
6993 em->ram_bytes = ram_bytes;
6994 em->generation = -1;
6995 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6996 if (type == BTRFS_ORDERED_PREALLOC)
6997 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7000 btrfs_drop_extent_cache(inode, em->start,
7001 em->start + em->len - 1, 0);
7002 write_lock(&em_tree->lock);
7003 ret = add_extent_mapping(em_tree, em, 1);
7004 write_unlock(&em_tree->lock);
7005 } while (ret == -EEXIST);
7008 free_extent_map(em);
7009 return ERR_PTR(ret);
7016 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7017 struct buffer_head *bh_result, int create)
7019 struct extent_map *em;
7020 struct btrfs_root *root = BTRFS_I(inode)->root;
7021 struct extent_state *cached_state = NULL;
7022 u64 start = iblock << inode->i_blkbits;
7023 u64 lockstart, lockend;
7024 u64 len = bh_result->b_size;
7025 int unlock_bits = EXTENT_LOCKED;
7029 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
7031 len = min_t(u64, len, root->sectorsize);
7034 lockend = start + len - 1;
7037 * If this errors out it's because we couldn't invalidate pagecache for
7038 * this range and we need to fallback to buffered.
7040 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
7043 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7050 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7051 * io. INLINE is special, and we could probably kludge it in here, but
7052 * it's still buffered so for safety lets just fall back to the generic
7055 * For COMPRESSED we _have_ to read the entire extent in so we can
7056 * decompress it, so there will be buffering required no matter what we
7057 * do, so go ahead and fallback to buffered.
7059 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7060 * to buffered IO. Don't blame me, this is the price we pay for using
7063 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7064 em->block_start == EXTENT_MAP_INLINE) {
7065 free_extent_map(em);
7070 /* Just a good old fashioned hole, return */
7071 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7072 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7073 free_extent_map(em);
7078 * We don't allocate a new extent in the following cases
7080 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7082 * 2) The extent is marked as PREALLOC. We're good to go here and can
7083 * just use the extent.
7087 len = min(len, em->len - (start - em->start));
7088 lockstart = start + len;
7092 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7093 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7094 em->block_start != EXTENT_MAP_HOLE)) {
7097 u64 block_start, orig_start, orig_block_len, ram_bytes;
7099 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7100 type = BTRFS_ORDERED_PREALLOC;
7102 type = BTRFS_ORDERED_NOCOW;
7103 len = min(len, em->len - (start - em->start));
7104 block_start = em->block_start + (start - em->start);
7106 if (can_nocow_extent(inode, start, &len, &orig_start,
7107 &orig_block_len, &ram_bytes) == 1) {
7108 if (type == BTRFS_ORDERED_PREALLOC) {
7109 free_extent_map(em);
7110 em = create_pinned_em(inode, start, len,
7119 ret = btrfs_add_ordered_extent_dio(inode, start,
7120 block_start, len, len, type);
7122 free_extent_map(em);
7130 * this will cow the extent, reset the len in case we changed
7133 len = bh_result->b_size;
7134 free_extent_map(em);
7135 em = btrfs_new_extent_direct(inode, start, len);
7140 len = min(len, em->len - (start - em->start));
7142 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7144 bh_result->b_size = len;
7145 bh_result->b_bdev = em->bdev;
7146 set_buffer_mapped(bh_result);
7148 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7149 set_buffer_new(bh_result);
7152 * Need to update the i_size under the extent lock so buffered
7153 * readers will get the updated i_size when we unlock.
7155 if (start + len > i_size_read(inode))
7156 i_size_write(inode, start + len);
7158 spin_lock(&BTRFS_I(inode)->lock);
7159 BTRFS_I(inode)->outstanding_extents++;
7160 spin_unlock(&BTRFS_I(inode)->lock);
7162 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7163 lockstart + len - 1, EXTENT_DELALLOC, NULL,
7164 &cached_state, GFP_NOFS);
7169 * In the case of write we need to clear and unlock the entire range,
7170 * in the case of read we need to unlock only the end area that we
7171 * aren't using if there is any left over space.
7173 if (lockstart < lockend) {
7174 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7175 lockend, unlock_bits, 1, 0,
7176 &cached_state, GFP_NOFS);
7178 free_extent_state(cached_state);
7181 free_extent_map(em);
7186 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7187 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7191 static void btrfs_endio_direct_read(struct bio *bio, int err)
7193 struct btrfs_dio_private *dip = bio->bi_private;
7194 struct bio_vec *bvec;
7195 struct inode *inode = dip->inode;
7196 struct btrfs_root *root = BTRFS_I(inode)->root;
7197 struct bio *dio_bio;
7198 u32 *csums = (u32 *)dip->csum;
7202 start = dip->logical_offset;
7203 bio_for_each_segment_all(bvec, bio, i) {
7204 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
7205 struct page *page = bvec->bv_page;
7208 unsigned long flags;
7210 local_irq_save(flags);
7211 kaddr = kmap_atomic(page);
7212 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
7213 csum, bvec->bv_len);
7214 btrfs_csum_final(csum, (char *)&csum);
7215 kunmap_atomic(kaddr);
7216 local_irq_restore(flags);
7218 flush_dcache_page(bvec->bv_page);
7219 if (csum != csums[i]) {
7220 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
7221 btrfs_ino(inode), start, csum,
7227 start += bvec->bv_len;
7230 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
7231 dip->logical_offset + dip->bytes - 1);
7232 dio_bio = dip->dio_bio;
7236 /* If we had a csum failure make sure to clear the uptodate flag */
7238 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7239 dio_end_io(dio_bio, err);
7243 static void btrfs_endio_direct_write(struct bio *bio, int err)
7245 struct btrfs_dio_private *dip = bio->bi_private;
7246 struct inode *inode = dip->inode;
7247 struct btrfs_root *root = BTRFS_I(inode)->root;
7248 struct btrfs_ordered_extent *ordered = NULL;
7249 u64 ordered_offset = dip->logical_offset;
7250 u64 ordered_bytes = dip->bytes;
7251 struct bio *dio_bio;
7257 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
7259 ordered_bytes, !err);
7263 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
7264 finish_ordered_fn, NULL, NULL);
7265 btrfs_queue_work(root->fs_info->endio_write_workers,
7269 * our bio might span multiple ordered extents. If we haven't
7270 * completed the accounting for the whole dio, go back and try again
7272 if (ordered_offset < dip->logical_offset + dip->bytes) {
7273 ordered_bytes = dip->logical_offset + dip->bytes -
7279 dio_bio = dip->dio_bio;
7283 /* If we had an error make sure to clear the uptodate flag */
7285 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7286 dio_end_io(dio_bio, err);
7290 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
7291 struct bio *bio, int mirror_num,
7292 unsigned long bio_flags, u64 offset)
7295 struct btrfs_root *root = BTRFS_I(inode)->root;
7296 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
7297 BUG_ON(ret); /* -ENOMEM */
7301 static void btrfs_end_dio_bio(struct bio *bio, int err)
7303 struct btrfs_dio_private *dip = bio->bi_private;
7306 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
7307 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7308 btrfs_ino(dip->inode), bio->bi_rw,
7309 (unsigned long long)bio->bi_iter.bi_sector,
7310 bio->bi_iter.bi_size, err);
7314 * before atomic variable goto zero, we must make sure
7315 * dip->errors is perceived to be set.
7317 smp_mb__before_atomic();
7320 /* if there are more bios still pending for this dio, just exit */
7321 if (!atomic_dec_and_test(&dip->pending_bios))
7325 bio_io_error(dip->orig_bio);
7327 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
7328 bio_endio(dip->orig_bio, 0);
7334 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
7335 u64 first_sector, gfp_t gfp_flags)
7337 int nr_vecs = bio_get_nr_vecs(bdev);
7338 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
7341 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
7342 int rw, u64 file_offset, int skip_sum,
7345 struct btrfs_dio_private *dip = bio->bi_private;
7346 int write = rw & REQ_WRITE;
7347 struct btrfs_root *root = BTRFS_I(inode)->root;
7351 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7356 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
7364 if (write && async_submit) {
7365 ret = btrfs_wq_submit_bio(root->fs_info,
7366 inode, rw, bio, 0, 0,
7368 __btrfs_submit_bio_start_direct_io,
7369 __btrfs_submit_bio_done);
7373 * If we aren't doing async submit, calculate the csum of the
7376 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
7379 } else if (!skip_sum) {
7380 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
7387 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
7393 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7396 struct inode *inode = dip->inode;
7397 struct btrfs_root *root = BTRFS_I(inode)->root;
7399 struct bio *orig_bio = dip->orig_bio;
7400 struct bio_vec *bvec = orig_bio->bi_io_vec;
7401 u64 start_sector = orig_bio->bi_iter.bi_sector;
7402 u64 file_offset = dip->logical_offset;
7407 int async_submit = 0;
7409 map_length = orig_bio->bi_iter.bi_size;
7410 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
7411 &map_length, NULL, 0);
7415 if (map_length >= orig_bio->bi_iter.bi_size) {
7420 /* async crcs make it difficult to collect full stripe writes. */
7421 if (btrfs_get_alloc_profile(root, 1) &
7422 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7427 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7431 bio->bi_private = dip;
7432 bio->bi_end_io = btrfs_end_dio_bio;
7433 atomic_inc(&dip->pending_bios);
7435 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7436 if (unlikely(map_length < submit_len + bvec->bv_len ||
7437 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7438 bvec->bv_offset) < bvec->bv_len)) {
7440 * inc the count before we submit the bio so
7441 * we know the end IO handler won't happen before
7442 * we inc the count. Otherwise, the dip might get freed
7443 * before we're done setting it up
7445 atomic_inc(&dip->pending_bios);
7446 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7447 file_offset, skip_sum,
7451 atomic_dec(&dip->pending_bios);
7455 start_sector += submit_len >> 9;
7456 file_offset += submit_len;
7461 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7462 start_sector, GFP_NOFS);
7465 bio->bi_private = dip;
7466 bio->bi_end_io = btrfs_end_dio_bio;
7468 map_length = orig_bio->bi_iter.bi_size;
7469 ret = btrfs_map_block(root->fs_info, rw,
7471 &map_length, NULL, 0);
7477 submit_len += bvec->bv_len;
7484 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
7493 * before atomic variable goto zero, we must
7494 * make sure dip->errors is perceived to be set.
7496 smp_mb__before_atomic();
7497 if (atomic_dec_and_test(&dip->pending_bios))
7498 bio_io_error(dip->orig_bio);
7500 /* bio_end_io() will handle error, so we needn't return it */
7504 static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7505 struct inode *inode, loff_t file_offset)
7507 struct btrfs_root *root = BTRFS_I(inode)->root;
7508 struct btrfs_dio_private *dip;
7512 int write = rw & REQ_WRITE;
7516 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7518 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7524 if (!skip_sum && !write) {
7525 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7526 sum_len = dio_bio->bi_iter.bi_size >>
7527 inode->i_sb->s_blocksize_bits;
7528 sum_len *= csum_size;
7533 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
7539 dip->private = dio_bio->bi_private;
7541 dip->logical_offset = file_offset;
7542 dip->bytes = dio_bio->bi_iter.bi_size;
7543 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
7544 io_bio->bi_private = dip;
7546 dip->orig_bio = io_bio;
7547 dip->dio_bio = dio_bio;
7548 atomic_set(&dip->pending_bios, 0);
7551 io_bio->bi_end_io = btrfs_endio_direct_write;
7553 io_bio->bi_end_io = btrfs_endio_direct_read;
7555 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7564 * If this is a write, we need to clean up the reserved space and kill
7565 * the ordered extent.
7568 struct btrfs_ordered_extent *ordered;
7569 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
7570 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7571 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7572 btrfs_free_reserved_extent(root, ordered->start,
7573 ordered->disk_len, 1);
7574 btrfs_put_ordered_extent(ordered);
7575 btrfs_put_ordered_extent(ordered);
7577 bio_endio(dio_bio, ret);
7580 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7581 const struct iov_iter *iter, loff_t offset)
7585 unsigned blocksize_mask = root->sectorsize - 1;
7586 ssize_t retval = -EINVAL;
7588 if (offset & blocksize_mask)
7591 if (iov_iter_alignment(iter) & blocksize_mask)
7594 /* If this is a write we don't need to check anymore */
7598 * Check to make sure we don't have duplicate iov_base's in this
7599 * iovec, if so return EINVAL, otherwise we'll get csum errors
7600 * when reading back.
7602 for (seg = 0; seg < iter->nr_segs; seg++) {
7603 for (i = seg + 1; i < iter->nr_segs; i++) {
7604 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7613 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7614 struct iov_iter *iter, loff_t offset)
7616 struct file *file = iocb->ki_filp;
7617 struct inode *inode = file->f_mapping->host;
7621 bool relock = false;
7624 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iter, offset))
7627 atomic_inc(&inode->i_dio_count);
7628 smp_mb__after_atomic();
7631 * The generic stuff only does filemap_write_and_wait_range, which
7632 * isn't enough if we've written compressed pages to this area, so
7633 * we need to flush the dirty pages again to make absolutely sure
7634 * that any outstanding dirty pages are on disk.
7636 count = iov_iter_count(iter);
7637 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7638 &BTRFS_I(inode)->runtime_flags))
7639 filemap_fdatawrite_range(inode->i_mapping, offset,
7640 offset + count - 1);
7644 * If the write DIO is beyond the EOF, we need update
7645 * the isize, but it is protected by i_mutex. So we can
7646 * not unlock the i_mutex at this case.
7648 if (offset + count <= inode->i_size) {
7649 mutex_unlock(&inode->i_mutex);
7652 ret = btrfs_delalloc_reserve_space(inode, count);
7655 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7656 &BTRFS_I(inode)->runtime_flags))) {
7657 inode_dio_done(inode);
7658 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7662 ret = __blockdev_direct_IO(rw, iocb, inode,
7663 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7664 iter, offset, btrfs_get_blocks_direct, NULL,
7665 btrfs_submit_direct, flags);
7667 if (ret < 0 && ret != -EIOCBQUEUED)
7668 btrfs_delalloc_release_space(inode, count);
7669 else if (ret >= 0 && (size_t)ret < count)
7670 btrfs_delalloc_release_space(inode,
7671 count - (size_t)ret);
7673 btrfs_delalloc_release_metadata(inode, 0);
7677 inode_dio_done(inode);
7679 mutex_lock(&inode->i_mutex);
7684 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7686 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7687 __u64 start, __u64 len)
7691 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7695 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
7698 int btrfs_readpage(struct file *file, struct page *page)
7700 struct extent_io_tree *tree;
7701 tree = &BTRFS_I(page->mapping->host)->io_tree;
7702 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
7705 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7707 struct extent_io_tree *tree;
7710 if (current->flags & PF_MEMALLOC) {
7711 redirty_page_for_writepage(wbc, page);
7715 tree = &BTRFS_I(page->mapping->host)->io_tree;
7716 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7719 static int btrfs_writepages(struct address_space *mapping,
7720 struct writeback_control *wbc)
7722 struct extent_io_tree *tree;
7724 tree = &BTRFS_I(mapping->host)->io_tree;
7725 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7729 btrfs_readpages(struct file *file, struct address_space *mapping,
7730 struct list_head *pages, unsigned nr_pages)
7732 struct extent_io_tree *tree;
7733 tree = &BTRFS_I(mapping->host)->io_tree;
7734 return extent_readpages(tree, mapping, pages, nr_pages,
7737 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7739 struct extent_io_tree *tree;
7740 struct extent_map_tree *map;
7743 tree = &BTRFS_I(page->mapping->host)->io_tree;
7744 map = &BTRFS_I(page->mapping->host)->extent_tree;
7745 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
7747 ClearPagePrivate(page);
7748 set_page_private(page, 0);
7749 page_cache_release(page);
7754 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7756 if (PageWriteback(page) || PageDirty(page))
7758 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
7761 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7762 unsigned int length)
7764 struct inode *inode = page->mapping->host;
7765 struct extent_io_tree *tree;
7766 struct btrfs_ordered_extent *ordered;
7767 struct extent_state *cached_state = NULL;
7768 u64 page_start = page_offset(page);
7769 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
7770 int inode_evicting = inode->i_state & I_FREEING;
7773 * we have the page locked, so new writeback can't start,
7774 * and the dirty bit won't be cleared while we are here.
7776 * Wait for IO on this page so that we can safely clear
7777 * the PagePrivate2 bit and do ordered accounting
7779 wait_on_page_writeback(page);
7781 tree = &BTRFS_I(inode)->io_tree;
7783 btrfs_releasepage(page, GFP_NOFS);
7787 if (!inode_evicting)
7788 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7789 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7792 * IO on this page will never be started, so we need
7793 * to account for any ordered extents now
7795 if (!inode_evicting)
7796 clear_extent_bit(tree, page_start, page_end,
7797 EXTENT_DIRTY | EXTENT_DELALLOC |
7798 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7799 EXTENT_DEFRAG, 1, 0, &cached_state,
7802 * whoever cleared the private bit is responsible
7803 * for the finish_ordered_io
7805 if (TestClearPagePrivate2(page)) {
7806 struct btrfs_ordered_inode_tree *tree;
7809 tree = &BTRFS_I(inode)->ordered_tree;
7811 spin_lock_irq(&tree->lock);
7812 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
7813 new_len = page_start - ordered->file_offset;
7814 if (new_len < ordered->truncated_len)
7815 ordered->truncated_len = new_len;
7816 spin_unlock_irq(&tree->lock);
7818 if (btrfs_dec_test_ordered_pending(inode, &ordered,
7820 PAGE_CACHE_SIZE, 1))
7821 btrfs_finish_ordered_io(ordered);
7823 btrfs_put_ordered_extent(ordered);
7824 if (!inode_evicting) {
7825 cached_state = NULL;
7826 lock_extent_bits(tree, page_start, page_end, 0,
7831 if (!inode_evicting) {
7832 clear_extent_bit(tree, page_start, page_end,
7833 EXTENT_LOCKED | EXTENT_DIRTY |
7834 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7835 EXTENT_DEFRAG, 1, 1,
7836 &cached_state, GFP_NOFS);
7838 __btrfs_releasepage(page, GFP_NOFS);
7841 ClearPageChecked(page);
7842 if (PagePrivate(page)) {
7843 ClearPagePrivate(page);
7844 set_page_private(page, 0);
7845 page_cache_release(page);
7850 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7851 * called from a page fault handler when a page is first dirtied. Hence we must
7852 * be careful to check for EOF conditions here. We set the page up correctly
7853 * for a written page which means we get ENOSPC checking when writing into
7854 * holes and correct delalloc and unwritten extent mapping on filesystems that
7855 * support these features.
7857 * We are not allowed to take the i_mutex here so we have to play games to
7858 * protect against truncate races as the page could now be beyond EOF. Because
7859 * vmtruncate() writes the inode size before removing pages, once we have the
7860 * page lock we can determine safely if the page is beyond EOF. If it is not
7861 * beyond EOF, then the page is guaranteed safe against truncation until we
7864 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
7866 struct page *page = vmf->page;
7867 struct inode *inode = file_inode(vma->vm_file);
7868 struct btrfs_root *root = BTRFS_I(inode)->root;
7869 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7870 struct btrfs_ordered_extent *ordered;
7871 struct extent_state *cached_state = NULL;
7873 unsigned long zero_start;
7880 sb_start_pagefault(inode->i_sb);
7881 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
7883 ret = file_update_time(vma->vm_file);
7889 else /* -ENOSPC, -EIO, etc */
7890 ret = VM_FAULT_SIGBUS;
7896 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
7899 size = i_size_read(inode);
7900 page_start = page_offset(page);
7901 page_end = page_start + PAGE_CACHE_SIZE - 1;
7903 if ((page->mapping != inode->i_mapping) ||
7904 (page_start >= size)) {
7905 /* page got truncated out from underneath us */
7908 wait_on_page_writeback(page);
7910 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
7911 set_page_extent_mapped(page);
7914 * we can't set the delalloc bits if there are pending ordered
7915 * extents. Drop our locks and wait for them to finish
7917 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7919 unlock_extent_cached(io_tree, page_start, page_end,
7920 &cached_state, GFP_NOFS);
7922 btrfs_start_ordered_extent(inode, ordered, 1);
7923 btrfs_put_ordered_extent(ordered);
7928 * XXX - page_mkwrite gets called every time the page is dirtied, even
7929 * if it was already dirty, so for space accounting reasons we need to
7930 * clear any delalloc bits for the range we are fixing to save. There
7931 * is probably a better way to do this, but for now keep consistent with
7932 * prepare_pages in the normal write path.
7934 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
7935 EXTENT_DIRTY | EXTENT_DELALLOC |
7936 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
7937 0, 0, &cached_state, GFP_NOFS);
7939 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7942 unlock_extent_cached(io_tree, page_start, page_end,
7943 &cached_state, GFP_NOFS);
7944 ret = VM_FAULT_SIGBUS;
7949 /* page is wholly or partially inside EOF */
7950 if (page_start + PAGE_CACHE_SIZE > size)
7951 zero_start = size & ~PAGE_CACHE_MASK;
7953 zero_start = PAGE_CACHE_SIZE;
7955 if (zero_start != PAGE_CACHE_SIZE) {
7957 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7958 flush_dcache_page(page);
7961 ClearPageChecked(page);
7962 set_page_dirty(page);
7963 SetPageUptodate(page);
7965 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7966 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
7967 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
7969 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
7973 sb_end_pagefault(inode->i_sb);
7974 return VM_FAULT_LOCKED;
7978 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
7980 sb_end_pagefault(inode->i_sb);
7984 static int btrfs_truncate(struct inode *inode)
7986 struct btrfs_root *root = BTRFS_I(inode)->root;
7987 struct btrfs_block_rsv *rsv;
7990 struct btrfs_trans_handle *trans;
7991 u64 mask = root->sectorsize - 1;
7992 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
7994 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8000 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8001 * 3 things going on here
8003 * 1) We need to reserve space for our orphan item and the space to
8004 * delete our orphan item. Lord knows we don't want to have a dangling
8005 * orphan item because we didn't reserve space to remove it.
8007 * 2) We need to reserve space to update our inode.
8009 * 3) We need to have something to cache all the space that is going to
8010 * be free'd up by the truncate operation, but also have some slack
8011 * space reserved in case it uses space during the truncate (thank you
8012 * very much snapshotting).
8014 * And we need these to all be seperate. The fact is we can use alot of
8015 * space doing the truncate, and we have no earthly idea how much space
8016 * we will use, so we need the truncate reservation to be seperate so it
8017 * doesn't end up using space reserved for updating the inode or
8018 * removing the orphan item. We also need to be able to stop the
8019 * transaction and start a new one, which means we need to be able to
8020 * update the inode several times, and we have no idea of knowing how
8021 * many times that will be, so we can't just reserve 1 item for the
8022 * entirety of the opration, so that has to be done seperately as well.
8023 * Then there is the orphan item, which does indeed need to be held on
8024 * to for the whole operation, and we need nobody to touch this reserved
8025 * space except the orphan code.
8027 * So that leaves us with
8029 * 1) root->orphan_block_rsv - for the orphan deletion.
8030 * 2) rsv - for the truncate reservation, which we will steal from the
8031 * transaction reservation.
8032 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8033 * updating the inode.
8035 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
8038 rsv->size = min_size;
8042 * 1 for the truncate slack space
8043 * 1 for updating the inode.
8045 trans = btrfs_start_transaction(root, 2);
8046 if (IS_ERR(trans)) {
8047 err = PTR_ERR(trans);
8051 /* Migrate the slack space for the truncate to our reserve */
8052 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
8057 * So if we truncate and then write and fsync we normally would just
8058 * write the extents that changed, which is a problem if we need to
8059 * first truncate that entire inode. So set this flag so we write out
8060 * all of the extents in the inode to the sync log so we're completely
8063 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
8064 trans->block_rsv = rsv;
8067 ret = btrfs_truncate_inode_items(trans, root, inode,
8069 BTRFS_EXTENT_DATA_KEY);
8070 if (ret != -ENOSPC) {
8075 trans->block_rsv = &root->fs_info->trans_block_rsv;
8076 ret = btrfs_update_inode(trans, root, inode);
8082 btrfs_end_transaction(trans, root);
8083 btrfs_btree_balance_dirty(root);
8085 trans = btrfs_start_transaction(root, 2);
8086 if (IS_ERR(trans)) {
8087 ret = err = PTR_ERR(trans);
8092 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
8094 BUG_ON(ret); /* shouldn't happen */
8095 trans->block_rsv = rsv;
8098 if (ret == 0 && inode->i_nlink > 0) {
8099 trans->block_rsv = root->orphan_block_rsv;
8100 ret = btrfs_orphan_del(trans, inode);
8106 trans->block_rsv = &root->fs_info->trans_block_rsv;
8107 ret = btrfs_update_inode(trans, root, inode);
8111 ret = btrfs_end_transaction(trans, root);
8112 btrfs_btree_balance_dirty(root);
8116 btrfs_free_block_rsv(root, rsv);
8125 * create a new subvolume directory/inode (helper for the ioctl).
8127 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
8128 struct btrfs_root *new_root,
8129 struct btrfs_root *parent_root,
8132 struct inode *inode;
8136 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8137 new_dirid, new_dirid,
8138 S_IFDIR | (~current_umask() & S_IRWXUGO),
8141 return PTR_ERR(inode);
8142 inode->i_op = &btrfs_dir_inode_operations;
8143 inode->i_fop = &btrfs_dir_file_operations;
8145 set_nlink(inode, 1);
8146 btrfs_i_size_write(inode, 0);
8147 unlock_new_inode(inode);
8149 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8151 btrfs_err(new_root->fs_info,
8152 "error inheriting subvolume %llu properties: %d",
8153 new_root->root_key.objectid, err);
8155 err = btrfs_update_inode(trans, new_root, inode);
8161 struct inode *btrfs_alloc_inode(struct super_block *sb)
8163 struct btrfs_inode *ei;
8164 struct inode *inode;
8166 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
8173 ei->last_sub_trans = 0;
8174 ei->logged_trans = 0;
8175 ei->delalloc_bytes = 0;
8176 ei->disk_i_size = 0;
8179 ei->index_cnt = (u64)-1;
8181 ei->last_unlink_trans = 0;
8182 ei->last_log_commit = 0;
8184 spin_lock_init(&ei->lock);
8185 ei->outstanding_extents = 0;
8186 ei->reserved_extents = 0;
8188 ei->runtime_flags = 0;
8189 ei->force_compress = BTRFS_COMPRESS_NONE;
8191 ei->delayed_node = NULL;
8193 inode = &ei->vfs_inode;
8194 extent_map_tree_init(&ei->extent_tree);
8195 extent_io_tree_init(&ei->io_tree, &inode->i_data);
8196 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
8197 ei->io_tree.track_uptodate = 1;
8198 ei->io_failure_tree.track_uptodate = 1;
8199 atomic_set(&ei->sync_writers, 0);
8200 mutex_init(&ei->log_mutex);
8201 mutex_init(&ei->delalloc_mutex);
8202 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
8203 INIT_LIST_HEAD(&ei->delalloc_inodes);
8204 RB_CLEAR_NODE(&ei->rb_node);
8209 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8210 void btrfs_test_destroy_inode(struct inode *inode)
8212 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8213 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8217 static void btrfs_i_callback(struct rcu_head *head)
8219 struct inode *inode = container_of(head, struct inode, i_rcu);
8220 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8223 void btrfs_destroy_inode(struct inode *inode)
8225 struct btrfs_ordered_extent *ordered;
8226 struct btrfs_root *root = BTRFS_I(inode)->root;
8228 WARN_ON(!hlist_empty(&inode->i_dentry));
8229 WARN_ON(inode->i_data.nrpages);
8230 WARN_ON(BTRFS_I(inode)->outstanding_extents);
8231 WARN_ON(BTRFS_I(inode)->reserved_extents);
8232 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
8233 WARN_ON(BTRFS_I(inode)->csum_bytes);
8236 * This can happen where we create an inode, but somebody else also
8237 * created the same inode and we need to destroy the one we already
8243 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8244 &BTRFS_I(inode)->runtime_flags)) {
8245 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
8247 atomic_dec(&root->orphan_inodes);
8251 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8255 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
8256 ordered->file_offset, ordered->len);
8257 btrfs_remove_ordered_extent(inode, ordered);
8258 btrfs_put_ordered_extent(ordered);
8259 btrfs_put_ordered_extent(ordered);
8262 inode_tree_del(inode);
8263 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8265 call_rcu(&inode->i_rcu, btrfs_i_callback);
8268 int btrfs_drop_inode(struct inode *inode)
8270 struct btrfs_root *root = BTRFS_I(inode)->root;
8275 /* the snap/subvol tree is on deleting */
8276 if (btrfs_root_refs(&root->root_item) == 0)
8279 return generic_drop_inode(inode);
8282 static void init_once(void *foo)
8284 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8286 inode_init_once(&ei->vfs_inode);
8289 void btrfs_destroy_cachep(void)
8292 * Make sure all delayed rcu free inodes are flushed before we
8296 if (btrfs_inode_cachep)
8297 kmem_cache_destroy(btrfs_inode_cachep);
8298 if (btrfs_trans_handle_cachep)
8299 kmem_cache_destroy(btrfs_trans_handle_cachep);
8300 if (btrfs_transaction_cachep)
8301 kmem_cache_destroy(btrfs_transaction_cachep);
8302 if (btrfs_path_cachep)
8303 kmem_cache_destroy(btrfs_path_cachep);
8304 if (btrfs_free_space_cachep)
8305 kmem_cache_destroy(btrfs_free_space_cachep);
8306 if (btrfs_delalloc_work_cachep)
8307 kmem_cache_destroy(btrfs_delalloc_work_cachep);
8310 int btrfs_init_cachep(void)
8312 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
8313 sizeof(struct btrfs_inode), 0,
8314 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
8315 if (!btrfs_inode_cachep)
8318 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
8319 sizeof(struct btrfs_trans_handle), 0,
8320 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8321 if (!btrfs_trans_handle_cachep)
8324 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
8325 sizeof(struct btrfs_transaction), 0,
8326 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8327 if (!btrfs_transaction_cachep)
8330 btrfs_path_cachep = kmem_cache_create("btrfs_path",
8331 sizeof(struct btrfs_path), 0,
8332 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8333 if (!btrfs_path_cachep)
8336 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
8337 sizeof(struct btrfs_free_space), 0,
8338 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
8339 if (!btrfs_free_space_cachep)
8342 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
8343 sizeof(struct btrfs_delalloc_work), 0,
8344 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
8346 if (!btrfs_delalloc_work_cachep)
8351 btrfs_destroy_cachep();
8355 static int btrfs_getattr(struct vfsmount *mnt,
8356 struct dentry *dentry, struct kstat *stat)
8359 struct inode *inode = dentry->d_inode;
8360 u32 blocksize = inode->i_sb->s_blocksize;
8362 generic_fillattr(inode, stat);
8363 stat->dev = BTRFS_I(inode)->root->anon_dev;
8364 stat->blksize = PAGE_CACHE_SIZE;
8366 spin_lock(&BTRFS_I(inode)->lock);
8367 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
8368 spin_unlock(&BTRFS_I(inode)->lock);
8369 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
8370 ALIGN(delalloc_bytes, blocksize)) >> 9;
8374 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8375 struct inode *new_dir, struct dentry *new_dentry)
8377 struct btrfs_trans_handle *trans;
8378 struct btrfs_root *root = BTRFS_I(old_dir)->root;
8379 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
8380 struct inode *new_inode = new_dentry->d_inode;
8381 struct inode *old_inode = old_dentry->d_inode;
8382 struct timespec ctime = CURRENT_TIME;
8386 u64 old_ino = btrfs_ino(old_inode);
8388 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
8391 /* we only allow rename subvolume link between subvolumes */
8392 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8395 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8396 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
8399 if (S_ISDIR(old_inode->i_mode) && new_inode &&
8400 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
8404 /* check for collisions, even if the name isn't there */
8405 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
8406 new_dentry->d_name.name,
8407 new_dentry->d_name.len);
8410 if (ret == -EEXIST) {
8412 * eexist without a new_inode */
8413 if (WARN_ON(!new_inode)) {
8417 /* maybe -EOVERFLOW */
8424 * we're using rename to replace one file with another. Start IO on it
8425 * now so we don't add too much work to the end of the transaction
8427 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
8428 filemap_flush(old_inode->i_mapping);
8430 /* close the racy window with snapshot create/destroy ioctl */
8431 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
8432 down_read(&root->fs_info->subvol_sem);
8434 * We want to reserve the absolute worst case amount of items. So if
8435 * both inodes are subvols and we need to unlink them then that would
8436 * require 4 item modifications, but if they are both normal inodes it
8437 * would require 5 item modifications, so we'll assume their normal
8438 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8439 * should cover the worst case number of items we'll modify.
8441 trans = btrfs_start_transaction(root, 11);
8442 if (IS_ERR(trans)) {
8443 ret = PTR_ERR(trans);
8448 btrfs_record_root_in_trans(trans, dest);
8450 ret = btrfs_set_inode_index(new_dir, &index);
8454 BTRFS_I(old_inode)->dir_index = 0ULL;
8455 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8456 /* force full log commit if subvolume involved. */
8457 btrfs_set_log_full_commit(root->fs_info, trans);
8459 ret = btrfs_insert_inode_ref(trans, dest,
8460 new_dentry->d_name.name,
8461 new_dentry->d_name.len,
8463 btrfs_ino(new_dir), index);
8467 * this is an ugly little race, but the rename is required
8468 * to make sure that if we crash, the inode is either at the
8469 * old name or the new one. pinning the log transaction lets
8470 * us make sure we don't allow a log commit to come in after
8471 * we unlink the name but before we add the new name back in.
8473 btrfs_pin_log_trans(root);
8476 inode_inc_iversion(old_dir);
8477 inode_inc_iversion(new_dir);
8478 inode_inc_iversion(old_inode);
8479 old_dir->i_ctime = old_dir->i_mtime = ctime;
8480 new_dir->i_ctime = new_dir->i_mtime = ctime;
8481 old_inode->i_ctime = ctime;
8483 if (old_dentry->d_parent != new_dentry->d_parent)
8484 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8486 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8487 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8488 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8489 old_dentry->d_name.name,
8490 old_dentry->d_name.len);
8492 ret = __btrfs_unlink_inode(trans, root, old_dir,
8493 old_dentry->d_inode,
8494 old_dentry->d_name.name,
8495 old_dentry->d_name.len);
8497 ret = btrfs_update_inode(trans, root, old_inode);
8500 btrfs_abort_transaction(trans, root, ret);
8505 inode_inc_iversion(new_inode);
8506 new_inode->i_ctime = CURRENT_TIME;
8507 if (unlikely(btrfs_ino(new_inode) ==
8508 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8509 root_objectid = BTRFS_I(new_inode)->location.objectid;
8510 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8512 new_dentry->d_name.name,
8513 new_dentry->d_name.len);
8514 BUG_ON(new_inode->i_nlink == 0);
8516 ret = btrfs_unlink_inode(trans, dest, new_dir,
8517 new_dentry->d_inode,
8518 new_dentry->d_name.name,
8519 new_dentry->d_name.len);
8521 if (!ret && new_inode->i_nlink == 0)
8522 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
8524 btrfs_abort_transaction(trans, root, ret);
8529 ret = btrfs_add_link(trans, new_dir, old_inode,
8530 new_dentry->d_name.name,
8531 new_dentry->d_name.len, 0, index);
8533 btrfs_abort_transaction(trans, root, ret);
8537 if (old_inode->i_nlink == 1)
8538 BTRFS_I(old_inode)->dir_index = index;
8540 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
8541 struct dentry *parent = new_dentry->d_parent;
8542 btrfs_log_new_name(trans, old_inode, old_dir, parent);
8543 btrfs_end_log_trans(root);
8546 btrfs_end_transaction(trans, root);
8548 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
8549 up_read(&root->fs_info->subvol_sem);
8554 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
8555 struct inode *new_dir, struct dentry *new_dentry,
8558 if (flags & ~RENAME_NOREPLACE)
8561 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry);
8564 static void btrfs_run_delalloc_work(struct btrfs_work *work)
8566 struct btrfs_delalloc_work *delalloc_work;
8567 struct inode *inode;
8569 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8571 inode = delalloc_work->inode;
8572 if (delalloc_work->wait) {
8573 btrfs_wait_ordered_range(inode, 0, (u64)-1);
8575 filemap_flush(inode->i_mapping);
8576 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8577 &BTRFS_I(inode)->runtime_flags))
8578 filemap_flush(inode->i_mapping);
8581 if (delalloc_work->delay_iput)
8582 btrfs_add_delayed_iput(inode);
8585 complete(&delalloc_work->completion);
8588 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8589 int wait, int delay_iput)
8591 struct btrfs_delalloc_work *work;
8593 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8597 init_completion(&work->completion);
8598 INIT_LIST_HEAD(&work->list);
8599 work->inode = inode;
8601 work->delay_iput = delay_iput;
8602 WARN_ON_ONCE(!inode);
8603 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
8604 btrfs_run_delalloc_work, NULL, NULL);
8609 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8611 wait_for_completion(&work->completion);
8612 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8616 * some fairly slow code that needs optimization. This walks the list
8617 * of all the inodes with pending delalloc and forces them to disk.
8619 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
8622 struct btrfs_inode *binode;
8623 struct inode *inode;
8624 struct btrfs_delalloc_work *work, *next;
8625 struct list_head works;
8626 struct list_head splice;
8629 INIT_LIST_HEAD(&works);
8630 INIT_LIST_HEAD(&splice);
8632 mutex_lock(&root->delalloc_mutex);
8633 spin_lock(&root->delalloc_lock);
8634 list_splice_init(&root->delalloc_inodes, &splice);
8635 while (!list_empty(&splice)) {
8636 binode = list_entry(splice.next, struct btrfs_inode,
8639 list_move_tail(&binode->delalloc_inodes,
8640 &root->delalloc_inodes);
8641 inode = igrab(&binode->vfs_inode);
8643 cond_resched_lock(&root->delalloc_lock);
8646 spin_unlock(&root->delalloc_lock);
8648 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8649 if (unlikely(!work)) {
8651 btrfs_add_delayed_iput(inode);
8657 list_add_tail(&work->list, &works);
8658 btrfs_queue_work(root->fs_info->flush_workers,
8661 if (nr != -1 && ret >= nr)
8664 spin_lock(&root->delalloc_lock);
8666 spin_unlock(&root->delalloc_lock);
8669 list_for_each_entry_safe(work, next, &works, list) {
8670 list_del_init(&work->list);
8671 btrfs_wait_and_free_delalloc_work(work);
8674 if (!list_empty_careful(&splice)) {
8675 spin_lock(&root->delalloc_lock);
8676 list_splice_tail(&splice, &root->delalloc_inodes);
8677 spin_unlock(&root->delalloc_lock);
8679 mutex_unlock(&root->delalloc_mutex);
8683 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8687 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
8690 ret = __start_delalloc_inodes(root, delay_iput, -1);
8694 * the filemap_flush will queue IO into the worker threads, but
8695 * we have to make sure the IO is actually started and that
8696 * ordered extents get created before we return
8698 atomic_inc(&root->fs_info->async_submit_draining);
8699 while (atomic_read(&root->fs_info->nr_async_submits) ||
8700 atomic_read(&root->fs_info->async_delalloc_pages)) {
8701 wait_event(root->fs_info->async_submit_wait,
8702 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8703 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8705 atomic_dec(&root->fs_info->async_submit_draining);
8709 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
8712 struct btrfs_root *root;
8713 struct list_head splice;
8716 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
8719 INIT_LIST_HEAD(&splice);
8721 mutex_lock(&fs_info->delalloc_root_mutex);
8722 spin_lock(&fs_info->delalloc_root_lock);
8723 list_splice_init(&fs_info->delalloc_roots, &splice);
8724 while (!list_empty(&splice) && nr) {
8725 root = list_first_entry(&splice, struct btrfs_root,
8727 root = btrfs_grab_fs_root(root);
8729 list_move_tail(&root->delalloc_root,
8730 &fs_info->delalloc_roots);
8731 spin_unlock(&fs_info->delalloc_root_lock);
8733 ret = __start_delalloc_inodes(root, delay_iput, nr);
8734 btrfs_put_fs_root(root);
8742 spin_lock(&fs_info->delalloc_root_lock);
8744 spin_unlock(&fs_info->delalloc_root_lock);
8747 atomic_inc(&fs_info->async_submit_draining);
8748 while (atomic_read(&fs_info->nr_async_submits) ||
8749 atomic_read(&fs_info->async_delalloc_pages)) {
8750 wait_event(fs_info->async_submit_wait,
8751 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8752 atomic_read(&fs_info->async_delalloc_pages) == 0));
8754 atomic_dec(&fs_info->async_submit_draining);
8756 if (!list_empty_careful(&splice)) {
8757 spin_lock(&fs_info->delalloc_root_lock);
8758 list_splice_tail(&splice, &fs_info->delalloc_roots);
8759 spin_unlock(&fs_info->delalloc_root_lock);
8761 mutex_unlock(&fs_info->delalloc_root_mutex);
8765 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8766 const char *symname)
8768 struct btrfs_trans_handle *trans;
8769 struct btrfs_root *root = BTRFS_I(dir)->root;
8770 struct btrfs_path *path;
8771 struct btrfs_key key;
8772 struct inode *inode = NULL;
8780 struct btrfs_file_extent_item *ei;
8781 struct extent_buffer *leaf;
8783 name_len = strlen(symname);
8784 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8785 return -ENAMETOOLONG;
8788 * 2 items for inode item and ref
8789 * 2 items for dir items
8790 * 1 item for xattr if selinux is on
8792 trans = btrfs_start_transaction(root, 5);
8794 return PTR_ERR(trans);
8796 err = btrfs_find_free_ino(root, &objectid);
8800 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
8801 dentry->d_name.len, btrfs_ino(dir), objectid,
8802 S_IFLNK|S_IRWXUGO, &index);
8803 if (IS_ERR(inode)) {
8804 err = PTR_ERR(inode);
8809 * If the active LSM wants to access the inode during
8810 * d_instantiate it needs these. Smack checks to see
8811 * if the filesystem supports xattrs by looking at the
8814 inode->i_fop = &btrfs_file_operations;
8815 inode->i_op = &btrfs_file_inode_operations;
8816 inode->i_mapping->a_ops = &btrfs_aops;
8817 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8818 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8820 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8822 goto out_unlock_inode;
8824 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
8826 goto out_unlock_inode;
8828 path = btrfs_alloc_path();
8831 goto out_unlock_inode;
8833 key.objectid = btrfs_ino(inode);
8835 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8836 datasize = btrfs_file_extent_calc_inline_size(name_len);
8837 err = btrfs_insert_empty_item(trans, root, path, &key,
8840 btrfs_free_path(path);
8841 goto out_unlock_inode;
8843 leaf = path->nodes[0];
8844 ei = btrfs_item_ptr(leaf, path->slots[0],
8845 struct btrfs_file_extent_item);
8846 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8847 btrfs_set_file_extent_type(leaf, ei,
8848 BTRFS_FILE_EXTENT_INLINE);
8849 btrfs_set_file_extent_encryption(leaf, ei, 0);
8850 btrfs_set_file_extent_compression(leaf, ei, 0);
8851 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8852 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8854 ptr = btrfs_file_extent_inline_start(ei);
8855 write_extent_buffer(leaf, symname, ptr, name_len);
8856 btrfs_mark_buffer_dirty(leaf);
8857 btrfs_free_path(path);
8859 inode->i_op = &btrfs_symlink_inode_operations;
8860 inode->i_mapping->a_ops = &btrfs_symlink_aops;
8861 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8862 inode_set_bytes(inode, name_len);
8863 btrfs_i_size_write(inode, name_len);
8864 err = btrfs_update_inode(trans, root, inode);
8867 goto out_unlock_inode;
8870 unlock_new_inode(inode);
8871 d_instantiate(dentry, inode);
8874 btrfs_end_transaction(trans, root);
8876 inode_dec_link_count(inode);
8879 btrfs_btree_balance_dirty(root);
8884 unlock_new_inode(inode);
8888 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8889 u64 start, u64 num_bytes, u64 min_size,
8890 loff_t actual_len, u64 *alloc_hint,
8891 struct btrfs_trans_handle *trans)
8893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8894 struct extent_map *em;
8895 struct btrfs_root *root = BTRFS_I(inode)->root;
8896 struct btrfs_key ins;
8897 u64 cur_offset = start;
8901 bool own_trans = true;
8905 while (num_bytes > 0) {
8907 trans = btrfs_start_transaction(root, 3);
8908 if (IS_ERR(trans)) {
8909 ret = PTR_ERR(trans);
8914 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8915 cur_bytes = max(cur_bytes, min_size);
8916 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8917 *alloc_hint, &ins, 1, 0);
8920 btrfs_end_transaction(trans, root);
8924 ret = insert_reserved_file_extent(trans, inode,
8925 cur_offset, ins.objectid,
8926 ins.offset, ins.offset,
8927 ins.offset, 0, 0, 0,
8928 BTRFS_FILE_EXTENT_PREALLOC);
8930 btrfs_free_reserved_extent(root, ins.objectid,
8932 btrfs_abort_transaction(trans, root, ret);
8934 btrfs_end_transaction(trans, root);
8937 btrfs_drop_extent_cache(inode, cur_offset,
8938 cur_offset + ins.offset -1, 0);
8940 em = alloc_extent_map();
8942 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8943 &BTRFS_I(inode)->runtime_flags);
8947 em->start = cur_offset;
8948 em->orig_start = cur_offset;
8949 em->len = ins.offset;
8950 em->block_start = ins.objectid;
8951 em->block_len = ins.offset;
8952 em->orig_block_len = ins.offset;
8953 em->ram_bytes = ins.offset;
8954 em->bdev = root->fs_info->fs_devices->latest_bdev;
8955 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8956 em->generation = trans->transid;
8959 write_lock(&em_tree->lock);
8960 ret = add_extent_mapping(em_tree, em, 1);
8961 write_unlock(&em_tree->lock);
8964 btrfs_drop_extent_cache(inode, cur_offset,
8965 cur_offset + ins.offset - 1,
8968 free_extent_map(em);
8970 num_bytes -= ins.offset;
8971 cur_offset += ins.offset;
8972 *alloc_hint = ins.objectid + ins.offset;
8974 inode_inc_iversion(inode);
8975 inode->i_ctime = CURRENT_TIME;
8976 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
8977 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
8978 (actual_len > inode->i_size) &&
8979 (cur_offset > inode->i_size)) {
8980 if (cur_offset > actual_len)
8981 i_size = actual_len;
8983 i_size = cur_offset;
8984 i_size_write(inode, i_size);
8985 btrfs_ordered_update_i_size(inode, i_size, NULL);
8988 ret = btrfs_update_inode(trans, root, inode);
8991 btrfs_abort_transaction(trans, root, ret);
8993 btrfs_end_transaction(trans, root);
8998 btrfs_end_transaction(trans, root);
9003 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9004 u64 start, u64 num_bytes, u64 min_size,
9005 loff_t actual_len, u64 *alloc_hint)
9007 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9008 min_size, actual_len, alloc_hint,
9012 int btrfs_prealloc_file_range_trans(struct inode *inode,
9013 struct btrfs_trans_handle *trans, int mode,
9014 u64 start, u64 num_bytes, u64 min_size,
9015 loff_t actual_len, u64 *alloc_hint)
9017 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9018 min_size, actual_len, alloc_hint, trans);
9021 static int btrfs_set_page_dirty(struct page *page)
9023 return __set_page_dirty_nobuffers(page);
9026 static int btrfs_permission(struct inode *inode, int mask)
9028 struct btrfs_root *root = BTRFS_I(inode)->root;
9029 umode_t mode = inode->i_mode;
9031 if (mask & MAY_WRITE &&
9032 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9033 if (btrfs_root_readonly(root))
9035 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9038 return generic_permission(inode, mask);
9041 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9043 struct btrfs_trans_handle *trans;
9044 struct btrfs_root *root = BTRFS_I(dir)->root;
9045 struct inode *inode = NULL;
9051 * 5 units required for adding orphan entry
9053 trans = btrfs_start_transaction(root, 5);
9055 return PTR_ERR(trans);
9057 ret = btrfs_find_free_ino(root, &objectid);
9061 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9062 btrfs_ino(dir), objectid, mode, &index);
9063 if (IS_ERR(inode)) {
9064 ret = PTR_ERR(inode);
9069 inode->i_fop = &btrfs_file_operations;
9070 inode->i_op = &btrfs_file_inode_operations;
9072 inode->i_mapping->a_ops = &btrfs_aops;
9073 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
9074 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9076 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9080 ret = btrfs_update_inode(trans, root, inode);
9083 ret = btrfs_orphan_add(trans, inode);
9088 * We set number of links to 0 in btrfs_new_inode(), and here we set
9089 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9092 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9094 set_nlink(inode, 1);
9095 unlock_new_inode(inode);
9096 d_tmpfile(dentry, inode);
9097 mark_inode_dirty(inode);
9100 btrfs_end_transaction(trans, root);
9103 btrfs_balance_delayed_items(root);
9104 btrfs_btree_balance_dirty(root);
9108 unlock_new_inode(inode);
9113 static const struct inode_operations btrfs_dir_inode_operations = {
9114 .getattr = btrfs_getattr,
9115 .lookup = btrfs_lookup,
9116 .create = btrfs_create,
9117 .unlink = btrfs_unlink,
9119 .mkdir = btrfs_mkdir,
9120 .rmdir = btrfs_rmdir,
9121 .rename2 = btrfs_rename2,
9122 .symlink = btrfs_symlink,
9123 .setattr = btrfs_setattr,
9124 .mknod = btrfs_mknod,
9125 .setxattr = btrfs_setxattr,
9126 .getxattr = btrfs_getxattr,
9127 .listxattr = btrfs_listxattr,
9128 .removexattr = btrfs_removexattr,
9129 .permission = btrfs_permission,
9130 .get_acl = btrfs_get_acl,
9131 .set_acl = btrfs_set_acl,
9132 .update_time = btrfs_update_time,
9133 .tmpfile = btrfs_tmpfile,
9135 static const struct inode_operations btrfs_dir_ro_inode_operations = {
9136 .lookup = btrfs_lookup,
9137 .permission = btrfs_permission,
9138 .get_acl = btrfs_get_acl,
9139 .set_acl = btrfs_set_acl,
9140 .update_time = btrfs_update_time,
9143 static const struct file_operations btrfs_dir_file_operations = {
9144 .llseek = generic_file_llseek,
9145 .read = generic_read_dir,
9146 .iterate = btrfs_real_readdir,
9147 .unlocked_ioctl = btrfs_ioctl,
9148 #ifdef CONFIG_COMPAT
9149 .compat_ioctl = btrfs_ioctl,
9151 .release = btrfs_release_file,
9152 .fsync = btrfs_sync_file,
9155 static struct extent_io_ops btrfs_extent_io_ops = {
9156 .fill_delalloc = run_delalloc_range,
9157 .submit_bio_hook = btrfs_submit_bio_hook,
9158 .merge_bio_hook = btrfs_merge_bio_hook,
9159 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
9160 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
9161 .writepage_start_hook = btrfs_writepage_start_hook,
9162 .set_bit_hook = btrfs_set_bit_hook,
9163 .clear_bit_hook = btrfs_clear_bit_hook,
9164 .merge_extent_hook = btrfs_merge_extent_hook,
9165 .split_extent_hook = btrfs_split_extent_hook,
9169 * btrfs doesn't support the bmap operation because swapfiles
9170 * use bmap to make a mapping of extents in the file. They assume
9171 * these extents won't change over the life of the file and they
9172 * use the bmap result to do IO directly to the drive.
9174 * the btrfs bmap call would return logical addresses that aren't
9175 * suitable for IO and they also will change frequently as COW
9176 * operations happen. So, swapfile + btrfs == corruption.
9178 * For now we're avoiding this by dropping bmap.
9180 static const struct address_space_operations btrfs_aops = {
9181 .readpage = btrfs_readpage,
9182 .writepage = btrfs_writepage,
9183 .writepages = btrfs_writepages,
9184 .readpages = btrfs_readpages,
9185 .direct_IO = btrfs_direct_IO,
9186 .invalidatepage = btrfs_invalidatepage,
9187 .releasepage = btrfs_releasepage,
9188 .set_page_dirty = btrfs_set_page_dirty,
9189 .error_remove_page = generic_error_remove_page,
9192 static const struct address_space_operations btrfs_symlink_aops = {
9193 .readpage = btrfs_readpage,
9194 .writepage = btrfs_writepage,
9195 .invalidatepage = btrfs_invalidatepage,
9196 .releasepage = btrfs_releasepage,
9199 static const struct inode_operations btrfs_file_inode_operations = {
9200 .getattr = btrfs_getattr,
9201 .setattr = btrfs_setattr,
9202 .setxattr = btrfs_setxattr,
9203 .getxattr = btrfs_getxattr,
9204 .listxattr = btrfs_listxattr,
9205 .removexattr = btrfs_removexattr,
9206 .permission = btrfs_permission,
9207 .fiemap = btrfs_fiemap,
9208 .get_acl = btrfs_get_acl,
9209 .set_acl = btrfs_set_acl,
9210 .update_time = btrfs_update_time,
9212 static const struct inode_operations btrfs_special_inode_operations = {
9213 .getattr = btrfs_getattr,
9214 .setattr = btrfs_setattr,
9215 .permission = btrfs_permission,
9216 .setxattr = btrfs_setxattr,
9217 .getxattr = btrfs_getxattr,
9218 .listxattr = btrfs_listxattr,
9219 .removexattr = btrfs_removexattr,
9220 .get_acl = btrfs_get_acl,
9221 .set_acl = btrfs_set_acl,
9222 .update_time = btrfs_update_time,
9224 static const struct inode_operations btrfs_symlink_inode_operations = {
9225 .readlink = generic_readlink,
9226 .follow_link = page_follow_link_light,
9227 .put_link = page_put_link,
9228 .getattr = btrfs_getattr,
9229 .setattr = btrfs_setattr,
9230 .permission = btrfs_permission,
9231 .setxattr = btrfs_setxattr,
9232 .getxattr = btrfs_getxattr,
9233 .listxattr = btrfs_listxattr,
9234 .removexattr = btrfs_removexattr,
9235 .update_time = btrfs_update_time,
9238 const struct dentry_operations btrfs_dentry_operations = {
9239 .d_delete = btrfs_dentry_delete,
9240 .d_release = btrfs_dentry_release,