2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
45 #include "transaction.h"
46 #include "btrfs_inode.h"
48 #include "print-tree.h"
49 #include "ordered-data.h"
53 #include "compression.h"
55 #include "free-space-cache.h"
56 #include "inode-map.h"
58 struct btrfs_iget_args {
60 struct btrfs_root *root;
63 static const struct inode_operations btrfs_dir_inode_operations;
64 static const struct inode_operations btrfs_symlink_inode_operations;
65 static const struct inode_operations btrfs_dir_ro_inode_operations;
66 static const struct inode_operations btrfs_special_inode_operations;
67 static const struct inode_operations btrfs_file_inode_operations;
68 static const struct address_space_operations btrfs_aops;
69 static const struct address_space_operations btrfs_symlink_aops;
70 static const struct file_operations btrfs_dir_file_operations;
71 static struct extent_io_ops btrfs_extent_io_ops;
73 static struct kmem_cache *btrfs_inode_cachep;
74 struct kmem_cache *btrfs_trans_handle_cachep;
75 struct kmem_cache *btrfs_transaction_cachep;
76 struct kmem_cache *btrfs_path_cachep;
77 struct kmem_cache *btrfs_free_space_cachep;
80 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
90 static int btrfs_setsize(struct inode *inode, loff_t newsize);
91 static int btrfs_truncate(struct inode *inode);
92 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
93 static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
97 static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode);
100 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
101 struct inode *inode, struct inode *dir,
102 const struct qstr *qstr)
106 err = btrfs_init_acl(trans, inode, dir);
108 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
117 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root, struct inode *inode,
119 u64 start, size_t size, size_t compressed_size,
121 struct page **compressed_pages)
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 struct extent_buffer *leaf;
126 struct page *page = NULL;
129 struct btrfs_file_extent_item *ei;
132 size_t cur_size = size;
134 unsigned long offset;
136 if (compressed_size && compressed_pages)
137 cur_size = compressed_size;
139 path = btrfs_alloc_path();
143 path->leave_spinning = 1;
145 key.objectid = btrfs_ino(inode);
147 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
148 datasize = btrfs_file_extent_calc_inline_size(cur_size);
150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
167 if (compress_type != BTRFS_COMPRESS_NONE) {
170 while (compressed_size > 0) {
171 cpage = compressed_pages[i];
172 cur_size = min_t(unsigned long, compressed_size,
175 kaddr = kmap_atomic(cpage, KM_USER0);
176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
177 kunmap_atomic(kaddr, KM_USER0);
181 compressed_size -= cur_size;
183 btrfs_set_file_extent_compression(leaf, ei,
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
189 kaddr = kmap_atomic(page, KM_USER0);
190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
192 kunmap_atomic(kaddr, KM_USER0);
193 page_cache_release(page);
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
207 BTRFS_I(inode)->disk_i_size = inode->i_size;
208 ret = btrfs_update_inode(trans, root, inode);
212 btrfs_free_path(path);
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
222 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
225 size_t compressed_size, int compress_type,
226 struct page **compressed_pages)
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
234 u64 data_len = inline_len;
238 data_len = compressed_size;
241 actual_end >= PAGE_CACHE_SIZE ||
242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
244 (actual_end & (root->sectorsize - 1)) == 0) ||
246 data_len > root->fs_info->max_inline) {
250 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
259 compress_type, compressed_pages);
261 btrfs_abort_transaction(trans, root, ret);
264 btrfs_delalloc_release_metadata(inode, end + 1 - start);
265 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
269 struct async_extent {
274 unsigned long nr_pages;
276 struct list_head list;
281 struct btrfs_root *root;
282 struct page *locked_page;
285 struct list_head extents;
286 struct btrfs_work work;
289 static noinline int add_async_extent(struct async_cow *cow,
290 u64 start, u64 ram_size,
293 unsigned long nr_pages,
296 struct async_extent *async_extent;
298 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
299 BUG_ON(!async_extent); /* -ENOMEM */
300 async_extent->start = start;
301 async_extent->ram_size = ram_size;
302 async_extent->compressed_size = compressed_size;
303 async_extent->pages = pages;
304 async_extent->nr_pages = nr_pages;
305 async_extent->compress_type = compress_type;
306 list_add_tail(&async_extent->list, &cow->extents);
311 * we create compressed extents in two phases. The first
312 * phase compresses a range of pages that have already been
313 * locked (both pages and state bits are locked).
315 * This is done inside an ordered work queue, and the compression
316 * is spread across many cpus. The actual IO submission is step
317 * two, and the ordered work queue takes care of making sure that
318 * happens in the same order things were put onto the queue by
319 * writepages and friends.
321 * If this code finds it can't get good compression, it puts an
322 * entry onto the work queue to write the uncompressed bytes. This
323 * makes sure that both compressed inodes and uncompressed inodes
324 * are written in the same order that pdflush sent them down.
326 static noinline int compress_file_range(struct inode *inode,
327 struct page *locked_page,
329 struct async_cow *async_cow,
332 struct btrfs_root *root = BTRFS_I(inode)->root;
333 struct btrfs_trans_handle *trans;
335 u64 blocksize = root->sectorsize;
337 u64 isize = i_size_read(inode);
339 struct page **pages = NULL;
340 unsigned long nr_pages;
341 unsigned long nr_pages_ret = 0;
342 unsigned long total_compressed = 0;
343 unsigned long total_in = 0;
344 unsigned long max_compressed = 128 * 1024;
345 unsigned long max_uncompressed = 128 * 1024;
348 int compress_type = root->fs_info->compress_type;
350 /* if this is a small write inside eof, kick off a defragbot */
351 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
352 btrfs_add_inode_defrag(NULL, inode);
354 actual_end = min_t(u64, isize, end + 1);
357 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
358 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
361 * we don't want to send crud past the end of i_size through
362 * compression, that's just a waste of CPU time. So, if the
363 * end of the file is before the start of our current
364 * requested range of bytes, we bail out to the uncompressed
365 * cleanup code that can deal with all of this.
367 * It isn't really the fastest way to fix things, but this is a
368 * very uncommon corner.
370 if (actual_end <= start)
371 goto cleanup_and_bail_uncompressed;
373 total_compressed = actual_end - start;
375 /* we want to make sure that amount of ram required to uncompress
376 * an extent is reasonable, so we limit the total size in ram
377 * of a compressed extent to 128k. This is a crucial number
378 * because it also controls how easily we can spread reads across
379 * cpus for decompression.
381 * We also want to make sure the amount of IO required to do
382 * a random read is reasonably small, so we limit the size of
383 * a compressed extent to 128k.
385 total_compressed = min(total_compressed, max_uncompressed);
386 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
387 num_bytes = max(blocksize, num_bytes);
392 * we do compression for mount -o compress and when the
393 * inode has not been flagged as nocompress. This flag can
394 * change at any time if we discover bad compression ratios.
396 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
397 (btrfs_test_opt(root, COMPRESS) ||
398 (BTRFS_I(inode)->force_compress) ||
399 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
401 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
403 /* just bail out to the uncompressed code */
407 if (BTRFS_I(inode)->force_compress)
408 compress_type = BTRFS_I(inode)->force_compress;
410 ret = btrfs_compress_pages(compress_type,
411 inode->i_mapping, start,
412 total_compressed, pages,
413 nr_pages, &nr_pages_ret,
419 unsigned long offset = total_compressed &
420 (PAGE_CACHE_SIZE - 1);
421 struct page *page = pages[nr_pages_ret - 1];
424 /* zero the tail end of the last page, we might be
425 * sending it down to disk
428 kaddr = kmap_atomic(page, KM_USER0);
429 memset(kaddr + offset, 0,
430 PAGE_CACHE_SIZE - offset);
431 kunmap_atomic(kaddr, KM_USER0);
438 trans = btrfs_join_transaction(root);
440 ret = PTR_ERR(trans);
442 goto cleanup_and_out;
444 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
446 /* lets try to make an inline extent */
447 if (ret || total_in < (actual_end - start)) {
448 /* we didn't compress the entire range, try
449 * to make an uncompressed inline extent.
451 ret = cow_file_range_inline(trans, root, inode,
452 start, end, 0, 0, NULL);
454 /* try making a compressed inline extent */
455 ret = cow_file_range_inline(trans, root, inode,
458 compress_type, pages);
462 * inline extent creation worked or returned error,
463 * we don't need to create any more async work items.
464 * Unlock and free up our temp pages.
466 extent_clear_unlock_delalloc(inode,
467 &BTRFS_I(inode)->io_tree,
469 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
470 EXTENT_CLEAR_DELALLOC |
471 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
473 btrfs_end_transaction(trans, root);
476 btrfs_end_transaction(trans, root);
481 * we aren't doing an inline extent round the compressed size
482 * up to a block size boundary so the allocator does sane
485 total_compressed = (total_compressed + blocksize - 1) &
489 * one last check to make sure the compression is really a
490 * win, compare the page count read with the blocks on disk
492 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
493 ~(PAGE_CACHE_SIZE - 1);
494 if (total_compressed >= total_in) {
497 num_bytes = total_in;
500 if (!will_compress && pages) {
502 * the compression code ran but failed to make things smaller,
503 * free any pages it allocated and our page pointer array
505 for (i = 0; i < nr_pages_ret; i++) {
506 WARN_ON(pages[i]->mapping);
507 page_cache_release(pages[i]);
511 total_compressed = 0;
514 /* flag the file so we don't compress in the future */
515 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
516 !(BTRFS_I(inode)->force_compress)) {
517 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
523 /* the async work queues will take care of doing actual
524 * allocation on disk for these compressed pages,
525 * and will submit them to the elevator.
527 add_async_extent(async_cow, start, num_bytes,
528 total_compressed, pages, nr_pages_ret,
531 if (start + num_bytes < end) {
538 cleanup_and_bail_uncompressed:
540 * No compression, but we still need to write the pages in
541 * the file we've been given so far. redirty the locked
542 * page if it corresponds to our extent and set things up
543 * for the async work queue to run cow_file_range to do
544 * the normal delalloc dance
546 if (page_offset(locked_page) >= start &&
547 page_offset(locked_page) <= end) {
548 __set_page_dirty_nobuffers(locked_page);
549 /* unlocked later on in the async handlers */
551 add_async_extent(async_cow, start, end - start + 1,
552 0, NULL, 0, BTRFS_COMPRESS_NONE);
560 for (i = 0; i < nr_pages_ret; i++) {
561 WARN_ON(pages[i]->mapping);
562 page_cache_release(pages[i]);
569 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
571 EXTENT_CLEAR_UNLOCK_PAGE |
573 EXTENT_CLEAR_DELALLOC |
574 EXTENT_SET_WRITEBACK |
575 EXTENT_END_WRITEBACK);
576 if (!trans || IS_ERR(trans))
577 btrfs_error(root->fs_info, ret, "Failed to join transaction");
579 btrfs_abort_transaction(trans, root, ret);
584 * phase two of compressed writeback. This is the ordered portion
585 * of the code, which only gets called in the order the work was
586 * queued. We walk all the async extents created by compress_file_range
587 * and send them down to the disk.
589 static noinline int submit_compressed_extents(struct inode *inode,
590 struct async_cow *async_cow)
592 struct async_extent *async_extent;
594 struct btrfs_trans_handle *trans;
595 struct btrfs_key ins;
596 struct extent_map *em;
597 struct btrfs_root *root = BTRFS_I(inode)->root;
598 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
599 struct extent_io_tree *io_tree;
602 if (list_empty(&async_cow->extents))
606 while (!list_empty(&async_cow->extents)) {
607 async_extent = list_entry(async_cow->extents.next,
608 struct async_extent, list);
609 list_del(&async_extent->list);
611 io_tree = &BTRFS_I(inode)->io_tree;
614 /* did the compression code fall back to uncompressed IO? */
615 if (!async_extent->pages) {
616 int page_started = 0;
617 unsigned long nr_written = 0;
619 lock_extent(io_tree, async_extent->start,
620 async_extent->start +
621 async_extent->ram_size - 1);
623 /* allocate blocks */
624 ret = cow_file_range(inode, async_cow->locked_page,
626 async_extent->start +
627 async_extent->ram_size - 1,
628 &page_started, &nr_written, 0);
633 * if page_started, cow_file_range inserted an
634 * inline extent and took care of all the unlocking
635 * and IO for us. Otherwise, we need to submit
636 * all those pages down to the drive.
638 if (!page_started && !ret)
639 extent_write_locked_range(io_tree,
640 inode, async_extent->start,
641 async_extent->start +
642 async_extent->ram_size - 1,
650 lock_extent(io_tree, async_extent->start,
651 async_extent->start + async_extent->ram_size - 1);
653 trans = btrfs_join_transaction(root);
655 ret = PTR_ERR(trans);
657 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
658 ret = btrfs_reserve_extent(trans, root,
659 async_extent->compressed_size,
660 async_extent->compressed_size,
664 btrfs_abort_transaction(trans, root, ret);
665 btrfs_end_transaction(trans, root);
670 for (i = 0; i < async_extent->nr_pages; i++) {
671 WARN_ON(async_extent->pages[i]->mapping);
672 page_cache_release(async_extent->pages[i]);
674 kfree(async_extent->pages);
675 async_extent->nr_pages = 0;
676 async_extent->pages = NULL;
677 unlock_extent(io_tree, async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1);
682 goto out_free; /* JDM: Requeue? */
686 * here we're doing allocation and writeback of the
689 btrfs_drop_extent_cache(inode, async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1, 0);
693 em = alloc_extent_map();
694 BUG_ON(!em); /* -ENOMEM */
695 em->start = async_extent->start;
696 em->len = async_extent->ram_size;
697 em->orig_start = em->start;
699 em->block_start = ins.objectid;
700 em->block_len = ins.offset;
701 em->bdev = root->fs_info->fs_devices->latest_bdev;
702 em->compress_type = async_extent->compress_type;
703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
704 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
707 write_lock(&em_tree->lock);
708 ret = add_extent_mapping(em_tree, em);
709 write_unlock(&em_tree->lock);
710 if (ret != -EEXIST) {
714 btrfs_drop_extent_cache(inode, async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1, 0);
719 ret = btrfs_add_ordered_extent_compress(inode,
722 async_extent->ram_size,
724 BTRFS_ORDERED_COMPRESSED,
725 async_extent->compress_type);
726 BUG_ON(ret); /* -ENOMEM */
729 * clear dirty, set writeback and unlock the pages.
731 extent_clear_unlock_delalloc(inode,
732 &BTRFS_I(inode)->io_tree,
734 async_extent->start +
735 async_extent->ram_size - 1,
736 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
737 EXTENT_CLEAR_UNLOCK |
738 EXTENT_CLEAR_DELALLOC |
739 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
741 ret = btrfs_submit_compressed_write(inode,
743 async_extent->ram_size,
745 ins.offset, async_extent->pages,
746 async_extent->nr_pages);
748 BUG_ON(ret); /* -ENOMEM */
749 alloc_hint = ins.objectid + ins.offset;
761 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 struct extent_map *em;
768 read_lock(&em_tree->lock);
769 em = search_extent_mapping(em_tree, start, num_bytes);
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
784 alloc_hint = em->block_start;
788 read_unlock(&em_tree->lock);
794 * when extent_io.c finds a delayed allocation range in the file,
795 * the call backs end up in this code. The basic idea is to
796 * allocate extents on disk for the range, and create ordered data structs
797 * in ram to track those extents.
799 * locked_page is the page that writepage had locked already. We use
800 * it to make sure we don't do extra locks or unlocks.
802 * *page_started is set to one if we unlock locked_page and do everything
803 * required to start IO on it. It may be clean and already done with
806 static noinline int cow_file_range(struct inode *inode,
807 struct page *locked_page,
808 u64 start, u64 end, int *page_started,
809 unsigned long *nr_written,
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
816 unsigned long ram_size;
819 u64 blocksize = root->sectorsize;
820 struct btrfs_key ins;
821 struct extent_map *em;
822 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
825 BUG_ON(btrfs_is_free_space_inode(root, inode));
826 trans = btrfs_join_transaction(root);
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes = max(blocksize, num_bytes);
843 disk_num_bytes = num_bytes;
846 /* if this is a small write inside eof, kick off defrag */
847 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
848 btrfs_add_inode_defrag(trans, inode);
851 /* lets try to make an inline extent */
852 ret = cow_file_range_inline(trans, root, inode,
853 start, end, 0, 0, NULL);
855 extent_clear_unlock_delalloc(inode,
856 &BTRFS_I(inode)->io_tree,
858 EXTENT_CLEAR_UNLOCK_PAGE |
859 EXTENT_CLEAR_UNLOCK |
860 EXTENT_CLEAR_DELALLOC |
862 EXTENT_SET_WRITEBACK |
863 EXTENT_END_WRITEBACK);
865 *nr_written = *nr_written +
866 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
869 } else if (ret < 0) {
870 btrfs_abort_transaction(trans, root, ret);
875 BUG_ON(disk_num_bytes >
876 btrfs_super_total_bytes(root->fs_info->super_copy));
878 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
879 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
881 while (disk_num_bytes > 0) {
884 cur_alloc_size = disk_num_bytes;
885 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
886 root->sectorsize, 0, alloc_hint,
889 btrfs_abort_transaction(trans, root, ret);
893 em = alloc_extent_map();
894 BUG_ON(!em); /* -ENOMEM */
896 em->orig_start = em->start;
897 ram_size = ins.offset;
898 em->len = ins.offset;
900 em->block_start = ins.objectid;
901 em->block_len = ins.offset;
902 em->bdev = root->fs_info->fs_devices->latest_bdev;
903 set_bit(EXTENT_FLAG_PINNED, &em->flags);
906 write_lock(&em_tree->lock);
907 ret = add_extent_mapping(em_tree, em);
908 write_unlock(&em_tree->lock);
909 if (ret != -EEXIST) {
913 btrfs_drop_extent_cache(inode, start,
914 start + ram_size - 1, 0);
917 cur_alloc_size = ins.offset;
918 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
919 ram_size, cur_alloc_size, 0);
920 BUG_ON(ret); /* -ENOMEM */
922 if (root->root_key.objectid ==
923 BTRFS_DATA_RELOC_TREE_OBJECTID) {
924 ret = btrfs_reloc_clone_csums(inode, start,
927 btrfs_abort_transaction(trans, root, ret);
932 if (disk_num_bytes < cur_alloc_size)
935 /* we're not doing compressed IO, don't unlock the first
936 * page (which the caller expects to stay locked), don't
937 * clear any dirty bits and don't set any writeback bits
939 * Do set the Private2 bit so we know this page was properly
940 * setup for writepage
942 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
943 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
946 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
947 start, start + ram_size - 1,
949 disk_num_bytes -= cur_alloc_size;
950 num_bytes -= cur_alloc_size;
951 alloc_hint = ins.objectid + ins.offset;
952 start += cur_alloc_size;
956 btrfs_end_transaction(trans, root);
960 extent_clear_unlock_delalloc(inode,
961 &BTRFS_I(inode)->io_tree,
963 EXTENT_CLEAR_UNLOCK_PAGE |
964 EXTENT_CLEAR_UNLOCK |
965 EXTENT_CLEAR_DELALLOC |
967 EXTENT_SET_WRITEBACK |
968 EXTENT_END_WRITEBACK);
974 * work queue call back to started compression on a file and pages
976 static noinline void async_cow_start(struct btrfs_work *work)
978 struct async_cow *async_cow;
980 async_cow = container_of(work, struct async_cow, work);
982 compress_file_range(async_cow->inode, async_cow->locked_page,
983 async_cow->start, async_cow->end, async_cow,
986 async_cow->inode = NULL;
990 * work queue call back to submit previously compressed pages
992 static noinline void async_cow_submit(struct btrfs_work *work)
994 struct async_cow *async_cow;
995 struct btrfs_root *root;
996 unsigned long nr_pages;
998 async_cow = container_of(work, struct async_cow, work);
1000 root = async_cow->root;
1001 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1004 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1006 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1008 waitqueue_active(&root->fs_info->async_submit_wait))
1009 wake_up(&root->fs_info->async_submit_wait);
1011 if (async_cow->inode)
1012 submit_compressed_extents(async_cow->inode, async_cow);
1015 static noinline void async_cow_free(struct btrfs_work *work)
1017 struct async_cow *async_cow;
1018 async_cow = container_of(work, struct async_cow, work);
1022 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1023 u64 start, u64 end, int *page_started,
1024 unsigned long *nr_written)
1026 struct async_cow *async_cow;
1027 struct btrfs_root *root = BTRFS_I(inode)->root;
1028 unsigned long nr_pages;
1030 int limit = 10 * 1024 * 1042;
1032 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1033 1, 0, NULL, GFP_NOFS);
1034 while (start < end) {
1035 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1036 BUG_ON(!async_cow); /* -ENOMEM */
1037 async_cow->inode = inode;
1038 async_cow->root = root;
1039 async_cow->locked_page = locked_page;
1040 async_cow->start = start;
1042 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
1045 cur_end = min(end, start + 512 * 1024 - 1);
1047 async_cow->end = cur_end;
1048 INIT_LIST_HEAD(&async_cow->extents);
1050 async_cow->work.func = async_cow_start;
1051 async_cow->work.ordered_func = async_cow_submit;
1052 async_cow->work.ordered_free = async_cow_free;
1053 async_cow->work.flags = 0;
1055 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1057 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1059 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1062 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1063 wait_event(root->fs_info->async_submit_wait,
1064 (atomic_read(&root->fs_info->async_delalloc_pages) <
1068 while (atomic_read(&root->fs_info->async_submit_draining) &&
1069 atomic_read(&root->fs_info->async_delalloc_pages)) {
1070 wait_event(root->fs_info->async_submit_wait,
1071 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1075 *nr_written += nr_pages;
1076 start = cur_end + 1;
1082 static noinline int csum_exist_in_range(struct btrfs_root *root,
1083 u64 bytenr, u64 num_bytes)
1086 struct btrfs_ordered_sum *sums;
1089 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1090 bytenr + num_bytes - 1, &list, 0);
1091 if (ret == 0 && list_empty(&list))
1094 while (!list_empty(&list)) {
1095 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1096 list_del(&sums->list);
1103 * when nowcow writeback call back. This checks for snapshots or COW copies
1104 * of the extents that exist in the file, and COWs the file as required.
1106 * If no cow copies or snapshots exist, we write directly to the existing
1109 static noinline int run_delalloc_nocow(struct inode *inode,
1110 struct page *locked_page,
1111 u64 start, u64 end, int *page_started, int force,
1112 unsigned long *nr_written)
1114 struct btrfs_root *root = BTRFS_I(inode)->root;
1115 struct btrfs_trans_handle *trans;
1116 struct extent_buffer *leaf;
1117 struct btrfs_path *path;
1118 struct btrfs_file_extent_item *fi;
1119 struct btrfs_key found_key;
1132 u64 ino = btrfs_ino(inode);
1134 path = btrfs_alloc_path();
1138 nolock = btrfs_is_free_space_inode(root, inode);
1141 trans = btrfs_join_transaction_nolock(root);
1143 trans = btrfs_join_transaction(root);
1145 if (IS_ERR(trans)) {
1146 btrfs_free_path(path);
1147 return PTR_ERR(trans);
1150 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1152 cow_start = (u64)-1;
1155 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1158 btrfs_abort_transaction(trans, root, ret);
1161 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1162 leaf = path->nodes[0];
1163 btrfs_item_key_to_cpu(leaf, &found_key,
1164 path->slots[0] - 1);
1165 if (found_key.objectid == ino &&
1166 found_key.type == BTRFS_EXTENT_DATA_KEY)
1171 leaf = path->nodes[0];
1172 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1173 ret = btrfs_next_leaf(root, path);
1175 btrfs_abort_transaction(trans, root, ret);
1180 leaf = path->nodes[0];
1186 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1188 if (found_key.objectid > ino ||
1189 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1190 found_key.offset > end)
1193 if (found_key.offset > cur_offset) {
1194 extent_end = found_key.offset;
1199 fi = btrfs_item_ptr(leaf, path->slots[0],
1200 struct btrfs_file_extent_item);
1201 extent_type = btrfs_file_extent_type(leaf, fi);
1203 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1204 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1205 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1206 extent_offset = btrfs_file_extent_offset(leaf, fi);
1207 extent_end = found_key.offset +
1208 btrfs_file_extent_num_bytes(leaf, fi);
1209 if (extent_end <= start) {
1213 if (disk_bytenr == 0)
1215 if (btrfs_file_extent_compression(leaf, fi) ||
1216 btrfs_file_extent_encryption(leaf, fi) ||
1217 btrfs_file_extent_other_encoding(leaf, fi))
1219 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1221 if (btrfs_extent_readonly(root, disk_bytenr))
1223 if (btrfs_cross_ref_exist(trans, root, ino,
1225 extent_offset, disk_bytenr))
1227 disk_bytenr += extent_offset;
1228 disk_bytenr += cur_offset - found_key.offset;
1229 num_bytes = min(end + 1, extent_end) - cur_offset;
1231 * force cow if csum exists in the range.
1232 * this ensure that csum for a given extent are
1233 * either valid or do not exist.
1235 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1238 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1239 extent_end = found_key.offset +
1240 btrfs_file_extent_inline_len(leaf, fi);
1241 extent_end = ALIGN(extent_end, root->sectorsize);
1246 if (extent_end <= start) {
1251 if (cow_start == (u64)-1)
1252 cow_start = cur_offset;
1253 cur_offset = extent_end;
1254 if (cur_offset > end)
1260 btrfs_release_path(path);
1261 if (cow_start != (u64)-1) {
1262 ret = cow_file_range(inode, locked_page, cow_start,
1263 found_key.offset - 1, page_started,
1266 btrfs_abort_transaction(trans, root, ret);
1269 cow_start = (u64)-1;
1272 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1273 struct extent_map *em;
1274 struct extent_map_tree *em_tree;
1275 em_tree = &BTRFS_I(inode)->extent_tree;
1276 em = alloc_extent_map();
1277 BUG_ON(!em); /* -ENOMEM */
1278 em->start = cur_offset;
1279 em->orig_start = em->start;
1280 em->len = num_bytes;
1281 em->block_len = num_bytes;
1282 em->block_start = disk_bytenr;
1283 em->bdev = root->fs_info->fs_devices->latest_bdev;
1284 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1286 write_lock(&em_tree->lock);
1287 ret = add_extent_mapping(em_tree, em);
1288 write_unlock(&em_tree->lock);
1289 if (ret != -EEXIST) {
1290 free_extent_map(em);
1293 btrfs_drop_extent_cache(inode, em->start,
1294 em->start + em->len - 1, 0);
1296 type = BTRFS_ORDERED_PREALLOC;
1298 type = BTRFS_ORDERED_NOCOW;
1301 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1302 num_bytes, num_bytes, type);
1303 BUG_ON(ret); /* -ENOMEM */
1305 if (root->root_key.objectid ==
1306 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1307 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1310 btrfs_abort_transaction(trans, root, ret);
1315 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1316 cur_offset, cur_offset + num_bytes - 1,
1317 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1318 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1319 EXTENT_SET_PRIVATE2);
1320 cur_offset = extent_end;
1321 if (cur_offset > end)
1324 btrfs_release_path(path);
1326 if (cur_offset <= end && cow_start == (u64)-1)
1327 cow_start = cur_offset;
1328 if (cow_start != (u64)-1) {
1329 ret = cow_file_range(inode, locked_page, cow_start, end,
1330 page_started, nr_written, 1);
1332 btrfs_abort_transaction(trans, root, ret);
1339 err = btrfs_end_transaction_nolock(trans, root);
1341 err = btrfs_end_transaction(trans, root);
1346 btrfs_free_path(path);
1351 * extent_io.c call back to do delayed allocation processing
1353 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1354 u64 start, u64 end, int *page_started,
1355 unsigned long *nr_written)
1358 struct btrfs_root *root = BTRFS_I(inode)->root;
1360 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1361 ret = run_delalloc_nocow(inode, locked_page, start, end,
1362 page_started, 1, nr_written);
1363 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1364 ret = run_delalloc_nocow(inode, locked_page, start, end,
1365 page_started, 0, nr_written);
1366 else if (!btrfs_test_opt(root, COMPRESS) &&
1367 !(BTRFS_I(inode)->force_compress) &&
1368 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1369 ret = cow_file_range(inode, locked_page, start, end,
1370 page_started, nr_written, 1);
1372 ret = cow_file_range_async(inode, locked_page, start, end,
1373 page_started, nr_written);
1377 static void btrfs_split_extent_hook(struct inode *inode,
1378 struct extent_state *orig, u64 split)
1380 /* not delalloc, ignore it */
1381 if (!(orig->state & EXTENT_DELALLOC))
1384 spin_lock(&BTRFS_I(inode)->lock);
1385 BTRFS_I(inode)->outstanding_extents++;
1386 spin_unlock(&BTRFS_I(inode)->lock);
1390 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1391 * extents so we can keep track of new extents that are just merged onto old
1392 * extents, such as when we are doing sequential writes, so we can properly
1393 * account for the metadata space we'll need.
1395 static void btrfs_merge_extent_hook(struct inode *inode,
1396 struct extent_state *new,
1397 struct extent_state *other)
1399 /* not delalloc, ignore it */
1400 if (!(other->state & EXTENT_DELALLOC))
1403 spin_lock(&BTRFS_I(inode)->lock);
1404 BTRFS_I(inode)->outstanding_extents--;
1405 spin_unlock(&BTRFS_I(inode)->lock);
1409 * extent_io.c set_bit_hook, used to track delayed allocation
1410 * bytes in this file, and to maintain the list of inodes that
1411 * have pending delalloc work to be done.
1413 static void btrfs_set_bit_hook(struct inode *inode,
1414 struct extent_state *state, int *bits)
1418 * set_bit and clear bit hooks normally require _irqsave/restore
1419 * but in this case, we are only testing for the DELALLOC
1420 * bit, which is only set or cleared with irqs on
1422 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1423 struct btrfs_root *root = BTRFS_I(inode)->root;
1424 u64 len = state->end + 1 - state->start;
1425 bool do_list = !btrfs_is_free_space_inode(root, inode);
1427 if (*bits & EXTENT_FIRST_DELALLOC) {
1428 *bits &= ~EXTENT_FIRST_DELALLOC;
1430 spin_lock(&BTRFS_I(inode)->lock);
1431 BTRFS_I(inode)->outstanding_extents++;
1432 spin_unlock(&BTRFS_I(inode)->lock);
1435 spin_lock(&root->fs_info->delalloc_lock);
1436 BTRFS_I(inode)->delalloc_bytes += len;
1437 root->fs_info->delalloc_bytes += len;
1438 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1439 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1440 &root->fs_info->delalloc_inodes);
1442 spin_unlock(&root->fs_info->delalloc_lock);
1447 * extent_io.c clear_bit_hook, see set_bit_hook for why
1449 static void btrfs_clear_bit_hook(struct inode *inode,
1450 struct extent_state *state, int *bits)
1453 * set_bit and clear bit hooks normally require _irqsave/restore
1454 * but in this case, we are only testing for the DELALLOC
1455 * bit, which is only set or cleared with irqs on
1457 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1458 struct btrfs_root *root = BTRFS_I(inode)->root;
1459 u64 len = state->end + 1 - state->start;
1460 bool do_list = !btrfs_is_free_space_inode(root, inode);
1462 if (*bits & EXTENT_FIRST_DELALLOC) {
1463 *bits &= ~EXTENT_FIRST_DELALLOC;
1464 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1465 spin_lock(&BTRFS_I(inode)->lock);
1466 BTRFS_I(inode)->outstanding_extents--;
1467 spin_unlock(&BTRFS_I(inode)->lock);
1470 if (*bits & EXTENT_DO_ACCOUNTING)
1471 btrfs_delalloc_release_metadata(inode, len);
1473 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1475 btrfs_free_reserved_data_space(inode, len);
1477 spin_lock(&root->fs_info->delalloc_lock);
1478 root->fs_info->delalloc_bytes -= len;
1479 BTRFS_I(inode)->delalloc_bytes -= len;
1481 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1482 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1483 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1485 spin_unlock(&root->fs_info->delalloc_lock);
1490 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1491 * we don't create bios that span stripes or chunks
1493 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1494 size_t size, struct bio *bio,
1495 unsigned long bio_flags)
1497 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1498 struct btrfs_mapping_tree *map_tree;
1499 u64 logical = (u64)bio->bi_sector << 9;
1504 if (bio_flags & EXTENT_BIO_COMPRESSED)
1507 length = bio->bi_size;
1508 map_tree = &root->fs_info->mapping_tree;
1509 map_length = length;
1510 ret = btrfs_map_block(map_tree, READ, logical,
1511 &map_length, NULL, 0);
1512 /* Will always return 0 or 1 with map_multi == NULL */
1514 if (map_length < length + size)
1520 * in order to insert checksums into the metadata in large chunks,
1521 * we wait until bio submission time. All the pages in the bio are
1522 * checksummed and sums are attached onto the ordered extent record.
1524 * At IO completion time the cums attached on the ordered extent record
1525 * are inserted into the btree
1527 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1528 struct bio *bio, int mirror_num,
1529 unsigned long bio_flags,
1532 struct btrfs_root *root = BTRFS_I(inode)->root;
1535 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1536 BUG_ON(ret); /* -ENOMEM */
1541 * in order to insert checksums into the metadata in large chunks,
1542 * we wait until bio submission time. All the pages in the bio are
1543 * checksummed and sums are attached onto the ordered extent record.
1545 * At IO completion time the cums attached on the ordered extent record
1546 * are inserted into the btree
1548 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1549 int mirror_num, unsigned long bio_flags,
1552 struct btrfs_root *root = BTRFS_I(inode)->root;
1553 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1557 * extent_io.c submission hook. This does the right thing for csum calculation
1558 * on write, or reading the csums from the tree before a read
1560 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1561 int mirror_num, unsigned long bio_flags,
1564 struct btrfs_root *root = BTRFS_I(inode)->root;
1569 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1571 if (btrfs_is_free_space_inode(root, inode))
1574 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1578 if (!(rw & REQ_WRITE)) {
1579 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1580 return btrfs_submit_compressed_read(inode, bio,
1581 mirror_num, bio_flags);
1582 } else if (!skip_sum) {
1583 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1588 } else if (!skip_sum) {
1589 /* csum items have already been cloned */
1590 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1592 /* we're doing a write, do the async checksumming */
1593 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1594 inode, rw, bio, mirror_num,
1595 bio_flags, bio_offset,
1596 __btrfs_submit_bio_start,
1597 __btrfs_submit_bio_done);
1601 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1605 * given a list of ordered sums record them in the inode. This happens
1606 * at IO completion time based on sums calculated at bio submission time.
1608 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1609 struct inode *inode, u64 file_offset,
1610 struct list_head *list)
1612 struct btrfs_ordered_sum *sum;
1614 list_for_each_entry(sum, list, list) {
1615 btrfs_csum_file_blocks(trans,
1616 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1621 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1622 struct extent_state **cached_state)
1624 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1626 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1627 cached_state, GFP_NOFS);
1630 /* see btrfs_writepage_start_hook for details on why this is required */
1631 struct btrfs_writepage_fixup {
1633 struct btrfs_work work;
1636 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1638 struct btrfs_writepage_fixup *fixup;
1639 struct btrfs_ordered_extent *ordered;
1640 struct extent_state *cached_state = NULL;
1642 struct inode *inode;
1647 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1651 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1652 ClearPageChecked(page);
1656 inode = page->mapping->host;
1657 page_start = page_offset(page);
1658 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1660 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1663 /* already ordered? We're done */
1664 if (PagePrivate2(page))
1667 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1669 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1670 page_end, &cached_state, GFP_NOFS);
1672 btrfs_start_ordered_extent(inode, ordered, 1);
1673 btrfs_put_ordered_extent(ordered);
1677 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1679 mapping_set_error(page->mapping, ret);
1680 end_extent_writepage(page, ret, page_start, page_end);
1681 ClearPageChecked(page);
1685 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1686 ClearPageChecked(page);
1687 set_page_dirty(page);
1689 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1690 &cached_state, GFP_NOFS);
1693 page_cache_release(page);
1698 * There are a few paths in the higher layers of the kernel that directly
1699 * set the page dirty bit without asking the filesystem if it is a
1700 * good idea. This causes problems because we want to make sure COW
1701 * properly happens and the data=ordered rules are followed.
1703 * In our case any range that doesn't have the ORDERED bit set
1704 * hasn't been properly setup for IO. We kick off an async process
1705 * to fix it up. The async helper will wait for ordered extents, set
1706 * the delalloc bit and make it safe to write the page.
1708 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1710 struct inode *inode = page->mapping->host;
1711 struct btrfs_writepage_fixup *fixup;
1712 struct btrfs_root *root = BTRFS_I(inode)->root;
1714 /* this page is properly in the ordered list */
1715 if (TestClearPagePrivate2(page))
1718 if (PageChecked(page))
1721 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1725 SetPageChecked(page);
1726 page_cache_get(page);
1727 fixup->work.func = btrfs_writepage_fixup_worker;
1729 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1733 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1734 struct inode *inode, u64 file_pos,
1735 u64 disk_bytenr, u64 disk_num_bytes,
1736 u64 num_bytes, u64 ram_bytes,
1737 u8 compression, u8 encryption,
1738 u16 other_encoding, int extent_type)
1740 struct btrfs_root *root = BTRFS_I(inode)->root;
1741 struct btrfs_file_extent_item *fi;
1742 struct btrfs_path *path;
1743 struct extent_buffer *leaf;
1744 struct btrfs_key ins;
1748 path = btrfs_alloc_path();
1752 path->leave_spinning = 1;
1755 * we may be replacing one extent in the tree with another.
1756 * The new extent is pinned in the extent map, and we don't want
1757 * to drop it from the cache until it is completely in the btree.
1759 * So, tell btrfs_drop_extents to leave this extent in the cache.
1760 * the caller is expected to unpin it and allow it to be merged
1763 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1768 ins.objectid = btrfs_ino(inode);
1769 ins.offset = file_pos;
1770 ins.type = BTRFS_EXTENT_DATA_KEY;
1771 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1774 leaf = path->nodes[0];
1775 fi = btrfs_item_ptr(leaf, path->slots[0],
1776 struct btrfs_file_extent_item);
1777 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1778 btrfs_set_file_extent_type(leaf, fi, extent_type);
1779 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1780 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1781 btrfs_set_file_extent_offset(leaf, fi, 0);
1782 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1783 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1784 btrfs_set_file_extent_compression(leaf, fi, compression);
1785 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1786 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1788 btrfs_unlock_up_safe(path, 1);
1789 btrfs_set_lock_blocking(leaf);
1791 btrfs_mark_buffer_dirty(leaf);
1793 inode_add_bytes(inode, num_bytes);
1795 ins.objectid = disk_bytenr;
1796 ins.offset = disk_num_bytes;
1797 ins.type = BTRFS_EXTENT_ITEM_KEY;
1798 ret = btrfs_alloc_reserved_file_extent(trans, root,
1799 root->root_key.objectid,
1800 btrfs_ino(inode), file_pos, &ins);
1802 btrfs_free_path(path);
1808 * helper function for btrfs_finish_ordered_io, this
1809 * just reads in some of the csum leaves to prime them into ram
1810 * before we start the transaction. It limits the amount of btree
1811 * reads required while inside the transaction.
1813 /* as ordered data IO finishes, this gets called so we can finish
1814 * an ordered extent if the range of bytes in the file it covers are
1817 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1819 struct btrfs_root *root = BTRFS_I(inode)->root;
1820 struct btrfs_trans_handle *trans = NULL;
1821 struct btrfs_ordered_extent *ordered_extent = NULL;
1822 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1823 struct extent_state *cached_state = NULL;
1824 int compress_type = 0;
1828 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1832 BUG_ON(!ordered_extent); /* Logic error */
1834 nolock = btrfs_is_free_space_inode(root, inode);
1836 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1837 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
1838 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1841 trans = btrfs_join_transaction_nolock(root);
1843 trans = btrfs_join_transaction(root);
1845 return PTR_ERR(trans);
1846 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1847 ret = btrfs_update_inode_fallback(trans, root, inode);
1848 if (ret) /* -ENOMEM or corruption */
1849 btrfs_abort_transaction(trans, root, ret);
1854 lock_extent_bits(io_tree, ordered_extent->file_offset,
1855 ordered_extent->file_offset + ordered_extent->len - 1,
1859 trans = btrfs_join_transaction_nolock(root);
1861 trans = btrfs_join_transaction(root);
1862 if (IS_ERR(trans)) {
1863 ret = PTR_ERR(trans);
1867 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1869 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1870 compress_type = ordered_extent->compress_type;
1871 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1872 BUG_ON(compress_type);
1873 ret = btrfs_mark_extent_written(trans, inode,
1874 ordered_extent->file_offset,
1875 ordered_extent->file_offset +
1876 ordered_extent->len);
1878 BUG_ON(root == root->fs_info->tree_root);
1879 ret = insert_reserved_file_extent(trans, inode,
1880 ordered_extent->file_offset,
1881 ordered_extent->start,
1882 ordered_extent->disk_len,
1883 ordered_extent->len,
1884 ordered_extent->len,
1885 compress_type, 0, 0,
1886 BTRFS_FILE_EXTENT_REG);
1887 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1888 ordered_extent->file_offset,
1889 ordered_extent->len);
1891 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1892 ordered_extent->file_offset +
1893 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1895 btrfs_abort_transaction(trans, root, ret);
1899 add_pending_csums(trans, inode, ordered_extent->file_offset,
1900 &ordered_extent->list);
1902 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1903 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1904 ret = btrfs_update_inode_fallback(trans, root, inode);
1905 if (ret) { /* -ENOMEM or corruption */
1906 btrfs_abort_transaction(trans, root, ret);
1912 if (root != root->fs_info->tree_root)
1913 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1916 btrfs_end_transaction_nolock(trans, root);
1918 btrfs_end_transaction(trans, root);
1922 btrfs_put_ordered_extent(ordered_extent);
1923 /* once for the tree */
1924 btrfs_put_ordered_extent(ordered_extent);
1928 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1929 ordered_extent->file_offset +
1930 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1934 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1935 struct extent_state *state, int uptodate)
1937 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1939 ClearPagePrivate2(page);
1940 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1944 * when reads are done, we need to check csums to verify the data is correct
1945 * if there's a match, we allow the bio to finish. If not, the code in
1946 * extent_io.c will try to find good copies for us.
1948 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1949 struct extent_state *state)
1951 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1952 struct inode *inode = page->mapping->host;
1953 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1955 u64 private = ~(u32)0;
1957 struct btrfs_root *root = BTRFS_I(inode)->root;
1960 if (PageChecked(page)) {
1961 ClearPageChecked(page);
1965 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1968 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1969 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1970 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1975 if (state && state->start == start) {
1976 private = state->private;
1979 ret = get_state_private(io_tree, start, &private);
1981 kaddr = kmap_atomic(page, KM_USER0);
1985 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1986 btrfs_csum_final(csum, (char *)&csum);
1987 if (csum != private)
1990 kunmap_atomic(kaddr, KM_USER0);
1995 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
1997 (unsigned long long)btrfs_ino(page->mapping->host),
1998 (unsigned long long)start, csum,
1999 (unsigned long long)private);
2000 memset(kaddr + offset, 1, end - start + 1);
2001 flush_dcache_page(page);
2002 kunmap_atomic(kaddr, KM_USER0);
2008 struct delayed_iput {
2009 struct list_head list;
2010 struct inode *inode;
2013 /* JDM: If this is fs-wide, why can't we add a pointer to
2014 * btrfs_inode instead and avoid the allocation? */
2015 void btrfs_add_delayed_iput(struct inode *inode)
2017 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2018 struct delayed_iput *delayed;
2020 if (atomic_add_unless(&inode->i_count, -1, 1))
2023 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2024 delayed->inode = inode;
2026 spin_lock(&fs_info->delayed_iput_lock);
2027 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2028 spin_unlock(&fs_info->delayed_iput_lock);
2031 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2034 struct btrfs_fs_info *fs_info = root->fs_info;
2035 struct delayed_iput *delayed;
2038 spin_lock(&fs_info->delayed_iput_lock);
2039 empty = list_empty(&fs_info->delayed_iputs);
2040 spin_unlock(&fs_info->delayed_iput_lock);
2044 down_read(&root->fs_info->cleanup_work_sem);
2045 spin_lock(&fs_info->delayed_iput_lock);
2046 list_splice_init(&fs_info->delayed_iputs, &list);
2047 spin_unlock(&fs_info->delayed_iput_lock);
2049 while (!list_empty(&list)) {
2050 delayed = list_entry(list.next, struct delayed_iput, list);
2051 list_del(&delayed->list);
2052 iput(delayed->inode);
2055 up_read(&root->fs_info->cleanup_work_sem);
2058 enum btrfs_orphan_cleanup_state {
2059 ORPHAN_CLEANUP_STARTED = 1,
2060 ORPHAN_CLEANUP_DONE = 2,
2064 * This is called in transaction commit time. If there are no orphan
2065 * files in the subvolume, it removes orphan item and frees block_rsv
2068 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2069 struct btrfs_root *root)
2071 struct btrfs_block_rsv *block_rsv;
2074 if (!list_empty(&root->orphan_list) ||
2075 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2078 spin_lock(&root->orphan_lock);
2079 if (!list_empty(&root->orphan_list)) {
2080 spin_unlock(&root->orphan_lock);
2084 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2085 spin_unlock(&root->orphan_lock);
2089 block_rsv = root->orphan_block_rsv;
2090 root->orphan_block_rsv = NULL;
2091 spin_unlock(&root->orphan_lock);
2093 if (root->orphan_item_inserted &&
2094 btrfs_root_refs(&root->root_item) > 0) {
2095 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2096 root->root_key.objectid);
2098 root->orphan_item_inserted = 0;
2102 WARN_ON(block_rsv->size > 0);
2103 btrfs_free_block_rsv(root, block_rsv);
2108 * This creates an orphan entry for the given inode in case something goes
2109 * wrong in the middle of an unlink/truncate.
2111 * NOTE: caller of this function should reserve 5 units of metadata for
2114 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2116 struct btrfs_root *root = BTRFS_I(inode)->root;
2117 struct btrfs_block_rsv *block_rsv = NULL;
2122 if (!root->orphan_block_rsv) {
2123 block_rsv = btrfs_alloc_block_rsv(root);
2128 spin_lock(&root->orphan_lock);
2129 if (!root->orphan_block_rsv) {
2130 root->orphan_block_rsv = block_rsv;
2131 } else if (block_rsv) {
2132 btrfs_free_block_rsv(root, block_rsv);
2136 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2137 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2140 * For proper ENOSPC handling, we should do orphan
2141 * cleanup when mounting. But this introduces backward
2142 * compatibility issue.
2144 if (!xchg(&root->orphan_item_inserted, 1))
2152 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2153 BTRFS_I(inode)->orphan_meta_reserved = 1;
2156 spin_unlock(&root->orphan_lock);
2158 /* grab metadata reservation from transaction handle */
2160 ret = btrfs_orphan_reserve_metadata(trans, inode);
2161 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
2164 /* insert an orphan item to track this unlinked/truncated file */
2166 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2167 if (ret && ret != -EEXIST) {
2168 btrfs_abort_transaction(trans, root, ret);
2174 /* insert an orphan item to track subvolume contains orphan files */
2176 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2177 root->root_key.objectid);
2178 if (ret && ret != -EEXIST) {
2179 btrfs_abort_transaction(trans, root, ret);
2187 * We have done the truncate/delete so we can go ahead and remove the orphan
2188 * item for this particular inode.
2190 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2192 struct btrfs_root *root = BTRFS_I(inode)->root;
2193 int delete_item = 0;
2194 int release_rsv = 0;
2197 spin_lock(&root->orphan_lock);
2198 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2199 list_del_init(&BTRFS_I(inode)->i_orphan);
2203 if (BTRFS_I(inode)->orphan_meta_reserved) {
2204 BTRFS_I(inode)->orphan_meta_reserved = 0;
2207 spin_unlock(&root->orphan_lock);
2209 if (trans && delete_item) {
2210 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2211 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2215 btrfs_orphan_release_metadata(inode);
2221 * this cleans up any orphans that may be left on the list from the last use
2224 int btrfs_orphan_cleanup(struct btrfs_root *root)
2226 struct btrfs_path *path;
2227 struct extent_buffer *leaf;
2228 struct btrfs_key key, found_key;
2229 struct btrfs_trans_handle *trans;
2230 struct inode *inode;
2231 u64 last_objectid = 0;
2232 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2234 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2237 path = btrfs_alloc_path();
2244 key.objectid = BTRFS_ORPHAN_OBJECTID;
2245 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2246 key.offset = (u64)-1;
2249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2254 * if ret == 0 means we found what we were searching for, which
2255 * is weird, but possible, so only screw with path if we didn't
2256 * find the key and see if we have stuff that matches
2260 if (path->slots[0] == 0)
2265 /* pull out the item */
2266 leaf = path->nodes[0];
2267 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2269 /* make sure the item matches what we want */
2270 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2272 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2275 /* release the path since we're done with it */
2276 btrfs_release_path(path);
2279 * this is where we are basically btrfs_lookup, without the
2280 * crossing root thing. we store the inode number in the
2281 * offset of the orphan item.
2284 if (found_key.offset == last_objectid) {
2285 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2286 "stopping orphan cleanup\n");
2291 last_objectid = found_key.offset;
2293 found_key.objectid = found_key.offset;
2294 found_key.type = BTRFS_INODE_ITEM_KEY;
2295 found_key.offset = 0;
2296 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2297 ret = PTR_RET(inode);
2298 if (ret && ret != -ESTALE)
2301 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2302 struct btrfs_root *dead_root;
2303 struct btrfs_fs_info *fs_info = root->fs_info;
2304 int is_dead_root = 0;
2307 * this is an orphan in the tree root. Currently these
2308 * could come from 2 sources:
2309 * a) a snapshot deletion in progress
2310 * b) a free space cache inode
2311 * We need to distinguish those two, as the snapshot
2312 * orphan must not get deleted.
2313 * find_dead_roots already ran before us, so if this
2314 * is a snapshot deletion, we should find the root
2315 * in the dead_roots list
2317 spin_lock(&fs_info->trans_lock);
2318 list_for_each_entry(dead_root, &fs_info->dead_roots,
2320 if (dead_root->root_key.objectid ==
2321 found_key.objectid) {
2326 spin_unlock(&fs_info->trans_lock);
2328 /* prevent this orphan from being found again */
2329 key.offset = found_key.objectid - 1;
2334 * Inode is already gone but the orphan item is still there,
2335 * kill the orphan item.
2337 if (ret == -ESTALE) {
2338 trans = btrfs_start_transaction(root, 1);
2339 if (IS_ERR(trans)) {
2340 ret = PTR_ERR(trans);
2343 ret = btrfs_del_orphan_item(trans, root,
2344 found_key.objectid);
2345 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2346 btrfs_end_transaction(trans, root);
2351 * add this inode to the orphan list so btrfs_orphan_del does
2352 * the proper thing when we hit it
2354 spin_lock(&root->orphan_lock);
2355 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2356 spin_unlock(&root->orphan_lock);
2358 /* if we have links, this was a truncate, lets do that */
2359 if (inode->i_nlink) {
2360 if (!S_ISREG(inode->i_mode)) {
2366 ret = btrfs_truncate(inode);
2371 /* this will do delete_inode and everything for us */
2376 /* release the path since we're done with it */
2377 btrfs_release_path(path);
2379 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2381 if (root->orphan_block_rsv)
2382 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2385 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2386 trans = btrfs_join_transaction(root);
2388 btrfs_end_transaction(trans, root);
2392 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2394 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2398 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2399 btrfs_free_path(path);
2404 * very simple check to peek ahead in the leaf looking for xattrs. If we
2405 * don't find any xattrs, we know there can't be any acls.
2407 * slot is the slot the inode is in, objectid is the objectid of the inode
2409 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2410 int slot, u64 objectid)
2412 u32 nritems = btrfs_header_nritems(leaf);
2413 struct btrfs_key found_key;
2417 while (slot < nritems) {
2418 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2420 /* we found a different objectid, there must not be acls */
2421 if (found_key.objectid != objectid)
2424 /* we found an xattr, assume we've got an acl */
2425 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2429 * we found a key greater than an xattr key, there can't
2430 * be any acls later on
2432 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2439 * it goes inode, inode backrefs, xattrs, extents,
2440 * so if there are a ton of hard links to an inode there can
2441 * be a lot of backrefs. Don't waste time searching too hard,
2442 * this is just an optimization
2447 /* we hit the end of the leaf before we found an xattr or
2448 * something larger than an xattr. We have to assume the inode
2455 * read an inode from the btree into the in-memory inode
2457 static void btrfs_read_locked_inode(struct inode *inode)
2459 struct btrfs_path *path;
2460 struct extent_buffer *leaf;
2461 struct btrfs_inode_item *inode_item;
2462 struct btrfs_timespec *tspec;
2463 struct btrfs_root *root = BTRFS_I(inode)->root;
2464 struct btrfs_key location;
2468 bool filled = false;
2470 ret = btrfs_fill_inode(inode, &rdev);
2474 path = btrfs_alloc_path();
2478 path->leave_spinning = 1;
2479 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2481 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2485 leaf = path->nodes[0];
2490 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2491 struct btrfs_inode_item);
2492 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2493 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
2494 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2495 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2496 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2498 tspec = btrfs_inode_atime(inode_item);
2499 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2500 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2502 tspec = btrfs_inode_mtime(inode_item);
2503 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2504 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2506 tspec = btrfs_inode_ctime(inode_item);
2507 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2508 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2510 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2511 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2512 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2513 inode->i_generation = BTRFS_I(inode)->generation;
2515 rdev = btrfs_inode_rdev(leaf, inode_item);
2517 BTRFS_I(inode)->index_cnt = (u64)-1;
2518 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2521 * try to precache a NULL acl entry for files that don't have
2522 * any xattrs or acls
2524 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2527 cache_no_acl(inode);
2529 btrfs_free_path(path);
2531 switch (inode->i_mode & S_IFMT) {
2533 inode->i_mapping->a_ops = &btrfs_aops;
2534 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2535 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2536 inode->i_fop = &btrfs_file_operations;
2537 inode->i_op = &btrfs_file_inode_operations;
2540 inode->i_fop = &btrfs_dir_file_operations;
2541 if (root == root->fs_info->tree_root)
2542 inode->i_op = &btrfs_dir_ro_inode_operations;
2544 inode->i_op = &btrfs_dir_inode_operations;
2547 inode->i_op = &btrfs_symlink_inode_operations;
2548 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2549 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2552 inode->i_op = &btrfs_special_inode_operations;
2553 init_special_inode(inode, inode->i_mode, rdev);
2557 btrfs_update_iflags(inode);
2561 btrfs_free_path(path);
2562 make_bad_inode(inode);
2566 * given a leaf and an inode, copy the inode fields into the leaf
2568 static void fill_inode_item(struct btrfs_trans_handle *trans,
2569 struct extent_buffer *leaf,
2570 struct btrfs_inode_item *item,
2571 struct inode *inode)
2573 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2574 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2575 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2576 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2577 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2579 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2580 inode->i_atime.tv_sec);
2581 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2582 inode->i_atime.tv_nsec);
2584 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2585 inode->i_mtime.tv_sec);
2586 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2587 inode->i_mtime.tv_nsec);
2589 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2590 inode->i_ctime.tv_sec);
2591 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2592 inode->i_ctime.tv_nsec);
2594 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2595 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2596 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2597 btrfs_set_inode_transid(leaf, item, trans->transid);
2598 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2599 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2600 btrfs_set_inode_block_group(leaf, item, 0);
2604 * copy everything in the in-memory inode into the btree.
2606 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
2607 struct btrfs_root *root, struct inode *inode)
2609 struct btrfs_inode_item *inode_item;
2610 struct btrfs_path *path;
2611 struct extent_buffer *leaf;
2614 path = btrfs_alloc_path();
2618 path->leave_spinning = 1;
2619 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2627 btrfs_unlock_up_safe(path, 1);
2628 leaf = path->nodes[0];
2629 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2630 struct btrfs_inode_item);
2632 fill_inode_item(trans, leaf, inode_item, inode);
2633 btrfs_mark_buffer_dirty(leaf);
2634 btrfs_set_inode_last_trans(trans, inode);
2637 btrfs_free_path(path);
2642 * copy everything in the in-memory inode into the btree.
2644 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2645 struct btrfs_root *root, struct inode *inode)
2650 * If the inode is a free space inode, we can deadlock during commit
2651 * if we put it into the delayed code.
2653 * The data relocation inode should also be directly updated
2656 if (!btrfs_is_free_space_inode(root, inode)
2657 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2658 ret = btrfs_delayed_update_inode(trans, root, inode);
2660 btrfs_set_inode_last_trans(trans, inode);
2664 return btrfs_update_inode_item(trans, root, inode);
2667 static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2668 struct btrfs_root *root, struct inode *inode)
2672 ret = btrfs_update_inode(trans, root, inode);
2674 return btrfs_update_inode_item(trans, root, inode);
2679 * unlink helper that gets used here in inode.c and in the tree logging
2680 * recovery code. It remove a link in a directory with a given name, and
2681 * also drops the back refs in the inode to the directory
2683 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct inode *dir, struct inode *inode,
2686 const char *name, int name_len)
2688 struct btrfs_path *path;
2690 struct extent_buffer *leaf;
2691 struct btrfs_dir_item *di;
2692 struct btrfs_key key;
2694 u64 ino = btrfs_ino(inode);
2695 u64 dir_ino = btrfs_ino(dir);
2697 path = btrfs_alloc_path();
2703 path->leave_spinning = 1;
2704 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2705 name, name_len, -1);
2714 leaf = path->nodes[0];
2715 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2716 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2719 btrfs_release_path(path);
2721 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2724 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2725 "inode %llu parent %llu\n", name_len, name,
2726 (unsigned long long)ino, (unsigned long long)dir_ino);
2727 btrfs_abort_transaction(trans, root, ret);
2731 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2733 btrfs_abort_transaction(trans, root, ret);
2737 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2739 if (ret != 0 && ret != -ENOENT) {
2740 btrfs_abort_transaction(trans, root, ret);
2744 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2749 btrfs_free_path(path);
2753 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2754 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2755 btrfs_update_inode(trans, root, dir);
2760 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2761 struct btrfs_root *root,
2762 struct inode *dir, struct inode *inode,
2763 const char *name, int name_len)
2766 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2768 btrfs_drop_nlink(inode);
2769 ret = btrfs_update_inode(trans, root, inode);
2775 /* helper to check if there is any shared block in the path */
2776 static int check_path_shared(struct btrfs_root *root,
2777 struct btrfs_path *path)
2779 struct extent_buffer *eb;
2783 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2786 if (!path->nodes[level])
2788 eb = path->nodes[level];
2789 if (!btrfs_block_can_be_shared(root, eb))
2791 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2800 * helper to start transaction for unlink and rmdir.
2802 * unlink and rmdir are special in btrfs, they do not always free space.
2803 * so in enospc case, we should make sure they will free space before
2804 * allowing them to use the global metadata reservation.
2806 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2807 struct dentry *dentry)
2809 struct btrfs_trans_handle *trans;
2810 struct btrfs_root *root = BTRFS_I(dir)->root;
2811 struct btrfs_path *path;
2812 struct btrfs_inode_ref *ref;
2813 struct btrfs_dir_item *di;
2814 struct inode *inode = dentry->d_inode;
2819 u64 ino = btrfs_ino(inode);
2820 u64 dir_ino = btrfs_ino(dir);
2823 * 1 for the possible orphan item
2824 * 1 for the dir item
2825 * 1 for the dir index
2826 * 1 for the inode ref
2827 * 1 for the inode ref in the tree log
2828 * 2 for the dir entries in the log
2831 trans = btrfs_start_transaction(root, 8);
2832 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2835 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2836 return ERR_PTR(-ENOSPC);
2838 /* check if there is someone else holds reference */
2839 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2840 return ERR_PTR(-ENOSPC);
2842 if (atomic_read(&inode->i_count) > 2)
2843 return ERR_PTR(-ENOSPC);
2845 if (xchg(&root->fs_info->enospc_unlink, 1))
2846 return ERR_PTR(-ENOSPC);
2848 path = btrfs_alloc_path();
2850 root->fs_info->enospc_unlink = 0;
2851 return ERR_PTR(-ENOMEM);
2854 /* 1 for the orphan item */
2855 trans = btrfs_start_transaction(root, 1);
2856 if (IS_ERR(trans)) {
2857 btrfs_free_path(path);
2858 root->fs_info->enospc_unlink = 0;
2862 path->skip_locking = 1;
2863 path->search_commit_root = 1;
2865 ret = btrfs_lookup_inode(trans, root, path,
2866 &BTRFS_I(dir)->location, 0);
2872 if (check_path_shared(root, path))
2877 btrfs_release_path(path);
2879 ret = btrfs_lookup_inode(trans, root, path,
2880 &BTRFS_I(inode)->location, 0);
2886 if (check_path_shared(root, path))
2891 btrfs_release_path(path);
2893 if (ret == 0 && S_ISREG(inode->i_mode)) {
2894 ret = btrfs_lookup_file_extent(trans, root, path,
2900 BUG_ON(ret == 0); /* Corruption */
2901 if (check_path_shared(root, path))
2903 btrfs_release_path(path);
2911 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2912 dentry->d_name.name, dentry->d_name.len, 0);
2918 if (check_path_shared(root, path))
2924 btrfs_release_path(path);
2926 ref = btrfs_lookup_inode_ref(trans, root, path,
2927 dentry->d_name.name, dentry->d_name.len,
2933 BUG_ON(!ref); /* Logic error */
2934 if (check_path_shared(root, path))
2936 index = btrfs_inode_ref_index(path->nodes[0], ref);
2937 btrfs_release_path(path);
2940 * This is a commit root search, if we can lookup inode item and other
2941 * relative items in the commit root, it means the transaction of
2942 * dir/file creation has been committed, and the dir index item that we
2943 * delay to insert has also been inserted into the commit root. So
2944 * we needn't worry about the delayed insertion of the dir index item
2947 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2948 dentry->d_name.name, dentry->d_name.len, 0);
2953 BUG_ON(ret == -ENOENT);
2954 if (check_path_shared(root, path))
2959 btrfs_free_path(path);
2960 /* Migrate the orphan reservation over */
2962 err = btrfs_block_rsv_migrate(trans->block_rsv,
2963 &root->fs_info->global_block_rsv,
2964 trans->bytes_reserved);
2967 btrfs_end_transaction(trans, root);
2968 root->fs_info->enospc_unlink = 0;
2969 return ERR_PTR(err);
2972 trans->block_rsv = &root->fs_info->global_block_rsv;
2976 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2977 struct btrfs_root *root)
2979 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2980 btrfs_block_rsv_release(root, trans->block_rsv,
2981 trans->bytes_reserved);
2982 trans->block_rsv = &root->fs_info->trans_block_rsv;
2983 BUG_ON(!root->fs_info->enospc_unlink);
2984 root->fs_info->enospc_unlink = 0;
2986 btrfs_end_transaction(trans, root);
2989 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2991 struct btrfs_root *root = BTRFS_I(dir)->root;
2992 struct btrfs_trans_handle *trans;
2993 struct inode *inode = dentry->d_inode;
2995 unsigned long nr = 0;
2997 trans = __unlink_start_trans(dir, dentry);
2999 return PTR_ERR(trans);
3001 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3003 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3004 dentry->d_name.name, dentry->d_name.len);
3008 if (inode->i_nlink == 0) {
3009 ret = btrfs_orphan_add(trans, inode);
3015 nr = trans->blocks_used;
3016 __unlink_end_trans(trans, root);
3017 btrfs_btree_balance_dirty(root, nr);
3021 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 struct inode *dir, u64 objectid,
3024 const char *name, int name_len)
3026 struct btrfs_path *path;
3027 struct extent_buffer *leaf;
3028 struct btrfs_dir_item *di;
3029 struct btrfs_key key;
3032 u64 dir_ino = btrfs_ino(dir);
3034 path = btrfs_alloc_path();
3038 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3039 name, name_len, -1);
3040 if (IS_ERR_OR_NULL(di)) {
3048 leaf = path->nodes[0];
3049 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3050 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3051 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3053 btrfs_abort_transaction(trans, root, ret);
3056 btrfs_release_path(path);
3058 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3059 objectid, root->root_key.objectid,
3060 dir_ino, &index, name, name_len);
3062 if (ret != -ENOENT) {
3063 btrfs_abort_transaction(trans, root, ret);
3066 di = btrfs_search_dir_index_item(root, path, dir_ino,
3068 if (IS_ERR_OR_NULL(di)) {
3073 btrfs_abort_transaction(trans, root, ret);
3077 leaf = path->nodes[0];
3078 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3079 btrfs_release_path(path);
3082 btrfs_release_path(path);
3084 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3086 btrfs_abort_transaction(trans, root, ret);
3090 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3091 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3092 ret = btrfs_update_inode(trans, root, dir);
3094 btrfs_abort_transaction(trans, root, ret);
3096 btrfs_free_path(path);
3100 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3102 struct inode *inode = dentry->d_inode;
3104 struct btrfs_root *root = BTRFS_I(dir)->root;
3105 struct btrfs_trans_handle *trans;
3106 unsigned long nr = 0;
3108 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3109 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3112 trans = __unlink_start_trans(dir, dentry);
3114 return PTR_ERR(trans);
3116 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3117 err = btrfs_unlink_subvol(trans, root, dir,
3118 BTRFS_I(inode)->location.objectid,
3119 dentry->d_name.name,
3120 dentry->d_name.len);
3124 err = btrfs_orphan_add(trans, inode);
3128 /* now the directory is empty */
3129 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3130 dentry->d_name.name, dentry->d_name.len);
3132 btrfs_i_size_write(inode, 0);
3134 nr = trans->blocks_used;
3135 __unlink_end_trans(trans, root);
3136 btrfs_btree_balance_dirty(root, nr);
3142 * this can truncate away extent items, csum items and directory items.
3143 * It starts at a high offset and removes keys until it can't find
3144 * any higher than new_size
3146 * csum items that cross the new i_size are truncated to the new size
3149 * min_type is the minimum key type to truncate down to. If set to 0, this
3150 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3152 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root,
3154 struct inode *inode,
3155 u64 new_size, u32 min_type)
3157 struct btrfs_path *path;
3158 struct extent_buffer *leaf;
3159 struct btrfs_file_extent_item *fi;
3160 struct btrfs_key key;
3161 struct btrfs_key found_key;
3162 u64 extent_start = 0;
3163 u64 extent_num_bytes = 0;
3164 u64 extent_offset = 0;
3166 u64 mask = root->sectorsize - 1;
3167 u32 found_type = (u8)-1;
3170 int pending_del_nr = 0;
3171 int pending_del_slot = 0;
3172 int extent_type = -1;
3175 u64 ino = btrfs_ino(inode);
3177 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3179 path = btrfs_alloc_path();
3184 if (root->ref_cows || root == root->fs_info->tree_root)
3185 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3188 * This function is also used to drop the items in the log tree before
3189 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3190 * it is used to drop the loged items. So we shouldn't kill the delayed
3193 if (min_type == 0 && root == BTRFS_I(inode)->root)
3194 btrfs_kill_delayed_inode_items(inode);
3197 key.offset = (u64)-1;
3201 path->leave_spinning = 1;
3202 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3209 /* there are no items in the tree for us to truncate, we're
3212 if (path->slots[0] == 0)
3219 leaf = path->nodes[0];
3220 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3221 found_type = btrfs_key_type(&found_key);
3223 if (found_key.objectid != ino)
3226 if (found_type < min_type)
3229 item_end = found_key.offset;
3230 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3231 fi = btrfs_item_ptr(leaf, path->slots[0],
3232 struct btrfs_file_extent_item);
3233 extent_type = btrfs_file_extent_type(leaf, fi);
3234 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3236 btrfs_file_extent_num_bytes(leaf, fi);
3237 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3238 item_end += btrfs_file_extent_inline_len(leaf,
3243 if (found_type > min_type) {
3246 if (item_end < new_size)
3248 if (found_key.offset >= new_size)
3254 /* FIXME, shrink the extent if the ref count is only 1 */
3255 if (found_type != BTRFS_EXTENT_DATA_KEY)
3258 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3260 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3262 u64 orig_num_bytes =
3263 btrfs_file_extent_num_bytes(leaf, fi);
3264 extent_num_bytes = new_size -
3265 found_key.offset + root->sectorsize - 1;
3266 extent_num_bytes = extent_num_bytes &
3267 ~((u64)root->sectorsize - 1);
3268 btrfs_set_file_extent_num_bytes(leaf, fi,
3270 num_dec = (orig_num_bytes -
3272 if (root->ref_cows && extent_start != 0)
3273 inode_sub_bytes(inode, num_dec);
3274 btrfs_mark_buffer_dirty(leaf);
3277 btrfs_file_extent_disk_num_bytes(leaf,
3279 extent_offset = found_key.offset -
3280 btrfs_file_extent_offset(leaf, fi);
3282 /* FIXME blocksize != 4096 */
3283 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3284 if (extent_start != 0) {
3287 inode_sub_bytes(inode, num_dec);
3290 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3292 * we can't truncate inline items that have had
3296 btrfs_file_extent_compression(leaf, fi) == 0 &&
3297 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3298 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3299 u32 size = new_size - found_key.offset;
3301 if (root->ref_cows) {
3302 inode_sub_bytes(inode, item_end + 1 -
3306 btrfs_file_extent_calc_inline_size(size);
3307 btrfs_truncate_item(trans, root, path,
3309 } else if (root->ref_cows) {
3310 inode_sub_bytes(inode, item_end + 1 -
3316 if (!pending_del_nr) {
3317 /* no pending yet, add ourselves */
3318 pending_del_slot = path->slots[0];
3320 } else if (pending_del_nr &&
3321 path->slots[0] + 1 == pending_del_slot) {
3322 /* hop on the pending chunk */
3324 pending_del_slot = path->slots[0];
3331 if (found_extent && (root->ref_cows ||
3332 root == root->fs_info->tree_root)) {
3333 btrfs_set_path_blocking(path);
3334 ret = btrfs_free_extent(trans, root, extent_start,
3335 extent_num_bytes, 0,
3336 btrfs_header_owner(leaf),
3337 ino, extent_offset, 0);
3341 if (found_type == BTRFS_INODE_ITEM_KEY)
3344 if (path->slots[0] == 0 ||
3345 path->slots[0] != pending_del_slot) {
3346 if (root->ref_cows &&
3347 BTRFS_I(inode)->location.objectid !=
3348 BTRFS_FREE_INO_OBJECTID) {
3352 if (pending_del_nr) {
3353 ret = btrfs_del_items(trans, root, path,
3357 btrfs_abort_transaction(trans,
3363 btrfs_release_path(path);
3370 if (pending_del_nr) {
3371 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3374 btrfs_abort_transaction(trans, root, ret);
3377 btrfs_free_path(path);
3382 * taken from block_truncate_page, but does cow as it zeros out
3383 * any bytes left in the last page in the file.
3385 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3387 struct inode *inode = mapping->host;
3388 struct btrfs_root *root = BTRFS_I(inode)->root;
3389 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3390 struct btrfs_ordered_extent *ordered;
3391 struct extent_state *cached_state = NULL;
3393 u32 blocksize = root->sectorsize;
3394 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3395 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3397 gfp_t mask = btrfs_alloc_write_mask(mapping);
3402 if ((offset & (blocksize - 1)) == 0)
3404 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
3410 page = find_or_create_page(mapping, index, mask);
3412 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3416 page_start = page_offset(page);
3417 page_end = page_start + PAGE_CACHE_SIZE - 1;
3419 if (!PageUptodate(page)) {
3420 ret = btrfs_readpage(NULL, page);
3422 if (page->mapping != mapping) {
3424 page_cache_release(page);
3427 if (!PageUptodate(page)) {
3432 wait_on_page_writeback(page);
3434 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
3435 set_page_extent_mapped(page);
3437 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3439 unlock_extent_cached(io_tree, page_start, page_end,
3440 &cached_state, GFP_NOFS);
3442 page_cache_release(page);
3443 btrfs_start_ordered_extent(inode, ordered, 1);
3444 btrfs_put_ordered_extent(ordered);
3448 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3449 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3450 0, 0, &cached_state, GFP_NOFS);
3452 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3455 unlock_extent_cached(io_tree, page_start, page_end,
3456 &cached_state, GFP_NOFS);
3461 if (offset != PAGE_CACHE_SIZE) {
3463 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3464 flush_dcache_page(page);
3467 ClearPageChecked(page);
3468 set_page_dirty(page);
3469 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3474 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3476 page_cache_release(page);
3482 * This function puts in dummy file extents for the area we're creating a hole
3483 * for. So if we are truncating this file to a larger size we need to insert
3484 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3485 * the range between oldsize and size
3487 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3489 struct btrfs_trans_handle *trans;
3490 struct btrfs_root *root = BTRFS_I(inode)->root;
3491 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3492 struct extent_map *em = NULL;
3493 struct extent_state *cached_state = NULL;
3494 u64 mask = root->sectorsize - 1;
3495 u64 hole_start = (oldsize + mask) & ~mask;
3496 u64 block_end = (size + mask) & ~mask;
3502 if (size <= hole_start)
3506 struct btrfs_ordered_extent *ordered;
3507 btrfs_wait_ordered_range(inode, hole_start,
3508 block_end - hole_start);
3509 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3511 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3514 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3515 &cached_state, GFP_NOFS);
3516 btrfs_put_ordered_extent(ordered);
3519 cur_offset = hole_start;
3521 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3522 block_end - cur_offset, 0);
3527 last_byte = min(extent_map_end(em), block_end);
3528 last_byte = (last_byte + mask) & ~mask;
3529 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3531 hole_size = last_byte - cur_offset;
3533 trans = btrfs_start_transaction(root, 3);
3534 if (IS_ERR(trans)) {
3535 err = PTR_ERR(trans);
3539 err = btrfs_drop_extents(trans, inode, cur_offset,
3540 cur_offset + hole_size,
3543 btrfs_abort_transaction(trans, root, err);
3544 btrfs_end_transaction(trans, root);
3548 err = btrfs_insert_file_extent(trans, root,
3549 btrfs_ino(inode), cur_offset, 0,
3550 0, hole_size, 0, hole_size,
3553 btrfs_abort_transaction(trans, root, err);
3554 btrfs_end_transaction(trans, root);
3558 btrfs_drop_extent_cache(inode, hole_start,
3561 btrfs_update_inode(trans, root, inode);
3562 btrfs_end_transaction(trans, root);
3564 free_extent_map(em);
3566 cur_offset = last_byte;
3567 if (cur_offset >= block_end)
3571 free_extent_map(em);
3572 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3577 static int btrfs_setsize(struct inode *inode, loff_t newsize)
3579 struct btrfs_root *root = BTRFS_I(inode)->root;
3580 struct btrfs_trans_handle *trans;
3581 loff_t oldsize = i_size_read(inode);
3584 if (newsize == oldsize)
3587 if (newsize > oldsize) {
3588 truncate_pagecache(inode, oldsize, newsize);
3589 ret = btrfs_cont_expand(inode, oldsize, newsize);
3593 trans = btrfs_start_transaction(root, 1);
3595 return PTR_ERR(trans);
3597 i_size_write(inode, newsize);
3598 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3599 ret = btrfs_update_inode(trans, root, inode);
3600 btrfs_end_transaction(trans, root);
3604 * We're truncating a file that used to have good data down to
3605 * zero. Make sure it gets into the ordered flush list so that
3606 * any new writes get down to disk quickly.
3609 BTRFS_I(inode)->ordered_data_close = 1;
3611 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3612 truncate_setsize(inode, newsize);
3613 ret = btrfs_truncate(inode);
3619 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3621 struct inode *inode = dentry->d_inode;
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3625 if (btrfs_root_readonly(root))
3628 err = inode_change_ok(inode, attr);
3632 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3633 err = btrfs_setsize(inode, attr->ia_size);
3638 if (attr->ia_valid) {
3639 setattr_copy(inode, attr);
3640 err = btrfs_dirty_inode(inode);
3642 if (!err && attr->ia_valid & ATTR_MODE)
3643 err = btrfs_acl_chmod(inode);
3649 void btrfs_evict_inode(struct inode *inode)
3651 struct btrfs_trans_handle *trans;
3652 struct btrfs_root *root = BTRFS_I(inode)->root;
3653 struct btrfs_block_rsv *rsv, *global_rsv;
3654 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
3658 trace_btrfs_inode_evict(inode);
3660 truncate_inode_pages(&inode->i_data, 0);
3661 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3662 btrfs_is_free_space_inode(root, inode)))
3665 if (is_bad_inode(inode)) {
3666 btrfs_orphan_del(NULL, inode);
3669 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3670 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3672 if (root->fs_info->log_root_recovering) {
3673 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3677 if (inode->i_nlink > 0) {
3678 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3682 rsv = btrfs_alloc_block_rsv(root);
3684 btrfs_orphan_del(NULL, inode);
3687 rsv->size = min_size;
3688 global_rsv = &root->fs_info->global_block_rsv;
3690 btrfs_i_size_write(inode, 0);
3693 * This is a bit simpler than btrfs_truncate since
3695 * 1) We've already reserved our space for our orphan item in the
3697 * 2) We're going to delete the inode item, so we don't need to update
3700 * So we just need to reserve some slack space in case we add bytes when
3701 * doing the truncate.
3704 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
3707 * Try and steal from the global reserve since we will
3708 * likely not use this space anyway, we want to try as
3709 * hard as possible to get this to work.
3712 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3715 printk(KERN_WARNING "Could not get space for a "
3716 "delete, will truncate on mount %d\n", ret);
3717 btrfs_orphan_del(NULL, inode);
3718 btrfs_free_block_rsv(root, rsv);
3722 trans = btrfs_start_transaction(root, 0);
3723 if (IS_ERR(trans)) {
3724 btrfs_orphan_del(NULL, inode);
3725 btrfs_free_block_rsv(root, rsv);
3729 trans->block_rsv = rsv;
3731 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3735 nr = trans->blocks_used;
3736 btrfs_end_transaction(trans, root);
3738 btrfs_btree_balance_dirty(root, nr);
3741 btrfs_free_block_rsv(root, rsv);
3744 trans->block_rsv = root->orphan_block_rsv;
3745 ret = btrfs_orphan_del(trans, inode);
3749 trans->block_rsv = &root->fs_info->trans_block_rsv;
3750 if (!(root == root->fs_info->tree_root ||
3751 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3752 btrfs_return_ino(root, btrfs_ino(inode));
3754 nr = trans->blocks_used;
3755 btrfs_end_transaction(trans, root);
3756 btrfs_btree_balance_dirty(root, nr);
3758 end_writeback(inode);
3763 * this returns the key found in the dir entry in the location pointer.
3764 * If no dir entries were found, location->objectid is 0.
3766 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3767 struct btrfs_key *location)
3769 const char *name = dentry->d_name.name;
3770 int namelen = dentry->d_name.len;
3771 struct btrfs_dir_item *di;
3772 struct btrfs_path *path;
3773 struct btrfs_root *root = BTRFS_I(dir)->root;
3776 path = btrfs_alloc_path();
3780 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3785 if (IS_ERR_OR_NULL(di))
3788 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3790 btrfs_free_path(path);
3793 location->objectid = 0;
3798 * when we hit a tree root in a directory, the btrfs part of the inode
3799 * needs to be changed to reflect the root directory of the tree root. This
3800 * is kind of like crossing a mount point.
3802 static int fixup_tree_root_location(struct btrfs_root *root,
3804 struct dentry *dentry,
3805 struct btrfs_key *location,
3806 struct btrfs_root **sub_root)
3808 struct btrfs_path *path;
3809 struct btrfs_root *new_root;
3810 struct btrfs_root_ref *ref;
3811 struct extent_buffer *leaf;
3815 path = btrfs_alloc_path();
3822 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3823 BTRFS_I(dir)->root->root_key.objectid,
3824 location->objectid);
3831 leaf = path->nodes[0];
3832 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3833 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3834 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3837 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3838 (unsigned long)(ref + 1),
3839 dentry->d_name.len);
3843 btrfs_release_path(path);
3845 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3846 if (IS_ERR(new_root)) {
3847 err = PTR_ERR(new_root);
3851 if (btrfs_root_refs(&new_root->root_item) == 0) {
3856 *sub_root = new_root;
3857 location->objectid = btrfs_root_dirid(&new_root->root_item);
3858 location->type = BTRFS_INODE_ITEM_KEY;
3859 location->offset = 0;
3862 btrfs_free_path(path);
3866 static void inode_tree_add(struct inode *inode)
3868 struct btrfs_root *root = BTRFS_I(inode)->root;
3869 struct btrfs_inode *entry;
3871 struct rb_node *parent;
3872 u64 ino = btrfs_ino(inode);
3874 p = &root->inode_tree.rb_node;
3877 if (inode_unhashed(inode))
3880 spin_lock(&root->inode_lock);
3883 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3885 if (ino < btrfs_ino(&entry->vfs_inode))
3886 p = &parent->rb_left;
3887 else if (ino > btrfs_ino(&entry->vfs_inode))
3888 p = &parent->rb_right;
3890 WARN_ON(!(entry->vfs_inode.i_state &
3891 (I_WILL_FREE | I_FREEING)));
3892 rb_erase(parent, &root->inode_tree);
3893 RB_CLEAR_NODE(parent);
3894 spin_unlock(&root->inode_lock);
3898 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3899 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3900 spin_unlock(&root->inode_lock);
3903 static void inode_tree_del(struct inode *inode)
3905 struct btrfs_root *root = BTRFS_I(inode)->root;
3908 spin_lock(&root->inode_lock);
3909 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3910 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3911 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3912 empty = RB_EMPTY_ROOT(&root->inode_tree);
3914 spin_unlock(&root->inode_lock);
3917 * Free space cache has inodes in the tree root, but the tree root has a
3918 * root_refs of 0, so this could end up dropping the tree root as a
3919 * snapshot, so we need the extra !root->fs_info->tree_root check to
3920 * make sure we don't drop it.
3922 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3923 root != root->fs_info->tree_root) {
3924 synchronize_srcu(&root->fs_info->subvol_srcu);
3925 spin_lock(&root->inode_lock);
3926 empty = RB_EMPTY_ROOT(&root->inode_tree);
3927 spin_unlock(&root->inode_lock);
3929 btrfs_add_dead_root(root);
3933 void btrfs_invalidate_inodes(struct btrfs_root *root)
3935 struct rb_node *node;
3936 struct rb_node *prev;
3937 struct btrfs_inode *entry;
3938 struct inode *inode;
3941 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3943 spin_lock(&root->inode_lock);
3945 node = root->inode_tree.rb_node;
3949 entry = rb_entry(node, struct btrfs_inode, rb_node);
3951 if (objectid < btrfs_ino(&entry->vfs_inode))
3952 node = node->rb_left;
3953 else if (objectid > btrfs_ino(&entry->vfs_inode))
3954 node = node->rb_right;
3960 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3961 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
3965 prev = rb_next(prev);
3969 entry = rb_entry(node, struct btrfs_inode, rb_node);
3970 objectid = btrfs_ino(&entry->vfs_inode) + 1;
3971 inode = igrab(&entry->vfs_inode);
3973 spin_unlock(&root->inode_lock);
3974 if (atomic_read(&inode->i_count) > 1)
3975 d_prune_aliases(inode);
3977 * btrfs_drop_inode will have it removed from
3978 * the inode cache when its usage count
3983 spin_lock(&root->inode_lock);
3987 if (cond_resched_lock(&root->inode_lock))
3990 node = rb_next(node);
3992 spin_unlock(&root->inode_lock);
3995 static int btrfs_init_locked_inode(struct inode *inode, void *p)
3997 struct btrfs_iget_args *args = p;
3998 inode->i_ino = args->ino;
3999 BTRFS_I(inode)->root = args->root;
4000 btrfs_set_inode_space_info(args->root, inode);
4004 static int btrfs_find_actor(struct inode *inode, void *opaque)
4006 struct btrfs_iget_args *args = opaque;
4007 return args->ino == btrfs_ino(inode) &&
4008 args->root == BTRFS_I(inode)->root;
4011 static struct inode *btrfs_iget_locked(struct super_block *s,
4013 struct btrfs_root *root)
4015 struct inode *inode;
4016 struct btrfs_iget_args args;
4017 args.ino = objectid;
4020 inode = iget5_locked(s, objectid, btrfs_find_actor,
4021 btrfs_init_locked_inode,
4026 /* Get an inode object given its location and corresponding root.
4027 * Returns in *is_new if the inode was read from disk
4029 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4030 struct btrfs_root *root, int *new)
4032 struct inode *inode;
4034 inode = btrfs_iget_locked(s, location->objectid, root);
4036 return ERR_PTR(-ENOMEM);
4038 if (inode->i_state & I_NEW) {
4039 BTRFS_I(inode)->root = root;
4040 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4041 btrfs_read_locked_inode(inode);
4042 if (!is_bad_inode(inode)) {
4043 inode_tree_add(inode);
4044 unlock_new_inode(inode);
4048 unlock_new_inode(inode);
4050 inode = ERR_PTR(-ESTALE);
4057 static struct inode *new_simple_dir(struct super_block *s,
4058 struct btrfs_key *key,
4059 struct btrfs_root *root)
4061 struct inode *inode = new_inode(s);
4064 return ERR_PTR(-ENOMEM);
4066 BTRFS_I(inode)->root = root;
4067 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4068 BTRFS_I(inode)->dummy_inode = 1;
4070 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4071 inode->i_op = &simple_dir_inode_operations;
4072 inode->i_fop = &simple_dir_operations;
4073 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4074 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4079 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4081 struct inode *inode;
4082 struct btrfs_root *root = BTRFS_I(dir)->root;
4083 struct btrfs_root *sub_root = root;
4084 struct btrfs_key location;
4088 if (dentry->d_name.len > BTRFS_NAME_LEN)
4089 return ERR_PTR(-ENAMETOOLONG);
4091 if (unlikely(d_need_lookup(dentry))) {
4092 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4093 kfree(dentry->d_fsdata);
4094 dentry->d_fsdata = NULL;
4095 /* This thing is hashed, drop it for now */
4098 ret = btrfs_inode_by_name(dir, dentry, &location);
4102 return ERR_PTR(ret);
4104 if (location.objectid == 0)
4107 if (location.type == BTRFS_INODE_ITEM_KEY) {
4108 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4112 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4114 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4115 ret = fixup_tree_root_location(root, dir, dentry,
4116 &location, &sub_root);
4119 inode = ERR_PTR(ret);
4121 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4123 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4125 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4127 if (!IS_ERR(inode) && root != sub_root) {
4128 down_read(&root->fs_info->cleanup_work_sem);
4129 if (!(inode->i_sb->s_flags & MS_RDONLY))
4130 ret = btrfs_orphan_cleanup(sub_root);
4131 up_read(&root->fs_info->cleanup_work_sem);
4133 inode = ERR_PTR(ret);
4139 static int btrfs_dentry_delete(const struct dentry *dentry)
4141 struct btrfs_root *root;
4143 if (!dentry->d_inode && !IS_ROOT(dentry))
4144 dentry = dentry->d_parent;
4146 if (dentry->d_inode) {
4147 root = BTRFS_I(dentry->d_inode)->root;
4148 if (btrfs_root_refs(&root->root_item) == 0)
4154 static void btrfs_dentry_release(struct dentry *dentry)
4156 if (dentry->d_fsdata)
4157 kfree(dentry->d_fsdata);
4160 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4161 struct nameidata *nd)
4165 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4166 if (unlikely(d_need_lookup(dentry))) {
4167 spin_lock(&dentry->d_lock);
4168 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4169 spin_unlock(&dentry->d_lock);
4174 unsigned char btrfs_filetype_table[] = {
4175 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4178 static int btrfs_real_readdir(struct file *filp, void *dirent,
4181 struct inode *inode = filp->f_dentry->d_inode;
4182 struct btrfs_root *root = BTRFS_I(inode)->root;
4183 struct btrfs_item *item;
4184 struct btrfs_dir_item *di;
4185 struct btrfs_key key;
4186 struct btrfs_key found_key;
4187 struct btrfs_path *path;
4188 struct list_head ins_list;
4189 struct list_head del_list;
4192 struct extent_buffer *leaf;
4194 unsigned char d_type;
4199 int key_type = BTRFS_DIR_INDEX_KEY;
4203 int is_curr = 0; /* filp->f_pos points to the current index? */
4205 /* FIXME, use a real flag for deciding about the key type */
4206 if (root->fs_info->tree_root == root)
4207 key_type = BTRFS_DIR_ITEM_KEY;
4209 /* special case for "." */
4210 if (filp->f_pos == 0) {
4211 over = filldir(dirent, ".", 1,
4212 filp->f_pos, btrfs_ino(inode), DT_DIR);
4217 /* special case for .., just use the back ref */
4218 if (filp->f_pos == 1) {
4219 u64 pino = parent_ino(filp->f_path.dentry);
4220 over = filldir(dirent, "..", 2,
4221 filp->f_pos, pino, DT_DIR);
4226 path = btrfs_alloc_path();
4232 if (key_type == BTRFS_DIR_INDEX_KEY) {
4233 INIT_LIST_HEAD(&ins_list);
4234 INIT_LIST_HEAD(&del_list);
4235 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4238 btrfs_set_key_type(&key, key_type);
4239 key.offset = filp->f_pos;
4240 key.objectid = btrfs_ino(inode);
4242 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4247 leaf = path->nodes[0];
4248 slot = path->slots[0];
4249 if (slot >= btrfs_header_nritems(leaf)) {
4250 ret = btrfs_next_leaf(root, path);
4258 item = btrfs_item_nr(leaf, slot);
4259 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4261 if (found_key.objectid != key.objectid)
4263 if (btrfs_key_type(&found_key) != key_type)
4265 if (found_key.offset < filp->f_pos)
4267 if (key_type == BTRFS_DIR_INDEX_KEY &&
4268 btrfs_should_delete_dir_index(&del_list,
4272 filp->f_pos = found_key.offset;
4275 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4277 di_total = btrfs_item_size(leaf, item);
4279 while (di_cur < di_total) {
4280 struct btrfs_key location;
4283 if (verify_dir_item(root, leaf, di))
4286 name_len = btrfs_dir_name_len(leaf, di);
4287 if (name_len <= sizeof(tmp_name)) {
4288 name_ptr = tmp_name;
4290 name_ptr = kmalloc(name_len, GFP_NOFS);
4296 read_extent_buffer(leaf, name_ptr,
4297 (unsigned long)(di + 1), name_len);
4299 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4300 btrfs_dir_item_key_to_cpu(leaf, di, &location);
4304 q.hash = full_name_hash(q.name, q.len);
4305 tmp = d_lookup(filp->f_dentry, &q);
4307 struct btrfs_key *newkey;
4309 newkey = kzalloc(sizeof(struct btrfs_key),
4313 tmp = d_alloc(filp->f_dentry, &q);
4319 memcpy(newkey, &location,
4320 sizeof(struct btrfs_key));
4321 tmp->d_fsdata = newkey;
4322 tmp->d_flags |= DCACHE_NEED_LOOKUP;
4329 /* is this a reference to our own snapshot? If so
4332 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4333 location.objectid == root->root_key.objectid) {
4337 over = filldir(dirent, name_ptr, name_len,
4338 found_key.offset, location.objectid,
4342 if (name_ptr != tmp_name)
4347 di_len = btrfs_dir_name_len(leaf, di) +
4348 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4350 di = (struct btrfs_dir_item *)((char *)di + di_len);
4356 if (key_type == BTRFS_DIR_INDEX_KEY) {
4359 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4365 /* Reached end of directory/root. Bump pos past the last item. */
4366 if (key_type == BTRFS_DIR_INDEX_KEY)
4368 * 32-bit glibc will use getdents64, but then strtol -
4369 * so the last number we can serve is this.
4371 filp->f_pos = 0x7fffffff;
4377 if (key_type == BTRFS_DIR_INDEX_KEY)
4378 btrfs_put_delayed_items(&ins_list, &del_list);
4379 btrfs_free_path(path);
4383 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4385 struct btrfs_root *root = BTRFS_I(inode)->root;
4386 struct btrfs_trans_handle *trans;
4388 bool nolock = false;
4390 if (BTRFS_I(inode)->dummy_inode)
4393 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
4396 if (wbc->sync_mode == WB_SYNC_ALL) {
4398 trans = btrfs_join_transaction_nolock(root);
4400 trans = btrfs_join_transaction(root);
4402 return PTR_ERR(trans);
4404 ret = btrfs_end_transaction_nolock(trans, root);
4406 ret = btrfs_commit_transaction(trans, root);
4412 * This is somewhat expensive, updating the tree every time the
4413 * inode changes. But, it is most likely to find the inode in cache.
4414 * FIXME, needs more benchmarking...there are no reasons other than performance
4415 * to keep or drop this code.
4417 int btrfs_dirty_inode(struct inode *inode)
4419 struct btrfs_root *root = BTRFS_I(inode)->root;
4420 struct btrfs_trans_handle *trans;
4423 if (BTRFS_I(inode)->dummy_inode)
4426 trans = btrfs_join_transaction(root);
4428 return PTR_ERR(trans);
4430 ret = btrfs_update_inode(trans, root, inode);
4431 if (ret && ret == -ENOSPC) {
4432 /* whoops, lets try again with the full transaction */
4433 btrfs_end_transaction(trans, root);
4434 trans = btrfs_start_transaction(root, 1);
4436 return PTR_ERR(trans);
4438 ret = btrfs_update_inode(trans, root, inode);
4440 btrfs_end_transaction(trans, root);
4441 if (BTRFS_I(inode)->delayed_node)
4442 btrfs_balance_delayed_items(root);
4448 * This is a copy of file_update_time. We need this so we can return error on
4449 * ENOSPC for updating the inode in the case of file write and mmap writes.
4451 int btrfs_update_time(struct file *file)
4453 struct inode *inode = file->f_path.dentry->d_inode;
4454 struct timespec now;
4456 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4458 /* First try to exhaust all avenues to not sync */
4459 if (IS_NOCMTIME(inode))
4462 now = current_fs_time(inode->i_sb);
4463 if (!timespec_equal(&inode->i_mtime, &now))
4466 if (!timespec_equal(&inode->i_ctime, &now))
4469 if (IS_I_VERSION(inode))
4470 sync_it |= S_VERSION;
4475 /* Finally allowed to write? Takes lock. */
4476 if (mnt_want_write_file(file))
4479 /* Only change inode inside the lock region */
4480 if (sync_it & S_VERSION)
4481 inode_inc_iversion(inode);
4482 if (sync_it & S_CTIME)
4483 inode->i_ctime = now;
4484 if (sync_it & S_MTIME)
4485 inode->i_mtime = now;
4486 ret = btrfs_dirty_inode(inode);
4488 mark_inode_dirty_sync(inode);
4489 mnt_drop_write(file->f_path.mnt);
4494 * find the highest existing sequence number in a directory
4495 * and then set the in-memory index_cnt variable to reflect
4496 * free sequence numbers
4498 static int btrfs_set_inode_index_count(struct inode *inode)
4500 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 struct btrfs_key key, found_key;
4502 struct btrfs_path *path;
4503 struct extent_buffer *leaf;
4506 key.objectid = btrfs_ino(inode);
4507 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4508 key.offset = (u64)-1;
4510 path = btrfs_alloc_path();
4514 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4517 /* FIXME: we should be able to handle this */
4523 * MAGIC NUMBER EXPLANATION:
4524 * since we search a directory based on f_pos we have to start at 2
4525 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4526 * else has to start at 2
4528 if (path->slots[0] == 0) {
4529 BTRFS_I(inode)->index_cnt = 2;
4535 leaf = path->nodes[0];
4536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4538 if (found_key.objectid != btrfs_ino(inode) ||
4539 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4540 BTRFS_I(inode)->index_cnt = 2;
4544 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4546 btrfs_free_path(path);
4551 * helper to find a free sequence number in a given directory. This current
4552 * code is very simple, later versions will do smarter things in the btree
4554 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4558 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4559 ret = btrfs_inode_delayed_dir_index_count(dir);
4561 ret = btrfs_set_inode_index_count(dir);
4567 *index = BTRFS_I(dir)->index_cnt;
4568 BTRFS_I(dir)->index_cnt++;
4573 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4574 struct btrfs_root *root,
4576 const char *name, int name_len,
4577 u64 ref_objectid, u64 objectid,
4578 umode_t mode, u64 *index)
4580 struct inode *inode;
4581 struct btrfs_inode_item *inode_item;
4582 struct btrfs_key *location;
4583 struct btrfs_path *path;
4584 struct btrfs_inode_ref *ref;
4585 struct btrfs_key key[2];
4591 path = btrfs_alloc_path();
4593 return ERR_PTR(-ENOMEM);
4595 inode = new_inode(root->fs_info->sb);
4597 btrfs_free_path(path);
4598 return ERR_PTR(-ENOMEM);
4602 * we have to initialize this early, so we can reclaim the inode
4603 * number if we fail afterwards in this function.
4605 inode->i_ino = objectid;
4608 trace_btrfs_inode_request(dir);
4610 ret = btrfs_set_inode_index(dir, index);
4612 btrfs_free_path(path);
4614 return ERR_PTR(ret);
4618 * index_cnt is ignored for everything but a dir,
4619 * btrfs_get_inode_index_count has an explanation for the magic
4622 BTRFS_I(inode)->index_cnt = 2;
4623 BTRFS_I(inode)->root = root;
4624 BTRFS_I(inode)->generation = trans->transid;
4625 inode->i_generation = BTRFS_I(inode)->generation;
4626 btrfs_set_inode_space_info(root, inode);
4633 key[0].objectid = objectid;
4634 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4637 key[1].objectid = objectid;
4638 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4639 key[1].offset = ref_objectid;
4641 sizes[0] = sizeof(struct btrfs_inode_item);
4642 sizes[1] = name_len + sizeof(*ref);
4644 path->leave_spinning = 1;
4645 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4649 inode_init_owner(inode, dir, mode);
4650 inode_set_bytes(inode, 0);
4651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4652 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4653 struct btrfs_inode_item);
4654 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4656 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4657 struct btrfs_inode_ref);
4658 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4659 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4660 ptr = (unsigned long)(ref + 1);
4661 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4663 btrfs_mark_buffer_dirty(path->nodes[0]);
4664 btrfs_free_path(path);
4666 location = &BTRFS_I(inode)->location;
4667 location->objectid = objectid;
4668 location->offset = 0;
4669 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4671 btrfs_inherit_iflags(inode, dir);
4673 if (S_ISREG(mode)) {
4674 if (btrfs_test_opt(root, NODATASUM))
4675 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4676 if (btrfs_test_opt(root, NODATACOW) ||
4677 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4678 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4681 insert_inode_hash(inode);
4682 inode_tree_add(inode);
4684 trace_btrfs_inode_new(inode);
4685 btrfs_set_inode_last_trans(trans, inode);
4690 BTRFS_I(dir)->index_cnt--;
4691 btrfs_free_path(path);
4693 return ERR_PTR(ret);
4696 static inline u8 btrfs_inode_type(struct inode *inode)
4698 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4702 * utility function to add 'inode' into 'parent_inode' with
4703 * a give name and a given sequence number.
4704 * if 'add_backref' is true, also insert a backref from the
4705 * inode to the parent directory.
4707 int btrfs_add_link(struct btrfs_trans_handle *trans,
4708 struct inode *parent_inode, struct inode *inode,
4709 const char *name, int name_len, int add_backref, u64 index)
4712 struct btrfs_key key;
4713 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4714 u64 ino = btrfs_ino(inode);
4715 u64 parent_ino = btrfs_ino(parent_inode);
4717 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4718 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4721 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4725 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4726 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4727 key.objectid, root->root_key.objectid,
4728 parent_ino, index, name, name_len);
4729 } else if (add_backref) {
4730 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4734 /* Nothing to clean up yet */
4738 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4740 btrfs_inode_type(inode), index);
4744 btrfs_abort_transaction(trans, root, ret);
4748 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4750 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4751 ret = btrfs_update_inode(trans, root, parent_inode);
4753 btrfs_abort_transaction(trans, root, ret);
4757 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4760 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4761 key.objectid, root->root_key.objectid,
4762 parent_ino, &local_index, name, name_len);
4764 } else if (add_backref) {
4768 err = btrfs_del_inode_ref(trans, root, name, name_len,
4769 ino, parent_ino, &local_index);
4774 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4775 struct inode *dir, struct dentry *dentry,
4776 struct inode *inode, int backref, u64 index)
4778 int err = btrfs_add_link(trans, dir, inode,
4779 dentry->d_name.name, dentry->d_name.len,
4786 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4787 umode_t mode, dev_t rdev)
4789 struct btrfs_trans_handle *trans;
4790 struct btrfs_root *root = BTRFS_I(dir)->root;
4791 struct inode *inode = NULL;
4795 unsigned long nr = 0;
4798 if (!new_valid_dev(rdev))
4802 * 2 for inode item and ref
4804 * 1 for xattr if selinux is on
4806 trans = btrfs_start_transaction(root, 5);
4808 return PTR_ERR(trans);
4810 err = btrfs_find_free_ino(root, &objectid);
4814 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4815 dentry->d_name.len, btrfs_ino(dir), objectid,
4817 if (IS_ERR(inode)) {
4818 err = PTR_ERR(inode);
4822 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4829 * If the active LSM wants to access the inode during
4830 * d_instantiate it needs these. Smack checks to see
4831 * if the filesystem supports xattrs by looking at the
4835 inode->i_op = &btrfs_special_inode_operations;
4836 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4840 init_special_inode(inode, inode->i_mode, rdev);
4841 btrfs_update_inode(trans, root, inode);
4842 d_instantiate(dentry, inode);
4845 nr = trans->blocks_used;
4846 btrfs_end_transaction(trans, root);
4847 btrfs_btree_balance_dirty(root, nr);
4849 inode_dec_link_count(inode);
4855 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4856 umode_t mode, struct nameidata *nd)
4858 struct btrfs_trans_handle *trans;
4859 struct btrfs_root *root = BTRFS_I(dir)->root;
4860 struct inode *inode = NULL;
4863 unsigned long nr = 0;
4868 * 2 for inode item and ref
4870 * 1 for xattr if selinux is on
4872 trans = btrfs_start_transaction(root, 5);
4874 return PTR_ERR(trans);
4876 err = btrfs_find_free_ino(root, &objectid);
4880 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4881 dentry->d_name.len, btrfs_ino(dir), objectid,
4883 if (IS_ERR(inode)) {
4884 err = PTR_ERR(inode);
4888 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4895 * If the active LSM wants to access the inode during
4896 * d_instantiate it needs these. Smack checks to see
4897 * if the filesystem supports xattrs by looking at the
4900 inode->i_fop = &btrfs_file_operations;
4901 inode->i_op = &btrfs_file_inode_operations;
4903 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4907 inode->i_mapping->a_ops = &btrfs_aops;
4908 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4909 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4910 d_instantiate(dentry, inode);
4913 nr = trans->blocks_used;
4914 btrfs_end_transaction(trans, root);
4916 inode_dec_link_count(inode);
4919 btrfs_btree_balance_dirty(root, nr);
4923 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4924 struct dentry *dentry)
4926 struct btrfs_trans_handle *trans;
4927 struct btrfs_root *root = BTRFS_I(dir)->root;
4928 struct inode *inode = old_dentry->d_inode;
4930 unsigned long nr = 0;
4934 /* do not allow sys_link's with other subvols of the same device */
4935 if (root->objectid != BTRFS_I(inode)->root->objectid)
4938 if (inode->i_nlink == ~0U)
4941 err = btrfs_set_inode_index(dir, &index);
4946 * 2 items for inode and inode ref
4947 * 2 items for dir items
4948 * 1 item for parent inode
4950 trans = btrfs_start_transaction(root, 5);
4951 if (IS_ERR(trans)) {
4952 err = PTR_ERR(trans);
4956 btrfs_inc_nlink(inode);
4957 inode->i_ctime = CURRENT_TIME;
4960 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
4965 struct dentry *parent = dentry->d_parent;
4966 err = btrfs_update_inode(trans, root, inode);
4969 d_instantiate(dentry, inode);
4970 btrfs_log_new_name(trans, inode, NULL, parent);
4973 nr = trans->blocks_used;
4974 btrfs_end_transaction(trans, root);
4977 inode_dec_link_count(inode);
4980 btrfs_btree_balance_dirty(root, nr);
4984 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4986 struct inode *inode = NULL;
4987 struct btrfs_trans_handle *trans;
4988 struct btrfs_root *root = BTRFS_I(dir)->root;
4990 int drop_on_err = 0;
4993 unsigned long nr = 1;
4996 * 2 items for inode and ref
4997 * 2 items for dir items
4998 * 1 for xattr if selinux is on
5000 trans = btrfs_start_transaction(root, 5);
5002 return PTR_ERR(trans);
5004 err = btrfs_find_free_ino(root, &objectid);
5008 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5009 dentry->d_name.len, btrfs_ino(dir), objectid,
5010 S_IFDIR | mode, &index);
5011 if (IS_ERR(inode)) {
5012 err = PTR_ERR(inode);
5018 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5022 inode->i_op = &btrfs_dir_inode_operations;
5023 inode->i_fop = &btrfs_dir_file_operations;
5025 btrfs_i_size_write(inode, 0);
5026 err = btrfs_update_inode(trans, root, inode);
5030 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5031 dentry->d_name.len, 0, index);
5035 d_instantiate(dentry, inode);
5039 nr = trans->blocks_used;
5040 btrfs_end_transaction(trans, root);
5043 btrfs_btree_balance_dirty(root, nr);
5047 /* helper for btfs_get_extent. Given an existing extent in the tree,
5048 * and an extent that you want to insert, deal with overlap and insert
5049 * the new extent into the tree.
5051 static int merge_extent_mapping(struct extent_map_tree *em_tree,
5052 struct extent_map *existing,
5053 struct extent_map *em,
5054 u64 map_start, u64 map_len)
5058 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5059 start_diff = map_start - em->start;
5060 em->start = map_start;
5062 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5063 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5064 em->block_start += start_diff;
5065 em->block_len -= start_diff;
5067 return add_extent_mapping(em_tree, em);
5070 static noinline int uncompress_inline(struct btrfs_path *path,
5071 struct inode *inode, struct page *page,
5072 size_t pg_offset, u64 extent_offset,
5073 struct btrfs_file_extent_item *item)
5076 struct extent_buffer *leaf = path->nodes[0];
5079 unsigned long inline_size;
5083 WARN_ON(pg_offset != 0);
5084 compress_type = btrfs_file_extent_compression(leaf, item);
5085 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5086 inline_size = btrfs_file_extent_inline_item_len(leaf,
5087 btrfs_item_nr(leaf, path->slots[0]));
5088 tmp = kmalloc(inline_size, GFP_NOFS);
5091 ptr = btrfs_file_extent_inline_start(item);
5093 read_extent_buffer(leaf, tmp, ptr, inline_size);
5095 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5096 ret = btrfs_decompress(compress_type, tmp, page,
5097 extent_offset, inline_size, max_size);
5099 char *kaddr = kmap_atomic(page, KM_USER0);
5100 unsigned long copy_size = min_t(u64,
5101 PAGE_CACHE_SIZE - pg_offset,
5102 max_size - extent_offset);
5103 memset(kaddr + pg_offset, 0, copy_size);
5104 kunmap_atomic(kaddr, KM_USER0);
5111 * a bit scary, this does extent mapping from logical file offset to the disk.
5112 * the ugly parts come from merging extents from the disk with the in-ram
5113 * representation. This gets more complex because of the data=ordered code,
5114 * where the in-ram extents might be locked pending data=ordered completion.
5116 * This also copies inline extents directly into the page.
5119 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5120 size_t pg_offset, u64 start, u64 len,
5126 u64 extent_start = 0;
5128 u64 objectid = btrfs_ino(inode);
5130 struct btrfs_path *path = NULL;
5131 struct btrfs_root *root = BTRFS_I(inode)->root;
5132 struct btrfs_file_extent_item *item;
5133 struct extent_buffer *leaf;
5134 struct btrfs_key found_key;
5135 struct extent_map *em = NULL;
5136 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5138 struct btrfs_trans_handle *trans = NULL;
5142 read_lock(&em_tree->lock);
5143 em = lookup_extent_mapping(em_tree, start, len);
5145 em->bdev = root->fs_info->fs_devices->latest_bdev;
5146 read_unlock(&em_tree->lock);
5149 if (em->start > start || em->start + em->len <= start)
5150 free_extent_map(em);
5151 else if (em->block_start == EXTENT_MAP_INLINE && page)
5152 free_extent_map(em);
5156 em = alloc_extent_map();
5161 em->bdev = root->fs_info->fs_devices->latest_bdev;
5162 em->start = EXTENT_MAP_HOLE;
5163 em->orig_start = EXTENT_MAP_HOLE;
5165 em->block_len = (u64)-1;
5168 path = btrfs_alloc_path();
5174 * Chances are we'll be called again, so go ahead and do
5180 ret = btrfs_lookup_file_extent(trans, root, path,
5181 objectid, start, trans != NULL);
5188 if (path->slots[0] == 0)
5193 leaf = path->nodes[0];
5194 item = btrfs_item_ptr(leaf, path->slots[0],
5195 struct btrfs_file_extent_item);
5196 /* are we inside the extent that was found? */
5197 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5198 found_type = btrfs_key_type(&found_key);
5199 if (found_key.objectid != objectid ||
5200 found_type != BTRFS_EXTENT_DATA_KEY) {
5204 found_type = btrfs_file_extent_type(leaf, item);
5205 extent_start = found_key.offset;
5206 compress_type = btrfs_file_extent_compression(leaf, item);
5207 if (found_type == BTRFS_FILE_EXTENT_REG ||
5208 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5209 extent_end = extent_start +
5210 btrfs_file_extent_num_bytes(leaf, item);
5211 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5213 size = btrfs_file_extent_inline_len(leaf, item);
5214 extent_end = (extent_start + size + root->sectorsize - 1) &
5215 ~((u64)root->sectorsize - 1);
5218 if (start >= extent_end) {
5220 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5221 ret = btrfs_next_leaf(root, path);
5228 leaf = path->nodes[0];
5230 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5231 if (found_key.objectid != objectid ||
5232 found_key.type != BTRFS_EXTENT_DATA_KEY)
5234 if (start + len <= found_key.offset)
5237 em->len = found_key.offset - start;
5241 if (found_type == BTRFS_FILE_EXTENT_REG ||
5242 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5243 em->start = extent_start;
5244 em->len = extent_end - extent_start;
5245 em->orig_start = extent_start -
5246 btrfs_file_extent_offset(leaf, item);
5247 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5249 em->block_start = EXTENT_MAP_HOLE;
5252 if (compress_type != BTRFS_COMPRESS_NONE) {
5253 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5254 em->compress_type = compress_type;
5255 em->block_start = bytenr;
5256 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5259 bytenr += btrfs_file_extent_offset(leaf, item);
5260 em->block_start = bytenr;
5261 em->block_len = em->len;
5262 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5263 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5266 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5270 size_t extent_offset;
5273 em->block_start = EXTENT_MAP_INLINE;
5274 if (!page || create) {
5275 em->start = extent_start;
5276 em->len = extent_end - extent_start;
5280 size = btrfs_file_extent_inline_len(leaf, item);
5281 extent_offset = page_offset(page) + pg_offset - extent_start;
5282 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
5283 size - extent_offset);
5284 em->start = extent_start + extent_offset;
5285 em->len = (copy_size + root->sectorsize - 1) &
5286 ~((u64)root->sectorsize - 1);
5287 em->orig_start = EXTENT_MAP_INLINE;
5288 if (compress_type) {
5289 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5290 em->compress_type = compress_type;
5292 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5293 if (create == 0 && !PageUptodate(page)) {
5294 if (btrfs_file_extent_compression(leaf, item) !=
5295 BTRFS_COMPRESS_NONE) {
5296 ret = uncompress_inline(path, inode, page,
5298 extent_offset, item);
5299 BUG_ON(ret); /* -ENOMEM */
5302 read_extent_buffer(leaf, map + pg_offset, ptr,
5304 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5305 memset(map + pg_offset + copy_size, 0,
5306 PAGE_CACHE_SIZE - pg_offset -
5311 flush_dcache_page(page);
5312 } else if (create && PageUptodate(page)) {
5316 free_extent_map(em);
5319 btrfs_release_path(path);
5320 trans = btrfs_join_transaction(root);
5323 return ERR_CAST(trans);
5327 write_extent_buffer(leaf, map + pg_offset, ptr,
5330 btrfs_mark_buffer_dirty(leaf);
5332 set_extent_uptodate(io_tree, em->start,
5333 extent_map_end(em) - 1, NULL, GFP_NOFS);
5336 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
5343 em->block_start = EXTENT_MAP_HOLE;
5344 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5346 btrfs_release_path(path);
5347 if (em->start > start || extent_map_end(em) <= start) {
5348 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5349 "[%llu %llu]\n", (unsigned long long)em->start,
5350 (unsigned long long)em->len,
5351 (unsigned long long)start,
5352 (unsigned long long)len);
5358 write_lock(&em_tree->lock);
5359 ret = add_extent_mapping(em_tree, em);
5360 /* it is possible that someone inserted the extent into the tree
5361 * while we had the lock dropped. It is also possible that
5362 * an overlapping map exists in the tree
5364 if (ret == -EEXIST) {
5365 struct extent_map *existing;
5369 existing = lookup_extent_mapping(em_tree, start, len);
5370 if (existing && (existing->start > start ||
5371 existing->start + existing->len <= start)) {
5372 free_extent_map(existing);
5376 existing = lookup_extent_mapping(em_tree, em->start,
5379 err = merge_extent_mapping(em_tree, existing,
5382 free_extent_map(existing);
5384 free_extent_map(em);
5389 free_extent_map(em);
5393 free_extent_map(em);
5398 write_unlock(&em_tree->lock);
5401 trace_btrfs_get_extent(root, em);
5404 btrfs_free_path(path);
5406 ret = btrfs_end_transaction(trans, root);
5411 free_extent_map(em);
5412 return ERR_PTR(err);
5414 BUG_ON(!em); /* Error is always set */
5418 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5419 size_t pg_offset, u64 start, u64 len,
5422 struct extent_map *em;
5423 struct extent_map *hole_em = NULL;
5424 u64 range_start = start;
5430 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5435 * if our em maps to a hole, there might
5436 * actually be delalloc bytes behind it
5438 if (em->block_start != EXTENT_MAP_HOLE)
5444 /* check to see if we've wrapped (len == -1 or similar) */
5453 /* ok, we didn't find anything, lets look for delalloc */
5454 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5455 end, len, EXTENT_DELALLOC, 1);
5456 found_end = range_start + found;
5457 if (found_end < range_start)
5458 found_end = (u64)-1;
5461 * we didn't find anything useful, return
5462 * the original results from get_extent()
5464 if (range_start > end || found_end <= start) {
5470 /* adjust the range_start to make sure it doesn't
5471 * go backwards from the start they passed in
5473 range_start = max(start,range_start);
5474 found = found_end - range_start;
5477 u64 hole_start = start;
5480 em = alloc_extent_map();
5486 * when btrfs_get_extent can't find anything it
5487 * returns one huge hole
5489 * make sure what it found really fits our range, and
5490 * adjust to make sure it is based on the start from
5494 u64 calc_end = extent_map_end(hole_em);
5496 if (calc_end <= start || (hole_em->start > end)) {
5497 free_extent_map(hole_em);
5500 hole_start = max(hole_em->start, start);
5501 hole_len = calc_end - hole_start;
5505 if (hole_em && range_start > hole_start) {
5506 /* our hole starts before our delalloc, so we
5507 * have to return just the parts of the hole
5508 * that go until the delalloc starts
5510 em->len = min(hole_len,
5511 range_start - hole_start);
5512 em->start = hole_start;
5513 em->orig_start = hole_start;
5515 * don't adjust block start at all,
5516 * it is fixed at EXTENT_MAP_HOLE
5518 em->block_start = hole_em->block_start;
5519 em->block_len = hole_len;
5521 em->start = range_start;
5523 em->orig_start = range_start;
5524 em->block_start = EXTENT_MAP_DELALLOC;
5525 em->block_len = found;
5527 } else if (hole_em) {
5532 free_extent_map(hole_em);
5534 free_extent_map(em);
5535 return ERR_PTR(err);
5540 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5541 struct extent_map *em,
5544 struct btrfs_root *root = BTRFS_I(inode)->root;
5545 struct btrfs_trans_handle *trans;
5546 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5547 struct btrfs_key ins;
5550 bool insert = false;
5553 * Ok if the extent map we looked up is a hole and is for the exact
5554 * range we want, there is no reason to allocate a new one, however if
5555 * it is not right then we need to free this one and drop the cache for
5558 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5560 free_extent_map(em);
5563 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5566 trans = btrfs_join_transaction(root);
5568 return ERR_CAST(trans);
5570 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5571 btrfs_add_inode_defrag(trans, inode);
5573 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5575 alloc_hint = get_extent_allocation_hint(inode, start, len);
5576 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5577 alloc_hint, (u64)-1, &ins, 1);
5584 em = alloc_extent_map();
5586 em = ERR_PTR(-ENOMEM);
5592 em->orig_start = em->start;
5593 em->len = ins.offset;
5595 em->block_start = ins.objectid;
5596 em->block_len = ins.offset;
5597 em->bdev = root->fs_info->fs_devices->latest_bdev;
5600 * We need to do this because if we're using the original em we searched
5601 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5604 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5607 write_lock(&em_tree->lock);
5608 ret = add_extent_mapping(em_tree, em);
5609 write_unlock(&em_tree->lock);
5612 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5615 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5616 ins.offset, ins.offset, 0);
5618 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5622 btrfs_end_transaction(trans, root);
5627 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5628 * block must be cow'd
5630 static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5631 struct inode *inode, u64 offset, u64 len)
5633 struct btrfs_path *path;
5635 struct extent_buffer *leaf;
5636 struct btrfs_root *root = BTRFS_I(inode)->root;
5637 struct btrfs_file_extent_item *fi;
5638 struct btrfs_key key;
5646 path = btrfs_alloc_path();
5650 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5655 slot = path->slots[0];
5658 /* can't find the item, must cow */
5665 leaf = path->nodes[0];
5666 btrfs_item_key_to_cpu(leaf, &key, slot);
5667 if (key.objectid != btrfs_ino(inode) ||
5668 key.type != BTRFS_EXTENT_DATA_KEY) {
5669 /* not our file or wrong item type, must cow */
5673 if (key.offset > offset) {
5674 /* Wrong offset, must cow */
5678 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5679 found_type = btrfs_file_extent_type(leaf, fi);
5680 if (found_type != BTRFS_FILE_EXTENT_REG &&
5681 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5682 /* not a regular extent, must cow */
5685 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5686 backref_offset = btrfs_file_extent_offset(leaf, fi);
5688 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5689 if (extent_end < offset + len) {
5690 /* extent doesn't include our full range, must cow */
5694 if (btrfs_extent_readonly(root, disk_bytenr))
5698 * look for other files referencing this extent, if we
5699 * find any we must cow
5701 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5702 key.offset - backref_offset, disk_bytenr))
5706 * adjust disk_bytenr and num_bytes to cover just the bytes
5707 * in this extent we are about to write. If there
5708 * are any csums in that range we have to cow in order
5709 * to keep the csums correct
5711 disk_bytenr += backref_offset;
5712 disk_bytenr += offset - key.offset;
5713 num_bytes = min(offset + len, extent_end) - offset;
5714 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5717 * all of the above have passed, it is safe to overwrite this extent
5722 btrfs_free_path(path);
5726 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5727 struct buffer_head *bh_result, int create)
5729 struct extent_map *em;
5730 struct btrfs_root *root = BTRFS_I(inode)->root;
5731 u64 start = iblock << inode->i_blkbits;
5732 u64 len = bh_result->b_size;
5733 struct btrfs_trans_handle *trans;
5735 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5740 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5741 * io. INLINE is special, and we could probably kludge it in here, but
5742 * it's still buffered so for safety lets just fall back to the generic
5745 * For COMPRESSED we _have_ to read the entire extent in so we can
5746 * decompress it, so there will be buffering required no matter what we
5747 * do, so go ahead and fallback to buffered.
5749 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5750 * to buffered IO. Don't blame me, this is the price we pay for using
5753 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5754 em->block_start == EXTENT_MAP_INLINE) {
5755 free_extent_map(em);
5759 /* Just a good old fashioned hole, return */
5760 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5761 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5762 free_extent_map(em);
5763 /* DIO will do one hole at a time, so just unlock a sector */
5764 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5765 start + root->sectorsize - 1);
5770 * We don't allocate a new extent in the following cases
5772 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5774 * 2) The extent is marked as PREALLOC. We're good to go here and can
5775 * just use the extent.
5779 len = em->len - (start - em->start);
5783 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5784 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5785 em->block_start != EXTENT_MAP_HOLE)) {
5790 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5791 type = BTRFS_ORDERED_PREALLOC;
5793 type = BTRFS_ORDERED_NOCOW;
5794 len = min(len, em->len - (start - em->start));
5795 block_start = em->block_start + (start - em->start);
5798 * we're not going to log anything, but we do need
5799 * to make sure the current transaction stays open
5800 * while we look for nocow cross refs
5802 trans = btrfs_join_transaction(root);
5806 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5807 ret = btrfs_add_ordered_extent_dio(inode, start,
5808 block_start, len, len, type);
5809 btrfs_end_transaction(trans, root);
5811 free_extent_map(em);
5816 btrfs_end_transaction(trans, root);
5820 * this will cow the extent, reset the len in case we changed
5823 len = bh_result->b_size;
5824 em = btrfs_new_extent_direct(inode, em, start, len);
5827 len = min(len, em->len - (start - em->start));
5829 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5830 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5833 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5835 bh_result->b_size = len;
5836 bh_result->b_bdev = em->bdev;
5837 set_buffer_mapped(bh_result);
5838 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5839 set_buffer_new(bh_result);
5841 free_extent_map(em);
5846 struct btrfs_dio_private {
5847 struct inode *inode;
5854 /* number of bios pending for this dio */
5855 atomic_t pending_bios;
5860 struct bio *orig_bio;
5863 static void btrfs_endio_direct_read(struct bio *bio, int err)
5865 struct btrfs_dio_private *dip = bio->bi_private;
5866 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5867 struct bio_vec *bvec = bio->bi_io_vec;
5868 struct inode *inode = dip->inode;
5869 struct btrfs_root *root = BTRFS_I(inode)->root;
5871 u32 *private = dip->csums;
5873 start = dip->logical_offset;
5875 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5876 struct page *page = bvec->bv_page;
5879 unsigned long flags;
5881 local_irq_save(flags);
5882 kaddr = kmap_atomic(page, KM_IRQ0);
5883 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5884 csum, bvec->bv_len);
5885 btrfs_csum_final(csum, (char *)&csum);
5886 kunmap_atomic(kaddr, KM_IRQ0);
5887 local_irq_restore(flags);
5889 flush_dcache_page(bvec->bv_page);
5890 if (csum != *private) {
5891 printk(KERN_ERR "btrfs csum failed ino %llu off"
5892 " %llu csum %u private %u\n",
5893 (unsigned long long)btrfs_ino(inode),
5894 (unsigned long long)start,
5900 start += bvec->bv_len;
5903 } while (bvec <= bvec_end);
5905 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5906 dip->logical_offset + dip->bytes - 1);
5907 bio->bi_private = dip->private;
5912 /* If we had a csum failure make sure to clear the uptodate flag */
5914 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5915 dio_end_io(bio, err);
5918 static void btrfs_endio_direct_write(struct bio *bio, int err)
5920 struct btrfs_dio_private *dip = bio->bi_private;
5921 struct inode *inode = dip->inode;
5922 struct btrfs_root *root = BTRFS_I(inode)->root;
5923 struct btrfs_trans_handle *trans;
5924 struct btrfs_ordered_extent *ordered = NULL;
5925 struct extent_state *cached_state = NULL;
5926 u64 ordered_offset = dip->logical_offset;
5927 u64 ordered_bytes = dip->bytes;
5933 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5941 trans = btrfs_join_transaction(root);
5942 if (IS_ERR(trans)) {
5946 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5948 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5949 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5951 err = btrfs_update_inode_fallback(trans, root, inode);
5955 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5956 ordered->file_offset + ordered->len - 1, 0,
5959 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5960 ret = btrfs_mark_extent_written(trans, inode,
5961 ordered->file_offset,
5962 ordered->file_offset +
5969 ret = insert_reserved_file_extent(trans, inode,
5970 ordered->file_offset,
5976 BTRFS_FILE_EXTENT_REG);
5977 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5978 ordered->file_offset, ordered->len);
5986 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5987 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5988 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags))
5989 btrfs_update_inode_fallback(trans, root, inode);
5992 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5993 ordered->file_offset + ordered->len - 1,
5994 &cached_state, GFP_NOFS);
5996 btrfs_delalloc_release_metadata(inode, ordered->len);
5997 btrfs_end_transaction(trans, root);
5998 ordered_offset = ordered->file_offset + ordered->len;
5999 btrfs_put_ordered_extent(ordered);
6000 btrfs_put_ordered_extent(ordered);
6004 * our bio might span multiple ordered extents. If we haven't
6005 * completed the accounting for the whole dio, go back and try again
6007 if (ordered_offset < dip->logical_offset + dip->bytes) {
6008 ordered_bytes = dip->logical_offset + dip->bytes -
6013 bio->bi_private = dip->private;
6018 /* If we had an error make sure to clear the uptodate flag */
6020 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6021 dio_end_io(bio, err);
6024 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6025 struct bio *bio, int mirror_num,
6026 unsigned long bio_flags, u64 offset)
6029 struct btrfs_root *root = BTRFS_I(inode)->root;
6030 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6031 BUG_ON(ret); /* -ENOMEM */
6035 static void btrfs_end_dio_bio(struct bio *bio, int err)
6037 struct btrfs_dio_private *dip = bio->bi_private;
6040 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
6041 "sector %#Lx len %u err no %d\n",
6042 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
6043 (unsigned long long)bio->bi_sector, bio->bi_size, err);
6047 * before atomic variable goto zero, we must make sure
6048 * dip->errors is perceived to be set.
6050 smp_mb__before_atomic_dec();
6053 /* if there are more bios still pending for this dio, just exit */
6054 if (!atomic_dec_and_test(&dip->pending_bios))
6058 bio_io_error(dip->orig_bio);
6060 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6061 bio_endio(dip->orig_bio, 0);
6067 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6068 u64 first_sector, gfp_t gfp_flags)
6070 int nr_vecs = bio_get_nr_vecs(bdev);
6071 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6074 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6075 int rw, u64 file_offset, int skip_sum,
6076 u32 *csums, int async_submit)
6078 int write = rw & REQ_WRITE;
6079 struct btrfs_root *root = BTRFS_I(inode)->root;
6083 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6090 if (write && async_submit) {
6091 ret = btrfs_wq_submit_bio(root->fs_info,
6092 inode, rw, bio, 0, 0,
6094 __btrfs_submit_bio_start_direct_io,
6095 __btrfs_submit_bio_done);
6099 * If we aren't doing async submit, calculate the csum of the
6102 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6105 } else if (!skip_sum) {
6106 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
6107 file_offset, csums);
6113 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6119 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6122 struct inode *inode = dip->inode;
6123 struct btrfs_root *root = BTRFS_I(inode)->root;
6124 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6126 struct bio *orig_bio = dip->orig_bio;
6127 struct bio_vec *bvec = orig_bio->bi_io_vec;
6128 u64 start_sector = orig_bio->bi_sector;
6129 u64 file_offset = dip->logical_offset;
6133 u32 *csums = dip->csums;
6135 int async_submit = 0;
6136 int write = rw & REQ_WRITE;
6138 map_length = orig_bio->bi_size;
6139 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6140 &map_length, NULL, 0);
6146 if (map_length >= orig_bio->bi_size) {
6152 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6155 bio->bi_private = dip;
6156 bio->bi_end_io = btrfs_end_dio_bio;
6157 atomic_inc(&dip->pending_bios);
6159 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6160 if (unlikely(map_length < submit_len + bvec->bv_len ||
6161 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6162 bvec->bv_offset) < bvec->bv_len)) {
6164 * inc the count before we submit the bio so
6165 * we know the end IO handler won't happen before
6166 * we inc the count. Otherwise, the dip might get freed
6167 * before we're done setting it up
6169 atomic_inc(&dip->pending_bios);
6170 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6171 file_offset, skip_sum,
6172 csums, async_submit);
6175 atomic_dec(&dip->pending_bios);
6179 /* Write's use the ordered csums */
6180 if (!write && !skip_sum)
6181 csums = csums + nr_pages;
6182 start_sector += submit_len >> 9;
6183 file_offset += submit_len;
6188 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6189 start_sector, GFP_NOFS);
6192 bio->bi_private = dip;
6193 bio->bi_end_io = btrfs_end_dio_bio;
6195 map_length = orig_bio->bi_size;
6196 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6197 &map_length, NULL, 0);
6203 submit_len += bvec->bv_len;
6210 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6211 csums, async_submit);
6219 * before atomic variable goto zero, we must
6220 * make sure dip->errors is perceived to be set.
6222 smp_mb__before_atomic_dec();
6223 if (atomic_dec_and_test(&dip->pending_bios))
6224 bio_io_error(dip->orig_bio);
6226 /* bio_end_io() will handle error, so we needn't return it */
6230 static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6233 struct btrfs_root *root = BTRFS_I(inode)->root;
6234 struct btrfs_dio_private *dip;
6235 struct bio_vec *bvec = bio->bi_io_vec;
6237 int write = rw & REQ_WRITE;
6240 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6242 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6249 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6250 if (!write && !skip_sum) {
6251 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6259 dip->private = bio->bi_private;
6261 dip->logical_offset = file_offset;
6265 dip->bytes += bvec->bv_len;
6267 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6269 dip->disk_bytenr = (u64)bio->bi_sector << 9;
6270 bio->bi_private = dip;
6272 dip->orig_bio = bio;
6273 atomic_set(&dip->pending_bios, 0);
6276 bio->bi_end_io = btrfs_endio_direct_write;
6278 bio->bi_end_io = btrfs_endio_direct_read;
6280 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6285 * If this is a write, we need to clean up the reserved space and kill
6286 * the ordered extent.
6289 struct btrfs_ordered_extent *ordered;
6290 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
6291 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6292 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6293 btrfs_free_reserved_extent(root, ordered->start,
6295 btrfs_put_ordered_extent(ordered);
6296 btrfs_put_ordered_extent(ordered);
6298 bio_endio(bio, ret);
6301 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6302 const struct iovec *iov, loff_t offset,
6303 unsigned long nr_segs)
6309 unsigned blocksize_mask = root->sectorsize - 1;
6310 ssize_t retval = -EINVAL;
6311 loff_t end = offset;
6313 if (offset & blocksize_mask)
6316 /* Check the memory alignment. Blocks cannot straddle pages */
6317 for (seg = 0; seg < nr_segs; seg++) {
6318 addr = (unsigned long)iov[seg].iov_base;
6319 size = iov[seg].iov_len;
6321 if ((addr & blocksize_mask) || (size & blocksize_mask))
6324 /* If this is a write we don't need to check anymore */
6329 * Check to make sure we don't have duplicate iov_base's in this
6330 * iovec, if so return EINVAL, otherwise we'll get csum errors
6331 * when reading back.
6333 for (i = seg + 1; i < nr_segs; i++) {
6334 if (iov[seg].iov_base == iov[i].iov_base)
6342 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6343 const struct iovec *iov, loff_t offset,
6344 unsigned long nr_segs)
6346 struct file *file = iocb->ki_filp;
6347 struct inode *inode = file->f_mapping->host;
6348 struct btrfs_ordered_extent *ordered;
6349 struct extent_state *cached_state = NULL;
6350 u64 lockstart, lockend;
6352 int writing = rw & WRITE;
6354 size_t count = iov_length(iov, nr_segs);
6356 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6362 lockend = offset + count - 1;
6365 ret = btrfs_delalloc_reserve_space(inode, count);
6371 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6374 * We're concerned with the entire range that we're going to be
6375 * doing DIO to, so we need to make sure theres no ordered
6376 * extents in this range.
6378 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6379 lockend - lockstart + 1);
6382 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6383 &cached_state, GFP_NOFS);
6384 btrfs_start_ordered_extent(inode, ordered, 1);
6385 btrfs_put_ordered_extent(ordered);
6390 * we don't use btrfs_set_extent_delalloc because we don't want
6391 * the dirty or uptodate bits
6394 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6395 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6396 EXTENT_DELALLOC, NULL, &cached_state,
6399 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6400 lockend, EXTENT_LOCKED | write_bits,
6401 1, 0, &cached_state, GFP_NOFS);
6406 free_extent_state(cached_state);
6407 cached_state = NULL;
6409 ret = __blockdev_direct_IO(rw, iocb, inode,
6410 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6411 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6412 btrfs_submit_direct, 0);
6414 if (ret < 0 && ret != -EIOCBQUEUED) {
6415 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6416 offset + iov_length(iov, nr_segs) - 1,
6417 EXTENT_LOCKED | write_bits, 1, 0,
6418 &cached_state, GFP_NOFS);
6419 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6421 * We're falling back to buffered, unlock the section we didn't
6424 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6425 offset + iov_length(iov, nr_segs) - 1,
6426 EXTENT_LOCKED | write_bits, 1, 0,
6427 &cached_state, GFP_NOFS);
6430 free_extent_state(cached_state);
6434 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6435 __u64 start, __u64 len)
6437 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6440 int btrfs_readpage(struct file *file, struct page *page)
6442 struct extent_io_tree *tree;
6443 tree = &BTRFS_I(page->mapping->host)->io_tree;
6444 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
6447 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6449 struct extent_io_tree *tree;
6452 if (current->flags & PF_MEMALLOC) {
6453 redirty_page_for_writepage(wbc, page);
6457 tree = &BTRFS_I(page->mapping->host)->io_tree;
6458 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6461 int btrfs_writepages(struct address_space *mapping,
6462 struct writeback_control *wbc)
6464 struct extent_io_tree *tree;
6466 tree = &BTRFS_I(mapping->host)->io_tree;
6467 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6471 btrfs_readpages(struct file *file, struct address_space *mapping,
6472 struct list_head *pages, unsigned nr_pages)
6474 struct extent_io_tree *tree;
6475 tree = &BTRFS_I(mapping->host)->io_tree;
6476 return extent_readpages(tree, mapping, pages, nr_pages,
6479 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6481 struct extent_io_tree *tree;
6482 struct extent_map_tree *map;
6485 tree = &BTRFS_I(page->mapping->host)->io_tree;
6486 map = &BTRFS_I(page->mapping->host)->extent_tree;
6487 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
6489 ClearPagePrivate(page);
6490 set_page_private(page, 0);
6491 page_cache_release(page);
6496 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6498 if (PageWriteback(page) || PageDirty(page))
6500 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
6503 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6505 struct extent_io_tree *tree;
6506 struct btrfs_ordered_extent *ordered;
6507 struct extent_state *cached_state = NULL;
6508 u64 page_start = page_offset(page);
6509 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
6513 * we have the page locked, so new writeback can't start,
6514 * and the dirty bit won't be cleared while we are here.
6516 * Wait for IO on this page so that we can safely clear
6517 * the PagePrivate2 bit and do ordered accounting
6519 wait_on_page_writeback(page);
6521 tree = &BTRFS_I(page->mapping->host)->io_tree;
6523 btrfs_releasepage(page, GFP_NOFS);
6526 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
6527 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6531 * IO on this page will never be started, so we need
6532 * to account for any ordered extents now
6534 clear_extent_bit(tree, page_start, page_end,
6535 EXTENT_DIRTY | EXTENT_DELALLOC |
6536 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
6537 &cached_state, GFP_NOFS);
6539 * whoever cleared the private bit is responsible
6540 * for the finish_ordered_io
6542 if (TestClearPagePrivate2(page)) {
6543 btrfs_finish_ordered_io(page->mapping->host,
6544 page_start, page_end);
6546 btrfs_put_ordered_extent(ordered);
6547 cached_state = NULL;
6548 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
6550 clear_extent_bit(tree, page_start, page_end,
6551 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
6552 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
6553 __btrfs_releasepage(page, GFP_NOFS);
6555 ClearPageChecked(page);
6556 if (PagePrivate(page)) {
6557 ClearPagePrivate(page);
6558 set_page_private(page, 0);
6559 page_cache_release(page);
6564 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6565 * called from a page fault handler when a page is first dirtied. Hence we must
6566 * be careful to check for EOF conditions here. We set the page up correctly
6567 * for a written page which means we get ENOSPC checking when writing into
6568 * holes and correct delalloc and unwritten extent mapping on filesystems that
6569 * support these features.
6571 * We are not allowed to take the i_mutex here so we have to play games to
6572 * protect against truncate races as the page could now be beyond EOF. Because
6573 * vmtruncate() writes the inode size before removing pages, once we have the
6574 * page lock we can determine safely if the page is beyond EOF. If it is not
6575 * beyond EOF, then the page is guaranteed safe against truncation until we
6578 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
6580 struct page *page = vmf->page;
6581 struct inode *inode = fdentry(vma->vm_file)->d_inode;
6582 struct btrfs_root *root = BTRFS_I(inode)->root;
6583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6584 struct btrfs_ordered_extent *ordered;
6585 struct extent_state *cached_state = NULL;
6587 unsigned long zero_start;
6594 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6596 ret = btrfs_update_time(vma->vm_file);
6602 else /* -ENOSPC, -EIO, etc */
6603 ret = VM_FAULT_SIGBUS;
6609 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
6612 size = i_size_read(inode);
6613 page_start = page_offset(page);
6614 page_end = page_start + PAGE_CACHE_SIZE - 1;
6616 if ((page->mapping != inode->i_mapping) ||
6617 (page_start >= size)) {
6618 /* page got truncated out from underneath us */
6621 wait_on_page_writeback(page);
6623 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
6624 set_page_extent_mapped(page);
6627 * we can't set the delalloc bits if there are pending ordered
6628 * extents. Drop our locks and wait for them to finish
6630 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6632 unlock_extent_cached(io_tree, page_start, page_end,
6633 &cached_state, GFP_NOFS);
6635 btrfs_start_ordered_extent(inode, ordered, 1);
6636 btrfs_put_ordered_extent(ordered);
6641 * XXX - page_mkwrite gets called every time the page is dirtied, even
6642 * if it was already dirty, so for space accounting reasons we need to
6643 * clear any delalloc bits for the range we are fixing to save. There
6644 * is probably a better way to do this, but for now keep consistent with
6645 * prepare_pages in the normal write path.
6647 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
6648 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
6649 0, 0, &cached_state, GFP_NOFS);
6651 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6654 unlock_extent_cached(io_tree, page_start, page_end,
6655 &cached_state, GFP_NOFS);
6656 ret = VM_FAULT_SIGBUS;
6661 /* page is wholly or partially inside EOF */
6662 if (page_start + PAGE_CACHE_SIZE > size)
6663 zero_start = size & ~PAGE_CACHE_MASK;
6665 zero_start = PAGE_CACHE_SIZE;
6667 if (zero_start != PAGE_CACHE_SIZE) {
6669 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6670 flush_dcache_page(page);
6673 ClearPageChecked(page);
6674 set_page_dirty(page);
6675 SetPageUptodate(page);
6677 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6678 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6680 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
6684 return VM_FAULT_LOCKED;
6687 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
6692 static int btrfs_truncate(struct inode *inode)
6694 struct btrfs_root *root = BTRFS_I(inode)->root;
6695 struct btrfs_block_rsv *rsv;
6698 struct btrfs_trans_handle *trans;
6700 u64 mask = root->sectorsize - 1;
6701 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
6703 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6707 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6708 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6711 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6712 * 3 things going on here
6714 * 1) We need to reserve space for our orphan item and the space to
6715 * delete our orphan item. Lord knows we don't want to have a dangling
6716 * orphan item because we didn't reserve space to remove it.
6718 * 2) We need to reserve space to update our inode.
6720 * 3) We need to have something to cache all the space that is going to
6721 * be free'd up by the truncate operation, but also have some slack
6722 * space reserved in case it uses space during the truncate (thank you
6723 * very much snapshotting).
6725 * And we need these to all be seperate. The fact is we can use alot of
6726 * space doing the truncate, and we have no earthly idea how much space
6727 * we will use, so we need the truncate reservation to be seperate so it
6728 * doesn't end up using space reserved for updating the inode or
6729 * removing the orphan item. We also need to be able to stop the
6730 * transaction and start a new one, which means we need to be able to
6731 * update the inode several times, and we have no idea of knowing how
6732 * many times that will be, so we can't just reserve 1 item for the
6733 * entirety of the opration, so that has to be done seperately as well.
6734 * Then there is the orphan item, which does indeed need to be held on
6735 * to for the whole operation, and we need nobody to touch this reserved
6736 * space except the orphan code.
6738 * So that leaves us with
6740 * 1) root->orphan_block_rsv - for the orphan deletion.
6741 * 2) rsv - for the truncate reservation, which we will steal from the
6742 * transaction reservation.
6743 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6744 * updating the inode.
6746 rsv = btrfs_alloc_block_rsv(root);
6749 rsv->size = min_size;
6752 * 1 for the truncate slack space
6753 * 1 for the orphan item we're going to add
6754 * 1 for the orphan item deletion
6755 * 1 for updating the inode.
6757 trans = btrfs_start_transaction(root, 4);
6758 if (IS_ERR(trans)) {
6759 err = PTR_ERR(trans);
6763 /* Migrate the slack space for the truncate to our reserve */
6764 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6768 ret = btrfs_orphan_add(trans, inode);
6770 btrfs_end_transaction(trans, root);
6775 * setattr is responsible for setting the ordered_data_close flag,
6776 * but that is only tested during the last file release. That
6777 * could happen well after the next commit, leaving a great big
6778 * window where new writes may get lost if someone chooses to write
6779 * to this file after truncating to zero
6781 * The inode doesn't have any dirty data here, and so if we commit
6782 * this is a noop. If someone immediately starts writing to the inode
6783 * it is very likely we'll catch some of their writes in this
6784 * transaction, and the commit will find this file on the ordered
6785 * data list with good things to send down.
6787 * This is a best effort solution, there is still a window where
6788 * using truncate to replace the contents of the file will
6789 * end up with a zero length file after a crash.
6791 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6792 btrfs_add_ordered_operation(trans, root, inode);
6795 ret = btrfs_block_rsv_refill(root, rsv, min_size);
6798 * This can only happen with the original transaction we
6799 * started above, every other time we shouldn't have a
6800 * transaction started yet.
6809 /* Just need the 1 for updating the inode */
6810 trans = btrfs_start_transaction(root, 1);
6811 if (IS_ERR(trans)) {
6812 ret = err = PTR_ERR(trans);
6818 trans->block_rsv = rsv;
6820 ret = btrfs_truncate_inode_items(trans, root, inode,
6822 BTRFS_EXTENT_DATA_KEY);
6823 if (ret != -EAGAIN) {
6828 trans->block_rsv = &root->fs_info->trans_block_rsv;
6829 ret = btrfs_update_inode(trans, root, inode);
6835 nr = trans->blocks_used;
6836 btrfs_end_transaction(trans, root);
6838 btrfs_btree_balance_dirty(root, nr);
6841 if (ret == 0 && inode->i_nlink > 0) {
6842 trans->block_rsv = root->orphan_block_rsv;
6843 ret = btrfs_orphan_del(trans, inode);
6846 } else if (ret && inode->i_nlink > 0) {
6848 * Failed to do the truncate, remove us from the in memory
6851 ret = btrfs_orphan_del(NULL, inode);
6855 trans->block_rsv = &root->fs_info->trans_block_rsv;
6856 ret = btrfs_update_inode(trans, root, inode);
6860 nr = trans->blocks_used;
6861 ret = btrfs_end_transaction(trans, root);
6862 btrfs_btree_balance_dirty(root, nr);
6866 btrfs_free_block_rsv(root, rsv);
6875 * create a new subvolume directory/inode (helper for the ioctl).
6877 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
6878 struct btrfs_root *new_root, u64 new_dirid)
6880 struct inode *inode;
6884 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6885 new_dirid, new_dirid,
6886 S_IFDIR | (~current_umask() & S_IRWXUGO),
6889 return PTR_ERR(inode);
6890 inode->i_op = &btrfs_dir_inode_operations;
6891 inode->i_fop = &btrfs_dir_file_operations;
6893 set_nlink(inode, 1);
6894 btrfs_i_size_write(inode, 0);
6896 err = btrfs_update_inode(trans, new_root, inode);
6902 struct inode *btrfs_alloc_inode(struct super_block *sb)
6904 struct btrfs_inode *ei;
6905 struct inode *inode;
6907 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6912 ei->space_info = NULL;
6916 ei->last_sub_trans = 0;
6917 ei->logged_trans = 0;
6918 ei->delalloc_bytes = 0;
6919 ei->disk_i_size = 0;
6922 ei->index_cnt = (u64)-1;
6923 ei->last_unlink_trans = 0;
6925 spin_lock_init(&ei->lock);
6926 ei->outstanding_extents = 0;
6927 ei->reserved_extents = 0;
6929 ei->ordered_data_close = 0;
6930 ei->orphan_meta_reserved = 0;
6931 ei->dummy_inode = 0;
6933 ei->delalloc_meta_reserved = 0;
6934 ei->force_compress = BTRFS_COMPRESS_NONE;
6936 ei->delayed_node = NULL;
6938 inode = &ei->vfs_inode;
6939 extent_map_tree_init(&ei->extent_tree);
6940 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6941 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
6942 mutex_init(&ei->log_mutex);
6943 mutex_init(&ei->delalloc_mutex);
6944 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6945 INIT_LIST_HEAD(&ei->i_orphan);
6946 INIT_LIST_HEAD(&ei->delalloc_inodes);
6947 INIT_LIST_HEAD(&ei->ordered_operations);
6948 RB_CLEAR_NODE(&ei->rb_node);
6953 static void btrfs_i_callback(struct rcu_head *head)
6955 struct inode *inode = container_of(head, struct inode, i_rcu);
6956 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6959 void btrfs_destroy_inode(struct inode *inode)
6961 struct btrfs_ordered_extent *ordered;
6962 struct btrfs_root *root = BTRFS_I(inode)->root;
6964 WARN_ON(!list_empty(&inode->i_dentry));
6965 WARN_ON(inode->i_data.nrpages);
6966 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6967 WARN_ON(BTRFS_I(inode)->reserved_extents);
6968 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6969 WARN_ON(BTRFS_I(inode)->csum_bytes);
6972 * This can happen where we create an inode, but somebody else also
6973 * created the same inode and we need to destroy the one we already
6980 * Make sure we're properly removed from the ordered operation
6984 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6985 spin_lock(&root->fs_info->ordered_extent_lock);
6986 list_del_init(&BTRFS_I(inode)->ordered_operations);
6987 spin_unlock(&root->fs_info->ordered_extent_lock);
6990 spin_lock(&root->orphan_lock);
6991 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6992 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6993 (unsigned long long)btrfs_ino(inode));
6994 list_del_init(&BTRFS_I(inode)->i_orphan);
6996 spin_unlock(&root->orphan_lock);
6999 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7003 printk(KERN_ERR "btrfs found ordered "
7004 "extent %llu %llu on inode cleanup\n",
7005 (unsigned long long)ordered->file_offset,
7006 (unsigned long long)ordered->len);
7007 btrfs_remove_ordered_extent(inode, ordered);
7008 btrfs_put_ordered_extent(ordered);
7009 btrfs_put_ordered_extent(ordered);
7012 inode_tree_del(inode);
7013 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7015 btrfs_remove_delayed_node(inode);
7016 call_rcu(&inode->i_rcu, btrfs_i_callback);
7019 int btrfs_drop_inode(struct inode *inode)
7021 struct btrfs_root *root = BTRFS_I(inode)->root;
7023 if (btrfs_root_refs(&root->root_item) == 0 &&
7024 !btrfs_is_free_space_inode(root, inode))
7027 return generic_drop_inode(inode);
7030 static void init_once(void *foo)
7032 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7034 inode_init_once(&ei->vfs_inode);
7037 void btrfs_destroy_cachep(void)
7039 if (btrfs_inode_cachep)
7040 kmem_cache_destroy(btrfs_inode_cachep);
7041 if (btrfs_trans_handle_cachep)
7042 kmem_cache_destroy(btrfs_trans_handle_cachep);
7043 if (btrfs_transaction_cachep)
7044 kmem_cache_destroy(btrfs_transaction_cachep);
7045 if (btrfs_path_cachep)
7046 kmem_cache_destroy(btrfs_path_cachep);
7047 if (btrfs_free_space_cachep)
7048 kmem_cache_destroy(btrfs_free_space_cachep);
7051 int btrfs_init_cachep(void)
7053 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7054 sizeof(struct btrfs_inode), 0,
7055 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
7056 if (!btrfs_inode_cachep)
7059 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7060 sizeof(struct btrfs_trans_handle), 0,
7061 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7062 if (!btrfs_trans_handle_cachep)
7065 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7066 sizeof(struct btrfs_transaction), 0,
7067 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7068 if (!btrfs_transaction_cachep)
7071 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7072 sizeof(struct btrfs_path), 0,
7073 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7074 if (!btrfs_path_cachep)
7077 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7078 sizeof(struct btrfs_free_space), 0,
7079 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7080 if (!btrfs_free_space_cachep)
7085 btrfs_destroy_cachep();
7089 static int btrfs_getattr(struct vfsmount *mnt,
7090 struct dentry *dentry, struct kstat *stat)
7092 struct inode *inode = dentry->d_inode;
7093 u32 blocksize = inode->i_sb->s_blocksize;
7095 generic_fillattr(inode, stat);
7096 stat->dev = BTRFS_I(inode)->root->anon_dev;
7097 stat->blksize = PAGE_CACHE_SIZE;
7098 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7099 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
7104 * If a file is moved, it will inherit the cow and compression flags of the new
7107 static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7109 struct btrfs_inode *b_dir = BTRFS_I(dir);
7110 struct btrfs_inode *b_inode = BTRFS_I(inode);
7112 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7113 b_inode->flags |= BTRFS_INODE_NODATACOW;
7115 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7117 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7118 b_inode->flags |= BTRFS_INODE_COMPRESS;
7120 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7123 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7124 struct inode *new_dir, struct dentry *new_dentry)
7126 struct btrfs_trans_handle *trans;
7127 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7128 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7129 struct inode *new_inode = new_dentry->d_inode;
7130 struct inode *old_inode = old_dentry->d_inode;
7131 struct timespec ctime = CURRENT_TIME;
7135 u64 old_ino = btrfs_ino(old_inode);
7137 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7140 /* we only allow rename subvolume link between subvolumes */
7141 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7144 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7145 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7148 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7149 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7152 * we're using rename to replace one file with another.
7153 * and the replacement file is large. Start IO on it now so
7154 * we don't add too much work to the end of the transaction
7156 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7157 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7158 filemap_flush(old_inode->i_mapping);
7160 /* close the racy window with snapshot create/destroy ioctl */
7161 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7162 down_read(&root->fs_info->subvol_sem);
7164 * We want to reserve the absolute worst case amount of items. So if
7165 * both inodes are subvols and we need to unlink them then that would
7166 * require 4 item modifications, but if they are both normal inodes it
7167 * would require 5 item modifications, so we'll assume their normal
7168 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7169 * should cover the worst case number of items we'll modify.
7171 trans = btrfs_start_transaction(root, 20);
7172 if (IS_ERR(trans)) {
7173 ret = PTR_ERR(trans);
7178 btrfs_record_root_in_trans(trans, dest);
7180 ret = btrfs_set_inode_index(new_dir, &index);
7184 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7185 /* force full log commit if subvolume involved. */
7186 root->fs_info->last_trans_log_full_commit = trans->transid;
7188 ret = btrfs_insert_inode_ref(trans, dest,
7189 new_dentry->d_name.name,
7190 new_dentry->d_name.len,
7192 btrfs_ino(new_dir), index);
7196 * this is an ugly little race, but the rename is required
7197 * to make sure that if we crash, the inode is either at the
7198 * old name or the new one. pinning the log transaction lets
7199 * us make sure we don't allow a log commit to come in after
7200 * we unlink the name but before we add the new name back in.
7202 btrfs_pin_log_trans(root);
7205 * make sure the inode gets flushed if it is replacing
7208 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7209 btrfs_add_ordered_operation(trans, root, old_inode);
7211 old_dir->i_ctime = old_dir->i_mtime = ctime;
7212 new_dir->i_ctime = new_dir->i_mtime = ctime;
7213 old_inode->i_ctime = ctime;
7215 if (old_dentry->d_parent != new_dentry->d_parent)
7216 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7218 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7219 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7220 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7221 old_dentry->d_name.name,
7222 old_dentry->d_name.len);
7224 ret = __btrfs_unlink_inode(trans, root, old_dir,
7225 old_dentry->d_inode,
7226 old_dentry->d_name.name,
7227 old_dentry->d_name.len);
7229 ret = btrfs_update_inode(trans, root, old_inode);
7232 btrfs_abort_transaction(trans, root, ret);
7237 new_inode->i_ctime = CURRENT_TIME;
7238 if (unlikely(btrfs_ino(new_inode) ==
7239 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7240 root_objectid = BTRFS_I(new_inode)->location.objectid;
7241 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7243 new_dentry->d_name.name,
7244 new_dentry->d_name.len);
7245 BUG_ON(new_inode->i_nlink == 0);
7247 ret = btrfs_unlink_inode(trans, dest, new_dir,
7248 new_dentry->d_inode,
7249 new_dentry->d_name.name,
7250 new_dentry->d_name.len);
7252 if (!ret && new_inode->i_nlink == 0) {
7253 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7257 btrfs_abort_transaction(trans, root, ret);
7262 fixup_inode_flags(new_dir, old_inode);
7264 ret = btrfs_add_link(trans, new_dir, old_inode,
7265 new_dentry->d_name.name,
7266 new_dentry->d_name.len, 0, index);
7268 btrfs_abort_transaction(trans, root, ret);
7272 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7273 struct dentry *parent = new_dentry->d_parent;
7274 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7275 btrfs_end_log_trans(root);
7278 btrfs_end_transaction(trans, root);
7280 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7281 up_read(&root->fs_info->subvol_sem);
7287 * some fairly slow code that needs optimization. This walks the list
7288 * of all the inodes with pending delalloc and forces them to disk.
7290 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7292 struct list_head *head = &root->fs_info->delalloc_inodes;
7293 struct btrfs_inode *binode;
7294 struct inode *inode;
7296 if (root->fs_info->sb->s_flags & MS_RDONLY)
7299 spin_lock(&root->fs_info->delalloc_lock);
7300 while (!list_empty(head)) {
7301 binode = list_entry(head->next, struct btrfs_inode,
7303 inode = igrab(&binode->vfs_inode);
7305 list_del_init(&binode->delalloc_inodes);
7306 spin_unlock(&root->fs_info->delalloc_lock);
7308 filemap_flush(inode->i_mapping);
7310 btrfs_add_delayed_iput(inode);
7315 spin_lock(&root->fs_info->delalloc_lock);
7317 spin_unlock(&root->fs_info->delalloc_lock);
7319 /* the filemap_flush will queue IO into the worker threads, but
7320 * we have to make sure the IO is actually started and that
7321 * ordered extents get created before we return
7323 atomic_inc(&root->fs_info->async_submit_draining);
7324 while (atomic_read(&root->fs_info->nr_async_submits) ||
7325 atomic_read(&root->fs_info->async_delalloc_pages)) {
7326 wait_event(root->fs_info->async_submit_wait,
7327 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7328 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7330 atomic_dec(&root->fs_info->async_submit_draining);
7334 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7335 const char *symname)
7337 struct btrfs_trans_handle *trans;
7338 struct btrfs_root *root = BTRFS_I(dir)->root;
7339 struct btrfs_path *path;
7340 struct btrfs_key key;
7341 struct inode *inode = NULL;
7349 struct btrfs_file_extent_item *ei;
7350 struct extent_buffer *leaf;
7351 unsigned long nr = 0;
7353 name_len = strlen(symname) + 1;
7354 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7355 return -ENAMETOOLONG;
7358 * 2 items for inode item and ref
7359 * 2 items for dir items
7360 * 1 item for xattr if selinux is on
7362 trans = btrfs_start_transaction(root, 5);
7364 return PTR_ERR(trans);
7366 err = btrfs_find_free_ino(root, &objectid);
7370 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7371 dentry->d_name.len, btrfs_ino(dir), objectid,
7372 S_IFLNK|S_IRWXUGO, &index);
7373 if (IS_ERR(inode)) {
7374 err = PTR_ERR(inode);
7378 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
7385 * If the active LSM wants to access the inode during
7386 * d_instantiate it needs these. Smack checks to see
7387 * if the filesystem supports xattrs by looking at the
7390 inode->i_fop = &btrfs_file_operations;
7391 inode->i_op = &btrfs_file_inode_operations;
7393 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7397 inode->i_mapping->a_ops = &btrfs_aops;
7398 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7399 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7404 path = btrfs_alloc_path();
7410 key.objectid = btrfs_ino(inode);
7412 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7413 datasize = btrfs_file_extent_calc_inline_size(name_len);
7414 err = btrfs_insert_empty_item(trans, root, path, &key,
7418 btrfs_free_path(path);
7421 leaf = path->nodes[0];
7422 ei = btrfs_item_ptr(leaf, path->slots[0],
7423 struct btrfs_file_extent_item);
7424 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7425 btrfs_set_file_extent_type(leaf, ei,
7426 BTRFS_FILE_EXTENT_INLINE);
7427 btrfs_set_file_extent_encryption(leaf, ei, 0);
7428 btrfs_set_file_extent_compression(leaf, ei, 0);
7429 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7430 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7432 ptr = btrfs_file_extent_inline_start(ei);
7433 write_extent_buffer(leaf, symname, ptr, name_len);
7434 btrfs_mark_buffer_dirty(leaf);
7435 btrfs_free_path(path);
7437 inode->i_op = &btrfs_symlink_inode_operations;
7438 inode->i_mapping->a_ops = &btrfs_symlink_aops;
7439 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7440 inode_set_bytes(inode, name_len);
7441 btrfs_i_size_write(inode, name_len - 1);
7442 err = btrfs_update_inode(trans, root, inode);
7448 d_instantiate(dentry, inode);
7449 nr = trans->blocks_used;
7450 btrfs_end_transaction(trans, root);
7452 inode_dec_link_count(inode);
7455 btrfs_btree_balance_dirty(root, nr);
7459 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7460 u64 start, u64 num_bytes, u64 min_size,
7461 loff_t actual_len, u64 *alloc_hint,
7462 struct btrfs_trans_handle *trans)
7464 struct btrfs_root *root = BTRFS_I(inode)->root;
7465 struct btrfs_key ins;
7466 u64 cur_offset = start;
7469 bool own_trans = true;
7473 while (num_bytes > 0) {
7475 trans = btrfs_start_transaction(root, 3);
7476 if (IS_ERR(trans)) {
7477 ret = PTR_ERR(trans);
7482 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7483 0, *alloc_hint, (u64)-1, &ins, 1);
7486 btrfs_end_transaction(trans, root);
7490 ret = insert_reserved_file_extent(trans, inode,
7491 cur_offset, ins.objectid,
7492 ins.offset, ins.offset,
7493 ins.offset, 0, 0, 0,
7494 BTRFS_FILE_EXTENT_PREALLOC);
7496 btrfs_abort_transaction(trans, root, ret);
7498 btrfs_end_transaction(trans, root);
7501 btrfs_drop_extent_cache(inode, cur_offset,
7502 cur_offset + ins.offset -1, 0);
7504 num_bytes -= ins.offset;
7505 cur_offset += ins.offset;
7506 *alloc_hint = ins.objectid + ins.offset;
7508 inode->i_ctime = CURRENT_TIME;
7509 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
7510 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
7511 (actual_len > inode->i_size) &&
7512 (cur_offset > inode->i_size)) {
7513 if (cur_offset > actual_len)
7514 i_size = actual_len;
7516 i_size = cur_offset;
7517 i_size_write(inode, i_size);
7518 btrfs_ordered_update_i_size(inode, i_size, NULL);
7521 ret = btrfs_update_inode(trans, root, inode);
7524 btrfs_abort_transaction(trans, root, ret);
7526 btrfs_end_transaction(trans, root);
7531 btrfs_end_transaction(trans, root);
7536 int btrfs_prealloc_file_range(struct inode *inode, int mode,
7537 u64 start, u64 num_bytes, u64 min_size,
7538 loff_t actual_len, u64 *alloc_hint)
7540 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7541 min_size, actual_len, alloc_hint,
7545 int btrfs_prealloc_file_range_trans(struct inode *inode,
7546 struct btrfs_trans_handle *trans, int mode,
7547 u64 start, u64 num_bytes, u64 min_size,
7548 loff_t actual_len, u64 *alloc_hint)
7550 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7551 min_size, actual_len, alloc_hint, trans);
7554 static int btrfs_set_page_dirty(struct page *page)
7556 return __set_page_dirty_nobuffers(page);
7559 static int btrfs_permission(struct inode *inode, int mask)
7561 struct btrfs_root *root = BTRFS_I(inode)->root;
7562 umode_t mode = inode->i_mode;
7564 if (mask & MAY_WRITE &&
7565 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7566 if (btrfs_root_readonly(root))
7568 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7571 return generic_permission(inode, mask);
7574 static const struct inode_operations btrfs_dir_inode_operations = {
7575 .getattr = btrfs_getattr,
7576 .lookup = btrfs_lookup,
7577 .create = btrfs_create,
7578 .unlink = btrfs_unlink,
7580 .mkdir = btrfs_mkdir,
7581 .rmdir = btrfs_rmdir,
7582 .rename = btrfs_rename,
7583 .symlink = btrfs_symlink,
7584 .setattr = btrfs_setattr,
7585 .mknod = btrfs_mknod,
7586 .setxattr = btrfs_setxattr,
7587 .getxattr = btrfs_getxattr,
7588 .listxattr = btrfs_listxattr,
7589 .removexattr = btrfs_removexattr,
7590 .permission = btrfs_permission,
7591 .get_acl = btrfs_get_acl,
7593 static const struct inode_operations btrfs_dir_ro_inode_operations = {
7594 .lookup = btrfs_lookup,
7595 .permission = btrfs_permission,
7596 .get_acl = btrfs_get_acl,
7599 static const struct file_operations btrfs_dir_file_operations = {
7600 .llseek = generic_file_llseek,
7601 .read = generic_read_dir,
7602 .readdir = btrfs_real_readdir,
7603 .unlocked_ioctl = btrfs_ioctl,
7604 #ifdef CONFIG_COMPAT
7605 .compat_ioctl = btrfs_ioctl,
7607 .release = btrfs_release_file,
7608 .fsync = btrfs_sync_file,
7611 static struct extent_io_ops btrfs_extent_io_ops = {
7612 .fill_delalloc = run_delalloc_range,
7613 .submit_bio_hook = btrfs_submit_bio_hook,
7614 .merge_bio_hook = btrfs_merge_bio_hook,
7615 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7616 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7617 .writepage_start_hook = btrfs_writepage_start_hook,
7618 .set_bit_hook = btrfs_set_bit_hook,
7619 .clear_bit_hook = btrfs_clear_bit_hook,
7620 .merge_extent_hook = btrfs_merge_extent_hook,
7621 .split_extent_hook = btrfs_split_extent_hook,
7625 * btrfs doesn't support the bmap operation because swapfiles
7626 * use bmap to make a mapping of extents in the file. They assume
7627 * these extents won't change over the life of the file and they
7628 * use the bmap result to do IO directly to the drive.
7630 * the btrfs bmap call would return logical addresses that aren't
7631 * suitable for IO and they also will change frequently as COW
7632 * operations happen. So, swapfile + btrfs == corruption.
7634 * For now we're avoiding this by dropping bmap.
7636 static const struct address_space_operations btrfs_aops = {
7637 .readpage = btrfs_readpage,
7638 .writepage = btrfs_writepage,
7639 .writepages = btrfs_writepages,
7640 .readpages = btrfs_readpages,
7641 .direct_IO = btrfs_direct_IO,
7642 .invalidatepage = btrfs_invalidatepage,
7643 .releasepage = btrfs_releasepage,
7644 .set_page_dirty = btrfs_set_page_dirty,
7645 .error_remove_page = generic_error_remove_page,
7648 static const struct address_space_operations btrfs_symlink_aops = {
7649 .readpage = btrfs_readpage,
7650 .writepage = btrfs_writepage,
7651 .invalidatepage = btrfs_invalidatepage,
7652 .releasepage = btrfs_releasepage,
7655 static const struct inode_operations btrfs_file_inode_operations = {
7656 .getattr = btrfs_getattr,
7657 .setattr = btrfs_setattr,
7658 .setxattr = btrfs_setxattr,
7659 .getxattr = btrfs_getxattr,
7660 .listxattr = btrfs_listxattr,
7661 .removexattr = btrfs_removexattr,
7662 .permission = btrfs_permission,
7663 .fiemap = btrfs_fiemap,
7664 .get_acl = btrfs_get_acl,
7666 static const struct inode_operations btrfs_special_inode_operations = {
7667 .getattr = btrfs_getattr,
7668 .setattr = btrfs_setattr,
7669 .permission = btrfs_permission,
7670 .setxattr = btrfs_setxattr,
7671 .getxattr = btrfs_getxattr,
7672 .listxattr = btrfs_listxattr,
7673 .removexattr = btrfs_removexattr,
7674 .get_acl = btrfs_get_acl,
7676 static const struct inode_operations btrfs_symlink_inode_operations = {
7677 .readlink = generic_readlink,
7678 .follow_link = page_follow_link_light,
7679 .put_link = page_put_link,
7680 .getattr = btrfs_getattr,
7681 .setattr = btrfs_setattr,
7682 .permission = btrfs_permission,
7683 .setxattr = btrfs_setxattr,
7684 .getxattr = btrfs_getxattr,
7685 .listxattr = btrfs_listxattr,
7686 .removexattr = btrfs_removexattr,
7687 .get_acl = btrfs_get_acl,
7690 const struct dentry_operations btrfs_dentry_operations = {
7691 .d_delete = btrfs_dentry_delete,
7692 .d_release = btrfs_dentry_release,