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"
53 #include "free-space-cache.h"
54 #include "inode-map.h"
56 struct btrfs_iget_args {
58 struct btrfs_root *root;
61 static const struct inode_operations btrfs_dir_inode_operations;
62 static const struct inode_operations btrfs_symlink_inode_operations;
63 static const struct inode_operations btrfs_dir_ro_inode_operations;
64 static const struct inode_operations btrfs_special_inode_operations;
65 static const struct inode_operations btrfs_file_inode_operations;
66 static const struct address_space_operations btrfs_aops;
67 static const struct address_space_operations btrfs_symlink_aops;
68 static const struct file_operations btrfs_dir_file_operations;
69 static struct extent_io_ops btrfs_extent_io_ops;
71 static struct kmem_cache *btrfs_inode_cachep;
72 struct kmem_cache *btrfs_trans_handle_cachep;
73 struct kmem_cache *btrfs_transaction_cachep;
74 struct kmem_cache *btrfs_path_cachep;
75 struct kmem_cache *btrfs_free_space_cachep;
78 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
79 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
80 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
81 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
82 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
83 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
84 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
85 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88 static int btrfs_setsize(struct inode *inode, loff_t newsize);
89 static int btrfs_truncate(struct inode *inode);
90 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
91 static noinline int cow_file_range(struct inode *inode,
92 struct page *locked_page,
93 u64 start, u64 end, int *page_started,
94 unsigned long *nr_written, int unlock);
96 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
97 struct inode *inode, struct inode *dir)
101 err = btrfs_init_acl(trans, inode, dir);
103 err = btrfs_xattr_security_init(trans, inode, dir);
108 * this does all the hard work for inserting an inline extent into
109 * the btree. The caller should have done a btrfs_drop_extents so that
110 * no overlapping inline items exist in the btree
112 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
113 struct btrfs_root *root, struct inode *inode,
114 u64 start, size_t size, size_t compressed_size,
116 struct page **compressed_pages)
118 struct btrfs_key key;
119 struct btrfs_path *path;
120 struct extent_buffer *leaf;
121 struct page *page = NULL;
124 struct btrfs_file_extent_item *ei;
127 size_t cur_size = size;
129 unsigned long offset;
131 if (compressed_size && compressed_pages)
132 cur_size = compressed_size;
134 path = btrfs_alloc_path();
138 path->leave_spinning = 1;
139 btrfs_set_trans_block_group(trans, inode);
141 key.objectid = btrfs_ino(inode);
143 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
144 datasize = btrfs_file_extent_calc_inline_size(cur_size);
146 inode_add_bytes(inode, size);
147 ret = btrfs_insert_empty_item(trans, root, path, &key,
154 leaf = path->nodes[0];
155 ei = btrfs_item_ptr(leaf, path->slots[0],
156 struct btrfs_file_extent_item);
157 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
158 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
159 btrfs_set_file_extent_encryption(leaf, ei, 0);
160 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
161 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
162 ptr = btrfs_file_extent_inline_start(ei);
164 if (compress_type != BTRFS_COMPRESS_NONE) {
167 while (compressed_size > 0) {
168 cpage = compressed_pages[i];
169 cur_size = min_t(unsigned long, compressed_size,
172 kaddr = kmap_atomic(cpage, KM_USER0);
173 write_extent_buffer(leaf, kaddr, ptr, cur_size);
174 kunmap_atomic(kaddr, KM_USER0);
178 compressed_size -= cur_size;
180 btrfs_set_file_extent_compression(leaf, ei,
183 page = find_get_page(inode->i_mapping,
184 start >> PAGE_CACHE_SHIFT);
185 btrfs_set_file_extent_compression(leaf, ei, 0);
186 kaddr = kmap_atomic(page, KM_USER0);
187 offset = start & (PAGE_CACHE_SIZE - 1);
188 write_extent_buffer(leaf, kaddr + offset, ptr, size);
189 kunmap_atomic(kaddr, KM_USER0);
190 page_cache_release(page);
192 btrfs_mark_buffer_dirty(leaf);
193 btrfs_free_path(path);
196 * we're an inline extent, so nobody can
197 * extend the file past i_size without locking
198 * a page we already have locked.
200 * We must do any isize and inode updates
201 * before we unlock the pages. Otherwise we
202 * could end up racing with unlink.
204 BTRFS_I(inode)->disk_i_size = inode->i_size;
205 btrfs_update_inode(trans, root, inode);
209 btrfs_free_path(path);
215 * conditionally insert an inline extent into the file. This
216 * does the checks required to make sure the data is small enough
217 * to fit as an inline extent.
219 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
220 struct btrfs_root *root,
221 struct inode *inode, u64 start, u64 end,
222 size_t compressed_size, int compress_type,
223 struct page **compressed_pages)
225 u64 isize = i_size_read(inode);
226 u64 actual_end = min(end + 1, isize);
227 u64 inline_len = actual_end - start;
228 u64 aligned_end = (end + root->sectorsize - 1) &
229 ~((u64)root->sectorsize - 1);
231 u64 data_len = inline_len;
235 data_len = compressed_size;
238 actual_end >= PAGE_CACHE_SIZE ||
239 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (actual_end & (root->sectorsize - 1)) == 0) ||
243 data_len > root->fs_info->max_inline) {
247 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
251 if (isize > actual_end)
252 inline_len = min_t(u64, isize, actual_end);
253 ret = insert_inline_extent(trans, root, inode, start,
254 inline_len, compressed_size,
255 compress_type, compressed_pages);
257 btrfs_delalloc_release_metadata(inode, end + 1 - start);
258 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
262 struct async_extent {
267 unsigned long nr_pages;
269 struct list_head list;
274 struct btrfs_root *root;
275 struct page *locked_page;
278 struct list_head extents;
279 struct btrfs_work work;
282 static noinline int add_async_extent(struct async_cow *cow,
283 u64 start, u64 ram_size,
286 unsigned long nr_pages,
289 struct async_extent *async_extent;
291 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
292 BUG_ON(!async_extent);
293 async_extent->start = start;
294 async_extent->ram_size = ram_size;
295 async_extent->compressed_size = compressed_size;
296 async_extent->pages = pages;
297 async_extent->nr_pages = nr_pages;
298 async_extent->compress_type = compress_type;
299 list_add_tail(&async_extent->list, &cow->extents);
304 * we create compressed extents in two phases. The first
305 * phase compresses a range of pages that have already been
306 * locked (both pages and state bits are locked).
308 * This is done inside an ordered work queue, and the compression
309 * is spread across many cpus. The actual IO submission is step
310 * two, and the ordered work queue takes care of making sure that
311 * happens in the same order things were put onto the queue by
312 * writepages and friends.
314 * If this code finds it can't get good compression, it puts an
315 * entry onto the work queue to write the uncompressed bytes. This
316 * makes sure that both compressed inodes and uncompressed inodes
317 * are written in the same order that pdflush sent them down.
319 static noinline int compress_file_range(struct inode *inode,
320 struct page *locked_page,
322 struct async_cow *async_cow,
325 struct btrfs_root *root = BTRFS_I(inode)->root;
326 struct btrfs_trans_handle *trans;
328 u64 blocksize = root->sectorsize;
330 u64 isize = i_size_read(inode);
332 struct page **pages = NULL;
333 unsigned long nr_pages;
334 unsigned long nr_pages_ret = 0;
335 unsigned long total_compressed = 0;
336 unsigned long total_in = 0;
337 unsigned long max_compressed = 128 * 1024;
338 unsigned long max_uncompressed = 128 * 1024;
341 int compress_type = root->fs_info->compress_type;
343 actual_end = min_t(u64, isize, end + 1);
346 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
347 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
350 * we don't want to send crud past the end of i_size through
351 * compression, that's just a waste of CPU time. So, if the
352 * end of the file is before the start of our current
353 * requested range of bytes, we bail out to the uncompressed
354 * cleanup code that can deal with all of this.
356 * It isn't really the fastest way to fix things, but this is a
357 * very uncommon corner.
359 if (actual_end <= start)
360 goto cleanup_and_bail_uncompressed;
362 total_compressed = actual_end - start;
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
374 total_compressed = min(total_compressed, max_uncompressed);
375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
376 num_bytes = max(blocksize, num_bytes);
381 * we do compression for mount -o compress and when the
382 * inode has not been flagged as nocompress. This flag can
383 * change at any time if we discover bad compression ratios.
385 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
386 (btrfs_test_opt(root, COMPRESS) ||
387 (BTRFS_I(inode)->force_compress) ||
388 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
390 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
393 if (BTRFS_I(inode)->force_compress)
394 compress_type = BTRFS_I(inode)->force_compress;
396 ret = btrfs_compress_pages(compress_type,
397 inode->i_mapping, start,
398 total_compressed, pages,
399 nr_pages, &nr_pages_ret,
405 unsigned long offset = total_compressed &
406 (PAGE_CACHE_SIZE - 1);
407 struct page *page = pages[nr_pages_ret - 1];
410 /* zero the tail end of the last page, we might be
411 * sending it down to disk
414 kaddr = kmap_atomic(page, KM_USER0);
415 memset(kaddr + offset, 0,
416 PAGE_CACHE_SIZE - offset);
417 kunmap_atomic(kaddr, KM_USER0);
423 trans = btrfs_join_transaction(root, 1);
424 BUG_ON(IS_ERR(trans));
425 btrfs_set_trans_block_group(trans, inode);
426 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
428 /* lets try to make an inline extent */
429 if (ret || total_in < (actual_end - start)) {
430 /* we didn't compress the entire range, try
431 * to make an uncompressed inline extent.
433 ret = cow_file_range_inline(trans, root, inode,
434 start, end, 0, 0, NULL);
436 /* try making a compressed inline extent */
437 ret = cow_file_range_inline(trans, root, inode,
440 compress_type, pages);
444 * inline extent creation worked, we don't need
445 * to create any more async work items. Unlock
446 * and free up our temp pages.
448 extent_clear_unlock_delalloc(inode,
449 &BTRFS_I(inode)->io_tree,
451 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
452 EXTENT_CLEAR_DELALLOC |
453 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
455 btrfs_end_transaction(trans, root);
458 btrfs_end_transaction(trans, root);
463 * we aren't doing an inline extent round the compressed size
464 * up to a block size boundary so the allocator does sane
467 total_compressed = (total_compressed + blocksize - 1) &
471 * one last check to make sure the compression is really a
472 * win, compare the page count read with the blocks on disk
474 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
475 ~(PAGE_CACHE_SIZE - 1);
476 if (total_compressed >= total_in) {
479 num_bytes = total_in;
482 if (!will_compress && pages) {
484 * the compression code ran but failed to make things smaller,
485 * free any pages it allocated and our page pointer array
487 for (i = 0; i < nr_pages_ret; i++) {
488 WARN_ON(pages[i]->mapping);
489 page_cache_release(pages[i]);
493 total_compressed = 0;
496 /* flag the file so we don't compress in the future */
497 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
498 !(BTRFS_I(inode)->force_compress)) {
499 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
505 /* the async work queues will take care of doing actual
506 * allocation on disk for these compressed pages,
507 * and will submit them to the elevator.
509 add_async_extent(async_cow, start, num_bytes,
510 total_compressed, pages, nr_pages_ret,
513 if (start + num_bytes < end) {
520 cleanup_and_bail_uncompressed:
522 * No compression, but we still need to write the pages in
523 * the file we've been given so far. redirty the locked
524 * page if it corresponds to our extent and set things up
525 * for the async work queue to run cow_file_range to do
526 * the normal delalloc dance
528 if (page_offset(locked_page) >= start &&
529 page_offset(locked_page) <= end) {
530 __set_page_dirty_nobuffers(locked_page);
531 /* unlocked later on in the async handlers */
533 add_async_extent(async_cow, start, end - start + 1,
534 0, NULL, 0, BTRFS_COMPRESS_NONE);
542 for (i = 0; i < nr_pages_ret; i++) {
543 WARN_ON(pages[i]->mapping);
544 page_cache_release(pages[i]);
552 * phase two of compressed writeback. This is the ordered portion
553 * of the code, which only gets called in the order the work was
554 * queued. We walk all the async extents created by compress_file_range
555 * and send them down to the disk.
557 static noinline int submit_compressed_extents(struct inode *inode,
558 struct async_cow *async_cow)
560 struct async_extent *async_extent;
562 struct btrfs_trans_handle *trans;
563 struct btrfs_key ins;
564 struct extent_map *em;
565 struct btrfs_root *root = BTRFS_I(inode)->root;
566 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
567 struct extent_io_tree *io_tree;
570 if (list_empty(&async_cow->extents))
574 while (!list_empty(&async_cow->extents)) {
575 async_extent = list_entry(async_cow->extents.next,
576 struct async_extent, list);
577 list_del(&async_extent->list);
579 io_tree = &BTRFS_I(inode)->io_tree;
582 /* did the compression code fall back to uncompressed IO? */
583 if (!async_extent->pages) {
584 int page_started = 0;
585 unsigned long nr_written = 0;
587 lock_extent(io_tree, async_extent->start,
588 async_extent->start +
589 async_extent->ram_size - 1, GFP_NOFS);
591 /* allocate blocks */
592 ret = cow_file_range(inode, async_cow->locked_page,
594 async_extent->start +
595 async_extent->ram_size - 1,
596 &page_started, &nr_written, 0);
599 * if page_started, cow_file_range inserted an
600 * inline extent and took care of all the unlocking
601 * and IO for us. Otherwise, we need to submit
602 * all those pages down to the drive.
604 if (!page_started && !ret)
605 extent_write_locked_range(io_tree,
606 inode, async_extent->start,
607 async_extent->start +
608 async_extent->ram_size - 1,
616 lock_extent(io_tree, async_extent->start,
617 async_extent->start + async_extent->ram_size - 1,
620 trans = btrfs_join_transaction(root, 1);
621 BUG_ON(IS_ERR(trans));
622 ret = btrfs_reserve_extent(trans, root,
623 async_extent->compressed_size,
624 async_extent->compressed_size,
627 btrfs_end_transaction(trans, root);
631 for (i = 0; i < async_extent->nr_pages; i++) {
632 WARN_ON(async_extent->pages[i]->mapping);
633 page_cache_release(async_extent->pages[i]);
635 kfree(async_extent->pages);
636 async_extent->nr_pages = 0;
637 async_extent->pages = NULL;
638 unlock_extent(io_tree, async_extent->start,
639 async_extent->start +
640 async_extent->ram_size - 1, GFP_NOFS);
645 * here we're doing allocation and writeback of the
648 btrfs_drop_extent_cache(inode, async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1, 0);
652 em = alloc_extent_map(GFP_NOFS);
654 em->start = async_extent->start;
655 em->len = async_extent->ram_size;
656 em->orig_start = em->start;
658 em->block_start = ins.objectid;
659 em->block_len = ins.offset;
660 em->bdev = root->fs_info->fs_devices->latest_bdev;
661 em->compress_type = async_extent->compress_type;
662 set_bit(EXTENT_FLAG_PINNED, &em->flags);
663 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
666 write_lock(&em_tree->lock);
667 ret = add_extent_mapping(em_tree, em);
668 write_unlock(&em_tree->lock);
669 if (ret != -EEXIST) {
673 btrfs_drop_extent_cache(inode, async_extent->start,
674 async_extent->start +
675 async_extent->ram_size - 1, 0);
678 ret = btrfs_add_ordered_extent_compress(inode,
681 async_extent->ram_size,
683 BTRFS_ORDERED_COMPRESSED,
684 async_extent->compress_type);
688 * clear dirty, set writeback and unlock the pages.
690 extent_clear_unlock_delalloc(inode,
691 &BTRFS_I(inode)->io_tree,
693 async_extent->start +
694 async_extent->ram_size - 1,
695 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
696 EXTENT_CLEAR_UNLOCK |
697 EXTENT_CLEAR_DELALLOC |
698 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
700 ret = btrfs_submit_compressed_write(inode,
702 async_extent->ram_size,
704 ins.offset, async_extent->pages,
705 async_extent->nr_pages);
708 alloc_hint = ins.objectid + ins.offset;
716 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
719 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
720 struct extent_map *em;
723 read_lock(&em_tree->lock);
724 em = search_extent_mapping(em_tree, start, num_bytes);
727 * if block start isn't an actual block number then find the
728 * first block in this inode and use that as a hint. If that
729 * block is also bogus then just don't worry about it.
731 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
733 em = search_extent_mapping(em_tree, 0, 0);
734 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
735 alloc_hint = em->block_start;
739 alloc_hint = em->block_start;
743 read_unlock(&em_tree->lock);
748 static inline bool is_free_space_inode(struct btrfs_root *root,
751 if (root == root->fs_info->tree_root ||
752 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
758 * when extent_io.c finds a delayed allocation range in the file,
759 * the call backs end up in this code. The basic idea is to
760 * allocate extents on disk for the range, and create ordered data structs
761 * in ram to track those extents.
763 * locked_page is the page that writepage had locked already. We use
764 * it to make sure we don't do extra locks or unlocks.
766 * *page_started is set to one if we unlock locked_page and do everything
767 * required to start IO on it. It may be clean and already done with
770 static noinline int cow_file_range(struct inode *inode,
771 struct page *locked_page,
772 u64 start, u64 end, int *page_started,
773 unsigned long *nr_written,
776 struct btrfs_root *root = BTRFS_I(inode)->root;
777 struct btrfs_trans_handle *trans;
780 unsigned long ram_size;
783 u64 blocksize = root->sectorsize;
784 struct btrfs_key ins;
785 struct extent_map *em;
786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
789 BUG_ON(is_free_space_inode(root, inode));
790 trans = btrfs_join_transaction(root, 1);
791 BUG_ON(IS_ERR(trans));
792 btrfs_set_trans_block_group(trans, inode);
793 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
795 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
796 num_bytes = max(blocksize, num_bytes);
797 disk_num_bytes = num_bytes;
801 /* lets try to make an inline extent */
802 ret = cow_file_range_inline(trans, root, inode,
803 start, end, 0, 0, NULL);
805 extent_clear_unlock_delalloc(inode,
806 &BTRFS_I(inode)->io_tree,
808 EXTENT_CLEAR_UNLOCK_PAGE |
809 EXTENT_CLEAR_UNLOCK |
810 EXTENT_CLEAR_DELALLOC |
812 EXTENT_SET_WRITEBACK |
813 EXTENT_END_WRITEBACK);
815 *nr_written = *nr_written +
816 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
823 BUG_ON(disk_num_bytes >
824 btrfs_super_total_bytes(&root->fs_info->super_copy));
826 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
827 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
829 while (disk_num_bytes > 0) {
832 cur_alloc_size = disk_num_bytes;
833 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
834 root->sectorsize, 0, alloc_hint,
838 em = alloc_extent_map(GFP_NOFS);
841 em->orig_start = em->start;
842 ram_size = ins.offset;
843 em->len = ins.offset;
845 em->block_start = ins.objectid;
846 em->block_len = ins.offset;
847 em->bdev = root->fs_info->fs_devices->latest_bdev;
848 set_bit(EXTENT_FLAG_PINNED, &em->flags);
851 write_lock(&em_tree->lock);
852 ret = add_extent_mapping(em_tree, em);
853 write_unlock(&em_tree->lock);
854 if (ret != -EEXIST) {
858 btrfs_drop_extent_cache(inode, start,
859 start + ram_size - 1, 0);
862 cur_alloc_size = ins.offset;
863 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
864 ram_size, cur_alloc_size, 0);
867 if (root->root_key.objectid ==
868 BTRFS_DATA_RELOC_TREE_OBJECTID) {
869 ret = btrfs_reloc_clone_csums(inode, start,
874 if (disk_num_bytes < cur_alloc_size)
877 /* we're not doing compressed IO, don't unlock the first
878 * page (which the caller expects to stay locked), don't
879 * clear any dirty bits and don't set any writeback bits
881 * Do set the Private2 bit so we know this page was properly
882 * setup for writepage
884 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
885 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
888 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
889 start, start + ram_size - 1,
891 disk_num_bytes -= cur_alloc_size;
892 num_bytes -= cur_alloc_size;
893 alloc_hint = ins.objectid + ins.offset;
894 start += cur_alloc_size;
898 btrfs_end_transaction(trans, root);
904 * work queue call back to started compression on a file and pages
906 static noinline void async_cow_start(struct btrfs_work *work)
908 struct async_cow *async_cow;
910 async_cow = container_of(work, struct async_cow, work);
912 compress_file_range(async_cow->inode, async_cow->locked_page,
913 async_cow->start, async_cow->end, async_cow,
916 async_cow->inode = NULL;
920 * work queue call back to submit previously compressed pages
922 static noinline void async_cow_submit(struct btrfs_work *work)
924 struct async_cow *async_cow;
925 struct btrfs_root *root;
926 unsigned long nr_pages;
928 async_cow = container_of(work, struct async_cow, work);
930 root = async_cow->root;
931 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
934 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
936 if (atomic_read(&root->fs_info->async_delalloc_pages) <
938 waitqueue_active(&root->fs_info->async_submit_wait))
939 wake_up(&root->fs_info->async_submit_wait);
941 if (async_cow->inode)
942 submit_compressed_extents(async_cow->inode, async_cow);
945 static noinline void async_cow_free(struct btrfs_work *work)
947 struct async_cow *async_cow;
948 async_cow = container_of(work, struct async_cow, work);
952 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
953 u64 start, u64 end, int *page_started,
954 unsigned long *nr_written)
956 struct async_cow *async_cow;
957 struct btrfs_root *root = BTRFS_I(inode)->root;
958 unsigned long nr_pages;
960 int limit = 10 * 1024 * 1042;
962 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
963 1, 0, NULL, GFP_NOFS);
964 while (start < end) {
965 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
966 async_cow->inode = inode;
967 async_cow->root = root;
968 async_cow->locked_page = locked_page;
969 async_cow->start = start;
971 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
974 cur_end = min(end, start + 512 * 1024 - 1);
976 async_cow->end = cur_end;
977 INIT_LIST_HEAD(&async_cow->extents);
979 async_cow->work.func = async_cow_start;
980 async_cow->work.ordered_func = async_cow_submit;
981 async_cow->work.ordered_free = async_cow_free;
982 async_cow->work.flags = 0;
984 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
986 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
988 btrfs_queue_worker(&root->fs_info->delalloc_workers,
991 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
992 wait_event(root->fs_info->async_submit_wait,
993 (atomic_read(&root->fs_info->async_delalloc_pages) <
997 while (atomic_read(&root->fs_info->async_submit_draining) &&
998 atomic_read(&root->fs_info->async_delalloc_pages)) {
999 wait_event(root->fs_info->async_submit_wait,
1000 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1004 *nr_written += nr_pages;
1005 start = cur_end + 1;
1011 static noinline int csum_exist_in_range(struct btrfs_root *root,
1012 u64 bytenr, u64 num_bytes)
1015 struct btrfs_ordered_sum *sums;
1018 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1019 bytenr + num_bytes - 1, &list);
1020 if (ret == 0 && list_empty(&list))
1023 while (!list_empty(&list)) {
1024 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1025 list_del(&sums->list);
1032 * when nowcow writeback call back. This checks for snapshots or COW copies
1033 * of the extents that exist in the file, and COWs the file as required.
1035 * If no cow copies or snapshots exist, we write directly to the existing
1038 static noinline int run_delalloc_nocow(struct inode *inode,
1039 struct page *locked_page,
1040 u64 start, u64 end, int *page_started, int force,
1041 unsigned long *nr_written)
1043 struct btrfs_root *root = BTRFS_I(inode)->root;
1044 struct btrfs_trans_handle *trans;
1045 struct extent_buffer *leaf;
1046 struct btrfs_path *path;
1047 struct btrfs_file_extent_item *fi;
1048 struct btrfs_key found_key;
1061 u64 ino = btrfs_ino(inode);
1063 path = btrfs_alloc_path();
1066 nolock = is_free_space_inode(root, inode);
1069 trans = btrfs_join_transaction_nolock(root, 1);
1071 trans = btrfs_join_transaction(root, 1);
1072 BUG_ON(IS_ERR(trans));
1074 cow_start = (u64)-1;
1077 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1080 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1081 leaf = path->nodes[0];
1082 btrfs_item_key_to_cpu(leaf, &found_key,
1083 path->slots[0] - 1);
1084 if (found_key.objectid == ino &&
1085 found_key.type == BTRFS_EXTENT_DATA_KEY)
1090 leaf = path->nodes[0];
1091 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1092 ret = btrfs_next_leaf(root, path);
1097 leaf = path->nodes[0];
1103 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1105 if (found_key.objectid > ino ||
1106 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1107 found_key.offset > end)
1110 if (found_key.offset > cur_offset) {
1111 extent_end = found_key.offset;
1116 fi = btrfs_item_ptr(leaf, path->slots[0],
1117 struct btrfs_file_extent_item);
1118 extent_type = btrfs_file_extent_type(leaf, fi);
1120 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1121 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1122 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1123 extent_offset = btrfs_file_extent_offset(leaf, fi);
1124 extent_end = found_key.offset +
1125 btrfs_file_extent_num_bytes(leaf, fi);
1126 if (extent_end <= start) {
1130 if (disk_bytenr == 0)
1132 if (btrfs_file_extent_compression(leaf, fi) ||
1133 btrfs_file_extent_encryption(leaf, fi) ||
1134 btrfs_file_extent_other_encoding(leaf, fi))
1136 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1138 if (btrfs_extent_readonly(root, disk_bytenr))
1140 if (btrfs_cross_ref_exist(trans, root, ino,
1142 extent_offset, disk_bytenr))
1144 disk_bytenr += extent_offset;
1145 disk_bytenr += cur_offset - found_key.offset;
1146 num_bytes = min(end + 1, extent_end) - cur_offset;
1148 * force cow if csum exists in the range.
1149 * this ensure that csum for a given extent are
1150 * either valid or do not exist.
1152 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1155 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1156 extent_end = found_key.offset +
1157 btrfs_file_extent_inline_len(leaf, fi);
1158 extent_end = ALIGN(extent_end, root->sectorsize);
1163 if (extent_end <= start) {
1168 if (cow_start == (u64)-1)
1169 cow_start = cur_offset;
1170 cur_offset = extent_end;
1171 if (cur_offset > end)
1177 btrfs_release_path(root, path);
1178 if (cow_start != (u64)-1) {
1179 ret = cow_file_range(inode, locked_page, cow_start,
1180 found_key.offset - 1, page_started,
1183 cow_start = (u64)-1;
1186 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1187 struct extent_map *em;
1188 struct extent_map_tree *em_tree;
1189 em_tree = &BTRFS_I(inode)->extent_tree;
1190 em = alloc_extent_map(GFP_NOFS);
1192 em->start = cur_offset;
1193 em->orig_start = em->start;
1194 em->len = num_bytes;
1195 em->block_len = num_bytes;
1196 em->block_start = disk_bytenr;
1197 em->bdev = root->fs_info->fs_devices->latest_bdev;
1198 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1200 write_lock(&em_tree->lock);
1201 ret = add_extent_mapping(em_tree, em);
1202 write_unlock(&em_tree->lock);
1203 if (ret != -EEXIST) {
1204 free_extent_map(em);
1207 btrfs_drop_extent_cache(inode, em->start,
1208 em->start + em->len - 1, 0);
1210 type = BTRFS_ORDERED_PREALLOC;
1212 type = BTRFS_ORDERED_NOCOW;
1215 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1216 num_bytes, num_bytes, type);
1219 if (root->root_key.objectid ==
1220 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1221 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1226 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1227 cur_offset, cur_offset + num_bytes - 1,
1228 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1229 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1230 EXTENT_SET_PRIVATE2);
1231 cur_offset = extent_end;
1232 if (cur_offset > end)
1235 btrfs_release_path(root, path);
1237 if (cur_offset <= end && cow_start == (u64)-1)
1238 cow_start = cur_offset;
1239 if (cow_start != (u64)-1) {
1240 ret = cow_file_range(inode, locked_page, cow_start, end,
1241 page_started, nr_written, 1);
1246 ret = btrfs_end_transaction_nolock(trans, root);
1249 ret = btrfs_end_transaction(trans, root);
1252 btrfs_free_path(path);
1257 * extent_io.c call back to do delayed allocation processing
1259 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1260 u64 start, u64 end, int *page_started,
1261 unsigned long *nr_written)
1264 struct btrfs_root *root = BTRFS_I(inode)->root;
1266 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1267 ret = run_delalloc_nocow(inode, locked_page, start, end,
1268 page_started, 1, nr_written);
1269 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1270 ret = run_delalloc_nocow(inode, locked_page, start, end,
1271 page_started, 0, nr_written);
1272 else if (!btrfs_test_opt(root, COMPRESS) &&
1273 !(BTRFS_I(inode)->force_compress) &&
1274 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1275 ret = cow_file_range(inode, locked_page, start, end,
1276 page_started, nr_written, 1);
1278 ret = cow_file_range_async(inode, locked_page, start, end,
1279 page_started, nr_written);
1283 static int btrfs_split_extent_hook(struct inode *inode,
1284 struct extent_state *orig, u64 split)
1286 /* not delalloc, ignore it */
1287 if (!(orig->state & EXTENT_DELALLOC))
1290 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1295 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1296 * extents so we can keep track of new extents that are just merged onto old
1297 * extents, such as when we are doing sequential writes, so we can properly
1298 * account for the metadata space we'll need.
1300 static int btrfs_merge_extent_hook(struct inode *inode,
1301 struct extent_state *new,
1302 struct extent_state *other)
1304 /* not delalloc, ignore it */
1305 if (!(other->state & EXTENT_DELALLOC))
1308 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1313 * extent_io.c set_bit_hook, used to track delayed allocation
1314 * bytes in this file, and to maintain the list of inodes that
1315 * have pending delalloc work to be done.
1317 static int btrfs_set_bit_hook(struct inode *inode,
1318 struct extent_state *state, int *bits)
1322 * set_bit and clear bit hooks normally require _irqsave/restore
1323 * but in this case, we are only testeing for the DELALLOC
1324 * bit, which is only set or cleared with irqs on
1326 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1327 struct btrfs_root *root = BTRFS_I(inode)->root;
1328 u64 len = state->end + 1 - state->start;
1329 bool do_list = !is_free_space_inode(root, inode);
1331 if (*bits & EXTENT_FIRST_DELALLOC)
1332 *bits &= ~EXTENT_FIRST_DELALLOC;
1334 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1336 spin_lock(&root->fs_info->delalloc_lock);
1337 BTRFS_I(inode)->delalloc_bytes += len;
1338 root->fs_info->delalloc_bytes += len;
1339 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1340 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1341 &root->fs_info->delalloc_inodes);
1343 spin_unlock(&root->fs_info->delalloc_lock);
1349 * extent_io.c clear_bit_hook, see set_bit_hook for why
1351 static int btrfs_clear_bit_hook(struct inode *inode,
1352 struct extent_state *state, int *bits)
1355 * set_bit and clear bit hooks normally require _irqsave/restore
1356 * but in this case, we are only testeing for the DELALLOC
1357 * bit, which is only set or cleared with irqs on
1359 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1360 struct btrfs_root *root = BTRFS_I(inode)->root;
1361 u64 len = state->end + 1 - state->start;
1362 bool do_list = !is_free_space_inode(root, inode);
1364 if (*bits & EXTENT_FIRST_DELALLOC)
1365 *bits &= ~EXTENT_FIRST_DELALLOC;
1366 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1367 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1369 if (*bits & EXTENT_DO_ACCOUNTING)
1370 btrfs_delalloc_release_metadata(inode, len);
1372 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1374 btrfs_free_reserved_data_space(inode, len);
1376 spin_lock(&root->fs_info->delalloc_lock);
1377 root->fs_info->delalloc_bytes -= len;
1378 BTRFS_I(inode)->delalloc_bytes -= len;
1380 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1381 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1382 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1384 spin_unlock(&root->fs_info->delalloc_lock);
1390 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1391 * we don't create bios that span stripes or chunks
1393 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1394 size_t size, struct bio *bio,
1395 unsigned long bio_flags)
1397 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1398 struct btrfs_mapping_tree *map_tree;
1399 u64 logical = (u64)bio->bi_sector << 9;
1404 if (bio_flags & EXTENT_BIO_COMPRESSED)
1407 length = bio->bi_size;
1408 map_tree = &root->fs_info->mapping_tree;
1409 map_length = length;
1410 ret = btrfs_map_block(map_tree, READ, logical,
1411 &map_length, NULL, 0);
1413 if (map_length < length + size)
1419 * in order to insert checksums into the metadata in large chunks,
1420 * we wait until bio submission time. All the pages in the bio are
1421 * checksummed and sums are attached onto the ordered extent record.
1423 * At IO completion time the cums attached on the ordered extent record
1424 * are inserted into the btree
1426 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1427 struct bio *bio, int mirror_num,
1428 unsigned long bio_flags,
1431 struct btrfs_root *root = BTRFS_I(inode)->root;
1434 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1440 * in order to insert checksums into the metadata in large chunks,
1441 * we wait until bio submission time. All the pages in the bio are
1442 * checksummed and sums are attached onto the ordered extent record.
1444 * At IO completion time the cums attached on the ordered extent record
1445 * are inserted into the btree
1447 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1448 int mirror_num, unsigned long bio_flags,
1451 struct btrfs_root *root = BTRFS_I(inode)->root;
1452 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1456 * extent_io.c submission hook. This does the right thing for csum calculation
1457 * on write, or reading the csums from the tree before a read
1459 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1460 int mirror_num, unsigned long bio_flags,
1463 struct btrfs_root *root = BTRFS_I(inode)->root;
1467 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1469 if (is_free_space_inode(root, inode))
1470 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1472 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1475 if (!(rw & REQ_WRITE)) {
1476 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1477 return btrfs_submit_compressed_read(inode, bio,
1478 mirror_num, bio_flags);
1479 } else if (!skip_sum) {
1480 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1485 } else if (!skip_sum) {
1486 /* csum items have already been cloned */
1487 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1489 /* we're doing a write, do the async checksumming */
1490 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1491 inode, rw, bio, mirror_num,
1492 bio_flags, bio_offset,
1493 __btrfs_submit_bio_start,
1494 __btrfs_submit_bio_done);
1498 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1502 * given a list of ordered sums record them in the inode. This happens
1503 * at IO completion time based on sums calculated at bio submission time.
1505 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1506 struct inode *inode, u64 file_offset,
1507 struct list_head *list)
1509 struct btrfs_ordered_sum *sum;
1511 btrfs_set_trans_block_group(trans, inode);
1513 list_for_each_entry(sum, list, list) {
1514 btrfs_csum_file_blocks(trans,
1515 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1520 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1521 struct extent_state **cached_state)
1523 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1525 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1526 cached_state, GFP_NOFS);
1529 /* see btrfs_writepage_start_hook for details on why this is required */
1530 struct btrfs_writepage_fixup {
1532 struct btrfs_work work;
1535 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1537 struct btrfs_writepage_fixup *fixup;
1538 struct btrfs_ordered_extent *ordered;
1539 struct extent_state *cached_state = NULL;
1541 struct inode *inode;
1545 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1549 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1550 ClearPageChecked(page);
1554 inode = page->mapping->host;
1555 page_start = page_offset(page);
1556 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1558 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1559 &cached_state, GFP_NOFS);
1561 /* already ordered? We're done */
1562 if (PagePrivate2(page))
1565 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1567 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1568 page_end, &cached_state, GFP_NOFS);
1570 btrfs_start_ordered_extent(inode, ordered, 1);
1575 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1576 ClearPageChecked(page);
1578 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1579 &cached_state, GFP_NOFS);
1582 page_cache_release(page);
1587 * There are a few paths in the higher layers of the kernel that directly
1588 * set the page dirty bit without asking the filesystem if it is a
1589 * good idea. This causes problems because we want to make sure COW
1590 * properly happens and the data=ordered rules are followed.
1592 * In our case any range that doesn't have the ORDERED bit set
1593 * hasn't been properly setup for IO. We kick off an async process
1594 * to fix it up. The async helper will wait for ordered extents, set
1595 * the delalloc bit and make it safe to write the page.
1597 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1599 struct inode *inode = page->mapping->host;
1600 struct btrfs_writepage_fixup *fixup;
1601 struct btrfs_root *root = BTRFS_I(inode)->root;
1603 /* this page is properly in the ordered list */
1604 if (TestClearPagePrivate2(page))
1607 if (PageChecked(page))
1610 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1614 SetPageChecked(page);
1615 page_cache_get(page);
1616 fixup->work.func = btrfs_writepage_fixup_worker;
1618 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1622 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1623 struct inode *inode, u64 file_pos,
1624 u64 disk_bytenr, u64 disk_num_bytes,
1625 u64 num_bytes, u64 ram_bytes,
1626 u8 compression, u8 encryption,
1627 u16 other_encoding, int extent_type)
1629 struct btrfs_root *root = BTRFS_I(inode)->root;
1630 struct btrfs_file_extent_item *fi;
1631 struct btrfs_path *path;
1632 struct extent_buffer *leaf;
1633 struct btrfs_key ins;
1637 path = btrfs_alloc_path();
1640 path->leave_spinning = 1;
1643 * we may be replacing one extent in the tree with another.
1644 * The new extent is pinned in the extent map, and we don't want
1645 * to drop it from the cache until it is completely in the btree.
1647 * So, tell btrfs_drop_extents to leave this extent in the cache.
1648 * the caller is expected to unpin it and allow it to be merged
1651 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1655 ins.objectid = btrfs_ino(inode);
1656 ins.offset = file_pos;
1657 ins.type = BTRFS_EXTENT_DATA_KEY;
1658 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1660 leaf = path->nodes[0];
1661 fi = btrfs_item_ptr(leaf, path->slots[0],
1662 struct btrfs_file_extent_item);
1663 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1664 btrfs_set_file_extent_type(leaf, fi, extent_type);
1665 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1666 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1667 btrfs_set_file_extent_offset(leaf, fi, 0);
1668 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1669 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1670 btrfs_set_file_extent_compression(leaf, fi, compression);
1671 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1672 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1674 btrfs_unlock_up_safe(path, 1);
1675 btrfs_set_lock_blocking(leaf);
1677 btrfs_mark_buffer_dirty(leaf);
1679 inode_add_bytes(inode, num_bytes);
1681 ins.objectid = disk_bytenr;
1682 ins.offset = disk_num_bytes;
1683 ins.type = BTRFS_EXTENT_ITEM_KEY;
1684 ret = btrfs_alloc_reserved_file_extent(trans, root,
1685 root->root_key.objectid,
1686 btrfs_ino(inode), file_pos, &ins);
1688 btrfs_free_path(path);
1694 * helper function for btrfs_finish_ordered_io, this
1695 * just reads in some of the csum leaves to prime them into ram
1696 * before we start the transaction. It limits the amount of btree
1697 * reads required while inside the transaction.
1699 /* as ordered data IO finishes, this gets called so we can finish
1700 * an ordered extent if the range of bytes in the file it covers are
1703 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1705 struct btrfs_root *root = BTRFS_I(inode)->root;
1706 struct btrfs_trans_handle *trans = NULL;
1707 struct btrfs_ordered_extent *ordered_extent = NULL;
1708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1709 struct extent_state *cached_state = NULL;
1710 int compress_type = 0;
1714 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1718 BUG_ON(!ordered_extent);
1720 nolock = is_free_space_inode(root, inode);
1722 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1723 BUG_ON(!list_empty(&ordered_extent->list));
1724 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1727 trans = btrfs_join_transaction_nolock(root, 1);
1729 trans = btrfs_join_transaction(root, 1);
1730 BUG_ON(IS_ERR(trans));
1731 btrfs_set_trans_block_group(trans, inode);
1732 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1733 ret = btrfs_update_inode(trans, root, inode);
1739 lock_extent_bits(io_tree, ordered_extent->file_offset,
1740 ordered_extent->file_offset + ordered_extent->len - 1,
1741 0, &cached_state, GFP_NOFS);
1744 trans = btrfs_join_transaction_nolock(root, 1);
1746 trans = btrfs_join_transaction(root, 1);
1747 BUG_ON(IS_ERR(trans));
1748 btrfs_set_trans_block_group(trans, inode);
1749 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1751 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1752 compress_type = ordered_extent->compress_type;
1753 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1754 BUG_ON(compress_type);
1755 ret = btrfs_mark_extent_written(trans, inode,
1756 ordered_extent->file_offset,
1757 ordered_extent->file_offset +
1758 ordered_extent->len);
1761 BUG_ON(root == root->fs_info->tree_root);
1762 ret = insert_reserved_file_extent(trans, inode,
1763 ordered_extent->file_offset,
1764 ordered_extent->start,
1765 ordered_extent->disk_len,
1766 ordered_extent->len,
1767 ordered_extent->len,
1768 compress_type, 0, 0,
1769 BTRFS_FILE_EXTENT_REG);
1770 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1771 ordered_extent->file_offset,
1772 ordered_extent->len);
1775 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1776 ordered_extent->file_offset +
1777 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1779 add_pending_csums(trans, inode, ordered_extent->file_offset,
1780 &ordered_extent->list);
1782 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1784 ret = btrfs_update_inode(trans, root, inode);
1791 btrfs_end_transaction_nolock(trans, root);
1793 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1795 btrfs_end_transaction(trans, root);
1799 btrfs_put_ordered_extent(ordered_extent);
1800 /* once for the tree */
1801 btrfs_put_ordered_extent(ordered_extent);
1806 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1807 struct extent_state *state, int uptodate)
1809 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1811 ClearPagePrivate2(page);
1812 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1816 * When IO fails, either with EIO or csum verification fails, we
1817 * try other mirrors that might have a good copy of the data. This
1818 * io_failure_record is used to record state as we go through all the
1819 * mirrors. If another mirror has good data, the page is set up to date
1820 * and things continue. If a good mirror can't be found, the original
1821 * bio end_io callback is called to indicate things have failed.
1823 struct io_failure_record {
1828 unsigned long bio_flags;
1832 static int btrfs_io_failed_hook(struct bio *failed_bio,
1833 struct page *page, u64 start, u64 end,
1834 struct extent_state *state)
1836 struct io_failure_record *failrec = NULL;
1838 struct extent_map *em;
1839 struct inode *inode = page->mapping->host;
1840 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1841 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1848 ret = get_state_private(failure_tree, start, &private);
1850 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1853 failrec->start = start;
1854 failrec->len = end - start + 1;
1855 failrec->last_mirror = 0;
1856 failrec->bio_flags = 0;
1858 read_lock(&em_tree->lock);
1859 em = lookup_extent_mapping(em_tree, start, failrec->len);
1860 if (em->start > start || em->start + em->len < start) {
1861 free_extent_map(em);
1864 read_unlock(&em_tree->lock);
1866 if (!em || IS_ERR(em)) {
1870 logical = start - em->start;
1871 logical = em->block_start + logical;
1872 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1873 logical = em->block_start;
1874 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1875 extent_set_compress_type(&failrec->bio_flags,
1878 failrec->logical = logical;
1879 free_extent_map(em);
1880 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1881 EXTENT_DIRTY, GFP_NOFS);
1882 set_state_private(failure_tree, start,
1883 (u64)(unsigned long)failrec);
1885 failrec = (struct io_failure_record *)(unsigned long)private;
1887 num_copies = btrfs_num_copies(
1888 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1889 failrec->logical, failrec->len);
1890 failrec->last_mirror++;
1892 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1893 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1896 if (state && state->start != failrec->start)
1898 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1900 if (!state || failrec->last_mirror > num_copies) {
1901 set_state_private(failure_tree, failrec->start, 0);
1902 clear_extent_bits(failure_tree, failrec->start,
1903 failrec->start + failrec->len - 1,
1904 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1908 bio = bio_alloc(GFP_NOFS, 1);
1909 bio->bi_private = state;
1910 bio->bi_end_io = failed_bio->bi_end_io;
1911 bio->bi_sector = failrec->logical >> 9;
1912 bio->bi_bdev = failed_bio->bi_bdev;
1915 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1916 if (failed_bio->bi_rw & REQ_WRITE)
1921 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1922 failrec->last_mirror,
1923 failrec->bio_flags, 0);
1928 * each time an IO finishes, we do a fast check in the IO failure tree
1929 * to see if we need to process or clean up an io_failure_record
1931 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1934 u64 private_failure;
1935 struct io_failure_record *failure;
1939 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1940 (u64)-1, 1, EXTENT_DIRTY, 0)) {
1941 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1942 start, &private_failure);
1944 failure = (struct io_failure_record *)(unsigned long)
1946 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1948 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1950 failure->start + failure->len - 1,
1951 EXTENT_DIRTY | EXTENT_LOCKED,
1960 * when reads are done, we need to check csums to verify the data is correct
1961 * if there's a match, we allow the bio to finish. If not, we go through
1962 * the io_failure_record routines to find good copies
1964 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1965 struct extent_state *state)
1967 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1968 struct inode *inode = page->mapping->host;
1969 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1971 u64 private = ~(u32)0;
1973 struct btrfs_root *root = BTRFS_I(inode)->root;
1976 if (PageChecked(page)) {
1977 ClearPageChecked(page);
1981 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1984 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1985 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1986 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1991 if (state && state->start == start) {
1992 private = state->private;
1995 ret = get_state_private(io_tree, start, &private);
1997 kaddr = kmap_atomic(page, KM_USER0);
2001 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2002 btrfs_csum_final(csum, (char *)&csum);
2003 if (csum != private)
2006 kunmap_atomic(kaddr, KM_USER0);
2008 /* if the io failure tree for this inode is non-empty,
2009 * check to see if we've recovered from a failed IO
2011 btrfs_clean_io_failures(inode, start);
2015 if (printk_ratelimit()) {
2016 printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2018 (unsigned long long)btrfs_ino(page->mapping->host),
2019 (unsigned long long)start, csum,
2020 (unsigned long long)private);
2022 memset(kaddr + offset, 1, end - start + 1);
2023 flush_dcache_page(page);
2024 kunmap_atomic(kaddr, KM_USER0);
2030 struct delayed_iput {
2031 struct list_head list;
2032 struct inode *inode;
2035 void btrfs_add_delayed_iput(struct inode *inode)
2037 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2038 struct delayed_iput *delayed;
2040 if (atomic_add_unless(&inode->i_count, -1, 1))
2043 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2044 delayed->inode = inode;
2046 spin_lock(&fs_info->delayed_iput_lock);
2047 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2048 spin_unlock(&fs_info->delayed_iput_lock);
2051 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2054 struct btrfs_fs_info *fs_info = root->fs_info;
2055 struct delayed_iput *delayed;
2058 spin_lock(&fs_info->delayed_iput_lock);
2059 empty = list_empty(&fs_info->delayed_iputs);
2060 spin_unlock(&fs_info->delayed_iput_lock);
2064 down_read(&root->fs_info->cleanup_work_sem);
2065 spin_lock(&fs_info->delayed_iput_lock);
2066 list_splice_init(&fs_info->delayed_iputs, &list);
2067 spin_unlock(&fs_info->delayed_iput_lock);
2069 while (!list_empty(&list)) {
2070 delayed = list_entry(list.next, struct delayed_iput, list);
2071 list_del(&delayed->list);
2072 iput(delayed->inode);
2075 up_read(&root->fs_info->cleanup_work_sem);
2079 * calculate extra metadata reservation when snapshotting a subvolume
2080 * contains orphan files.
2082 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2083 struct btrfs_pending_snapshot *pending,
2084 u64 *bytes_to_reserve)
2086 struct btrfs_root *root;
2087 struct btrfs_block_rsv *block_rsv;
2091 root = pending->root;
2092 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2095 block_rsv = root->orphan_block_rsv;
2097 /* orphan block reservation for the snapshot */
2098 num_bytes = block_rsv->size;
2101 * after the snapshot is created, COWing tree blocks may use more
2102 * space than it frees. So we should make sure there is enough
2105 index = trans->transid & 0x1;
2106 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2107 num_bytes += block_rsv->size -
2108 (block_rsv->reserved + block_rsv->freed[index]);
2111 *bytes_to_reserve += num_bytes;
2114 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2115 struct btrfs_pending_snapshot *pending)
2117 struct btrfs_root *root = pending->root;
2118 struct btrfs_root *snap = pending->snap;
2119 struct btrfs_block_rsv *block_rsv;
2124 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2127 /* refill source subvolume's orphan block reservation */
2128 block_rsv = root->orphan_block_rsv;
2129 index = trans->transid & 0x1;
2130 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2131 num_bytes = block_rsv->size -
2132 (block_rsv->reserved + block_rsv->freed[index]);
2133 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2134 root->orphan_block_rsv,
2139 /* setup orphan block reservation for the snapshot */
2140 block_rsv = btrfs_alloc_block_rsv(snap);
2143 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2144 snap->orphan_block_rsv = block_rsv;
2146 num_bytes = root->orphan_block_rsv->size;
2147 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2148 block_rsv, num_bytes);
2152 /* insert orphan item for the snapshot */
2153 WARN_ON(!root->orphan_item_inserted);
2154 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2155 snap->root_key.objectid);
2157 snap->orphan_item_inserted = 1;
2161 enum btrfs_orphan_cleanup_state {
2162 ORPHAN_CLEANUP_STARTED = 1,
2163 ORPHAN_CLEANUP_DONE = 2,
2167 * This is called in transaction commmit time. If there are no orphan
2168 * files in the subvolume, it removes orphan item and frees block_rsv
2171 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2172 struct btrfs_root *root)
2176 if (!list_empty(&root->orphan_list) ||
2177 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2180 if (root->orphan_item_inserted &&
2181 btrfs_root_refs(&root->root_item) > 0) {
2182 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2183 root->root_key.objectid);
2185 root->orphan_item_inserted = 0;
2188 if (root->orphan_block_rsv) {
2189 WARN_ON(root->orphan_block_rsv->size > 0);
2190 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2191 root->orphan_block_rsv = NULL;
2196 * This creates an orphan entry for the given inode in case something goes
2197 * wrong in the middle of an unlink/truncate.
2199 * NOTE: caller of this function should reserve 5 units of metadata for
2202 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2204 struct btrfs_root *root = BTRFS_I(inode)->root;
2205 struct btrfs_block_rsv *block_rsv = NULL;
2210 if (!root->orphan_block_rsv) {
2211 block_rsv = btrfs_alloc_block_rsv(root);
2215 spin_lock(&root->orphan_lock);
2216 if (!root->orphan_block_rsv) {
2217 root->orphan_block_rsv = block_rsv;
2218 } else if (block_rsv) {
2219 btrfs_free_block_rsv(root, block_rsv);
2223 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2224 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2227 * For proper ENOSPC handling, we should do orphan
2228 * cleanup when mounting. But this introduces backward
2229 * compatibility issue.
2231 if (!xchg(&root->orphan_item_inserted, 1))
2239 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2240 BTRFS_I(inode)->orphan_meta_reserved = 1;
2243 spin_unlock(&root->orphan_lock);
2246 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2248 /* grab metadata reservation from transaction handle */
2250 ret = btrfs_orphan_reserve_metadata(trans, inode);
2254 /* insert an orphan item to track this unlinked/truncated file */
2256 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2260 /* insert an orphan item to track subvolume contains orphan files */
2262 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2263 root->root_key.objectid);
2270 * We have done the truncate/delete so we can go ahead and remove the orphan
2271 * item for this particular inode.
2273 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2275 struct btrfs_root *root = BTRFS_I(inode)->root;
2276 int delete_item = 0;
2277 int release_rsv = 0;
2280 spin_lock(&root->orphan_lock);
2281 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2282 list_del_init(&BTRFS_I(inode)->i_orphan);
2286 if (BTRFS_I(inode)->orphan_meta_reserved) {
2287 BTRFS_I(inode)->orphan_meta_reserved = 0;
2290 spin_unlock(&root->orphan_lock);
2292 if (trans && delete_item) {
2293 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2298 btrfs_orphan_release_metadata(inode);
2304 * this cleans up any orphans that may be left on the list from the last use
2307 int btrfs_orphan_cleanup(struct btrfs_root *root)
2309 struct btrfs_path *path;
2310 struct extent_buffer *leaf;
2311 struct btrfs_key key, found_key;
2312 struct btrfs_trans_handle *trans;
2313 struct inode *inode;
2314 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2316 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2319 path = btrfs_alloc_path();
2326 key.objectid = BTRFS_ORPHAN_OBJECTID;
2327 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2328 key.offset = (u64)-1;
2331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2336 * if ret == 0 means we found what we were searching for, which
2337 * is weird, but possible, so only screw with path if we didnt
2338 * find the key and see if we have stuff that matches
2342 if (path->slots[0] == 0)
2347 /* pull out the item */
2348 leaf = path->nodes[0];
2349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2351 /* make sure the item matches what we want */
2352 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2354 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2357 /* release the path since we're done with it */
2358 btrfs_release_path(root, path);
2361 * this is where we are basically btrfs_lookup, without the
2362 * crossing root thing. we store the inode number in the
2363 * offset of the orphan item.
2365 found_key.objectid = found_key.offset;
2366 found_key.type = BTRFS_INODE_ITEM_KEY;
2367 found_key.offset = 0;
2368 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2369 if (IS_ERR(inode)) {
2370 ret = PTR_ERR(inode);
2375 * add this inode to the orphan list so btrfs_orphan_del does
2376 * the proper thing when we hit it
2378 spin_lock(&root->orphan_lock);
2379 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2380 spin_unlock(&root->orphan_lock);
2383 * if this is a bad inode, means we actually succeeded in
2384 * removing the inode, but not the orphan record, which means
2385 * we need to manually delete the orphan since iput will just
2386 * do a destroy_inode
2388 if (is_bad_inode(inode)) {
2389 trans = btrfs_start_transaction(root, 0);
2390 if (IS_ERR(trans)) {
2391 ret = PTR_ERR(trans);
2394 btrfs_orphan_del(trans, inode);
2395 btrfs_end_transaction(trans, root);
2400 /* if we have links, this was a truncate, lets do that */
2401 if (inode->i_nlink) {
2402 if (!S_ISREG(inode->i_mode)) {
2408 ret = btrfs_truncate(inode);
2413 /* this will do delete_inode and everything for us */
2418 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2420 if (root->orphan_block_rsv)
2421 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2424 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2425 trans = btrfs_join_transaction(root, 1);
2427 btrfs_end_transaction(trans, root);
2431 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2433 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2437 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2438 btrfs_free_path(path);
2443 * very simple check to peek ahead in the leaf looking for xattrs. If we
2444 * don't find any xattrs, we know there can't be any acls.
2446 * slot is the slot the inode is in, objectid is the objectid of the inode
2448 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2449 int slot, u64 objectid)
2451 u32 nritems = btrfs_header_nritems(leaf);
2452 struct btrfs_key found_key;
2456 while (slot < nritems) {
2457 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2459 /* we found a different objectid, there must not be acls */
2460 if (found_key.objectid != objectid)
2463 /* we found an xattr, assume we've got an acl */
2464 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2468 * we found a key greater than an xattr key, there can't
2469 * be any acls later on
2471 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2478 * it goes inode, inode backrefs, xattrs, extents,
2479 * so if there are a ton of hard links to an inode there can
2480 * be a lot of backrefs. Don't waste time searching too hard,
2481 * this is just an optimization
2486 /* we hit the end of the leaf before we found an xattr or
2487 * something larger than an xattr. We have to assume the inode
2494 * read an inode from the btree into the in-memory inode
2496 static void btrfs_read_locked_inode(struct inode *inode)
2498 struct btrfs_path *path;
2499 struct extent_buffer *leaf;
2500 struct btrfs_inode_item *inode_item;
2501 struct btrfs_timespec *tspec;
2502 struct btrfs_root *root = BTRFS_I(inode)->root;
2503 struct btrfs_key location;
2505 u64 alloc_group_block;
2509 path = btrfs_alloc_path();
2511 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2513 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2517 leaf = path->nodes[0];
2518 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2519 struct btrfs_inode_item);
2521 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2522 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2523 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2524 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2525 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2527 tspec = btrfs_inode_atime(inode_item);
2528 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2529 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2531 tspec = btrfs_inode_mtime(inode_item);
2532 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2533 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2535 tspec = btrfs_inode_ctime(inode_item);
2536 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2537 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2539 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2540 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2541 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2542 inode->i_generation = BTRFS_I(inode)->generation;
2544 rdev = btrfs_inode_rdev(leaf, inode_item);
2546 BTRFS_I(inode)->index_cnt = (u64)-1;
2547 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2549 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2552 * try to precache a NULL acl entry for files that don't have
2553 * any xattrs or acls
2555 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2558 cache_no_acl(inode);
2560 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2561 alloc_group_block, 0);
2562 btrfs_free_path(path);
2565 switch (inode->i_mode & S_IFMT) {
2567 inode->i_mapping->a_ops = &btrfs_aops;
2568 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2569 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2570 inode->i_fop = &btrfs_file_operations;
2571 inode->i_op = &btrfs_file_inode_operations;
2574 inode->i_fop = &btrfs_dir_file_operations;
2575 if (root == root->fs_info->tree_root)
2576 inode->i_op = &btrfs_dir_ro_inode_operations;
2578 inode->i_op = &btrfs_dir_inode_operations;
2581 inode->i_op = &btrfs_symlink_inode_operations;
2582 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2583 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2586 inode->i_op = &btrfs_special_inode_operations;
2587 init_special_inode(inode, inode->i_mode, rdev);
2591 btrfs_update_iflags(inode);
2595 btrfs_free_path(path);
2596 make_bad_inode(inode);
2600 * given a leaf and an inode, copy the inode fields into the leaf
2602 static void fill_inode_item(struct btrfs_trans_handle *trans,
2603 struct extent_buffer *leaf,
2604 struct btrfs_inode_item *item,
2605 struct inode *inode)
2607 if (!leaf->map_token)
2608 map_private_extent_buffer(leaf, (unsigned long)item,
2609 sizeof(struct btrfs_inode_item),
2610 &leaf->map_token, &leaf->kaddr,
2611 &leaf->map_start, &leaf->map_len,
2614 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2615 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2616 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2617 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2618 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2620 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2621 inode->i_atime.tv_sec);
2622 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2623 inode->i_atime.tv_nsec);
2625 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2626 inode->i_mtime.tv_sec);
2627 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2628 inode->i_mtime.tv_nsec);
2630 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2631 inode->i_ctime.tv_sec);
2632 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2633 inode->i_ctime.tv_nsec);
2635 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2636 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2637 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2638 btrfs_set_inode_transid(leaf, item, trans->transid);
2639 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2640 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2641 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2643 if (leaf->map_token) {
2644 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2645 leaf->map_token = NULL;
2650 * copy everything in the in-memory inode into the btree.
2652 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2653 struct btrfs_root *root, struct inode *inode)
2655 struct btrfs_inode_item *inode_item;
2656 struct btrfs_path *path;
2657 struct extent_buffer *leaf;
2660 path = btrfs_alloc_path();
2662 path->leave_spinning = 1;
2663 ret = btrfs_lookup_inode(trans, root, path,
2664 &BTRFS_I(inode)->location, 1);
2671 btrfs_unlock_up_safe(path, 1);
2672 leaf = path->nodes[0];
2673 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2674 struct btrfs_inode_item);
2676 fill_inode_item(trans, leaf, inode_item, inode);
2677 btrfs_mark_buffer_dirty(leaf);
2678 btrfs_set_inode_last_trans(trans, inode);
2681 btrfs_free_path(path);
2687 * unlink helper that gets used here in inode.c and in the tree logging
2688 * recovery code. It remove a link in a directory with a given name, and
2689 * also drops the back refs in the inode to the directory
2691 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root,
2693 struct inode *dir, struct inode *inode,
2694 const char *name, int name_len)
2696 struct btrfs_path *path;
2698 struct extent_buffer *leaf;
2699 struct btrfs_dir_item *di;
2700 struct btrfs_key key;
2702 u64 ino = btrfs_ino(inode);
2703 u64 dir_ino = btrfs_ino(dir);
2705 path = btrfs_alloc_path();
2711 path->leave_spinning = 1;
2712 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2713 name, name_len, -1);
2722 leaf = path->nodes[0];
2723 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2724 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2727 btrfs_release_path(root, path);
2729 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2732 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2733 "inode %llu parent %llu\n", name_len, name,
2734 (unsigned long long)ino, (unsigned long long)dir_ino);
2738 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
2739 index, name, name_len, -1);
2748 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2749 btrfs_release_path(root, path);
2751 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2753 BUG_ON(ret != 0 && ret != -ENOENT);
2755 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2760 btrfs_free_path(path);
2764 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2765 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2766 btrfs_update_inode(trans, root, dir);
2771 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root,
2773 struct inode *dir, struct inode *inode,
2774 const char *name, int name_len)
2777 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2779 btrfs_drop_nlink(inode);
2780 ret = btrfs_update_inode(trans, root, inode);
2786 /* helper to check if there is any shared block in the path */
2787 static int check_path_shared(struct btrfs_root *root,
2788 struct btrfs_path *path)
2790 struct extent_buffer *eb;
2794 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2797 if (!path->nodes[level])
2799 eb = path->nodes[level];
2800 if (!btrfs_block_can_be_shared(root, eb))
2802 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2811 * helper to start transaction for unlink and rmdir.
2813 * unlink and rmdir are special in btrfs, they do not always free space.
2814 * so in enospc case, we should make sure they will free space before
2815 * allowing them to use the global metadata reservation.
2817 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2818 struct dentry *dentry)
2820 struct btrfs_trans_handle *trans;
2821 struct btrfs_root *root = BTRFS_I(dir)->root;
2822 struct btrfs_path *path;
2823 struct btrfs_inode_ref *ref;
2824 struct btrfs_dir_item *di;
2825 struct inode *inode = dentry->d_inode;
2830 u64 ino = btrfs_ino(inode);
2831 u64 dir_ino = btrfs_ino(dir);
2833 trans = btrfs_start_transaction(root, 10);
2834 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2837 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2838 return ERR_PTR(-ENOSPC);
2840 /* check if there is someone else holds reference */
2841 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2842 return ERR_PTR(-ENOSPC);
2844 if (atomic_read(&inode->i_count) > 2)
2845 return ERR_PTR(-ENOSPC);
2847 if (xchg(&root->fs_info->enospc_unlink, 1))
2848 return ERR_PTR(-ENOSPC);
2850 path = btrfs_alloc_path();
2852 root->fs_info->enospc_unlink = 0;
2853 return ERR_PTR(-ENOMEM);
2856 trans = btrfs_start_transaction(root, 0);
2857 if (IS_ERR(trans)) {
2858 btrfs_free_path(path);
2859 root->fs_info->enospc_unlink = 0;
2863 path->skip_locking = 1;
2864 path->search_commit_root = 1;
2866 ret = btrfs_lookup_inode(trans, root, path,
2867 &BTRFS_I(dir)->location, 0);
2873 if (check_path_shared(root, path))
2878 btrfs_release_path(root, path);
2880 ret = btrfs_lookup_inode(trans, root, path,
2881 &BTRFS_I(inode)->location, 0);
2887 if (check_path_shared(root, path))
2892 btrfs_release_path(root, path);
2894 if (ret == 0 && S_ISREG(inode->i_mode)) {
2895 ret = btrfs_lookup_file_extent(trans, root, path,
2902 if (check_path_shared(root, path))
2904 btrfs_release_path(root, path);
2912 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2913 dentry->d_name.name, dentry->d_name.len, 0);
2919 if (check_path_shared(root, path))
2925 btrfs_release_path(root, path);
2927 ref = btrfs_lookup_inode_ref(trans, root, path,
2928 dentry->d_name.name, dentry->d_name.len,
2935 if (check_path_shared(root, path))
2937 index = btrfs_inode_ref_index(path->nodes[0], ref);
2938 btrfs_release_path(root, path);
2940 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2941 dentry->d_name.name, dentry->d_name.len, 0);
2946 BUG_ON(ret == -ENOENT);
2947 if (check_path_shared(root, path))
2952 btrfs_free_path(path);
2954 btrfs_end_transaction(trans, root);
2955 root->fs_info->enospc_unlink = 0;
2956 return ERR_PTR(err);
2959 trans->block_rsv = &root->fs_info->global_block_rsv;
2963 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2964 struct btrfs_root *root)
2966 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2967 BUG_ON(!root->fs_info->enospc_unlink);
2968 root->fs_info->enospc_unlink = 0;
2970 btrfs_end_transaction_throttle(trans, root);
2973 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2975 struct btrfs_root *root = BTRFS_I(dir)->root;
2976 struct btrfs_trans_handle *trans;
2977 struct inode *inode = dentry->d_inode;
2979 unsigned long nr = 0;
2981 trans = __unlink_start_trans(dir, dentry);
2983 return PTR_ERR(trans);
2985 btrfs_set_trans_block_group(trans, dir);
2987 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2989 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2990 dentry->d_name.name, dentry->d_name.len);
2993 if (inode->i_nlink == 0) {
2994 ret = btrfs_orphan_add(trans, inode);
2998 nr = trans->blocks_used;
2999 __unlink_end_trans(trans, root);
3000 btrfs_btree_balance_dirty(root, nr);
3004 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3005 struct btrfs_root *root,
3006 struct inode *dir, u64 objectid,
3007 const char *name, int name_len)
3009 struct btrfs_path *path;
3010 struct extent_buffer *leaf;
3011 struct btrfs_dir_item *di;
3012 struct btrfs_key key;
3015 u64 dir_ino = btrfs_ino(dir);
3017 path = btrfs_alloc_path();
3021 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3022 name, name_len, -1);
3023 BUG_ON(!di || IS_ERR(di));
3025 leaf = path->nodes[0];
3026 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3027 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3028 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3030 btrfs_release_path(root, path);
3032 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3033 objectid, root->root_key.objectid,
3034 dir_ino, &index, name, name_len);
3036 BUG_ON(ret != -ENOENT);
3037 di = btrfs_search_dir_index_item(root, path, dir_ino,
3039 BUG_ON(!di || IS_ERR(di));
3041 leaf = path->nodes[0];
3042 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3043 btrfs_release_path(root, path);
3047 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino,
3048 index, name, name_len, -1);
3049 BUG_ON(!di || IS_ERR(di));
3051 leaf = path->nodes[0];
3052 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3053 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3054 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3056 btrfs_release_path(root, path);
3058 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3059 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3060 ret = btrfs_update_inode(trans, root, dir);
3063 btrfs_free_path(path);
3067 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3069 struct inode *inode = dentry->d_inode;
3071 struct btrfs_root *root = BTRFS_I(dir)->root;
3072 struct btrfs_trans_handle *trans;
3073 unsigned long nr = 0;
3075 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3076 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3079 trans = __unlink_start_trans(dir, dentry);
3081 return PTR_ERR(trans);
3083 btrfs_set_trans_block_group(trans, dir);
3085 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3086 err = btrfs_unlink_subvol(trans, root, dir,
3087 BTRFS_I(inode)->location.objectid,
3088 dentry->d_name.name,
3089 dentry->d_name.len);
3093 err = btrfs_orphan_add(trans, inode);
3097 /* now the directory is empty */
3098 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3099 dentry->d_name.name, dentry->d_name.len);
3101 btrfs_i_size_write(inode, 0);
3103 nr = trans->blocks_used;
3104 __unlink_end_trans(trans, root);
3105 btrfs_btree_balance_dirty(root, nr);
3112 * when truncating bytes in a file, it is possible to avoid reading
3113 * the leaves that contain only checksum items. This can be the
3114 * majority of the IO required to delete a large file, but it must
3115 * be done carefully.
3117 * The keys in the level just above the leaves are checked to make sure
3118 * the lowest key in a given leaf is a csum key, and starts at an offset
3119 * after the new size.
3121 * Then the key for the next leaf is checked to make sure it also has
3122 * a checksum item for the same file. If it does, we know our target leaf
3123 * contains only checksum items, and it can be safely freed without reading
3126 * This is just an optimization targeted at large files. It may do
3127 * nothing. It will return 0 unless things went badly.
3129 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3130 struct btrfs_root *root,
3131 struct btrfs_path *path,
3132 struct inode *inode, u64 new_size)
3134 struct btrfs_key key;
3137 struct btrfs_key found_key;
3138 struct btrfs_key other_key;
3139 struct btrfs_leaf_ref *ref;
3143 path->lowest_level = 1;
3144 key.objectid = inode->i_ino;
3145 key.type = BTRFS_CSUM_ITEM_KEY;
3146 key.offset = new_size;
3148 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3152 if (path->nodes[1] == NULL) {
3157 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3158 nritems = btrfs_header_nritems(path->nodes[1]);
3163 if (path->slots[1] >= nritems)
3166 /* did we find a key greater than anything we want to delete? */
3167 if (found_key.objectid > inode->i_ino ||
3168 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3171 /* we check the next key in the node to make sure the leave contains
3172 * only checksum items. This comparison doesn't work if our
3173 * leaf is the last one in the node
3175 if (path->slots[1] + 1 >= nritems) {
3177 /* search forward from the last key in the node, this
3178 * will bring us into the next node in the tree
3180 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3182 /* unlikely, but we inc below, so check to be safe */
3183 if (found_key.offset == (u64)-1)
3186 /* search_forward needs a path with locks held, do the
3187 * search again for the original key. It is possible
3188 * this will race with a balance and return a path that
3189 * we could modify, but this drop is just an optimization
3190 * and is allowed to miss some leaves.
3192 btrfs_release_path(root, path);
3195 /* setup a max key for search_forward */
3196 other_key.offset = (u64)-1;
3197 other_key.type = key.type;
3198 other_key.objectid = key.objectid;
3200 path->keep_locks = 1;
3201 ret = btrfs_search_forward(root, &found_key, &other_key,
3203 path->keep_locks = 0;
3204 if (ret || found_key.objectid != key.objectid ||
3205 found_key.type != key.type) {
3210 key.offset = found_key.offset;
3211 btrfs_release_path(root, path);
3216 /* we know there's one more slot after us in the tree,
3217 * read that key so we can verify it is also a checksum item
3219 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3221 if (found_key.objectid < inode->i_ino)
3224 if (found_key.type != key.type || found_key.offset < new_size)
3228 * if the key for the next leaf isn't a csum key from this objectid,
3229 * we can't be sure there aren't good items inside this leaf.
3232 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3235 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3236 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3238 * it is safe to delete this leaf, it contains only
3239 * csum items from this inode at an offset >= new_size
3241 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3244 if (root->ref_cows && leaf_gen < trans->transid) {
3245 ref = btrfs_alloc_leaf_ref(root, 0);
3247 ref->root_gen = root->root_key.offset;
3248 ref->bytenr = leaf_start;
3250 ref->generation = leaf_gen;
3253 btrfs_sort_leaf_ref(ref);
3255 ret = btrfs_add_leaf_ref(root, ref, 0);
3257 btrfs_free_leaf_ref(root, ref);
3263 btrfs_release_path(root, path);
3265 if (other_key.objectid == inode->i_ino &&
3266 other_key.type == key.type && other_key.offset > key.offset) {
3267 key.offset = other_key.offset;
3273 /* fixup any changes we've made to the path */
3274 path->lowest_level = 0;
3275 path->keep_locks = 0;
3276 btrfs_release_path(root, path);
3283 * this can truncate away extent items, csum items and directory items.
3284 * It starts at a high offset and removes keys until it can't find
3285 * any higher than new_size
3287 * csum items that cross the new i_size are truncated to the new size
3290 * min_type is the minimum key type to truncate down to. If set to 0, this
3291 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3293 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3294 struct btrfs_root *root,
3295 struct inode *inode,
3296 u64 new_size, u32 min_type)
3298 struct btrfs_path *path;
3299 struct extent_buffer *leaf;
3300 struct btrfs_file_extent_item *fi;
3301 struct btrfs_key key;
3302 struct btrfs_key found_key;
3303 u64 extent_start = 0;
3304 u64 extent_num_bytes = 0;
3305 u64 extent_offset = 0;
3307 u64 mask = root->sectorsize - 1;
3308 u32 found_type = (u8)-1;
3311 int pending_del_nr = 0;
3312 int pending_del_slot = 0;
3313 int extent_type = -1;
3317 u64 ino = btrfs_ino(inode);
3319 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3321 if (root->ref_cows || root == root->fs_info->tree_root)
3322 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3324 path = btrfs_alloc_path();
3329 key.offset = (u64)-1;
3333 path->leave_spinning = 1;
3334 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3341 /* there are no items in the tree for us to truncate, we're
3344 if (path->slots[0] == 0)
3351 leaf = path->nodes[0];
3352 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3353 found_type = btrfs_key_type(&found_key);
3356 if (found_key.objectid != ino)
3359 if (found_type < min_type)
3362 item_end = found_key.offset;
3363 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3364 fi = btrfs_item_ptr(leaf, path->slots[0],
3365 struct btrfs_file_extent_item);
3366 extent_type = btrfs_file_extent_type(leaf, fi);
3367 encoding = btrfs_file_extent_compression(leaf, fi);
3368 encoding |= btrfs_file_extent_encryption(leaf, fi);
3369 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3371 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3373 btrfs_file_extent_num_bytes(leaf, fi);
3374 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3375 item_end += btrfs_file_extent_inline_len(leaf,
3380 if (found_type > min_type) {
3383 if (item_end < new_size)
3385 if (found_key.offset >= new_size)
3391 /* FIXME, shrink the extent if the ref count is only 1 */
3392 if (found_type != BTRFS_EXTENT_DATA_KEY)
3395 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3397 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3398 if (!del_item && !encoding) {
3399 u64 orig_num_bytes =
3400 btrfs_file_extent_num_bytes(leaf, fi);
3401 extent_num_bytes = new_size -
3402 found_key.offset + root->sectorsize - 1;
3403 extent_num_bytes = extent_num_bytes &
3404 ~((u64)root->sectorsize - 1);
3405 btrfs_set_file_extent_num_bytes(leaf, fi,
3407 num_dec = (orig_num_bytes -
3409 if (root->ref_cows && extent_start != 0)
3410 inode_sub_bytes(inode, num_dec);
3411 btrfs_mark_buffer_dirty(leaf);
3414 btrfs_file_extent_disk_num_bytes(leaf,
3416 extent_offset = found_key.offset -
3417 btrfs_file_extent_offset(leaf, fi);
3419 /* FIXME blocksize != 4096 */
3420 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3421 if (extent_start != 0) {
3424 inode_sub_bytes(inode, num_dec);
3427 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3429 * we can't truncate inline items that have had
3433 btrfs_file_extent_compression(leaf, fi) == 0 &&
3434 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3435 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3436 u32 size = new_size - found_key.offset;
3438 if (root->ref_cows) {
3439 inode_sub_bytes(inode, item_end + 1 -
3443 btrfs_file_extent_calc_inline_size(size);
3444 ret = btrfs_truncate_item(trans, root, path,
3447 } else if (root->ref_cows) {
3448 inode_sub_bytes(inode, item_end + 1 -
3454 if (!pending_del_nr) {
3455 /* no pending yet, add ourselves */
3456 pending_del_slot = path->slots[0];
3458 } else if (pending_del_nr &&
3459 path->slots[0] + 1 == pending_del_slot) {
3460 /* hop on the pending chunk */
3462 pending_del_slot = path->slots[0];
3469 if (found_extent && (root->ref_cows ||
3470 root == root->fs_info->tree_root)) {
3471 btrfs_set_path_blocking(path);
3472 ret = btrfs_free_extent(trans, root, extent_start,
3473 extent_num_bytes, 0,
3474 btrfs_header_owner(leaf),
3475 ino, extent_offset);
3479 if (found_type == BTRFS_INODE_ITEM_KEY)
3482 if (path->slots[0] == 0 ||
3483 path->slots[0] != pending_del_slot) {
3484 if (root->ref_cows &&
3485 BTRFS_I(inode)->location.objectid !=
3486 BTRFS_FREE_INO_OBJECTID) {
3490 if (pending_del_nr) {
3491 ret = btrfs_del_items(trans, root, path,
3497 btrfs_release_path(root, path);
3504 if (pending_del_nr) {
3505 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3509 btrfs_free_path(path);
3514 * taken from block_truncate_page, but does cow as it zeros out
3515 * any bytes left in the last page in the file.
3517 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3519 struct inode *inode = mapping->host;
3520 struct btrfs_root *root = BTRFS_I(inode)->root;
3521 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3522 struct btrfs_ordered_extent *ordered;
3523 struct extent_state *cached_state = NULL;
3525 u32 blocksize = root->sectorsize;
3526 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3527 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3533 if ((offset & (blocksize - 1)) == 0)
3535 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
3541 page = grab_cache_page(mapping, index);
3543 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3547 page_start = page_offset(page);
3548 page_end = page_start + PAGE_CACHE_SIZE - 1;
3550 if (!PageUptodate(page)) {
3551 ret = btrfs_readpage(NULL, page);
3553 if (page->mapping != mapping) {
3555 page_cache_release(page);
3558 if (!PageUptodate(page)) {
3563 wait_on_page_writeback(page);
3565 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3567 set_page_extent_mapped(page);
3569 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3571 unlock_extent_cached(io_tree, page_start, page_end,
3572 &cached_state, GFP_NOFS);
3574 page_cache_release(page);
3575 btrfs_start_ordered_extent(inode, ordered, 1);
3576 btrfs_put_ordered_extent(ordered);
3580 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3581 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3582 0, 0, &cached_state, GFP_NOFS);
3584 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3587 unlock_extent_cached(io_tree, page_start, page_end,
3588 &cached_state, GFP_NOFS);
3593 if (offset != PAGE_CACHE_SIZE) {
3595 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3596 flush_dcache_page(page);
3599 ClearPageChecked(page);
3600 set_page_dirty(page);
3601 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3606 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3608 page_cache_release(page);
3614 * This function puts in dummy file extents for the area we're creating a hole
3615 * for. So if we are truncating this file to a larger size we need to insert
3616 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3617 * the range between oldsize and size
3619 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3621 struct btrfs_trans_handle *trans;
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3624 struct extent_map *em = NULL;
3625 struct extent_state *cached_state = NULL;
3626 u64 mask = root->sectorsize - 1;
3627 u64 hole_start = (oldsize + mask) & ~mask;
3628 u64 block_end = (size + mask) & ~mask;
3634 if (size <= hole_start)
3638 struct btrfs_ordered_extent *ordered;
3639 btrfs_wait_ordered_range(inode, hole_start,
3640 block_end - hole_start);
3641 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3642 &cached_state, GFP_NOFS);
3643 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3646 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3647 &cached_state, GFP_NOFS);
3648 btrfs_put_ordered_extent(ordered);
3651 cur_offset = hole_start;
3653 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3654 block_end - cur_offset, 0);
3655 BUG_ON(IS_ERR(em) || !em);
3656 last_byte = min(extent_map_end(em), block_end);
3657 last_byte = (last_byte + mask) & ~mask;
3658 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3660 hole_size = last_byte - cur_offset;
3662 trans = btrfs_start_transaction(root, 2);
3663 if (IS_ERR(trans)) {
3664 err = PTR_ERR(trans);
3667 btrfs_set_trans_block_group(trans, inode);
3669 err = btrfs_drop_extents(trans, inode, cur_offset,
3670 cur_offset + hole_size,
3675 err = btrfs_insert_file_extent(trans, root,
3676 btrfs_ino(inode), cur_offset, 0,
3677 0, hole_size, 0, hole_size,
3682 btrfs_drop_extent_cache(inode, hole_start,
3685 btrfs_end_transaction(trans, root);
3687 free_extent_map(em);
3689 cur_offset = last_byte;
3690 if (cur_offset >= block_end)
3694 free_extent_map(em);
3695 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3700 static int btrfs_setsize(struct inode *inode, loff_t newsize)
3702 loff_t oldsize = i_size_read(inode);
3705 if (newsize == oldsize)
3708 if (newsize > oldsize) {
3709 i_size_write(inode, newsize);
3710 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3711 truncate_pagecache(inode, oldsize, newsize);
3712 ret = btrfs_cont_expand(inode, oldsize, newsize);
3714 btrfs_setsize(inode, oldsize);
3718 mark_inode_dirty(inode);
3722 * We're truncating a file that used to have good data down to
3723 * zero. Make sure it gets into the ordered flush list so that
3724 * any new writes get down to disk quickly.
3727 BTRFS_I(inode)->ordered_data_close = 1;
3729 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3730 truncate_setsize(inode, newsize);
3731 ret = btrfs_truncate(inode);
3737 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3739 struct inode *inode = dentry->d_inode;
3740 struct btrfs_root *root = BTRFS_I(inode)->root;
3743 if (btrfs_root_readonly(root))
3746 err = inode_change_ok(inode, attr);
3750 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3751 err = btrfs_setsize(inode, attr->ia_size);
3756 if (attr->ia_valid) {
3757 setattr_copy(inode, attr);
3758 mark_inode_dirty(inode);
3760 if (attr->ia_valid & ATTR_MODE)
3761 err = btrfs_acl_chmod(inode);
3767 void btrfs_evict_inode(struct inode *inode)
3769 struct btrfs_trans_handle *trans;
3770 struct btrfs_root *root = BTRFS_I(inode)->root;
3774 trace_btrfs_inode_evict(inode);
3776 truncate_inode_pages(&inode->i_data, 0);
3777 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3778 is_free_space_inode(root, inode)))
3781 if (is_bad_inode(inode)) {
3782 btrfs_orphan_del(NULL, inode);
3785 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3786 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3788 if (root->fs_info->log_root_recovering) {
3789 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3793 if (inode->i_nlink > 0) {
3794 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3798 btrfs_i_size_write(inode, 0);
3801 trans = btrfs_start_transaction(root, 0);
3802 BUG_ON(IS_ERR(trans));
3803 btrfs_set_trans_block_group(trans, inode);
3804 trans->block_rsv = root->orphan_block_rsv;
3806 ret = btrfs_block_rsv_check(trans, root,
3807 root->orphan_block_rsv, 0, 5);
3809 BUG_ON(ret != -EAGAIN);
3810 ret = btrfs_commit_transaction(trans, root);
3815 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3819 nr = trans->blocks_used;
3820 btrfs_end_transaction(trans, root);
3822 btrfs_btree_balance_dirty(root, nr);
3827 ret = btrfs_orphan_del(trans, inode);
3831 if (!(root == root->fs_info->tree_root ||
3832 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3833 btrfs_return_ino(root, btrfs_ino(inode));
3835 nr = trans->blocks_used;
3836 btrfs_end_transaction(trans, root);
3837 btrfs_btree_balance_dirty(root, nr);
3839 end_writeback(inode);
3844 * this returns the key found in the dir entry in the location pointer.
3845 * If no dir entries were found, location->objectid is 0.
3847 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3848 struct btrfs_key *location)
3850 const char *name = dentry->d_name.name;
3851 int namelen = dentry->d_name.len;
3852 struct btrfs_dir_item *di;
3853 struct btrfs_path *path;
3854 struct btrfs_root *root = BTRFS_I(dir)->root;
3857 path = btrfs_alloc_path();
3860 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3865 if (!di || IS_ERR(di))
3868 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3870 btrfs_free_path(path);
3873 location->objectid = 0;
3878 * when we hit a tree root in a directory, the btrfs part of the inode
3879 * needs to be changed to reflect the root directory of the tree root. This
3880 * is kind of like crossing a mount point.
3882 static int fixup_tree_root_location(struct btrfs_root *root,
3884 struct dentry *dentry,
3885 struct btrfs_key *location,
3886 struct btrfs_root **sub_root)
3888 struct btrfs_path *path;
3889 struct btrfs_root *new_root;
3890 struct btrfs_root_ref *ref;
3891 struct extent_buffer *leaf;
3895 path = btrfs_alloc_path();
3902 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3903 BTRFS_I(dir)->root->root_key.objectid,
3904 location->objectid);
3911 leaf = path->nodes[0];
3912 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3913 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3914 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3917 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3918 (unsigned long)(ref + 1),
3919 dentry->d_name.len);
3923 btrfs_release_path(root->fs_info->tree_root, path);
3925 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3926 if (IS_ERR(new_root)) {
3927 err = PTR_ERR(new_root);
3931 if (btrfs_root_refs(&new_root->root_item) == 0) {
3936 *sub_root = new_root;
3937 location->objectid = btrfs_root_dirid(&new_root->root_item);
3938 location->type = BTRFS_INODE_ITEM_KEY;
3939 location->offset = 0;
3942 btrfs_free_path(path);
3946 static void inode_tree_add(struct inode *inode)
3948 struct btrfs_root *root = BTRFS_I(inode)->root;
3949 struct btrfs_inode *entry;
3951 struct rb_node *parent;
3952 u64 ino = btrfs_ino(inode);
3954 p = &root->inode_tree.rb_node;
3957 if (inode_unhashed(inode))
3960 spin_lock(&root->inode_lock);
3963 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3965 if (ino < btrfs_ino(&entry->vfs_inode))
3966 p = &parent->rb_left;
3967 else if (ino > btrfs_ino(&entry->vfs_inode))
3968 p = &parent->rb_right;
3970 WARN_ON(!(entry->vfs_inode.i_state &
3971 (I_WILL_FREE | I_FREEING)));
3972 rb_erase(parent, &root->inode_tree);
3973 RB_CLEAR_NODE(parent);
3974 spin_unlock(&root->inode_lock);
3978 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3979 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3980 spin_unlock(&root->inode_lock);
3983 static void inode_tree_del(struct inode *inode)
3985 struct btrfs_root *root = BTRFS_I(inode)->root;
3988 spin_lock(&root->inode_lock);
3989 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3990 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3991 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3992 empty = RB_EMPTY_ROOT(&root->inode_tree);
3994 spin_unlock(&root->inode_lock);
3997 * Free space cache has inodes in the tree root, but the tree root has a
3998 * root_refs of 0, so this could end up dropping the tree root as a
3999 * snapshot, so we need the extra !root->fs_info->tree_root check to
4000 * make sure we don't drop it.
4002 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4003 root != root->fs_info->tree_root) {
4004 synchronize_srcu(&root->fs_info->subvol_srcu);
4005 spin_lock(&root->inode_lock);
4006 empty = RB_EMPTY_ROOT(&root->inode_tree);
4007 spin_unlock(&root->inode_lock);
4009 btrfs_add_dead_root(root);
4013 int btrfs_invalidate_inodes(struct btrfs_root *root)
4015 struct rb_node *node;
4016 struct rb_node *prev;
4017 struct btrfs_inode *entry;
4018 struct inode *inode;
4021 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4023 spin_lock(&root->inode_lock);
4025 node = root->inode_tree.rb_node;
4029 entry = rb_entry(node, struct btrfs_inode, rb_node);
4031 if (objectid < btrfs_ino(&entry->vfs_inode))
4032 node = node->rb_left;
4033 else if (objectid > btrfs_ino(&entry->vfs_inode))
4034 node = node->rb_right;
4040 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4041 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4045 prev = rb_next(prev);
4049 entry = rb_entry(node, struct btrfs_inode, rb_node);
4050 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4051 inode = igrab(&entry->vfs_inode);
4053 spin_unlock(&root->inode_lock);
4054 if (atomic_read(&inode->i_count) > 1)
4055 d_prune_aliases(inode);
4057 * btrfs_drop_inode will have it removed from
4058 * the inode cache when its usage count
4063 spin_lock(&root->inode_lock);
4067 if (cond_resched_lock(&root->inode_lock))
4070 node = rb_next(node);
4072 spin_unlock(&root->inode_lock);
4076 static int btrfs_init_locked_inode(struct inode *inode, void *p)
4078 struct btrfs_iget_args *args = p;
4079 inode->i_ino = args->ino;
4080 BTRFS_I(inode)->root = args->root;
4081 btrfs_set_inode_space_info(args->root, inode);
4085 static int btrfs_find_actor(struct inode *inode, void *opaque)
4087 struct btrfs_iget_args *args = opaque;
4088 return args->ino == btrfs_ino(inode) &&
4089 args->root == BTRFS_I(inode)->root;
4092 static struct inode *btrfs_iget_locked(struct super_block *s,
4094 struct btrfs_root *root)
4096 struct inode *inode;
4097 struct btrfs_iget_args args;
4098 args.ino = objectid;
4101 inode = iget5_locked(s, objectid, btrfs_find_actor,
4102 btrfs_init_locked_inode,
4107 /* Get an inode object given its location and corresponding root.
4108 * Returns in *is_new if the inode was read from disk
4110 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4111 struct btrfs_root *root, int *new)
4113 struct inode *inode;
4115 inode = btrfs_iget_locked(s, location->objectid, root);
4117 return ERR_PTR(-ENOMEM);
4119 if (inode->i_state & I_NEW) {
4120 BTRFS_I(inode)->root = root;
4121 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4122 btrfs_read_locked_inode(inode);
4123 inode_tree_add(inode);
4124 unlock_new_inode(inode);
4132 static struct inode *new_simple_dir(struct super_block *s,
4133 struct btrfs_key *key,
4134 struct btrfs_root *root)
4136 struct inode *inode = new_inode(s);
4139 return ERR_PTR(-ENOMEM);
4141 BTRFS_I(inode)->root = root;
4142 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4143 BTRFS_I(inode)->dummy_inode = 1;
4145 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4146 inode->i_op = &simple_dir_inode_operations;
4147 inode->i_fop = &simple_dir_operations;
4148 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4149 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4154 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4156 struct inode *inode;
4157 struct btrfs_root *root = BTRFS_I(dir)->root;
4158 struct btrfs_root *sub_root = root;
4159 struct btrfs_key location;
4163 if (dentry->d_name.len > BTRFS_NAME_LEN)
4164 return ERR_PTR(-ENAMETOOLONG);
4166 ret = btrfs_inode_by_name(dir, dentry, &location);
4169 return ERR_PTR(ret);
4171 if (location.objectid == 0)
4174 if (location.type == BTRFS_INODE_ITEM_KEY) {
4175 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4179 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4181 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4182 ret = fixup_tree_root_location(root, dir, dentry,
4183 &location, &sub_root);
4186 inode = ERR_PTR(ret);
4188 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4190 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4192 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4194 if (!IS_ERR(inode) && root != sub_root) {
4195 down_read(&root->fs_info->cleanup_work_sem);
4196 if (!(inode->i_sb->s_flags & MS_RDONLY))
4197 ret = btrfs_orphan_cleanup(sub_root);
4198 up_read(&root->fs_info->cleanup_work_sem);
4200 inode = ERR_PTR(ret);
4206 static int btrfs_dentry_delete(const struct dentry *dentry)
4208 struct btrfs_root *root;
4210 if (!dentry->d_inode && !IS_ROOT(dentry))
4211 dentry = dentry->d_parent;
4213 if (dentry->d_inode) {
4214 root = BTRFS_I(dentry->d_inode)->root;
4215 if (btrfs_root_refs(&root->root_item) == 0)
4221 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4222 struct nameidata *nd)
4224 struct inode *inode;
4226 inode = btrfs_lookup_dentry(dir, dentry);
4228 return ERR_CAST(inode);
4230 return d_splice_alias(inode, dentry);
4233 static unsigned char btrfs_filetype_table[] = {
4234 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4237 static int btrfs_real_readdir(struct file *filp, void *dirent,
4240 struct inode *inode = filp->f_dentry->d_inode;
4241 struct btrfs_root *root = BTRFS_I(inode)->root;
4242 struct btrfs_item *item;
4243 struct btrfs_dir_item *di;
4244 struct btrfs_key key;
4245 struct btrfs_key found_key;
4246 struct btrfs_path *path;
4248 struct extent_buffer *leaf;
4250 unsigned char d_type;
4255 int key_type = BTRFS_DIR_INDEX_KEY;
4260 /* FIXME, use a real flag for deciding about the key type */
4261 if (root->fs_info->tree_root == root)
4262 key_type = BTRFS_DIR_ITEM_KEY;
4264 /* special case for "." */
4265 if (filp->f_pos == 0) {
4266 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4271 /* special case for .., just use the back ref */
4272 if (filp->f_pos == 1) {
4273 u64 pino = parent_ino(filp->f_path.dentry);
4274 over = filldir(dirent, "..", 2,
4280 path = btrfs_alloc_path();
4283 btrfs_set_key_type(&key, key_type);
4284 key.offset = filp->f_pos;
4285 key.objectid = btrfs_ino(inode);
4287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4292 leaf = path->nodes[0];
4293 slot = path->slots[0];
4294 if (slot >= btrfs_header_nritems(leaf)) {
4295 ret = btrfs_next_leaf(root, path);
4303 item = btrfs_item_nr(leaf, slot);
4304 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4306 if (found_key.objectid != key.objectid)
4308 if (btrfs_key_type(&found_key) != key_type)
4310 if (found_key.offset < filp->f_pos)
4313 filp->f_pos = found_key.offset;
4315 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4317 di_total = btrfs_item_size(leaf, item);
4319 while (di_cur < di_total) {
4320 struct btrfs_key location;
4322 if (verify_dir_item(root, leaf, di))
4325 name_len = btrfs_dir_name_len(leaf, di);
4326 if (name_len <= sizeof(tmp_name)) {
4327 name_ptr = tmp_name;
4329 name_ptr = kmalloc(name_len, GFP_NOFS);
4335 read_extent_buffer(leaf, name_ptr,
4336 (unsigned long)(di + 1), name_len);
4338 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4339 btrfs_dir_item_key_to_cpu(leaf, di, &location);
4341 /* is this a reference to our own snapshot? If so
4344 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4345 location.objectid == root->root_key.objectid) {
4349 over = filldir(dirent, name_ptr, name_len,
4350 found_key.offset, location.objectid,
4354 if (name_ptr != tmp_name)
4359 di_len = btrfs_dir_name_len(leaf, di) +
4360 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4362 di = (struct btrfs_dir_item *)((char *)di + di_len);
4368 /* Reached end of directory/root. Bump pos past the last item. */
4369 if (key_type == BTRFS_DIR_INDEX_KEY)
4371 * 32-bit glibc will use getdents64, but then strtol -
4372 * so the last number we can serve is this.
4374 filp->f_pos = 0x7fffffff;
4380 btrfs_free_path(path);
4384 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4386 struct btrfs_root *root = BTRFS_I(inode)->root;
4387 struct btrfs_trans_handle *trans;
4389 bool nolock = false;
4391 if (BTRFS_I(inode)->dummy_inode)
4395 if (root->fs_info->closing && is_free_space_inode(root, inode))
4398 if (wbc->sync_mode == WB_SYNC_ALL) {
4400 trans = btrfs_join_transaction_nolock(root, 1);
4402 trans = btrfs_join_transaction(root, 1);
4404 return PTR_ERR(trans);
4405 btrfs_set_trans_block_group(trans, inode);
4407 ret = btrfs_end_transaction_nolock(trans, root);
4409 ret = btrfs_commit_transaction(trans, root);
4415 * This is somewhat expensive, updating the tree every time the
4416 * inode changes. But, it is most likely to find the inode in cache.
4417 * FIXME, needs more benchmarking...there are no reasons other than performance
4418 * to keep or drop this code.
4420 void btrfs_dirty_inode(struct inode *inode)
4422 struct btrfs_root *root = BTRFS_I(inode)->root;
4423 struct btrfs_trans_handle *trans;
4426 if (BTRFS_I(inode)->dummy_inode)
4429 trans = btrfs_join_transaction(root, 1);
4430 BUG_ON(IS_ERR(trans));
4431 btrfs_set_trans_block_group(trans, inode);
4433 ret = btrfs_update_inode(trans, root, inode);
4434 if (ret && ret == -ENOSPC) {
4435 /* whoops, lets try again with the full transaction */
4436 btrfs_end_transaction(trans, root);
4437 trans = btrfs_start_transaction(root, 1);
4438 if (IS_ERR(trans)) {
4439 if (printk_ratelimit()) {
4440 printk(KERN_ERR "btrfs: fail to "
4441 "dirty inode %llu error %ld\n",
4442 (unsigned long long)btrfs_ino(inode),
4447 btrfs_set_trans_block_group(trans, inode);
4449 ret = btrfs_update_inode(trans, root, inode);
4451 if (printk_ratelimit()) {
4452 printk(KERN_ERR "btrfs: fail to "
4453 "dirty inode %llu error %d\n",
4454 (unsigned long long)btrfs_ino(inode),
4459 btrfs_end_transaction(trans, root);
4463 * find the highest existing sequence number in a directory
4464 * and then set the in-memory index_cnt variable to reflect
4465 * free sequence numbers
4467 static int btrfs_set_inode_index_count(struct inode *inode)
4469 struct btrfs_root *root = BTRFS_I(inode)->root;
4470 struct btrfs_key key, found_key;
4471 struct btrfs_path *path;
4472 struct extent_buffer *leaf;
4475 key.objectid = btrfs_ino(inode);
4476 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4477 key.offset = (u64)-1;
4479 path = btrfs_alloc_path();
4483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4486 /* FIXME: we should be able to handle this */
4492 * MAGIC NUMBER EXPLANATION:
4493 * since we search a directory based on f_pos we have to start at 2
4494 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4495 * else has to start at 2
4497 if (path->slots[0] == 0) {
4498 BTRFS_I(inode)->index_cnt = 2;
4504 leaf = path->nodes[0];
4505 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4507 if (found_key.objectid != btrfs_ino(inode) ||
4508 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4509 BTRFS_I(inode)->index_cnt = 2;
4513 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4515 btrfs_free_path(path);
4520 * helper to find a free sequence number in a given directory. This current
4521 * code is very simple, later versions will do smarter things in the btree
4523 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4527 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4528 ret = btrfs_set_inode_index_count(dir);
4533 *index = BTRFS_I(dir)->index_cnt;
4534 BTRFS_I(dir)->index_cnt++;
4539 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4540 struct btrfs_root *root,
4542 const char *name, int name_len,
4543 u64 ref_objectid, u64 objectid,
4544 u64 alloc_hint, int mode, u64 *index)
4546 struct inode *inode;
4547 struct btrfs_inode_item *inode_item;
4548 struct btrfs_key *location;
4549 struct btrfs_path *path;
4550 struct btrfs_inode_ref *ref;
4551 struct btrfs_key key[2];
4557 path = btrfs_alloc_path();
4560 inode = new_inode(root->fs_info->sb);
4562 btrfs_free_path(path);
4563 return ERR_PTR(-ENOMEM);
4567 * we have to initialize this early, so we can reclaim the inode
4568 * number if we fail afterwards in this function.
4570 inode->i_ino = objectid;
4573 trace_btrfs_inode_request(dir);
4575 ret = btrfs_set_inode_index(dir, index);
4577 btrfs_free_path(path);
4579 return ERR_PTR(ret);
4583 * index_cnt is ignored for everything but a dir,
4584 * btrfs_get_inode_index_count has an explanation for the magic
4587 BTRFS_I(inode)->index_cnt = 2;
4588 BTRFS_I(inode)->root = root;
4589 BTRFS_I(inode)->generation = trans->transid;
4590 inode->i_generation = BTRFS_I(inode)->generation;
4591 btrfs_set_inode_space_info(root, inode);
4597 BTRFS_I(inode)->block_group =
4598 btrfs_find_block_group(root, 0, alloc_hint, owner);
4600 key[0].objectid = objectid;
4601 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4604 key[1].objectid = objectid;
4605 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4606 key[1].offset = ref_objectid;
4608 sizes[0] = sizeof(struct btrfs_inode_item);
4609 sizes[1] = name_len + sizeof(*ref);
4611 path->leave_spinning = 1;
4612 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4616 inode_init_owner(inode, dir, mode);
4617 inode_set_bytes(inode, 0);
4618 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4619 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4620 struct btrfs_inode_item);
4621 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4623 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4624 struct btrfs_inode_ref);
4625 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4626 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4627 ptr = (unsigned long)(ref + 1);
4628 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4630 btrfs_mark_buffer_dirty(path->nodes[0]);
4631 btrfs_free_path(path);
4633 location = &BTRFS_I(inode)->location;
4634 location->objectid = objectid;
4635 location->offset = 0;
4636 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4638 btrfs_inherit_iflags(inode, dir);
4640 if ((mode & S_IFREG)) {
4641 if (btrfs_test_opt(root, NODATASUM))
4642 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4643 if (btrfs_test_opt(root, NODATACOW) ||
4644 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4645 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4648 insert_inode_hash(inode);
4649 inode_tree_add(inode);
4651 trace_btrfs_inode_new(inode);
4656 BTRFS_I(dir)->index_cnt--;
4657 btrfs_free_path(path);
4659 return ERR_PTR(ret);
4662 static inline u8 btrfs_inode_type(struct inode *inode)
4664 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4668 * utility function to add 'inode' into 'parent_inode' with
4669 * a give name and a given sequence number.
4670 * if 'add_backref' is true, also insert a backref from the
4671 * inode to the parent directory.
4673 int btrfs_add_link(struct btrfs_trans_handle *trans,
4674 struct inode *parent_inode, struct inode *inode,
4675 const char *name, int name_len, int add_backref, u64 index)
4678 struct btrfs_key key;
4679 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4680 u64 ino = btrfs_ino(inode);
4681 u64 parent_ino = btrfs_ino(parent_inode);
4683 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4684 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4687 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4691 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4692 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4693 key.objectid, root->root_key.objectid,
4694 parent_ino, index, name, name_len);
4695 } else if (add_backref) {
4696 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4701 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4703 btrfs_inode_type(inode), index);
4706 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4708 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4709 ret = btrfs_update_inode(trans, root, parent_inode);
4714 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4715 struct inode *dir, struct dentry *dentry,
4716 struct inode *inode, int backref, u64 index)
4718 int err = btrfs_add_link(trans, dir, inode,
4719 dentry->d_name.name, dentry->d_name.len,
4722 d_instantiate(dentry, inode);
4730 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4731 int mode, dev_t rdev)
4733 struct btrfs_trans_handle *trans;
4734 struct btrfs_root *root = BTRFS_I(dir)->root;
4735 struct inode *inode = NULL;
4739 unsigned long nr = 0;
4742 if (!new_valid_dev(rdev))
4746 * 2 for inode item and ref
4748 * 1 for xattr if selinux is on
4750 trans = btrfs_start_transaction(root, 5);
4752 return PTR_ERR(trans);
4754 btrfs_set_trans_block_group(trans, dir);
4756 err = btrfs_find_free_ino(root, &objectid);
4760 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4761 dentry->d_name.len, btrfs_ino(dir), objectid,
4762 BTRFS_I(dir)->block_group, mode, &index);
4763 err = PTR_ERR(inode);
4767 err = btrfs_init_inode_security(trans, inode, dir);
4773 btrfs_set_trans_block_group(trans, inode);
4774 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4778 inode->i_op = &btrfs_special_inode_operations;
4779 init_special_inode(inode, inode->i_mode, rdev);
4780 btrfs_update_inode(trans, root, inode);
4782 btrfs_update_inode_block_group(trans, inode);
4783 btrfs_update_inode_block_group(trans, dir);
4785 nr = trans->blocks_used;
4786 btrfs_end_transaction_throttle(trans, root);
4787 btrfs_btree_balance_dirty(root, nr);
4789 inode_dec_link_count(inode);
4795 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4796 int mode, struct nameidata *nd)
4798 struct btrfs_trans_handle *trans;
4799 struct btrfs_root *root = BTRFS_I(dir)->root;
4800 struct inode *inode = NULL;
4803 unsigned long nr = 0;
4808 * 2 for inode item and ref
4810 * 1 for xattr if selinux is on
4812 trans = btrfs_start_transaction(root, 5);
4814 return PTR_ERR(trans);
4816 btrfs_set_trans_block_group(trans, dir);
4818 err = btrfs_find_free_ino(root, &objectid);
4822 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4823 dentry->d_name.len, btrfs_ino(dir), objectid,
4824 BTRFS_I(dir)->block_group, mode, &index);
4825 err = PTR_ERR(inode);
4829 err = btrfs_init_inode_security(trans, inode, dir);
4835 btrfs_set_trans_block_group(trans, inode);
4836 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4840 inode->i_mapping->a_ops = &btrfs_aops;
4841 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4842 inode->i_fop = &btrfs_file_operations;
4843 inode->i_op = &btrfs_file_inode_operations;
4844 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4846 btrfs_update_inode_block_group(trans, inode);
4847 btrfs_update_inode_block_group(trans, dir);
4849 nr = trans->blocks_used;
4850 btrfs_end_transaction_throttle(trans, root);
4852 inode_dec_link_count(inode);
4855 btrfs_btree_balance_dirty(root, nr);
4859 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4860 struct dentry *dentry)
4862 struct btrfs_trans_handle *trans;
4863 struct btrfs_root *root = BTRFS_I(dir)->root;
4864 struct inode *inode = old_dentry->d_inode;
4866 unsigned long nr = 0;
4870 if (inode->i_nlink == 0)
4873 /* do not allow sys_link's with other subvols of the same device */
4874 if (root->objectid != BTRFS_I(inode)->root->objectid)
4877 if (inode->i_nlink == ~0U)
4880 err = btrfs_set_inode_index(dir, &index);
4885 * 2 items for inode and inode ref
4886 * 2 items for dir items
4887 * 1 item for parent inode
4889 trans = btrfs_start_transaction(root, 5);
4890 if (IS_ERR(trans)) {
4891 err = PTR_ERR(trans);
4895 btrfs_inc_nlink(inode);
4896 inode->i_ctime = CURRENT_TIME;
4898 btrfs_set_trans_block_group(trans, dir);
4901 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
4906 struct dentry *parent = dget_parent(dentry);
4907 btrfs_update_inode_block_group(trans, dir);
4908 err = btrfs_update_inode(trans, root, inode);
4910 btrfs_log_new_name(trans, inode, NULL, parent);
4914 nr = trans->blocks_used;
4915 btrfs_end_transaction_throttle(trans, root);
4918 inode_dec_link_count(inode);
4921 btrfs_btree_balance_dirty(root, nr);
4925 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4927 struct inode *inode = NULL;
4928 struct btrfs_trans_handle *trans;
4929 struct btrfs_root *root = BTRFS_I(dir)->root;
4931 int drop_on_err = 0;
4934 unsigned long nr = 1;
4937 * 2 items for inode and ref
4938 * 2 items for dir items
4939 * 1 for xattr if selinux is on
4941 trans = btrfs_start_transaction(root, 5);
4943 return PTR_ERR(trans);
4944 btrfs_set_trans_block_group(trans, dir);
4946 err = btrfs_find_free_ino(root, &objectid);
4950 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4951 dentry->d_name.len, btrfs_ino(dir), objectid,
4952 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4954 if (IS_ERR(inode)) {
4955 err = PTR_ERR(inode);
4961 err = btrfs_init_inode_security(trans, inode, dir);
4965 inode->i_op = &btrfs_dir_inode_operations;
4966 inode->i_fop = &btrfs_dir_file_operations;
4967 btrfs_set_trans_block_group(trans, inode);
4969 btrfs_i_size_write(inode, 0);
4970 err = btrfs_update_inode(trans, root, inode);
4974 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4975 dentry->d_name.len, 0, index);
4979 d_instantiate(dentry, inode);
4981 btrfs_update_inode_block_group(trans, inode);
4982 btrfs_update_inode_block_group(trans, dir);
4985 nr = trans->blocks_used;
4986 btrfs_end_transaction_throttle(trans, root);
4989 btrfs_btree_balance_dirty(root, nr);
4993 /* helper for btfs_get_extent. Given an existing extent in the tree,
4994 * and an extent that you want to insert, deal with overlap and insert
4995 * the new extent into the tree.
4997 static int merge_extent_mapping(struct extent_map_tree *em_tree,
4998 struct extent_map *existing,
4999 struct extent_map *em,
5000 u64 map_start, u64 map_len)
5004 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5005 start_diff = map_start - em->start;
5006 em->start = map_start;
5008 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5009 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5010 em->block_start += start_diff;
5011 em->block_len -= start_diff;
5013 return add_extent_mapping(em_tree, em);
5016 static noinline int uncompress_inline(struct btrfs_path *path,
5017 struct inode *inode, struct page *page,
5018 size_t pg_offset, u64 extent_offset,
5019 struct btrfs_file_extent_item *item)
5022 struct extent_buffer *leaf = path->nodes[0];
5025 unsigned long inline_size;
5029 WARN_ON(pg_offset != 0);
5030 compress_type = btrfs_file_extent_compression(leaf, item);
5031 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5032 inline_size = btrfs_file_extent_inline_item_len(leaf,
5033 btrfs_item_nr(leaf, path->slots[0]));
5034 tmp = kmalloc(inline_size, GFP_NOFS);
5035 ptr = btrfs_file_extent_inline_start(item);
5037 read_extent_buffer(leaf, tmp, ptr, inline_size);
5039 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5040 ret = btrfs_decompress(compress_type, tmp, page,
5041 extent_offset, inline_size, max_size);
5043 char *kaddr = kmap_atomic(page, KM_USER0);
5044 unsigned long copy_size = min_t(u64,
5045 PAGE_CACHE_SIZE - pg_offset,
5046 max_size - extent_offset);
5047 memset(kaddr + pg_offset, 0, copy_size);
5048 kunmap_atomic(kaddr, KM_USER0);
5055 * a bit scary, this does extent mapping from logical file offset to the disk.
5056 * the ugly parts come from merging extents from the disk with the in-ram
5057 * representation. This gets more complex because of the data=ordered code,
5058 * where the in-ram extents might be locked pending data=ordered completion.
5060 * This also copies inline extents directly into the page.
5063 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5064 size_t pg_offset, u64 start, u64 len,
5070 u64 extent_start = 0;
5072 u64 objectid = btrfs_ino(inode);
5074 struct btrfs_path *path = NULL;
5075 struct btrfs_root *root = BTRFS_I(inode)->root;
5076 struct btrfs_file_extent_item *item;
5077 struct extent_buffer *leaf;
5078 struct btrfs_key found_key;
5079 struct extent_map *em = NULL;
5080 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5081 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5082 struct btrfs_trans_handle *trans = NULL;
5086 read_lock(&em_tree->lock);
5087 em = lookup_extent_mapping(em_tree, start, len);
5089 em->bdev = root->fs_info->fs_devices->latest_bdev;
5090 read_unlock(&em_tree->lock);
5093 if (em->start > start || em->start + em->len <= start)
5094 free_extent_map(em);
5095 else if (em->block_start == EXTENT_MAP_INLINE && page)
5096 free_extent_map(em);
5100 em = alloc_extent_map(GFP_NOFS);
5105 em->bdev = root->fs_info->fs_devices->latest_bdev;
5106 em->start = EXTENT_MAP_HOLE;
5107 em->orig_start = EXTENT_MAP_HOLE;
5109 em->block_len = (u64)-1;
5112 path = btrfs_alloc_path();
5116 ret = btrfs_lookup_file_extent(trans, root, path,
5117 objectid, start, trans != NULL);
5124 if (path->slots[0] == 0)
5129 leaf = path->nodes[0];
5130 item = btrfs_item_ptr(leaf, path->slots[0],
5131 struct btrfs_file_extent_item);
5132 /* are we inside the extent that was found? */
5133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5134 found_type = btrfs_key_type(&found_key);
5135 if (found_key.objectid != objectid ||
5136 found_type != BTRFS_EXTENT_DATA_KEY) {
5140 found_type = btrfs_file_extent_type(leaf, item);
5141 extent_start = found_key.offset;
5142 compress_type = btrfs_file_extent_compression(leaf, item);
5143 if (found_type == BTRFS_FILE_EXTENT_REG ||
5144 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5145 extent_end = extent_start +
5146 btrfs_file_extent_num_bytes(leaf, item);
5147 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5149 size = btrfs_file_extent_inline_len(leaf, item);
5150 extent_end = (extent_start + size + root->sectorsize - 1) &
5151 ~((u64)root->sectorsize - 1);
5154 if (start >= extent_end) {
5156 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5157 ret = btrfs_next_leaf(root, path);
5164 leaf = path->nodes[0];
5166 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5167 if (found_key.objectid != objectid ||
5168 found_key.type != BTRFS_EXTENT_DATA_KEY)
5170 if (start + len <= found_key.offset)
5173 em->len = found_key.offset - start;
5177 if (found_type == BTRFS_FILE_EXTENT_REG ||
5178 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5179 em->start = extent_start;
5180 em->len = extent_end - extent_start;
5181 em->orig_start = extent_start -
5182 btrfs_file_extent_offset(leaf, item);
5183 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5185 em->block_start = EXTENT_MAP_HOLE;
5188 if (compress_type != BTRFS_COMPRESS_NONE) {
5189 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5190 em->compress_type = compress_type;
5191 em->block_start = bytenr;
5192 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5195 bytenr += btrfs_file_extent_offset(leaf, item);
5196 em->block_start = bytenr;
5197 em->block_len = em->len;
5198 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5199 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5202 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5206 size_t extent_offset;
5209 em->block_start = EXTENT_MAP_INLINE;
5210 if (!page || create) {
5211 em->start = extent_start;
5212 em->len = extent_end - extent_start;
5216 size = btrfs_file_extent_inline_len(leaf, item);
5217 extent_offset = page_offset(page) + pg_offset - extent_start;
5218 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
5219 size - extent_offset);
5220 em->start = extent_start + extent_offset;
5221 em->len = (copy_size + root->sectorsize - 1) &
5222 ~((u64)root->sectorsize - 1);
5223 em->orig_start = EXTENT_MAP_INLINE;
5224 if (compress_type) {
5225 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5226 em->compress_type = compress_type;
5228 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5229 if (create == 0 && !PageUptodate(page)) {
5230 if (btrfs_file_extent_compression(leaf, item) !=
5231 BTRFS_COMPRESS_NONE) {
5232 ret = uncompress_inline(path, inode, page,
5234 extent_offset, item);
5238 read_extent_buffer(leaf, map + pg_offset, ptr,
5240 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5241 memset(map + pg_offset + copy_size, 0,
5242 PAGE_CACHE_SIZE - pg_offset -
5247 flush_dcache_page(page);
5248 } else if (create && PageUptodate(page)) {
5252 free_extent_map(em);
5254 btrfs_release_path(root, path);
5255 trans = btrfs_join_transaction(root, 1);
5257 return ERR_CAST(trans);
5261 write_extent_buffer(leaf, map + pg_offset, ptr,
5264 btrfs_mark_buffer_dirty(leaf);
5266 set_extent_uptodate(io_tree, em->start,
5267 extent_map_end(em) - 1, NULL, GFP_NOFS);
5270 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
5277 em->block_start = EXTENT_MAP_HOLE;
5278 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5280 btrfs_release_path(root, path);
5281 if (em->start > start || extent_map_end(em) <= start) {
5282 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5283 "[%llu %llu]\n", (unsigned long long)em->start,
5284 (unsigned long long)em->len,
5285 (unsigned long long)start,
5286 (unsigned long long)len);
5292 write_lock(&em_tree->lock);
5293 ret = add_extent_mapping(em_tree, em);
5294 /* it is possible that someone inserted the extent into the tree
5295 * while we had the lock dropped. It is also possible that
5296 * an overlapping map exists in the tree
5298 if (ret == -EEXIST) {
5299 struct extent_map *existing;
5303 existing = lookup_extent_mapping(em_tree, start, len);
5304 if (existing && (existing->start > start ||
5305 existing->start + existing->len <= start)) {
5306 free_extent_map(existing);
5310 existing = lookup_extent_mapping(em_tree, em->start,
5313 err = merge_extent_mapping(em_tree, existing,
5316 free_extent_map(existing);
5318 free_extent_map(em);
5323 free_extent_map(em);
5327 free_extent_map(em);
5332 write_unlock(&em_tree->lock);
5335 trace_btrfs_get_extent(root, em);
5338 btrfs_free_path(path);
5340 ret = btrfs_end_transaction(trans, root);
5345 free_extent_map(em);
5346 return ERR_PTR(err);
5351 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5352 size_t pg_offset, u64 start, u64 len,
5355 struct extent_map *em;
5356 struct extent_map *hole_em = NULL;
5357 u64 range_start = start;
5363 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5368 * if our em maps to a hole, there might
5369 * actually be delalloc bytes behind it
5371 if (em->block_start != EXTENT_MAP_HOLE)
5377 /* check to see if we've wrapped (len == -1 or similar) */
5386 /* ok, we didn't find anything, lets look for delalloc */
5387 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5388 end, len, EXTENT_DELALLOC, 1);
5389 found_end = range_start + found;
5390 if (found_end < range_start)
5391 found_end = (u64)-1;
5394 * we didn't find anything useful, return
5395 * the original results from get_extent()
5397 if (range_start > end || found_end <= start) {
5403 /* adjust the range_start to make sure it doesn't
5404 * go backwards from the start they passed in
5406 range_start = max(start,range_start);
5407 found = found_end - range_start;
5410 u64 hole_start = start;
5413 em = alloc_extent_map(GFP_NOFS);
5419 * when btrfs_get_extent can't find anything it
5420 * returns one huge hole
5422 * make sure what it found really fits our range, and
5423 * adjust to make sure it is based on the start from
5427 u64 calc_end = extent_map_end(hole_em);
5429 if (calc_end <= start || (hole_em->start > end)) {
5430 free_extent_map(hole_em);
5433 hole_start = max(hole_em->start, start);
5434 hole_len = calc_end - hole_start;
5438 if (hole_em && range_start > hole_start) {
5439 /* our hole starts before our delalloc, so we
5440 * have to return just the parts of the hole
5441 * that go until the delalloc starts
5443 em->len = min(hole_len,
5444 range_start - hole_start);
5445 em->start = hole_start;
5446 em->orig_start = hole_start;
5448 * don't adjust block start at all,
5449 * it is fixed at EXTENT_MAP_HOLE
5451 em->block_start = hole_em->block_start;
5452 em->block_len = hole_len;
5454 em->start = range_start;
5456 em->orig_start = range_start;
5457 em->block_start = EXTENT_MAP_DELALLOC;
5458 em->block_len = found;
5460 } else if (hole_em) {
5465 free_extent_map(hole_em);
5467 free_extent_map(em);
5468 return ERR_PTR(err);
5473 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5474 struct extent_map *em,
5477 struct btrfs_root *root = BTRFS_I(inode)->root;
5478 struct btrfs_trans_handle *trans;
5479 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5480 struct btrfs_key ins;
5483 bool insert = false;
5486 * Ok if the extent map we looked up is a hole and is for the exact
5487 * range we want, there is no reason to allocate a new one, however if
5488 * it is not right then we need to free this one and drop the cache for
5491 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5493 free_extent_map(em);
5496 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5499 trans = btrfs_join_transaction(root, 0);
5501 return ERR_CAST(trans);
5503 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5505 alloc_hint = get_extent_allocation_hint(inode, start, len);
5506 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5507 alloc_hint, (u64)-1, &ins, 1);
5514 em = alloc_extent_map(GFP_NOFS);
5516 em = ERR_PTR(-ENOMEM);
5522 em->orig_start = em->start;
5523 em->len = ins.offset;
5525 em->block_start = ins.objectid;
5526 em->block_len = ins.offset;
5527 em->bdev = root->fs_info->fs_devices->latest_bdev;
5530 * We need to do this because if we're using the original em we searched
5531 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5534 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5537 write_lock(&em_tree->lock);
5538 ret = add_extent_mapping(em_tree, em);
5539 write_unlock(&em_tree->lock);
5542 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5545 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5546 ins.offset, ins.offset, 0);
5548 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5552 btrfs_end_transaction(trans, root);
5557 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5558 * block must be cow'd
5560 static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5561 struct inode *inode, u64 offset, u64 len)
5563 struct btrfs_path *path;
5565 struct extent_buffer *leaf;
5566 struct btrfs_root *root = BTRFS_I(inode)->root;
5567 struct btrfs_file_extent_item *fi;
5568 struct btrfs_key key;
5576 path = btrfs_alloc_path();
5580 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5585 slot = path->slots[0];
5588 /* can't find the item, must cow */
5595 leaf = path->nodes[0];
5596 btrfs_item_key_to_cpu(leaf, &key, slot);
5597 if (key.objectid != btrfs_ino(inode) ||
5598 key.type != BTRFS_EXTENT_DATA_KEY) {
5599 /* not our file or wrong item type, must cow */
5603 if (key.offset > offset) {
5604 /* Wrong offset, must cow */
5608 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5609 found_type = btrfs_file_extent_type(leaf, fi);
5610 if (found_type != BTRFS_FILE_EXTENT_REG &&
5611 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5612 /* not a regular extent, must cow */
5615 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5616 backref_offset = btrfs_file_extent_offset(leaf, fi);
5618 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5619 if (extent_end < offset + len) {
5620 /* extent doesn't include our full range, must cow */
5624 if (btrfs_extent_readonly(root, disk_bytenr))
5628 * look for other files referencing this extent, if we
5629 * find any we must cow
5631 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5632 key.offset - backref_offset, disk_bytenr))
5636 * adjust disk_bytenr and num_bytes to cover just the bytes
5637 * in this extent we are about to write. If there
5638 * are any csums in that range we have to cow in order
5639 * to keep the csums correct
5641 disk_bytenr += backref_offset;
5642 disk_bytenr += offset - key.offset;
5643 num_bytes = min(offset + len, extent_end) - offset;
5644 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5647 * all of the above have passed, it is safe to overwrite this extent
5652 btrfs_free_path(path);
5656 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5657 struct buffer_head *bh_result, int create)
5659 struct extent_map *em;
5660 struct btrfs_root *root = BTRFS_I(inode)->root;
5661 u64 start = iblock << inode->i_blkbits;
5662 u64 len = bh_result->b_size;
5663 struct btrfs_trans_handle *trans;
5665 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5670 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5671 * io. INLINE is special, and we could probably kludge it in here, but
5672 * it's still buffered so for safety lets just fall back to the generic
5675 * For COMPRESSED we _have_ to read the entire extent in so we can
5676 * decompress it, so there will be buffering required no matter what we
5677 * do, so go ahead and fallback to buffered.
5679 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5680 * to buffered IO. Don't blame me, this is the price we pay for using
5683 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5684 em->block_start == EXTENT_MAP_INLINE) {
5685 free_extent_map(em);
5689 /* Just a good old fashioned hole, return */
5690 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5691 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5692 free_extent_map(em);
5693 /* DIO will do one hole at a time, so just unlock a sector */
5694 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5695 start + root->sectorsize - 1, GFP_NOFS);
5700 * We don't allocate a new extent in the following cases
5702 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5704 * 2) The extent is marked as PREALLOC. We're good to go here and can
5705 * just use the extent.
5709 len = em->len - (start - em->start);
5713 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5714 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5715 em->block_start != EXTENT_MAP_HOLE)) {
5720 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5721 type = BTRFS_ORDERED_PREALLOC;
5723 type = BTRFS_ORDERED_NOCOW;
5724 len = min(len, em->len - (start - em->start));
5725 block_start = em->block_start + (start - em->start);
5728 * we're not going to log anything, but we do need
5729 * to make sure the current transaction stays open
5730 * while we look for nocow cross refs
5732 trans = btrfs_join_transaction(root, 0);
5736 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5737 ret = btrfs_add_ordered_extent_dio(inode, start,
5738 block_start, len, len, type);
5739 btrfs_end_transaction(trans, root);
5741 free_extent_map(em);
5746 btrfs_end_transaction(trans, root);
5750 * this will cow the extent, reset the len in case we changed
5753 len = bh_result->b_size;
5754 em = btrfs_new_extent_direct(inode, em, start, len);
5757 len = min(len, em->len - (start - em->start));
5759 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5760 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5763 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5765 bh_result->b_size = len;
5766 bh_result->b_bdev = em->bdev;
5767 set_buffer_mapped(bh_result);
5768 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5769 set_buffer_new(bh_result);
5771 free_extent_map(em);
5776 struct btrfs_dio_private {
5777 struct inode *inode;
5784 /* number of bios pending for this dio */
5785 atomic_t pending_bios;
5790 struct bio *orig_bio;
5793 static void btrfs_endio_direct_read(struct bio *bio, int err)
5795 struct btrfs_dio_private *dip = bio->bi_private;
5796 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5797 struct bio_vec *bvec = bio->bi_io_vec;
5798 struct inode *inode = dip->inode;
5799 struct btrfs_root *root = BTRFS_I(inode)->root;
5801 u32 *private = dip->csums;
5803 start = dip->logical_offset;
5805 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5806 struct page *page = bvec->bv_page;
5809 unsigned long flags;
5811 local_irq_save(flags);
5812 kaddr = kmap_atomic(page, KM_IRQ0);
5813 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5814 csum, bvec->bv_len);
5815 btrfs_csum_final(csum, (char *)&csum);
5816 kunmap_atomic(kaddr, KM_IRQ0);
5817 local_irq_restore(flags);
5819 flush_dcache_page(bvec->bv_page);
5820 if (csum != *private) {
5821 printk(KERN_ERR "btrfs csum failed ino %llu off"
5822 " %llu csum %u private %u\n",
5823 (unsigned long long)btrfs_ino(inode),
5824 (unsigned long long)start,
5830 start += bvec->bv_len;
5833 } while (bvec <= bvec_end);
5835 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5836 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5837 bio->bi_private = dip->private;
5842 /* If we had a csum failure make sure to clear the uptodate flag */
5844 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5845 dio_end_io(bio, err);
5848 static void btrfs_endio_direct_write(struct bio *bio, int err)
5850 struct btrfs_dio_private *dip = bio->bi_private;
5851 struct inode *inode = dip->inode;
5852 struct btrfs_root *root = BTRFS_I(inode)->root;
5853 struct btrfs_trans_handle *trans;
5854 struct btrfs_ordered_extent *ordered = NULL;
5855 struct extent_state *cached_state = NULL;
5856 u64 ordered_offset = dip->logical_offset;
5857 u64 ordered_bytes = dip->bytes;
5863 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5871 trans = btrfs_join_transaction(root, 1);
5872 if (IS_ERR(trans)) {
5876 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5878 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5879 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5881 ret = btrfs_update_inode(trans, root, inode);
5886 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5887 ordered->file_offset + ordered->len - 1, 0,
5888 &cached_state, GFP_NOFS);
5890 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5891 ret = btrfs_mark_extent_written(trans, inode,
5892 ordered->file_offset,
5893 ordered->file_offset +
5900 ret = insert_reserved_file_extent(trans, inode,
5901 ordered->file_offset,
5907 BTRFS_FILE_EXTENT_REG);
5908 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5909 ordered->file_offset, ordered->len);
5917 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5918 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5920 btrfs_update_inode(trans, root, inode);
5923 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5924 ordered->file_offset + ordered->len - 1,
5925 &cached_state, GFP_NOFS);
5927 btrfs_delalloc_release_metadata(inode, ordered->len);
5928 btrfs_end_transaction(trans, root);
5929 ordered_offset = ordered->file_offset + ordered->len;
5930 btrfs_put_ordered_extent(ordered);
5931 btrfs_put_ordered_extent(ordered);
5935 * our bio might span multiple ordered extents. If we haven't
5936 * completed the accounting for the whole dio, go back and try again
5938 if (ordered_offset < dip->logical_offset + dip->bytes) {
5939 ordered_bytes = dip->logical_offset + dip->bytes -
5944 bio->bi_private = dip->private;
5949 /* If we had an error make sure to clear the uptodate flag */
5951 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5952 dio_end_io(bio, err);
5955 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5956 struct bio *bio, int mirror_num,
5957 unsigned long bio_flags, u64 offset)
5960 struct btrfs_root *root = BTRFS_I(inode)->root;
5961 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5966 static void btrfs_end_dio_bio(struct bio *bio, int err)
5968 struct btrfs_dio_private *dip = bio->bi_private;
5971 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5972 "sector %#Lx len %u err no %d\n",
5973 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5974 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5978 * before atomic variable goto zero, we must make sure
5979 * dip->errors is perceived to be set.
5981 smp_mb__before_atomic_dec();
5984 /* if there are more bios still pending for this dio, just exit */
5985 if (!atomic_dec_and_test(&dip->pending_bios))
5989 bio_io_error(dip->orig_bio);
5991 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5992 bio_endio(dip->orig_bio, 0);
5998 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5999 u64 first_sector, gfp_t gfp_flags)
6001 int nr_vecs = bio_get_nr_vecs(bdev);
6002 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6005 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6006 int rw, u64 file_offset, int skip_sum,
6007 u32 *csums, int async_submit)
6009 int write = rw & REQ_WRITE;
6010 struct btrfs_root *root = BTRFS_I(inode)->root;
6014 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6021 if (write && async_submit) {
6022 ret = btrfs_wq_submit_bio(root->fs_info,
6023 inode, rw, bio, 0, 0,
6025 __btrfs_submit_bio_start_direct_io,
6026 __btrfs_submit_bio_done);
6030 * If we aren't doing async submit, calculate the csum of the
6033 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6036 } else if (!skip_sum) {
6037 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
6038 file_offset, csums);
6044 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6050 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6053 struct inode *inode = dip->inode;
6054 struct btrfs_root *root = BTRFS_I(inode)->root;
6055 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6057 struct bio *orig_bio = dip->orig_bio;
6058 struct bio_vec *bvec = orig_bio->bi_io_vec;
6059 u64 start_sector = orig_bio->bi_sector;
6060 u64 file_offset = dip->logical_offset;
6064 u32 *csums = dip->csums;
6066 int async_submit = 0;
6067 int write = rw & REQ_WRITE;
6069 map_length = orig_bio->bi_size;
6070 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6071 &map_length, NULL, 0);
6077 if (map_length >= orig_bio->bi_size) {
6083 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6086 bio->bi_private = dip;
6087 bio->bi_end_io = btrfs_end_dio_bio;
6088 atomic_inc(&dip->pending_bios);
6090 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6091 if (unlikely(map_length < submit_len + bvec->bv_len ||
6092 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6093 bvec->bv_offset) < bvec->bv_len)) {
6095 * inc the count before we submit the bio so
6096 * we know the end IO handler won't happen before
6097 * we inc the count. Otherwise, the dip might get freed
6098 * before we're done setting it up
6100 atomic_inc(&dip->pending_bios);
6101 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6102 file_offset, skip_sum,
6103 csums, async_submit);
6106 atomic_dec(&dip->pending_bios);
6110 /* Write's use the ordered csums */
6111 if (!write && !skip_sum)
6112 csums = csums + nr_pages;
6113 start_sector += submit_len >> 9;
6114 file_offset += submit_len;
6119 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6120 start_sector, GFP_NOFS);
6123 bio->bi_private = dip;
6124 bio->bi_end_io = btrfs_end_dio_bio;
6126 map_length = orig_bio->bi_size;
6127 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6128 &map_length, NULL, 0);
6134 submit_len += bvec->bv_len;
6141 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6142 csums, async_submit);
6150 * before atomic variable goto zero, we must
6151 * make sure dip->errors is perceived to be set.
6153 smp_mb__before_atomic_dec();
6154 if (atomic_dec_and_test(&dip->pending_bios))
6155 bio_io_error(dip->orig_bio);
6157 /* bio_end_io() will handle error, so we needn't return it */
6161 static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6164 struct btrfs_root *root = BTRFS_I(inode)->root;
6165 struct btrfs_dio_private *dip;
6166 struct bio_vec *bvec = bio->bi_io_vec;
6168 int write = rw & REQ_WRITE;
6171 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6173 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6180 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6181 if (!write && !skip_sum) {
6182 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6190 dip->private = bio->bi_private;
6192 dip->logical_offset = file_offset;
6196 dip->bytes += bvec->bv_len;
6198 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6200 dip->disk_bytenr = (u64)bio->bi_sector << 9;
6201 bio->bi_private = dip;
6203 dip->orig_bio = bio;
6204 atomic_set(&dip->pending_bios, 0);
6207 bio->bi_end_io = btrfs_endio_direct_write;
6209 bio->bi_end_io = btrfs_endio_direct_read;
6211 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6216 * If this is a write, we need to clean up the reserved space and kill
6217 * the ordered extent.
6220 struct btrfs_ordered_extent *ordered;
6221 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
6222 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6223 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6224 btrfs_free_reserved_extent(root, ordered->start,
6226 btrfs_put_ordered_extent(ordered);
6227 btrfs_put_ordered_extent(ordered);
6229 bio_endio(bio, ret);
6232 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6233 const struct iovec *iov, loff_t offset,
6234 unsigned long nr_segs)
6240 unsigned blocksize_mask = root->sectorsize - 1;
6241 ssize_t retval = -EINVAL;
6242 loff_t end = offset;
6244 if (offset & blocksize_mask)
6247 /* Check the memory alignment. Blocks cannot straddle pages */
6248 for (seg = 0; seg < nr_segs; seg++) {
6249 addr = (unsigned long)iov[seg].iov_base;
6250 size = iov[seg].iov_len;
6252 if ((addr & blocksize_mask) || (size & blocksize_mask))
6255 /* If this is a write we don't need to check anymore */
6260 * Check to make sure we don't have duplicate iov_base's in this
6261 * iovec, if so return EINVAL, otherwise we'll get csum errors
6262 * when reading back.
6264 for (i = seg + 1; i < nr_segs; i++) {
6265 if (iov[seg].iov_base == iov[i].iov_base)
6273 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6274 const struct iovec *iov, loff_t offset,
6275 unsigned long nr_segs)
6277 struct file *file = iocb->ki_filp;
6278 struct inode *inode = file->f_mapping->host;
6279 struct btrfs_ordered_extent *ordered;
6280 struct extent_state *cached_state = NULL;
6281 u64 lockstart, lockend;
6283 int writing = rw & WRITE;
6285 size_t count = iov_length(iov, nr_segs);
6287 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6293 lockend = offset + count - 1;
6296 ret = btrfs_delalloc_reserve_space(inode, count);
6302 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6303 0, &cached_state, GFP_NOFS);
6305 * We're concerned with the entire range that we're going to be
6306 * doing DIO to, so we need to make sure theres no ordered
6307 * extents in this range.
6309 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6310 lockend - lockstart + 1);
6313 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6314 &cached_state, GFP_NOFS);
6315 btrfs_start_ordered_extent(inode, ordered, 1);
6316 btrfs_put_ordered_extent(ordered);
6321 * we don't use btrfs_set_extent_delalloc because we don't want
6322 * the dirty or uptodate bits
6325 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6326 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6327 EXTENT_DELALLOC, 0, NULL, &cached_state,
6330 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6331 lockend, EXTENT_LOCKED | write_bits,
6332 1, 0, &cached_state, GFP_NOFS);
6337 free_extent_state(cached_state);
6338 cached_state = NULL;
6340 ret = __blockdev_direct_IO(rw, iocb, inode,
6341 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6342 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6343 btrfs_submit_direct, 0);
6345 if (ret < 0 && ret != -EIOCBQUEUED) {
6346 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6347 offset + iov_length(iov, nr_segs) - 1,
6348 EXTENT_LOCKED | write_bits, 1, 0,
6349 &cached_state, GFP_NOFS);
6350 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6352 * We're falling back to buffered, unlock the section we didn't
6355 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6356 offset + iov_length(iov, nr_segs) - 1,
6357 EXTENT_LOCKED | write_bits, 1, 0,
6358 &cached_state, GFP_NOFS);
6361 free_extent_state(cached_state);
6365 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6366 __u64 start, __u64 len)
6368 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6371 int btrfs_readpage(struct file *file, struct page *page)
6373 struct extent_io_tree *tree;
6374 tree = &BTRFS_I(page->mapping->host)->io_tree;
6375 return extent_read_full_page(tree, page, btrfs_get_extent);
6378 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6380 struct extent_io_tree *tree;
6383 if (current->flags & PF_MEMALLOC) {
6384 redirty_page_for_writepage(wbc, page);
6388 tree = &BTRFS_I(page->mapping->host)->io_tree;
6389 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6392 int btrfs_writepages(struct address_space *mapping,
6393 struct writeback_control *wbc)
6395 struct extent_io_tree *tree;
6397 tree = &BTRFS_I(mapping->host)->io_tree;
6398 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6402 btrfs_readpages(struct file *file, struct address_space *mapping,
6403 struct list_head *pages, unsigned nr_pages)
6405 struct extent_io_tree *tree;
6406 tree = &BTRFS_I(mapping->host)->io_tree;
6407 return extent_readpages(tree, mapping, pages, nr_pages,
6410 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6412 struct extent_io_tree *tree;
6413 struct extent_map_tree *map;
6416 tree = &BTRFS_I(page->mapping->host)->io_tree;
6417 map = &BTRFS_I(page->mapping->host)->extent_tree;
6418 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
6420 ClearPagePrivate(page);
6421 set_page_private(page, 0);
6422 page_cache_release(page);
6427 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6429 if (PageWriteback(page) || PageDirty(page))
6431 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
6434 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6436 struct extent_io_tree *tree;
6437 struct btrfs_ordered_extent *ordered;
6438 struct extent_state *cached_state = NULL;
6439 u64 page_start = page_offset(page);
6440 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
6444 * we have the page locked, so new writeback can't start,
6445 * and the dirty bit won't be cleared while we are here.
6447 * Wait for IO on this page so that we can safely clear
6448 * the PagePrivate2 bit and do ordered accounting
6450 wait_on_page_writeback(page);
6452 tree = &BTRFS_I(page->mapping->host)->io_tree;
6454 btrfs_releasepage(page, GFP_NOFS);
6457 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6459 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6463 * IO on this page will never be started, so we need
6464 * to account for any ordered extents now
6466 clear_extent_bit(tree, page_start, page_end,
6467 EXTENT_DIRTY | EXTENT_DELALLOC |
6468 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
6469 &cached_state, GFP_NOFS);
6471 * whoever cleared the private bit is responsible
6472 * for the finish_ordered_io
6474 if (TestClearPagePrivate2(page)) {
6475 btrfs_finish_ordered_io(page->mapping->host,
6476 page_start, page_end);
6478 btrfs_put_ordered_extent(ordered);
6479 cached_state = NULL;
6480 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6483 clear_extent_bit(tree, page_start, page_end,
6484 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
6485 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
6486 __btrfs_releasepage(page, GFP_NOFS);
6488 ClearPageChecked(page);
6489 if (PagePrivate(page)) {
6490 ClearPagePrivate(page);
6491 set_page_private(page, 0);
6492 page_cache_release(page);
6497 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6498 * called from a page fault handler when a page is first dirtied. Hence we must
6499 * be careful to check for EOF conditions here. We set the page up correctly
6500 * for a written page which means we get ENOSPC checking when writing into
6501 * holes and correct delalloc and unwritten extent mapping on filesystems that
6502 * support these features.
6504 * We are not allowed to take the i_mutex here so we have to play games to
6505 * protect against truncate races as the page could now be beyond EOF. Because
6506 * vmtruncate() writes the inode size before removing pages, once we have the
6507 * page lock we can determine safely if the page is beyond EOF. If it is not
6508 * beyond EOF, then the page is guaranteed safe against truncation until we
6511 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
6513 struct page *page = vmf->page;
6514 struct inode *inode = fdentry(vma->vm_file)->d_inode;
6515 struct btrfs_root *root = BTRFS_I(inode)->root;
6516 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6517 struct btrfs_ordered_extent *ordered;
6518 struct extent_state *cached_state = NULL;
6520 unsigned long zero_start;
6526 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6530 else /* -ENOSPC, -EIO, etc */
6531 ret = VM_FAULT_SIGBUS;
6535 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
6538 size = i_size_read(inode);
6539 page_start = page_offset(page);
6540 page_end = page_start + PAGE_CACHE_SIZE - 1;
6542 if ((page->mapping != inode->i_mapping) ||
6543 (page_start >= size)) {
6544 /* page got truncated out from underneath us */
6547 wait_on_page_writeback(page);
6549 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6551 set_page_extent_mapped(page);
6554 * we can't set the delalloc bits if there are pending ordered
6555 * extents. Drop our locks and wait for them to finish
6557 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6559 unlock_extent_cached(io_tree, page_start, page_end,
6560 &cached_state, GFP_NOFS);
6562 btrfs_start_ordered_extent(inode, ordered, 1);
6563 btrfs_put_ordered_extent(ordered);
6568 * XXX - page_mkwrite gets called every time the page is dirtied, even
6569 * if it was already dirty, so for space accounting reasons we need to
6570 * clear any delalloc bits for the range we are fixing to save. There
6571 * is probably a better way to do this, but for now keep consistent with
6572 * prepare_pages in the normal write path.
6574 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
6575 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
6576 0, 0, &cached_state, GFP_NOFS);
6578 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6581 unlock_extent_cached(io_tree, page_start, page_end,
6582 &cached_state, GFP_NOFS);
6583 ret = VM_FAULT_SIGBUS;
6588 /* page is wholly or partially inside EOF */
6589 if (page_start + PAGE_CACHE_SIZE > size)
6590 zero_start = size & ~PAGE_CACHE_MASK;
6592 zero_start = PAGE_CACHE_SIZE;
6594 if (zero_start != PAGE_CACHE_SIZE) {
6596 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6597 flush_dcache_page(page);
6600 ClearPageChecked(page);
6601 set_page_dirty(page);
6602 SetPageUptodate(page);
6604 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6605 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6607 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
6611 return VM_FAULT_LOCKED;
6613 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
6618 static int btrfs_truncate(struct inode *inode)
6620 struct btrfs_root *root = BTRFS_I(inode)->root;
6623 struct btrfs_trans_handle *trans;
6625 u64 mask = root->sectorsize - 1;
6627 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6631 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6632 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6634 trans = btrfs_start_transaction(root, 5);
6636 return PTR_ERR(trans);
6638 btrfs_set_trans_block_group(trans, inode);
6640 ret = btrfs_orphan_add(trans, inode);
6642 btrfs_end_transaction(trans, root);
6646 nr = trans->blocks_used;
6647 btrfs_end_transaction(trans, root);
6648 btrfs_btree_balance_dirty(root, nr);
6650 /* Now start a transaction for the truncate */
6651 trans = btrfs_start_transaction(root, 0);
6653 return PTR_ERR(trans);
6654 btrfs_set_trans_block_group(trans, inode);
6655 trans->block_rsv = root->orphan_block_rsv;
6658 * setattr is responsible for setting the ordered_data_close flag,
6659 * but that is only tested during the last file release. That
6660 * could happen well after the next commit, leaving a great big
6661 * window where new writes may get lost if someone chooses to write
6662 * to this file after truncating to zero
6664 * The inode doesn't have any dirty data here, and so if we commit
6665 * this is a noop. If someone immediately starts writing to the inode
6666 * it is very likely we'll catch some of their writes in this
6667 * transaction, and the commit will find this file on the ordered
6668 * data list with good things to send down.
6670 * This is a best effort solution, there is still a window where
6671 * using truncate to replace the contents of the file will
6672 * end up with a zero length file after a crash.
6674 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6675 btrfs_add_ordered_operation(trans, root, inode);
6679 trans = btrfs_start_transaction(root, 0);
6681 return PTR_ERR(trans);
6682 btrfs_set_trans_block_group(trans, inode);
6683 trans->block_rsv = root->orphan_block_rsv;
6686 ret = btrfs_block_rsv_check(trans, root,
6687 root->orphan_block_rsv, 0, 5);
6688 if (ret == -EAGAIN) {
6689 ret = btrfs_commit_transaction(trans, root);
6699 ret = btrfs_truncate_inode_items(trans, root, inode,
6701 BTRFS_EXTENT_DATA_KEY);
6702 if (ret != -EAGAIN) {
6707 ret = btrfs_update_inode(trans, root, inode);
6713 nr = trans->blocks_used;
6714 btrfs_end_transaction(trans, root);
6716 btrfs_btree_balance_dirty(root, nr);
6719 if (ret == 0 && inode->i_nlink > 0) {
6720 ret = btrfs_orphan_del(trans, inode);
6723 } else if (ret && inode->i_nlink > 0) {
6725 * Failed to do the truncate, remove us from the in memory
6728 ret = btrfs_orphan_del(NULL, inode);
6731 ret = btrfs_update_inode(trans, root, inode);
6735 nr = trans->blocks_used;
6736 ret = btrfs_end_transaction_throttle(trans, root);
6739 btrfs_btree_balance_dirty(root, nr);
6745 * create a new subvolume directory/inode (helper for the ioctl).
6747 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
6748 struct btrfs_root *new_root,
6749 u64 new_dirid, u64 alloc_hint)
6751 struct inode *inode;
6755 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
6756 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
6758 return PTR_ERR(inode);
6759 inode->i_op = &btrfs_dir_inode_operations;
6760 inode->i_fop = &btrfs_dir_file_operations;
6763 btrfs_i_size_write(inode, 0);
6765 err = btrfs_update_inode(trans, new_root, inode);
6772 /* helper function for file defrag and space balancing. This
6773 * forces readahead on a given range of bytes in an inode
6775 unsigned long btrfs_force_ra(struct address_space *mapping,
6776 struct file_ra_state *ra, struct file *file,
6777 pgoff_t offset, pgoff_t last_index)
6779 pgoff_t req_size = last_index - offset + 1;
6781 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6782 return offset + req_size;
6785 struct inode *btrfs_alloc_inode(struct super_block *sb)
6787 struct btrfs_inode *ei;
6788 struct inode *inode;
6790 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6795 ei->space_info = NULL;
6799 ei->last_sub_trans = 0;
6800 ei->logged_trans = 0;
6801 ei->delalloc_bytes = 0;
6802 ei->reserved_bytes = 0;
6803 ei->disk_i_size = 0;
6805 ei->index_cnt = (u64)-1;
6806 ei->last_unlink_trans = 0;
6808 atomic_set(&ei->outstanding_extents, 0);
6809 atomic_set(&ei->reserved_extents, 0);
6811 ei->ordered_data_close = 0;
6812 ei->orphan_meta_reserved = 0;
6813 ei->dummy_inode = 0;
6814 ei->force_compress = BTRFS_COMPRESS_NONE;
6816 inode = &ei->vfs_inode;
6817 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6818 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6819 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6820 mutex_init(&ei->log_mutex);
6821 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6822 INIT_LIST_HEAD(&ei->i_orphan);
6823 INIT_LIST_HEAD(&ei->delalloc_inodes);
6824 INIT_LIST_HEAD(&ei->ordered_operations);
6825 RB_CLEAR_NODE(&ei->rb_node);
6830 static void btrfs_i_callback(struct rcu_head *head)
6832 struct inode *inode = container_of(head, struct inode, i_rcu);
6833 INIT_LIST_HEAD(&inode->i_dentry);
6834 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6837 void btrfs_destroy_inode(struct inode *inode)
6839 struct btrfs_ordered_extent *ordered;
6840 struct btrfs_root *root = BTRFS_I(inode)->root;
6842 WARN_ON(!list_empty(&inode->i_dentry));
6843 WARN_ON(inode->i_data.nrpages);
6844 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6845 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
6848 * This can happen where we create an inode, but somebody else also
6849 * created the same inode and we need to destroy the one we already
6856 * Make sure we're properly removed from the ordered operation
6860 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6861 spin_lock(&root->fs_info->ordered_extent_lock);
6862 list_del_init(&BTRFS_I(inode)->ordered_operations);
6863 spin_unlock(&root->fs_info->ordered_extent_lock);
6866 if (root == root->fs_info->tree_root) {
6867 struct btrfs_block_group_cache *block_group;
6869 block_group = btrfs_lookup_block_group(root->fs_info,
6870 BTRFS_I(inode)->block_group);
6871 if (block_group && block_group->inode == inode) {
6872 spin_lock(&block_group->lock);
6873 block_group->inode = NULL;
6874 spin_unlock(&block_group->lock);
6875 btrfs_put_block_group(block_group);
6876 } else if (block_group) {
6877 btrfs_put_block_group(block_group);
6881 spin_lock(&root->orphan_lock);
6882 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6883 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6884 (unsigned long long)btrfs_ino(inode));
6885 list_del_init(&BTRFS_I(inode)->i_orphan);
6887 spin_unlock(&root->orphan_lock);
6890 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6894 printk(KERN_ERR "btrfs found ordered "
6895 "extent %llu %llu on inode cleanup\n",
6896 (unsigned long long)ordered->file_offset,
6897 (unsigned long long)ordered->len);
6898 btrfs_remove_ordered_extent(inode, ordered);
6899 btrfs_put_ordered_extent(ordered);
6900 btrfs_put_ordered_extent(ordered);
6903 inode_tree_del(inode);
6904 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6906 call_rcu(&inode->i_rcu, btrfs_i_callback);
6909 int btrfs_drop_inode(struct inode *inode)
6911 struct btrfs_root *root = BTRFS_I(inode)->root;
6913 if (btrfs_root_refs(&root->root_item) == 0 &&
6914 !is_free_space_inode(root, inode))
6917 return generic_drop_inode(inode);
6920 static void init_once(void *foo)
6922 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6924 inode_init_once(&ei->vfs_inode);
6927 void btrfs_destroy_cachep(void)
6929 if (btrfs_inode_cachep)
6930 kmem_cache_destroy(btrfs_inode_cachep);
6931 if (btrfs_trans_handle_cachep)
6932 kmem_cache_destroy(btrfs_trans_handle_cachep);
6933 if (btrfs_transaction_cachep)
6934 kmem_cache_destroy(btrfs_transaction_cachep);
6935 if (btrfs_path_cachep)
6936 kmem_cache_destroy(btrfs_path_cachep);
6937 if (btrfs_free_space_cachep)
6938 kmem_cache_destroy(btrfs_free_space_cachep);
6941 int btrfs_init_cachep(void)
6943 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6944 sizeof(struct btrfs_inode), 0,
6945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
6946 if (!btrfs_inode_cachep)
6949 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6950 sizeof(struct btrfs_trans_handle), 0,
6951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6952 if (!btrfs_trans_handle_cachep)
6955 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6956 sizeof(struct btrfs_transaction), 0,
6957 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6958 if (!btrfs_transaction_cachep)
6961 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6962 sizeof(struct btrfs_path), 0,
6963 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6964 if (!btrfs_path_cachep)
6967 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6968 sizeof(struct btrfs_free_space), 0,
6969 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6970 if (!btrfs_free_space_cachep)
6975 btrfs_destroy_cachep();
6979 static int btrfs_getattr(struct vfsmount *mnt,
6980 struct dentry *dentry, struct kstat *stat)
6982 struct inode *inode = dentry->d_inode;
6983 generic_fillattr(inode, stat);
6984 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
6985 stat->blksize = PAGE_CACHE_SIZE;
6986 stat->blocks = (inode_get_bytes(inode) +
6987 BTRFS_I(inode)->delalloc_bytes) >> 9;
6992 * If a file is moved, it will inherit the cow and compression flags of the new
6995 static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6997 struct btrfs_inode *b_dir = BTRFS_I(dir);
6998 struct btrfs_inode *b_inode = BTRFS_I(inode);
7000 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7001 b_inode->flags |= BTRFS_INODE_NODATACOW;
7003 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7005 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7006 b_inode->flags |= BTRFS_INODE_COMPRESS;
7008 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7011 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7012 struct inode *new_dir, struct dentry *new_dentry)
7014 struct btrfs_trans_handle *trans;
7015 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7016 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7017 struct inode *new_inode = new_dentry->d_inode;
7018 struct inode *old_inode = old_dentry->d_inode;
7019 struct timespec ctime = CURRENT_TIME;
7023 u64 old_ino = btrfs_ino(old_inode);
7025 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7028 /* we only allow rename subvolume link between subvolumes */
7029 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7032 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7033 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7036 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7037 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7040 * we're using rename to replace one file with another.
7041 * and the replacement file is large. Start IO on it now so
7042 * we don't add too much work to the end of the transaction
7044 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7045 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7046 filemap_flush(old_inode->i_mapping);
7048 /* close the racy window with snapshot create/destroy ioctl */
7049 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7050 down_read(&root->fs_info->subvol_sem);
7052 * We want to reserve the absolute worst case amount of items. So if
7053 * both inodes are subvols and we need to unlink them then that would
7054 * require 4 item modifications, but if they are both normal inodes it
7055 * would require 5 item modifications, so we'll assume their normal
7056 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7057 * should cover the worst case number of items we'll modify.
7059 trans = btrfs_start_transaction(root, 20);
7060 if (IS_ERR(trans)) {
7061 ret = PTR_ERR(trans);
7065 btrfs_set_trans_block_group(trans, new_dir);
7068 btrfs_record_root_in_trans(trans, dest);
7070 ret = btrfs_set_inode_index(new_dir, &index);
7074 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7075 /* force full log commit if subvolume involved. */
7076 root->fs_info->last_trans_log_full_commit = trans->transid;
7078 ret = btrfs_insert_inode_ref(trans, dest,
7079 new_dentry->d_name.name,
7080 new_dentry->d_name.len,
7082 btrfs_ino(new_dir), index);
7086 * this is an ugly little race, but the rename is required
7087 * to make sure that if we crash, the inode is either at the
7088 * old name or the new one. pinning the log transaction lets
7089 * us make sure we don't allow a log commit to come in after
7090 * we unlink the name but before we add the new name back in.
7092 btrfs_pin_log_trans(root);
7095 * make sure the inode gets flushed if it is replacing
7098 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7099 btrfs_add_ordered_operation(trans, root, old_inode);
7101 old_dir->i_ctime = old_dir->i_mtime = ctime;
7102 new_dir->i_ctime = new_dir->i_mtime = ctime;
7103 old_inode->i_ctime = ctime;
7105 if (old_dentry->d_parent != new_dentry->d_parent)
7106 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7108 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7109 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7110 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7111 old_dentry->d_name.name,
7112 old_dentry->d_name.len);
7114 ret = __btrfs_unlink_inode(trans, root, old_dir,
7115 old_dentry->d_inode,
7116 old_dentry->d_name.name,
7117 old_dentry->d_name.len);
7119 ret = btrfs_update_inode(trans, root, old_inode);
7124 new_inode->i_ctime = CURRENT_TIME;
7125 if (unlikely(btrfs_ino(new_inode) ==
7126 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7127 root_objectid = BTRFS_I(new_inode)->location.objectid;
7128 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7130 new_dentry->d_name.name,
7131 new_dentry->d_name.len);
7132 BUG_ON(new_inode->i_nlink == 0);
7134 ret = btrfs_unlink_inode(trans, dest, new_dir,
7135 new_dentry->d_inode,
7136 new_dentry->d_name.name,
7137 new_dentry->d_name.len);
7140 if (new_inode->i_nlink == 0) {
7141 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7146 fixup_inode_flags(new_dir, old_inode);
7148 ret = btrfs_add_link(trans, new_dir, old_inode,
7149 new_dentry->d_name.name,
7150 new_dentry->d_name.len, 0, index);
7153 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7154 struct dentry *parent = dget_parent(new_dentry);
7155 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7157 btrfs_end_log_trans(root);
7160 btrfs_end_transaction_throttle(trans, root);
7162 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7163 up_read(&root->fs_info->subvol_sem);
7169 * some fairly slow code that needs optimization. This walks the list
7170 * of all the inodes with pending delalloc and forces them to disk.
7172 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7174 struct list_head *head = &root->fs_info->delalloc_inodes;
7175 struct btrfs_inode *binode;
7176 struct inode *inode;
7178 if (root->fs_info->sb->s_flags & MS_RDONLY)
7181 spin_lock(&root->fs_info->delalloc_lock);
7182 while (!list_empty(head)) {
7183 binode = list_entry(head->next, struct btrfs_inode,
7185 inode = igrab(&binode->vfs_inode);
7187 list_del_init(&binode->delalloc_inodes);
7188 spin_unlock(&root->fs_info->delalloc_lock);
7190 filemap_flush(inode->i_mapping);
7192 btrfs_add_delayed_iput(inode);
7197 spin_lock(&root->fs_info->delalloc_lock);
7199 spin_unlock(&root->fs_info->delalloc_lock);
7201 /* the filemap_flush will queue IO into the worker threads, but
7202 * we have to make sure the IO is actually started and that
7203 * ordered extents get created before we return
7205 atomic_inc(&root->fs_info->async_submit_draining);
7206 while (atomic_read(&root->fs_info->nr_async_submits) ||
7207 atomic_read(&root->fs_info->async_delalloc_pages)) {
7208 wait_event(root->fs_info->async_submit_wait,
7209 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7210 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7212 atomic_dec(&root->fs_info->async_submit_draining);
7216 int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7219 struct btrfs_inode *binode;
7220 struct inode *inode = NULL;
7222 spin_lock(&root->fs_info->delalloc_lock);
7223 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7224 binode = list_entry(root->fs_info->delalloc_inodes.next,
7225 struct btrfs_inode, delalloc_inodes);
7226 inode = igrab(&binode->vfs_inode);
7228 list_move_tail(&binode->delalloc_inodes,
7229 &root->fs_info->delalloc_inodes);
7233 list_del_init(&binode->delalloc_inodes);
7234 cond_resched_lock(&root->fs_info->delalloc_lock);
7236 spin_unlock(&root->fs_info->delalloc_lock);
7240 filemap_write_and_wait(inode->i_mapping);
7242 * We have to do this because compression doesn't
7243 * actually set PG_writeback until it submits the pages
7244 * for IO, which happens in an async thread, so we could
7245 * race and not actually wait for any writeback pages
7246 * because they've not been submitted yet. Technically
7247 * this could still be the case for the ordered stuff
7248 * since the async thread may not have started to do its
7249 * work yet. If this becomes the case then we need to
7250 * figure out a way to make sure that in writepage we
7251 * wait for any async pages to be submitted before
7252 * returning so that fdatawait does what its supposed to
7255 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7257 filemap_flush(inode->i_mapping);
7260 btrfs_add_delayed_iput(inode);
7268 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7269 const char *symname)
7271 struct btrfs_trans_handle *trans;
7272 struct btrfs_root *root = BTRFS_I(dir)->root;
7273 struct btrfs_path *path;
7274 struct btrfs_key key;
7275 struct inode *inode = NULL;
7283 struct btrfs_file_extent_item *ei;
7284 struct extent_buffer *leaf;
7285 unsigned long nr = 0;
7287 name_len = strlen(symname) + 1;
7288 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7289 return -ENAMETOOLONG;
7292 * 2 items for inode item and ref
7293 * 2 items for dir items
7294 * 1 item for xattr if selinux is on
7296 trans = btrfs_start_transaction(root, 5);
7298 return PTR_ERR(trans);
7300 btrfs_set_trans_block_group(trans, dir);
7302 err = btrfs_find_free_ino(root, &objectid);
7306 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7307 dentry->d_name.len, btrfs_ino(dir), objectid,
7308 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7310 err = PTR_ERR(inode);
7314 err = btrfs_init_inode_security(trans, inode, dir);
7320 btrfs_set_trans_block_group(trans, inode);
7321 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7325 inode->i_mapping->a_ops = &btrfs_aops;
7326 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7327 inode->i_fop = &btrfs_file_operations;
7328 inode->i_op = &btrfs_file_inode_operations;
7329 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7331 btrfs_update_inode_block_group(trans, inode);
7332 btrfs_update_inode_block_group(trans, dir);
7336 path = btrfs_alloc_path();
7338 key.objectid = btrfs_ino(inode);
7340 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7341 datasize = btrfs_file_extent_calc_inline_size(name_len);
7342 err = btrfs_insert_empty_item(trans, root, path, &key,
7348 leaf = path->nodes[0];
7349 ei = btrfs_item_ptr(leaf, path->slots[0],
7350 struct btrfs_file_extent_item);
7351 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7352 btrfs_set_file_extent_type(leaf, ei,
7353 BTRFS_FILE_EXTENT_INLINE);
7354 btrfs_set_file_extent_encryption(leaf, ei, 0);
7355 btrfs_set_file_extent_compression(leaf, ei, 0);
7356 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7357 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7359 ptr = btrfs_file_extent_inline_start(ei);
7360 write_extent_buffer(leaf, symname, ptr, name_len);
7361 btrfs_mark_buffer_dirty(leaf);
7362 btrfs_free_path(path);
7364 inode->i_op = &btrfs_symlink_inode_operations;
7365 inode->i_mapping->a_ops = &btrfs_symlink_aops;
7366 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7367 inode_set_bytes(inode, name_len);
7368 btrfs_i_size_write(inode, name_len - 1);
7369 err = btrfs_update_inode(trans, root, inode);
7374 nr = trans->blocks_used;
7375 btrfs_end_transaction_throttle(trans, root);
7377 inode_dec_link_count(inode);
7380 btrfs_btree_balance_dirty(root, nr);
7384 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7385 u64 start, u64 num_bytes, u64 min_size,
7386 loff_t actual_len, u64 *alloc_hint,
7387 struct btrfs_trans_handle *trans)
7389 struct btrfs_root *root = BTRFS_I(inode)->root;
7390 struct btrfs_key ins;
7391 u64 cur_offset = start;
7394 bool own_trans = true;
7398 while (num_bytes > 0) {
7400 trans = btrfs_start_transaction(root, 3);
7401 if (IS_ERR(trans)) {
7402 ret = PTR_ERR(trans);
7407 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7408 0, *alloc_hint, (u64)-1, &ins, 1);
7411 btrfs_end_transaction(trans, root);
7415 ret = insert_reserved_file_extent(trans, inode,
7416 cur_offset, ins.objectid,
7417 ins.offset, ins.offset,
7418 ins.offset, 0, 0, 0,
7419 BTRFS_FILE_EXTENT_PREALLOC);
7421 btrfs_drop_extent_cache(inode, cur_offset,
7422 cur_offset + ins.offset -1, 0);
7424 num_bytes -= ins.offset;
7425 cur_offset += ins.offset;
7426 *alloc_hint = ins.objectid + ins.offset;
7428 inode->i_ctime = CURRENT_TIME;
7429 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
7430 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
7431 (actual_len > inode->i_size) &&
7432 (cur_offset > inode->i_size)) {
7433 if (cur_offset > actual_len)
7434 i_size = actual_len;
7436 i_size = cur_offset;
7437 i_size_write(inode, i_size);
7438 btrfs_ordered_update_i_size(inode, i_size, NULL);
7441 ret = btrfs_update_inode(trans, root, inode);
7445 btrfs_end_transaction(trans, root);
7450 int btrfs_prealloc_file_range(struct inode *inode, int mode,
7451 u64 start, u64 num_bytes, u64 min_size,
7452 loff_t actual_len, u64 *alloc_hint)
7454 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7455 min_size, actual_len, alloc_hint,
7459 int btrfs_prealloc_file_range_trans(struct inode *inode,
7460 struct btrfs_trans_handle *trans, int mode,
7461 u64 start, u64 num_bytes, u64 min_size,
7462 loff_t actual_len, u64 *alloc_hint)
7464 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7465 min_size, actual_len, alloc_hint, trans);
7468 static int btrfs_set_page_dirty(struct page *page)
7470 return __set_page_dirty_nobuffers(page);
7473 static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
7475 struct btrfs_root *root = BTRFS_I(inode)->root;
7477 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7479 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
7481 return generic_permission(inode, mask, flags, btrfs_check_acl);
7484 static const struct inode_operations btrfs_dir_inode_operations = {
7485 .getattr = btrfs_getattr,
7486 .lookup = btrfs_lookup,
7487 .create = btrfs_create,
7488 .unlink = btrfs_unlink,
7490 .mkdir = btrfs_mkdir,
7491 .rmdir = btrfs_rmdir,
7492 .rename = btrfs_rename,
7493 .symlink = btrfs_symlink,
7494 .setattr = btrfs_setattr,
7495 .mknod = btrfs_mknod,
7496 .setxattr = btrfs_setxattr,
7497 .getxattr = btrfs_getxattr,
7498 .listxattr = btrfs_listxattr,
7499 .removexattr = btrfs_removexattr,
7500 .permission = btrfs_permission,
7502 static const struct inode_operations btrfs_dir_ro_inode_operations = {
7503 .lookup = btrfs_lookup,
7504 .permission = btrfs_permission,
7507 static const struct file_operations btrfs_dir_file_operations = {
7508 .llseek = generic_file_llseek,
7509 .read = generic_read_dir,
7510 .readdir = btrfs_real_readdir,
7511 .unlocked_ioctl = btrfs_ioctl,
7512 #ifdef CONFIG_COMPAT
7513 .compat_ioctl = btrfs_ioctl,
7515 .release = btrfs_release_file,
7516 .fsync = btrfs_sync_file,
7519 static struct extent_io_ops btrfs_extent_io_ops = {
7520 .fill_delalloc = run_delalloc_range,
7521 .submit_bio_hook = btrfs_submit_bio_hook,
7522 .merge_bio_hook = btrfs_merge_bio_hook,
7523 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7524 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7525 .writepage_start_hook = btrfs_writepage_start_hook,
7526 .readpage_io_failed_hook = btrfs_io_failed_hook,
7527 .set_bit_hook = btrfs_set_bit_hook,
7528 .clear_bit_hook = btrfs_clear_bit_hook,
7529 .merge_extent_hook = btrfs_merge_extent_hook,
7530 .split_extent_hook = btrfs_split_extent_hook,
7534 * btrfs doesn't support the bmap operation because swapfiles
7535 * use bmap to make a mapping of extents in the file. They assume
7536 * these extents won't change over the life of the file and they
7537 * use the bmap result to do IO directly to the drive.
7539 * the btrfs bmap call would return logical addresses that aren't
7540 * suitable for IO and they also will change frequently as COW
7541 * operations happen. So, swapfile + btrfs == corruption.
7543 * For now we're avoiding this by dropping bmap.
7545 static const struct address_space_operations btrfs_aops = {
7546 .readpage = btrfs_readpage,
7547 .writepage = btrfs_writepage,
7548 .writepages = btrfs_writepages,
7549 .readpages = btrfs_readpages,
7550 .sync_page = block_sync_page,
7551 .direct_IO = btrfs_direct_IO,
7552 .invalidatepage = btrfs_invalidatepage,
7553 .releasepage = btrfs_releasepage,
7554 .set_page_dirty = btrfs_set_page_dirty,
7555 .error_remove_page = generic_error_remove_page,
7558 static const struct address_space_operations btrfs_symlink_aops = {
7559 .readpage = btrfs_readpage,
7560 .writepage = btrfs_writepage,
7561 .invalidatepage = btrfs_invalidatepage,
7562 .releasepage = btrfs_releasepage,
7565 static const struct inode_operations btrfs_file_inode_operations = {
7566 .getattr = btrfs_getattr,
7567 .setattr = btrfs_setattr,
7568 .setxattr = btrfs_setxattr,
7569 .getxattr = btrfs_getxattr,
7570 .listxattr = btrfs_listxattr,
7571 .removexattr = btrfs_removexattr,
7572 .permission = btrfs_permission,
7573 .fiemap = btrfs_fiemap,
7575 static const struct inode_operations btrfs_special_inode_operations = {
7576 .getattr = btrfs_getattr,
7577 .setattr = btrfs_setattr,
7578 .permission = btrfs_permission,
7579 .setxattr = btrfs_setxattr,
7580 .getxattr = btrfs_getxattr,
7581 .listxattr = btrfs_listxattr,
7582 .removexattr = btrfs_removexattr,
7584 static const struct inode_operations btrfs_symlink_inode_operations = {
7585 .readlink = generic_readlink,
7586 .follow_link = page_follow_link_light,
7587 .put_link = page_put_link,
7588 .getattr = btrfs_getattr,
7589 .permission = btrfs_permission,
7590 .setxattr = btrfs_setxattr,
7591 .getxattr = btrfs_getxattr,
7592 .listxattr = btrfs_listxattr,
7593 .removexattr = btrfs_removexattr,
7596 const struct dentry_operations btrfs_dentry_operations = {
7597 .d_delete = btrfs_dentry_delete,