2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
43 #include "transaction.h"
44 #include "btrfs_inode.h"
46 #include "print-tree.h"
48 #include "ordered-data.h"
51 #include "compression.h"
54 struct btrfs_iget_args {
56 struct btrfs_root *root;
59 static const struct inode_operations btrfs_dir_inode_operations;
60 static const struct inode_operations btrfs_symlink_inode_operations;
61 static const struct inode_operations btrfs_dir_ro_inode_operations;
62 static const struct inode_operations btrfs_special_inode_operations;
63 static const struct inode_operations btrfs_file_inode_operations;
64 static const struct address_space_operations btrfs_aops;
65 static const struct address_space_operations btrfs_symlink_aops;
66 static const struct file_operations btrfs_dir_file_operations;
67 static struct extent_io_ops btrfs_extent_io_ops;
69 static struct kmem_cache *btrfs_inode_cachep;
70 struct kmem_cache *btrfs_trans_handle_cachep;
71 struct kmem_cache *btrfs_transaction_cachep;
72 struct kmem_cache *btrfs_path_cachep;
75 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85 static void btrfs_truncate(struct inode *inode);
86 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
87 static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
92 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
97 err = btrfs_init_acl(trans, inode, dir);
99 err = btrfs_xattr_security_init(trans, inode, dir);
104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
108 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
119 struct btrfs_file_extent_item *ei;
122 size_t cur_size = size;
124 unsigned long offset;
125 int use_compress = 0;
127 if (compressed_size && compressed_pages) {
129 cur_size = compressed_size;
132 path = btrfs_alloc_path();
136 path->leave_spinning = 1;
137 btrfs_set_trans_block_group(trans, inode);
139 key.objectid = inode->i_ino;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
165 while (compressed_size > 0) {
166 cpage = compressed_pages[i];
167 cur_size = min_t(unsigned long, compressed_size,
170 kaddr = kmap_atomic(cpage, KM_USER0);
171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
172 kunmap_atomic(kaddr, KM_USER0);
176 compressed_size -= cur_size;
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
207 btrfs_free_path(path);
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
217 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
229 u64 data_len = inline_len;
233 data_len = compressed_size;
236 actual_end >= PAGE_CACHE_SIZE ||
237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
239 (actual_end & (root->sectorsize - 1)) == 0) ||
241 data_len > root->fs_info->max_inline) {
245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
255 btrfs_delalloc_release_metadata(inode, end + 1 - start);
256 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
260 struct async_extent {
265 unsigned long nr_pages;
266 struct list_head list;
271 struct btrfs_root *root;
272 struct page *locked_page;
275 struct list_head extents;
276 struct btrfs_work work;
279 static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size,
283 unsigned long nr_pages)
285 struct async_extent *async_extent;
287 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
288 async_extent->start = start;
289 async_extent->ram_size = ram_size;
290 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages;
293 list_add_tail(&async_extent->list, &cow->extents);
298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
313 static noinline int compress_file_range(struct inode *inode,
314 struct page *locked_page,
316 struct async_cow *async_cow,
319 struct btrfs_root *root = BTRFS_I(inode)->root;
320 struct btrfs_trans_handle *trans;
322 u64 blocksize = root->sectorsize;
324 u64 isize = i_size_read(inode);
326 struct page **pages = NULL;
327 unsigned long nr_pages;
328 unsigned long nr_pages_ret = 0;
329 unsigned long total_compressed = 0;
330 unsigned long total_in = 0;
331 unsigned long max_compressed = 128 * 1024;
332 unsigned long max_uncompressed = 128 * 1024;
336 actual_end = min_t(u64, isize, end + 1);
339 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
340 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
343 * we don't want to send crud past the end of i_size through
344 * compression, that's just a waste of CPU time. So, if the
345 * end of the file is before the start of our current
346 * requested range of bytes, we bail out to the uncompressed
347 * cleanup code that can deal with all of this.
349 * It isn't really the fastest way to fix things, but this is a
350 * very uncommon corner.
352 if (actual_end <= start)
353 goto cleanup_and_bail_uncompressed;
355 total_compressed = actual_end - start;
357 /* we want to make sure that amount of ram required to uncompress
358 * an extent is reasonable, so we limit the total size in ram
359 * of a compressed extent to 128k. This is a crucial number
360 * because it also controls how easily we can spread reads across
361 * cpus for decompression.
363 * We also want to make sure the amount of IO required to do
364 * a random read is reasonably small, so we limit the size of
365 * a compressed extent to 128k.
367 total_compressed = min(total_compressed, max_uncompressed);
368 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
369 num_bytes = max(blocksize, num_bytes);
374 * we do compression for mount -o compress and when the
375 * inode has not been flagged as nocompress. This flag can
376 * change at any time if we discover bad compression ratios.
378 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
379 (btrfs_test_opt(root, COMPRESS) ||
380 (BTRFS_I(inode)->force_compress))) {
382 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
384 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
385 total_compressed, pages,
386 nr_pages, &nr_pages_ret,
392 unsigned long offset = total_compressed &
393 (PAGE_CACHE_SIZE - 1);
394 struct page *page = pages[nr_pages_ret - 1];
397 /* zero the tail end of the last page, we might be
398 * sending it down to disk
401 kaddr = kmap_atomic(page, KM_USER0);
402 memset(kaddr + offset, 0,
403 PAGE_CACHE_SIZE - offset);
404 kunmap_atomic(kaddr, KM_USER0);
410 trans = btrfs_join_transaction(root, 1);
412 btrfs_set_trans_block_group(trans, inode);
413 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
415 /* lets try to make an inline extent */
416 if (ret || total_in < (actual_end - start)) {
417 /* we didn't compress the entire range, try
418 * to make an uncompressed inline extent.
420 ret = cow_file_range_inline(trans, root, inode,
421 start, end, 0, NULL);
423 /* try making a compressed inline extent */
424 ret = cow_file_range_inline(trans, root, inode,
426 total_compressed, pages);
430 * inline extent creation worked, we don't need
431 * to create any more async work items. Unlock
432 * and free up our temp pages.
434 extent_clear_unlock_delalloc(inode,
435 &BTRFS_I(inode)->io_tree,
437 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
438 EXTENT_CLEAR_DELALLOC |
439 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
441 btrfs_end_transaction(trans, root);
444 btrfs_end_transaction(trans, root);
449 * we aren't doing an inline extent round the compressed size
450 * up to a block size boundary so the allocator does sane
453 total_compressed = (total_compressed + blocksize - 1) &
457 * one last check to make sure the compression is really a
458 * win, compare the page count read with the blocks on disk
460 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
461 ~(PAGE_CACHE_SIZE - 1);
462 if (total_compressed >= total_in) {
465 num_bytes = total_in;
468 if (!will_compress && pages) {
470 * the compression code ran but failed to make things smaller,
471 * free any pages it allocated and our page pointer array
473 for (i = 0; i < nr_pages_ret; i++) {
474 WARN_ON(pages[i]->mapping);
475 page_cache_release(pages[i]);
479 total_compressed = 0;
482 /* flag the file so we don't compress in the future */
483 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
484 !(BTRFS_I(inode)->force_compress)) {
485 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
491 /* the async work queues will take care of doing actual
492 * allocation on disk for these compressed pages,
493 * and will submit them to the elevator.
495 add_async_extent(async_cow, start, num_bytes,
496 total_compressed, pages, nr_pages_ret);
498 if (start + num_bytes < end) {
505 cleanup_and_bail_uncompressed:
507 * No compression, but we still need to write the pages in
508 * the file we've been given so far. redirty the locked
509 * page if it corresponds to our extent and set things up
510 * for the async work queue to run cow_file_range to do
511 * the normal delalloc dance
513 if (page_offset(locked_page) >= start &&
514 page_offset(locked_page) <= end) {
515 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
526 for (i = 0; i < nr_pages_ret; i++) {
527 WARN_ON(pages[i]->mapping);
528 page_cache_release(pages[i]);
536 * phase two of compressed writeback. This is the ordered portion
537 * of the code, which only gets called in the order the work was
538 * queued. We walk all the async extents created by compress_file_range
539 * and send them down to the disk.
541 static noinline int submit_compressed_extents(struct inode *inode,
542 struct async_cow *async_cow)
544 struct async_extent *async_extent;
546 struct btrfs_trans_handle *trans;
547 struct btrfs_key ins;
548 struct extent_map *em;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551 struct extent_io_tree *io_tree;
554 if (list_empty(&async_cow->extents))
558 while (!list_empty(&async_cow->extents)) {
559 async_extent = list_entry(async_cow->extents.next,
560 struct async_extent, list);
561 list_del(&async_extent->list);
563 io_tree = &BTRFS_I(inode)->io_tree;
566 /* did the compression code fall back to uncompressed IO? */
567 if (!async_extent->pages) {
568 int page_started = 0;
569 unsigned long nr_written = 0;
571 lock_extent(io_tree, async_extent->start,
572 async_extent->start +
573 async_extent->ram_size - 1, GFP_NOFS);
575 /* allocate blocks */
576 ret = cow_file_range(inode, async_cow->locked_page,
578 async_extent->start +
579 async_extent->ram_size - 1,
580 &page_started, &nr_written, 0);
583 * if page_started, cow_file_range inserted an
584 * inline extent and took care of all the unlocking
585 * and IO for us. Otherwise, we need to submit
586 * all those pages down to the drive.
588 if (!page_started && !ret)
589 extent_write_locked_range(io_tree,
590 inode, async_extent->start,
591 async_extent->start +
592 async_extent->ram_size - 1,
600 lock_extent(io_tree, async_extent->start,
601 async_extent->start + async_extent->ram_size - 1,
604 trans = btrfs_join_transaction(root, 1);
605 ret = btrfs_reserve_extent(trans, root,
606 async_extent->compressed_size,
607 async_extent->compressed_size,
610 btrfs_end_transaction(trans, root);
614 for (i = 0; i < async_extent->nr_pages; i++) {
615 WARN_ON(async_extent->pages[i]->mapping);
616 page_cache_release(async_extent->pages[i]);
618 kfree(async_extent->pages);
619 async_extent->nr_pages = 0;
620 async_extent->pages = NULL;
621 unlock_extent(io_tree, async_extent->start,
622 async_extent->start +
623 async_extent->ram_size - 1, GFP_NOFS);
628 * here we're doing allocation and writeback of the
631 btrfs_drop_extent_cache(inode, async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1, 0);
635 em = alloc_extent_map(GFP_NOFS);
636 em->start = async_extent->start;
637 em->len = async_extent->ram_size;
638 em->orig_start = em->start;
640 em->block_start = ins.objectid;
641 em->block_len = ins.offset;
642 em->bdev = root->fs_info->fs_devices->latest_bdev;
643 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
647 write_lock(&em_tree->lock);
648 ret = add_extent_mapping(em_tree, em);
649 write_unlock(&em_tree->lock);
650 if (ret != -EEXIST) {
654 btrfs_drop_extent_cache(inode, async_extent->start,
655 async_extent->start +
656 async_extent->ram_size - 1, 0);
659 ret = btrfs_add_ordered_extent(inode, async_extent->start,
661 async_extent->ram_size,
663 BTRFS_ORDERED_COMPRESSED);
667 * clear dirty, set writeback and unlock the pages.
669 extent_clear_unlock_delalloc(inode,
670 &BTRFS_I(inode)->io_tree,
672 async_extent->start +
673 async_extent->ram_size - 1,
674 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
675 EXTENT_CLEAR_UNLOCK |
676 EXTENT_CLEAR_DELALLOC |
677 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
679 ret = btrfs_submit_compressed_write(inode,
681 async_extent->ram_size,
683 ins.offset, async_extent->pages,
684 async_extent->nr_pages);
687 alloc_hint = ins.objectid + ins.offset;
695 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
698 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
699 struct extent_map *em;
702 read_lock(&em_tree->lock);
703 em = search_extent_mapping(em_tree, start, num_bytes);
706 * if block start isn't an actual block number then find the
707 * first block in this inode and use that as a hint. If that
708 * block is also bogus then just don't worry about it.
710 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
712 em = search_extent_mapping(em_tree, 0, 0);
713 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
714 alloc_hint = em->block_start;
718 alloc_hint = em->block_start;
722 read_unlock(&em_tree->lock);
728 * when extent_io.c finds a delayed allocation range in the file,
729 * the call backs end up in this code. The basic idea is to
730 * allocate extents on disk for the range, and create ordered data structs
731 * in ram to track those extents.
733 * locked_page is the page that writepage had locked already. We use
734 * it to make sure we don't do extra locks or unlocks.
736 * *page_started is set to one if we unlock locked_page and do everything
737 * required to start IO on it. It may be clean and already done with
740 static noinline int cow_file_range(struct inode *inode,
741 struct page *locked_page,
742 u64 start, u64 end, int *page_started,
743 unsigned long *nr_written,
746 struct btrfs_root *root = BTRFS_I(inode)->root;
747 struct btrfs_trans_handle *trans;
750 unsigned long ram_size;
753 u64 blocksize = root->sectorsize;
754 struct btrfs_key ins;
755 struct extent_map *em;
756 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
759 BUG_ON(root == root->fs_info->tree_root);
760 trans = btrfs_join_transaction(root, 1);
762 btrfs_set_trans_block_group(trans, inode);
763 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
765 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
766 num_bytes = max(blocksize, num_bytes);
767 disk_num_bytes = num_bytes;
771 /* lets try to make an inline extent */
772 ret = cow_file_range_inline(trans, root, inode,
773 start, end, 0, NULL);
775 extent_clear_unlock_delalloc(inode,
776 &BTRFS_I(inode)->io_tree,
778 EXTENT_CLEAR_UNLOCK_PAGE |
779 EXTENT_CLEAR_UNLOCK |
780 EXTENT_CLEAR_DELALLOC |
782 EXTENT_SET_WRITEBACK |
783 EXTENT_END_WRITEBACK);
785 *nr_written = *nr_written +
786 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
793 BUG_ON(disk_num_bytes >
794 btrfs_super_total_bytes(&root->fs_info->super_copy));
796 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
797 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
799 while (disk_num_bytes > 0) {
802 cur_alloc_size = disk_num_bytes;
803 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
804 root->sectorsize, 0, alloc_hint,
808 em = alloc_extent_map(GFP_NOFS);
810 em->orig_start = em->start;
811 ram_size = ins.offset;
812 em->len = ins.offset;
814 em->block_start = ins.objectid;
815 em->block_len = ins.offset;
816 em->bdev = root->fs_info->fs_devices->latest_bdev;
817 set_bit(EXTENT_FLAG_PINNED, &em->flags);
820 write_lock(&em_tree->lock);
821 ret = add_extent_mapping(em_tree, em);
822 write_unlock(&em_tree->lock);
823 if (ret != -EEXIST) {
827 btrfs_drop_extent_cache(inode, start,
828 start + ram_size - 1, 0);
831 cur_alloc_size = ins.offset;
832 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
833 ram_size, cur_alloc_size, 0);
836 if (root->root_key.objectid ==
837 BTRFS_DATA_RELOC_TREE_OBJECTID) {
838 ret = btrfs_reloc_clone_csums(inode, start,
843 if (disk_num_bytes < cur_alloc_size)
846 /* we're not doing compressed IO, don't unlock the first
847 * page (which the caller expects to stay locked), don't
848 * clear any dirty bits and don't set any writeback bits
850 * Do set the Private2 bit so we know this page was properly
851 * setup for writepage
853 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
854 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
857 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
858 start, start + ram_size - 1,
860 disk_num_bytes -= cur_alloc_size;
861 num_bytes -= cur_alloc_size;
862 alloc_hint = ins.objectid + ins.offset;
863 start += cur_alloc_size;
867 btrfs_end_transaction(trans, root);
873 * work queue call back to started compression on a file and pages
875 static noinline void async_cow_start(struct btrfs_work *work)
877 struct async_cow *async_cow;
879 async_cow = container_of(work, struct async_cow, work);
881 compress_file_range(async_cow->inode, async_cow->locked_page,
882 async_cow->start, async_cow->end, async_cow,
885 async_cow->inode = NULL;
889 * work queue call back to submit previously compressed pages
891 static noinline void async_cow_submit(struct btrfs_work *work)
893 struct async_cow *async_cow;
894 struct btrfs_root *root;
895 unsigned long nr_pages;
897 async_cow = container_of(work, struct async_cow, work);
899 root = async_cow->root;
900 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
903 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
905 if (atomic_read(&root->fs_info->async_delalloc_pages) <
907 waitqueue_active(&root->fs_info->async_submit_wait))
908 wake_up(&root->fs_info->async_submit_wait);
910 if (async_cow->inode)
911 submit_compressed_extents(async_cow->inode, async_cow);
914 static noinline void async_cow_free(struct btrfs_work *work)
916 struct async_cow *async_cow;
917 async_cow = container_of(work, struct async_cow, work);
921 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
922 u64 start, u64 end, int *page_started,
923 unsigned long *nr_written)
925 struct async_cow *async_cow;
926 struct btrfs_root *root = BTRFS_I(inode)->root;
927 unsigned long nr_pages;
929 int limit = 10 * 1024 * 1042;
931 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
932 1, 0, NULL, GFP_NOFS);
933 while (start < end) {
934 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
935 async_cow->inode = inode;
936 async_cow->root = root;
937 async_cow->locked_page = locked_page;
938 async_cow->start = start;
940 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
943 cur_end = min(end, start + 512 * 1024 - 1);
945 async_cow->end = cur_end;
946 INIT_LIST_HEAD(&async_cow->extents);
948 async_cow->work.func = async_cow_start;
949 async_cow->work.ordered_func = async_cow_submit;
950 async_cow->work.ordered_free = async_cow_free;
951 async_cow->work.flags = 0;
953 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
955 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
957 btrfs_queue_worker(&root->fs_info->delalloc_workers,
960 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
961 wait_event(root->fs_info->async_submit_wait,
962 (atomic_read(&root->fs_info->async_delalloc_pages) <
966 while (atomic_read(&root->fs_info->async_submit_draining) &&
967 atomic_read(&root->fs_info->async_delalloc_pages)) {
968 wait_event(root->fs_info->async_submit_wait,
969 (atomic_read(&root->fs_info->async_delalloc_pages) ==
973 *nr_written += nr_pages;
980 static noinline int csum_exist_in_range(struct btrfs_root *root,
981 u64 bytenr, u64 num_bytes)
984 struct btrfs_ordered_sum *sums;
987 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
988 bytenr + num_bytes - 1, &list);
989 if (ret == 0 && list_empty(&list))
992 while (!list_empty(&list)) {
993 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
994 list_del(&sums->list);
1001 * when nowcow writeback call back. This checks for snapshots or COW copies
1002 * of the extents that exist in the file, and COWs the file as required.
1004 * If no cow copies or snapshots exist, we write directly to the existing
1007 static noinline int run_delalloc_nocow(struct inode *inode,
1008 struct page *locked_page,
1009 u64 start, u64 end, int *page_started, int force,
1010 unsigned long *nr_written)
1012 struct btrfs_root *root = BTRFS_I(inode)->root;
1013 struct btrfs_trans_handle *trans;
1014 struct extent_buffer *leaf;
1015 struct btrfs_path *path;
1016 struct btrfs_file_extent_item *fi;
1017 struct btrfs_key found_key;
1029 bool nolock = false;
1031 path = btrfs_alloc_path();
1033 if (root == root->fs_info->tree_root) {
1035 trans = btrfs_join_transaction_nolock(root, 1);
1037 trans = btrfs_join_transaction(root, 1);
1041 cow_start = (u64)-1;
1044 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1047 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1048 leaf = path->nodes[0];
1049 btrfs_item_key_to_cpu(leaf, &found_key,
1050 path->slots[0] - 1);
1051 if (found_key.objectid == inode->i_ino &&
1052 found_key.type == BTRFS_EXTENT_DATA_KEY)
1057 leaf = path->nodes[0];
1058 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1059 ret = btrfs_next_leaf(root, path);
1064 leaf = path->nodes[0];
1070 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1072 if (found_key.objectid > inode->i_ino ||
1073 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1074 found_key.offset > end)
1077 if (found_key.offset > cur_offset) {
1078 extent_end = found_key.offset;
1083 fi = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_file_extent_item);
1085 extent_type = btrfs_file_extent_type(leaf, fi);
1087 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1088 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1089 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1090 extent_offset = btrfs_file_extent_offset(leaf, fi);
1091 extent_end = found_key.offset +
1092 btrfs_file_extent_num_bytes(leaf, fi);
1093 if (extent_end <= start) {
1097 if (disk_bytenr == 0)
1099 if (btrfs_file_extent_compression(leaf, fi) ||
1100 btrfs_file_extent_encryption(leaf, fi) ||
1101 btrfs_file_extent_other_encoding(leaf, fi))
1103 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1105 if (btrfs_extent_readonly(root, disk_bytenr))
1107 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1109 extent_offset, disk_bytenr))
1111 disk_bytenr += extent_offset;
1112 disk_bytenr += cur_offset - found_key.offset;
1113 num_bytes = min(end + 1, extent_end) - cur_offset;
1115 * force cow if csum exists in the range.
1116 * this ensure that csum for a given extent are
1117 * either valid or do not exist.
1119 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1122 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1123 extent_end = found_key.offset +
1124 btrfs_file_extent_inline_len(leaf, fi);
1125 extent_end = ALIGN(extent_end, root->sectorsize);
1130 if (extent_end <= start) {
1135 if (cow_start == (u64)-1)
1136 cow_start = cur_offset;
1137 cur_offset = extent_end;
1138 if (cur_offset > end)
1144 btrfs_release_path(root, path);
1145 if (cow_start != (u64)-1) {
1146 ret = cow_file_range(inode, locked_page, cow_start,
1147 found_key.offset - 1, page_started,
1150 cow_start = (u64)-1;
1153 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1154 struct extent_map *em;
1155 struct extent_map_tree *em_tree;
1156 em_tree = &BTRFS_I(inode)->extent_tree;
1157 em = alloc_extent_map(GFP_NOFS);
1158 em->start = cur_offset;
1159 em->orig_start = em->start;
1160 em->len = num_bytes;
1161 em->block_len = num_bytes;
1162 em->block_start = disk_bytenr;
1163 em->bdev = root->fs_info->fs_devices->latest_bdev;
1164 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1166 write_lock(&em_tree->lock);
1167 ret = add_extent_mapping(em_tree, em);
1168 write_unlock(&em_tree->lock);
1169 if (ret != -EEXIST) {
1170 free_extent_map(em);
1173 btrfs_drop_extent_cache(inode, em->start,
1174 em->start + em->len - 1, 0);
1176 type = BTRFS_ORDERED_PREALLOC;
1178 type = BTRFS_ORDERED_NOCOW;
1181 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1182 num_bytes, num_bytes, type);
1185 if (root->root_key.objectid ==
1186 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1187 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1192 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1193 cur_offset, cur_offset + num_bytes - 1,
1194 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1195 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1196 EXTENT_SET_PRIVATE2);
1197 cur_offset = extent_end;
1198 if (cur_offset > end)
1201 btrfs_release_path(root, path);
1203 if (cur_offset <= end && cow_start == (u64)-1)
1204 cow_start = cur_offset;
1205 if (cow_start != (u64)-1) {
1206 ret = cow_file_range(inode, locked_page, cow_start, end,
1207 page_started, nr_written, 1);
1212 ret = btrfs_end_transaction_nolock(trans, root);
1215 ret = btrfs_end_transaction(trans, root);
1218 btrfs_free_path(path);
1223 * extent_io.c call back to do delayed allocation processing
1225 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1226 u64 start, u64 end, int *page_started,
1227 unsigned long *nr_written)
1230 struct btrfs_root *root = BTRFS_I(inode)->root;
1232 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1233 ret = run_delalloc_nocow(inode, locked_page, start, end,
1234 page_started, 1, nr_written);
1235 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1236 ret = run_delalloc_nocow(inode, locked_page, start, end,
1237 page_started, 0, nr_written);
1238 else if (!btrfs_test_opt(root, COMPRESS) &&
1239 !(BTRFS_I(inode)->force_compress))
1240 ret = cow_file_range(inode, locked_page, start, end,
1241 page_started, nr_written, 1);
1243 ret = cow_file_range_async(inode, locked_page, start, end,
1244 page_started, nr_written);
1248 static int btrfs_split_extent_hook(struct inode *inode,
1249 struct extent_state *orig, u64 split)
1251 /* not delalloc, ignore it */
1252 if (!(orig->state & EXTENT_DELALLOC))
1255 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1260 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1261 * extents so we can keep track of new extents that are just merged onto old
1262 * extents, such as when we are doing sequential writes, so we can properly
1263 * account for the metadata space we'll need.
1265 static int btrfs_merge_extent_hook(struct inode *inode,
1266 struct extent_state *new,
1267 struct extent_state *other)
1269 /* not delalloc, ignore it */
1270 if (!(other->state & EXTENT_DELALLOC))
1273 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1278 * extent_io.c set_bit_hook, used to track delayed allocation
1279 * bytes in this file, and to maintain the list of inodes that
1280 * have pending delalloc work to be done.
1282 static int btrfs_set_bit_hook(struct inode *inode,
1283 struct extent_state *state, int *bits)
1287 * set_bit and clear bit hooks normally require _irqsave/restore
1288 * but in this case, we are only testeing for the DELALLOC
1289 * bit, which is only set or cleared with irqs on
1291 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1292 struct btrfs_root *root = BTRFS_I(inode)->root;
1293 u64 len = state->end + 1 - state->start;
1294 int do_list = (root->root_key.objectid !=
1295 BTRFS_ROOT_TREE_OBJECTID);
1297 if (*bits & EXTENT_FIRST_DELALLOC)
1298 *bits &= ~EXTENT_FIRST_DELALLOC;
1300 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1302 spin_lock(&root->fs_info->delalloc_lock);
1303 BTRFS_I(inode)->delalloc_bytes += len;
1304 root->fs_info->delalloc_bytes += len;
1305 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1306 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1307 &root->fs_info->delalloc_inodes);
1309 spin_unlock(&root->fs_info->delalloc_lock);
1315 * extent_io.c clear_bit_hook, see set_bit_hook for why
1317 static int btrfs_clear_bit_hook(struct inode *inode,
1318 struct extent_state *state, int *bits)
1321 * set_bit and clear bit hooks normally require _irqsave/restore
1322 * but in this case, we are only testeing for the DELALLOC
1323 * bit, which is only set or cleared with irqs on
1325 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1326 struct btrfs_root *root = BTRFS_I(inode)->root;
1327 u64 len = state->end + 1 - state->start;
1328 int do_list = (root->root_key.objectid !=
1329 BTRFS_ROOT_TREE_OBJECTID);
1331 if (*bits & EXTENT_FIRST_DELALLOC)
1332 *bits &= ~EXTENT_FIRST_DELALLOC;
1333 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1334 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1336 if (*bits & EXTENT_DO_ACCOUNTING)
1337 btrfs_delalloc_release_metadata(inode, len);
1339 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1341 btrfs_free_reserved_data_space(inode, len);
1343 spin_lock(&root->fs_info->delalloc_lock);
1344 root->fs_info->delalloc_bytes -= len;
1345 BTRFS_I(inode)->delalloc_bytes -= len;
1347 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1348 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1349 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1351 spin_unlock(&root->fs_info->delalloc_lock);
1357 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1358 * we don't create bios that span stripes or chunks
1360 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1361 size_t size, struct bio *bio,
1362 unsigned long bio_flags)
1364 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1365 struct btrfs_mapping_tree *map_tree;
1366 u64 logical = (u64)bio->bi_sector << 9;
1371 if (bio_flags & EXTENT_BIO_COMPRESSED)
1374 length = bio->bi_size;
1375 map_tree = &root->fs_info->mapping_tree;
1376 map_length = length;
1377 ret = btrfs_map_block(map_tree, READ, logical,
1378 &map_length, NULL, 0);
1380 if (map_length < length + size)
1386 * in order to insert checksums into the metadata in large chunks,
1387 * we wait until bio submission time. All the pages in the bio are
1388 * checksummed and sums are attached onto the ordered extent record.
1390 * At IO completion time the cums attached on the ordered extent record
1391 * are inserted into the btree
1393 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1394 struct bio *bio, int mirror_num,
1395 unsigned long bio_flags,
1398 struct btrfs_root *root = BTRFS_I(inode)->root;
1401 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1407 * in order to insert checksums into the metadata in large chunks,
1408 * we wait until bio submission time. All the pages in the bio are
1409 * checksummed and sums are attached onto the ordered extent record.
1411 * At IO completion time the cums attached on the ordered extent record
1412 * are inserted into the btree
1414 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1415 int mirror_num, unsigned long bio_flags,
1418 struct btrfs_root *root = BTRFS_I(inode)->root;
1419 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1423 * extent_io.c submission hook. This does the right thing for csum calculation
1424 * on write, or reading the csums from the tree before a read
1426 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1427 int mirror_num, unsigned long bio_flags,
1430 struct btrfs_root *root = BTRFS_I(inode)->root;
1434 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1436 if (root == root->fs_info->tree_root)
1437 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1439 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1442 if (!(rw & REQ_WRITE)) {
1443 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1444 return btrfs_submit_compressed_read(inode, bio,
1445 mirror_num, bio_flags);
1446 } else if (!skip_sum)
1447 btrfs_lookup_bio_sums(root, inode, bio, NULL);
1449 } else if (!skip_sum) {
1450 /* csum items have already been cloned */
1451 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1453 /* we're doing a write, do the async checksumming */
1454 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1455 inode, rw, bio, mirror_num,
1456 bio_flags, bio_offset,
1457 __btrfs_submit_bio_start,
1458 __btrfs_submit_bio_done);
1462 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1466 * given a list of ordered sums record them in the inode. This happens
1467 * at IO completion time based on sums calculated at bio submission time.
1469 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1470 struct inode *inode, u64 file_offset,
1471 struct list_head *list)
1473 struct btrfs_ordered_sum *sum;
1475 btrfs_set_trans_block_group(trans, inode);
1477 list_for_each_entry(sum, list, list) {
1478 btrfs_csum_file_blocks(trans,
1479 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1484 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1485 struct extent_state **cached_state)
1487 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1489 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1490 cached_state, GFP_NOFS);
1493 /* see btrfs_writepage_start_hook for details on why this is required */
1494 struct btrfs_writepage_fixup {
1496 struct btrfs_work work;
1499 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1501 struct btrfs_writepage_fixup *fixup;
1502 struct btrfs_ordered_extent *ordered;
1503 struct extent_state *cached_state = NULL;
1505 struct inode *inode;
1509 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1513 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1514 ClearPageChecked(page);
1518 inode = page->mapping->host;
1519 page_start = page_offset(page);
1520 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1522 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1523 &cached_state, GFP_NOFS);
1525 /* already ordered? We're done */
1526 if (PagePrivate2(page))
1529 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1531 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1532 page_end, &cached_state, GFP_NOFS);
1534 btrfs_start_ordered_extent(inode, ordered, 1);
1539 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1540 ClearPageChecked(page);
1542 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1543 &cached_state, GFP_NOFS);
1546 page_cache_release(page);
1550 * There are a few paths in the higher layers of the kernel that directly
1551 * set the page dirty bit without asking the filesystem if it is a
1552 * good idea. This causes problems because we want to make sure COW
1553 * properly happens and the data=ordered rules are followed.
1555 * In our case any range that doesn't have the ORDERED bit set
1556 * hasn't been properly setup for IO. We kick off an async process
1557 * to fix it up. The async helper will wait for ordered extents, set
1558 * the delalloc bit and make it safe to write the page.
1560 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1562 struct inode *inode = page->mapping->host;
1563 struct btrfs_writepage_fixup *fixup;
1564 struct btrfs_root *root = BTRFS_I(inode)->root;
1566 /* this page is properly in the ordered list */
1567 if (TestClearPagePrivate2(page))
1570 if (PageChecked(page))
1573 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1577 SetPageChecked(page);
1578 page_cache_get(page);
1579 fixup->work.func = btrfs_writepage_fixup_worker;
1581 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1585 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1586 struct inode *inode, u64 file_pos,
1587 u64 disk_bytenr, u64 disk_num_bytes,
1588 u64 num_bytes, u64 ram_bytes,
1589 u8 compression, u8 encryption,
1590 u16 other_encoding, int extent_type)
1592 struct btrfs_root *root = BTRFS_I(inode)->root;
1593 struct btrfs_file_extent_item *fi;
1594 struct btrfs_path *path;
1595 struct extent_buffer *leaf;
1596 struct btrfs_key ins;
1600 path = btrfs_alloc_path();
1603 path->leave_spinning = 1;
1606 * we may be replacing one extent in the tree with another.
1607 * The new extent is pinned in the extent map, and we don't want
1608 * to drop it from the cache until it is completely in the btree.
1610 * So, tell btrfs_drop_extents to leave this extent in the cache.
1611 * the caller is expected to unpin it and allow it to be merged
1614 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1618 ins.objectid = inode->i_ino;
1619 ins.offset = file_pos;
1620 ins.type = BTRFS_EXTENT_DATA_KEY;
1621 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1623 leaf = path->nodes[0];
1624 fi = btrfs_item_ptr(leaf, path->slots[0],
1625 struct btrfs_file_extent_item);
1626 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1627 btrfs_set_file_extent_type(leaf, fi, extent_type);
1628 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1629 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1630 btrfs_set_file_extent_offset(leaf, fi, 0);
1631 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1632 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1633 btrfs_set_file_extent_compression(leaf, fi, compression);
1634 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1635 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1637 btrfs_unlock_up_safe(path, 1);
1638 btrfs_set_lock_blocking(leaf);
1640 btrfs_mark_buffer_dirty(leaf);
1642 inode_add_bytes(inode, num_bytes);
1644 ins.objectid = disk_bytenr;
1645 ins.offset = disk_num_bytes;
1646 ins.type = BTRFS_EXTENT_ITEM_KEY;
1647 ret = btrfs_alloc_reserved_file_extent(trans, root,
1648 root->root_key.objectid,
1649 inode->i_ino, file_pos, &ins);
1651 btrfs_free_path(path);
1657 * helper function for btrfs_finish_ordered_io, this
1658 * just reads in some of the csum leaves to prime them into ram
1659 * before we start the transaction. It limits the amount of btree
1660 * reads required while inside the transaction.
1662 /* as ordered data IO finishes, this gets called so we can finish
1663 * an ordered extent if the range of bytes in the file it covers are
1666 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1668 struct btrfs_root *root = BTRFS_I(inode)->root;
1669 struct btrfs_trans_handle *trans = NULL;
1670 struct btrfs_ordered_extent *ordered_extent = NULL;
1671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1672 struct extent_state *cached_state = NULL;
1675 bool nolock = false;
1677 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1681 BUG_ON(!ordered_extent);
1683 nolock = (root == root->fs_info->tree_root);
1685 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1686 BUG_ON(!list_empty(&ordered_extent->list));
1687 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1690 trans = btrfs_join_transaction_nolock(root, 1);
1692 trans = btrfs_join_transaction(root, 1);
1694 btrfs_set_trans_block_group(trans, inode);
1695 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1696 ret = btrfs_update_inode(trans, root, inode);
1702 lock_extent_bits(io_tree, ordered_extent->file_offset,
1703 ordered_extent->file_offset + ordered_extent->len - 1,
1704 0, &cached_state, GFP_NOFS);
1707 trans = btrfs_join_transaction_nolock(root, 1);
1709 trans = btrfs_join_transaction(root, 1);
1710 btrfs_set_trans_block_group(trans, inode);
1711 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1713 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1715 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1717 ret = btrfs_mark_extent_written(trans, inode,
1718 ordered_extent->file_offset,
1719 ordered_extent->file_offset +
1720 ordered_extent->len);
1723 BUG_ON(root == root->fs_info->tree_root);
1724 ret = insert_reserved_file_extent(trans, inode,
1725 ordered_extent->file_offset,
1726 ordered_extent->start,
1727 ordered_extent->disk_len,
1728 ordered_extent->len,
1729 ordered_extent->len,
1731 BTRFS_FILE_EXTENT_REG);
1732 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1733 ordered_extent->file_offset,
1734 ordered_extent->len);
1737 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1738 ordered_extent->file_offset +
1739 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1741 add_pending_csums(trans, inode, ordered_extent->file_offset,
1742 &ordered_extent->list);
1744 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1745 ret = btrfs_update_inode(trans, root, inode);
1750 btrfs_end_transaction_nolock(trans, root);
1752 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1754 btrfs_end_transaction(trans, root);
1758 btrfs_put_ordered_extent(ordered_extent);
1759 /* once for the tree */
1760 btrfs_put_ordered_extent(ordered_extent);
1765 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1766 struct extent_state *state, int uptodate)
1768 ClearPagePrivate2(page);
1769 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1773 * When IO fails, either with EIO or csum verification fails, we
1774 * try other mirrors that might have a good copy of the data. This
1775 * io_failure_record is used to record state as we go through all the
1776 * mirrors. If another mirror has good data, the page is set up to date
1777 * and things continue. If a good mirror can't be found, the original
1778 * bio end_io callback is called to indicate things have failed.
1780 struct io_failure_record {
1785 unsigned long bio_flags;
1789 static int btrfs_io_failed_hook(struct bio *failed_bio,
1790 struct page *page, u64 start, u64 end,
1791 struct extent_state *state)
1793 struct io_failure_record *failrec = NULL;
1795 struct extent_map *em;
1796 struct inode *inode = page->mapping->host;
1797 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1798 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1805 ret = get_state_private(failure_tree, start, &private);
1807 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1810 failrec->start = start;
1811 failrec->len = end - start + 1;
1812 failrec->last_mirror = 0;
1813 failrec->bio_flags = 0;
1815 read_lock(&em_tree->lock);
1816 em = lookup_extent_mapping(em_tree, start, failrec->len);
1817 if (em->start > start || em->start + em->len < start) {
1818 free_extent_map(em);
1821 read_unlock(&em_tree->lock);
1823 if (!em || IS_ERR(em)) {
1827 logical = start - em->start;
1828 logical = em->block_start + logical;
1829 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1830 logical = em->block_start;
1831 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1833 failrec->logical = logical;
1834 free_extent_map(em);
1835 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1836 EXTENT_DIRTY, GFP_NOFS);
1837 set_state_private(failure_tree, start,
1838 (u64)(unsigned long)failrec);
1840 failrec = (struct io_failure_record *)(unsigned long)private;
1842 num_copies = btrfs_num_copies(
1843 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1844 failrec->logical, failrec->len);
1845 failrec->last_mirror++;
1847 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1848 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1851 if (state && state->start != failrec->start)
1853 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1855 if (!state || failrec->last_mirror > num_copies) {
1856 set_state_private(failure_tree, failrec->start, 0);
1857 clear_extent_bits(failure_tree, failrec->start,
1858 failrec->start + failrec->len - 1,
1859 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1863 bio = bio_alloc(GFP_NOFS, 1);
1864 bio->bi_private = state;
1865 bio->bi_end_io = failed_bio->bi_end_io;
1866 bio->bi_sector = failrec->logical >> 9;
1867 bio->bi_bdev = failed_bio->bi_bdev;
1870 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1871 if (failed_bio->bi_rw & REQ_WRITE)
1876 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1877 failrec->last_mirror,
1878 failrec->bio_flags, 0);
1883 * each time an IO finishes, we do a fast check in the IO failure tree
1884 * to see if we need to process or clean up an io_failure_record
1886 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1889 u64 private_failure;
1890 struct io_failure_record *failure;
1894 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1895 (u64)-1, 1, EXTENT_DIRTY)) {
1896 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1897 start, &private_failure);
1899 failure = (struct io_failure_record *)(unsigned long)
1901 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1903 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1905 failure->start + failure->len - 1,
1906 EXTENT_DIRTY | EXTENT_LOCKED,
1915 * when reads are done, we need to check csums to verify the data is correct
1916 * if there's a match, we allow the bio to finish. If not, we go through
1917 * the io_failure_record routines to find good copies
1919 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1920 struct extent_state *state)
1922 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1923 struct inode *inode = page->mapping->host;
1924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1926 u64 private = ~(u32)0;
1928 struct btrfs_root *root = BTRFS_I(inode)->root;
1931 if (PageChecked(page)) {
1932 ClearPageChecked(page);
1936 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1939 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1940 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1941 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1946 if (state && state->start == start) {
1947 private = state->private;
1950 ret = get_state_private(io_tree, start, &private);
1952 kaddr = kmap_atomic(page, KM_USER0);
1956 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1957 btrfs_csum_final(csum, (char *)&csum);
1958 if (csum != private)
1961 kunmap_atomic(kaddr, KM_USER0);
1963 /* if the io failure tree for this inode is non-empty,
1964 * check to see if we've recovered from a failed IO
1966 btrfs_clean_io_failures(inode, start);
1970 if (printk_ratelimit()) {
1971 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1972 "private %llu\n", page->mapping->host->i_ino,
1973 (unsigned long long)start, csum,
1974 (unsigned long long)private);
1976 memset(kaddr + offset, 1, end - start + 1);
1977 flush_dcache_page(page);
1978 kunmap_atomic(kaddr, KM_USER0);
1984 struct delayed_iput {
1985 struct list_head list;
1986 struct inode *inode;
1989 void btrfs_add_delayed_iput(struct inode *inode)
1991 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1992 struct delayed_iput *delayed;
1994 if (atomic_add_unless(&inode->i_count, -1, 1))
1997 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1998 delayed->inode = inode;
2000 spin_lock(&fs_info->delayed_iput_lock);
2001 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2002 spin_unlock(&fs_info->delayed_iput_lock);
2005 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2008 struct btrfs_fs_info *fs_info = root->fs_info;
2009 struct delayed_iput *delayed;
2012 spin_lock(&fs_info->delayed_iput_lock);
2013 empty = list_empty(&fs_info->delayed_iputs);
2014 spin_unlock(&fs_info->delayed_iput_lock);
2018 down_read(&root->fs_info->cleanup_work_sem);
2019 spin_lock(&fs_info->delayed_iput_lock);
2020 list_splice_init(&fs_info->delayed_iputs, &list);
2021 spin_unlock(&fs_info->delayed_iput_lock);
2023 while (!list_empty(&list)) {
2024 delayed = list_entry(list.next, struct delayed_iput, list);
2025 list_del(&delayed->list);
2026 iput(delayed->inode);
2029 up_read(&root->fs_info->cleanup_work_sem);
2033 * calculate extra metadata reservation when snapshotting a subvolume
2034 * contains orphan files.
2036 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2037 struct btrfs_pending_snapshot *pending,
2038 u64 *bytes_to_reserve)
2040 struct btrfs_root *root;
2041 struct btrfs_block_rsv *block_rsv;
2045 root = pending->root;
2046 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2049 block_rsv = root->orphan_block_rsv;
2051 /* orphan block reservation for the snapshot */
2052 num_bytes = block_rsv->size;
2055 * after the snapshot is created, COWing tree blocks may use more
2056 * space than it frees. So we should make sure there is enough
2059 index = trans->transid & 0x1;
2060 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2061 num_bytes += block_rsv->size -
2062 (block_rsv->reserved + block_rsv->freed[index]);
2065 *bytes_to_reserve += num_bytes;
2068 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2069 struct btrfs_pending_snapshot *pending)
2071 struct btrfs_root *root = pending->root;
2072 struct btrfs_root *snap = pending->snap;
2073 struct btrfs_block_rsv *block_rsv;
2078 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2081 /* refill source subvolume's orphan block reservation */
2082 block_rsv = root->orphan_block_rsv;
2083 index = trans->transid & 0x1;
2084 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2085 num_bytes = block_rsv->size -
2086 (block_rsv->reserved + block_rsv->freed[index]);
2087 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2088 root->orphan_block_rsv,
2093 /* setup orphan block reservation for the snapshot */
2094 block_rsv = btrfs_alloc_block_rsv(snap);
2097 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2098 snap->orphan_block_rsv = block_rsv;
2100 num_bytes = root->orphan_block_rsv->size;
2101 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2102 block_rsv, num_bytes);
2106 /* insert orphan item for the snapshot */
2107 WARN_ON(!root->orphan_item_inserted);
2108 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2109 snap->root_key.objectid);
2111 snap->orphan_item_inserted = 1;
2115 enum btrfs_orphan_cleanup_state {
2116 ORPHAN_CLEANUP_STARTED = 1,
2117 ORPHAN_CLEANUP_DONE = 2,
2121 * This is called in transaction commmit time. If there are no orphan
2122 * files in the subvolume, it removes orphan item and frees block_rsv
2125 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2126 struct btrfs_root *root)
2130 if (!list_empty(&root->orphan_list) ||
2131 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2134 if (root->orphan_item_inserted &&
2135 btrfs_root_refs(&root->root_item) > 0) {
2136 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2137 root->root_key.objectid);
2139 root->orphan_item_inserted = 0;
2142 if (root->orphan_block_rsv) {
2143 WARN_ON(root->orphan_block_rsv->size > 0);
2144 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2145 root->orphan_block_rsv = NULL;
2150 * This creates an orphan entry for the given inode in case something goes
2151 * wrong in the middle of an unlink/truncate.
2153 * NOTE: caller of this function should reserve 5 units of metadata for
2156 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2158 struct btrfs_root *root = BTRFS_I(inode)->root;
2159 struct btrfs_block_rsv *block_rsv = NULL;
2164 if (!root->orphan_block_rsv) {
2165 block_rsv = btrfs_alloc_block_rsv(root);
2169 spin_lock(&root->orphan_lock);
2170 if (!root->orphan_block_rsv) {
2171 root->orphan_block_rsv = block_rsv;
2172 } else if (block_rsv) {
2173 btrfs_free_block_rsv(root, block_rsv);
2177 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2178 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2181 * For proper ENOSPC handling, we should do orphan
2182 * cleanup when mounting. But this introduces backward
2183 * compatibility issue.
2185 if (!xchg(&root->orphan_item_inserted, 1))
2192 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2195 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2196 BTRFS_I(inode)->orphan_meta_reserved = 1;
2199 spin_unlock(&root->orphan_lock);
2202 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2204 /* grab metadata reservation from transaction handle */
2206 ret = btrfs_orphan_reserve_metadata(trans, inode);
2210 /* insert an orphan item to track this unlinked/truncated file */
2212 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2216 /* insert an orphan item to track subvolume contains orphan files */
2218 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2219 root->root_key.objectid);
2226 * We have done the truncate/delete so we can go ahead and remove the orphan
2227 * item for this particular inode.
2229 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2231 struct btrfs_root *root = BTRFS_I(inode)->root;
2232 int delete_item = 0;
2233 int release_rsv = 0;
2236 spin_lock(&root->orphan_lock);
2237 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2238 list_del_init(&BTRFS_I(inode)->i_orphan);
2242 if (BTRFS_I(inode)->orphan_meta_reserved) {
2243 BTRFS_I(inode)->orphan_meta_reserved = 0;
2246 spin_unlock(&root->orphan_lock);
2248 if (trans && delete_item) {
2249 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2254 btrfs_orphan_release_metadata(inode);
2260 * this cleans up any orphans that may be left on the list from the last use
2263 void btrfs_orphan_cleanup(struct btrfs_root *root)
2265 struct btrfs_path *path;
2266 struct extent_buffer *leaf;
2267 struct btrfs_key key, found_key;
2268 struct btrfs_trans_handle *trans;
2269 struct inode *inode;
2270 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2272 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2275 path = btrfs_alloc_path();
2279 key.objectid = BTRFS_ORPHAN_OBJECTID;
2280 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2281 key.offset = (u64)-1;
2284 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2286 printk(KERN_ERR "Error searching slot for orphan: %d"
2292 * if ret == 0 means we found what we were searching for, which
2293 * is weird, but possible, so only screw with path if we didnt
2294 * find the key and see if we have stuff that matches
2297 if (path->slots[0] == 0)
2302 /* pull out the item */
2303 leaf = path->nodes[0];
2304 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2306 /* make sure the item matches what we want */
2307 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2309 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2312 /* release the path since we're done with it */
2313 btrfs_release_path(root, path);
2316 * this is where we are basically btrfs_lookup, without the
2317 * crossing root thing. we store the inode number in the
2318 * offset of the orphan item.
2320 found_key.objectid = found_key.offset;
2321 found_key.type = BTRFS_INODE_ITEM_KEY;
2322 found_key.offset = 0;
2323 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2324 BUG_ON(IS_ERR(inode));
2327 * add this inode to the orphan list so btrfs_orphan_del does
2328 * the proper thing when we hit it
2330 spin_lock(&root->orphan_lock);
2331 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2332 spin_unlock(&root->orphan_lock);
2335 * if this is a bad inode, means we actually succeeded in
2336 * removing the inode, but not the orphan record, which means
2337 * we need to manually delete the orphan since iput will just
2338 * do a destroy_inode
2340 if (is_bad_inode(inode)) {
2341 trans = btrfs_start_transaction(root, 0);
2342 btrfs_orphan_del(trans, inode);
2343 btrfs_end_transaction(trans, root);
2348 /* if we have links, this was a truncate, lets do that */
2349 if (inode->i_nlink) {
2351 btrfs_truncate(inode);
2356 /* this will do delete_inode and everything for us */
2359 btrfs_free_path(path);
2361 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2363 if (root->orphan_block_rsv)
2364 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2367 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2368 trans = btrfs_join_transaction(root, 1);
2369 btrfs_end_transaction(trans, root);
2373 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2375 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2379 * very simple check to peek ahead in the leaf looking for xattrs. If we
2380 * don't find any xattrs, we know there can't be any acls.
2382 * slot is the slot the inode is in, objectid is the objectid of the inode
2384 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2385 int slot, u64 objectid)
2387 u32 nritems = btrfs_header_nritems(leaf);
2388 struct btrfs_key found_key;
2392 while (slot < nritems) {
2393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2395 /* we found a different objectid, there must not be acls */
2396 if (found_key.objectid != objectid)
2399 /* we found an xattr, assume we've got an acl */
2400 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2404 * we found a key greater than an xattr key, there can't
2405 * be any acls later on
2407 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2414 * it goes inode, inode backrefs, xattrs, extents,
2415 * so if there are a ton of hard links to an inode there can
2416 * be a lot of backrefs. Don't waste time searching too hard,
2417 * this is just an optimization
2422 /* we hit the end of the leaf before we found an xattr or
2423 * something larger than an xattr. We have to assume the inode
2430 * read an inode from the btree into the in-memory inode
2432 static void btrfs_read_locked_inode(struct inode *inode)
2434 struct btrfs_path *path;
2435 struct extent_buffer *leaf;
2436 struct btrfs_inode_item *inode_item;
2437 struct btrfs_timespec *tspec;
2438 struct btrfs_root *root = BTRFS_I(inode)->root;
2439 struct btrfs_key location;
2441 u64 alloc_group_block;
2445 path = btrfs_alloc_path();
2447 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2449 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2453 leaf = path->nodes[0];
2454 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2455 struct btrfs_inode_item);
2457 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2458 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2459 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2460 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2461 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2463 tspec = btrfs_inode_atime(inode_item);
2464 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2465 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2467 tspec = btrfs_inode_mtime(inode_item);
2468 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2469 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2471 tspec = btrfs_inode_ctime(inode_item);
2472 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2473 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2475 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2476 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2477 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2478 inode->i_generation = BTRFS_I(inode)->generation;
2480 rdev = btrfs_inode_rdev(leaf, inode_item);
2482 BTRFS_I(inode)->index_cnt = (u64)-1;
2483 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2485 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2488 * try to precache a NULL acl entry for files that don't have
2489 * any xattrs or acls
2491 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
2493 cache_no_acl(inode);
2495 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2496 alloc_group_block, 0);
2497 btrfs_free_path(path);
2500 switch (inode->i_mode & S_IFMT) {
2502 inode->i_mapping->a_ops = &btrfs_aops;
2503 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2504 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2505 inode->i_fop = &btrfs_file_operations;
2506 inode->i_op = &btrfs_file_inode_operations;
2509 inode->i_fop = &btrfs_dir_file_operations;
2510 if (root == root->fs_info->tree_root)
2511 inode->i_op = &btrfs_dir_ro_inode_operations;
2513 inode->i_op = &btrfs_dir_inode_operations;
2516 inode->i_op = &btrfs_symlink_inode_operations;
2517 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2518 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2521 inode->i_op = &btrfs_special_inode_operations;
2522 init_special_inode(inode, inode->i_mode, rdev);
2526 btrfs_update_iflags(inode);
2530 btrfs_free_path(path);
2531 make_bad_inode(inode);
2535 * given a leaf and an inode, copy the inode fields into the leaf
2537 static void fill_inode_item(struct btrfs_trans_handle *trans,
2538 struct extent_buffer *leaf,
2539 struct btrfs_inode_item *item,
2540 struct inode *inode)
2542 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2543 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2544 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2545 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2546 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2548 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2549 inode->i_atime.tv_sec);
2550 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2551 inode->i_atime.tv_nsec);
2553 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2554 inode->i_mtime.tv_sec);
2555 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2556 inode->i_mtime.tv_nsec);
2558 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2559 inode->i_ctime.tv_sec);
2560 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2561 inode->i_ctime.tv_nsec);
2563 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2564 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2565 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2566 btrfs_set_inode_transid(leaf, item, trans->transid);
2567 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2568 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2569 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2573 * copy everything in the in-memory inode into the btree.
2575 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2576 struct btrfs_root *root, struct inode *inode)
2578 struct btrfs_inode_item *inode_item;
2579 struct btrfs_path *path;
2580 struct extent_buffer *leaf;
2583 path = btrfs_alloc_path();
2585 path->leave_spinning = 1;
2586 ret = btrfs_lookup_inode(trans, root, path,
2587 &BTRFS_I(inode)->location, 1);
2594 btrfs_unlock_up_safe(path, 1);
2595 leaf = path->nodes[0];
2596 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2597 struct btrfs_inode_item);
2599 fill_inode_item(trans, leaf, inode_item, inode);
2600 btrfs_mark_buffer_dirty(leaf);
2601 btrfs_set_inode_last_trans(trans, inode);
2604 btrfs_free_path(path);
2610 * unlink helper that gets used here in inode.c and in the tree logging
2611 * recovery code. It remove a link in a directory with a given name, and
2612 * also drops the back refs in the inode to the directory
2614 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root,
2616 struct inode *dir, struct inode *inode,
2617 const char *name, int name_len)
2619 struct btrfs_path *path;
2621 struct extent_buffer *leaf;
2622 struct btrfs_dir_item *di;
2623 struct btrfs_key key;
2626 path = btrfs_alloc_path();
2632 path->leave_spinning = 1;
2633 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2634 name, name_len, -1);
2643 leaf = path->nodes[0];
2644 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2645 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2648 btrfs_release_path(root, path);
2650 ret = btrfs_del_inode_ref(trans, root, name, name_len,
2652 dir->i_ino, &index);
2654 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2655 "inode %lu parent %lu\n", name_len, name,
2656 inode->i_ino, dir->i_ino);
2660 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2661 index, name, name_len, -1);
2670 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2671 btrfs_release_path(root, path);
2673 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2675 BUG_ON(ret != 0 && ret != -ENOENT);
2677 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2682 btrfs_free_path(path);
2686 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2687 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2688 btrfs_update_inode(trans, root, dir);
2689 btrfs_drop_nlink(inode);
2690 ret = btrfs_update_inode(trans, root, inode);
2695 /* helper to check if there is any shared block in the path */
2696 static int check_path_shared(struct btrfs_root *root,
2697 struct btrfs_path *path)
2699 struct extent_buffer *eb;
2702 int uninitialized_var(ret);
2704 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2705 if (!path->nodes[level])
2707 eb = path->nodes[level];
2708 if (!btrfs_block_can_be_shared(root, eb))
2710 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2715 return ret; /* XXX callers? */
2719 * helper to start transaction for unlink and rmdir.
2721 * unlink and rmdir are special in btrfs, they do not always free space.
2722 * so in enospc case, we should make sure they will free space before
2723 * allowing them to use the global metadata reservation.
2725 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2726 struct dentry *dentry)
2728 struct btrfs_trans_handle *trans;
2729 struct btrfs_root *root = BTRFS_I(dir)->root;
2730 struct btrfs_path *path;
2731 struct btrfs_inode_ref *ref;
2732 struct btrfs_dir_item *di;
2733 struct inode *inode = dentry->d_inode;
2739 trans = btrfs_start_transaction(root, 10);
2740 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2743 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2744 return ERR_PTR(-ENOSPC);
2746 /* check if there is someone else holds reference */
2747 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2748 return ERR_PTR(-ENOSPC);
2750 if (atomic_read(&inode->i_count) > 2)
2751 return ERR_PTR(-ENOSPC);
2753 if (xchg(&root->fs_info->enospc_unlink, 1))
2754 return ERR_PTR(-ENOSPC);
2756 path = btrfs_alloc_path();
2758 root->fs_info->enospc_unlink = 0;
2759 return ERR_PTR(-ENOMEM);
2762 trans = btrfs_start_transaction(root, 0);
2763 if (IS_ERR(trans)) {
2764 btrfs_free_path(path);
2765 root->fs_info->enospc_unlink = 0;
2769 path->skip_locking = 1;
2770 path->search_commit_root = 1;
2772 ret = btrfs_lookup_inode(trans, root, path,
2773 &BTRFS_I(dir)->location, 0);
2779 if (check_path_shared(root, path))
2784 btrfs_release_path(root, path);
2786 ret = btrfs_lookup_inode(trans, root, path,
2787 &BTRFS_I(inode)->location, 0);
2793 if (check_path_shared(root, path))
2798 btrfs_release_path(root, path);
2800 if (ret == 0 && S_ISREG(inode->i_mode)) {
2801 ret = btrfs_lookup_file_extent(trans, root, path,
2802 inode->i_ino, (u64)-1, 0);
2808 if (check_path_shared(root, path))
2810 btrfs_release_path(root, path);
2818 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2819 dentry->d_name.name, dentry->d_name.len, 0);
2825 if (check_path_shared(root, path))
2831 btrfs_release_path(root, path);
2833 ref = btrfs_lookup_inode_ref(trans, root, path,
2834 dentry->d_name.name, dentry->d_name.len,
2835 inode->i_ino, dir->i_ino, 0);
2841 if (check_path_shared(root, path))
2843 index = btrfs_inode_ref_index(path->nodes[0], ref);
2844 btrfs_release_path(root, path);
2846 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2847 dentry->d_name.name, dentry->d_name.len, 0);
2852 BUG_ON(ret == -ENOENT);
2853 if (check_path_shared(root, path))
2858 btrfs_free_path(path);
2860 btrfs_end_transaction(trans, root);
2861 root->fs_info->enospc_unlink = 0;
2862 return ERR_PTR(err);
2865 trans->block_rsv = &root->fs_info->global_block_rsv;
2869 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2870 struct btrfs_root *root)
2872 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2873 BUG_ON(!root->fs_info->enospc_unlink);
2874 root->fs_info->enospc_unlink = 0;
2876 btrfs_end_transaction_throttle(trans, root);
2879 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2881 struct btrfs_root *root = BTRFS_I(dir)->root;
2882 struct btrfs_trans_handle *trans;
2883 struct inode *inode = dentry->d_inode;
2885 unsigned long nr = 0;
2887 trans = __unlink_start_trans(dir, dentry);
2889 return PTR_ERR(trans);
2891 btrfs_set_trans_block_group(trans, dir);
2893 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2895 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2896 dentry->d_name.name, dentry->d_name.len);
2899 if (inode->i_nlink == 0) {
2900 ret = btrfs_orphan_add(trans, inode);
2904 nr = trans->blocks_used;
2905 __unlink_end_trans(trans, root);
2906 btrfs_btree_balance_dirty(root, nr);
2910 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *root,
2912 struct inode *dir, u64 objectid,
2913 const char *name, int name_len)
2915 struct btrfs_path *path;
2916 struct extent_buffer *leaf;
2917 struct btrfs_dir_item *di;
2918 struct btrfs_key key;
2922 path = btrfs_alloc_path();
2926 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2927 name, name_len, -1);
2928 BUG_ON(!di || IS_ERR(di));
2930 leaf = path->nodes[0];
2931 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2932 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2933 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2935 btrfs_release_path(root, path);
2937 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2938 objectid, root->root_key.objectid,
2939 dir->i_ino, &index, name, name_len);
2941 BUG_ON(ret != -ENOENT);
2942 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2944 BUG_ON(!di || IS_ERR(di));
2946 leaf = path->nodes[0];
2947 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2948 btrfs_release_path(root, path);
2952 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2953 index, name, name_len, -1);
2954 BUG_ON(!di || IS_ERR(di));
2956 leaf = path->nodes[0];
2957 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2958 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2959 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2961 btrfs_release_path(root, path);
2963 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2964 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2965 ret = btrfs_update_inode(trans, root, dir);
2968 btrfs_free_path(path);
2972 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2974 struct inode *inode = dentry->d_inode;
2976 struct btrfs_root *root = BTRFS_I(dir)->root;
2977 struct btrfs_trans_handle *trans;
2978 unsigned long nr = 0;
2980 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2981 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2984 trans = __unlink_start_trans(dir, dentry);
2986 return PTR_ERR(trans);
2988 btrfs_set_trans_block_group(trans, dir);
2990 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2991 err = btrfs_unlink_subvol(trans, root, dir,
2992 BTRFS_I(inode)->location.objectid,
2993 dentry->d_name.name,
2994 dentry->d_name.len);
2998 err = btrfs_orphan_add(trans, inode);
3002 /* now the directory is empty */
3003 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3004 dentry->d_name.name, dentry->d_name.len);
3006 btrfs_i_size_write(inode, 0);
3008 nr = trans->blocks_used;
3009 __unlink_end_trans(trans, root);
3010 btrfs_btree_balance_dirty(root, nr);
3017 * when truncating bytes in a file, it is possible to avoid reading
3018 * the leaves that contain only checksum items. This can be the
3019 * majority of the IO required to delete a large file, but it must
3020 * be done carefully.
3022 * The keys in the level just above the leaves are checked to make sure
3023 * the lowest key in a given leaf is a csum key, and starts at an offset
3024 * after the new size.
3026 * Then the key for the next leaf is checked to make sure it also has
3027 * a checksum item for the same file. If it does, we know our target leaf
3028 * contains only checksum items, and it can be safely freed without reading
3031 * This is just an optimization targeted at large files. It may do
3032 * nothing. It will return 0 unless things went badly.
3034 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3035 struct btrfs_root *root,
3036 struct btrfs_path *path,
3037 struct inode *inode, u64 new_size)
3039 struct btrfs_key key;
3042 struct btrfs_key found_key;
3043 struct btrfs_key other_key;
3044 struct btrfs_leaf_ref *ref;
3048 path->lowest_level = 1;
3049 key.objectid = inode->i_ino;
3050 key.type = BTRFS_CSUM_ITEM_KEY;
3051 key.offset = new_size;
3053 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3057 if (path->nodes[1] == NULL) {
3062 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3063 nritems = btrfs_header_nritems(path->nodes[1]);
3068 if (path->slots[1] >= nritems)
3071 /* did we find a key greater than anything we want to delete? */
3072 if (found_key.objectid > inode->i_ino ||
3073 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3076 /* we check the next key in the node to make sure the leave contains
3077 * only checksum items. This comparison doesn't work if our
3078 * leaf is the last one in the node
3080 if (path->slots[1] + 1 >= nritems) {
3082 /* search forward from the last key in the node, this
3083 * will bring us into the next node in the tree
3085 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3087 /* unlikely, but we inc below, so check to be safe */
3088 if (found_key.offset == (u64)-1)
3091 /* search_forward needs a path with locks held, do the
3092 * search again for the original key. It is possible
3093 * this will race with a balance and return a path that
3094 * we could modify, but this drop is just an optimization
3095 * and is allowed to miss some leaves.
3097 btrfs_release_path(root, path);
3100 /* setup a max key for search_forward */
3101 other_key.offset = (u64)-1;
3102 other_key.type = key.type;
3103 other_key.objectid = key.objectid;
3105 path->keep_locks = 1;
3106 ret = btrfs_search_forward(root, &found_key, &other_key,
3108 path->keep_locks = 0;
3109 if (ret || found_key.objectid != key.objectid ||
3110 found_key.type != key.type) {
3115 key.offset = found_key.offset;
3116 btrfs_release_path(root, path);
3121 /* we know there's one more slot after us in the tree,
3122 * read that key so we can verify it is also a checksum item
3124 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3126 if (found_key.objectid < inode->i_ino)
3129 if (found_key.type != key.type || found_key.offset < new_size)
3133 * if the key for the next leaf isn't a csum key from this objectid,
3134 * we can't be sure there aren't good items inside this leaf.
3137 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3140 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3141 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3143 * it is safe to delete this leaf, it contains only
3144 * csum items from this inode at an offset >= new_size
3146 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3149 if (root->ref_cows && leaf_gen < trans->transid) {
3150 ref = btrfs_alloc_leaf_ref(root, 0);
3152 ref->root_gen = root->root_key.offset;
3153 ref->bytenr = leaf_start;
3155 ref->generation = leaf_gen;
3158 btrfs_sort_leaf_ref(ref);
3160 ret = btrfs_add_leaf_ref(root, ref, 0);
3162 btrfs_free_leaf_ref(root, ref);
3168 btrfs_release_path(root, path);
3170 if (other_key.objectid == inode->i_ino &&
3171 other_key.type == key.type && other_key.offset > key.offset) {
3172 key.offset = other_key.offset;
3178 /* fixup any changes we've made to the path */
3179 path->lowest_level = 0;
3180 path->keep_locks = 0;
3181 btrfs_release_path(root, path);
3188 * this can truncate away extent items, csum items and directory items.
3189 * It starts at a high offset and removes keys until it can't find
3190 * any higher than new_size
3192 * csum items that cross the new i_size are truncated to the new size
3195 * min_type is the minimum key type to truncate down to. If set to 0, this
3196 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3198 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3199 struct btrfs_root *root,
3200 struct inode *inode,
3201 u64 new_size, u32 min_type)
3203 struct btrfs_path *path;
3204 struct extent_buffer *leaf;
3205 struct btrfs_file_extent_item *fi;
3206 struct btrfs_key key;
3207 struct btrfs_key found_key;
3208 u64 extent_start = 0;
3209 u64 extent_num_bytes = 0;
3210 u64 extent_offset = 0;
3212 u64 mask = root->sectorsize - 1;
3213 u32 found_type = (u8)-1;
3216 int pending_del_nr = 0;
3217 int pending_del_slot = 0;
3218 int extent_type = -1;
3223 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3225 if (root->ref_cows || root == root->fs_info->tree_root)
3226 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3228 path = btrfs_alloc_path();
3232 key.objectid = inode->i_ino;
3233 key.offset = (u64)-1;
3237 path->leave_spinning = 1;
3238 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3245 /* there are no items in the tree for us to truncate, we're
3248 if (path->slots[0] == 0)
3255 leaf = path->nodes[0];
3256 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3257 found_type = btrfs_key_type(&found_key);