2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
44 #include "transaction.h"
45 #include "btrfs_inode.h"
47 #include "print-tree.h"
48 #include "ordered-data.h"
52 #include "compression.h"
54 #include "free-space-cache.h"
55 #include "inode-map.h"
57 struct btrfs_iget_args {
59 struct btrfs_root *root;
62 static const struct inode_operations btrfs_dir_inode_operations;
63 static const struct inode_operations btrfs_symlink_inode_operations;
64 static const struct inode_operations btrfs_dir_ro_inode_operations;
65 static const struct inode_operations btrfs_special_inode_operations;
66 static const struct inode_operations btrfs_file_inode_operations;
67 static const struct address_space_operations btrfs_aops;
68 static const struct address_space_operations btrfs_symlink_aops;
69 static const struct file_operations btrfs_dir_file_operations;
70 static struct extent_io_ops btrfs_extent_io_ops;
72 static struct kmem_cache *btrfs_inode_cachep;
73 struct kmem_cache *btrfs_trans_handle_cachep;
74 struct kmem_cache *btrfs_transaction_cachep;
75 struct kmem_cache *btrfs_path_cachep;
76 struct kmem_cache *btrfs_free_space_cachep;
79 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
80 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
81 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
82 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
83 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
84 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
85 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
86 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89 static int btrfs_setsize(struct inode *inode, loff_t newsize);
90 static int btrfs_truncate(struct inode *inode);
91 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
92 static noinline int cow_file_range(struct inode *inode,
93 struct page *locked_page,
94 u64 start, u64 end, int *page_started,
95 unsigned long *nr_written, int unlock);
96 static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, struct inode *inode);
99 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
100 struct inode *inode, struct inode *dir,
101 const struct qstr *qstr)
105 err = btrfs_init_acl(trans, inode, dir);
107 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
112 * this does all the hard work for inserting an inline extent into
113 * the btree. The caller should have done a btrfs_drop_extents so that
114 * no overlapping inline items exist in the btree
116 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
117 struct btrfs_root *root, struct inode *inode,
118 u64 start, size_t size, size_t compressed_size,
120 struct page **compressed_pages)
122 struct btrfs_key key;
123 struct btrfs_path *path;
124 struct extent_buffer *leaf;
125 struct page *page = NULL;
128 struct btrfs_file_extent_item *ei;
131 size_t cur_size = size;
133 unsigned long offset;
135 if (compressed_size && compressed_pages)
136 cur_size = compressed_size;
138 path = btrfs_alloc_path();
142 path->leave_spinning = 1;
144 key.objectid = btrfs_ino(inode);
146 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
147 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149 inode_add_bytes(inode, size);
150 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
167 if (compress_type != BTRFS_COMPRESS_NONE) {
170 while (compressed_size > 0) {
171 cpage = compressed_pages[i];
172 cur_size = min_t(unsigned long, compressed_size,
175 kaddr = kmap_atomic(cpage, KM_USER0);
176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
177 kunmap_atomic(kaddr, KM_USER0);
181 compressed_size -= cur_size;
183 btrfs_set_file_extent_compression(leaf, ei,
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
189 kaddr = kmap_atomic(page, KM_USER0);
190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
192 kunmap_atomic(kaddr, KM_USER0);
193 page_cache_release(page);
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
207 BTRFS_I(inode)->disk_i_size = inode->i_size;
208 btrfs_update_inode(trans, root, inode);
212 btrfs_free_path(path);
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
222 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
225 size_t compressed_size, int compress_type,
226 struct page **compressed_pages)
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
234 u64 data_len = inline_len;
238 data_len = compressed_size;
241 actual_end >= PAGE_CACHE_SIZE ||
242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
244 (actual_end & (root->sectorsize - 1)) == 0) ||
246 data_len > root->fs_info->max_inline) {
250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
254 if (isize > actual_end)
255 inline_len = min_t(u64, isize, actual_end);
256 ret = insert_inline_extent(trans, root, inode, start,
257 inline_len, compressed_size,
258 compress_type, compressed_pages);
260 btrfs_delalloc_release_metadata(inode, end + 1 - start);
261 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
265 struct async_extent {
270 unsigned long nr_pages;
272 struct list_head list;
277 struct btrfs_root *root;
278 struct page *locked_page;
281 struct list_head extents;
282 struct btrfs_work work;
285 static noinline int add_async_extent(struct async_cow *cow,
286 u64 start, u64 ram_size,
289 unsigned long nr_pages,
292 struct async_extent *async_extent;
294 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
295 BUG_ON(!async_extent);
296 async_extent->start = start;
297 async_extent->ram_size = ram_size;
298 async_extent->compressed_size = compressed_size;
299 async_extent->pages = pages;
300 async_extent->nr_pages = nr_pages;
301 async_extent->compress_type = compress_type;
302 list_add_tail(&async_extent->list, &cow->extents);
307 * we create compressed extents in two phases. The first
308 * phase compresses a range of pages that have already been
309 * locked (both pages and state bits are locked).
311 * This is done inside an ordered work queue, and the compression
312 * is spread across many cpus. The actual IO submission is step
313 * two, and the ordered work queue takes care of making sure that
314 * happens in the same order things were put onto the queue by
315 * writepages and friends.
317 * If this code finds it can't get good compression, it puts an
318 * entry onto the work queue to write the uncompressed bytes. This
319 * makes sure that both compressed inodes and uncompressed inodes
320 * are written in the same order that pdflush sent them down.
322 static noinline int compress_file_range(struct inode *inode,
323 struct page *locked_page,
325 struct async_cow *async_cow,
328 struct btrfs_root *root = BTRFS_I(inode)->root;
329 struct btrfs_trans_handle *trans;
331 u64 blocksize = root->sectorsize;
333 u64 isize = i_size_read(inode);
335 struct page **pages = NULL;
336 unsigned long nr_pages;
337 unsigned long nr_pages_ret = 0;
338 unsigned long total_compressed = 0;
339 unsigned long total_in = 0;
340 unsigned long max_compressed = 128 * 1024;
341 unsigned long max_uncompressed = 128 * 1024;
344 int compress_type = root->fs_info->compress_type;
346 /* if this is a small write inside eof, kick off a defragbot */
347 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
348 btrfs_add_inode_defrag(NULL, inode);
350 actual_end = min_t(u64, isize, end + 1);
353 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
354 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
357 * we don't want to send crud past the end of i_size through
358 * compression, that's just a waste of CPU time. So, if the
359 * end of the file is before the start of our current
360 * requested range of bytes, we bail out to the uncompressed
361 * cleanup code that can deal with all of this.
363 * It isn't really the fastest way to fix things, but this is a
364 * very uncommon corner.
366 if (actual_end <= start)
367 goto cleanup_and_bail_uncompressed;
369 total_compressed = actual_end - start;
371 /* we want to make sure that amount of ram required to uncompress
372 * an extent is reasonable, so we limit the total size in ram
373 * of a compressed extent to 128k. This is a crucial number
374 * because it also controls how easily we can spread reads across
375 * cpus for decompression.
377 * We also want to make sure the amount of IO required to do
378 * a random read is reasonably small, so we limit the size of
379 * a compressed extent to 128k.
381 total_compressed = min(total_compressed, max_uncompressed);
382 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
383 num_bytes = max(blocksize, num_bytes);
388 * we do compression for mount -o compress and when the
389 * inode has not been flagged as nocompress. This flag can
390 * change at any time if we discover bad compression ratios.
392 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
393 (btrfs_test_opt(root, COMPRESS) ||
394 (BTRFS_I(inode)->force_compress) ||
395 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
397 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
399 /* just bail out to the uncompressed code */
403 if (BTRFS_I(inode)->force_compress)
404 compress_type = BTRFS_I(inode)->force_compress;
406 ret = btrfs_compress_pages(compress_type,
407 inode->i_mapping, start,
408 total_compressed, pages,
409 nr_pages, &nr_pages_ret,
415 unsigned long offset = total_compressed &
416 (PAGE_CACHE_SIZE - 1);
417 struct page *page = pages[nr_pages_ret - 1];
420 /* zero the tail end of the last page, we might be
421 * sending it down to disk
424 kaddr = kmap_atomic(page, KM_USER0);
425 memset(kaddr + offset, 0,
426 PAGE_CACHE_SIZE - offset);
427 kunmap_atomic(kaddr, KM_USER0);
434 trans = btrfs_join_transaction(root);
435 BUG_ON(IS_ERR(trans));
436 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
438 /* lets try to make an inline extent */
439 if (ret || total_in < (actual_end - start)) {
440 /* we didn't compress the entire range, try
441 * to make an uncompressed inline extent.
443 ret = cow_file_range_inline(trans, root, inode,
444 start, end, 0, 0, NULL);
446 /* try making a compressed inline extent */
447 ret = cow_file_range_inline(trans, root, inode,
450 compress_type, pages);
454 * inline extent creation worked, we don't need
455 * to create any more async work items. Unlock
456 * and free up our temp pages.
458 extent_clear_unlock_delalloc(inode,
459 &BTRFS_I(inode)->io_tree,
461 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
462 EXTENT_CLEAR_DELALLOC |
463 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
465 btrfs_end_transaction(trans, root);
468 btrfs_end_transaction(trans, root);
473 * we aren't doing an inline extent round the compressed size
474 * up to a block size boundary so the allocator does sane
477 total_compressed = (total_compressed + blocksize - 1) &
481 * one last check to make sure the compression is really a
482 * win, compare the page count read with the blocks on disk
484 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
485 ~(PAGE_CACHE_SIZE - 1);
486 if (total_compressed >= total_in) {
489 num_bytes = total_in;
492 if (!will_compress && pages) {
494 * the compression code ran but failed to make things smaller,
495 * free any pages it allocated and our page pointer array
497 for (i = 0; i < nr_pages_ret; i++) {
498 WARN_ON(pages[i]->mapping);
499 page_cache_release(pages[i]);
503 total_compressed = 0;
506 /* flag the file so we don't compress in the future */
507 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
508 !(BTRFS_I(inode)->force_compress)) {
509 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
515 /* the async work queues will take care of doing actual
516 * allocation on disk for these compressed pages,
517 * and will submit them to the elevator.
519 add_async_extent(async_cow, start, num_bytes,
520 total_compressed, pages, nr_pages_ret,
523 if (start + num_bytes < end) {
530 cleanup_and_bail_uncompressed:
532 * No compression, but we still need to write the pages in
533 * the file we've been given so far. redirty the locked
534 * page if it corresponds to our extent and set things up
535 * for the async work queue to run cow_file_range to do
536 * the normal delalloc dance
538 if (page_offset(locked_page) >= start &&
539 page_offset(locked_page) <= end) {
540 __set_page_dirty_nobuffers(locked_page);
541 /* unlocked later on in the async handlers */
543 add_async_extent(async_cow, start, end - start + 1,
544 0, NULL, 0, BTRFS_COMPRESS_NONE);
552 for (i = 0; i < nr_pages_ret; i++) {
553 WARN_ON(pages[i]->mapping);
554 page_cache_release(pages[i]);
562 * phase two of compressed writeback. This is the ordered portion
563 * of the code, which only gets called in the order the work was
564 * queued. We walk all the async extents created by compress_file_range
565 * and send them down to the disk.
567 static noinline int submit_compressed_extents(struct inode *inode,
568 struct async_cow *async_cow)
570 struct async_extent *async_extent;
572 struct btrfs_trans_handle *trans;
573 struct btrfs_key ins;
574 struct extent_map *em;
575 struct btrfs_root *root = BTRFS_I(inode)->root;
576 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
577 struct extent_io_tree *io_tree;
580 if (list_empty(&async_cow->extents))
584 while (!list_empty(&async_cow->extents)) {
585 async_extent = list_entry(async_cow->extents.next,
586 struct async_extent, list);
587 list_del(&async_extent->list);
589 io_tree = &BTRFS_I(inode)->io_tree;
592 /* did the compression code fall back to uncompressed IO? */
593 if (!async_extent->pages) {
594 int page_started = 0;
595 unsigned long nr_written = 0;
597 lock_extent(io_tree, async_extent->start,
598 async_extent->start +
599 async_extent->ram_size - 1, GFP_NOFS);
601 /* allocate blocks */
602 ret = cow_file_range(inode, async_cow->locked_page,
604 async_extent->start +
605 async_extent->ram_size - 1,
606 &page_started, &nr_written, 0);
609 * if page_started, cow_file_range inserted an
610 * inline extent and took care of all the unlocking
611 * and IO for us. Otherwise, we need to submit
612 * all those pages down to the drive.
614 if (!page_started && !ret)
615 extent_write_locked_range(io_tree,
616 inode, async_extent->start,
617 async_extent->start +
618 async_extent->ram_size - 1,
626 lock_extent(io_tree, async_extent->start,
627 async_extent->start + async_extent->ram_size - 1,
630 trans = btrfs_join_transaction(root);
631 BUG_ON(IS_ERR(trans));
632 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
633 ret = btrfs_reserve_extent(trans, root,
634 async_extent->compressed_size,
635 async_extent->compressed_size,
638 btrfs_end_transaction(trans, root);
642 for (i = 0; i < async_extent->nr_pages; i++) {
643 WARN_ON(async_extent->pages[i]->mapping);
644 page_cache_release(async_extent->pages[i]);
646 kfree(async_extent->pages);
647 async_extent->nr_pages = 0;
648 async_extent->pages = NULL;
649 unlock_extent(io_tree, async_extent->start,
650 async_extent->start +
651 async_extent->ram_size - 1, GFP_NOFS);
656 * here we're doing allocation and writeback of the
659 btrfs_drop_extent_cache(inode, async_extent->start,
660 async_extent->start +
661 async_extent->ram_size - 1, 0);
663 em = alloc_extent_map();
665 em->start = async_extent->start;
666 em->len = async_extent->ram_size;
667 em->orig_start = em->start;
669 em->block_start = ins.objectid;
670 em->block_len = ins.offset;
671 em->bdev = root->fs_info->fs_devices->latest_bdev;
672 em->compress_type = async_extent->compress_type;
673 set_bit(EXTENT_FLAG_PINNED, &em->flags);
674 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
677 write_lock(&em_tree->lock);
678 ret = add_extent_mapping(em_tree, em);
679 write_unlock(&em_tree->lock);
680 if (ret != -EEXIST) {
684 btrfs_drop_extent_cache(inode, async_extent->start,
685 async_extent->start +
686 async_extent->ram_size - 1, 0);
689 ret = btrfs_add_ordered_extent_compress(inode,
692 async_extent->ram_size,
694 BTRFS_ORDERED_COMPRESSED,
695 async_extent->compress_type);
699 * clear dirty, set writeback and unlock the pages.
701 extent_clear_unlock_delalloc(inode,
702 &BTRFS_I(inode)->io_tree,
704 async_extent->start +
705 async_extent->ram_size - 1,
706 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
707 EXTENT_CLEAR_UNLOCK |
708 EXTENT_CLEAR_DELALLOC |
709 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
711 ret = btrfs_submit_compressed_write(inode,
713 async_extent->ram_size,
715 ins.offset, async_extent->pages,
716 async_extent->nr_pages);
719 alloc_hint = ins.objectid + ins.offset;
727 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
730 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
731 struct extent_map *em;
734 read_lock(&em_tree->lock);
735 em = search_extent_mapping(em_tree, start, num_bytes);
738 * if block start isn't an actual block number then find the
739 * first block in this inode and use that as a hint. If that
740 * block is also bogus then just don't worry about it.
742 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
744 em = search_extent_mapping(em_tree, 0, 0);
745 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
746 alloc_hint = em->block_start;
750 alloc_hint = em->block_start;
754 read_unlock(&em_tree->lock);
760 * when extent_io.c finds a delayed allocation range in the file,
761 * the call backs end up in this code. The basic idea is to
762 * allocate extents on disk for the range, and create ordered data structs
763 * in ram to track those extents.
765 * locked_page is the page that writepage had locked already. We use
766 * it to make sure we don't do extra locks or unlocks.
768 * *page_started is set to one if we unlock locked_page and do everything
769 * required to start IO on it. It may be clean and already done with
772 static noinline int cow_file_range(struct inode *inode,
773 struct page *locked_page,
774 u64 start, u64 end, int *page_started,
775 unsigned long *nr_written,
778 struct btrfs_root *root = BTRFS_I(inode)->root;
779 struct btrfs_trans_handle *trans;
782 unsigned long ram_size;
785 u64 blocksize = root->sectorsize;
786 struct btrfs_key ins;
787 struct extent_map *em;
788 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
791 BUG_ON(btrfs_is_free_space_inode(root, inode));
792 trans = btrfs_join_transaction(root);
793 BUG_ON(IS_ERR(trans));
794 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
796 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
797 num_bytes = max(blocksize, num_bytes);
798 disk_num_bytes = num_bytes;
801 /* if this is a small write inside eof, kick off defrag */
802 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
803 btrfs_add_inode_defrag(trans, inode);
806 /* lets try to make an inline extent */
807 ret = cow_file_range_inline(trans, root, inode,
808 start, end, 0, 0, NULL);
810 extent_clear_unlock_delalloc(inode,
811 &BTRFS_I(inode)->io_tree,
813 EXTENT_CLEAR_UNLOCK_PAGE |
814 EXTENT_CLEAR_UNLOCK |
815 EXTENT_CLEAR_DELALLOC |
817 EXTENT_SET_WRITEBACK |
818 EXTENT_END_WRITEBACK);
820 *nr_written = *nr_written +
821 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
828 BUG_ON(disk_num_bytes >
829 btrfs_super_total_bytes(root->fs_info->super_copy));
831 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
832 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
834 while (disk_num_bytes > 0) {
837 cur_alloc_size = disk_num_bytes;
838 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
839 root->sectorsize, 0, alloc_hint,
843 em = alloc_extent_map();
846 em->orig_start = em->start;
847 ram_size = ins.offset;
848 em->len = ins.offset;
850 em->block_start = ins.objectid;
851 em->block_len = ins.offset;
852 em->bdev = root->fs_info->fs_devices->latest_bdev;
853 set_bit(EXTENT_FLAG_PINNED, &em->flags);
856 write_lock(&em_tree->lock);
857 ret = add_extent_mapping(em_tree, em);
858 write_unlock(&em_tree->lock);
859 if (ret != -EEXIST) {
863 btrfs_drop_extent_cache(inode, start,
864 start + ram_size - 1, 0);
867 cur_alloc_size = ins.offset;
868 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
869 ram_size, cur_alloc_size, 0);
872 if (root->root_key.objectid ==
873 BTRFS_DATA_RELOC_TREE_OBJECTID) {
874 ret = btrfs_reloc_clone_csums(inode, start,
879 if (disk_num_bytes < cur_alloc_size)
882 /* we're not doing compressed IO, don't unlock the first
883 * page (which the caller expects to stay locked), don't
884 * clear any dirty bits and don't set any writeback bits
886 * Do set the Private2 bit so we know this page was properly
887 * setup for writepage
889 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
890 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
893 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
894 start, start + ram_size - 1,
896 disk_num_bytes -= cur_alloc_size;
897 num_bytes -= cur_alloc_size;
898 alloc_hint = ins.objectid + ins.offset;
899 start += cur_alloc_size;
903 btrfs_end_transaction(trans, root);
909 * work queue call back to started compression on a file and pages
911 static noinline void async_cow_start(struct btrfs_work *work)
913 struct async_cow *async_cow;
915 async_cow = container_of(work, struct async_cow, work);
917 compress_file_range(async_cow->inode, async_cow->locked_page,
918 async_cow->start, async_cow->end, async_cow,
921 async_cow->inode = NULL;
925 * work queue call back to submit previously compressed pages
927 static noinline void async_cow_submit(struct btrfs_work *work)
929 struct async_cow *async_cow;
930 struct btrfs_root *root;
931 unsigned long nr_pages;
933 async_cow = container_of(work, struct async_cow, work);
935 root = async_cow->root;
936 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
939 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
941 if (atomic_read(&root->fs_info->async_delalloc_pages) <
943 waitqueue_active(&root->fs_info->async_submit_wait))
944 wake_up(&root->fs_info->async_submit_wait);
946 if (async_cow->inode)
947 submit_compressed_extents(async_cow->inode, async_cow);
950 static noinline void async_cow_free(struct btrfs_work *work)
952 struct async_cow *async_cow;
953 async_cow = container_of(work, struct async_cow, work);
957 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
958 u64 start, u64 end, int *page_started,
959 unsigned long *nr_written)
961 struct async_cow *async_cow;
962 struct btrfs_root *root = BTRFS_I(inode)->root;
963 unsigned long nr_pages;
965 int limit = 10 * 1024 * 1042;
967 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
968 1, 0, NULL, GFP_NOFS);
969 while (start < end) {
970 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
972 async_cow->inode = inode;
973 async_cow->root = root;
974 async_cow->locked_page = locked_page;
975 async_cow->start = start;
977 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
980 cur_end = min(end, start + 512 * 1024 - 1);
982 async_cow->end = cur_end;
983 INIT_LIST_HEAD(&async_cow->extents);
985 async_cow->work.func = async_cow_start;
986 async_cow->work.ordered_func = async_cow_submit;
987 async_cow->work.ordered_free = async_cow_free;
988 async_cow->work.flags = 0;
990 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
992 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
994 btrfs_queue_worker(&root->fs_info->delalloc_workers,
997 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
998 wait_event(root->fs_info->async_submit_wait,
999 (atomic_read(&root->fs_info->async_delalloc_pages) <
1003 while (atomic_read(&root->fs_info->async_submit_draining) &&
1004 atomic_read(&root->fs_info->async_delalloc_pages)) {
1005 wait_event(root->fs_info->async_submit_wait,
1006 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1010 *nr_written += nr_pages;
1011 start = cur_end + 1;
1017 static noinline int csum_exist_in_range(struct btrfs_root *root,
1018 u64 bytenr, u64 num_bytes)
1021 struct btrfs_ordered_sum *sums;
1024 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1025 bytenr + num_bytes - 1, &list, 0);
1026 if (ret == 0 && list_empty(&list))
1029 while (!list_empty(&list)) {
1030 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1031 list_del(&sums->list);
1038 * when nowcow writeback call back. This checks for snapshots or COW copies
1039 * of the extents that exist in the file, and COWs the file as required.
1041 * If no cow copies or snapshots exist, we write directly to the existing
1044 static noinline int run_delalloc_nocow(struct inode *inode,
1045 struct page *locked_page,
1046 u64 start, u64 end, int *page_started, int force,
1047 unsigned long *nr_written)
1049 struct btrfs_root *root = BTRFS_I(inode)->root;
1050 struct btrfs_trans_handle *trans;
1051 struct extent_buffer *leaf;
1052 struct btrfs_path *path;
1053 struct btrfs_file_extent_item *fi;
1054 struct btrfs_key found_key;
1067 u64 ino = btrfs_ino(inode);
1069 path = btrfs_alloc_path();
1073 nolock = btrfs_is_free_space_inode(root, inode);
1076 trans = btrfs_join_transaction_nolock(root);
1078 trans = btrfs_join_transaction(root);
1080 BUG_ON(IS_ERR(trans));
1081 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1083 cow_start = (u64)-1;
1086 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1089 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1090 leaf = path->nodes[0];
1091 btrfs_item_key_to_cpu(leaf, &found_key,
1092 path->slots[0] - 1);
1093 if (found_key.objectid == ino &&
1094 found_key.type == BTRFS_EXTENT_DATA_KEY)
1099 leaf = path->nodes[0];
1100 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1101 ret = btrfs_next_leaf(root, path);
1106 leaf = path->nodes[0];
1112 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1114 if (found_key.objectid > ino ||
1115 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1116 found_key.offset > end)
1119 if (found_key.offset > cur_offset) {
1120 extent_end = found_key.offset;
1125 fi = btrfs_item_ptr(leaf, path->slots[0],
1126 struct btrfs_file_extent_item);
1127 extent_type = btrfs_file_extent_type(leaf, fi);
1129 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1130 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1131 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1132 extent_offset = btrfs_file_extent_offset(leaf, fi);
1133 extent_end = found_key.offset +
1134 btrfs_file_extent_num_bytes(leaf, fi);
1135 if (extent_end <= start) {
1139 if (disk_bytenr == 0)
1141 if (btrfs_file_extent_compression(leaf, fi) ||
1142 btrfs_file_extent_encryption(leaf, fi) ||
1143 btrfs_file_extent_other_encoding(leaf, fi))
1145 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1147 if (btrfs_extent_readonly(root, disk_bytenr))
1149 if (btrfs_cross_ref_exist(trans, root, ino,
1151 extent_offset, disk_bytenr))
1153 disk_bytenr += extent_offset;
1154 disk_bytenr += cur_offset - found_key.offset;
1155 num_bytes = min(end + 1, extent_end) - cur_offset;
1157 * force cow if csum exists in the range.
1158 * this ensure that csum for a given extent are
1159 * either valid or do not exist.
1161 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1164 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1165 extent_end = found_key.offset +
1166 btrfs_file_extent_inline_len(leaf, fi);
1167 extent_end = ALIGN(extent_end, root->sectorsize);
1172 if (extent_end <= start) {
1177 if (cow_start == (u64)-1)
1178 cow_start = cur_offset;
1179 cur_offset = extent_end;
1180 if (cur_offset > end)
1186 btrfs_release_path(path);
1187 if (cow_start != (u64)-1) {
1188 ret = cow_file_range(inode, locked_page, cow_start,
1189 found_key.offset - 1, page_started,
1192 cow_start = (u64)-1;
1195 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1196 struct extent_map *em;
1197 struct extent_map_tree *em_tree;
1198 em_tree = &BTRFS_I(inode)->extent_tree;
1199 em = alloc_extent_map();
1201 em->start = cur_offset;
1202 em->orig_start = em->start;
1203 em->len = num_bytes;
1204 em->block_len = num_bytes;
1205 em->block_start = disk_bytenr;
1206 em->bdev = root->fs_info->fs_devices->latest_bdev;
1207 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1209 write_lock(&em_tree->lock);
1210 ret = add_extent_mapping(em_tree, em);
1211 write_unlock(&em_tree->lock);
1212 if (ret != -EEXIST) {
1213 free_extent_map(em);
1216 btrfs_drop_extent_cache(inode, em->start,
1217 em->start + em->len - 1, 0);
1219 type = BTRFS_ORDERED_PREALLOC;
1221 type = BTRFS_ORDERED_NOCOW;
1224 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1225 num_bytes, num_bytes, type);
1228 if (root->root_key.objectid ==
1229 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1230 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1235 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1236 cur_offset, cur_offset + num_bytes - 1,
1237 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1238 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1239 EXTENT_SET_PRIVATE2);
1240 cur_offset = extent_end;
1241 if (cur_offset > end)
1244 btrfs_release_path(path);
1246 if (cur_offset <= end && cow_start == (u64)-1)
1247 cow_start = cur_offset;
1248 if (cow_start != (u64)-1) {
1249 ret = cow_file_range(inode, locked_page, cow_start, end,
1250 page_started, nr_written, 1);
1255 ret = btrfs_end_transaction_nolock(trans, root);
1258 ret = btrfs_end_transaction(trans, root);
1261 btrfs_free_path(path);
1266 * extent_io.c call back to do delayed allocation processing
1268 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1269 u64 start, u64 end, int *page_started,
1270 unsigned long *nr_written)
1273 struct btrfs_root *root = BTRFS_I(inode)->root;
1275 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1276 ret = run_delalloc_nocow(inode, locked_page, start, end,
1277 page_started, 1, nr_written);
1278 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1279 ret = run_delalloc_nocow(inode, locked_page, start, end,
1280 page_started, 0, nr_written);
1281 else if (!btrfs_test_opt(root, COMPRESS) &&
1282 !(BTRFS_I(inode)->force_compress) &&
1283 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1284 ret = cow_file_range(inode, locked_page, start, end,
1285 page_started, nr_written, 1);
1287 ret = cow_file_range_async(inode, locked_page, start, end,
1288 page_started, nr_written);
1292 static void btrfs_split_extent_hook(struct inode *inode,
1293 struct extent_state *orig, u64 split)
1295 /* not delalloc, ignore it */
1296 if (!(orig->state & EXTENT_DELALLOC))
1299 spin_lock(&BTRFS_I(inode)->lock);
1300 BTRFS_I(inode)->outstanding_extents++;
1301 spin_unlock(&BTRFS_I(inode)->lock);
1305 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1306 * extents so we can keep track of new extents that are just merged onto old
1307 * extents, such as when we are doing sequential writes, so we can properly
1308 * account for the metadata space we'll need.
1310 static void btrfs_merge_extent_hook(struct inode *inode,
1311 struct extent_state *new,
1312 struct extent_state *other)
1314 /* not delalloc, ignore it */
1315 if (!(other->state & EXTENT_DELALLOC))
1318 spin_lock(&BTRFS_I(inode)->lock);
1319 BTRFS_I(inode)->outstanding_extents--;
1320 spin_unlock(&BTRFS_I(inode)->lock);
1324 * extent_io.c set_bit_hook, used to track delayed allocation
1325 * bytes in this file, and to maintain the list of inodes that
1326 * have pending delalloc work to be done.
1328 static void btrfs_set_bit_hook(struct inode *inode,
1329 struct extent_state *state, int *bits)
1333 * set_bit and clear bit hooks normally require _irqsave/restore
1334 * but in this case, we are only testing for the DELALLOC
1335 * bit, which is only set or cleared with irqs on
1337 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1338 struct btrfs_root *root = BTRFS_I(inode)->root;
1339 u64 len = state->end + 1 - state->start;
1340 bool do_list = !btrfs_is_free_space_inode(root, inode);
1342 if (*bits & EXTENT_FIRST_DELALLOC) {
1343 *bits &= ~EXTENT_FIRST_DELALLOC;
1345 spin_lock(&BTRFS_I(inode)->lock);
1346 BTRFS_I(inode)->outstanding_extents++;
1347 spin_unlock(&BTRFS_I(inode)->lock);
1350 spin_lock(&root->fs_info->delalloc_lock);
1351 BTRFS_I(inode)->delalloc_bytes += len;
1352 root->fs_info->delalloc_bytes += len;
1353 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1354 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1355 &root->fs_info->delalloc_inodes);
1357 spin_unlock(&root->fs_info->delalloc_lock);
1362 * extent_io.c clear_bit_hook, see set_bit_hook for why
1364 static void btrfs_clear_bit_hook(struct inode *inode,
1365 struct extent_state *state, int *bits)
1368 * set_bit and clear bit hooks normally require _irqsave/restore
1369 * but in this case, we are only testing for the DELALLOC
1370 * bit, which is only set or cleared with irqs on
1372 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1373 struct btrfs_root *root = BTRFS_I(inode)->root;
1374 u64 len = state->end + 1 - state->start;
1375 bool do_list = !btrfs_is_free_space_inode(root, inode);
1377 if (*bits & EXTENT_FIRST_DELALLOC) {
1378 *bits &= ~EXTENT_FIRST_DELALLOC;
1379 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1380 spin_lock(&BTRFS_I(inode)->lock);
1381 BTRFS_I(inode)->outstanding_extents--;
1382 spin_unlock(&BTRFS_I(inode)->lock);
1385 if (*bits & EXTENT_DO_ACCOUNTING)
1386 btrfs_delalloc_release_metadata(inode, len);
1388 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1390 btrfs_free_reserved_data_space(inode, len);
1392 spin_lock(&root->fs_info->delalloc_lock);
1393 root->fs_info->delalloc_bytes -= len;
1394 BTRFS_I(inode)->delalloc_bytes -= len;
1396 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1397 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1398 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1400 spin_unlock(&root->fs_info->delalloc_lock);
1405 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1406 * we don't create bios that span stripes or chunks
1408 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1409 size_t size, struct bio *bio,
1410 unsigned long bio_flags)
1412 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1413 struct btrfs_mapping_tree *map_tree;
1414 u64 logical = (u64)bio->bi_sector << 9;
1419 if (bio_flags & EXTENT_BIO_COMPRESSED)
1422 length = bio->bi_size;
1423 map_tree = &root->fs_info->mapping_tree;
1424 map_length = length;
1425 ret = btrfs_map_block(map_tree, READ, logical,
1426 &map_length, NULL, 0);
1428 if (map_length < length + size)
1434 * in order to insert checksums into the metadata in large chunks,
1435 * we wait until bio submission time. All the pages in the bio are
1436 * checksummed and sums are attached onto the ordered extent record.
1438 * At IO completion time the cums attached on the ordered extent record
1439 * are inserted into the btree
1441 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1442 struct bio *bio, int mirror_num,
1443 unsigned long bio_flags,
1446 struct btrfs_root *root = BTRFS_I(inode)->root;
1449 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1455 * in order to insert checksums into the metadata in large chunks,
1456 * we wait until bio submission time. All the pages in the bio are
1457 * checksummed and sums are attached onto the ordered extent record.
1459 * At IO completion time the cums attached on the ordered extent record
1460 * are inserted into the btree
1462 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1463 int mirror_num, unsigned long bio_flags,
1466 struct btrfs_root *root = BTRFS_I(inode)->root;
1467 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1471 * extent_io.c submission hook. This does the right thing for csum calculation
1472 * on write, or reading the csums from the tree before a read
1474 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1475 int mirror_num, unsigned long bio_flags,
1478 struct btrfs_root *root = BTRFS_I(inode)->root;
1482 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1484 if (btrfs_is_free_space_inode(root, inode))
1485 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1487 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1490 if (!(rw & REQ_WRITE)) {
1491 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1492 return btrfs_submit_compressed_read(inode, bio,
1493 mirror_num, bio_flags);
1494 } else if (!skip_sum) {
1495 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1500 } else if (!skip_sum) {
1501 /* csum items have already been cloned */
1502 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1504 /* we're doing a write, do the async checksumming */
1505 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1506 inode, rw, bio, mirror_num,
1507 bio_flags, bio_offset,
1508 __btrfs_submit_bio_start,
1509 __btrfs_submit_bio_done);
1513 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1517 * given a list of ordered sums record them in the inode. This happens
1518 * at IO completion time based on sums calculated at bio submission time.
1520 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1521 struct inode *inode, u64 file_offset,
1522 struct list_head *list)
1524 struct btrfs_ordered_sum *sum;
1526 list_for_each_entry(sum, list, list) {
1527 btrfs_csum_file_blocks(trans,
1528 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1533 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1534 struct extent_state **cached_state)
1536 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1538 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1539 cached_state, GFP_NOFS);
1542 /* see btrfs_writepage_start_hook for details on why this is required */
1543 struct btrfs_writepage_fixup {
1545 struct btrfs_work work;
1548 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1550 struct btrfs_writepage_fixup *fixup;
1551 struct btrfs_ordered_extent *ordered;
1552 struct extent_state *cached_state = NULL;
1554 struct inode *inode;
1558 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1562 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1563 ClearPageChecked(page);
1567 inode = page->mapping->host;
1568 page_start = page_offset(page);
1569 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1571 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1572 &cached_state, GFP_NOFS);
1574 /* already ordered? We're done */
1575 if (PagePrivate2(page))
1578 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1580 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1581 page_end, &cached_state, GFP_NOFS);
1583 btrfs_start_ordered_extent(inode, ordered, 1);
1588 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1589 ClearPageChecked(page);
1591 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1592 &cached_state, GFP_NOFS);
1595 page_cache_release(page);
1600 * There are a few paths in the higher layers of the kernel that directly
1601 * set the page dirty bit without asking the filesystem if it is a
1602 * good idea. This causes problems because we want to make sure COW
1603 * properly happens and the data=ordered rules are followed.
1605 * In our case any range that doesn't have the ORDERED bit set
1606 * hasn't been properly setup for IO. We kick off an async process
1607 * to fix it up. The async helper will wait for ordered extents, set
1608 * the delalloc bit and make it safe to write the page.
1610 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1612 struct inode *inode = page->mapping->host;
1613 struct btrfs_writepage_fixup *fixup;
1614 struct btrfs_root *root = BTRFS_I(inode)->root;
1616 /* this page is properly in the ordered list */
1617 if (TestClearPagePrivate2(page))
1620 if (PageChecked(page))
1623 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1627 SetPageChecked(page);
1628 page_cache_get(page);
1629 fixup->work.func = btrfs_writepage_fixup_worker;
1631 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1635 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1636 struct inode *inode, u64 file_pos,
1637 u64 disk_bytenr, u64 disk_num_bytes,
1638 u64 num_bytes, u64 ram_bytes,
1639 u8 compression, u8 encryption,
1640 u16 other_encoding, int extent_type)
1642 struct btrfs_root *root = BTRFS_I(inode)->root;
1643 struct btrfs_file_extent_item *fi;
1644 struct btrfs_path *path;
1645 struct extent_buffer *leaf;
1646 struct btrfs_key ins;
1650 path = btrfs_alloc_path();
1654 path->leave_spinning = 1;
1657 * we may be replacing one extent in the tree with another.
1658 * The new extent is pinned in the extent map, and we don't want
1659 * to drop it from the cache until it is completely in the btree.
1661 * So, tell btrfs_drop_extents to leave this extent in the cache.
1662 * the caller is expected to unpin it and allow it to be merged
1665 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1669 ins.objectid = btrfs_ino(inode);
1670 ins.offset = file_pos;
1671 ins.type = BTRFS_EXTENT_DATA_KEY;
1672 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1674 leaf = path->nodes[0];
1675 fi = btrfs_item_ptr(leaf, path->slots[0],
1676 struct btrfs_file_extent_item);
1677 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1678 btrfs_set_file_extent_type(leaf, fi, extent_type);
1679 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1680 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1681 btrfs_set_file_extent_offset(leaf, fi, 0);
1682 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1683 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1684 btrfs_set_file_extent_compression(leaf, fi, compression);
1685 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1686 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1688 btrfs_unlock_up_safe(path, 1);
1689 btrfs_set_lock_blocking(leaf);
1691 btrfs_mark_buffer_dirty(leaf);
1693 inode_add_bytes(inode, num_bytes);
1695 ins.objectid = disk_bytenr;
1696 ins.offset = disk_num_bytes;
1697 ins.type = BTRFS_EXTENT_ITEM_KEY;
1698 ret = btrfs_alloc_reserved_file_extent(trans, root,
1699 root->root_key.objectid,
1700 btrfs_ino(inode), file_pos, &ins);
1702 btrfs_free_path(path);
1708 * helper function for btrfs_finish_ordered_io, this
1709 * just reads in some of the csum leaves to prime them into ram
1710 * before we start the transaction. It limits the amount of btree
1711 * reads required while inside the transaction.
1713 /* as ordered data IO finishes, this gets called so we can finish
1714 * an ordered extent if the range of bytes in the file it covers are
1717 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1719 struct btrfs_root *root = BTRFS_I(inode)->root;
1720 struct btrfs_trans_handle *trans = NULL;
1721 struct btrfs_ordered_extent *ordered_extent = NULL;
1722 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1723 struct extent_state *cached_state = NULL;
1724 int compress_type = 0;
1728 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1732 BUG_ON(!ordered_extent);
1734 nolock = btrfs_is_free_space_inode(root, inode);
1736 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1737 BUG_ON(!list_empty(&ordered_extent->list));
1738 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1741 trans = btrfs_join_transaction_nolock(root);
1743 trans = btrfs_join_transaction(root);
1744 BUG_ON(IS_ERR(trans));
1745 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1746 ret = btrfs_update_inode_fallback(trans, root, inode);
1752 lock_extent_bits(io_tree, ordered_extent->file_offset,
1753 ordered_extent->file_offset + ordered_extent->len - 1,
1754 0, &cached_state, GFP_NOFS);
1757 trans = btrfs_join_transaction_nolock(root);
1759 trans = btrfs_join_transaction(root);
1760 BUG_ON(IS_ERR(trans));
1761 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1763 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1764 compress_type = ordered_extent->compress_type;
1765 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1766 BUG_ON(compress_type);
1767 ret = btrfs_mark_extent_written(trans, inode,
1768 ordered_extent->file_offset,
1769 ordered_extent->file_offset +
1770 ordered_extent->len);
1773 BUG_ON(root == root->fs_info->tree_root);
1774 ret = insert_reserved_file_extent(trans, inode,
1775 ordered_extent->file_offset,
1776 ordered_extent->start,
1777 ordered_extent->disk_len,
1778 ordered_extent->len,
1779 ordered_extent->len,
1780 compress_type, 0, 0,
1781 BTRFS_FILE_EXTENT_REG);
1782 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1783 ordered_extent->file_offset,
1784 ordered_extent->len);
1787 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1788 ordered_extent->file_offset +
1789 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1791 add_pending_csums(trans, inode, ordered_extent->file_offset,
1792 &ordered_extent->list);
1794 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1795 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1796 ret = btrfs_update_inode_fallback(trans, root, inode);
1801 if (root != root->fs_info->tree_root)
1802 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1805 btrfs_end_transaction_nolock(trans, root);
1807 btrfs_end_transaction(trans, root);
1811 btrfs_put_ordered_extent(ordered_extent);
1812 /* once for the tree */
1813 btrfs_put_ordered_extent(ordered_extent);
1818 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1819 struct extent_state *state, int uptodate)
1821 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1823 ClearPagePrivate2(page);
1824 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1828 * when reads are done, we need to check csums to verify the data is correct
1829 * if there's a match, we allow the bio to finish. If not, the code in
1830 * extent_io.c will try to find good copies for us.
1832 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1833 struct extent_state *state)
1835 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1836 struct inode *inode = page->mapping->host;
1837 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1839 u64 private = ~(u32)0;
1841 struct btrfs_root *root = BTRFS_I(inode)->root;
1844 if (PageChecked(page)) {
1845 ClearPageChecked(page);
1849 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1852 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1854 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1859 if (state && state->start == start) {
1860 private = state->private;
1863 ret = get_state_private(io_tree, start, &private);
1865 kaddr = kmap_atomic(page, KM_USER0);
1869 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1870 btrfs_csum_final(csum, (char *)&csum);
1871 if (csum != private)
1874 kunmap_atomic(kaddr, KM_USER0);
1879 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
1881 (unsigned long long)btrfs_ino(page->mapping->host),
1882 (unsigned long long)start, csum,
1883 (unsigned long long)private);
1884 memset(kaddr + offset, 1, end - start + 1);
1885 flush_dcache_page(page);
1886 kunmap_atomic(kaddr, KM_USER0);
1892 struct delayed_iput {
1893 struct list_head list;
1894 struct inode *inode;
1897 void btrfs_add_delayed_iput(struct inode *inode)
1899 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1900 struct delayed_iput *delayed;
1902 if (atomic_add_unless(&inode->i_count, -1, 1))
1905 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1906 delayed->inode = inode;
1908 spin_lock(&fs_info->delayed_iput_lock);
1909 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1910 spin_unlock(&fs_info->delayed_iput_lock);
1913 void btrfs_run_delayed_iputs(struct btrfs_root *root)
1916 struct btrfs_fs_info *fs_info = root->fs_info;
1917 struct delayed_iput *delayed;
1920 spin_lock(&fs_info->delayed_iput_lock);
1921 empty = list_empty(&fs_info->delayed_iputs);
1922 spin_unlock(&fs_info->delayed_iput_lock);
1926 down_read(&root->fs_info->cleanup_work_sem);
1927 spin_lock(&fs_info->delayed_iput_lock);
1928 list_splice_init(&fs_info->delayed_iputs, &list);
1929 spin_unlock(&fs_info->delayed_iput_lock);
1931 while (!list_empty(&list)) {
1932 delayed = list_entry(list.next, struct delayed_iput, list);
1933 list_del(&delayed->list);
1934 iput(delayed->inode);
1937 up_read(&root->fs_info->cleanup_work_sem);
1940 enum btrfs_orphan_cleanup_state {
1941 ORPHAN_CLEANUP_STARTED = 1,
1942 ORPHAN_CLEANUP_DONE = 2,
1946 * This is called in transaction commmit time. If there are no orphan
1947 * files in the subvolume, it removes orphan item and frees block_rsv
1950 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
1951 struct btrfs_root *root)
1955 if (!list_empty(&root->orphan_list) ||
1956 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
1959 if (root->orphan_item_inserted &&
1960 btrfs_root_refs(&root->root_item) > 0) {
1961 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
1962 root->root_key.objectid);
1964 root->orphan_item_inserted = 0;
1967 if (root->orphan_block_rsv) {
1968 WARN_ON(root->orphan_block_rsv->size > 0);
1969 btrfs_free_block_rsv(root, root->orphan_block_rsv);
1970 root->orphan_block_rsv = NULL;
1975 * This creates an orphan entry for the given inode in case something goes
1976 * wrong in the middle of an unlink/truncate.
1978 * NOTE: caller of this function should reserve 5 units of metadata for
1981 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1983 struct btrfs_root *root = BTRFS_I(inode)->root;
1984 struct btrfs_block_rsv *block_rsv = NULL;
1989 if (!root->orphan_block_rsv) {
1990 block_rsv = btrfs_alloc_block_rsv(root);
1995 spin_lock(&root->orphan_lock);
1996 if (!root->orphan_block_rsv) {
1997 root->orphan_block_rsv = block_rsv;
1998 } else if (block_rsv) {
1999 btrfs_free_block_rsv(root, block_rsv);
2003 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2004 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2007 * For proper ENOSPC handling, we should do orphan
2008 * cleanup when mounting. But this introduces backward
2009 * compatibility issue.
2011 if (!xchg(&root->orphan_item_inserted, 1))
2019 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2020 BTRFS_I(inode)->orphan_meta_reserved = 1;
2023 spin_unlock(&root->orphan_lock);
2025 /* grab metadata reservation from transaction handle */
2027 ret = btrfs_orphan_reserve_metadata(trans, inode);
2031 /* insert an orphan item to track this unlinked/truncated file */
2033 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2037 /* insert an orphan item to track subvolume contains orphan files */
2039 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2040 root->root_key.objectid);
2047 * We have done the truncate/delete so we can go ahead and remove the orphan
2048 * item for this particular inode.
2050 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2052 struct btrfs_root *root = BTRFS_I(inode)->root;
2053 int delete_item = 0;
2054 int release_rsv = 0;
2057 spin_lock(&root->orphan_lock);
2058 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2059 list_del_init(&BTRFS_I(inode)->i_orphan);
2063 if (BTRFS_I(inode)->orphan_meta_reserved) {
2064 BTRFS_I(inode)->orphan_meta_reserved = 0;
2067 spin_unlock(&root->orphan_lock);
2069 if (trans && delete_item) {
2070 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2075 btrfs_orphan_release_metadata(inode);
2081 * this cleans up any orphans that may be left on the list from the last use
2084 int btrfs_orphan_cleanup(struct btrfs_root *root)
2086 struct btrfs_path *path;
2087 struct extent_buffer *leaf;
2088 struct btrfs_key key, found_key;
2089 struct btrfs_trans_handle *trans;
2090 struct inode *inode;
2091 u64 last_objectid = 0;
2092 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2094 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2097 path = btrfs_alloc_path();
2104 key.objectid = BTRFS_ORPHAN_OBJECTID;
2105 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2106 key.offset = (u64)-1;
2109 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2114 * if ret == 0 means we found what we were searching for, which
2115 * is weird, but possible, so only screw with path if we didn't
2116 * find the key and see if we have stuff that matches
2120 if (path->slots[0] == 0)
2125 /* pull out the item */
2126 leaf = path->nodes[0];
2127 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2129 /* make sure the item matches what we want */
2130 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2132 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2135 /* release the path since we're done with it */
2136 btrfs_release_path(path);
2139 * this is where we are basically btrfs_lookup, without the
2140 * crossing root thing. we store the inode number in the
2141 * offset of the orphan item.
2144 if (found_key.offset == last_objectid) {
2145 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2146 "stopping orphan cleanup\n");
2151 last_objectid = found_key.offset;
2153 found_key.objectid = found_key.offset;
2154 found_key.type = BTRFS_INODE_ITEM_KEY;
2155 found_key.offset = 0;
2156 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2157 ret = PTR_RET(inode);
2158 if (ret && ret != -ESTALE)
2162 * Inode is already gone but the orphan item is still there,
2163 * kill the orphan item.
2165 if (ret == -ESTALE) {
2166 trans = btrfs_start_transaction(root, 1);
2167 if (IS_ERR(trans)) {
2168 ret = PTR_ERR(trans);
2171 ret = btrfs_del_orphan_item(trans, root,
2172 found_key.objectid);
2174 btrfs_end_transaction(trans, root);
2179 * add this inode to the orphan list so btrfs_orphan_del does
2180 * the proper thing when we hit it
2182 spin_lock(&root->orphan_lock);
2183 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2184 spin_unlock(&root->orphan_lock);
2186 /* if we have links, this was a truncate, lets do that */
2187 if (inode->i_nlink) {
2188 if (!S_ISREG(inode->i_mode)) {
2194 ret = btrfs_truncate(inode);
2199 /* this will do delete_inode and everything for us */
2204 /* release the path since we're done with it */
2205 btrfs_release_path(path);
2207 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2209 if (root->orphan_block_rsv)
2210 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2213 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2214 trans = btrfs_join_transaction(root);
2216 btrfs_end_transaction(trans, root);
2220 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2222 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2226 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2227 btrfs_free_path(path);
2232 * very simple check to peek ahead in the leaf looking for xattrs. If we
2233 * don't find any xattrs, we know there can't be any acls.
2235 * slot is the slot the inode is in, objectid is the objectid of the inode
2237 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2238 int slot, u64 objectid)
2240 u32 nritems = btrfs_header_nritems(leaf);
2241 struct btrfs_key found_key;
2245 while (slot < nritems) {
2246 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2248 /* we found a different objectid, there must not be acls */
2249 if (found_key.objectid != objectid)
2252 /* we found an xattr, assume we've got an acl */
2253 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2257 * we found a key greater than an xattr key, there can't
2258 * be any acls later on
2260 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2267 * it goes inode, inode backrefs, xattrs, extents,
2268 * so if there are a ton of hard links to an inode there can
2269 * be a lot of backrefs. Don't waste time searching too hard,
2270 * this is just an optimization
2275 /* we hit the end of the leaf before we found an xattr or
2276 * something larger than an xattr. We have to assume the inode
2283 * read an inode from the btree into the in-memory inode
2285 static void btrfs_read_locked_inode(struct inode *inode)
2287 struct btrfs_path *path;
2288 struct extent_buffer *leaf;
2289 struct btrfs_inode_item *inode_item;
2290 struct btrfs_timespec *tspec;
2291 struct btrfs_root *root = BTRFS_I(inode)->root;
2292 struct btrfs_key location;
2296 bool filled = false;
2298 ret = btrfs_fill_inode(inode, &rdev);
2302 path = btrfs_alloc_path();
2306 path->leave_spinning = 1;
2307 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2309 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2313 leaf = path->nodes[0];
2318 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2319 struct btrfs_inode_item);
2320 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2321 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
2322 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2323 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2324 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2326 tspec = btrfs_inode_atime(inode_item);
2327 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2328 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2330 tspec = btrfs_inode_mtime(inode_item);
2331 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2332 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2334 tspec = btrfs_inode_ctime(inode_item);
2335 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2336 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2338 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2339 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2340 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2341 inode->i_generation = BTRFS_I(inode)->generation;
2343 rdev = btrfs_inode_rdev(leaf, inode_item);
2345 BTRFS_I(inode)->index_cnt = (u64)-1;
2346 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2349 * try to precache a NULL acl entry for files that don't have
2350 * any xattrs or acls
2352 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2355 cache_no_acl(inode);
2357 btrfs_free_path(path);
2359 switch (inode->i_mode & S_IFMT) {
2361 inode->i_mapping->a_ops = &btrfs_aops;
2362 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2363 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2364 inode->i_fop = &btrfs_file_operations;
2365 inode->i_op = &btrfs_file_inode_operations;
2368 inode->i_fop = &btrfs_dir_file_operations;
2369 if (root == root->fs_info->tree_root)
2370 inode->i_op = &btrfs_dir_ro_inode_operations;
2372 inode->i_op = &btrfs_dir_inode_operations;
2375 inode->i_op = &btrfs_symlink_inode_operations;
2376 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2377 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2380 inode->i_op = &btrfs_special_inode_operations;
2381 init_special_inode(inode, inode->i_mode, rdev);
2385 btrfs_update_iflags(inode);
2389 btrfs_free_path(path);
2390 make_bad_inode(inode);
2394 * given a leaf and an inode, copy the inode fields into the leaf
2396 static void fill_inode_item(struct btrfs_trans_handle *trans,
2397 struct extent_buffer *leaf,
2398 struct btrfs_inode_item *item,
2399 struct inode *inode)
2401 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2402 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2403 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2404 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2405 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2407 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2408 inode->i_atime.tv_sec);
2409 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2410 inode->i_atime.tv_nsec);
2412 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2413 inode->i_mtime.tv_sec);
2414 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2415 inode->i_mtime.tv_nsec);
2417 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2418 inode->i_ctime.tv_sec);
2419 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2420 inode->i_ctime.tv_nsec);
2422 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2423 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2424 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2425 btrfs_set_inode_transid(leaf, item, trans->transid);
2426 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2427 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2428 btrfs_set_inode_block_group(leaf, item, 0);
2432 * copy everything in the in-memory inode into the btree.
2434 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
2435 struct btrfs_root *root, struct inode *inode)
2437 struct btrfs_inode_item *inode_item;
2438 struct btrfs_path *path;
2439 struct extent_buffer *leaf;
2442 path = btrfs_alloc_path();
2446 path->leave_spinning = 1;
2447 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2455 btrfs_unlock_up_safe(path, 1);
2456 leaf = path->nodes[0];
2457 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2458 struct btrfs_inode_item);
2460 fill_inode_item(trans, leaf, inode_item, inode);
2461 btrfs_mark_buffer_dirty(leaf);
2462 btrfs_set_inode_last_trans(trans, inode);
2465 btrfs_free_path(path);
2470 * copy everything in the in-memory inode into the btree.
2472 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2473 struct btrfs_root *root, struct inode *inode)
2478 * If the inode is a free space inode, we can deadlock during commit
2479 * if we put it into the delayed code.
2481 * The data relocation inode should also be directly updated
2484 if (!btrfs_is_free_space_inode(root, inode)
2485 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2486 ret = btrfs_delayed_update_inode(trans, root, inode);
2488 btrfs_set_inode_last_trans(trans, inode);
2492 return btrfs_update_inode_item(trans, root, inode);
2495 static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2496 struct btrfs_root *root, struct inode *inode)
2500 ret = btrfs_update_inode(trans, root, inode);
2502 return btrfs_update_inode_item(trans, root, inode);
2507 * unlink helper that gets used here in inode.c and in the tree logging
2508 * recovery code. It remove a link in a directory with a given name, and
2509 * also drops the back refs in the inode to the directory
2511 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2512 struct btrfs_root *root,
2513 struct inode *dir, struct inode *inode,
2514 const char *name, int name_len)
2516 struct btrfs_path *path;
2518 struct extent_buffer *leaf;
2519 struct btrfs_dir_item *di;
2520 struct btrfs_key key;
2522 u64 ino = btrfs_ino(inode);
2523 u64 dir_ino = btrfs_ino(dir);
2525 path = btrfs_alloc_path();
2531 path->leave_spinning = 1;
2532 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2533 name, name_len, -1);
2542 leaf = path->nodes[0];
2543 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2544 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2547 btrfs_release_path(path);
2549 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2552 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2553 "inode %llu parent %llu\n", name_len, name,
2554 (unsigned long long)ino, (unsigned long long)dir_ino);
2558 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2562 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2564 BUG_ON(ret != 0 && ret != -ENOENT);
2566 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2571 btrfs_free_path(path);
2575 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2576 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2577 btrfs_update_inode(trans, root, dir);
2582 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2583 struct btrfs_root *root,
2584 struct inode *dir, struct inode *inode,
2585 const char *name, int name_len)
2588 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2590 btrfs_drop_nlink(inode);
2591 ret = btrfs_update_inode(trans, root, inode);
2597 /* helper to check if there is any shared block in the path */
2598 static int check_path_shared(struct btrfs_root *root,
2599 struct btrfs_path *path)
2601 struct extent_buffer *eb;
2605 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2608 if (!path->nodes[level])
2610 eb = path->nodes[level];
2611 if (!btrfs_block_can_be_shared(root, eb))
2613 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2622 * helper to start transaction for unlink and rmdir.
2624 * unlink and rmdir are special in btrfs, they do not always free space.
2625 * so in enospc case, we should make sure they will free space before
2626 * allowing them to use the global metadata reservation.
2628 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2629 struct dentry *dentry)
2631 struct btrfs_trans_handle *trans;
2632 struct btrfs_root *root = BTRFS_I(dir)->root;
2633 struct btrfs_path *path;
2634 struct btrfs_inode_ref *ref;
2635 struct btrfs_dir_item *di;
2636 struct inode *inode = dentry->d_inode;
2641 u64 ino = btrfs_ino(inode);
2642 u64 dir_ino = btrfs_ino(dir);
2645 * 1 for the possible orphan item
2646 * 1 for the dir item
2647 * 1 for the dir index
2648 * 1 for the inode ref
2649 * 1 for the inode ref in the tree log
2650 * 2 for the dir entries in the log
2653 trans = btrfs_start_transaction(root, 8);
2654 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2657 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2658 return ERR_PTR(-ENOSPC);
2660 /* check if there is someone else holds reference */
2661 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2662 return ERR_PTR(-ENOSPC);
2664 if (atomic_read(&inode->i_count) > 2)
2665 return ERR_PTR(-ENOSPC);
2667 if (xchg(&root->fs_info->enospc_unlink, 1))
2668 return ERR_PTR(-ENOSPC);
2670 path = btrfs_alloc_path();
2672 root->fs_info->enospc_unlink = 0;
2673 return ERR_PTR(-ENOMEM);
2676 /* 1 for the orphan item */
2677 trans = btrfs_start_transaction(root, 1);
2678 if (IS_ERR(trans)) {
2679 btrfs_free_path(path);
2680 root->fs_info->enospc_unlink = 0;
2684 path->skip_locking = 1;
2685 path->search_commit_root = 1;
2687 ret = btrfs_lookup_inode(trans, root, path,
2688 &BTRFS_I(dir)->location, 0);
2694 if (check_path_shared(root, path))
2699 btrfs_release_path(path);
2701 ret = btrfs_lookup_inode(trans, root, path,
2702 &BTRFS_I(inode)->location, 0);
2708 if (check_path_shared(root, path))
2713 btrfs_release_path(path);
2715 if (ret == 0 && S_ISREG(inode->i_mode)) {
2716 ret = btrfs_lookup_file_extent(trans, root, path,
2723 if (check_path_shared(root, path))
2725 btrfs_release_path(path);
2733 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2734 dentry->d_name.name, dentry->d_name.len, 0);
2740 if (check_path_shared(root, path))
2746 btrfs_release_path(path);
2748 ref = btrfs_lookup_inode_ref(trans, root, path,
2749 dentry->d_name.name, dentry->d_name.len,
2756 if (check_path_shared(root, path))
2758 index = btrfs_inode_ref_index(path->nodes[0], ref);
2759 btrfs_release_path(path);
2762 * This is a commit root search, if we can lookup inode item and other
2763 * relative items in the commit root, it means the transaction of
2764 * dir/file creation has been committed, and the dir index item that we
2765 * delay to insert has also been inserted into the commit root. So
2766 * we needn't worry about the delayed insertion of the dir index item
2769 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2770 dentry->d_name.name, dentry->d_name.len, 0);
2775 BUG_ON(ret == -ENOENT);
2776 if (check_path_shared(root, path))
2781 btrfs_free_path(path);
2782 /* Migrate the orphan reservation over */
2784 err = btrfs_block_rsv_migrate(trans->block_rsv,
2785 &root->fs_info->global_block_rsv,
2786 trans->bytes_reserved);
2789 btrfs_end_transaction(trans, root);
2790 root->fs_info->enospc_unlink = 0;
2791 return ERR_PTR(err);
2794 trans->block_rsv = &root->fs_info->global_block_rsv;
2798 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2799 struct btrfs_root *root)
2801 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2802 btrfs_block_rsv_release(root, trans->block_rsv,
2803 trans->bytes_reserved);
2804 trans->block_rsv = &root->fs_info->trans_block_rsv;
2805 BUG_ON(!root->fs_info->enospc_unlink);
2806 root->fs_info->enospc_unlink = 0;
2808 btrfs_end_transaction_throttle(trans, root);
2811 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2813 struct btrfs_root *root = BTRFS_I(dir)->root;
2814 struct btrfs_trans_handle *trans;
2815 struct inode *inode = dentry->d_inode;
2817 unsigned long nr = 0;
2819 trans = __unlink_start_trans(dir, dentry);
2821 return PTR_ERR(trans);
2823 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2825 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2826 dentry->d_name.name, dentry->d_name.len);
2830 if (inode->i_nlink == 0) {
2831 ret = btrfs_orphan_add(trans, inode);
2837 nr = trans->blocks_used;
2838 __unlink_end_trans(trans, root);
2839 btrfs_btree_balance_dirty(root, nr);
2843 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root,
2845 struct inode *dir, u64 objectid,
2846 const char *name, int name_len)
2848 struct btrfs_path *path;
2849 struct extent_buffer *leaf;
2850 struct btrfs_dir_item *di;
2851 struct btrfs_key key;
2854 u64 dir_ino = btrfs_ino(dir);
2856 path = btrfs_alloc_path();
2860 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2861 name, name_len, -1);
2862 BUG_ON(IS_ERR_OR_NULL(di));
2864 leaf = path->nodes[0];
2865 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2866 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2867 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2869 btrfs_release_path(path);
2871 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2872 objectid, root->root_key.objectid,
2873 dir_ino, &index, name, name_len);
2875 BUG_ON(ret != -ENOENT);
2876 di = btrfs_search_dir_index_item(root, path, dir_ino,
2878 BUG_ON(IS_ERR_OR_NULL(di));
2880 leaf = path->nodes[0];
2881 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2882 btrfs_release_path(path);
2885 btrfs_release_path(path);
2887 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2890 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2891 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2892 ret = btrfs_update_inode(trans, root, dir);
2895 btrfs_free_path(path);
2899 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2901 struct inode *inode = dentry->d_inode;
2903 struct btrfs_root *root = BTRFS_I(dir)->root;
2904 struct btrfs_trans_handle *trans;
2905 unsigned long nr = 0;
2907 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2908 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
2911 trans = __unlink_start_trans(dir, dentry);
2913 return PTR_ERR(trans);
2915 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2916 err = btrfs_unlink_subvol(trans, root, dir,
2917 BTRFS_I(inode)->location.objectid,
2918 dentry->d_name.name,
2919 dentry->d_name.len);
2923 err = btrfs_orphan_add(trans, inode);
2927 /* now the directory is empty */
2928 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2929 dentry->d_name.name, dentry->d_name.len);
2931 btrfs_i_size_write(inode, 0);
2933 nr = trans->blocks_used;
2934 __unlink_end_trans(trans, root);
2935 btrfs_btree_balance_dirty(root, nr);
2941 * this can truncate away extent items, csum items and directory items.
2942 * It starts at a high offset and removes keys until it can't find
2943 * any higher than new_size
2945 * csum items that cross the new i_size are truncated to the new size
2948 * min_type is the minimum key type to truncate down to. If set to 0, this
2949 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2951 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root,
2953 struct inode *inode,
2954 u64 new_size, u32 min_type)
2956 struct btrfs_path *path;
2957 struct extent_buffer *leaf;
2958 struct btrfs_file_extent_item *fi;
2959 struct btrfs_key key;
2960 struct btrfs_key found_key;
2961 u64 extent_start = 0;
2962 u64 extent_num_bytes = 0;
2963 u64 extent_offset = 0;
2965 u64 mask = root->sectorsize - 1;
2966 u32 found_type = (u8)-1;
2969 int pending_del_nr = 0;
2970 int pending_del_slot = 0;
2971 int extent_type = -1;
2975 u64 ino = btrfs_ino(inode);
2977 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
2979 path = btrfs_alloc_path();
2984 if (root->ref_cows || root == root->fs_info->tree_root)
2985 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2988 * This function is also used to drop the items in the log tree before
2989 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
2990 * it is used to drop the loged items. So we shouldn't kill the delayed
2993 if (min_type == 0 && root == BTRFS_I(inode)->root)
2994 btrfs_kill_delayed_inode_items(inode);
2997 key.offset = (u64)-1;
3001 path->leave_spinning = 1;
3002 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3009 /* there are no items in the tree for us to truncate, we're
3012 if (path->slots[0] == 0)
3019 leaf = path->nodes[0];
3020 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3021 found_type = btrfs_key_type(&found_key);
3024 if (found_key.objectid != ino)
3027 if (found_type < min_type)
3030 item_end = found_key.offset;
3031 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3032 fi = btrfs_item_ptr(leaf, path->slots[0],
3033 struct btrfs_file_extent_item);
3034 extent_type = btrfs_file_extent_type(leaf, fi);
3035 encoding = btrfs_file_extent_compression(leaf, fi);
3036 encoding |= btrfs_file_extent_encryption(leaf, fi);
3037 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3039 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3041 btrfs_file_extent_num_bytes(leaf, fi);
3042 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3043 item_end += btrfs_file_extent_inline_len(leaf,
3048 if (found_type > min_type) {
3051 if (item_end < new_size)
3053 if (found_key.offset >= new_size)
3059 /* FIXME, shrink the extent if the ref count is only 1 */
3060 if (found_type != BTRFS_EXTENT_DATA_KEY)
3063 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3065 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3066 if (!del_item && !encoding) {
3067 u64 orig_num_bytes =
3068 btrfs_file_extent_num_bytes(leaf, fi);
3069 extent_num_bytes = new_size -
3070 found_key.offset + root->sectorsize - 1;
3071 extent_num_bytes = extent_num_bytes &
3072 ~((u64)root->sectorsize - 1);
3073 btrfs_set_file_extent_num_bytes(leaf, fi,
3075 num_dec = (orig_num_bytes -
3077 if (root->ref_cows && extent_start != 0)
3078 inode_sub_bytes(inode, num_dec);
3079 btrfs_mark_buffer_dirty(leaf);
3082 btrfs_file_extent_disk_num_bytes(leaf,